Skip to content

Commit ba3a735

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

File tree

1 file changed

+63
-51
lines changed

1 file changed

+63
-51
lines changed

RPi_utils/RFSniffer.cpp

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,77 @@
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"
2021

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;
3224

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+
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+
}
41+
42+
int main(int argc, char *argv[]) {
43+
if(wiringPiSetup() == -1) {
44+
cout << "wiringPiSetup failed, exiting..." << endl;
45+
return 101;
46+
}
3547

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-
}
48+
unsigned int error = 0;
49+
// first argument will parsed as the pulselength
50+
int pulseLength = cast<int>(argv[1], error);
51+
if(error)
52+
{
53+
cout << "invalid first argument! needs to be signed number." << endl;
54+
return 102;
55+
}
6056

61-
exit(0);
57+
RCSwitch mySwitch;
58+
if (pulseLength) mySwitch.setPulseLength(pulseLength);
59+
mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2
6260

61+
while(1) {
62+
if (mySwitch.available()) {
63+
auto value = mySwitch.getReceivedValue();
64+
if (!value) {
65+
cout << "Unknown encoding";
66+
} else {
67+
cout << "Received " << value;
68+
}
69+
cout << endl;
70+
mySwitch.resetAvailable();
71+
}
72+
usleep(100);
6373

74+
}
75+
return 0;
6476
}
6577

0 commit comments

Comments
 (0)