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.
72 lines
3.1 KiB
72 lines
3.1 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, "======================================");
|
|
}
|
|
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 = KXSYNC_REG_STM32_CONFIG0_MASK_TIMECODE_REPORT_ENABLE | //
|
|
KXSYNC_REG_STM32_CONFIG0_MASK_CAMERA_SYNC_REPORT_ENABLE;
|
|
|
|
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) {
|
|
xs_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) {
|
|
xs_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³õʼ»¯
|
|
*/
|
|
xs_flash_init((uint32_t *)&_config, sizeof(config_t) / 4);
|
|
bool cfg_is_error = !xs_flash_check();
|
|
|
|
create_default_config(&_config, cfg_is_error, &_default_val_config);
|
|
|
|
xs_flash_set_default_data((uint32_t *)&_default_val_config);
|
|
|
|
if (cfg_is_error) {
|
|
xs_flash_factory_reset();
|
|
}
|
|
/**
|
|
* @brief ´òÓ¡ÅäÖÃÐÅÏ¢
|
|
*/
|
|
dump_config(&_config);
|
|
}
|
|
config_t *config_get(void) { return &_config; }
|
|
void config_flush(void) { xs_flash_flush(); }
|
|
void config_factory_reset(void) { xs_flash_factory_reset(); }
|
|
|
|
void config_generate_random_mac(void) {
|
|
static mac_t mac;
|
|
xs_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);
|
|
}
|