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/functional/bind.h>
26 #include <base/location.h>
27 #include <bluetooth/log.h>
28 
29 #include <cstdint>
30 #include <memory>
31 #include <vector>
32 
33 #include "bta/gatt/bta_gatts_int.h"
34 #include "internal_include/bt_target.h"
35 #include "osi/include/allocator.h"
36 #include "stack/include/bt_hdr.h"
37 #include "stack/include/main_thread.h"
38 #include "types/bluetooth/uuid.h"
39 #include "types/bt_transport.h"
40 #include "types/raw_address.h"
41 
42 // TODO(b/369381361) Enfore -Wmissing-prototypes
43 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
44 
45 using namespace bluetooth;
46 
47 /*****************************************************************************
48  *  Constants
49  ****************************************************************************/
50 
51 static const tBTA_SYS_REG bta_gatts_reg = {bta_gatts_hdl_event, BTA_GATTS_Disable};
52 
53 /*******************************************************************************
54  *
55  * Function         BTA_GATTS_Disable
56  *
57  * Description      This function is called to disable GATTS module
58  *
59  * Parameters       None.
60  *
61  * Returns          None
62  *
63  ******************************************************************************/
BTA_GATTS_Disable(void)64 void BTA_GATTS_Disable(void) {
65   if (!bta_sys_is_register(BTA_ID_GATTS)) {
66     log::warn("GATTS Module not enabled/already disabled");
67     return;
68   }
69 
70   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
71   p_buf->event = BTA_GATTS_API_DISABLE_EVT;
72   bta_sys_sendmsg(p_buf);
73   bta_sys_deregister(BTA_ID_GATTS);
74 }
75 
76 /*******************************************************************************
77  *
78  * Function         BTA_GATTS_AppRegister
79  *
80  * Description      This function is called to register application callbacks
81  *                    with BTA GATTS module.
82  *
83  * Parameters       p_app_uuid - application UUID
84  *                  p_cback - pointer to the application callback function.
85  *
86  * Returns          None
87  *
88  ******************************************************************************/
BTA_GATTS_AppRegister(const bluetooth::Uuid & app_uuid,tBTA_GATTS_CBACK * p_cback,bool eatt_support)89 void BTA_GATTS_AppRegister(const bluetooth::Uuid& app_uuid, tBTA_GATTS_CBACK* p_cback,
90                            bool eatt_support) {
91   tBTA_GATTS_API_REG* p_buf = (tBTA_GATTS_API_REG*)osi_malloc(sizeof(tBTA_GATTS_API_REG));
92 
93   /* register with BTA system manager */
94   if (!bta_sys_is_register(BTA_ID_GATTS)) {
95     bta_sys_register(BTA_ID_GATTS, &bta_gatts_reg);
96   }
97 
98   p_buf->hdr.event = BTA_GATTS_API_REG_EVT;
99   p_buf->app_uuid = app_uuid;
100   p_buf->p_cback = p_cback;
101   p_buf->eatt_support = eatt_support;
102 
103   bta_sys_sendmsg(p_buf);
104 }
105 
106 /*******************************************************************************
107  *
108  * Function         BTA_GATTS_AppDeregister
109  *
110  * Description      De-register with GATT Server.
111  *
112  * Parameters       app_id: applicatino ID.
113  *
114  * Returns          void
115  *
116  ******************************************************************************/
BTA_GATTS_AppDeregister(tGATT_IF server_if)117 void BTA_GATTS_AppDeregister(tGATT_IF server_if) {
118   tBTA_GATTS_API_DEREG* p_buf = (tBTA_GATTS_API_DEREG*)osi_malloc(sizeof(tBTA_GATTS_API_DEREG));
119 
120   p_buf->hdr.event = BTA_GATTS_API_DEREG_EVT;
121   p_buf->server_if = server_if;
122 
123   bta_sys_sendmsg(p_buf);
124 }
125 
bta_gatts_add_service_impl(tGATT_IF server_if,std::vector<btgatt_db_element_t> service,BTA_GATTS_AddServiceCb cb)126 void bta_gatts_add_service_impl(tGATT_IF server_if, std::vector<btgatt_db_element_t> service,
127                                 BTA_GATTS_AddServiceCb cb) {
128   uint8_t rcb_idx = bta_gatts_find_app_rcb_idx_by_app_if(&bta_gatts_cb, server_if);
129 
130   log::info("rcb_idx={}", rcb_idx);
131 
132   if (rcb_idx == BTA_GATTS_INVALID_APP) {
133     cb.Run(GATT_ERROR, server_if, std::move(service));
134     return;
135   }
136 
137   uint8_t srvc_idx = bta_gatts_alloc_srvc_cb(&bta_gatts_cb, rcb_idx);
138   if (srvc_idx == BTA_GATTS_INVALID_APP) {
139     cb.Run(GATT_ERROR, server_if, std::move(service));
140     return;
141   }
142 
143   tGATT_STATUS status = 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("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 void BTA_GATTS_AddService(tGATT_IF server_if, std::vector<btgatt_db_element_t> service,
177                           BTA_GATTS_AddServiceCb cb) {
178   do_in_main_thread(base::BindOnce(&bta_gatts_add_service_impl, server_if, std::move(service),
179                                    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, std::vector<uint8_t> value,
241                                      bool need_confirm) {
242   if (value.size() > sizeof(tBTA_GATTS_API_INDICATION::value)) {
243     log::error("data to indicate is too long");
244     return;
245   }
246 
247   tBTA_GATTS_API_INDICATION* p_buf =
248           (tBTA_GATTS_API_INDICATION*)osi_calloc(sizeof(tBTA_GATTS_API_INDICATION));
249 
250   p_buf->hdr.event = BTA_GATTS_API_INDICATION_EVT;
251   p_buf->hdr.layer_specific = conn_id;
252   p_buf->attr_id = attr_id;
253   p_buf->need_confirm = need_confirm;
254   if (value.size() > 0) {
255     p_buf->len = value.size();
256     memcpy(p_buf->value, value.data(), value.size());
257   }
258 
259   bta_sys_sendmsg(p_buf);
260 }
261 
262 /*******************************************************************************
263  *
264  * Function         BTA_GATTS_SendRsp
265  *
266  * Description      This function is called to send a response to a request.
267  *
268  * Parameters       conn_id - connection identifier.
269  *                  trans_id - transaction ID.
270  *                  status - response status
271  *                  p_msg - response data.
272  *
273  * Returns          None
274  *
275  ******************************************************************************/
BTA_GATTS_SendRsp(uint16_t conn_id,uint32_t trans_id,tGATT_STATUS status,tGATTS_RSP * p_msg)276 void BTA_GATTS_SendRsp(uint16_t conn_id, uint32_t trans_id, tGATT_STATUS status,
277                        tGATTS_RSP* p_msg) {
278   const size_t len = sizeof(tBTA_GATTS_API_RSP) + sizeof(tGATTS_RSP);
279   tBTA_GATTS_API_RSP* p_buf = (tBTA_GATTS_API_RSP*)osi_calloc(len);
280 
281   p_buf->hdr.event = BTA_GATTS_API_RSP_EVT;
282   p_buf->hdr.layer_specific = conn_id;
283   p_buf->trans_id = trans_id;
284   p_buf->status = status;
285   if (p_msg != NULL) {
286     p_buf->p_rsp = (tGATTS_RSP*)(p_buf + 1);
287     memcpy(p_buf->p_rsp, p_msg, sizeof(tGATTS_RSP));
288   }
289 
290   bta_sys_sendmsg(p_buf);
291 }
292 
293 /*******************************************************************************
294  *
295  * Function         BTA_GATTS_Open
296  *
297  * Description      Open a direct open connection or add a background auto
298  *                  connection bd address
299  *
300  * Parameters       server_if: server interface.
301  *                  remote_bda: remote device BD address.
302  *                  is_direct: direct connection or background auto connection
303  *                  transport : Transport on which GATT connection to be opened
304  *                              (BR/EDR or LE)
305  *
306  * Returns          void
307  *
308  ******************************************************************************/
BTA_GATTS_Open(tGATT_IF server_if,const RawAddress & remote_bda,tBLE_ADDR_TYPE addr_type,bool is_direct,tBT_TRANSPORT transport)309 void BTA_GATTS_Open(tGATT_IF server_if, const RawAddress& remote_bda, tBLE_ADDR_TYPE addr_type,
310                     bool is_direct, tBT_TRANSPORT transport) {
311   tBTA_GATTS_API_OPEN* p_buf = (tBTA_GATTS_API_OPEN*)osi_malloc(sizeof(tBTA_GATTS_API_OPEN));
312 
313   p_buf->hdr.event = BTA_GATTS_API_OPEN_EVT;
314   p_buf->server_if = server_if;
315   if (is_direct) {
316     p_buf->connection_type = BTM_BLE_DIRECT_CONNECTION;
317   } else {
318     p_buf->connection_type = BTM_BLE_BKG_CONNECT_ALLOW_LIST;
319   }
320   p_buf->transport = transport;
321   p_buf->remote_bda = remote_bda;
322   p_buf->remote_addr_type = addr_type;
323 
324   bta_sys_sendmsg(p_buf);
325 }
326 
327 /*******************************************************************************
328  *
329  * Function         BTA_GATTS_CancelOpen
330  *
331  * Description      Cancel a direct open connection or remove a background auto
332  *                  connection bd address
333  *
334  * Parameters       server_if: server interface.
335  *                  remote_bda: remote device BD address.
336  *                  is_direct: direct connection or background auto connection
337  *
338  * Returns          void
339  *
340  ******************************************************************************/
BTA_GATTS_CancelOpen(tGATT_IF server_if,const RawAddress & remote_bda,bool is_direct)341 void BTA_GATTS_CancelOpen(tGATT_IF server_if, const RawAddress& remote_bda, bool is_direct) {
342   tBTA_GATTS_API_CANCEL_OPEN* p_buf =
343           (tBTA_GATTS_API_CANCEL_OPEN*)osi_malloc(sizeof(tBTA_GATTS_API_CANCEL_OPEN));
344 
345   p_buf->hdr.event = BTA_GATTS_API_CANCEL_OPEN_EVT;
346   p_buf->server_if = server_if;
347   p_buf->is_direct = is_direct;
348   p_buf->remote_bda = remote_bda;
349 
350   bta_sys_sendmsg(p_buf);
351 }
352 
353 /*******************************************************************************
354  *
355  * Function         BTA_GATTS_Close
356  *
357  * Description      Close a connection  a remote device.
358  *
359  * Parameters       conn_id: connectino ID to be closed.
360  *
361  * Returns          void
362  *
363  ******************************************************************************/
BTA_GATTS_Close(uint16_t conn_id)364 void BTA_GATTS_Close(uint16_t conn_id) {
365   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
366 
367   p_buf->event = BTA_GATTS_API_CLOSE_EVT;
368   p_buf->layer_specific = conn_id;
369 
370   bta_sys_sendmsg(p_buf);
371 }
372 
BTA_GATTS_InitBonded(void)373 void BTA_GATTS_InitBonded(void) {
374   log::info("");
375 
376   BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
377   p_buf->event = BTA_GATTS_API_INIT_BONDED_EVT;
378   bta_sys_sendmsg(p_buf);
379 }
380