From 682766edd5358808be526be19d07bb08c98b0eda Mon Sep 17 00:00:00 2001 From: Michel Korfhage Date: Sat, 14 Mar 2020 12:24:46 +0100 Subject: [PATCH] improved validation of RFSniffer pulse_length parameter + cpp-style --- RPi_utils/RFSniffer.cpp | 122 +++++++++++++++++++++++----------------- 1 file changed, 69 insertions(+), 53 deletions(-) diff --git a/RPi_utils/RFSniffer.cpp b/RPi_utils/RFSniffer.cpp index 286ab3d..d3f3db5 100644 --- a/RPi_utils/RFSniffer.cpp +++ b/RPi_utils/RFSniffer.cpp @@ -1,65 +1,81 @@ /* - RFSniffer + RFSniffer - Usage: ./RFSniffer [] - [] = optional +Usage: ./RFSniffer [] +[] = optional - Hacked from http://code.google.com/p/rc-switch/ - by @justy to provide a handy RF code sniffer -*/ +Hacked from http://code.google.com/p/rc-switch/ +by @justy to provide a handy RF code sniffer +Adapted by korfhage(dot)michel(at)web(dot)de + */ + +#include +#include + +#include +#include -#include "../rc-switch/RCSwitch.h" -#include -#include #include - - -RCSwitch mySwitch; - +#include "../rc-switch/RCSwitch.h" + +using std::cout; +using std::endl; + +// This pin is not the first pin on the RPi GPIO header! +// Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/ +// for more information. +constexpr int PIN = 2; + +template +dest cast(char* param, unsigned int& errorCode) +{ + char* end; + errno = 0; + long l = std::strtol(param, &end, 10); + cout << "set pulse length to " << l << endl; + dest ret(l); + if(errno || ret != l) errorCode = errno; + return ret; +} 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) { - printf("wiringPiSetup failed, exiting..."); - return 0; - } - - int pulseLength = 0; - if (argv[1] != NULL) pulseLength = atoi(argv[1]); - - mySwitch = RCSwitch(); - if (pulseLength != 0) mySwitch.setPulseLength(pulseLength); - mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2 - - - while(1) { - - if (mySwitch.available()) { - - int value = mySwitch.getReceivedValue(); - - if (value == 0) { - printf("Unknown encoding\n"); - } else { - - printf("Received %i\n", mySwitch.getReceivedValue() ); - } - - fflush(stdout); - mySwitch.resetAvailable(); - } - usleep(100); - - } - - exit(0); + if(wiringPiSetup() == -1) { + cout << "wiringPiSetup failed, exiting..." << endl; + return 101; + } + + RCSwitch mySwitch; + + if(argv[1]) + { + unsigned int error = 0; + // first argument will parsed as the pulselength + int pulseLength = cast(argv[1], error); + if(error) + { + cout << "invalid first argument! needs to be signed number." << endl; + return 102; + } + if (pulseLength) mySwitch.setPulseLength(pulseLength); + } + + mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2 + while(1) { + if (mySwitch.available()) { + auto value = mySwitch.getReceivedValue(); + if (!value) { + cout << "Unknown encoding"; + } else { + cout << "Received " << value; + } + cout << endl; + mySwitch.resetAvailable(); + } + usleep(100); + } + return 0; }