From 8ad494f9772fef0a1183026175309a64c328f7be Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Wed, 18 Oct 2017 22:31:39 +0200 Subject: [PATCH] Added console application, Fixes #5 --- source/CMakeLists.txt | 10 ++++++++++ source/sniff.cpp | 22 ++++++++++++++++++++++ source/sniffer.cpp | 22 ++++++++++++++++++++++ source/sniffer.h | 15 +++++++++++++++ source/sniffthread.cpp | 31 +++++++------------------------ 5 files changed, 76 insertions(+), 24 deletions(-) create mode 100644 source/sniff.cpp create mode 100644 source/sniffer.cpp create mode 100644 source/sniffer.h diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index baae8a1..dcabd65 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -20,7 +20,17 @@ add_executable(qSniff mainwindow.ui sniffthread.cpp sniffthread.h + sniffer.cpp + sniffer.h ) target_link_libraries(qSniff rc-switch wiringPi Qt5::Widgets) +add_executable(Sniff + sniff.cpp + sniffer.cpp + sniffer.h +) + +target_link_libraries(Sniff rc-switch wiringPi) + diff --git a/source/sniff.cpp b/source/sniff.cpp new file mode 100644 index 0000000..996c040 --- /dev/null +++ b/source/sniff.cpp @@ -0,0 +1,22 @@ +#include "sniffer.h" +#include + +int main(int argc, char **argv) +{ + std::cout << "Sniff" << std::endl; + + Sniffer sniffer(6); + + while (true) + { + unsigned int data = sniffer.receive(); + if (data != 0) + { + std::cout << "Data: " << data << std::endl; + } + + delay(100); + } + + return 0; +} diff --git a/source/sniffer.cpp b/source/sniffer.cpp new file mode 100644 index 0000000..8c6899d --- /dev/null +++ b/source/sniffer.cpp @@ -0,0 +1,22 @@ +#include "sniffer.h" + +Sniffer::Sniffer(unsigned int pin) +{ + wiringPiSetup(); + + m_switch.enableReceive(pin); +} + +unsigned int Sniffer::receive() +{ + + if (m_switch.available()) + { + unsigned int receivedValue = m_switch.getReceivedValue(); + m_switch.resetAvailable(); + return receivedValue; + } + + return 0; +} + diff --git a/source/sniffer.h b/source/sniffer.h new file mode 100644 index 0000000..8b2747a --- /dev/null +++ b/source/sniffer.h @@ -0,0 +1,15 @@ +#pragma once + +#include "rc-switch/RCSwitch.h" + +class Sniffer +{ + private: + RCSwitch m_switch; + + public: + Sniffer() = delete; + Sniffer(unsigned int pin); + + unsigned int receive(); +}; diff --git a/source/sniffthread.cpp b/source/sniffthread.cpp index c9e48f7..e792481 100644 --- a/source/sniffthread.cpp +++ b/source/sniffthread.cpp @@ -1,37 +1,20 @@ #include "sniffthread.h" -#include -#include "rc-switch/RCSwitch.h" +#include "sniffer.h" #include void SniffThread::run() { - wiringPiSetup(); - - RCSwitch mySwitch = RCSwitch(); - - mySwitch.enableReceive(6); + Sniffer sniffer(6); while (true) { - if (mySwitch.available()) + unsigned int data = sniffer.receive(); + if (data != 0) { - int value = mySwitch.getReceivedValue(); + std::stringstream dataStream; + dataStream << data; - if (value == 0) - { - emit dataReceived("Unknown encoding"); - } - else - { - std::stringstream result; - result << "Received " << value << " / "; - result << mySwitch.getReceivedBitlength() << " bit "; - result << "Protocol: " << mySwitch.getReceivedProtocol(); - - emit dataReceived(result.str().c_str()); - } - - mySwitch.resetAvailable(); + emit dataReceived(dataStream.str().c_str()); } delay(100);