luat_nimble_air101.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "luat_base.h"
  2. #include "luat_msgbus.h"
  3. #include "luat_nimble.h"
  4. #include "wm_bt_config.h"
  5. #include "wm_regs.h"
  6. #include "wm_bt.h"
  7. #include "wm_bt_util.h"
  8. #include "host/ble_hs.h"
  9. #include "host/util/util.h"
  10. #include "nimble/nimble_port.h"
  11. #include "transport/uart/ble_hci_uart.h"
  12. #include "nimble/tls_nimble.h"
  13. #include "wm_ble_gap.h"
  14. #include "wm_ble_uart_if.h"
  15. #include "syscfg/syscfg.h"
  16. #define LUAT_LOG_TAG "nimble"
  17. #include "luat_log.h"
  18. static uint8_t inited;
  19. int luat_nimble_deinit() {
  20. if (inited == 0)
  21. return 0;
  22. int rc;
  23. /*Stop hs system*/
  24. rc = nimble_port_stop();
  25. // assert(rc == 0);
  26. /*Stop controller and free vuart resource */
  27. rc = ble_hci_vuart_deinit();
  28. // assert(rc == 0);
  29. /*Free hs system resource*/
  30. nimble_port_deinit();
  31. /*Free task stack ptr and free hs task*/
  32. tls_nimble_stop();
  33. /*Application levels resource cleanup*/
  34. tls_ble_gap_deinit();
  35. // tls_bt_util_deinit(); // 关这个会死机, 还不知道为啥
  36. inited = 0;
  37. return 0;
  38. }
  39. int luat_nimble_trace_level(int level) {
  40. return 0;
  41. }
  42. int luat_nimble_init_peripheral(uint8_t uart_idx, char* name, int mode);
  43. int luat_nimble_init_central(uint8_t uart_idx, char* name, int mode);
  44. int luat_nimble_init_ibeacon(uint8_t uart_idx, char* name, int mode);
  45. int luat_nimble_init(uint8_t uart_idx, char* name, int mode) {
  46. if (inited == 1)
  47. return 0;
  48. int ret = 0;
  49. // tls_reg_write32(HR_CLK_BBP_CLT_CTRL, 0x7F);
  50. ble_hs_cfg.sm_io_cap = MYNEWT_VAL(BLE_SM_IO_CAP),
  51. ble_hs_cfg.sm_oob_data_flag = MYNEWT_VAL(BLE_SM_OOB_DATA_FLAG),
  52. ble_hs_cfg.sm_bonding = MYNEWT_VAL(BLE_SM_BONDING),
  53. ble_hs_cfg.sm_mitm = MYNEWT_VAL(BLE_SM_MITM),
  54. ble_hs_cfg.sm_sc = MYNEWT_VAL(BLE_SM_SC),
  55. ble_hs_cfg.sm_keypress = MYNEWT_VAL(BLE_SM_KEYPRESS),
  56. ble_hs_cfg.sm_our_key_dist = MYNEWT_VAL(BLE_SM_OUR_KEY_DIST),
  57. ble_hs_cfg.sm_their_key_dist = MYNEWT_VAL(BLE_SM_THEIR_KEY_DIST);
  58. if (mode == 0) {
  59. LLOGD("CALL luat_nimble_init_peripheral");
  60. ret = luat_nimble_init_peripheral(uart_idx, name, mode);
  61. }
  62. else if (mode == 1) {
  63. LLOGD("CALL luat_nimble_init_central");
  64. ret = luat_nimble_init_central(uart_idx, name, mode);
  65. }
  66. else if (mode == 2) {
  67. ret = luat_nimble_init_ibeacon(uart_idx, name, mode);
  68. }
  69. else {
  70. return -1;
  71. }
  72. if (ret == 0) {
  73. /*Initialize the vuart interface and enable controller*/
  74. ble_hci_vuart_init(0xFF);
  75. /* As the last thing, process events from default event queue. */
  76. tls_nimble_start();
  77. inited = 1;
  78. #ifndef LUAT_USE_WLAN
  79. tls_bt_ctrl_sleep(true);
  80. #endif
  81. }
  82. return 0;
  83. }
  84. #include "wm_efuse.h"
  85. int luat_nimble_mac_set(const char* tmac) {
  86. tls_set_bt_mac_addr((u8*)tmac);
  87. return 0;
  88. }
  89. int luat_nimble_mac_get(char* tmac) {
  90. tls_get_bt_mac_addr((u8*)tmac);
  91. return 0;
  92. }