# HG changeset patch # User Michiel Broek # Date 1400360799 -7200 # Node ID dafbbd5e99227b89bf7083a130846a2f58784c3e # Parent 442357970a34e06bf66618bca47b6fb93e2eefef Prepared unix socket communication diff -r 442357970a34 -r dafbbd5e9922 coolers/coolers.c --- a/coolers/coolers.c Sat May 17 10:50:16 2014 +0200 +++ b/coolers/coolers.c Sat May 17 23:06:39 2014 +0200 @@ -33,7 +33,7 @@ int coolerA = 0; int coolerB = 0; -bool shutdown = false; +bool my_shutdown = false; static pid_t pgrp, mypid; extern bool debug; @@ -41,6 +41,13 @@ extern int lcdHandle; int lcdupdate; +int sock = -1; /* Datagram socket */ +struct sockaddr_un servaddr; /* Server address */ +struct sockaddr_un from; /* From address */ +static char spath[PATH_MAX]; /* Socket path */ +socklen_t fromlen; + + int server(void); void help(void); void die(int); @@ -68,14 +75,14 @@ default: syslog(LOG_NOTICE, "die() on signal %d", onsig); } - shutdown = true; + my_shutdown = true; } void stopLCD(void) { - lcdClear(lcdHandle); + mb_lcdClear(lcdHandle); setBacklight(0); } @@ -158,10 +165,11 @@ } lcdPosition(lcdHandle, 0, 0); - lcdPuts(lcdHandle, "Coolers"); + sprintf(buf, "Coolers"); + mb_lcdPuts(lcdHandle, buf); lcdPosition(lcdHandle, 0, 1); sprintf(buf, "Version %s", VERSION); - lcdPuts(lcdHandle, buf); + mb_lcdPuts(lcdHandle, buf); if (Config.tx433 != -1) { if (debug) @@ -239,7 +247,9 @@ char buf[1024]; w1_therm *tmp1, *old1; rc_switch *tmp2, *old2; - int rc, run = 0, temp; + int rc, run = 0, temp, rlen; + struct pollfd pfd[1]; + if (lockprog((char *)"coolers")) { syslog(LOG_NOTICE, "Can't lock"); @@ -254,10 +264,72 @@ syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc); } + /* + * Setup socket so that clients can communicate with this server. + */ + if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { + fprintf(stderr, "Can't create socket\n"); + return 1; + } + + snprintf(spath, PATH_MAX, "/var/run/coolers.sock"); + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sun_family = AF_UNIX; + strcpy(servaddr.sun_path, spath); + + if (bind(sock, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { + close(sock); + sock = -1; + fprintf(stderr, "Can't bind socket %s\n", spath); + return 1; + } + if (chmod(spath, 0666)) { + fprintf(stderr, "Can't chmod 0666 %s\n", spath); + close(sock); + sock = -1; + unlink(spath); + return 1; + } + if (debug) + fprintf(stdout, "Unix socket created\n"); + snprintf(buf, 1023, "tempA,coolerA,tempB,coolerB"); logger((char *)"coolers.log", (char *)"coolers", buf); do { + /* + * Poll UNIX Datagram socket the defined timeout of one second. + * This means we listen if a client program has something to tell us. + * Timeout is one second, after the timeout the rest of the mainloop is executed. + */ + pfd[0].fd = sock; + pfd[0].events = POLLIN; + pfd[0].revents = 0; + rc = poll(pfd, 1, 1000); + + if (rc == -1) { + /* + * Poll can be interrupted by a finished child so that's not a real error. + */ + if (errno != EINTR) { + syslog(LOG_NOTICE, "poll() rc=%d sock=%d, events=%04x", rc, sock, pfd[0].revents); + } + } else if (rc) { + if (pfd[0].revents & POLLIN) { + /* + * Process the clients request for mbtask commands. + */ + memset(&buf, 0, sizeof(buf)); + fromlen = sizeof(from); + rlen = recvfrom(sock, buf, sizeof(buf) -1, 0, (struct sockaddr *)&from, &fromlen); + if (rlen == -1) { + syslog(LOG_NOTICE, "recvfrom() for socket receiver"); + } else { +// do_cmd(buf); + } + } + } + lcdupdate = FALSE; run = my_mosquitto_loop(); @@ -297,13 +369,13 @@ lcdPosition(lcdHandle, 0, 0); tmp1 = Config.w1therms; snprintf(buf, 16, "%4.1f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerA ? '+' : ' ', tmp1->alias); - lcdPuts(lcdHandle, buf); + mb_lcdPuts(lcdHandle, buf); temp = tmp1->lastval; old1 = tmp1->next; tmp1 = old1; lcdPosition(lcdHandle, 0, 1); snprintf(buf, 16, "%4.1f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, coolerB ? '+' : ' ', tmp1->alias); - lcdPuts(lcdHandle, buf); + mb_lcdPuts(lcdHandle, buf); snprintf(buf, 1023, "%.1f,%s,%.1f,%s", temp / 1000.0, coolerA ? (char *)"on" : (char *)"off", tmp1->lastval / 1000.0, coolerB ? (char *)"on" : (char *)"off"); logger((char *)"coolers.log", (char *)"coolers", buf); @@ -329,6 +401,7 @@ disableTransmit(); ulockprog((char *)"coolers"); + unlink(spath); if (debug) fprintf(stdout, "Goodbye\n"); diff -r 442357970a34 -r dafbbd5e9922 coolers/mosquitto.c --- a/coolers/mosquitto.c Sat May 17 10:50:16 2014 +0200 +++ b/coolers/mosquitto.c Sat May 17 23:06:39 2014 +0200 @@ -42,7 +42,7 @@ static bool disconnect_sent = false; static bool connect_lost = false; -extern bool shutdown; +extern bool my_shutdown; extern bool debug; extern sys_config Config; extern int lcdHandle; @@ -67,7 +67,7 @@ void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc) { - if (shutdown) { + if (my_shutdown) { syslog(LOG_NOTICE, "Acknowledged DISCONNECT from %s", Config.mosq_host); connected = false; } else { @@ -330,7 +330,7 @@ } } - if (shutdown) { + if (my_shutdown) { /* * Final publish 0 to clients//coolers/state */ @@ -338,9 +338,9 @@ mosquitto_publish(mymosq, &mid_sent, state, strlen(buf), buf, qos, true); last_mid = mid_sent; status = STATUS_WAITING; - lcdClear(lcdHandle); - lcdPosition(lcdHandle, 0, 0); - lcdPuts(lcdHandle, "Shuting down ..."); + mb_lcdClear(lcdHandle); +// lcdPosition(lcdHandle, 0, 0); + mb_lcdPuts(lcdHandle, "Shuting down ..."); } } else if (status == STATUS_WAITING) { diff -r 442357970a34 -r dafbbd5e9922 coolers/sensors.c --- a/coolers/sensors.c Sat May 17 10:50:16 2014 +0200 +++ b/coolers/sensors.c Sat May 17 23:06:39 2014 +0200 @@ -28,7 +28,7 @@ extern bool debug; extern sys_config Config; -extern int shutdown; +extern int my_shutdown; @@ -53,7 +53,7 @@ for (tmp1 = Config.w1therms; tmp1; tmp1 = old1) { old1 = tmp1->next; - if (shutdown) { + if (my_shutdown) { syslog(LOG_NOTICE, "Thread my_sensors_loop stopped"); if (debug) fprintf(stdout, "Thread my_sensors_loop stopped\n"); diff -r 442357970a34 -r dafbbd5e9922 lib/lcd-pcf8574.c --- a/lib/lcd-pcf8574.c Sat May 17 10:50:16 2014 +0200 +++ b/lib/lcd-pcf8574.c Sat May 17 23:06:39 2014 +0200 @@ -28,7 +28,24 @@ #ifdef HAVE_WIRINGPI_H -int lcdHandle; +int lcdHandle; +static unsigned char lcdbuf[MAX_LCDS][20][4]; + +struct lcdDataStruct +{ + int bits, rows, cols ; + int rsPin, strbPin ; + int dataPins [8] ; + int cx, cy ; +}; +extern struct lcdDataStruct *lcds [MAX_LCDS]; + + +/* + * Some LCD functions are extended shadow copies of the wiringPi functions. + * The difference is that the lcdbuf will be updated with the contents on + * the hardware display. This copy can then be used for a remote display + */ /* @@ -51,38 +68,76 @@ int initLCD (int cols, int rows) { - if (!((rows == 1) || (rows == 2) || (rows == 4))) - { - fprintf (stderr, "rows must be 1, 2 or 4\n") ; - return EXIT_FAILURE ; - } + int x, y; + + if (!((rows == 1) || (rows == 2) || (rows == 4))) { + fprintf (stderr, "rows must be 1, 2 or 4\n") ; + return EXIT_FAILURE ; + } + + if (!((cols == 16) || (cols == 20))) { + fprintf (stderr, "cols must be 16 or 20\n") ; + return EXIT_FAILURE ; + } + + pcf8574Setup(AF_BASE, 0x27) ; + pinMode (AF_RW, OUTPUT) ; + digitalWrite (AF_RW, LOW) ; // Not used with wiringPi - always in write mode - if (!((cols == 16) || (cols == 20))) - { - fprintf (stderr, "cols must be 16 or 20\n") ; - return EXIT_FAILURE ; - } + /* + * The other control pins are initialised with lcdInit () + */ + lcdHandle = lcdInit (rows, cols, 4, AF_RS, AF_E, AF_DB4, AF_DB5, AF_DB6, AF_DB7, 0, 0, 0, 0) ; + if (lcdHandle < 0) { + fprintf (stderr, "lcdInit failed\n") ; + return -1 ; + } - pcf8574Setup (AF_BASE, 0x27) ; + lcdClear (lcdHandle) ; + for (x = 0; x < 20; x++) + for (y = 0; y < 4; y++) + lcdbuf[lcdHandle][x][y] = ' '; - setBacklight (1) ; + setBacklight (1) ; + + return 0 ; +} - pinMode (AF_RW, OUTPUT) ; - digitalWrite (AF_RW, LOW) ; // Not used with wiringPi - always in write mode + + +void mb_lcdPutchar(const int fd, unsigned char data) +{ + struct lcdDataStruct *lcd = lcds[fd]; - // The other control pins are initialised with lcdInit () + /* + * Write to our buffer first, then to the wiringPi driver. + * Writing to wiringPi updates the cursor position. + */ + lcdbuf[fd][lcd->cx][lcd->cy] = data; + lcdPutchar(fd, data); +} - lcdHandle = lcdInit (rows, cols, 4, AF_RS, AF_E, AF_DB4, AF_DB5, AF_DB6, AF_DB7, 0, 0, 0, 0) ; + - if (lcdHandle < 0) - { - fprintf (stderr, "lcdInit failed\n") ; - return -1 ; - } +void mb_lcdPuts(const int fd, const char *string) +{ + while (*string) + mb_lcdPutchar (fd, *string++); +} + + - lcdClear (lcdHandle) ; - return 0 ; +void mb_lcdClear(const int fd) +{ + int x, y; + + lcdClear(fd); + for (x = 0; x < 20; x++) + for (y = 0; y < 4; y++) + lcdbuf[fd][x][y] = ' '; } + + #endif diff -r 442357970a34 -r dafbbd5e9922 lib/mbselib.h --- a/lib/mbselib.h Sat May 17 10:50:16 2014 +0200 +++ b/lib/mbselib.h Sat May 17 23:06:39 2014 +0200 @@ -19,6 +19,9 @@ #include #include #include +#include +#include +#include /* mosquitto */ @@ -108,6 +111,9 @@ void setBacklight (int); int initLCD (int, int); +void mb_lcdPutchar(const int, unsigned char); +void mb_lcdPuts(const int, const char *); +void mb_lcdClear(const int); /* logger.c */ diff -r 442357970a34 -r dafbbd5e9922 thermometers/main.c --- a/thermometers/main.c Sat May 17 10:50:16 2014 +0200 +++ b/thermometers/main.c Sat May 17 23:06:39 2014 +0200 @@ -37,7 +37,7 @@ static bool connected = true; static bool disconnect_sent = false; static bool connect_lost = false; -static bool shutdown = false; +static bool my_shutdown = false; static pid_t pgrp, mypid; extern bool debug; @@ -73,7 +73,7 @@ default: syslog(LOG_NOTICE, "die() on signal %d", onsig); } - shutdown = true; + my_shutdown = true; } @@ -96,7 +96,7 @@ void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc) { - if (shutdown) { + if (my_shutdown) { syslog(LOG_NOTICE, "Acknowledged DISCONNECT from %s", Config.mosq_host); connected = false; } else { @@ -508,7 +508,7 @@ } #endif - if (shutdown) { + if (my_shutdown) { /* * Final publish 0 to clients//thermometers/state */