bta_pan_main.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /******************************************************************************
  2. *
  3. * Copyright 2004-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. * This file contains the PAN main functions and state machine.
  21. *
  22. ******************************************************************************/
  23. #include "bt_target.h"
  24. #if (BTA_PAN_INCLUDED == TRUE)
  25. #include <string.h>
  26. #include "bt_common.h"
  27. #include "bta_api.h"
  28. #include "bta_pan_api.h"
  29. #include "bta_pan_int.h"
  30. #include "bta_sys.h"
  31. #include "osi/include/osi.h"
  32. #include "pan_api.h"
  33. #include "utl.h"
  34. /*****************************************************************************
  35. * Constants and types
  36. ****************************************************************************/
  37. /* state machine action enumeration list */
  38. enum {
  39. BTA_PAN_API_CLOSE,
  40. BTA_PAN_TX_PATH,
  41. BTA_PAN_RX_PATH,
  42. BTA_PAN_TX_FLOW,
  43. BTA_PAN_WRITE_BUF,
  44. BTA_PAN_CONN_OPEN,
  45. BTA_PAN_CONN_CLOSE,
  46. BTA_PAN_FREE_BUF,
  47. BTA_PAN_IGNORE,
  48. BTA_PAN_MAX_ACTIONS
  49. };
  50. /* type for action functions */
  51. typedef void (*tBTA_PAN_ACTION)(tBTA_PAN_SCB* p_scb, tBTA_PAN_DATA* p_data);
  52. /* action function list */
  53. const tBTA_PAN_ACTION bta_pan_action[] = {
  54. bta_pan_api_close, bta_pan_tx_path, bta_pan_rx_path, bta_pan_tx_flow,
  55. bta_pan_write_buf, bta_pan_conn_open, bta_pan_conn_close, bta_pan_free_buf,
  56. };
  57. /* state table information */
  58. #define BTA_PAN_ACTIONS 1 /* number of actions */
  59. #define BTA_PAN_NEXT_STATE 1 /* position of next state */
  60. #define BTA_PAN_NUM_COLS 2 /* number of columns in state tables */
  61. /* state table for listen state */
  62. const uint8_t bta_pan_st_idle[][BTA_PAN_NUM_COLS] = {
  63. /* API_CLOSE */ {BTA_PAN_API_CLOSE, BTA_PAN_IDLE_ST},
  64. /* CI_TX_READY */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
  65. /* CI_RX_READY */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
  66. /* CI_TX_FLOW */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
  67. /* CI_RX_WRITE */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
  68. /* CI_RX_WRITEBUF */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
  69. /* PAN_CONN_OPEN */ {BTA_PAN_CONN_OPEN, BTA_PAN_OPEN_ST},
  70. /* PAN_CONN_CLOSE */ {BTA_PAN_CONN_OPEN, BTA_PAN_IDLE_ST},
  71. /* FLOW_ENABLE */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST},
  72. /* BNEP_DATA */ {BTA_PAN_IGNORE, BTA_PAN_IDLE_ST}
  73. };
  74. /* state table for open state */
  75. const uint8_t bta_pan_st_open[][BTA_PAN_NUM_COLS] = {
  76. /* API_CLOSE */ {BTA_PAN_API_CLOSE, BTA_PAN_OPEN_ST},
  77. /* CI_TX_READY */ {BTA_PAN_TX_PATH, BTA_PAN_OPEN_ST},
  78. /* CI_RX_READY */ {BTA_PAN_RX_PATH, BTA_PAN_OPEN_ST},
  79. /* CI_TX_FLOW */ {BTA_PAN_TX_FLOW, BTA_PAN_OPEN_ST},
  80. /* CI_RX_WRITE */ {BTA_PAN_IGNORE, BTA_PAN_OPEN_ST},
  81. /* CI_RX_WRITEBUF */ {BTA_PAN_WRITE_BUF, BTA_PAN_OPEN_ST},
  82. /* PAN_CONN_OPEN */ {BTA_PAN_IGNORE, BTA_PAN_OPEN_ST},
  83. /* PAN_CONN_CLOSE */ {BTA_PAN_CONN_CLOSE, BTA_PAN_IDLE_ST},
  84. /* FLOW_ENABLE */ {BTA_PAN_RX_PATH, BTA_PAN_OPEN_ST},
  85. /* BNEP_DATA */ {BTA_PAN_TX_PATH, BTA_PAN_OPEN_ST}};
  86. /* state table for closing state */
  87. const uint8_t bta_pan_st_closing[][BTA_PAN_NUM_COLS] = {
  88. /* API_CLOSE */ {BTA_PAN_IGNORE, BTA_PAN_CLOSING_ST},
  89. /* CI_TX_READY */ {BTA_PAN_TX_PATH, BTA_PAN_CLOSING_ST},
  90. /* CI_RX_READY */ {BTA_PAN_RX_PATH, BTA_PAN_CLOSING_ST},
  91. /* CI_TX_FLOW */ {BTA_PAN_TX_FLOW, BTA_PAN_CLOSING_ST},
  92. /* CI_RX_WRITE */ {BTA_PAN_IGNORE, BTA_PAN_CLOSING_ST},
  93. /* CI_RX_WRITEBUF */ {BTA_PAN_FREE_BUF, BTA_PAN_CLOSING_ST},
  94. /* PAN_CONN_OPEN */ {BTA_PAN_IGNORE, BTA_PAN_CLOSING_ST},
  95. /* PAN_CONN_CLOSE */ {BTA_PAN_CONN_CLOSE, BTA_PAN_IDLE_ST},
  96. /* FLOW_ENABLE */ {BTA_PAN_RX_PATH, BTA_PAN_CLOSING_ST},
  97. /* BNEP_DATA */ {BTA_PAN_TX_PATH, BTA_PAN_CLOSING_ST}};
  98. /* type for state table */
  99. typedef const uint8_t (*tBTA_PAN_ST_TBL)[BTA_PAN_NUM_COLS];
  100. /* state table */
  101. const tBTA_PAN_ST_TBL bta_pan_st_tbl[] = {bta_pan_st_idle, bta_pan_st_open,
  102. bta_pan_st_closing};
  103. /*****************************************************************************
  104. * Global data
  105. ****************************************************************************/
  106. /* PAN control block */
  107. tBTA_PAN_CB bta_pan_cb;
  108. /*******************************************************************************
  109. *
  110. * Function bta_pan_scb_alloc
  111. *
  112. * Description Allocate a PAN server control block.
  113. *
  114. *
  115. * Returns pointer to the scb, or NULL if none could be allocated.
  116. *
  117. ******************************************************************************/
  118. tBTA_PAN_SCB* bta_pan_scb_alloc(void) {
  119. tBTA_PAN_SCB* p_scb = &bta_pan_cb.scb[0];
  120. int i;
  121. for (i = 0; i < BTA_PAN_NUM_CONN; i++, p_scb++) {
  122. if (!p_scb->in_use) {
  123. p_scb->in_use = true;
  124. APPL_TRACE_DEBUG("bta_pan_scb_alloc %d", i);
  125. break;
  126. }
  127. }
  128. if (i == BTA_PAN_NUM_CONN) {
  129. /* out of scbs */
  130. p_scb = NULL;
  131. APPL_TRACE_WARNING("Out of scbs");
  132. }
  133. return p_scb;
  134. }
  135. /*******************************************************************************
  136. *
  137. * Function bta_pan_sm_execute
  138. *
  139. * Description State machine event handling function for PAN
  140. *
  141. *
  142. * Returns void
  143. *
  144. ******************************************************************************/
  145. static void bta_pan_sm_execute(tBTA_PAN_SCB* p_scb, uint16_t event,
  146. tBTA_PAN_DATA* p_data) {
  147. tBTA_PAN_ST_TBL state_table;
  148. uint8_t action;
  149. int i;
  150. APPL_TRACE_EVENT("PAN scb=%d event=0x%x state=%d", bta_pan_scb_to_idx(p_scb),
  151. event, p_scb->state);
  152. /* look up the state table for the current state */
  153. state_table = bta_pan_st_tbl[p_scb->state];
  154. event &= 0x00FF;
  155. /* set next state */
  156. p_scb->state = state_table[event][BTA_PAN_NEXT_STATE];
  157. /* execute action functions */
  158. for (i = 0; i < BTA_PAN_ACTIONS; i++) {
  159. action = state_table[event][i];
  160. CHECK(action < BTA_PAN_MAX_ACTIONS);
  161. if (action == BTA_PAN_IGNORE) continue;
  162. (*bta_pan_action[action])(p_scb, p_data);
  163. }
  164. }
  165. /*******************************************************************************
  166. *
  167. * Function bta_pan_api_enable
  168. *
  169. * Description Handle an API enable event.
  170. *
  171. *
  172. * Returns void
  173. *
  174. ******************************************************************************/
  175. static void bta_pan_api_enable(tBTA_PAN_DATA* p_data) {
  176. /* initialize control block */
  177. memset(&bta_pan_cb, 0, sizeof(bta_pan_cb));
  178. /* store callback function */
  179. bta_pan_cb.p_cback = p_data->api_enable.p_cback;
  180. bta_pan_enable(p_data);
  181. }
  182. /*******************************************************************************
  183. *
  184. * Function bta_pan_api_disable
  185. *
  186. * Description Handle an API disable event.
  187. *
  188. *
  189. * Returns void
  190. *
  191. ******************************************************************************/
  192. static void bta_pan_api_disable(UNUSED_ATTR tBTA_PAN_DATA* p_data) {
  193. bta_pan_disable();
  194. }
  195. /*******************************************************************************
  196. *
  197. * Function bta_pan_api_open
  198. *
  199. * Description Handle an API listen event.
  200. *
  201. *
  202. * Returns void
  203. *
  204. ******************************************************************************/
  205. static void bta_pan_api_open(tBTA_PAN_DATA* p_data) {
  206. tBTA_PAN_SCB* p_scb;
  207. tBTA_PAN bta_pan;
  208. /* allocate an scb */
  209. p_scb = bta_pan_scb_alloc();
  210. if (p_scb != NULL) {
  211. bta_pan_open(p_scb, p_data);
  212. } else {
  213. bta_pan.open.bd_addr = p_data->api_open.bd_addr;
  214. bta_pan.open.status = BTA_PAN_FAIL;
  215. bta_pan_cb.p_cback(BTA_PAN_OPEN_EVT, &bta_pan);
  216. }
  217. }
  218. /*******************************************************************************
  219. *
  220. * Function bta_pan_scb_dealloc
  221. *
  222. * Description Deallocate a link control block.
  223. *
  224. *
  225. * Returns void
  226. *
  227. ******************************************************************************/
  228. void bta_pan_scb_dealloc(tBTA_PAN_SCB* p_scb) {
  229. APPL_TRACE_DEBUG("bta_pan_scb_dealloc %d", bta_pan_scb_to_idx(p_scb));
  230. fixed_queue_free(p_scb->data_queue, NULL);
  231. memset(p_scb, 0, sizeof(tBTA_PAN_SCB));
  232. }
  233. /*******************************************************************************
  234. *
  235. * Function bta_pan_scb_to_idx
  236. *
  237. * Description Given a pointer to an scb, return its index.
  238. *
  239. *
  240. * Returns Index of scb.
  241. *
  242. ******************************************************************************/
  243. uint8_t bta_pan_scb_to_idx(tBTA_PAN_SCB* p_scb) {
  244. return ((uint8_t)(p_scb - bta_pan_cb.scb)) + 1;
  245. }
  246. /*******************************************************************************
  247. *
  248. * Function bta_pan_scb_by_handle
  249. *
  250. * Description Find scb associated with handle.
  251. *
  252. *
  253. * Returns Pointer to scb or NULL if not found.
  254. *
  255. ******************************************************************************/
  256. tBTA_PAN_SCB* bta_pan_scb_by_handle(uint16_t handle) {
  257. tBTA_PAN_SCB* p_scb = &bta_pan_cb.scb[0];
  258. uint8_t i;
  259. for (i = 0; i < BTA_PAN_NUM_CONN; i++, p_scb++) {
  260. if (p_scb->handle == handle) {
  261. return p_scb;
  262. ;
  263. }
  264. }
  265. APPL_TRACE_WARNING("No scb for handle %d", handle);
  266. return NULL;
  267. }
  268. /*******************************************************************************
  269. *
  270. * Function bta_pan_hdl_event
  271. *
  272. * Description Data gateway main event handling function.
  273. *
  274. *
  275. * Returns void
  276. *
  277. ******************************************************************************/
  278. bool bta_pan_hdl_event(BT_HDR* p_msg) {
  279. tBTA_PAN_SCB* p_scb;
  280. bool freebuf = true;
  281. switch (p_msg->event) {
  282. /* handle enable event */
  283. case BTA_PAN_API_ENABLE_EVT:
  284. bta_pan_api_enable((tBTA_PAN_DATA*)p_msg);
  285. break;
  286. /* handle disable event */
  287. case BTA_PAN_API_DISABLE_EVT:
  288. bta_pan_api_disable((tBTA_PAN_DATA*)p_msg);
  289. break;
  290. /* handle set role event */
  291. case BTA_PAN_API_SET_ROLE_EVT:
  292. bta_pan_set_role((tBTA_PAN_DATA*)p_msg);
  293. break;
  294. /* handle open event */
  295. case BTA_PAN_API_OPEN_EVT:
  296. bta_pan_api_open((tBTA_PAN_DATA*)p_msg);
  297. break;
  298. /* events that require buffer not be released */
  299. case BTA_PAN_CI_RX_WRITEBUF_EVT:
  300. freebuf = false;
  301. p_scb = bta_pan_scb_by_handle(p_msg->layer_specific);
  302. if (p_scb != NULL) {
  303. bta_pan_sm_execute(p_scb, p_msg->event, (tBTA_PAN_DATA*)p_msg);
  304. }
  305. break;
  306. /* all other events */
  307. default:
  308. p_scb = bta_pan_scb_by_handle(p_msg->layer_specific);
  309. if (p_scb != NULL) {
  310. bta_pan_sm_execute(p_scb, p_msg->event, (tBTA_PAN_DATA*)p_msg);
  311. }
  312. break;
  313. }
  314. return freebuf;
  315. }
  316. #endif /* BTA_PAN_INCLUDED */