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.

71 lines
2.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "config_service.h"
  2. #include "project_dep.h"
  3. static config_t _config;
  4. static config_t _default_val_config;
  5. #define TAG "config"
  6. static void dump_config(config_t *pcfg) {
  7. ZLOGI(TAG, "=============== config ===============");
  8. ZLOGI(TAG, "obtaining_ip_mode : %s", obtaining_ip_mode_to_string(pcfg->obtaining_ip_mode));
  9. ZLOGI(TAG, "ip : %s", inet_ntoa(pcfg->ip));
  10. ZLOGI(TAG, "gw : %s", inet_ntoa(pcfg->gw));
  11. ZLOGI(TAG, "netmask : %s", inet_ntoa(pcfg->netmask));
  12. 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]);
  13. ZLOGI(TAG, "======================================");
  14. }
  15. static void create_default_config(config_t *now_cfg, bool cfg_is_error, config_t *default_cfg) { //
  16. default_cfg->config_mark = FLASH_MASK_VAL;
  17. IP4_ADDR((ip4_addr_t *)&default_cfg->ip, 192, 168, 8, 10);
  18. IP4_ADDR((ip4_addr_t *)&default_cfg->gw, 192, 168, 8, 1);
  19. IP4_ADDR((ip4_addr_t *)&default_cfg->netmask, 255, 255, 255, 0);
  20. default_cfg->obtaining_ip_mode = obtaining_ip_mode_type_static; // dhcp
  21. default_cfg->config0 = KXSYNC_REG_STM32_CONFIG0_MASK_TIMECODE_REPORT_ENABLE | //
  22. KXSYNC_REG_STM32_CONFIG0_MASK_CAMERA_SYNC_REPORT_ENABLE;
  23. static mac_t mac;
  24. 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) {
  25. xs_id_generate_random_mac(&mac);
  26. 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]);
  27. } 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) {
  28. xs_id_generate_random_mac(&mac);
  29. 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]);
  30. } else {
  31. memcpy(&mac.mac, now_cfg->mac, 6);
  32. }
  33. memset(&default_cfg->mac[0], 0, sizeof(default_cfg->mac));
  34. memcpy(&default_cfg->mac[0], mac.mac, 6);
  35. }
  36. void config_init(void) {
  37. ZLOGI(TAG, "config_init");
  38. /**
  39. * @brief flashʼ
  40. */
  41. xs_flash_init((uint32_t *)&_config, sizeof(config_t) / 4);
  42. bool cfg_is_error = !xs_flash_check();
  43. create_default_config(&_config, cfg_is_error, &_default_val_config);
  44. xs_flash_set_default_data((uint32_t *)&_default_val_config);
  45. if (cfg_is_error) {
  46. xs_flash_factory_reset();
  47. }
  48. /**
  49. * @brief ӡϢ
  50. */
  51. dump_config(&_config);
  52. }
  53. config_t *config_get(void) { return &_config; }
  54. void config_flush(void) { xs_flash_flush(); }
  55. void config_factory_reset(void) { xs_flash_factory_reset(); }
  56. void config_generate_random_mac(void) {
  57. static mac_t mac;
  58. xs_id_generate_random_mac(&mac);
  59. memset(&_config.mac[0], 0, sizeof(_config.mac));
  60. memcpy(&_config.mac[0], mac.mac, 6);
  61. }