wm_ble_client.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. #include <string.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4. #include <assert.h>
  5. #include "wm_bt_config.h"
  6. #if (WM_BLE_INCLUDED == CFG_ON)
  7. #include "wm_ble_gatt.h"
  8. #include "wm_ble_client.h"
  9. #include "wm_bt_util.h"
  10. /*
  11. * STRUCTURE DEFINITIONS
  12. ****************************************************************************************
  13. */
  14. typedef struct
  15. {
  16. uint16_t uuid;
  17. int client_if;
  18. int connect_id[WM_BLE_MAX_CONNECTION];
  19. wm_ble_client_callbacks_t *ps_callbak;
  20. uint8_t in_use;
  21. } app_ble_client_t;
  22. /*
  23. * DEFINES
  24. ****************************************************************************************
  25. */
  26. #define GATT_MAX_CNT_SUPPORT 7
  27. /*
  28. * GLOBAL VARIABLE DEFINITIONS
  29. ****************************************************************************************
  30. */
  31. static app_ble_client_t app_env[GATT_MAX_CNT_SUPPORT] = {0};
  32. /*
  33. * LOCAL FUNCTION DEFINITIONS
  34. ****************************************************************************************
  35. */
  36. static int get_free_app_env_index()
  37. {
  38. int index = 0;
  39. for(index = 0; index < GATT_MAX_CNT_SUPPORT; index++)
  40. {
  41. if(app_env[index].in_use == 0)
  42. {
  43. return index;
  44. }
  45. }
  46. return -1;
  47. }
  48. static int get_app_env_index_by_uuid(uint16_t uuid)
  49. {
  50. int index = 0;
  51. for(index = 0; index < GATT_MAX_CNT_SUPPORT; index++)
  52. {
  53. if(app_env[index].in_use == 1 && app_env[index].uuid == uuid)
  54. {
  55. return index;
  56. }
  57. }
  58. return -1;
  59. }
  60. static int get_app_env_index_by_client_if(int client_if)
  61. {
  62. int index = 0;
  63. for(index = 0; index < GATT_MAX_CNT_SUPPORT; index++)
  64. {
  65. if(app_env[index].in_use == 1 && app_env[index].client_if == client_if)
  66. {
  67. return index;
  68. }
  69. }
  70. return -1;
  71. }
  72. static int get_app_env_index_by_conn_id(int conn_id)
  73. {
  74. int index = 0;
  75. int conn_id_index = 0;
  76. for(index = 0; index < GATT_MAX_CNT_SUPPORT; index++)
  77. {
  78. if(app_env[index].in_use == 1)
  79. {
  80. for(conn_id_index=0; conn_id_index < WM_BLE_MAX_CONNECTION; conn_id_index++)
  81. {
  82. if(app_env[index].connect_id[conn_id_index] == conn_id)
  83. {
  84. return index;
  85. }
  86. }
  87. }
  88. }
  89. return -1;
  90. }
  91. /** Callback invoked in response to register_client */
  92. void btgattc_register_client_callback(int status, int client_if,
  93. tls_bt_uuid_t *uuid)
  94. {
  95. TLS_BT_APPL_TRACE_VERBOSE("%s ,status = %d, client_if = %d\r\n", __FUNCTION__, status, client_if);
  96. uint16_t app_uuid = 0;
  97. int index = 0;
  98. app_uuid = app_uuid128_to_uuid16(uuid);
  99. index = get_app_env_index_by_uuid(app_uuid);
  100. if(index<0)
  101. {
  102. TLS_BT_APPL_TRACE_ERROR("%s ,status = %d, client_if = %d,uuid=0x%04x\r\n", __FUNCTION__, status, client_if, app_uuid);
  103. return;
  104. }
  105. if(status != 0)
  106. {
  107. app_env[index].in_use = 0;
  108. }
  109. app_env[index].client_if = client_if;
  110. TLS_HAL_CBACK(app_env[index].ps_callbak, register_client_cb, status, client_if, app_uuid);
  111. }
  112. void btgattc_deregister_client_callback(int status, int client_if)
  113. {
  114. int index = -1;
  115. TLS_BT_APPL_TRACE_VERBOSE("%s, status=%d, client_if=%d\r\n", __FUNCTION__, status, client_if);
  116. index = get_app_env_index_by_client_if(client_if);
  117. if(index<0)
  118. {
  119. TLS_BT_APPL_TRACE_ERROR("%s, status=%d, client_if=%d\r\n", __FUNCTION__, status, client_if);
  120. return;
  121. }
  122. app_env[index].in_use = 0;
  123. TLS_HAL_CBACK(app_env[index].ps_callbak, deregister_client_cb, status, client_if);
  124. }
  125. /** GATT open callback invoked in response to open */
  126. void btgattc_connect_callback(int conn_id, int status, int client_if, tls_bt_addr_t *bda)
  127. {
  128. int index = -1;
  129. int conn_id_index = 0;
  130. TLS_BT_APPL_TRACE_VERBOSE("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  131. index = get_app_env_index_by_client_if(client_if);
  132. if(index<0)
  133. {
  134. TLS_BT_APPL_TRACE_ERROR("%s, status=%d, client_if=%d, conn_id=%d\r\n", __FUNCTION__, status, client_if, conn_id);
  135. return;
  136. }
  137. //app_env[index].connected_status = status;
  138. //app_env[index].connect_id = conn_id;
  139. //memcpy(&app_env[index].addr, bda, sizeof(tls_bt_addr_t));
  140. /*find a free pos to store the connection id belongs to the client_if*/
  141. for(conn_id_index = 0; conn_id_index < WM_BLE_MAX_CONNECTION; conn_id_index++)
  142. {
  143. if((status == 0))
  144. {
  145. if(app_env[index].connect_id[conn_id_index] == 0)
  146. {
  147. app_env[index].connect_id[conn_id_index] = conn_id;
  148. break;
  149. }
  150. }else
  151. {
  152. if(app_env[index].connect_id[conn_id_index] == conn_id)
  153. {
  154. app_env[index].connect_id[conn_id_index] = 0;
  155. break;
  156. }
  157. }
  158. }
  159. TLS_HAL_CBACK(app_env[index].ps_callbak, open_cb, conn_id, status, client_if, bda);
  160. }
  161. /** Callback invoked in response to close */
  162. void btgattc_disconnect_callback(int conn_id, int status, int reason,
  163. int client_if, tls_bt_addr_t *bda)
  164. {
  165. int index = -1;
  166. int conn_id_index = 0;
  167. TLS_BT_APPL_TRACE_VERBOSE("%s, client_if=%d, conn_id=%d\r\n", __FUNCTION__, client_if,conn_id);
  168. index = get_app_env_index_by_client_if(client_if);
  169. if(index<0)
  170. {
  171. TLS_BT_APPL_TRACE_ERROR("%s, status=%d, reason=%d, client_if=%d, conn_id=%d\r\n", __FUNCTION__, status, reason, client_if, conn_id);
  172. return;
  173. }
  174. for(conn_id_index = 0; conn_id_index < WM_BLE_MAX_CONNECTION; conn_id_index++)
  175. {
  176. if(app_env[index].connect_id[conn_id_index] == conn_id)
  177. {
  178. app_env[index].connect_id[conn_id_index] = 0;
  179. break;
  180. }
  181. }
  182. TLS_HAL_CBACK(app_env[index].ps_callbak, close_cb, conn_id, status, reason, client_if, bda);
  183. }
  184. /**
  185. * Invoked in response to search_service when the GATT service search
  186. * has been completed.
  187. */
  188. void btgattc_search_complete_callback(int conn_id, int status)
  189. {
  190. int index = -1;
  191. TLS_BT_APPL_TRACE_VERBOSE("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  192. index = get_app_env_index_by_conn_id(conn_id);
  193. if(index<0)
  194. {
  195. TLS_BT_APPL_TRACE_ERROR("%s, status=%d, conn_id=%d\r\n", __FUNCTION__, status, conn_id);
  196. return;
  197. }
  198. TLS_HAL_CBACK(app_env[index].ps_callbak, search_complete_cb, conn_id, status);
  199. }
  200. void btgattc_search_service_result_callback(int conn_id, tls_bt_uuid_t *p_uuid, uint8_t inst_id)
  201. {
  202. int index = -1;
  203. uint16_t app_uuid = 0;
  204. TLS_BT_APPL_TRACE_VERBOSE("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  205. index = get_app_env_index_by_conn_id(conn_id);
  206. if(index<0)
  207. {
  208. TLS_BT_APPL_TRACE_ERROR("%s, inst_id=%d, conn_id=%d\r\n", __FUNCTION__, inst_id, conn_id);
  209. return;
  210. }
  211. app_uuid = app_uuid128_to_uuid16(p_uuid);
  212. TLS_HAL_CBACK(app_env[index].ps_callbak, search_serv_res_cb, conn_id, app_uuid, inst_id);
  213. }
  214. /** Callback invoked in response to [de]register_for_notification */
  215. void btgattc_register_for_notification_callback(int conn_id,
  216. int registered, int status, uint16_t handle)
  217. {
  218. int index = -1;
  219. TLS_BT_APPL_TRACE_VERBOSE("%s, status=%d, conn_id=%d,registered=%d, handle=%d\r\n", __FUNCTION__, status, conn_id,registered, handle);
  220. index = get_app_env_index_by_conn_id(conn_id);
  221. if(index<0)
  222. {
  223. TLS_BT_APPL_TRACE_ERROR("%s, status=%d, conn_id=%d,registered=%d, handle=%d\r\n", __FUNCTION__, status, conn_id,registered, handle);
  224. return;
  225. }
  226. TLS_HAL_CBACK(app_env[index].ps_callbak, register_for_notification_cb, conn_id, registered, status, handle);
  227. }
  228. /**
  229. * Remote device notification callback, invoked when a remote device sends
  230. * a notification or indication that a client has registered for.
  231. */
  232. void btgattc_notify_callback(int conn_id, uint8_t *value, tls_bt_addr_t *bda, uint16_t handle, uint16_t len, uint8_t is_notify)
  233. {
  234. int index = -1;
  235. //TLS_BT_APPL_TRACE_VERBOSE("%s, is_notify=%d, conn_id=%d,len=%d, handle=%d\r\n", __FUNCTION__, is_notify, conn_id,len, handle);
  236. index = get_app_env_index_by_conn_id(conn_id);
  237. if(index<0)
  238. {
  239. TLS_BT_APPL_TRACE_ERROR("%s, is_notify=%d, conn_id=%d,len=%d, handle=%d\r\n", __FUNCTION__, is_notify, conn_id,len, handle);
  240. return;
  241. }
  242. TLS_HAL_CBACK(app_env[index].ps_callbak, notify_cb, conn_id, value, bda, handle, len, is_notify);
  243. }
  244. /** Reports result of a GATT read operation */
  245. void btgattc_read_characteristic_callback(int conn_id, int status,
  246. uint16_t handle, uint8_t *value, int len, int value_type, int rstatus)
  247. {
  248. int index = -1;
  249. TLS_BT_APPL_TRACE_VERBOSE("%s, status=%d, conn_id=%d,handle=%d,len=%d\r\n", __FUNCTION__, status, conn_id,handle, len);
  250. index = get_app_env_index_by_conn_id(conn_id);
  251. if(index<0)
  252. {
  253. TLS_BT_APPL_TRACE_ERROR("%s, status=%d, conn_id=%d,handle=%d,len=%d\r\n", __FUNCTION__, status, conn_id,handle, len);
  254. return;
  255. }
  256. TLS_HAL_CBACK(app_env[index].ps_callbak, read_characteristic_cb, conn_id, status, handle, value,
  257. len, value_type, rstatus);
  258. }
  259. /** GATT write characteristic operation callback */
  260. void btgattc_write_characteristic_callback(int conn_id, int status, uint16_t handle)
  261. {
  262. int index = -1;
  263. TLS_BT_APPL_TRACE_VERBOSE("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  264. index = get_app_env_index_by_conn_id(conn_id);
  265. if(index<0)
  266. {
  267. TLS_BT_APPL_TRACE_ERROR("%s, status=%d, conn_id=%d,handle=%d\r\n", __FUNCTION__, status, conn_id,handle);
  268. return;
  269. }
  270. TLS_HAL_CBACK(app_env[index].ps_callbak, write_characteristic_cb, conn_id, status, handle);
  271. }
  272. /** GATT execute prepared write callback */
  273. void btgattc_execute_write_callback(int conn_id, int status)
  274. {
  275. int index = -1;
  276. TLS_BT_APPL_TRACE_VERBOSE("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  277. index = get_app_env_index_by_conn_id(conn_id);
  278. if(index<0)
  279. {
  280. TLS_BT_APPL_TRACE_ERROR("%s, status=%d, conn_id=%d\r\n", __FUNCTION__, status, conn_id);
  281. return;
  282. }
  283. TLS_HAL_CBACK(app_env[index].ps_callbak, execute_write_cb, conn_id, status);
  284. }
  285. /** Callback invoked in response to read_descriptor */
  286. void btgattc_read_descriptor_callback(int conn_id, int status,uint16_t handle, uint8_t* value, int len, int value_type, int rstatus)
  287. {
  288. int index = -1;
  289. TLS_BT_APPL_TRACE_VERBOSE("%s, status=%d, conn_id=%d,len=%d\r\n", __FUNCTION__, status, conn_id,len);
  290. index = get_app_env_index_by_conn_id(conn_id);
  291. if(index<0)
  292. {
  293. TLS_BT_APPL_TRACE_ERROR("%s, status=%d, conn_id=%d,len=%d\r\n", __FUNCTION__, status, conn_id,len);
  294. return;
  295. }
  296. TLS_HAL_CBACK(app_env[index].ps_callbak, read_descriptor_cb, conn_id, status, handle, value,
  297. len, value_type, rstatus);
  298. }
  299. /** Callback invoked in response to write_descriptor */
  300. void btgattc_write_descriptor_callback(int conn_id, int status, uint16_t handle)
  301. {
  302. int index = -1;
  303. TLS_BT_APPL_TRACE_VERBOSE("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  304. index = get_app_env_index_by_conn_id(conn_id);
  305. if(index<0)
  306. {
  307. TLS_BT_APPL_TRACE_ERROR("%s, status=%d, conn_id=%d,handle=%d\r\n", __FUNCTION__, status, conn_id,handle);
  308. return;
  309. }
  310. TLS_HAL_CBACK(app_env[index].ps_callbak, write_descriptor_cb, conn_id, status, handle);
  311. }
  312. /**
  313. * Callback indicating the status of a listen() operation
  314. */
  315. void btgattc_listen_callback(int status, int server_if)
  316. {
  317. int index = -1;
  318. TLS_BT_APPL_TRACE_VERBOSE("%s, server_if=%d\r\n", __FUNCTION__, server_if);
  319. index = get_app_env_index_by_client_if(server_if);
  320. if(index<0)
  321. {
  322. TLS_BT_APPL_TRACE_ERROR("%s, status=%d, server_if=%d\r\n", __FUNCTION__, status, server_if);
  323. return;
  324. }
  325. TLS_HAL_CBACK(app_env[index].ps_callbak, listen_cb, status, server_if);
  326. }
  327. /** Callback invoked when the MTU for a given connection changes */
  328. void btgattc_configure_mtu_callback(int conn_id, int status, int mtu)
  329. {
  330. int index = -1;
  331. TLS_BT_APPL_TRACE_VERBOSE("%s, status=%d, conn_id=%d,mtu=%d\r\n", __FUNCTION__, status, conn_id,mtu);
  332. index = get_app_env_index_by_conn_id(conn_id);
  333. if(index<0)
  334. {
  335. TLS_BT_APPL_TRACE_ERROR("%s, status=%d, conn_id=%d,mtu=%d\r\n", __FUNCTION__, status, conn_id,mtu);
  336. return;
  337. }
  338. TLS_HAL_CBACK(app_env[index].ps_callbak, configure_mtu_cb, conn_id, status, mtu);
  339. }
  340. /**
  341. * Callback notifying an application that a remote device connection is currently congested
  342. * and cannot receive any more data. An application should avoid sending more data until
  343. * a further callback is received indicating the congestion status has been cleared.
  344. */
  345. void btgattc_congestion_callback(int conn_id, uint8_t congested)
  346. {
  347. int index = -1;
  348. TLS_BT_APPL_TRACE_VERBOSE("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  349. index = get_app_env_index_by_conn_id(conn_id);
  350. if(index<0)
  351. {
  352. TLS_BT_APPL_TRACE_ERROR("%s, congested=%d, conn_id=%d\r\n", __FUNCTION__, congested, conn_id);
  353. return;
  354. }
  355. TLS_HAL_CBACK(app_env[index].ps_callbak, congestion_cb, conn_id, congested);
  356. }
  357. /** GATT get database callback */
  358. void btgattc_get_gatt_db_callback(int status, int conn_id, tls_btgatt_db_element_t *db, int count)
  359. {
  360. int index = -1;
  361. TLS_BT_APPL_TRACE_VERBOSE("%s, status=%d, conn_id=%d, count=%d\r\n", __FUNCTION__, status, conn_id, count);
  362. index = get_app_env_index_by_conn_id(conn_id);
  363. if(index<0)
  364. {
  365. TLS_BT_APPL_TRACE_ERROR("%s, count=%d, conn_id=%d\r\n", __FUNCTION__, count, conn_id);
  366. return;
  367. }
  368. TLS_HAL_CBACK(app_env[index].ps_callbak, get_gatt_db_cb,status, conn_id, db, count);
  369. }
  370. /** GATT services between start_handle and end_handle were removed */
  371. void btgattc_services_removed_callback(int conn_id, uint16_t start_handle, uint16_t end_handle)
  372. {
  373. int index = -1;
  374. TLS_BT_APPL_TRACE_VERBOSE("%s, conn_id=%d,start_handle=%d,end_handle=%d\r\n", __FUNCTION__, conn_id,start_handle,end_handle);
  375. index = get_app_env_index_by_conn_id(conn_id);
  376. if(index<0)
  377. {
  378. TLS_BT_APPL_TRACE_ERROR("%s, conn_id=%d,start_handle=%d,end_handle=%d\r\n", __FUNCTION__, conn_id,start_handle,end_handle);
  379. return;
  380. }
  381. TLS_HAL_CBACK(app_env[index].ps_callbak, services_removed_cb, conn_id, start_handle, end_handle);
  382. }
  383. /** GATT services were added */
  384. void btgattc_services_added_callback(int conn_id, tls_btgatt_db_element_t *added, int added_count)
  385. {
  386. int index = -1;
  387. TLS_BT_APPL_TRACE_VERBOSE("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  388. index = get_app_env_index_by_conn_id(conn_id);
  389. if(index<0)
  390. {
  391. TLS_BT_APPL_TRACE_ERROR("%s, conn_id=%d,added_count=%d\r\n", __FUNCTION__, conn_id,added_count);
  392. return;
  393. }
  394. TLS_HAL_CBACK(app_env[index].ps_callbak, services_added_cb, conn_id, added, added_count);
  395. }
  396. static void tls_ble_client_event_handler(tls_ble_evt_t evt, tls_ble_msg_t *msg)
  397. {
  398. //TLS_BT_APPL_TRACE_EVENT("%s, event:%s,%d\r\n", __FUNCTION__, tls_gatt_evt_2_str(evt), evt);
  399. tls_bt_addr_t addr;
  400. switch(evt)
  401. {
  402. case WM_BLE_CL_REGISTER_EVT:
  403. btgattc_register_client_callback(msg->cli_register.status, msg->cli_register.client_if, &msg->cli_register.app_uuid);
  404. break;
  405. case WM_BLE_CL_DEREGISTER_EVT:
  406. btgattc_deregister_client_callback(msg->cli_register.status, msg->cli_register.client_if);
  407. break;
  408. case WM_BLE_CL_OPEN_EVT:
  409. memcpy(addr.address, msg->cli_open.bd_addr, 6);
  410. btgattc_connect_callback(msg->cli_open.conn_id, msg->cli_open.status, msg->cli_open.client_if, &addr);
  411. break;
  412. case WM_BLE_CL_CLOSE_EVT:
  413. memcpy(addr.address, msg->cli_close.remote_bda, 6);
  414. btgattc_disconnect_callback(msg->cli_close.conn_id, msg->cli_close.status, msg->cli_close.reason, msg->cli_close.client_if, &addr);
  415. break;
  416. case WM_BLE_CL_SEARCH_CMPL_EVT:
  417. btgattc_search_complete_callback(msg->cli_search_cmpl.conn_id, msg->cli_search_cmpl.status);
  418. break;
  419. case WM_BLE_CL_SEARCH_RES_EVT:
  420. btgattc_search_service_result_callback(msg->cli_search_res.conn_id, &msg->cli_search_res.uuid, msg->cli_search_res.inst_id);
  421. break;
  422. case WM_BLE_CL_REG_NOTIFY_EVT:
  423. btgattc_register_for_notification_callback(msg->cli_reg_notify.conn_id, msg->cli_reg_notify.reg, msg->cli_reg_notify.status, msg->cli_reg_notify.handle);
  424. break;
  425. case WM_BLE_CL_NOTIF_EVT:
  426. memcpy(addr.address, msg->cli_notif.bda, 6);
  427. btgattc_notify_callback(msg->cli_notif.conn_id, msg->cli_notif.value, &addr, msg->cli_notif.handle, msg->cli_notif.len, msg->cli_notif.is_notify);
  428. break;
  429. case WM_BLE_CL_READ_CHAR_EVT:
  430. btgattc_read_characteristic_callback(msg->cli_read.conn_id, msg->cli_read.status, msg->cli_read.handle, msg->cli_read.value, msg->cli_read.len, msg->cli_read.value_type, msg->cli_read.status);
  431. break;
  432. case WM_BLE_CL_WRITE_CHAR_EVT:
  433. btgattc_write_characteristic_callback(msg->cli_write.conn_id, msg->cli_write.status, msg->cli_write.handle);
  434. break;
  435. case WM_BLE_CL_EXEC_CMPL_EVT:
  436. btgattc_execute_write_callback(msg->cli_write.conn_id, msg->cli_write.status);
  437. break;
  438. case WM_BLE_CL_READ_DESCR_EVT:
  439. btgattc_read_descriptor_callback(msg->cli_read.conn_id, msg->cli_read.status, msg->cli_read.handle, msg->cli_read.value, msg->cli_read.len, msg->cli_read.value_type, msg->cli_read.status);
  440. break;
  441. case WM_BLE_CL_WRITE_DESCR_EVT:
  442. btgattc_write_descriptor_callback(msg->cli_write.conn_id, msg->cli_write.status, msg->cli_write.handle);
  443. break;
  444. case WM_BLE_CL_LISTEN_EVT:
  445. btgattc_listen_callback(msg->cli_listen.status, msg->cli_listen.client_if);
  446. break;
  447. case WM_BLE_CL_CFG_MTU_EVT:
  448. btgattc_configure_mtu_callback(msg->cli_cfg_mtu.conn_id, msg->cli_cfg_mtu.status, msg->cli_cfg_mtu.mtu);
  449. break;
  450. case WM_BLE_CL_CONGEST_EVT:
  451. btgattc_congestion_callback(msg->cli_congest.conn_id, msg->cli_congest.congested);
  452. break;
  453. case WM_BLE_CL_REPORT_DB_EVT:
  454. btgattc_get_gatt_db_callback(msg->cli_db.status, msg->cli_db.conn_id, msg->cli_db.db, msg->cli_db.count);
  455. break;
  456. default:
  457. TLS_BT_APPL_TRACE_WARNING("warning, unknow ble client evt=%d\r\n",evt);
  458. break;
  459. }
  460. }
  461. /*
  462. * EXPORTED FUNCTION DEFINITIONS
  463. ****************************************************************************************
  464. */
  465. tls_bt_status_t tls_ble_client_init()
  466. {
  467. memset(&app_env, 0, sizeof(app_ble_client_t)*GATT_MAX_CNT_SUPPORT);
  468. tls_ble_client_app_init(tls_ble_client_event_handler);
  469. return TLS_BT_STATUS_SUCCESS;
  470. }
  471. tls_bt_status_t tls_ble_client_deinit()
  472. {
  473. tls_ble_client_app_deinit();
  474. return TLS_BT_STATUS_SUCCESS;
  475. }
  476. tls_bt_status_t tls_ble_client_register_client(uint16_t app_uuid, wm_ble_client_callbacks_t *callback)
  477. {
  478. int index = -1;
  479. tls_bt_status_t status;
  480. index = get_app_env_index_by_uuid(app_uuid);
  481. if(index>=0)
  482. {
  483. TLS_BT_APPL_TRACE_WARNING("0x%04x, already registered\r\n", app_uuid)
  484. return TLS_BT_STATUS_DONE;
  485. }
  486. index = get_free_app_env_index();
  487. if(index < 0)
  488. {
  489. return TLS_BT_STATUS_NOMEM;
  490. }
  491. app_env[index].in_use = 1;
  492. app_env[index].uuid = app_uuid;
  493. app_env[index].ps_callbak = callback;
  494. status = tls_ble_client_app_register(app_uuid16_to_uuid128(app_uuid));
  495. if(status != TLS_BT_STATUS_SUCCESS)
  496. {
  497. app_env[index].in_use = 0;
  498. }
  499. return status;
  500. }
  501. /** Unregister a client application from the stack */
  502. tls_bt_status_t tls_ble_client_unregister_client(int client_if)
  503. {
  504. int index = -1;
  505. index = get_app_env_index_by_client_if(client_if);
  506. if(index<0)
  507. {
  508. return TLS_BT_STATUS_PARM_INVALID;
  509. }
  510. return tls_ble_client_app_unregister(client_if);
  511. }
  512. #endif