1 /******************************************************************************
2 *
3 * Copyright (C) 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 #if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
28
29 #include <string.h>
30 #include "gki.h"
31 #include "bta_sys.h"
32 #include "bta_gatt_api.h"
33 #include "bta_gatts_int.h"
34
35 /*****************************************************************************
36 ** Constants
37 *****************************************************************************/
38
39 static const tBTA_SYS_REG bta_gatts_reg =
40 {
41 bta_gatts_hdl_event,
42 BTA_GATTS_Disable
43 };
44
45 /*******************************************************************************
46 **
47 ** Function BTA_GATTS_Disable
48 **
49 ** Description This function is called to disable GATTS module
50 **
51 ** Parameters None.
52 **
53 ** Returns None
54 **
55 *******************************************************************************/
BTA_GATTS_Disable(void)56 void BTA_GATTS_Disable(void)
57 {
58 BT_HDR *p_buf;
59
60 if (bta_sys_is_register(BTA_ID_GATTS) == FALSE)
61 {
62 APPL_TRACE_WARNING0("GATTS Module not enabled/already disabled");
63 return;
64 }
65
66 if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
67 {
68 p_buf->event = BTA_GATTS_API_DISABLE_EVT;
69 bta_sys_sendmsg(p_buf);
70 }
71 bta_sys_deregister(BTA_ID_GATTS);
72
73 }
74
75 /*******************************************************************************
76 **
77 ** Function BTA_GATTS_AppRegister
78 **
79 ** Description This function is called to register application callbacks
80 ** with BTA GATTS module.
81 **
82 ** Parameters p_app_uuid - applicaiton UUID
83 ** p_cback - pointer to the application callback function.
84 **
85 ** Returns None
86 **
87 *******************************************************************************/
BTA_GATTS_AppRegister(tBT_UUID * p_app_uuid,tBTA_GATTS_CBACK * p_cback)88 void BTA_GATTS_AppRegister(tBT_UUID *p_app_uuid, tBTA_GATTS_CBACK *p_cback)
89 {
90 tBTA_GATTS_API_REG *p_buf;
91
92 /* register with BTA system manager */
93 if (bta_sys_is_register(BTA_ID_GATTS) == FALSE)
94 {
95 GKI_sched_lock();
96 bta_sys_register(BTA_ID_GATTS, &bta_gatts_reg);
97 GKI_sched_unlock();
98 }
99
100 if ((p_buf = (tBTA_GATTS_API_REG *) GKI_getbuf(sizeof(tBTA_GATTS_API_REG))) != NULL)
101 {
102 p_buf->hdr.event = BTA_GATTS_API_REG_EVT;
103
104 if (p_app_uuid != NULL)
105 memcpy(&p_buf->app_uuid, p_app_uuid, sizeof(tBT_UUID));
106 p_buf->p_cback = p_cback;
107
108 bta_sys_sendmsg(p_buf);
109 }
110 return;
111 }
112
113
114
115 /*******************************************************************************
116 **
117 ** Function BTA_GATTS_AppDeregister
118 **
119 ** Description De-register with GATT Server.
120 **
121 ** Parameters app_id: applicatino ID.
122 **
123 ** Returns void
124 **
125 *******************************************************************************/
BTA_GATTS_AppDeregister(tBTA_GATTS_IF server_if)126 void BTA_GATTS_AppDeregister(tBTA_GATTS_IF server_if)
127 {
128 tBTA_GATTS_API_DEREG *p_buf;
129
130 if ((p_buf = (tBTA_GATTS_API_DEREG *) GKI_getbuf(sizeof(tBTA_GATTS_API_DEREG))) != NULL)
131 {
132 p_buf->hdr.event = BTA_GATTS_API_DEREG_EVT;
133 p_buf->server_if = server_if;
134
135 bta_sys_sendmsg(p_buf);
136 }
137 return;
138 }
139
140 /*******************************************************************************
141 **
142 ** Function BTA_GATTS_CreateService
143 **
144 ** Description Create a service. When service creation is done, a callback
145 ** event BTA_GATTS_CREATE_SRVC_EVT is called to report status
146 ** and service ID to the profile. The service ID obtained in
147 ** the callback function needs to be used when adding included
148 ** service and characteristics/descriptors into the service.
149 **
150 ** Parameters app_id: Profile ID this service is belonged to.
151 ** p_service_uuid: service UUID.
152 ** inst: instance ID number of this service.
153 ** num_handle: numble of handle requessted for this service.
154 ** is_primary: is this service a primary one or not.
155 **
156 ** Returns void
157 **
158 *******************************************************************************/
BTA_GATTS_CreateService(tBTA_GATTS_IF server_if,tBT_UUID * p_service_uuid,UINT8 inst,UINT16 num_handle,BOOLEAN is_primary)159 void BTA_GATTS_CreateService(tBTA_GATTS_IF server_if, tBT_UUID *p_service_uuid, UINT8 inst,
160 UINT16 num_handle, BOOLEAN is_primary)
161 {
162 tBTA_GATTS_API_CREATE_SRVC *p_buf;
163
164 if ((p_buf = (tBTA_GATTS_API_CREATE_SRVC *) GKI_getbuf(sizeof(tBTA_GATTS_API_CREATE_SRVC))) != NULL)
165 {
166 p_buf->hdr.event = BTA_GATTS_API_CREATE_SRVC_EVT;
167
168 p_buf->server_if = server_if;
169 p_buf->inst = inst;
170 memcpy(&p_buf->service_uuid, p_service_uuid, sizeof(tBT_UUID));
171 p_buf->num_handle = num_handle;
172 p_buf->is_pri = is_primary;
173
174 bta_sys_sendmsg(p_buf);
175 }
176 return;
177 }
178 /*******************************************************************************
179 **
180 ** Function BTA_GATTS_AddIncludeService
181 **
182 ** Description This function is called to add an included service. After included
183 ** service is included, a callback event BTA_GATTS_ADD_INCL_SRVC_EVT
184 ** is reported the included service ID.
185 **
186 ** Parameters service_id: service ID to which this included service is to
187 ** be added.
188 ** included_service_id: the service ID to be included.
189 **
190 ** Returns void
191 **
192 *******************************************************************************/
BTA_GATTS_AddIncludeService(UINT16 service_id,UINT16 included_service_id)193 void BTA_GATTS_AddIncludeService(UINT16 service_id, UINT16 included_service_id)
194 {
195 tBTA_GATTS_API_ADD_INCL_SRVC *p_buf;
196
197 if ((p_buf =
198 (tBTA_GATTS_API_ADD_INCL_SRVC *) GKI_getbuf(sizeof(tBTA_GATTS_API_ADD_INCL_SRVC)))
199 != NULL)
200 {
201 p_buf->hdr.event = BTA_GATTS_API_ADD_INCL_SRVC_EVT;
202
203 p_buf->hdr.layer_specific = service_id;
204 p_buf->included_service_id = included_service_id;
205
206 bta_sys_sendmsg(p_buf);
207 }
208 return;
209
210 }
211 /*******************************************************************************
212 **
213 ** Function BTA_GATTS_AddCharacteristic
214 **
215 ** Description This function is called to add a characteristic into a service.
216 **
217 ** Parameters service_id: service ID to which this included service is to
218 ** be added.
219 ** p_char_uuid : Characteristic UUID.
220 ** perm : Characteristic value declaration attribute permission.
221 ** property : Characteristic Properties
222 **
223 ** Returns None
224 **
225 *******************************************************************************/
BTA_GATTS_AddCharacteristic(UINT16 service_id,tBT_UUID * p_char_uuid,tBTA_GATT_PERM perm,tBTA_GATT_CHAR_PROP property)226 void BTA_GATTS_AddCharacteristic (UINT16 service_id, tBT_UUID *p_char_uuid,
227 tBTA_GATT_PERM perm, tBTA_GATT_CHAR_PROP property)
228 {
229 tBTA_GATTS_API_ADD_CHAR *p_buf;
230
231 if ((p_buf = (tBTA_GATTS_API_ADD_CHAR *) GKI_getbuf(sizeof(tBTA_GATTS_API_ADD_CHAR))) != NULL)
232 {
233 memset(p_buf, 0, sizeof(tBTA_GATTS_API_ADD_CHAR));
234
235 p_buf->hdr.event = BTA_GATTS_API_ADD_CHAR_EVT;
236 p_buf->hdr.layer_specific = service_id;
237 p_buf->perm = perm;
238 p_buf->property = property;
239
240 if (p_char_uuid)
241 {
242 memcpy(&p_buf->char_uuid, p_char_uuid, sizeof(tBT_UUID));
243 }
244 bta_sys_sendmsg(p_buf);
245 }
246 return;
247 }
248
249 /*******************************************************************************
250 **
251 ** Function BTA_GATTS_AddCharDescriptor
252 **
253 ** Description This function is called to add characteristic descriptor. When
254 ** it's done, a callback event BTA_GATTS_ADD_DESCR_EVT is called
255 ** to report the status and an ID number for this descriptor.
256 **
257 ** Parameters service_id: service ID to which this charatceristic descriptor is to
258 ** be added.
259 ** perm: descriptor access permission.
260 ** p_descr_uuid: descriptor UUID.
261 **
262 ** Returns returns status.
263 **
264 *******************************************************************************/
BTA_GATTS_AddCharDescriptor(UINT16 service_id,tBTA_GATT_PERM perm,tBT_UUID * p_descr_uuid)265 void BTA_GATTS_AddCharDescriptor (UINT16 service_id,
266 tBTA_GATT_PERM perm,
267 tBT_UUID * p_descr_uuid)
268 {
269 tBTA_GATTS_API_ADD_DESCR *p_buf;
270 UINT16 len = sizeof(tBTA_GATTS_API_ADD_DESCR);
271
272
273 if ((p_buf = (tBTA_GATTS_API_ADD_DESCR *) GKI_getbuf(len)) != NULL)
274 {
275 memset(p_buf, 0, len);
276
277 p_buf->hdr.event = BTA_GATTS_API_ADD_DESCR_EVT;
278 p_buf->hdr.layer_specific = service_id;
279 p_buf->perm = perm;
280
281 if (p_descr_uuid)
282 {
283 memcpy(&p_buf->descr_uuid, p_descr_uuid, sizeof(tBT_UUID));
284 }
285 bta_sys_sendmsg(p_buf);
286 }
287 return;
288
289 }
290
291 /*******************************************************************************
292 **
293 ** Function BTA_GATTS_DeleteService
294 **
295 ** Description This function is called to delete a service. When this is done,
296 ** a callback event BTA_GATTS_DELETE_EVT is report with the status.
297 **
298 ** Parameters service_id: service_id to be deleted.
299 **
300 ** Returns returns none.
301 **
302 *******************************************************************************/
BTA_GATTS_DeleteService(UINT16 service_id)303 void BTA_GATTS_DeleteService(UINT16 service_id)
304 {
305 BT_HDR *p_buf;
306
307 if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
308 {
309 p_buf->event = BTA_GATTS_API_DEL_SRVC_EVT;
310
311 p_buf->layer_specific = service_id;
312
313 bta_sys_sendmsg(p_buf);
314 }
315 return;
316
317 }
318
319 /*******************************************************************************
320 **
321 ** Function BTA_GATTS_StartService
322 **
323 ** Description This function is called to start a service.
324 **
325 ** Parameters service_id: the service ID to be started.
326 ** sup_transport: supported trasnport.
327 **
328 ** Returns None.
329 **
330 *******************************************************************************/
BTA_GATTS_StartService(UINT16 service_id,tBTA_GATT_TRANSPORT sup_transport)331 void BTA_GATTS_StartService(UINT16 service_id, tBTA_GATT_TRANSPORT sup_transport)
332 {
333 tBTA_GATTS_API_START *p_buf;
334
335 if ((p_buf = (tBTA_GATTS_API_START *) GKI_getbuf(sizeof(tBTA_GATTS_API_START))) != NULL)
336 {
337 p_buf->hdr.event = BTA_GATTS_API_START_SRVC_EVT;
338
339 p_buf->hdr.layer_specific = service_id;
340 p_buf->transport = sup_transport;
341
342 bta_sys_sendmsg(p_buf);
343 }
344 return;
345 }
346
347 /*******************************************************************************
348 **
349 ** Function BTA_GATTS_StopService
350 **
351 ** Description This function is called to stop a service.
352 **
353 ** Parameters service_id - service to be topped.
354 **
355 ** Returns None
356 **
357 *******************************************************************************/
BTA_GATTS_StopService(UINT16 service_id)358 void BTA_GATTS_StopService(UINT16 service_id)
359 {
360 BT_HDR *p_buf;
361
362 if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
363 {
364 p_buf->event = BTA_GATTS_API_STOP_SRVC_EVT;
365
366 p_buf->layer_specific = service_id;
367
368 bta_sys_sendmsg(p_buf);
369 }
370 return;
371 }
372
373 /*******************************************************************************
374 **
375 ** Function BTA_GATTS_HandleValueIndication
376 **
377 ** Description This function is called to read a characteristics descriptor.
378 **
379 ** Parameters bda - remote device bd address to indicate.
380 ** attr_id - attribute ID to indicate.
381 ** data_len - indicate data length.
382 ** p_data: data to indicate.
383 ** need_confirm - if this indication expects a confirmation or not.
384 **
385 ** Returns None
386 **
387 *******************************************************************************/
BTA_GATTS_HandleValueIndication(UINT16 conn_id,UINT16 attr_id,UINT16 data_len,UINT8 * p_data,BOOLEAN need_confirm)388 void BTA_GATTS_HandleValueIndication (UINT16 conn_id, UINT16 attr_id, UINT16 data_len,
389 UINT8 *p_data, BOOLEAN need_confirm)
390 {
391 tBTA_GATTS_API_INDICATION *p_buf;
392 UINT16 len = sizeof(tBTA_GATTS_API_INDICATION);
393
394 if ((p_buf = (tBTA_GATTS_API_INDICATION *) GKI_getbuf(len)) != NULL)
395 {
396 memset(p_buf, 0, len);
397
398 p_buf->hdr.event = BTA_GATTS_API_INDICATION_EVT;
399 p_buf->hdr.layer_specific = conn_id;
400 p_buf->attr_id = attr_id;
401 p_buf->need_confirm = need_confirm;
402
403 if (data_len > 0 && p_data != NULL)
404 {
405 p_buf->len = data_len;
406 memcpy(p_buf->value, p_data, data_len);
407
408 }
409 bta_sys_sendmsg(p_buf);
410 }
411 return;
412
413 }
414 /*******************************************************************************
415 **
416 ** Function BTA_GATTS_SendRsp
417 **
418 ** Description This function is called to send a response to a request.
419 **
420 ** Parameters conn_id - connection identifier.
421 ** trans_id - transaction ID.
422 ** status - response status
423 ** p_msg - response data.
424 **
425 ** Returns None
426 **
427 *******************************************************************************/
BTA_GATTS_SendRsp(UINT16 conn_id,UINT32 trans_id,tBTA_GATT_STATUS status,tBTA_GATTS_RSP * p_msg)428 void BTA_GATTS_SendRsp (UINT16 conn_id, UINT32 trans_id,
429 tBTA_GATT_STATUS status, tBTA_GATTS_RSP *p_msg)
430 {
431 tBTA_GATTS_API_RSP *p_buf;
432 UINT16 len = sizeof(tBTA_GATTS_API_RSP) + sizeof(tBTA_GATTS_RSP);
433
434 if ((p_buf = (tBTA_GATTS_API_RSP *) GKI_getbuf(len)) != NULL)
435 {
436 memset(p_buf, 0, len);
437
438 p_buf->hdr.event = BTA_GATTS_API_RSP_EVT;
439 p_buf->hdr.layer_specific = conn_id;
440 p_buf->trans_id = trans_id;
441 p_buf->status = status;
442
443 if (p_msg != NULL)
444 {
445 p_buf->p_rsp = (tBTA_GATTS_RSP *)(p_buf + 1);
446 memcpy(p_buf->p_rsp, p_msg, sizeof(tBTA_GATTS_RSP));
447 }
448
449 bta_sys_sendmsg(p_buf);
450 }
451 return;
452
453 }
454
455
456
457 /*******************************************************************************
458 **
459 ** Function BTA_GATTS_Open
460 **
461 ** Description Open a direct open connection or add a background auto connection
462 ** bd address
463 **
464 ** Parameters server_if: server interface.
465 ** remote_bda: remote device BD address.
466 ** is_direct: direct connection or background auto connection
467 **
468 ** Returns void
469 **
470 *******************************************************************************/
BTA_GATTS_Open(tBTA_GATTS_IF server_if,BD_ADDR remote_bda,BOOLEAN is_direct)471 void BTA_GATTS_Open(tBTA_GATTS_IF server_if, BD_ADDR remote_bda, BOOLEAN is_direct)
472 {
473 tBTA_GATTS_API_OPEN *p_buf;
474
475 if ((p_buf = (tBTA_GATTS_API_OPEN *) GKI_getbuf(sizeof(tBTA_GATTS_API_OPEN))) != NULL)
476 {
477 p_buf->hdr.event = BTA_GATTS_API_OPEN_EVT;
478 p_buf->server_if = server_if;
479 p_buf->is_direct = is_direct;
480 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
481
482 bta_sys_sendmsg(p_buf);
483 }
484 return;
485 }
486
487
488 /*******************************************************************************
489 **
490 ** Function BTA_GATTS_CancelOpen
491 **
492 ** Description Cancel a direct open connection or remove a background auto connection
493 ** bd address
494 **
495 ** Parameters server_if: server interface.
496 ** remote_bda: remote device BD address.
497 ** is_direct: direct connection or background auto connection
498 **
499 ** Returns void
500 **
501 *******************************************************************************/
BTA_GATTS_CancelOpen(tBTA_GATTS_IF server_if,BD_ADDR remote_bda,BOOLEAN is_direct)502 void BTA_GATTS_CancelOpen(tBTA_GATTS_IF server_if, BD_ADDR remote_bda, BOOLEAN is_direct)
503 {
504 tBTA_GATTS_API_CANCEL_OPEN *p_buf;
505
506 if ((p_buf = (tBTA_GATTS_API_CANCEL_OPEN *) GKI_getbuf(sizeof(tBTA_GATTS_API_CANCEL_OPEN))) != NULL)
507 {
508 p_buf->hdr.event = BTA_GATTS_API_CANCEL_OPEN_EVT;
509 p_buf->server_if = server_if;
510 p_buf->is_direct = is_direct;
511 memcpy(p_buf->remote_bda, remote_bda, BD_ADDR_LEN);
512 bta_sys_sendmsg(p_buf);
513 }
514 return;
515 }
516
517 /*******************************************************************************
518 **
519 ** Function BTA_GATTS_Close
520 **
521 ** Description Close a connection a remote device.
522 **
523 ** Parameters conn_id: connectino ID to be closed.
524 **
525 ** Returns void
526 **
527 *******************************************************************************/
BTA_GATTS_Close(UINT16 conn_id)528 void BTA_GATTS_Close(UINT16 conn_id)
529 {
530 BT_HDR *p_buf;
531
532 if ((p_buf = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
533 {
534 p_buf->event = BTA_GATTS_API_CLOSE_EVT;
535 p_buf->layer_specific = conn_id;
536 bta_sys_sendmsg(p_buf);
537 }
538 return;
539
540 }
541 /*******************************************************************************
542 **
543 ** Function BTA_GATTS_Listen
544 **
545 ** Description Start advertisement to listen for connection request for a
546 ** GATT server
547 **
548 ** Parameters server_if: server interface.
549 ** start: to start or stop listening for connection
550 ** remote_bda: remote device BD address, if listen to all device
551 ** use NULL.
552 **
553 ** Returns void
554 **
555 *******************************************************************************/
BTA_GATTS_Listen(tBTA_GATTS_IF server_if,BOOLEAN start,BD_ADDR_PTR target_bda)556 void BTA_GATTS_Listen(tBTA_GATTS_IF server_if, BOOLEAN start, BD_ADDR_PTR target_bda)
557 {
558 tBTA_GATTS_API_LISTEN *p_buf;
559
560 if ((p_buf = (tBTA_GATTS_API_LISTEN *) GKI_getbuf((UINT16)(sizeof(tBTA_GATTS_API_LISTEN) + BD_ADDR_LEN))) != NULL)
561 {
562 p_buf->hdr.event = BTA_GATTS_API_LISTEN_EVT;
563
564 p_buf->server_if = server_if;
565 p_buf->start = start;
566
567 if (target_bda)
568 {
569 p_buf->remote_bda = (UINT8*)(p_buf + 1);
570 memcpy(p_buf->remote_bda, target_bda, BD_ADDR_LEN);
571 }
572 else
573 p_buf->remote_bda = NULL;
574
575 bta_sys_sendmsg(p_buf);
576 }
577 return;
578 }
579
580 #endif /* BTA_GATT_INCLUDED */
581