bt_app_source.c 36 KB

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