]> granicus.if.org Git - esp-idf/blob - examples/bluetooth/a2dp_sink/main/main.c
1e634dfe7658831ed4077a54361a7a39f5e96271
[esp-idf] / examples / bluetooth / a2dp_sink / main / main.c
1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include "freertos/FreeRTOS.h"
20 #include "freertos/task.h"
21 #include "nvs.h"
22 #include "nvs_flash.h"
23 #include "esp_system.h"
24 #include "esp_log.h"
25
26 #include "esp_bt.h"
27 #include "bt_app_core.h"
28 #include "bt_app_av.h"
29 #include "esp_bt_main.h"
30 #include "esp_bt_device.h"
31 #include "esp_gap_bt_api.h"
32 #include "esp_a2dp_api.h"
33 #include "esp_avrc_api.h"
34 #include "driver/i2s.h"
35
36 /* event for handler "bt_av_hdl_stack_up */
37 enum {
38     BT_APP_EVT_STACK_UP = 0,
39 };
40
41 /* handler for bluetooth stack enabled events */
42 static void bt_av_hdl_stack_evt(uint16_t event, void *p_param);
43
44
45 void app_main()
46 {
47     /* Initialize NVS — it is used to store PHY calibration data */
48     esp_err_t ret = nvs_flash_init();
49     if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
50         ESP_ERROR_CHECK(nvs_flash_erase());
51         ret = nvs_flash_init();
52     }
53     ESP_ERROR_CHECK( ret );
54
55     i2s_config_t i2s_config = {
56 #ifdef CONFIG_A2DP_SINK_OUTPUT_INTERNAL_DAC
57         .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN,
58 #else
59         .mode = I2S_MODE_MASTER | I2S_MODE_TX,                                  // Only TX
60 #endif
61         .sample_rate = 44100,
62         .bits_per_sample = 16,
63         .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,                           //2-channels
64         .communication_format = I2S_COMM_FORMAT_I2S_MSB,
65         .dma_buf_count = 6,
66         .dma_buf_len = 60,                                                      //
67         .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1                                //Interrupt level 1
68     };
69
70
71     i2s_driver_install(0, &i2s_config, 0, NULL);
72 #ifdef CONFIG_A2DP_SINK_OUTPUT_INTERNAL_DAC
73     i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
74     i2s_set_pin(0, NULL);
75 #else
76     i2s_pin_config_t pin_config = {
77         .bck_io_num = CONFIG_I2S_BCK_PIN,
78         .ws_io_num = CONFIG_I2S_LRCK_PIN,
79         .data_out_num = CONFIG_I2S_DATA_PIN,
80         .data_in_num = -1                                                       //Not used
81     };
82
83     i2s_set_pin(0, &pin_config);
84 #endif
85
86
87     ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
88
89     esp_err_t err;
90     esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
91     if ((err = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
92         ESP_LOGE(BT_AV_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
93         return;
94     }
95
96     if ((err = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) {
97         ESP_LOGE(BT_AV_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
98         return;
99     }
100
101     if ((err = esp_bluedroid_init()) != ESP_OK) {
102         ESP_LOGE(BT_AV_TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
103         return;
104     }
105
106     if ((err = esp_bluedroid_enable()) != ESP_OK) {
107         ESP_LOGE(BT_AV_TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
108         return;
109     }
110
111     /* create application task */
112     bt_app_task_start_up();
113
114     /* Bluetooth device name, connection mode and profile set up */
115     bt_app_work_dispatch(bt_av_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL);
116
117 #if (BT_SPP_INCLUDED)
118     esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
119     esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_IO;
120     esp_bt_gap_set_security_param(param_type, &iocap, sizeof(uint8_t));
121 #endif ///BT_SPP_INCLUDED
122 }
123
124 void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *param)
125 {
126     switch (event) {
127 #if (BT_SPP_INCLUDED)
128     case ESP_BT_GAP_AUTH_CMPL_EVT:{
129         if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) {
130             ESP_LOGI(BT_AV_TAG, "authentication success: %s", param->auth_cmpl.device_name);
131             esp_log_buffer_hex(BT_AV_TAG, param->auth_cmpl.bda, ESP_BD_ADDR_LEN);
132         } else {
133             ESP_LOGE(BT_AV_TAG, "authentication failed, status:%d", param->auth_cmpl.stat);
134         }
135         break;
136     }
137     case ESP_BT_GAP_CFM_REQ_EVT:
138         ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_CFM_REQ_EVT Please compare the numeric value: %d", param->cfm_req.num_val);
139         esp_bt_gap_ssp_confirm_reply(param->cfm_req.bda, true);
140         break;
141     case ESP_BT_GAP_KEY_NOTIF_EVT:
142         ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_NOTIF_EVT passkey:%d", param->key_notif.passkey);
143         break;
144     case ESP_BT_GAP_KEY_REQ_EVT:
145         ESP_LOGI(BT_AV_TAG, "ESP_BT_GAP_KEY_REQ_EVT Please enter passkey!");
146         break;
147 #endif ///BT_SPP_INCLUDED
148     default: {
149         ESP_LOGI(BT_AV_TAG, "event: %d", event);
150         break;
151     }
152     }
153     return;
154 }
155 static void bt_av_hdl_stack_evt(uint16_t event, void *p_param)
156 {
157     ESP_LOGD(BT_AV_TAG, "%s evt %d", __func__, event);
158     switch (event) {
159     case BT_APP_EVT_STACK_UP: {
160         /* set up device name */
161         char *dev_name = "ESP_SPEAKER";
162         esp_bt_dev_set_device_name(dev_name);
163
164         esp_bt_gap_register_callback(bt_app_gap_cb);
165         /* initialize A2DP sink */
166         esp_a2d_register_callback(&bt_app_a2d_cb);
167         esp_a2d_sink_register_data_callback(bt_app_a2d_data_cb);
168         esp_a2d_sink_init();
169
170         /* initialize AVRCP controller */
171         esp_avrc_ct_init();
172         esp_avrc_ct_register_callback(bt_app_rc_ct_cb);
173
174         /* set discoverable and connectable mode, wait to be connected */
175         esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
176         break;
177     }
178     default:
179         ESP_LOGE(BT_AV_TAG, "%s unhandled evt %d", __func__, event);
180         break;
181     }
182 }