luat_nimble_air101.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. int luat_nimble_deinit() {
  19. int rc;
  20. /*Stop hs system*/
  21. rc = nimble_port_stop();
  22. // assert(rc == 0);
  23. /*Stop controller and free vuart resource */
  24. rc = ble_hci_vuart_deinit();
  25. // assert(rc == 0);
  26. /*Free hs system resource*/
  27. nimble_port_deinit();
  28. /*Free task stack ptr and free hs task*/
  29. tls_nimble_stop();
  30. /*Application levels resource cleanup*/
  31. tls_ble_gap_deinit();
  32. // tls_bt_util_deinit(); // 关这个会死机, 还不知道为啥
  33. return 0;
  34. }
  35. int luat_nimble_trace_level(int level) {
  36. return 0;
  37. }
  38. int luat_nimble_init_peripheral(uint8_t uart_idx, char* name, int mode);
  39. int luat_nimble_init_central(uint8_t uart_idx, char* name, int mode);
  40. int luat_nimble_init_ibeacon(uint8_t uart_idx, char* name, int mode);
  41. int luat_nimble_init(uint8_t uart_idx, char* name, int mode) {
  42. int ret = 0;
  43. // tls_reg_write32(HR_CLK_BBP_CLT_CTRL, 0x7F);
  44. ble_hs_cfg.sm_io_cap = MYNEWT_VAL(BLE_SM_IO_CAP),
  45. ble_hs_cfg.sm_oob_data_flag = MYNEWT_VAL(BLE_SM_OOB_DATA_FLAG),
  46. ble_hs_cfg.sm_bonding = MYNEWT_VAL(BLE_SM_BONDING),
  47. ble_hs_cfg.sm_mitm = MYNEWT_VAL(BLE_SM_MITM),
  48. ble_hs_cfg.sm_sc = MYNEWT_VAL(BLE_SM_SC),
  49. ble_hs_cfg.sm_keypress = MYNEWT_VAL(BLE_SM_KEYPRESS),
  50. ble_hs_cfg.sm_our_key_dist = MYNEWT_VAL(BLE_SM_OUR_KEY_DIST),
  51. ble_hs_cfg.sm_their_key_dist = MYNEWT_VAL(BLE_SM_THEIR_KEY_DIST);
  52. if (mode == 0) {
  53. LLOGD("CALL luat_nimble_init_peripheral");
  54. ret = luat_nimble_init_peripheral(uart_idx, name, mode);
  55. }
  56. else if (mode == 1) {
  57. LLOGD("CALL luat_nimble_init_central");
  58. ret = luat_nimble_init_central(uart_idx, name, mode);
  59. }
  60. else if (mode == 2) {
  61. ret = luat_nimble_init_ibeacon(uart_idx, name, mode);
  62. }
  63. else {
  64. return -1;
  65. }
  66. if (ret == 0) {
  67. /*Initialize the vuart interface and enable controller*/
  68. ble_hci_vuart_init(0xFF);
  69. /* As the last thing, process events from default event queue. */
  70. tls_nimble_start();
  71. }
  72. return 0;
  73. }