Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 41 additions & 34 deletions components/ts_api/src/ts_api_ota.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ static esp_err_t api_ota_upload_abort(const cJSON *params, ts_api_result_t *resu
/**
* @brief Start WWW partition OTA from URL
* API: ota.www.start
* Params: url (required), skip_verify (optional)
* Params: url (optional if the OTA server is configured), skip_verify (optional)
*/
static esp_err_t api_ota_www_start(const cJSON *params, ts_api_result_t *result)
{
Expand All @@ -711,44 +711,51 @@ static esp_err_t api_ota_www_start(const cJSON *params, ts_api_result_t *result)
return ESP_ERR_INVALID_ARG;
}

// Get URL - either from params or use server URL with www.bin suffix
cJSON *url_item = cJSON_GetObjectItem(params, "url");
char url[300] = {0};
const char *server = ota_server_url();

if (url_item && cJSON_IsString(url_item) && strlen(url_item->valuestring) >= sizeof(url)) {
ts_api_result_error(result, TS_API_ERR_INVALID_ARG, "URL too long");
return ESP_ERR_INVALID_ARG;
}
if (url_item && cJSON_IsString(url_item) && url_item->valuestring[0]) {
strncpy(url, url_item->valuestring, sizeof(url) - 1);
} else if (server[0]) {
// Use server URL and replace firmware.bin with www.bin
strncpy(url, server, sizeof(url) - 1);
// Try to replace common firmware names
char *bin_pos = strstr(url, "firmware.bin");
if (bin_pos) {
strcpy(bin_pos, "www.bin");
} else {
bin_pos = strstr(url, "TianShanOS.bin");
if (bin_pos) {
strcpy(bin_pos, "www.bin");
} else {
// URL doesn't contain known .bin name
size_t len = strlen(url);
if (len > 0 && url[len-1] == '/') {
// URL ends with /, append www.bin
strncat(url, "www.bin", sizeof(url) - len - 1);
} else {
// URL doesn't end with /, append /www.bin
strncat(url, "/www.bin", sizeof(url) - len - 1);
const cJSON *url_item = cJSON_GetObjectItem(params, "url");
const char *prefix = cJSON_IsString(url_item) ? url_item->valuestring : "";
char url[TS_OTA_WWW_URL_MAX_LEN + 1];
size_t prefix_len = strlen(prefix);
const char *suffix = "";
if (prefix_len == 0) {
prefix = ota_server_url();
if (prefix == s_ota_server_url) {
// Parse and substitute using one copy of the mutable server setting.
_Static_assert(sizeof(s_ota_server_url) <= sizeof(url), "OTA server URL must fit the local copy");
memcpy(url, s_ota_server_url, sizeof(s_ota_server_url));
prefix = url;
}
prefix_len = strlen(prefix);
if (prefix_len == 0) {
ts_api_result_error(result, TS_API_ERR_INVALID_ARG, "缺少 url 参数且未设置 OTA 服务器");
return ESP_ERR_INVALID_ARG;
}
suffix = prefix[prefix_len - 1] == '/' ? "www.bin" : "/www.bin";
struct http_parser_url parsed;
if (ts_http_url_parse(prefix, &parsed) && (parsed.field_set & (1U << UF_PATH))) {
static const char *const firmware_names[] = {
"/TianshanOS.bin", "/TianShanOS.bin", "/firmware.bin",
};
size_t path_len = parsed.field_data[UF_PATH].len;
size_t path_end = parsed.field_data[UF_PATH].off + path_len;
for (size_t i = 0; i < sizeof(firmware_names) / sizeof(firmware_names[0]); ++i) {
size_t name_len = strlen(firmware_names[i]);
if (path_len >= name_len &&
memcmp(prefix + path_end - name_len, firmware_names[i], name_len) == 0) {
// Drop the firmware URL's query and fragment for its companion image.
prefix_len = path_end - name_len;
suffix = "/www.bin";
break;
}
}
}
} else {
ts_api_result_error(result, TS_API_ERR_INVALID_ARG, "缺少 url 参数且未设置 OTA 服务器");
}
size_t suffix_len = strlen(suffix);
if (prefix_len + suffix_len > TS_OTA_WWW_URL_MAX_LEN) {
ts_api_result_error(result, TS_API_ERR_INVALID_ARG, "URL too long");
return ESP_ERR_INVALID_ARG;
}
memmove(url, prefix, prefix_len);
memcpy(url + prefix_len, suffix, suffix_len + 1);

// Get skip_verify option
bool skip_verify = false;
Expand Down
4 changes: 3 additions & 1 deletion components/ts_ota/include/ts_ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ bool ts_ota_https_is_running(void);
// WWW Partition OTA API
// ============================================================================

#define TS_OTA_WWW_URL_MAX_LEN 255

/**
* @brief Canonical recovery image paths
*/
Expand All @@ -443,7 +445,7 @@ esp_err_t ts_ota_www_init(void);
* Downloads www.bin and writes to the www (SPIFFS) partition.
* This is typically the second step after app OTA completes.
*
* @param url URL to download www.bin from
* @param url URL to download www.bin from (up to TS_OTA_WWW_URL_MAX_LEN bytes)
* @param skip_cert_verify Skip SSL certificate verification
* @param progress_cb Progress callback (optional)
* @param user_data User data for callback
Expand Down
7 changes: 3 additions & 4 deletions components/ts_ota/src/ts_ota_www.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static bool s_www_ota_abort_requested = false;

// Configuration for current www OTA
typedef struct {
char url[256]; // URL or file path
char url[TS_OTA_WWW_URL_MAX_LEN + 1]; // URL or file path
bool skip_cert_verify;
bool from_sdcard; // true = SD card file, false = HTTP URL
ts_ota_progress_cb_t progress_cb;
Expand Down Expand Up @@ -120,7 +120,7 @@ esp_err_t ts_ota_www_get_progress(ts_ota_progress_t *progress)
esp_err_t ts_ota_www_start(const char *url, bool skip_cert_verify,
ts_ota_progress_cb_t progress_cb, void *user_data)
{
if (!url || strlen(url) == 0) {
if (!url || !url[0] || strlen(url) > TS_OTA_WWW_URL_MAX_LEN) {
return ESP_ERR_INVALID_ARG;
}

Expand All @@ -141,8 +141,7 @@ esp_err_t ts_ota_www_start(const char *url, bool skip_cert_verify,
s_www_ota_abort_requested = false;

// Store config
strncpy(s_www_config.url, url, sizeof(s_www_config.url) - 1);
s_www_config.url[sizeof(s_www_config.url) - 1] = '\0';
strcpy(s_www_config.url, url);
s_www_config.skip_cert_verify = skip_cert_verify;
s_www_config.progress_cb = progress_cb;
s_www_config.user_data = user_data;
Expand Down
134 changes: 121 additions & 13 deletions tests/runtime/test_ota_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Production OTA server resolution and persisted legacy address migration."""
"""Production OTA server migration and WWW URL resolution through downloader startup."""
from pathlib import Path
import re
from test_console_lifecycle import IDF, body, run_native
Expand All @@ -15,6 +15,8 @@
#include <stdlib.h>
#include <string.h>
#include "ts_api.h"
#include "ts_http_url.h"
#include "ts_ota.h"
#define ESP_LOGI(...) ((void)0)
#define ESP_LOGE(...) ((void)0)
#define ESP_LOGW(...) ((void)0)
Expand Down Expand Up @@ -47,17 +49,38 @@
}
static esp_err_t nvs_commit(nvs_handle_t h) { commits++; return fail_commit ? ESP_FAIL : ESP_OK; }
static void nvs_close(nvs_handle_t h) {}
static char www_url[300];
static esp_err_t ts_ota_www_start(const char *url,bool skip,void *callback,void *ctx) {
assert(!skip); strcpy(www_url,url); return ESP_OK;
}
typedef int BaseType_t;
static bool s_www_ota_running, s_www_ota_abort_requested;
static bool s_www_progress_mutex = true;
static unsigned www_task_starts;
#define pdPASS 1
/* Capture task creation without running the downloader. */
#define xTaskCreate(...) (++www_task_starts, pdPASS)
esp_err_t ts_ota_www_init(void) { return ESP_OK; }
'''
www_source = Path('components/ts_ota/src/ts_ota_www.c')
www_text = www_source.read_text()
code += www_text[www_text.index('// Configuration for current www OTA'):
www_text.index('// WWW OTA progress tracking')]
code += body(www_source, 'ts_ota_www_start')
code += f'#define CONFIG_TS_OTA_DEFAULT_URL "{default}"\n'
source_text = source.read_text()
code += source_text[source_text.index('#define NVS_NAMESPACE_OTA'):source_text.index('static esp_err_t api_ota_status')]
for name in ('ts_api_result_ok', 'ts_api_result_error', 'ts_api_result_free'):
code += body('components/ts_api/src/ts_api.c', name)
code += body(source, 'api_ota_www_start')
code += r'''
static bool change_server_during_parse;
static bool parse_www_url(const char *url, struct http_parser_url *parsed) {
bool valid = ts_http_url_parse(url, parsed);
if (change_server_during_parse) {
strcpy(s_ota_server_url, "https://other.fixture.invalid");
change_server_during_parse = false;
}
return valid;
}
#define ts_http_url_parse parse_www_url
'''
code += body(source, 'api_ota_www_start') + '#undef ts_http_url_parse\n'
code += r'''
static void check_server(const char *expected) {
ts_api_result_t result={0};
Expand All @@ -67,6 +90,31 @@
ts_api_result_free(&result);
}

static void check_www(const char *explicit_url, const char *expected) {
ts_api_result_t result = {0};
cJSON *params = cJSON_CreateObject();
assert(params);
if (explicit_url) assert(cJSON_AddStringToObject(params, "url", explicit_url));
s_www_config.url[0] = '\0';
s_www_ota_running = false;
unsigned previous_starts = www_task_starts;
esp_err_t ret = api_ota_www_start(params, &result);
if (expected) {
assert(ret == ESP_OK && result.code == TS_API_OK);
assert(www_task_starts == previous_starts + 1);
if (strcmp(s_www_config.url, expected) != 0) {
fprintf(stderr, "WWW OTA URL: expected %s, got %s\n", expected, s_www_config.url);
abort();
}
assert(!strcmp(cJSON_GetObjectItem(result.data, "url")->valuestring, expected));
} else {
assert(ret == ESP_ERR_INVALID_ARG && result.code == TS_API_ERR_INVALID_ARG);
assert(www_task_starts == previous_starts && !s_www_config.url[0]);
}
cJSON_Delete(params);
ts_api_result_free(&result);
}

int main(void) {
init_ota_server_config();
check_server("https://ota.rminte.com");
Expand Down Expand Up @@ -98,12 +146,72 @@
cJSON_Delete(params);
ts_api_result_free(&result);

params=cJSON_CreateObject();
assert(api_ota_www_start(params,&result)==ESP_OK);
assert(!strcmp(www_url,"https://ota.rminte.com/www.bin"));
cJSON_Delete(params);
ts_api_result_free(&result);
puts("PASS OTA server: default, exact legacy migration, custom URL, clear and WWW fallback");
check_www(NULL, "https://ota.rminte.com/www.bin");

static const struct { const char *server; const char *expected; } cases[] = {
{"https://local.fixture.invalid/update/TianshanOS.bin", "https://local.fixture.invalid/update/www.bin"},
{"https://local.fixture.invalid/update/TianShanOS.bin", "https://local.fixture.invalid/update/www.bin"},
{"https://local.fixture.invalid/update/firmware.bin", "https://local.fixture.invalid/update/www.bin"},
{"https://local.fixture.invalid", "https://local.fixture.invalid/www.bin"},
{"https://local.fixture.invalid/", "https://local.fixture.invalid/www.bin"},
{"https://local.fixture.invalid/firmware.bin/channel", "https://local.fixture.invalid/firmware.bin/channel/www.bin"},
{"https://local.fixture.invalid/update/TianShanOS.bin?download=1", "https://local.fixture.invalid/update/www.bin"},
};
for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
strcpy(stored, cases[i].server);
init_ota_server_config();
check_www(NULL, cases[i].expected);
}

const char *explicit_url = "https://other.fixture.invalid/web/custom.bin?source=TianshanOS.bin";
check_www(explicit_url, explicit_url);

strcpy(stored, "https://local.fixture.invalid/update/TianshanOS.bin");
init_ota_server_config();
change_server_during_parse = true;
check_www(NULL, "https://local.fixture.invalid/update/www.bin");
check_www(NULL, "https://other.fixture.invalid/www.bin");

char boundary_url[257] = "https://local.fixture.invalid/";
const size_t prefix_len = strlen(boundary_url);
const size_t max_root_len = 255 - strlen("/www.bin");
memset(boundary_url + prefix_len, 'a', max_root_len + 1 - prefix_len);
boundary_url[max_root_len] = '\0';
strcpy(stored, boundary_url);
init_ota_server_config();
strcpy(boundary_url + max_root_len, "/www.bin");
assert(strlen(boundary_url) == 255);
check_www(NULL, boundary_url);
check_www(boundary_url, boundary_url);

boundary_url[max_root_len] = 'a';
boundary_url[max_root_len + 1] = '\0';
strcpy(stored, boundary_url);
init_ota_server_config();
strcpy(boundary_url + max_root_len + 1, "/www.bin");
assert(strlen(boundary_url) == 256);
check_www(NULL, NULL);
check_www(boundary_url, NULL);
unsigned previous_starts = www_task_starts;
assert(ts_ota_www_start(boundary_url, false, NULL, NULL) == ESP_ERR_INVALID_ARG);
assert(www_task_starts == previous_starts && !s_www_ota_running && !s_www_config.url[0]);

/* A 255-byte firmware URL can still be replaced with its shorter companion URL. */
memset(boundary_url + prefix_len, 'a', 255 - prefix_len);
strcpy(boundary_url + 255 - strlen("/TianshanOS.bin"), "/TianshanOS.bin");
strcpy(stored, boundary_url);
init_ota_server_config();
strcpy(boundary_url + 255 - strlen("/TianshanOS.bin"), "/www.bin");
check_www(NULL, boundary_url);
puts("PASS OTA server: defaults, migration and WWW download URL resolution");
}
'''
run_native(code, [IDF / 'components/json/cJSON/cJSON.c'])
http_parser = IDF / 'components/http_parser'
run_native(code, [
IDF / 'components/json/cJSON/cJSON.c',
http_parser / 'http_parser.c',
Path('components/ts_http_fetch/src/ts_http_url.c'),
], extra_include_dirs=[
http_parser, Path('components/ts_http_fetch/include'), Path('components/ts_ota/include'),
Path('components/ts_core/ts_event/include'),
])
Loading