coolers/coolers.c

Mon, 05 May 2014 23:33:31 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 05 May 2014 23:33:31 +0200
changeset 26
9322c619c525
child 27
4703cc10b99a
permissions
-rw-r--r--

Added coolers program, first draft

26
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /*****************************************************************************
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * Copyright (C) 2014
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 *
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * Michiel Broek <mbroek at mbse dot eu>
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 *
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 * This file is part of the mbsePi-apps
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 *
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 * This is free software; you can redistribute it and/or modify it
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 * under the terms of the GNU General Public License as published by the
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 * Free Software Foundation; either version 2, or (at your option) any
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 * later version.
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 *
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 * mbsePi-apps is distributed in the hope that it will be useful, but
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 * General Public License for more details.
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 *
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 * along with EC-65K; see the file COPYING. If not, write to the Free
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 *****************************************************************************/
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 #include "../lib/mbselib.h"
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 #include "coolers.h"
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 #include "mosquitto.h"
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26 #include "sensors.h"
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 #ifdef HAVE_WIRINGPI_H
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31 bool shutdown = false;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 static pid_t pgrp, mypid;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34 extern bool debug;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35 extern sys_config Config;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 extern int lcdHandle;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 int lcdupdate;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39 int server(void);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40 void help(void);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 void die(int);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 void help(void)
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 fprintf(stdout, "mbsePi-apps coolers v%s starting\n\n", VERSION);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47 fprintf(stdout, "Usage: coolers [-d] [-h]\n");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 fprintf(stdout, " -d --debug Debug and run in foreground\n");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49 fprintf(stdout, " -h --help Display this help\n");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 void die(int onsig)
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55 {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 switch (onsig) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58 break;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 break;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61 case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62 break;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63 default: syslog(LOG_NOTICE, "die() on signal %d", onsig);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
66 shutdown = true;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
70
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
71 void stopLCD(void)
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72 {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
73 lcdClear(lcdHandle);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
74 setBacklight(0);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
79 int main(int argc, char *argv[])
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
80 {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81 int rc, c, i;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82 pid_t frk;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83 char buf[80];
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85 while (1) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86 int option_index = 0;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87 static struct option long_options[] = {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
88 {"debug", 0, 0, 'c'},
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
89 {"help", 0, 0, 'h'},
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
90 {0, 0, 0, 0}
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
91 };
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
92
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93 c = getopt_long(argc, argv, "dh", long_options, &option_index);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94 if (c == -1)
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 break;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
97 switch (c) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
98 case 'd': debug = true;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
99 break;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
100 case 'h': help();
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
101 return 1;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
102 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
103 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
104
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105 openlog("coolers", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
106 syslog(LOG_NOTICE, "mbsePi-apps coolers v%s starting", VERSION);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
107 if (debug)
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108 fprintf(stdout, "mbsePi-apps coolers v%s starting\n", VERSION);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
109
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
110 if (rdconfig((char *)"coolers.conf")) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
111 fprintf(stderr, "Error reading configuration\n");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
112 syslog(LOG_NOTICE, "halted");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
113 return 1;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
114 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
115
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
116 /*
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
117 * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
118 * but that's live. This daemon should only be stopped by SIGTERM.
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
119 * Don't catch SIGCHLD.
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
120 */
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
121 for (i = 0; i < NSIG; i++) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP))
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
123 signal(i, (void (*))die);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
124 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
125
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
126 if ((rc = initLCD (16, 2))) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
127 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
128 return 1;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
129 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
130
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131 lcdPosition(lcdHandle, 0, 0);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
132 lcdPuts(lcdHandle, "Coolers");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
133 lcdPosition(lcdHandle, 0, 1);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134 sprintf(buf, "Version %s", VERSION);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
135 lcdPuts(lcdHandle, buf);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
136
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
137 if (debug) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138 /*
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
139 * For debugging run in foreground.
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
140 */
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
141 rc = server();
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
142 } else {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143 /*
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
144 * Server initialization is complete. Now we can fork the
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
145 * daemon and return to the user. We need to do a setpgrp
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
146 * so that the daemon will no longer be assosiated with the
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
147 * users control terminal. This is done before the fork, so
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
148 * that the child will not be a process group leader. Otherwise,
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
149 * if the child were to open a terminal, it would become
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
150 * associated with that terminal as its control terminal.
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
151 */
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
152 if ((pgrp = setpgid(0, 0)) == -1) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
153 syslog(LOG_NOTICE, "setpgpid failed");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
154 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
155
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
156 frk = fork();
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
157 switch (frk) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
158 case -1:
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
159 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno));
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
160 syslog(LOG_NOTICE, "Finished, rc=1");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
161 stopLCD();
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
162 exit(1);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
163 case 0: /*
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
164 * Run the daemon
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
165 */
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
166 fclose(stdin);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
167 if (open("/dev/null", O_RDONLY) != 0) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
168 syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
169 _exit(2);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
170 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
171 fclose(stdout);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
172 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
173 syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
174 _exit(2);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
175 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
176 fclose(stderr);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
177 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
178 syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
179 _exit(2);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
180 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
181 mypid = getpid();
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
182 rc = server();
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
183 break;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
184 /* Not reached */
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
185 default:
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
186 /*
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
187 * Here we detach this process and let the child
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
188 * run the deamon process.
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
189 */
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
190 syslog(LOG_NOTICE, "Starting daemon with pid %d", frk);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
191 exit(0);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
192 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
193 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
194
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
195 syslog(LOG_NOTICE, "Finished, rc=%d", rc);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
196 return rc;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
197 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
198
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
199
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
200
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
201 int server(void)
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
202 {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
203 char buf[1024];
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
204 w1_therm *tmp1, *old1;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
205 int run = 0;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
206
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
207 my_mosquitto_init();
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
208
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
209 do {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
210 lcdupdate = FALSE;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
211
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
212 my_sensors_loop();
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
213 run = my_mosquitto_loop();
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
214
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
215 if (run && lcdupdate) {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
216 lcdPosition(lcdHandle, 0, 0);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
217 tmp1 = Config.w1therms;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
218 snprintf(buf, 16, "%4.1f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, ' ', tmp1->alias);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
219 lcdPuts(lcdHandle, buf);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
220 old1 = tmp1->next;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
221 tmp1 = old1;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
222 lcdPosition(lcdHandle, 0, 1);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
223 snprintf(buf, 16, "%4.1f %cC %c %s ", tmp1->lastval / 1000.0, 0xdf, ' ', tmp1->alias);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
224 lcdPuts(lcdHandle, buf);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
225 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
226
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
227 } while (run);
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
228
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
229 if (debug)
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
230 fprintf(stdout, (char *)"Out of loop\n");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
231
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
232 my_mosquitto_exit();
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
233 stopLCD();
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
234
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
235 return 0;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
236 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
237
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
238 #else
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
239
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
240
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
241 int main(int argc, char *argv[])
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
242 {
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
243 fprintf(stderr, "Compiled on a system without a wiringPi library.\n");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
244 fprintf(stderr, "This program is useless and will do nothing.\n");
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
245 return 0;
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
246 }
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
247
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
248
9322c619c525 Added coolers program, first draft
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
249 #endif

mercurial