|
1 | 1 | /* |
2 | | - RFSniffer |
| 2 | + RFSniffer |
3 | 3 |
|
4 | | - Usage: ./RFSniffer [<pulseLength>] |
5 | | - [] = optional |
| 4 | +Usage: ./RFSniffer [<pulseLength>] |
| 5 | +[] = optional |
6 | 6 |
|
7 | | - Hacked from http://code.google.com/p/rc-switch/ |
8 | | - by @justy to provide a handy RF code sniffer |
9 | | -*/ |
| 7 | +Hacked from http://code.google.com/p/rc-switch/ |
| 8 | +by @justy to provide a handy RF code sniffer |
| 9 | +Adapted by korfhage(dot)michel(at)web(dot)de |
| 10 | + */ |
| 11 | + |
| 12 | +#include <cstdlib> |
| 13 | +#include <cerrno> |
| 14 | + |
| 15 | +#include <string> |
| 16 | +#include <iostream> |
10 | 17 |
|
11 | | -#include "../rc-switch/RCSwitch.h" |
12 | | -#include <stdlib.h> |
13 | | -#include <stdio.h> |
14 | 18 | #include <unistd.h> |
15 | | - |
16 | | - |
17 | | -RCSwitch mySwitch; |
18 | | - |
19 | 19 |
|
| 20 | +#include "../rc-switch/RCSwitch.h" |
20 | 21 |
|
21 | | -int main(int argc, char *argv[]) { |
22 | | - |
23 | | - // This pin is not the first pin on the RPi GPIO header! |
24 | | - // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/ |
25 | | - // for more information. |
26 | | - int PIN = 2; |
27 | | - |
28 | | - if(wiringPiSetup() == -1) { |
29 | | - printf("wiringPiSetup failed, exiting..."); |
30 | | - return 0; |
31 | | - } |
| 22 | +using std::cout; |
| 23 | +using std::endl; |
32 | 24 |
|
33 | | - int pulseLength = 0; |
34 | | - if (argv[1] != NULL) pulseLength = atoi(argv[1]); |
| 25 | +// This pin is not the first pin on the RPi GPIO header! |
| 26 | +// Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/ |
| 27 | +// for more information. |
| 28 | +constexpr int PIN = 2; |
| 29 | + |
| 30 | +template<class dest> |
| 31 | +dest cast(char* param, unsigned int& errorCode) |
| 32 | +{ |
| 33 | + char* end; |
| 34 | + long l = std::strtol(param, &end, 10); |
| 35 | + dest ret(l); |
| 36 | + if(errno || ret != l) errorCode = 1; |
| 37 | + return ret; |
| 38 | +} |
| 39 | + |
| 40 | +int main(int argc, char *argv[]) { |
| 41 | + if(wiringPiSetup() == -1) { |
| 42 | + cout << "wiringPiSetup failed, exiting..." << endl; |
| 43 | + return 101; |
| 44 | + } |
35 | 45 |
|
36 | | - mySwitch = RCSwitch(); |
37 | | - if (pulseLength != 0) mySwitch.setPulseLength(pulseLength); |
38 | | - mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2 |
39 | | - |
40 | | - |
41 | | - while(1) { |
42 | | - |
43 | | - if (mySwitch.available()) { |
44 | | - |
45 | | - int value = mySwitch.getReceivedValue(); |
46 | | - |
47 | | - if (value == 0) { |
48 | | - printf("Unknown encoding\n"); |
49 | | - } else { |
50 | | - |
51 | | - printf("Received %i\n", mySwitch.getReceivedValue() ); |
52 | | - } |
53 | | - |
54 | | - fflush(stdout); |
55 | | - mySwitch.resetAvailable(); |
56 | | - } |
57 | | - usleep(100); |
58 | | - |
59 | | - } |
| 46 | + unsigned int error = 0; |
| 47 | + // first argument will parsed as the pulselength |
| 48 | + int pulseLength = cast<int>(argv[1], error); |
| 49 | + if(error) |
| 50 | + { |
| 51 | + cout << "invalid first argument! needs to be signed number." << endl; |
| 52 | + return 102; |
| 53 | + } |
60 | 54 |
|
61 | | - exit(0); |
| 55 | + RCSwitch mySwitch; |
| 56 | + if (pulseLength) mySwitch.setPulseLength(pulseLength); |
| 57 | + mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2 |
62 | 58 |
|
| 59 | + while(1) { |
| 60 | + if (mySwitch.available()) { |
| 61 | + auto value = mySwitch.getReceivedValue(); |
| 62 | + if (!value) { |
| 63 | + cout << "Unknown encoding"; |
| 64 | + } else { |
| 65 | + cout << "Received " << value; |
| 66 | + } |
| 67 | + cout << endl; |
| 68 | + mySwitch.resetAvailable(); |
| 69 | + } |
| 70 | + usleep(100); |
63 | 71 |
|
| 72 | + } |
| 73 | + return 0; |
64 | 74 | } |
65 | 75 |
|
0 commit comments