Skip to content

Commit bce662d

Browse files
committed
feat: default to flash non assets
1 parent d04b081 commit bce662d

File tree

4 files changed

+99
-56
lines changed

4 files changed

+99
-56
lines changed

main/CMakeLists.txt

Lines changed: 78 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
# Define default assets files (Absolute url starting with http or https is supported)
2-
set(ASSETS_PUHUI_COMMON_14_1 "none-font_puhui_common_14_1-none.bin")
3-
set(ASSETS_XIAOZHI_WAKENET_ONLY "wn9_nihaoxiaozhi_tts-none-none.bin")
4-
set(ASSETS_XIAOZHI_PUHUI_COMMON_14_1 "wn9_nihaoxiaozhi_tts-font_puhui_common_14_1-none.bin")
5-
set(ASSETS_XIAOZHI_PUHUI_COMMON_16_4_EMOJI_32 "wn9_nihaoxiaozhi_tts-font_puhui_common_16_4-emojis_32.bin")
6-
set(ASSETS_XIAOZHI_PUHUI_COMMON_16_4_EMOJI_64 "wn9_nihaoxiaozhi_tts-font_puhui_common_16_4-emojis_64.bin")
7-
set(ASSETS_XIAOZHI_PUHUI_COMMON_20_4_EMOJI_64 "wn9_nihaoxiaozhi_tts-font_puhui_common_20_4-emojis_64.bin")
8-
set(ASSETS_XIAOZHI_PUHUI_COMMON_30_4_EMOJI_64 "wn9_nihaoxiaozhi_tts-font_puhui_common_30_4-emojis_64.bin")
9-
set(ASSETS_XIAOZHI_S_WAKENET_ONLY "wn9s_nihaoxiaozhi-none-none.bin")
10-
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_14_1 "wn9s_nihaoxiaozhi-font_puhui_common_14_1-none.bin")
11-
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_16_4_EMOJI_32 "wn9s_nihaoxiaozhi-font_puhui_common_16_4-emojis_32.bin")
12-
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_20_4_EMOJI_32 "wn9s_nihaoxiaozhi-font_puhui_common_20_4-emojis_32.bin")
13-
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_20_4_EMOJI_64 "wn9s_nihaoxiaozhi-font_puhui_common_20_4-emojis_64.bin")
14-
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_30_4_EMOJI_64 "wn9s_nihaoxiaozhi-font_puhui_common_30_4-emojis_64.bin")
2+
set(ASSETS_URL_PREFIX "https://files.xiaozhi.me/assets/default/")
3+
4+
set(ASSETS_PUHUI_COMMON_14_1 "${ASSETS_URL_PREFIX}none-font_puhui_common_14_1-none.bin")
5+
set(ASSETS_XIAOZHI_WAKENET_ONLY "${ASSETS_URL_PREFIX}wn9_nihaoxiaozhi_tts-none-none.bin")
6+
set(ASSETS_XIAOZHI_PUHUI_COMMON_14_1 "${ASSETS_URL_PREFIX}wn9_nihaoxiaozhi_tts-font_puhui_common_14_1-none.bin")
7+
set(ASSETS_XIAOZHI_PUHUI_COMMON_16_4_EMOJI_32 "${ASSETS_URL_PREFIX}wn9_nihaoxiaozhi_tts-font_puhui_common_16_4-emojis_32.bin")
8+
set(ASSETS_XIAOZHI_PUHUI_COMMON_16_4_EMOJI_64 "${ASSETS_URL_PREFIX}wn9_nihaoxiaozhi_tts-font_puhui_common_16_4-emojis_64.bin")
9+
set(ASSETS_XIAOZHI_PUHUI_COMMON_20_4_EMOJI_64 "${ASSETS_URL_PREFIX}wn9_nihaoxiaozhi_tts-font_puhui_common_20_4-emojis_64.bin")
10+
set(ASSETS_XIAOZHI_PUHUI_COMMON_30_4_EMOJI_64 "${ASSETS_URL_PREFIX}wn9_nihaoxiaozhi_tts-font_puhui_common_30_4-emojis_64.bin")
11+
set(ASSETS_XIAOZHI_S_WAKENET_ONLY "${ASSETS_URL_PREFIX}wn9s_nihaoxiaozhi-none-none.bin")
12+
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_14_1 "${ASSETS_URL_PREFIX}wn9s_nihaoxiaozhi-font_puhui_common_14_1-none.bin")
13+
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_16_4_EMOJI_32 "${ASSETS_URL_PREFIX}wn9s_nihaoxiaozhi-font_puhui_common_16_4-emojis_32.bin")
14+
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_20_4_EMOJI_32 "${ASSETS_URL_PREFIX}wn9s_nihaoxiaozhi-font_puhui_common_20_4-emojis_32.bin")
15+
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_20_4_EMOJI_64 "${ASSETS_URL_PREFIX}wn9s_nihaoxiaozhi-font_puhui_common_20_4-emojis_64.bin")
16+
set(ASSETS_XIAOZHI_S_PUHUI_COMMON_30_4_EMOJI_64 "${ASSETS_URL_PREFIX}wn9s_nihaoxiaozhi-font_puhui_common_30_4-emojis_64.bin")
1517

