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