thermferm/server.c

changeset 645
49eb753a958b
parent 614
389097dc665d
child 646
e3edc783006b
--- a/thermferm/server.c	Sat Mar 23 09:31:01 2024 +0100
+++ b/thermferm/server.c	Sat Mar 23 12:18:14 2024 +0100
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2008-2022
+ * Copyright (C) 2008-2024
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
@@ -41,6 +41,7 @@
 extern const char	DEVPRESENT[4][6];
 extern const char	DEVDIR[7][11];
 extern const char	PROFSTATE[5][6];
+extern pthread_mutex_t	mutexes[5];
 
 
 int			s;		/* connected socket			*/
@@ -166,18 +167,14 @@
 
     for (device = Config.devices; device; device = device->next) {
 	if (strcmp(device->uuid, uuid) == 0) {
-#ifdef HAVE_WIRINGPI_H
-	    piLock(LOCK_DEVICES);
-#endif
+	    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 	    if (plus == TRUE) {
 		device->inuse++;
 	    } else {
 		if (device->inuse)
 		    device->inuse--;
 	    }
-#ifdef HAVE_WIRINGPI_H
-	    piUnlock(LOCK_DEVICES);
-#endif
+	    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 	}
     }
 }
@@ -371,9 +368,7 @@
 	    device->description = xstrcpy((char *)"Describe me here");
 	    device->comment = xstrcpy((char *)"Comment here");
 
-#ifdef HAVE_WIRINGPI_H
-	    piLock(LOCK_DEVICES);
-#endif
+	    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 	    if (Config.devices == NULL) {
 		Config.devices = device;
 	    } else {
@@ -384,9 +379,7 @@
 		    }
 		}
 	    }
-#ifdef HAVE_WIRINGPI_H
-	    piUnlock(LOCK_DEVICES);
-#endif
+	    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 	    syslog(LOG_NOTICE, "Device %s added", device->uuid);
 	    srv_send((char *)"211 Device %s added", device->uuid);
 	    return 1;
