2017-04-12 08:42:14 +00:00
|
|
|
|
2018-01-15 11:47:23 +00:00
|
|
|
/*
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
2017-04-12 08:42:14 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
|
|
|
#include "bt_app_core.h"
|
|
|
|
#include "bt_app_av.h"
|
|
|
|
#include "esp_bt_main.h"
|
|
|
|
#include "esp_bt_device.h"
|
|
|
|
#include "esp_gap_bt_api.h"
|
|
|
|
#include "esp_a2dp_api.h"
|
|
|
|
#include "esp_avrc_api.h"
|
|
|
|
|
2017-10-16 07:19:29 +00:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "driver/i2s.h"
|
|
|
|
|
2017-04-12 08:42:14 +00:00
|
|
|
/* a2dp event handler */
|
|
|
|
static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param);
|
|
|
|
/* avrc event handler */
|
|
|
|
static void bt_av_hdl_avrc_evt(uint16_t event, void *p_param);
|
|
|
|
|
2018-08-13 09:39:07 +00:00
|
|
|
static uint32_t s_pkt_cnt = 0;
|
|
|
|
static esp_a2d_audio_state_t s_audio_state = ESP_A2D_AUDIO_STATE_STOPPED;
|
|
|
|
static const char *s_a2d_conn_state_str[] = {"Disconnected", "Connecting", "Connected", "Disconnecting"};
|
|
|
|
static const char *s_a2d_audio_state_str[] = {"Suspended", "Stopped", "Started"};
|
2017-04-12 08:42:14 +00:00
|
|
|
|
|
|
|
/* callback for A2DP sink */
|
|
|
|
void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
|
|
|
|
{
|
|
|
|
switch (event) {
|
|
|
|
case ESP_A2D_CONNECTION_STATE_EVT:
|
|
|
|
case ESP_A2D_AUDIO_STATE_EVT:
|
|
|
|
case ESP_A2D_AUDIO_CFG_EVT: {
|
|
|
|
bt_app_work_dispatch(bt_av_hdl_a2d_evt, event, param, sizeof(esp_a2d_cb_param_t), NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2018-01-22 02:24:22 +00:00
|
|
|
ESP_LOGE(BT_AV_TAG, "Invalid A2DP event: %d", event);
|
2017-04-12 08:42:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len)
|
|
|
|
{
|
2018-04-11 11:37:31 +00:00
|
|
|
size_t bytes_written;
|
|
|
|
i2s_write(0, data, len, &bytes_written, portMAX_DELAY);
|
2018-08-13 09:39:07 +00:00
|
|
|
if (++s_pkt_cnt % 100 == 0) {
|
|
|
|
ESP_LOGI(BT_AV_TAG, "Audio packet count %u", s_pkt_cnt);
|
2017-04-12 08:42:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 02:56:13 +00:00
|
|
|
void bt_app_alloc_meta_buffer(esp_avrc_ct_cb_param_t *param)
|
|
|
|
{
|
|
|
|
esp_avrc_ct_cb_param_t *rc = (esp_avrc_ct_cb_param_t *)(param);
|
|
|
|
uint8_t *attr_text = (uint8_t *) malloc (rc->meta_rsp.attr_length + 1);
|
|
|
|
memcpy(attr_text, rc->meta_rsp.attr_text, rc->meta_rsp.attr_length);
|
|
|
|
attr_text[rc->meta_rsp.attr_length] = 0;
|
|
|
|
|
|
|
|
rc->meta_rsp.attr_text = attr_text;
|
|
|
|
}
|
|
|
|
|
2017-04-12 08:42:14 +00:00
|
|
|
void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param)
|
|
|
|
{
|
|
|
|
switch (event) {
|
2017-10-03 02:56:13 +00:00
|
|
|
case ESP_AVRC_CT_METADATA_RSP_EVT:
|
|
|
|
bt_app_alloc_meta_buffer(param);
|
2018-08-29 04:59:30 +00:00
|
|
|
/* fall through */
|
2017-04-12 08:42:14 +00:00
|
|
|
case ESP_AVRC_CT_CONNECTION_STATE_EVT:
|
2017-10-03 02:56:13 +00:00
|
|
|
case ESP_AVRC_CT_PASSTHROUGH_RSP_EVT:
|
2017-12-27 11:49:11 +00:00
|
|
|
case ESP_AVRC_CT_CHANGE_NOTIFY_EVT:
|
|
|
|
case ESP_AVRC_CT_REMOTE_FEATURES_EVT: {
|
2017-04-12 08:42:14 +00:00
|
|
|
bt_app_work_dispatch(bt_av_hdl_avrc_evt, event, param, sizeof(esp_avrc_ct_cb_param_t), NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2018-01-22 02:24:22 +00:00
|
|
|
ESP_LOGE(BT_AV_TAG, "Invalid AVRC event: %d", event);
|
2017-04-12 08:42:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param)
|
|
|
|
{
|
|
|
|
ESP_LOGD(BT_AV_TAG, "%s evt %d", __func__, event);
|
|
|
|
esp_a2d_cb_param_t *a2d = NULL;
|
|
|
|
switch (event) {
|
|
|
|
case ESP_A2D_CONNECTION_STATE_EVT: {
|
|
|
|
a2d = (esp_a2d_cb_param_t *)(p_param);
|
2018-01-22 02:24:22 +00:00
|
|
|
uint8_t *bda = a2d->conn_stat.remote_bda;
|
|
|
|
ESP_LOGI(BT_AV_TAG, "A2DP connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]",
|
2018-08-13 09:39:07 +00:00
|
|
|
s_a2d_conn_state_str[a2d->conn_stat.state], bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
|
2018-05-15 08:35:48 +00:00
|
|
|
if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
|
|
|
|
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
|
|
|
|
} else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED){
|
|
|
|
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_NONE);
|
|
|
|
}
|
2017-04-12 08:42:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_A2D_AUDIO_STATE_EVT: {
|
|
|
|
a2d = (esp_a2d_cb_param_t *)(p_param);
|
2018-08-13 09:39:07 +00:00
|
|
|
ESP_LOGI(BT_AV_TAG, "A2DP audio state: %s", s_a2d_audio_state_str[a2d->audio_stat.state]);
|
|
|
|
s_audio_state = a2d->audio_stat.state;
|
2017-04-12 08:42:14 +00:00
|
|
|
if (ESP_A2D_AUDIO_STATE_STARTED == a2d->audio_stat.state) {
|
2018-08-13 09:39:07 +00:00
|
|
|
s_pkt_cnt = 0;
|
2017-04-12 08:42:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_A2D_AUDIO_CFG_EVT: {
|
|
|
|
a2d = (esp_a2d_cb_param_t *)(p_param);
|
2018-01-22 02:24:22 +00:00
|
|
|
ESP_LOGI(BT_AV_TAG, "A2DP audio stream configuration, codec type %d", a2d->audio_cfg.mcc.type);
|
2017-04-12 08:42:14 +00:00
|
|
|
// for now only SBC stream is supported
|
|
|
|
if (a2d->audio_cfg.mcc.type == ESP_A2D_MCT_SBC) {
|
2017-10-16 07:19:29 +00:00
|
|
|
int sample_rate = 16000;
|
|
|
|
char oct0 = a2d->audio_cfg.mcc.cie.sbc[0];
|
|
|
|
if (oct0 & (0x01 << 6)) {
|
|
|
|
sample_rate = 32000;
|
|
|
|
} else if (oct0 & (0x01 << 5)) {
|
|
|
|
sample_rate = 44100;
|
|
|
|
} else if (oct0 & (0x01 << 4)) {
|
|
|
|
sample_rate = 48000;
|
|
|
|
}
|
|
|
|
i2s_set_clk(0, sample_rate, 16, 2);
|
2018-01-17 11:23:41 +00:00
|
|
|
|
2018-01-22 02:24:22 +00:00
|
|
|
ESP_LOGI(BT_AV_TAG, "Configure audio player %x-%x-%x-%x",
|
2017-10-16 07:19:29 +00:00
|
|
|
a2d->audio_cfg.mcc.cie.sbc[0],
|
|
|
|
a2d->audio_cfg.mcc.cie.sbc[1],
|
|
|
|
a2d->audio_cfg.mcc.cie.sbc[2],
|
|
|
|
a2d->audio_cfg.mcc.cie.sbc[3]);
|
2018-01-22 02:24:22 +00:00
|
|
|
ESP_LOGI(BT_AV_TAG, "Audio player configured, sample rate=%d", sample_rate);
|
2017-04-12 08:42:14 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ESP_LOGE(BT_AV_TAG, "%s unhandled evt %d", __func__, event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 02:56:13 +00:00
|
|
|
static void bt_av_new_track()
|
|
|
|
{
|
|
|
|
//Register notifications and request metadata
|
|
|
|
esp_avrc_ct_send_metadata_cmd(0, ESP_AVRC_MD_ATTR_TITLE | ESP_AVRC_MD_ATTR_ARTIST | ESP_AVRC_MD_ATTR_ALBUM | ESP_AVRC_MD_ATTR_GENRE);
|
|
|
|
esp_avrc_ct_send_register_notification_cmd(1, ESP_AVRC_RN_TRACK_CHANGE, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bt_av_notify_evt_handler(uint8_t event_id, uint32_t event_parameter)
|
|
|
|
{
|
|
|
|
switch (event_id) {
|
|
|
|
case ESP_AVRC_RN_TRACK_CHANGE:
|
|
|
|
bt_av_new_track();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-12 08:42:14 +00:00
|
|
|
static void bt_av_hdl_avrc_evt(uint16_t event, void *p_param)
|
|
|
|
{
|
|
|
|
ESP_LOGD(BT_AV_TAG, "%s evt %d", __func__, event);
|
|
|
|
esp_avrc_ct_cb_param_t *rc = (esp_avrc_ct_cb_param_t *)(p_param);
|
|
|
|
switch (event) {
|
|
|
|
case ESP_AVRC_CT_CONNECTION_STATE_EVT: {
|
|
|
|
uint8_t *bda = rc->conn_stat.remote_bda;
|
2018-01-22 02:24:22 +00:00
|
|
|
ESP_LOGI(BT_AV_TAG, "AVRC conn_state evt: state %d, [%02x:%02x:%02x:%02x:%02x:%02x]",
|
2017-12-27 11:49:11 +00:00
|
|
|
rc->conn_stat.connected, bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
|
2017-10-03 02:56:13 +00:00
|
|
|
|
|
|
|
if (rc->conn_stat.connected) {
|
|
|
|
bt_av_new_track();
|
|
|
|
}
|
2017-04-12 08:42:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_AVRC_CT_PASSTHROUGH_RSP_EVT: {
|
2018-01-22 02:24:22 +00:00
|
|
|
ESP_LOGI(BT_AV_TAG, "AVRC passthrough rsp: key_code 0x%x, key_state %d", rc->psth_rsp.key_code, rc->psth_rsp.key_state);
|
2017-04-12 08:42:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-10-03 02:56:13 +00:00
|
|
|
case ESP_AVRC_CT_METADATA_RSP_EVT: {
|
2018-01-22 02:24:22 +00:00
|
|
|
ESP_LOGI(BT_AV_TAG, "AVRC metadata rsp: attribute id 0x%x, %s", rc->meta_rsp.attr_id, rc->meta_rsp.attr_text);
|
2017-10-03 02:56:13 +00:00
|
|
|
free(rc->meta_rsp.attr_text);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ESP_AVRC_CT_CHANGE_NOTIFY_EVT: {
|
2018-01-22 02:24:22 +00:00
|
|
|
ESP_LOGI(BT_AV_TAG, "AVRC event notification: %d, param: %d", rc->change_ntf.event_id, rc->change_ntf.event_parameter);
|
2017-10-03 02:56:13 +00:00
|
|
|
bt_av_notify_evt_handler(rc->change_ntf.event_id, rc->change_ntf.event_parameter);
|
|
|
|
break;
|
|
|
|
}
|
2017-12-27 11:49:11 +00:00
|
|
|
case ESP_AVRC_CT_REMOTE_FEATURES_EVT: {
|
2018-01-22 02:24:22 +00:00
|
|
|
ESP_LOGI(BT_AV_TAG, "AVRC remote features %x", rc->rmt_feats.feat_mask);
|
2017-12-27 11:49:11 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-04-12 08:42:14 +00:00
|
|
|
default:
|
|
|
|
ESP_LOGE(BT_AV_TAG, "%s unhandled evt %d", __func__, event);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|