coolers/coolers.c

changeset 26
9322c619c525
child 27
4703cc10b99a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/coolers/coolers.c	Mon May 05 23:33:31 2014 +0200
@@ -0,0 +1,249 @@
+/*****************************************************************************
+ * Copyright (C) 2014
+ *   
+ * Michiel Broek <mbroek at mbse dot eu>
+ *
+ * This file is part of the mbsePi-apps
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2, or (at your option) any
+ * later version.
+ *
+ * mbsePi-apps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with EC-65K; see the file COPYING.  If not, write to the Free
+ * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ *****************************************************************************/
+
+#include "../lib/mbselib.h"
+#include "coolers.h"
+#include "mosquitto.h"
+#include "sensors.h"
+
+#ifdef HAVE_WIRINGPI_H
+
+
+bool			shutdown = false;
+static pid_t		pgrp, mypid;
+
+extern bool		debug;
+extern sys_config	Config;
+extern int		lcdHandle;
+int			lcdupdate;
+
+int server(void);
+void help(void);
+void die(int);
+
+
+void help(void)
+{
+    fprintf(stdout, "mbsePi-apps coolers v%s starting\n\n", VERSION);
+    fprintf(stdout, "Usage: coolers [-d] [-h]\n");
+    fprintf(stdout, "  -d --debug              Debug and run in foreground\n");
+    fprintf(stdout, "  -h --help               Display this help\n");
+}
+
+
+
+void die(int onsig)
+{
+    switch (onsig) {
+	case SIGHUP:	syslog(LOG_NOTICE, "Got SIGHUP, shutting down");
+			break;
+	case SIGINT:	syslog(LOG_NOTICE, "Keyboard interrupt, shutting down");
+			break;
+	case SIGTERM:	syslog(LOG_NOTICE, "Got SIGTERM, shutting down");
+			break;
+	default:	syslog(LOG_NOTICE, "die() on signal %d", onsig);
+    }
+
+    shutdown = true;
+}
+
+
+
+void stopLCD(void)
+{
+    lcdClear(lcdHandle);
+    setBacklight(0);
+}
+
+
+
+int main(int argc, char *argv[])
+{
+    int		rc, c, i;
+    pid_t	frk;
+    char	buf[80];
+
+    while (1) {
+	int option_index = 0;
+	static struct option long_options[] = {
+	    {"debug", 0, 0, 'c'},
+	    {"help", 0, 0, 'h'},
+	    {0, 0, 0, 0}
+	};
+
+	c = getopt_long(argc, argv, "dh", long_options, &option_index);
+	if (c == -1)
+	    break;
+
+	switch (c) {
+	    case 'd':	debug = true;
+			break;
+	    case 'h':	help();
+			return 1;
+	}
+    }
+
+    openlog("coolers", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER);
+    syslog(LOG_NOTICE, "mbsePi-apps coolers v%s starting", VERSION);
+    if (debug)
+	fprintf(stdout, "mbsePi-apps coolers v%s starting\n", VERSION);
+
+    if (rdconfig((char *)"coolers.conf")) {
+	fprintf(stderr, "Error reading configuration\n");
+	syslog(LOG_NOTICE, "halted");
+	return 1;
+    }
+
+    /*
+     *  Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored
+     *  but that's live. This daemon should only be stopped by SIGTERM.
+     *  Don't catch SIGCHLD.
+     */
+    for (i = 0; i < NSIG; i++) {
+    	if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP))
+	    signal(i, (void (*))die);
+    }
+
+    if ((rc = initLCD (16, 2))) {
+	fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc);
+	return 1;
+    }
+
+    lcdPosition(lcdHandle, 0, 0);
+    lcdPuts(lcdHandle, "Coolers");
+    lcdPosition(lcdHandle, 0, 1);
+    sprintf(buf, "Version %s", VERSION);
+    lcdPuts(lcdHandle, buf);
+
+    if (debug) {
+	/*
+	 * For debugging run in foreground.
+	 */
+	rc = server();
+    } else {
+	/*
+	 * Server initialization is complete. Now we can fork the 
+	 * daemon and return to the user. We need to do a setpgrp
+	 * so that the daemon will no longer be assosiated with the
+	 * users control terminal. This is done before the fork, so
+	 * that the child will not be a process group leader. Otherwise,
+	 * if the child were to open a terminal, it would become
+	 * associated with that terminal as its control terminal.
+	 */
+	if ((pgrp = setpgid(0, 0)) == -1) {
+	    syslog(LOG_NOTICE, "setpgpid failed");
+	}
+
+	frk = fork();
+	switch (frk) {
+	    case -1:	
+		    	syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno));
+			syslog(LOG_NOTICE, "Finished, rc=1");
+			stopLCD();
+			exit(1);
+	    case 0:	/*
+			 * Run the daemon
+			 */
+			fclose(stdin);
+			if (open("/dev/null", O_RDONLY) != 0) {
+			    syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed");
+			    _exit(2);
+			}
+			fclose(stdout);
+			if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) {
+			    syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed");
+			    _exit(2);
+			}
+			fclose(stderr);
+			if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) {
+			    syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed");
+			    _exit(2);
+			}
+			mypid = getpid();
+			rc = server();
+			break;
+			/* Not reached */
+	    default:
+			/*
+			 * Here we detach this process and let the child
+			 * run the deamon process.
+			 */
+			syslog(LOG_NOTICE, "Starting daemon with pid %d", frk);
+			exit(0);
+	}
+    }
+
+    syslog(LOG_NOTICE, "Finished, rc=%d", rc);
+    return rc;
+}
+
+
+
+int server(void)
+{
+    char                buf[1024];
+    w1_therm		*tmp1, *old1;
+    int			run = 0;
+
+    my_mosquitto_init();
+
+    do {
+	lcdupdate = FALSE;
+
+	my_sensors_loop();
+	run = my_mosquitto_loop();
+
+	if (run && lcdupdate) {
+	    lcdPosition(lcdHandle, 0, 0);
+	    tmp1 = Config.w1therms;
+	    snprintf(buf, 16, "%4.1f %cC %c %s            ", tmp1->lastval / 1000.0, 0xdf, ' ', tmp1->alias);
+	    lcdPuts(lcdHandle, buf);
+	    old1 = tmp1->next;
+	    tmp1 = old1;
+	    lcdPosition(lcdHandle, 0, 1);
+	    snprintf(buf, 16, "%4.1f %cC %c %s            ", tmp1->lastval / 1000.0, 0xdf, ' ', tmp1->alias);
+	    lcdPuts(lcdHandle, buf);
+	}
+
+    } while (run);
+
+    if (debug)
+	fprintf(stdout, (char *)"Out of loop\n");
+
+    my_mosquitto_exit();
+    stopLCD();
+
+    return 0;
+}
+
+#else
+
+
+int main(int argc, char *argv[])
+{
+    fprintf(stderr, "Compiled on a system without a wiringPi library.\n");
+    fprintf(stderr, "This program is useless and will do nothing.\n");
+    return 0;
+}
+
+
+#endif

mercurial