Skip to content

Commit 682766e

Browse files
author
Michel Korfhage
committed
improved validation of RFSniffer pulse_length parameter + cpp-style
1 parent 31c0ea4 commit 682766e

File tree

1 file changed

+69
-53
lines changed

1 file changed

+69
-53
lines changed

RPi_utils/RFSniffer.cpp

Lines changed: 69 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,81 @@
11
/*
2-
RFSniffer
2+
RFSniffer
33
4-
Usage: ./RFSniffer [<pulseLength>]
5-
[] = optional
4+
Usage: ./RFSniffer [<pulseLength>]
5+
[] = optional
66
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>
1017

11-
#include "../rc-switch/RCSwitch.h"
12-
#include <stdlib.h>
13-
#include <stdio.h>
1418
#include <unistd.h>
15-
16-
17-
RCSwitch mySwitch;
18-
1919

20+
#include "../rc-switch/RCSwitch.h"
21+
22+
using std::cout;
23+
using std::endl;
24+
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+
errno = 0;
35+
long l = std::strtol(param, &end, 10);
36+
cout << "set pulse length to " << l << endl;
37+
dest ret(l);
38+
if(errno || ret != l) errorCode = errno;
39+
return ret;
40+
}
2041

2142
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-
}
32-
33-
int pulseLength = 0;
34-
if (argv[1] != NULL) pulseLength = atoi(argv[1]);
35-
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-
}
60-
61-
exit(0);
43+
if(wiringPiSetup() == -1) {
44+
cout << "wiringPiSetup failed, exiting..." << endl;
45+
return 101;
46+
}
47+
48+
RCSwitch mySwitch;
49+
50+
if(argv[1])
51+
{
52+
unsigned int error = 0;
53+
// first argument will parsed as the pulselength
54+
int pulseLength = cast<int>(argv[1], error);
55+
if(error)
56+
{
57+
cout << "invalid first argument! needs to be signed number." << endl;
58+
return 102;
59+
}
60+
if (pulseLength) mySwitch.setPulseLength(pulseLength);
61+
}
62+
63+
mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2
6264

65+
while(1) {
66+
if (mySwitch.available()) {
67+
auto value = mySwitch.getReceivedValue();
68+
if (!value) {
69+
cout << "Unknown encoding";
70+
} else {
71+
cout << "Received " << value;
72+
}
73+
cout << endl;
74+
mySwitch.resetAvailable();
75+
}
76+
usleep(100);
6377

78+
}
79+
return 0;
6480
}
6581

0 commit comments

Comments
 (0)