You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

97 lines
3.7 KiB

#include "config_service.h"
#include "project_dep.h"
static config_t _config;
static config_t _default_val_config;
#define TAG "config"
static void dump_config(config_t *pcfg) {
ZLOGI(TAG, "=============== config ===============");
ZLOGI(TAG, "obtaining_ip_mode : %s", obtaining_ip_mode_to_string(pcfg->obtaining_ip_mode));
ZLOGI(TAG, "ip : %s", inet_ntoa(pcfg->ip));
ZLOGI(TAG, "gw : %s", inet_ntoa(pcfg->gw));
ZLOGI(TAG, "netmask : %s", inet_ntoa(pcfg->netmask));
ZLOGI(TAG, "mac : %02x:%02x:%02x:%02x:%02x:%02x", pcfg->mac[0], pcfg->mac[1], pcfg->mac[2], pcfg->mac[3], pcfg->mac[4], pcfg->mac[5]);
ZLOGI(TAG, "======================================");
config_t *config = config_get();
for (uint32_t i = 0; i < ZARRAY_SIZE(config->reg_config_storage); i++) {
if (config->reg_config_storage[i].add == 0) {
break;
}
ZLOGI(TAG, "init reg [0x%x] [0x%x]", config->reg_config_storage[i].add, config->reg_config_storage[i].val);
}
}
static void create_default_config(config_t *now_cfg, bool cfg_is_error, config_t *default_cfg) { //
default_cfg->config_mark = FLASH_MASK_VAL;
IP4_ADDR((ip4_addr_t *)&default_cfg->ip, 192, 168, 8, 10);
IP4_ADDR((ip4_addr_t *)&default_cfg->gw, 192, 168, 8, 1);
IP4_ADDR((ip4_addr_t *)&default_cfg->netmask, 255, 255, 255, 0);
default_cfg->obtaining_ip_mode = obtaining_ip_mode_type_static; // dhcp
default_cfg->config0 = 0;
static mac_t mac;
if (now_cfg->mac[0] == 0 && now_cfg->mac[1] == 0 && now_cfg->mac[2] == 0 && now_cfg->mac[3] == 0 && now_cfg->mac[4] == 0 && now_cfg->mac[5] == 0) {
zaf_id_generate_random_mac(&mac);
ZLOGI(TAG, "gen random mac is %02x:%02x:%02x:%02x:%02x:%02x", mac.mac[0], mac.mac[1], mac.mac[2], mac.mac[3], mac.mac[4], mac.mac[5]);
} else if (now_cfg->mac[0] == 0xff && now_cfg->mac[1] == 0xff && now_cfg->mac[2] == 0xff && now_cfg->mac[3] == 0xff && now_cfg->mac[4] == 0xff && now_cfg->mac[5] == 0xff) {
zaf_id_generate_random_mac(&mac);
ZLOGI(TAG, "gen random mac is %02x:%02x:%02x:%02x:%02x:%02x", mac.mac[0], mac.mac[1], mac.mac[2], mac.mac[3], mac.mac[4], mac.mac[5]);
} else {
memcpy(&mac.mac, now_cfg->mac, 6);
}
memset(&default_cfg->mac[0], 0, sizeof(default_cfg->mac));
memcpy(&default_cfg->mac[0], mac.mac, 6);
}
void config_init(void) {
ZLOGI(TAG, "config_init");
/**
* @brief flash³õʼ»¯
*/
_Static_assert(sizeof(config_t) < FLASH_SOTRAGE_SIZE);
zaf_flash_init((uint32_t *)&_config, sizeof(config_t) / 4);
bool cfg_is_error = !zaf_flash_check();
create_default_config(&_config, cfg_is_error, &_default_val_config);
zaf_flash_set_default_data((uint32_t *)&_default_val_config);
if (cfg_is_error) {
zaf_flash_factory_reset();
}
/**
* @brief ´òÓ¡ÅäÖÃÐÅÏ¢
*/
// dump_config(&_config);
}
void config_dump() { dump_config(&_config); }
config_t *config_get(void) { return &_config; }
void config_flush(void) { zaf_flash_flush(); }
void config_factory_reset(void) { zaf_flash_factory_reset(); }
void config_generate_random_mac(void) {
static mac_t mac;
zaf_id_generate_random_mac(&mac);
ZLOGI(TAG, "gen random mac is %02x:%02x:%02x:%02x:%02x:%02x", mac.mac[0], mac.mac[1], mac.mac[2], mac.mac[3], mac.mac[4], mac.mac[5]);
memset(&_config.mac[0], 0, sizeof(_config.mac));
memcpy(&_config.mac[0], mac.mac, 6);
}
void config_update_reg(uint32_t add, uint32_t val) {
for (uint32_t i = 0; i < ZARRAY_SIZE(_config.reg_config_storage); i++) {
if (_config.reg_config_storage[i].add == add) {
_config.reg_config_storage[i].val = val;
break;
} else if (_config.reg_config_storage[i].add == 0) {
_config.reg_config_storage[i].add = add;
_config.reg_config_storage[i].val = val;
break;
}
}
return;
}