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.

96 lines
3.7 KiB

1 year 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. config_t *config = config_get();
  15. for (uint32_t i = 0; i < ZARRAY_SIZE(config->reg_config_storage); i++) {
  16. if (config->reg_config_storage[i].add == 0) {
  17. break;
  18. }
  19. ZLOGI(TAG, "init reg [0x%x] [0x%x]", config->reg_config_storage[i].add, config->reg_config_storage[i].val);
  20. }
  21. }
  22. static void create_default_config(config_t *now_cfg, bool cfg_is_error, config_t *default_cfg) { //
  23. default_cfg->config_mark = FLASH_MASK_VAL;
  24. IP4_ADDR((ip4_addr_t *)&default_cfg->ip, 192, 168, 8, 10);
  25. IP4_ADDR((ip4_addr_t *)&default_cfg->gw, 192, 168, 8, 1);
  26. IP4_ADDR((ip4_addr_t *)&default_cfg->netmask, 255, 255, 255, 0);
  27. default_cfg->obtaining_ip_mode = obtaining_ip_mode_type_static; // dhcp
  28. default_cfg->config0 = 0;
  29. static mac_t mac;
  30. 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) {
  31. zaf_id_generate_random_mac(&mac);
  32. 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]);
  33. } 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) {
  34. zaf_id_generate_random_mac(&mac);
  35. 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]);
  36. } else {
  37. memcpy(&mac.mac, now_cfg->mac, 6);
  38. }
  39. memset(&default_cfg->mac[0], 0, sizeof(default_cfg->mac));
  40. memcpy(&default_cfg->mac[0], mac.mac, 6);
  41. }
  42. void config_init(void) {
  43. ZLOGI(TAG, "config_init");
  44. /**
  45. * @brief flashʼ
  46. */
  47. _Static_assert(sizeof(config_t) < FLASH_SOTRAGE_SIZE);
  48. zaf_flash_init((uint32_t *)&_config, sizeof(config_t) / 4);
  49. bool cfg_is_error = !zaf_flash_check();
  50. create_default_config(&_config, cfg_is_error, &_default_val_config);
  51. zaf_flash_set_default_data((uint32_t *)&_default_val_config);
  52. if (cfg_is_error) {
  53. zaf_flash_factory_reset();
  54. }
  55. /**
  56. * @brief ӡϢ
  57. */
  58. // dump_config(&_config);
  59. }
  60. void config_dump() { dump_config(&_config); }
  61. config_t *config_get(void) { return &_config; }
  62. void config_flush(void) { zaf_flash_flush(); }
  63. void config_factory_reset(void) { zaf_flash_factory_reset(); }
  64. void config_generate_random_mac(void) {
  65. static mac_t mac;
  66. zaf_id_generate_random_mac(&mac);
  67. 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]);
  68. memset(&_config.mac[0], 0, sizeof(_config.mac));
  69. memcpy(&_config.mac[0], mac.mac, 6);
  70. }
  71. void config_update_reg(uint32_t add, uint32_t val) {
  72. for (uint32_t i = 0; i < ZARRAY_SIZE(_config.reg_config_storage); i++) {
  73. if (_config.reg_config_storage[i].add == add) {
  74. _config.reg_config_storage[i].val = val;
  75. break;
  76. } else if (_config.reg_config_storage[i].add == 0) {
  77. _config.reg_config_storage[i].add = add;
  78. _config.reg_config_storage[i].val = val;
  79. break;
  80. }
  81. }
  82. return;
  83. }