|
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 "mash.h" |
|
24 #include "lock.h" |
|
25 #include "logger.h" |
|
26 #include "xutil.h" |
|
27 #include "beerxml.h" |
|
28 |
|
29 #ifdef HAVE_WIRINGPI_H |
|
30 |
|
31 |
|
32 int tempA = 80; |
|
33 int tempB = 80; |
|
34 int coolerA = 0; |
|
35 int coolerB = 0; |
|
36 |
|
37 bool my_shutdown = false; |
|
38 static pid_t pgrp, mypid; |
|
39 |
|
40 extern bool debug; |
|
41 extern sys_config Config; |
|
42 extern int lcdHandle; |
|
43 extern unsigned char lcdbuf[MAX_LCDS][20][4]; |
|
44 int lcdupdate; |
|
45 |
|
46 |
|
47 void help(void); |
|
48 void die(int); |
|
49 void stopLCD(void); |
|
50 int server(void); |
|
51 |
|
52 |
|
53 void help(void) |
|
54 { |
|
55 fprintf(stdout, "mbsePi-apps mash v%s starting\n\n", VERSION); |
|
56 fprintf(stdout, "Usage: mash [-d] [-h]\n"); |
|
57 fprintf(stdout, " -d --debug Debug and run in foreground\n"); |
|
58 fprintf(stdout, " -h --help Display this help\n"); |
|
59 } |
|
60 |
|
61 |
|
62 |
|
63 void die(int onsig) |
|
64 { |
|
65 switch (onsig) { |
|
66 case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); |
|
67 break; |
|
68 case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); |
|
69 break; |
|
70 case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); |
|
71 break; |
|
72 default: syslog(LOG_NOTICE, "die() on signal %d", onsig); |
|
73 } |
|
74 |
|
75 my_shutdown = true; |
|
76 } |
|
77 |
|
78 |
|
79 |
|
80 void stopLCD(void) |
|
81 { |
|
82 mb_lcdClear(lcdHandle); |
|
83 setBacklight(0); |
|
84 } |
|
85 |
|
86 |
|
87 |
|
88 int main(int argc, char *argv[]) |
|
89 { |
|
90 int rc, c, i; |
|
91 pid_t frk; |
|
92 char buf[80]; |
|
93 |
|
94 while (1) { |
|
95 int option_index = 0; |
|
96 static struct option long_options[] = { |
|
97 {"debug", 0, 0, 'c'}, |
|
98 {"help", 0, 0, 'h'}, |
|
99 {0, 0, 0, 0} |
|
100 }; |
|
101 |
|
102 c = getopt_long(argc, argv, "dh", long_options, &option_index); |
|
103 if (c == -1) |
|
104 break; |
|
105 |
|
106 switch (c) { |
|
107 case 'd': debug = true; |
|
108 break; |
|
109 case 'h': help(); |
|
110 return 1; |
|
111 } |
|
112 } |
|
113 |
|
114 |
|
115 parseBeerXML((char *)"/home/mbroek/Downloads/SalmoGold.xml"); |
|
116 |
|
117 return 0; |
|
118 |
|
119 openlog("mash", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); |
|
120 syslog(LOG_NOTICE, "mbsePi-apps mash v%s starting", VERSION); |
|
121 if (debug) |
|
122 fprintf(stdout, "mbsePi-apps mash v%s starting\n", VERSION); |
|
123 |
|
124 if (rdconfig((char *)"mash.conf")) { |
|
125 fprintf(stderr, "Error reading configuration\n"); |
|
126 syslog(LOG_NOTICE, "halted"); |
|
127 return 1; |
|
128 } |
|
129 |
|
130 /* |
|
131 * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored |
|
132 * but that's live. This daemon should only be stopped by SIGTERM. |
|
133 * Don't catch SIGCHLD. |
|
134 */ |
|
135 for (i = 0; i < NSIG; i++) { |
|
136 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) |
|
137 signal(i, (void (*))die); |
|
138 } |
|
139 |
|
140 if (wiringPiSetup () ) |
|
141 return 1; |
|
142 |
|
143 if ((rc = initLCD (16, 2))) { |
|
144 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); |
|
145 return 1; |
|
146 } |
|
147 |
|
148 lcdPosition(lcdHandle, 0, 0); |
|
149 sprintf(buf, " Mash"); |
|
150 mb_lcdPuts(lcdHandle, buf); |
|
151 lcdPosition(lcdHandle, 0, 1); |
|
152 sprintf(buf, " Version %s", VERSION); |
|
153 mb_lcdPuts(lcdHandle, buf); |
|
154 |
|
155 if (debug) { |
|
156 /* |
|
157 * For debugging run in foreground. |
|
158 */ |
|
159 rc = server(); |
|
160 } else { |
|
161 /* |
|
162 * Server initialization is complete. Now we can fork the |
|
163 * daemon and return to the user. We need to do a setpgrp |
|
164 * so that the daemon will no longer be assosiated with the |
|
165 * users control terminal. This is done before the fork, so |
|
166 * that the child will not be a process group leader. Otherwise, |
|
167 * if the child were to open a terminal, it would become |
|
168 * associated with that terminal as its control terminal. |
|
169 */ |
|
170 if ((pgrp = setpgid(0, 0)) == -1) { |
|
171 syslog(LOG_NOTICE, "setpgpid failed"); |
|
172 } |
|
173 |
|
174 frk = fork(); |
|
175 switch (frk) { |
|
176 case -1: |
|
177 syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); |
|
178 syslog(LOG_NOTICE, "Finished, rc=1"); |
|
179 stopLCD(); |
|
180 exit(1); |
|
181 case 0: /* |
|
182 * Run the daemon |
|
183 */ |
|
184 fclose(stdin); |
|
185 if (open("/dev/null", O_RDONLY) != 0) { |
|
186 syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed"); |
|
187 _exit(2); |
|
188 } |
|
189 fclose(stdout); |
|
190 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) { |
|
191 syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed"); |
|
192 _exit(2); |
|
193 } |
|
194 fclose(stderr); |
|
195 if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { |
|
196 syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed"); |
|
197 _exit(2); |
|
198 } |
|
199 mypid = getpid(); |
|
200 rc = server(); |
|
201 break; |
|
202 /* Not reached */ |
|
203 default: |
|
204 /* |
|
205 * Here we detach this process and let the child |
|
206 * run the deamon process. |
|
207 */ |
|
208 syslog(LOG_NOTICE, "Starting daemon with pid %d", frk); |
|
209 exit(0); |
|
210 } |
|
211 } |
|
212 |
|
213 syslog(LOG_NOTICE, "Finished, rc=%d", rc); |
|
214 return rc; |
|
215 } |
|
216 |
|
217 |
|
218 |
|
219 int server(void) |
|
220 { |
|
221 char buf[1024]; |
|
222 w1_therm *tmp1; |
|
223 int rc, run = 1, temp; |
|
224 |
|
225 if (lockprog((char *)"mash")) { |
|
226 syslog(LOG_NOTICE, "Can't lock"); |
|
227 return 1; |
|
228 } |
|
229 |
|
230 rc = piThreadCreate(my_sensors_loop); |
|
231 if (rc) { |
|
232 fprintf(stderr, "my_sensors_loop thread didn't start rc=%d\n", rc); |
|
233 syslog(LOG_NOTICE, "my_sensors_loop thread didn't start rc=%d", rc); |
|
234 } |
|
235 |
|
236 // rc = piThreadCreate(my_server_loop); |
|
237 // if (rc) { |
|
238 // fprintf(stderr, "my_server_loop thread didn't start rc=%d\n", rc); |
|
239 // syslog(LOG_NOTICE, "my_server_loop thread didn't start rc=%d", rc); |
|
240 // } |
|
241 |
|
242 snprintf(buf, 1023, "temp,step,description"); |
|
243 logger((char *)"mash.log", (char *)"mash", buf); |
|
244 |
|
245 do { |
|
246 lcdupdate = FALSE; |
|
247 |
|
248 if (my_shutdown) |
|
249 run = 0; |
|
250 |
|
251 tmp1 = Config.w1therms; |
|
252 // if (((tmp1->lastval / 100) < (tempA - 5)) && (coolerA == 1)) { |
|
253 // syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); |
|
254 // lcdupdate = TRUE; |
|
255 // } |
|
256 // if (((tmp1->lastval / 100) > (tempA + 5)) && (coolerA == 0)) { |
|
257 // syslog(LOG_NOTICE, "Temperature A is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); |
|
258 // lcdupdate = TRUE; |
|
259 // } |
|
260 if (tmp1->update) { |
|
261 tmp1->update = FALSE; |
|
262 lcdupdate = TRUE; |
|
263 } |
|
264 // old1 = tmp1->next; |
|
265 // tmp1 = old1; |
|
266 // if (((tmp1->lastval / 100) < (tempB - 5)) && (coolerB == 1)) { |
|
267 // syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler off", (tmp1->lastval / 1000.0)); |
|
268 // lcdupdate = TRUE; |
|
269 // } |
|
270 // if (((tmp1->lastval / 100) > (tempB + 5)) && (coolerB == 0)) { |
|
271 // syslog(LOG_NOTICE, "Temperature B is %.1f, switched cooler on", (tmp1->lastval / 1000.0)); |
|
272 // lcdupdate = TRUE; |
|
273 // } |
|
274 // if (tmp1->update) { |
|
275 // tmp1->update = FALSE; |
|
276 // lcdupdate = TRUE; |
|
277 // } |
|
278 |
|
279 if (run && lcdupdate) { |
|
280 lcdPosition(lcdHandle, 0, 0); |
|
281 tmp1 = Config.w1therms; |
|
282 snprintf(buf, 16, "%4.1f %cC %s ", tmp1->lastval / 1000.0, 0xdf, tmp1->alias); |
|
283 mb_lcdPuts(lcdHandle, buf); |
|
284 temp = tmp1->lastval; |
|
285 // old1 = tmp1->next; |
|
286 // tmp1 = old1; |
|
287 lcdPosition(lcdHandle, 0, 1); |
|
288 snprintf(buf, 16, " "); |
|
289 mb_lcdPuts(lcdHandle, buf); |
|
290 snprintf(buf, 1023, "%.1f,%d,%s", temp / 1000.0, 1, (char *)"step description"); |
|
291 logger((char *)"mash.log", (char *)"mash", buf); |
|
292 } |
|
293 usleep(100000); |
|
294 |
|
295 } while (run); |
|
296 |
|
297 if (debug) |
|
298 fprintf(stdout, (char *)"Out of loop\n"); |
|
299 |
|
300 /* |
|
301 * Give threads time to cleanup |
|
302 */ |
|
303 usleep(1500000); |
|
304 |
|
305 stopLCD(); |
|
306 |
|
307 // wrconfig((char *)"mash.conf"); |
|
308 |
|
309 ulockprog((char *)"mash"); |
|
310 |
|
311 if (debug) |
|
312 fprintf(stdout, "Goodbye\n"); |
|
313 |
|
314 return 0; |
|
315 } |
|
316 |
|
317 #else |
|
318 |
|
319 |
|
320 int main(int argc, char *argv[]) |
|
321 { |
|
322 parseBeerXML((char *)"/home/mbroek/Downloads/beerxml.xml"); |
|
323 printBeerXML(); |
|
324 // fprintf(stderr, "Compiled on a system without a wiringPi library.\n"); |
|
325 // fprintf(stderr, "This program is useless and will do nothing.\n"); |
|
326 return 0; |
|
327 } |
|
328 |
|
329 |
|
330 #endif |