From 61ddb0334e221dbfd675c01bb342a262c65345ba Mon Sep 17 00:00:00 2001 From: Pim van Pelt Date: Sun, 4 Nov 2018 19:19:17 +0100 Subject: [PATCH] Commit timespec to file on Timespec.Add; Unlink timespec file on Timespec.Clear; Initialize timespec from file in channel_init() --- src/channel.c | 6 ++++++ src/rpc.c | 14 +++++++++++++- src/timespec.c | 8 +++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/channel.c b/src/channel.c index 9e13000..966ba92 100644 --- a/src/channel.c +++ b/src/channel.c @@ -71,6 +71,7 @@ bool channel_init(const char *fn) { int led = -1, relay = -1, button = -1; bool led_invert = false; bool relay_invert = false; + char fn[100]; LOG(LL_DEBUG, ("[%d]: [%.*s]", idx, val.len, val.ptr)); if (val.len == 0 || !val.ptr) { @@ -105,6 +106,11 @@ bool channel_init(const char *fn) { s_channels[idx].led_invert = led_invert; s_channels[idx].channel_override = false; s_channels[idx].timespec = timespec_create(); + + snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx); + if (timespec_read_file(s_channels[idx].timespec, fn)) { + LOG(LL_INFO, ("Initialized timespec from file %s", fn)); + } } ret = true; diff --git a/src/rpc.c b/src/rpc.c index d83ac23..bd1fd13 100644 --- a/src/rpc.c +++ b/src/rpc.c @@ -157,7 +157,8 @@ static void rpc_timespec_get_handler(struct mg_rpc_request_info *ri, void *cb_ar static void rpc_timespec_clear_handler(struct mg_rpc_request_info *ri, void *cb_arg, struct mg_rpc_frame_info *fi, struct mg_str args) { struct mgos_timespec *ts; - int idx; + char fn[100]; + int idx; rpc_log(ri, args); @@ -181,6 +182,9 @@ static void rpc_timespec_clear_handler(struct mg_rpc_request_info *ri, void *cb_ } channel_override_set(idx); + snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx); + unlink(fn); + mg_rpc_send_responsef(ri, "{idx: %d}", idx); ri = NULL; @@ -192,6 +196,7 @@ static void rpc_timespec_add_handler(struct mg_rpc_request_info *ri, void *cb_ar struct mgos_timespec *ts; int idx; char *spec = NULL; + char fn[100]; rpc_log(ri, args); @@ -214,6 +219,13 @@ static void rpc_timespec_add_handler(struct mg_rpc_request_info *ri, void *cb_ar } channel_override_set(idx); + snprintf(fn, sizeof(fn) - 1, "timespec.chan%d", idx); + if (!timespec_write_file(ts, fn)) { + mg_rpc_send_errorf(ri, 400, "Could not commit timespec to file: %s", fn); + ri = NULL; + return; + } + mg_rpc_send_responsef(ri, "{idx: %d}", idx); ri = NULL; diff --git a/src/timespec.c b/src/timespec.c index fca57f3..9cbb7b2 100644 --- a/src/timespec.c +++ b/src/timespec.c @@ -274,6 +274,7 @@ bool timespec_write_file(struct mgos_timespec *ts, const char *fn) { } close(fd); + LOG(LL_INFO, ("Wrote timespec to %s", fn)); return true; } @@ -294,13 +295,18 @@ bool timespec_read_file(struct mgos_timespec *ts, const char *fn) { if (0 != stat(fn, &fp_stat)) { LOG(LL_ERROR, ("Could not stat %s", fn)); + return false; } + if (fp_stat.st_size > 1024) { LOG(LL_ERROR, ("File size of %s is larger than 1024 bytes (%u)", fn, (uint32_t)fp_stat.st_size)); + return false; } + buf = malloc(fp_stat.st_size + 1); if (!buf) { LOG(LL_ERROR, ("Could not malloc %u bytes for file %s", (uint32_t)fp_stat.st_size, fn)); + return false; } if (!(fd = open(fn, O_RDONLY))) { @@ -316,7 +322,6 @@ bool timespec_read_file(struct mgos_timespec *ts, const char *fn) { } buf[fp_stat.st_size] = '\0'; close(fd); - LOG(LL_INFO, ("buf='%s'", buf)); // Wipe the timespec and parse back timespec_clear_spec(ts); @@ -328,5 +333,6 @@ bool timespec_read_file(struct mgos_timespec *ts, const char *fn) { } } free(buf); + LOG(LL_INFO, ("Read timespec from %s", fn)); return true; }