bt_app_sink.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "esp_log.h"
  12. #include "bt_app_core.h"
  13. #include "bt_app_sink.h"
  14. #include "esp_bt.h"
  15. #include "esp_bt_main.h"
  16. #include "esp_bt_device.h"
  17. #include "esp_gap_bt_api.h"
  18. #include "esp_a2dp_api.h"
  19. #include "esp_avrc_api.h"
  20. #include "nvs.h"
  21. #include "platform_config.h"
  22. #include "freertos/FreeRTOS.h"
  23. #include "freertos/task.h"
  24. #include "tools.h"
  25. #include "audio_controls.h"
  26. #include "sys/lock.h"
  27. #include "display.h"
  28. // AVRCP used transaction label
  29. #define APP_RC_CT_TL_GET_CAPS (0)
  30. #define APP_RC_CT_TL_GET_META_DATA (1)
  31. #define APP_RC_CT_TL_RN_TRACK_CHANGE (2)
  32. #define APP_RC_CT_TL_RN_PLAYBACK_CHANGE (3)
  33. #define APP_RC_CT_TL_RN_PLAY_POS_CHANGE (4)
  34. static const char BT_AV_TAG[] = "BT_AV";
  35. static const char BT_RC_TG_TAG[] = "RCTG";
  36. static const char BT_RC_CT_TAG[] = "RCCT";
  37. #ifndef CONFIG_BT_NAME
  38. #define CONFIG_BT_NAME "ESP32-BT"
  39. #endif
  40. static char * bt_name = NULL;
  41. static bool (*bt_app_a2d_cmd_cb)(bt_sink_cmd_t cmd, ...);
  42. static void (*bt_app_a2d_data_cb)(const uint8_t *data, uint32_t len);
  43. /* handler for bluetooth stack enabled events */
  44. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
  45. /* a2dp event handler */
  46. static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param);
  47. /* avrc CT event handler */
  48. static void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param);
  49. /* avrc TG event handler */
  50. static void bt_av_hdl_avrc_tg_evt(uint16_t event, void *p_param);
  51. static void volume_set_by_local_host(int value, bool is_step);
  52. static void bt_av_notify_evt_handler(uint8_t event_id, esp_avrc_rn_param_t *event_parameter);
  53. static const char *s_a2d_conn_state_str[] = {"Disconnected", "Connecting", "Connected", "Disconnecting"};
  54. static const char *s_a2d_audio_state_str[] = {"Suspended", "Stopped", "Started"};
  55. static esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap;
  56. static _lock_t s_volume_lock;
  57. static int s_volume, abs_volume, sink_volume;
  58. static bool s_volume_notify;
  59. static enum { AUDIO_IDLE, AUDIO_CONNECTED, AUDIO_PLAYING } s_audio = AUDIO_IDLE;
  60. static int s_sample_rate;
  61. static int tl;
  62. static bt_cmd_vcb_t cmd_handler_chain;
  63. #define METADATA_LEN 128
  64. static EXT_RAM_ATTR struct {
  65. char artist[METADATA_LEN + 1];
  66. char album[METADATA_LEN + 1];
  67. char title[METADATA_LEN + 1];
  68. int duration;
  69. bool updated;
  70. } s_metadata;
  71. static void bt_volume_up(bool pressed) {
  72. if (!pressed) return;
  73. volume_set_by_local_host(+3, true);
  74. (*bt_app_a2d_cmd_cb)(BT_SINK_VOLUME, s_volume);
  75. ESP_LOGD(BT_AV_TAG, "BT volume up %u", s_volume);
  76. }
  77. static void bt_volume_down(bool pressed) {
  78. if (!pressed) return;
  79. volume_set_by_local_host(-3, true);
  80. (*bt_app_a2d_cmd_cb)(BT_SINK_VOLUME, s_volume);
  81. }
  82. static void bt_toggle(bool pressed) {
  83. if (!pressed) return;
  84. if (s_audio == AUDIO_PLAYING) esp_avrc_ct_send_passthrough_cmd(tl++ & 0x0f, ESP_AVRC_PT_CMD_STOP, ESP_AVRC_PT_CMD_STATE_PRESSED);
  85. else esp_avrc_ct_send_passthrough_cmd(tl++, ESP_AVRC_PT_CMD_PLAY, ESP_AVRC_PT_CMD_STATE_PRESSED);
  86. }
  87. static void bt_play(bool pressed) {
  88. if (!pressed) return;
  89. esp_avrc_ct_send_passthrough_cmd(tl++ & 0x0f, ESP_AVRC_PT_CMD_PLAY, ESP_AVRC_PT_CMD_STATE_PRESSED);
  90. }
  91. static void bt_pause(bool pressed) {
  92. if (!pressed) return;
  93. esp_avrc_ct_send_passthrough_cmd(tl++ & 0x0f, ESP_AVRC_PT_CMD_PAUSE, ESP_AVRC_PT_CMD_STATE_PRESSED);
  94. }
  95. static void bt_stop(bool pressed) {
  96. if (!pressed) return;
  97. esp_avrc_ct_send_passthrough_cmd(tl++ & 0x0f, ESP_AVRC_PT_CMD_STOP, ESP_AVRC_PT_CMD_STATE_PRESSED);
  98. }
  99. static void bt_prev(bool pressed) {
  100. if (!pressed) return;
  101. esp_avrc_ct_send_passthrough_cmd(tl++ & 0x0f, ESP_AVRC_PT_CMD_BACKWARD, ESP_AVRC_PT_CMD_STATE_PRESSED);
  102. }
  103. static void bt_next(bool pressed) {
  104. if (!pressed) return;
  105. esp_avrc_ct_send_passthrough_cmd(tl++ & 0x0f, ESP_AVRC_PT_CMD_FORWARD, ESP_AVRC_PT_CMD_STATE_PRESSED);
  106. }
  107. const static actrls_t controls = {
  108. NULL, // power
  109. bt_volume_up, bt_volume_down, // volume up, volume down
  110. bt_toggle, bt_play, // toggle, play
  111. bt_pause, bt_stop, // pause, stop
  112. NULL, NULL, // rew, fwd
  113. bt_prev, bt_next, // prev, next
  114. NULL, NULL, NULL, NULL, // left, right, up, down
  115. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, // pre1-10
  116. bt_volume_down, bt_volume_up, bt_toggle// knob left, knob_right, knob push
  117. };
  118. /* disconnection */
  119. void bt_disconnect(void) {
  120. displayer_control(DISPLAYER_SHUTDOWN);
  121. if (s_audio == AUDIO_PLAYING) esp_avrc_ct_send_passthrough_cmd(tl++ & 0x0f, ESP_AVRC_PT_CMD_STOP, ESP_AVRC_PT_CMD_STATE_PRESSED);
  122. actrls_unset();
  123. ESP_LOGD(BT_AV_TAG, "forced disconnection %d", s_audio);
  124. }
  125. /* update metadata if any */
  126. void update_metadata(bool force) {
  127. if ((s_metadata.updated || force) && s_audio == AUDIO_PLAYING) {
  128. (*bt_app_a2d_cmd_cb)(BT_SINK_PROGRESS, -1, s_metadata.duration);
  129. (*bt_app_a2d_cmd_cb)(BT_SINK_METADATA, s_metadata.artist, s_metadata.album, s_metadata.title);
  130. s_metadata.updated = false;
  131. } else s_metadata.updated = force;
  132. }
  133. /* command handler */
  134. static bool cmd_handler(bt_sink_cmd_t cmd, ...) {
  135. va_list args;
  136. va_start(args, cmd);
  137. // handle audio event and stop if forbidden
  138. if (!cmd_handler_chain(cmd, args)) {
  139. va_end(args);
  140. return false;
  141. }
  142. // now handle events for display
  143. switch(cmd) {
  144. case BT_SINK_AUDIO_STARTED:
  145. displayer_control(DISPLAYER_ACTIVATE, "BLUETOOTH", false);
  146. break;
  147. case BT_SINK_AUDIO_STOPPED:
  148. displayer_control(DISPLAYER_SUSPEND);
  149. break;
  150. case BT_SINK_PLAY:
  151. displayer_control(DISPLAYER_TIMER_RUN);
  152. break;
  153. case BT_SINK_STOP:
  154. // not sure of difference between pause and stop for displayer
  155. case BT_SINK_PAUSE:
  156. displayer_control(DISPLAYER_TIMER_PAUSE);
  157. break;
  158. case BT_SINK_METADATA: {
  159. char *artist = va_arg(args, char*), *album = va_arg(args, char*), *title = va_arg(args, char*);
  160. displayer_metadata(artist, album, title);
  161. break;
  162. }
  163. case BT_SINK_PROGRESS: {
  164. int elapsed = va_arg(args, int), duration = va_arg(args, int);
  165. displayer_timer(DISPLAYER_ELAPSED, elapsed, duration);
  166. break;
  167. }
  168. default:
  169. break;
  170. }
  171. va_end(args);
  172. return true;
  173. }
  174. /* callback for A2DP sink */
  175. void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
  176. {
  177. switch (event) {
  178. case ESP_A2D_CONNECTION_STATE_EVT:
  179. case ESP_A2D_AUDIO_STATE_EVT:
  180. case ESP_A2D_AUDIO_CFG_EVT: {
  181. bt_app_work_dispatch(bt_av_hdl_a2d_evt, event, param, sizeof(esp_a2d_cb_param_t), NULL);
  182. break;
  183. }
  184. case ESP_A2D_PROF_STATE_EVT: {
  185. ESP_LOGI(BT_AV_TAG, "Bluetooth Init complete");
  186. break;
  187. }
  188. default:
  189. ESP_LOGE(BT_AV_TAG, "Invalid A2DP event: %d", event);
  190. break;
  191. }
  192. }
  193. void bt_app_alloc_meta_buffer(esp_avrc_ct_cb_param_t *param)
  194. {
  195. esp_avrc_ct_cb_param_t *rc = (esp_avrc_ct_cb_param_t *)(param);
  196. uint8_t *attr_text = (uint8_t *) malloc (rc->meta_rsp.attr_length + 1);
  197. memcpy(attr_text, rc->meta_rsp.attr_text, rc->meta_rsp.attr_length);
  198. attr_text[rc->meta_rsp.attr_length] = 0;
  199. rc->meta_rsp.attr_text = attr_text;
  200. }
  201. void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param)
  202. {
  203. switch (event) {
  204. case ESP_AVRC_CT_METADATA_RSP_EVT:
  205. bt_app_alloc_meta_buffer(param);
  206. /* fall through */
  207. case ESP_AVRC_CT_CONNECTION_STATE_EVT:
  208. case ESP_AVRC_CT_PASSTHROUGH_RSP_EVT:
  209. case ESP_AVRC_CT_CHANGE_NOTIFY_EVT:
  210. case ESP_AVRC_CT_REMOTE_FEATURES_EVT:
  211. case ESP_AVRC_CT_GET_RN_CAPABILITIES_RSP_EVT: {
  212. bt_app_work_dispatch(bt_av_hdl_avrc_ct_evt, event, param, sizeof(esp_avrc_ct_cb_param_t), NULL);
  213. break;
  214. }
  215. default:
  216. ESP_LOGE(BT_RC_CT_TAG, "Invalid AVRC event: %d", event);
  217. break;
  218. }
  219. }
  220. void bt_app_rc_tg_cb(esp_avrc_tg_cb_event_t event, esp_avrc_tg_cb_param_t *param)
  221. {
  222. switch (event) {
  223. case ESP_AVRC_TG_CONNECTION_STATE_EVT:
  224. case ESP_AVRC_TG_REMOTE_FEATURES_EVT:
  225. case ESP_AVRC_TG_PASSTHROUGH_CMD_EVT:
  226. case ESP_AVRC_TG_SET_ABSOLUTE_VOLUME_CMD_EVT:
  227. case ESP_AVRC_TG_REGISTER_NOTIFICATION_EVT:
  228. bt_app_work_dispatch(bt_av_hdl_avrc_tg_evt, event, param, sizeof(esp_avrc_tg_cb_param_t), NULL);
  229. break;
  230. default:
  231. ESP_LOGE(BT_RC_TG_TAG, "Invalid AVRC event: %d", event);
  232. break;
  233. }
  234. }
  235. static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param)
  236. {
  237. ESP_LOGD(BT_AV_TAG, "%s evt %d", __func__, event);
  238. esp_a2d_cb_param_t *a2d = NULL;
  239. switch (event) {
  240. case ESP_A2D_CONNECTION_STATE_EVT: {
  241. a2d = (esp_a2d_cb_param_t *)(p_param);
  242. uint8_t *bda = a2d->conn_stat.remote_bda;
  243. ESP_LOGD(BT_AV_TAG, "A2DP connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]",
  244. s_a2d_conn_state_str[a2d->conn_stat.state], bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
  245. if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
  246. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  247. (*bt_app_a2d_cmd_cb)(BT_SINK_DISCONNECTED);
  248. } else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED){
  249. abs_volume = -1;
  250. s_volume = sink_volume;
  251. esp_bt_gap_set_scan_mode(ESP_BT_NON_CONNECTABLE, ESP_BT_NON_DISCOVERABLE);
  252. (*bt_app_a2d_cmd_cb)(BT_SINK_CONNECTED);
  253. }
  254. break;
  255. }
  256. case ESP_A2D_AUDIO_STATE_EVT: {
  257. a2d = (esp_a2d_cb_param_t *)(p_param);
  258. ESP_LOGD(BT_AV_TAG, "A2DP audio state: %s", s_a2d_audio_state_str[a2d->audio_stat.state]);
  259. if (ESP_A2D_AUDIO_STATE_STARTED == a2d->audio_stat.state) {
  260. s_audio = AUDIO_CONNECTED;
  261. // send memorized volume for devices that can't do absolute volume
  262. (*bt_app_a2d_cmd_cb)(BT_SINK_VOLUME, s_volume);
  263. // verify that we can take control
  264. if ((*bt_app_a2d_cmd_cb)(BT_SINK_AUDIO_STARTED, s_sample_rate)) {
  265. // if PLAY is sent before AUDIO_STARTED, generate the event here
  266. s_audio = AUDIO_PLAYING;
  267. (*bt_app_a2d_cmd_cb)(BT_SINK_PLAY);
  268. // force metadata update
  269. update_metadata(true);
  270. actrls_set(controls, false, NULL, actrls_ir_action);
  271. } else {
  272. // if decoder is busy, stop it (would be better to not ACK this command, but don't know how)
  273. esp_avrc_ct_send_passthrough_cmd(tl++ & 0x0f, ESP_AVRC_PT_CMD_STOP, ESP_AVRC_PT_CMD_STATE_PRESSED);
  274. }
  275. } else if (ESP_A2D_AUDIO_STATE_STOPPED == a2d->audio_stat.state ||
  276. ESP_A2D_AUDIO_STATE_REMOTE_SUSPEND == a2d->audio_stat.state) {
  277. (*bt_app_a2d_cmd_cb)(BT_SINK_AUDIO_STOPPED);
  278. s_audio = AUDIO_IDLE;
  279. actrls_unset();
  280. }
  281. break;
  282. }
  283. case ESP_A2D_AUDIO_CFG_EVT: {
  284. a2d = (esp_a2d_cb_param_t *)(p_param);
  285. ESP_LOGD(BT_AV_TAG, "A2DP audio stream configuration, codec type %d", a2d->audio_cfg.mcc.type);
  286. // for now only SBC stream is supported
  287. if (a2d->audio_cfg.mcc.type == ESP_A2D_MCT_SBC) {
  288. s_sample_rate = 16000;
  289. char oct0 = a2d->audio_cfg.mcc.cie.sbc[0];
  290. if (oct0 & (0x01 << 6)) {
  291. s_sample_rate = 32000;
  292. } else if (oct0 & (0x01 << 5)) {
  293. s_sample_rate = 44100;
  294. } else if (oct0 & (0x01 << 4)) {
  295. s_sample_rate = 48000;
  296. }
  297. (*bt_app_a2d_cmd_cb)(BT_SINK_RATE, s_sample_rate);
  298. ESP_LOGI(BT_AV_TAG, "Configure audio player %x-%x-%x-%x",
  299. a2d->audio_cfg.mcc.cie.sbc[0],
  300. a2d->audio_cfg.mcc.cie.sbc[1],
  301. a2d->audio_cfg.mcc.cie.sbc[2],
  302. a2d->audio_cfg.mcc.cie.sbc[3]);
  303. ESP_LOGI(BT_AV_TAG, "Audio player configured, sample rate=%d", s_sample_rate);
  304. }
  305. break;
  306. }
  307. default:
  308. ESP_LOGE(BT_AV_TAG, "%s unhandled evt %d", __func__, event);
  309. break;
  310. }
  311. }
  312. static void bt_av_new_track(void)
  313. {
  314. // request metadata
  315. uint8_t attr_mask = ESP_AVRC_MD_ATTR_TITLE | ESP_AVRC_MD_ATTR_ARTIST | ESP_AVRC_MD_ATTR_ALBUM | ESP_AVRC_MD_ATTR_PLAYING_TIME;
  316. esp_avrc_ct_send_metadata_cmd(APP_RC_CT_TL_GET_META_DATA, attr_mask);
  317. // register notification if peer support the event_id
  318. if (esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_TEST, &s_avrc_peer_rn_cap,
  319. ESP_AVRC_RN_TRACK_CHANGE)) {
  320. esp_avrc_ct_send_register_notification_cmd(APP_RC_CT_TL_RN_TRACK_CHANGE, ESP_AVRC_RN_TRACK_CHANGE, 0);
  321. }
  322. }
  323. static void bt_av_playback_changed(void)
  324. {
  325. if (esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_TEST, &s_avrc_peer_rn_cap,
  326. ESP_AVRC_RN_PLAY_STATUS_CHANGE)) {
  327. esp_avrc_ct_send_register_notification_cmd(APP_RC_CT_TL_RN_PLAYBACK_CHANGE, ESP_AVRC_RN_PLAY_STATUS_CHANGE, 0);
  328. }
  329. }
  330. static void bt_av_play_pos_changed(void)
  331. {
  332. if (esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_TEST, &s_avrc_peer_rn_cap,
  333. ESP_AVRC_RN_PLAY_POS_CHANGED)) {
  334. esp_avrc_ct_send_register_notification_cmd(APP_RC_CT_TL_RN_PLAY_POS_CHANGE, ESP_AVRC_RN_PLAY_POS_CHANGED, 10);
  335. }
  336. }
  337. static void bt_av_notify_evt_handler(uint8_t event_id, esp_avrc_rn_param_t *event_parameter)
  338. {
  339. switch (event_id) {
  340. case ESP_AVRC_RN_TRACK_CHANGE:
  341. ESP_LOGD(BT_AV_TAG, "Track changed");
  342. bt_av_new_track();
  343. (*bt_app_a2d_cmd_cb)(BT_SINK_PROGRESS, 0, 0);
  344. break;
  345. case ESP_AVRC_RN_PLAY_STATUS_CHANGE:
  346. ESP_LOGD(BT_AV_TAG, "Playback status changed: 0x%x", event_parameter->playback);
  347. if (s_audio != AUDIO_IDLE) {
  348. switch (event_parameter->playback) {
  349. case ESP_AVRC_PLAYBACK_PLAYING:
  350. // if decoder is busy then stop (would be better to not ACK this command, but don't know how)
  351. if (s_audio != AUDIO_PLAYING && !(*bt_app_a2d_cmd_cb)(BT_SINK_PLAY)) {
  352. ESP_LOGW(BT_AV_TAG, "Player busy with another controller");
  353. esp_avrc_ct_send_passthrough_cmd(tl++ & 0x0f, ESP_AVRC_PT_CMD_STOP, ESP_AVRC_PT_CMD_STATE_PRESSED);
  354. } else {
  355. s_audio = AUDIO_PLAYING;
  356. update_metadata(false);
  357. }
  358. break;
  359. case ESP_AVRC_PLAYBACK_PAUSED:
  360. s_audio = AUDIO_CONNECTED;
  361. (*bt_app_a2d_cmd_cb)(BT_SINK_PAUSE);
  362. break;
  363. case ESP_AVRC_PLAYBACK_STOPPED:
  364. s_audio = AUDIO_CONNECTED;
  365. (*bt_app_a2d_cmd_cb)(BT_SINK_PROGRESS, 0, -1);
  366. (*bt_app_a2d_cmd_cb)(BT_SINK_STOP);
  367. break;
  368. default:
  369. ESP_LOGW(BT_AV_TAG, "Un-handled event");
  370. break;
  371. }
  372. } else {
  373. ESP_LOGW(BT_AV_TAG, "Not yet in BT connected mode: 0x%x", event_parameter->playback);
  374. }
  375. bt_av_playback_changed();
  376. break;
  377. case ESP_AVRC_RN_PLAY_POS_CHANGED:
  378. ESP_LOGD(BT_AV_TAG, "Play position changed: %d (ms)", event_parameter->play_pos);
  379. (*bt_app_a2d_cmd_cb)(BT_SINK_PROGRESS, event_parameter->play_pos, -1);
  380. bt_av_play_pos_changed();
  381. break;
  382. }
  383. }
  384. static void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param)
  385. {
  386. ESP_LOGD(BT_RC_CT_TAG, "%s evt %d", __func__, event);
  387. esp_avrc_ct_cb_param_t *rc = (esp_avrc_ct_cb_param_t *)(p_param);
  388. switch (event) {
  389. case ESP_AVRC_CT_CONNECTION_STATE_EVT: {
  390. uint8_t *bda = rc->conn_stat.remote_bda;
  391. ESP_LOGD(BT_RC_CT_TAG, "AVRC conn_state evt: state %d, [%02x:%02x:%02x:%02x:%02x:%02x]",
  392. rc->conn_stat.connected, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
  393. if (rc->conn_stat.connected) {
  394. // get remote supported event_ids of peer AVRCP Target
  395. esp_avrc_ct_send_get_rn_capabilities_cmd(APP_RC_CT_TL_GET_CAPS);
  396. } else {
  397. // clear peer notification capability record
  398. s_avrc_peer_rn_cap.bits = 0;
  399. }
  400. break;
  401. }
  402. case ESP_AVRC_CT_PASSTHROUGH_RSP_EVT: {
  403. ESP_LOGD(BT_RC_CT_TAG, "AVRC passthrough rsp: key_code 0x%x, key_state %d", rc->psth_rsp.key_code, rc->psth_rsp.key_state);
  404. break;
  405. }
  406. case ESP_AVRC_CT_METADATA_RSP_EVT: {
  407. ESP_LOGD(BT_RC_CT_TAG, "AVRC metadata rsp: attribute id 0x%x, %s", rc->meta_rsp.attr_id, rc->meta_rsp.attr_text);
  408. if (rc->meta_rsp.attr_id == ESP_AVRC_MD_ATTR_PLAYING_TIME) s_metadata.duration = atoi((char*) rc->meta_rsp.attr_text);
  409. else if (rc->meta_rsp.attr_id == ESP_AVRC_MD_ATTR_TITLE) strncpy(s_metadata.title, (char*) rc->meta_rsp.attr_text, METADATA_LEN);
  410. else if (rc->meta_rsp.attr_id == ESP_AVRC_MD_ATTR_ARTIST) strncpy(s_metadata.artist, (char*) rc->meta_rsp.attr_text, METADATA_LEN);
  411. else if (rc->meta_rsp.attr_id == ESP_AVRC_MD_ATTR_ALBUM) strncpy(s_metadata.album, (char*) rc->meta_rsp.attr_text, METADATA_LEN);
  412. update_metadata(true);
  413. free(rc->meta_rsp.attr_text);
  414. break;
  415. }
  416. case ESP_AVRC_CT_CHANGE_NOTIFY_EVT: {
  417. ESP_LOGD(BT_RC_CT_TAG, "AVRC event notification: %d", rc->change_ntf.event_id);
  418. bt_av_notify_evt_handler(rc->change_ntf.event_id, &rc->change_ntf.event_parameter);
  419. break;
  420. }
  421. case ESP_AVRC_CT_REMOTE_FEATURES_EVT: {
  422. ESP_LOGD(BT_RC_CT_TAG, "AVRC remote features %x, TG features %x", rc->rmt_feats.feat_mask, rc->rmt_feats.tg_feat_flag);
  423. break;
  424. }
  425. case ESP_AVRC_CT_GET_RN_CAPABILITIES_RSP_EVT: {
  426. ESP_LOGD(BT_RC_CT_TAG, "remote rn_cap: count %d, bitmask 0x%x", rc->get_rn_caps_rsp.cap_count,
  427. rc->get_rn_caps_rsp.evt_set.bits);
  428. s_avrc_peer_rn_cap.bits = rc->get_rn_caps_rsp.evt_set.bits;
  429. bt_av_new_track();
  430. bt_av_playback_changed();
  431. bt_av_play_pos_changed();
  432. break;
  433. }
  434. default:
  435. ESP_LOGE(BT_RC_CT_TAG, "%s unhandled evt %d", __func__, event);
  436. break;
  437. }
  438. }
  439. static void volume_set_by_controller(uint8_t volume)
  440. {
  441. // do not modified NVS volume
  442. _lock_acquire(&s_volume_lock);
  443. s_volume = abs_volume = (volume * 100) / 127;
  444. _lock_release(&s_volume_lock);
  445. (*bt_app_a2d_cmd_cb)(BT_SINK_VOLUME, s_volume);
  446. }
  447. static void volume_set_by_local_host(int value, bool is_step)
  448. {
  449. _lock_acquire(&s_volume_lock);
  450. s_volume = is_step ? s_volume + value : value;
  451. if (s_volume > 127) s_volume = 127;
  452. else if (s_volume < 0) s_volume = 0;
  453. if (abs_volume >= 0) abs_volume = s_volume;
  454. else sink_volume = s_volume;
  455. _lock_release(&s_volume_lock);
  456. // volume has been set by controller, do not store it in NVS
  457. if (abs_volume < 0) {
  458. char p[4];
  459. config_set_value(NVS_TYPE_STR, "bt_sink_volume", itoa(s_volume, p, 10));
  460. }
  461. if (s_volume_notify) {
  462. esp_avrc_rn_param_t rn_param;
  463. rn_param.volume = s_volume;
  464. esp_avrc_tg_send_rn_rsp(ESP_AVRC_RN_VOLUME_CHANGE, ESP_AVRC_RN_RSP_CHANGED, &rn_param);
  465. s_volume_notify = false;
  466. }
  467. }
  468. static void bt_av_hdl_avrc_tg_evt(uint16_t event, void *p_param)
  469. {
  470. ESP_LOGD(BT_RC_TG_TAG, "%s evt %d", __func__, event);
  471. esp_avrc_tg_cb_param_t *rc = (esp_avrc_tg_cb_param_t *)(p_param);
  472. switch (event) {
  473. case ESP_AVRC_TG_CONNECTION_STATE_EVT: {
  474. uint8_t *bda = rc->conn_stat.remote_bda;
  475. ESP_LOGD(BT_RC_TG_TAG, "AVRC conn_state evt: state %d, [%02x:%02x:%02x:%02x:%02x:%02x]",
  476. rc->conn_stat.connected, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
  477. break;
  478. }
  479. case ESP_AVRC_TG_PASSTHROUGH_CMD_EVT: {
  480. ESP_LOGD(BT_RC_TG_TAG, "AVRC passthrough cmd: key_code 0x%x, key_state %d", rc->psth_cmd.key_code, rc->psth_cmd.key_state);
  481. break;
  482. }
  483. case ESP_AVRC_TG_SET_ABSOLUTE_VOLUME_CMD_EVT: {
  484. ESP_LOGD(BT_RC_TG_TAG, "AVRC set absolute volume: %d%%", (rc->set_abs_vol.volume * 100) / 127);
  485. volume_set_by_controller(rc->set_abs_vol.volume);
  486. break;
  487. }
  488. case ESP_AVRC_TG_REGISTER_NOTIFICATION_EVT: {
  489. ESP_LOGD(BT_RC_TG_TAG, "AVRC register event notification: %d, param: 0x%x", rc->reg_ntf.event_id, rc->reg_ntf.event_parameter);
  490. if (rc->reg_ntf.event_id == ESP_AVRC_RN_VOLUME_CHANGE) {
  491. s_volume_notify = true;
  492. esp_avrc_rn_param_t rn_param;
  493. rn_param.volume = s_volume;
  494. esp_avrc_tg_send_rn_rsp(ESP_AVRC_RN_VOLUME_CHANGE, ESP_AVRC_RN_RSP_INTERIM, &rn_param);
  495. }
  496. break;
  497. }
  498. case ESP_AVRC_TG_REMOTE_FEATURES_EVT: {
  499. ESP_LOGD(BT_RC_TG_TAG, "AVRC remote features %x, CT features %x", rc->rmt_feats.feat_mask, rc->rmt_feats.ct_feat_flag);
  500. break;
  501. }
  502. default:
  503. ESP_LOGE(BT_RC_TG_TAG, "%s unhandled evt %d", __func__, event);
  504. break;
  505. }
  506. }
  507. void bt_sink_init(bt_cmd_vcb_t cmd_cb, bt_data_cb_t data_cb)
  508. {
  509. bt_app_a2d_cmd_cb = cmd_handler;
  510. cmd_handler_chain = cmd_cb;
  511. bt_app_a2d_data_cb = data_cb;
  512. // create task and run event loop
  513. bt_app_task_start_up(bt_av_hdl_stack_evt);
  514. char *item = config_alloc_get_default(NVS_TYPE_STR, "bt_sink_volume", "127", 0);
  515. sink_volume = atol(item);
  516. free(item);
  517. /*
  518. * Set default parameters for Legacy Pairing
  519. */
  520. esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED;
  521. char * pin_code = config_alloc_get_default(NVS_TYPE_STR, "bt_sink_pin", STR(CONFIG_BT_SINK_PIN), 0);
  522. if(strlen(pin_code)>ESP_BT_PIN_CODE_LEN){
  523. ESP_LOGW(BT_AV_TAG, "BT Sink pin code [%s] too long. ", pin_code);
  524. pin_code[ESP_BT_PIN_CODE_LEN] = '\0';
  525. ESP_LOGW(BT_AV_TAG, "BT Sink pin truncated code [%s]. ", pin_code);
  526. }
  527. esp_bt_pin_code_t esp_pin_code;
  528. bool bError=false;
  529. memset(esp_pin_code, 0x00, sizeof(esp_pin_code) );
  530. ESP_LOGW(BT_AV_TAG, "BT Sink pin code is: [%s] ", pin_code);
  531. for(int i=0;i<strlen(pin_code);i++){
  532. if(pin_code[i] < '0' || pin_code[i] > '9' ) {
  533. ESP_LOGE(BT_AV_TAG,"Invalid number found in sequence");
  534. bError=true;
  535. }
  536. esp_pin_code[i]= pin_code[i];
  537. }
  538. if (bError) memcpy(esp_pin_code, "1234", 4);
  539. esp_bt_gap_set_pin(pin_type, strlen(pin_code), esp_pin_code);
  540. free(pin_code);
  541. }
  542. void bt_sink_deinit(void)
  543. {
  544. bt_app_task_shut_down();
  545. }
  546. static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
  547. {
  548. switch (event) {
  549. case ESP_BT_GAP_AUTH_CMPL_EVT: {
  550. if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
  551. ESP_LOGD(BT_AV_TAG, "authentication success: %s", param->auth_cmpl.device_name);
  552. esp_log_buffer_hex(BT_AV_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
  553. } else {
  554. ESP_LOGE(BT_AV_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
  555. }
  556. break;
  557. }
  558. #if (CONFIG_BT_SSP_ENABLED == true)
  559. case ESP_BT_GAP_CFM_REQ_EVT:
  560. ESP_LOGD(BT_AV_TAG, "ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
  561. esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
  562. break;
  563. case ESP_BT_GAP_KEY_NOTIF_EVT:
  564. ESP_LOGD(BT_AV_TAG, "ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
  565. break;
  566. case ESP_BT_GAP_KEY_REQ_EVT:
  567. ESP_LOGD(BT_AV_TAG, "ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
  568. break;
  569. #endif
  570. default: {
  571. ESP_LOGD(BT_AV_TAG, "event: %d", event);
  572. break;
  573. }
  574. }
  575. return;
  576. }
  577. static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
  578. {
  579. ESP_LOGD(BT_AV_TAG, "%s evt %d", __func__, event);
  580. switch (event) {
  581. case BT_APP_EVT_STACK_UP: {
  582. /* set up device name */
  583. bt_name = (char * )config_alloc_get_default(NVS_TYPE_STR, "bt_name", CONFIG_BT_NAME, 0);
  584. esp_bt_dev_set_device_name(bt_name);
  585. free(bt_name);
  586. esp_bt_gap_register_callback(bt_app_gap_cb);
  587. /* initialize AVRCP controller */
  588. esp_avrc_ct_init();
  589. esp_avrc_ct_register_callback(bt_app_rc_ct_cb);
  590. /* initialize AVRCP target */
  591. assert (esp_avrc_tg_init() == ESP_OK);
  592. esp_avrc_tg_register_callback(bt_app_rc_tg_cb);
  593. esp_avrc_rn_evt_cap_mask_t evt_set = {0};
  594. esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_SET, &evt_set, ESP_AVRC_RN_VOLUME_CHANGE);
  595. assert(esp_avrc_tg_set_rn_evt_cap(&evt_set) == ESP_OK);
  596. /* initialize A2DP sink */
  597. esp_a2d_register_callback(&bt_app_a2d_cb);
  598. esp_a2d_sink_register_data_callback(bt_app_a2d_data_cb);
  599. esp_a2d_sink_init();
  600. /* set discoverable and connectable mode, wait to be connected */
  601. esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
  602. break;
  603. }
  604. default:
  605. ESP_LOGE(BT_AV_TAG, "%s unhandled evt %d", __func__, event);
  606. break;
  607. }
  608. }