// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include #include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "nvs.h" #include "nvs_flash.h" #include "esp_system.h" #include "esp_log.h" #include "esp_bt.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" #include "driver/i2s.h" /* device name */ #define LOCAL_DEVICE_NAME "ESP_SPEAKER" /* event for stack up */ enum { BT_APP_EVT_STACK_UP = 0, }; /******************************** * STATIC FUNCTION DECLARATIONS *******************************/ /* GAP callback function */ static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param); /* handler for bluetooth stack enabled events */ static void bt_av_hdl_stack_evt(uint16_t event, void *p_param); /******************************* * STATIC FUNCTION DEFINITIONS ******************************/ static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param) { switch (event) { /* when authentication completed, this event comes */ case ESP_BT_GAP_AUTH_CMPL_EVT: { if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) { ESP_LOGI(BT_AV_TAG, "authentication success: %s", param->auth_cmpl.device_name); esp_log_buffer_hex(BT_AV_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN); } else { ESP_LOGE(BT_AV_TAG, "authentication failed, status: %d", param->auth_cmpl.stat); } break; } #if (CONFIG_BT_SSP_ENABLED == true) /* when Security Simple Pairing user confirmation requested, this event comes */ case ESP_BT_GAP_CFM_REQ_EVT: ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val); esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true); break; /* when Security Simple Pairing passkey notified, this event comes */ case ESP_BT_GAP_KEY_NOTIF_EVT: ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_NOTIF_EVT passkey: %d", param->key_notif.passkey); break; /* when Security Simple Pairing passkey requested, this event comes */ case ESP_BT_GAP_KEY_REQ_EVT: ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!"); break; #endif /* when GAP mode changed, this event comes */ case ESP_BT_GAP_MODE_CHG_EVT: ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_MODE_CHG_EVT mode: %d", param->mode_chg.mode); break; /* others */ default: { ESP_LOGI(BT_AV_TAG, "event: %d", event); break; } } } static void bt_av_hdl_stack_evt(uint16_t event, void *p_param) { ESP_LOGD(BT_AV_TAG, "%s event: %d", __func__, event); switch (event) { /* when do the stack up, this event comes */ case BT_APP_EVT_STACK_UP: { esp_bt_dev_set_device_name(LOCAL_DEVICE_NAME); esp_bt_gap_register_callback(bt_app_gap_cb); assert(esp_avrc_ct_init() == ESP_OK); esp_avrc_ct_register_callback(bt_app_rc_ct_cb); assert(esp_avrc_tg_init() == ESP_OK); esp_avrc_tg_register_callback(bt_app_rc_tg_cb); esp_avrc_rn_evt_cap_mask_t evt_set = {0}; esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_SET, &evt_set, ESP_AVRC_RN_VOLUME_CHANGE); assert(esp_avrc_tg_set_rn_evt_cap(&evt_set) == ESP_OK); assert(esp_a2d_sink_init() == ESP_OK); esp_a2d_register_callback(&bt_app_a2d_cb); esp_a2d_sink_register_data_callback(bt_app_a2d_data_cb); /* set discoverable and connectable mode, wait to be connected */ esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE); break; } /* others */ default: ESP_LOGE(BT_AV_TAG, "%s unhandled event: %d", __func__, event); break; } } /******************************* * MAIN ENTRY POINT ******************************/ void app_main(void) { /* initialize NVS — it is used to store PHY calibration data */ esp_err_t err = nvs_flash_init(); if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); err = nvs_flash_init(); } ESP_ERROR_CHECK(err); /* * This example only uses the functions of Classical Bluetooth. * So release the controller memory for Bluetooth Low Energy. */ ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE)); esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT(); if ((err = esp_bt_controller_init(&bt_cfg)) != ESP_OK) { ESP_LOGE(BT_AV_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(err)); return; } if ((err = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) { ESP_LOGE(BT_AV_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(err)); return; } if ((err = esp_bluedroid_init()) != ESP_OK) { ESP_LOGE(BT_AV_TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(err)); return; } if ((err = esp_bluedroid_enable()) != ESP_OK) { ESP_LOGE(BT_AV_TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(err)); return; } /* I2S configuration parameters */ i2s_config_t i2s_config = { #ifdef CONFIG_EXAMPLE_A2DP_SINK_OUTPUT_INTERNAL_DAC .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN, #else .mode = I2S_MODE_MASTER | I2S_MODE_TX, /* only TX */ #endif .sample_rate = 44100, .bits_per_sample = 16, .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT, /* 2-channels */ .communication_format = I2S_COMM_FORMAT_STAND_MSB, .dma_buf_count = 6, .dma_buf_len = 60, .intr_alloc_flags = 0, /* default interrupt priority */ .tx_desc_auto_clear = true /* auto clear tx descriptor on underflow */ }; /* enable I2S */ i2s_driver_install(0, &i2s_config, 0, NULL); #ifdef CONFIG_EXAMPLE_A2DP_SINK_OUTPUT_INTERNAL_DAC i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN); i2s_set_pin(0, NULL); #else i2s_pin_config_t pin_config = { .bck_io_num = CONFIG_EXAMPLE_I2S_BCK_PIN, .ws_io_num = CONFIG_EXAMPLE_I2S_LRCK_PIN, .data_out_num = CONFIG_EXAMPLE_I2S_DATA_PIN, .data_in_num = -1 /* not used */ }; i2s_set_pin(0, &pin_config); #endif #if (CONFIG_BT_SSP_ENABLED == true) /* set default parameters for Secure Simple Pairing */ esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE; esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO; esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t)); #endif /* set default parameters for Legacy Pairing (use fixed pin code 1234) */ esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_FIXED; esp_bt_pin_code_t pin_code; pin_code[0] = '1'; pin_code[1] = '2'; pin_code[2] = '3'; pin_code[3] = '4'; esp_bt_gap_set_pin(pin_type, 4, pin_code); bt_app_task_start_up(); /* bluetooth device name, connection mode and profile set up */ bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL); }