forked from pilight/pilight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
346 lines (298 loc) · 7.83 KB
/
debug.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
/*
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 <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <limits.h>
#include <errno.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include "pilight.h"
#include "common.h"
#include "operator.h"
#include "action.h"
#include "config.h"
#include "hardware.h"
#include "log.h"
#include "options.h"
#include "threads.h"
#include "wiringX.h"
#include "datetime.h"
#include "ssdp.h"
#include "socket.h"
#include "irq.h"
#include "gc.h"
#include "dso.h"
struct pilight_t pilight;
static int pulselen = 0;
static unsigned short main_loop = 1;
static unsigned short inner_loop = 1;
static int normalize(int i) {
double x;
x=(double)i/pulselen;
return (int)(round(x));
}
int main_gc(void) {
log_shell_disable();
main_loop = 0;
inner_loop = 0;
datetime_gc();
ssdp_gc();
event_operator_gc();
event_action_gc();
options_gc();
socket_gc();
config_gc();
protocol_gc();
whitelist_free();
threads_gc();
wiringXGC();
dso_gc();
log_gc();
gc_clear();
FREE(progname);
xfree();
return EXIT_SUCCESS;
}
void *receive_code(void *param) {
int duration = 0;
int i = 0;
unsigned int y = 0;
int recording = 1;
int bit = 0;
int raw[MAXPULSESTREAMLENGTH] = {0};
int pRaw[MAXPULSESTREAMLENGTH] = {0};
int code[MAXPULSESTREAMLENGTH] = {0};
int binary[MAXPULSESTREAMLENGTH/2] = {0};
int footer = 0;
int pulse = 0;
int rawLength = 0;
int binaryLength = 0;
time_t now = 0, later = 0;
struct hardware_t *hw = (hardware_t *)param;
if(hw->init) {
hw->init();
}
while(main_loop) {
memset(&raw, '\0', MAXPULSESTREAMLENGTH);
memset(&pRaw, '\0', MAXPULSESTREAMLENGTH);
memset(&code, '\0', MAXPULSESTREAMLENGTH);
memset(&binary, '\0', MAXPULSESTREAMLENGTH/2);
recording = 1;
bit = 0;
footer = 0;
pulse = 0;
rawLength = 0;
binaryLength = 0;
inner_loop = 1;
duration = 0;
i = 0;
y = 0;
time(&now);
while(inner_loop && hw->receive) {
duration = hw->receive();
time(&later);
if(difftime(later, now) > 1) {
inner_loop = 0;
}
/* If we are recording, keep recording until the next footer has been matched */
if(recording == 1) {
if(bit < MAXPULSESTREAMLENGTH) {
raw[bit++] = duration;
} else {
bit = 0;
recording = 0;
}
}
/* First try to catch code that seems to be a footer.
If a real footer has been recognized, start using that as the new footer */
if((duration > 5100 && footer == 0) || ((footer-(footer*0.3)<duration) && (footer+(footer*0.3)>duration))) {
recording = 1;
pulselen = (int)duration/PULSE_DIV;
/* Check if we are recording similar codes */
for(i=0;i<(bit-1);i++) {
if(!(((pRaw[i]-(pRaw[i]*0.3)) < raw[i]) && ((pRaw[i]+(pRaw[i]*0.3)) > raw[i]))) {
y=0;
recording=0;
}
pRaw[i]=raw[i];
}
y++;
/* Continue if we have 2 matches */
if(y>=1) {
/* If we are certain we are recording similar codes. Save the raw code length. */
if(footer>0) {
if(rawLength == 0)
rawLength=bit;
}
/* Try to catch the footer, and the low and high values */
for(i=0;i<bit;i++) {
if((i+1)<bit && i > 2 && footer > 0) {
if((raw[i]/pulselen) >= 2) {
pulse=raw[i];
}
}
if(duration > 5100) {
footer=raw[i];
}
}
/* If we have gathered all data, stop with the loop */
if(footer > 0 && pulse > 0 && rawLength > 0) {
inner_loop = 0;
}
}
bit=0;
}
fflush(stdout);
};
/* Convert the raw code into binary code */
for(i=0;i<rawLength;i++) {
if((unsigned int)raw[i] > (pulse-pulselen)) {
code[i]=1;
} else {
code[i]=0;
}
}
for(i=2;i<rawLength; i+=4) {
if(code[i+1] == 1) {
binary[i/4]=1;
} else {
binary[i/4]=0;
}
}
binaryLength = (int)((float)i/4);
if(rawLength > 0 && normalize(pulse) > 0 && rawLength > 25) {
/* Print everything */
printf("--[RESULTS]--\n");
printf("\n");
printf("time:\t\t%s", asctime(localtime(&now)));
printf("hardware:\t%s\n", hw->id);
printf("pulse:\t\t%d\n", normalize(pulse));
printf("rawlen:\t\t%d\n", rawLength);
printf("binlen:\t\t%d\n", binaryLength);
printf("pulselen:\t%d\n", pulselen);
printf("\n");
printf("Raw code:\n");
for(i=0;i<rawLength;i++) {
printf("%d ",normalize(raw[i])*pulselen);
}
printf("\n");
printf("Binary code:\n");
for(i=0;i<binaryLength;i++) {
printf("%d",binary[i]);
}
printf("\n");
}
}
main_loop = 0;
return NULL;
}
int main(int argc, char **argv) {
// memtrack();
gc_attach(main_gc);
/* Catch all exit signals for gc */
gc_catch();
log_shell_enable();
log_file_disable();
log_level_set(LOG_NOTICE);
wiringXLog = logprintf;
struct options_t *options = NULL;
char *args = NULL;
pid_t pid = 0;
char configtmp[] = CONFIG_FILE;
config_set_file(configtmp);
progname = MALLOC(15);
if(!progname) {
logprintf(LOG_ERR, "out of memory");
exit(EXIT_FAILURE);
}
strcpy(progname, "pilight-debug");
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, 'C', "config", OPTION_HAS_VALUE, 0, JSON_NULL, NULL, NULL);
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("Usage: %s [options]\n", progname);
printf("\t -H --help\t\tdisplay usage summary\n");
printf("\t -V --version\t\tdisplay version\n");
printf("\t -C --config\t\tconfig file\n");
goto clear;
break;
case 'V':
printf("%s %s\n", progname, PILIGHT_VERSION);
goto clear;
break;
case 'C':
if(config_set_file(args) == EXIT_FAILURE) {
goto clear;
}
break;
default:
printf("Usage: %s [options]\n", progname);
goto clear;
break;
}
}
options_delete(options);
if((pid = isrunning("pilight-daemon")) != -1) {
logprintf(LOG_ERR, "pilight-daemon instance found (%d)", (int)pid);
goto clear;
}
if((pid = isrunning("pilight-raw")) != -1) {
logprintf(LOG_ERR, "pilight-raw instance found (%d)", (int)pid);
goto clear;
}
protocol_init();
config_init();
if(config_read() != EXIT_SUCCESS) {
goto clear;
}
/* Start threads library that keeps track of all threads used */
threads_start();
struct conf_hardware_t *tmp_confhw = conf_hardware;
while(tmp_confhw) {
if(tmp_confhw->hardware->init) {
if(tmp_confhw->hardware->init() == EXIT_FAILURE) {
logprintf(LOG_ERR, "could not initialize %s hardware mode", tmp_confhw->hardware->id);
goto clear;
}
threads_register(tmp_confhw->hardware->id, &receive_code, (void *)tmp_confhw->hardware, 0);
}
tmp_confhw = tmp_confhw->next;
}
printf("Press and hold one of the buttons on your remote or wait until\n");
printf("another device such as a weather station has send new codes\n");
printf("The debugger will automatically reset itself after one second of\n");
printf("failed leads. It will keep running until you explicitly stop it.\n");
printf("This is done by pressing both the [CTRL] and C buttons on your keyboard.\n");
while(main_loop) {
sleep(1);
}
clear:
if(main_loop == 1) {
main_gc();
}
return (EXIT_FAILURE);
}