chore: checkpoint 第一版
ci / verify (push) Canceled after 0s

This commit is contained in:
Codex Local
2026-09-06 17:04:11 +08:00
parent a8cdec98d1
commit 0c2151ace9
659 changed files with 185855 additions and 554 deletions
@@ -0,0 +1,381 @@
// Copyright (2025) Beijing Volcano Engine Technology Ltd.
// SPDX-License-Identifier: Apache-2.0
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <inttypes.h>
#include "esp_board_manager.h"
#include "dev_lcd_touch_i2c.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_netif.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_heap_task_info.h"
#include "esp_random.h"
#include "esp_sntp.h"
#include "freertos/semphr.h"
#include "esp_err.h"
#include "sdkconfig.h"
// #include "periph_wifi.h"
// #include "fatfs_stream.h"
#include "cJSON.h"
#include "network.h"
#include "volc_manager_service.h"
#include "volc_service_common.h"
#include "volc_conv_service.h"
#include "local_logic_service.h"
#include "volc_function_call_service.h"
#include "volc_hal.h"
#include "volc_hal_display.h"
#include "volc_hal_capture.h"
#include "volc_lvgl_source.h"
// #include "board.h"
#include "esp_mac.h"
#include "esp_gmf_afe.h"
#include "mmap_generate_eaf.h"
#include "platform_client.h"
#include "hal/usb_serial_jtag_ll.h"
#define STATS_TASK_PRIO 5
static const char *TAG = "VolcConvAI";
static volatile bool is_interrupt = false;
static volatile bool conversation_service_ready = false;
extern void conv_ai_service_diagnostic_say(void);
/* A local USB diagnostic command lets service tests run without repeated taps.
* It does not run automatically or accept commands over the network. */
static void usb_diagnostic_task(void *arg)
{
char command[32];
size_t length = 0;
while (true) {
while (usb_serial_jtag_ll_rxfifo_data_available()) {
uint8_t ch;
if (!usb_serial_jtag_ll_read_rxfifo(&ch, 1)) break;
if (ch == '\n' || ch == '\r') {
command[length] = 0;
if (conversation_service_ready && !strcmp(command, "diag-say")) {
ESP_LOGI(TAG, "USB diagnostic: requesting fixed spoken sentence");
conv_ai_service_diagnostic_say();
}
length = 0;
} else if (length < sizeof(command) - 1) {
command[length++] = ch;
} else {
length = 0;
}
}
vTaskDelay(pdMS_TO_TICKS(20));
}
}
void session_init(){
volc_service_manager_init();
volc_conv_service_manager_init();
function_call_service_init();
local_logic_service_init();
}
void Task(void* data){
aios_init(VOLC_SERVICE_EVENT_MAX); // AIOS初始化
session_init();
conversation_service_ready = true;
xTaskCreate(usb_diagnostic_task, "usb_diagnostic", 3072, NULL, 3, NULL);
aios_run(); // AIOS启动
return;
}
static void sys_monitor_task(void *pvParameters)
{
static char run_info[1024] = {0};
while (1)
{
// vTaskGetRunTimeStats(run_info);
vTaskList(run_info);
printf("Task Runtime Stats:\n%s", run_info);
printf("--------------------------------\n");
// ESP_LOGI(TAG, "Task Runtime Stats:\n%s", run_info);
// ESP_LOGI(TAG, "--------------------------------\n");
vTaskDelay(pdMS_TO_TICKS(5000));
}
}
void initialize_sntp(void)
{
ESP_LOGI(TAG, "Initializing SNTP");
// 设置SNTP操作模式为轮询(客户端)
esp_sntp_setoperatingmode(SNTP_OPMODE_POLL);
// 设置NTP服务器(可以设置多个备用)
esp_sntp_setservername(0, "pool.ntp.org"); // 主服务器
esp_sntp_setservername(1, "cn.pool.ntp.org"); // 备用服务器:中国区
esp_sntp_setservername(2, "ntp1.aliyun.com"); // 备用服务器:阿里云
// 初始化SNTP服务
esp_sntp_init();
// 设置时区(例如北京时间CST-8)
setenv("TZ", "CST-8", 1);
tzset();
}
void wait_for_time_sync(void)
{
ESP_LOGI(TAG, "Waiting for time synchronization...");
// 检查时间是否已同步(1970年之后)
time_t now = 0;
struct tm timeinfo = {0};
int retry_count = 5; // 最大重试次数
while (timeinfo.tm_year < (2024 - 1900) && retry_count > 0)
{
ESP_LOGI(TAG, "Time not set yet, retrying... (%d)", retry_count);
vTaskDelay(1000 / portTICK_PERIOD_MS);
time(&now);
localtime_r(&now, &timeinfo);
retry_count--;
}
if (retry_count > 0)
{
// print_current_time();
}
else
{
ESP_LOGE(TAG, "Failed to synchronize time within timeout period");
}
}
static void rec_engine_cb(void *event, void *user_data)
{
esp_gmf_afe_evt_t * e = (esp_gmf_afe_evt_t *)event;
if (ESP_GMF_AFE_EVT_WAKEUP_START == e->type) {
if (conversation_service_ready) {
ESP_LOGI(TAG, "Wake word detected; requesting conversation");
volc_hal_context_t *ctx = volc_get_global_hal_context();
if (ctx && ctx->display_handle) {
int index = MMAP_EAF_EMOTION_BLINK_FAST_284_126_EAF;
volc_hal_display_set_content(ctx->display_handle, VOLC_DISPLAY_OBJ_MAIN,
VOLC_DISPLAY_IMAGE, &index);
}
aios_event_pub(VOLC_SERVICE_AI_CONVERSATION,NULL,NULL);
}
}
return;
}
static void wifi_setup_touch_task(void *arg)
{
dev_lcd_touch_i2c_handles_t *touch = arg;
TickType_t pressed_at = 0;
TickType_t last_conversation_request = 0;
uint16_t pressed_x = 0;
uint16_t pressed_y = 0;
bool holding = false;
bool setup_triggered = false;
for (;;) {
uint16_t x = 0, y = 0;
uint8_t count = 0;
bool pressed = esp_lcd_touch_read_data(touch->touch_handle) == ESP_OK &&
esp_lcd_touch_get_coordinates(touch->touch_handle, &x, &y, NULL, &count, 1) &&
count > 0;
if (!pressed) {
if (holding && !setup_triggered) {
TickType_t held_for = xTaskGetTickCount() - pressed_at;
ESP_LOGI(TAG, "Touch released at (%u,%u) after %u ms",
pressed_x, pressed_y, (unsigned)(held_for * portTICK_PERIOD_MS));
if (held_for >= pdMS_TO_TICKS(50) &&
held_for < pdMS_TO_TICKS(1500) && conversation_service_ready) {
TickType_t now = xTaskGetTickCount();
if (last_conversation_request != 0 &&
now - last_conversation_request < pdMS_TO_TICKS(5000)) {
ESP_LOGI(TAG, "Ignoring repeated conversation tap");
holding = false;
vTaskDelay(pdMS_TO_TICKS(50));
continue;
}
last_conversation_request = now;
ESP_LOGI(TAG, "Screen tapped; requesting conversation");
volc_hal_context_t *ctx = volc_get_global_hal_context();
if (ctx && ctx->display_handle) {
int index = MMAP_EAF_EMOTION_BLINK_FAST_284_126_EAF;
volc_hal_display_set_content(ctx->display_handle, VOLC_DISPLAY_OBJ_MAIN,
VOLC_DISPLAY_IMAGE, &index);
}
aios_event_pub(VOLC_SERVICE_AI_CONVERSATION, NULL, NULL);
}
}
holding = false;
setup_triggered = false;
} else if (!holding) {
holding = true;
pressed_at = xTaskGetTickCount();
pressed_x = x;
pressed_y = y;
} else if (!setup_triggered && pressed_y >= 280 && pressed_y <= 335 &&
pressed_x >= 60 && pressed_x <= 300 &&
(TickType_t)(xTaskGetTickCount() - pressed_at) >= pdMS_TO_TICKS(3000)) {
nvs_handle_t nvs;
esp_err_t ret = nvs_open("wifi", NVS_READWRITE, &nvs);
if (ret == ESP_OK) {
ret = nvs_set_u8(nvs, "setup_requested", 1);
if (ret == ESP_OK) ret = nvs_commit(nvs);
nvs_close(nvs);
}
if (ret == ESP_OK) {
ESP_LOGI(TAG, "Restarting into setup; device identity and Wi-Fi preserved");
setup_triggered = true;
esp_restart();
}
ESP_LOGE(TAG, "Cannot request setup: %s", esp_err_to_name(ret));
holding = false;
}
vTaskDelay(pdMS_TO_TICKS(50));
}
}
static void wifi_setup_button_init(void)
{
dev_lcd_touch_i2c_handles_t *touch = NULL;
esp_err_t ret = esp_board_manager_get_device_handle("lcd_touch", (void **)&touch);
if (ret != ESP_OK || !touch || !touch->touch_handle) {
ESP_LOGE(TAG, "Wi-Fi setup touch input unavailable");
return;
}
if (xTaskCreate(wifi_setup_touch_task, "wifi_setup", 4096, touch, 5, NULL) != pdPASS) {
ESP_LOGE(TAG, "Cannot start Wi-Fi setup touch task");
}
}
void hal_level_init()
{
volc_hal_context_t* g_hal_context = volc_get_global_hal_context();
volc_hal_capture_config_t config = {0};
config.media_type = VOLC_MEDIA_TYPE_AUDIO;
config.data_cb = NULL;
config.user_data = g_hal_context;
config.audio_wakeup_cb = (volc_hal_audio_wakeup_cb_t)rec_engine_cb;
volc_hal_capture_t capture = volc_hal_capture_create(&config);
volc_hal_capture_start(capture,VOLC_AUDIO_MODE_WAKEUP);
// Allow other core to finish initialization
// vTaskDelay(pdMS_TO_TICKS(2000));
}
static void application_boot(void)
{
// xTaskCreate(&sys_monitor_task, "sys_monitor_task", 4096, NULL, 5, NULL);
volc_hal_init();
volc_hal_display_config_t display_config = {0};
volc_hal_display_create(&display_config);
volc_hal_context_t* g_hal_context = volc_get_global_hal_context();
if(g_hal_context == NULL){
return;
}
volc_hal_display_t global_display = g_hal_context->display_handle;
/* Initialize the default event loop */
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_log_level_set("*", ESP_LOG_WARN);
esp_log_level_set("*", ESP_LOG_INFO);
// /* Initialize NVS flash for WiFi configuration */
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
ESP_LOGW(TAG, "Erasing NVS flash to fix corruption");
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_ERROR_CHECK(platform_client_init());
ESP_ERROR_CHECK(esp_netif_init());
wifi_setup_button_init();
uint8_t station_mac[6] = {0};
char setup_status[96] = "等待配网";
if (esp_read_mac(station_mac, ESP_MAC_WIFI_STA) == ESP_OK) {
snprintf(setup_status, sizeof(setup_status),
"WiFi: VolcConvAI-%02X%02X%02X",
station_mac[3], station_mac[4], station_mac[5]);
}
volc_hal_display_set_content(global_display, VOLC_DISPLAY_OBJ_STATUS,
VOLC_DISPLAY_TEXT, setup_status);
bool connected = configure_network();
if (connected == false)
{
volc_hal_display_set_content(global_display, VOLC_DISPLAY_OBJ_STATUS,
VOLC_DISPLAY_TEXT, setup_status);
volc_hal_display_set_content(global_display, VOLC_DISPLAY_OBJ_SUBTITLE,
VOLC_DISPLAY_TEXT, "192.168.4.1 密码同热点名");
ESP_LOGI(TAG, "Waiting for setup through the Wi-Fi portal");
return;
}
initialize_sntp();
wait_for_time_sync();
if (!platform_identity_is_configured()) {
ESP_LOGE(TAG, "Platform identity is missing; erase Wi-Fi NVS or use the setup portal");
volc_hal_display_set_content(global_display, VOLC_DISPLAY_OBJ_STATUS, VOLC_DISPLAY_TEXT,
"请通过配网页填写平台设备凭据");
return;
}
volc_hal_display_set_content(global_display, VOLC_DISPLAY_OBJ_STATUS, VOLC_DISPLAY_TEXT,
"网络已连接,正在激活");
ret = platform_provision_volc_credential();
ESP_LOGI(TAG, "Startup stack headroom after activation: %u bytes", (unsigned)uxTaskGetStackHighWaterMark(NULL));
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Platform provider provisioning failed: %s", esp_err_to_name(ret));
const char *code = platform_last_error_code();
const char *message = "平台激活失败,请检查配置";
if (strcmp(code, "DEVICE_ACTIVATION_REQUIRED") == 0) message = "请先在平台绑定家庭和长者";
else if (strcmp(code, "HARDWARE_AGENT_BINDING_REQUIRED") == 0) message = "请在平台配置硬件智能体";
else if (strcmp(code, "HARDWARE_AGENT_PRODUCT_MISMATCH") == 0) message = "平台产品配置不一致";
else if (strcmp(code, "HARDWARE_AGENT_HARDWARE_MISMATCH") == 0) message = "设备凭据与本机不匹配";
volc_hal_display_set_content(global_display, VOLC_DISPLAY_OBJ_STATUS, VOLC_DISPLAY_TEXT, message);
return;
}
hal_level_init();
ESP_LOGI(TAG, "Startup stack headroom after audio: %u bytes", (unsigned)uxTaskGetStackHighWaterMark(NULL));
xTaskCreate(&Task, "aios_task", 8192, NULL, 5, NULL);
int index = MMAP_EAF_HAPPY_EAF;
volc_hal_display_set_content(global_display,VOLC_DISPLAY_OBJ_MAIN,VOLC_DISPLAY_IMAGE,&index);
volc_hal_display_set_content(global_display,VOLC_DISPLAY_OBJ_STATUS,VOLC_DISPLAY_TEXT,"轻触屏幕或说嗨,乐鑫");
}
/* Network signing/TLS and audio startup exceed the default 4 KiB main stack.
* Keep this memory available only during startup and free it on every exit. */
static void application_boot_task(void *arg)
{
application_boot();
ESP_LOGI(TAG, "Startup task finished; stack headroom: %u bytes",
(unsigned)uxTaskGetStackHighWaterMark(NULL));
vTaskDelete(NULL);
}
void app_main(void)
{
if (xTaskCreate(application_boot_task, "platform_boot", 24 * 1024,
NULL, 5, NULL) != pdPASS) {
ESP_LOGE(TAG, "Cannot allocate platform startup task");
}
}