Sun, 25 May 2014 22:37:23 +0200
Code cleanup
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 | { | |
52 | 126 | char *inp, 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 | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
131 | // if (debug) { |
52 | 132 | // char *hostname = inet_ntoa(peeraddr_in.sin_addr); |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
133 | // 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
|
134 | // 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
|
135 | // } |
43 | 136 | |
48 | 137 | memset((char *)&buf, 0, SS_BUFSIZE); |
138 | fromlen = sizeof(peeraddr_in); | |
139 | rlen = recvfrom(s, buf, sizeof(buf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen); | |
140 | if (rlen == -1) { | |
141 | syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno)); | |
142 | } else { | |
143 | for (i = 0; i < strlen(buf); i++) { | |
144 | if (buf[i] == '\n') | |
145 | buf[i] = '\0'; | |
146 | if (buf[i] == '\r') | |
147 | buf[i] = '\0'; | |
148 | } | |
149 | if (strlen(buf)) { | |
150 | if (debug) { | |
151 | syslog(LOG_NOTICE, "recv: \"%s\"", buf); | |
152 | fprintf(stdout, "recv: \"%s\"\n", buf); | |
43 | 153 | } |
154 | ||
155 | /* | |
48 | 156 | * Process commands from the client |
43 | 157 | */ |
48 | 158 | if (strncmp(buf, "ack", 3) == 0) { |
159 | srv_send((char *)"ack"); | |
160 | } else if (strncmp(buf, "lcd", 3) == 0) { | |
161 | sprintf(obuf, "[\" \", \" \", \" \", \" \"]"); | |
162 | for (i = 0; i < 20; i++) { | |
163 | obuf[i+2] = lcdbuf[lcdHandle][i][0]; | |
164 | obuf[i+26] = lcdbuf[lcdHandle][i][1]; | |
165 | obuf[i+50] = lcdbuf[lcdHandle][i][2]; | |
166 | obuf[i+74] = lcdbuf[lcdHandle][i][3]; | |
167 | } | |
168 | srv_send(obuf); | |
169 | } else if (strncmp(buf, "getMode", 7) == 0) { | |
170 | srv_send("%c", cs_mode); | |
171 | } else if (strncmp(buf, "getFridge", 9) == 0) { | |
172 | srv_send("%.1f", cs_fridgeSet); | |
173 | } else if (strncmp(buf, "getBeer", 7) == 0) { | |
174 | srv_send("%.1f", cs_beerSet); | |
175 | } else if (strncmp(buf, "getControlConstants", 19) == 0) { | |
176 | srv_send("{ \"tempFormat\":\"%c\", \"tempSetMin\":%.1f, \"tempSetMax\":%.1f, \"idleRangeH\":%.3f, \"idleRangeL\":%.3f }", | |
177 | cc_tempFormat, cc_tempSetMin, cc_tempSetMax, cc_idleRangeH, cc_idleRangeL ); | |
178 | } else if (strncmp(buf, "getControlSettings", 18) == 0) { | |
179 | srv_send("{ \"mode\":\"%c\", \"beerSet\":%.1f, \"fridgeSet\":%.1f, \"heatEstimator\":%.1f, \"coolEstimator\":%.1f }", | |
180 | cs_mode, cs_beerSet, cs_fridgeSet, cs_heatEstimator, cs_coolEstimator); | |
181 | } else if (strncmp(buf, "getControlVariables", 19) == 0) { | |
182 | srv_send("{ \"beerDiff\":%.2f }", cv_beerDiff); | |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
183 | } else if (strncmp(buf, "loadDefaultControlSettings", 26) == 0) { |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
184 | defaultControlSettings(); |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
185 | srv_send("ack"); |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
186 | } else if (strncmp(buf, "loadDefaultControlConstants", 27) == 0) { |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
187 | defaultControlConstants(); |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
188 | srv_send("ack"); |
48 | 189 | } else if (strncmp(buf, "setBeer=", 8) == 0) { |
190 | inp = xstrcpy(buf+8); | |
191 | rc = sscanf(inp, "%f", &newtemp); | |
192 | if (debug) | |
193 | fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); | |
194 | if (rc == 1) { | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
195 | 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
|
196 | 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
|
197 | srv_send("ack"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
198 | cs_mode = 'b'; |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
199 | cs_beerSet = newtemp; |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
200 | } else { |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
201 | 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
|
202 | srv_send("err"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
203 | } |
48 | 204 | } else { |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
205 | syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp); |
48 | 206 | srv_send("err"); |
207 | } | |
208 | free(inp); | |
209 | } else if (strncmp(buf, "setFridge=", 10) == 0) { | |
210 | inp = xstrcpy(buf+10); | |
211 | rc = sscanf(inp, "%f", &newtemp); | |
212 | if (debug) | |
213 | fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); | |
214 | if (rc == 1) { | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
215 | 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
|
216 | 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
|
217 | srv_send("ack"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
218 | cs_mode = 'f'; |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
219 | cs_fridgeSet = newtemp; |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
220 | } else { |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
221 | 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
|
222 | srv_send("err"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
223 | } |
48 | 224 | } else { |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
225 | syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp); |
48 | 226 | srv_send("err"); |
227 | } | |
228 | free(inp); | |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
229 | } else if (strncmp(buf, "setOff", 6) == 0) { |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
230 | if (debug) |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
231 | 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
|
232 | syslog(LOG_NOTICE, "Notification: Temperature control disabled"); |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
233 | cs_mode = 'o'; |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
234 | srv_send("ack"); |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
235 | } 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
|
236 | if (debug) |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
237 | fprintf(stdout, "FIXME:\n"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
238 | srv_send("ack"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
239 | // stopScript |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
240 | // quit |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
241 | // eraseLogs |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
242 | // interval |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
243 | // startNewBrew |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
244 | // pauseLogging |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
245 | // stopLogging |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
246 | // resumeLogging |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
247 | // dateTimeFormatDisplay |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
248 | } 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
|
249 | 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
|
250 | // programArduino |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
251 | // refreshDeviceList |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
252 | // getDeviceList |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
253 | // applyDevice |
43 | 254 | } else { |
48 | 255 | if (debug) |
256 | fprintf(stdout, "unknown command \"%s\"\n", buf); | |
257 | srv_send((char *)"ERR"); | |
43 | 258 | } |
259 | } | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
260 | } |
44 | 261 | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
262 | // if (debug) { |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
263 | // 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
|
264 | // 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
|
265 | // } |
48 | 266 | |
44 | 267 | close(s); |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
268 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
269 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
270 | |
43 | 271 | |
272 | PI_THREAD (my_server_loop) | |
273 | { | |
274 | socklen_t addrlen; | |
44 | 275 | int optval = 1; |
43 | 276 | |
277 | syslog(LOG_NOTICE, "Thread my_server_loop started"); | |
278 | if (debug) | |
279 | fprintf(stdout, "Thread my_server_loop started\n"); | |
280 | ||
44 | 281 | memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in)); |
282 | memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in)); | |
283 | myaddr_in.sin_family = AF_INET; | |
284 | myaddr_in.sin_addr.s_addr = INADDR_ANY; | |
285 | myaddr_in.sin_port = htons(Config.my_port); | |
286 | ||
287 | ls = socket(AF_INET, SOCK_STREAM, 0); | |
288 | if (ls == -1) { | |
289 | syslog(LOG_NOTICE, "Can't create listen socket: %s", strerror(errno)); | |
290 | fprintf(stderr, "Can't create listen socket: %s\n", strerror(errno)); | |
291 | return 0; | |
292 | } | |
293 | ||
294 | if (setsockopt(ls, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1) { | |
295 | syslog(LOG_NOTICE, "Can't setsockopt SO_KEEPALIVE socket: %s", strerror(errno)); | |
296 | close(ls); | |
297 | return 0; | |
298 | } | |
299 | ||
300 | if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { | |
301 | syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno)); | |
302 | close(ls); | |
303 | return NULL; | |
304 | } | |
305 | ||
306 | if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) { | |
307 | syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno)); | |
308 | close(ls); | |
309 | return NULL; | |
310 | } | |
311 | ||
312 | if (listen(ls, 5) == -1) { | |
313 | syslog(LOG_NOTICE, "Can't listen on listen socket: %s", strerror(errno)); | |
314 | close(ls); | |
315 | return NULL; | |
316 | } | |
317 | ||
318 | syslog(LOG_NOTICE, "listen socket created %d", ls); | |
319 | if (debug) | |
320 | fprintf(stdout, "listen socket created %d\n", ls); | |
321 | ||
322 | ||
43 | 323 | /* |
324 | * Loop forever until the external shutdown variable is set. | |
325 | */ | |
326 | for (;;) { | |
327 | ||
328 | addrlen = sizeof(struct sockaddr_in); | |
329 | /* | |
330 | * This call will block until a new connection | |
331 | * arrives. Then it will return the address of | |
332 | * the connecting peer, and a new socket | |
333 | * descriptor, s, for that connection. | |
334 | */ | |
335 | s = accept(ls, (struct sockaddr *)&peeraddr_in, &addrlen); | |
336 | if (s == -1) { | |
337 | syslog(LOG_NOTICE, "my_server_loop accept failed %s", strerror(errno)); | |
338 | if (debug) | |
339 | fprintf(stdout, "my_server_loop accept failed %s\n", strerror(errno)); | |
44 | 340 | return 0; |
43 | 341 | } |
342 | ||
48 | 343 | cmd_server(); |
43 | 344 | |
345 | if (my_shutdown) { | |
346 | syslog(LOG_NOTICE, "Thread my_server_loop stopped"); | |
347 | if (debug) | |
348 | fprintf(stdout, "Thread my_server_loop stopped\n"); | |
349 | return 0; | |
350 | } | |
351 | ||
352 | } | |
353 | } | |
354 | ||
355 | ||
356 |