wm_ble_client_demo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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_client.h"
  8. #include "wm_ble_client_demo.h"
  9. #include "wm_ble_gap.h"
  10. #include "wm_bt_util.h"
  11. static tls_ble_callback_t tls_demo_at_cb_ptr;
  12. /** Callback invoked in response to register_client */
  13. void ble_client_demo_register_client_callback(int status, int client_if,
  14. uint16_t app_uuid)
  15. {
  16. TLS_BT_APPL_TRACE_DEBUG("%s, status=%d,client_if=%d,uuid=0x%04x\r\n", __FUNCTION__, status, client_if, app_uuid);
  17. tls_ble_msg_t msg;
  18. msg.cli_register.status = status;
  19. msg.cli_register.client_if = client_if;
  20. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_REGISTER_EVT, &msg);
  21. }
  22. void ble_client_demo_deregister_client_callback(int status, int client_if)
  23. {
  24. TLS_BT_APPL_TRACE_DEBUG("%s, status=%d,client_if=%d\r\n", __FUNCTION__, status, client_if);
  25. tls_ble_msg_t msg;
  26. msg.cli_register.status = status;
  27. msg.cli_register.client_if = client_if;
  28. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_DEREGISTER_EVT, &msg);
  29. tls_demo_at_cb_ptr = NULL;
  30. }
  31. /** GATT open callback invoked in response to open */
  32. void ble_client_demo_connect_callback(int conn_id, int status, int client_if, tls_bt_addr_t *bda)
  33. {
  34. TLS_BT_APPL_TRACE_DEBUG("%s, status=%d,client_if=%d,conn_id=%d\r\n", __FUNCTION__, status, client_if, conn_id);
  35. tls_ble_msg_t msg;
  36. memcpy(msg.cli_open.bd_addr, bda->address, 6);
  37. msg.cli_open.client_if = client_if;
  38. msg.cli_open.conn_id = conn_id;
  39. msg.cli_open.status = status;
  40. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_OPEN_EVT, &msg);
  41. }
  42. /** Callback invoked in response to close */
  43. void ble_client_demo_disconnect_callback(int conn_id, int status,int reason,
  44. int client_if, tls_bt_addr_t *bda)
  45. {
  46. TLS_BT_APPL_TRACE_DEBUG("%s, status = %d, reason=%d, conn_id=%d\r\n", __FUNCTION__, status, reason, conn_id);
  47. tls_ble_msg_t msg;
  48. msg.cli_close.client_if = client_if;
  49. msg.cli_close.conn_id = conn_id;
  50. msg.cli_close.status = status;
  51. msg.cli_close.reason = reason;
  52. memcpy(msg.cli_close.remote_bda, bda->address, 6);
  53. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_CLOSE_EVT, &msg);
  54. }
  55. /**
  56. * Invoked in response to search_service when the GATT service search
  57. * has been completed.
  58. */
  59. void ble_client_demo_search_complete_callback(int conn_id, int status)
  60. {
  61. TLS_BT_APPL_TRACE_DEBUG("%s, conn_id=%d, status=%d\r\n", __FUNCTION__, conn_id, status);
  62. tls_ble_msg_t msg;
  63. msg.cli_search_cmpl.conn_id = conn_id;
  64. msg.cli_search_cmpl.status = status;
  65. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_SEARCH_CMPL_EVT, &msg);
  66. }
  67. void ble_client_demo_search_service_result_callback(int conn_id, tls_bt_uuid_t *p_uuid, uint8_t inst_id)
  68. {
  69. TLS_BT_APPL_TRACE_DEBUG("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  70. }
  71. /** Callback invoked in response to [de]register_for_notification */
  72. void ble_client_demo_register_for_notification_callback(int conn_id,
  73. int registered, int status, uint16_t handle)
  74. {
  75. TLS_BT_APPL_TRACE_DEBUG("%s, conn_id=%d, registered=%d, handle=%d\r\n", __FUNCTION__, conn_id, registered, handle);
  76. tls_ble_msg_t msg;
  77. msg.cli_reg_notify.conn_id = conn_id;
  78. msg.cli_reg_notify.handle = handle;
  79. msg.cli_reg_notify.reg = registered;
  80. msg.cli_reg_notify.status = status;
  81. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_REG_NOTIFY_EVT, &msg);
  82. }
  83. /**
  84. * Remote device notification callback, invoked when a remote device sends
  85. * a notification or indication that a client has registered for.
  86. */
  87. void ble_client_demo_notify_callback(int conn_id, uint8_t *value, tls_bt_addr_t *addr, uint16_t handle, uint16_t len, uint8_t is_notify)
  88. {
  89. TLS_BT_APPL_TRACE_DEBUG("%s\r\n", __FUNCTION__);
  90. tls_ble_msg_t msg;
  91. memcpy(msg.cli_notif.bda, addr->address, 6);
  92. msg.cli_notif.conn_id = conn_id;
  93. msg.cli_notif.handle = handle;
  94. msg.cli_notif.is_notify = is_notify;
  95. msg.cli_notif.len = len;
  96. msg.cli_notif.value = value;
  97. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_NOTIF_EVT, &msg);
  98. }
  99. /** Reports result of a GATT read operation */
  100. void ble_client_demo_read_characteristic_callback(int conn_id, int status,
  101. uint16_t handle, uint8_t *value, int length, uint16_t value_type, uint8_t p_status)
  102. {
  103. //hci_dbg_hexstring("read out:", value, length);
  104. TLS_BT_APPL_TRACE_DEBUG("%s\r\n", __FUNCTION__);
  105. tls_ble_msg_t msg;
  106. msg.cli_read.conn_id = conn_id;
  107. msg.cli_read.handle = handle;
  108. msg.cli_read.len = length;
  109. msg.cli_read.status = status;
  110. msg.cli_read.value = value;
  111. msg.cli_read.value_type = value_type;
  112. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_READ_CHAR_EVT, &msg);
  113. }
  114. /** GATT write characteristic operation callback */
  115. void ble_client_demo_write_characteristic_callback(int conn_id, int status, uint16_t handle)
  116. {
  117. TLS_BT_APPL_TRACE_DEBUG("%s, conn_id=%d,handle=%d\r\n", __FUNCTION__, conn_id, handle);
  118. tls_ble_msg_t msg;
  119. msg.cli_write.conn_id = conn_id;
  120. msg.cli_write.handle = handle;
  121. msg.cli_write.status = status;
  122. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_WRITE_CHAR_EVT, &msg);
  123. }
  124. /** GATT execute prepared write callback */
  125. void ble_client_demo_execute_write_callback(int conn_id, int status)
  126. {
  127. TLS_BT_APPL_TRACE_DEBUG("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  128. }
  129. /** Callback invoked in response to read_descriptor */
  130. void ble_client_demo_read_descriptor_callback(int conn_id, int status, uint16_t handle, uint8_t *p_value, uint16_t length, uint16_t value_type, uint8_t pa_status)
  131. {
  132. TLS_BT_APPL_TRACE_DEBUG("%s, conn_id=%d, handle=%d\r\n", __FUNCTION__, conn_id, handle);
  133. //hci_dbg_hexstring("value", p_value, length);
  134. tls_ble_msg_t msg;
  135. msg.cli_read.conn_id = conn_id;
  136. msg.cli_read.handle = handle;
  137. msg.cli_read.len = length;
  138. msg.cli_read.status = status;
  139. msg.cli_read.value = p_value;
  140. msg.cli_read.value_type = value_type;
  141. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_READ_DESCR_EVT, &msg);
  142. }
  143. /** Callback invoked in response to write_descriptor */
  144. void ble_client_demo_write_descriptor_callback(int conn_id, int status, uint16_t handle)
  145. {
  146. TLS_BT_APPL_TRACE_DEBUG("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  147. tls_ble_msg_t msg;
  148. msg.cli_write.conn_id = conn_id;
  149. msg.cli_write.handle = handle;
  150. msg.cli_write.status = status;
  151. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_WRITE_DESCR_EVT, &msg);
  152. }
  153. /** Callback triggered in response to read_remote_rssi */
  154. void ble_client_demo_read_remote_rssi_callback(int client_if, tls_bt_addr_t *bda,
  155. int rssi, int status)
  156. {
  157. TLS_BT_APPL_TRACE_DEBUG("%s, client_if=%d\r\n", __FUNCTION__, client_if);
  158. }
  159. /**
  160. * Callback indicating the status of a listen() operation
  161. */
  162. void ble_client_demo_listen_callback(int status, int server_if)
  163. {
  164. TLS_BT_APPL_TRACE_DEBUG("%s, server_if=%d\r\n", __FUNCTION__, server_if);
  165. tls_ble_msg_t msg;
  166. msg.cli_listen.client_if = server_if;
  167. msg.cli_listen.status = status;
  168. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_LISTEN_EVT, &msg);
  169. }
  170. /** Callback invoked when the MTU for a given connection changes */
  171. void ble_client_demo_configure_mtu_callback(int conn_id, int status, int mtu)
  172. {
  173. TLS_BT_APPL_TRACE_DEBUG("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  174. tls_ble_msg_t msg;
  175. msg.cli_cfg_mtu.conn_id = conn_id;
  176. msg.cli_cfg_mtu.status = status;
  177. msg.cli_cfg_mtu.mtu = mtu;
  178. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_CFG_MTU_EVT, &msg);
  179. }
  180. /**
  181. * Callback notifying an application that a remote device connection is currently congested
  182. * and cannot receive any more data. An application should avoid sending more data until
  183. * a further callback is received indicating the congestion status has been cleared.
  184. */
  185. void ble_client_demo_congestion_callback(int conn_id, uint8_t congested)
  186. {
  187. TLS_BT_APPL_TRACE_DEBUG("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  188. tls_ble_msg_t msg;
  189. msg.cli_congest.congested = congested;
  190. msg.cli_congest.conn_id = conn_id;
  191. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_CONGEST_EVT, &msg);
  192. }
  193. /** GATT get database callback */
  194. void ble_client_demo_get_gatt_db_callback(int status, int conn_id, tls_btgatt_db_element_t *db, int count)
  195. {
  196. tls_ble_msg_t msg;
  197. TLS_BT_APPL_TRACE_DEBUG("===========btgattc_get_gatt_db_callback(count=%d)(conn_id=%d)================\r\n", count, conn_id);
  198. #if 0
  199. int i = 0;
  200. uint16_t cared_handle, tmp_uuid;
  201. for(i = 0; i < count; i++)
  202. {
  203. if(db->type == 0)
  204. {
  205. hci_dbg_hexstring("#", db->uuid.uu + 12, 2);
  206. hci_dbg_msg("type:%d, attr_handle:%d, properties:0x%02x, s=%d, e=%d\r\n", db->type, db->attribute_handle, db->properties, db->start_handle, db->end_handle);
  207. }
  208. else
  209. {
  210. hci_dbg_hexstring("\t#", db->uuid.uu + 12, 2);
  211. tmp_uuid = db->uuid.uu[12]<<8|db->uuid.uu[13];
  212. if(tmp_uuid == 0xBC2A)
  213. {
  214. cared_handle = db->attribute_handle;
  215. }
  216. hci_dbg_msg("\ttype:%d, attr_handle:%d, properties:0x%02x, s=%d, e=%d\r\n", db->type, db->attribute_handle, db->properties, db->start_handle, db->end_handle);
  217. }
  218. db++;
  219. }
  220. #endif
  221. msg.cli_db.conn_id = conn_id;
  222. msg.cli_db.count = count;
  223. msg.cli_db.db = db;
  224. msg.cli_db.status = status;
  225. if(tls_demo_at_cb_ptr)(tls_demo_at_cb_ptr)(WM_BLE_CL_REPORT_DB_EVT, &msg);
  226. }
  227. /** GATT services between start_handle and end_handle were removed */
  228. void ble_client_demo_services_removed_callback(int conn_id, uint16_t start_handle, uint16_t end_handle)
  229. {
  230. TLS_BT_APPL_TRACE_DEBUG("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  231. }
  232. /** GATT services were added */
  233. void ble_client_demo_services_added_callback(int conn_id, tls_btgatt_db_element_t *added, int added_count)
  234. {
  235. TLS_BT_APPL_TRACE_DEBUG("%s, conn_id=%d\r\n", __FUNCTION__, conn_id);
  236. }
  237. static const wm_ble_client_callbacks_t swmbleclientcb =
  238. {
  239. ble_client_demo_register_client_callback,
  240. ble_client_demo_deregister_client_callback,
  241. ble_client_demo_connect_callback,
  242. ble_client_demo_disconnect_callback,
  243. ble_client_demo_search_complete_callback,
  244. ble_client_demo_search_service_result_callback,
  245. ble_client_demo_register_for_notification_callback,
  246. ble_client_demo_notify_callback,
  247. ble_client_demo_read_characteristic_callback,
  248. ble_client_demo_write_characteristic_callback,
  249. ble_client_demo_read_descriptor_callback,
  250. ble_client_demo_write_descriptor_callback,
  251. ble_client_demo_execute_write_callback,
  252. ble_client_demo_read_remote_rssi_callback,
  253. ble_client_demo_listen_callback,
  254. ble_client_demo_configure_mtu_callback,
  255. ble_client_demo_congestion_callback,
  256. ble_client_demo_get_gatt_db_callback,
  257. ble_client_demo_services_removed_callback,
  258. ble_client_demo_services_added_callback,
  259. } ;
  260. int tls_ble_demo_cli_init(uint16_t demo_uuid,tls_ble_callback_t at_cb_ptr)
  261. {
  262. tls_bt_status_t status;
  263. if(tls_demo_at_cb_ptr)
  264. {
  265. TLS_BT_APPL_TRACE_WARNING("%s, done already\r\n", __FUNCTION__);
  266. return TLS_BT_STATUS_DONE;
  267. }
  268. tls_demo_at_cb_ptr = at_cb_ptr;
  269. status = tls_ble_client_register_client(demo_uuid, &swmbleclientcb);
  270. if(status != TLS_BT_STATUS_SUCCESS)
  271. {
  272. tls_demo_at_cb_ptr = NULL;
  273. TLS_BT_APPL_TRACE_ERROR("%s, failed, clear the tls_demo_at_cb_ptr\r\n", __FUNCTION__);
  274. }
  275. return status;
  276. }
  277. int tls_ble_demo_cli_deinit(int client_if)
  278. {
  279. if(tls_demo_at_cb_ptr == NULL)
  280. {
  281. TLS_BT_APPL_TRACE_WARNING("%s, done already\r\n", __FUNCTION__);
  282. return TLS_BT_STATUS_DONE;
  283. }
  284. return tls_ble_client_unregister_client(client_if);
  285. }
  286. #endif