coolers/coolers.c

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

mercurial