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_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_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)145 void BTA_GATTC_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)
147 {
148 tBTA_GATTC_API_OPEN *p_buf;
149
150 if ((p_buf = (tBTA_GATTC_API_OPEN *) osi_malloc(sizeof(tBTA_GATTC_API_OPEN))) != NULL) {
151 p_buf->hdr.event = BTA_GATTC_API_OPEN_EVT;
152
153 p_buf->client_if = client_if;
154 p_buf->is_direct = is_direct;
155 p_buf->transport = transport;
156 p_buf->is_aux = is_aux;
157 p_buf->remote_addr_type = remote_addr_type;
158 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
159
160
161 bta_sys_sendmsg(p_buf);
162 }
163 return;
164 }
165
166 /*******************************************************************************
167 **
168 ** Function BTA_GATTC_CancelOpen
169 **
170 ** Description Cancel a direct open connection or remove a background auto connection
171 ** bd address
172 **
173 ** Parameters client_if: server interface.
174 ** remote_bda: remote device BD address.
175 ** is_direct: direct connection or background auto connection
176 **
177 ** Returns void
178 **
179 *******************************************************************************/
BTA_GATTC_CancelOpen(tBTA_GATTC_IF client_if,BD_ADDR remote_bda,BOOLEAN is_direct)180 void BTA_GATTC_CancelOpen(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, BOOLEAN is_direct)
181 {
182 //If the registration callback is NULL, return
183 if(bta_sys_is_register(BTA_ID_GATTC) == FALSE) {
184 return;
185 }
186
187 tBTA_GATTC_API_CANCEL_OPEN *p_buf;
188
189 if ((p_buf = (tBTA_GATTC_API_CANCEL_OPEN *) osi_malloc(sizeof(tBTA_GATTC_API_CANCEL_OPEN))) != NULL) {
190 p_buf->hdr.event = BTA_GATTC_API_CANCEL_OPEN_EVT;
191
192 p_buf->client_if = client_if;
193 p_buf->is_direct = is_direct;
194 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
195
196 bta_sys_sendmsg(p_buf);
197 }
198 return;
199 }
200
201 /*******************************************************************************
202 **
203 ** Function BTA_GATTC_Close
204 **
205 ** Description Close a connection to a GATT server.
206 **
207 ** Parameters conn_id: connection ID to be closed.
208 **
209 ** Returns void
210 **
211 *******************************************************************************/
BTA_GATTC_Close(UINT16 conn_id)212 void BTA_GATTC_Close(UINT16 conn_id)
213 {
214 BT_HDR *p_buf;
215
216 if ((p_buf = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
217 p_buf->event = BTA_GATTC_API_CLOSE_EVT;
218
219 p_buf->layer_specific = conn_id;
220
221 bta_sys_sendmsg(p_buf);
222 }
223 return;
224
225 }
226 /*******************************************************************************
227 **
228 ** Function BTA_GATTC_ConfigureMTU
229 **
230 ** Description Configure the MTU size in the GATT channel. This can be done
231 ** only once per connection.
232 **
233 ** Parameters conn_id: connection ID.
234 ** mtu: desired MTU size to use.
235 **
236 ** Returns void
237 **
238 *******************************************************************************/
BTA_GATTC_ConfigureMTU(UINT16 conn_id)239 void BTA_GATTC_ConfigureMTU (UINT16 conn_id)
240 {
241 tBTA_GATTC_API_CFG_MTU *p_buf;
242
243 if ((p_buf = (tBTA_GATTC_API_CFG_MTU *) osi_malloc(sizeof(tBTA_GATTC_API_CFG_MTU))) != NULL) {
244 p_buf->hdr.event = BTA_GATTC_API_CFG_MTU_EVT;
245 p_buf->hdr.layer_specific = conn_id;
246
247 bta_sys_sendmsg(p_buf);
248 }
249 return;
250 }
251 /*******************************************************************************
252 **
253 ** Function BTA_GATTC_ServiceSearchRequest
254 **
255 ** Description This function is called to request a GATT service discovery
256 ** on a GATT server. This function report service search result
257 ** by a callback event, and followed by a service search complete
258 ** event.
259 **
260 ** Parameters conn_id: connection ID.
261 ** p_srvc_uuid: a UUID of the service application is interested in.
262 ** If Null, discover for all services.
263 **
264 ** Returns None
265 **
266 *******************************************************************************/
BTA_GATTC_ServiceSearchRequest(UINT16 conn_id,tBT_UUID * p_srvc_uuid)267 void BTA_GATTC_ServiceSearchRequest (UINT16 conn_id, tBT_UUID *p_srvc_uuid)
268 {
269 tBTA_GATTC_API_SEARCH *p_buf;
270 UINT16 len = sizeof(tBTA_GATTC_API_SEARCH) + sizeof(tBT_UUID);
271
272 if ((p_buf = (tBTA_GATTC_API_SEARCH *) osi_malloc(len)) != NULL) {
273 memset(p_buf, 0, len);
274
275 p_buf->hdr.event = BTA_GATTC_API_SEARCH_EVT;
276 p_buf->hdr.layer_specific = conn_id;
277
278 if (p_srvc_uuid) {
279 p_buf->p_srvc_uuid = (tBT_UUID *)(p_buf + 1);
280 memcpy(p_buf->p_srvc_uuid, p_srvc_uuid, sizeof(tBT_UUID));
281 } else {
282 p_buf->p_srvc_uuid = NULL;
283 }
284
285 bta_sys_sendmsg(p_buf);
286 }
287 return;
288 }
289
290
291 /*******************************************************************************
292 **
293 ** Function BTA_GATTC_GetServices
294 **
295 ** Description This function is called to find the services on the given server.
296 **
297 ** Parameters conn_id: connection ID which identify the server.
298 **
299 ** Returns returns list_t of tBTA_GATTC_SERVICE or NULL.
300 **
301 *******************************************************************************/
BTA_GATTC_GetServices(UINT16 conn_id)302 const list_t* BTA_GATTC_GetServices(UINT16 conn_id)
303 {
304 return bta_gattc_get_services(conn_id);
305 }
306
307 /*******************************************************************************
308 **
309 ** Function BTA_GATTC_GetCharacteristic
310 **
311 ** Description This function is called to find the characteristic on the given server.
312 **
313 ** Parameters conn_id - connection ID which identify the server.
314 ** handle - characteristic handle
315 **
316 ** Returns returns pointer to tBTA_GATTC_CHARACTERISTIC or NULL.
317 **
318 *******************************************************************************/
BTA_GATTC_GetCharacteristic(UINT16 conn_id,UINT16 handle)319 const tBTA_GATTC_CHARACTERISTIC* BTA_GATTC_GetCharacteristic(UINT16 conn_id, UINT16 handle)
320 {
321 return bta_gattc_get_characteristic(conn_id, handle);
322 }
323
324 /*******************************************************************************
325 **
326 ** Function BTA_GATTC_GetDescriptor
327 **
328 ** Description This function is called to find the characteristic on the given server.
329 **
330 ** Parameters conn_id - connection ID which identify the server.
331 ** handle - descriptor handle
332 **
333 ** Returns returns pointer to tBTA_GATTC_DESCRIPTOR or NULL.
334 **
335 *******************************************************************************/
BTA_GATTC_GetDescriptor(UINT16 conn_id,UINT16 handle)336 const tBTA_GATTC_DESCRIPTOR* BTA_GATTC_GetDescriptor(UINT16 conn_id, UINT16 handle)
337 {
338 return bta_gattc_get_descriptor(conn_id, handle);
339 }
340
BTA_GATTC_GetServiceWithUUID(UINT16 conn_id,tBT_UUID * svc_uuid,btgatt_db_element_t ** db,UINT16 * count)341 void BTA_GATTC_GetServiceWithUUID(UINT16 conn_id, tBT_UUID *svc_uuid,
342 btgatt_db_element_t **db, UINT16 *count)
343 {
344 bta_gattc_get_service_with_uuid(conn_id, svc_uuid, db, count);
345 }
346
BTA_GATTC_GetAllChar(UINT16 conn_id,UINT16 start_handle,UINT16 end_handle,btgatt_db_element_t ** db,UINT16 * count)347 void BTA_GATTC_GetAllChar(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle,
348 btgatt_db_element_t **db, UINT16 *count)
349 {
350 bta_gattc_get_db_with_opration(conn_id,
351 GATT_OP_GET_ALL_CHAR,
352 0,
353 NULL,
354 NULL,
355 NULL,
356 start_handle,
357 end_handle,
358 db,
359 count);
360 }
361
BTA_GATTC_GetAllDescriptor(UINT16 conn_id,UINT16 char_handle,btgatt_db_element_t ** db,UINT16 * count)362 void BTA_GATTC_GetAllDescriptor(UINT16 conn_id, UINT16 char_handle,
363 btgatt_db_element_t **db, UINT16 *count)
364 {
365 bta_gattc_get_db_with_opration(conn_id,
366 GATT_OP_GET_ALL_DESCRI,
367 char_handle,
368 NULL,
369 NULL,
370 NULL,
371 0,
372 0xFFFF,
373 db,
374 count);
375 }
376
BTA_GATTC_GetCharByUUID(UINT16 conn_id,UINT16 start_handle,UINT16 end_handle,tBT_UUID char_uuid,btgatt_db_element_t ** db,UINT16 * count)377 void BTA_GATTC_GetCharByUUID(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, tBT_UUID char_uuid,
378 btgatt_db_element_t **db, UINT16 *count)
379 {
380 bta_gattc_get_db_with_opration(conn_id,
381 GATT_OP_GET_CHAR_BY_UUID,
382 0,
383 NULL,
384 &char_uuid,
385 NULL,
386 start_handle,
387 end_handle,
388 db,
389 count);
390 }
391
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)392 void BTA_GATTC_GetDescrByUUID(UINT16 conn_id, uint16_t start_handle, uint16_t end_handle,
393 tBT_UUID char_uuid, tBT_UUID descr_uuid,
394 btgatt_db_element_t **db, UINT16 *count)
395 {
396 bta_gattc_get_db_with_opration(conn_id,
397 GATT_OP_GET_DESCRI_BY_UUID,
398 0,
399 NULL,
400 &char_uuid,
401 &descr_uuid,
402 start_handle,
403 end_handle,
404 db,
405 count);
406 }
407
BTA_GATTC_GetDescrByCharHandle(UINT16 conn_id,UINT16 char_handle,tBT_UUID descr_uuid,btgatt_db_element_t ** db,UINT16 * count)408 void BTA_GATTC_GetDescrByCharHandle(UINT16 conn_id, UINT16 char_handle, tBT_UUID descr_uuid,
409 btgatt_db_element_t **db, UINT16 *count)
410 {
411 bta_gattc_get_db_with_opration(conn_id,
412 GATT_OP_GET_DESCRI_BY_HANDLE,
413 char_handle,
414 NULL,
415 NULL,
416 &descr_uuid,
417 0,
418 0xFFFF,
419 db,
420 count);
421 }
422
BTA_GATTC_GetIncludeService(UINT16 conn_id,UINT16 start_handle,UINT16 end_handle,tBT_UUID * incl_uuid,btgatt_db_element_t ** db,UINT16 * count)423 void BTA_GATTC_GetIncludeService(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle,
424 tBT_UUID *incl_uuid, btgatt_db_element_t **db, UINT16 *count)
425 {
426 bta_gattc_get_db_with_opration(conn_id,
427 GATT_OP_GET_INCLUDE_SVC,
428 0,
429 incl_uuid,
430 NULL,
431 NULL,
432 start_handle,
433 end_handle,
434 db,
435 count);
436 }
437
BTA_GATTC_GetDBSize(UINT16 conn_id,UINT16 start_handle,UINT16 end_handle,UINT16 * count)438 void BTA_GATTC_GetDBSize(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle, UINT16 *count)
439 {
440 bta_gattc_get_db_size_handle(conn_id, start_handle, end_handle, count);
441 }
442
BTA_GATTC_GetDBSizeByType(UINT16 conn_id,bt_gatt_db_attribute_type_t type,UINT16 start_handle,UINT16 end_handle,UINT16 char_handle,UINT16 * count)443 void BTA_GATTC_GetDBSizeByType(UINT16 conn_id, bt_gatt_db_attribute_type_t type,
444 UINT16 start_handle, UINT16 end_handle, UINT16 char_handle, UINT16 *count)
445 {
446 bta_gattc_get_db_size_with_type_handle(conn_id, type, start_handle, end_handle, char_handle, count);
447 }
448
449
450 /*******************************************************************************
451 **
452 ** Function BTA_GATTC_GetGattDb
453 **
454 ** Description This function is called to get the GATT database.
455 **
456 ** Parameters conn_id: connection ID which identify the server.
457 ** db: output parameter which will contain the GATT database copy.
458 ** Caller is responsible for freeing it.
459 ** count: number of elements in database.
460 **
461 *******************************************************************************/
BTA_GATTC_GetGattDb(UINT16 conn_id,UINT16 start_handle,UINT16 end_handle,btgatt_db_element_t ** db,UINT16 * count)462 void BTA_GATTC_GetGattDb(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle,
463 btgatt_db_element_t **db, UINT16 *count)
464 {
465 bta_gattc_get_gatt_db(conn_id, start_handle, end_handle, db, count);
466 }
467
468 /*******************************************************************************
469 **
470 ** Function BTA_GATTC_ReadCharacteristic
471 **
472 ** Description This function is called to read a characteristics value
473 **
474 ** Parameters conn_id - connection ID.
475 ** handle - characteritic handle to read.
476 **
477 ** Returns None
478 **
479 *******************************************************************************/
BTA_GATTC_ReadCharacteristic(UINT16 conn_id,UINT16 handle,tBTA_GATT_AUTH_REQ auth_req)480 void BTA_GATTC_ReadCharacteristic(UINT16 conn_id, UINT16 handle, tBTA_GATT_AUTH_REQ auth_req)
481 {
482 tBTA_GATTC_API_READ *p_buf;
483
484 if ((p_buf = (tBTA_GATTC_API_READ *) osi_malloc(sizeof(tBTA_GATTC_API_READ))) != NULL) {
485 memset(p_buf, 0, sizeof(tBTA_GATTC_API_READ));
486
487 p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
488 p_buf->hdr.layer_specific = conn_id;
489 p_buf->auth_req = auth_req;
490 p_buf->handle = handle;
491 p_buf->cmpl_evt = BTA_GATTC_READ_CHAR_EVT;
492
493 bta_sys_sendmsg(p_buf);
494 }
495 return;
496 }
497
498 /*******************************************************************************
499 **
500 ** Function BTA_GATTC_ReadCharDescr
501 **
502 ** Description This function is called to read a descriptor value.
503 **
504 ** Parameters conn_id - connection ID.
505 ** handle - descriptor handle to read.
506 **
507 ** Returns None
508 **
509 *******************************************************************************/
BTA_GATTC_ReadCharDescr(UINT16 conn_id,UINT16 handle,tBTA_GATT_AUTH_REQ auth_req)510 void BTA_GATTC_ReadCharDescr (UINT16 conn_id, UINT16 handle, tBTA_GATT_AUTH_REQ auth_req)
511 {
512 tBTA_GATTC_API_READ *p_buf;
513 UINT16 len = (UINT16)(sizeof(tBTA_GATT_ID) + sizeof(tBTA_GATTC_API_READ));
514
515 if ((p_buf = (tBTA_GATTC_API_READ *) osi_malloc(len)) != NULL) {
516 memset(p_buf, 0, sizeof(tBTA_GATTC_API_READ));
517
518 p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
519 p_buf->hdr.layer_specific = conn_id;
520 p_buf->auth_req = auth_req;
521 p_buf->handle = handle;
522 p_buf->cmpl_evt = BTA_GATTC_READ_DESCR_EVT;
523
524 bta_sys_sendmsg(p_buf);
525 }
526 return;
527
528 }
529 /*******************************************************************************
530 **
531 ** Function BTA_GATTC_ReadMultiple
532 **
533 ** Description This function is called to read multiple characteristic or
534 ** characteristic descriptors.
535 **
536 ** Parameters conn_id - connection ID.
537 ** p_read_multi - pointer to the read multiple parameter.
538 **
539 ** Returns None
540 **
541 *******************************************************************************/
BTA_GATTC_ReadMultiple(UINT16 conn_id,tBTA_GATTC_MULTI * p_read_multi,tBTA_GATT_AUTH_REQ auth_req)542 void BTA_GATTC_ReadMultiple(UINT16 conn_id, tBTA_GATTC_MULTI *p_read_multi,
543 tBTA_GATT_AUTH_REQ auth_req)
544 {
545 tBTA_GATTC_API_READ_MULTI *p_buf;
546 //tBTA_GATTC_API_READ_MULTI *p_value;
547 UINT16 len = (UINT16)(sizeof(tBTA_GATTC_API_READ_MULTI));
548
549 if ((p_buf = (tBTA_GATTC_API_READ_MULTI *) osi_malloc(len)) != NULL) {
550 memset(p_buf, 0, len);
551
552 p_buf->hdr.event = BTA_GATTC_API_READ_MULTI_EVT;
553 p_buf->hdr.layer_specific = conn_id;
554 p_buf->auth_req = auth_req;
555 p_buf->num_attr = p_read_multi->num_attr;
556 p_buf->cmpl_evt = BTA_GATTC_READ_MULTIPLE_EVT;
557 if (p_buf->num_attr > 0) {
558 memcpy(p_buf->handles, p_read_multi->handles, sizeof(UINT16) * p_read_multi->num_attr);
559 }
560
561 bta_sys_sendmsg(p_buf);
562 }
563 return;
564 }
565
566 /*******************************************************************************
567 **
568 ** Function BTA_GATTC_Read_by_type
569 **
570 ** Description This function is called to read a attribute value by uuid
571 **
572 ** Parameters conn_id - connection ID.
573 ** s_handle - start handle.
574 ** e_handle - end hanle
575 ** uuid - The attribute UUID.
576 **
577 ** Returns None
578 **
579 *******************************************************************************/
BTA_GATTC_Read_by_type(UINT16 conn_id,UINT16 s_handle,UINT16 e_handle,tBT_UUID * uuid,tBTA_GATT_AUTH_REQ auth_req)580 void BTA_GATTC_Read_by_type(UINT16 conn_id, UINT16 s_handle,UINT16 e_handle, tBT_UUID *uuid, tBTA_GATT_AUTH_REQ auth_req)
581 {
582 tBTA_GATTC_API_READ *p_buf;
583
584 if ((p_buf = (tBTA_GATTC_API_READ *) osi_malloc(sizeof(tBTA_GATTC_API_READ))) != NULL) {
585 memset(p_buf, 0, sizeof(tBTA_GATTC_API_READ));
586
587 p_buf->hdr.event = BTA_GATTC_API_READ_BY_TYPE_EVT;
588 p_buf->hdr.layer_specific = conn_id;
589 p_buf->auth_req = auth_req;
590 p_buf->s_handle = s_handle;
591 p_buf->e_handle = e_handle;
592 memcpy(&(p_buf->uuid), uuid, sizeof(tBT_UUID));
593 p_buf->cmpl_evt = BTA_GATTC_READ_CHAR_EVT;
594
595 bta_sys_sendmsg(p_buf);
596 }
597 return;
598 }
599
600 /*******************************************************************************
601 **
602 ** Function BTA_GATTC_WriteCharValue
603 **
604 ** Description This function is called to write characteristic value.
605 **
606 ** Parameters conn_id - connection ID.
607 ** handle - characteristic handle to write.
608 ** write_type - type of write.
609 ** len: length of the data to be written.
610 ** p_value - the value to be written.
611 **
612 ** Returns None
613 **
614 *******************************************************************************/
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)615 void BTA_GATTC_WriteCharValue ( UINT16 conn_id,
616 UINT16 handle,
617 tBTA_GATTC_WRITE_TYPE write_type,
618 UINT16 len,
619 UINT8 *p_value,
620 tBTA_GATT_AUTH_REQ auth_req)
621 {
622 tBTA_GATTC_API_WRITE *p_buf;
623
624 if ((p_buf = (tBTA_GATTC_API_WRITE *) osi_malloc((UINT16)(sizeof(tBTA_GATTC_API_WRITE) + len))) != NULL) {
625 memset(p_buf, 0, sizeof(tBTA_GATTC_API_WRITE) + len);
626
627 p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
628 p_buf->hdr.layer_specific = conn_id;
629 p_buf->auth_req = auth_req;
630 p_buf->handle = handle;
631 p_buf->cmpl_evt = BTA_GATTC_WRITE_CHAR_EVT;
632 p_buf->write_type = write_type;
633 p_buf->len = len;
634
635 if (p_value && len > 0) {
636 p_buf->p_value = (UINT8 *)(p_buf + 1);
637 memcpy(p_buf->p_value, p_value, len);
638 }
639 if(write_type == BTA_GATTC_TYPE_WRITE_NO_RSP){
640 l2ble_update_att_acl_pkt_num(L2CA_DECREASE_BTC_NUM, NULL);
641 l2ble_update_att_acl_pkt_num(L2CA_ADD_BTU_NUM, NULL);
642 }
643 bta_sys_sendmsg(p_buf);
644 }
645 return;
646 }
647 /*******************************************************************************
648 **
649 ** Function BTA_GATTC_WriteCharDescr
650 **
651 ** Description This function is called to write descriptor value.
652 **
653 ** Parameters conn_id - connection ID
654 ** handle - descriptor hadle to write.
655 ** write_type - write type.
656 ** p_value - the value to be written.
657 **
658 ** Returns None
659 **
660 *******************************************************************************/
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)661 void BTA_GATTC_WriteCharDescr (UINT16 conn_id,
662 UINT16 handle,
663 tBTA_GATTC_WRITE_TYPE write_type,
664 tBTA_GATT_UNFMT *p_data,
665 tBTA_GATT_AUTH_REQ auth_req)
666 {
667 size_t len = sizeof(tBTA_GATTC_API_WRITE);
668 tBTA_GATTC_API_WRITE *p_buf;
669 if (p_data != NULL) {
670 len += p_data->len;
671 }
672
673 if ((p_buf = (tBTA_GATTC_API_WRITE *) osi_malloc(len)) != NULL) {
674 memset(p_buf, 0, len);
675
676 p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
677 p_buf->hdr.layer_specific = conn_id;
678 p_buf->auth_req = auth_req;
679 p_buf->handle = handle;
680 p_buf->cmpl_evt = BTA_GATTC_WRITE_DESCR_EVT;
681 p_buf->write_type = write_type;
682
683 if (p_data && p_data->len != 0) {
684 p_buf->p_value = (UINT8 *)(p_buf + 1);
685 p_buf->len = p_data->len;
686 /* pack the descr data */
687 memcpy(p_buf->p_value, p_data->p_value, p_data->len);
688 }
689 if(write_type == BTA_GATTC_TYPE_WRITE_NO_RSP){
690 l2ble_update_att_acl_pkt_num(L2CA_DECREASE_BTC_NUM, NULL);
691 l2ble_update_att_acl_pkt_num(L2CA_ADD_BTU_NUM, NULL);
692 }
693 bta_sys_sendmsg(p_buf);
694 }
695 return;
696
697 }
698 /*******************************************************************************
699 **
700 ** Function BTA_GATTC_PrepareWrite
701 **
702 ** Description This function is called to prepare write a characteristic value.
703 **
704 ** Parameters conn_id - connection ID.
705 ** p_char_id - GATT characteritic ID of the service.
706 ** offset - offset of the write value.
707 ** len: length of the data to be written.
708 ** p_value - the value to be written.
709 **
710 ** Returns None
711 **
712 *******************************************************************************/
BTA_GATTC_PrepareWrite(UINT16 conn_id,UINT16 handle,UINT16 offset,UINT16 len,UINT8 * p_value,tBTA_GATT_AUTH_REQ auth_req)713 void BTA_GATTC_PrepareWrite (UINT16 conn_id, UINT16 handle,
714 UINT16 offset, UINT16 len, UINT8 *p_value,
715 tBTA_GATT_AUTH_REQ auth_req)
716 {
717 tBTA_GATTC_API_WRITE *p_buf;
718
719 if ((p_buf = (tBTA_GATTC_API_WRITE *) osi_malloc((UINT16)(sizeof(tBTA_GATTC_API_WRITE) + len))) != NULL) {
720 memset(p_buf, 0, sizeof(tBTA_GATTC_API_WRITE) + len);
721
722 p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
723 p_buf->hdr.layer_specific = conn_id;
724 p_buf->auth_req = auth_req;
725 p_buf->handle = handle;
726
727 p_buf->write_type = BTA_GATTC_WRITE_PREPARE;
728 p_buf->offset = offset;
729 p_buf->len = len;
730
731 if (p_value && len > 0) {
732 p_buf->p_value = (UINT8 *)(p_buf + 1);
733 memcpy(p_buf->p_value, p_value, len);
734 }
735
736 bta_sys_sendmsg(p_buf);
737 }
738 return;
739
740 }
741 /*******************************************************************************
742 **
743 ** Function BTA_GATTC_PrepareWriteCharDescr
744 **
745 ** Description This function is called to prepare write a characteristic descriptor value.
746 **
747 ** Parameters conn_id - connection ID.
748 ** p_char_descr_id - GATT characteritic descriptor ID of the service.
749 ** offset - offset of the write value.
750 ** len: length of the data to be written.
751 ** p_value - the value to be written.
752 **
753 ** Returns None
754 **
755 *******************************************************************************/
BTA_GATTC_PrepareWriteCharDescr(UINT16 conn_id,UINT16 handle,UINT16 offset,tBTA_GATT_UNFMT * p_data,tBTA_GATT_AUTH_REQ auth_req)756 void BTA_GATTC_PrepareWriteCharDescr (UINT16 conn_id, UINT16 handle,
757 UINT16 offset,tBTA_GATT_UNFMT *p_data,
758 tBTA_GATT_AUTH_REQ auth_req)
759 {
760 tBTA_GATTC_API_WRITE *p_buf;
761 UINT16 len = sizeof(tBTA_GATTC_API_WRITE);
762
763 if (p_data != NULL) {
764 len += p_data->len;
765 }
766
767 if ((p_buf = (tBTA_GATTC_API_WRITE *) osi_malloc(len)) != NULL) {
768 memset(p_buf, 0, 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 p_buf->write_type = BTA_GATTC_WRITE_PREPARE;
775 p_buf->offset = offset;
776
777 if (p_data && p_data->len != 0) {
778 p_buf->len = p_data->len;
779 p_buf->p_value = (UINT8 *)(p_buf + 1);
780 /* pack the descr data */
781 memcpy(p_buf->p_value, p_data->p_value, p_data->len);
782 }
783
784 bta_sys_sendmsg(p_buf);
785 }
786 return;
787
788 }
789 /*******************************************************************************
790 **
791 ** Function BTA_GATTC_ExecuteWrite
792 **
793 ** Description This function is called to execute write a prepare write sequence.
794 **
795 ** Parameters conn_id - connection ID.
796 ** is_execute - execute or cancel.
797 **
798 ** Returns None
799 **
800 *******************************************************************************/
BTA_GATTC_ExecuteWrite(UINT16 conn_id,BOOLEAN is_execute)801 void BTA_GATTC_ExecuteWrite (UINT16 conn_id, BOOLEAN is_execute)
802 {
803 tBTA_GATTC_API_EXEC *p_buf;
804
805 if ((p_buf = (tBTA_GATTC_API_EXEC *) osi_malloc((UINT16)sizeof(tBTA_GATTC_API_EXEC))) != NULL) {
806 memset(p_buf, 0, sizeof(tBTA_GATTC_API_EXEC));
807 p_buf->hdr.event = BTA_GATTC_API_EXEC_EVT;
808 p_buf->hdr.layer_specific = conn_id;
809
810 p_buf->is_execute = is_execute;
811
812 bta_sys_sendmsg(p_buf);
813 }
814 return;
815
816 }
817 /*******************************************************************************
818 **
819 ** Function BTA_GATTC_SendIndConfirm
820 **
821 ** Description This function is called to send handle value confirmation.
822 **
823 ** Parameters conn_id - connection ID.
824 ** p_char_id - characteristic ID to confirm.
825 **
826 ** Returns None
827 **
828 *******************************************************************************/
BTA_GATTC_SendIndConfirm(UINT16 conn_id,UINT16 handle)829 void BTA_GATTC_SendIndConfirm (UINT16 conn_id, UINT16 handle)
830 {
831 tBTA_GATTC_API_CONFIRM *p_buf;
832
833 APPL_TRACE_API("BTA_GATTC_SendIndConfirm conn_id=%d handle =0x%x",
834 conn_id, handle);
835
836 if ((p_buf = (tBTA_GATTC_API_CONFIRM *) osi_malloc(sizeof(tBTA_GATTC_API_CONFIRM))) != NULL) {
837 memset(p_buf, 0, sizeof(tBTA_GATTC_API_CONFIRM));
838 p_buf->hdr.event = BTA_GATTC_API_CONFIRM_EVT;
839 p_buf->hdr.layer_specific = conn_id;
840 p_buf->handle = handle;
841
842 bta_sys_sendmsg(p_buf);
843 }
844 return;
845
846 }
847
848 /*******************************************************************************
849 **
850 ** Function BTA_GATTC_RegisterForNotifications
851 **
852 ** Description This function is called to register for notification of a service.
853 **
854 ** Parameters client_if - client interface.
855 ** bda - target GATT server.
856 ** handle - GATT characteristic handle.
857 **
858 ** Returns OK if registration succeed, otherwise failed.
859 **
860 *******************************************************************************/
BTA_GATTC_RegisterForNotifications(tBTA_GATTC_IF client_if,BD_ADDR bda,UINT16 handle)861 tBTA_GATT_STATUS BTA_GATTC_RegisterForNotifications (tBTA_GATTC_IF client_if,
862 BD_ADDR bda, UINT16 handle)
863 {
864 tBTA_GATTC_RCB *p_clreg;
865 tBTA_GATT_STATUS status = BTA_GATT_ILLEGAL_PARAMETER;
866 UINT8 i;
867
868 if (!handle)
869 {
870 APPL_TRACE_ERROR("deregistration failed, handle is 0");
871 return status;
872 }
873
874 if ((p_clreg = bta_gattc_cl_get_regcb(client_if)) != NULL) {
875 for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
876 if ( p_clreg->notif_reg[i].in_use &&
877 !memcmp(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN) &&
878 p_clreg->notif_reg[i].handle == handle) {
879 APPL_TRACE_DEBUG("notification already registered");
880 status = BTA_GATT_OK;
881 break;
882 }
883 }
884 if (status != BTA_GATT_OK) {
885 for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
886 if (!p_clreg->notif_reg[i].in_use) {
887 memset((void *)&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
888
889 p_clreg->notif_reg[i].in_use = TRUE;
890 memcpy(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN);
891
892 p_clreg->notif_reg[i].handle = handle;
893 status = BTA_GATT_OK;
894 break;
895 }
896 }
897 if (i == BTA_GATTC_NOTIF_REG_MAX) {
898 status = BTA_GATT_NO_RESOURCES;
899 APPL_TRACE_ERROR("Max Notification Reached, registration failed.");
900 }
901 }
902 } else {
903 APPL_TRACE_ERROR("Client_if: %d Not Registered", client_if);
904 }
905
906 return status;
907 }
908
909 /*******************************************************************************
910 **
911 ** Function BTA_GATTC_DeregisterForNotifications
912 **
913 ** Description This function is called to de-register for notification of a service.
914 **
915 ** Parameters client_if - client interface.
916 ** remote_bda - target GATT server.
917 ** handle - GATT characteristic handle.
918 **
919 ** Returns OK if deregistration succeed, otherwise failed.
920 **
921 *******************************************************************************/
BTA_GATTC_DeregisterForNotifications(tBTA_GATTC_IF client_if,BD_ADDR bda,UINT16 handle)922 tBTA_GATT_STATUS BTA_GATTC_DeregisterForNotifications (tBTA_GATTC_IF client_if,
923 BD_ADDR bda, UINT16 handle)
924 {
925 if (!handle) {
926 APPL_TRACE_ERROR("%s: deregistration failed, handle is 0", __func__);
927 return BTA_GATT_ILLEGAL_PARAMETER;
928 }
929
930 tBTA_GATTC_RCB *p_clreg = bta_gattc_cl_get_regcb(client_if);
931 if (p_clreg == NULL) {
932 APPL_TRACE_ERROR("%s client_if: %d not registered bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
933 __func__, client_if, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
934 return BTA_GATT_ILLEGAL_PARAMETER;
935 }
936
937 for (int i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++) {
938 if (p_clreg->notif_reg[i].in_use &&
939 !memcmp(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN) &&
940 p_clreg->notif_reg[i].handle == handle) {
941 APPL_TRACE_DEBUG("%s deregistered bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
942 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
943 memset(&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
944 return BTA_GATT_OK;
945 }
946 }
947
948 APPL_TRACE_ERROR("%s registration not found bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
949 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
950 return BTA_GATT_ERROR;
951 }
952
953 /*******************************************************************************
954 **
955 ** Function BTA_GATTC_Refresh
956 **
957 ** Description Refresh the server cache of the remote device
958 **
959 ** Parameters remote_bda: remote device BD address.
960 ** erase_flash: delete cache from nvs flash
961 **
962 ** Returns void
963 **
964 *******************************************************************************/
BTA_GATTC_Refresh(BD_ADDR remote_bda,bool erase_flash)965 void BTA_GATTC_Refresh(BD_ADDR remote_bda, bool erase_flash)
966 {
967 #if(GATTC_CACHE_NVS == TRUE)
968 if(erase_flash) {
969 /* used to reset cache in application */
970 bta_gattc_cache_reset(remote_bda);
971 }
972 #endif
973 //If the registration callback is NULL, return
974 if(bta_sys_is_register(BTA_ID_GATTC) == FALSE) {
975 return;
976 }
977 tBTA_GATTC_API_OPEN *p_buf;
978
979 if ((p_buf = (tBTA_GATTC_API_OPEN *) osi_malloc(sizeof(tBTA_GATTC_API_OPEN))) != NULL) {
980 p_buf->hdr.event = BTA_GATTC_API_REFRESH_EVT;
981 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
982
983 bta_sys_sendmsg(p_buf);
984 }
985 return;
986 }
987
BTA_GATTC_CacheAssoc(tBTA_GATTC_IF client_if,BD_ADDR src_addr,BD_ADDR assoc_addr,BOOLEAN is_assoc)988 void BTA_GATTC_CacheAssoc(tBTA_GATTC_IF client_if, BD_ADDR src_addr, BD_ADDR assoc_addr, BOOLEAN is_assoc)
989 {
990 tBTA_GATTC_API_CACHE_ASSOC *p_buf;
991
992 if ((p_buf = (tBTA_GATTC_API_CACHE_ASSOC *)osi_malloc(sizeof(tBTA_GATTC_API_CACHE_ASSOC))) != NULL) {
993 p_buf->hdr.event = BTA_GATTC_API_CACHE_ASSOC_EVT;
994 p_buf->is_assoc = is_assoc;
995 p_buf->client_if = client_if;
996 memcpy(p_buf->src_addr, src_addr, sizeof(BD_ADDR));
997 memcpy(p_buf->assoc_addr, assoc_addr, sizeof(BD_ADDR));
998
999 bta_sys_sendmsg(p_buf);
1000
1001 }
1002 return;
1003 }
1004
BTA_GATTC_CacheGetAddrList(tBTA_GATTC_IF client_if)1005 void BTA_GATTC_CacheGetAddrList(tBTA_GATTC_IF client_if)
1006 {
1007 tBTA_GATTC_API_GET_ADDR *p_buf;
1008 if ((p_buf = (tBTA_GATTC_API_GET_ADDR *)osi_malloc(sizeof(tBTA_GATTC_API_GET_ADDR))) != NULL) {
1009 p_buf->hdr.event = BTA_GATTC_API_CACHE_GET_ADDR_LIST_EVT;
1010 p_buf->client_if = client_if;
1011
1012 bta_sys_sendmsg(p_buf);
1013 }
1014 return;
1015 }
1016
1017 /*******************************************************************************
1018 **
1019 ** Function BTA_GATTC_Clean
1020 **
1021 ** Description Clean the server cache of the remote device
1022 **
1023 ** Parameters remote_bda: remote device BD address.
1024 **
1025 ** Returns void
1026 **
1027 *******************************************************************************/
BTA_GATTC_Clean(BD_ADDR remote_bda)1028 void BTA_GATTC_Clean(BD_ADDR remote_bda)
1029 {
1030 #if(GATTC_CACHE_NVS == TRUE)
1031 /* used to reset cache in application */
1032 bta_gattc_cache_reset(remote_bda);
1033 #endif
1034
1035 tBTA_GATTC_API_OPEN *p_buf;
1036
1037 if ((p_buf = (tBTA_GATTC_API_OPEN *) osi_malloc(sizeof(tBTA_GATTC_API_OPEN))) != NULL) {
1038 p_buf->hdr.event = BTA_GATTC_API_CACHE_CLEAN_EVT;
1039 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
1040
1041 bta_sys_sendmsg(p_buf);
1042 }
1043 return;
1044 }
1045 /*******************************************************************************
1046 **
1047 ** Function BTA_GATTC_Listen
1048 **
1049 ** Description Start advertisement to listen for connection request for a GATT
1050 ** client application.
1051 **
1052 ** Parameters client_if: server interface.
1053 ** start: to start or stop listening for connection
1054 ** remote_bda: remote device BD address, if listen to all device
1055 ** use NULL.
1056 **
1057 ** Returns void
1058 **
1059 *******************************************************************************/
BTA_GATTC_Listen(tBTA_GATTC_IF client_if,BOOLEAN start,BD_ADDR_PTR target_bda)1060 void BTA_GATTC_Listen(tBTA_GATTC_IF client_if, BOOLEAN start, BD_ADDR_PTR target_bda)
1061 {
1062 tBTA_GATTC_API_LISTEN *p_buf;
1063
1064 if ((p_buf = (tBTA_GATTC_API_LISTEN *) osi_malloc((UINT16)(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN))) != NULL) {
1065 p_buf->hdr.event = BTA_GATTC_API_LISTEN_EVT;
1066
1067 p_buf->client_if = client_if;
1068 p_buf->start = start;
1069 if (target_bda) {
1070 p_buf->remote_bda = (UINT8 *)(p_buf + 1);
1071 memcpy(p_buf->remote_bda, target_bda, BD_ADDR_LEN);
1072 } else {
1073 p_buf->remote_bda = NULL;
1074 }
1075
1076 bta_sys_sendmsg(p_buf);
1077 }
1078 return;
1079 }
1080
1081 /*******************************************************************************
1082 **
1083 ** Function BTA_GATTC_Broadcast
1084 **
1085 ** Description Start broadcasting (non-connectable advertisements)
1086 **
1087 ** Parameters client_if: client interface.
1088 ** start: to start or stop listening for connection
1089 **
1090 ** Returns void
1091 **
1092 *******************************************************************************/
BTA_GATTC_Broadcast(tBTA_GATTC_IF client_if,BOOLEAN start)1093 void BTA_GATTC_Broadcast(tBTA_GATTC_IF client_if, BOOLEAN start)
1094 {
1095 tBTA_GATTC_API_LISTEN *p_buf;
1096
1097 if ((p_buf = (tBTA_GATTC_API_LISTEN *) osi_malloc((UINT16)(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN))) != NULL) {
1098 p_buf->hdr.event = BTA_GATTC_API_BROADCAST_EVT;
1099 p_buf->client_if = client_if;
1100 p_buf->start = start;
1101 bta_sys_sendmsg(p_buf);
1102 }
1103 return;
1104 }
1105
1106 #endif /* defined(GATTC_INCLUDED) && (GATTC_INCLUDED == TRUE) */
1107