bt_spp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Project: Arrow
  3. * Author: curiousmuch
  4. */
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include <stdbool.h>
  8. #include <stdio.h>
  9. #include "nvs.h"
  10. #include "nvs_flash.h"
  11. #include "freertos/FreeRTOS.h"
  12. #include "freertos/task.h"
  13. #include "esp_log.h"
  14. #include "esp_bt.h"
  15. #include "esp_bt_main.h"
  16. #include "esp_gap_bt_api.h"
  17. #include "esp_bt_device.h"
  18. #include "esp_spp_api.h"
  19. #include "bt_spp.h"
  20. // TODO: Remove and add call back functions to tie to main
  21. #include "kiss.h"
  22. // Why are these global? Should I move this to a .h settings file?
  23. #define DEVICE_NAME "Arrow 0"
  24. #define SPP_TAG "BT SPP"
  25. #define SPP_SERVER_NAME "Arrow_TNC"
  26. static const esp_spp_mode_t esp_spp_mode = ESP_SPP_MODE_CB;
  27. static const esp_spp_sec_t sec_mask = ESP_SPP_SEC_AUTHENTICATE;//ESP_SPP_SEC_AUTHENTICATE;
  28. static const esp_spp_role_t role_slave = ESP_SPP_ROLE_SLAVE;
  29. typedef void (* receive_cb_t)(uint8_t *p, uint16_t p_len);
  30. receive_cb_t receive_cb;
  31. void esp_set_rx_cb(void (*rx_cb)(uint8_t *p, uint16_t p_len) )
  32. {
  33. receive_cb = rx_cb;
  34. }
  35. uint32_t handle;
  36. // SPP Callback
  37. static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
  38. {
  39. switch(event) {
  40. case ESP_SPP_INIT_EVT:
  41. // called when SPP initialize
  42. ESP_LOGI(SPP_TAG, "ESP_SPP_INIT_ESP");
  43. esp_bt_dev_set_device_name(DEVICE_NAME);
  44. esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
  45. esp_spp_start_srv(sec_mask, role_slave, 0, SPP_SERVER_NAME);
  46. break;
  47. case ESP_SPP_DISCOVERY_COMP_EVT:
  48. // called with SPP discovery is complete
  49. ESP_LOGI(SPP_TAG, "ESP_SPP_DISCOVERY_COMP_EVT");
  50. break;
  51. case ESP_SPP_OPEN_EVT:
  52. // called when SPP client connection opens
  53. ESP_LOGI(SPP_TAG, "ESP_SPP_OPEN_EVT");
  54. break;
  55. case ESP_SPP_CLOSE_EVT:
  56. // called when SPP client connection closes
  57. ESP_LOGI(SPP_TAG, "ESP_SPP_START_EVT");
  58. break;
  59. case ESP_SPP_CL_INIT_EVT:
  60. // called when SPP client initiated a connection
  61. ESP_LOGI(SPP_TAG, "ESP_SPP_CL_INIT_EVT handle=%d", param->open.handle);
  62. break;
  63. case ESP_SPP_DATA_IND_EVT:
  64. // called when SPP connection receives data
  65. ESP_LOGI(SPP_TAG, "ESP_SPP_DATA_IND_EVT len=%d handle=%d", param->data_ind.len, param->data_ind.handle);
  66. //esp_log_buffer_hex("", param->data_ind.data, param->data_ind.len);
  67. kiss_receive(param->data_ind.data, param->data_ind.len);
  68. // [DEBUGING] echo received data
  69. // esp_spp_write(param->data_ind.handle, param->data_ind.len, param->data_ind.data);
  70. break;
  71. case ESP_SPP_CONG_EVT:
  72. // called when SPP connection congestion status changes
  73. ESP_LOGI(SPP_TAG, "ESP_SPP_CONG_EVT");
  74. break;
  75. case ESP_SPP_WRITE_EVT:
  76. // called when SPP write operation completes
  77. ESP_LOGI(SPP_TAG, "ESP_SPP_WRITE_EVT");
  78. // remove the need to write from the write queue
  79. break;
  80. case ESP_SPP_SRV_OPEN_EVT:
  81. // called when SPP server connection opens
  82. ESP_LOGI(SPP_TAG, "ESP_SPP_SRV_OPEN_EVT");
  83. handle = param->open.handle;
  84. break;
  85. default:
  86. break;
  87. }
  88. }
  89. void esp_bt_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
  90. {
  91. switch (event) {
  92. case ESP_BT_GAP_AUTH_CMPL_EVT:{
  93. // called with authentication process complete
  94. if(param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
  95. ESP_LOGI(SPP_TAG, "authentication success: %s", param->auth_cmpl.device_name);
  96. esp_log_buffer_hex(SPP_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
  97. } else {
  98. ESP_LOGE(SPP_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
  99. }
  100. break;
  101. }
  102. case ESP_BT_GAP_PIN_REQ_EVT:{
  103. // called when legacy pairing pin code requested
  104. ESP_LOGI(SPP_TAG, "ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d", param->pin_req.min_16_digit);
  105. if (param->pin_req.min_16_digit) {
  106. ESP_LOGI(SPP_TAG, "Input pin code: 0000 0000 0000 0000");
  107. esp_bt_pin_code_t pin_code = {0};
  108. esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);
  109. } else {
  110. ESP_LOGI(SPP_TAG, "Input pin code: 1234");
  111. esp_bt_pin_code_t pin_code;
  112. pin_code[0] = '1';
  113. pin_code[1] = '2';
  114. pin_code[2] = '3';
  115. pin_code[3] = '4';
  116. esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code);
  117. }
  118. break;
  119. }
  120. case ESP_BT_GAP_CFM_REQ_EVT:
  121. // called for simple pairing user confirmation
  122. ESP_LOGI(SPP_TAG, "ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
  123. esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
  124. break;
  125. case ESP_BT_GAP_KEY_NOTIF_EVT:
  126. // called during simple pairing passkey notification
  127. ESP_LOGI(SPP_TAG, "ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
  128. break;
  129. case ESP_BT_GAP_KEY_REQ_EVT:
  130. // called during simple pairing passkey request
  131. ESP_LOGI(SPP_TAG, "ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
  132. break;
  133. default:
  134. ESP_LOGI(SPP_TAG, "event: %d", event);
  135. break;
  136. }
  137. return;
  138. }
  139. void bt_spp_tx(uint8_t *buf, uint32_t len)
  140. {
  141. // if (handle != 0)
  142. esp_spp_write(handle, len, buf);
  143. }
  144. void bt_spp_init(void)
  145. {
  146. // Bluetooth Controller Initialization
  147. ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
  148. esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  149. esp_err_t ret;
  150. if ((ret = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
  151. ESP_LOGE(SPP_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
  152. return;
  153. }
  154. if ((ret = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) {
  155. ESP_LOGE(SPP_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
  156. return;
  157. }
  158. // Bluetooth Stack Initialization
  159. if ((ret = esp_bluedroid_init()) != ESP_OK) {
  160. ESP_LOGE(SPP_TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
  161. return;
  162. }
  163. if ((ret = esp_bluedroid_enable()) != ESP_OK) {
  164. ESP_LOGE(SPP_TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
  165. return;
  166. }
  167. // Set Bluetooth GAT Callback
  168. if ((ret = esp_bt_gap_register_callback(esp_bt_gap_cb)) != ESP_OK) {
  169. ESP_LOGE(SPP_TAG, "%s gap register failed: %s\n", __func__, esp_err_to_name(ret));
  170. return;
  171. }
  172. // Set Bluetooth SPP Callback
  173. if ((ret = esp_spp_register_callback(esp_spp_cb)) != ESP_OK) {
  174. ESP_LOGE(SPP_TAG, "%s spp register failed: %s\n", __func__, esp_err_to_name(ret));
  175. return;
  176. }
  177. if ((ret = esp_spp_init(esp_spp_mode)) != ESP_OK) {
  178. ESP_LOGE(SPP_TAG, "%s spp init failed: %s\n", __func__, esp_err_to_name(ret));
  179. return;
  180. }
  181. esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
  182. esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
  183. esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
  184. /*
  185. * Set default parameters for Legacy Pairing
  186. * Use variable pin, input pin code when pairing
  187. */
  188. esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
  189. esp_bt_pin_code_t pin_code;
  190. esp_bt_gap_set_pin(pin_type, 0, pin_code);
  191. }