bta_gattc_api.c revision 9426d530e9bbc5d3ffae55515388d49185c61325
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 "gki.h"
31#include "bta_sys.h"
32#include "bta_gatt_api.h"
33#include "bta_gattc_int.h"
34
35/*****************************************************************************
36**  Constants
37*****************************************************************************/
38
39static 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*******************************************************************************/
57void BTA_GATTC_Disable(void)
58{
59    BT_HDR  *p_buf;
60
61    if (bta_sys_is_register(BTA_ID_GATTC) == FALSE)
62    {
63        APPL_TRACE_WARNING0("GATTC Module not enabled/already disabled");
64        return;
65    }
66    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
67    {
68        p_buf->event = BTA_GATTC_API_DISABLE_EVT;
69        bta_sys_sendmsg(p_buf);
70    }
71    bta_sys_deregister(BTA_ID_GATTC);
72
73}
74
75/*******************************************************************************
76**
77** Function         BTA_GATTC_AppRegister
78**
79** Description      This function is called to register application callbacks
80**                    with BTA GATTC module.
81**
82** Parameters       p_app_uuid - applicaiton UUID
83**                  p_client_cb - pointer to the application callback function.
84**
85** Returns          None
86**
87*******************************************************************************/
88void BTA_GATTC_AppRegister(tBT_UUID *p_app_uuid, tBTA_GATTC_CBACK *p_client_cb)
89{
90    tBTA_GATTC_API_REG  *p_buf;
91
92    if (bta_sys_is_register(BTA_ID_GATTC) == FALSE)
93    {
94        GKI_sched_lock();
95        bta_sys_register(BTA_ID_GATTC, &bta_gattc_reg);
96        GKI_sched_unlock();
97    }
98
99    if ((p_buf = (tBTA_GATTC_API_REG *) GKI_getbuf(sizeof(tBTA_GATTC_API_REG))) != NULL)
100    {
101        p_buf->hdr.event    = BTA_GATTC_API_REG_EVT;
102        if (p_app_uuid != NULL)
103            memcpy(&p_buf->app_uuid, p_app_uuid, sizeof(tBT_UUID));
104        p_buf->p_cback      = p_client_cb;
105
106        bta_sys_sendmsg(p_buf);
107    }
108    return;
109}
110
111/*******************************************************************************
112**
113** Function         BTA_GATTC_AppDeregister
114**
115** Description      This function is called to deregister an application
116**                  from BTA GATTC module.
117**
118** Parameters       client_if - client interface identifier.
119**
120** Returns          None
121**
122*******************************************************************************/
123void BTA_GATTC_AppDeregister(tBTA_GATTC_IF client_if)
124{
125    tBTA_GATTC_API_DEREG  *p_buf;
126
127    if ((p_buf = (tBTA_GATTC_API_DEREG *) GKI_getbuf(sizeof(tBTA_GATTC_API_DEREG))) != NULL)
128    {
129        p_buf->hdr.event = BTA_GATTC_API_DEREG_EVT;
130        p_buf->client_if = client_if;
131        bta_sys_sendmsg(p_buf);
132    }
133    return;
134}
135
136/*******************************************************************************
137**
138** Function         BTA_GATTC_Open
139**
140** Description      Open a direct connection or add a background auto connection
141**                  bd address
142**
143** Parameters       client_if: server interface.
144**                  remote_bda: remote device BD address.
145**                  is_direct: direct connection or background auto connection
146**
147** Returns          void
148**
149*******************************************************************************/
150void BTA_GATTC_Open(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, BOOLEAN is_direct)
151{
152    tBTA_GATTC_API_OPEN  *p_buf;
153
154    if ((p_buf = (tBTA_GATTC_API_OPEN *) GKI_getbuf(sizeof(tBTA_GATTC_API_OPEN))) != NULL)
155    {
156        p_buf->hdr.event = BTA_GATTC_API_OPEN_EVT;
157
158        p_buf->client_if = client_if;
159        p_buf->is_direct = is_direct;
160        memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
161
162
163        bta_sys_sendmsg(p_buf);
164    }
165    return;
166}
167
168/*******************************************************************************
169**
170** Function         BTA_GATTC_CancelOpen
171**
172** Description      Cancel a direct open connection or remove a background auto connection
173**                  bd address
174**
175** Parameters       client_if: server interface.
176**                  remote_bda: remote device BD address.
177**                  is_direct: direct connection or background auto connection
178**
179** Returns          void
180**
181*******************************************************************************/
182void BTA_GATTC_CancelOpen(tBTA_GATTC_IF client_if, BD_ADDR remote_bda, BOOLEAN is_direct)
183{
184    tBTA_GATTC_API_CANCEL_OPEN  *p_buf;
185
186    if ((p_buf = (tBTA_GATTC_API_CANCEL_OPEN *) GKI_getbuf(sizeof(tBTA_GATTC_API_CANCEL_OPEN))) != NULL)
187    {
188        p_buf->hdr.event = BTA_GATTC_API_CANCEL_OPEN_EVT;
189
190        p_buf->client_if = client_if;
191        p_buf->is_direct = is_direct;
192        memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
193
194        bta_sys_sendmsg(p_buf);
195    }
196    return;
197}
198
199/*******************************************************************************
200**
201** Function         BTA_GATTC_Close
202**
203** Description      Close a connection to a GATT server.
204**
205** Parameters       conn_id: connectino ID to be closed.
206**
207** Returns          void
208**
209*******************************************************************************/
210void BTA_GATTC_Close(UINT16 conn_id)
211{
212    BT_HDR  *p_buf;
213
214    if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
215    {
216        p_buf->event = BTA_GATTC_API_CLOSE_EVT;
217
218        p_buf->layer_specific = conn_id;
219
220        bta_sys_sendmsg(p_buf);
221    }
222    return;
223
224}
225/*******************************************************************************
226**
227** Function         BTA_GATTC_ConfigureMTU
228**
229** Description      Configure the MTU size in the GATT channel. This can be done
230**                  only once per connection.
231**
232** Parameters       conn_id: connection ID.
233**                  mtu: desired MTU size to use.
234**
235** Returns          void
236**
237*******************************************************************************/
238void BTA_GATTC_ConfigureMTU (UINT16 conn_id, UINT16 mtu)
239{
240    tBTA_GATTC_API_CFG_MTU  *p_buf;
241
242    if ((p_buf = (tBTA_GATTC_API_CFG_MTU *) GKI_getbuf(sizeof(tBTA_GATTC_API_CFG_MTU))) != NULL)
243    {
244        p_buf->hdr.event = BTA_GATTC_API_CFG_MTU_EVT;
245        p_buf->hdr.layer_specific = conn_id;
246
247        p_buf->mtu = mtu;
248
249        bta_sys_sendmsg(p_buf);
250    }
251    return;
252}
253/*******************************************************************************
254**
255** Function         BTA_GATTC_ServiceSearchRequest
256**
257** Description      This function is called to request a GATT service discovery
258**                    on a GATT server. This function report service search result
259**                  by a callback event, and followed by a service search complete
260**                  event.
261**
262** Parameters       conn_id: connection ID.
263**                  p_srvc_uuid: a UUID of the service application is interested in.
264**                              If Null, discover for all services.
265**
266** Returns          None
267**
268*******************************************************************************/
269void BTA_GATTC_ServiceSearchRequest (UINT16 conn_id, tBT_UUID *p_srvc_uuid)
270{
271    tBTA_GATTC_API_SEARCH  *p_buf;
272    UINT16  len = sizeof(tBTA_GATTC_API_SEARCH) + sizeof(tBT_UUID);
273
274    if ((p_buf = (tBTA_GATTC_API_SEARCH *) GKI_getbuf(len)) != NULL)
275    {
276        memset(p_buf, 0, len);
277
278        p_buf->hdr.event = BTA_GATTC_API_SEARCH_EVT;
279        p_buf->hdr.layer_specific = conn_id;
280
281        if (p_srvc_uuid)
282        {
283            p_buf->p_srvc_uuid = (tBT_UUID *)(p_buf + 1);
284            memcpy(p_buf->p_srvc_uuid, p_srvc_uuid, sizeof(tBT_UUID));
285        }
286        else
287            p_buf->p_srvc_uuid = NULL;
288
289        bta_sys_sendmsg(p_buf);
290    }
291    return;
292}
293
294
295/*******************************************************************************
296**
297** Function         BTA_GATTC_GetFirstChar
298**
299** Description      This function is called to find the first characteristic of the
300**                  service on the given server.
301**
302** Parameters       conn_id: connection ID which identify the server.
303**                  p_srvc_id: the service ID of which the characteristic is belonged to.
304**                  p_char_uuid_cond: Characteristic UUID, if NULL find the first available
305**                               characteristic.
306**                  p_char_result: output parameter which will store the GATT
307**                                  characteristic ID.
308**                  p_property: output parameter to carry the characteristic property.
309**
310** Returns          returns status.
311**
312*******************************************************************************/
313tBTA_GATT_STATUS  BTA_GATTC_GetFirstChar (UINT16 conn_id, tBTA_GATT_SRVC_ID *p_srvc_id,
314                                          tBT_UUID *p_char_uuid_cond,
315                                          tBTA_GATTC_CHAR_ID *p_char_result,
316                                          tBTA_GATT_CHAR_PROP *p_property)
317{
318    tBTA_GATT_STATUS    status;
319
320    if (!p_srvc_id || !p_char_result)
321        return BTA_GATT_ILLEGAL_PARAMETER;
322
323    if ((status = bta_gattc_query_cache(conn_id, BTA_GATTC_ATTR_TYPE_CHAR, p_srvc_id, NULL,
324                                        p_char_uuid_cond, &p_char_result->char_id, (void *)p_property))
325        == BTA_GATT_OK)
326    {
327        memcpy(&p_char_result->srvc_id, p_srvc_id, sizeof(tBTA_GATT_SRVC_ID));
328    }
329
330    return status;
331
332}
333/*******************************************************************************
334**
335** Function         BTA_GATTC_GetNextChar
336**
337** Description      This function is called to find the next characteristic of the
338**                  service on the given server.
339**
340** Parameters       conn_id: connection ID which identify the server.
341**                  p_start_char_id: start the characteristic search from the next record
342**                           after the one identified by char_id.
343**                  p_char_uuid_cond: Characteristic UUID, if NULL find the first available
344**                               characteristic.
345**                  p_char_result: output parameter which will store the GATT
346**                                  characteristic ID.
347**                  p_property: output parameter to carry the characteristic property.
348**
349** Returns          returns status.
350**
351*******************************************************************************/
352tBTA_GATT_STATUS  BTA_GATTC_GetNextChar (UINT16 conn_id,
353                                         tBTA_GATTC_CHAR_ID *p_start_char_id,
354                                         tBT_UUID           *p_char_uuid_cond,
355                                         tBTA_GATTC_CHAR_ID *p_char_result,
356                                         tBTA_GATT_CHAR_PROP    *p_property)
357{
358    tBTA_GATT_STATUS    status;
359
360    if (!p_start_char_id || !p_char_result)
361        return BTA_GATT_ILLEGAL_PARAMETER;
362
363    if ((status = bta_gattc_query_cache(conn_id, BTA_GATTC_ATTR_TYPE_CHAR,
364                                        &p_start_char_id->srvc_id,
365                                        &p_start_char_id->char_id,
366                                        p_char_uuid_cond,
367                                        &p_char_result->char_id,
368                                        (void *) p_property))
369        == BTA_GATT_OK)
370    {
371        memcpy(&p_char_result->srvc_id, &p_start_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
372    }
373
374    return status;
375}
376
377/*******************************************************************************
378**
379** Function         BTA_GATTC_GetFirstCharDescr
380**
381** Description      This function is called to find the first characteristic descriptor of the
382**                  characteristic on the given server.
383**
384** Parameters       conn_id: connection ID which identify the server.
385**                  p_char_id: the characteristic ID of which the descriptor is belonged to.
386**                  p_descr_uuid_cond: Characteristic Descr UUID, if NULL find the first available
387**                               characteristic.
388**                  p_descr_result: output parameter which will store the GATT
389**                                  characteristic descriptor ID.
390**
391** Returns          returns status.
392**
393*******************************************************************************/
394tBTA_GATT_STATUS  BTA_GATTC_GetFirstCharDescr (UINT16 conn_id, tBTA_GATTC_CHAR_ID *p_char_id,
395                                                tBT_UUID *p_descr_uuid_cond,
396                                                tBTA_GATTC_CHAR_DESCR_ID *p_descr_result)
397{
398    tBTA_GATT_STATUS    status;
399
400    if (!p_char_id || !p_descr_result)
401        return BTA_GATT_ILLEGAL_PARAMETER;
402
403    memset(p_descr_result, 0, sizeof(tBTA_GATTC_CHAR_DESCR_ID));
404
405    if ((status = bta_gattc_query_cache(conn_id,
406                                        BTA_GATTC_ATTR_TYPE_CHAR_DESCR,
407                                        &p_char_id->srvc_id,
408                                        &p_char_id->char_id,
409                                        p_descr_uuid_cond,
410                                        &p_descr_result->char_id.char_id,
411                                        NULL))
412        == BTA_GATT_OK)
413    {
414        memcpy(&p_descr_result->descr_id, &p_descr_result->char_id.char_id, sizeof(tBTA_GATT_ID));
415        memcpy(&p_descr_result->char_id, p_char_id, sizeof(tBTA_GATTC_CHAR_ID));
416    }
417
418    return status;
419
420}
421/*******************************************************************************
422**
423** Function         BTA_GATTC_GetNextCharDescr
424**
425** Description      This function is called to find the next characteristic descriptor
426**                  of the characterisctic.
427**
428** Parameters       conn_id: connection ID which identify the server.
429**                  p_start_descr_id: start the characteristic search from the next record
430**                           after the one identified by p_start_descr_id.
431**                  p_descr_uuid_cond: Characteristic descriptor UUID, if NULL find
432**                               the first available characteristic descriptor.
433**                  p_descr_result: output parameter which will store the GATT
434**                                  characteristic descriptor ID.
435**
436** Returns          returns status.
437**
438*******************************************************************************/
439tBTA_GATT_STATUS  BTA_GATTC_GetNextCharDescr (UINT16 conn_id,
440                                             tBTA_GATTC_CHAR_DESCR_ID *p_start_descr_id,
441                                             tBT_UUID           *p_descr_uuid_cond,
442                                             tBTA_GATTC_CHAR_DESCR_ID *p_descr_result)
443{
444    tBTA_GATT_STATUS    status;
445
446    if (!p_start_descr_id || !p_descr_result)
447        return BTA_GATT_ILLEGAL_PARAMETER;
448
449    memset(p_descr_result, 0, sizeof(tBTA_GATTC_CHAR_DESCR_ID));
450
451    if ((status = bta_gattc_query_cache(conn_id, BTA_GATTC_ATTR_TYPE_CHAR_DESCR,
452                                        &p_start_descr_id->char_id.srvc_id,
453                                        &p_start_descr_id->char_id.char_id,
454                                        p_descr_uuid_cond,
455                                        &p_descr_result->char_id.char_id,
456                                        (void *)&p_start_descr_id->descr_id))
457        == BTA_GATT_OK)
458    {
459        memcpy(&p_descr_result->descr_id, &p_descr_result->char_id.char_id, sizeof(tBTA_GATT_ID));
460        memcpy(&p_descr_result->char_id, p_start_descr_id, sizeof(tBTA_GATTC_CHAR_ID));
461    }
462
463    return status;
464}
465
466
467/*******************************************************************************
468**
469** Function         BTA_GATTC_GetFirstIncludedService
470**
471** Description      This function is called to find the first included service of the
472**                  service on the given server.
473**
474** Parameters       conn_id: connection ID which identify the server.
475**                  p_srvc_id: the service ID of which the characteristic is belonged to.
476**                  p_uuid_cond: Characteristic UUID, if NULL find the first available
477**                               characteristic.
478**                  p_result: output parameter which will store the GATT ID
479**                              of the included service found.
480**
481** Returns          returns status.
482**
483*******************************************************************************/
484tBTA_GATT_STATUS  BTA_GATTC_GetFirstIncludedService(UINT16 conn_id, tBTA_GATT_SRVC_ID *p_srvc_id,
485                                                    tBT_UUID *p_uuid_cond, tBTA_GATTC_INCL_SVC_ID *p_result)
486{
487    tBTA_GATT_STATUS    status;
488
489    if (!p_srvc_id || !p_result)
490        return BTA_GATT_ILLEGAL_PARAMETER;
491
492    if ((status = bta_gattc_query_cache(conn_id,
493                                        BTA_GATTC_ATTR_TYPE_INCL_SRVC,
494                                        p_srvc_id,
495                                        NULL,
496                                        p_uuid_cond,
497                                        &p_result->incl_svc_id.id,
498                                        (void *)&p_result->incl_svc_id.is_primary))
499        == BTA_GATT_OK)
500    {
501        memcpy(&p_result->srvc_id, p_srvc_id, sizeof(tBTA_GATT_SRVC_ID));
502    }
503
504    return status;
505}
506/*******************************************************************************
507**
508** Function         BTA_GATTC_GetNextIncludedService
509**
510** Description      This function is called to find the next included service of the
511**                  service on the given server.
512**
513** Parameters       conn_id: connection ID which identify the server.
514**                  p_start_id: start the search from the next record
515**                                  after the one identified by p_start_id.
516**                  p_uuid_cond: Included service UUID, if NULL find the first available
517**                               included service.
518**                  p_result: output parameter which will store the GATT ID
519**                              of the included service found.
520**
521** Returns          returns status.
522**
523*******************************************************************************/
524tBTA_GATT_STATUS  BTA_GATTC_GetNextIncludedService(UINT16 conn_id,
525                                                   tBTA_GATTC_INCL_SVC_ID *p_start_id,
526                                                   tBT_UUID               *p_uuid_cond,
527                                                   tBTA_GATTC_INCL_SVC_ID *p_result)
528{
529    tBTA_GATT_STATUS    status;
530
531    if (!p_start_id || !p_result)
532        return BTA_GATT_ILLEGAL_PARAMETER;
533
534    if ((status = bta_gattc_query_cache(conn_id,
535                                        BTA_GATTC_ATTR_TYPE_INCL_SRVC,
536                                        &p_start_id->srvc_id,
537                                        &p_start_id->incl_svc_id.id,
538                                        p_uuid_cond,
539                                        &p_result->incl_svc_id.id,
540                                        (void *)&p_result->incl_svc_id.is_primary))
541        == BTA_GATT_OK)
542    {
543        memcpy(&p_result->srvc_id, &p_start_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
544    }
545
546    return status;
547}
548
549/*******************************************************************************
550**
551** Function         BTA_GATTC_ReadCharacteristic
552**
553** Description      This function is called to read a service's characteristics of
554**                    the given characteritisc ID.
555**
556** Parameters       conn_id - connectino ID.
557**                    p_char_id - characteritic ID to read.
558**
559** Returns          None
560**
561*******************************************************************************/
562void BTA_GATTC_ReadCharacteristic(UINT16 conn_id, tBTA_GATTC_CHAR_ID *p_char_id,
563                                  tBTA_GATT_AUTH_REQ auth_req)
564{
565    tBTA_GATTC_API_READ  *p_buf;
566
567    if ((p_buf = (tBTA_GATTC_API_READ *) GKI_getbuf(sizeof(tBTA_GATTC_API_READ))) != NULL)
568    {
569        memset(p_buf, 0, sizeof(tBTA_GATTC_API_READ));
570
571        p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
572        p_buf->hdr.layer_specific = conn_id;
573        p_buf->auth_req = auth_req;
574
575        memcpy(&p_buf->srvc_id, &p_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
576        memcpy(&p_buf->char_id, &p_char_id->char_id, sizeof(tBTA_GATT_ID));
577        p_buf->p_descr_type = NULL;
578
579        bta_sys_sendmsg(p_buf);
580    }
581    return;
582}
583
584/*******************************************************************************
585**
586** Function         BTA_GATTC_ReadCharDescr
587**
588** Description      This function is called to read a characteristics descriptor.
589**
590** Parameters       conn_id - connection ID.
591**                    p_char_descr_id - characteritic descriptor ID to read.
592**
593** Returns          None
594**
595*******************************************************************************/
596void BTA_GATTC_ReadCharDescr (UINT16 conn_id,
597                              tBTA_GATTC_CHAR_DESCR_ID  *p_descr_id,
598                              tBTA_GATT_AUTH_REQ auth_req)
599{
600    tBTA_GATTC_API_READ  *p_buf;
601    UINT16  len = (UINT16)(sizeof(tBTA_GATT_ID) + sizeof(tBTA_GATTC_API_READ));
602
603    if ((p_buf = (tBTA_GATTC_API_READ *) GKI_getbuf(len)) != NULL)
604    {
605        memset(p_buf, 0, sizeof(tBTA_GATTC_API_READ));
606
607        p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
608        p_buf->hdr.layer_specific = conn_id;
609        p_buf->auth_req = auth_req;
610
611        memcpy(&p_buf->srvc_id, &p_descr_id->char_id.srvc_id, sizeof(tBTA_GATT_SRVC_ID));
612        memcpy(&p_buf->char_id, &p_descr_id->char_id.char_id, sizeof(tBTA_GATT_ID));
613        p_buf->p_descr_type  = (tBTA_GATT_ID *)(p_buf + 1);
614
615        memcpy(p_buf->p_descr_type, &p_descr_id->descr_id, sizeof(tBTA_GATT_ID));
616
617        bta_sys_sendmsg(p_buf);
618    }
619    return;
620
621}
622/*******************************************************************************
623**
624** Function         BTA_GATTC_ReadMultiple
625**
626** Description      This function is called to read multiple characteristic or
627**                  characteristic descriptors.
628**
629** Parameters       conn_id - connectino ID.
630**                    p_read_multi - pointer to the read multiple parameter.
631**
632** Returns          None
633**
634*******************************************************************************/
635void BTA_GATTC_ReadMultiple(UINT16 conn_id, tBTA_GATTC_MULTI *p_read_multi,
636                            tBTA_GATT_AUTH_REQ auth_req)
637{
638    tBTA_GATTC_API_READ_MULTI  *p_buf;
639    tBTA_GATTC_ATTR_ID          *p_value;
640    UINT16      len = (UINT16)(sizeof(tBTA_GATTC_API_READ_MULTI) +
641                               p_read_multi->num_attr * sizeof(tBTA_GATTC_ATTR_ID));
642    UINT8       i;
643
644    if ((p_buf = (tBTA_GATTC_API_READ_MULTI *) GKI_getbuf(len)) != NULL)
645    {
646        memset(p_buf, 0, len);
647
648        p_buf->hdr.event = BTA_GATTC_API_READ_MULTI_EVT;
649        p_buf->hdr.layer_specific = conn_id;
650        p_buf->auth_req = auth_req;
651
652        p_buf->num_attr = p_read_multi->num_attr;
653
654        if (p_buf->num_attr > 0)
655        {
656            p_buf->p_id_list = p_value = (tBTA_GATTC_ATTR_ID *)(p_buf + 1);
657
658            for (i = 0; i < p_buf->num_attr; i ++, p_value ++)
659            {
660                memcpy(p_value, &p_read_multi->id_list[i], sizeof(tBTA_GATTC_ATTR_ID));
661            }
662        }
663        bta_sys_sendmsg(p_buf);
664    }
665    return;
666}
667
668
669/*******************************************************************************
670**
671** Function         BTA_GATTC_WriteCharValue
672**
673** Description      This function is called to write characteristic value.
674**
675** Parameters       conn_id - connection ID.
676**                    p_char_id - characteristic ID to write.
677**                    write_type - type of write.
678**                  len: length of the data to be written.
679**                  p_value - the value to be written.
680**
681** Returns          None
682**
683*******************************************************************************/
684void BTA_GATTC_WriteCharValue ( UINT16 conn_id,
685                                tBTA_GATTC_CHAR_ID *p_char_id,
686                                tBTA_GATTC_WRITE_TYPE  write_type,
687                                UINT16 len,
688                                UINT8 *p_value,
689                                tBTA_GATT_AUTH_REQ auth_req)
690{
691    tBTA_GATTC_API_WRITE  *p_buf;
692
693    if ((p_buf = (tBTA_GATTC_API_WRITE *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTC_API_WRITE) + len))) != NULL)
694    {
695        memset(p_buf, 0, sizeof(tBTA_GATTC_API_WRITE) + len);
696
697        p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
698        p_buf->hdr.layer_specific = conn_id;
699        p_buf->auth_req = auth_req;
700
701        memcpy(&p_buf->srvc_id, &p_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
702        memcpy(&p_buf->char_id, &p_char_id->char_id, sizeof(tBTA_GATT_ID));
703
704        p_buf->write_type = write_type;
705        p_buf->len = len;
706
707        if (p_value && len > 0)
708        {
709            p_buf->p_value = (UINT8 *)(p_buf + 1);
710            memcpy(p_buf->p_value, p_value, len);
711        }
712
713        bta_sys_sendmsg(p_buf);
714    }
715    return;
716}
717/*******************************************************************************
718**
719** Function         BTA_GATTC_WriteCharDescr
720**
721** Description      This function is called to write characteristic descriptor value.
722**
723** Parameters       conn_id - connection ID
724**                    p_char_descr_id - characteristic descriptor ID to write.
725**                  write_type - write type.
726**                  p_value - the value to be written.
727**
728** Returns          None
729**
730*******************************************************************************/
731void BTA_GATTC_WriteCharDescr (UINT16 conn_id,
732                               tBTA_GATTC_CHAR_DESCR_ID *p_char_descr_id,
733                               tBTA_GATTC_WRITE_TYPE  write_type,
734                               tBTA_GATT_UNFMT      *p_data,
735                               tBTA_GATT_AUTH_REQ auth_req)
736{
737    tBTA_GATTC_API_WRITE  *p_buf;
738    UINT16  len = sizeof(tBTA_GATTC_API_WRITE) + sizeof(tBTA_GATT_ID);
739
740    if (p_data != NULL)
741        len += p_data->len;
742
743    if ((p_buf = (tBTA_GATTC_API_WRITE *) GKI_getbuf(len)) != NULL)
744    {
745        memset(p_buf, 0, len);
746
747        p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
748        p_buf->hdr.layer_specific = conn_id;
749        p_buf->auth_req = auth_req;
750
751        memcpy(&p_buf->srvc_id, &p_char_descr_id->char_id.srvc_id, sizeof(tBTA_GATT_SRVC_ID));
752        memcpy(&p_buf->char_id, &p_char_descr_id->char_id.char_id, sizeof(tBTA_GATT_ID));
753        p_buf->p_descr_type = (tBTA_GATT_ID *)(p_buf + 1);
754        memcpy(p_buf->p_descr_type, &p_char_descr_id->descr_id, sizeof(tBTA_GATT_ID));
755        p_buf->write_type = write_type;
756
757        if (p_data && p_data->len != 0)
758        {
759            p_buf->p_value  = (UINT8 *)(p_buf->p_descr_type + 1);
760            p_buf->len      = p_data->len;
761            /* pack the descr data */
762            memcpy(p_buf->p_value, p_data->p_value, p_data->len);
763        }
764
765        bta_sys_sendmsg(p_buf);
766    }
767    return;
768
769}
770/*******************************************************************************
771**
772** Function         BTA_GATTC_PrepareWrite
773**
774** Description      This function is called to prepare write a characteristic value.
775**
776** Parameters       conn_id - connection ID.
777**                    p_char_id - GATT characteritic ID of the service.
778**                  offset - offset of the write value.
779**                  len: length of the data to be written.
780**                  p_value - the value to be written.
781**
782** Returns          None
783**
784*******************************************************************************/
785void BTA_GATTC_PrepareWrite  (UINT16 conn_id, tBTA_GATTC_CHAR_ID *p_char_id,
786                              UINT16 offset, UINT16 len, UINT8 *p_value,
787                              tBTA_GATT_AUTH_REQ auth_req)
788{
789    tBTA_GATTC_API_WRITE  *p_buf;
790
791    if ((p_buf = (tBTA_GATTC_API_WRITE *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTC_API_WRITE) + len))) != NULL)
792    {
793        memset(p_buf, 0, sizeof(tBTA_GATTC_API_WRITE) + len);
794
795        p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
796        p_buf->hdr.layer_specific = conn_id;
797        p_buf->auth_req = auth_req;
798
799        memcpy(&p_buf->srvc_id, &p_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
800        memcpy(&p_buf->char_id, &p_char_id->char_id, sizeof(tBTA_GATT_ID));
801
802        p_buf->write_type = BTA_GATTC_WRITE_PREPARE;
803        p_buf->offset   = offset;
804        p_buf->len = len;
805
806        if (p_value && len > 0)
807        {
808            p_buf->p_value = (UINT8 *)(p_buf + 1);
809            memcpy(p_buf->p_value, p_value, len);
810        }
811
812        bta_sys_sendmsg(p_buf);
813    }
814    return;
815
816}
817/*******************************************************************************
818**
819** Function         BTA_GATTC_ExecuteWrite
820**
821** Description      This function is called to execute write a prepare write sequence.
822**
823** Parameters       conn_id - connection ID.
824**                    is_execute - execute or cancel.
825**
826** Returns          None
827**
828*******************************************************************************/
829void BTA_GATTC_ExecuteWrite  (UINT16 conn_id, BOOLEAN is_execute)
830{
831    tBTA_GATTC_API_EXEC  *p_buf;
832
833    if ((p_buf = (tBTA_GATTC_API_EXEC *) GKI_getbuf((UINT16)sizeof(tBTA_GATTC_API_EXEC))) != NULL)
834    {
835        memset(p_buf, 0, sizeof(tBTA_GATTC_API_EXEC));
836
837        p_buf->hdr.event = BTA_GATTC_API_EXEC_EVT;
838        p_buf->hdr.layer_specific = conn_id;
839
840        p_buf->is_execute = is_execute;
841
842        bta_sys_sendmsg(p_buf);
843    }
844    return;
845
846}
847/*******************************************************************************
848**
849** Function         BTA_GATTC_SendIndConfirm
850**
851** Description      This function is called to send handle value confirmation.
852**
853** Parameters       conn_id - connection ID.
854**                    p_char_id - characteristic ID to confirm.
855**
856** Returns          None
857**
858*******************************************************************************/
859void BTA_GATTC_SendIndConfirm (UINT16 conn_id, tBTA_GATTC_CHAR_ID *p_char_id)
860{
861    tBTA_GATTC_API_CONFIRM  *p_buf;
862
863    APPL_TRACE_API3("BTA_GATTC_SendIndConfirm conn_id=%d service uuid1=0x%x char uuid=0x%x",
864                    conn_id, p_char_id->srvc_id.id.uuid.uu.uuid16, p_char_id->char_id.uuid.uu.uuid16);
865
866    if ((p_buf = (tBTA_GATTC_API_CONFIRM *) GKI_getbuf(sizeof(tBTA_GATTC_API_CONFIRM))) != NULL)
867    {
868        memset(p_buf, 0, sizeof(tBTA_GATTC_API_CONFIRM));
869
870        p_buf->hdr.event = BTA_GATTC_API_CONFIRM_EVT;
871        p_buf->hdr.layer_specific = conn_id;
872
873        memcpy(&p_buf->srvc_id, &p_char_id->srvc_id, sizeof(tBTA_GATT_SRVC_ID));
874        memcpy(&p_buf->char_id, &p_char_id->char_id, sizeof(tBTA_GATT_ID));
875
876        bta_sys_sendmsg(p_buf);
877    }
878    return;
879
880}
881
882/*******************************************************************************
883**
884** Function         BTA_GATTC_RegisterForNotifications
885**
886** Description      This function is called to register for notification of a service.
887**
888** Parameters       client_if - client interface.
889**                  bda - target GATT server.
890**                  p_char_id - pointer to GATT characteristic ID.
891**
892** Returns          OK if registration succeed, otherwise failed.
893**
894*******************************************************************************/
895tBTA_GATT_STATUS BTA_GATTC_RegisterForNotifications (tBTA_GATTC_IF client_if,
896                                                     BD_ADDR bda,
897                                                     tBTA_GATTC_CHAR_ID *p_char_id)
898{
899    tBTA_GATTC_RCB      *p_clreg;
900    tBTA_GATT_STATUS    status = BTA_GATT_ILLEGAL_PARAMETER;
901    UINT8               i;
902
903    if (!p_char_id)
904    {
905        APPL_TRACE_ERROR0("deregistration failed, unknow char id");
906        return status;
907    }
908
909    /* lock other GKI task */
910    GKI_sched_lock();
911
912    if ((p_clreg = bta_gattc_cl_get_regcb(client_if)) != NULL)
913    {
914        for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
915        {
916            if ( p_clreg->notif_reg[i].in_use &&
917                 !memcmp(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN) &&
918                  bta_gattc_charid_compare(&p_clreg->notif_reg[i].char_id, p_char_id))
919            {
920                APPL_TRACE_WARNING0("notification already registered");
921                status = BTA_GATT_OK;
922                break;
923            }
924        }
925        if (status != BTA_GATT_OK)
926        {
927            for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
928            {
929                if (!p_clreg->notif_reg[i].in_use)
930                {
931                    memset((void *)&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
932
933                    p_clreg->notif_reg[i].in_use = TRUE;
934                    memcpy(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN);
935
936                    p_clreg->notif_reg[i].char_id.srvc_id.is_primary = p_char_id->srvc_id.is_primary;
937                    bta_gattc_cpygattid(&p_clreg->notif_reg[i].char_id.srvc_id.id, &p_char_id->srvc_id.id);
938                    bta_gattc_cpygattid(&p_clreg->notif_reg[i].char_id.char_id, &p_char_id->char_id);
939
940                    status = BTA_GATT_OK;
941                    break;
942                }
943            }
944            if (i == BTA_GATTC_NOTIF_REG_MAX)
945            {
946                status = BTA_GATT_NO_RESOURCES;
947                APPL_TRACE_ERROR0("Max Notification Reached, registration failed.");
948            }
949        }
950    }
951    else
952    {
953        APPL_TRACE_ERROR1("Client_if: %d Not Registered", client_if);
954    }
955
956    GKI_sched_unlock();
957
958    return status;
959}
960
961/*******************************************************************************
962**
963** Function         BTA_GATTC_DeregisterForNotifications
964**
965** Description      This function is called to de-register for notification of a service.
966**
967** Parameters       client_if - client interface.
968**                  bda - target GATT server.
969**                  p_char_id - pointer to GATT characteristic ID.
970**
971** Returns          OK if deregistration succeed, otherwise failed.
972**
973*******************************************************************************/
974tBTA_GATT_STATUS BTA_GATTC_DeregisterForNotifications (tBTA_GATTC_IF client_if,
975                                                       BD_ADDR bda,
976                                                       tBTA_GATTC_CHAR_ID *p_char_id)
977{
978    tBTA_GATTC_RCB      *p_clreg;
979    tBTA_GATT_STATUS    status = BTA_GATT_ILLEGAL_PARAMETER;
980    UINT8               i;
981
982    if (!p_char_id)
983    {
984        APPL_TRACE_ERROR0("deregistration failed, unknow char id");
985        return status;
986    }
987
988    /* lock other GKI task */
989    GKI_sched_lock();
990
991    if ((p_clreg = bta_gattc_cl_get_regcb(client_if)) != NULL)
992    {
993        for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i ++)
994        {
995            if (p_clreg->notif_reg[i].in_use &&
996                !memcmp(p_clreg->notif_reg[i].remote_bda, bda, BD_ADDR_LEN) &&
997                bta_gattc_charid_compare(&p_clreg->notif_reg[i].char_id, p_char_id))
998            {
999                APPL_TRACE_DEBUG0("Deregistered.");
1000                memset(&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
1001                status = BTA_GATT_OK;
1002                break;
1003            }
1004        }
1005        if (i == BTA_GATTC_NOTIF_REG_MAX)
1006        {
1007            status = BTA_GATT_ERROR;
1008
1009            APPL_TRACE_ERROR0("registration not found");
1010        }
1011    }
1012    else
1013    {
1014        APPL_TRACE_ERROR1("Client_if: %d Not Registered", client_if);
1015    }
1016
1017    GKI_sched_unlock();
1018
1019    return status;
1020}
1021
1022/*******************************************************************************
1023**
1024** Function         BTA_GATTC_Refresh
1025**
1026** Description      Refresh the server cache of the remote device
1027**
1028** Parameters       remote_bda: remote device BD address.
1029**
1030** Returns          void
1031**
1032*******************************************************************************/
1033void BTA_GATTC_Refresh(BD_ADDR remote_bda)
1034{
1035    tBTA_GATTC_API_OPEN  *p_buf;
1036
1037    if ((p_buf = (tBTA_GATTC_API_OPEN *) GKI_getbuf(sizeof(tBTA_GATTC_API_OPEN))) != NULL)
1038    {
1039        p_buf->hdr.event = BTA_GATTC_API_REFRESH_EVT;
1040
1041        memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
1042
1043
1044        bta_sys_sendmsg(p_buf);
1045    }
1046    return;
1047}
1048
1049/*******************************************************************************
1050**
1051** Function         BTA_GATTC_Listen
1052**
1053** Description      Start advertisement to listen for connection request for a GATT
1054**                  client application.
1055**
1056** Parameters       client_if: server interface.
1057**                  start: to start or stop listening for connection
1058**                  remote_bda: remote device BD address, if listen to all device
1059**                              use NULL.
1060**
1061** Returns          void
1062**
1063*******************************************************************************/
1064void BTA_GATTC_Listen(tBTA_GATTC_IF client_if, BOOLEAN start, BD_ADDR_PTR target_bda)
1065{
1066    tBTA_GATTC_API_LISTEN  *p_buf;
1067
1068    if ((p_buf = (tBTA_GATTC_API_LISTEN *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN))) != NULL)
1069    {
1070        p_buf->hdr.event = BTA_GATTC_API_LISTEN_EVT;
1071
1072        p_buf->client_if = client_if;
1073        p_buf->start = start;
1074        if (target_bda)
1075        {
1076            p_buf->remote_bda = (UINT8*)(p_buf + 1);
1077            memcpy(p_buf->remote_bda, target_bda, BD_ADDR_LEN);
1078        }
1079        else
1080            p_buf->remote_bda = NULL;
1081
1082        bta_sys_sendmsg(p_buf);
1083    }
1084    return;
1085}
1086
1087/*******************************************************************************
1088**
1089** Function         BTA_GATTC_Broadcast
1090**
1091** Description      Start broadcasting (non-connectable advertisements)
1092**
1093** Parameters       client_if: client interface.
1094**                  start: to start or stop listening for connection
1095**
1096** Returns          void
1097**
1098*******************************************************************************/
1099void BTA_GATTC_Broadcast(tBTA_GATTC_IF client_if, BOOLEAN start)
1100{
1101    tBTA_GATTC_API_LISTEN  *p_buf;
1102
1103    if ((p_buf = (tBTA_GATTC_API_LISTEN *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTC_API_LISTEN) + BD_ADDR_LEN))) != NULL)
1104    {
1105        p_buf->hdr.event = BTA_GATTC_API_BROADCAST_EVT;
1106        p_buf->client_if = client_if;
1107        p_buf->start = start;
1108        bta_sys_sendmsg(p_buf);
1109    }
1110    return;
1111}
1112
1113#endif /* BTA_GATT_INCLUDED */
1114
1115