luat_nimble.c 6.6 KB

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