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