main.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <string.h>
  10. #include <inttypes.h>
  11. #include "app_io.h"
  12. #include "freertos/FreeRTOS.h"
  13. #include "freertos/task.h"
  14. #include "freertos/queue.h"
  15. #include "nvs.h"
  16. #include "nvs_flash.h"
  17. #include "esp_system.h"
  18. #include "esp_log.h"
  19. #include "esp_bt.h"
  20. #include "bt_app_core.h"
  21. #include "bt_app_av.h"
  22. #include "esp_bt_main.h"
  23. #include "esp_bt_device.h"
  24. #include "esp_gap_bt_api.h"
  25. #include "esp_a2dp_api.h"
  26. #include "esp_avrc_api.h"
  27. #include "driver/gpio.h"
  28. /* device name */
  29. #define LOCAL_DEVICE_NAME "ESP_SPEAKER"
  30. /* event for stack up */
  31. enum {
  32. BT_APP_EVT_STACK_UP = 0,
  33. };
  34. /* GPIO definitions */
  35. #define GPIO_BTN GPIO_NUM_2 // 按钮引脚
  36. #define GPIO_OUTPUT GPIO_NUM_19 // 输出控制引脚
  37. /* Audio mode states */
  38. typedef enum {
  39. AUDIO_MODE_NORMAL = 0,
  40. AUDIO_MODE_BASS_BOOST,
  41. AUDIO_MODE_MAX
  42. } audio_mode_t;
  43. static audio_mode_t current_audio_mode = AUDIO_MODE_NORMAL;
  44. /********************************
  45. * STATIC FUNCTION DECLARATIONS
  46. *******************************/
  47. /* Device callback function */
  48. static void bt_app_dev_cb(esp_bt_dev_cb_event_t event, esp_bt_dev_cb_param_t *param);
  49. /* GAP callback function */
  50. static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param);
  51. /* handler for bluetooth stack enabled events */
  52. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
  53. /* GPIO interrupt handler */
  54. static void IRAM_ATTR gpio_isr_handler(void* arg);
  55. /* Switch audio mode */
  56. static void switch_audio_mode(void);
  57. /* Update GPIO output based on audio mode */
  58. static void update_gpio_output(void);
  59. /*******************************
  60. * STATIC FUNCTION DEFINITIONS
  61. ******************************/
  62. static char *bda2str(uint8_t * bda, char *str, size_t size)
  63. {
  64. if (bda == NULL || str == NULL || size < 18) {
  65. return NULL;
  66. }
  67. uint8_t *p = bda;
  68. sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
  69. p[0], p[1], p[2], p[3], p[4], p[5]);
  70. return str;
  71. }
  72. static void bt_app_dev_cb(esp_bt_dev_cb_event_t event, esp_bt_dev_cb_param_t *param)
  73. {
  74. switch (event) {
  75. case ESP_BT_DEV_NAME_RES_EVT: {
  76. if (param->name_res.status == ESP_BT_STATUS_SUCCESS) {
  77. ESP_LOGI(BT_AV_TAG, "Get local device name success: %s", param->name_res.name);
  78. } else {
  79. ESP_LOGE(BT_AV_TAG, "Get local device name failed, status: %d", param->name_res.status);
  80. }
  81. break;
  82. }
  83. default: {
  84. ESP_LOGI(BT_AV_TAG, "event: %d", event);
  85. break;
  86. }
  87. }
  88. }
  89. static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
  90. {
  91. uint8_t *bda = NULL;
  92. switch (event) {
  93. /* when authentication completed, this event comes */
  94. case ESP_BT_GAP_AUTH_CMPL_EVT: {
  95. if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
  96. ESP_LOGI(BT_AV_TAG, "authentication success: %s", param->auth_cmpl.device_name);
  97. esp_log_buffer_hex(BT_AV_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
  98. } else {
  99. ESP_LOGE(BT_AV_TAG, "authentication failed, status: %d", param->auth_cmpl.stat);
  100. }
  101. ESP_LOGI(BT_AV_TAG, "link key type of current link is: %d", param->auth_cmpl.lk_type);
  102. break;
  103. }
  104. case ESP_BT_GAP_ENC_CHG_EVT: {
  105. char *str_enc[3] = {"OFF", "E0", "AES"};
  106. bda = (uint8_t *)param->enc_chg.bda;
  107. ESP_LOGI(BT_AV_TAG, "Encryption mode to [%02x:%02x:%02x:%02x:%02x:%02x] changed to %s",
  108. bda[0], bda[1], bda[2], bda[3], bda[4], bda[5], str_enc[param->enc_chg.enc_mode]);
  109. break;
  110. }
  111. #if (CONFIG_EXAMPLE_A2DP_SINK_SSP_ENABLED == true)
  112. /* when Security Simple Pairing user confirmation requested, this event comes */
  113. case ESP_BT_GAP_CFM_REQ_EVT:
  114. ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %"PRIu32, param->cfm_req.num_val);
  115. esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
  116. break;
  117. /* when Security Simple Pairing passkey notified, this event comes */
  118. case ESP_BT_GAP_KEY_NOTIF_EVT:
  119. ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_NOTIF_EVT passkey: %"PRIu32, param->key_notif.passkey);
  120. break;
  121. /* when Security Simple Pairing passkey requested, this event comes */
  122. case ESP_BT_GAP_KEY_REQ_EVT:
  123. ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
  124. break;
  125. #endif
  126. /* when GAP mode changed, this event comes */
  127. case ESP_BT_GAP_MODE_CHG_EVT:
  128. ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_MODE_CHG_EVT mode: %d", param->mode_chg.mode);
  129. break;
  130. /* when ACL connection completed, this event comes */
  131. case ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT:
  132. bda = (uint8_t *)param->acl_conn_cmpl_stat.bda;
  133. ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_ACL_CONN_CMPL_STAT_EVT Connected to [%02x:%02x:%02x:%02x:%02x:%02x], status: 0x%x",
  134. bda[0], bda[1], bda[2], bda[3], bda[4], bda[5], param->acl_conn_cmpl_stat.stat);
  135. break;
  136. /* when ACL disconnection completed, this event comes */
  137. case ESP_BT_GAP_ACL_DISCONN_CMPL_STAT_EVT:
  138. bda = (uint8_t *)param->acl_disconn_cmpl_stat.bda;
  139. ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_ACL_DISC_CMPL_STAT_EVT Disconnected from [%02x:%02x:%02x:%02x:%02x:%02x], reason: 0x%x",
  140. bda[0], bda[1], bda[2], bda[3], bda[4], bda[5], param->acl_disconn_cmpl_stat.reason);
  141. break;
  142. /* others */
  143. default: {
  144. ESP_LOGI(BT_AV_TAG, "event: %d", event);
  145. break;
  146. }
  147. }
  148. }
  149. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
  150. {
  151. ESP_LOGD(BT_AV_TAG, "%s event: %d", __func__, event);
  152. switch (event) {
  153. /* when do the stack up, this event comes */
  154. case BT_APP_EVT_STACK_UP: {
  155. esp_bt_gap_set_device_name(LOCAL_DEVICE_NAME);
  156. esp_bt_dev_register_callback(bt_app_dev_cb);
  157. esp_bt_gap_register_callback(bt_app_gap_cb);
  158. assert(esp_avrc_ct_init() == ESP_OK);
  159. esp_avrc_ct_register_callback(bt_app_rc_ct_cb);
  160. assert(esp_avrc_tg_init() == ESP_OK);
  161. esp_avrc_tg_register_callback(bt_app_rc_tg_cb);
  162. esp_avrc_rn_evt_cap_mask_t evt_set = {0};
  163. esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_SET, &evt_set, ESP_AVRC_RN_VOLUME_CHANGE);
  164. assert(esp_avrc_tg_set_rn_evt_cap(&evt_set) == ESP_OK);
  165. assert(esp_a2d_sink_init() == ESP_OK);
  166. esp_a2d_register_callback(&bt_app_a2d_cb);
  167. esp_a2d_sink_register_data_callback(bt_app_a2d_data_cb);
  168. /* Get the default value of the delay value */
  169. esp_a2d_sink_get_delay_value();
  170. /* Get local device name */
  171. esp_bt_gap_get_device_name();
  172. /* set discoverable and connectable mode, wait to be connected */
  173. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  174. break;
  175. }
  176. /* others */
  177. default:
  178. ESP_LOGE(BT_AV_TAG, "%s unhandled event: %d", __func__, event);
  179. break;
  180. }
  181. }
  182. /*******************************
  183. * MAIN ENTRY POINT
  184. ******************************/
  185. void app_main(void)
  186. {
  187. char bda_str[18] = {0};
  188. /* initialize NVS — it is used to store PHY calibration data */
  189. esp_err_t err = nvs_flash_init();
  190. if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  191. ESP_ERROR_CHECK(nvs_flash_erase());
  192. err = nvs_flash_init();
  193. }
  194. ESP_ERROR_CHECK(err);
  195. ESP_LOGI(BT_AV_TAG, "Initializing hw gpio ...");
  196. init_io();
  197. ESP_LOGI(BT_AV_TAG, "Initializing bluetooth ...");
  198. /*
  199. * This example only uses the functions of Classical Bluetooth.
  200. * So release the controller memory for Bluetooth Low Energy.
  201. */
  202. ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
  203. esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  204. if ((err = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
  205. ESP_LOGE(BT_AV_TAG, "%s initialize controller failed: %s", __func__, esp_err_to_name(err));
  206. return;
  207. }
  208. if ((err = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) {
  209. ESP_LOGE(BT_AV_TAG, "%s enable controller failed: %s", __func__, esp_err_to_name(err));
  210. return;
  211. }
  212. esp_bluedroid_config_t bluedroid_cfg = BT_BLUEDROID_INIT_CONFIG_DEFAULT();
  213. #if (CONFIG_EXAMPLE_A2DP_SINK_SSP_ENABLED == false)
  214. bluedroid_cfg.ssp_en = false;
  215. #endif
  216. if ((err = esp_bluedroid_init_with_cfg(&bluedroid_cfg)) != ESP_OK) {
  217. ESP_LOGE(BT_AV_TAG, "%s initialize bluedroid failed: %s", __func__, esp_err_to_name(err));
  218. return;
  219. }
  220. if ((err = esp_bluedroid_enable()) != ESP_OK) {
  221. ESP_LOGE(BT_AV_TAG, "%s enable bluedroid failed: %s", __func__, esp_err_to_name(err));
  222. return;
  223. }
  224. #if (CONFIG_EXAMPLE_A2DP_SINK_SSP_ENABLED == true)
  225. /* set default parameters for Secure Simple Pairing */
  226. esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
  227. esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
  228. esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
  229. #endif
  230. /* set default parameters for Legacy Pairing (use fixed pin code 1234) */
  231. esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
  232. esp_bt_pin_code_t pin_code;
  233. pin_code[0] = '1';
  234. pin_code[1] = '2';
  235. pin_code[2] = '3';
  236. pin_code[3] = '4';
  237. esp_bt_gap_set_pin(pin_type, 4, pin_code);
  238. ESP_LOGI(BT_AV_TAG, "Own address:[%s]", bda2str((uint8_t *)esp_bt_dev_get_address(), bda_str, sizeof(bda_str)));
  239. bt_app_task_start_up();
  240. /* bluetooth device name, connection mode and profile set up */
  241. bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL);
  242. }