1 /******************************************************************************
3 * Copyright (C) 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 #if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
30 #include "bt_common.h"
32 #include "bta_gatt_api.h"
33 #include "bta_gatts_int.h"
35 /*****************************************************************************
37 *****************************************************************************/
39 static const tBTA_SYS_REG bta_gatts_reg =
45 /*******************************************************************************
47 ** Function BTA_GATTS_Disable
49 ** Description This function is called to disable GATTS module
55 *******************************************************************************/
56 void BTA_GATTS_Disable(void)
58 if (bta_sys_is_register(BTA_ID_GATTS) == FALSE)
60 APPL_TRACE_WARNING("GATTS Module not enabled/already disabled");
64 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR));
65 p_buf->event = BTA_GATTS_API_DISABLE_EVT;
66 bta_sys_sendmsg(p_buf);
67 bta_sys_deregister(BTA_ID_GATTS);
70 /*******************************************************************************
72 ** Function BTA_GATTS_AppRegister
74 ** Description This function is called to register application callbacks
75 ** with BTA GATTS module.
77 ** Parameters p_app_uuid - applicaiton UUID
78 ** p_cback - pointer to the application callback function.
82 *******************************************************************************/
83 void BTA_GATTS_AppRegister(tBT_UUID *p_app_uuid, tBTA_GATTS_CBACK *p_cback)
85 tBTA_GATTS_API_REG *p_buf =
86 (tBTA_GATTS_API_REG *)osi_malloc(sizeof(tBTA_GATTS_API_REG));
88 /* register with BTA system manager */
89 if (bta_sys_is_register(BTA_ID_GATTS) == FALSE)
90 bta_sys_register(BTA_ID_GATTS, &bta_gatts_reg);
92 p_buf->hdr.event = BTA_GATTS_API_REG_EVT;
93 if (p_app_uuid != NULL)
94 memcpy(&p_buf->app_uuid, p_app_uuid, sizeof(tBT_UUID));
95 p_buf->p_cback = p_cback;
97 bta_sys_sendmsg(p_buf);
102 /*******************************************************************************
104 ** Function BTA_GATTS_AppDeregister
106 ** Description De-register with GATT Server.
108 ** Parameters app_id: applicatino ID.
112 *******************************************************************************/
113 void BTA_GATTS_AppDeregister(tBTA_GATTS_IF server_if)
115 tBTA_GATTS_API_DEREG *p_buf =
116 (tBTA_GATTS_API_DEREG *)osi_malloc(sizeof(tBTA_GATTS_API_DEREG));
118 p_buf->hdr.event = BTA_GATTS_API_DEREG_EVT;
119 p_buf->server_if = server_if;
121 bta_sys_sendmsg(p_buf);
124 /*******************************************************************************
126 ** Function BTA_GATTS_CreateService
128 ** Description Create a service. When service creation is done, a callback
129 ** event BTA_GATTS_CREATE_SRVC_EVT is called to report status
130 ** and service ID to the profile. The service ID obtained in
131 ** the callback function needs to be used when adding included
132 ** service and characteristics/descriptors into the service.
134 ** Parameters app_id: Profile ID this service is belonged to.
135 ** p_service_uuid: service UUID.
136 ** inst: instance ID number of this service.
137 ** num_handle: numble of handle requessted for this service.
138 ** is_primary: is this service a primary one or not.
142 *******************************************************************************/
143 void BTA_GATTS_CreateService(tBTA_GATTS_IF server_if, tBT_UUID *p_service_uuid, UINT8 inst,
144 UINT16 num_handle, BOOLEAN is_primary)
146 tBTA_GATTS_API_CREATE_SRVC *p_buf =
147 (tBTA_GATTS_API_CREATE_SRVC *)osi_malloc(sizeof(tBTA_GATTS_API_CREATE_SRVC));
149 p_buf->hdr.event = BTA_GATTS_API_CREATE_SRVC_EVT;
150 p_buf->server_if = server_if;
152 memcpy(&p_buf->service_uuid, p_service_uuid, sizeof(tBT_UUID));
153 p_buf->num_handle = num_handle;
154 p_buf->is_pri = is_primary;
156 bta_sys_sendmsg(p_buf);
158 /*******************************************************************************
160 ** Function BTA_GATTS_AddIncludeService
162 ** Description This function is called to add an included service. After included
163 ** service is included, a callback event BTA_GATTS_ADD_INCL_SRVC_EVT
164 ** is reported the included service ID.
166 ** Parameters service_id: service ID to which this included service is to
168 ** included_service_id: the service ID to be included.
172 *******************************************************************************/
173 void BTA_GATTS_AddIncludeService(UINT16 service_id, UINT16 included_service_id)
175 tBTA_GATTS_API_ADD_INCL_SRVC *p_buf =
176 (tBTA_GATTS_API_ADD_INCL_SRVC *)osi_malloc(sizeof(tBTA_GATTS_API_ADD_INCL_SRVC));
178 p_buf->hdr.event = BTA_GATTS_API_ADD_INCL_SRVC_EVT;
179 p_buf->hdr.layer_specific = service_id;
180 p_buf->included_service_id = included_service_id;
182 bta_sys_sendmsg(p_buf);
185 /*******************************************************************************
187 ** Function BTA_GATTS_AddCharacteristic
189 ** Description This function is called to add a characteristic into a service.
191 ** Parameters service_id: service ID to which this included service is to
193 ** p_char_uuid : Characteristic UUID.
194 ** perm : Characteristic value declaration attribute permission.
195 ** property : Characteristic Properties
199 *******************************************************************************/
200 void BTA_GATTS_AddCharacteristic (UINT16 service_id, tBT_UUID *p_char_uuid,
201 tBTA_GATT_PERM perm, tBTA_GATT_CHAR_PROP property)
203 tBTA_GATTS_API_ADD_CHAR *p_buf =
204 (tBTA_GATTS_API_ADD_CHAR *)osi_calloc(sizeof(tBTA_GATTS_API_ADD_CHAR));
206 p_buf->hdr.event = BTA_GATTS_API_ADD_CHAR_EVT;
207 p_buf->hdr.layer_specific = service_id;
209 p_buf->property = property;
212 memcpy(&p_buf->char_uuid, p_char_uuid, sizeof(tBT_UUID));
214 bta_sys_sendmsg(p_buf);
217 /*******************************************************************************
219 ** Function BTA_GATTS_AddCharDescriptor
221 ** Description This function is called to add characteristic descriptor. When
222 ** it's done, a callback event BTA_GATTS_ADD_DESCR_EVT is called
223 ** to report the status and an ID number for this descriptor.
225 ** Parameters service_id: service ID to which this charatceristic descriptor is to
227 ** perm: descriptor access permission.
228 ** p_descr_uuid: descriptor UUID.
230 ** Returns returns status.
232 *******************************************************************************/
233 void BTA_GATTS_AddCharDescriptor (UINT16 service_id,
235 tBT_UUID * p_descr_uuid)
237 tBTA_GATTS_API_ADD_DESCR *p_buf =
238 (tBTA_GATTS_API_ADD_DESCR *)osi_calloc(sizeof(tBTA_GATTS_API_ADD_DESCR));
240 p_buf->hdr.event = BTA_GATTS_API_ADD_DESCR_EVT;
241 p_buf->hdr.layer_specific = service_id;
244 memcpy(&p_buf->descr_uuid, p_descr_uuid, sizeof(tBT_UUID));
246 bta_sys_sendmsg(p_buf);
249 /*******************************************************************************
251 ** Function BTA_GATTS_DeleteService
253 ** Description This function is called to delete a service. When this is done,
254 ** a callback event BTA_GATTS_DELETE_EVT is report with the status.
256 ** Parameters service_id: service_id to be deleted.
258 ** Returns returns none.
260 *******************************************************************************/
261 void BTA_GATTS_DeleteService(UINT16 service_id)
263 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR));
265 p_buf->event = BTA_GATTS_API_DEL_SRVC_EVT;
266 p_buf->layer_specific = service_id;
268 bta_sys_sendmsg(p_buf);
271 /*******************************************************************************
273 ** Function BTA_GATTS_StartService
275 ** Description This function is called to start a service.
277 ** Parameters service_id: the service ID to be started.
278 ** sup_transport: supported trasnport.
282 *******************************************************************************/
283 void BTA_GATTS_StartService(UINT16 service_id, tBTA_GATT_TRANSPORT sup_transport)
285 tBTA_GATTS_API_START *p_buf =
286 (tBTA_GATTS_API_START *)osi_malloc(sizeof(tBTA_GATTS_API_START));
288 p_buf->hdr.event = BTA_GATTS_API_START_SRVC_EVT;
289 p_buf->hdr.layer_specific = service_id;
290 p_buf->transport = sup_transport;
292 bta_sys_sendmsg(p_buf);
295 /*******************************************************************************
297 ** Function BTA_GATTS_StopService
299 ** Description This function is called to stop a service.
301 ** Parameters service_id - service to be topped.
305 *******************************************************************************/
306 void BTA_GATTS_StopService(UINT16 service_id)
308 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR));
310 p_buf->event = BTA_GATTS_API_STOP_SRVC_EVT;
311 p_buf->layer_specific = service_id;
313 bta_sys_sendmsg(p_buf);
316 /*******************************************************************************
318 ** Function BTA_GATTS_HandleValueIndication
320 ** Description This function is called to read a characteristics descriptor.
322 ** Parameters bda - remote device bd address to indicate.
323 ** attr_id - attribute ID to indicate.
324 ** data_len - indicate data length.
325 ** p_data: data to indicate.
326 ** need_confirm - if this indication expects a confirmation or not.
330 *******************************************************************************/
331 void BTA_GATTS_HandleValueIndication (UINT16 conn_id, UINT16 attr_id, UINT16 data_len,
332 UINT8 *p_data, BOOLEAN need_confirm)
334 tBTA_GATTS_API_INDICATION *p_buf =
335 (tBTA_GATTS_API_INDICATION *)osi_calloc(sizeof(tBTA_GATTS_API_INDICATION));
337 p_buf->hdr.event = BTA_GATTS_API_INDICATION_EVT;
338 p_buf->hdr.layer_specific = conn_id;
339 p_buf->attr_id = attr_id;
340 p_buf->need_confirm = need_confirm;
341 if (data_len > 0 && p_data != NULL) {
342 p_buf->len = data_len;
343 memcpy(p_buf->value, p_data, data_len);
346 bta_sys_sendmsg(p_buf);
349 /*******************************************************************************
351 ** Function BTA_GATTS_SendRsp
353 ** Description This function is called to send a response to a request.
355 ** Parameters conn_id - connection identifier.
356 ** trans_id - transaction ID.
357 ** status - response status
358 ** p_msg - response data.
362 *******************************************************************************/
363 void BTA_GATTS_SendRsp (UINT16 conn_id, UINT32 trans_id,
364 tBTA_GATT_STATUS status, tBTA_GATTS_RSP *p_msg)
366 const size_t len = sizeof(tBTA_GATTS_API_RSP) + sizeof(tBTA_GATTS_RSP);
367 tBTA_GATTS_API_RSP *p_buf = (tBTA_GATTS_API_RSP *)osi_calloc(len);
369 p_buf->hdr.event = BTA_GATTS_API_RSP_EVT;
370 p_buf->hdr.layer_specific = conn_id;
371 p_buf->trans_id = trans_id;
372 p_buf->status = status;
374 p_buf->p_rsp = (tBTA_GATTS_RSP *)(p_buf + 1);
375 memcpy(p_buf->p_rsp, p_msg, sizeof(tBTA_GATTS_RSP));
378 bta_sys_sendmsg(p_buf);
381 /*******************************************************************************
383 ** Function BTA_GATTS_Open
385 ** Description Open a direct open connection or add a background auto connection
388 ** Parameters server_if: server interface.
389 ** remote_bda: remote device BD address.
390 ** is_direct: direct connection or background auto connection
391 ** transport : Transport on which GATT connection to be opened (BR/EDR or LE)
395 *******************************************************************************/
396 void BTA_GATTS_Open(tBTA_GATTS_IF server_if, BD_ADDR remote_bda, BOOLEAN is_direct,
397 tBTA_GATT_TRANSPORT transport)
399 tBTA_GATTS_API_OPEN *p_buf =
400 (tBTA_GATTS_API_OPEN *)osi_malloc(sizeof(tBTA_GATTS_API_OPEN));
402 p_buf->hdr.event = BTA_GATTS_API_OPEN_EVT;
403 p_buf->server_if = server_if;
404 p_buf->is_direct = is_direct;
405 p_buf->transport = transport;
406 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
408 bta_sys_sendmsg(p_buf);
411 /*******************************************************************************
413 ** Function BTA_GATTS_CancelOpen
415 ** Description Cancel a direct open connection or remove a background auto connection
418 ** Parameters server_if: server interface.
419 ** remote_bda: remote device BD address.
420 ** is_direct: direct connection or background auto connection
424 *******************************************************************************/
425 void BTA_GATTS_CancelOpen(tBTA_GATTS_IF server_if, BD_ADDR remote_bda, BOOLEAN is_direct)
427 tBTA_GATTS_API_CANCEL_OPEN *p_buf =
428 (tBTA_GATTS_API_CANCEL_OPEN *)osi_malloc(sizeof(tBTA_GATTS_API_CANCEL_OPEN));
430 p_buf->hdr.event = BTA_GATTS_API_CANCEL_OPEN_EVT;
431 p_buf->server_if = server_if;
432 p_buf->is_direct = is_direct;
433 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
435 bta_sys_sendmsg(p_buf);
438 /*******************************************************************************
440 ** Function BTA_GATTS_Close
442 ** Description Close a connection a remote device.
444 ** Parameters conn_id: connectino ID to be closed.
448 *******************************************************************************/
449 void BTA_GATTS_Close(UINT16 conn_id)
451 BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(BT_HDR));
453 p_buf->event = BTA_GATTS_API_CLOSE_EVT;
454 p_buf->layer_specific = conn_id;
456 bta_sys_sendmsg(p_buf);
459 /*******************************************************************************
461 ** Function BTA_GATTS_Listen
463 ** Description Start advertisement to listen for connection request for a
466 ** Parameters server_if: server interface.
467 ** start: to start or stop listening for connection
468 ** remote_bda: remote device BD address, if listen to all device
473 *******************************************************************************/
474 void BTA_GATTS_Listen(tBTA_GATTS_IF server_if, BOOLEAN start, BD_ADDR_PTR target_bda)
476 tBTA_GATTS_API_LISTEN *p_buf =
477 (tBTA_GATTS_API_LISTEN *)osi_malloc(sizeof(tBTA_GATTS_API_LISTEN) + BD_ADDR_LEN);
479 p_buf->hdr.event = BTA_GATTS_API_LISTEN_EVT;
480 p_buf->server_if = server_if;
481 p_buf->start = start;
483 p_buf->remote_bda = (UINT8 *)(p_buf + 1);
484 memcpy(p_buf->remote_bda, target_bda, BD_ADDR_LEN);
486 p_buf->remote_bda = NULL;
489 bta_sys_sendmsg(p_buf);
492 #endif /* BTA_GATT_INCLUDED */