# HG changeset patch # User Michiel Broek # Date 1408367409 -7200 # Node ID b01b6238eb675d7ca3cb3a5508ff572cc421adad # Parent 2f868eaefec276010005d1e21edd263fa6b50f8f You can now safely add and remove units on a running system. diff -r 2f868eaefec2 -r b01b6238eb67 thermferm/Makefile --- a/thermferm/Makefile Sat Aug 16 17:11:09 2014 +0200 +++ b/thermferm/Makefile Mon Aug 18 15:10:09 2014 +0200 @@ -64,6 +64,6 @@ lcd-pcf8574.o: thermferm.h lcd-pcf8574.h thermferm.o: lock.h logger.h rdconfig.h devices.h server.h thermferm.h lcd-pcf8574.h lcd-buffer.h panel.h futil.h xutil.h xutil.o: thermferm.h xutil.h -server.o: rdconfig.h thermferm.h logger.h devices.h server.h xutil.h +server.o: rdconfig.h thermferm.h logger.h devices.h server.h lcd-buffer.h xutil.h rdconfig.o: rdconfig.h thermferm.h futil.h xutil.h # End of generated dependencies diff -r 2f868eaefec2 -r b01b6238eb67 thermferm/lcd-buffer.c --- a/thermferm/lcd-buffer.c Sat Aug 16 17:11:09 2014 +0200 +++ b/thermferm/lcd-buffer.c Mon Aug 18 15:10:09 2014 +0200 @@ -38,6 +38,24 @@ /* + * Reset the LCD screens chain, it will automatic rebuild. + */ +void lcd_buf_reset(void) +{ + lcd_rows *tmp, *old; + + for (tmp = my_lcd_rows; tmp; tmp = old) { + old = tmp->next; + tmp->next = NULL; + free(tmp); + } + current_lines = current_offset = 0; + my_lcd_rows = NULL; +} + + + +/* * Write to buffer array. Allocate more lines if needed. */ void lcd_buf_write(int row, const char *format, ...) diff -r 2f868eaefec2 -r b01b6238eb67 thermferm/lcd-buffer.h --- a/thermferm/lcd-buffer.h Sat Aug 16 17:11:09 2014 +0200 +++ b/thermferm/lcd-buffer.h Mon Aug 18 15:10:09 2014 +0200 @@ -8,6 +8,7 @@ } lcd_rows; +void lcd_buf_reset(void); void lcd_buf_write(int, const char *, ...); void lcd_buf_step(int); void lcd_buf_show(void); diff -r 2f868eaefec2 -r b01b6238eb67 thermferm/rdconfig.c --- a/thermferm/rdconfig.c Sat Aug 16 17:11:09 2014 +0200 +++ b/thermferm/rdconfig.c Mon Aug 18 15:10:09 2014 +0200 @@ -33,7 +33,6 @@ const char TEMPSTATE[3][8] = { "OK", "MISSING", "ERROR" }; const char UNITMODE[5][8] = { "OFF", "NONE", "FRIDGE", "BEER", "PROFILE" }; -const char UNITmode[5] = { 'o', 'n', 'f', 'b', 'p' }; const char PROFSTATE[5][6] = { "OFF", "PAUSE", "RUN", "DONE", "ABORT" }; const char DEVTYPE[7][6] = { "NA", "W1", "GPIO", "RC433", "DHT", "I2C", "SPI" }; const char DEVPRESENT[4][6] = { "UNDEF", "NO", "YES", "ERROR" }; diff -r 2f868eaefec2 -r b01b6238eb67 thermferm/server.c --- a/thermferm/server.c Sat Aug 16 17:11:09 2014 +0200 +++ b/thermferm/server.c Mon Aug 18 15:10:09 2014 +0200 @@ -25,14 +25,16 @@ #include "logger.h" #include "devices.h" #include "server.h" +#include "lcd-buffer.h" #include "xutil.h" extern int my_shutdown; extern int debug; +extern int run_pause; +extern int run_hold; extern sys_config Config; extern const char UNITMODE[5][8]; -extern const char UNITmode[5]; extern const char TEMPSTATE[3][8]; extern const char DEVTYPE[7][6]; extern const char DEVPRESENT[4][6]; @@ -963,6 +965,7 @@ if (current->profile) free(current->profile); current->profile = NULL; + previous->next = current->next; free(current); current = previous->next; return 1; @@ -990,7 +993,7 @@ units_list *unit, *tmpu; uuid_t uu; socklen_t fromlen; - int ival, i, rlen; + int ival, i, rc, rlen; float fval; opt = strtok(buf, " \0"); @@ -1038,6 +1041,16 @@ unit->prof_started = unit->prof_paused = (time_t)0; unit->PID_err_old = unit->PID_I_err = 0.0; + /* + * Block main process + */ + run_pause = TRUE; + for (;;) { + usleep(100000); + if (run_hold) + break; + } + if (Config.units == NULL) { Config.units = unit; } else { @@ -1048,6 +1061,8 @@ } } } + lcd_buf_reset(); + run_pause = FALSE; syslog(LOG_NOTICE, "Unit %s added", unit->uuid); srv_send((char *)"211 Unit %s added", unit->uuid); @@ -1055,7 +1070,21 @@ } if (strcmp(opt, (char *)"DEL") == 0) { - if (delete_Unit(param)) { + /* + * Block main process. + */ + run_pause = TRUE; + for (;;) { + usleep(100000); + if (run_hold) + break; + } + + rc = delete_Unit(param); + lcd_buf_reset(); + run_pause = FALSE; + + if (rc) { syslog(LOG_NOTICE, "Unit %s deleted", param); srv_send((char *)"211 Unit %s deleted", param); return 0; diff -r 2f868eaefec2 -r b01b6238eb67 thermferm/thermferm.c --- a/thermferm/thermferm.c Sat Aug 16 17:11:09 2014 +0200 +++ b/thermferm/thermferm.c Mon Aug 18 15:10:09 2014 +0200 @@ -35,6 +35,8 @@ int my_shutdown = FALSE; static pid_t pgrp, mypid; +int run_pause = FALSE; +int run_hold = FALSE; extern int debug; extern sys_config Config; @@ -500,6 +502,31 @@ if (my_shutdown) run = 0; + /* + * Use to stop processing units. Should be used when a unit is + * added or removed. + */ + if (run_pause) { + run_hold = TRUE; + syslog(LOG_NOTICE, "run_pause: entering hold state"); + for (;;) { + usleep(100000); + if (! run_pause) + break; + } + syslog(LOG_NOTICE, "run_pause: leaving hold state"); + run_hold = FALSE; +#ifdef HAVE_WIRINGPI_H + /* + * In case the LCD buffers were cleared, setup the first page. + */ + piLock(LOCK_LCD); + lcd_buf_write(1, (char *)" ThermFerm "); + lcd_buf_write(2, (char *)" Version %s ", VERSION); + piUnlock(LOCK_LCD); +#endif + } + now = time(NULL); if (now != last) { /*