# HG changeset patch # User Michiel Broek # Date 1408049021 -7200 # Node ID 6bdda35b4a1317858d8737d378363d85f76faecc # Parent 793c09ece5423a958c2520d711155d8202c38498 Added thread locks for device processing diff -r 793c09ece542 -r 6bdda35b4a13 thermferm/devices.c --- a/thermferm/devices.c Thu Aug 14 22:02:33 2014 +0200 +++ b/thermferm/devices.c Thu Aug 14 22:43:41 2014 +0200 @@ -178,7 +178,7 @@ now = time(NULL); for (device = Config.devices; device; device = device->next) { - if (strcmp(uuid, device->uuid) == 0) { + if (! strcmp(uuid, device->uuid)) { /* * Execute command if different then the old value. But also * every 2 minutes because commands can have temporary @@ -189,18 +189,20 @@ #ifdef HAVE_WIRINGPI_H rc = 0; if ((device->type == DEVTYPE_RC433) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { - enableTransmit(device->gpiopin); snprintf(buf, 39, "%s,%d", device->address, value ? 1:0); for (i = 0; i < strlen(buf); i++) if (buf[i] == '-') - buf[i] = ','; + buf[i] = ','; + enableTransmit(device->gpiopin); rc = toggleSwitch(buf); + disableTransmit(); syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc); if (debug) fprintf(stdout, "RC433 command %s rc=%d\n", buf, rc); - disableTransmit(); + piLock(LOCK_DEVICES); device->value = value; device->timestamp = time(NULL); + piUnlock(LOCK_DEVICES); return rc; } @@ -436,7 +438,13 @@ if ((fp = fopen(addr, "r"))) { if (device->present != DEVPRESENT_YES) { syslog(LOG_WARNING, "sensor %s is back", device->address); +#ifdef HAVE_WIRINGPI_H + piLock(LOCK_DEVICES); +#endif device->present = DEVPRESENT_YES; +#ifdef HAVE_WIRINGPI_H + piUnlock(LOCK_DEVICES); +#endif } /* * The output looks like: @@ -452,19 +460,37 @@ strtok(line, (char *)"="); p = strtok(NULL, (char *)"="); rc = sscanf(p, "%d", &temp); +#ifdef HAVE_WIRINGPI_H + piLock(LOCK_DEVICES); +#endif if ((rc == 1) && (device->value != temp)) { device->value = temp; device->timestamp = time(NULL); } +#ifdef HAVE_WIRINGPI_H + piUnlock(LOCK_DEVICES); +#endif } else { syslog(LOG_WARNING, "sensor %s CRC error", device->address); +#ifdef HAVE_WIRINGPI_H + piLock(LOCK_DEVICES); +#endif device->present = DEVPRESENT_ERROR; +#ifdef HAVE_WIRINGPI_H + piUnlock(LOCK_DEVICES); +#endif } fclose(fp); } else { if (device->present != DEVPRESENT_NO) { syslog(LOG_WARNING, "sensor %s is missing", device->address); +#ifdef HAVE_WIRINGPI_H + piLock(LOCK_DEVICES); +#endif device->present = DEVPRESENT_NO; +#ifdef HAVE_WIRINGPI_H + piUnlock(LOCK_DEVICES); +#endif } } free(addr); @@ -484,6 +510,7 @@ dht11_last = now; } if (device->subdevice == 0) { + piLock(LOCK_DEVICES); if (dht11_valid) { device->value = dht11_temperature * 1000; device->timestamp = time(NULL); @@ -491,7 +518,9 @@ } else { device->present = DEVPRESENT_ERROR; } + piUnlock(LOCK_DEVICES); } else if (device->subdevice == 1) { + piLock(LOCK_DEVICES); if (dht11_valid) { device->value = dht11_humidity * 1000; device->timestamp = time(NULL); @@ -499,6 +528,7 @@ } else { device->present = DEVPRESENT_ERROR; } + piUnlock(LOCK_DEVICES); } break; #endif diff -r 793c09ece542 -r 6bdda35b4a13 thermferm/rdconfig.c --- a/thermferm/rdconfig.c Thu Aug 14 22:02:33 2014 +0200 +++ b/thermferm/rdconfig.c Thu Aug 14 22:43:41 2014 +0200 @@ -507,6 +507,9 @@ syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterStartElement"); return 1; } +#ifdef HAVE_WIRINGPI_H + piLock(LOCK_DEVICES); +#endif for (device = Config.devices; device; device = device->next) { if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "DEVICE")) < 0) { syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterStartElement"); @@ -573,6 +576,9 @@ return 1; } } +#ifdef HAVE_WIRINGPI_H + piUnlock(LOCK_DEVICES); +#endif if ((rc = xmlTextWriterEndElement(writer)) < 0) { syslog(LOG_WARNING, "wrconfig: error at xmlTextWriterEndElement"); diff -r 793c09ece542 -r 6bdda35b4a13 thermferm/server.c --- a/thermferm/server.c Thu Aug 14 22:02:33 2014 +0200 +++ b/thermferm/server.c Thu Aug 14 22:43:41 2014 +0200 @@ -97,12 +97,18 @@ for (device = Config.devices; device; device = device->next) { if (strcmp(device->uuid, uuid) == 0) { +#ifdef HAVE_WIRINGPI_H + piLock(LOCK_DEVICES); +#endif if (plus == TRUE) { device->inuse++; } else { if (device->inuse) device->inuse--; } +#ifdef HAVE_WIRINGPI_H + piUnlock(LOCK_DEVICES); +#endif } } } @@ -215,7 +221,7 @@ char *opt, *param, *kwd, *val, ibuf[SS_BUFSIZE]; devices_list *device, *tmpd; socklen_t fromlen; - int i, rlen, ival; + int i, rc, rlen, ival; uuid_t uu; opt = strtok(buf, " \0"); @@ -265,6 +271,9 @@ device->description = xstrcpy((char *)"Describe me here"); device->comment = xstrcpy((char *)"Comment here"); +#ifdef HAVE_WIRINGPI_H + piLock(LOCK_DEVICES); +#endif if (Config.devices == NULL) { Config.devices = device; } else { @@ -275,6 +284,9 @@ } } } +#ifdef HAVE_WIRINGPI_H + piUnlock(LOCK_DEVICES); +#endif syslog(LOG_NOTICE, "Device %s added", device->uuid); srv_send((char *)"211 Device %s added", device->uuid); return 0; @@ -287,7 +299,14 @@ if (strcmp(opt, (char *)"DEL") == 0) { - if (delete_Device(param)) { +#ifdef HAVE_WIRINGPI_H + piLock(LOCK_DEVICES); +#endif + rc = delete_Device(param); +#ifdef HAVE_WIRINGPI_H + piUnlock(LOCK_DEVICES); +#endif + if (rc) { syslog(LOG_NOTICE, "Device %s deleted", param); srv_send((char *)"211 Device %s deleted", param); return 0; diff -r 793c09ece542 -r 6bdda35b4a13 thermferm/thermferm.c --- a/thermferm/thermferm.c Thu Aug 14 22:02:33 2014 +0200 +++ b/thermferm/thermferm.c Thu Aug 14 22:43:41 2014 +0200 @@ -252,7 +252,13 @@ for (device = Config.devices; device; device = device->next) { if (strcmp(address, device->uuid) == 0) { +#ifdef HAVE_WIRINGPI_H + piLock(LOCK_DEVICES); +#endif tmp = device->value + device->offset; +#ifdef HAVE_WIRINGPI_H + piUnlock(LOCK_DEVICES); +#endif *val = tmp; return device->present; } diff -r 793c09ece542 -r 6bdda35b4a13 thermferm/thermferm.h --- a/thermferm/thermferm.h Thu Aug 14 22:02:33 2014 +0200 +++ b/thermferm/thermferm.h Thu Aug 14 22:43:41 2014 +0200 @@ -45,6 +45,19 @@ #include +/* + * Thread locks + */ +#define LOCK_DEVICES 0 +#define LOCK_UNITS 1 +#define LOCK_SPARE1 2 +#define LOCK_SPARE2 3 + + + +/* + * Frontpanel menu numbers + */ #define MENU_NONE 0 #define MENU_TOP_DEFAULT 1 #define MENU_TOP_UNITS 2