]> git.itanic.dy.fi Git - BME280_driver/commitdiff
tempd: Use uncached data on retry
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Wed, 21 Apr 2021 17:37:05 +0000 (20:37 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Wed, 21 Apr 2021 17:37:05 +0000 (20:37 +0300)
If data read is retried due to error 85, we must read an uncached
value to prevent pulling the same error back from the cache.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
tempd.c

diff --git a/tempd.c b/tempd.c
index 5120b00b6d0de4bc6a5a843efff9168699c546a0..56905e251e6b06be9bae8b4330047f170b0e6f47 100644 (file)
--- a/tempd.c
+++ b/tempd.c
 #define min(a, b) ((a) < (b) ? (a) : (b))
 #define max(a, b) ((a) > (b) ? (a) : (b))
 
-static int read_1wire_temp(const char *path, double *temp)
+static inline char *_strlcat(char *dst, const char *src, size_t len)
 {
+       strncat(dst, src, len - strnlen(dst, len) - 1);
+       dst[len - 1] = '\0';
+
+       return dst;
+}
+
+static int read_1wire_temp(const char *_path, double *temp)
+{
+       char uncached[512];
        char buf[16];
        int fd;
        int ret;
        int retries = 0;
+       const char *path = _path;
+       char *c;
+       int len;
 
 retry:
        fd = open(path, O_RDONLY);
@@ -44,16 +56,32 @@ retry:
 out_close:
        close(fd);
 
-       if (*temp == 85) {
-               ret = -1;
-               if (retries < 3) {
-                       retries++;
-                       printf("Retrying 1wire read, try %d\n", retries);
-                       goto retry;
-               }
+       if (*temp != 85 || retries >= 3)
+               return ret;
+
+       ret = -1;
+       retries++;
+       printf("Retrying 1wire read, try %d\n", retries);
+
+       if (retries > 1)
+               goto retry;
+
+       /* Make an uncached path first */
+       c = strstr(_path, "/28.");
+       if (!c)
+               c = strstr(_path, "/10.");
+       if (!c) {
+               printf("Unable to make uncached path of \"%s\", will not retry\n", _path);
+               return -1;
        }
 
-       return ret;
+       len = c - _path;
+       strncpy(uncached, _path, len);
+       uncached[len] = '\0';
+       _strlcat(uncached, "/uncached/", sizeof(uncached));
+       _strlcat(uncached, c, sizeof(uncached));
+       path = uncached;
+       goto retry;
 }
 
 struct data_entry {