1 /****************************************************************************** 2 * 3 * Copyright (C) 2003-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 file contains the GATT client main functions and state machine. 22 * 23 ******************************************************************************/ 24 25 #include "bt_target.h" 26 27 #include <string.h> 28 29 #include "bt_common.h" 30 #include "bta_gattc_int.h" 31 32 /***************************************************************************** 33 * Constants and types 34 ****************************************************************************/ 35 36 /* state machine action enumeration list */ 37 enum { 38 BTA_GATTC_OPEN, 39 BTA_GATTC_OPEN_FAIL, 40 BTA_GATTC_OPEN_ERROR, 41 BTA_GATTC_CANCEL_OPEN, 42 BTA_GATTC_CANCEL_OPEN_OK, 43 BTA_GATTC_CANCEL_OPEN_ERROR, 44 BTA_GATTC_CONN, 45 BTA_GATTC_START_DISCOVER, 46 BTA_GATTC_DISC_CMPL, 47 48 BTA_GATTC_Q_CMD, 49 BTA_GATTC_CLOSE, 50 BTA_GATTC_CLOSE_FAIL, 51 BTA_GATTC_READ, 52 BTA_GATTC_WRITE, 53 54 BTA_GATTC_OP_CMPL, 55 BTA_GATTC_SEARCH, 56 BTA_GATTC_FAIL, 57 BTA_GATTC_CONFIRM, 58 BTA_GATTC_EXEC, 59 BTA_GATTC_READ_MULTI, 60 BTA_GATTC_IGNORE_OP_CMPL, 61 BTA_GATTC_DISC_CLOSE, 62 BTA_GATTC_RESTART_DISCOVER, 63 BTA_GATTC_CFG_MTU, 64 65 BTA_GATTC_IGNORE 66 }; 67 /* type for action functions */ 68 typedef void (*tBTA_GATTC_ACTION)(tBTA_GATTC_CLCB* p_clcb, 69 tBTA_GATTC_DATA* p_data); 70 71 /* action function list */ 72 const tBTA_GATTC_ACTION bta_gattc_action[] = {bta_gattc_open, 73 bta_gattc_open_fail, 74 bta_gattc_open_error, 75 bta_gattc_cancel_open, 76 bta_gattc_cancel_open_ok, 77 bta_gattc_cancel_open_error, 78 bta_gattc_conn, 79 bta_gattc_start_discover, 80 bta_gattc_disc_cmpl, 81 82 bta_gattc_q_cmd, 83 bta_gattc_close, 84 bta_gattc_close_fail, 85 bta_gattc_read, 86 bta_gattc_write, 87 88 bta_gattc_op_cmpl, 89 bta_gattc_search, 90 bta_gattc_fail, 91 bta_gattc_confirm, 92 bta_gattc_execute, 93 bta_gattc_read_multi, 94 bta_gattc_ignore_op_cmpl, 95 bta_gattc_disc_close, 96 bta_gattc_restart_discover, 97 bta_gattc_cfg_mtu}; 98 99 /* state table information */ 100 #define BTA_GATTC_ACTIONS 1 /* number of actions */ 101 #define BTA_GATTC_NEXT_STATE 1 /* position of next state */ 102 #define BTA_GATTC_NUM_COLS 2 /* number of columns in state tables */ 103 104 /* state table for idle state */ 105 static const uint8_t bta_gattc_st_idle[][BTA_GATTC_NUM_COLS] = { 106 /* Event Action 1 Next state */ 107 /* BTA_GATTC_API_OPEN_EVT */ {BTA_GATTC_OPEN, 108 BTA_GATTC_W4_CONN_ST}, 109 /* BTA_GATTC_INT_OPEN_FAIL_EVT */ {BTA_GATTC_IGNORE, 110 BTA_GATTC_IDLE_ST}, 111 /* BTA_GATTC_API_CANCEL_OPEN_EVT */ {BTA_GATTC_IGNORE, 112 BTA_GATTC_IDLE_ST}, 113 /* BTA_GATTC_INT_CANCEL_OPEN_OK_EVT */ {BTA_GATTC_IGNORE, 114 BTA_GATTC_IDLE_ST}, 115 116 /* BTA_GATTC_API_READ_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST}, 117 /* BTA_GATTC_API_WRITE_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST}, 118 /* BTA_GATTC_API_EXEC_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST}, 119 /* BTA_GATTC_API_CFG_MTU_EVT */ {BTA_GATTC_IGNORE, 120 BTA_GATTC_IDLE_ST}, 121 122 /* BTA_GATTC_API_CLOSE_EVT */ {BTA_GATTC_CLOSE_FAIL, 123 BTA_GATTC_IDLE_ST}, 124 125 /* BTA_GATTC_API_SEARCH_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST}, 126 /* BTA_GATTC_API_CONFIRM_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST}, 127 /* BTA_GATTC_API_READ_MULTI_EVT */ {BTA_GATTC_FAIL, BTA_GATTC_IDLE_ST}, 128 129 /* BTA_GATTC_INT_CONN_EVT */ {BTA_GATTC_CONN, BTA_GATTC_CONN_ST}, 130 /* BTA_GATTC_INT_DISCOVER_EVT */ {BTA_GATTC_IGNORE, 131 BTA_GATTC_IDLE_ST}, 132 /* BTA_GATTC_DISCOVER_CMPL_EVT */ {BTA_GATTC_IGNORE, 133 BTA_GATTC_IDLE_ST}, 134 /* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_IGNORE, 135 BTA_GATTC_IDLE_ST}, 136 /* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_IGNORE, BTA_GATTC_IDLE_ST}, 137 138 }; 139 140 /* state table for wait for open state */ 141 static const uint8_t bta_gattc_st_w4_conn[][BTA_GATTC_NUM_COLS] = { 142 /* Event Action 1 Next state */ 143 /* BTA_GATTC_API_OPEN_EVT */ {BTA_GATTC_OPEN, 144 BTA_GATTC_W4_CONN_ST}, 145 /* BTA_GATTC_INT_OPEN_FAIL_EVT */ {BTA_GATTC_OPEN_FAIL, 146 BTA_GATTC_IDLE_ST}, 147 /* BTA_GATTC_API_CANCEL_OPEN_EVT */ {BTA_GATTC_CANCEL_OPEN, 148 BTA_GATTC_W4_CONN_ST}, 149 /* BTA_GATTC_INT_CANCEL_OPEN_OK_EVT */ {BTA_GATTC_CANCEL_OPEN_OK, 150 BTA_GATTC_IDLE_ST}, 151 152 /* BTA_GATTC_API_READ_EVT */ {BTA_GATTC_FAIL, 153 BTA_GATTC_W4_CONN_ST}, 154 /* BTA_GATTC_API_WRITE_EVT */ {BTA_GATTC_FAIL, 155 BTA_GATTC_W4_CONN_ST}, 156 /* BTA_GATTC_API_EXEC_EVT */ {BTA_GATTC_FAIL, 157 BTA_GATTC_W4_CONN_ST}, 158 /* BTA_GATTC_API_CFG_MTU_EVT */ {BTA_GATTC_IGNORE, 159 BTA_GATTC_W4_CONN_ST}, 160 161 /* BTA_GATTC_API_CLOSE_EVT */ {BTA_GATTC_CANCEL_OPEN, 162 BTA_GATTC_W4_CONN_ST}, 163 164 /* BTA_GATTC_API_SEARCH_EVT */ {BTA_GATTC_FAIL, 165 BTA_GATTC_W4_CONN_ST}, 166 /* BTA_GATTC_API_CONFIRM_EVT */ {BTA_GATTC_FAIL, 167 BTA_GATTC_W4_CONN_ST}, 168 /* BTA_GATTC_API_READ_MULTI_EVT */ {BTA_GATTC_FAIL, 169 BTA_GATTC_W4_CONN_ST}, 170 171 /* BTA_GATTC_INT_CONN_EVT */ {BTA_GATTC_CONN, BTA_GATTC_CONN_ST}, 172 /* BTA_GATTC_INT_DISCOVER_EVT */ {BTA_GATTC_IGNORE, 173 BTA_GATTC_W4_CONN_ST}, 174 /* BTA_GATTC_DISCOVER_CMPL_EVT */ {BTA_GATTC_IGNORE, 175 BTA_GATTC_W4_CONN_ST}, 176 /* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_IGNORE, 177 BTA_GATTC_W4_CONN_ST}, 178 /* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_OPEN_FAIL, 179 BTA_GATTC_IDLE_ST}, 180 181 }; 182 183 /* state table for open state */ 184 static const uint8_t bta_gattc_st_connected[][BTA_GATTC_NUM_COLS] = { 185 /* Event Action 1 Next state */ 186 /* BTA_GATTC_API_OPEN_EVT */ {BTA_GATTC_OPEN, BTA_GATTC_CONN_ST}, 187 /* BTA_GATTC_INT_OPEN_FAIL_EVT */ {BTA_GATTC_IGNORE, 188 BTA_GATTC_CONN_ST}, 189 /* BTA_GATTC_API_CANCEL_OPEN_EVT */ {BTA_GATTC_CANCEL_OPEN_ERROR, 190 BTA_GATTC_CONN_ST}, 191 /* BTA_GATTC_INT_CANCEL_OPEN_OK_EVT */ {BTA_GATTC_IGNORE, 192 BTA_GATTC_CONN_ST}, 193 194 /* BTA_GATTC_API_READ_EVT */ {BTA_GATTC_READ, BTA_GATTC_CONN_ST}, 195 /* BTA_GATTC_API_WRITE_EVT */ {BTA_GATTC_WRITE, BTA_GATTC_CONN_ST}, 196 /* BTA_GATTC_API_EXEC_EVT */ {BTA_GATTC_EXEC, BTA_GATTC_CONN_ST}, 197 /* BTA_GATTC_API_CFG_MTU_EVT */ {BTA_GATTC_CFG_MTU, 198 BTA_GATTC_CONN_ST}, 199 200 /* BTA_GATTC_API_CLOSE_EVT */ {BTA_GATTC_CLOSE, BTA_GATTC_IDLE_ST}, 201 202 /* BTA_GATTC_API_SEARCH_EVT */ {BTA_GATTC_SEARCH, 203 BTA_GATTC_CONN_ST}, 204 /* BTA_GATTC_API_CONFIRM_EVT */ {BTA_GATTC_CONFIRM, 205 BTA_GATTC_CONN_ST}, 206 /* BTA_GATTC_API_READ_MULTI_EVT */ {BTA_GATTC_READ_MULTI, 207 BTA_GATTC_CONN_ST}, 208 209 /* BTA_GATTC_INT_CONN_EVT */ {BTA_GATTC_IGNORE, 210 BTA_GATTC_CONN_ST}, 211 /* BTA_GATTC_INT_DISCOVER_EVT */ {BTA_GATTC_START_DISCOVER, 212 BTA_GATTC_DISCOVER_ST}, 213 /* BTA_GATTC_DISCOVER_CMPL_EVT */ {BTA_GATTC_IGNORE, 214 BTA_GATTC_CONN_ST}, 215 /* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_OP_CMPL, 216 BTA_GATTC_CONN_ST}, 217 218 /* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_CLOSE, BTA_GATTC_IDLE_ST}, 219 220 }; 221 222 /* state table for discover state */ 223 static const uint8_t bta_gattc_st_discover[][BTA_GATTC_NUM_COLS] = { 224 /* Event Action 1 Next state */ 225 /* BTA_GATTC_API_OPEN_EVT */ {BTA_GATTC_OPEN, 226 BTA_GATTC_DISCOVER_ST}, 227 /* BTA_GATTC_INT_OPEN_FAIL_EVT */ {BTA_GATTC_IGNORE, 228 BTA_GATTC_DISCOVER_ST}, 229 /* BTA_GATTC_API_CANCEL_OPEN_EVT */ {BTA_GATTC_CANCEL_OPEN_ERROR, 230 BTA_GATTC_DISCOVER_ST}, 231 /* BTA_GATTC_INT_CANCEL_OPEN_OK_EVT */ {BTA_GATTC_FAIL, 232 BTA_GATTC_DISCOVER_ST}, 233 234 /* BTA_GATTC_API_READ_EVT */ {BTA_GATTC_Q_CMD, 235 BTA_GATTC_DISCOVER_ST}, 236 /* BTA_GATTC_API_WRITE_EVT */ {BTA_GATTC_Q_CMD, 237 BTA_GATTC_DISCOVER_ST}, 238 /* BTA_GATTC_API_EXEC_EVT */ {BTA_GATTC_Q_CMD, 239 BTA_GATTC_DISCOVER_ST}, 240 /* BTA_GATTC_API_CFG_MTU_EVT */ {BTA_GATTC_Q_CMD, 241 BTA_GATTC_DISCOVER_ST}, 242 243 /* BTA_GATTC_API_CLOSE_EVT */ {BTA_GATTC_DISC_CLOSE, 244 BTA_GATTC_DISCOVER_ST}, 245 246 /* BTA_GATTC_API_SEARCH_EVT */ {BTA_GATTC_Q_CMD, 247 BTA_GATTC_DISCOVER_ST}, 248 /* BTA_GATTC_API_CONFIRM_EVT */ {BTA_GATTC_CONFIRM, 249 BTA_GATTC_DISCOVER_ST}, 250 /* BTA_GATTC_API_READ_MULTI_EVT */ {BTA_GATTC_Q_CMD, 251 BTA_GATTC_DISCOVER_ST}, 252 253 /* BTA_GATTC_INT_CONN_EVT */ {BTA_GATTC_CONN, 254 BTA_GATTC_DISCOVER_ST}, 255 /* BTA_GATTC_INT_DISCOVER_EVT */ {BTA_GATTC_RESTART_DISCOVER, 256 BTA_GATTC_DISCOVER_ST}, 257 /* BTA_GATTC_DISCOVER_CMPL_EVT */ {BTA_GATTC_DISC_CMPL, 258 BTA_GATTC_CONN_ST}, 259 /* BTA_GATTC_OP_CMPL_EVT */ {BTA_GATTC_IGNORE_OP_CMPL, 260 BTA_GATTC_DISCOVER_ST}, 261 /* BTA_GATTC_INT_DISCONN_EVT */ {BTA_GATTC_CLOSE, BTA_GATTC_IDLE_ST}, 262 263 }; 264 265 /* type for state table */ 266 typedef const uint8_t (*tBTA_GATTC_ST_TBL)[BTA_GATTC_NUM_COLS]; 267 268 /* state table */ 269 const tBTA_GATTC_ST_TBL bta_gattc_st_tbl[] = { 270 bta_gattc_st_idle, bta_gattc_st_w4_conn, bta_gattc_st_connected, 271 bta_gattc_st_discover}; 272 273 /***************************************************************************** 274 * Global data 275 ****************************************************************************/ 276 277 /* GATTC control block */ 278 tBTA_GATTC_CB bta_gattc_cb; 279 280 #if (BTA_GATT_DEBUG == TRUE) 281 static char* gattc_evt_code(tBTA_GATTC_INT_EVT evt_code); 282 static char* gattc_state_code(tBTA_GATTC_STATE state_code); 283 #endif 284 285 /******************************************************************************* 286 * 287 * Function bta_gattc_sm_execute 288 * 289 * Description State machine event handling function for GATTC 290 * 291 * 292 * Returns bool : true if queued client request buffer can be 293 * immediately released, else false 294 * 295 ******************************************************************************/ 296 bool bta_gattc_sm_execute(tBTA_GATTC_CLCB* p_clcb, uint16_t event, 297 tBTA_GATTC_DATA* p_data) { 298 tBTA_GATTC_ST_TBL state_table; 299 uint8_t action; 300 int i; 301 bool rt = true; 302 #if (BTA_GATT_DEBUG == TRUE) 303 tBTA_GATTC_STATE in_state = p_clcb->state; 304 uint16_t in_event = event; 305 APPL_TRACE_DEBUG("bta_gattc_sm_execute: State 0x%02x [%s], Event 0x%x[%s]", 306 in_state, gattc_state_code(in_state), in_event, 307 gattc_evt_code(in_event)); 308 #endif 309 310 /* look up the state table for the current state */ 311 state_table = bta_gattc_st_tbl[p_clcb->state]; 312 313 event &= 0x00FF; 314 315 /* set next state */ 316 p_clcb->state = state_table[event][BTA_GATTC_NEXT_STATE]; 317 318 /* execute action functions */ 319 for (i = 0; i < BTA_GATTC_ACTIONS; i++) { 320 action = state_table[event][i]; 321 if (action != BTA_GATTC_IGNORE) { 322 (*bta_gattc_action[action])(p_clcb, p_data); 323 if (p_clcb->p_q_cmd == p_data) { 324 /* buffer is queued, don't free in the bta dispatcher. 325 * we free it ourselves when a completion event is received. 326 */ 327 rt = false; 328 } 329 } else { 330 break; 331 } 332 } 333 334 #if (BTA_GATT_DEBUG == TRUE) 335 if (in_state != p_clcb->state) { 336 APPL_TRACE_DEBUG("GATTC State Change: [%s] -> [%s] after Event [%s]", 337 gattc_state_code(in_state), 338 gattc_state_code(p_clcb->state), gattc_evt_code(in_event)); 339 } 340 #endif 341 return rt; 342 } 343 344 /******************************************************************************* 345 * 346 * Function bta_gattc_hdl_event 347 * 348 * Description GATT client main event handling function. 349 * 350 * 351 * Returns bool 352 * 353 ******************************************************************************/ 354 bool bta_gattc_hdl_event(BT_HDR* p_msg) { 355 tBTA_GATTC_CLCB* p_clcb = NULL; 356 bool rt = true; 357 #if (BTA_GATT_DEBUG == TRUE) 358 APPL_TRACE_DEBUG("bta_gattc_hdl_event: Event [%s]", 359 gattc_evt_code(p_msg->event)); 360 #endif 361 switch (p_msg->event) { 362 363 case BTA_GATTC_API_OPEN_EVT: 364 bta_gattc_process_api_open((tBTA_GATTC_DATA*)p_msg); 365 break; 366 367 case BTA_GATTC_API_CANCEL_OPEN_EVT: 368 bta_gattc_process_api_open_cancel((tBTA_GATTC_DATA*)p_msg); 369 break; 370 371 default: 372 if (p_msg->event == BTA_GATTC_INT_CONN_EVT) 373 p_clcb = bta_gattc_find_int_conn_clcb((tBTA_GATTC_DATA*)p_msg); 374 else if (p_msg->event == BTA_GATTC_INT_DISCONN_EVT) 375 p_clcb = bta_gattc_find_int_disconn_clcb((tBTA_GATTC_DATA*)p_msg); 376 else 377 p_clcb = bta_gattc_find_clcb_by_conn_id(p_msg->layer_specific); 378 379 if (p_clcb != NULL) { 380 rt = 381 bta_gattc_sm_execute(p_clcb, p_msg->event, (tBTA_GATTC_DATA*)p_msg); 382 } else { 383 APPL_TRACE_DEBUG("Ignore unknown conn ID: %d", p_msg->layer_specific); 384 } 385 386 break; 387 } 388 389 return rt; 390 } 391 392 /***************************************************************************** 393 * Debug Functions 394 ****************************************************************************/ 395 #if (BTA_GATT_DEBUG == TRUE) 396 397 /******************************************************************************* 398 * 399 * Function gattc_evt_code 400 * 401 * Description 402 * 403 * Returns void 404 * 405 ******************************************************************************/ 406 static char* gattc_evt_code(tBTA_GATTC_INT_EVT evt_code) { 407 switch (evt_code) { 408 case BTA_GATTC_API_OPEN_EVT: 409 return "BTA_GATTC_API_OPEN_EVT"; 410 case BTA_GATTC_INT_OPEN_FAIL_EVT: 411 return "BTA_GATTC_INT_OPEN_FAIL_EVT"; 412 case BTA_GATTC_API_CANCEL_OPEN_EVT: 413 return "BTA_GATTC_API_CANCEL_OPEN_EVT"; 414 case BTA_GATTC_INT_CANCEL_OPEN_OK_EVT: 415 return "BTA_GATTC_INT_CANCEL_OPEN_OK_EVT"; 416 case BTA_GATTC_API_READ_EVT: 417 return "BTA_GATTC_API_READ_EVT"; 418 case BTA_GATTC_API_WRITE_EVT: 419 return "BTA_GATTC_API_WRITE_EVT"; 420 case BTA_GATTC_API_EXEC_EVT: 421 return "BTA_GATTC_API_EXEC_EVT"; 422 case BTA_GATTC_API_CLOSE_EVT: 423 return "BTA_GATTC_API_CLOSE_EVT"; 424 case BTA_GATTC_API_SEARCH_EVT: 425 return "BTA_GATTC_API_SEARCH_EVT"; 426 case BTA_GATTC_API_CONFIRM_EVT: 427 return "BTA_GATTC_API_CONFIRM_EVT"; 428 case BTA_GATTC_API_READ_MULTI_EVT: 429 return "BTA_GATTC_API_READ_MULTI_EVT"; 430 case BTA_GATTC_INT_CONN_EVT: 431 return "BTA_GATTC_INT_CONN_EVT"; 432 case BTA_GATTC_INT_DISCOVER_EVT: 433 return "BTA_GATTC_INT_DISCOVER_EVT"; 434 case BTA_GATTC_DISCOVER_CMPL_EVT: 435 return "BTA_GATTC_DISCOVER_CMPL_EVT"; 436 case BTA_GATTC_OP_CMPL_EVT: 437 return "BTA_GATTC_OP_CMPL_EVT"; 438 case BTA_GATTC_INT_DISCONN_EVT: 439 return "BTA_GATTC_INT_DISCONN_EVT"; 440 case BTA_GATTC_API_CFG_MTU_EVT: 441 return "BTA_GATTC_API_CFG_MTU_EVT"; 442 default: 443 return "unknown GATTC event code"; 444 } 445 } 446 447 /******************************************************************************* 448 * 449 * Function gattc_state_code 450 * 451 * Description 452 * 453 * Returns void 454 * 455 ******************************************************************************/ 456 static char* gattc_state_code(tBTA_GATTC_STATE state_code) { 457 switch (state_code) { 458 case BTA_GATTC_IDLE_ST: 459 return "GATTC_IDLE_ST"; 460 case BTA_GATTC_W4_CONN_ST: 461 return "GATTC_W4_CONN_ST"; 462 case BTA_GATTC_CONN_ST: 463 return "GATTC_CONN_ST"; 464 case BTA_GATTC_DISCOVER_ST: 465 return "GATTC_DISCOVER_ST"; 466 default: 467 return "unknown GATTC state code"; 468 } 469 } 470 471 #endif /* Debug Functions */ 472