feat: default to flash non assets

This commit is contained in:
Terrence
2025-09-10 01:02:23 +08:00
parent d04b08133f
commit bce662d135
4 changed files with 99 additions and 56 deletions

View File

@@ -1,17 +1,19 @@
# Define default assets files (Absolute url starting with http or https is supported) # Define default assets files (Absolute url starting with http or https is supported)
set(ASSETS_PUHUI_COMMON_14_1 "none-font_puhui_common_14_1-none.bin") set(ASSETS_URL_PREFIX "https://files.xiaozhi.me/assets/default/")
set(ASSETS_XIAOZHI_WAKENET_ONLY "wn9_nihaoxiaozhi_tts-none-none.bin")
set(ASSETS_XIAOZHI_PUHUI_COMMON_14_1 "wn9_nihaoxiaozhi_tts-font_puhui_common_14_1-none.bin") set(ASSETS_PUHUI_COMMON_14_1 "${ASSETS_URL_PREFIX}none-font_puhui_common_14_1-none.bin")
set(ASSETS_XIAOZHI_PUHUI_COMMON_16_4_EMOJI_32 "wn9_nihaoxiaozhi_tts-font_puhui_common_16_4-emojis_32.bin") set(ASSETS_XIAOZHI_WAKENET_ONLY "${ASSETS_URL_PREFIX}wn9_nihaoxiaozhi_tts-none-none.bin")
set(ASSETS_XIAOZHI_PUHUI_COMMON_16_4_EMOJI_64 "wn9_nihaoxiaozhi_tts-font_puhui_common_16_4-emojis_64.bin") set(ASSETS_XIAOZHI_PUHUI_COMMON_14_1 "${ASSETS_URL_PREFIX}wn9_nihaoxiaozhi_tts-font_puhui_common_14_1-none.bin")
set(ASSETS_XIAOZHI_PUHUI_COMMON_20_4_EMOJI_64 "wn9_nihaoxiaozhi_tts-font_puhui_common_20_4-emojis_64.bin") set(ASSETS_XIAOZHI_PUHUI_COMMON_16_4_EMOJI_32 "${ASSETS_URL_PREFIX}wn9_nihaoxiaozhi_tts-font_puhui_common_16_4-emojis_32.bin")
set(ASSETS_XIAOZHI_PUHUI_COMMON_30_4_EMOJI_64 "wn9_nihaoxiaozhi_tts-font_puhui_common_30_4-emojis_64.bin") set(ASSETS_XIAOZHI_PUHUI_COMMON_16_4_EMOJI_64 "${ASSETS_URL_PREFIX}wn9_nihaoxiaozhi_tts-font_puhui_common_16_4-emojis_64.bin")
set(ASSETS_XIAOZHI_S_WAKENET_ONLY "wn9s_nihaoxiaozhi-none-none.bin") set(ASSETS_XIAOZHI_PUHUI_COMMON_20_4_EMOJI_64 "${ASSETS_URL_PREFIX}wn9_nihaoxiaozhi_tts-font_puhui_common_20_4-emojis_64.bin")
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_14_1 "wn9s_nihaoxiaozhi-font_puhui_common_14_1-none.bin") set(ASSETS_XIAOZHI_PUHUI_COMMON_30_4_EMOJI_64 "${ASSETS_URL_PREFIX}wn9_nihaoxiaozhi_tts-font_puhui_common_30_4-emojis_64.bin")
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_16_4_EMOJI_32 "wn9s_nihaoxiaozhi-font_puhui_common_16_4-emojis_32.bin") set(ASSETS_XIAOZHI_S_WAKENET_ONLY "${ASSETS_URL_PREFIX}wn9s_nihaoxiaozhi-none-none.bin")
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_20_4_EMOJI_32 "wn9s_nihaoxiaozhi-font_puhui_common_20_4-emojis_32.bin") set(ASSETS_XIAOZHI_S_PUHUI_COMMON_14_1 "${ASSETS_URL_PREFIX}wn9s_nihaoxiaozhi-font_puhui_common_14_1-none.bin")
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_20_4_EMOJI_64 "wn9s_nihaoxiaozhi-font_puhui_common_20_4-emojis_64.bin") set(ASSETS_XIAOZHI_S_PUHUI_COMMON_16_4_EMOJI_32 "${ASSETS_URL_PREFIX}wn9s_nihaoxiaozhi-font_puhui_common_16_4-emojis_32.bin")
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_30_4_EMOJI_64 "wn9s_nihaoxiaozhi-font_puhui_common_30_4-emojis_64.bin") set(ASSETS_XIAOZHI_S_PUHUI_COMMON_20_4_EMOJI_32 "${ASSETS_URL_PREFIX}wn9s_nihaoxiaozhi-font_puhui_common_20_4-emojis_32.bin")
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_20_4_EMOJI_64 "${ASSETS_URL_PREFIX}wn9s_nihaoxiaozhi-font_puhui_common_20_4-emojis_64.bin")
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_30_4_EMOJI_64 "${ASSETS_URL_PREFIX}wn9s_nihaoxiaozhi-font_puhui_common_30_4-emojis_64.bin")
# Embedded font files defined in `xiaozhi-fonts` component # Embedded font files defined in `xiaozhi-fonts` component
# Basic fonts include ASCII and about 600 characters used in assets/locales # Basic fonts include ASCII and about 600 characters used in assets/locales
@@ -743,43 +745,76 @@ if(DEFAULT_ASSETS)
validate_default_assets_prefix("${BOARD_TYPE}" "${DEFAULT_ASSETS}") validate_default_assets_prefix("${BOARD_TYPE}" "${DEFAULT_ASSETS}")
endif() endif()
# Download and flash DEFAULT_ASSETS if defined and enabled # Function to get local assets file path (handles both URL and local file)
if(DEFAULT_ASSETS AND CONFIG_FLASH_DEFAULT_ASSETS) function(get_assets_local_file assets_source assets_local_file_var)
# Construct the full URL # Check if it's a URL (starts with http:// or https://)
set(ASSETS_URL "${CONFIG_DEFAULT_ASSETS_URL_PREFIX}${DEFAULT_ASSETS}") if(assets_source MATCHES "^https?://")
set(ASSETS_LOCAL_FILE "${CMAKE_BINARY_DIR}/${DEFAULT_ASSETS}") # It's a URL, download it
set(ASSETS_TEMP_FILE "${CMAKE_BINARY_DIR}/${DEFAULT_ASSETS}.tmp") get_filename_component(ASSETS_FILENAME "${assets_source}" NAME)
set(ASSETS_LOCAL_FILE "${CMAKE_BINARY_DIR}/${ASSETS_FILENAME}")
# Check if local file exists set(ASSETS_TEMP_FILE "${CMAKE_BINARY_DIR}/${ASSETS_FILENAME}.tmp")
if(EXISTS ${ASSETS_LOCAL_FILE})
message(STATUS "Assets file ${DEFAULT_ASSETS} already exists, skipping download")
else()
message(STATUS "Downloading ${DEFAULT_ASSETS}")
# Clean up any existing temp file # Check if local file exists
if(EXISTS ${ASSETS_TEMP_FILE}) if(EXISTS ${ASSETS_LOCAL_FILE})
file(REMOVE ${ASSETS_TEMP_FILE}) message(STATUS "Assets file ${ASSETS_FILENAME} already exists, skipping download")
endif() else()
message(STATUS "Downloading ${ASSETS_FILENAME}")
# Download to temporary file first
file(DOWNLOAD ${ASSETS_URL} ${ASSETS_TEMP_FILE} # Clean up any existing temp file
STATUS DOWNLOAD_STATUS)
list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
if(NOT STATUS_CODE EQUAL 0)
# Clean up temp file on failure
if(EXISTS ${ASSETS_TEMP_FILE}) if(EXISTS ${ASSETS_TEMP_FILE})
file(REMOVE ${ASSETS_TEMP_FILE}) file(REMOVE ${ASSETS_TEMP_FILE})
endif() endif()
message(FATAL_ERROR "Failed to download ${DEFAULT_ASSETS} from ${ASSETS_URL}")
# Download to temporary file first
file(DOWNLOAD ${assets_source} ${ASSETS_TEMP_FILE}
STATUS DOWNLOAD_STATUS)
list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
if(NOT STATUS_CODE EQUAL 0)
# Clean up temp file on failure
if(EXISTS ${ASSETS_TEMP_FILE})
file(REMOVE ${ASSETS_TEMP_FILE})
endif()
message(FATAL_ERROR "Failed to download ${ASSETS_FILENAME} from ${assets_source}")
endif()
# Move temp file to final location (atomic operation)
file(RENAME ${ASSETS_TEMP_FILE} ${ASSETS_LOCAL_FILE})
message(STATUS "Successfully downloaded ${ASSETS_FILENAME}")
endif()
else()
# It's a local file path
if(IS_ABSOLUTE "${assets_source}")
set(ASSETS_LOCAL_FILE "${assets_source}")
else()
set(ASSETS_LOCAL_FILE "${CMAKE_CURRENT_SOURCE_DIR}/${assets_source}")
endif() endif()
# Move temp file to final location (atomic operation) # Check if local file exists
file(RENAME ${ASSETS_TEMP_FILE} ${ASSETS_LOCAL_FILE}) if(NOT EXISTS ${ASSETS_LOCAL_FILE})
message(STATUS "Successfully downloaded ${DEFAULT_ASSETS}") message(FATAL_ERROR "Assets file not found: ${ASSETS_LOCAL_FILE}")
endif()
message(STATUS "Using assets file: ${ASSETS_LOCAL_FILE}")
endif() endif()
# Flash assets to partition using esptool_py_flash_to_partition set(${assets_local_file_var} ${ASSETS_LOCAL_FILE} PARENT_SCOPE)
endfunction()
# Flash assets based on configuration
if(CONFIG_FLASH_DEFAULT_ASSETS)
# Flash default assets
if(DEFAULT_ASSETS)
get_assets_local_file("${DEFAULT_ASSETS}" ASSETS_LOCAL_FILE)
esptool_py_flash_to_partition(flash "assets" "${ASSETS_LOCAL_FILE}")
message(STATUS "Default assets download and flash configured: ${DEFAULT_ASSETS} -> assets partition")
else()
message(WARNING "FLASH_DEFAULT_ASSETS is enabled but no DEFAULT_ASSETS is defined for board ${BOARD_TYPE}")
endif()
elseif(CONFIG_FLASH_CUSTOM_ASSETS)
# Flash custom assets
get_assets_local_file("${CONFIG_CUSTOM_ASSETS_FILE}" ASSETS_LOCAL_FILE)
esptool_py_flash_to_partition(flash "assets" "${ASSETS_LOCAL_FILE}") esptool_py_flash_to_partition(flash "assets" "${ASSETS_LOCAL_FILE}")
message(STATUS "Custom assets flash configured: ${ASSETS_LOCAL_FILE} -> assets partition")
message(STATUS "Assets download and flash configured: ${ASSETS_URL} -> assets partition") elseif(CONFIG_FLASH_NONE_ASSETS)
message(STATUS "Assets flashing disabled (FLASH_NONE_ASSETS)")
endif() endif()

