From b1580c6d1b8b5e7bd6a58ba089e3130c8da17c03 Mon Sep 17 00:00:00 2001 From: Joseph Tang Date: Mon, 9 Sep 2024 11:44:18 +0800 Subject: [PATCH] DuerOS: 1. Update docs about xiaodu app 2. Disconnect Wi-Fi when starting provisioning. wifi_service: 1. Connect to the configured SSID by default. --- components/wifi_service/src/wifi_service.c | 108 ++++++------ examples/dueros/README.md | 14 +- examples/dueros/README_CN.md | 17 +- examples/dueros/main/dueros_app.c | 196 ++++++++++----------- 4 files changed, 180 insertions(+), 155 deletions(-) diff --git a/components/wifi_service/src/wifi_service.c b/components/wifi_service/src/wifi_service.c index c50debba..ce6c41ea 100644 --- a/components/wifi_service/src/wifi_service.c +++ b/components/wifi_service/src/wifi_service.c @@ -45,8 +45,8 @@ #include "audio_idf_version.h" -static const char *TAG = "WIFI_SERV"; -const static int WIFI_TASK_DESTROY_BIT = BIT0; +static const char *TAG = "WIFI_SERV"; +const static int WIFI_TASK_DESTROY_BIT = BIT0; typedef enum { WIFI_SERV_CMD_UNKNOWN, @@ -59,32 +59,32 @@ typedef enum { } wifi_serv_cmd_t; typedef struct wifi_setting_item { - STAILQ_ENTRY(wifi_setting_item) next; - esp_wifi_setting_handle_t on_handle; - int index; - bool running; + STAILQ_ENTRY(wifi_setting_item) next; + esp_wifi_setting_handle_t on_handle; + int index; + bool running; } wifi_setting_item_t; typedef STAILQ_HEAD(wifi_setting_list, wifi_setting_item) wifi_setting_list_t; typedef struct { - xQueueHandle wifi_serv_que; - wifi_service_event_t wifi_serv_state; - wifi_config_t info; - wifi_setting_list_t setting_list; - int setting_index; - wifi_service_disconnect_reason_t reason; - wifi_ssid_manager_handle_t ssid_manager; - bool is_setting; - esp_timer_handle_t retry_timer; - esp_timer_handle_t setting_timer; - int setting_timeout_s; - EventGroupHandle_t sync_evt; - int retry_times; - int max_retry_time; - int prov_retry_times; - int max_prov_retry_time; - bool retrying; + xQueueHandle wifi_serv_que; + wifi_service_event_t wifi_serv_state; + wifi_config_t info; + wifi_setting_list_t setting_list; + int setting_index; + wifi_service_disconnect_reason_t reason; + wifi_ssid_manager_handle_t ssid_manager; + bool is_setting; + esp_timer_handle_t retry_timer; + esp_timer_handle_t setting_timer; + int setting_timeout_s; + EventGroupHandle_t sync_evt; + int retry_times; + int max_retry_time; + int prov_retry_times; + int max_prov_retry_time; + bool retrying; } wifi_service_t; typedef enum { @@ -93,10 +93,10 @@ typedef enum { } wifi_serv_event_type_t; typedef struct { - wifi_serv_event_type_t msg_type; - int type; - uint32_t *pdata; - int len; + wifi_serv_event_type_t msg_type; + int type; + uint32_t *pdata; + int len; } wifi_task_msg_t; static void wifi_serv_cmd_send(void *que, int type, void *data, int len, int dir) @@ -107,7 +107,7 @@ static void wifi_serv_cmd_send(void *que, int type, void *data, int len, int dir evt.pdata = data; evt.len = len; if (dir) { - xQueueSendToFront(que, &evt, 0) ; + xQueueSendToFront(que, &evt, 0); } else { xQueueSend(que, &evt, 0); } @@ -121,7 +121,7 @@ static void wifi_serv_state_send(void *que, int type, void *data, int len, int d evt.pdata = data; evt.len = len; if (dir) { - xQueueSendToFront(que, &evt, 0) ; + xQueueSendToFront(que, &evt, 0); } else { xQueueSend(que, &evt, 0); } @@ -135,12 +135,12 @@ static void wifi_event_cb(void *arg, esp_event_base_t event_base, wifi_service_t *serv = periph_service_get_data(serv_handle); if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { esp_wifi_connect(); - } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { - ip_event_got_ip_t *event = (ip_event_got_ip_t *) event_data; + } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { + ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; ESP_LOGI(TAG, "Got ip:" IPSTR, IP2STR(&event->ip_info.ip)); wifi_serv_state_send(serv->wifi_serv_que, WIFI_SERV_EVENT_CONNECTED, 0, 0, 0); } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { - wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *) event_data; + wifi_event_sta_disconnected_t *event = (wifi_event_sta_disconnected_t *)event_data; if (serv->reason == WIFI_SERV_STA_BY_USER) { ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED, reason is WIFI_SERV_STA_BY_USER"); wifi_serv_state_send(serv->wifi_serv_que, WIFI_SERV_EVENT_DISCONNECTED, 0, 0, 0); @@ -220,12 +220,12 @@ static esp_err_t wifi_event_cb(void *ctx, system_event_t *event) wifi_serv_state_send(serv->wifi_serv_que, WIFI_SERV_EVENT_DISCONNECTED, 0, 0, 0); break; case SYSTEM_EVENT_AP_STACONNECTED: - ESP_LOGI(TAG, "Station:"MACSTR" join, AID=%d", + ESP_LOGI(TAG, "Station:" MACSTR " join, AID=%d", MAC2STR(event->event_info.sta_connected.mac), event->event_info.sta_connected.aid); break; case SYSTEM_EVENT_AP_STADISCONNECTED: - ESP_LOGI(TAG, "Station:"MACSTR"leave, AID=%d", + ESP_LOGI(TAG, "Station:" MACSTR "leave, AID=%d", MAC2STR(event->event_info.sta_disconnected.mac), event->event_info.sta_disconnected.aid); break; @@ -300,7 +300,7 @@ static void wifi_task(void *pvParameters) wifi_config_t *stored_ssid = NULL; esp_dispatcher_handle_t dispatcher = esp_dispatcher_get_delegate_handle(); - action_result_t result = { 0 }; + action_result_t result = {0}; esp_dispatcher_execute_with_func(dispatcher, wifi_init, pvParameters, NULL, &result); esp_timer_create_args_t tmr_args = { @@ -329,7 +329,7 @@ static void wifi_task(void *pvParameters) ESP_LOGW(TAG, "STATE type:%d, pdata:%p, len:%d", wifi_msg.type, wifi_msg.pdata, wifi_msg.len); if (wifi_msg.type == WIFI_SERV_EVENT_SETTING_TIMEOUT) { ESP_LOGI(TAG, "WIFI_SERV_EVENT_SETTING_TIMEOUT"); - STAILQ_FOREACH(item, &serv->setting_list, next) { + STAILQ_FOREACH (item, &serv->setting_list, next) { if (item->running) { esp_wifi_setting_stop(item->on_handle); } @@ -344,7 +344,7 @@ static void wifi_task(void *pvParameters) serv->retrying = false; if (serv->is_setting) { esp_timer_stop(serv->setting_timer); - STAILQ_FOREACH(item, &serv->setting_list, next) { + STAILQ_FOREACH (item, &serv->setting_list, next) { if (item->running == false) { continue; } @@ -353,12 +353,12 @@ static void wifi_task(void *pvParameters) } serv->is_setting = false; } - wifi_ssid_manager_save(serv->ssid_manager, (const char *)wifi_cfg.sta.ssid, (const char *)wifi_cfg.sta.password); + wifi_ssid_manager_save(serv->ssid_manager, (const char *)wifi_cfg.sta.ssid, (const char *)wifi_cfg.sta.password); } if (wifi_msg.type == WIFI_SERV_EVENT_DISCONNECTED) { if ((serv->reason != WIFI_SERV_STA_BY_USER) && (serv->reason != WIFI_SERV_STA_SET_INFO)) { retry_interval = serv->retry_times * 1000 * 1000 * 2; - if (retry_interval > 60 * 1000 * 1000) { // Longest interval is 60s + if (retry_interval > 60 * 1000 * 1000) { // Longest interval is 60s retry_interval = 60 * 1000 * 1000; } // reconnect the SSID @@ -383,7 +383,7 @@ static void wifi_task(void *pvParameters) } } else { serv->retrying = true; - serv->retry_times ++; + serv->retry_times++; // The time interval for reconnection will gradually increase. At the maximum, the connection will be once a minute. esp_timer_start_once(serv->retry_timer, retry_interval); ESP_LOGW(TAG, "Got max_retry_time = %d, the station will try to reconnect until connected", serv->max_retry_time); @@ -394,7 +394,7 @@ static void wifi_task(void *pvParameters) vTaskDelay(2000 / portTICK_PERIOD_MS); configure_wifi_sta_mode(&wifi_cfg); wifi_service_connect(serv_handle); - serv->prov_retry_times ++; + serv->prov_retry_times++; ESP_LOGW(TAG, "Fail to connect to provision wifi, reason: %d, start to reconnect ...", serv->reason); } else { serv->prov_retry_times = 0; @@ -410,13 +410,15 @@ static void wifi_task(void *pvParameters) } else if (wifi_msg.msg_type == WIFI_SERV_EVENT_TYPE_CMD) { if (wifi_msg.type == WIFI_SERV_CMD_CONNECT) { if (serv->reason != WIFI_SERV_STA_SET_INFO) { - if (wifi_ssid_manager_get_latest_config(serv->ssid_manager, &wifi_cfg) != ESP_OK) { - ESP_LOGW(TAG, "No ssid stored in flash, try to connect to wifi set by wifi_service_set_sta_info()"); - if (serv->info.sta.ssid[0] == 0) { + if (strlen((char *)(serv->info.sta.ssid))) { + memcpy(&wifi_cfg, &serv->info, sizeof(wifi_config_t)); + memset(&serv->info, 0x00, sizeof(wifi_config_t)); + } else { + ESP_LOGW(TAG, "No ssid set by wifi_service_set_sta_info(), try to connect ssid stored in flash"); + if (wifi_ssid_manager_get_latest_config(serv->ssid_manager, &wifi_cfg) != ESP_OK) { ESP_LOGW(TAG, "There is no preset ssid, please set the wifi first"); continue; } - memcpy(&wifi_cfg, &serv->info, sizeof(wifi_config_t)); } } configure_wifi_sta_mode(&wifi_cfg); @@ -436,7 +438,7 @@ static void wifi_task(void *pvParameters) if (serv->retrying == true) { esp_timer_stop(serv->retry_timer); } - STAILQ_FOREACH(item, &serv->setting_list, next) { + STAILQ_FOREACH (item, &serv->setting_list, next) { if (idx == 0) { esp_wifi_setting_start(item->on_handle); item->running = true; @@ -448,11 +450,11 @@ static void wifi_task(void *pvParameters) } serv->is_setting = true; esp_timer_start_once(serv->setting_timer, (uint64_t)serv->setting_timeout_s * 1000 * 1000); - } else if (wifi_msg.type == WIFI_SERV_CMD_SETTING_STOP) { + } else if (wifi_msg.type == WIFI_SERV_CMD_SETTING_STOP) { int idx = (int)wifi_msg.pdata; ESP_LOGI(TAG, "WIFI_SERV_CMD_SETTING_STOP,index:%d", idx); esp_timer_stop(serv->setting_timer); - STAILQ_FOREACH(item, &serv->setting_list, next) { + STAILQ_FOREACH (item, &serv->setting_list, next) { if (idx == 0) { esp_wifi_setting_stop(item->on_handle); } else if (item->index == idx) { @@ -626,7 +628,7 @@ esp_err_t wifi_service_erase_ssid_manager_info(periph_service_handle_t handle) return ret; } -esp_err_t wifi_service_get_last_ssid_cfg(periph_service_handle_t handle, wifi_config_t* wifi_cfg) +esp_err_t wifi_service_get_last_ssid_cfg(periph_service_handle_t handle, wifi_config_t *wifi_cfg) { wifi_service_t *serv = periph_service_get_data(handle); return wifi_ssid_manager_get_latest_config(serv->ssid_manager, wifi_cfg); @@ -634,7 +636,7 @@ esp_err_t wifi_service_get_last_ssid_cfg(periph_service_handle_t handle, wifi_co periph_service_handle_t wifi_service_create(wifi_service_config_t *config) { - wifi_service_t *serv = audio_calloc(1, sizeof(wifi_service_t)); + wifi_service_t *serv = audio_calloc(1, sizeof(wifi_service_t)); AUDIO_MEM_CHECK(TAG, serv, return NULL); serv->ssid_manager = wifi_ssid_manager_create(config->max_ssid_num); @@ -665,10 +667,10 @@ periph_service_handle_t wifi_service_create(wifi_service_config_t *config) serv->setting_timeout_s = config->setting_timeout_s; periph_service_config_t cfg = { .task_stack = config->task_stack, - .task_prio = config->task_prio, - .task_core = config->task_core, + .task_prio = config->task_prio, + .task_core = config->task_core, .extern_stack = config->extern_stack, - .task_func = wifi_task, + .task_func = wifi_task, .service_start = _wifi_start, .service_stop = _wifi_stop, .service_ioctl = NULL, diff --git a/examples/dueros/README.md b/examples/dueros/README.md index 0c3f8b0c..3e720195 100755 --- a/examples/dueros/README.md +++ b/examples/dueros/README.md @@ -10,7 +10,19 @@ This example demonstrates how to connect to the DuerOS 3.2 cloud developed by Ba Among them, [esp audio](../../components/esp-adf-libs/esp_audio/include/esp_audio.h) manages audio playback, [wifi service](../../components/wifi_service) manages provisioning and Wi-Fi connection, [audio recorder](../../components/audio_recorder/include/audio_recorder.h) manages wake-up and voice data, [display service](../../components/display_service/display_service.c) manages system indicators, [Dueros service](../../components/dueros_service) manages connection to DuerOS, and [esp_periph_set_register_callback](../../components/esp_peripherals/include/esp_peripherals.h) or [Key service](../../components/input_key_service/input_key_service.c) manages key events. -In addition, you need to apply for the DuerOS profile on the [DuerOS Open Platform](https://dueros.baidu.com/didp/doc/overall/console-guide_markdown) in advance, and replace the empty file in `ADF_PATH/components/dueros_service/duer_profile`. +In addition, you need to apply for the DuerOS profile on the [DuerOS Open Platform](https://dueros.baidu.com/didp/doc/overall/console-guide_markdown) in advance, and copy the content to the empty file: `ADF_PATH/components/dueros_service/duer_profile`. + +## XiaoDu APP + +This example works with the XiaoDu app provided by Baidu and uses the app for Wi-Fi configuration. To use this feature, the profile, device name prefix, and encryption key are provided specifically. Please contact Baidu to obtain these, as they are currently not available through the webpage. + +- Select `Lightduer DIPB WiFi config` and complete the configuration. + - Duer client ID: Provied by Baidu + - Duer device ID:`uuid` in DuerOS profile + - Duer ECC public key:Provied by Baidu +- Set device name + - `menuconfig > Component config > DuerOS Service > Duer device name` + - The device name requires a specific prefix provided by Baidu. Different prefixes correspond to different devices. For example, `xdtp-D1` should select `Add Device > Children -> Story Machine` during Wi-Fi provisioning. ## `WenXinYiYan` support diff --git a/examples/dueros/README_CN.md b/examples/dueros/README_CN.md index 41fd9bfe..52e35023 100644 --- a/examples/dueros/README_CN.md +++ b/examples/dueros/README_CN.md @@ -10,7 +10,22 @@ 其中,[esp audio](../../components/esp-adf-libs/esp_audio/include/esp_audio.h) 处理音频播放,[wifi service](../../components/wifi_service) 接口管理配网和连接 Wi-Fi,[audio recorder](../../components/audio_recorder/include/audio_recorder.h) 负责唤醒和语音数据管理,[display service](../../components/display_service/display_service.c) 管理系统指示灯,[Dueros service](../../components/dueros_service) 连接 DuerOS,[esp_periph_set_register_callback](../../components/esp_peripherals/include/esp_peripherals.h) 管理按键事件,也可以使用 [Key service](../../components/input_key_service/input_key_service.c) 按键服务来管理按键。 -此外,本例程需要预先在 [百度 DuerOS 开放平台](https://dueros.baidu.com/didp/doc/overall/console-guide_markdown) 申请 DuerOS 的 profile,并替换 `ADF_PATH/components/dueros_service/duer_profile` 中的空文件。 +此外,本例程需要预先在 [百度 DuerOS 开放平台](https://dueros.baidu.com/didp/doc/overall/console-guide_markdown) 申请 DuerOS 的 profile,并拷贝内容到 `ADF_PATH/components/dueros_service/duer_profile` 文件中。 + +## 小度APP + +本例程已经支持接入`小度APP`并使用该应用配置 WiFi,使用该功能需要联系百度以获取其特殊提供的 DuerOS profile,设备名前缀和密钥,当前无法自行通过网页获取。 + +### 使用 `小度APP` 配置 WiFi + +- 选择`小度APP`配网模式,并填写配置 + - `menuconfig > Example Configuration > WiFi Setting type > Lightduer DIPB WiFi config` + - Duer client ID: 由百度提供 + - Duer device ID:DuerOS profile 中的 `uuid` 字段 + - Duer ECC public key:由百度提供 +- 设置设备名 + - `menuconfig > Component config > DuerOS Service > Duer device name` + - 设备名需要特殊前缀,由百度提供。不同前缀对应的不同设备类型,如 `xdtp-D1` 在配网时需要选择 `添加设备 -> 儿童设备 -> 儿童故事机` ## 文心一言 diff --git a/examples/dueros/main/dueros_app.c b/examples/dueros/main/dueros_app.c index 78b29bc7..60b50773 100644 --- a/examples/dueros/main/dueros_app.c +++ b/examples/dueros/main/dueros_app.c @@ -75,28 +75,28 @@ #include "esp_bt_defs.h" #include "esp_bt_main.h" #include "esp_gatt_common_api.h" -#endif // CONFIG_DUER_WIFI_CONFIG +#endif // CONFIG_DUER_WIFI_CONFIG #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)) #include "driver/touch_pad.h" -#endif +#endif /* (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)) */ #define DUER_REC_READING (BIT0) -static const char *TAG = "DUEROS"; -static esp_audio_handle_t player = NULL; -static audio_rec_handle_t recorder = NULL; -static audio_service_handle_t duer_serv_handle = NULL; -static display_service_handle_t disp_serv = NULL; -static periph_service_handle_t wifi_serv = NULL; -static bool wifi_setting_flag = false; -static EventGroupHandle_t duer_evt = NULL; -static esp_dispatcher_handle_t esp_dispatcher = NULL; +static const char *TAG = "DUEROS"; +static esp_audio_handle_t player = NULL; +static audio_rec_handle_t recorder = NULL; +static audio_service_handle_t duer_serv_handle = NULL; +static display_service_handle_t disp_serv = NULL; +static periph_service_handle_t wifi_serv = NULL; +static bool wifi_setting_flag = false; +static EventGroupHandle_t duer_evt = NULL; +static esp_dispatcher_handle_t esp_dispatcher = NULL; #ifdef CONFIG_DUER_WIFI_CONFIG static esp_err_t wifi_cfg_start(); -static void wifi_cfg_stop(); -#endif /* CONFIG_DUER_WIFI_CONFIG */ +static void wifi_cfg_stop(); +#endif /* CONFIG_DUER_WIFI_CONFIG */ extern int duer_dcs_audio_sync_play_tone(const char *uri); @@ -145,7 +145,7 @@ static esp_err_t rec_engine_cb(audio_rec_evt_t *event, void *user_data) action_arg_t action_arg = {0}; action_arg.data = (void *)"spiffs://spiffs/dingding.wav"; - action_result_t result = { 0 }; + action_result_t result = {0}; esp_dispatcher_execute_with_func(esp_dispatcher, dispatcher_audio_play, NULL, &action_arg, &result); } else if (AUDIO_REC_VAD_START == event->type) { ESP_LOGI(TAG, "rec_engine_cb - AUDIO_REC_VAD_START"); @@ -164,7 +164,6 @@ static esp_err_t rec_engine_cb(audio_rec_evt_t *event, void *user_data) display_service_set_pattern(disp_serv, DISPLAY_PATTERN_TURN_OFF, 0); ESP_LOGI(TAG, "rec_engine_cb - AUDIO_REC_WAKEUP_END"); } else { - } return ESP_OK; @@ -263,11 +262,11 @@ static void duer_wifi_cfg_user_cb(duer_wifi_cfg_event_t event, void *data) case DUER_WIFI_CFG_SSID_GET: { duer_wifi_ssid_get_t *wifi_info = data; ESP_LOGI(TAG, "wifi info get %s, %s, %s, %s", - wifi_info->ssid, wifi_info->pwd, wifi_info->bduss, wifi_info->device_bduss); + wifi_info->ssid, wifi_info->pwd, wifi_info->bduss, wifi_info->device_bduss); duer_profile_update(wifi_info->bduss, CONFIG_DUER_CLIENT_ID); - wifi_config_t sta_cfg = { 0 }; + wifi_config_t sta_cfg = {0}; memcpy((char *)&sta_cfg.sta.ssid, wifi_info->ssid, sizeof(sta_cfg.sta.ssid)); memcpy((char *)&sta_cfg.sta.password, wifi_info->pwd, sizeof(sta_cfg.sta.password)); wifi_service_set_sta_info(wifi_serv, &sta_cfg); @@ -318,7 +317,7 @@ static void wifi_cfg_stop() stop_ble(); } -#endif //CONFIG_DUER_WIFI_CONFIG +#endif // CONFIG_DUER_WIFI_CONFIG static esp_err_t duer_callback(audio_service_handle_t handle, service_event_t *evt, void *ctx) { @@ -327,31 +326,34 @@ static esp_err_t duer_callback(audio_service_handle_t handle, service_event_t *e ESP_LOGW(TAG, "duer_callback: type:%x, source:%p data:%d, data_len:%d", evt->type, evt->source, state, evt->len); switch (state) { case SERVICE_STATE_IDLE: { - xTimerHandle retry_login_timer = (xTimerHandle) ctx; - ESP_LOGW(TAG, "reason:%d", wifi_service_disconnect_reason_get(wifi_serv)); + xTimerHandle retry_login_timer = (xTimerHandle)ctx; + ESP_LOGW(TAG, "reason:%d", wifi_service_disconnect_reason_get(wifi_serv)); - if (WIFI_SERV_STA_BY_USER == wifi_service_disconnect_reason_get(wifi_serv)) { - break; - } - if (retry_num < 128) { - retry_num *= 2; - ESP_LOGI(TAG, "Dueros DUER_CMD_QUIT reconnect, retry_num:%d", retry_num); - } else { - ESP_LOGE(TAG, "Dueros reconnect failed,time num:%d ", retry_num); - xTimerStop(retry_login_timer, portMAX_DELAY); - break; - } + if (WIFI_SERV_STA_BY_USER == wifi_service_disconnect_reason_get(wifi_serv)) { + break; + } + if (retry_num < 128) { + retry_num *= 2; + ESP_LOGI(TAG, "Dueros DUER_CMD_QUIT reconnect, retry_num:%d", retry_num); + } else { + ESP_LOGE(TAG, "Dueros reconnect failed,time num:%d ", retry_num); xTimerStop(retry_login_timer, portMAX_DELAY); - xTimerChangePeriod(retry_login_timer, (1000 / portTICK_PERIOD_MS) * retry_num, portMAX_DELAY); - xTimerStart(retry_login_timer, portMAX_DELAY); break; } - case SERVICE_STATE_CONNECTING: break; + xTimerStop(retry_login_timer, portMAX_DELAY); + xTimerChangePeriod(retry_login_timer, (1000 / portTICK_PERIOD_MS) * retry_num, portMAX_DELAY); + xTimerStart(retry_login_timer, portMAX_DELAY); + break; + } + case SERVICE_STATE_CONNECTING: + break; case SERVICE_STATE_CONNECTED: retry_num = 1; break; - case SERVICE_STATE_RUNNING: break; - case SERVICE_STATE_STOPPED: break; + case SERVICE_STATE_RUNNING: + break; + case SERVICE_STATE_STOPPED: + break; default: break; } @@ -359,7 +361,6 @@ static esp_err_t duer_callback(audio_service_handle_t handle, service_event_t *e return ESP_OK; } - esp_err_t periph_callback(audio_event_iface_msg_t *event, void *context) { #if FUNC_BUTTON_EN @@ -367,72 +368,67 @@ esp_err_t periph_callback(audio_event_iface_msg_t *event, void *context) event->source_type, event->source, event->cmd, event->data, event->data_len); switch (event->source_type) { case PERIPH_ID_BUTTON: { - if ((int)event->data == get_input_rec_id() && event->cmd == PERIPH_BUTTON_PRESSED) { - ESP_LOGI(TAG, "PERIPH_NOTIFY_KEY_REC"); - audio_recorder_trigger_start(recorder); - } else if ((int)event->data == get_input_mode_id() && - ((event->cmd == PERIPH_BUTTON_RELEASE) || (event->cmd == PERIPH_BUTTON_LONG_RELEASE))) { - ESP_LOGI(TAG, "PERIPH_NOTIFY_KEY_REC_QUIT"); - } - break; + if ((int)event->data == get_input_rec_id() && event->cmd == PERIPH_BUTTON_PRESSED) { + ESP_LOGI(TAG, "PERIPH_NOTIFY_KEY_REC"); + audio_recorder_trigger_start(recorder); + } else if ((int)event->data == get_input_mode_id() && ((event->cmd == PERIPH_BUTTON_RELEASE) || (event->cmd == PERIPH_BUTTON_LONG_RELEASE))) { + ESP_LOGI(TAG, "PERIPH_NOTIFY_KEY_REC_QUIT"); } + break; + } case PERIPH_ID_TOUCH: { - if ((int)event->data == TOUCH_PAD_NUM4 && event->cmd == PERIPH_BUTTON_PRESSED) { - - int player_volume = 0; - esp_audio_vol_get(player, &player_volume); - player_volume -= 10; - if (player_volume < 0) { - player_volume = 0; - } - esp_audio_vol_set(player, player_volume); - ESP_LOGI(TAG, "AUDIO_USER_KEY_VOL_DOWN [%d]", player_volume); - } else if ((int)event->data == TOUCH_PAD_NUM4 && (event->cmd == PERIPH_BUTTON_RELEASE)) { - - - } else if ((int)event->data == TOUCH_PAD_NUM7 && event->cmd == PERIPH_BUTTON_PRESSED) { - int player_volume = 0; - esp_audio_vol_get(player, &player_volume); - player_volume += 10; - if (player_volume > 100) { - player_volume = 100; - } - esp_audio_vol_set(player, player_volume); - ESP_LOGI(TAG, "AUDIO_USER_KEY_VOL_UP [%d]", player_volume); - } else if ((int)event->data == TOUCH_PAD_NUM7 && (event->cmd == PERIPH_BUTTON_RELEASE)) { + if ((int)event->data == TOUCH_PAD_NUM4 && event->cmd == PERIPH_BUTTON_PRESSED) { + int player_volume = 0; + esp_audio_vol_get(player, &player_volume); + player_volume -= 10; + if (player_volume < 0) { + player_volume = 0; + } + esp_audio_vol_set(player, player_volume); + ESP_LOGI(TAG, "AUDIO_USER_KEY_VOL_DOWN [%d]", player_volume); + } else if ((int)event->data == TOUCH_PAD_NUM4 && (event->cmd == PERIPH_BUTTON_RELEASE)) { - } else if ((int)event->data == TOUCH_PAD_NUM8 && event->cmd == PERIPH_BUTTON_PRESSED) { - ESP_LOGI(TAG, "AUDIO_USER_KEY_PLAY [%d]", __LINE__); + } else if ((int)event->data == TOUCH_PAD_NUM7 && event->cmd == PERIPH_BUTTON_PRESSED) { + int player_volume = 0; + esp_audio_vol_get(player, &player_volume); + player_volume += 10; + if (player_volume > 100) { + player_volume = 100; + } + esp_audio_vol_set(player, player_volume); + ESP_LOGI(TAG, "AUDIO_USER_KEY_VOL_UP [%d]", player_volume); + } else if ((int)event->data == TOUCH_PAD_NUM7 && (event->cmd == PERIPH_BUTTON_RELEASE)) { - } else if ((int)event->data == TOUCH_PAD_NUM8 && (event->cmd == PERIPH_BUTTON_RELEASE)) { + } else if ((int)event->data == TOUCH_PAD_NUM8 && event->cmd == PERIPH_BUTTON_PRESSED) { + ESP_LOGI(TAG, "AUDIO_USER_KEY_PLAY [%d]", __LINE__); + } else if ((int)event->data == TOUCH_PAD_NUM8 && (event->cmd == PERIPH_BUTTON_RELEASE)) { - } else if ((int)event->data == TOUCH_PAD_NUM9 && event->cmd == PERIPH_BUTTON_PRESSED) { - if (wifi_setting_flag == false) { + } else if ((int)event->data == TOUCH_PAD_NUM9 && event->cmd == PERIPH_BUTTON_PRESSED) { + if (wifi_setting_flag == false) { #ifdef CONFIG_DUER_WIFI_CONFIG - wifi_cfg_start(); + wifi_cfg_start(); #else - wifi_service_setting_start(wifi_serv, 0); -#endif // CONFIG_DUER_WIFI_CONFIG - wifi_setting_flag = true; - display_service_set_pattern(disp_serv, DISPLAY_PATTERN_WIFI_SETTING, 0); - ESP_LOGI(TAG, "AUDIO_USER_KEY_WIFI_SET, WiFi setting started."); - } else { - ESP_LOGW(TAG, "AUDIO_USER_KEY_WIFI_SET, WiFi setting will be stopped."); + wifi_service_setting_start(wifi_serv, 0); +#endif // CONFIG_DUER_WIFI_CONFIG + wifi_setting_flag = true; + display_service_set_pattern(disp_serv, DISPLAY_PATTERN_WIFI_SETTING, 0); + ESP_LOGI(TAG, "AUDIO_USER_KEY_WIFI_SET, WiFi setting started."); + } else { + ESP_LOGW(TAG, "AUDIO_USER_KEY_WIFI_SET, WiFi setting will be stopped."); #ifdef CONFIG_DUER_WIFI_CONFIG - wifi_cfg_stop(); + wifi_cfg_stop(); #else - wifi_service_setting_stop(wifi_serv, 0); -#endif // CONFIG_DUER_WIFI_CONFIG - wifi_setting_flag = false; - display_service_set_pattern(disp_serv, DISPLAY_PATTERN_TURN_OFF, 0); - } - } else if ((int)event->data == TOUCH_PAD_NUM9 && (event->cmd == PERIPH_BUTTON_RELEASE)) { - + wifi_service_setting_stop(wifi_serv, 0); +#endif // CONFIG_DUER_WIFI_CONFIG + wifi_setting_flag = false; + display_service_set_pattern(disp_serv, DISPLAY_PATTERN_TURN_OFF, 0); } - break; + } else if ((int)event->data == TOUCH_PAD_NUM9 && (event->cmd == PERIPH_BUTTON_RELEASE)) { } + break; + } case PERIPH_ID_ADC_BTN: if (((int)event->data == get_input_volup_id()) && (event->cmd == PERIPH_ADC_BUTTON_RELEASE)) { int player_volume = 0; @@ -460,10 +456,11 @@ esp_err_t periph_callback(audio_event_iface_msg_t *event, void *context) } else if (((int)event->data == get_input_set_id()) && (event->cmd == PERIPH_ADC_BUTTON_RELEASE)) { if (wifi_setting_flag == false) { #ifdef CONFIG_DUER_WIFI_CONFIG + wifi_service_disconnect(wifi_serv); wifi_cfg_start(); #else wifi_service_setting_start(wifi_serv, 0); -#endif // CONFIG_DUER_WIFI_CONFIG +#endif // CONFIG_DUER_WIFI_CONFIG wifi_setting_flag = true; display_service_set_pattern(disp_serv, DISPLAY_PATTERN_WIFI_SETTING, 0); ESP_LOGI(TAG, "AUDIO_USER_KEY_WIFI_SET, WiFi setting started."); @@ -473,7 +470,7 @@ esp_err_t periph_callback(audio_event_iface_msg_t *event, void *context) wifi_cfg_stop(); #else wifi_service_setting_stop(wifi_serv, 0); -#endif // CONFIG_DUER_WIFI_CONFIG +#endif // CONFIG_DUER_WIFI_CONFIG wifi_setting_flag = false; display_service_set_pattern(disp_serv, DISPLAY_PATTERN_TURN_OFF, 0); } @@ -483,7 +480,7 @@ esp_err_t periph_callback(audio_event_iface_msg_t *event, void *context) default: break; } -#endif // FUNC_BUTTON_EN +#endif // FUNC_BUTTON_EN return ESP_OK; } @@ -518,8 +515,7 @@ esp_err_t duer_init_hal(void *instance, action_arg_t *arg, action_result_t *resu .root = "/spiffs", .partition_label = "spiffs_data", .max_files = 5, - .format_if_mount_failed = true - }; + .format_if_mount_failed = true}; esp_periph_handle_t spiffs_handle = periph_spiffs_init(&spiffs_cfg); esp_periph_start(set, spiffs_handle); @@ -545,13 +541,13 @@ void duer_app_init(void) ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } - ESP_ERROR_CHECK( ret ); + ESP_ERROR_CHECK(ret); esp_dispatcher = esp_dispatcher_get_delegate_handle(); - action_result_t result = { 0 }; + action_result_t result = {0}; esp_dispatcher_execute_with_func(esp_dispatcher, duer_init_hal, NULL, NULL, &result); xTimerHandle retry_login_timer = xTimerCreate("tm_duer_login", 1000 / portTICK_PERIOD_MS, - pdFALSE, NULL, retry_login_timer_cb); + pdFALSE, NULL, retry_login_timer_cb); duer_serv_handle = dueros_service_create(); audio_service_set_callback(duer_serv_handle, duer_callback, retry_login_timer); @@ -577,8 +573,8 @@ void duer_app_init(void) esp_wifi_setting_register_notify_handle(h, (void *)wifi_serv); wifi_service_register_setting_handle(wifi_serv, h, ®_idx); #elif (defined CONFIG_DUER_WIFI_CONFIG) - int32_t profile_state = duer_profile_certified(); - bool wifi_configured = wifi_has_cfg_saved(); + int32_t profile_state = duer_profile_certified(); + bool wifi_configured = wifi_has_cfg_saved(); ESP_LOGI(TAG, "profile state %d, wifi_configured %d", (int)profile_state, (int)wifi_configured); if (profile_state == 1 || profile_state == 2) { ESP_LOGW(TAG, "\nPlease fill ${ADF_PATH}/components/dueros_service/duer_profile and enable CONFIG_DUEROS_GEN_PROFILE & CONFIG_DUEROS_FLASH_PROFILE in sdkconfig\n");