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