1 /******************************************************************************
3 * Copyright 2010-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This is the implementation of the API for GATT server of BTA.
23 ******************************************************************************/
25 #include "bt_target.h"
27 #include <base/bind.h>
30 #include "bt_common.h"
31 #include "bta_gatt_api.h"
32 #include "bta_gatts_int.h"
34 #include "stack/include/btu.h"
35 #include "types/bt_transport.h"
37 /*****************************************************************************
39 ****************************************************************************/
41 static const tBTA_SYS_REG bta_gatts_reg = {bta_gatts_hdl_event,
44 /*******************************************************************************
46 * Function BTA_GATTS_Disable
48 * Description This function is called to disable GATTS module
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";
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);
67 /*******************************************************************************
69 * Function BTA_GATTS_AppRegister
71 * Description This function is called to register application callbacks
72 * with BTA GATTS module.
74 * Parameters p_app_uuid - applicaiton UUID
75 * p_cback - pointer to the application callback function.
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));
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);
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;
94 bta_sys_sendmsg(p_buf);
97 /*******************************************************************************
99 * Function BTA_GATTS_AppDeregister
101 * Description De-register with GATT Server.
103 * Parameters app_id: applicatino ID.
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));
112 p_buf->hdr.event = BTA_GATTS_API_DEREG_EVT;
113 p_buf->server_if = server_if;
115 bta_sys_sendmsg(p_buf);
118 void bta_gatts_add_service_impl(tGATT_IF server_if,
119 std::vector<btgatt_db_element_t> service,
120 BTA_GATTS_AddServiceCb cb) {
122 bta_gatts_find_app_rcb_idx_by_app_if(&bta_gatts_cb, server_if);
124 LOG(INFO) << __func__ << ": rcb_idx=" << +rcb_idx;
126 if (rcb_idx == BTA_GATTS_INVALID_APP) {
127 cb.Run(GATT_ERROR, server_if, std::move(service));
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));
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));
146 bta_gatts_cb.srvc_cb[srvc_idx].service_uuid = service[0].uuid;
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;
152 cb.Run(GATT_SUCCESS, server_if, std::move(service));
156 /*******************************************************************************
158 * Function BTA_GATTS_AddService
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.
164 * Parameters server_if: server interface.
165 * service: pointer vector describing service.
167 * Returns Returns |GATT_SUCCESS| on success or |GATT_ERROR| if the
168 * service cannot be added.
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)));
179 /*******************************************************************************
181 * Function BTA_GATTS_DeleteService
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
187 * Parameters service_id: service_id to be deleted.
189 * Returns returns none.
191 ******************************************************************************/
192 void BTA_GATTS_DeleteService(uint16_t service_id) {
193 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
195 p_buf->event = BTA_GATTS_API_DEL_SRVC_EVT;
196 p_buf->layer_specific = service_id;
198 bta_sys_sendmsg(p_buf);
201 /*******************************************************************************
203 * Function BTA_GATTS_StopService
205 * Description This function is called to stop a service.
207 * Parameters service_id - service to be topped.
211 ******************************************************************************/
212 void BTA_GATTS_StopService(uint16_t service_id) {
213 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
215 p_buf->event = BTA_GATTS_API_STOP_SRVC_EVT;
216 p_buf->layer_specific = service_id;
218 bta_sys_sendmsg(p_buf);
221 /*******************************************************************************
223 * Function BTA_GATTS_HandleValueIndication
225 * Description This function is called to read a characteristics
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
236 ******************************************************************************/
237 void BTA_GATTS_HandleValueIndication(uint16_t conn_id, uint16_t attr_id,
238 std::vector<uint8_t> value,
240 tBTA_GATTS_API_INDICATION* p_buf =
241 (tBTA_GATTS_API_INDICATION*)osi_calloc(sizeof(tBTA_GATTS_API_INDICATION));
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());
252 bta_sys_sendmsg(p_buf);
255 /*******************************************************************************
257 * Function BTA_GATTS_SendRsp
259 * Description This function is called to send a response to a request.
261 * Parameters conn_id - connection identifier.
262 * trans_id - transaction ID.
263 * status - response status
264 * p_msg - response data.
268 ******************************************************************************/
269 void BTA_GATTS_SendRsp(uint16_t conn_id, uint32_t trans_id, tGATT_STATUS status,
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);
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;
279 p_buf->p_rsp = (tGATTS_RSP*)(p_buf + 1);
280 memcpy(p_buf->p_rsp, p_msg, sizeof(tGATTS_RSP));
283 bta_sys_sendmsg(p_buf);
286 /*******************************************************************************
288 * Function BTA_GATTS_Open
290 * Description Open a direct open connection or add a background auto
291 * connection bd address
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
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));
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;
313 bta_sys_sendmsg(p_buf);
316 /*******************************************************************************
318 * Function BTA_GATTS_CancelOpen
320 * Description Cancel a direct open connection or remove a background auto
321 * connection bd address
323 * Parameters server_if: server interface.
324 * remote_bda: remote device BD address.
325 * is_direct: direct connection or background auto connection
329 ******************************************************************************/
330 void BTA_GATTS_CancelOpen(tGATT_IF server_if, const RawAddress& remote_bda,
332 tBTA_GATTS_API_CANCEL_OPEN* p_buf = (tBTA_GATTS_API_CANCEL_OPEN*)osi_malloc(
333 sizeof(tBTA_GATTS_API_CANCEL_OPEN));
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;
340 bta_sys_sendmsg(p_buf);
343 /*******************************************************************************
345 * Function BTA_GATTS_Close
347 * Description Close a connection a remote device.
349 * Parameters conn_id: connectino ID to be closed.
353 ******************************************************************************/
354 void BTA_GATTS_Close(uint16_t conn_id) {
355 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
357 p_buf->event = BTA_GATTS_API_CLOSE_EVT;
358 p_buf->layer_specific = conn_id;
360 bta_sys_sendmsg(p_buf);