thermferm/devices.c

changeset 660
a28ef4d9afa4
parent 658
b10d0f6337a3
child 661
8c1e7a52e24f
--- a/thermferm/devices.c	Wed Apr 03 19:33:38 2024 +0200
+++ b/thermferm/devices.c	Fri Apr 05 16:19:39 2024 +0200
@@ -28,10 +28,10 @@
 #include "xutil.h"
 
 
+int			my_devices_shutdown = 0;
 int			my_devices_state = 0;
 
 extern sys_config	Config;
-extern int		my_shutdown;
 extern pthread_mutex_t  mutexes[5];
 extern w1_list		*w1_devices;
 
@@ -314,8 +314,10 @@
 
 			    if ((write_w1(device->address, (char *)"output", output)) == 0) {
 			    	syslog(LOG_NOTICE, "DS2413 PIO%c value=%d (%s)", (device->subdevice == 0) ? 'A' : 'B', (value == 0) ? 0 : 1, device->comment);
+				pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				device->value = (value == 0) ? 0 : 1;
 				device->timestamp = time(NULL);
+				pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 			    }
 			}
 		    }
@@ -419,7 +421,8 @@
 	}
 
 	if (found == FALSE) {
-	    for (i = 0; i < dev_w1->subdevices; i++) {
+	    subdevices = (strcmp(dev_w1->family, (char *)"3a") == 0) ? 2:1;
+	    for (i = 0; i < subdevices; i++) {
 		ndev = (devices_list *)malloc(sizeof(devices_list));
 		ndev->next = NULL;
 		ndev->uuid = malloc(37);
@@ -714,12 +717,13 @@
 void *my_devices_loop(void *threadid)
 {
     devices_list	*device;
+    w1_list		*dev_w1;
 #ifdef USE_SIMULATOR
     simulator_list	*simulator;
 #endif
-    char		*addr = NULL, line1[60], line2[60], *p = NULL;
-    FILE		*fp;
-    int			temp, rc;
+//    char		*addr = NULL, line1[60], line2[60], *p = NULL;
+//    FILE		*fp;
+    int			rc, found;
     time_t		now;
 
     my_devices_state = 1;
@@ -735,27 +739,27 @@
     /*
      * Set the temperature sensors to 12 bits resolution and write it in EEPROM
      */
-    for (device = Config.devices; device; device = device->next) {
-	if ((device->type == DEVTYPE_W1) &&
-	    ((strncmp(device->address, (char *)"10", 2) == 0) ||
-             (strncmp(device->address, (char *)"22", 2) == 0) ||
-             (strncmp(device->address, (char *)"28", 2) == 0) ||
-             (strncmp(device->address, (char *)"3b", 2) == 0) ||
-             (strncmp(device->address, (char *)"42", 2) == 0))) {
-	    addr = xstrcpy((char *)"/sys/bus/w1/devices/");
-            addr = xstrcat(addr, device->address);
-            addr = xstrcat(addr, (char *)"/w1_slave");
-            if ((fp = fopen(addr, "w"))) {
-		rc = fprintf(fp, "12\n0\n");	// According to the kernel documentation. Seems to work.
-		fclose(fp);
-		if (rc != 5) {
-		    syslog(LOG_NOTICE, "Program 12 bits resolution error rc=%d for %s", rc, addr);
-                }
-	    }
-	    free(addr);
-	    addr = NULL;
-	}
-    }
+//    for (device = Config.devices; device; device = device->next) {
+//	if ((device->type == DEVTYPE_W1) &&
+//	    ((strncmp(device->address, (char *)"10", 2) == 0) ||
+  //           (strncmp(device->address, (char *)"22", 2) == 0) ||
+//             (strncmp(device->address, (char *)"28", 2) == 0) ||
+//             (strncmp(device->address, (char *)"3b", 2) == 0) ||
+//             (strncmp(device->address, (char *)"42", 2) == 0))) {
+//	    addr = xstrcpy((char *)"/sys/bus/w1/devices/");
+//            addr = xstrcat(addr, device->address);
+//            addr = xstrcat(addr, (char *)"/w1_slave");
+//            if ((fp = fopen(addr, "w"))) {
+//		rc = fprintf(fp, "12\n0\n");	// According to the kernel documentation. Seems to work.
+//		fclose(fp);
+//		if (rc != 5) {
+//		    syslog(LOG_NOTICE, "Program 12 bits resolution error rc=%d for %s", rc, addr);
+//                }
+//	    }
+//	    free(addr);
+//	    addr = NULL;
+//	}
+//    }
 
     /*
      * Loop forever until the external shutdown variable is set.
@@ -767,7 +771,7 @@
     	 */
     	for (device = Config.devices; device; device = device->next) {
 
-	    if (my_shutdown)
+	    if (my_devices_shutdown)
 		break;
 
 	    switch (device->type) {
@@ -781,51 +785,23 @@
 			    (strncmp(device->address, (char *)"28", 2) == 0) ||
 			    (strncmp(device->address, (char *)"3b", 2) == 0) ||
 			    (strncmp(device->address, (char *)"42", 2) == 0)) {
-			    addr = xstrcpy((char *)"/sys/bus/w1/devices/");
-			    addr = xstrcat(addr, device->address);
-			    addr = xstrcat(addr, (char *)"/w1_slave");
-			    if ((fp = fopen(addr, "r"))) {
-				fgets(line1, 50, fp);	// Read 2 lines
-				fgets(line2, 50, fp);
-				fclose(fp);
-				/*
-				 * The output looks like:
-				 * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES
-				 * 72 01 4b 46 7f ff 0e 10 57 t=23125
-				 */
-				line1[strlen(line1)-1] = '\0';
-				if ((line1[36] == 'Y') && (line1[37] == 'E')) {
-				    /* CRC is Ok, continue */
-				    if (device->present != DEVPRESENT_YES) {
-					syslog(LOG_NOTICE, "sensor %s is Ok", device->address);
-					pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
+			    found = FALSE;
+			    for (dev_w1 = w1_devices; dev_w1; dev_w1 = dev_w1->next) {
+				if (strcmp(device->address, dev_w1->address) == 0) {
+				    syslog(LOG_NOTICE, "sensor %s value %d", dev_w1->address, dev_w1->value);
+				    found = TRUE;
+				    if ((dev_w1->value == -1) || (dev_w1->value < -55000)) {
+					syslog(LOG_NOTICE, "sensor %s value error %d", device->address, dev_w1->value);
+					device->present = DEVPRESENT_ERROR;
+				    } else {
+					device->value = dev_w1->value;
+					device->timestamp = time(NULL);
 					device->present = DEVPRESENT_YES;
-					pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				    }
+				}
+			    }
 
-				    line2[strlen(line2)-1] = '\0';
-				    strtok(line2, (char *)"=");
-				    p = strtok(NULL, (char *)"=");
-				    rc = sscanf(p, "%d", &temp);
-				    if ((rc == 1) && (device->value != temp)) {
-					pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
-					if (temp < -55000) {
-					    syslog(LOG_NOTICE, "sensor %s value error '%d` '%s`", device->address, temp, line1);
-					    device->present = DEVPRESENT_ERROR;
-					} else {
-					    device->value = temp;
-				    	    device->timestamp = time(NULL);
-					    device->present = DEVPRESENT_YES;
-					}
-					pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
-				    }
-				} else {
-				    syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line1);
-				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
-				    device->present = DEVPRESENT_ERROR;
-				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
-				}
-			    } else {
+			    if (found == FALSE) {
 				if (device->present != DEVPRESENT_NO) {
 				    syslog(LOG_NOTICE, "sensor %s is missing", device->address);
 				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
@@ -833,8 +809,7 @@
 				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				}
 			    }
-			    free(addr);
-			    addr = NULL;
+
 			} /* if temperature sensor */
 
 			break;
@@ -914,12 +889,14 @@
 			break;
 	    }
 
+	    if (my_devices_shutdown)
+		break;
 	    /*
 	     * Delay a bit after procesing a device.
 	     */
 	    mDelay(10);
 	}
-	if (my_shutdown)
+	if (my_devices_shutdown)
 	    break;
 	/*
 	 * Delay a bit after all devices

mercurial