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