• 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 server of BTA.
22  *
23  ******************************************************************************/
24 
25 #include <base/bind.h>
26 #include <base/location.h>
27 #include <base/logging.h>
28 
29 #include <cstdint>
30 #include <memory>
31 #include <vector>
32 
33 #include "bt_target.h"  // Must be first to define build configuration
34 #include "bta/gatt/bta_gatts_int.h"
35 #include "osi/include/allocator.h"
36 #include "stack/include/bt_hdr.h"
37 #include "stack/include/btu.h"  // do_in_main_thread
38 #include "types/bluetooth/uuid.h"
39 #include "types/bt_transport.h"
40 #include "types/raw_address.h"
41 
42 /*****************************************************************************
43  *  Constants
44  ****************************************************************************/
45 
46 static const tBTA_SYS_REG bta_gatts_reg = {bta_gatts_hdl_event,
47                                            BTA_GATTS_Disable};
48 
49 /*******************************************************************************
50  *
51  * Function         BTA_GATTS_Disable
52  *
53  * Description      This function is called to disable GATTS module
54  *
55  * Parameters       None.
56  *
57  * Returns          None
58  *
59  ******************************************************************************/
BTA_GATTS_Disable(void)60 void BTA_GATTS_Disable(void) {
61   if (!bta_sys_is_register(BTA_ID_GATTS)) {
62     LOG(WARNING) << "GATTS Module not enabled/already disabled";
63     return;
64   }
65 
66   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
67   p_buf->event = BTA_GATTS_API_DISABLE_EVT;
68   bta_sys_sendmsg(p_buf);
69   bta_sys_deregister(BTA_ID_GATTS);
70 }
71 
72 /*******************************************************************************
73  *
74  * Function         BTA_GATTS_AppRegister
75  *
76  * Description      This function is called to register application callbacks
77  *                    with BTA GATTS module.
78  *
79  * Parameters       p_app_uuid - applicaiton UUID
80  *                  p_cback - pointer to the application callback function.
81  *
82  * Returns          None
83  *
84  ******************************************************************************/
BTA_GATTS_AppRegister(const bluetooth::Uuid & app_uuid,tBTA_GATTS_CBACK * p_cback,bool eatt_support)85 void BTA_GATTS_AppRegister(const bluetooth::Uuid& app_uuid,
86                            tBTA_GATTS_CBACK* p_cback, bool eatt_support) {
87   tBTA_GATTS_API_REG* p_buf =
88       (tBTA_GATTS_API_REG*)osi_malloc(sizeof(tBTA_GATTS_API_REG));
89 
90   /* register with BTA system manager */
91   if (!bta_sys_is_register(BTA_ID_GATTS))
92     bta_sys_register(BTA_ID_GATTS, &bta_gatts_reg);
93 
94   p_buf->hdr.event = BTA_GATTS_API_REG_EVT;
95   p_buf->app_uuid = app_uuid;
96   p_buf->p_cback = p_cback;
97   p_buf->eatt_support = eatt_support;
98 
99   bta_sys_sendmsg(p_buf);
100 }
101 
102 /*******************************************************************************
103  *
104  * Function         BTA_GATTS_AppDeregister
105  *
106  * Description      De-register with GATT Server.
107  *
108  * Parameters       app_id: applicatino ID.
109  *
110  * Returns          void
111  *
112  ******************************************************************************/
BTA_GATTS_AppDeregister(tGATT_IF server_if)113 void BTA_GATTS_AppDeregister(tGATT_IF server_if) {
114   tBTA_GATTS_API_DEREG* p_buf =
115       (tBTA_GATTS_API_DEREG*)osi_malloc(sizeof(tBTA_GATTS_API_DEREG));
116 
117   p_buf->hdr.event = BTA_GATTS_API_DEREG_EVT;
118   p_buf->server_if = server_if;
119 
120   bta_sys_sendmsg(p_buf);
121 }
122 
bta_gatts_add_service_impl(tGATT_IF server_if,std::vector<btgatt_db_element_t> service,BTA_GATTS_AddServiceCb cb)123 void bta_gatts_add_service_impl(tGATT_IF server_if,
124                                 std::vector<btgatt_db_element_t> service,
125                                 BTA_GATTS_AddServiceCb cb) {
126   uint8_t rcb_idx =
127       bta_gatts_find_app_rcb_idx_by_app_if(&bta_gatts_cb, server_if);
128 
129   LOG(INFO) << __func__ << ": rcb_idx=" << +rcb_idx;
130 
131   if (rcb_idx == BTA_GATTS_INVALID_APP) {
132     cb.Run(GATT_ERROR, server_if, std::move(service));
133     return;
134   }
135 
136   uint8_t srvc_idx = bta_gatts_alloc_srvc_cb(&bta_gatts_cb, rcb_idx);
137   if (srvc_idx == BTA_GATTS_INVALID_APP) {
138     cb.Run(GATT_ERROR, server_if, std::move(service));
139     return;
140   }
141 
142   tGATT_STATUS status =
143       GATTS_AddService(server_if, service.data(), service.size());
144   if (status != GATT_SERVICE_STARTED) {
145     memset(&bta_gatts_cb.srvc_cb[srvc_idx], 0, sizeof(tBTA_GATTS_SRVC_CB));
146     LOG(ERROR) << __func__ << ": service creation failed.";
147     cb.Run(GATT_ERROR, server_if, std::move(service));
148     return;
149   }
150 
151   bta_gatts_cb.srvc_cb[srvc_idx].service_uuid = service[0].uuid;
152 
153   // service_id is equal to service start handle
154   bta_gatts_cb.srvc_cb[srvc_idx].service_id = service[0].attribute_handle;
155   bta_gatts_cb.srvc_cb[srvc_idx].idx = srvc_idx;
156 
157   cb.Run(GATT_SUCCESS, server_if, std::move(service));
158   return;
159 }
160 
161 /*******************************************************************************
162  *
163  * Function         BTA_GATTS_AddService
164  *
165  * Description      Add the given |service| and all included elements to the
166  *                  GATT database. a |BTA_GATTS_ADD_SRVC_EVT| is triggered to
167  *                  report the status and attribute handles.
168  *
169  * Parameters       server_if: server interface.
170  *                  service: pointer vector describing service.
171  *
172  * Returns          Returns |GATT_SUCCESS| on success or |GATT_ERROR| if the
173  *                  service cannot be added.
174  *
175  ******************************************************************************/
BTA_GATTS_AddService(tGATT_IF server_if,std::vector<btgatt_db_element_t> service,BTA_GATTS_AddServiceCb cb)176 extern void BTA_GATTS_AddService(tGATT_IF server_if,
177                                  std::vector<btgatt_db_element_t> service,
178                                  BTA_GATTS_AddServiceCb cb) {
179   do_in_main_thread(FROM_HERE,
180                     base::Bind(&bta_gatts_add_service_impl, server_if,
181                                std::move(service), std::move(cb)));
182 }
183 
184 /*******************************************************************************
185  *
186  * Function         BTA_GATTS_DeleteService
187  *
188  * Description      This function is called to delete a service. When this is
189  *                  done, a callback event BTA_GATTS_DELETE_EVT is report with
190  *                  the status.
191  *
192  * Parameters       service_id: service_id to be deleted.
193  *
194  * Returns          returns none.
195  *
196  ******************************************************************************/
BTA_GATTS_DeleteService(uint16_t service_id)197 void BTA_GATTS_DeleteService(uint16_t service_id) {
198   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
199 
200   p_buf->event = BTA_GATTS_API_DEL_SRVC_EVT;
201   p_buf->layer_specific = service_id;
202 
203   bta_sys_sendmsg(p_buf);
204 }
205 
206 /*******************************************************************************
207  *
208  * Function         BTA_GATTS_StopService
209  *
210  * Description      This function is called to stop a service.
211  *
212  * Parameters       service_id - service to be topped.
213  *
214  * Returns          None
215  *
216  ******************************************************************************/
BTA_GATTS_StopService(uint16_t service_id)217 void BTA_GATTS_StopService(uint16_t service_id) {
218   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
219 
220   p_buf->event = BTA_GATTS_API_STOP_SRVC_EVT;
221   p_buf->layer_specific = service_id;
222 
223   bta_sys_sendmsg(p_buf);
224 }
225 
226 /*******************************************************************************
227  *
228  * Function         BTA_GATTS_HandleValueIndication
229  *
230  * Description      This function is called to read a characteristics
231  *                  descriptor.
232  *
233  * Parameters       bda - remote device bd address to indicate.
234  *                  attr_id - attribute ID to indicate.
235  *                  value - data to indicate.
236  *                  need_confirm - if this indication expects a confirmation or
237  *                                 not.
238  *
239  * Returns          None
240  *
241  ******************************************************************************/
BTA_GATTS_HandleValueIndication(uint16_t conn_id,uint16_t attr_id,std::vector<uint8_t> value,bool need_confirm)242 void BTA_GATTS_HandleValueIndication(uint16_t conn_id, uint16_t attr_id,
243                                      std::vector<uint8_t> value,
244                                      bool need_confirm) {
245 
246   if (value.size() > sizeof(tBTA_GATTS_API_INDICATION::value)) {
247     LOG(ERROR) << __func__ << "data to indicate is too long";
248     return;
249   }
250 
251   tBTA_GATTS_API_INDICATION* p_buf =
252       (tBTA_GATTS_API_INDICATION*)osi_calloc(sizeof(tBTA_GATTS_API_INDICATION));
253 
254   p_buf->hdr.event = BTA_GATTS_API_INDICATION_EVT;
255   p_buf->hdr.layer_specific = conn_id;
256   p_buf->attr_id = attr_id;
257   p_buf->need_confirm = need_confirm;
258   if (value.size() > 0) {
259     p_buf->len = value.size();
260     memcpy(p_buf->value, value.data(), value.size());
261   }
262 
263   bta_sys_sendmsg(p_buf);
264 }
265 
266 /*******************************************************************************
267  *
268  * Function         BTA_GATTS_SendRsp
269  *
270  * Description      This function is called to send a response to a request.
271  *
272  * Parameters       conn_id - connection identifier.
273  *                  trans_id - transaction ID.
274  *                  status - response status
275  *                  p_msg - response data.
276  *
277  * Returns          None
278  *
279  ******************************************************************************/
BTA_GATTS_SendRsp(uint16_t conn_id,uint32_t trans_id,tGATT_STATUS status,tGATTS_RSP * p_msg)280 void BTA_GATTS_SendRsp(uint16_t conn_id, uint32_t trans_id, tGATT_STATUS status,
281                        tGATTS_RSP* p_msg) {
282   const size_t len = sizeof(tBTA_GATTS_API_RSP) + sizeof(tGATTS_RSP);
283   tBTA_GATTS_API_RSP* p_buf = (tBTA_GATTS_API_RSP*)osi_calloc(len);
284 
285   p_buf->hdr.event = BTA_GATTS_API_RSP_EVT;
286   p_buf->hdr.layer_specific = conn_id;
287   p_buf->trans_id = trans_id;
288   p_buf->status = status;
289   if (p_msg != NULL) {
290     p_buf->p_rsp = (tGATTS_RSP*)(p_buf + 1);
291     memcpy(p_buf->p_rsp, p_msg, sizeof(tGATTS_RSP));
292   }
293 
294   bta_sys_sendmsg(p_buf);
295 }
296 
297 /*******************************************************************************
298  *
299  * Function         BTA_GATTS_Open
300  *
301  * Description      Open a direct open connection or add a background auto
302  *                  connection bd address
303  *
304  * Parameters       server_if: server interface.
305  *                  remote_bda: remote device BD address.
306  *                  is_direct: direct connection or background auto connection
307  *                  transport : Transport on which GATT connection to be opened
308  *                              (BR/EDR or LE)
309  *
310  * Returns          void
311  *
312  ******************************************************************************/
BTA_GATTS_Open(tGATT_IF server_if,const RawAddress & remote_bda,bool is_direct,tBT_TRANSPORT transport)313 void BTA_GATTS_Open(tGATT_IF server_if, const RawAddress& remote_bda,
314                     bool is_direct, tBT_TRANSPORT transport) {
315   tBTA_GATTS_API_OPEN* p_buf =
316       (tBTA_GATTS_API_OPEN*)osi_malloc(sizeof(tBTA_GATTS_API_OPEN));
317 
318   p_buf->hdr.event = BTA_GATTS_API_OPEN_EVT;
319   p_buf->server_if = server_if;
320   if (is_direct) {
321     p_buf->connection_type = BTM_BLE_DIRECT_CONNECTION;
322   } else {
323     p_buf->connection_type = BTM_BLE_BKG_CONNECT_ALLOW_LIST;
324   }
325   p_buf->transport = transport;
326   p_buf->remote_bda = remote_bda;
327 
328   bta_sys_sendmsg(p_buf);
329 }
330 
331 /*******************************************************************************
332  *
333  * Function         BTA_GATTS_CancelOpen
334  *
335  * Description      Cancel a direct open connection or remove a background auto
336  *                  connection bd address
337  *
338  * Parameters       server_if: server interface.
339  *                  remote_bda: remote device BD address.
340  *                  is_direct: direct connection or background auto connection
341  *
342  * Returns          void
343  *
344  ******************************************************************************/
BTA_GATTS_CancelOpen(tGATT_IF server_if,const RawAddress & remote_bda,bool is_direct)345 void BTA_GATTS_CancelOpen(tGATT_IF server_if, const RawAddress& remote_bda,
346                           bool is_direct) {
347   tBTA_GATTS_API_CANCEL_OPEN* p_buf = (tBTA_GATTS_API_CANCEL_OPEN*)osi_malloc(
348       sizeof(tBTA_GATTS_API_CANCEL_OPEN));
349 
350   p_buf->hdr.event = BTA_GATTS_API_CANCEL_OPEN_EVT;
351   p_buf->server_if = server_if;
352   p_buf->is_direct = is_direct;
353   p_buf->remote_bda = remote_bda;
354 
355   bta_sys_sendmsg(p_buf);
356 }
357 
358 /*******************************************************************************
359  *
360  * Function         BTA_GATTS_Close
361  *
362  * Description      Close a connection  a remote device.
363  *
364  * Parameters       conn_id: connectino ID to be closed.
365  *
366  * Returns          void
367  *
368  ******************************************************************************/
BTA_GATTS_Close(uint16_t conn_id)369 void BTA_GATTS_Close(uint16_t conn_id) {
370   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
371 
372   p_buf->event = BTA_GATTS_API_CLOSE_EVT;
373   p_buf->layer_specific = conn_id;
374 
375   bta_sys_sendmsg(p_buf);
376 }
377 
BTA_GATTS_InitBonded(void)378 void BTA_GATTS_InitBonded(void) {
379   LOG(INFO) << __func__;
380 
381   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
382   p_buf->event = BTA_GATTS_API_INIT_BONDED_EVT;
383   bta_sys_sendmsg(p_buf);
384 }
385