bt_app_source - Copy.c.old 38 KB

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