-
Notifications
You must be signed in to change notification settings - Fork 1
/
sniff.c
85 lines (73 loc) · 2.06 KB
/
sniff.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
#include <stdio.h>
#include <unistd.h>
#include <wiringPi.h>
static const int pulse_length = 186;
static const int pulse_pad = 74; // 0.4 * 186
// http://wiringpi.com/wp-content/uploads/2013/03/pins.pdf
static const int pin = 2;
static void handle_interrupt();
static int switch_number(int code);
static int group_number(int code);
int main(int argc, char *argv[])
{
wiringPiSetup();
pinMode(pin, INPUT);
wiringPiISR(pin, INT_EDGE_BOTH, &handle_interrupt);
sleep(10);
}
static void handle_interrupt()
{
static unsigned int last_time;
static unsigned int first_duration;
static int code;
static int processing;
unsigned int time = micros();
unsigned int duration = time - last_time;
if (duration > 30 * pulse_length) {
if (code != 0) {
printf("code: %d (raw: 0x%06x)\n", code, code);
printf(" group: %04d (raw: 0x%x)\n", group_number(code), code >> 16);
printf(" switch: %d\n", switch_number(code));
printf(" on/off: %s\n", (code & 0xf) == 0x3 ? "on" : "off");
}
code = 0;
processing = 1;
} else if (processing) {
if ((duration > pulse_length - pulse_pad && duration < pulse_length + pulse_pad) ||
(duration > pulse_length * 3 - pulse_pad && duration < pulse_length * 3 + pulse_pad)) {
if (first_duration == 0) {
first_duration = duration;
} else {
// we have a full bit, so shift what we've collected so far to the left
code <<= 1;
if (first_duration > duration) {
// this is a one, longer pulse was first
code += 1;
}
first_duration = 0;
}
} else {
code = 0;
processing = 0;
first_duration = 0;
}
}
last_time = time;
}
static int switch_number(int code)
{
int tranform = (code >> 4) & 0x3ff;
if ((tranform & 0x3) == 0x3) return 1;
if ((tranform & 0xc) == 0xc) return 2;
if ((tranform & 0x30) == 0x30) return 3;
if ((tranform & 0xc0) == 0xc0) return 4;
if ((tranform & 0x300) == 0x300) return 5;
return -1;
}
static int group_number(int code)
{
int transform = code >> 16;
// obviously this is not the algorithm, but we need more codes
if (transform == 0x14) return 349;
return -1;
}