forked from pilight/pilight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
receive.c
209 lines (183 loc) · 5.42 KB
/
receive.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
/*
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 <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "libs/pilight/core/pilight.h"
#include "libs/pilight/core/common.h"
#include "libs/pilight/config/settings.h"
#include "libs/pilight/core/log.h"
#include "libs/pilight/core/options.h"
#include "libs/pilight/core/socket.h"
#include "libs/pilight/core/ssdp.h"
#include "libs/pilight/core/gc.h"
static int main_loop = 1;
static int sockfd = 0;
static char *recvBuff = NULL;
int main_gc(void) {
main_loop = 0;
sleep(1);
if(recvBuff != NULL) {
FREE(recvBuff);
recvBuff = NULL;
}
if(sockfd > 0) {
socket_write(sockfd, "HEART");
socket_close(sockfd);
}
xfree();
#ifdef _WIN32
WSACleanup();
#endif
return 0;
}
int main(int argc, char **argv) {
// memtrack();
atomicinit();
gc_attach(main_gc);
/* Catch all exit signals for gc */
gc_catch();
log_shell_enable();
log_file_disable();
log_level_set(LOG_NOTICE);
if((progname = MALLOC(16)) == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(progname, "pilight-receive");
struct options_t *options = NULL;
struct ssdp_list_t *ssdp_list = NULL;
char *server = NULL;
unsigned short port = 0;
unsigned short stats = 0;
char *args = NULL;
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, '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, 's', "stats", OPTION_NO_VALUE, 0, JSON_NULL, NULL, "[0-9]{1,4}");
/* 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, &args);
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 -P --port=xxxx\t\t\tconnect to server port\n");
printf("\t -s --stats\t\t\tshow CPU and RAM statistics\n");
exit(EXIT_SUCCESS);
break;
case 'V':
printf("%s v%s\n", progname, PILIGHT_VERSION);
exit(EXIT_SUCCESS);
break;
case 'S':
if((server = MALLOC(strlen(args)+1)) == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(server, args);
break;
case 'P':
port = (unsigned short)atoi(args);
break;
case 's':
stats = 1;
break;
default:
printf("Usage: %s -l location -d device\n", progname);
exit(EXIT_SUCCESS);
break;
}
}
options_delete(options);
if(server != NULL && port > 0) {
if((sockfd = socket_connect(server, port)) == -1) {
logprintf(LOG_ERR, "could not connect to pilight-daemon");
return EXIT_FAILURE;
}
} else if(ssdp_seek(&ssdp_list) == -1) {
logprintf(LOG_NOTICE, "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 != NULL) {
ssdp_free(ssdp_list);
}
if(server != NULL) {
FREE(server);
}
struct JsonNode *jclient = json_mkobject();
struct JsonNode *joptions = json_mkobject();
json_append_member(jclient, "action", json_mkstring("identify"));
json_append_member(joptions, "receiver", json_mknumber(1, 0));
json_append_member(joptions, "stats", json_mknumber(stats, 0));
json_append_member(jclient, "options", joptions);
char *out = json_stringify(jclient, NULL);
socket_write(sockfd, out);
json_free(out);
json_delete(jclient);
if(socket_read(sockfd, &recvBuff, 0) != 0 ||
strcmp(recvBuff, "{\"status\":\"success\"}") != 0) {
goto close;
}
while(main_loop) {
if(socket_read(sockfd, &recvBuff, 0) != 0) {
goto close;
}
char **array = NULL;
unsigned int n = explode(recvBuff, "\n", &array), i = 0;
for(i=0;i<n;i++) {
struct JsonNode *jcontent = json_decode(array[i]);
struct JsonNode *jtype = json_find_member(jcontent, "type");
if(jtype != NULL) {
json_remove_from_parent(jtype);
json_delete(jtype);
}
char *content = json_stringify(jcontent, "\t");
printf("%s\n", content);
json_delete(jcontent);
json_free(content);
}
array_free(&array, n);
}
close:
if(sockfd > 0) {
socket_close(sockfd);
}
if(recvBuff != NULL) {
FREE(recvBuff);
recvBuff = NULL;
}
options_gc();
log_shell_disable();
log_gc();
FREE(progname);
return EXIT_SUCCESS;
}