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