config.c 5.2 KB

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