Thu, 22 May 2014 13:54:52 +0200
Single client server 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" |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
26 | #include "thermferm.h" |
42
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; |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
33 | |
43 | 34 | int s; /* connected socket */ |
35 | int ls; /* listen socket */ | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
36 | |
43 | 37 | struct sockaddr_in myaddr_in; /* for local socket address */ |
38 | struct sockaddr_in peeraddr_in; /* for peer socket address */ | |
39 | ||
40 | struct hostent *hp; | |
41 | ||
42 | #define SS_BUFSIZE 1024 | |
43 | #define SS_TIMEOUT 300 | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
44 | |
48 | 45 | unsigned char cs_mode = 'o'; /* o = Off, f = fridge, b = beer, p = profile-run */ |
46 | float cs_beerSet = 20.0; | |
47 | float cs_fridgeSet = 20.0; | |
48 | float cs_heatEstimator = 0.2; | |
49 | float cs_coolEstimator = 5; | |
50 | ||
51 | unsigned char cc_tempFormat = 'C'; | |
52 | float cc_tempSetMin = 1.0; | |
53 | float cc_tempSetMax = 30.0; | |
54 | float cc_idleRangeH = 1.000; | |
55 | float cc_idleRangeL = -1.000; | |
56 | ||
57 | float cv_beerDiff = 0.0; | |
58 | ||
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
59 | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
60 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
61 | void defaultControlsettings(void) |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
62 | { |
48 | 63 | cs_mode = 'o'; /* o = Off, f = fridge, b = beer, p = profile-run */ |
64 | cs_beerSet = 20.0; | |
65 | cs_fridgeSet = 20.0; | |
66 | cs_heatEstimator = 0.2; | |
67 | cs_coolEstimator = 5; | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
68 | } |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
69 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
70 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
71 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
72 | void defaultControlConstants(void) |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
73 | { |
48 | 74 | cc_tempFormat = 'C'; |
75 | cc_tempSetMin = 1.0; | |
76 | cc_tempSetMax = 30.0; | |
77 | cc_idleRangeH = 1.000; | |
78 | cc_idleRangeL = -1.000; | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
79 | } |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
80 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
81 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
82 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
83 | void defaultControlVariables(void) |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
84 | { |
48 | 85 | cv_beerDiff = 0.0; |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
86 | } |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
87 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
88 | |
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
89 | |
43 | 90 | /* |
91 | * Send message to client | |
92 | */ | |
93 | int srv_send(const char *format, ...) | |
94 | { | |
95 | char out[SS_BUFSIZE]; | |
96 | va_list va_ptr; | |
97 | ||
98 | if (s == -1) | |
99 | return -1; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
100 | |
43 | 101 | va_start(va_ptr, format); |
102 | vsnprintf(out, SS_BUFSIZE-1, format, va_ptr); | |
103 | va_end(va_ptr); | |
104 | ||
48 | 105 | if (debug) { |
106 | syslog(LOG_NOTICE, "send: \"%s\"", out); | |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
107 | fprintf(stdout, "send: \"%s\"\n", out); |
48 | 108 | } |
44 | 109 | |
43 | 110 | if (send(s, out, strlen(out), 0) != strlen(out)) { |
111 | syslog(LOG_NOTICE, "srv_send failed"); | |
112 | return -1; | |
113 | } | |
114 | ||
115 | if (send(s, (char *)"\r\n", 2, 0) != 2) { | |
116 | syslog(LOG_NOTICE, "srv_send failed"); | |
117 | return -1; | |
118 | } | |
119 | ||
120 | return 0; | |
121 | } | |
122 | ||
123 | ||
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
124 | |
43 | 125 | void cmd_die(int onsig) |
126 | { | |
127 | syslog(LOG_NOTICE, "Server process die on signal %d", onsig); | |
128 | close(s); | |
129 | exit(0); | |
130 | } | |
131 | ||
132 | ||
133 | ||
134 | void cmd_server(void) | |
135 | { | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
136 | char *inp, *hostname, buf[SS_BUFSIZE], obuf[SS_BUFSIZE]; |
48 | 137 | int i, rc, rlen; |
43 | 138 | socklen_t fromlen; |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
139 | float newtemp; |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
140 | |
43 | 141 | hp = gethostbyaddr ((char *) &peeraddr_in.sin_addr, sizeof(struct in_addr), peeraddr_in.sin_family); |
142 | if (hp == NULL) { | |
143 | hostname = inet_ntoa(peeraddr_in.sin_addr); | |
144 | } else { | |
145 | hostname = hp->h_name; | |
146 | } | |
147 | ||
48 | 148 | if (debug) { |
149 | syslog(LOG_NOTICE, "Start new client connection from %s port %u", hostname, ntohs(peeraddr_in.sin_port)); | |
150 | fprintf(stdout, "Start new client connection from %s port %u\n", hostname, ntohs(peeraddr_in.sin_port)); | |
151 | } | |
43 | 152 | |
48 | 153 | memset((char *)&buf, 0, SS_BUFSIZE); |
154 | fromlen = sizeof(peeraddr_in); | |
155 | rlen = recvfrom(s, buf, sizeof(buf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen); | |
156 | if (rlen == -1) { | |
157 | syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno)); | |
158 | } else { | |
159 | for (i = 0; i < strlen(buf); i++) { | |
160 | if (buf[i] == '\n') | |
161 | buf[i] = '\0'; | |
162 | if (buf[i] == '\r') | |
163 | buf[i] = '\0'; | |
164 | } | |
165 | if (strlen(buf)) { | |
166 | if (debug) { | |
167 | syslog(LOG_NOTICE, "recv: \"%s\"", buf); | |
168 | fprintf(stdout, "recv: \"%s\"\n", buf); | |
43 | 169 | } |
170 | ||
171 | /* | |
48 | 172 | * Process commands from the client |
43 | 173 | */ |
48 | 174 | if (strncmp(buf, "ack", 3) == 0) { |
175 | srv_send((char *)"ack"); | |
176 | } else if (strncmp(buf, "lcd", 3) == 0) { | |
177 | sprintf(obuf, "[\" \", \" \", \" \", \" \"]"); | |
178 | for (i = 0; i < 20; i++) { | |
179 | obuf[i+2] = lcdbuf[lcdHandle][i][0]; | |
180 | obuf[i+26] = lcdbuf[lcdHandle][i][1]; | |
181 | obuf[i+50] = lcdbuf[lcdHandle][i][2]; | |
182 | obuf[i+74] = lcdbuf[lcdHandle][i][3]; | |
183 | } | |
184 | srv_send(obuf); | |
185 | } else if (strncmp(buf, "getMode", 7) == 0) { | |
186 | srv_send("%c", cs_mode); | |
187 | } else if (strncmp(buf, "getFridge", 9) == 0) { | |
188 | srv_send("%.1f", cs_fridgeSet); | |
189 | } else if (strncmp(buf, "getBeer", 7) == 0) { | |
190 | srv_send("%.1f", cs_beerSet); | |
191 | } else if (strncmp(buf, "getControlConstants", 19) == 0) { | |
192 | srv_send("{ \"tempFormat\":\"%c\", \"tempSetMin\":%.1f, \"tempSetMax\":%.1f, \"idleRangeH\":%.3f, \"idleRangeL\":%.3f }", | |
193 | cc_tempFormat, cc_tempSetMin, cc_tempSetMax, cc_idleRangeH, cc_idleRangeL ); | |
194 | } else if (strncmp(buf, "getControlSettings", 18) == 0) { | |
195 | srv_send("{ \"mode\":\"%c\", \"beerSet\":%.1f, \"fridgeSet\":%.1f, \"heatEstimator\":%.1f, \"coolEstimator\":%.1f }", | |
196 | cs_mode, cs_beerSet, cs_fridgeSet, cs_heatEstimator, cs_coolEstimator); | |
197 | } else if (strncmp(buf, "getControlVariables", 19) == 0) { | |
198 | srv_send("{ \"beerDiff\":%.2f }", cv_beerDiff); | |
199 | } else if (strncmp(buf, "setBeer=", 8) == 0) { | |
200 | inp = xstrcpy(buf+8); | |
201 | rc = sscanf(inp, "%f", &newtemp); | |
202 | if (debug) | |
203 | fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); | |
204 | if (rc == 1) { | |
205 | srv_send("ack"); | |
206 | cs_mode = 'b'; | |
207 | cs_beerSet = newtemp; | |
208 | } else { | |
209 | srv_send("err"); | |
210 | } | |
211 | free(inp); | |
212 | } else if (strncmp(buf, "setFridge=", 10) == 0) { | |
213 | inp = xstrcpy(buf+10); | |
214 | rc = sscanf(inp, "%f", &newtemp); | |
215 | if (debug) | |
216 | fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); | |
217 | if (rc == 1) { | |
218 | srv_send("ack"); | |
219 | cs_mode = 'f'; | |
220 | cs_fridgeSet = newtemp; | |
221 | } else { | |
222 | srv_send("err"); | |
223 | } | |
224 | free(inp); | |
43 | 225 | } else { |
48 | 226 | if (debug) |
227 | fprintf(stdout, "unknown command \"%s\"\n", buf); | |
228 | srv_send((char *)"ERR"); | |
43 | 229 | } |
230 | } | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
231 | } |
44 | 232 | |
48 | 233 | if (debug) { |
234 | syslog(LOG_NOTICE, "End connection from %s port %u", hostname, ntohs(peeraddr_in.sin_port)); | |
44 | 235 | fprintf(stdout, "End connection from %s port %u\n", hostname, ntohs(peeraddr_in.sin_port)); |
48 | 236 | } |
237 | ||
44 | 238 | close(s); |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
239 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
240 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
241 | |
43 | 242 | |
243 | PI_THREAD (my_server_loop) | |
244 | { | |
245 | socklen_t addrlen; | |
44 | 246 | int optval = 1; |
43 | 247 | |
248 | syslog(LOG_NOTICE, "Thread my_server_loop started"); | |
249 | if (debug) | |
250 | fprintf(stdout, "Thread my_server_loop started\n"); | |
251 | ||
44 | 252 | memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in)); |
253 | memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in)); | |
254 | myaddr_in.sin_family = AF_INET; | |
255 | myaddr_in.sin_addr.s_addr = INADDR_ANY; | |
256 | myaddr_in.sin_port = htons(Config.my_port); | |
257 | ||
258 | ls = socket(AF_INET, SOCK_STREAM, 0); | |
259 | if (ls == -1) { | |
260 | syslog(LOG_NOTICE, "Can't create listen socket: %s", strerror(errno)); | |
261 | fprintf(stderr, "Can't create listen socket: %s\n", strerror(errno)); | |
262 | return 0; | |
263 | } | |
264 | ||
265 | if (setsockopt(ls, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1) { | |
266 | syslog(LOG_NOTICE, "Can't setsockopt SO_KEEPALIVE socket: %s", strerror(errno)); | |
267 | close(ls); | |
268 | return 0; | |
269 | } | |
270 | ||
271 | if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { | |
272 | syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno)); | |
273 | close(ls); | |
274 | return NULL; | |
275 | } | |
276 | ||
277 | if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) { | |
278 | syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno)); | |
279 | close(ls); | |
280 | return NULL; | |
281 | } | |
282 | ||
283 | if (listen(ls, 5) == -1) { | |
284 | syslog(LOG_NOTICE, "Can't listen on listen socket: %s", strerror(errno)); | |
285 | close(ls); | |
286 | return NULL; | |
287 | } | |
288 | ||
289 | syslog(LOG_NOTICE, "listen socket created %d", ls); | |
290 | if (debug) | |
291 | fprintf(stdout, "listen socket created %d\n", ls); | |
292 | ||
293 | ||
43 | 294 | /* |
295 | * Loop forever until the external shutdown variable is set. | |
296 | */ | |
297 | for (;;) { | |
298 | ||
299 | addrlen = sizeof(struct sockaddr_in); | |
300 | /* | |
301 | * This call will block until a new connection | |
302 | * arrives. Then it will return the address of | |
303 | * the connecting peer, and a new socket | |
304 | * descriptor, s, for that connection. | |
305 | */ | |
306 | s = accept(ls, (struct sockaddr *)&peeraddr_in, &addrlen); | |
307 | if (s == -1) { | |
308 | syslog(LOG_NOTICE, "my_server_loop accept failed %s", strerror(errno)); | |
309 | if (debug) | |
310 | fprintf(stdout, "my_server_loop accept failed %s\n", strerror(errno)); | |
44 | 311 | return 0; |
43 | 312 | } |
313 | ||
48 | 314 | cmd_server(); |
43 | 315 | |
316 | if (my_shutdown) { | |
317 | syslog(LOG_NOTICE, "Thread my_server_loop stopped"); | |
318 | if (debug) | |
319 | fprintf(stdout, "Thread my_server_loop stopped\n"); | |
320 | return 0; | |
321 | } | |
322 | ||
323 | } | |
324 | } | |
325 | ||
326 | ||
327 |