2
0

bt_app_source.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include "bt_app_core.h"
  6. #include "esp_log.h"
  7. #include "esp_bt.h"
  8. #include "esp_bt_device.h"
  9. #include "esp_bt_main.h"
  10. #include "esp_gap_bt_api.h"
  11. #include "esp_a2dp_api.h"
  12. #include "esp_avrc_api.h"
  13. #include "esp_console.h"
  14. #include "esp_pthread.h"
  15. #include "esp_system.h"
  16. #include "esp_wifi.h"
  17. #include "freertos/timers.h"
  18. #include "argtable3/argtable3.h"
  19. #include "platform_config.h"
  20. #include "messaging.h"
  21. #include "cJSON.h"
  22. #include "tools.h"
  23. static const char * TAG = "bt_app_source";
  24. static const char * BT_RC_CT_TAG="RCCT";
  25. extern int32_t output_bt_data(uint8_t *data, int32_t len);
  26. extern void output_bt_tick(void);
  27. extern void output_bt_stop(void);
  28. extern void output_bt_start(void);
  29. extern char* output_state_str(void);
  30. extern bool output_stopped(void);
  31. extern bool is_recovery_running;
  32. static void bt_app_av_state_connecting(uint16_t event, void *param);
  33. static void filter_inquiry_scan_result(esp_bt_gap_cb_param_t *param);
  34. char * APP_AV_STATE_DESC[] = {
  35. "APP_AV_STATE_IDLE",
  36. "APP_AV_STATE_DISCOVERING",
  37. "APP_AV_STATE_DISCOVERED",
  38. "APP_AV_STATE_UNCONNECTED",
  39. "APP_AV_STATE_CONNECTING",
  40. "APP_AV_STATE_CONNECTED",
  41. "APP_AV_STATE_DISCONNECTING"
  42. };
  43. static char * ESP_AVRC_CT_DESC[]={
  44. "ESP_AVRC_CT_CONNECTION_STATE_EVT",
  45. "ESP_AVRC_CT_PASSTHROUGH_RSP_EVT",
  46. "ESP_AVRC_CT_METADATA_RSP_EVT",
  47. "ESP_AVRC_CT_PLAY_STATUS_RSP_EVT",
  48. "ESP_AVRC_CT_CHANGE_NOTIFY_EVT",
  49. "ESP_AVRC_CT_REMOTE_FEATURES_EVT",
  50. "ESP_AVRC_CT_GET_RN_CAPABILITIES_RSP_EVT",
  51. "ESP_AVRC_CT_SET_ABSOLUTE_VOLUME_RSP_EVT"
  52. };
  53. #define BT_APP_HEART_BEAT_EVT (0xff00)
  54. // AVRCP used transaction label
  55. #define APP_RC_CT_TL_GET_CAPS (0)
  56. #define APP_RC_CT_TL_RN_VOLUME_CHANGE (1)
  57. #define PEERS_LIST_MAINTAIN_RESET -129
  58. #define PEERS_LIST_MAINTAIN_PURGE -129
  59. /// handler for bluetooth stack enabled events
  60. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
  61. /// callback function for A2DP source
  62. static void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
  63. /// callback function for AVRCP controller
  64. static void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
  65. /// avrc CT event handler
  66. static void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param);
  67. /// callback function for A2DP source audio data stream
  68. static void a2d_app_heart_beat(void *arg);
  69. /// A2DP application state machine
  70. static void bt_app_av_sm_hdlr(uint16_t event, void *param);
  71. /* A2DP application state machine handler for each state */
  72. static void bt_app_av_state_unconnected(uint16_t event, void *param);
  73. static void bt_app_av_state_connecting(uint16_t event, void *param);
  74. static void bt_app_av_state_connected(uint16_t event, void *param);
  75. static void bt_app_av_state_disconnecting(uint16_t event, void *param);
  76. static void handle_connect_state_unconnected(uint16_t event, esp_a2d_cb_param_t *param);
  77. static void handle_connect_state_connecting(uint16_t event, esp_a2d_cb_param_t *param);
  78. static void handle_connect_state_connected(uint16_t event, esp_a2d_cb_param_t *param);
  79. static void handle_connect_state_disconnecting(uint16_t event, esp_a2d_cb_param_t *param);
  80. static void bt_av_notify_evt_handler(uint8_t event_id, esp_avrc_rn_param_t *event_parameter);
  81. static esp_bd_addr_t s_peer_bda = {0};
  82. static uint8_t s_peer_bdname[ESP_BT_GAP_MAX_BDNAME_LEN + 1];
  83. int bt_app_source_a2d_state = APP_AV_STATE_IDLE;
  84. int bt_app_source_media_state = APP_AV_MEDIA_STATE_IDLE;
  85. static uint32_t s_pkt_cnt = 0;
  86. static TimerHandle_t s_tmr=NULL;
  87. static int prev_duration=10000;
  88. static esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap;
  89. static int s_connecting_intv = 0;
  90. cJSON * peers_list=NULL;
  91. static struct {
  92. char * sink_name;
  93. } squeezelite_conf;
  94. static cJSON * peers_list_get_entry(const char * s_peer_bdname){
  95. cJSON * element=NULL;
  96. cJSON_ArrayForEach(element,peers_list){
  97. cJSON * name = cJSON_GetObjectItem(element,"name");
  98. if(name && !strcmp(cJSON_GetStringValue(name),s_peer_bdname)){
  99. ESP_LOGV(TAG,"Entry name %s found in current scan list", s_peer_bdname);
  100. return element;
  101. }
  102. }
  103. ESP_LOGV(TAG,"Entry name %s NOT found in current scan list", s_peer_bdname);
  104. return NULL;
  105. }
  106. static void peers_list_reset(){
  107. cJSON * element=NULL;
  108. cJSON_ArrayForEach(element,peers_list){
  109. cJSON * rssi = cJSON_GetObjectItem(element,"rssi");
  110. if(rssi){
  111. rssi->valuedouble = -129;
  112. rssi->valueint = -129;
  113. }
  114. }
  115. }
  116. static void peers_list_purge(){
  117. cJSON * element=NULL;
  118. cJSON_ArrayForEach(element,peers_list){
  119. cJSON * rssi_val = cJSON_GetObjectItem(element,"rssi");
  120. if(rssi_val && rssi_val->valuedouble == -129){
  121. cJSON * name = cJSON_GetObjectItem(element,"name");
  122. ESP_LOGV(TAG,"Purging %s", cJSON_GetStringValue(name)?cJSON_GetStringValue(name):"Unknown");
  123. cJSON_DetachItemViaPointer(peers_list,element);
  124. cJSON_Delete(element);
  125. }
  126. }
  127. }
  128. static cJSON * peers_list_create_entry(const char * s_peer_bdname, int32_t rssi){
  129. cJSON * entry = cJSON_CreateObject();
  130. cJSON_AddStringToObject(entry,"name",s_peer_bdname);
  131. cJSON_AddNumberToObject(entry,"rssi",rssi);
  132. return entry;
  133. }
  134. static void peers_list_update_add(const char * s_peer_bdname, int32_t rssi){
  135. cJSON * element= peers_list_get_entry(s_peer_bdname);
  136. if(element){
  137. cJSON * rssi_val = cJSON_GetObjectItem(element,"rssi");
  138. if(rssi_val && rssi_val->valuedouble != rssi){
  139. ESP_LOGV(TAG,"Updating BT Sink Device: %s rssi to %i", s_peer_bdname,rssi);
  140. rssi_val->valuedouble = rssi;
  141. rssi_val->valueint = rssi;
  142. }
  143. }
  144. else {
  145. ESP_LOGI(TAG,"Found BT Sink Device: %s rssi is %i", s_peer_bdname,rssi);
  146. element = peers_list_create_entry( s_peer_bdname, rssi);
  147. cJSON_AddItemToArray(peers_list,element);
  148. }
  149. }
  150. static void peers_list_maintain(const char * s_peer_bdname, int32_t rssi){
  151. if(!peers_list){
  152. ESP_LOGV(TAG,"Initializing BT peers list");
  153. peers_list=cJSON_CreateArray();
  154. }
  155. if(rssi==PEERS_LIST_MAINTAIN_RESET){
  156. ESP_LOGV(TAG,"Resetting BT peers list");
  157. peers_list_reset();
  158. }
  159. else if(rssi==PEERS_LIST_MAINTAIN_PURGE){
  160. ESP_LOGV(TAG,"Purging BT peers list");
  161. peers_list_purge();
  162. }
  163. if(s_peer_bdname) {
  164. ESP_LOGV(TAG,"Adding/Updating peer %s rssi %i", s_peer_bdname,rssi);
  165. peers_list_update_add(s_peer_bdname, rssi);
  166. }
  167. char * list_json = cJSON_Print(peers_list);
  168. if(list_json){
  169. messaging_post_message(MESSAGING_INFO, MESSAGING_CLASS_BT, list_json);
  170. ESP_LOGV(TAG,"%s", list_json);
  171. free(list_json);
  172. }
  173. }
  174. int bt_app_source_get_a2d_state(){
  175. if(!is_recovery_running){
  176. // if we are in recovery mode, don't log BT status
  177. ESP_LOGD(TAG,"a2dp status: %u = %s", bt_app_source_a2d_state, APP_AV_STATE_DESC[bt_app_source_a2d_state]);
  178. }
  179. return bt_app_source_a2d_state;
  180. }
  181. int bt_app_source_get_media_state(){
  182. ESP_LOGD(TAG,"media state : %u ", bt_app_source_media_state);
  183. return bt_app_source_media_state;
  184. }
  185. void set_app_source_state(int new_state){
  186. if(bt_app_source_a2d_state!=new_state){
  187. ESP_LOGD(TAG, "Updating state from %s to %s", APP_AV_STATE_DESC[bt_app_source_a2d_state], APP_AV_STATE_DESC[new_state]);
  188. bt_app_source_a2d_state=new_state;
  189. }
  190. }
  191. void set_a2dp_media_state(int new_state){
  192. if(bt_app_source_media_state!=new_state){
  193. bt_app_source_media_state=new_state;
  194. }
  195. }
  196. void hal_bluetooth_init(const char * options)
  197. {
  198. struct {
  199. struct arg_str *sink_name;
  200. struct arg_end *end;
  201. } squeezelite_args;
  202. ESP_LOGD(TAG,"Initializing Bluetooth HAL");
  203. squeezelite_args.sink_name = arg_str0("n", "name", "<sink name>", "the name of the bluetooth to connect to");
  204. squeezelite_args.end = arg_end(2);
  205. ESP_LOGD(TAG,"Copying parameters");
  206. char * opts = strdup_psram(options);
  207. char **argv = malloc_init_external(sizeof(char**)*15);
  208. size_t argv_size=15;
  209. // change parms so ' appear as " for parsing the options
  210. for (char* p = opts; (p = strchr(p, '\'')); ++p) *p = '"';
  211. ESP_LOGD(TAG,"Splitting arg line: %s", opts);
  212. argv_size = esp_console_split_argv(opts, argv, argv_size);
  213. ESP_LOGD(TAG,"Parsing parameters");
  214. int nerrors = arg_parse(argv_size , argv, (void **) &squeezelite_args);
  215. if (nerrors != 0) {
  216. ESP_LOGD(TAG,"Parsing Errors");
  217. arg_print_errors(stdout, squeezelite_args.end, "BT");
  218. arg_print_glossary_gnu(stdout, (void **) &squeezelite_args);
  219. free(opts);
  220. free(argv);
  221. return;
  222. }
  223. if(squeezelite_args.sink_name->count == 0)
  224. {
  225. squeezelite_conf.sink_name = config_alloc_get_default(NVS_TYPE_STR, "a2dp_sink_name", NULL, 0);
  226. if(!squeezelite_conf.sink_name || strlen(squeezelite_conf.sink_name)==0 ){
  227. ESP_LOGW(TAG,"Unable to retrieve the a2dp sink name from nvs.");
  228. }
  229. } else {
  230. squeezelite_conf.sink_name=strdup_psram(squeezelite_args.sink_name->sval[0]);
  231. // sync with NVS
  232. esp_err_t err=ESP_OK;
  233. if((err= config_set_value(NVS_TYPE_STR, "a2dp_sink_name", squeezelite_args.sink_name->sval[0]))!=ESP_OK){
  234. ESP_LOGE(TAG,"Error setting Bluetooth audio device name %s. %s",squeezelite_args.sink_name->sval[0], esp_err_to_name(err));
  235. }
  236. else {
  237. ESP_LOGI(TAG,"Bluetooth audio device name changed to %s",squeezelite_args.sink_name->sval[0]);
  238. }
  239. }
  240. ESP_LOGD(TAG,"Freeing options");
  241. free(argv);
  242. free(opts);
  243. // create task and run event loop
  244. bt_app_task_start_up(bt_av_hdl_stack_evt);
  245. /*
  246. * Set default parameters for Legacy Pairing
  247. * Use variable pin, input pin code when pairing
  248. */
  249. esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
  250. esp_bt_pin_code_t pin_code;
  251. esp_bt_gap_set_pin(pin_type, 0, pin_code);
  252. }
  253. void hal_bluetooth_stop(void) {
  254. bt_app_task_shut_down();
  255. }
  256. static void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
  257. {
  258. bt_app_work_dispatch(bt_app_av_sm_hdlr, event, param, sizeof(esp_a2d_cb_param_t), NULL);
  259. }
  260. static void handle_bt_gap_pin_req(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param){
  261. char * pin_str = config_alloc_get_default(NVS_TYPE_STR, "a2dp_spin", "0000", 0);
  262. int pinlen=pin_str?strlen(pin_str):0;
  263. if (pin_str && ((!param->pin_req.min_16_digit && pinlen==4) || (param->pin_req.min_16_digit && pinlen==16))) {
  264. ESP_LOGI(TAG,"Input pin code %s: ",pin_str);
  265. esp_bt_pin_code_t pin_code;
  266. for (size_t i = 0; i < pinlen; i++)
  267. {
  268. pin_code[i] = pin_str[i];
  269. }
  270. esp_bt_gap_pin_reply(param->pin_req.bda, true, pinlen, pin_code);
  271. }
  272. else {
  273. if(pinlen>0){
  274. ESP_LOGW(TAG,"Pin length: %u does not match the length expected by the device: %u", pinlen, ((param->pin_req.min_16_digit)?16:4));
  275. }
  276. else {
  277. ESP_LOGW(TAG, "No security Pin provided. Trying with default pins.");
  278. }
  279. if (param->pin_req.min_16_digit) {
  280. ESP_LOGI(TAG,"Input pin code: 0000 0000 0000 0000");
  281. esp_bt_pin_code_t pin_code = {0};
  282. esp_bt_gap_pin_reply(param->pin_req.bda, true, 16, pin_code);
  283. } else {
  284. ESP_LOGI(TAG,"Input pin code: 1234");
  285. esp_bt_pin_code_t pin_code;
  286. pin_code[0] = '1';
  287. pin_code[1] = '2';
  288. pin_code[2] = '3';
  289. pin_code[3] = '4';
  290. esp_bt_gap_pin_reply(param->pin_req.bda, true, 4, pin_code);
  291. }
  292. }
  293. FREE_AND_NULL(pin_str);
  294. }
  295. static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
  296. {
  297. switch (event) {
  298. case ESP_BT_GAP_DISC_RES_EVT: {
  299. filter_inquiry_scan_result(param);
  300. break;
  301. }
  302. case ESP_BT_GAP_DISC_STATE_CHANGED_EVT: {
  303. if (param->disc_st_chg.state == ESP_BT_GAP_DISCOVERY_STOPPED) {
  304. peers_list_maintain(NULL, PEERS_LIST_MAINTAIN_PURGE);
  305. if (bt_app_source_a2d_state == APP_AV_STATE_DISCOVERED) {
  306. set_app_source_state(APP_AV_STATE_CONNECTING);
  307. ESP_LOGI(TAG,"Discovery completed. Ready to start connecting to %s. ", s_peer_bdname);
  308. esp_a2d_source_connect(s_peer_bda);
  309. } else {
  310. // not discovered, continue to discover
  311. ESP_LOGI(TAG, "Device discovery failed, continue to discover...");
  312. esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
  313. }
  314. } else if (param->disc_st_chg.state == ESP_BT_GAP_DISCOVERY_STARTED) {
  315. ESP_LOGI(TAG, "Discovery started.");
  316. peers_list_maintain(NULL, PEERS_LIST_MAINTAIN_RESET);
  317. }
  318. break;
  319. }
  320. case ESP_BT_GAP_RMT_SRVCS_EVT:
  321. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_BT_GAP_RMT_SRVCS_EVT));
  322. break;
  323. case ESP_BT_GAP_RMT_SRVC_REC_EVT:
  324. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_BT_GAP_RMT_SRVC_REC_EVT));
  325. break;
  326. case ESP_BT_GAP_AUTH_CMPL_EVT: {
  327. if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
  328. ESP_LOGI(TAG,"authentication success: %s", param->auth_cmpl.device_name);
  329. //esp_log_buffer_hex(param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
  330. } else {
  331. ESP_LOGE(TAG,"authentication failed, status:%d", param->auth_cmpl.stat);
  332. }
  333. break;
  334. }
  335. case ESP_BT_GAP_PIN_REQ_EVT:
  336. handle_bt_gap_pin_req(event, param);
  337. break;
  338. #if (CONFIG_BT_SSP_ENABLED == true)
  339. case ESP_BT_GAP_CFM_REQ_EVT:
  340. ESP_LOGI(TAG,"ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
  341. esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
  342. break;
  343. case ESP_BT_GAP_KEY_NOTIF_EVT:
  344. ESP_LOGI(TAG,"ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
  345. break;
  346. ESP_LOGI(TAG,"ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
  347. break;
  348. #endif
  349. default: {
  350. ESP_LOGI(TAG,"event: %d", event);
  351. break;
  352. }
  353. }
  354. return;
  355. }
  356. int heart_beat_delay[] = {
  357. 1000,
  358. 1000,
  359. 1000,
  360. 1000,
  361. 10000,
  362. 500,
  363. 1000
  364. };
  365. static void a2d_app_heart_beat(void *arg)
  366. {
  367. bt_app_work_dispatch(bt_app_av_sm_hdlr, BT_APP_HEART_BEAT_EVT, NULL, 0, NULL);
  368. int tmrduration=heart_beat_delay[bt_app_source_a2d_state];
  369. if(prev_duration!=tmrduration){
  370. xTimerChangePeriod(s_tmr,tmrduration, portMAX_DELAY);
  371. ESP_LOGD(TAG,"New heartbeat is %u",tmrduration);
  372. prev_duration=tmrduration;
  373. }
  374. else {
  375. ESP_LOGD(TAG,"Starting Heart beat timer for %ums",tmrduration);
  376. }
  377. xTimerStart(s_tmr, portMAX_DELAY);
  378. }
  379. static const char * conn_state_str(esp_a2d_connection_state_t state){
  380. char * statestr = "Unknown";
  381. switch (state)
  382. {
  383. case ESP_A2D_CONNECTION_STATE_DISCONNECTED:
  384. statestr=STR(ESP_A2D_CONNECTION_STATE_DISCONNECTED);
  385. break;
  386. case ESP_A2D_CONNECTION_STATE_CONNECTING:
  387. statestr=STR(ESP_A2D_CONNECTION_STATE_CONNECTING);
  388. break;
  389. case ESP_A2D_CONNECTION_STATE_CONNECTED:
  390. statestr=STR(ESP_A2D_CONNECTION_STATE_CONNECTED);
  391. break;
  392. case ESP_A2D_CONNECTION_STATE_DISCONNECTING:
  393. statestr=STR(ESP_A2D_CONNECTION_STATE_DISCONNECTING);
  394. break;
  395. default:
  396. break;
  397. }
  398. return statestr;
  399. }
  400. static void unexpected_connection_state(int from, esp_a2d_connection_state_t to)
  401. {
  402. ESP_LOGW(TAG,"Unexpected connection state change. App State: %s (%u) Connection State %s (%u)", APP_AV_STATE_DESC[from], from,conn_state_str(to), to);
  403. }
  404. static void handle_connect_state_unconnected(uint16_t event, esp_a2d_cb_param_t *param){
  405. ESP_LOGV(TAG, "A2DP Event while unconnected ");
  406. switch (param->conn_stat.state)
  407. {
  408. case ESP_A2D_CONNECTION_STATE_DISCONNECTED:
  409. unexpected_connection_state(bt_app_source_a2d_state, param->conn_stat.state);
  410. break;
  411. case ESP_A2D_CONNECTION_STATE_CONNECTING:
  412. unexpected_connection_state(bt_app_source_a2d_state, param->conn_stat.state);
  413. break;
  414. case ESP_A2D_CONNECTION_STATE_CONNECTED:
  415. unexpected_connection_state(bt_app_source_a2d_state, param->conn_stat.state);
  416. ESP_LOGE(TAG,"Connection state event received while status was unconnected. Routing message to connecting state handler. State : %u",param->conn_stat.state);
  417. if (param->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED){
  418. handle_connect_state_connecting(event, param);
  419. }
  420. break;
  421. case ESP_A2D_CONNECTION_STATE_DISCONNECTING:
  422. unexpected_connection_state(bt_app_source_a2d_state, param->conn_stat.state);
  423. break;
  424. default:
  425. break;
  426. }
  427. }
  428. static void handle_connect_state_connecting(uint16_t event, esp_a2d_cb_param_t *param){
  429. ESP_LOGV(TAG, "A2DP connection state event : %s ",conn_state_str(param->conn_stat.state));
  430. switch (param->conn_stat.state)
  431. {
  432. case ESP_A2D_CONNECTION_STATE_DISCONNECTED:
  433. if(param->conn_stat.disc_rsn!=ESP_A2D_DISC_RSN_NORMAL){
  434. ESP_LOGE(TAG,"A2DP had an abnormal disconnect event");
  435. }
  436. else {
  437. ESP_LOGW(TAG,"A2DP connect unsuccessful");
  438. }
  439. set_app_source_state(APP_AV_STATE_UNCONNECTED);
  440. break;
  441. case ESP_A2D_CONNECTION_STATE_CONNECTING:
  442. break;
  443. case ESP_A2D_CONNECTION_STATE_CONNECTED:
  444. set_app_source_state(APP_AV_STATE_CONNECTED);
  445. set_a2dp_media_state(APP_AV_MEDIA_STATE_IDLE);
  446. ESP_LOGD(TAG,"Setting scan mode to ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE");
  447. esp_bt_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE);
  448. ESP_LOGD(TAG,"Done setting scan mode. App state is now CONNECTED and media state IDLE.");
  449. break;
  450. case ESP_A2D_CONNECTION_STATE_DISCONNECTING:
  451. unexpected_connection_state(bt_app_source_a2d_state, param->conn_stat.state);
  452. set_app_source_state(APP_AV_STATE_DISCONNECTING);
  453. break;
  454. default:
  455. break;
  456. }
  457. }
  458. static void handle_connect_state_connected(uint16_t event, esp_a2d_cb_param_t *param){
  459. ESP_LOGV(TAG, "A2DP Event while connected ");
  460. switch (param->conn_stat.state)
  461. {
  462. case ESP_A2D_CONNECTION_STATE_DISCONNECTED:
  463. ESP_LOGW(TAG,"a2dp disconnected");
  464. set_app_source_state(APP_AV_STATE_UNCONNECTED);
  465. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  466. break;
  467. case ESP_A2D_CONNECTION_STATE_CONNECTING:
  468. unexpected_connection_state(bt_app_source_a2d_state, param->conn_stat.state);
  469. break;
  470. case ESP_A2D_CONNECTION_STATE_CONNECTED:
  471. unexpected_connection_state(bt_app_source_a2d_state, param->conn_stat.state);
  472. break;
  473. case ESP_A2D_CONNECTION_STATE_DISCONNECTING:
  474. set_app_source_state(APP_AV_STATE_DISCONNECTING);
  475. break;
  476. default:
  477. break;
  478. }
  479. }
  480. static void handle_connect_state_disconnecting(uint16_t event, esp_a2d_cb_param_t *param){
  481. ESP_LOGV(TAG, "A2DP Event while disconnecting ");
  482. switch (param->conn_stat.state)
  483. {
  484. case ESP_A2D_CONNECTION_STATE_DISCONNECTED:
  485. ESP_LOGI(TAG,"a2dp disconnected");
  486. set_app_source_state(APP_AV_STATE_UNCONNECTED);
  487. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  488. break;
  489. case ESP_A2D_CONNECTION_STATE_CONNECTING:
  490. unexpected_connection_state(bt_app_source_a2d_state, param->conn_stat.state);
  491. break;
  492. case ESP_A2D_CONNECTION_STATE_CONNECTED:
  493. unexpected_connection_state(bt_app_source_a2d_state, param->conn_stat.state);
  494. break;
  495. case ESP_A2D_CONNECTION_STATE_DISCONNECTING:
  496. unexpected_connection_state(bt_app_source_a2d_state, param->conn_stat.state);
  497. break;
  498. default:
  499. break;
  500. }
  501. }
  502. static void bt_app_av_sm_hdlr(uint16_t event, void *param)
  503. {
  504. ESP_LOGV(TAG,"bt_app_av_sm_hdlr.%s a2d state: %s", event==BT_APP_HEART_BEAT_EVT?"Heart Beat.":"",APP_AV_STATE_DESC[bt_app_source_a2d_state]);
  505. switch (bt_app_source_a2d_state) {
  506. case APP_AV_STATE_DISCOVERING:
  507. ESP_LOGV(TAG,"state %s, evt 0x%x, output state: %s", APP_AV_STATE_DESC[bt_app_source_a2d_state], event, output_state_str());
  508. break;
  509. case APP_AV_STATE_DISCOVERED:
  510. ESP_LOGV(TAG,"state %s, evt 0x%x, output state: %s", APP_AV_STATE_DESC[bt_app_source_a2d_state], event, output_state_str());
  511. break;
  512. case APP_AV_STATE_UNCONNECTED:
  513. bt_app_av_state_unconnected(event, param);
  514. break;
  515. case APP_AV_STATE_CONNECTING:
  516. bt_app_av_state_connecting(event, param);
  517. break;
  518. case APP_AV_STATE_CONNECTED:
  519. bt_app_av_state_connected(event, param);
  520. break;
  521. case APP_AV_STATE_DISCONNECTING:
  522. bt_app_av_state_disconnecting(event, param);
  523. break;
  524. default:
  525. ESP_LOGE(TAG,"%s invalid state %d", __func__, bt_app_source_a2d_state);
  526. break;
  527. }
  528. }
  529. static char *bda2str(esp_bd_addr_t bda, char *str, size_t size)
  530. {
  531. if (bda == NULL || str == NULL || size < 18) {
  532. return NULL;
  533. }
  534. uint8_t *p = bda;
  535. sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
  536. p[0], p[1], p[2], p[3], p[4], p[5]);
  537. return str;
  538. }
  539. static bool get_name_from_eir(uint8_t *eir, uint8_t *bdname, uint8_t *bdname_len)
  540. {
  541. uint8_t *rmt_bdname = NULL;
  542. uint8_t rmt_bdname_len = 0;
  543. if (!eir) {
  544. return false;
  545. }
  546. rmt_bdname = esp_bt_gap_resolve_eir_data(eir, ESP_BT_EIR_TYPE_CMPL_LOCAL_NAME, &rmt_bdname_len);
  547. if (!rmt_bdname) {
  548. rmt_bdname = esp_bt_gap_resolve_eir_data(eir, ESP_BT_EIR_TYPE_SHORT_LOCAL_NAME, &rmt_bdname_len);
  549. }
  550. if (rmt_bdname) {
  551. if (rmt_bdname_len > ESP_BT_GAP_MAX_BDNAME_LEN) {
  552. rmt_bdname_len = ESP_BT_GAP_MAX_BDNAME_LEN;
  553. }
  554. if (bdname) {
  555. memcpy(bdname, rmt_bdname, rmt_bdname_len);
  556. bdname[rmt_bdname_len] = '\0';
  557. }
  558. if (bdname_len) {
  559. *bdname_len = rmt_bdname_len;
  560. }
  561. return true;
  562. }
  563. return false;
  564. }
  565. static void filter_inquiry_scan_result(esp_bt_gap_cb_param_t *param)
  566. {
  567. char bda_str[18];
  568. uint32_t cod = 0;
  569. int32_t rssi = -129; /* invalid value */
  570. uint8_t *eir = NULL;
  571. uint8_t nameLen = 0;
  572. esp_bt_gap_dev_prop_t *p;
  573. memset(bda_str, 0x00, sizeof(bda_str));
  574. if(bt_app_source_a2d_state != APP_AV_STATE_DISCOVERING)
  575. {
  576. // Ignore messages that might have been queued already
  577. // when we've discovered the target device.
  578. return;
  579. }
  580. memset(s_peer_bdname, 0x00,sizeof(s_peer_bdname));
  581. bda2str(param->disc_res.bda, bda_str, 18);
  582. ESP_LOGV(TAG,"\n=======================\nScanned device: %s",bda_str );
  583. for (int i = 0; i < param->disc_res.num_prop; i++) {
  584. p = param->disc_res.prop + i;
  585. switch (p->type) {
  586. case ESP_BT_GAP_DEV_PROP_COD:
  587. cod = *(uint32_t *)(p->val);
  588. ESP_LOGV(TAG,"-- Class of Device: 0x%x", cod);
  589. break;
  590. case ESP_BT_GAP_DEV_PROP_RSSI:
  591. rssi = *(int8_t *)(p->val);
  592. ESP_LOGV(TAG,"-- RSSI: %d", rssi);
  593. break;
  594. case ESP_BT_GAP_DEV_PROP_EIR:
  595. eir = (uint8_t *)(p->val);
  596. ESP_LOGV(TAG,"-- EIR: %u", *eir);
  597. break;
  598. case ESP_BT_GAP_DEV_PROP_BDNAME:
  599. nameLen = (p->len > ESP_BT_GAP_MAX_BDNAME_LEN) ? ESP_BT_GAP_MAX_BDNAME_LEN : (uint8_t)p->len;
  600. memcpy(s_peer_bdname, (uint8_t *)(p->val), nameLen);
  601. s_peer_bdname[nameLen] = '\0';
  602. ESP_LOGV(TAG,"-- Name: %s", s_peer_bdname);
  603. break;
  604. default:
  605. break;
  606. }
  607. }
  608. if (!esp_bt_gap_is_valid_cod(cod)){
  609. /* search for device with MAJOR service class as "rendering" in COD */
  610. ESP_LOGV(TAG,"--Invalid class of device. Skipping.\n");
  611. return;
  612. }
  613. else if (!(esp_bt_gap_get_cod_srvc(cod) & (ESP_BT_COD_SRVC_RENDERING | ESP_BT_COD_SRVC_AUDIO)))
  614. {
  615. ESP_LOGV(TAG,"--Not a rendering or audio device. Skipping.\n");
  616. return;
  617. }
  618. if (eir) {
  619. ESP_LOGV(TAG,"--Getting details from eir.\n");
  620. get_name_from_eir(eir, s_peer_bdname, NULL);
  621. ESP_LOGV(TAG,"--Device name is %s\n",s_peer_bdname);
  622. }
  623. if(strlen((char *)s_peer_bdname)>0) {
  624. peers_list_maintain((const char *)s_peer_bdname, rssi);
  625. }
  626. if (squeezelite_conf.sink_name && strlen(squeezelite_conf.sink_name) >0 && strcmp((char *)s_peer_bdname, squeezelite_conf.sink_name) == 0) {
  627. ESP_LOGI(TAG,"Found our target device. address %s, name %s", bda_str, s_peer_bdname);
  628. memcpy(s_peer_bda, param->disc_res.bda, ESP_BD_ADDR_LEN);
  629. set_app_source_state(APP_AV_STATE_DISCOVERED);
  630. esp_bt_gap_cancel_discovery();
  631. } else {
  632. ESP_LOGV(TAG,"Not the device we are looking for (%s). Continuing scan", squeezelite_conf.sink_name?squeezelite_conf.sink_name:"N/A");
  633. }
  634. }
  635. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
  636. {
  637. switch (event) {
  638. case BT_APP_EVT_STACK_UP: {
  639. ESP_LOGI(TAG,"BT Stack going up.");
  640. /* set up device name */
  641. char * a2dp_dev_name = config_alloc_get_default(NVS_TYPE_STR, "a2dp_dev_name", CONFIG_A2DP_DEV_NAME, 0);
  642. if(a2dp_dev_name == NULL){
  643. ESP_LOGW(TAG,"Unable to retrieve the a2dp device name from nvs");
  644. esp_bt_dev_set_device_name(CONFIG_A2DP_DEV_NAME);
  645. }
  646. else {
  647. esp_bt_dev_set_device_name(a2dp_dev_name);
  648. free(a2dp_dev_name);
  649. }
  650. ESP_LOGI(TAG,"Preparing to connect");
  651. /* register GAP callback function */
  652. esp_bt_gap_register_callback(bt_app_gap_cb);
  653. /* initialize AVRCP controller */
  654. esp_avrc_ct_init();
  655. esp_avrc_ct_register_callback(bt_app_rc_ct_cb);
  656. esp_avrc_rn_evt_cap_mask_t evt_set = {0};
  657. esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_SET, &evt_set, ESP_AVRC_RN_VOLUME_CHANGE);
  658. assert(esp_avrc_tg_set_rn_evt_cap(&evt_set) == ESP_OK);
  659. /* initialize A2DP source */
  660. esp_a2d_register_callback(&bt_app_a2d_cb);
  661. esp_a2d_source_register_data_callback(&output_bt_data);
  662. esp_a2d_source_init();
  663. /* set discoverable and connectable mode */
  664. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  665. /* start device discovery */
  666. ESP_LOGI(TAG,"Starting device discovery...");
  667. set_app_source_state(APP_AV_STATE_DISCOVERING);
  668. esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
  669. /* create and start heart beat timer */
  670. int tmr_id = 0;
  671. s_tmr = xTimerCreate("connTmr", ( prev_duration/ portTICK_RATE_MS),pdFALSE, (void *)tmr_id, a2d_app_heart_beat);
  672. xTimerStart(s_tmr, portMAX_DELAY);
  673. break;
  674. }
  675. default:
  676. ESP_LOGE(TAG,"%s unhandled evt %d", __func__, event);
  677. break;
  678. }
  679. }
  680. static void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param)
  681. {
  682. switch (event) {
  683. case ESP_AVRC_CT_METADATA_RSP_EVT:
  684. case ESP_AVRC_CT_CONNECTION_STATE_EVT:
  685. case ESP_AVRC_CT_PASSTHROUGH_RSP_EVT:
  686. case ESP_AVRC_CT_CHANGE_NOTIFY_EVT:
  687. case ESP_AVRC_CT_REMOTE_FEATURES_EVT:
  688. case ESP_AVRC_CT_GET_RN_CAPABILITIES_RSP_EVT:
  689. case ESP_AVRC_CT_SET_ABSOLUTE_VOLUME_RSP_EVT: {
  690. ESP_LOGD(TAG,"Received %s message", ESP_AVRC_CT_DESC[event]);
  691. bt_app_work_dispatch(bt_av_hdl_avrc_ct_evt, event, param, sizeof(esp_avrc_ct_cb_param_t), NULL);
  692. break;
  693. }
  694. default:
  695. ESP_LOGE(BT_RC_CT_TAG, "Invalid AVRC event: %d", event);
  696. break;
  697. }
  698. }
  699. static void bt_app_av_media_proc(uint16_t event, void *param)
  700. {
  701. esp_a2d_cb_param_t *a2d = NULL;
  702. switch (bt_app_source_media_state) {
  703. case APP_AV_MEDIA_STATE_IDLE: {
  704. if (event == BT_APP_HEART_BEAT_EVT) {
  705. if(!output_stopped())
  706. {
  707. ESP_LOGI(TAG,"Output state is %s, Checking if A2DP is ready.", output_state_str());
  708. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_CHECK_SRC_RDY);
  709. }
  710. } else if (event == ESP_A2D_MEDIA_CTRL_ACK_EVT) {
  711. a2d = (esp_a2d_cb_param_t *)(param);
  712. if (a2d->media_ctrl_stat.cmd == ESP_A2D_MEDIA_CTRL_CHECK_SRC_RDY &&
  713. a2d->media_ctrl_stat.status == ESP_A2D_MEDIA_CTRL_ACK_SUCCESS
  714. ) {
  715. ESP_LOGI(TAG,"a2dp media ready, starting playback!");
  716. set_a2dp_media_state(APP_AV_MEDIA_STATE_STARTING);
  717. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_START);
  718. }
  719. }
  720. break;
  721. }
  722. case APP_AV_MEDIA_STATE_STARTING: {
  723. if (event == ESP_A2D_MEDIA_CTRL_ACK_EVT) {
  724. a2d = (esp_a2d_cb_param_t *)(param);
  725. if (a2d->media_ctrl_stat.cmd == ESP_A2D_MEDIA_CTRL_START &&
  726. a2d->media_ctrl_stat.status == ESP_A2D_MEDIA_CTRL_ACK_SUCCESS) {
  727. ESP_LOGI(TAG,"a2dp media started successfully.");
  728. output_bt_start();
  729. set_a2dp_media_state(APP_AV_MEDIA_STATE_STARTED);
  730. } else {
  731. // not started succesfully, transfer to idle state
  732. ESP_LOGI(TAG,"a2dp media start failed.");
  733. set_a2dp_media_state(APP_AV_MEDIA_STATE_IDLE);
  734. }
  735. }
  736. break;
  737. }
  738. case APP_AV_MEDIA_STATE_STARTED: {
  739. if (event == BT_APP_HEART_BEAT_EVT) {
  740. if(output_stopped()) {
  741. ESP_LOGI(TAG,"Output state is %s. Stopping a2dp media ...", output_state_str());
  742. set_a2dp_media_state(APP_AV_MEDIA_STATE_STOPPING);
  743. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_STOP);
  744. } else {
  745. output_bt_tick();
  746. }
  747. }
  748. break;
  749. }
  750. case APP_AV_MEDIA_STATE_STOPPING: {
  751. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(APP_AV_MEDIA_STATE_STOPPING));
  752. if (event == ESP_A2D_MEDIA_CTRL_ACK_EVT) {
  753. a2d = (esp_a2d_cb_param_t *)(param);
  754. if (a2d->media_ctrl_stat.cmd == ESP_A2D_MEDIA_CTRL_STOP &&
  755. a2d->media_ctrl_stat.status == ESP_A2D_MEDIA_CTRL_ACK_SUCCESS) {
  756. ESP_LOGI(TAG,"a2dp media stopped successfully...");
  757. output_bt_stop();
  758. set_a2dp_media_state(APP_AV_MEDIA_STATE_IDLE);
  759. } else {
  760. ESP_LOGI(TAG,"a2dp media stopping...");
  761. esp_a2d_media_ctrl(ESP_A2D_MEDIA_CTRL_STOP);
  762. }
  763. }
  764. break;
  765. }
  766. case APP_AV_MEDIA_STATE_WAIT_DISCONNECT:{
  767. esp_a2d_source_disconnect(s_peer_bda);
  768. set_app_source_state(APP_AV_STATE_DISCONNECTING);
  769. ESP_LOGI(TAG,"a2dp disconnecting...");
  770. }
  771. }
  772. }
  773. static void bt_app_av_state_unconnected(uint16_t event, void *param)
  774. {
  775. ESP_LOGV(TAG, "Handling state unconnected A2DP event");
  776. switch (event) {
  777. case ESP_A2D_CONNECTION_STATE_EVT:
  778. handle_connect_state_unconnected(event, (esp_a2d_cb_param_t *)param);
  779. break;
  780. case ESP_A2D_AUDIO_STATE_EVT:
  781. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_STATE_EVT));
  782. break;
  783. case ESP_A2D_AUDIO_CFG_EVT:
  784. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_CFG_EVT));
  785. break;
  786. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  787. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_MEDIA_CTRL_ACK_EVT));
  788. break;
  789. case BT_APP_HEART_BEAT_EVT: {
  790. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(BT_APP_HEART_BEAT_EVT));
  791. switch (esp_bluedroid_get_status()) {
  792. case ESP_BLUEDROID_STATUS_UNINITIALIZED:
  793. ESP_LOGV(TAG,"BlueDroid Status is ESP_BLUEDROID_STATUS_UNINITIALIZED.");
  794. break;
  795. case ESP_BLUEDROID_STATUS_INITIALIZED:
  796. ESP_LOGV(TAG,"BlueDroid Status is ESP_BLUEDROID_STATUS_INITIALIZED.");
  797. break;
  798. case ESP_BLUEDROID_STATUS_ENABLED:
  799. ESP_LOGV(TAG,"BlueDroid Status is ESP_BLUEDROID_STATUS_ENABLED.");
  800. break;
  801. default:
  802. break;
  803. }
  804. uint8_t *p = s_peer_bda;
  805. ESP_LOGI(TAG, "a2dp connecting to %s, BT peer: %02x:%02x:%02x:%02x:%02x:%02x",s_peer_bdname,p[0], p[1], p[2], p[3], p[4], p[5]);
  806. if(esp_a2d_source_connect(s_peer_bda)==ESP_OK) {
  807. set_app_source_state(APP_AV_STATE_CONNECTING);
  808. s_connecting_intv = 0;
  809. }
  810. else {
  811. set_app_source_state(APP_AV_STATE_UNCONNECTED);
  812. // there was an issue connecting... continue to discover
  813. ESP_LOGE(TAG,"Attempt at connecting failed, restart at discover...");
  814. esp_bt_gap_start_discovery(ESP_BT_INQ_MODE_GENERAL_INQUIRY, 10, 0);
  815. }
  816. break;
  817. }
  818. default:
  819. ESP_LOGE(TAG,"%s unhandled evt %d", __func__, event);
  820. break;
  821. }
  822. }
  823. static void bt_app_av_state_connecting(uint16_t event, void *param)
  824. {
  825. switch (event) {
  826. case ESP_A2D_CONNECTION_STATE_EVT:
  827. handle_connect_state_connecting(event, (esp_a2d_cb_param_t *)param);
  828. break;
  829. case ESP_A2D_AUDIO_STATE_EVT:
  830. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_STATE_EVT));
  831. break;
  832. case ESP_A2D_AUDIO_CFG_EVT:
  833. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_CFG_EVT));
  834. break;
  835. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  836. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_MEDIA_CTRL_ACK_EVT));
  837. break;
  838. case BT_APP_HEART_BEAT_EVT:
  839. if (++s_connecting_intv >= 2) {
  840. set_app_source_state(APP_AV_STATE_UNCONNECTED);
  841. ESP_LOGW(TAG,"A2DP Connect time out! Setting state to Unconnected. ");
  842. s_connecting_intv = 0;
  843. }
  844. break;
  845. default:
  846. ESP_LOGE(TAG,"%s unhandled evt %d", __func__, event);
  847. break;
  848. }
  849. }
  850. static void bt_app_av_state_connected(uint16_t event, void *param)
  851. {
  852. esp_a2d_cb_param_t *a2d = NULL;
  853. switch (event) {
  854. case ESP_A2D_CONNECTION_STATE_EVT: {
  855. handle_connect_state_connected(event, (esp_a2d_cb_param_t *)param);
  856. break;
  857. }
  858. case ESP_A2D_AUDIO_STATE_EVT: {
  859. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_STATE_EVT));
  860. a2d = (esp_a2d_cb_param_t *)(param);
  861. if (ESP_A2D_AUDIO_STATE_STARTED == a2d->audio_stat.state) {
  862. s_pkt_cnt = 0;
  863. }
  864. break;
  865. }
  866. case ESP_A2D_AUDIO_CFG_EVT:
  867. // not suppposed to occur for A2DP source
  868. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_CFG_EVT));
  869. break;
  870. case ESP_A2D_MEDIA_CTRL_ACK_EVT:{
  871. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_MEDIA_CTRL_ACK_EVT));
  872. bt_app_av_media_proc(event, param);
  873. break;
  874. }
  875. case BT_APP_HEART_BEAT_EVT: {
  876. ESP_LOGV(TAG,QUOTE(BT_APP_HEART_BEAT_EVT));
  877. bt_app_av_media_proc(event, param);
  878. break;
  879. }
  880. default:
  881. ESP_LOGE(TAG,"%s unhandled evt %d", __func__, event);
  882. break;
  883. }
  884. }
  885. static void bt_app_av_state_disconnecting(uint16_t event, void *param)
  886. {
  887. switch (event) {
  888. case ESP_A2D_CONNECTION_STATE_EVT:
  889. handle_connect_state_disconnecting( event, (esp_a2d_cb_param_t *)param);
  890. break;
  891. case ESP_A2D_AUDIO_STATE_EVT:
  892. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_STATE_EVT));
  893. break;
  894. case ESP_A2D_AUDIO_CFG_EVT:
  895. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_AUDIO_CFG_EVT));
  896. break;
  897. case ESP_A2D_MEDIA_CTRL_ACK_EVT:
  898. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(ESP_A2D_MEDIA_CTRL_ACK_EVT));
  899. break;
  900. case BT_APP_HEART_BEAT_EVT:
  901. ESP_LOG_DEBUG_EVENT(TAG,QUOTE(BT_APP_HEART_BEAT_EVT));
  902. break;
  903. default:
  904. ESP_LOGE(TAG,"%s unhandled evt %d", __func__, event);
  905. break;
  906. }
  907. }
  908. static void bt_av_volume_changed(void)
  909. {
  910. if (esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_TEST, &s_avrc_peer_rn_cap,
  911. ESP_AVRC_RN_VOLUME_CHANGE)) {
  912. esp_avrc_ct_send_register_notification_cmd(APP_RC_CT_TL_RN_VOLUME_CHANGE, ESP_AVRC_RN_VOLUME_CHANGE, 0);
  913. }
  914. }
  915. static void bt_av_notify_evt_handler(uint8_t event_id, esp_avrc_rn_param_t *event_parameter)
  916. {
  917. switch (event_id) {
  918. case ESP_AVRC_RN_VOLUME_CHANGE:
  919. ESP_LOGI(BT_RC_CT_TAG, "Volume changed: %d", event_parameter->volume);
  920. ESP_LOGI(BT_RC_CT_TAG, "Set absolute volume: volume %d", event_parameter->volume + 5);
  921. esp_avrc_ct_send_set_absolute_volume_cmd(APP_RC_CT_TL_RN_VOLUME_CHANGE, event_parameter->volume + 5);
  922. bt_av_volume_changed();
  923. break;
  924. }
  925. }
  926. static void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param)
  927. {
  928. ESP_LOGD(BT_RC_CT_TAG, "%s evt %d", __func__, event);
  929. esp_avrc_ct_cb_param_t *rc = (esp_avrc_ct_cb_param_t *)(p_param);
  930. switch (event) {
  931. case ESP_AVRC_CT_CONNECTION_STATE_EVT: {
  932. uint8_t *bda = rc->conn_stat.remote_bda;
  933. ESP_LOGI(BT_RC_CT_TAG, "AVRC conn_state evt: state %d, [%02x:%02x:%02x:%02x:%02x:%02x]",
  934. rc->conn_stat.connected, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
  935. if (rc->conn_stat.connected) {
  936. // get remote supported event_ids of peer AVRCP Target
  937. esp_avrc_ct_send_get_rn_capabilities_cmd(APP_RC_CT_TL_GET_CAPS);
  938. } else {
  939. // clear peer notification capability record
  940. s_avrc_peer_rn_cap.bits = 0;
  941. }
  942. break;
  943. }
  944. case ESP_AVRC_CT_PASSTHROUGH_RSP_EVT: {
  945. ESP_LOGI(BT_RC_CT_TAG, "AVRC passthrough rsp: key_code 0x%x, key_state %d", rc->psth_rsp.key_code, rc->psth_rsp.key_state);
  946. break;
  947. }
  948. case ESP_AVRC_CT_METADATA_RSP_EVT: {
  949. ESP_LOGI(BT_RC_CT_TAG, "AVRC metadata rsp: attribute id 0x%x, %s", rc->meta_rsp.attr_id, rc->meta_rsp.attr_text);
  950. free(rc->meta_rsp.attr_text);
  951. break;
  952. }
  953. case ESP_AVRC_CT_CHANGE_NOTIFY_EVT: {
  954. ESP_LOGI(BT_RC_CT_TAG, "AVRC event notification: %d", rc->change_ntf.event_id);
  955. bt_av_notify_evt_handler(rc->change_ntf.event_id, &rc->change_ntf.event_parameter);
  956. break;
  957. }
  958. case ESP_AVRC_CT_REMOTE_FEATURES_EVT: {
  959. ESP_LOGI(BT_RC_CT_TAG, "AVRC remote features %x, TG features %x", rc->rmt_feats.feat_mask, rc->rmt_feats.tg_feat_flag);
  960. break;
  961. }
  962. case ESP_AVRC_CT_GET_RN_CAPABILITIES_RSP_EVT: {
  963. ESP_LOGI(BT_RC_CT_TAG, "remote rn_cap: count %d, bitmask 0x%x", rc->get_rn_caps_rsp.cap_count,
  964. rc->get_rn_caps_rsp.evt_set.bits);
  965. s_avrc_peer_rn_cap.bits = rc->get_rn_caps_rsp.evt_set.bits;
  966. bt_av_volume_changed();
  967. break;
  968. }
  969. case ESP_AVRC_CT_SET_ABSOLUTE_VOLUME_RSP_EVT: {
  970. ESP_LOGI(BT_RC_CT_TAG, "Set absolute volume rsp: volume %d", rc->set_volume_rsp.volume);
  971. break;
  972. }
  973. default:
  974. ESP_LOGE(BT_RC_CT_TAG, "%s unhandled evt %d", __func__, event);
  975. break;
  976. }
  977. }