View File

@@ -6,18 +6,27 @@ config OTA_URL
help help
The application will access this URL to check for new firmwares and server address. The application will access this URL to check for new firmwares and server address.
config DEFAULT_ASSETS_URL_PREFIX choice
string "Default Assets URL Prefix" prompt "Flash Assets"
default "https://files.xiaozhi.me/assets/default/" default FLASH_NONE_ASSETS
help help
The assets will be downloaded from this URL. Select the assets to flash.
config FLASH_DEFAULT_ASSETS config FLASH_NONE_ASSETS
bool "Flash Default Assets" bool "Do not flash assets"
default y config FLASH_DEFAULT_ASSETS
bool "Flash Default Assets"
config FLASH_CUSTOM_ASSETS
bool "Flash Custom Assets"
endchoice
config CUSTOM_ASSETS_FILE
depends on FLASH_CUSTOM_ASSETS
string "Custom Assets File"
default "assets.bin"
help help
Download and flash default assets to the assets partition during build. The custom assets file to flash.
If disabled, the assets partition will not be automatically populated. It can be a local file relative to the project directory or a remote url.
choice choice
prompt "Default Language" prompt "Default Language"

View File

@@ -21,11 +21,10 @@ struct mmap_assets_table {
Assets::Assets(std::string default_assets_url) { Assets::Assets(std::string default_assets_url) {
// If the default_assets_url is not a http url, add the prefix
if (default_assets_url.find("http") == 0) { if (default_assets_url.find("http") == 0) {
default_assets_url_ = default_assets_url; default_assets_url_ = default_assets_url;
} else { } else {
default_assets_url_ = CONFIG_DEFAULT_ASSETS_URL_PREFIX + default_assets_url; ESP_LOGE(TAG, "The default assets url is not a http url: %s", default_assets_url.c_str());
} }
// Initialize the partition // Initialize the partition

View File

@@ -105,8 +105,8 @@ def release(board_type, board_config, config_filename="config.json"):
# Append sdkconfig # Append sdkconfig
with open("sdkconfig", "a") as f: with open("sdkconfig", "a") as f:
f.write("\n") f.write("\n")
f.write("# Release\n") f.write("# Append by release.py\n")
f.write("CONFIG_FLASH_DEFAULT_ASSETS=n\n") f.write("CONFIG_FLASH_NONE_ASSETS=y\n")
for append in sdkconfig_append: for append in sdkconfig_append:
f.write(f"{append}\n") f.write(f"{append}\n")
# Build with macro BOARD_NAME defined to name # Build with macro BOARD_NAME defined to name