thermferm/devices.c

changeset 645
49eb753a958b
parent 644
07cc86900473
child 646
e3edc783006b
--- a/thermferm/devices.c	Sat Mar 23 09:31:01 2024 +0100
+++ b/thermferm/devices.c	Sat Mar 23 12:18:14 2024 +0100
@@ -29,6 +29,7 @@
 
 extern sys_config	Config;
 extern int		my_shutdown;
+extern pthread_mutex_t  mutexes[5];
 
 #ifdef USE_SIMULATOR
 
@@ -39,6 +40,7 @@
 
 #endif
 
+
 #ifdef HAVE_WIRINGPI_H
 
 #define MAXTIMINGS 100
@@ -299,10 +301,7 @@
 	return 0;
 
     now = time(NULL);
-
-#ifdef HAVE_WIRINGPI_H
-    piLock(LOCK_DEVICES);
-#endif
+    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 
     for (device = Config.devices; device; device = device->next) {
 	if (! strcmp(uuid, device->uuid)) {
@@ -329,15 +328,15 @@
 		    for (i = 0; i < strlen(buf); i++)
 			if (buf[i] == '-')
 			    buf[i] = ',';
-		    piUnlock(LOCK_DEVICES);
+		    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 	    	    enableTransmit(device->gpiopin);	    
 		    rc = toggleSwitch(buf);
 		    disableTransmit();
-		    piLock(LOCK_DEVICES);
+		    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 		    syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc);
                     device->value = value;
 		    device->timestamp = time(NULL);
-		    piUnlock(LOCK_DEVICES);
+		    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 		    return rc;
 	        }
 
@@ -398,17 +397,13 @@
 		}
 #endif
 	    } else {
-#ifdef HAVE_WIRINGPI_H
-		piUnlock(LOCK_DEVICES);
-#endif
+		pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 		return 0;
 	    } // if ((test_value != my_value) || (((int)now - (int)my_timestamp) >= 120))
 	} // if (! strcmp(uuid, device->uuid))
     } // for (device = Config.devices; device; device = device->next)
-#ifdef HAVE_WIRINGPI_H
-    piUnlock(LOCK_DEVICES);
-#endif
 
+    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
     return 0;
 }	   
 
@@ -425,9 +420,7 @@
     if (uuid == NULL)
 	return 0;
 
-#ifdef HAVE_WIRINGPI_H
-    piLock(LOCK_DEVICES);
-#endif
+    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 
     for (device = Config.devices; device; device = device->next) {
 	if (! strcmp(uuid, device->uuid)) {
@@ -437,18 +430,13 @@
 	    } else {
 		tmp = 0;
 	    }
-#ifdef HAVE_WIRINGPI_H
-	    piUnlock(LOCK_DEVICES);
-#endif
+	    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 	    *value = tmp;
 	    return present;
 	}
     }
 
-#ifdef HAVE_WIRINGPI_H
-    piUnlock(LOCK_DEVICES);
-#endif
-
+    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
     return DEVPRESENT_NO;
 }
 
@@ -552,6 +540,10 @@
 	closedir(fd);
     }
 
+    /*
+     * DHT11 as kernel module.
+     */
+
 #ifdef HAVE_WIRINGPI_H
     if (piBoardRev() == 2) {
 	/*
@@ -597,7 +589,7 @@
 		} else if (i == 3) {
 		    ndev->direction = DEVDIR_INTERN;
 		    ndev->inuse = 1;
-		    ndev->comment = xstrcpy((char *)"DHT11 room sensor");
+		    ndev->comment = xstrcpy((char *)"DHT11 sensor");
 		} else if (i == PANEL_LED) {
 		    ndev->direction = DEVDIR_OUT_BIN;
 		    ndev->inuse = 1;
@@ -725,11 +717,7 @@
 
 
 
-#ifdef HAVE_WIRINGPI_H
-PI_THREAD (my_devices_loop)
-#else
 void *my_devices_loop(void *threadid)
-#endif
 {
     devices_list	*device;
 #ifdef USE_SIMULATOR
@@ -815,13 +803,9 @@
 				    /* CRC is Ok, continue */
 				    if (device->present != DEVPRESENT_YES) {
 					syslog(LOG_NOTICE, "sensor %s is Ok", device->address);
-#ifdef HAVE_WIRINGPI_H
-					piLock(LOCK_DEVICES);
-#endif
+					pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 					device->present = DEVPRESENT_YES;
-#ifdef HAVE_WIRINGPI_H
-					piUnlock(LOCK_DEVICES);
-#endif
+					pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				    }
 
 				    line2[strlen(line2)-1] = '\0';
@@ -829,9 +813,7 @@
 				    p = strtok(NULL, (char *)"=");
 				    rc = sscanf(p, "%d", &temp);
 				    if ((rc == 1) && (device->value != temp)) {
-#ifdef HAVE_WIRINGPI_H
-					piLock(LOCK_DEVICES);
-#endif
+					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;
@@ -840,30 +822,20 @@
 				    	    device->timestamp = time(NULL);
 					    device->present = DEVPRESENT_YES;
 					}
-#ifdef HAVE_WIRINGPI_H
-				    	piUnlock(LOCK_DEVICES);
-#endif
+					pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				    }
 				} else {
 				    syslog(LOG_NOTICE, "sensor %s CRC error '%s`", device->address, line1);
-#ifdef HAVE_WIRINGPI_H
-				    piLock(LOCK_DEVICES);                
-#endif
+				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				    device->present = DEVPRESENT_ERROR;
-#ifdef HAVE_WIRINGPI_H
-				    piUnlock(LOCK_DEVICES);              
-#endif
+				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				}
 			    } else {
 				if (device->present != DEVPRESENT_NO) {
 				    syslog(LOG_NOTICE, "sensor %s is missing", device->address);
-#ifdef HAVE_WIRINGPI_H
-				    piLock(LOCK_DEVICES);                
-#endif
+				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				    device->present = DEVPRESENT_NO;
-#ifdef HAVE_WIRINGPI_H          
-				    piUnlock(LOCK_DEVICES);              
-#endif
+				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				}
 			    }
 			    free(addr);
