Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved validation of RFSniffer pulse_length parameter + cpp-style #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 69 additions & 53 deletions RPi_utils/RFSniffer.cpp
Original file line number Diff line number Diff line change
@@ -1,65 +1,81 @@
/*
RFSniffer
RFSniffer

Usage: ./RFSniffer [<pulseLength>]
[] = optional
Usage: ./RFSniffer [<pulseLength>]
[] = 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 <cstdlib>
#include <cerrno>

#include <string>
#include <iostream>

#include "../rc-switch/RCSwitch.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>


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<class dest>
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<int>(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;
}