1 /******************************************************************************
2 *
3 * Copyright (C) 2010-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This is the implementation of the API for GATT module of BTA.
22 *
23 ******************************************************************************/
24
25 #include "common/bt_target.h"
26 #include "osi/allocator.h"
27
28 #if defined(GATTC_INCLUDED) && (GATTC_INCLUDED == TRUE)
29
30 #include <string.h>
31 #include "bta/bta_sys.h"
32 #include "bta/bta_gatt_api.h"
33 #include "bta_gattc_int.h"
34 #include "stack/l2c_api.h"
35 /*****************************************************************************
36 ** Constants
37 *****************************************************************************/
38
39 static const tBTA_SYS_REG bta_gattc_reg = {
40 bta_gattc_hdl_event,
41 BTA_GATTC_Disable
42 };
43
44
45 /*******************************************************************************
46 **
47 ** Function BTA_GATTC_Disable
48 **
49 ** Description This function is called to disable GATTC module
50 **
51 ** Parameters None.
52 **
53 ** Returns None
54 **
55 *******************************************************************************/
BTA_GATTC_Disable(void)56 void BTA_GATTC_Disable(void)
57 {
58 BT_HDR *p_buf;
59
60 if (bta_sys_is_register(BTA_ID_GATTC) == FALSE) {
61 APPL_TRACE_WARNING("GATTC Module not enabled/already disabled\n");
62 return;
63 }
64 if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
65 p_buf->event = BTA_GATTC_API_DISABLE_EVT;
66 bta_sys_sendmsg(p_buf);
67 }
68 bta_sys_deregister(BTA_ID_GATTC);
69
70 }
71
72 /*******************************************************************************
73 **
74 ** Function BTA_GATTC_AppRegister
75 **
76 ** Description This function is called to register application callbacks
77 ** with BTA GATTC module.
78 **
79 ** Parameters p_app_uuid - application UUID
80 ** p_client_cb - pointer to the application callback function.
81 **
82 ** Returns None
83 **
84 *******************************************************************************/
BTA_GATTC_AppRegister(tBT_UUID * p_app_uuid,tBTA_GATTC_CBACK * p_client_cb)85 void BTA_GATTC_AppRegister(tBT_UUID *p_app_uuid, tBTA_GATTC_CBACK *p_client_cb)
86 {
87 tBTA_GATTC_API_REG *p_buf;
88
89 if (bta_sys_is_register(BTA_ID_GATTC) == FALSE) {
90 bta_sys_register(BTA_ID_GATTC, &bta_gattc_reg);
91 }
92
93 if ((p_buf = (tBTA_GATTC_API_REG *) osi_malloc(sizeof(tBTA_GATTC_API_REG))) != NULL) {
94 p_buf->hdr.event = BTA_GATTC_API_REG_EVT;
95 if (p_app_uuid != NULL) {
96 memcpy(&p_buf->app_uuid, p_app_uuid, sizeof(tBT_UUID));
97 }
98 p_buf->p_cback = p_client_cb;
99
100 bta_sys_sendmsg(p_buf);
101 }
102 return;
103 }
104
105 /*******************************************************************************
106 **
107 ** Function BTA_GATTC_AppDeregister
108 **
109 ** Description This function is called to deregister an application
110 ** from BTA GATTC module.
111 **
112 ** Parameters client_if - client interface identifier.
113 **
114 ** Returns None
115 **
116 *******************************************************************************/
BTA_GATTC_AppDeregister(tBTA_GATTC_IF client_if)117 void BTA_GATTC_AppDeregister(tBTA_GATTC_IF client_if)
118 {
119 tBTA_GATTC_API_DEREG *p_buf;
120
121 if ((p_buf = (tBTA_GATTC_API_DEREG *) osi_malloc(sizeof(tBTA_GATTC_API_DEREG))) != NULL) {
122 p_buf->hdr.event = BTA_GATTC_API_DEREG_EVT;
123 p_buf->client_if = client_if;
124 bta_sys_sendmsg(p_buf);
125 }
126 return;
127 }
128
129 /*******************************************************************************
130 **
131 ** Function BTA_GATTC_Enh_Open
132 **
133 ** Description Open a direct connection or add a background auto connection
134 ** bd address
135 **
136 ** Parameters client_if: server interface.
137 ** remote_bda: remote device BD address.
138 ** remote_addr_type: remote device BD address type.
139 ** is_direct: direct connection or background auto connection
140 ** transport: Transport to be used for GATT connection (BREDR/LE)
141 **
142 ** Returns void
143 **
144 *******************************************************************************/
BTA_GATTC_Enh_Open(tBTA_GATTC_IF client_if,BD_ADDR remote_bda,tBTA_ADDR_TYPE remote_addr_type,BOOLEAN is_direct,tBTA_GATT_TRANSPORT transport,BOOLEAN is_aux,tBTA_ADDR_TYPE own_addr_type,UINT8 phy_mask,tBTA_BLE_CONN_PARAMS * phy_1m_conn_params,tBTA_BLE_CONN_PARAMS * phy_2m_conn_params,tBTA_BLE_CONN_PARAMS * phy_coded_conn_params)145 void BTA_GATTC_Enh_Open(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, tBTA_ADDR_TYPE remote_addr_type,
146 BOOLEAN is_direct, tBTA_GATT_TRANSPORT transport, BOOLEAN is_aux, tBTA_ADDR_TYPE own_addr_type,
147 UINT8 phy_mask, tBTA_BLE_CONN_PARAMS *phy_1m_conn_params, tBTA_BLE_CONN_PARAMS *phy_2m_conn_params,
148 tBTA_BLE_CONN_PARAMS *phy_coded_conn_params)
149 {
150 tBTA_GATTC_API_OPEN *p_buf;
151
152 if ((p_buf = (tBTA_GATTC_API_OPEN *) osi_malloc(sizeof(tBTA_GATTC_API_OPEN))) != NULL) {
153 p_buf->hdr.event = BTA_GATTC_API_OPEN_EVT;
154
155 p_buf->client_if = client_if;
156 p_buf->is_direct = is_direct;
157 p_buf->transport = transport;
158 p_buf->is_aux = is_aux;
159 p_buf->remote_addr_type = remote_addr_type;
160 p_buf->own_addr_type = own_addr_type;
161 p_buf->phy_mask = phy_mask;
162 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
163 if ((phy_mask & BTA_BLE_PHY_1M_MASK) && phy_1m_conn_params) {
164 memcpy(&p_buf->phy_1m_conn_params, phy_1m_conn_params, sizeof(tBTA_BLE_CONN_PARAMS));
165 }
166 if ((phy_mask & BTA_BLE_PHY_2M_MASK) && phy_2m_conn_params) {
167 memcpy(&p_buf->phy_2m_conn_params, phy_2m_conn_params, sizeof(tBTA_BLE_CONN_PARAMS));
168 }
169 if ((phy_mask & BTA_BLE_PHY_CODED_MASK) && phy_coded_conn_params) {
170 memcpy(&p_buf->phy_coded_conn_params, phy_coded_conn_params, sizeof(tBTA_BLE_CONN_PARAMS));
171 }
172
173 bta_sys_sendmsg(p_buf);
174 }
175 return;
176 }
177
178 /*******************************************************************************
179 **
180 ** Function BTA_GATTC_CancelOpen
181 **
182 ** Description Cancel a direct open connection or remove a background auto connection
183 ** bd address
184 **
185 ** Parameters client_if: server interface.
186 ** remote_bda: remote device BD address.
187 ** is_direct: direct connection or background auto connection
188 **
189 ** Returns void
190 **
191 *******************************************************************************/
BTA_GATTC_CancelOpen(tBTA_GATTC_IF client_if,BD_ADDR remote_bda,BOOLEAN is_direct)192 void BTA_GATTC_CancelOpen(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, BOOLEAN is_direct)
193 {
194 //If the registration callback is NULL, return
195 if(bta_sys_is_register(BTA_ID_GATTC) == FALSE) {
196 return;
197 }
198
199 tBTA_GATTC_API_CANCEL_OPEN *p_buf;
200
201 if ((p_buf = (tBTA_GATTC_API_CANCEL_OPEN *) osi_malloc(sizeof(tBTA_GATTC_API_CANCEL_OPEN))) != NULL) {
202 p_buf->hdr.event = BTA_GATTC_API_CANCEL_OPEN_EVT;
203
204 p_buf->client_if = client_if;
205 p_buf->is_direct = is_direct;
206 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
207
208 bta_sys_sendmsg(p_buf);
209 }
210 return;
211 }
212
213 /*******************************************************************************
214 **
215 ** Function BTA_GATTC_Close
216 **
217 ** Description Close a connection to a GATT server.
218 **
219 ** Parameters conn_id: connection ID to be closed.
220 **
221 ** Returns void
222 **
223 *******************************************************************************/
BTA_GATTC_Close(UINT16 conn_id)224 void BTA_GATTC_Close(UINT16 conn_id)
225 {
226 BT_HDR *p_buf;
227
228 if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
229 p_buf->event = BTA_GATTC_API_CLOSE_EVT;
230
231 p_buf->layer_specific = conn_id;
232
233 bta_sys_sendmsg(p_buf);
234 }
235 return;
236
237 }
238 /*******************************************************************************
239 **
240 ** Function BTA_GATTC_ConfigureMTU
241 **
242 ** Description Configure the MTU size in the GATT channel. This can be done
243 ** only once per connection.
244 **
245 ** Parameters conn_id: connection ID.
246 ** mtu: desired MTU size to use.
247 **
248 ** Returns void
249 **
250 *******************************************************************************/
BTA_GATTC_ConfigureMTU(UINT16 conn_id)251 void BTA_GATTC_ConfigureMTU (UINT16 conn_id)
252 {
253 tBTA_GATTC_API_CFG_MTU *p_buf;
254
255 if ((p_buf = (tBTA_GATTC_API_CFG_MTU *) osi_malloc(sizeof(tBTA_GATTC_API_CFG_MTU))) != NULL) {
256 p_buf->hdr.event = BTA_GATTC_API_CFG_MTU_EVT;
257 p_buf->hdr.layer_specific = conn_id;
258
259 bta_sys_sendmsg(p_buf);
260 }
261 return;
262 }
263 /*******************************************************************************
264 **
265 ** Function BTA_GATTC_ServiceSearchRequest
266 **
267 ** Description This function is called to request a GATT service discovery
268 ** on a GATT server. This function report service search result
269 ** by a callback event, and followed by a service search complete
270 ** event.
271 **
272 ** Parameters conn_id: connection ID.
273 ** p_srvc_uuid: a UUID of the service application is interested in.
274 ** If Null, discover for all services.
275 **
276 ** Returns None
277 **
278 *******************************************************************************/
BTA_GATTC_ServiceSearchRequest(UINT16 conn_id,tBT_UUID * p_srvc_uuid)279 void BTA_GATTC_ServiceSearchRequest (UINT16 conn_id, tBT_UUID *p_srvc_uuid)
280 {
281 tBTA_GATTC_API_SEARCH *p_buf;
282 UINT16 len = sizeof(tBTA_GATTC_API_SEARCH) + sizeof(tBT_UUID);
283
284 if ((p_buf = (tBTA_GATTC_API_SEARCH *) osi_malloc(len)) != NULL) {
285 memset(p_buf, 0, len);
286
287 p_buf->hdr.event = BTA_GATTC_API_SEARCH_EVT;
288 p_buf->hdr.layer_specific = conn_id;
289
290 if (p_srvc_uuid) {
291 p_buf->p_srvc_uuid = (tBT_UUID *)(p_buf + 1);
292 memcpy(p_buf->p_srvc_uuid, p_srvc_uuid, sizeof(tBT_UUID));
293 } else {
294 p_buf->p_srvc_uuid = NULL;
295 }
296
297 bta_sys_sendmsg(p_buf);
298 }
299 return;
300 }
301
302
303 /*******************************************************************************
304 **
305 ** Function BTA_GATTC_GetServices
306 **
307 ** Description This function is called to find the services on the given server.
308 **
309 ** Parameters conn_id: connection ID which identify the server.
310 **
311 ** Returns returns list_t of tBTA_GATTC_SERVICE or NULL.
312 **
313 *******************************************************************************/
BTA_GATTC_GetServices(UINT16 conn_id)314 const list_t* BTA_GATTC_GetServices(UINT16 conn_id)
315 {
316 return bta_gattc_get_services(conn_id);
317 }
318
319 /*******************************************************************************
320 **
321 ** Function BTA_GATTC_GetCharacteristic
322 **
323 ** Description This function is called to find the characteristic on the given server.
324 **
325 ** Parameters conn_id - connection ID which identify the server.
326 ** handle - characteristic handle
327 **
328 ** Returns returns pointer to tBTA_GATTC_CHARACTERISTIC or NULL.
329 **
330 *******************************************************************************/
BTA_GATTC_GetCharacteristic(UINT16 conn_id,UINT16 handle)331 const tBTA_GATTC_CHARACTERISTIC* BTA_GATTC_GetCharacteristic(UINT16 conn_id, UINT16 handle)
332 {
333 return bta_gattc_get_characteristic(conn_id, handle);
334 }
335
336 /*******************************************************************************
337 **
338 ** Function BTA_GATTC_GetDescriptor
339 **
340 ** Description This function is called to find the characteristic on the given server.
341 **
342 ** Parameters conn_id - connection ID which identify the server.
343 ** handle - descriptor handle
344 **
345 ** Returns returns pointer to tBTA_GATTC_DESCRIPTOR or NULL.
346 **
347 *******************************************************************************/
BTA_GATTC_GetDescriptor(UINT16 conn_id,UINT16 handle)348 const tBTA_GATTC_DESCRIPTOR* BTA_GATTC_GetDescriptor(UINT16 conn_id, UINT16 handle)
349 {
350 return bta_gattc_get_descriptor(conn_id, handle);
351 }
352
BTA_GATTC_GetServiceWithUUID(UINT16 conn_id,tBT_UUID * svc_uuid,btgatt_db_element_t ** db,UINT16 * count)353 void BTA_GATTC_GetServiceWithUUID(UINT16 conn_id, tBT_UUID *svc_uuid,
354 btgatt_db_element_t **db, UINT16 *count)
355 {
356 bta_gattc_get_service_with_uuid(conn_id, svc_uuid, db, count);
357 }
358
BTA_GATTC_GetAllChar(UINT16 conn_id,UINT16 start_handle,UINT16 end_handle,btgatt_db_element_t ** db,UINT16 * count)359 void BTA_GATTC_GetAllChar(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle,
360 btgatt_db_element_t **db, UINT16 *count)
361 {
362 bta_gattc_get_db_with_opration(conn_id,
363 GATT_OP_GET_ALL_CHAR,
364 0,
365 NULL,
366 NULL,
367 NULL,
368 start_handle,
369 end_handle,
370 db,
371 count);
372 }
373
BTA_GATTC_GetAllDescriptor(UINT16 conn_id,UINT16 char_handle,btgatt_db_element_t ** db,UINT16 * count)374 void BTA_GATTC_GetAllDescriptor(UINT16 conn_id, UINT16 char_handle,
375 btgatt_db_element_t **db, UINT16 *count)
376 {
377 bta_gattc_get_db_with_opration(conn_id,
378 GATT_OP_GET_ALL_DESCRI,
379 char_handle,
380 NULL,
381 NULL,
382 NULL,
383 0,
384 0xFFFF,
385 db,
386 count);
387 }
388
BTA_GATTC_GetCharByUUID(UINT16 conn_id,UINT16 start_handle,UINT16 end_handle,tBT_UUID char_uuid,btgatt_db_element_t ** db,UINT16 * count)389 void BTA_GATTC_GetCharByUUID(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, tBT_UUID char_uuid,
390 btgatt_db_element_t **db, UINT16 *count)
391 {
392 bta_gattc_get_db_with_opration(conn_id,
393 GATT_OP_GET_CHAR_BY_UUID,
394 0,
395 NULL,
396 &char_uuid,
397 NULL,
398 start_handle,
399 end_handle,
400 db,
401 count);
402 }
403
BTA_GATTC_GetDescrByUUID(UINT16 conn_id,uint16_t start_handle,uint16_t end_handle,tBT_UUID char_uuid,tBT_UUID descr_uuid,btgatt_db_element_t ** db,UINT16 * count)404 void BTA_GATTC_GetDescrByUUID(UINT16 conn_id, uint16_t start_handle, uint16_t end_handle,
405 tBT_UUID char_uuid, tBT_UUID descr_uuid,
406 btgatt_db_element_t **db, UINT16 *count)
407 {
408 bta_gattc_get_db_with_opration(conn_id,
409 GATT_OP_GET_DESCRI_BY_UUID,
410 0,
411 NULL,
412 &char_uuid,
413 &descr_uuid,
414 start_handle,
415 end_handle,
416 db,
417 count);
418 }
419
BTA_GATTC_GetDescrByCharHandle(UINT16 conn_id,UINT16 char_handle,tBT_UUID descr_uuid,btgatt_db_element_t ** db,UINT16 * count)420 void BTA_GATTC_GetDescrByCharHandle(UINT16 conn_id, UINT16 char_handle, tBT_UUID descr_uuid,
421 btgatt_db_element_t **db, UINT16 *count)
422 {
423 bta_gattc_get_db_with_opration(conn_id,
424 GATT_OP_GET_DESCRI_BY_HANDLE,
425 char_handle,
426 NULL,
427 NULL,
428 &descr_uuid,
429 0,
430 0xFFFF,
431 db,
432 count);
433 }
434
BTA_GATTC_GetIncludeService(UINT16 conn_id,UINT16 start_handle,UINT16 end_handle,tBT_UUID * incl_uuid,btgatt_db_element_t ** db,UINT16 * count)435 void BTA_GATTC_GetIncludeService(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle,
436 tBT_UUID *incl_uuid, btgatt_db_element_t **db, UINT16 *count)
437 {
438 bta_gattc_get_db_with_opration(conn_id,
439 GATT_OP_GET_INCLUDE_SVC,
440 0,
441 incl_uuid,
442 NULL,
443 NULL,
444 start_handle,
445 end_handle,
446 db,
447 count);
448 }
449
BTA_GATTC_GetDBSize(UINT16 conn_id,UINT16 start_handle,UINT16 end_handle,UINT16 * count)450 void BTA_GATTC_GetDBSize(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, UINT16 *count)
451 {
452 bta_gattc_get_db_size_handle(conn_id, start_handle, end_handle, count);
453 }
454
BTA_GATTC_GetDBSizeByType(UINT16 conn_id,bt_gatt_db_attribute_type_t type,UINT16 start_handle,UINT16 end_handle,UINT16 char_handle,UINT16 * count)455 void BTA_GATTC_GetDBSizeByType(UINT16 conn_id, bt_gatt_db_attribute_type_t type,
456 UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, UINT16 *count)
457 {
458 bta_gattc_get_db_size_with_type_handle(conn_id, type, start_handle, end_handle, char_handle, count);
459 }
460
461
462 /*******************************************************************************
463 **
464 ** Function BTA_GATTC_GetGattDb
465 **
466 ** Description This function is called to get the GATT database.
467 **
468 ** Parameters conn_id: connection ID which identify the server.
469 ** db: output parameter which will contain the GATT database copy.
470 ** Caller is responsible for freeing it.
471 ** count: number of elements in database.
472 **
473 *******************************************************************************/
BTA_GATTC_GetGattDb(UINT16 conn_id,UINT16 start_handle,UINT16 end_handle,btgatt_db_element_t ** db,UINT16 * count)474 void BTA_GATTC_GetGattDb(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle,
475 btgatt_db_element_t **db, UINT16 *count)
476 {
477 bta_gattc_get_gatt_db(conn_id, start_handle, end_handle, db, count);
478 }
479
480 /*******************************************************************************
481 **
482 ** Function BTA_GATTC_ReadCharacteristic
483 **
484 ** Description This function is called to read a characteristics value
485 **
486 ** Parameters conn_id - connection ID.
487 ** handle - characteristic handle to read.
488 **
489 ** Returns None
490 **
491 *******************************************************************************/
BTA_GATTC_ReadCharacteristic(UINT16 conn_id,UINT16 handle,tBTA_GATT_AUTH_REQ auth_req)492 void BTA_GATTC_ReadCharacteristic(UINT16 conn_id, UINT16 handle, tBTA_GATT_AUTH_REQ auth_req)
493 {
494 tBTA_GATTC_API_READ *p_buf;
495
496 if ((p_buf = (tBTA_GATTC_API_READ *) osi_malloc(sizeof(tBTA_GATTC_API_READ))) != NULL) {
497 memset(p_buf, 0, sizeof(tBTA_GATTC_API_READ));
498
499 p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
500 p_buf->hdr.layer_specific = conn_id;
501 p_buf->auth_req = auth_req;
502 p_buf->handle = handle;
503 p_buf->cmpl_evt = BTA_GATTC_READ_CHAR_EVT;
504
505 bta_sys_sendmsg(p_buf);
506 }
507 return;
508 }
509
510 /*******************************************************************************
511 **
512 ** Function BTA_GATTC_ReadCharDescr
513 **
514 ** Description This function is called to read a descriptor value.
515 **
516 ** Parameters conn_id - connection ID.
517 ** handle - descriptor handle to read.
518 **
519 ** Returns None
520 **
521 *******************************************************************************/
BTA_GATTC_ReadCharDescr(UINT16 conn_id,UINT16 handle,tBTA_GATT_AUTH_REQ auth_req)522 void BTA_GATTC_ReadCharDescr (UINT16 conn_id, UINT16 handle, tBTA_GATT_AUTH_REQ auth_req)
523 {
524 tBTA_GATTC_API_READ *p_buf;
525 UINT16 len = (UINT16)(sizeof(tBTA_GATT_ID) + sizeof(tBTA_GATTC_API_READ));
526
527 if ((p_buf = (tBTA_GATTC_API_READ *) osi_malloc(len)) != NULL) {
528 memset(p_buf, 0, sizeof(tBTA_GATTC_API_READ));
529
530 p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
531 p_buf->hdr.layer_specific = conn_id;
532 p_buf->auth_req = auth_req;
533 p_buf->handle = handle;
534 p_buf->cmpl_evt = BTA_GATTC_READ_DESCR_EVT;
535
536 bta_sys_sendmsg(p_buf);
537 }
538 return;
539
540 }
541 /*******************************************************************************
542 **
543 ** Function BTA_GATTC_ReadMultiple
544 **
545 ** Description This function is called to read multiple characteristic or
546 ** characteristic descriptors.
547 **
548 ** Parameters conn_id - connection ID.
549 ** p_read_multi - pointer to the read multiple parameter.
550 **
551 ** Returns None
552 **
553 *******************************************************************************/
BTA_GATTC_ReadMultiple(UINT16 conn_id,tBTA_GATTC_MULTI * p_read_multi,tBTA_GATT_AUTH_REQ auth_req)554 void BTA_GATTC_ReadMultiple(UINT16 conn_id, tBTA_GATTC_MULTI *p_read_multi,
555 tBTA_GATT_AUTH_REQ auth_req)
556 {
557 tBTA_GATTC_API_READ_MULTI *p_buf;
558 //tBTA_GATTC_API_READ_MULTI *p_value;
559 UINT16 len = (UINT16)(sizeof(tBTA_GATTC_API_READ_MULTI));
560
561 if ((p_buf = (tBTA_GATTC_API_READ_MULTI *) osi_malloc(len)) != NULL) {
562 memset(p_buf, 0, len);
563
564 p_buf->hdr.event = BTA_GATTC_API_READ_MULTI_EVT;
565 p_buf->hdr.layer_specific = conn_id;
566 p_buf->auth_req = auth_req;
567 p_buf->num_attr = p_read_multi->num_attr;
568 p_buf->cmpl_evt = BTA_GATTC_READ_MULTIPLE_EVT;
569 if (p_buf->num_attr > 0) {
570 memcpy(p_buf->handles, p_read_multi->handles, sizeof(UINT16) * p_read_multi->num_attr);
571 }
572
573 bta_sys_sendmsg(p_buf);
574 }
575 return;
576 }
577
578 /*******************************************************************************
579 **
580 ** Function BTA_GATTC_ReadMultipleVariable
581 **
582 ** Description This function is called to read multiple variable length characteristic or
583 ** characteristic descriptors.
584 **
585 ** Parameters conn_id - connection ID.
586 ** p_read_multi - pointer to the read multiple parameter.
587 **
588 ** Returns None
589 **
590 *******************************************************************************/
BTA_GATTC_ReadMultipleVariable(UINT16 conn_id,tBTA_GATTC_MULTI * p_read_multi,tBTA_GATT_AUTH_REQ auth_req)591 void BTA_GATTC_ReadMultipleVariable(UINT16 conn_id, tBTA_GATTC_MULTI *p_read_multi,
592 tBTA_GATT_AUTH_REQ auth_req)
593 {
594 tBTA_GATTC_API_READ_MULTI *p_buf;
595 UINT16 len = (UINT16)(sizeof(tBTA_GATTC_API_READ_MULTI));
596
597 if ((p_buf = (tBTA_GATTC_API_READ_MULTI *) osi_malloc(len)) != NULL) {
598 memset(p_buf, 0, len);
599
600 p_buf->hdr.event = BTA_GATTC_API_READ_MULTI_VAR_EVT;
601 p_buf->hdr.layer_specific = conn_id;
602 p_buf->auth_req = auth_req;
603 p_buf->num_attr = p_read_multi->num_attr;
604 p_buf->cmpl_evt = BTA_GATTC_READ_MULTI_VAR_EVT;
605 if (p_buf->num_attr > 0) {
606 memcpy(p_buf->handles, p_read_multi->handles, sizeof(UINT16) * p_read_multi->num_attr);
607 }
608
609 bta_sys_sendmsg(p_buf);
610 }
611 return;
612 }
613
614 /*******************************************************************************
615 **
616 ** Function BTA_GATTC_Read_by_type
617 **
618 ** Description This function is called to read a attribute value by uuid
619 **
620 ** Parameters conn_id - connection ID.
621 ** s_handle - start handle.
622 ** e_handle - end handle
623 ** uuid - The attribute UUID.
624 **
625 ** Returns None
626 **
627 *******************************************************************************/
BTA_GATTC_Read_by_type(UINT16 conn_id,UINT16 s_handle,UINT16 e_handle,tBT_UUID * uuid,tBTA_GATT_AUTH_REQ auth_req)628 void BTA_GATTC_Read_by_type(UINT16 conn_id, UINT16 s_handle,UINT16 e_handle, tBT_UUID *uuid, tBTA_GATT_AUTH_REQ auth_req)
629 {
630 tBTA_GATTC_API_READ *p_buf;
631
632 if ((p_buf = (tBTA_GATTC_API_READ *) osi_malloc(sizeof(tBTA_GATTC_API_READ))) != NULL) {
633 memset(p_buf, 0, sizeof(tBTA_GATTC_API_READ));
634
635 p_buf->hdr.event = BTA_GATTC_API_READ_BY_TYPE_EVT;
636 p_buf->hdr.layer_specific = conn_id;
637 p_buf->auth_req = auth_req;
638 p_buf->s_handle = s_handle;
639 p_buf->e_handle = e_handle;
640 memcpy(&(p_buf->uuid), uuid, sizeof(tBT_UUID));
641 p_buf->cmpl_evt = BTA_GATTC_READ_CHAR_EVT;
642
643 bta_sys_sendmsg(p_buf);
644 }
645 return;
646 }
647
648 /*******************************************************************************
649 **
650 ** Function BTA_GATTC_WriteCharValue
651 **
652 ** Description This function is called to write characteristic value.
653 **
654 ** Parameters conn_id - connection ID.
655 ** handle - characteristic handle to write.
656 ** write_type - type of write.
657 ** len: length of the data to be written.
658 ** p_value - the value to be written.
659 **
660 ** Returns None
661 **
662 *******************************************************************************/
BTA_GATTC_WriteCharValue(UINT16 conn_id,UINT16 handle,tBTA_GATTC_WRITE_TYPE write_type,UINT16 len,UINT8 * p_value,tBTA_GATT_AUTH_REQ auth_req)663 void BTA_GATTC_WriteCharValue ( UINT16 conn_id,
664 UINT16 handle,
665 tBTA_GATTC_WRITE_TYPE write_type,
666 UINT16 len,
667 UINT8 *p_value,
668 tBTA_GATT_AUTH_REQ auth_req)
669 {
670 tBTA_GATTC_API_WRITE *p_buf;
671
672 if ((p_buf = (tBTA_GATTC_API_WRITE *) osi_malloc((UINT16)(sizeof(tBTA_GATTC_API_WRITE) + len))) != NULL) {
673 memset(p_buf, 0, sizeof(tBTA_GATTC_API_WRITE) + len);
674
675 p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
676 p_buf->hdr.layer_specific = conn_id;
677 p_buf->auth_req = auth_req;
678 p_buf->handle = handle;
679 p_buf->cmpl_evt = BTA_GATTC_WRITE_CHAR_EVT;
680 p_buf->write_type = write_type;
681 p_buf->len = len;
682
683 if (p_value && len > 0) {
684 p_buf->p_value = (UINT8 *)(p_buf + 1);
685 memcpy(p_buf->p_value, p_value, len);
686 }
687 if(write_type == BTA_GATTC_TYPE_WRITE_NO_RSP){
688 l2ble_update_att_acl_pkt_num(L2CA_DECREASE_BTC_NUM, NULL);
689 l2ble_update_att_acl_pkt_num(L2CA_ADD_BTU_NUM, NULL);
690 }
691 bta_sys_sendmsg(p_buf);
692 }
693 return;
694 }
695 /*******************************************************************************
696 **
697 ** Function BTA_GATTC_WriteCharDescr
698 **
699 ** Description This function is called to write descriptor value.
700 **
701 ** Parameters conn_id - connection ID
702 ** handle - descriptor handle to write.
703 ** write_type - write type.
704 ** p_value - the value to be written.
705 **
706 ** Returns None
707 **
708 *******************************************************************************/
BTA_GATTC_WriteCharDescr(UINT16 conn_id,UINT16 handle,tBTA_GATTC_WRITE_TYPE write_type,tBTA_GATT_UNFMT * p_data,tBTA_GATT_AUTH_REQ auth_req)709 void BTA_GATTC_WriteCharDescr (UINT16 conn_id,
710 UINT16 handle,
711 tBTA_GATTC_WRITE_TYPE write_type,
712 tBTA_GATT_UNFMT *p_data,
713 tBTA_GATT_AUTH_REQ auth_req)
714 {
715 size_t len = sizeof(tBTA_GATTC_API_WRITE);
716 tBTA_GATTC_API_WRITE *p_buf;
717 if (p_data != NULL) {
718 len += p_data->len;
719 }
720
721 if ((p_buf = (tBTA_GATTC_API_WRITE *) osi_malloc(len)) != NULL) {
722 memset(p_buf, 0, len);
723
724 p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
725 p_buf->hdr.layer_specific = conn_id;
726 p_buf->auth_req = auth_req;
727 p_buf->handle = handle;
728 p_buf->cmpl_evt = BTA_GATTC_WRITE_DESCR_EVT;
729 p_buf->write_type = write_type;
730
731 if (p_data && p_data->len != 0) {
732 p_buf->p_value = (UINT8 *)(p_buf + 1);
733 p_buf->len = p_data->len;
734 /* pack the descr data */
735 memcpy(p_buf->p_value, p_data->p_value, p_data->len);
736 }
737 if(write_type == BTA_GATTC_TYPE_WRITE_NO_RSP){
738 l2ble_update_att_acl_pkt_num(L2CA_DECREASE_BTC_NUM, NULL);
739 l2ble_update_att_acl_pkt_num(L2CA_ADD_BTU_NUM, NULL);
740 }
741 bta_sys_sendmsg(p_buf);
742 }
743 return;
744
745 }
746 /*******************************************************************************
747 **
748 ** Function BTA_GATTC_PrepareWrite
749 **
750 ** Description This function is called to prepare write a characteristic value.
751 **
752 ** Parameters conn_id - connection ID.
753 ** p_char_id - GATT characteristic ID of the service.
754 ** offset - offset of the write value.
755 ** len: length of the data to be written.
756 ** p_value - the value to be written.
757 **
758 ** Returns None
759 **
760 *******************************************************************************/
BTA_GATTC_PrepareWrite(UINT16 conn_id,UINT16 handle,UINT16 offset,UINT16 len,UINT8 * p_value,tBTA_GATT_AUTH_REQ auth_req)761 void BTA_GATTC_PrepareWrite (UINT16 conn_id, UINT16 handle,
762 UINT16 offset, UINT16 len, UINT8 *p_value,
763 tBTA_GATT_AUTH_REQ auth_req)
764 {
765 tBTA_GATTC_API_WRITE *p_buf;
766
767 if ((p_buf = (tBTA_GATTC_API_WRITE *) osi_malloc((UINT16)(sizeof(tBTA_GATTC_API_WRITE) + len))) != NULL) {
768 memset(p_buf, 0, sizeof(tBTA_GATTC_API_WRITE) + len);
769
770 p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
771 p_buf->hdr.layer_specific = conn_id;
772 p_buf->auth_req = auth_req;
773 p_buf->handle = handle;
774
775 p_buf->write_type = BTA_GATTC_WRITE_PREPARE;
776 p_buf->offset = offset;
777 p_buf->len = len;
778
779 if (p_value && len > 0) {
780 p_buf->p_value = (UINT8 *)(p_buf + 1);
781 memcpy(p_buf->p_value, p_value, len);
782 }
783
784 bta_sys_sendmsg(p_buf);
785 }
786 return;
787
788 }
789 /*******************************************************************************
790 **
791 ** Function BTA_GATTC_PrepareWriteCharDescr
792 **
793 ** Description This function is called to prepare write a characteristic descriptor value.
794 **
795 ** Parameters conn_id - connection ID.
796 ** p_char_descr_id - GATT characteristic descriptor ID of the service.
797 ** offset - offset of the write value.
798 ** len: length of the data to be written.
799 ** p_value - the value to be written.
800 **
801 ** Returns None
802 **
803 *******************************************************************************/
BTA_GATTC_PrepareWriteCharDescr(UINT16 conn_id,UINT16 handle,UINT16 offset,tBTA_GATT_UNFMT * p_data,tBTA_GATT_AUTH_REQ auth_req)804 void BTA_GATTC_PrepareWriteCharDescr (UINT16 conn_id, UINT16 handle,
805 UINT16 offset,tBTA_GATT_UNFMT *p_data,
806 tBTA_GATT_AUTH_REQ auth_req)
807 {
808 tBTA_GATTC_API_WRITE *p_buf;
809 UINT16 len = sizeof(tBTA_GATTC_API_WRITE);
810
811 if (p_data != NULL) {
812 len += p_data->len;
813 }
814
815 if ((p_buf = (tBTA_GATTC_API_WRITE *) osi_malloc(len)) != NULL) {
816 memset(p_buf, 0, len);
817
818 p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
819 p_buf->hdr.layer_specific = conn_id;
820 p_buf->auth_req = auth_req;
821 p_buf->handle = handle;
822 p_buf->write_type = BTA_GATTC_WRITE_PREPARE;
823 p_buf->offset = offset;
824
825 if (p_data && p_data->len != 0) {
826 p_buf->len = p_data->len;
827 p_buf->p_value = (UINT8 *)(p_buf + 1);
828 /* pack the descr data */
829 memcpy(p_buf->p_value, p_data->p_value, p_data->len);
830 }
831
832 bta_sys_sendmsg(p_buf);
833 }
834 return;
835
836 }
837 /*******************************************************************************
838 **
839 ** Function BTA_GATTC_ExecuteWrite
840 **
841 ** Description This function is called to execute write a prepare write sequence.
842 **
843 ** Parameters conn_id - connection ID.
844 ** is_execute - execute or cancel.
845 **
846 ** Returns None
847 **
848 *******************************************************************************/
BTA_GATTC_ExecuteWrite(UINT16 conn_id,BOOLEAN is_execute)849 void BTA_GATTC_ExecuteWrite (UINT16 conn_id, BOOLEAN is_execute)
850 {
851 tBTA_GATTC_API_EXEC *p_buf;
852
853 if ((p_buf = (tBTA_GATTC_API_EXEC *) osi_malloc((UINT16)sizeof(tBTA_GATTC_API_EXEC))) != NULL) {
854 memset(p_buf, 0, sizeof(tBTA_GATTC_API_EXEC));
855 p_buf->hdr.event = BTA_GATTC_API_EXEC_EVT;
856 p_buf->hdr.layer_specific = conn_id;
857
858 p_buf->is_execute = is_execute;
859
860 bta_sys_sendmsg(p_buf);
861 }
862 return;
863
864 }
865 /*******************************************************************************
866 **
867 ** Function BTA_GATTC_SendIndConfirm
868 **
869 ** Description This function is called to send handle value confirmation.
870 **
871 ** Parameters conn_id - connection ID.
872 ** p_char_id - characteristic ID to confirm.
873 **
874 ** Returns None
875 **
876 *******************************************************************************/
BTA_GATTC_SendIndConfirm(UINT16 conn_id,UINT16 handle)877 void BTA_GATTC_SendIndConfirm (UINT16 conn_id, UINT16 handle)
878 {
879 tBTA_GATTC_API_CONFIRM *p_buf;
880
881 APPL_TRACE_API("BTA_GATTC_SendIndConfirm conn_id=%d handle =0x%x",
882 conn_id, handle);
883
884 if ((p_buf = (tBTA_GATTC_API_CONFIRM *) osi_malloc(sizeof(tBTA_GATTC_API_CONFIRM))) != NULL) {
885 memset(p_buf, 0, sizeof(tBTA_GATTC_API_CONFIRM));
886 p_buf->hdr.event = BTA_GATTC_API_CONFIRM_EVT;
887 p_buf->hdr.layer_specific = conn_id;
888 p_buf->handle = handle;
889
890 bta_sys_sendmsg(p_buf);
891 }
892 return;
893
894 }
895
896 /*******************************************************************************
897 **
898 ** Function BTA_GATTC_RegisterForNotifications
899 **
900 ** Description This function is called to register for notification of a service.
901 **
902 ** Parameters client_if - client interface.
903 ** bda - target GATT server.
904 ** handle - GATT characteristic handle.
905 **
906 ** Returns OK if registration succeed, otherwise failed.
907 **
908 *******************************************************************************/
BTA_GATTC_RegisterForNotifications(tBTA_GATTC_IF client_if,BD_ADDR bda,UINT16 handle)909 tBTA_GATT_STATUS BTA_GATTC_RegisterForNotifications (tBTA_GATTC_IF client_if,
910 BD_ADDR bda, UINT16 handle)
911 {
912 tBTA_GATTC_RCB *p_clreg;
913 tBTA_GATT_STATUS status = BTA_GATT_ILLEGAL_PARAMETER;
914 UINT8 i;
915
916 if (!handle)
917 {
918 APPL_TRACE_ERROR("deregistration failed, handle is 0");
919 return status;
920 }
921
922 if ((p_clreg = bta_gattc_cl_get_regcb(client_if)) != NULL) {
923 for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
924 if ( p_clreg->notif_reg[i].in_use &&
925 !memcmp(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN) &&
926 p_clreg->notif_reg[i].handle == handle) {
927 APPL_TRACE_DEBUG("notification already registered");
928 status = BTA_GATT_OK;
929 break;
930 }
931 }
932 if (status != BTA_GATT_OK) {
933 for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
934 if (!p_clreg->notif_reg[i].in_use) {
935 memset((void *)&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
936
937 p_clreg->notif_reg[i].in_use = TRUE;
938 memcpy(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN);
939
940 p_clreg->notif_reg[i].handle = handle;
941 status = BTA_GATT_OK;
942 break;
943 }
944 }
945 if (i == BTA_GATTC_NOTIF_REG_MAX) {
946 status = BTA_GATT_NO_RESOURCES;
947 APPL_TRACE_ERROR("Max Notification Reached, registration failed,see CONFIG_BT_GATTC_NOTIF_REG_MAX in menuconfig");
948 }
949 }
950 } else {
951 APPL_TRACE_ERROR("Client_if: %d Not Registered", client_if);
952 }
953
954 return status;
955 }
956
957 /*******************************************************************************
958 **
959 ** Function BTA_GATTC_DeregisterForNotifications
960 **
961 ** Description This function is called to de-register for notification of a service.
962 **
963 ** Parameters client_if - client interface.
964 ** remote_bda - target GATT server.
965 ** handle - GATT characteristic handle.
966 **
967 ** Returns OK if deregistration succeed, otherwise failed.
968 **
969 *******************************************************************************/
BTA_GATTC_DeregisterForNotifications(tBTA_GATTC_IF client_if,BD_ADDR bda,UINT16 handle)970 tBTA_GATT_STATUS BTA_GATTC_DeregisterForNotifications (tBTA_GATTC_IF client_if,
971 BD_ADDR bda, UINT16 handle)
972 {
973 if (!handle) {
974 APPL_TRACE_ERROR("%s: deregistration failed, handle is 0", __func__);
975 return BTA_GATT_ILLEGAL_PARAMETER;
976 }
977
978 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(client_if);
979 if (p_clreg == NULL) {
980 APPL_TRACE_ERROR("%s client_if: %d not registered bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
981 __func__, client_if, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
982 return BTA_GATT_ILLEGAL_PARAMETER;
983 }
984
985 for (int i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
986 if (p_clreg->notif_reg[i].in_use &&
987 !memcmp(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN) &&
988 p_clreg->notif_reg[i].handle == handle) {
989 APPL_TRACE_DEBUG("%s deregistered bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
990 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
991 memset(&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
992 return BTA_GATT_OK;
993 }
994 }
995
996 APPL_TRACE_ERROR("%s registration not found bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
997 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
998 return BTA_GATT_ERROR;
999 }
1000
1001 /*******************************************************************************
1002 **
1003 ** Function BTA_GATTC_Refresh
1004 **
1005 ** Description Refresh the server cache of the remote device
1006 **
1007 ** Parameters remote_bda: remote device BD address.
1008 ** erase_flash: delete cache from nvs flash
1009 **
1010 ** Returns void
1011 **
1012 *******************************************************************************/
BTA_GATTC_Refresh(BD_ADDR remote_bda,bool erase_flash)1013 void BTA_GATTC_Refresh(BD_ADDR remote_bda, bool erase_flash)
1014 {
1015 #if(GATTC_CACHE_NVS == TRUE)
1016 if(erase_flash) {
1017 /* used to reset cache in application */
1018 bta_gattc_cache_reset(remote_bda);
1019 }
1020 #endif
1021 //If the registration callback is NULL, return
1022 if(bta_sys_is_register(BTA_ID_GATTC) == FALSE) {
1023 return;
1024 }
1025 tBTA_GATTC_API_CACHE_REFRESH *p_buf;
1026
1027 if ((p_buf = (tBTA_GATTC_API_CACHE_REFRESH *) osi_malloc(sizeof(tBTA_GATTC_API_CACHE_REFRESH))) != NULL) {
1028 p_buf->hdr.event = BTA_GATTC_API_REFRESH_EVT;
1029 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
1030
1031 bta_sys_sendmsg(p_buf);
1032 }
1033 return;
1034 }
1035
BTA_GATTC_CacheAssoc(tBTA_GATTC_IF client_if,BD_ADDR src_addr,BD_ADDR assoc_addr,BOOLEAN is_assoc)1036 void BTA_GATTC_CacheAssoc(tBTA_GATTC_IF client_if, BD_ADDR src_addr, BD_ADDR assoc_addr, BOOLEAN is_assoc)
1037 {
1038 tBTA_GATTC_API_CACHE_ASSOC *p_buf;
1039
1040 if ((p_buf = (tBTA_GATTC_API_CACHE_ASSOC *)osi_malloc(sizeof(tBTA_GATTC_API_CACHE_ASSOC))) != NULL) {
1041 p_buf->hdr.event = BTA_GATTC_API_CACHE_ASSOC_EVT;
1042 p_buf->is_assoc = is_assoc;
1043 p_buf->client_if = client_if;
1044 memcpy(p_buf->src_addr, src_addr, sizeof(BD_ADDR));
1045 memcpy(p_buf->assoc_addr, assoc_addr, sizeof(BD_ADDR));
1046
1047 bta_sys_sendmsg(p_buf);
1048
1049 }
1050 return;
1051 }
1052
BTA_GATTC_CacheGetAddrList(tBTA_GATTC_IF client_if)1053 void BTA_GATTC_CacheGetAddrList(tBTA_GATTC_IF client_if)
1054 {
1055 tBTA_GATTC_API_GET_ADDR *p_buf;
1056 if ((p_buf = (tBTA_GATTC_API_GET_ADDR *)osi_malloc(sizeof(tBTA_GATTC_API_GET_ADDR))) != NULL) {
1057 p_buf->hdr.event = BTA_GATTC_API_CACHE_GET_ADDR_LIST_EVT;
1058 p_buf->client_if = client_if;
1059
1060 bta_sys_sendmsg(p_buf);
1061 }
1062 return;
1063 }
1064
1065 /*******************************************************************************
1066 **
1067 ** Function BTA_GATTC_Clean
1068 **
1069 ** Description Clean the server cache of the remote device
1070 **
1071 ** Parameters remote_bda: remote device BD address.
1072 **
1073 ** Returns void
1074 **
1075 *******************************************************************************/
BTA_GATTC_Clean(BD_ADDR remote_bda)1076 void BTA_GATTC_Clean(BD_ADDR remote_bda)
1077 {
1078 #if(GATTC_CACHE_NVS == TRUE)
1079 /* used to reset cache in application */
1080 bta_gattc_cache_reset(remote_bda);
1081 #endif
1082
1083 tBTA_GATTC_API_CACHE_CLEAN *p_buf;
1084
1085 if ((p_buf = (tBTA_GATTC_API_CACHE_CLEAN *) osi_malloc(sizeof(tBTA_GATTC_API_CACHE_CLEAN))) != NULL) {
1086 p_buf->hdr.event = BTA_GATTC_API_CACHE_CLEAN_EVT;
1087 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
1088
1089 bta_sys_sendmsg(p_buf);
1090 }
1091 return;
1092 }
1093 /*******************************************************************************
1094 **
1095 ** Function BTA_GATTC_Listen
1096 **
1097 ** Description Start advertisement to listen for connection request for a GATT
1098 ** client application.
1099 **
1100 ** Parameters client_if: server interface.
1101 ** start: to start or stop listening for connection
1102 ** remote_bda: remote device BD address, if listen to all device
1103 ** use NULL.
1104 **
1105 ** Returns void
1106 **
1107 *******************************************************************************/
BTA_GATTC_Listen(tBTA_GATTC_IF client_if,BOOLEAN start,BD_ADDR_PTR target_bda)1108 void BTA_GATTC_Listen(tBTA_GATTC_IF client_if, BOOLEAN start, BD_ADDR_PTR target_bda)
1109 {
1110 tBTA_GATTC_API_LISTEN *p_buf;
1111
1112 if ((p_buf = (tBTA_GATTC_API_LISTEN *) osi_malloc((UINT16)(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN))) != NULL) {
1113 p_buf->hdr.event = BTA_GATTC_API_LISTEN_EVT;
1114
1115 p_buf->client_if = client_if;
1116 p_buf->start = start;
1117 if (target_bda) {
1118 p_buf->remote_bda = (UINT8 *)(p_buf + 1);
1119 memcpy(p_buf->remote_bda, target_bda, BD_ADDR_LEN);
1120 } else {
1121 p_buf->remote_bda = NULL;
1122 }
1123
1124 bta_sys_sendmsg(p_buf);
1125 }
1126 return;
1127 }
1128
1129 /*******************************************************************************
1130 **
1131 ** Function BTA_GATTC_Broadcast
1132 **
1133 ** Description Start broadcasting (non-connectable advertisements)
1134 **
1135 ** Parameters client_if: client interface.
1136 ** start: to start or stop listening for connection
1137 **
1138 ** Returns void
1139 **
1140 *******************************************************************************/
BTA_GATTC_Broadcast(tBTA_GATTC_IF client_if,BOOLEAN start)1141 void BTA_GATTC_Broadcast(tBTA_GATTC_IF client_if, BOOLEAN start)
1142 {
1143 tBTA_GATTC_API_LISTEN *p_buf;
1144
1145 if ((p_buf = (tBTA_GATTC_API_LISTEN *) osi_malloc((UINT16)(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN))) != NULL) {
1146 p_buf->hdr.event = BTA_GATTC_API_BROADCAST_EVT;
1147 p_buf->client_if = client_if;
1148 p_buf->start = start;
1149 bta_sys_sendmsg(p_buf);
1150 }
1151 return;
1152 }
1153
1154 /* Add For BLE PTS */
BTA_GATTC_AutoDiscoverEnable(uint8_t enable)1155 uint8_t BTA_GATTC_AutoDiscoverEnable(uint8_t enable)
1156 {
1157 APPL_TRACE_DEBUG("%s enable %d", __func__, enable);
1158
1159 bta_gattc_cb.auto_disc = ((enable > 0) ? true : false);
1160 GATTC_AutoDiscoverEnable(enable);
1161
1162 return 0;
1163 }
1164
1165 typedef struct {
1166 UINT16 len;
1167 union {
1168 UINT16 uuid16;
1169 UINT32 uuid32;
1170 UINT8 uuid128[LEN_UUID_128];
1171 } uuid;
1172 } __attribute__((packed)) tAPP_UUID;
1173
BTA_GATTC_Discover(uint8_t gatt_if,uint16_t conn_id,void * uuid,uint8_t disc_type,uint16_t s_handle,uint16_t e_handle)1174 uint8_t BTA_GATTC_Discover(uint8_t gatt_if, uint16_t conn_id, void *uuid, uint8_t disc_type, uint16_t s_handle, uint16_t e_handle)
1175 {
1176 tGATT_STATUS status;
1177 tGATT_DISC_PARAM param;
1178 tAPP_UUID *app_uuid = (tAPP_UUID *)uuid;
1179
1180 conn_id = (UINT16)((((UINT8)conn_id) << 8) | gatt_if);
1181 memset(¶m, 0, sizeof(tGATT_DISC_PARAM));
1182
1183 if (disc_type == GATT_DISC_SRVC_ALL || disc_type == GATT_DISC_SRVC_BY_UUID) {
1184 param.s_handle = 1;
1185 param.e_handle = 0xFFFF;
1186 } else {
1187 param.s_handle = s_handle;
1188 param.e_handle = e_handle;
1189 }
1190
1191 if (app_uuid) {
1192 param.service.len = app_uuid->len;
1193 if (app_uuid->len == LEN_UUID_16) {
1194 param.service.uu.uuid16 = app_uuid->uuid.uuid16;
1195 } else if (app_uuid->len == LEN_UUID_32) {
1196 param.service.uu.uuid32 = app_uuid->uuid.uuid32;
1197 } else if (app_uuid->len == LEN_UUID_128) {
1198 memcpy(param.service.uu.uuid128, app_uuid->uuid.uuid128, LEN_UUID_128);
1199 } else {
1200 APPL_TRACE_ERROR("%s invalid uuid len %u", __func__, app_uuid->len);
1201 }
1202 }
1203
1204 status = GATTC_Discover (conn_id, disc_type, ¶m);
1205 if (status != GATT_SUCCESS) {
1206 APPL_TRACE_ERROR("%s status %x", __func__, status);
1207 return -1;
1208 }
1209
1210 return 0;
1211 }
1212
BTA_GATTC_ReadLongChar(uint8_t gatt_if,uint16_t conn_id,uint16_t handle,uint16_t offset,uint8_t auth_req)1213 uint8_t BTA_GATTC_ReadLongChar(uint8_t gatt_if, uint16_t conn_id, uint16_t handle, uint16_t offset, uint8_t auth_req)
1214 {
1215 tGATT_STATUS status;
1216 tGATT_READ_PARAM read_param;
1217
1218 conn_id = (UINT16)((((UINT8)conn_id) << 8) | gatt_if);
1219 memset (&read_param, 0, sizeof(tGATT_READ_PARAM));
1220 read_param.partial.handle = handle;
1221 read_param.partial.offset = offset;
1222 read_param.partial.auth_req = auth_req;
1223
1224 status = GATTC_Read(conn_id, GATT_READ_PARTIAL, &read_param);
1225 if (status != GATT_SUCCESS) {
1226 APPL_TRACE_ERROR("%s status %x", __func__, status);
1227 return -1;
1228 }
1229
1230 return 0;
1231 }
1232 /* End BLE PTS */
1233 #endif /* defined(GATTC_INCLUDED) && (GATTC_INCLUDED == TRUE) */
1234