config.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #include "common.h"
  2. #include "config.h"
  3. #include <esp_spiffs.h>
  4. #include <ctype.h>
  5. #define CONFIG_FILE "/spiffs/config.txt"
  6. struct env_var {
  7. const char *var, *val;
  8. };
  9. static const struct env_var default_config[] = {
  10. {"LANG","sv"},
  11. {"TZ", "CET-1CEST,M3.5.0,M10.5.0/3"}, /* Sweden */
  12. {"tzname", "Europe/Stockholm"},
  13. {"hostname", "max80"},
  14. {"sntp.enabled", "1"},
  15. {"sntp.server","time.max80.abc80.org"}
  16. };
  17. static int save_config(void);
  18. static bool config_changed;
  19. int setenv_config(const char *name, const char *value)
  20. {
  21. config_changed = true;
  22. if (name[0] == '-') {
  23. name++;
  24. value = NULL;
  25. }
  26. if (value)
  27. return setenv(name, value, 1);
  28. else
  29. return unsetenv(name);
  30. }
  31. static int reset_config(void)
  32. {
  33. while (1) {
  34. char **envp;
  35. for (envp = environ; *envp; envp++) {
  36. if (!strncmp("status.", *envp, 7))
  37. continue;
  38. else
  39. break;
  40. }
  41. if (!*envp)
  42. break;
  43. const char *eq = strchr(*envp, '=');
  44. char ename[eq - *envp + 1];
  45. memcpy(ename, *envp, eq - *envp);
  46. ename[eq - *envp] = '\0';
  47. unsetenv(ename);
  48. }
  49. size_t i;
  50. for (i = 0; i < ARRAY_SIZE(default_config); i++)
  51. setenv(default_config[i].var, default_config[i].val, 1);
  52. config_changed = true;
  53. }
  54. static bool is_end_of_string(int c)
  55. {
  56. return c <= 0 || c == '\n' || c == '\r';
  57. }
  58. /*
  59. * Note: the string must be mutable; use strdup() if necessary.
  60. * The "separator" allows the string to be separated into multiple
  61. * arguments; no other decoding is done.
  62. */
  63. static int set_config_string(char *str, unsigned int separator)
  64. {
  65. char *p, *q;
  66. unsigned char c;
  67. p = str;
  68. do {
  69. const char *var = p;
  70. do {
  71. c = *p++;
  72. } while (isalnum(c) || c == '.' || c == '-');
  73. if (c != '=')
  74. return -EINVAL; /* Invalid config line (blank, comment...) */
  75. p[-1] = '\0';
  76. q = p;
  77. do {
  78. c = *q++;
  79. } while (!is_end_of_string(c) && c != separator);
  80. /* Overlong line */
  81. if (q >= str + MAX_CONFIG_LINE)
  82. return -EOVERFLOW;
  83. q[-1] = '\0';
  84. setenv_config(var, p);
  85. p = q;
  86. } while (c == separator);
  87. return 0;
  88. }
  89. static void finish_config_update(bool save)
  90. {
  91. if (config_changed) {
  92. if (save)
  93. save_config();
  94. tzset();
  95. config_changed = false;
  96. }
  97. }
  98. /*
  99. * At the moment "url" just allows values to be separated by semicolons;
  100. * no other decoding is done, and '&' is not supported.
  101. */
  102. int set_config_url_string(const char *str)
  103. {
  104. char *wstr = strdup(str);
  105. if (!wstr)
  106. return -ENOMEM;
  107. int err = set_config_string(wstr, ';');
  108. free(wstr);
  109. finish_config_update(true);
  110. return err;
  111. }
  112. static void skip_rest_of_line(FILE *f)
  113. {
  114. int c;
  115. do {
  116. c = getc(f);
  117. } while (!is_end_of_string(c));
  118. }
  119. /* Calling with with f == NULL just finalizes the update */
  120. int read_config(FILE *f, bool save)
  121. {
  122. char *linebuf = NULL;
  123. int err = -1;
  124. if (f) {
  125. linebuf = malloc(MAX_CONFIG_LINE);
  126. if (!linebuf) {
  127. err = -ENOMEM;
  128. goto exit;
  129. }
  130. while (fgets(linebuf, MAX_CONFIG_LINE, f)) {
  131. if (set_config_string(linebuf, -1) == -EOVERFLOW)
  132. skip_rest_of_line(f);
  133. }
  134. }
  135. err = 0;
  136. if (linebuf)
  137. free(linebuf);
  138. exit:
  139. finish_config_update(save);
  140. return err;
  141. };
  142. int write_env(FILE *f, bool status)
  143. {
  144. size_t skip = status ? 7 : 0;
  145. for (char **var = environ; *var; var++) {
  146. if (!strncmp(*var, "status.", 7) == status) {
  147. fputs(*var + skip, f);
  148. putc('\n', f);
  149. }
  150. }
  151. return ferror(f) ? -1 : 0;
  152. }
  153. static int save_config(void)
  154. {
  155. int err = -ENOENT;
  156. FILE *f = fopen(CONFIG_FILE, "w");
  157. if (f) {
  158. err = write_env(f, false);
  159. fclose(f);
  160. }
  161. if (err)
  162. printf("[CONF] Failed to save configuration (error %d)\n", err);
  163. return err;
  164. }
  165. static const esp_vfs_spiffs_conf_t spiffs_conf = {
  166. .base_path = "/spiffs",
  167. .partition_label = NULL,
  168. .max_files = 4,
  169. .format_if_mount_failed = true
  170. };
  171. void init_config(void)
  172. {
  173. if (!esp_spiffs_mounted(spiffs_conf.partition_label)) {
  174. esp_err_t err;
  175. err = esp_vfs_spiffs_register(&spiffs_conf);
  176. if (err)
  177. printf("[CONF] Failed to mount %s (error 0x%x)\n",
  178. spiffs_conf.base_path, err);
  179. }
  180. reset_config();
  181. FILE *f = fopen(CONFIG_FILE, "r");
  182. if (!f)
  183. printf("[CONF] No configuration file found, using defaults\n");
  184. read_config(f, false);
  185. if (f)
  186. fclose(f);
  187. }
  188. const char *getenv_def(const char *var, const char *def)
  189. {
  190. const char *val = getenv(var);
  191. return val ? val : def;
  192. }
  193. long getenv_l(const char *var, long def)
  194. {
  195. const char *ep;
  196. var = getenv(var);
  197. if (!var || !*var)
  198. return def;
  199. long val = strtol(var, (char **)&ep, 0);
  200. return *ep ? def : val;
  201. }
  202. void setenv_l(const char *var, long val)
  203. {
  204. char vbuf[2+3*sizeof val];
  205. snprintf(vbuf, sizeof vbuf, "%ld", val);
  206. setenv(var, vbuf, 1);
  207. }
  208. unsigned long getenv_ul(const char *var, unsigned long def)
  209. {
  210. const char *ep;
  211. var = getenv(var);
  212. if (!var || !*var)
  213. return def;
  214. unsigned long val = strtol(var, (char **)&ep, 0);
  215. return *ep ? def : val;
  216. }
  217. void setenv_ul(const char *var, unsigned long val)
  218. {
  219. char vbuf[2+3*sizeof val];
  220. snprintf(vbuf, sizeof vbuf, "%lu", val);
  221. setenv(var, vbuf, 1);
  222. }
  223. bool getenv_bool(const char *var)
  224. {
  225. var = getenv(var);
  226. if (!var)
  227. return false;
  228. unsigned char c = *var;
  229. unsigned char cl = c | 0x20;
  230. return !(!c || c == '0' || cl == 'f' || cl == 'n' || cl == 'd' ||
  231. (cl == 'o' && (var[1] | 0x20) == 'f'));
  232. }
  233. void setenv_bool(const char *var, bool val)
  234. {
  235. return setenv_ul(var, val);
  236. }