-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsimpleSniff.cpp
More file actions
129 lines (107 loc) · 2.7 KB
/
simpleSniff.cpp
File metadata and controls
129 lines (107 loc) · 2.7 KB
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
#include <wiringPi.h>
#include <iostream>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <sched.h>
#include <sstream>
using namespace std;
//store data into array
//state
int states[67];
//length
int durations[67];
void log(string a){
cout << a << endl;
}
string longToString(long mylong){
string mystring;
stringstream mystream;
mystream << mylong;
return mystream.str();
}
string intToString(int myint){
string mystring;
stringstream mystream;
mystream << myint;
return mystream.str();
}
string measure2String(long duration, int value){
string mystring;
stringstream mystream;
mystream << value;
mystream << " : ";
mystream << duration;
return mystream.str();
}
void handleInterrupt() {
static unsigned int duration;
static unsigned int changeCount;
static unsigned long lastTime;
static int lockPassed = -1;
//1 X10
static int lockType = -1;
long time = micros();
duration = time - lastTime;
//wait for lock
if (duration > 25000 && lockPassed == -1) { //X10 lock started
changeCount = 0;
log("X10 lock started");
log(measure2String(duration, digitalRead(2)));
durations[changeCount++] = duration;
lockType = 1;
} else if (changeCount == 1 && lockType == 1) { //X10 second bit
if(duration > 8000 && duration < 10000) {
log("X10 lock continue");
log(measure2String(duration, digitalRead(2)));
durations[changeCount++] = duration;
} else {
lockType = -1;
lockPassed = -1;
}
} else if (changeCount == 2 && lockType == 1) { //x10 third bit
if(duration > 4000 && duration < 5000) {
lockPassed = 1; //ok passed
log("X10 lock continue 2");
log(measure2String(duration, digitalRead(2)));
durations[changeCount++] = duration;
} else {
lockType = -1;
lockPassed = -1;
}
}
else if (lockPassed != -1) { // REST OF DATA
//Store duration and state
//
//log(measure2String(duration, digitalRead(2)));
if (changeCount >= 70) {
//ended
lockPassed = -1;
changeCount = 0;
log("===============================");
//decode
if (lockType == 1) { // X10
//ignore first 3 changes
for (int i=3; i<=69; i=i+2) {
if (durations[i]*2 < durations[i+1]) {
cout << "1";
} else {
cout << "0";
}
}
cout << endl;
}
} else {
durations[changeCount++] = duration;
}
}
lastTime = time;
}
int main(void) {
if(wiringPiSetup() == -1)
return 0;
//attach interrupt to changes on the pin
wiringPiISR(2, INT_EDGE_BOTH, &handleInterrupt);
while(true);
}