@@ -880,13 +852,9 @@
 			    if ((access(addr, R_OK)) == 0) {
 				if (device->present != DEVPRESENT_YES) {
 				    syslog(LOG_NOTICE, "DS2413 %s is back", device->address);
-#ifdef HAVE_WIRINGPI_H
-				    piLock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				    device->present = DEVPRESENT_YES;
-#ifdef HAVE_WIRINGPI_H
-				    piUnlock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				}
 				/*
 				 * First make sure that if this device is configured as input
@@ -911,9 +879,7 @@
 				    }
 				}
 				if ((rc = read_w1(device->address, (char *)"state")) >= 0) {
-#ifdef HAVE_WIRINGPI_H
-				    piLock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				    /*
 				     * Read PIOA or PIOB pin state bits
 				     */
@@ -922,20 +888,14 @@
 				    else if (device->subdevice == 1)
 					device->value = (rc & 0x04) ? 0 : 1;
 				    device->timestamp = time(NULL);
-#ifdef HAVE_WIRINGPI_H
-				    piUnlock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				}
 			    } else {
 				if (device->present != DEVPRESENT_NO) {
 				    syslog(LOG_NOTICE, "DS2413 %s is missing", device->address);
-#ifdef HAVE_WIRINGPI_H
-				    piLock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				    device->present = DEVPRESENT_NO;
-#ifdef HAVE_WIRINGPI_H          
-				    piUnlock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				}
 			    }
 			    free(addr);
@@ -956,7 +916,7 @@
 				/* Read once during subdevice 0 */
 			    	dht11_pin = device->gpiopin;
 			    	dht11Read();
-			    	piLock(LOCK_DEVICES);
+				pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 			    	if (dht11_valid) {
 				    device->value = dht11_temperature * 1000;
 				    device->timestamp = time(NULL);
@@ -964,10 +924,10 @@
 			    	} else {
 				    device->present = DEVPRESENT_ERROR;
 			    	}
-			    	piUnlock(LOCK_DEVICES);
+				pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 			    } else if (device->subdevice == 1) {
 				/* Data already present */
-			    	piLock(LOCK_DEVICES);
+				pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 			    	if (dht11_valid) {
 				    device->value = dht11_humidity * 1000;
 				    device->timestamp = time(NULL);
@@ -975,7 +935,7 @@
 			    	} else {
 				    device->present = DEVPRESENT_ERROR;
 			    	}
-			    	piUnlock(LOCK_DEVICES);
+				pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				dht11_last = now;	/* Okay for a new reading. */
 			    }
 			}
@@ -983,20 +943,18 @@
 
 		case DEVTYPE_GPIO:
 			if (device->direction == DEVDIR_IN_BIN) {
-			    piLock(LOCK_DEVICES);
+			    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 			    device->value = digitalRead(device->gpiopin);
 			    device->offset = 0;
 			    device->timestamp = time(NULL);
-			    piUnlock(LOCK_DEVICES);
+			    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 			}
 			break;
 
 #endif
 #ifdef USE_SIMULATOR
 		case DEVTYPE_SIM:
-#ifdef HAVE_WIRINGPI_H
-			piLock(LOCK_DEVICES);
-#endif
+			pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 			if (Config.simulators) {
 			    simulator = Config.simulators;
 			    if (device->subdevice == 0) {
@@ -1016,9 +974,7 @@
 				device->timestamp = time(NULL);
 			    }
 			}
-#ifdef HAVE_WIRINGPI_H
-			piUnlock(LOCK_DEVICES);
-#endif
+			pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 			break;
 #endif
 		default:

mercurial