bt_app_source - Copy.c.old 38 KB

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