luat_nimble.c 5.7 KB

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