More device locks in the network server

Sat, 07 Mar 2015 13:36:45 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 07 Mar 2015 13:36:45 +0100
changeset 325
35b6f6605ae3
parent 324
940a668e568f
child 326
ae34b47700bb

More device locks in the network server

thermferm/devices.c file | annotate | diff | comparison | revisions
thermferm/server.c file | annotate | diff | comparison | revisions
--- a/thermferm/devices.c	Sat Mar 07 12:08:00 2015 +0100
+++ b/thermferm/devices.c	Sat Mar 07 13:36:45 2015 +0100
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (C) 2014
+ * Copyright (C) 2014..2015
  *   
  * Michiel Broek <mbroek at mbse dot eu>
  *
--- a/thermferm/server.c	Sat Mar 07 12:08:00 2015 +0100
+++ b/thermferm/server.c	Sat Mar 07 13:36:45 2015 +0100
@@ -323,10 +323,18 @@
     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
+		int	my_value = device->value;
+		int	my_timestamp = (int)device->timestamp;
+#ifdef HAVE_WIRINGPI_H
+		piUnlock(LOCK_DEVICES);
+#endif
 		srv_send((char *)"213 Device record follows:");
 		srv_send((char *)"TYPE,%s", DEVTYPE[device->type]);
 		srv_send((char *)"DIRECTION,%s", DEVDIR[device->direction]);
-		srv_send((char *)"VALUE,%d", device->value);
+		srv_send((char *)"VALUE,%d", my_value);
 		srv_send((char *)"OFFSET,%d", device->offset);
 		srv_send((char *)"PRESENT,%s", DEVPRESENT[device->present]);
 		srv_send((char *)"ADDRESS,%s", device->address);
@@ -335,7 +343,7 @@
 		srv_send((char *)"DESCRIPTION,%s", device->description);
 		srv_send((char *)"INUSE,%d", device->inuse);
 		srv_send((char *)"COMMENT,%s", device->comment);
-		srv_send((char *)"TIMESTAMP,%d", (int)device->timestamp);
+		srv_send((char *)"TIMESTAMP,%d", my_timestamp);
 		srv_send((char *)".");
 		return 1;
 	    }
@@ -383,9 +391,15 @@
 			    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
 					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
 					break;
 				    }
 				}
@@ -393,74 +407,128 @@
 			    } 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
 					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
 					break;
 				    }
 				}
 
 			    } else if (strcmp(kwd, (char *)"VALUE") == 0) {
 				if (sscanf(val, "%d", &ival) == 1) {
-				    if (device->value != ival)
+#ifdef HAVE_WIRINGPI_H
+				    piLock(LOCK_DEVICES);
+#endif
+    				    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
 				}
 
 			    } else if (strcmp(kwd, (char *)"OFFSET") == 0) {
 				if (sscanf(val, "%d", &ival) == 1) {
-				    if (device->offset != ival)
+#ifdef HAVE_WIRINGPI_H
+				    piLock(LOCK_DEVICES);
+#endif
+    				    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
 				}
 
 			    } 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
 					if (device->present != i)
 					    syslog(LOG_NOTICE, "Device %s changed present %s to %s", device->uuid, DEVPRESENT[device->present], DEVPRESENT[i]);
 					device->present = i;
-				        break;
+#ifdef HAVE_WIRINGPI_H
+					piUnlock(LOCK_DEVICES);
+#endif
+					break;
 				    }
 				}
 
 			    } else if (strcmp(kwd, (char *)"ADDRESS") == 0) {
+#ifdef HAVE_WIRINGPI_H
+				piLock(LOCK_DEVICES);
+#endif
 				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
 
 			    } else if (strcmp(kwd, (char *)"SUBDEVICE") == 0) {
 				if (sscanf(val, "%d", &ival) == 1) {
+#ifdef HAVE_WIRINGPI_H
+				    piLock(LOCK_DEVICES);
+#endif
 				    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
 				}
 
 			    } else if (strcmp(kwd, (char *)"GPIOPIN") == 0) {
 				if (sscanf(val, "%d", &ival) == 1) {
+#ifdef HAVE_WIRINGPI_H
+				    piLock(LOCK_DEVICES);
+#endif
 				    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
 				}
 
 			    } else if (strcmp(kwd, (char *)"DESCRIPTION") == 0) {
+#ifdef HAVE_WIRINGPI_H
+				piLock(LOCK_DEVICES);
+#endif
 				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
 
 			    } else if (strcmp(kwd, (char *)"COMMENT") == 0) {
+#ifdef HAVE_WIRINGPI_H
+				piLock(LOCK_DEVICES);
+#endif
 				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
 
 			    }
 			}

mercurial