1618
# Embedded font files defined in `xiaozhi-fonts` component
1719
# Basic fonts include ASCII and about 600 characters used in assets/locales
@@ -743,43 +745,76 @@ if(DEFAULT_ASSETS)
743745
validate_default_assets_prefix("${BOARD_TYPE}" "${DEFAULT_ASSETS}")
744746
endif()
745747

746-
# Download and flash DEFAULT_ASSETS if defined and enabled
747-
if(DEFAULT_ASSETS AND CONFIG_FLASH_DEFAULT_ASSETS)
748-
# Construct the full URL
749-
set(ASSETS_URL "${CONFIG_DEFAULT_ASSETS_URL_PREFIX}${DEFAULT_ASSETS}")
750-
set(ASSETS_LOCAL_FILE "${CMAKE_BINARY_DIR}/${DEFAULT_ASSETS}")
751-
set(ASSETS_TEMP_FILE "${CMAKE_BINARY_DIR}/${DEFAULT_ASSETS}.tmp")
752-
753-
# Check if local file exists
754-
if(EXISTS ${ASSETS_LOCAL_FILE})
755-
message(STATUS "Assets file ${DEFAULT_ASSETS} already exists, skipping download")
756-
else()
757-
message(STATUS "Downloading ${DEFAULT_ASSETS}")
758-
759-
# Clean up any existing temp file
760-
if(EXISTS ${ASSETS_TEMP_FILE})
761-
file(REMOVE ${ASSETS_TEMP_FILE})
762-
endif()
748+
# Function to get local assets file path (handles both URL and local file)
749+
function(get_assets_local_file assets_source assets_local_file_var)
750+
# Check if it's a URL (starts with http:// or https://)
751+
if(assets_source MATCHES "^https?://")
752+
# It's a URL, download it
753+
get_filename_component(ASSETS_FILENAME "${assets_source}" NAME)
754+
set(ASSETS_LOCAL_FILE "${CMAKE_BINARY_DIR}/${ASSETS_FILENAME}")
755+
set(ASSETS_TEMP_FILE "${CMAKE_BINARY_DIR}/${ASSETS_FILENAME}.tmp")
763756

764-
# Download to temporary file first
765-
file(DOWNLOAD ${ASSETS_URL} ${ASSETS_TEMP_FILE}
766-
STATUS DOWNLOAD_STATUS)
767-
list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
768-
if(NOT STATUS_CODE EQUAL 0)
769-
# Clean up temp file on failure
757+
# Check if local file exists
758+
if(EXISTS ${ASSETS_LOCAL_FILE})
759+
message(STATUS "Assets file ${ASSETS_FILENAME} already exists, skipping download")
760+
else()
761+
message(STATUS "Downloading ${ASSETS_FILENAME}")
762+
763+
# Clean up any existing temp file
770764
if(EXISTS ${ASSETS_TEMP_FILE})
771765
file(REMOVE ${ASSETS_TEMP_FILE})
772766
endif()
773-
message(FATAL_ERROR "Failed to download ${DEFAULT_ASSETS} from ${ASSETS_URL}")
767+
768+
# Download to temporary file first
769+
file(DOWNLOAD ${assets_source} ${ASSETS_TEMP_FILE}
770+
STATUS DOWNLOAD_STATUS)
771+
list(GET DOWNLOAD_STATUS 0 STATUS_CODE)
772+
if(NOT STATUS_CODE EQUAL 0)
773+
# Clean up temp file on failure
774+
if(EXISTS ${ASSETS_TEMP_FILE})
775+
file(REMOVE ${ASSETS_TEMP_FILE})
776+
endif()
777+
message(FATAL_ERROR "Failed to download ${ASSETS_FILENAME} from ${assets_source}")
778+
endif()
779+
780+
# Move temp file to final location (atomic operation)
781+
file(RENAME ${ASSETS_TEMP_FILE} ${ASSETS_LOCAL_FILE})
782+
message(STATUS "Successfully downloaded ${ASSETS_FILENAME}")
783+
endif()
784+
else()
785+
# It's a local file path
786+
if(IS_ABSOLUTE "${assets_source}")
787+
set(ASSETS_LOCAL_FILE "${assets_source}")
788+
else()
789+
set(ASSETS_LOCAL_FILE "${CMAKE_CURRENT_SOURCE_DIR}/${assets_source}")
790+
endif()
791+
792+
# Check if local file exists
793+
if(NOT EXISTS ${ASSETS_LOCAL_FILE})
794+
message(FATAL_ERROR "Assets file not found: ${ASSETS_LOCAL_FILE}")
774795
endif()
775796

