luat_nimble.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "luat_base.h"
  2. #include "luat_nimble.h"
  3. #include "luat_msgbus.h"
  4. #include <string.h>
  5. #include <stdint.h>
  6. #include <stdbool.h>
  7. #include <assert.h>
  8. #include "wm_bt_config.h"
  9. #include "wm_bt.h"
  10. #include "wm_bt_util.h"
  11. #include "host/ble_hs.h"
  12. #include "host/util/util.h"
  13. #include "nimble/nimble_port.h"
  14. #include "transport/uart/ble_hci_uart.h"
  15. #include "nimble/tls_nimble.h"
  16. #include "wm_ble_gap.h"
  17. #include "wm_ble_uart_if.h"
  18. #define LUAT_LOG_TAG "ble"
  19. #include "luat_log.h"
  20. int tls_ble_server_api_init(tls_ble_output_func_ptr output_func_ptr);
  21. int tls_ble_server_api_deinit();
  22. uint32_t tls_ble_server_api_get_mtu();
  23. int tls_ble_server_api_send_msg(uint8_t *data, int data_len);
  24. static bool ble_system_state_on = false;
  25. extern volatile tls_bt_state_t bt_adapter_state;
  26. extern tls_bt_log_level_t tls_appl_trace_level;
  27. int tls_bt_util_init(void);
  28. int luat_nimble_trace_level(int level) {
  29. if (level >= 0 && level <= 6) {
  30. tls_appl_trace_level = (tls_bt_log_level_t)level;
  31. }
  32. return tls_appl_trace_level;
  33. }
  34. static void xxx_ble_income(uint8_t *p_data, uint32_t length) {
  35. printf("ble income len=%d ", length);
  36. for (size_t i = 0; i < length; i++)
  37. {
  38. printf("%02X ", p_data[i]);
  39. }
  40. printf("\n");
  41. }
  42. static int luat_ble_state_changed_handler(lua_State* L, void* ptr) {
  43. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  44. lua_getglobal(L, "sys_pub");
  45. if (lua_isfunction(L, -1)) {
  46. lua_pushstring(L, "BLE_STATE_INC");
  47. lua_pushinteger(L, msg->arg1);
  48. lua_call(L, 2, 0);
  49. }
  50. return 0;
  51. }
  52. void test_server_api_init() {
  53. tls_ble_server_api_init(xxx_ble_income);
  54. }
  55. void test_server_api_deinit() {
  56. tls_ble_server_api_deinit();
  57. }
  58. static void app_adapter_state_changed_callback(tls_bt_state_t status)
  59. {
  60. LLOGD("adapter status = %s\r\n", status==WM_BT_STATE_ON?"bt_state_on":"bt_state_off");
  61. bt_adapter_state = status;
  62. rtos_msg_t msg = {0};
  63. msg.handler = luat_ble_state_changed_handler;
  64. msg.arg1 = status;
  65. luat_msgbus_put(&msg, 0);
  66. // #if (TLS_CONFIG_BLE == CFG_ON)
  67. // if(status == WM_BT_STATE_ON)
  68. // {
  69. // TLS_BT_APPL_TRACE_VERBOSE("init base application\r\n");
  70. // //at here , user run their own applications;
  71. // #if 1
  72. // //tls_ble_wifi_cfg_init();
  73. // tls_ble_server_demo_api_init(xxx_ble_income);
  74. // //tls_ble_client_demo_api_init(NULL);
  75. // //tls_ble_server_demo_hid_init();
  76. // //tls_ble_server_hid_uart_init();
  77. // //tls_ble_client_multi_conn_demo_api_init();
  78. // #endif
  79. // }else
  80. // {
  81. // TLS_BT_APPL_TRACE_VERBOSE("deinit base application\r\n");
  82. // //here, user may free their application;
  83. // #if 1
  84. // // tls_ble_wifi_cfg_deinit(2);
  85. // tls_ble_server_demo_api_deinit();
  86. // // tls_ble_client_demo_api_deinit();
  87. // // tls_ble_client_multi_conn_demo_api_deinit();
  88. // #endif
  89. // }
  90. // #endif
  91. }
  92. static void
  93. on_sync(void)
  94. {
  95. //int rc;
  96. /* Make sure we have proper identity address set (public preferred) */
  97. //rc = ble_hs_util_ensure_addr(1);
  98. //assert(rc == 0);
  99. app_adapter_state_changed_callback(WM_BT_STATE_ON);
  100. }
  101. static void
  102. on_reset(int reason)
  103. {
  104. TLS_BT_APPL_TRACE_DEBUG("Resetting state; reason=%d\r\n", reason);
  105. app_adapter_state_changed_callback(WM_BT_STATE_OFF);
  106. }
  107. static void
  108. on_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
  109. {
  110. char buf[BLE_UUID_STR_LEN];
  111. switch (ctxt->op) {
  112. case BLE_GATT_REGISTER_OP_SVC:
  113. TLS_BT_APPL_TRACE_DEBUG("service,uuid16 %s handle=%d (%04X)\r\n",ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),ctxt->svc.handle, ctxt->svc.handle);
  114. break;
  115. case BLE_GATT_REGISTER_OP_CHR:
  116. TLS_BT_APPL_TRACE_DEBUG("charact,uuid16 %s arg %d def_handle=%d (%04X) val_handle=%d (%04X)\r\n",
  117. ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
  118. (int)ctxt->chr.chr_def->arg,
  119. ctxt->chr.def_handle, ctxt->chr.def_handle,
  120. ctxt->chr.val_handle, ctxt->chr.val_handle);
  121. break;
  122. case BLE_GATT_REGISTER_OP_DSC:
  123. TLS_BT_APPL_TRACE_DEBUG("descrip, uuid16 %s arg %d handle=%d (%04X)\r\n",
  124. ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
  125. (int)ctxt->dsc.dsc_def->arg,
  126. ctxt->dsc.handle, ctxt->dsc.handle);
  127. break;
  128. }
  129. return;
  130. }
  131. int
  132. luat_nimble_init(uint8_t uart_idx, char* name)
  133. {
  134. if(ble_system_state_on)
  135. {
  136. return BLE_HS_EALREADY;
  137. }
  138. memset(&ble_hs_cfg, 0, sizeof(ble_hs_cfg));
  139. /** Security manager settings. */
  140. ble_hs_cfg.sm_io_cap = MYNEWT_VAL(BLE_SM_IO_CAP),
  141. ble_hs_cfg.sm_oob_data_flag = MYNEWT_VAL(BLE_SM_OOB_DATA_FLAG),
  142. ble_hs_cfg.sm_bonding = MYNEWT_VAL(BLE_SM_BONDING),
  143. ble_hs_cfg.sm_mitm = MYNEWT_VAL(BLE_SM_MITM),
  144. ble_hs_cfg.sm_sc = MYNEWT_VAL(BLE_SM_SC),
  145. ble_hs_cfg.sm_keypress = MYNEWT_VAL(BLE_SM_KEYPRESS),
  146. ble_hs_cfg.sm_our_key_dist = MYNEWT_VAL(BLE_SM_OUR_KEY_DIST),
  147. ble_hs_cfg.sm_their_key_dist = MYNEWT_VAL(BLE_SM_THEIR_KEY_DIST),
  148. ble_hs_cfg.sync_cb = on_sync;
  149. ble_hs_cfg.reset_cb = on_reset;
  150. ble_hs_cfg.shutdown_cb = on_reset; /*same callback as on_reset */
  151. ble_hs_cfg.gatts_register_cb = on_svr_register_cb;
  152. ble_hs_cfg.store_status_cb = ble_store_util_status_rr;
  153. /* Initialize all packages. */
  154. nimble_port_init();
  155. /*Application levels code entry*/
  156. tls_ble_gap_init(name);
  157. tls_bt_util_init();
  158. /*Initialize the vuart interface and enable controller*/
  159. ble_hci_vuart_init(uart_idx);
  160. /* As the last thing, process events from default event queue. */
  161. tls_nimble_start();
  162. ble_system_state_on = true;
  163. return 0;
  164. }
  165. int
  166. luat_nimble_deinit(void)
  167. {
  168. int rc = 0;
  169. if(!ble_system_state_on)
  170. {
  171. return BLE_HS_EALREADY;
  172. }
  173. /*Stop hs system*/
  174. rc = nimble_port_stop();
  175. assert(rc == 0);
  176. /*Stop controller and free vuart resource */
  177. rc = ble_hci_vuart_deinit();
  178. assert(rc == 0);
  179. /*Free hs system resource*/
  180. nimble_port_deinit();
  181. /*Free task stack ptr and free hs task*/
  182. tls_nimble_stop();
  183. /*Application levels resource cleanup*/
  184. tls_ble_gap_deinit();
  185. tls_bt_util_deinit();
  186. ble_system_state_on = false;
  187. return rc;
  188. }
  189. //----------------------------------------
  190. // 设置广播数据
  191. int luat_nimble_gap_adv_set_fields() {
  192. }
  193. //----------------------------------------