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