bt_spp.c 6.2 KB

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