1 /******************************************************************************
2  *
3  *  Copyright 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 #include <string.h>
28 
29 #include <base/bind.h>
30 #include <base/bind_helpers.h>
31 #include <base/callback.h>
32 #include "bt_common.h"
33 #include "bta_gatt_api.h"
34 #include "bta_gattc_int.h"
35 #include "bta_sys.h"
36 #include "device/include/controller.h"
37 #include "stack/include/btu.h"
38 
39 using bluetooth::Uuid;
40 
41 /*****************************************************************************
42  *  Constants
43  ****************************************************************************/
44 
45 static const tBTA_SYS_REG bta_gattc_reg = {bta_gattc_hdl_event,
46                                            BTA_GATTC_Disable};
47 
48 /*******************************************************************************
49  *
50  * Function         BTA_GATTC_Disable
51  *
52  * Description      This function is called to disable GATTC module
53  *
54  * Parameters       None.
55  *
56  * Returns          None
57  *
58  ******************************************************************************/
BTA_GATTC_Disable(void)59 void BTA_GATTC_Disable(void) {
60   if (!bta_sys_is_register(BTA_ID_GATTC)) {
61     LOG(WARNING) << "GATTC Module not enabled/already disabled";
62     return;
63   }
64 
65   do_in_main_thread(FROM_HERE, base::Bind(&bta_gattc_disable));
66   bta_sys_deregister(BTA_ID_GATTC);
67 }
68 
69 /**
70  * This function is called to register application callbacks with BTA GATTC
71  * module. |client_cb| pointer to the application callback function.
72  * |cb| one time callback when registration is finished
73  */
BTA_GATTC_AppRegister(tBTA_GATTC_CBACK * p_client_cb,BtaAppRegisterCallback cb)74 void BTA_GATTC_AppRegister(tBTA_GATTC_CBACK* p_client_cb,
75                            BtaAppRegisterCallback cb) {
76   if (!bta_sys_is_register(BTA_ID_GATTC))
77     bta_sys_register(BTA_ID_GATTC, &bta_gattc_reg);
78 
79   do_in_main_thread(
80       FROM_HERE, base::Bind(&bta_gattc_register, Uuid::GetRandom(), p_client_cb,
81                             std::move(cb)));
82 }
83 
app_deregister_impl(tGATT_IF client_if)84 static void app_deregister_impl(tGATT_IF client_if) {
85   bta_gattc_deregister(bta_gattc_cl_get_regcb(client_if));
86 }
87 /*******************************************************************************
88  *
89  * Function         BTA_GATTC_AppDeregister
90  *
91  * Description      This function is called to deregister an application
92  *                  from BTA GATTC module.
93  *
94  * Parameters       client_if - client interface identifier.
95  *
96  * Returns          None
97  *
98  ******************************************************************************/
BTA_GATTC_AppDeregister(tGATT_IF client_if)99 void BTA_GATTC_AppDeregister(tGATT_IF client_if) {
100   do_in_main_thread(FROM_HERE, base::Bind(&app_deregister_impl, client_if));
101 }
102 
103 /*******************************************************************************
104  *
105  * Function         BTA_GATTC_Open
106  *
107  * Description      Open a direct connection or add a background auto connection
108  *                  bd address
109  *
110  * Parameters       client_if: server interface.
111  *                  remote_bda: remote device BD address.
112  *                  is_direct: direct connection or background auto connection
113  *                  transport: Transport to be used for GATT connection
114  *                             (BREDR/LE)
115  *                  initiating_phys: LE PHY to use, optional
116  *                  opportunistic: wether the connection shall be opportunistic,
117  *                                 and don't impact the disconnection timer
118  *
119  ******************************************************************************/
BTA_GATTC_Open(tGATT_IF client_if,const RawAddress & remote_bda,bool is_direct,tGATT_TRANSPORT transport,bool opportunistic)120 void BTA_GATTC_Open(tGATT_IF client_if, const RawAddress& remote_bda,
121                     bool is_direct, tGATT_TRANSPORT transport,
122                     bool opportunistic) {
123   uint8_t phy = controller_get_interface()->get_le_all_initiating_phys();
124   BTA_GATTC_Open(client_if, remote_bda, is_direct, transport, opportunistic,
125                  phy);
126 }
127 
BTA_GATTC_Open(tGATT_IF client_if,const RawAddress & remote_bda,bool is_direct,tGATT_TRANSPORT transport,bool opportunistic,uint8_t initiating_phys)128 void BTA_GATTC_Open(tGATT_IF client_if, const RawAddress& remote_bda,
129                     bool is_direct, tGATT_TRANSPORT transport,
130                     bool opportunistic, uint8_t initiating_phys) {
131   tBTA_GATTC_API_OPEN* p_buf =
132       (tBTA_GATTC_API_OPEN*)osi_malloc(sizeof(tBTA_GATTC_API_OPEN));
133 
134   p_buf->hdr.event = BTA_GATTC_API_OPEN_EVT;
135   p_buf->client_if = client_if;
136   p_buf->is_direct = is_direct;
137   p_buf->transport = transport;
138   p_buf->initiating_phys = initiating_phys;
139   p_buf->opportunistic = opportunistic;
140   p_buf->remote_bda = remote_bda;
141 
142   bta_sys_sendmsg(p_buf);
143 }
144 
145 /*******************************************************************************
146  *
147  * Function         BTA_GATTC_CancelOpen
148  *
149  * Description      Cancel a direct open connection or remove a background auto
150  *                  connection
151  *                  bd address
152  *
153  * Parameters       client_if: server interface.
154  *                  remote_bda: remote device BD address.
155  *                  is_direct: direct connection or background auto connection
156  *
157  * Returns          void
158  *
159  ******************************************************************************/
BTA_GATTC_CancelOpen(tGATT_IF client_if,const RawAddress & remote_bda,bool is_direct)160 void BTA_GATTC_CancelOpen(tGATT_IF client_if, const RawAddress& remote_bda,
161                           bool is_direct) {
162   tBTA_GATTC_API_CANCEL_OPEN* p_buf = (tBTA_GATTC_API_CANCEL_OPEN*)osi_malloc(
163       sizeof(tBTA_GATTC_API_CANCEL_OPEN));
164 
165   p_buf->hdr.event = BTA_GATTC_API_CANCEL_OPEN_EVT;
166   p_buf->client_if = client_if;
167   p_buf->is_direct = is_direct;
168   p_buf->remote_bda = remote_bda;
169 
170   bta_sys_sendmsg(p_buf);
171 }
172 
173 /*******************************************************************************
174  *
175  * Function         BTA_GATTC_Close
176  *
177  * Description      Close a connection to a GATT server.
178  *
179  * Parameters       conn_id: connectino ID to be closed.
180  *
181  * Returns          void
182  *
183  ******************************************************************************/
BTA_GATTC_Close(uint16_t conn_id)184 void BTA_GATTC_Close(uint16_t conn_id) {
185   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
186 
187   p_buf->event = BTA_GATTC_API_CLOSE_EVT;
188   p_buf->layer_specific = conn_id;
189 
190   bta_sys_sendmsg(p_buf);
191 }
192 
193 /*******************************************************************************
194  *
195  * Function         BTA_GATTC_ConfigureMTU
196  *
197  * Description      Configure the MTU size in the GATT channel. This can be done
198  *                  only once per connection.
199  *
200  * Parameters       conn_id: connection ID.
201  *                  mtu: desired MTU size to use.
202  *
203  * Returns          void
204  *
205  ******************************************************************************/
BTA_GATTC_ConfigureMTU(uint16_t conn_id,uint16_t mtu)206 void BTA_GATTC_ConfigureMTU(uint16_t conn_id, uint16_t mtu) {
207   tBTA_GATTC_API_CFG_MTU* p_buf =
208       (tBTA_GATTC_API_CFG_MTU*)osi_malloc(sizeof(tBTA_GATTC_API_CFG_MTU));
209 
210   p_buf->hdr.event = BTA_GATTC_API_CFG_MTU_EVT;
211   p_buf->hdr.layer_specific = conn_id;
212   p_buf->mtu = mtu;
213 
214   bta_sys_sendmsg(p_buf);
215 }
216 
217 /*******************************************************************************
218  *
219  * Function         BTA_GATTC_ServiceSearchRequest
220  *
221  * Description      This function is called to request a GATT service discovery
222  *                  on a GATT server. This function report service search
223  *                  result by a callback event, and followed by a service search
224  *                  complete event.
225  *
226  * Parameters       conn_id: connection ID.
227  *                  p_srvc_uuid: a UUID of the service application is interested
228  *                               in.
229  *                              If Null, discover for all services.
230  *
231  * Returns          None
232  *
233  ******************************************************************************/
BTA_GATTC_ServiceSearchRequest(uint16_t conn_id,Uuid * p_srvc_uuid)234 void BTA_GATTC_ServiceSearchRequest(uint16_t conn_id, Uuid* p_srvc_uuid) {
235   const size_t len = sizeof(tBTA_GATTC_API_SEARCH) + sizeof(Uuid);
236   tBTA_GATTC_API_SEARCH* p_buf = (tBTA_GATTC_API_SEARCH*)osi_calloc(len);
237 
238   p_buf->hdr.event = BTA_GATTC_API_SEARCH_EVT;
239   p_buf->hdr.layer_specific = conn_id;
240   if (p_srvc_uuid) {
241     p_buf->p_srvc_uuid = (Uuid*)(p_buf + 1);
242     *p_buf->p_srvc_uuid = *p_srvc_uuid;
243   } else {
244     p_buf->p_srvc_uuid = NULL;
245   }
246 
247   bta_sys_sendmsg(p_buf);
248 }
249 
BTA_GATTC_DiscoverServiceByUuid(uint16_t conn_id,const Uuid & srvc_uuid)250 void BTA_GATTC_DiscoverServiceByUuid(uint16_t conn_id, const Uuid& srvc_uuid) {
251   do_in_main_thread(
252       FROM_HERE,
253       base::Bind(
254           base::IgnoreResult<tGATT_STATUS (*)(uint16_t, tGATT_DISC_TYPE,
255                                               uint16_t, uint16_t, const Uuid&)>(
256               &GATTC_Discover),
257           conn_id, GATT_DISC_SRVC_BY_UUID, 0x0001, 0xFFFF, srvc_uuid));
258 }
259 
260 /*******************************************************************************
261  *
262  * Function         BTA_GATTC_GetServices
263  *
264  * Description      This function is called to find the services on the given
265  *                  server.
266  *
267  * Parameters       conn_id: connection ID which identify the server.
268  *
269  * Returns          returns list of gatt::Service or NULL.
270  *
271  ******************************************************************************/
BTA_GATTC_GetServices(uint16_t conn_id)272 const std::list<gatt::Service>* BTA_GATTC_GetServices(uint16_t conn_id) {
273   return bta_gattc_get_services(conn_id);
274 }
275 
276 /*******************************************************************************
277  *
278  * Function         BTA_GATTC_GetCharacteristic
279  *
280  * Description      This function is called to find the characteristic on the
281  *                  given server.
282  *
283  * Parameters       conn_id - connection ID which identify the server.
284  *                  handle - characteristic handle
285  *
286  * Returns          returns pointer to gatt::Characteristic or NULL.
287  *
288  ******************************************************************************/
BTA_GATTC_GetCharacteristic(uint16_t conn_id,uint16_t handle)289 const gatt::Characteristic* BTA_GATTC_GetCharacteristic(uint16_t conn_id,
290                                                         uint16_t handle) {
291   return bta_gattc_get_characteristic(conn_id, handle);
292 }
293 
294 /*******************************************************************************
295  *
296  * Function         BTA_GATTC_GetDescriptor
297  *
298  * Description      This function is called to find the characteristic on the
299  *                  given server.
300  *
301  * Parameters       conn_id - connection ID which identify the server.
302  *                  handle - descriptor handle
303  *
304  * Returns          returns pointer to gatt::Descriptor or NULL.
305  *
306  ******************************************************************************/
BTA_GATTC_GetDescriptor(uint16_t conn_id,uint16_t handle)307 const gatt::Descriptor* BTA_GATTC_GetDescriptor(uint16_t conn_id,
308                                                 uint16_t handle) {
309   return bta_gattc_get_descriptor(conn_id, handle);
310 }
311 
312 /* Return characteristic that owns descriptor with handle equal to |handle|, or
313  * NULL */
BTA_GATTC_GetOwningCharacteristic(uint16_t conn_id,uint16_t handle)314 const gatt::Characteristic* BTA_GATTC_GetOwningCharacteristic(uint16_t conn_id,
315                                                               uint16_t handle) {
316   return bta_gattc_get_owning_characteristic(conn_id, handle);
317 }
318 
319 /* Return service that owns descriptor or characteristic with handle equal to
320  * |handle|, or NULL */
BTA_GATTC_GetOwningService(uint16_t conn_id,uint16_t handle)321 const gatt::Service* BTA_GATTC_GetOwningService(uint16_t conn_id,
322                                                 uint16_t handle) {
323   return bta_gattc_get_service_for_handle(conn_id, handle);
324 }
325 
326 /*******************************************************************************
327  *
328  * Function         BTA_GATTC_GetGattDb
329  *
330  * Description      This function is called to get the GATT database.
331  *
332  * Parameters       conn_id: connection ID which identify the server.
333  *                  db: output parameter which will contain the GATT database
334  *                      copy. Caller is responsible for freeing it.
335  *                  count: number of elements in database.
336  *
337  ******************************************************************************/
BTA_GATTC_GetGattDb(uint16_t conn_id,uint16_t start_handle,uint16_t end_handle,btgatt_db_element_t ** db,int * count)338 void BTA_GATTC_GetGattDb(uint16_t conn_id, uint16_t start_handle,
339                          uint16_t end_handle, btgatt_db_element_t** db,
340                          int* count) {
341   bta_gattc_get_gatt_db(conn_id, start_handle, end_handle, db, count);
342 }
343 
344 /*******************************************************************************
345  *
346  * Function         BTA_GATTC_ReadCharacteristic
347  *
348  * Description      This function is called to read a characteristics value
349  *
350  * Parameters       conn_id - connection ID.
351  *                  handle - characteritic handle to read.
352  *
353  * Returns          None
354  *
355  ******************************************************************************/
BTA_GATTC_ReadCharacteristic(uint16_t conn_id,uint16_t handle,tGATT_AUTH_REQ auth_req,GATT_READ_OP_CB callback,void * cb_data)356 void BTA_GATTC_ReadCharacteristic(uint16_t conn_id, uint16_t handle,
357                                   tGATT_AUTH_REQ auth_req,
358                                   GATT_READ_OP_CB callback, void* cb_data) {
359   tBTA_GATTC_API_READ* p_buf =
360       (tBTA_GATTC_API_READ*)osi_calloc(sizeof(tBTA_GATTC_API_READ));
361 
362   p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
363   p_buf->hdr.layer_specific = conn_id;
364   p_buf->auth_req = auth_req;
365   p_buf->handle = handle;
366   p_buf->read_cb = callback;
367   p_buf->read_cb_data = cb_data;
368 
369   bta_sys_sendmsg(p_buf);
370 }
371 
372 /**
373  * This function is called to read a value of characteristic with uuid equal to
374  * |uuid|
375  */
BTA_GATTC_ReadUsingCharUuid(uint16_t conn_id,const Uuid & uuid,uint16_t s_handle,uint16_t e_handle,tGATT_AUTH_REQ auth_req,GATT_READ_OP_CB callback,void * cb_data)376 void BTA_GATTC_ReadUsingCharUuid(uint16_t conn_id, const Uuid& uuid,
377                                  uint16_t s_handle, uint16_t e_handle,
378                                  tGATT_AUTH_REQ auth_req,
379                                  GATT_READ_OP_CB callback, void* cb_data) {
380   tBTA_GATTC_API_READ* p_buf =
381       (tBTA_GATTC_API_READ*)osi_calloc(sizeof(tBTA_GATTC_API_READ));
382 
383   p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
384   p_buf->hdr.layer_specific = conn_id;
385   p_buf->auth_req = auth_req;
386   p_buf->handle = 0;
387   p_buf->uuid = uuid;
388   p_buf->s_handle = s_handle;
389   p_buf->e_handle = e_handle;
390   p_buf->read_cb = callback;
391   p_buf->read_cb_data = cb_data;
392 
393   bta_sys_sendmsg(p_buf);
394 }
395 
396 /*******************************************************************************
397  *
398  * Function         BTA_GATTC_ReadCharDescr
399  *
400  * Description      This function is called to read a descriptor value.
401  *
402  * Parameters       conn_id - connection ID.
403  *                  handle - descriptor handle to read.
404  *
405  * Returns          None
406  *
407  ******************************************************************************/
BTA_GATTC_ReadCharDescr(uint16_t conn_id,uint16_t handle,tGATT_AUTH_REQ auth_req,GATT_READ_OP_CB callback,void * cb_data)408 void BTA_GATTC_ReadCharDescr(uint16_t conn_id, uint16_t handle,
409                              tGATT_AUTH_REQ auth_req, GATT_READ_OP_CB callback,
410                              void* cb_data) {
411   tBTA_GATTC_API_READ* p_buf =
412       (tBTA_GATTC_API_READ*)osi_calloc(sizeof(tBTA_GATTC_API_READ));
413 
414   p_buf->hdr.event = BTA_GATTC_API_READ_EVT;
415   p_buf->hdr.layer_specific = conn_id;
416   p_buf->auth_req = auth_req;
417   p_buf->handle = handle;
418   p_buf->read_cb = callback;
419   p_buf->read_cb_data = cb_data;
420 
421   bta_sys_sendmsg(p_buf);
422 }
423 
424 /*******************************************************************************
425  *
426  * Function         BTA_GATTC_ReadMultiple
427  *
428  * Description      This function is called to read multiple characteristic or
429  *                  characteristic descriptors.
430  *
431  * Parameters       conn_id - connectino ID.
432  *                    p_read_multi - pointer to the read multiple parameter.
433  *
434  * Returns          None
435  *
436  ******************************************************************************/
BTA_GATTC_ReadMultiple(uint16_t conn_id,tBTA_GATTC_MULTI * p_read_multi,tGATT_AUTH_REQ auth_req)437 void BTA_GATTC_ReadMultiple(uint16_t conn_id, tBTA_GATTC_MULTI* p_read_multi,
438                             tGATT_AUTH_REQ auth_req) {
439   tBTA_GATTC_API_READ_MULTI* p_buf =
440       (tBTA_GATTC_API_READ_MULTI*)osi_calloc(sizeof(tBTA_GATTC_API_READ_MULTI));
441 
442   p_buf->hdr.event = BTA_GATTC_API_READ_MULTI_EVT;
443   p_buf->hdr.layer_specific = conn_id;
444   p_buf->auth_req = auth_req;
445   p_buf->num_attr = p_read_multi->num_attr;
446 
447   if (p_buf->num_attr > 0)
448     memcpy(p_buf->handles, p_read_multi->handles,
449            sizeof(uint16_t) * p_read_multi->num_attr);
450 
451   bta_sys_sendmsg(p_buf);
452 }
453 
454 /*******************************************************************************
455  *
456  * Function         BTA_GATTC_WriteCharValue
457  *
458  * Description      This function is called to write characteristic value.
459  *
460  * Parameters       conn_id - connection ID.
461  *                  handle - characteristic handle to write.
462  *                  write_type - type of write.
463  *                  value - the value to be written.
464  *
465  * Returns          None
466  *
467  ******************************************************************************/
BTA_GATTC_WriteCharValue(uint16_t conn_id,uint16_t handle,tGATT_WRITE_TYPE write_type,std::vector<uint8_t> value,tGATT_AUTH_REQ auth_req,GATT_WRITE_OP_CB callback,void * cb_data)468 void BTA_GATTC_WriteCharValue(uint16_t conn_id, uint16_t handle,
469                               tGATT_WRITE_TYPE write_type,
470                               std::vector<uint8_t> value,
471                               tGATT_AUTH_REQ auth_req,
472                               GATT_WRITE_OP_CB callback, void* cb_data) {
473   tBTA_GATTC_API_WRITE* p_buf = (tBTA_GATTC_API_WRITE*)osi_calloc(
474       sizeof(tBTA_GATTC_API_WRITE) + value.size());
475 
476   p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
477   p_buf->hdr.layer_specific = conn_id;
478   p_buf->auth_req = auth_req;
479   p_buf->handle = handle;
480   p_buf->write_type = write_type;
481   p_buf->len = value.size();
482   p_buf->write_cb = callback;
483   p_buf->write_cb_data = cb_data;
484 
485   if (value.size() > 0) {
486     p_buf->p_value = (uint8_t*)(p_buf + 1);
487     memcpy(p_buf->p_value, value.data(), value.size());
488   }
489 
490   bta_sys_sendmsg(p_buf);
491 }
492 
493 /*******************************************************************************
494  *
495  * Function         BTA_GATTC_WriteCharDescr
496  *
497  * Description      This function is called to write descriptor value.
498  *
499  * Parameters       conn_id - connection ID
500  *                  handle - descriptor hadle to write.
501  *                  value - the value to be written.
502  *
503  * Returns          None
504  *
505  ******************************************************************************/
BTA_GATTC_WriteCharDescr(uint16_t conn_id,uint16_t handle,std::vector<uint8_t> value,tGATT_AUTH_REQ auth_req,GATT_WRITE_OP_CB callback,void * cb_data)506 void BTA_GATTC_WriteCharDescr(uint16_t conn_id, uint16_t handle,
507                               std::vector<uint8_t> value,
508                               tGATT_AUTH_REQ auth_req,
509                               GATT_WRITE_OP_CB callback, void* cb_data) {
510   tBTA_GATTC_API_WRITE* p_buf = (tBTA_GATTC_API_WRITE*)osi_calloc(
511       sizeof(tBTA_GATTC_API_WRITE) + value.size());
512 
513   p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
514   p_buf->hdr.layer_specific = conn_id;
515   p_buf->auth_req = auth_req;
516   p_buf->handle = handle;
517   p_buf->write_type = GATT_WRITE;
518   p_buf->write_cb = callback;
519   p_buf->write_cb_data = cb_data;
520 
521   if (value.size() != 0) {
522     p_buf->p_value = (uint8_t*)(p_buf + 1);
523     p_buf->len = value.size();
524     memcpy(p_buf->p_value, value.data(), value.size());
525   }
526 
527   bta_sys_sendmsg(p_buf);
528 }
529 
530 /*******************************************************************************
531  *
532  * Function         BTA_GATTC_PrepareWrite
533  *
534  * Description      This function is called to prepare write a characteristic
535  *                  value.
536  *
537  * Parameters       conn_id - connection ID.
538  *                  p_char_id - GATT characteritic ID of the service.
539  *                  offset - offset of the write value.
540  *                  value - the value to be written.
541  *
542  * Returns          None
543  *
544  ******************************************************************************/
BTA_GATTC_PrepareWrite(uint16_t conn_id,uint16_t handle,uint16_t offset,std::vector<uint8_t> value,tGATT_AUTH_REQ auth_req,GATT_WRITE_OP_CB callback,void * cb_data)545 void BTA_GATTC_PrepareWrite(uint16_t conn_id, uint16_t handle, uint16_t offset,
546                             std::vector<uint8_t> value, tGATT_AUTH_REQ auth_req,
547                             GATT_WRITE_OP_CB callback, void* cb_data) {
548   tBTA_GATTC_API_WRITE* p_buf = (tBTA_GATTC_API_WRITE*)osi_calloc(
549       sizeof(tBTA_GATTC_API_WRITE) + value.size());
550 
551   p_buf->hdr.event = BTA_GATTC_API_WRITE_EVT;
552   p_buf->hdr.layer_specific = conn_id;
553   p_buf->auth_req = auth_req;
554   p_buf->handle = handle;
555   p_buf->write_cb = callback;
556   p_buf->write_cb_data = cb_data;
557 
558   p_buf->write_type = BTA_GATTC_WRITE_PREPARE;
559   p_buf->offset = offset;
560   p_buf->len = value.size();
561 
562   if (value.size() > 0) {
563     p_buf->p_value = (uint8_t*)(p_buf + 1);
564     memcpy(p_buf->p_value, value.data(), value.size());
565   }
566 
567   bta_sys_sendmsg(p_buf);
568 }
569 
570 /*******************************************************************************
571  *
572  * Function         BTA_GATTC_ExecuteWrite
573  *
574  * Description      This function is called to execute write a prepare write
575  *                  sequence.
576  *
577  * Parameters       conn_id - connection ID.
578  *                    is_execute - execute or cancel.
579  *
580  * Returns          None
581  *
582  ******************************************************************************/
BTA_GATTC_ExecuteWrite(uint16_t conn_id,bool is_execute)583 void BTA_GATTC_ExecuteWrite(uint16_t conn_id, bool is_execute) {
584   tBTA_GATTC_API_EXEC* p_buf =
585       (tBTA_GATTC_API_EXEC*)osi_calloc(sizeof(tBTA_GATTC_API_EXEC));
586 
587   p_buf->hdr.event = BTA_GATTC_API_EXEC_EVT;
588   p_buf->hdr.layer_specific = conn_id;
589   p_buf->is_execute = is_execute;
590 
591   bta_sys_sendmsg(p_buf);
592 }
593 
594 /*******************************************************************************
595  *
596  * Function         BTA_GATTC_SendIndConfirm
597  *
598  * Description      This function is called to send handle value confirmation.
599  *
600  * Parameters       conn_id - connection ID.
601  *                    p_char_id - characteristic ID to confirm.
602  *
603  * Returns          None
604  *
605  ******************************************************************************/
BTA_GATTC_SendIndConfirm(uint16_t conn_id,uint16_t handle)606 void BTA_GATTC_SendIndConfirm(uint16_t conn_id, uint16_t handle) {
607   tBTA_GATTC_API_CONFIRM* p_buf =
608       (tBTA_GATTC_API_CONFIRM*)osi_calloc(sizeof(tBTA_GATTC_API_CONFIRM));
609 
610   VLOG(1) << __func__ << ": conn_id=" << +conn_id << " handle=0x" << std::hex
611           << +handle;
612 
613   p_buf->hdr.event = BTA_GATTC_API_CONFIRM_EVT;
614   p_buf->hdr.layer_specific = conn_id;
615   p_buf->handle = handle;
616 
617   bta_sys_sendmsg(p_buf);
618 }
619 
620 /*******************************************************************************
621  *
622  * Function         BTA_GATTC_RegisterForNotifications
623  *
624  * Description      This function is called to register for notification of a
625  *                  service.
626  *
627  * Parameters       client_if - client interface.
628  *                  bda - target GATT server.
629  *                  handle - GATT characteristic handle.
630  *
631  * Returns          OK if registration succeed, otherwise failed.
632  *
633  ******************************************************************************/
BTA_GATTC_RegisterForNotifications(tGATT_IF client_if,const RawAddress & bda,uint16_t handle)634 tGATT_STATUS BTA_GATTC_RegisterForNotifications(tGATT_IF client_if,
635                                                 const RawAddress& bda,
636                                                 uint16_t handle) {
637   tBTA_GATTC_RCB* p_clreg;
638   tGATT_STATUS status = GATT_ILLEGAL_PARAMETER;
639   uint8_t i;
640 
641   if (!handle) {
642     LOG(ERROR) << __func__ << ": registration failed, handle is 0";
643     return status;
644   }
645 
646   p_clreg = bta_gattc_cl_get_regcb(client_if);
647   if (p_clreg != NULL) {
648     for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) {
649       if (p_clreg->notif_reg[i].in_use &&
650           p_clreg->notif_reg[i].remote_bda == bda &&
651           p_clreg->notif_reg[i].handle == handle) {
652         LOG(WARNING) << "notification already registered";
653         status = GATT_SUCCESS;
654         break;
655       }
656     }
657     if (status != GATT_SUCCESS) {
658       for (i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) {
659         if (!p_clreg->notif_reg[i].in_use) {
660           memset((void*)&p_clreg->notif_reg[i], 0,
661                  sizeof(tBTA_GATTC_NOTIF_REG));
662 
663           p_clreg->notif_reg[i].in_use = true;
664           p_clreg->notif_reg[i].remote_bda = bda;
665 
666           p_clreg->notif_reg[i].handle = handle;
667           status = GATT_SUCCESS;
668           break;
669         }
670       }
671       if (i == BTA_GATTC_NOTIF_REG_MAX) {
672         status = GATT_NO_RESOURCES;
673         LOG(ERROR) << "Max Notification Reached, registration failed.";
674       }
675     }
676   } else {
677     LOG(ERROR) << "client_if=" << +client_if << " Not Registered";
678   }
679 
680   return status;
681 }
682 
683 /*******************************************************************************
684  *
685  * Function         BTA_GATTC_DeregisterForNotifications
686  *
687  * Description      This function is called to de-register for notification of a
688  *                  service.
689  *
690  * Parameters       client_if - client interface.
691  *                  remote_bda - target GATT server.
692  *                  handle - GATT characteristic handle.
693  *
694  * Returns          OK if deregistration succeed, otherwise failed.
695  *
696  ******************************************************************************/
BTA_GATTC_DeregisterForNotifications(tGATT_IF client_if,const RawAddress & bda,uint16_t handle)697 tGATT_STATUS BTA_GATTC_DeregisterForNotifications(tGATT_IF client_if,
698                                                   const RawAddress& bda,
699                                                   uint16_t handle) {
700   if (!handle) {
701     LOG(ERROR) << __func__ << ": deregistration failed, handle is 0";
702     return GATT_ILLEGAL_PARAMETER;
703   }
704 
705   tBTA_GATTC_RCB* p_clreg = bta_gattc_cl_get_regcb(client_if);
706   if (p_clreg == NULL) {
707     LOG(ERROR) << __func__ << " client_if=" << +client_if
708                << " not registered bd_addr=" << bda;
709     return GATT_ILLEGAL_PARAMETER;
710   }
711 
712   for (int i = 0; i < BTA_GATTC_NOTIF_REG_MAX; i++) {
713     if (p_clreg->notif_reg[i].in_use &&
714         p_clreg->notif_reg[i].remote_bda == bda &&
715         p_clreg->notif_reg[i].handle == handle) {
716       VLOG(1) << __func__ << " deregistered bd_addr=" << bda;
717       memset(&p_clreg->notif_reg[i], 0, sizeof(tBTA_GATTC_NOTIF_REG));
718       return GATT_SUCCESS;
719     }
720   }
721 
722   LOG(ERROR) << __func__ << " registration not found bd_addr=" << bda;
723   return GATT_ERROR;
724 }
725 
726 /*******************************************************************************
727  *
728  * Function         BTA_GATTC_Refresh
729  *
730  * Description      Refresh the server cache of the remote device
731  *
732  * Parameters       remote_bda: remote device BD address.
733  *
734  * Returns          void
735  *
736  ******************************************************************************/
BTA_GATTC_Refresh(const RawAddress & remote_bda)737 void BTA_GATTC_Refresh(const RawAddress& remote_bda) {
738   do_in_main_thread(FROM_HERE,
739                     base::Bind(&bta_gattc_process_api_refresh, remote_bda));
740 }
741