Sun, 13 Jul 2014 17:31:45 +0200
Added .h files
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 | |
106 | 23 | #include "rdconfig.h" |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
24 | #include "thermferm.h" |
106 | 25 | #include "server.h" |
26 | #include "xutil.h" | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
27 | |
58
e8e7b46b705b
Fixed compilation without wiringpi
Michiel Broek <mbroek@mbse.eu>
parents:
54
diff
changeset
|
28 | |
75
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
29 | extern int my_shutdown; |
4b976601737d
Writes a basic xml configuration next to the plain ascii config file
Michiel Broek <mbroek@mbse.eu>
parents:
74
diff
changeset
|
30 | extern int debug; |
71 | 31 | extern int current_unit; |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
32 | #ifdef HAVE_WIRINGPI_H |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
33 | extern int lcdHandle; |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
34 | extern unsigned char lcdbuf[MAX_LCDS][20][4]; |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
35 | #endif |
43 | 36 | extern sys_config Config; |
81 | 37 | extern const char UNITMODE[5][8]; |
95 | 38 | extern const char UNITmode[5]; |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
39 | extern const char TEMPSTATE[3][8]; |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
40 | |
43 | 41 | int s; /* connected socket */ |
42 | int ls; /* listen socket */ | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
43 | |
43 | 44 | struct sockaddr_in myaddr_in; /* for local socket address */ |
45 | struct sockaddr_in peeraddr_in; /* for peer socket address */ | |
46 | ||
47 | struct hostent *hp; | |
48 | ||
49 | #define SS_BUFSIZE 1024 | |
50 | #define SS_TIMEOUT 300 | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
51 | |
48 | 52 | |
95 | 53 | float cs_heatEstimator = 0.2; |
54 | float cs_coolEstimator = 5; | |
48 | 55 | float cv_beerDiff = 0.0; |
56 | ||
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
57 | |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
58 | |
43 | 59 | /* |
60 | * Send message to client | |
61 | */ | |
62 | int srv_send(const char *format, ...) | |
63 | { | |
64 | char out[SS_BUFSIZE]; | |
65 | va_list va_ptr; | |
66 | ||
67 | if (s == -1) | |
68 | return -1; | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
69 | |
43 | 70 | va_start(va_ptr, format); |
71 | vsnprintf(out, SS_BUFSIZE-1, format, va_ptr); | |
72 | va_end(va_ptr); | |
73 | ||
48 | 74 | if (debug) { |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
75 | syslog(LOG_NOTICE, "send: \"%s\"", out); |
45
053c4657105f
Mirror LCD display on the webpage works
Michiel Broek <mbroek@mbse.eu>
parents:
44
diff
changeset
|
76 | fprintf(stdout, "send: \"%s\"\n", out); |
48 | 77 | } |
44 | 78 | |
43 | 79 | if (send(s, out, strlen(out), 0) != strlen(out)) { |
80 | syslog(LOG_NOTICE, "srv_send failed"); | |
81 | return -1; | |
82 | } | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
83 | |
43 | 84 | if (send(s, (char *)"\r\n", 2, 0) != 2) { |
85 | syslog(LOG_NOTICE, "srv_send failed"); | |
86 | return -1; | |
87 | } | |
88 | ||
89 | return 0; | |
90 | } | |
91 | ||
92 | ||
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
93 | |
88 | 94 | /* |
92 | 95 | * ADD PROFILE name |
96 | * ADD UNIT name | |
88 | 97 | */ |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
98 | int unit_add(char *buf) |
43 | 99 | { |
92 | 100 | units_list *unit, *tmpu; |
101 | profiles_list *profile, *tmpp; | |
102 | uuid_t uu; | |
103 | char *opt, *param; | |
104 | int id = 1; | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
105 | |
92 | 106 | opt = strtok(buf, " \0"); |
107 | opt = strtok(NULL, " \0"); | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
108 | if (opt == NULL) { |
92 | 109 | srv_send((char *)"501 Subcommand missing"); |
110 | return 1; | |
111 | } | |
112 | ||
113 | param = strtok(NULL, "\0"); | |
114 | if (param == NULL) { | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
115 | srv_send((char *)"501 Parameter missing"); |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
116 | return 1; |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
117 | } |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
118 | |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
119 | if (debug) |
92 | 120 | fprintf(stdout, "opt: '%s' param: '%s'\n", MBSE_SS(opt), MBSE_SS(param)); |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
121 | |
92 | 122 | if (strcmp(opt, (char *)"PROFILE") == 0) { |
123 | profile = (profiles_list *)malloc(sizeof(profiles_list)); | |
124 | profile->next = NULL; | |
125 | profile->version = 1; | |
126 | profile->uuid = malloc(37); | |
127 | uuid_generate(uu); | |
128 | uuid_unparse(uu, profile->uuid); | |
129 | profile->name = xstrcpy(param); | |
130 | profile->steps = NULL; | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
131 | |
92 | 132 | if (Config.profiles == NULL) { |
133 | Config.profiles = profile; | |
134 | } else { | |
135 | for (tmpp = Config.profiles; tmpp; tmpp = tmpp->next) { | |
136 | if (tmpp->next == NULL) { | |
137 | tmpp->next = profile; | |
138 | break; | |
139 | } | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
140 | } |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
141 | } |
92 | 142 | |
143 | syslog(LOG_NOTICE, "Profile \"%s\" with uuid %s added", param, profile->uuid); | |
144 | srv_send((char *)"211 Profile \"%s\" with uuid %s added", param, profile->uuid); | |
145 | return 0; | |
146 | ||
147 | } else if (strcmp(opt, (char *)"UNIT") == 0) { | |
148 | unit = (units_list *)malloc(sizeof(units_list)); | |
149 | unit->next = NULL; | |
150 | unit->version = 1; | |
151 | unit->uuid = malloc(37); | |
152 | uuid_generate(uu); | |
153 | uuid_unparse(uu, unit->uuid); | |
154 | unit->name = xstrcpy(param); | |
155 | unit->air_address = unit->beer_address = unit->io1_address = unit->io2_address = unit->profile = NULL; | |
156 | unit->volume = 0.0; | |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
157 | unit->air_state = unit->beer_state = 1; |
92 | 158 | unit->heater_available = unit->cooler_available = unit->fan_available = FALSE; |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
159 | unit->air_temperature = unit->beer_temperature = 20000; |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
160 | unit->beer_set = unit->fridge_set = 20.0; |
92 | 161 | unit->heater_state = unit->cooler_state = unit->fan_state = unit->door_state = unit->mode = unit->prof_state = 0; |
162 | unit->temp_set_min = 1.0; | |
163 | unit->temp_set_max = 30.0; | |
164 | unit->idle_rangeH = 1.0; | |
165 | unit->idle_rangeL = -1.0; | |
166 | unit->prof_started = (time_t)0; | |
167 | ||
168 | if (Config.units == NULL) { | |
169 | Config.units = unit; | |
170 | } else { | |
171 | for (tmpu = Config.units; tmpu; tmpu = tmpu->next) { | |
172 | id++; | |
173 | if (tmpu->next == NULL) { | |
174 | tmpu->next = unit; | |
175 | break; | |
176 | } | |
177 | } | |
178 | } | |
179 | ||
180 | syslog(LOG_NOTICE, "Unit %d with uuid %s added", id, unit->uuid); | |
181 | srv_send((char *)"211 Unit %d with uuid %s added", id, unit->uuid); | |
182 | current_unit = id; | |
183 | return 0; | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
184 | } |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
185 | |
92 | 186 | srv_send((char *)"502 Unknown command option"); |
187 | return 1; | |
43 | 188 | } |
189 | ||
190 | ||
191 | ||
81 | 192 | /* |
193 | * LIST | |
84 | 194 | * LIST BUS |
195 | * LIST PROFILES | |
196 | * LIST UNIT | |
81 | 197 | */ |
198 | int cmd_list(char *buf) | |
199 | { | |
92 | 200 | char *opt; |
84 | 201 | units_list *unit; |
92 | 202 | profiles_list *profile; |
84 | 203 | int i; |
204 | DIR *fd; | |
205 | struct dirent *de; | |
81 | 206 | |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
207 | opt = strtok(buf, " \0"); |
92 | 208 | opt = strtok(NULL, "\0"); |
84 | 209 | |
81 | 210 | if (opt == NULL) { |
92 | 211 | /* |
212 | * Default, list available units | |
213 | */ | |
81 | 214 | srv_send((char *)"212 Fermenter list follows:"); |
215 | i = 0; | |
216 | for (unit = Config.units; unit; unit = unit->next) { | |
217 | i++; | |
100 | 218 | srv_send((char *)"%d,%s,%s,%s", i, unit->uuid, unit->name, UNITMODE[unit->mode]); |
81 | 219 | } |
220 | srv_send((char *)"."); | |
92 | 221 | return 0; |
222 | ||
84 | 223 | } else if (strcmp(opt, (char *)"BUS") == 0) { |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
224 | /* |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
225 | * 1-wire bus |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
226 | */ |
84 | 227 | if ((fd = opendir((char *)"/sys/bus/w1/devices"))) { |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
228 | srv_send((char *)"212 1-wire bus devices:"); |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
229 | while ((de = readdir(fd))) { |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
230 | if (de->d_name[0] != '.') { |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
231 | if (strncmp(de->d_name, (char *)"w1", 2) == 0) { |
100 | 232 | srv_send((char *)"%s,N/A,System device", de->d_name); |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
233 | } else if (strncmp(de->d_name, (char *)"10", 2) == 0) { |
100 | 234 | srv_send((char *)"%s,18S20,Digital thermometer", de->d_name); |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
235 | } else if (strncmp(de->d_name, (char *)"28", 2) == 0) { |
100 | 236 | srv_send((char *)"%s,18B20,Digital thermometer", de->d_name); |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
237 | } else if (strncmp(de->d_name, (char *)"29", 2) == 0) { |
100 | 238 | srv_send((char *)"%s,2408,8 Channel addressable switch/LCD", de->d_name); |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
239 | } else if (strncmp(de->d_name, (char *)"3a", 2) == 0) { |
100 | 240 | srv_send((char *)"%s,2413,Dual channel addressable switchs", de->d_name); |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
241 | } else { |
100 | 242 | srv_send((char *)"%s,N/A,Unknown device", de->d_name); |
84 | 243 | } |
244 | } | |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
245 | } |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
246 | srv_send((char *)"."); |
84 | 247 | closedir(fd); |
92 | 248 | return 0; |
84 | 249 | } else { |
250 | srv_send((char *)"503 directory /sys/bus/w1/devices: %s", strerror(errno)); | |
92 | 251 | return 1; |
84 | 252 | } |
92 | 253 | |
84 | 254 | } else if (strcmp(opt, (char *)"PROFILES") == 0) { |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
255 | /* |
92 | 256 | * Fermenting profiles |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
257 | */ |
92 | 258 | srv_send((char *)"212 profiles:"); |
259 | i = 0; | |
260 | for (profile = Config.profiles; profile; profile = profile->next) { | |
261 | i++; | |
100 | 262 | srv_send((char *)"%d,%s,%s", i, profile->uuid, profile->name); |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
263 | } |
92 | 264 | srv_send((char *)"."); |
265 | return 0; | |
266 | ||
84 | 267 | } else if (strcmp(opt, (char *)"UNIT") == 0) { |
86
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
268 | /* |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
269 | * List configured and selected fermenter unit |
3d7a241329e2
Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents:
85
diff
changeset
|
270 | */ |
84 | 271 | if (current_unit == -1) { |
272 | srv_send((char *)"401 No fermenter unit selected"); | |
92 | 273 | return 1; |
274 | } | |
275 | ||
276 | srv_send((char *)"213 Unit %d listing follows:", current_unit); | |
277 | i = 0; | |
278 | for (unit = Config.units; unit; unit = unit->next) { | |
279 | i++; | |
280 | if (i == current_unit) { | |
100 | 281 | srv_send((char *)"NAME,%s", unit->name); |
282 | srv_send((char *)"UUID,%s", unit->uuid); | |
92 | 283 | if (unit->air_address) { |
100 | 284 | srv_send((char *)"AIR_ADDRESS,%s", unit->air_address); |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
285 | srv_send((char *)"AIR_STATE,%s", TEMPSTATE[unit->air_state]); |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
286 | srv_send((char *)"AIR_TEMPERATURE,%.3f", unit->air_temperature / 1000.0); |
92 | 287 | } |
288 | if (unit->beer_address) { | |
100 | 289 | srv_send((char *)"BEER_ADDRESS,%s", unit->beer_address); |
101
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
290 | srv_send((char *)"BEER_STATE,%s", TEMPSTATE[unit->beer_state]); |
1302abe92eb1
Made temperature sensors working in the units
Michiel Broek <mbroek@mbse.eu>
parents:
100
diff
changeset
|
291 | srv_send((char *)"BEER_TEMPERATURE,%.3f", unit->beer_temperature / 1000.0); |
92 | 292 | } |
293 | if (unit->io1_address) { | |
100 | 294 | srv_send((char *)"IO1_ADDRESS,%s", unit->io1_address); |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
295 | } |
92 | 296 | if (unit->io2_address) { |
100 | 297 | srv_send((char *)"IO2_ADDRESS,%s", unit->io2_address); |
92 | 298 | } |
299 | if (unit->heater_available) { | |
100 | 300 | srv_send((char *)"HEATER_AVAILABLE,%s", unit->heater_available); |
92 | 301 | } |
302 | if (unit->cooler_available) { | |
100 | 303 | srv_send((char *)"COOLER_AVAILABLE,%s", unit->cooler_available); |
92 | 304 | } |
305 | if (unit->fan_available) { | |
100 | 306 | srv_send((char *)"FAN_AVAILABLE,%s", unit->fan_available); |
92 | 307 | } |
100 | 308 | srv_send((char *)"MODE,%s", UNITMODE[unit->mode]); |
309 | srv_send((char *)"FRIDGE_SET,%.1f", unit->fridge_set); | |
310 | srv_send((char *)"BEER_SET,%.1f", unit->beer_set); | |
92 | 311 | if (unit->profile) { |
100 | 312 | srv_send((char *)"PROFILE,%s", unit->profile); |
92 | 313 | } |
100 | 314 | srv_send((char *)"TEMP_SET_MIN,%.1f", unit->temp_set_min); |
315 | srv_send((char *)"TEMP_SET_MAX,%.1f", unit->temp_set_max); | |
316 | srv_send((char *)"IDLE_RANGE_L,%.1f", unit->idle_rangeL); | |
317 | srv_send((char *)"IDLE_RANGE_H,%.1f", unit->idle_rangeH); | |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
318 | } |
84 | 319 | } |
92 | 320 | srv_send((char *)"."); |
321 | return 0; | |
81 | 322 | } |
323 | ||
92 | 324 | srv_send((char *)"502 Unknown command option"); |
325 | return 1; | |
81 | 326 | } |
327 | ||
328 | ||
329 | ||
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
330 | /* |
88 | 331 | * Set new operating mode |
332 | * MODE OFF|NONE|BEER|FRIDGE|PROFILE | |
333 | */ | |
334 | int cmd_mode(char *buf) | |
335 | { | |
336 | return 1; | |
337 | } | |
338 | ||
339 | ||
340 | ||
341 | /* | |
92 | 342 | * SET BEER float |
343 | * SET FRIDGE float | |
344 | * SET VOLUME float | |
345 | * SET IDLE_LOW float | |
346 | * SET IDLE_HIGH float | |
347 | * SET TEMP_MIN float | |
348 | * SET TEMP_MAX float | |
349 | * SET NAME string | |
350 | * SET PROFILE string | |
88 | 351 | */ |
352 | int cmd_set(char *buf) | |
353 | { | |
354 | char *opt, *param; | |
355 | units_list *unit; | |
356 | int i, rc; | |
357 | float fval; | |
358 | ||
359 | opt = strtok(buf, " \0"); | |
360 | opt = strtok(NULL, " \0"); | |
361 | if (opt == NULL) { | |
362 | srv_send((char *)"501 Subcommand missing"); | |
363 | return 1; | |
364 | } | |
365 | ||
366 | param = strtok(NULL, "\0"); | |
367 | if (param == NULL) { | |
368 | srv_send((char *)"501 Parameter missing"); | |
369 | return 1; | |
370 | } | |
371 | ||
372 | rc = sscanf(param, "%f", &fval); | |
373 | ||
374 | if (debug) | |
375 | fprintf(stdout, "opt='%s' param='%s' rc=%d fval=%.1f\n", opt, param, rc, fval); | |
376 | ||
377 | /* | |
378 | * Commands below need a selected unit | |
379 | */ | |
380 | if (current_unit == -1) { | |
381 | srv_send((char *)"401 No fermenter unit selected"); | |
382 | return 1; | |
383 | } | |
384 | ||
385 | i = 0; | |
386 | for (unit = Config.units; unit; unit = unit->next) { | |
387 | i++; | |
388 | if (i == current_unit) | |
389 | break; | |
390 | } | |
391 | ||
392 | /* | |
393 | * If a valid parameter float value | |
394 | */ | |
395 | if (rc == 1) { | |
396 | if (strcmp(opt, (char *)"BEER") == 0) { | |
397 | if ((fval >= unit->temp_set_min) && (fval <= unit->temp_set_max)) { | |
398 | unit->beer_set = fval; | |
399 | srv_send((char *)"214 Unit %d BEER set to %.1f", current_unit, fval); | |
400 | return 0; | |
401 | } else { | |
402 | srv_send((char *)"510 New temperature not between %.1f and %.1f", unit->temp_set_min, unit->temp_set_max); | |
403 | return 1; | |
404 | } | |
405 | } else if (strcmp(opt, (char *)"FRIDGE") == 0) { | |
406 | if ((fval >= unit->temp_set_min) && (fval <= unit->temp_set_max)) { | |
407 | unit->fridge_set = fval; | |
408 | srv_send((char *)"214 Unit %d BEER set to %.1f", current_unit, fval); | |
409 | return 0; | |
410 | } else { | |
411 | srv_send((char *)"510 New temperature not between %.1f and %.1f", unit->temp_set_min, unit->temp_set_max); | |
412 | return 1; | |
413 | } | |
414 | } else if (strcmp(opt, (char *)"VOLUME") == 0) { | |
415 | /* | |
416 | * Must fit in a 2 TEU container | |
417 | */ | |
418 | if ((fval >= 0.0) && (fval <= 77020.0)) { | |
419 | unit->volume = fval; | |
420 | srv_send((char *)"214 Unit %d VOLUME set to %.1f", current_unit, fval); | |
421 | return 0; | |
422 | } else { | |
423 | srv_send((char *)"510 New volume not between 0 and 77020"); | |
424 | return 1; | |
425 | } | |
426 | } else if (strcmp(opt, (char *)"IDLE_LOW") == 0) { | |
427 | if ((fval >= -5.0) && (fval <= -0.1)) { | |
428 | unit->idle_rangeL = fval; | |
429 | srv_send((char *)"214 Unit %d IDLE_LOW set to %.1f", current_unit, fval); | |
430 | return 0; | |
431 | } else { | |
432 | srv_send((char *)"510 New value not between -5.0 and -0.1"); | |
433 | return 1; | |
434 | } | |
435 | } else if (strcmp(opt, (char *)"IDLE_HIGH") == 0) { | |
436 | if ((fval >= 0.1) && (fval <= 5.0)) { | |
437 | unit->idle_rangeH = fval; | |
438 | srv_send((char *)"214 Unit %d IDLE_HIGH set to %.1f", current_unit, fval); | |
439 | return 0; | |
440 | } else { | |
441 | srv_send((char *)"510 New value not between -5.0 and -0.1"); | |
442 | return 1; | |
443 | } | |
444 | } else if (strcmp(opt, (char *)"TEMP_MIN") == 0) { | |
445 | if ((fval >= -2.0) && (fval <= 35.0) && (fval < unit->temp_set_max)) { | |
446 | unit->temp_set_min = fval; | |
447 | srv_send((char *)"214 Unit %d TEMP_MIN set to %.1f", current_unit, fval); | |
448 | return 0; | |
449 | } else { | |
450 | srv_send((char *)"510 New value not between -2.0 and 35.0 and lower then TEMP_MAX"); | |
451 | return 1; | |
452 | } | |
453 | } else if (strcmp(opt, (char *)"TEMP_MAX") == 0) { | |
454 | if ((fval >= -2.0) && (fval <= 35.0) && (fval > unit->temp_set_min)) { | |
455 | unit->temp_set_max = fval; | |
456 | srv_send((char *)"214 Unit %d TEMP_MAX set to %.1f", current_unit, fval); | |
457 | return 0; | |
458 | } else { | |
459 | srv_send((char *)"510 New value not between -2.0 and 35.0 and higher then TEMP_MIN"); | |
460 | return 1; | |
461 | } | |
462 | } | |
463 | } | |
464 | ||
465 | /* | |
466 | * Set new unit or beer name | |
467 | */ | |
468 | if (strcmp(opt, (char *)"NAME") == 0) { | |
469 | if (unit->name) | |
470 | free(unit->name); | |
471 | unit->name = xstrcpy(param); | |
472 | srv_send((char *)"214 Unit %d NAME set to '%s'", current_unit, param); | |
473 | // TODO: change logfile name | |
474 | return 0; | |
475 | } else if (strcmp(opt, (char *)"PROFILE") == 0) { | |
476 | // check profile file, excists, type, at least one profile item | |
477 | } | |
478 | ||
479 | srv_send((char *)"502 Unknown command option"); | |
480 | return 1; | |
481 | } | |
482 | ||
483 | ||
484 | ||
485 | /* | |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
486 | * UNIT n |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
487 | * UNIT uuid |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
488 | */ |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
489 | int cmd_unit(char *buf) |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
490 | { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
491 | char *opt; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
492 | units_list *tmp; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
493 | int i, unit_no; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
494 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
495 | opt = strtok(buf, " \0"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
496 | opt = strtok(NULL, " \0"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
497 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
498 | if (opt == NULL) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
499 | srv_send((char *)"501 Parameter missing"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
500 | return 1; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
501 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
502 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
503 | i = 0; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
504 | if (strlen(opt) == 36) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
505 | /* |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
506 | * Search using uuid |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
507 | */ |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
508 | for (tmp = Config.units; tmp; tmp = tmp->next) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
509 | i++; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
510 | if (strcmp(opt, tmp->uuid) == 0) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
511 | srv_send((char *)"210 Unit %d selected", i); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
512 | current_unit = i; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
513 | return 0; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
514 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
515 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
516 | srv_send((char *)"410 No such unit"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
517 | return 1; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
518 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
519 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
520 | if (sscanf(opt, "%d", &unit_no) == 1) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
521 | /* |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
522 | * We got a number, see if it is valid. |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
523 | */ |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
524 | for (tmp = Config.units; tmp; tmp = tmp->next) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
525 | i++; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
526 | if (unit_no == i) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
527 | srv_send((char *)"210 Unit %d selected", i); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
528 | current_unit = i; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
529 | return 0; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
530 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
531 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
532 | srv_send((char *)"410 No such unit"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
533 | return 1; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
534 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
535 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
536 | srv_send((char *)"502 Unknown command option"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
537 | return 1; |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
538 | } |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
539 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
540 | |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
541 | |
43 | 542 | void cmd_server(void) |
543 | { | |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
544 | char *inp, *p, *q, buf[SS_BUFSIZE], obuf[SS_BUFSIZE]; |
48 | 545 | int i, rc, rlen; |
43 | 546 | socklen_t fromlen; |
46
000399c64d3f
More client/server communications
Michiel Broek <mbroek@mbse.eu>
parents:
45
diff
changeset
|
547 | float newtemp; |
68
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
548 | #ifdef HAVE_WIRINGPI_H |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
549 | int j; |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
550 | #endif |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
551 | |
48 | 552 | memset((char *)&buf, 0, SS_BUFSIZE); |
553 | fromlen = sizeof(peeraddr_in); | |
554 | rlen = recvfrom(s, buf, sizeof(buf) -1, 0, (struct sockaddr *)&peeraddr_in, &fromlen); | |
555 | if (rlen == -1) { | |
556 | syslog(LOG_NOTICE, "recvfrom(): %s", strerror(errno)); | |
557 | } else { | |
558 | for (i = 0; i < strlen(buf); i++) { | |
559 | if (buf[i] == '\n') | |
560 | buf[i] = '\0'; | |
561 | if (buf[i] == '\r') | |
562 | buf[i] = '\0'; | |
563 | } | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
564 | for (i = strlen(buf) -1; i > 0; i--) { |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
565 | if (buf[i] == ' ') |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
566 | buf[i] = '\0'; |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
567 | else |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
568 | break; |
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
569 | } |
48 | 570 | if (strlen(buf)) { |
571 | if (debug) { | |
572 | syslog(LOG_NOTICE, "recv: \"%s\"", buf); | |
573 | fprintf(stdout, "recv: \"%s\"\n", buf); | |
43 | 574 | } |
575 | ||
576 | /* | |
48 | 577 | * Process commands from the client |
43 | 578 | */ |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
579 | if (strncmp(buf, "ADD", 3) == 0) { |
92 | 580 | if (unit_add(buf) == 0) |
581 | wrconfig(); | |
80
81bf78a7618e
Switched to xml configuration
Michiel Broek <mbroek@mbse.eu>
parents:
79
diff
changeset
|
582 | } else if (strncmp(buf, "HELP", 4) == 0) { |
68
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
583 | srv_send((char *)"100 Help text follows"); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
584 | srv_send((char *)"Recognized commands:"); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
585 | srv_send((char *)""); |
92 | 586 | srv_send((char *)"ADD PROFILE name Add a new profile with \"name\""); |
587 | srv_send((char *)"ADD UNIT name Add a new unit with \"name\""); | |
68
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
588 | srv_send((char *)"LCD Get LCD screen (allways 4 rows of 20 characters)"); |
81 | 589 | srv_send((char *)"LIST List all fermenter units"); |
84 | 590 | srv_send((char *)"LIST BUS List 1-wire bus"); |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
591 | srv_send((char *)"LIST PROFILES List available profiles"); |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
592 | srv_send((char *)"LIST UNIT List fermenter unit"); |
88 | 593 | srv_send((char *)"MODE OFF|NONE|BEER|FRIDGE|PROFILE"); |
81 | 594 | // srv_send((char *)"PROFILE Profile status"); |
595 | // srv_send((char *)"PROFILE start|stop|pause Profile start, stop or pause"); | |
88 | 596 | srv_send((char *)"SET BEER val Set beer temperature"); |
597 | srv_send((char *)"SET FRIDGE val Set fridge temperature"); | |
598 | srv_send((char *)"SET IDLE_LOW val Set idle temperature low (-5.0 .. -0.1)"); | |
599 | srv_send((char *)"SET IDLE_HIGH val Set idle temperature high (0.1 .. 5.0)"); | |
600 | srv_send((char *)"SET NAME name Set name or beername for the unit"); | |
81 | 601 | // srv_send((char *)"SET PROFILE name Set named profile"); |
88 | 602 | srv_send((char *)"SET TEMP_MIN val Set unit minimum temperature"); |
603 | srv_send((char *)"SET TEMP_MAX val Set unit maximum temperature"); | |
604 | srv_send((char *)"SET VOLUME val Set unit volume"); | |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
605 | srv_send((char *)"UNIT n|uuid Select unit by number or uuid"); |
68
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
606 | srv_send((char *)"."); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
607 | } else if (strncmp(buf, "LCD", 3) == 0) { |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
608 | #ifdef HAVE_WIRINGPI_H |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
609 | srv_send((char *)"201 information follows"); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
610 | for (j = 0; j < 4; j++) { |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
611 | sprintf(obuf, " "); |
70 | 612 | obuf[20] = '\0'; |
68
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
613 | for (i = 0; i < 20; i++) |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
614 | obuf[i] = lcdbuf[lcdHandle][i][j]; |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
615 | srv_send(obuf); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
616 | } |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
617 | srv_send((char *)"."); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
618 | #else |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
619 | srv_send((char *)"403 LCD not available"); |
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
620 | #endif |
81 | 621 | } else if (strncmp(buf, "LIST", 4) == 0) { |
622 | cmd_list(buf); | |
88 | 623 | } else if (strncmp(buf, "MODE", 4) == 0) { |
624 | cmd_mode(buf); | |
625 | } else if (strncmp(buf, "SET", 3) == 0) { | |
92 | 626 | if (cmd_set(buf) == 0) |
627 | wrconfig(); | |
87
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
628 | } else if (strncmp(buf, "UNIT", 4) == 0) { |
55f1315c94f1
Added unit command and list unit command
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
629 | cmd_unit(buf); |
95 | 630 | |
631 | /* | |
632 | * Now the old style (brewPi) commands. These will go away. | |
633 | */ | |
68
6dba71439cd8
Added first part of out own protocol
Michiel Broek <mbroek@mbse.eu>
parents:
66
diff
changeset
|
634 | } else if (strncmp(buf, "ack", 3) == 0) { |
48 | 635 | srv_send((char *)"ack"); |
636 | } else if (strncmp(buf, "lcd", 3) == 0) { | |
637 | sprintf(obuf, "[\" \", \" \", \" \", \" \"]"); | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
638 | #ifdef HAVE_WIRINGPI_H |
48 | 639 | for (i = 0; i < 20; i++) { |
640 | obuf[i+2] = lcdbuf[lcdHandle][i][0]; | |
641 | obuf[i+26] = lcdbuf[lcdHandle][i][1]; | |
642 | obuf[i+50] = lcdbuf[lcdHandle][i][2]; | |
643 | obuf[i+74] = lcdbuf[lcdHandle][i][3]; | |
644 | } | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
645 | #endif |
48 | 646 | srv_send(obuf); |
647 | } else if (strncmp(buf, "getMode", 7) == 0) { | |
95 | 648 | srv_send("%c", UNITmode[Config.units->mode]); |
48 | 649 | } else if (strncmp(buf, "getFridge", 9) == 0) { |
95 | 650 | srv_send("%.1f", Config.units->fridge_set); |
48 | 651 | } else if (strncmp(buf, "getBeer", 7) == 0) { |
95 | 652 | srv_send("%.1f", Config.units->beer_set); |
48 | 653 | } else if (strncmp(buf, "getControlConstants", 19) == 0) { |
654 | srv_send("{ \"tempFormat\":\"%c\", \"tempSetMin\":%.1f, \"tempSetMax\":%.1f, \"idleRangeH\":%.3f, \"idleRangeL\":%.3f }", | |
95 | 655 | Config.tempFormat, Config.units->temp_set_min, Config.units->temp_set_max, Config.units->idle_rangeH, Config.units->idle_rangeL); |
48 | 656 | } else if (strncmp(buf, "getControlSettings", 18) == 0) { |
657 | srv_send("{ \"mode\":\"%c\", \"beerSet\":%.1f, \"fridgeSet\":%.1f, \"heatEstimator\":%.1f, \"coolEstimator\":%.1f }", | |
95 | 658 | UNITmode[Config.units->mode], Config.units->beer_set, Config.units->fridge_set, cs_heatEstimator, cs_coolEstimator); |
48 | 659 | } else if (strncmp(buf, "getControlVariables", 19) == 0) { |
660 | srv_send("{ \"beerDiff\":%.2f }", cv_beerDiff); | |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
661 | } else if (strncmp(buf, "loadDefaultControlSettings", 26) == 0) { |
95 | 662 | // Does nothing anymore |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
663 | srv_send("ack"); |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
664 | } else if (strncmp(buf, "loadDefaultControlConstants", 27) == 0) { |
95 | 665 | // Does nothing anymore |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
666 | srv_send("ack"); |
48 | 667 | } else if (strncmp(buf, "setBeer=", 8) == 0) { |
668 | inp = xstrcpy(buf+8); | |
669 | rc = sscanf(inp, "%f", &newtemp); | |
670 | if (debug) | |
671 | fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); | |
672 | if (rc == 1) { | |
95 | 673 | if ((Config.units->temp_set_min <= newtemp) && (newtemp <= Config.units->temp_set_max)) { |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
674 | 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
|
675 | srv_send("ack"); |
95 | 676 | Config.units->mode = UNITMODE_BEER; |
677 | Config.units->beer_set = newtemp; | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
678 | } else { |
95 | 679 | syslog(LOG_NOTICE, "Beer temperature setting %.1f is outside of allowed range %.1f - %.1f", newtemp, Config.units->temp_set_min, Config.units->temp_set_max); |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
680 | srv_send("err"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
681 | } |
48 | 682 | } else { |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
683 | syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp); |
48 | 684 | srv_send("err"); |
685 | } | |
686 | free(inp); | |
687 | } else if (strncmp(buf, "setFridge=", 10) == 0) { | |
688 | inp = xstrcpy(buf+10); | |
689 | rc = sscanf(inp, "%f", &newtemp); | |
690 | if (debug) | |
691 | fprintf(stdout, "new temp from %s, %.1f, rc=%d\n", inp, newtemp, rc); | |
692 | if (rc == 1) { | |
95 | 693 | if ((Config.units->temp_set_min <= newtemp) && (newtemp <= Config.units->temp_set_max)) { |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
694 | 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
|
695 | srv_send("ack"); |
95 | 696 | Config.units->mode = UNITMODE_FRIDGE; |
697 | Config.units->fridge_set = newtemp; | |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
698 | } else { |
95 | 699 | syslog(LOG_NOTICE, "Fridge temperature setting %.1f is outside of allowed range %.1f - %.1f", newtemp, Config.units->temp_set_min, Config.units->temp_set_max); |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
700 | srv_send("err"); |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
701 | } |
48 | 702 | } else { |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
703 | syslog(LOG_NOTICE, "Cannot convert temperature '%s' to float", inp); |
48 | 704 | srv_send("err"); |
705 | } | |
706 | free(inp); | |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
707 | } else if (strncmp(buf, "setOff", 6) == 0) { |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
708 | if (debug) |
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
709 | 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
|
710 | syslog(LOG_NOTICE, "Notification: Temperature control disabled"); |
95 | 711 | Config.units->mode = UNITMODE_OFF; |
49
92a9ca143677
Fixed temperature logging, added some commands
Michiel Broek <mbroek@mbse.eu>
parents:
48
diff
changeset
|
712 | srv_send("ack"); |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
713 | } else if (strncmp(buf, "setParameters=", 14) == 0) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
714 | inp = xstrcpy(buf+14); /* {"tempSetMax":30.5} */ |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
715 | if (debug) |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
716 | fprintf(stdout, "setParameters: %s\n", inp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
717 | strtok(inp, (char *)"\""); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
718 | p = strtok(NULL, (char *)"\""); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
719 | q = strtok(NULL, (char *)":,}"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
720 | if (strcmp(p, (char *)"tempSetMin") == 0) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
721 | rc = sscanf(q, "%f", &newtemp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
722 | if (rc == 1) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
723 | syslog(LOG_NOTICE, "cc_tempSetMin = %1.f", newtemp); |
95 | 724 | Config.units->temp_set_min = newtemp; |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
725 | srv_send("ack"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
726 | } else { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
727 | srv_send("ERR"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
728 | } |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
729 | } else if (strcmp(p, (char *)"tempSetMax") == 0) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
730 | rc = sscanf(q, "%f", &newtemp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
731 | if (rc == 1) { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
732 | syslog(LOG_NOTICE, "cc_tempSetMax = %1.f", newtemp); |
95 | 733 | Config.units->temp_set_max = newtemp; |
54
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
734 | srv_send("ack"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
735 | } else { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
736 | srv_send("ERR"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
737 | } |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
738 | } else { |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
739 | fprintf(stdout, "p='%s' q='%s' inp='%s'\n", p, q, inp); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
740 | srv_send("ERR"); |
c06190a58f22
More configuration changes. Writing settings implemented.
Michiel Broek <mbroek@mbse.eu>
parents:
53
diff
changeset
|
741 | } |
62 | 742 | free(inp); |
51
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
743 | // stopScript |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
744 | // quit |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
745 | // eraseLogs |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
746 | // interval |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
747 | // startNewBrew |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
748 | // pauseLogging |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
749 | // stopLogging |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
750 | // resumeLogging |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
751 | // dateTimeFormatDisplay |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
752 | } 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
|
753 | 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
|
754 | // programArduino |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
755 | // refreshDeviceList |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
756 | // getDeviceList |
a03b6dac5398
Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
49
diff
changeset
|
757 | // applyDevice |
43 | 758 | } else { |
48 | 759 | if (debug) |
760 | fprintf(stdout, "unknown command \"%s\"\n", buf); | |
761 | srv_send((char *)"ERR"); | |
43 | 762 | } |
763 | } | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
764 | } |
44 | 765 | |
766 | close(s); | |
42
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
767 | } |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
768 | |
01b96a24ae7c
Corrected missing and obsolete files
Michiel Broek <mbroek@mbse.eu>
parents:
diff
changeset
|
769 | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
770 | #ifdef HAVE_WIRINGPI_H |
43 | 771 | PI_THREAD (my_server_loop) |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
772 | #else |
74 | 773 | void *my_server_loop(void *threadid) |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
774 | #endif |
43 | 775 | { |
776 | socklen_t addrlen; | |
44 | 777 | int optval = 1; |
43 | 778 | |
779 | syslog(LOG_NOTICE, "Thread my_server_loop started"); | |
780 | if (debug) | |
781 | fprintf(stdout, "Thread my_server_loop started\n"); | |
782 | ||
44 | 783 | memset((char *)&myaddr_in, 0, sizeof(struct sockaddr_in)); |
784 | memset((char *)&peeraddr_in, 0, sizeof(struct sockaddr_in)); | |
785 | myaddr_in.sin_family = AF_INET; | |
786 | myaddr_in.sin_addr.s_addr = INADDR_ANY; | |
787 | myaddr_in.sin_port = htons(Config.my_port); | |
788 | ||
789 | ls = socket(AF_INET, SOCK_STREAM, 0); | |
790 | if (ls == -1) { | |
791 | syslog(LOG_NOTICE, "Can't create listen socket: %s", strerror(errno)); | |
792 | fprintf(stderr, "Can't create listen socket: %s\n", strerror(errno)); | |
793 | return 0; | |
794 | } | |
795 | ||
796 | if (setsockopt(ls, SOL_SOCKET, SO_KEEPALIVE, &optval, sizeof(optval)) == -1) { | |
797 | syslog(LOG_NOTICE, "Can't setsockopt SO_KEEPALIVE socket: %s", strerror(errno)); | |
798 | close(ls); | |
799 | return 0; | |
800 | } | |
801 | ||
802 | if (setsockopt(ls, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1) { | |
803 | syslog(LOG_NOTICE, "Can't setsockopt SO_REUSEADDR socket: %s", strerror(errno)); | |
804 | close(ls); | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
805 | return 0; |
44 | 806 | } |
807 | ||
808 | if (bind(ls, (struct sockaddr *)&myaddr_in, sizeof(struct sockaddr_in)) == -1) { | |
809 | syslog(LOG_NOTICE, "Can't bind to listen socket: %s", strerror(errno)); | |
810 | close(ls); | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
811 | return 0; |
44 | 812 | } |
813 | ||
814 | if (listen(ls, 5) == -1) { | |
815 | syslog(LOG_NOTICE, "Can't listen on listen socket: %s", strerror(errno)); | |
816 | close(ls); | |
66
805f1d285acd
Compiles with 1-wire bus on PC hardware
Michiel Broek <mbroek@mbse.eu>
parents:
62
diff
changeset
|
817 | return 0; |
44 | 818 | } |
819 | ||
820 | syslog(LOG_NOTICE, "listen socket created %d", ls); | |
821 | if (debug) | |
822 | fprintf(stdout, "listen socket created %d\n", ls); | |
823 | ||
824 | ||
43 | 825 | /* |
826 | * Loop forever until the external shutdown variable is set. | |
827 | */ | |
828 | for (;;) { | |
829 | ||
830 | addrlen = sizeof(struct sockaddr_in); | |
831 | /* | |
832 | * This call will block until a new connection | |
833 | * arrives. Then it will return the address of | |
834 | * the connecting peer, and a new socket | |
835 | * descriptor, s, for that connection. | |
836 | */ | |
837 | s = accept(ls, (struct sockaddr *)&peeraddr_in, &addrlen); | |
838 | if (s == -1) { | |
839 | syslog(LOG_NOTICE, "my_server_loop accept failed %s", strerror(errno)); | |
840 | if (debug) | |
841 | fprintf(stdout, "my_server_loop accept failed %s\n", strerror(errno)); | |
44 | 842 | return 0; |
43 | 843 | } |
844 | ||
48 | 845 | cmd_server(); |
43 | 846 | |
847 | if (my_shutdown) { | |
848 | syslog(LOG_NOTICE, "Thread my_server_loop stopped"); | |
849 | if (debug) | |
850 | fprintf(stdout, "Thread my_server_loop stopped\n"); | |
851 | return 0; | |
852 | } | |
853 | ||
854 | } | |
855 | } | |
856 | ||
857 |