-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFSniffer.cpp
100 lines (87 loc) · 2.61 KB
/
RFSniffer.cpp
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
/*
RFSniffer
Hacked from http://code.google.com/p/rc-switch/
by @justy to provide a handy RF code sniffer
Productized by cat101
*/
#include "node_modules/rc-switch/RCSwitch.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <ctype.h>
#include <wiringPi.h>
RCSwitch mySwitch;
// extern unsigned int duration;
// extern unsigned int test;
void print_usage() {
printf("Usage: RFSniffer [-p pin]\n");
}
int main(int argc, char *argv[]) {
// This pin is not the first pin on the RPi GPIO header!
// Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/
// for more information.
int PIN = 2;
if(wiringPiSetup() == -1) {
fprintf (stderr,"wiringPiSetup failed, exiting...\n");
exit(1);
}
mySwitch = RCSwitch();
opterr = 0;
int c;
while ((c = getopt (argc, argv, "p:")) != -1){
switch (c){
case 'p':
PIN = atoi(optarg);
break;
case '?':
print_usage();
if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
exit(1);
default:
print_usage();
abort ();
}
}
// mySwitch.setReceiveTolerance(80);
pullUpDnControl (PIN, PUD_OFF); // Disable internal pull downs
mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2
// unsigned long arduinocodeLast;
struct timespec tim, tim2;
tim.tv_sec = 0;
tim.tv_nsec = 100L * 1000L * 1000L; //100ms
while(1) {
if (mySwitch.available()) {
int value = mySwitch.getReceivedValue();
if (value == 0) {
printf("Unknown encoding\n");
} else {
unsigned long code=mySwitch.getReceivedValue();
// unsigned long arduinocode=mySwitch.getReceivedValue() ^ 0xFFFFFF;
// if(arduinocodeLast==arduinocode){
// Debounce packets
printf("{\"code\":\"0x%08lX\", \"bits\":\"%u\", \"protocol\":\"%u\", \"delay\":\"%u\"}\n"
,code
,mySwitch.getReceivedBitlength()
,mySwitch.getReceivedProtocol()
,mySwitch.getReceivedDelay()
);
// printf("Received 0x%08lX (bits %u, delay %u, prot %u)\n", arduinocode
// ,mySwitch.getReceivedBitlength()
// ,mySwitch.getReceivedDelay()
// ,mySwitch.getReceivedProtocol());
sleep(1);
// arduinocodeLast=0;
// }else{
// arduinocodeLast=arduinocode;
// }
}
mySwitch.resetAvailable();
}
nanosleep(&tim , &tim2); //Sleep
}
exit(0);
}