67 lines
5.5 KiB
JavaScript
67 lines
5.5 KiB
JavaScript
// Compile production configuration/URL policy functions against hardware and
|
|||
|
|
// NVS doubles. Hardware acceptance remains a separate COM6 check.
|
||
|
|
const fs=require('fs'),path=require('path');
|
||
|
|
const base=path.resolve(__dirname,'..');
|
||
|
|
const json=path.join(process.env.IDF_PATH||'E:/Codex/toolchains/esp-idf-v5.5.1','components/json/cJSON');
|
||
|
|
const source=fs.readFileSync(path.join(base,'main/device_control.c'),'utf8');
|
||
|
|
const apply=source.slice(source.indexOf('static esp_err_t apply_config('),source.indexOf('static cJSON *diagnostics('));
|
||
|
|
const ota=fs.readFileSync(path.join(base,'main/device_ota.c'),'utf8');
|
||
|
|
const url=ota.slice(ota.indexOf('static bool same_origin_download('),ota.indexOf('static esp_err_t download('));
|
||
|
|
process.stdout.write(fs.readFileSync(path.join(json,'cJSON.h'),'utf8')+'\n'+fs.readFileSync(path.join(json,'cJSON.c'),'utf8').replace('#include "cJSON.h"','')+`
|
||
|
|
#include <assert.h>
|
||
|
|
#include <stdbool.h>
|
||
|
|
#include <stdint.h>
|
||
|
|
typedef int esp_err_t;
|
||
|
|
typedef int nvs_handle_t;
|
||
|
|
#define ESP_OK 0
|
||
|
|
#define ESP_FAIL -1
|
||
|
|
#define ESP_ERR_INVALID_RESPONSE 1
|
||
|
|
#define ESP_ERR_NOT_SUPPORTED 2
|
||
|
|
#define NVS_READWRITE 1
|
||
|
|
static int64_t applied_revision;
|
||
|
|
static char device_display_name[97], stored_name[97], staged_name[97];
|
||
|
|
static int read_error;
|
||
|
|
static int power_error;
|
||
|
|
static int device_power_apply(const char *mode,bool held){return power_error;}
|
||
|
|
static int hardware_volume=70,stored=-1,staged=-1,codec_error,commit_error,commits;
|
||
|
|
static int volc_hal_get_device_volume(void){return hardware_volume;}
|
||
|
|
static int volc_hal_set_device_volume(int n){if(codec_error)return -1;hardware_volume=n;return 0;}
|
||
|
|
static int nvs_open(const char *s,int mode,int *n){*n=1;return 0;}
|
||
|
|
static int nvs_get_i32(int n,const char *k,int32_t *v){*v=stored;return stored<0?-1:0;}
|
||
|
|
static int nvs_set_i32(int n,const char *k,int32_t v){staged=v;return 0;}
|
||
|
|
static int nvs_commit(int n){if(commit_error)return -1;commits++;stored=staged;strcpy(stored_name,staged_name);return 0;}
|
||
|
|
static int nvs_set_str(int n,const char *k,const char *s){strcpy(staged_name,s);return 0;}
|
||
|
|
static int nvs_get_str(int n,const char *k,char *out,size_t *size){if(read_error)return -1;size_t len=strlen(stored_name)+1;if(*size<len)return -1;memcpy(out,stored_name,len);*size=len;return 0;}
|
||
|
|
static void nvs_close(int n){}
|
||
|
|
static const char *ble_cloud_origin(void){return "https://82.157.201.114";}
|
||
|
|
`+apply+'\n'+url+`
|
||
|
|
static int apply(const char *s){cJSON *j=cJSON_Parse(s);int e=apply_config(j,false);cJSON_Delete(j);return e;}
|
||
|
|
int main(void){
|
||
|
|
assert(apply("{\\"revision\\":2,\\"desired_config\\":{\\"volume\\":{\\"speaker\\":60},\\"voice_runtime\\":\\"volc_hardware_agent\\"}}" )==0);
|
||
|
|
assert(hardware_volume==60 && stored==60 && applied_revision==2 && commits==1);
|
||
|
|
assert(apply("{\\"revision\\":2,\\"desired_config\\":{\\"volume\\":{\\"speaker\\":60}}}")==0);assert(commits==1);
|
||
|
|
codec_error=1;assert(apply("{\\"revision\\":3,\\"desired_config\\":{\\"volume\\":{\\"speaker\\":45}}}")!=0);
|
||
|
|
assert(hardware_volume==60 && applied_revision==2);codec_error=0;
|
||
|
|
commit_error=1;assert(apply("{\\"revision\\":3,\\"desired_config\\":{\\"volume\\":{\\"speaker\\":45}}}")!=0);
|
||
|
|
assert(hardware_volume==45 && stored==60 && applied_revision==2);commit_error=0;
|
||
|
|
assert(apply("{\\"revision\\":3,\\"desired_config\\":{\\"volume\\":{\\"speaker\\":45}}}")==0);assert(stored==45 && applied_revision==3);
|
||
|
|
assert(apply("{\\"revision\\":4,\\"desired_config\\":{\\"volume\\":{\\"speaker\\":-1}}}")!=0);
|
||
|
|
assert(apply("{\\"revision\\":4,\\"desired_config\\":{\\"volume\\":{\\"speaker\\":100.5}}}")!=0);
|
||
|
|
assert(apply("{\\"revision\\":4,\\"desired_config\\":{\\"volume\\":{\\"speaker\\":50},\\"unsupported\\":{}}}")!=0);
|
||
|
|
assert(hardware_volume==45 && applied_revision==3);
|
||
|
|
commit_error=1;assert(apply("{\\"revision\\":4,\\"desired_config\\":{\\"identity\\":{\\"display_name\\":\\"客厅陪伴设备\\"}}}")!=0);assert(device_display_name[0]==0 && applied_revision==3);commit_error=0;
|
||
|
|
read_error=1;assert(apply("{\\"revision\\":4,\\"desired_config\\":{\\"identity\\":{\\"display_name\\":\\"客厅陪伴设备\\"}}}")!=0);assert(device_display_name[0]==0 && applied_revision==3);read_error=0;
|
||
|
|
assert(apply("{\\"revision\\":4,\\"desired_config\\":{\\"identity\\":{\\"display_name\\":\\"客厅陪伴设备\\"}}}")==0);assert(!strcmp(device_display_name,"客厅陪伴设备") && applied_revision==4);
|
||
|
|
int previous_commits=commits;assert(apply("{\\"revision\\":4,\\"desired_config\\":{\\"identity\\":{\\"display_name\\":\\"客厅陪伴设备\\"}}}")==0);assert(commits==previous_commits);
|
||
|
|
assert(apply("{\\"revision\\":5,\\"desired_config\\":{\\"identity\\":{\\"display_name\\":\\"名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名名\\"}}}")!=0);assert(applied_revision==4);
|
||
|
|
assert(apply("{\\"revision\\":5,\\"desired_config\\":{\\"identity\\":{\\"display_name\\":\\"尾部空格 \\"}}}")!=0);assert(applied_revision==4);
|
||
|
|
assert(apply("{\\"revision\\":5,\\"desired_config\\":{\\"identity\\":{\\"display_name\\":\\"\\"}}}")==0);assert(device_display_name[0]==0 && stored_name[0]==0 && applied_revision==5);
|
||
|
|
assert(same_origin_download("https://82.157.201.114/f/ticket"));
|
||
|
|
assert(!same_origin_download("http://82.157.201.114/f/ticket"));
|
||
|
|
assert(!same_origin_download("https://82.157.201.114.evil.test/f/ticket"));
|
||
|
|
assert(!same_origin_download("https://82.157.201.114@evil.test/f/ticket"));
|
||
|
|
assert(!same_origin_download("https://82.157.201.114/other/ticket"));
|
||
|
|
puts("PASS production C: idle codec volume, NVS durability, revision accuracy, invalid config and HTTPS OTA origin policy");return 0;
|
||
|
|
}
|
||
|
|
`);
|