forked from pilight/pilight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.c
338 lines (307 loc) · 9.11 KB
/
control.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
Copyright (C) 2013 - 2014 CurlyMo
This file is part of pilight.
pilight is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
pilight is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pilight. If not, see <http://www.gnu.org/licenses/>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <syslog.h>
#include "pilight.h"
#include "common.h"
#include "settings.h"
#include "config.h"
#include "log.h"
#include "options.h"
#include "socket.h"
#include "json.h"
#include "ssdp.h"
#include "dso.h"
#include "protocol.h"
typedef enum {
WELCOME,
IDENTIFY,
REJECT,
REQUEST,
CONFIG
} steps_t;
int main(int argc, char **argv) {
log_file_disable();
log_shell_enable();
log_level_set(LOG_NOTICE);
if(!(progname = malloc(16))) {
logprintf(LOG_ERR, "out of memory");
exit(EXIT_FAILURE);
}
strcpy(progname, "pilight-control");
struct options_t *options = NULL;
struct ssdp_list_t *ssdp_list = NULL;
int sockfd = 0;
char *recvBuff = NULL;
char *message = NULL;
char *pch = NULL;
steps_t steps = WELCOME;
char device[50];
char location[50];
char state[10];
char values[255];
struct conf_locations_t *slocation = NULL;
struct conf_devices_t *sdevice = NULL;
int has_values = 0;
memset(device, '\0', 50);
memset(location, '\0', 50);
memset(state, '\0', 10);
memset(values, '\0', 255);
char *server = NULL;
unsigned short port = 0;
JsonNode *json = NULL;
JsonNode *jconfig = NULL;
JsonNode *jcode = NULL;
JsonNode *jvalues = NULL;
char settingstmp[] = SETTINGS_FILE;
settings_set_file(settingstmp);
/* Define all CLI arguments of this program */
options_add(&options, 'H', "help", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
options_add(&options, 'V', "version", OPTION_NO_VALUE, 0, JSON_NULL, NULL, NULL);
options_add(&options, 'l', "location", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL);
options_add(&options, 'd', "device", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL);
options_add(&options, 's', "state", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL);
options_add(&options, 'v', "values", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL);
options_add(&options, 'S', "server", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
options_add(&options, 'P', "port", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, "[0-9]{1,4}");
options_add(&options, 'F', "settings", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL);
/* Store all CLI arguments for later usage
and also check if the CLI arguments where
used correctly by the user. This will also
fill all necessary values in the options struct */
while(1) {
int c;
c = options_parse(&options, argc, argv, 1, &optarg);
if(c == -1)
break;
if(c == -2)
c = 'H';
switch(c) {
case 'H':
printf("\t -H --help\t\t\tdisplay this message\n");
printf("\t -V --version\t\t\tdisplay version\n");
printf("\t -S --server=x.x.x.x\t\tconnect to server address\n");
printf("\t -F --settings\t\tsettings file\n");
printf("\t -P --port=xxxx\t\t\tconnect to server port\n");
printf("\t -l --location=location\t\tthe location in which the device resides\n");
printf("\t -d --device=device\t\tthe device that you want to control\n");
printf("\t -s --state=state\t\tthe new state of the device\n");
printf("\t -v --values=values\t\tspecific comma separated values, e.g.:\n");
printf("\t\t\t\t\t-v dimlevel=10\n");
goto close;
break;
case 'V':
printf("%s %s\n", progname, VERSION);
goto close;
break;
case 'l':
strcpy(location, optarg);
break;
case 'd':
strcpy(device, optarg);
break;
case 's':
strcpy(state, optarg);
break;
case 'v':
strcpy(values, optarg);
break;
case 'F':
if(settings_set_file(optarg) == EXIT_FAILURE) {
return EXIT_FAILURE;
}
break;
case 'S':
if(!(server = realloc(server, strlen(optarg)+1))) {
logprintf(LOG_ERR, "out of memory");
exit(EXIT_FAILURE);
}
strcpy(server, optarg);
break;
case 'P':
port = (unsigned short)atoi(optarg);
break;
default:
printf("Usage: %s -l location -d device -s state\n", progname);
goto close;
break;
}
}
options_delete(options);
if(strlen(location) == 0 || strlen(device) == 0 || strlen(state) == 0) {
printf("Usage: %s -l location -d device -s state\n", progname);
goto close;
}
if(server && port > 0) {
if((sockfd = socket_connect(server, port)) == -1) {
logprintf(LOG_ERR, "could not connect to pilight-daemon");
goto close;
}
} else if(ssdp_seek(&ssdp_list) == -1) {
logprintf(LOG_ERR, "no pilight ssdp connections found");
goto close;
} else {
if((sockfd = socket_connect(ssdp_list->ip, ssdp_list->port)) == -1) {
logprintf(LOG_ERR, "could not connect to pilight-daemon");
goto close;
}
}
if(ssdp_list) {
ssdp_free(ssdp_list);
}
if(settings_read() != 0) {
return EXIT_FAILURE;
}
protocol_init();
while(1) {
if(steps > WELCOME) {
/* Clear the receive buffer again and read the welcome message */
if((recvBuff = socket_read(sockfd))) {
json = json_decode(recvBuff);
json_find_string(json, "message", &message);
sfree((void *)&recvBuff);
} else {
goto close;
}
}
switch(steps) {
case WELCOME:
socket_write(sockfd, "{\"message\":\"client controller\"}");
steps=IDENTIFY;
break;
case IDENTIFY:
if(message && strlen(message) > 0) {
if(strcmp(message, "accept client") == 0) {
steps=REQUEST;
}
if(strcmp(message, "reject client") == 0) {
steps=REJECT;
}
} else {
goto close;
}
case REQUEST:
socket_write(sockfd, "{\"message\":\"request config\"}");
steps=CONFIG;
json_delete(json);
break;
case CONFIG:
if((jconfig = json_find_member(json, "config")) != NULL) {
config_parse(jconfig);
if(config_get_location(location, &slocation) == 0) {
if(config_get_device(location, device, &sdevice) == 0) {
JsonNode *joutput = json_mkobject();
jcode = json_mkobject();
jvalues = json_mkobject();
json_append_member(jcode, "location", json_mkstring(location));
json_append_member(jcode, "device", json_mkstring(device));
pch = strtok(values, ",=");
while(pch != NULL) {
char *name = strdup(pch);
pch = strtok(NULL, ",=");
if(pch == NULL) {
sfree((void *)&name);
break;
} else {
char *val = strdup(pch);
if(pch != NULL) {
if(config_valid_value(location, device, name, val) == 0) {
if(isNumeric(val) == EXIT_SUCCESS) {
json_append_member(jvalues, name, json_mknumber(atof(val)));
} else {
json_append_member(jvalues, name, json_mkstring(val));
}
has_values = 1;
} else {
logprintf(LOG_ERR, "\"%s\" is an invalid value for device \"%s\"", name, device);
sfree((void *)&name);
json_delete(json);
goto close;
}
} else {
logprintf(LOG_ERR, "\"%s\" is an invalid value for device \"%s\"", name, device);
json_delete(json);
goto close;
}
pch = strtok(NULL, ",=");
if(pch == NULL) {
sfree((void *)&name);
break;
}
}
sfree((void *)&name);
}
if(config_valid_state(location, device, state) == 0) {
json_append_member(jcode, "state", json_mkstring(state));
} else {
logprintf(LOG_ERR, "\"%s\" is an invalid state for device \"%s\"", state, device);
json_delete(json);
goto close;
}
if(has_values == 1) {
json_append_member(jcode, "values", jvalues);
} else {
json_delete(jvalues);
}
json_append_member(joutput, "message", json_mkstring("send"));
json_append_member(joutput, "code", jcode);
char *output = json_stringify(joutput, NULL);
socket_write(sockfd, output);
sfree((void *)&output);
json_delete(joutput);
} else {
logprintf(LOG_ERR, "the device \"%s\" does not exist", device);
json_delete(json);
goto close;
}
} else {
logprintf(LOG_ERR, "the location \"%s\" does not exist", location);
json_delete(json);
goto close;
}
}
json_delete(json);
goto close;
break;
case REJECT:
default:
json_delete(json);
goto close;
break;
}
}
close:
if(sockfd > 0) {
socket_close(sockfd);
}
if(server) {
sfree((void *)&server);
}
log_shell_disable();
config_gc();
protocol_gc();
socket_gc();
options_gc();
settings_gc();
dso_gc();
log_gc();
sfree((void *)&progname);
return EXIT_SUCCESS;
}