Tue, 20 May 2014 17:07:32 +0200
Mirror LCD display on the webpage works
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
1 | /***************************************************************************** |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
2 | * Copyright (C) 2008-2014 |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
3 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
4 | * Michiel Broek <mbroek at mbse dot eu> |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
5 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
6 | * This file is part of the mbsePi-apps |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
7 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
8 | * This is free software; you can redistribute it and/or modify it |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
9 | * under the terms of the GNU General Public License as published by the |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
10 | * Free Software Foundation; either version 2, or (at your option) any |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
11 | * later version. |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
12 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
13 | * mbsePi-apps is distributed in the hope that it will be useful, but |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
16 | * General Public License for more details. |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
17 | * |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
18 | * You should have received a copy of the GNU General Public License |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
19 | * along with EC-65K; see the file COPYING. If not, write to the Free |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
20 | * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
21 | *****************************************************************************/ |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
22 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
23 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
24 | #include "../lib/mbselib.h" |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
25 | #include "server.h" |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
26 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
28 | extern bool my_shutdown; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
29 | extern bool debug; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
30 | extern int lcdHandle; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
31 | extern unsigned char lcdbuf[MAX_LCDS][20][4]; |
43 | 32 | extern sys_config Config; |
33 | extern int clients; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
35 | /* beer settings */ |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
36 | float cs_beerSet = 20.0; |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
37 | float cs_fridgeSet = 20.0; |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
38 | unsigned char cs_mode = 'o'; /* o = Off, f = fridge, b = beer, p = profile-run */ |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
39 | |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
40 | |
43 | 41 | int s; /* connected socket */ |
42 | int ls; /* listen socket */ | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | |
43 | 44 | struct sockaddr_in myaddr_in; /* for local socket address */ |
45 | struct sockaddr_in peeraddr_in; /* for peer socket address */ | |
46 | ||
47 | struct hostent *hp; | |
48 | ||
49 | #define SS_BUFSIZE 1024 | |
50 | #define SS_TIMEOUT 300 | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
52 | |
43 | 53 | /* |
54 | * Send message to client | |
55 | */ | |
56 | int srv_send(const char *format, ...) | |
57 | { | |
58 | char out[SS_BUFSIZE]; | |
59 | va_list va_ptr; | |
60 | ||
61 | if (s == -1) | |
62 | return -1; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
63 | |
43 | 64 | va_start(va_ptr, format); |
65 | vsnprintf(out, SS_BUFSIZE-1, format, va_ptr); | |
66 | va_end(va_ptr); | |
67 | ||
44 | 68 | syslog(LOG_NOTICE, "send: \"%s\"", out); |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
69 | if (debug) |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
70 | fprintf(stdout, "send: \"%s\"\n", out); |
44 | 71 | |
43 | 72 | if (send(s, out, strlen(out), 0) != strlen(out)) { |
73 | syslog(LOG_NOTICE, "srv_send failed"); | |
74 | return -1; | |
75 | } | |
76 | ||
77 | if (send(s, (char *)"\r\n", 2, 0) != 2) { | |
78 | syslog(LOG_NOTICE, "srv_send failed"); | |
79 | return -1; | |
80 | } | |
81 | ||
82 | return 0; | |
83 | } | |
84 | ||
85 | ||
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
86 | |
43 | 87 | void cmd_die(int onsig) |
88 | { | |
89 | syslog(LOG_NOTICE, "Server process die on signal %d", onsig); | |
90 | close(s); | |
91 | exit(0); | |
92 | } | |
93 | ||
94 | ||
95 | ||
96 | void cmd_server(void) | |
97 | { | |
44 | 98 | char *hostname, buf[SS_BUFSIZE], obuf[SS_BUFSIZE]; |
43 | 99 | int i, rc, rlen, timer; |
100 | socklen_t fromlen; | |
101 | struct pollfd pfd[1]; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
102 | |
43 | 103 | /* |
104 | * Close listen socket | |
105 | */ | |
106 | close(ls); | |
107 | /* | |
108 | * Install private signal handler. | |
109 | */ | |
110 | for (i = 0; i < NSIG; i++) { | |
111 | if ((i == SIGHUP) || (i == SIGPIPE) || (i == SIGBUS) || (i == SIGILL) || (i == SIGSEGV)) | |
112 | signal(i, (void (*))cmd_die); | |
113 | else | |
114 | signal(i, SIG_IGN); | |
115 | } | |
116 | ||
117 | hp = gethostbyaddr ((char *) &peeraddr_in.sin_addr, sizeof(struct in_addr), peeraddr_in.sin_family); | |
118 | if (hp == NULL) { | |
119 | hostname = inet_ntoa(peeraddr_in.sin_addr); | |
120 | } else { | |
121 | hostname = hp->h_name; | |
122 | } | |
123 | ||
124 | clients++; | |
125 | syslog(LOG_NOTICE, "Start new client connection (%d) from %s port %u", clients, hostname, ntohs(peeraddr_in.sin_port)); | |
44 | 126 | if (debug) |
127 | fprintf(stdout, "Start new client connection (%d) from %s port %u\n", clients, hostname, ntohs(peeraddr_in.sin_port)); | |
43 | 128 | timer = SS_TIMEOUT * 4; |
129 | ||
130 | /* | |
131 | * Receive loop | |
132 | */ | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
133 | for (;;) { |
43 | 134 | /* |
135 | * Poll socket until a define timeout of 0,25 second. | |
136 | */ | |
137 | pfd[0].fd = s; | |
138 | pfd[0].events = POLLIN; | |
139 | pfd[0].revents = 0; | |
140 | rc = poll(pfd, 1, 250); | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
141 | |
43 | 142 | if (rc == -1) { |
143 | /* | |
144 | * Poll can be interrupted by a finished child so that's not a real error. | |
145 | */ | |
146 | if (errno != EINTR) { | |
147 | syslog(LOG_NOTICE, "poll() rc=%d sock=%d events=%04x", rc, s, pfd[0].revents); | |
148 | } | |
149 | } else if (rc) { | |
150 | if (pfd[0].revents & POLLIN) { | |
151 | memset((char *)&buf, 0, SS_BUFSIZE); | |
152 | fromlen = sizeof(peeraddr_in); | |
153 | rlen = recvfrom(s, buf, sizeof(buf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen); | |
154 | if (rlen == -1) { | |
155 | syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno)); | |
156 | } else { | |
157 | for (i = 0; i < strlen(buf); i++) { | |
158 | if (buf[i] == '\n') | |
159 | buf[i] = '\0'; | |
160 | if (buf[i] == '\r') | |
161 | buf[i] = '\0'; | |
162 | } | |
163 | timer = SS_TIMEOUT * 4; | |
164 | if (strlen(buf)) { | |
165 | syslog(LOG_NOTICE, "recv: \"%s\"", buf); | |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
166 | if (debug) |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
167 | fprintf(stdout, "recv: \"%s\"\n", buf); |
43 | 168 | |
169 | /* | |
44 | 170 | * Process commands from the client |
43 | 171 | */ |
44 | 172 | if (strncmp(buf, "ack", 3) == 0) { |
173 | srv_send((char *)"ack"); | |
174 | } else if (strncmp(buf, "lcd", 3) == 0) { | |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
175 | sprintf(obuf, "[\"12345678901234567890\", \"12345678901234567890\", \"12345678901234567890\", \"12345678901234567890\"]"); |
44 | 176 | for (i = 0; i < 20; i++) { |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
177 | obuf[i+2] = lcdbuf[lcdHandle][i][0]; |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
178 | obuf[i+26] = lcdbuf[lcdHandle][i][1]; |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
179 | obuf[i+50] = lcdbuf[lcdHandle][i][2]; |
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
180 | obuf[i+74] = lcdbuf[lcdHandle][i][3]; |
44 | 181 | } |
182 | srv_send(obuf); | |
183 | } else if (strncmp(buf, "getMode", 7) == 0) { | |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
184 | srv_send("%c", cs_mode); |
44 | 185 | } else if (strncmp(buf, "getControlSettings", 18) == 0) { |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
186 | srv_send("{ mode:%c, \"beerSet\":\"%.1f\", \"fridgeSet\":\"%.1f\", \"heatEstimator\":\"0.2\", \"coolEstimator\":\"5\" }", cs_mode, cs_beerSet, cs_fridgeSet); |
44 | 187 | } else { |
188 | if (debug) | |
189 | fprintf(stdout, "unknown command \"%s\"\n", buf); | |
190 | srv_send((char *)"ERR"); | |
191 | } | |
192 | break; | |
43 | 193 | } |
194 | } | |
195 | } else { | |
196 | syslog(LOG_NOTICE, "poll other event"); | |
197 | } | |
198 | ||
199 | } else { | |
200 | /* | |
201 | * Poll timeout, do some housekeeping | |
202 | */ | |
203 | if (timer) { | |
204 | timer--; | |
205 | } else { | |
206 | /* Inactivity timeout */ | |
207 | break; | |
208 | } | |
209 | if (my_shutdown) { | |
210 | break; | |
211 | } | |
212 | } | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
213 | } |
44 | 214 | |
215 | if (clients) | |
216 | clients--; | |
217 | syslog(LOG_NOTICE, "End connection from %s port %u", hostname, ntohs(peeraddr_in.sin_port)); | |
218 | if (debug) | |
219 | fprintf(stdout, "End connection from %s port %u\n", hostname, ntohs(peeraddr_in.sin_port)); | |
220 | close(s); | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
221 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
222 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
223 | |
43 | 224 | |
225 | PI_THREAD (my_server_loop) | |
226 | { | |
227 | socklen_t addrlen; | |
44 | 228 | int optval = 1; |
43 | 229 | |
230 | syslog(LOG_NOTICE, "Thread my_server_loop started"); | |
231 | if (debug) | |
232 | fprintf(stdout, "Thread my_server_loop started\n"); | |
233 | ||
44 | 234 | memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in)); |
235 | memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in)); | |
236 | myaddr_in.sin_family = AF_INET; | |
237 | myaddr_in.sin_addr.s_addr = INADDR_ANY; | |
238 | myaddr_in.sin_port = htons(Config.my_port); | |
239 | ||
240 | ls = socket(AF_INET, SOCK_STREAM, 0); | |
241 | if (ls == -1) { | |
242 | syslog(LOG_NOTICE, "Can't create listen socket: %s", strerror(errno)); | |
243 | fprintf(stderr, "Can't create listen socket: %s\n", strerror(errno)); | |
244 | return 0; | |
245 | } | |
246 | ||
247 | if (setsockopt(ls, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1) { | |
248 | syslog(LOG_NOTICE, "Can't setsockopt SO_KEEPALIVE socket: %s", strerror(errno)); | |
249 | close(ls); | |
250 | return 0; | |
251 | } | |
252 | ||
253 | if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { | |
254 | syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno)); | |
255 | close(ls); | |
256 | return NULL; | |
257 | } | |
258 | ||
259 | if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) { | |
260 | syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno)); | |
261 | close(ls); | |
262 | return NULL; | |
263 | } | |
264 | ||
265 | if (listen(ls, 5) == -1) { | |
266 | syslog(LOG_NOTICE, "Can't listen on listen socket: %s", strerror(errno)); | |
267 | close(ls); | |
268 | return NULL; | |
269 | } | |
270 | ||
271 | syslog(LOG_NOTICE, "listen socket created %d", ls); | |
272 | if (debug) | |
273 | fprintf(stdout, "listen socket created %d\n", ls); | |
274 | ||
275 | ||
43 | 276 | /* |
277 | * Loop forever until the external shutdown variable is set. | |
278 | */ | |
279 | for (;;) { | |
280 | ||
281 | addrlen = sizeof(struct sockaddr_in); | |
282 | /* | |
283 | * This call will block until a new connection | |
284 | * arrives. Then it will return the address of | |
285 | * the connecting peer, and a new socket | |
286 | * descriptor, s, for that connection. | |
287 | */ | |
288 | s = accept(ls, (struct sockaddr *)&peeraddr_in, &addrlen); | |
289 | if (s == -1) { | |
290 | syslog(LOG_NOTICE, "my_server_loop accept failed %s", strerror(errno)); | |
291 | if (debug) | |
292 | fprintf(stdout, "my_server_loop accept failed %s\n", strerror(errno)); | |
44 | 293 | return 0; |
43 | 294 | } |
295 | ||
296 | switch (fork()) { | |
297 | case -1: /* | |
298 | * Can't fork, just continue. | |
299 | */ | |
44 | 300 | return 0; |
43 | 301 | case 0: /* |
302 | * Child process, the commandline server. | |
303 | */ | |
304 | cmd_server(); | |
305 | return 0; | |
306 | default: /* | |
307 | * Daemon process comes here. The daemon | |
308 | * needs to remember to close the new | |
309 | * accept socket after forking the child. | |
310 | */ | |
311 | close(s); | |
312 | } | |
313 | ||
314 | if (my_shutdown) { | |
315 | syslog(LOG_NOTICE, "Thread my_server_loop stopped"); | |
316 | if (debug) | |
317 | fprintf(stdout, "Thread my_server_loop stopped\n"); | |
318 | return 0; | |
319 | } | |
320 | ||
321 | } | |
322 | } | |
323 | ||
324 | ||
325 |