OSDN Git Service

Restructure GATTC
[android-x86/system-bt.git] / bta / gatt / bta_gattc_api.c
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 "bt_target.h"
26
27 #if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
28
29 #include <string.h>
30 #include "bt_common.h"
31 #include "bta_sys.h"
32 #include "bta_gatt_api.h"
33 #include "bta_gattc_int.h"
34
35 /*****************************************************************************
36 **  Constants
37 *****************************************************************************/
38
39 static const tBTA_SYS_REG bta_gattc_reg =
40 {
41     bta_gattc_hdl_event,
42     BTA_GATTC_Disable
43 };
44
45
46 /*******************************************************************************
47 **
48 ** Function         BTA_GATTC_Disable
49 **
50 ** Description      This function is called to disable GATTC module
51 **
52 ** Parameters       None.
53 **
54 ** Returns          None
55 **
56 *******************************************************************************/
57 void BTA_GATTC_Disable(void)
58 {
59     if (bta_sys_is_register(BTA_ID_GATTC) == FALSE)
60     {
61         APPL_TRACE_WARNING("GATTC Module not enabled/already disabled");
62         return;
63     }
64
65     BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR));
66     p_buf->event = BTA_GATTC_API_DISABLE_EVT;
67
68     bta_sys_sendmsg(p_buf);
69     bta_sys_deregister(BTA_ID_GATTC);
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 - applicaiton UUID
80 **                  p_client_cb - pointer to the application callback function.
81 **
82 ** Returns          None
83 **
84 *******************************************************************************/
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         (tBTA_GATTC_API_REG *)osi_malloc(sizeof(tBTA_GATTC_API_REG));
89
90     if (bta_sys_is_register(BTA_ID_GATTC) == FALSE)
91         bta_sys_register(BTA_ID_GATTC, &bta_gattc_reg);
92
93     p_buf->hdr.event = BTA_GATTC_API_REG_EVT;
94     if (p_app_uuid != NULL)
95         memcpy(&p_buf->app_uuid, p_app_uuid, sizeof(tBT_UUID));
96     p_buf->p_cback = p_client_cb;
97
98     bta_sys_sendmsg(p_buf);
99 }
100
101 /*******************************************************************************
102 **
103 ** Function         BTA_GATTC_AppDeregister
104 **
105 ** Description      This function is called to deregister an application
106 **                  from BTA GATTC module.
107 **
108 ** Parameters       client_if - client interface identifier.
109 **
110 ** Returns          None
111 **
112 *******************************************************************************/
113 void BTA_GATTC_AppDeregister(tBTA_GATTC_IF client_if)
114 {
115     tBTA_GATTC_API_DEREG *p_buf =
116         (tBTA_GATTC_API_DEREG *)osi_malloc(sizeof(tBTA_GATTC_API_DEREG));
117
118     p_buf->hdr.event = BTA_GATTC_API_DEREG_EVT;
119     p_buf->client_if = client_if;
120
121     bta_sys_sendmsg(p_buf);
122 }
123
124 /*******************************************************************************
125 **
126 ** Function         BTA_GATTC_Open
127 **
128 ** Description      Open a direct connection or add a background auto connection
129 **                  bd address
130 **
131 ** Parameters       client_if: server interface.
132 **                  remote_bda: remote device BD address.
133 **                  is_direct: direct connection or background auto connection
134 **                  transport: Transport to be used for GATT connection (BREDR/LE)
135 **
136 ** Returns          void
137 **
138 *******************************************************************************/
139 void BTA_GATTC_Open(tBTA_GATTC_IF client_if, BD_ADDR remote_bda,
140                     BOOLEAN is_direct, tBTA_GATT_TRANSPORT transport)
141 {
142     tBTA_GATTC_API_OPEN *p_buf =
143        (tBTA_GATTC_API_OPEN *) osi_malloc(sizeof(tBTA_GATTC_API_OPEN));
144
145     p_buf->hdr.event = BTA_GATTC_API_OPEN_EVT;
146     p_buf->client_if = client_if;
147     p_buf->is_direct = is_direct;
148     p_buf->transport = transport;
149     memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
150
151     bta_sys_sendmsg(p_buf);
152 }
153
154 /*******************************************************************************
155 **
156 ** Function         BTA_GATTC_CancelOpen
157 **
158 ** Description      Cancel a direct open connection or remove a background auto connection
159 **                  bd address
160 **
161 ** Parameters       client_if: server interface.
162 **                  remote_bda: remote device BD address.
163 **                  is_direct: direct connection or background auto connection
164 **
165 ** Returns          void
166 **
167 *******************************************************************************/
168 void BTA_GATTC_CancelOpen(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, BOOLEAN is_direct)
169 {
170     tBTA_GATTC_API_CANCEL_OPEN *p_buf =
171         (tBTA_GATTC_API_CANCEL_OPEN *)osi_malloc(sizeof(tBTA_GATTC_API_CANCEL_OPEN));
172
173     p_buf->hdr.event = BTA_GATTC_API_CANCEL_OPEN_EVT;
174     p_buf->client_if = client_if;
175     p_buf->is_direct = is_direct;
176     memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
177
178     bta_sys_sendmsg(p_buf);
179 }
180
181 /*******************************************************************************
182 **
183 ** Function         BTA_GATTC_Close
184 **
185 ** Description      Close a connection to a GATT server.
186 **
187 ** Parameters       conn_id: connectino ID to be closed.
188 **
189 ** Returns          void
190 **
191 *******************************************************************************/
192 void BTA_GATTC_Close(UINT16 conn_id)
193 {
194     BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR));
195
196     p_buf->event = BTA_GATTC_API_CLOSE_EVT;
197     p_buf->layer_specific = conn_id;
198
199     bta_sys_sendmsg(p_buf);
200 }
201
202 /*******************************************************************************
203 **
204 ** Function         BTA_GATTC_ConfigureMTU
205 **
206 ** Description      Configure the MTU size in the GATT channel. This can be done
207 **                  only once per connection.
208 **
209 ** Parameters       conn_id: connection ID.
210 **                  mtu: desired MTU size to use.
211 **
212 ** Returns          void
213 **
214 *******************************************************************************/
215 void BTA_GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu)
216 {
217     tBTA_GATTC_API_CFG_MTU *p_buf =
218         (tBTA_GATTC_API_CFG_MTU *)osi_malloc(sizeof(tBTA_GATTC_API_CFG_MTU));
219
220     p_buf->hdr.event = BTA_GATTC_API_CFG_MTU_EVT;
221     p_buf->hdr.layer_specific = conn_id;
222     p_buf->mtu = mtu;
223
224     bta_sys_sendmsg(p_buf);
225 }
226
227 /*******************************************************************************
228 **
229 ** Function         BTA_GATTC_ServiceSearchRequest
230 **
231 ** Description      This function is called to request a GATT service discovery
232 **                    on a GATT server. This function report service search result
233 **                  by a callback event, and followed by a service search complete
234 **                  event.
235 **
236 ** Parameters       conn_id: connection ID.
237 **                  p_srvc_uuid: a UUID of the service application is interested in.
238 **                              If Null, discover for all services.
239 **
240 ** Returns          None
241 **
242 *******************************************************************************/
243 void BTA_GATTC_ServiceSearchRequest (UINT16 conn_id, tBT_UUID *p_srvc_uuid)
244 {
245     const size_t len = sizeof(tBTA_GATTC_API_SEARCH) + sizeof(tBT_UUID);
246     tBTA_GATTC_API_SEARCH *p_buf = (tBTA_GATTC_API_SEARCH *)osi_calloc(len);
247
248     p_buf->hdr.event = BTA_GATTC_API_SEARCH_EVT;
249     p_buf->hdr.layer_specific = conn_id;
250     if (p_srvc_uuid) {
251         p_buf->p_srvc_uuid = (tBT_UUID *)(p_buf + 1);
252         memcpy(p_buf->p_srvc_uuid, p_srvc_uuid, sizeof(tBT_UUID));
253     } else {
254         p_buf->p_srvc_uuid = NULL;
255     }
256
257     bta_sys_sendmsg(p_buf);
258 }
259
260 /*******************************************************************************
261 **
262 ** Function         BTA_GATTC_GetServices
263 **
264 ** Description      This function is called to find the services on the given server.
265 **
266 ** Parameters       conn_id: connection ID which identify the server.
267 **
268 ** Returns          returns list_t of tBTA_GATTC_SERVICE or NULL.
269 **
270 *******************************************************************************/
271 const list_t* BTA_GATTC_GetServices(UINT16 conn_id) {
272     return bta_gattc_get_services(conn_id);
273 }
274
275 /*******************************************************************************
276 **
277 ** Function         BTA_GATTC_GetCharacteristic
278 **
279 ** Description      This function is called to find the characteristic on the given server.
280 **
281 ** Parameters       conn_id: connection ID which identify the server.
282 **                  handle: characteristic handle
283 **
284 ** Returns          returns pointer to tBTA_GATTC_CHARACTERISTIC or NULL.
285 **
286 *******************************************************************************/
287 const tBTA_GATTC_CHARACTERISTIC* BTA_GATTC_GetCharacteristic(UINT16 conn_id, UINT16 handle) {
288     return bta_gattc_get_characteristic(conn_id, handle);
289 }
290
291 /*******************************************************************************
292 **
293 ** Function         BTA_GATTC_GetCharacteristic
294 **
295 ** Description      This function is called to find the characteristic on the given server.
296 **
297 ** Parameters       conn_id: connection ID which identify the server.
298 **                  handle: descriptor handle
299 **
300 ** Returns          returns pointer to tBTA_GATTC_DESCRIPTOR or NULL.
301 **
302 *******************************************************************************/
303 const tBTA_GATTC_DESCRIPTOR* BTA_GATTC_GetDescriptor(UINT16 conn_id, UINT16 handle) {
304     return bta_gattc_get_descriptor(conn_id, handle);
305 }
306
307 /*******************************************************************************
308 **
309 ** Function         BTA_GATTC_GetGattDb
310 **
311 ** Description      This function is called to get the GATT database.
312 **
313 ** Parameters       conn_id: connection ID which identify the server.
314 **                  db: output parameter which will contain the GATT database copy.
315 **                      Caller is responsible for freeing it.
316 **                  count: number of elements in database.
317 **
318 *******************************************************************************/
319 void  BTA_GATTC_GetGattDb(UINT16 conn_id, UINT16 start_handle, UINT16 end_handle,
320                           btgatt_db_element_t **db, int *count)
321 {
322     bta_gattc_get_gatt_db(conn_id, start_handle, end_handle, db, count);
323 }
324
325 /*******************************************************************************
326 **
327 ** Function         BTA_GATTC_ReadCharacteristic
328 **
329 ** Description      This function is called to read a service's characteristics of
330 **                    the given characteritisc ID.
331 **
332 ** Parameters       conn_id - connectino ID.
333 **                    p_char_id - characteritic ID to read.
334 **
335 ** Returns          None
336 **
337 *******************************************************************************/
338 void BTA_GATTC_ReadCharacteristic(UINT16 conn_id, tBTA_GATTC_CHAR_ID *p_char_id,
339                                   tBTA_GATT_AUTH_REQ auth_req)
340 {
341     tBTA_GATTC_API_READ *p_buf =
342         (tBTA_GATTC_API_READ *)osi_calloc(sizeof(tBTA_GATTC_API_READ));
343
344     p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
345     p_buf->hdr.layer_specific = conn_id;
346     p_buf->auth_req = auth_req;
347
348     memcpy(&p_buf->srvc_id, &p_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
349     memcpy(&p_buf->char_id, &p_char_id->char_id, sizeof(tBTA_GATT_ID));
350     p_buf->p_descr_type = NULL;
351
352     bta_sys_sendmsg(p_buf);
353 }
354
355 /*******************************************************************************
356 **
357 ** Function         BTA_GATTC_ReadCharDescr
358 **
359 ** Description      This function is called to read a characteristics descriptor.
360 **
361 ** Parameters       conn_id - connection ID.
362 **                    p_char_descr_id - characteritic descriptor ID to read.
363 **
364 ** Returns          None
365 **
366 *******************************************************************************/
367 void BTA_GATTC_ReadCharDescr (UINT16 conn_id,
368                               tBTA_GATTC_CHAR_DESCR_ID  *p_descr_id,
369                               tBTA_GATT_AUTH_REQ auth_req)
370 {
371     const size_t len = sizeof(tBTA_GATT_ID) + sizeof(tBTA_GATTC_API_READ);
372     tBTA_GATTC_API_READ *p_buf = (tBTA_GATTC_API_READ *)osi_calloc(len);
373
374     p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
375     p_buf->hdr.layer_specific = conn_id;
376     p_buf->auth_req = auth_req;
377
378     memcpy(&p_buf->srvc_id, &p_descr_id->char_id.srvc_id, sizeof(tBTA_GATT_SRVC_ID));
379     memcpy(&p_buf->char_id, &p_descr_id->char_id.char_id, sizeof(tBTA_GATT_ID));
380     p_buf->p_descr_type  = (tBTA_GATT_ID *)(p_buf + 1);
381
382     memcpy(p_buf->p_descr_type, &p_descr_id->descr_id, sizeof(tBTA_GATT_ID));
383
384     bta_sys_sendmsg(p_buf);
385 }
386
387 /*******************************************************************************
388 **
389 ** Function         BTA_GATTC_ReadMultiple
390 **
391 ** Description      This function is called to read multiple characteristic or
392 **                  characteristic descriptors.
393 **
394 ** Parameters       conn_id - connectino ID.
395 **                    p_read_multi - pointer to the read multiple parameter.
396 **
397 ** Returns          None
398 **
399 *******************************************************************************/
400 void BTA_GATTC_ReadMultiple(UINT16 conn_id, tBTA_GATTC_MULTI *p_read_multi,
401                             tBTA_GATT_AUTH_REQ auth_req)
402 {
403     const size_t len = sizeof(tBTA_GATTC_API_READ_MULTI) +
404                         p_read_multi->num_attr * sizeof(tBTA_GATTC_ATTR_ID);
405     tBTA_GATTC_API_READ_MULTI *p_buf =
406         (tBTA_GATTC_API_READ_MULTI *)osi_calloc(len);
407
408     p_buf->hdr.event = BTA_GATTC_API_READ_MULTI_EVT;
409     p_buf->hdr.layer_specific = conn_id;
410     p_buf->auth_req = auth_req;
411     p_buf->num_attr = p_read_multi->num_attr;
412
413     if (p_buf->num_attr > 0) {
414         tBTA_GATTC_ATTR_ID *p_value;
415         p_buf->p_id_list = p_value = (tBTA_GATTC_ATTR_ID *)(p_buf + 1);
416         for (int i = 0; i < p_buf->num_attr; i++, p_value++) {
417             memcpy(p_value, &p_read_multi->id_list[i], sizeof(tBTA_GATTC_ATTR_ID));
418         }
419     }
420
421     bta_sys_sendmsg(p_buf);
422 }
423
424 /*******************************************************************************
425 **
426 ** Function         BTA_GATTC_WriteCharValue
427 **
428 ** Description      This function is called to write characteristic value.
429 **
430 ** Parameters       conn_id - connection ID.
431 **                    p_char_id - characteristic ID to write.
432 **                    write_type - type of write.
433 **                  len: length of the data to be written.
434 **                  p_value - the value to be written.
435 **
436 ** Returns          None
437 **
438 *******************************************************************************/
439 void BTA_GATTC_WriteCharValue ( UINT16 conn_id,
440                                 tBTA_GATTC_CHAR_ID *p_char_id,
441                                 tBTA_GATTC_WRITE_TYPE  write_type,
442                                 UINT16 len,
443                                 UINT8 *p_value,
444                                 tBTA_GATT_AUTH_REQ auth_req)
445 {
446     tBTA_GATTC_API_WRITE  *p_buf =
447         (tBTA_GATTC_API_WRITE *)osi_calloc(sizeof(tBTA_GATTC_API_WRITE) + len);
448
449     p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
450     p_buf->hdr.layer_specific = conn_id;
451     p_buf->auth_req = auth_req;
452
453     memcpy(&p_buf->srvc_id, &p_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
454     memcpy(&p_buf->char_id, &p_char_id->char_id, sizeof(tBTA_GATT_ID));
455     p_buf->p_descr_type = NULL;
456
457     p_buf->write_type = write_type;
458     p_buf->len = len;
459
460     if (p_value && len > 0) {
461         p_buf->p_value = (UINT8 *)(p_buf + 1);
462         memcpy(p_buf->p_value, p_value, len);
463     }
464
465     bta_sys_sendmsg(p_buf);
466 }
467
468 /*******************************************************************************
469 **
470 ** Function         BTA_GATTC_WriteCharDescr
471 **
472 ** Description      This function is called to write characteristic descriptor value.
473 **
474 ** Parameters       conn_id - connection ID
475 **                    p_char_descr_id - characteristic descriptor ID to write.
476 **                  write_type - write type.
477 **                  p_value - the value to be written.
478 **
479 ** Returns          None
480 **
481 *******************************************************************************/
482 void BTA_GATTC_WriteCharDescr (UINT16 conn_id,
483                                tBTA_GATTC_CHAR_DESCR_ID *p_char_descr_id,
484                                tBTA_GATTC_WRITE_TYPE  write_type,
485                                tBTA_GATT_UNFMT      *p_data,
486                                tBTA_GATT_AUTH_REQ auth_req)
487 {
488     size_t len = sizeof(tBTA_GATTC_API_WRITE) + sizeof(tBTA_GATT_ID);
489
490     if (p_data != NULL)
491         len += p_data->len;
492
493     tBTA_GATTC_API_WRITE *p_buf = (tBTA_GATTC_API_WRITE *)osi_calloc(len);
494     p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
495     p_buf->hdr.layer_specific = conn_id;
496     p_buf->auth_req = auth_req;
497
498     memcpy(&p_buf->srvc_id, &p_char_descr_id->char_id.srvc_id, sizeof(tBTA_GATT_SRVC_ID));
499     memcpy(&p_buf->char_id, &p_char_descr_id->char_id.char_id, sizeof(tBTA_GATT_ID));
500     p_buf->p_descr_type = (tBTA_GATT_ID *)(p_buf + 1);
501     memcpy(p_buf->p_descr_type, &p_char_descr_id->descr_id, sizeof(tBTA_GATT_ID));
502     p_buf->write_type = write_type;
503
504     if (p_data && p_data->len != 0) {
505         p_buf->p_value  = (UINT8 *)(p_buf->p_descr_type + 1);
506         p_buf->len      = p_data->len;
507         /* pack the descr data */
508         memcpy(p_buf->p_value, p_data->p_value, p_data->len);
509     }
510
511     bta_sys_sendmsg(p_buf);
512 }
513
514 /*******************************************************************************
515 **
516 ** Function         BTA_GATTC_PrepareWrite
517 **
518 ** Description      This function is called to prepare write a characteristic value.
519 **
520 ** Parameters       conn_id - connection ID.
521 **                    p_char_id - GATT characteritic ID of the service.
522 **                  offset - offset of the write value.
523 **                  len: length of the data to be written.
524 **                  p_value - the value to be written.
525 **
526 ** Returns          None
527 **
528 *******************************************************************************/
529 void BTA_GATTC_PrepareWrite  (UINT16 conn_id, tBTA_GATTC_CHAR_ID *p_char_id,
530                               UINT16 offset, UINT16 len, UINT8 *p_value,
531                               tBTA_GATT_AUTH_REQ auth_req)
532 {
533     tBTA_GATTC_API_WRITE *p_buf =
534         (tBTA_GATTC_API_WRITE *)osi_calloc(sizeof(tBTA_GATTC_API_WRITE) + len);
535
536     p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
537     p_buf->hdr.layer_specific = conn_id;
538     p_buf->auth_req = auth_req;
539
540     memcpy(&p_buf->srvc_id, &p_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
541     memcpy(&p_buf->char_id, &p_char_id->char_id, sizeof(tBTA_GATT_ID));
542     p_buf->p_descr_type = NULL;
543
544     p_buf->write_type = BTA_GATTC_WRITE_PREPARE;
545     p_buf->offset   = offset;
546     p_buf->len = len;
547
548     if (p_value && len > 0) {
549         p_buf->p_value = (UINT8 *)(p_buf + 1);
550         memcpy(p_buf->p_value, p_value, len);
551     }
552
553     bta_sys_sendmsg(p_buf);
554 }
555
556 /*******************************************************************************
557 **
558 ** Function         BTA_GATTC_ExecuteWrite
559 **
560 ** Description      This function is called to execute write a prepare write sequence.
561 **
562 ** Parameters       conn_id - connection ID.
563 **                    is_execute - execute or cancel.
564 **
565 ** Returns          None
566 **
567 *******************************************************************************/
568 void BTA_GATTC_ExecuteWrite  (UINT16 conn_id, BOOLEAN is_execute)
569 {
570     tBTA_GATTC_API_EXEC *p_buf =
571         (tBTA_GATTC_API_EXEC *)osi_calloc(sizeof(tBTA_GATTC_API_EXEC));
572
573     p_buf->hdr.event = BTA_GATTC_API_EXEC_EVT;
574     p_buf->hdr.layer_specific = conn_id;
575     p_buf->is_execute = is_execute;
576
577     bta_sys_sendmsg(p_buf);
578 }
579
580 /*******************************************************************************
581 **
582 ** Function         BTA_GATTC_SendIndConfirm
583 **
584 ** Description      This function is called to send handle value confirmation.
585 **
586 ** Parameters       conn_id - connection ID.
587 **                    p_char_id - characteristic ID to confirm.
588 **
589 ** Returns          None
590 **
591 *******************************************************************************/
592 void BTA_GATTC_SendIndConfirm (UINT16 conn_id, tBTA_GATTC_CHAR_ID *p_char_id)
593 {
594     tBTA_GATTC_API_CONFIRM *p_buf =
595         (tBTA_GATTC_API_CONFIRM *)osi_calloc(sizeof(tBTA_GATTC_API_CONFIRM));
596
597     APPL_TRACE_API("%s conn_id=%d service uuid1=0x%x char uuid=0x%x",
598                    __func__, conn_id, p_char_id->srvc_id.id.uuid.uu.uuid16,
599                    p_char_id->char_id.uuid.uu.uuid16);
600
601     p_buf->hdr.event = BTA_GATTC_API_CONFIRM_EVT;
602     p_buf->hdr.layer_specific = conn_id;
603
604     memcpy(&p_buf->srvc_id, &p_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
605     memcpy(&p_buf->char_id, &p_char_id->char_id, sizeof(tBTA_GATT_ID));
606
607     bta_sys_sendmsg(p_buf);
608 }
609
610 /*******************************************************************************
611 **
612 ** Function         BTA_GATTC_RegisterForNotifications
613 **
614 ** Description      This function is called to register for notification of a service.
615 **
616 ** Parameters       client_if - client interface.
617 **                  bda - target GATT server.
618 **                  p_char_id - pointer to GATT characteristic ID.
619 **
620 ** Returns          OK if registration succeed, otherwise failed.
621 **
622 *******************************************************************************/
623 tBTA_GATT_STATUS BTA_GATTC_RegisterForNotifications (tBTA_GATTC_IF client_if,
624                                                      BD_ADDR bda,
625                                                      tBTA_GATTC_CHAR_ID *p_char_id)
626 {
627     tBTA_GATTC_RCB      *p_clreg;
628     tBTA_GATT_STATUS    status = BTA_GATT_ILLEGAL_PARAMETER;
629     UINT8               i;
630
631     if (!p_char_id)
632     {
633         APPL_TRACE_ERROR("deregistration failed, unknow char id");
634         return status;
635     }
636
637     if ((p_clreg = bta_gattc_cl_get_regcb(client_if)) != NULL)
638     {
639         for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
640         {
641             if ( p_clreg->notif_reg[i].in_use &&
642                  !memcmp(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN) &&
643                   bta_gattc_charid_compare(&p_clreg->notif_reg[i].char_id, p_char_id))
644             {
645                 APPL_TRACE_WARNING("notification already registered");
646                 status = BTA_GATT_OK;
647                 break;
648             }
649         }
650         if (status != BTA_GATT_OK)
651         {
652             for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
653             {
654                 if (!p_clreg->notif_reg[i].in_use)
655                 {
656                     memset((void *)&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
657
658                     p_clreg->notif_reg[i].in_use = TRUE;
659                     memcpy(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN);
660
661                     p_clreg->notif_reg[i].char_id.srvc_id.is_primary = p_char_id->srvc_id.is_primary;
662                     bta_gattc_cpygattid(&p_clreg->notif_reg[i].char_id.srvc_id.id, &p_char_id->srvc_id.id);
663                     bta_gattc_cpygattid(&p_clreg->notif_reg[i].char_id.char_id, &p_char_id->char_id);
664
665                     status = BTA_GATT_OK;
666                     break;
667                 }
668             }
669             if (i == BTA_GATTC_NOTIF_REG_MAX)
670             {
671                 status = BTA_GATT_NO_RESOURCES;
672                 APPL_TRACE_ERROR("Max Notification Reached, registration failed.");
673             }
674         }
675     }
676     else
677     {
678         APPL_TRACE_ERROR("Client_if: %d Not Registered", client_if);
679     }
680
681     return status;
682 }
683
684 /*******************************************************************************
685 **
686 ** Function         BTA_GATTC_DeregisterForNotifications
687 **
688 ** Description      This function is called to de-register for notification of a service.
689 **
690 ** Parameters       client_if - client interface.
691 **                  bda - target GATT server.
692 **                  p_char_id - pointer to GATT characteristic ID.
693 **
694 ** Returns          OK if deregistration succeed, otherwise failed.
695 **
696 *******************************************************************************/
697 tBTA_GATT_STATUS BTA_GATTC_DeregisterForNotifications (tBTA_GATTC_IF client_if,
698                                                        BD_ADDR bda,
699                                                        tBTA_GATTC_CHAR_ID *p_char_id)
700 {
701     tBTA_GATTC_RCB      *p_clreg;
702     tBTA_GATT_STATUS    status = BTA_GATT_ILLEGAL_PARAMETER;
703     UINT8               i;
704
705     if (!p_char_id)
706     {
707         APPL_TRACE_ERROR("%s deregistration failed, unknown char id", __func__);
708         return status;
709     }
710
711     if ((p_clreg = bta_gattc_cl_get_regcb(client_if)) != NULL)
712     {
713         for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
714         {
715             if (p_clreg->notif_reg[i].in_use &&
716                 !memcmp(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN) &&
717                 bta_gattc_charid_compare(&p_clreg->notif_reg[i].char_id, p_char_id))
718             {
719                 APPL_TRACE_DEBUG("%s deregistered bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
720                     __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
721                 memset(&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
722                 status = BTA_GATT_OK;
723                 break;
724             }
725         }
726         if (i == BTA_GATTC_NOTIF_REG_MAX)
727         {
728             status = BTA_GATT_ERROR;
729             APPL_TRACE_ERROR("%s registration not found bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
730                 __func__, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
731         }
732     }
733     else
734     {
735         APPL_TRACE_ERROR("%s client_if: %d not registered bd_addr:%02x:%02x:%02x:%02x:%02x:%02x",
736             __func__, client_if, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
737     }
738
739     return status;
740 }
741
742 /*******************************************************************************
743 **
744 ** Function         BTA_GATTC_Refresh
745 **
746 ** Description      Refresh the server cache of the remote device
747 **
748 ** Parameters       remote_bda: remote device BD address.
749 **
750 ** Returns          void
751 **
752 *******************************************************************************/
753 void BTA_GATTC_Refresh(BD_ADDR remote_bda)
754 {
755     tBTA_GATTC_API_OPEN *p_buf =
756         (tBTA_GATTC_API_OPEN *)osi_malloc(sizeof(tBTA_GATTC_API_OPEN));
757
758     p_buf->hdr.event = BTA_GATTC_API_REFRESH_EVT;
759     memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
760
761     bta_sys_sendmsg(p_buf);
762 }
763
764 /*******************************************************************************
765 **
766 ** Function         BTA_GATTC_Listen
767 **
768 ** Description      Start advertisement to listen for connection request for a GATT
769 **                  client application.
770 **
771 ** Parameters       client_if: server interface.
772 **                  start: to start or stop listening for connection
773 **                  remote_bda: remote device BD address, if listen to all device
774 **                              use NULL.
775 **
776 ** Returns          void
777 **
778 *******************************************************************************/
779 void BTA_GATTC_Listen(tBTA_GATTC_IF client_if, BOOLEAN start, BD_ADDR_PTR target_bda)
780 {
781     tBTA_GATTC_API_LISTEN *p_buf =
782         (tBTA_GATTC_API_LISTEN *)osi_malloc(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN);
783
784     p_buf->hdr.event = BTA_GATTC_API_LISTEN_EVT;
785     p_buf->client_if = client_if;
786     p_buf->start = start;
787     if (target_bda) {
788         p_buf->remote_bda = (UINT8*)(p_buf + 1);
789         memcpy(p_buf->remote_bda, target_bda, BD_ADDR_LEN);
790     } else {
791         p_buf->remote_bda = NULL;
792     }
793
794     bta_sys_sendmsg(p_buf);
795 }
796
797 /*******************************************************************************
798 **
799 ** Function         BTA_GATTC_Broadcast
800 **
801 ** Description      Start broadcasting (non-connectable advertisements)
802 **
803 ** Parameters       client_if: client interface.
804 **                  start: to start or stop listening for connection
805 **
806 ** Returns          void
807 **
808 *******************************************************************************/
809 void BTA_GATTC_Broadcast(tBTA_GATTC_IF client_if, BOOLEAN start)
810 {
811     tBTA_GATTC_API_LISTEN *p_buf =
812         (tBTA_GATTC_API_LISTEN *)osi_malloc(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN);
813
814     p_buf->hdr.event = BTA_GATTC_API_BROADCAST_EVT;
815     p_buf->client_if = client_if;
816     p_buf->start = start;
817
818     bta_sys_sendmsg(p_buf);
819 }
820
821 #endif /* BTA_GATT_INCLUDED */