1 /*
2 This example code is in the Public Domain (or CC0 licensed, at your option.)
3
4 Unless required by applicable law or agreed to in writing, this
5 software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
6 CONDITIONS OF ANY KIND, either express or implied.
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <string.h>
13 #include "freertos/FreeRTOS.h"
14 #include "freertos/task.h"
15 #include "nvs.h"
16 #include "nvs_flash.h"
17 #include "esp_system.h"
18 #include "esp_log.h"
19 #include "esp_bt.h"
20 #include "bt_app_core.h"
21 #include "esp_bt_main.h"
22 #include "esp_bt_device.h"
23 #include "esp_gap_bt_api.h"
24 #include "esp_hf_ag_api.h"
25 #include "bt_app_hf.h"
26 #include "gpio_pcm_config.h"
27 #include "esp_console.h"
28 #include "app_hf_msg_set.h"
29
30 #define BT_HF_AG_TAG "HF_AG_DEMO_MAIN"
31
32 /* event for handler "hf_ag_hdl_stack_up */
33 enum {
34 BT_APP_EVT_STACK_UP = 0,
35 };
36
37 /* handler for bluetooth stack enabled events */
bt_hf_hdl_stack_evt(uint16_t event,void * p_param)38 static void bt_hf_hdl_stack_evt(uint16_t event, void *p_param)
39 {
40 ESP_LOGD(BT_HF_TAG, "%s evt %d", __func__, event);
41 switch (event)
42 {
43 case BT_APP_EVT_STACK_UP:
44 {
45 /* set up device name */
46 char *dev_name = "ESP_HFP_AG";
47 esp_bt_dev_set_device_name(dev_name);
48
49 esp_bt_hf_register_callback(bt_app_hf_cb);
50
51 // init and register for HFP_AG functions
52 esp_bt_hf_init(hf_peer_addr);
53
54 /*
55 * Set default parameters for Legacy Pairing
56 * Use variable pin, input pin code when pairing
57 */
58 esp_bt_pin_type_t pin_type = ESP_BT_PIN_TYPE_VARIABLE;
59 esp_bt_pin_code_t pin_code;
60 pin_code[0] = '0';
61 pin_code[1] = '0';
62 pin_code[2] = '0';
63 pin_code[3] = '0';
64 esp_bt_gap_set_pin(pin_type, 4, pin_code);
65
66 /* set discoverable and connectable mode, wait to be connected */
67 esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
68 break;
69 }
70 default:
71 ESP_LOGE(BT_HF_TAG, "%s unhandled evt %d", __func__, event);
72 break;
73 }
74 }
75
app_main(void)76 void app_main(void)
77 {
78 /* Initialize NVS — it is used to store PHY calibration data */
79 esp_err_t ret = nvs_flash_init();
80 if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
81 ESP_ERROR_CHECK(nvs_flash_erase());
82 ret = nvs_flash_init();
83 }
84 ESP_ERROR_CHECK(ret);
85 ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_BLE));
86
87 esp_err_t err;
88 esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
89 if ((err = esp_bt_controller_init(&bt_cfg)) != ESP_OK) {
90 ESP_LOGE(BT_HF_TAG, "%s initialize controller failed: %s\n", __func__, esp_err_to_name(ret));
91 return;
92 }
93 if ((err = esp_bt_controller_enable(ESP_BT_MODE_CLASSIC_BT)) != ESP_OK) {
94 ESP_LOGE(BT_HF_TAG, "%s enable controller failed: %s\n", __func__, esp_err_to_name(ret));
95 return;
96 }
97 if ((err = esp_bluedroid_init()) != ESP_OK) {
98 ESP_LOGE(BT_HF_TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
99 return;
100 }
101 if ((err = esp_bluedroid_enable()) != ESP_OK) {
102 ESP_LOGE(BT_HF_TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(ret));
103 return;
104 }
105
106 /* create application task */
107 bt_app_task_start_up();
108
109 /* Bluetooth device name, connection mode and profile set up */
110 bt_app_work_dispatch(bt_hf_hdl_stack_evt, BT_APP_EVT_STACK_UP, NULL, 0, NULL);
111
112 #if CONFIG_BT_HFP_AUDIO_DATA_PATH_PCM
113 /* configure the PCM interface and PINs used */
114 app_gpio_pcm_io_cfg();
115 #endif
116
117 /* configure externel chip for acoustic echo cancellation */
118 #if ACOUSTIC_ECHO_CANCELLATION_ENABLE
119 app_gpio_aec_io_cfg();
120 #endif /* ACOUSTIC_ECHO_CANCELLATION_ENABLE */
121
122 esp_console_repl_t *repl = NULL;
123 esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
124 esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();
125 repl_config.prompt = "hfp_ag>";
126
127 // init console REPL environment
128 ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &repl));
129
130 /* Register commands */
131 register_hfp_ag();
132 printf("\n ==================================================\n");
133 printf(" | Steps to test hfp_ag |\n");
134 printf(" | |\n");
135 printf(" | 1. Print 'help' to gain overview of commands |\n");
136 printf(" | 2. Setup a service level connection |\n");
137 printf(" | 3. Run hfp_ag to test |\n");
138 printf(" | |\n");
139 printf(" =================================================\n\n");
140
141 // start console REPL
142 ESP_ERROR_CHECK(esp_console_start_repl(repl));
143 }
144