thermferm/devices.c

changeset 656
ca47c742a25d
parent 654
e981d0185485
child 657
38162f374842
--- a/thermferm/devices.c	Tue Apr 02 15:29:16 2024 +0200
+++ b/thermferm/devices.c	Wed Apr 03 12:14:45 2024 +0200
@@ -33,6 +33,8 @@
 extern sys_config	Config;
 extern int		my_shutdown;
 extern pthread_mutex_t  mutexes[5];
+extern w1_list		*w1_devices;
+
 
 #ifdef USE_SIMULATOR
 
@@ -393,99 +395,77 @@
 {
     struct dirent	*de;
     DIR			*fd;
+    w1_list		*dev_w1;
     devices_list	*device, *ndev;
-    int			found, subdevices, ival, i, rc = 0;
-    char		buf[40];
+    int			found, subdevices, i, rc = 0;
     uuid_t		uu;
 #ifdef HAVE_WIRINGPI_H
     int			pin;
 #endif
 
     /*
-     * Scan for 1-wire devices
+     * Scan for 1-wire devices. These are already detected by the
+     * one-wire thread that is already running. So we just check
+     * these detected devices.
      */
-    if ((fd = opendir((char *)"/sys/bus/w1/devices"))) {
-	while ((de = readdir(fd))) {
-	    if (de->d_name[0] != '.') {
-		found = FALSE;
-		for (device = Config.devices; device; device = device->next) {
-		    if (strcmp(device->address,de->d_name) == 0) {
-			found = TRUE;
-			break;
+    for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
+	found = FALSE;
+	for (device = Config.devices; device; device = device->next) {
+	    if (strcmp(device->address, dev_w1->address) == 0) {
+		found = TRUE;
+		break;
+	    }
+	}
+
+	if (found == FALSE) {
+	    for (i = 0; i < dev_w1->subdevices; i++) {
+		ndev = (devices_list *)malloc(sizeof(devices_list));
+		ndev->next = NULL;
+		ndev->uuid = malloc(37);
+		uuid_generate(uu);
+		uuid_unparse(uu, ndev->uuid);
+		ndev->type = DEVTYPE_W1;
+		ndev->direction = DEVDIR_IN_ANALOG;
+		if (strcmp(dev_w1->family, (char *)"10") == 0) {
+		    ndev->description = xstrcpy((char *)"DS18S20 Digital thermometer");
+		} else if (strcmp(dev_w1->family, (char *)"22") == 0) {
+		    ndev->description = xstrcpy((char *)"DS1822 Digital thermometer");
+		} else if (strcmp(dev_w1->family, (char *)"28") == 0) {
+		    ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer");
+		} else if (strcmp(dev_w1->family, (char *)"3a") == 0) {
+		    ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch");
+		    ndev->direction = DEVDIR_IN_BIN;
+		} else if (strcmp(dev_w1->family, (char *)"3b") == 0) {
+		    ndev->description = xstrcpy((char *)"DS1825 Digital thermometer");
+		} else if (strcmp(dev_w1->family, (char *)"42") == 0) {
+		    ndev->description = xstrcpy((char *)"DS28EA00 Digital thermometer");
+		} else {
+		    ndev->description = xstrcpy((char *)"Unknown device family ");
+		    ndev->direction = DEVDIR_UNDEF;
+		}
+		ndev->value = ndev->offset = ndev->inuse = 0;
+		ndev->present = DEVPRESENT_YES;
+		ndev->address = xstrcpy(dev_w1->address);
+		ndev->subdevice = i;
+		ndev->gpiopin = -1;
+		ndev->comment = xstrcpy((char *)"Auto detected device");
+		ndev->timestamp = time(NULL);
+
+		syslog(LOG_NOTICE, "New W1 device %s, subdevice %d, %s", ndev->address, ndev->subdevice, ndev->description);
+
+		if (Config.devices == NULL) {
+		    Config.devices = ndev;
+		} else {
+		    for (device = Config.devices; device; device = device->next) {
+			if (device->next == NULL) {
+			    device->next = ndev;
+			    break;
+			}
 		    }
 		}
-
-		if (found == FALSE) {
-		    strncpy(buf, de->d_name, 2);
-		    buf[2] = '\0';
-		    sscanf(buf, "%02x", &ival);
-		    subdevices = 0;
-
-		    /*
-		     * ival is zero when a ghost sensor is detected.
-		     */
-		    if (ival > 0)
-		    	subdevices = 1;
-		    if (strcmp(buf, (char *)"3a") == 0)
-			subdevices = 2;
-		    for (i = 0; i < subdevices; i++) {
-			ndev = (devices_list *)malloc(sizeof(devices_list));
-			ndev->next = NULL;
-			ndev->uuid = malloc(37);
-			uuid_generate(uu);
-			uuid_unparse(uu, ndev->uuid);
-			ndev->type = DEVTYPE_W1;
-			ndev->direction = DEVDIR_UNDEF;
-			if (strcmp(buf, (char *)"10") == 0) {
-			    ndev->direction = DEVDIR_IN_ANALOG;
-			    ndev->description = xstrcpy((char *)"DS18S20 Digital thermometer");
-			} else if (strcmp(buf, (char *)"22") == 0) {
-			    ndev->direction = DEVDIR_IN_ANALOG;
-			    ndev->description = xstrcpy((char *)"DS1822 Digital thermometer");
-			} else if (strcmp(buf, (char *)"28") == 0) {
-			    ndev->direction = DEVDIR_IN_ANALOG;
-			    ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer");
-			} else if (strcmp(buf, (char *)"3a") == 0) {
-			    ndev->direction = DEVDIR_IN_BIN;
-			    ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch");
-			} else if (strcmp(buf, (char *)"3b") == 0) {
-			    ndev->direction = DEVDIR_IN_ANALOG;
-			    ndev->description = xstrcpy((char *)"DS1825 Digital thermometer");
-			} else if (strcmp(buf, (char *)"42") == 0) {
-			    ndev->direction = DEVDIR_IN_ANALOG;
-			    ndev->description = xstrcpy((char *)"DS28EA00 Digital thermometer");
-			} else if (strcmp(buf, (char *)"w1") == 0) {
-			    ndev->description = xstrcpy((char *)"Master System device");
-			} else {
-			    ndev->description = xstrcpy((char *)"Unknown device family ");
-			    ndev->description = xstrcat(ndev->description, buf);
-			}
-			ndev->value = ndev->offset = ndev->inuse = 0;
-			ndev->present = DEVPRESENT_YES;
-			ndev->address = xstrcpy(de->d_name);
-			ndev->subdevice = i;
-			ndev->gpiopin = -1;
-			ndev->comment = xstrcpy((char *)"Auto detected device");
-			ndev->timestamp = time(NULL);
-
-			syslog(LOG_NOTICE, "New W1 device %s, subdevice %d, %s", ndev->address, ndev->subdevice, ndev->description);
-
-			if (Config.devices == NULL) {
-			    Config.devices = ndev;
-			} else {
-			    for (device = Config.devices; device; device = device->next) {
-				if (device->next == NULL) {
-				    device->next = ndev;
-				    break;
-				}
-			    }
-			}
-			rc++;
-		    }
-		}
+		rc++;
 	    }
 	}
-	closedir(fd);
     }
 
     /*

mercurial