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