Skip to content

Commit 8bc189c

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

File tree

1 file changed

+61
-51
lines changed

1 file changed

+61
-51
lines changed

RPi_utils/RFSniffer.cpp

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,75 @@
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+
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+
}
3545

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+
}
6054

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

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);
6371

72+
}
73+
return 0;
6474
}
6575

0 commit comments

Comments
 (0)