776-
# Move temp file to final location (atomic operation)
777-
file(RENAME ${ASSETS_TEMP_FILE} ${ASSETS_LOCAL_FILE})
778-
message(STATUS "Successfully downloaded ${DEFAULT_ASSETS}")
797+
message(STATUS "Using assets file: ${ASSETS_LOCAL_FILE}")
779798
endif()
780799

781-
# Flash assets to partition using esptool_py_flash_to_partition
800+
set(${assets_local_file_var} ${ASSETS_LOCAL_FILE} PARENT_SCOPE)
801+
endfunction()
802+
803+
# Flash assets based on configuration
804+
if(CONFIG_FLASH_DEFAULT_ASSETS)
805+
# Flash default assets
806+
if(DEFAULT_ASSETS)
807+
get_assets_local_file("${DEFAULT_ASSETS}" ASSETS_LOCAL_FILE)
808+
esptool_py_flash_to_partition(flash "assets" "${ASSETS_LOCAL_FILE}")
809+
message(STATUS "Default assets download and flash configured: ${DEFAULT_ASSETS} -> assets partition")
810+
else()
811+
message(WARNING "FLASH_DEFAULT_ASSETS is enabled but no DEFAULT_ASSETS is defined for board ${BOARD_TYPE}")
812+
endif()
813+
elseif(CONFIG_FLASH_CUSTOM_ASSETS)
814+
# Flash custom assets
815+
get_assets_local_file("${CONFIG_CUSTOM_ASSETS_FILE}" ASSETS_LOCAL_FILE)
782816
esptool_py_flash_to_partition(flash "assets" "${ASSETS_LOCAL_FILE}")
783-
784-
message(STATUS "Assets download and flash configured: ${ASSETS_URL} -> assets partition")
817+
message(STATUS "Custom assets flash configured: ${ASSETS_LOCAL_FILE} -> assets partition")
818+
elseif(CONFIG_FLASH_NONE_ASSETS)
819+
message(STATUS "Assets flashing disabled (FLASH_NONE_ASSETS)")
785820
endif()

main/Kconfig.projbuild

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,27 @@ config OTA_URL
66
help
77
The application will access this URL to check for new firmwares and server address.
88

9-
config DEFAULT_ASSETS_URL_PREFIX
10-
string "Default Assets URL Prefix"
11-
default "https://files.xiaozhi.me/assets/default/"
9+
choice
10+
prompt "Flash Assets"
11+
default FLASH_NONE_ASSETS
1212
help
13-
The assets will be downloaded from this URL.
13+
Select the assets to flash.
1414

15-
config FLASH_DEFAULT_ASSETS
16-
bool "Flash Default Assets"
17-
default y
15+
config FLASH_NONE_ASSETS
16+
bool "Do not flash assets"
17+
config FLASH_DEFAULT_ASSETS
18+
bool "Flash Default Assets"
19+
config FLASH_CUSTOM_ASSETS
20+
bool "Flash Custom Assets"
21+
endchoice
22+
23+
config CUSTOM_ASSETS_FILE
24+
depends on FLASH_CUSTOM_ASSETS
25+
string "Custom Assets File"
26+
default "assets.bin"
1827
help
19-
Download and flash default assets to the assets partition during build.
20-
If disabled, the assets partition will not be automatically populated.
28+
The custom assets file to flash.
29+
It can be a local file relative to the project directory or a remote url.
2130

2231
choice
2332
prompt "Default Language"

main/assets.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ struct mmap_assets_table {
2121

2222

2323
Assets::Assets(std::string default_assets_url) {
24-
// If the default_assets_url is not a http url, add the prefix
2524
if (default_assets_url.find("http") == 0) {
2625
default_assets_url_ = default_assets_url;
2726
} else {
28-
default_assets_url_ = CONFIG_DEFAULT_ASSETS_URL_PREFIX + default_assets_url;
27+
ESP_LOGE(TAG, "The default assets url is not a http url: %s", default_assets_url.c_str());
2928
}
3029

3130
// Initialize the partition

scripts/release.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ def release(board_type, board_config, config_filename="config.json"):
105105
# Append sdkconfig
106106
with open("sdkconfig", "a") as f:
107107
f.write("\n")
108-
f.write("# Release\n")
109-
f.write("CONFIG_FLASH_DEFAULT_ASSETS=n\n")
108+
f.write("# Append by release.py\n")
109+
f.write("CONFIG_FLASH_NONE_ASSETS=y\n")
110110
for append in sdkconfig_append:
111111
f.write(f"{append}\n")
112112
# Build with macro BOARD_NAME defined to name

0 commit comments

Comments
 (0)