Added state to scan vanished one-wire devices. Update devices if a one-wire device returns on the bus.

Wed, 03 Apr 2024 19:33:38 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 03 Apr 2024 19:33:38 +0200
changeset 659
bfab45f4d5cd
parent 658
b10d0f6337a3
child 660
a28ef4d9afa4

Added state to scan vanished one-wire devices. Update devices if a one-wire device returns on the bus.

thermferm/one-wire.c file | annotate | diff | comparison | revisions
--- a/thermferm/one-wire.c	Wed Apr 03 16:53:22 2024 +0200
+++ b/thermferm/one-wire.c	Wed Apr 03 19:33:38 2024 +0200
@@ -25,6 +25,7 @@
 #include "one-wire.h"
 #include "devices.h"
 #include "delay.h"
+#include "futil.h"
 #include "xutil.h"
 
 
@@ -62,12 +63,14 @@
 
 SM_DECL(one_wire,(char *)"one-wire")
 SM_STATES
-    Scan,
+    ScanNew,
+    ScanDel,
     Read2413,
     Reading,
     Missing
 SM_NAMES
-    (char *)"Scan",
+    (char *)"ScanNew",
+    (char *)"ScanDel",
     (char *)"Read2413",
     (char *)"Reading",
     (char *)"Missing"
@@ -77,12 +80,12 @@
     FILE		*fp;
     devices_list	*device;
     w1_list		*dev_w1, *n_w1, *cur_w1 = NULL;
-    char		buffer[25], w1type[10];
+    char		buffer[25], w1type[10], *devfile = NULL;
     uint8_t		state, output;
 
-SM_START(Scan)
+SM_START(ScanNew)
 
-SM_STATE(Scan)
+SM_STATE(ScanNew)
 
     if (my_shutdown) {
 	SM_SUCCESS;
@@ -117,6 +120,13 @@
 			pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]);
 			dev_w1->present = DEVPRESENT_YES;
 			pthread_mutex_unlock(&mutexes[LOCK_ONE_WIRE]);
+			for (device = Config.devices; device; device = device->next) {
+			    if (strcmp(dev_w1->address, device->address) == 0) {
+				pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
+				device->present = DEVPRESENT_YES;
+				pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
+			    }
+			}
 		    }
 		    break;
 		}
@@ -151,6 +161,39 @@
 	}
     }
     fclose(fp);
+    SM_PROCEED(ScanDel);
+
+SM_STATE(ScanDel)
+
+    if (my_shutdown) {
+	SM_SUCCESS;
+    }
+
+    /*
+     * Scan from the linked list if all devices are still present.
+     */
+    for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
+	devfile = xstrcpy((char *)"/sys/bus/w1/devices/");
+	devfile = xstrcat(devfile, dev_w1->address);
+	devfile = xstrcat(devfile, (char *)"/uevent");
+	if (file_exist(devfile, R_OK) && (dev_w1->present == DEVPRESENT_YES)) {
+	    /*
+	     * Gone missing
+	     */
+	    syslog(LOG_NOTICE, "One-wire device %s is missing", dev_w1->address);
+	    pthread_mutex_lock(&mutexes[LOCK_ONE_WIRE]);
+	    dev_w1->present = DEVPRESENT_NO;
+	    pthread_mutex_unlock(&mutexes[LOCK_ONE_WIRE]);
+	    for (device = Config.devices; device; device = device->next) {
+		if (strcmp(dev_w1->address, device->address) == 0) {
+		    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
+		    device->present = DEVPRESENT_NO;
+		    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
+		}
+	    }
+	}
+    }
+
     SM_PROCEED(Read2413);
 
 SM_STATE(Read2413)
@@ -220,9 +263,11 @@
      */
     if (cur_w1 != NULL) {
 
-	syslog(LOG_NOTICE, "Reading %s", cur_w1->address);
-
-	sleep(1);
+	if ((strcmp(cur_w1->family, (char *)"10") == 0) || (strcmp(cur_w1->family, (char *)"22") == 0) || (strcmp(cur_w1->family, (char *)"28") == 0) ||
+	    (strcmp(cur_w1->family, (char *)"3b") == 0) || (strcmp(cur_w1->family, (char *)"42") == 0)) {
+	    syslog(LOG_NOTICE, "Reading %s", cur_w1->address);
+	    sleep(1);
+	}
 
 	if (cur_w1->next != NULL) {
 	    cur_w1 = cur_w1->next;
@@ -243,7 +288,7 @@
     }
 
     sleep(1);
-    SM_PROCEED(Scan);
+    SM_PROCEED(ScanNew);
 
 SM_END
 SM_RETURN

mercurial