bt_app_handler.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include "esp_log.h"
  6. #include "esp_bt.h"
  7. #include "esp_bt_device.h"
  8. #include "esp_bt_main.h"
  9. #include "esp_gap_bt_api.h"
  10. #include "esp_a2dp_api.h"
  11. #include "esp_console.h"
  12. #include "esp_pthread.h"
  13. #include "esp_system.h"
  14. #include "esp_wifi.h"
  15. #include "freertos/timers.h"
  16. #include "argtable3/argtable3.h"
  17. #include "bt_app_core.h"
  18. #include "trace.h"
  19. static const char * TAG = "platform";
  20. extern int32_t output_bt_data(uint8_t *data, int32_t len);
  21. extern void output_bt_tick(void);
  22. extern char* output_state_str(void);
  23. extern bool output_stopped(void);
  24. int64_t connecting_timeout = 0;
  25. static const char * art_a2dp_connected[]={"\n",
  26. " ___ _____ _____ _____ _ _ _ ",
  27. " /\\ |__ \\| __ \\| __ \\ / ____| | | | | |",
  28. " / \\ ) | | | | |__) | | | ___ _ __ _ __ ___ ___| |_ ___ __| | |",
  29. " / /\\ \\ / /| | | | ___/ | | / _ \\| '_ \\| '_ \\ / _ \\/ __| __/ _ \\/ _` | |",
  30. " / ____ \\ / /_| |__| | | | |___| (_) | | | | | | | __/ (__| || __/ (_| |_|",
  31. " /_/ \\_\\____|_____/|_| \\_____\\___/|_| |_|_| |_|\\___|\\___|\\__\\___|\\__,_(_)\n",
  32. "\0"};
  33. static const char * art_a2dp_connecting[]= {"\n",
  34. " ___ _____ _____ _____ _ _ ",
  35. " /\\ |__ \\| __ \\| __ \\ / ____| | | (_) ",
  36. " / \\ ) | | | | |__) | | | ___ _ __ _ __ ___ ___| |_ _ _ __ __ _ ",
  37. " / /\\ \\ / /| | | | ___/ | | / _ \\| '_ \\| '_ \\ / _ \\/ __| __| | '_ \\ / _` | ",
  38. " / ____ \\ / /_| |__| | | | |___| (_) | | | | | | | __/ (__| |_| | | | | (_| |_ _ _ ",
  39. " /_/ \\_\\____|_____/|_| \\_____\\___/|_| |_|_| |_|\\___|\\___|\\__|_|_| |_|\\__, (_|_|_)",
  40. " __/ | ",
  41. " |___/ \n",
  42. "\0"};
  43. static void bt_app_av_state_connecting(uint16_t event, void *param);
  44. #define A2DP_TIMER_INIT connecting_timeout = esp_timer_get_time() +(CONFIG_A2DP_CONNECT_TIMEOUT_MS * 1000)
  45. #define IS_A2DP_TIMER_OVER esp_timer_get_time() >= connecting_timeout
  46. static void filter_inquiry_scan_result(esp_bt_gap_cb_param_t *param);
  47. /* event for handler "bt_av_hdl_stack_up */
  48. enum {
  49. BT_APP_EVT_STACK_UP = 0,
  50. };
  51. /* A2DP global state */
  52. enum {
  53. APP_AV_STATE_IDLE,
  54. APP_AV_STATE_DISCOVERING,
  55. APP_AV_STATE_DISCOVERED,
  56. APP_AV_STATE_UNCONNECTED,
  57. APP_AV_STATE_CONNECTING,
  58. APP_AV_STATE_CONNECTED,
  59. APP_AV_STATE_DISCONNECTING,
  60. };
  61. char * APP_AV_STATE_DESC[] = {
  62. "APP_AV_STATE_IDLE",
  63. "APP_AV_STATE_DISCOVERING",
  64. "APP_AV_STATE_DISCOVERED",
  65. "APP_AV_STATE_UNCONNECTED",
  66. "APP_AV_STATE_CONNECTING",
  67. "APP_AV_STATE_CONNECTED",
  68. "APP_AV_STATE_DISCONNECTING"
  69. };
  70. /* sub states of APP_AV_STATE_CONNECTED */
  71. enum {
  72. APP_AV_MEDIA_STATE_IDLE,
  73. APP_AV_MEDIA_STATE_STARTING,
  74. // APP_AV_MEDIA_STATE_BUFFERING,
  75. APP_AV_MEDIA_STATE_STARTED,
  76. APP_AV_MEDIA_STATE_STOPPING,
  77. APP_AV_MEDIA_STATE_WAIT_DISCONNECT
  78. };
  79. #define BT_APP_HEART_BEAT_EVT (0xff00)
  80. /// handler for bluetooth stack enabled events
  81. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
  82. /// callback function for A2DP source
  83. static void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
  84. /// callback function for A2DP source audio data stream
  85. static void a2d_app_heart_beat(void *arg);
  86. /// A2DP application state machine
  87. static void bt_app_av_sm_hdlr(uint16_t event, void *param);
  88. /* A2DP application state machine handler for each state */
  89. static void bt_app_av_state_unconnected(uint16_t event, void *param);
  90. static void bt_app_av_state_connecting(uint16_t event, void *param);
  91. static void bt_app_av_state_connected(uint16_t event, void *param);
  92. static void bt_app_av_state_disconnecting(uint16_t event, void *param);
  93. static esp_bd_addr_t s_peer_bda = {0};
  94. static uint8_t s_peer_bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
  95. static int s_a2d_state = APP_AV_STATE_IDLE;
  96. static int s_media_state = APP_AV_MEDIA_STATE_IDLE;
  97. static uint32_t s_pkt_cnt = 0;
  98. static TimerHandle_t s_tmr;
  99. static struct {
  100. struct arg_str *sink_name;
  101. struct arg_int *control_delay;
  102. struct arg_int *connect_timeout_delay;
  103. struct arg_end *end;
  104. } squeezelite_args;
  105. void hal_bluetooth_init(const char * options)
  106. {
  107. ESP_LOGD(TAG,"Initializing Bluetooth HAL");
  108. squeezelite_args.sink_name = arg_str1("n", "name", "<sink name>", "the name of the bluetooth to connect to");
  109. squeezelite_args.control_delay = arg_int0("d", "delay", "<control delay>", "the delay between each pass at the A2DP control loop");
  110. squeezelite_args.connect_timeout_delay = arg_int0("t","timeout", "<timeout>", "the timeout duration for connecting to the A2DP sink");
  111. squeezelite_args.end = arg_end(2);
  112. ESP_LOGD(TAG,"Copying parameters");
  113. char * opts = strdup(options);
  114. char **argv = malloc(sizeof(char**)*15);
  115. size_t argv_size=15;
  116. // change parms so ' appear as " for parsing the options
  117. for (char* p = opts; (p = strchr(p, '\'')); ++p) *p = '"';
  118. ESP_LOGD(TAG,"Splitting arg line: %s", opts);
  119. argv_size = esp_console_split_argv(opts, argv, argv_size);
  120. ESP_LOGD(TAG,"Parsing parameters");
  121. int nerrors = arg_parse(argv_size , argv, (void **) &squeezelite_args);
  122. if (nerrors != 0) {
  123. ESP_LOGD(TAG,"Parsing Errors");
  124. arg_print_errors(stdout, squeezelite_args.end, "BT");
  125. arg_print_glossary_gnu(stdout, (void **) &squeezelite_args);
  126. free(opts);
  127. free(argv);
  128. return;
  129. }
  130. if(squeezelite_args.sink_name->count == 0)
  131. {
  132. ESP_LOGD(TAG,"Using default sink name : %s",CONFIG_A2DP_SINK_NAME);
  133. squeezelite_args.sink_name->sval[0] = CONFIG_A2DP_SINK_NAME;
  134. }
  135. if(squeezelite_args.connect_timeout_delay->count == 0)
  136. {
  137. ESP_LOGD(TAG,"Using default connect timeout");
  138. squeezelite_args.connect_timeout_delay->ival[0]=CONFIG_A2DP_CONNECT_TIMEOUT_MS;
  139. }
  140. if(squeezelite_args.control_delay->count == 0)
  141. {
  142. ESP_LOGD(TAG,"Using default control delay");
  143. squeezelite_args.control_delay->ival[0]=CONFIG_A2DP_CONTROL_DELAY_MS;
  144. }
  145. ESP_LOGD(TAG,"Freeing options");
  146. free(argv);
  147. free(opts);
  148. /*
  149. * Bluetooth audio source init Start
  150. */
  151. //running_test = false;
  152. ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
  153. esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
  154. if (esp_bt_controller_init(&bt_cfg) != ESP_OK) {
  155. ESP_LOGE(TAG,"%s initialize controller failed\n", __func__);
  156. return;
  157. }
  158. if (esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT) != ESP_OK) {
  159. ESP_LOGE(TAG,"%s enable controller failed\n", __func__);
  160. return;
  161. }
  162. if (esp_bluedroid_init() != ESP_OK) {
  163. ESP_LOGE(TAG,"%s initialize bluedroid failed\n", __func__);
  164. return;
  165. }
  166. if (esp_bluedroid_enable() != ESP_OK) {
  167. ESP_LOGE(TAG,"%s enable bluedroid failed\n", __func__);
  168. return;
  169. }
  170. /* create application task */
  171. bt_app_task_start_up();
  172. /* Bluetooth device name, connection mode and profile set up */
  173. bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL);
  174. #if (CONFIG_BT_SSP_ENABLED == true)
  175. /* Set default parameters for Secure Simple Pairing */
  176. esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
  177. esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
  178. esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
  179. #endif
  180. /*
  181. * Set default parameters for Legacy Pairing
  182. * Use variable pin, input pin code when pairing
  183. */
  184. esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
  185. esp_bt_pin_code_t pin_code;
  186. esp_bt_gap_set_pin(pin_type, 0, pin_code);
  187. }
  188. static void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
  189. {
  190. bt_app_work_dispatch(bt_app_av_sm_hdlr, event, param, sizeof(esp_a2d_cb_param_t), NULL);
  191. }
  192. void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
  193. {
  194. switch (event) {
  195. case ESP_BT_GAP_DISC_RES_EVT: {
  196. filter_inquiry_scan_result(param);
  197. break;
  198. }
  199. case ESP_BT_GAP_DISC_STATE_CHANGED_EVT: {
  200. if (param->disc_st_chg.state == ESP_BT_GAP_DISCOVERY_STOPPED)
  201. {
  202. if (s_a2d_state == APP_AV_STATE_DISCOVERED)
  203. {
  204. ESP_LOGI(TAG,"Discovery completed. Ready to start connecting to %s. ",s_peer_bdname);
  205. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  206. }
  207. else
  208. {
  209. // not discovered, continue to discover
  210. ESP_LOGI(TAG,"Device discovery failed, continue to discover...");
  211. esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
  212. }
  213. }
  214. else if (param->disc_st_chg.state == ESP_BT_GAP_DISCOVERY_STARTED) {
  215. ESP_LOGI(TAG,"Discovery started.");
  216. }
  217. else
  218. {
  219. ESP_LOGD(TAG,"This shouldn't happen. Discovery has only 2 states (for now).");
  220. }
  221. break;
  222. }
  223. case ESP_BT_GAP_RMT_SRVCS_EVT:
  224. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_BT_GAP_RMT_SRVCS_EVT));
  225. break;
  226. case ESP_BT_GAP_RMT_SRVC_REC_EVT:
  227. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_BT_GAP_RMT_SRVC_REC_EVT));
  228. break;
  229. case ESP_BT_GAP_AUTH_CMPL_EVT: {
  230. if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
  231. ESP_LOGI(TAG,"authentication success: %s", param->auth_cmpl.device_name);
  232. //esp_log_buffer_hex(param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
  233. } else {
  234. ESP_LOGE(TAG,"authentication failed, status:%d", param->auth_cmpl.stat);
  235. }
  236. break;
  237. }
  238. case ESP_BT_GAP_PIN_REQ_EVT: {
  239. ESP_LOGI(TAG,"ESP_BT_GAP_PIN_REQ_EVT min_16_digit:%d", param->pin_req.min_16_digit);
  240. if (param->pin_req.min_16_digit) {
  241. ESP_LOGI(TAG,"Input pin code: 0000 0000 0000 0000");
  242. esp_bt_pin_code_t pin_code = {0};
  243. esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);
  244. } else {
  245. ESP_LOGI(TAG,"Input pin code: 1234");
  246. esp_bt_pin_code_t pin_code;
  247. pin_code[0] = '1';
  248. pin_code[1] = '2';
  249. pin_code[2] = '3';
  250. pin_code[3] = '4';
  251. esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code);
  252. }
  253. break;
  254. }
  255. #if (CONFIG_BT_SSP_ENABLED == true)
  256. case ESP_BT_GAP_CFM_REQ_EVT:
  257. ESP_LOGI(TAG,"ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
  258. esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
  259. break;
  260. case ESP_BT_GAP_KEY_NOTIF_EVT:
  261. ESP_LOGI(TAG,"ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
  262. break;
  263. ESP_LOGI(TAG,"ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
  264. break;
  265. #endif
  266. default: {
  267. ESP_LOGI(TAG,"event: %d", event);
  268. break;
  269. }
  270. }
  271. return;
  272. }
  273. static void a2d_app_heart_beat(void *arg)
  274. {
  275. bt_app_work_dispatch(bt_app_av_sm_hdlr, BT_APP_HEART_BEAT_EVT, NULL, 0, NULL);
  276. }
  277. static void bt_app_av_sm_hdlr(uint16_t event, void *param)
  278. {
  279. switch (s_a2d_state) {
  280. case APP_AV_STATE_DISCOVERING:
  281. ESP_LOGV(TAG,"state %s, evt 0x%x, output state: %s", APP_AV_STATE_DESC[s_a2d_state], event, output_state_str());
  282. break;
  283. case APP_AV_STATE_DISCOVERED:
  284. ESP_LOGV(TAG,"state %s, evt 0x%x, output state: %s", APP_AV_STATE_DESC[s_a2d_state], event, output_state_str());
  285. break;
  286. case APP_AV_STATE_UNCONNECTED:
  287. bt_app_av_state_unconnected(event, param);
  288. break;
  289. case APP_AV_STATE_CONNECTING:
  290. bt_app_av_state_connecting(event, param);
  291. break;
  292. case APP_AV_STATE_CONNECTED:
  293. bt_app_av_state_connected(event, param);
  294. break;
  295. case APP_AV_STATE_DISCONNECTING:
  296. bt_app_av_state_disconnecting(event, param);
  297. break;
  298. default:
  299. ESP_LOGE(TAG,"%s invalid state %d", __func__, s_a2d_state);
  300. break;
  301. }
  302. }
  303. static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
  304. {
  305. if (bda == NULL || str == NULL || size < 18) {
  306. return NULL;
  307. }
  308. uint8_t *p = bda;
  309. sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
  310. p[0], p[1], p[2], p[3], p[4], p[5]);
  311. return str;
  312. }
  313. static bool get_name_from_eir(uint8_t *eir, uint8_t *bdname, uint8_t *bdname_len)
  314. {
  315. uint8_t *rmt_bdname = NULL;
  316. uint8_t rmt_bdname_len = 0;
  317. if (!eir) {
  318. return false;
  319. }
  320. rmt_bdname = esp_bt_gap_resolve_eir_data(eir, ESP_BT_EIR_TYPE_CMPL_LOCAL_NAME, &rmt_bdname_len);
  321. if (!rmt_bdname) {
  322. rmt_bdname = esp_bt_gap_resolve_eir_data(eir, ESP_BT_EIR_TYPE_SHORT_LOCAL_NAME, &rmt_bdname_len);
  323. }
  324. if (rmt_bdname) {
  325. if (rmt_bdname_len > ESP_BT_GAP_MAX_BDNAME_LEN) {
  326. rmt_bdname_len = ESP_BT_GAP_MAX_BDNAME_LEN;
  327. }
  328. if (bdname) {
  329. memcpy(bdname, rmt_bdname, rmt_bdname_len);
  330. bdname[rmt_bdname_len] = '\0';
  331. }
  332. if (bdname_len) {
  333. *bdname_len = rmt_bdname_len;
  334. }
  335. return true;
  336. }
  337. return false;
  338. }
  339. static void filter_inquiry_scan_result(esp_bt_gap_cb_param_t *param)
  340. {
  341. char bda_str[18];
  342. uint32_t cod = 0;
  343. int32_t rssi = -129; /* invalid value */
  344. uint8_t *eir = NULL;
  345. uint8_t nameLen = 0;
  346. esp_bt_gap_dev_prop_t *p;
  347. if(s_a2d_state != APP_AV_STATE_DISCOVERING)
  348. {
  349. // Ignore messages that might have been queued already
  350. // when we've discovered the target device.
  351. return;
  352. }
  353. memset(s_peer_bdname, 0x00,sizeof(s_peer_bdname));
  354. ESP_LOGI(TAG,"\n=======================\nScanned device: %s", bda2str(param->disc_res.bda, bda_str, 18));
  355. for (int i = 0; i < param->disc_res.num_prop; i++) {
  356. p = param->disc_res.prop + i;
  357. switch (p->type) {
  358. case ESP_BT_GAP_DEV_PROP_COD:
  359. cod = *(uint32_t *)(p->val);
  360. ESP_LOGI(TAG,"-- Class of Device: 0x%x", cod);
  361. break;
  362. case ESP_BT_GAP_DEV_PROP_RSSI:
  363. rssi = *(int8_t *)(p->val);
  364. ESP_LOGI(TAG,"-- RSSI: %d", rssi);
  365. break;
  366. case ESP_BT_GAP_DEV_PROP_EIR:
  367. eir = (uint8_t *)(p->val);
  368. ESP_LOGI(TAG,"-- EIR: %u", *eir);
  369. break;
  370. case ESP_BT_GAP_DEV_PROP_BDNAME:
  371. nameLen = (p->len > ESP_BT_GAP_MAX_BDNAME_LEN) ? ESP_BT_GAP_MAX_BDNAME_LEN : (uint8_t)p->len;
  372. memcpy(s_peer_bdname, (uint8_t *)(p->val), nameLen);
  373. s_peer_bdname[nameLen] = '\0';
  374. ESP_LOGI(TAG,"-- Name: %s", s_peer_bdname);
  375. break;
  376. default:
  377. break;
  378. }
  379. }
  380. if (!esp_bt_gap_is_valid_cod(cod)){
  381. /* search for device with MAJOR service class as "rendering" in COD */
  382. ESP_LOGI(TAG,"--Invalid class of device. Skipping.\n");
  383. return;
  384. }
  385. else if (!(esp_bt_gap_get_cod_srvc(cod) & ESP_BT_COD_SRVC_RENDERING))
  386. {
  387. ESP_LOGI(TAG,"--Not a rendering device. Skipping.\n");
  388. return;
  389. }
  390. /* search for device named "ESP_SPEAKER" in its extended inqury response */
  391. if (eir) {
  392. ESP_LOGI(TAG,"--Getting details from eir.\n");
  393. get_name_from_eir(eir, s_peer_bdname, NULL);
  394. ESP_LOGI(TAG,"--Device name is %s\n",s_peer_bdname);
  395. }
  396. if (strcmp((char *)s_peer_bdname, CONFIG_A2DP_SINK_NAME) == 0) {
  397. ESP_LOGI(TAG,"Found a target device! address %s, name %s", bda_str, s_peer_bdname);
  398. ESP_LOGI(TAG,"=======================\n");
  399. if(esp_bt_gap_cancel_discovery()!=ESP_ERR_INVALID_STATE)
  400. {
  401. ESP_LOGI(TAG,"Cancel device discovery ...");
  402. memcpy(s_peer_bda, param->disc_res.bda, ESP_BD_ADDR_LEN);
  403. s_a2d_state = APP_AV_STATE_DISCOVERED;
  404. }
  405. else
  406. {
  407. ESP_LOGE(TAG,"Cancel device discovery failed...");
  408. }
  409. }
  410. else
  411. {
  412. ESP_LOGI(TAG,"Not the device we are looking for. Continuing scan.");
  413. }
  414. }
  415. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
  416. {
  417. switch (event) {
  418. case BT_APP_EVT_STACK_UP: {
  419. ESP_LOGI(TAG,"BT Stack going up.");
  420. /* set up device name */
  421. char *dev_name = CONFIG_A2DP_DEV_NAME;
  422. esp_bt_dev_set_device_name(dev_name);
  423. ESP_LOGI(TAG,"Preparing to connect to device: %s",CONFIG_A2DP_SINK_NAME);
  424. /* register GAP callback function */
  425. esp_bt_gap_register_callback(bt_app_gap_cb);
  426. /* initialize A2DP source */
  427. esp_a2d_register_callback(&bt_app_a2d_cb);
  428. esp_a2d_source_register_data_callback(&output_bt_data);
  429. esp_a2d_source_init();
  430. /* set discoverable and connectable mode */
  431. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  432. /* start device discovery */
  433. ESP_LOGI(TAG,"Starting device discovery...");
  434. s_a2d_state = APP_AV_STATE_DISCOVERING;
  435. esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
  436. /* create and start heart beat timer */
  437. do {
  438. int tmr_id = 0;
  439. s_tmr = xTimerCreate("connTmr", (CONFIG_A2DP_CONTROL_DELAY_MS / portTICK_RATE_MS),
  440. pdTRUE, (void *)tmr_id, a2d_app_heart_beat);
  441. xTimerStart(s_tmr, portMAX_DELAY);
  442. } while (0);
  443. break;
  444. }
  445. default:
  446. ESP_LOGE(TAG,"%s unhandled evt %d", __func__, event);
  447. break;
  448. }
  449. }
  450. static void bt_app_av_media_proc(uint16_t event, void *param)
  451. {
  452. esp_a2d_cb_param_t *a2d = NULL;
  453. switch (s_media_state) {
  454. case APP_AV_MEDIA_STATE_IDLE: {
  455. if (event == BT_APP_HEART_BEAT_EVT) {
  456. if(!output_stopped())
  457. {
  458. ESP_LOGI(TAG,"Output state is %s, Checking if A2DP is ready.", output_state_str());
  459. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_CHECK_SRC_RDY);
  460. }
  461. } else if (event == ESP_A2D_MEDIA_CTRL_ACK_EVT) {
  462. a2d = (esp_a2d_cb_param_t *)(param);
  463. if (a2d->media_ctrl_stat.cmd == ESP_A2D_MEDIA_CTRL_CHECK_SRC_RDY &&
  464. a2d->media_ctrl_stat.status == ESP_A2D_MEDIA_CTRL_ACK_SUCCESS
  465. ) {
  466. ESP_LOGI(TAG,"a2dp media ready, starting playback!");
  467. s_media_state = APP_AV_MEDIA_STATE_STARTING;
  468. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_START);
  469. }
  470. }
  471. break;
  472. }
  473. case APP_AV_MEDIA_STATE_STARTING: {
  474. if (event == ESP_A2D_MEDIA_CTRL_ACK_EVT) {
  475. a2d = (esp_a2d_cb_param_t *)(param);
  476. if (a2d->media_ctrl_stat.cmd == ESP_A2D_MEDIA_CTRL_START &&
  477. a2d->media_ctrl_stat.status == ESP_A2D_MEDIA_CTRL_ACK_SUCCESS) {
  478. ESP_LOGI(TAG,"a2dp media started successfully.");
  479. s_media_state = APP_AV_MEDIA_STATE_STARTED;
  480. } else {
  481. // not started succesfully, transfer to idle state
  482. ESP_LOGI(TAG,"a2dp media start failed.");
  483. s_media_state = APP_AV_MEDIA_STATE_IDLE;
  484. }
  485. }
  486. break;
  487. }
  488. case APP_AV_MEDIA_STATE_STARTED: {
  489. if (event == BT_APP_HEART_BEAT_EVT) {
  490. if(output_stopped()) {
  491. ESP_LOGI(TAG,"Output state is %s. Stopping a2dp media ...", output_state_str());
  492. s_media_state = APP_AV_MEDIA_STATE_STOPPING;
  493. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_STOP);
  494. } else {
  495. output_bt_tick();
  496. }
  497. }
  498. break;
  499. }
  500. case APP_AV_MEDIA_STATE_STOPPING: {
  501. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(APP_AV_MEDIA_STATE_STOPPING));
  502. if (event == ESP_A2D_MEDIA_CTRL_ACK_EVT) {
  503. a2d = (esp_a2d_cb_param_t *)(param);
  504. if (a2d->media_ctrl_stat.cmd == ESP_A2D_MEDIA_CTRL_STOP &&
  505. a2d->media_ctrl_stat.status == ESP_A2D_MEDIA_CTRL_ACK_SUCCESS) {
  506. ESP_LOGI(TAG,"a2dp media stopped successfully...");
  507. s_media_state = APP_AV_MEDIA_STATE_IDLE;
  508. } else {
  509. ESP_LOGI(TAG,"a2dp media stopping...");
  510. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_STOP);
  511. }
  512. }
  513. break;
  514. }
  515. case APP_AV_MEDIA_STATE_WAIT_DISCONNECT:{
  516. esp_a2d_source_disconnect(s_peer_bda);
  517. s_a2d_state = APP_AV_STATE_DISCONNECTING;
  518. ESP_LOGI(TAG,"a2dp disconnecting...");
  519. }
  520. }
  521. }
  522. static void bt_app_av_state_unconnected(uint16_t event, void *param)
  523. {
  524. switch (event) {
  525. case ESP_A2D_CONNECTION_STATE_EVT:
  526. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_CONNECTION_STATE_EVT));
  527. // this could happen if connection was established
  528. // right after we timed out. Pass the call down to the connecting
  529. // handler.
  530. esp_a2d_cb_param_t *a2d = (esp_a2d_cb_param_t *)(param);
  531. if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED){
  532. bt_app_av_state_connecting(event, param);
  533. }
  534. break;
  535. case ESP_A2D_AUDIO_STATE_EVT:
  536. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_STATE_EVT));
  537. break;
  538. case ESP_A2D_AUDIO_CFG_EVT:
  539. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_CFG_EVT));
  540. break;
  541. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  542. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_MEDIA_CTRL_ACK_EVT));
  543. break;
  544. case BT_APP_HEART_BEAT_EVT: {
  545. switch (esp_bluedroid_get_status()) {
  546. case ESP_BLUEDROID_STATUS_UNINITIALIZED:
  547. ESP_LOGV(TAG,"BlueDroid Status is ESP_BLUEDROID_STATUS_UNINITIALIZED.");
  548. break;
  549. case ESP_BLUEDROID_STATUS_INITIALIZED:
  550. ESP_LOGV(TAG,"BlueDroid Status is ESP_BLUEDROID_STATUS_INITIALIZED.");
  551. break;
  552. case ESP_BLUEDROID_STATUS_ENABLED:
  553. ESP_LOGV(TAG,"BlueDroid Status is ESP_BLUEDROID_STATUS_ENABLED.");
  554. break;
  555. default:
  556. break;
  557. }
  558. for(uint8_t l=0;art_a2dp_connecting[l][0]!='\0';l++){
  559. ESP_LOGI(TAG,"%s",art_a2dp_connecting[l]);
  560. }
  561. ESP_LOGI(TAG,"Device: %s", s_peer_bdname);
  562. if(esp_a2d_source_connect(s_peer_bda)==ESP_OK) {
  563. A2DP_TIMER_INIT;
  564. s_a2d_state = APP_AV_STATE_CONNECTING;
  565. }
  566. else {
  567. // there was an issue connecting... continue to discover
  568. ESP_LOGE(TAG,"Attempt at connecting failed, restart at discover...");
  569. esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
  570. }
  571. break;
  572. }
  573. default:
  574. ESP_LOGE(TAG,"%s unhandled evt %d", __func__, event);
  575. break;
  576. }
  577. }
  578. static void bt_app_av_state_connecting(uint16_t event, void *param)
  579. {
  580. esp_a2d_cb_param_t *a2d = NULL;
  581. switch (event) {
  582. case ESP_A2D_CONNECTION_STATE_EVT: {
  583. a2d = (esp_a2d_cb_param_t *)(param);
  584. if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED) {
  585. s_a2d_state = APP_AV_STATE_CONNECTED;
  586. s_media_state = APP_AV_MEDIA_STATE_IDLE;
  587. ESP_LOGD(TAG,"Setting scan mode to ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE");
  588. esp_bt_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE);
  589. ESP_LOGD(TAG,"Done setting scan mode. App state is now CONNECTED and media state IDLE.");
  590. for(uint8_t l=0;art_a2dp_connected[l][0]!='\0';l++){
  591. ESP_LOGI(TAG,"%s",art_a2dp_connected[l]);
  592. }
  593. } else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
  594. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  595. }
  596. break;
  597. }
  598. case ESP_A2D_AUDIO_STATE_EVT:
  599. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_STATE_EVT));
  600. break;
  601. case ESP_A2D_AUDIO_CFG_EVT:
  602. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_CFG_EVT));
  603. break;
  604. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  605. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_MEDIA_CTRL_ACK_EVT));
  606. break;
  607. case BT_APP_HEART_BEAT_EVT:
  608. if (IS_A2DP_TIMER_OVER)
  609. {
  610. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  611. ESP_LOGW(TAG,"A2DP Connect time out! Setting state to Unconnected. ");
  612. }
  613. ESP_LOGV(TAG,"BT_APP_HEART_BEAT_EVT");
  614. break;
  615. default:
  616. ESP_LOGE(TAG,"%s unhandled evt %d", __func__, event);
  617. break;
  618. }
  619. }
  620. static void bt_app_av_state_connected(uint16_t event, void *param)
  621. {
  622. esp_a2d_cb_param_t *a2d = NULL;
  623. switch (event) {
  624. case ESP_A2D_CONNECTION_STATE_EVT: {
  625. a2d = (esp_a2d_cb_param_t *)(param);
  626. if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
  627. ESP_LOGI(TAG,"a2dp disconnected");
  628. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  629. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  630. }
  631. break;
  632. }
  633. case ESP_A2D_AUDIO_STATE_EVT: {
  634. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_STATE_EVT));
  635. a2d = (esp_a2d_cb_param_t *)(param);
  636. if (ESP_A2D_AUDIO_STATE_STARTED == a2d->audio_stat.state) {
  637. s_pkt_cnt = 0;
  638. }
  639. break;
  640. }
  641. case ESP_A2D_AUDIO_CFG_EVT:
  642. // not suppposed to occur for A2DP source
  643. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_CFG_EVT));
  644. break;
  645. case ESP_A2D_MEDIA_CTRL_ACK_EVT:{
  646. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_MEDIA_CTRL_ACK_EVT));
  647. bt_app_av_media_proc(event, param);
  648. break;
  649. }
  650. case BT_APP_HEART_BEAT_EVT: {
  651. ESP_LOGV(TAG,QUOTE(BT_APP_HEART_BEAT_EVT));
  652. bt_app_av_media_proc(event, param);
  653. break;
  654. }
  655. default:
  656. ESP_LOGE(TAG,"%s unhandled evt %d", __func__, event);
  657. break;
  658. }
  659. }
  660. static void bt_app_av_state_disconnecting(uint16_t event, void *param)
  661. {
  662. esp_a2d_cb_param_t *a2d = NULL;
  663. switch (event) {
  664. case ESP_A2D_CONNECTION_STATE_EVT: {
  665. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_CONNECTION_STATE_EVT));
  666. a2d = (esp_a2d_cb_param_t *)(param);
  667. if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
  668. ESP_LOGI(TAG,"a2dp disconnected");
  669. s_a2d_state = APP_AV_STATE_UNCONNECTED;
  670. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  671. }
  672. break;
  673. }
  674. case ESP_A2D_AUDIO_STATE_EVT:
  675. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_STATE_EVT));
  676. break;
  677. case ESP_A2D_AUDIO_CFG_EVT:
  678. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_CFG_EVT));
  679. break;
  680. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  681. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_MEDIA_CTRL_ACK_EVT));
  682. break;
  683. case BT_APP_HEART_BEAT_EVT:
  684. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(BT_APP_HEART_BEAT_EVT));
  685. break;
  686. default:
  687. ESP_LOGE(TAG,"%s unhandled evt %d", __func__, event);
  688. break;
  689. }
  690. }