@@ -399,13 +392,9 @@
 
     if (strcmp(opt, (char *)"DEL") == 0) {
 	
-#ifdef HAVE_WIRINGPI_H
-	piLock(LOCK_DEVICES);
-#endif
+	pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 	rc = delete_Device(param);
-#ifdef HAVE_WIRINGPI_H
-	piUnlock(LOCK_DEVICES);
-#endif
+	pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 	if (rc) {
 	    syslog(LOG_NOTICE, "Device %s deleted", param);
 	    srv_send((char *)"211 Device %s deleted", param);
@@ -419,14 +408,10 @@
     if (strcmp(opt, (char *)"GET") == 0) {
 	for (device = Config.devices; device; device = device->next) {
 	    if (strcmp(device->uuid, param) == 0) {
-#ifdef HAVE_WIRINGPI_H
-		piLock(LOCK_DEVICES);
-#endif
+		pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 		int	my_value = device->value;
 		int	my_timestamp = (int)device->timestamp;
-#ifdef HAVE_WIRINGPI_H
-		piUnlock(LOCK_DEVICES);
-#endif
+		pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 		srv_send((char *)"213 Device record follows:");
 		srv_send((char *)"TYPE,%s", DEVTYPE[device->type]);
 		srv_send((char *)"ADDRESS,%s", device->address);
@@ -467,15 +452,11 @@
 			    if (strcmp(kwd, (char *)"TYPE") == 0) {
 				for (i = 0; i < 8; i++) {
 				    if (strcmp(val, DEVTYPE[i]) == 0) {
-#ifdef HAVE_WIRINGPI_H
-					piLock(LOCK_DEVICES);
-#endif
+					pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 					if (device->type != i)
 					    syslog(LOG_NOTICE, "Device %s changed type %s to %s", device->uuid, DEVTYPE[device->type], DEVTYPE[i]);
 					device->type = i;
-#ifdef HAVE_WIRINGPI_H
-					piUnlock(LOCK_DEVICES);
-#endif
+					pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 					break;
 				    }
 				}
@@ -483,128 +464,92 @@
 			    } else if (strcmp(kwd, (char *)"DIRECTION") == 0) {
 				for (i = 0; i < 7; i++) {
 				    if (strcmp(val, DEVDIR[i]) == 0) {
-#ifdef HAVE_WIRINGPI_H
-					piLock(LOCK_DEVICES);
-#endif
+					pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 					if (device->direction != i)
 					    syslog(LOG_NOTICE, "Device %s changed direction %s to %s", device->uuid, DEVDIR[device->type], DEVDIR[i]);
 					device->direction = i;
-#ifdef HAVE_WIRINGPI_H
-					piUnlock(LOCK_DEVICES);
-#endif
+					pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 					break;
 				    }
 				}
 
 			    } else if (strcmp(kwd, (char *)"VALUE") == 0) {
 				if (sscanf(val, "%d", &ival) == 1) {
-#ifdef HAVE_WIRINGPI_H
-				    piLock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
     				    if (device->value != ival)
 					syslog(LOG_NOTICE, "Device %s changed value %d to %d", device->uuid, device->value, ival);
 				    device->value = ival;
-#ifdef HAVE_WIRINGPI_H
-				    piUnlock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				}
 
 			    } else if (strcmp(kwd, (char *)"OFFSET") == 0) {
 				if (sscanf(val, "%d", &ival) == 1) {
-#ifdef HAVE_WIRINGPI_H
-				    piLock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
     				    if (device->offset != ival)
 					syslog(LOG_NOTICE, "Device %s changed offset %d to %d", device->uuid, device->offset, ival);
 				    device->offset = ival;
-#ifdef HAVE_WIRINGPI_H
-				    piUnlock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				}
 
 			    } else if (strcmp(kwd, (char *)"PRESENT") == 0) {
 				for (i = 0; i < 4; i++) {
 				    if (strcmp(val, DEVPRESENT[i]) == 0) {
-#ifdef HAVE_WIRINGPI_H
-					piLock(LOCK_DEVICES);
-#endif
+					pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 					if (device->present != i)
 					    syslog(LOG_NOTICE, "Device %s changed present %s to %s", device->uuid, DEVPRESENT[device->present], DEVPRESENT[i]);
 					device->present = i;
-#ifdef HAVE_WIRINGPI_H
-					piUnlock(LOCK_DEVICES);
-#endif
+					pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 					break;
 				    }
 				}
 
 			    } else if (strcmp(kwd, (char *)"ADDRESS") == 0) {
-#ifdef HAVE_WIRINGPI_H
-				piLock(LOCK_DEVICES);
-#endif
+				pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				if (device->address) {
 				    if (strcmp(device->address, val))
 					syslog(LOG_NOTICE, "Device %s changed address `%s' to `%s'", device->uuid, device->address, val);
 				    free(device->address);
 				}
 				device->address = xstrcpy(val);
-#ifdef HAVE_WIRINGPI_H               
-				piUnlock(LOCK_DEVICES);
-#endif
+				pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 
 			    } else if (strcmp(kwd, (char *)"SUBDEVICE") == 0) {
 				if (sscanf(val, "%d", &ival) == 1) {
-#ifdef HAVE_WIRINGPI_H
-				    piLock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				    if (device->subdevice != ival)
 					syslog(LOG_NOTICE, "Device %s changed subdevice %d to %d", device->uuid, device->subdevice, ival);
 				    device->subdevice = ival;
-#ifdef HAVE_WIRINGPI_H               
-				    piUnlock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				}
 
 			    } else if (strcmp(kwd, (char *)"GPIOPIN") == 0) {
 				if (sscanf(val, "%d", &ival) == 1) {
-#ifdef HAVE_WIRINGPI_H
-				    piLock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				    if (device->gpiopin != ival)
 					syslog(LOG_NOTICE, "Device %s changed gpiopin %d to %d", device->uuid, device->gpiopin, ival);
 				    device->gpiopin = ival;
-#ifdef HAVE_WIRINGPI_H               
-				    piUnlock(LOCK_DEVICES);
-#endif
+				    pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 				}
 
 			    } else if (strcmp(kwd, (char *)"DESCRIPTION") == 0) {
-#ifdef HAVE_WIRINGPI_H
-				piLock(LOCK_DEVICES);
-#endif
+				pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				if (device->description) {
 				    if (strcmp(device->description, val))
 					syslog(LOG_NOTICE, "Device %s changed description `%s' to `%s'", device->uuid, device->description, val);
 				    free(device->description);
 				}
 				device->description = xstrcpy(val);
-#ifdef HAVE_WIRINGPI_H               
-				piUnlock(LOCK_DEVICES);
-#endif
+				pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 
 			    } else if (strcmp(kwd, (char *)"COMMENT") == 0) {
-#ifdef HAVE_WIRINGPI_H
-				piLock(LOCK_DEVICES);
-#endif
+				pthread_mutex_lock(&mutexes[LOCK_DEVICES]);
 				if (device->comment) {
 				    if (strcmp(device->comment, val))
 					syslog(LOG_NOTICE, "Device %s changed comment `%s' to `%s'", device->uuid, device->comment, val);
 				    free(device->comment);
 				}
 				device->comment = xstrcpy(val);
-#ifdef HAVE_WIRINGPI_H               
-				piUnlock(LOCK_DEVICES);
-#endif
+				pthread_mutex_unlock(&mutexes[LOCK_DEVICES]);
 
 			    }
 			}

mercurial