OSDN Git Service

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