forked from pilight/pilight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send.c
433 lines (399 loc) · 10.8 KB
/
send.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
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 <stdarg.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 "log.h"
#include "options.h"
#include "socket.h"
#include "json.h"
#include "protocol.h"
#include "ssdp.h"
#include "dso.h"
typedef enum {
WELCOME,
IDENTIFY,
REJECT,
SEND
} steps_t;
typedef struct pname_t {
char *name;
char *desc;
struct pname_t *next;
} pname_t;
static struct pname_t *pname = NULL;
static void sort_list(void) {
struct pname_t *a = NULL;
struct pname_t *b = NULL;
struct pname_t *c = NULL;
struct pname_t *e = NULL;
struct pname_t *tmp = NULL;
while(pname && e != pname->next) {
c = a = pname;
b = a->next;
while(a != e) {
if(strcmp(a->name, b->name) > 0) {
if(a == pname) {
tmp = b->next;
b->next = a;
a->next = tmp;
pname = b;
c = b;
} else {
tmp = b->next;
b->next = a;
a->next = tmp;
c->next = b;
c = b;
}
} else {
c = a;
a = a->next;
}
b = a->next;
if(b == e)
e = a;
}
}
}
int main(int argc, char **argv) {
log_file_disable();
log_shell_enable();
log_level_set(LOG_NOTICE);
if(!(progname = malloc(13))) {
logprintf(LOG_ERR, "out of memory");
exit(EXIT_FAILURE);
}
strcpy(progname, "pilight-send");
struct options_t *options = NULL;
struct ssdp_list_t *ssdp_list = NULL;
int sockfd = 0;
char *recvBuff = NULL;
char *message = NULL;
char *args = NULL;
steps_t steps = WELCOME;
/* Hold the name of the protocol */
char *protobuffer = NULL;
/* Does this protocol exists */
int match = 0;
/* Do we need to print the help */
int help = 0;
/* Do we need to print the version */
int version = 0;
/* Do we need to print the protocol help */
int protohelp = 0;
char *server = NULL;
unsigned short port = 0;
/* Hold the final protocol struct */
protocol_t *protocol = NULL;
JsonNode *code = 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, 'p', "protocol", 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);
/* Get the protocol to be used */
while(1) {
int c;
c = options_parse(&options, argc, argv, 0, &args);
if(c == -1)
break;
if(c == -2)
c = 'H';
switch(c) {
case 'p':
if(strlen(args) == 0) {
logprintf(LOG_ERR, "options '-p' and '--protocol' require an argument");
exit(EXIT_FAILURE);
} else {
if(!(protobuffer = realloc(protobuffer, strlen(args)+1))) {
logprintf(LOG_ERR, "out of memory");
exit(EXIT_FAILURE);
}
strcpy(protobuffer, args);
}
break;
case 'V':
version = 1;
break;
case 'H':
help = 1;
break;
case 'F':
if(settings_set_file(args) == EXIT_FAILURE) {
return EXIT_FAILURE;
}
break;
case 'S':
if(!(server = realloc(server, strlen(args)+1))) {
logprintf(LOG_ERR, "out of memory");
exit(EXIT_FAILURE);
}
strcpy(server, args);
break;
case 'P':
port = (unsigned short)atoi(args);
break;
default:;
}
}
if(settings_read() != 0) {
return EXIT_FAILURE;
}
/* Initialize protocols */
protocol_init();
/* Check if a protocol was given */
if(protobuffer && strlen(protobuffer) > 0 && strcmp(protobuffer, "-V") != 0) {
if(strlen(protobuffer) > 0 && version) {
printf("-p and -V cannot be combined\n");
} else {
struct protocols_t *pnode = protocols;
/* Retrieve the used protocol */
while(pnode) {
/* Check if the protocol exists */
protocol = pnode->listener;
if(protocol_device_exists(protocol, protobuffer) == 0 && match == 0 && protocol->createCode != NULL) {
match=1;
/* Check if the protocol requires specific CLI arguments
and merge them with the main CLI arguments */
if(protocol->options && help == 0) {
options_merge(&options, &protocol->options);
} else if(help == 1) {
protohelp=1;
}
break;
}
pnode = pnode->next;
}
/* If no protocols matches the requested protocol */
if(!match) {
logprintf(LOG_ERR, "this protocol is not supported or doesn't support sending");
}
}
}
/* 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, 2, &args);
if(c == -1)
break;
if(c == -2) {
if(match == 1) {
protohelp = 1;
} else {
help = 1;
}
break;
}
}
/* Display help or version information */
if(version == 1) {
printf("%s %s\n", progname, VERSION);
goto close;
} else if(help == 1 || protohelp == 1 || match == 0) {
if(protohelp == 1 && match == 1 && protocol->printHelp)
printf("Usage: %s -p %s [options]\n", progname, protobuffer);
else
printf("Usage: %s -p protocol [options]\n", progname);
if(help == 1) {
printf("\t -H --help\t\t\tdisplay this message\n");
printf("\t -V --version\t\t\tdisplay version\n");
printf("\t -p --protocol=protocol\t\tthe protocol that you want to control\n");
printf("\t -S --server=x.x.x.x\t\tconnect to server address\n");
printf("\t -P --port=xxxx\t\t\tconnect to server port\n");
printf("\t -F --settings\t\t\tsettings file\n");
}
if(protohelp == 1 && match == 1 && protocol->printHelp) {
printf("\n\t[%s]\n", protobuffer);
protocol->printHelp();
} else {
printf("\nThe supported protocols are:\n");
struct protocols_t *pnode = protocols;
/* Retrieve the used protocol */
while(pnode) {
protocol = pnode->listener;
if(protocol->createCode) {
struct protocol_devices_t *tmpdev = protocol->devices;
while(tmpdev) {
struct pname_t *node = malloc(sizeof(struct pname_t));
if(!node) {
logprintf(LOG_ERR, "out of memory");
exit(EXIT_FAILURE);
}
if(!(node->name = malloc(strlen(tmpdev->id)+1))) {
logprintf(LOG_ERR, "out of memory");
exit(EXIT_FAILURE);
}
strcpy(node->name, tmpdev->id);
if(!(node->desc = malloc(strlen(tmpdev->desc)+1))) {
logprintf(LOG_ERR, "out of memory");
exit(EXIT_FAILURE);
}
strcpy(node->desc, tmpdev->desc);
node->next = pname;
pname = node;
tmpdev = tmpdev->next;
}
}
pnode = pnode->next;
}
sort_list();
struct pname_t *ptmp = NULL;
while(pname) {
ptmp = pname;
printf("\t %s\t\t",ptmp->name);
if(strlen(ptmp->name) < 7)
printf("\t");
if(strlen(ptmp->name) < 15)
printf("\t");
printf("%s\n", ptmp->desc);
sfree((void *)&ptmp->name);
sfree((void *)&ptmp->desc);
pname = pname->next;
sfree((void *)&ptmp);
}
sfree((void *)&pname);
}
goto close;
}
code = json_mkobject();
int itmp = 0;
/* Check if we got sufficient arguments from this protocol */
struct options_t *tmp = options;
while(tmp) {
if(strlen(tmp->name) > 0) {
/* Only send the CLI arguments that belong to this protocol, the protocol name
and those that are called by the user */
if((options_get_id(&protocol->options, tmp->name, &itmp) == 0)
&& strlen(tmp->value) > 0) {
if(isNumeric(tmp->value) == 0) {
json_append_member(code, tmp->name, json_mknumber(atof(tmp->value)));
} else {
json_append_member(code, tmp->name, json_mkstring(tmp->value));
}
}
if(strcmp(tmp->name, "protocol") == 0 && strlen(tmp->value) > 0) {
JsonNode *jprotocol = json_mkarray();
json_append_element(jprotocol, json_mkstring(tmp->value));
json_append_member(code, "protocol", jprotocol);
}
}
tmp = tmp->next;
}
if(protocol->createCode(code) == 0) {
if(protocol->message) {
json_delete(protocol->message);
}
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);
}
while(1) {
if(steps > WELCOME) {
/* Clear the receive buffer again and read the welcome message */
if((recvBuff = socket_read(sockfd))) {
JsonNode *jrecv = json_decode(recvBuff);
json_find_string(jrecv, "message", &message);
sfree((void *)&recvBuff);
json_delete(jrecv);
} else {
goto close;
}
}
switch(steps) {
case WELCOME:
socket_write(sockfd, "{\"message\":\"client sender\"}");
steps=IDENTIFY;
break;
case IDENTIFY:
if(message && strlen(message) > 0) {
if(strcmp(message, "accept client") == 0) {
steps=SEND;
}
if(strcmp(message, "reject client") == 0) {
steps=REJECT;
}
} else {
goto close;
}
case SEND: {
JsonNode *json = json_mkobject();
json_append_member(json, "message", json_mkstring("send"));
json_append_member(json, "code", code);
char *output = json_stringify(json, NULL);
socket_write(sockfd, output);
sfree((void *)&output);
json_delete(json);
code = NULL;
goto close;
} break;
case REJECT:
default:
goto close;
break;
}
}
}
close:
if(code) {
json_delete(code);
}
if(sockfd) {
socket_close(sockfd);
}
if(server) {
sfree((void *)&server);
}
if(protobuffer) {
sfree((void *)&protobuffer);
}
log_shell_disable();
protocol_gc();
options_delete(options);
options_gc();
settings_gc();
dso_gc();
log_gc();
sfree((void *)&progname);
return EXIT_SUCCESS;
}