More scanner changes

This commit is contained in:
ezratl 2019-10-05 17:09:54 -04:00
parent 621f2810ab
commit 2132de6b77
13 changed files with 155 additions and 64 deletions

View File

@ -6,6 +6,7 @@
*/
#include <unistd.h>
#include <mutex>
#include "ScannerSM.h"
#include "ListGenerator.h"
@ -17,7 +18,7 @@
using namespace piscan;
ScannerSM::ScannerSM(MessageReceiver& central, SystemList& dataSource) :
StateMachine(7), _centralQueue(central), _systems(dataSource) {
StateMachine(7), _centralQueue(central), _systems(dataSource), _externalHold(false), _manualMode(false) {
}
void ScannerSM::startScan(){
@ -34,8 +35,12 @@ void ScannerSM::startScan(){
END_TRANSITION_MAP(NULL)
}
void ScannerSM::holdScan(){
void ScannerSM::holdScan(std::vector<int> index){
_externalHold = true;
{
std::lock_guard<std::mutex> lock(_holdMutex);
_holdIndex = index;
}
LOG_F(1, "ExtEvent: holdScan");
BEGIN_TRANSITION_MAP
TRANSITION_MAP_ENTRY(EVENT_IGNORED)
@ -96,8 +101,10 @@ void ScannerSM::ST_Load(EventData* data){
void ScannerSM::ST_Scan(EventData* data){
DLOG_F(9, "ST_Scan");
if(currentState != lastState)
if(currentState != lastState){
_squelchHits = 0;
DLOG_F(6, "State change: %i -> %i", lastState, currentState);
}
_enableAudioOut(false);
_currentContext.state = ScannerContext::SCAN;
@ -106,40 +113,22 @@ void ScannerSM::ST_Scan(EventData* data){
if(currentState != lastState)
_broadcastContextUpdate();
// incremental scan pattern
if(!_squelchHits || (currentState != lastState)){
//_entryCounter = (_entryCounter + 1) % _currentSystem->size();
if (!_squelchHits || (currentState != lastState)) {
// if(_entryCounter == 0){
// _sysCounter = (_sysCounter + 1) % _systems.size();
//
// _currentSystem = _systems[_sysCounter];
// assert(_currentSystem != nullptr);
//
// //_broadcastContextUpdate();
// }
//CHECK_F(_currentSystem->size() > 0);
//_currentEntry = _currentSystem->operator[](_entryCounter);
//CHECK_F(_currentEntry != NULL);
_currentEntry = _systems.getNextEntry();
_currentEntry = _systems.getNextEntry();
}
if(_currentEntry->hasSignal()){
if (_currentEntry->hasSignal()) {
_squelchHits++;
if(_squelchHits >= SQUELCH_TRIGGER_HITS){
if (_squelchHits >= SQUELCH_TRIGGER_HITS) {
LOG_F(2, "Signal found: %s", _currentEntry->tag().c_str());
InternalEvent(ST_RECEIVE);
}
else{
} else {
InternalEvent(ST_SCAN);
}
}
else{
} else {
_squelchHits = 0;
InternalEvent(ST_SCAN);
}
@ -151,9 +140,27 @@ void ScannerSM::ST_Hold(EventData* data){
if(currentState != lastState)
DLOG_F(6, "State change: %i -> %i", lastState, currentState);
bool indexHold = false;
{
std::lock_guard < std::mutex > lock(_holdMutex);
if (_externalHold && _holdIndex.size() > 0) {
_currentEntry = _systems.getEntryByIndex(_holdIndex);
LOG_F(1, "Index hold");
_holdIndex.clear();
indexHold = true;
}
}
/* don't hold on dummy channels */
while(_currentEntry->isDummy()){
_currentEntry->hasSignal();
_currentEntry = _systems.getNextEntry();
}
_enableAudioOut(false);
_currentContext.state = ScannerContext::HOLD;
if(currentState != lastState)
if(currentState != lastState || indexHold)
_broadcastContextUpdate();
/* start receive if signal active */
@ -249,7 +256,7 @@ void ScannerSM::ST_Stopped(EventData* data){
}
void ScannerSM::_broadcastContextUpdate() {
DLOG_F(7, "Broadcasting context");
DLOG_F(6, "Broadcasting context");
std::lock_guard<std::mutex> lock(_contextMutex);
if (_currentContext.state != ScannerContext::SCAN)
{
@ -338,7 +345,13 @@ void ScannerSM::_handleRequest(ClientRequest& request) {
startScan();
break;
case SCANNER_STATE_HOLD:
holdScan();
if(request.pData != nullptr){
std::vector<int>* indexData = reinterpret_cast<std::vector<int>*>(request.pData);
holdScan(*indexData);
delete indexData;
}
else
holdScan();
break;
case SCANNER_STATE_MANUAL:
manualEntry(reinterpret_cast<uint32_t*>(rq->pData));

View File

@ -16,7 +16,7 @@
#include "messages.h"
#include "clientmessage.h"
#define SQUELCH_TRIGGER_HITS 10
#define SQUELCH_TRIGGER_HITS 25
namespace piscan {
@ -26,7 +26,7 @@ public:
~ScannerSM() {};
void startScan();
void holdScan();
void holdScan(std::vector<int> index = std::vector<int>());
void stopScanner();
void manualEntry(uint32_t* freq);
void giveMessage(std::shared_ptr<Message> message);
@ -72,8 +72,10 @@ private:
ScannerContext _currentContext;
std::mutex _contextMutex;
bool _externalHold = false;
bool _manualMode = false;
std::atomic_bool _externalHold;
std::atomic_bool _manualMode;
std::mutex _holdMutex;
std::vector<int> _holdIndex;
std::time_t timeoutStart = 0;
int _squelchHits = 0;

View File

@ -95,7 +95,9 @@ void StateMachine::StateEngine(void)
//lock.unlock();
}
// TBD - unlock semaphore here
// yield to let waiting threads generate external events
lock.unlock();
std::this_thread::yield();
}
void StateMachine::StateThreadFunc(void){

View File

@ -15,7 +15,7 @@ using namespace piscan;
DemodInterface* Entry::demod = nullptr;
bool DummyChannel::hasSignal(){
if(!demod->setFrequency(this->frequency))
if(!demod->setTunerFrequency(this->frequency))
return false;
return false;

View File

@ -25,7 +25,8 @@ public:
bool useDelay() { return _scanDelay; }
void lockout(bool val = true) { _lockedOut = val; }
virtual bool hasSignal() = 0;
virtual long freq() = 0;
virtual long long freq() = 0;
virtual bool isDummy() { return false; }
size_t getSysIndex() { return _sysIndex; };
void setSysIndex(size_t index) { _sysIndex = index; };
@ -47,27 +48,29 @@ class Channel: public Entry {
public:
Channel(long freq, std::string tag, bool lo, bool del) : Entry(tag, lo, del), frequency(freq){}
virtual ~Channel() {};
virtual long freq() { return frequency; };
virtual long long freq() { return frequency; };
protected:
const long frequency;
const long long frequency;
};
class DummyChannel: public Channel {
public:
DummyChannel(long freq) : Channel(freq, "", false, false){
DummyChannel(long long freq) : Channel(freq, "", false, false){
}
~DummyChannel(){};
std::string modulation() { return ""; };
bool isDummy() { return true; }
bool hasSignal();
};
class FMChannel : public Channel {
public:
FMChannel(long freq, std::string tag, bool lo, bool del) : Channel(freq, tag, lo, del){}
FMChannel(long long freq, std::string tag, bool lo, bool del) : Channel(freq, tag, lo, del){}
~FMChannel() {};
std::string modulation() {
@ -79,7 +82,7 @@ public:
class PLChannel: public FMChannel {
public:
PLChannel(long freq, float tn, std::string tag, bool lo, bool del) :
PLChannel(long long freq, float tn, std::string tag, bool lo, bool del) :
FMChannel(freq, tag, lo, del), tone(tn) {
}
~PLChannel() {};
@ -91,7 +94,7 @@ protected:
class DCChannel : public FMChannel {
public:
DCChannel(long freq, unsigned int tn, std::string tag, bool lo, bool del) :
DCChannel(long long freq, unsigned int tn, std::string tag, bool lo, bool del) :
FMChannel(freq, tag, lo, del), code(tn) {
}
~DCChannel() {};
@ -103,7 +106,7 @@ protected:
class AMChannel : public Channel {
public:
AMChannel(long freq, std::string tag, bool lo, bool del) : Channel(freq, tag, lo, del){}
AMChannel(long long freq, std::string tag, bool lo, bool del) : Channel(freq, tag, lo, del){}
~AMChannel() {};
bool hasSignal() { return false; };

View File

@ -18,14 +18,21 @@ SystemList::~SystemList() {
// TODO Auto-generated destructor stub
}
std::shared_ptr<Entry> SystemList::getEntryByIndex(std::vector<int> index){
if(index.size() < 2 || index[0] < 0 || index[1] < 0 || index[0] >= _systems.size() || index[1] >= _systems[index[0]]->size())
return getNextEntry();
return _systems[index[0]]->operator [](index[1]);
}
std::shared_ptr<Entry> SystemList::getNextEntry(){
if(_entryNum == 0 && _retune){
_retune = false;
return std::make_shared<DummyChannel>(_bins[_binNum]->getCenterFreq());
//if(_bins[_binNum]->size() > 1)
return std::make_shared<DummyChannel>(_bins[_binNum]->getCenterFreq());
}
auto entry = _bins[_binNum]->operator [](_entryNum);
auto entry = _bins[_binNum]->at(_entryNum);
_entryNum = (_entryNum + 1) % _bins[_binNum]->size();
@ -39,6 +46,8 @@ std::shared_ptr<Entry> SystemList::getNextEntry(){
}
void SystemList::sortBins(int bandwidth){
LOG_F(1, "Sorting bandwidth chunks...");
size_t numEntries = 0;
for(size_t i = 0; i < _systems.size(); i++)
for(size_t k = 0; k < _systems[i]->size(); k++)
@ -70,6 +79,16 @@ void SystemList::sortBins(int bandwidth){
lastFreq = entries[i]->freq();
newBin->push_back(entries[i]);
}
std::string binPrint = "";
for(size_t i = 0; i < _bins.size(); i++){
binPrint += (std::string("\tCenter: ") + std::to_string(_bins[i]->getCenterFreq()) + std::string(" | "));
for(size_t j = 0; j < _bins[i]->size(); j++)
binPrint += (std::to_string(_bins[i]->at(j)->freq()) + std::string(" "));
RAW_LOG_F(1, binPrint.c_str());
binPrint = "";
}
}
void SystemList::merge(std::shared_ptr<Entry> arr[], int l, int m, int r)

View File

@ -27,6 +27,8 @@ public:
_systems.push_back(system);
}
std::shared_ptr<Entry> getEntryByIndex(std::vector<int> index);
std::shared_ptr<Entry> getNextEntry();
void sortBins(int bandwidth);

View File

@ -52,7 +52,7 @@ int Connection::systemFunction(SystemFunction function) {
return issueRequest(params);
}
int Connection::scannerFunction(ScannerFunction function, uint32_t freq) {
/*int Connection::scannerFunction(ScannerFunction function, uint32_t freq) {
ClientRequest::RequestParams params = { .type = SCANNER_FUNCTION };
switch (function) {
case SCAN:
@ -69,6 +69,26 @@ int Connection::scannerFunction(ScannerFunction function, uint32_t freq) {
return -1;
}
return issueRequest(params);
}*/
int Connection::scanStart() {
ClientRequest::RequestParams params = { .type = SCANNER_FUNCTION, .subType = SCANNER_STATE_SCAN };
return issueRequest(params);
}
int Connection::scanHold() {
ClientRequest::RequestParams params = { .type = SCANNER_FUNCTION, .subType = SCANNER_STATE_HOLD };
return issueRequest(params);
}
int Connection::scanHoldEntry(std::vector<int> index) {
ClientRequest::RequestParams params = { .type = SCANNER_FUNCTION, .subType = SCANNER_STATE_HOLD };
return issueRequest(params, new std::vector<int>(index));
}
int Connection::scanManualEntry(long freq, Modulation mode) {
ClientRequest::RequestParams params = { .type = SCANNER_FUNCTION, .subType = SCANNER_STATE_MANUAL };
return issueRequest(params, new uint32_t(freq));
}
int Connection::setDemodSquelch(int level) {

View File

@ -72,9 +72,16 @@ void DebugConsole::_consoleInputFunc() {
else
getDemodContext();
} else if (!tokens[0].compare("scan"))
scannerFunction(ScannerFunction::SCAN);
scanStart();
else if (!tokens[0].compare("hold")) {
scannerFunction(ScannerFunction::HOLD);
if (tokens.size() > 2) {
std::vector<int> entryIndex;
for(size_t i = 1; i < tokens.size(); i++)
entryIndex.push_back(std::stoi(tokens[i]));
scanHoldEntry(entryIndex);
}
else
scanHold();
} else if (!tokens[0].compare("gain")) {
if (tokens.size() > 1) {
int gain = 0;
@ -87,7 +94,7 @@ void DebugConsole::_consoleInputFunc() {
else
getDemodContext();
} else if (!tokens[0].compare("manual")) {
scannerFunction(ScannerFunction::MANUAL, std::stof(tokens[1]));
scanManualEntry(std::stof(tokens[1]));
}
else if (!tokens[0].compare("get")){
if(!tokens[1].compare("context"))

View File

@ -180,26 +180,29 @@ void SocketConnection::_handleGeneralRequest(const piscan_pb::GeneralRequest& rq
void SocketConnection::_handleScanStateRequest(
const piscan_pb::ScannerStateRequest& rq) {
ScannerFunction func;
//ScannerFunction func;
uint32_t freq = 0;
switch(rq.state()){
case piscan_pb::ScannerStateRequest_NewState_SCAN:
func = SCAN;
//func = SCAN;
scanStart();
break;
case piscan_pb::ScannerStateRequest_NewState_HOLD:
func = HOLD;
//func = HOLD;
scanHold();
break;
case piscan_pb::ScannerStateRequest_NewState_MANUAL:
func = MANUAL;
freq = static_cast<uint32_t>(rq.manfreq());
//func = MANUAL;
//freq = static_cast<uint32_t>(rq.manfreq());
scanManualEntry(rq.manfreq());
break;
default:
LOG_F(WARNING, "Invalid ScannerStateRequest from %s", _socket.local_endpoint().address().to_string().c_str());
break;
}
scannerFunction(func, freq);
//scannerFunction(func, freq);
}
void SocketConnection::_handleDemodRequest(const piscan_pb::DemodRequest& rq) {

View File

@ -10,6 +10,7 @@
#include <memory>
#include <boost/shared_ptr.hpp>
#include <vector>
#include "constants.h"
#include "clientmessage.h"
@ -97,7 +98,17 @@ protected:
HOLD,
MANUAL,
};
int scannerFunction(ScannerFunction function, uint32_t freq = 0);
enum Modulation {
FM,
AM,
};
//int scannerFunction(ScannerFunction function, uint32_t freq = 0);
int scanStart();
int scanHold();
int scanHoldEntry(std::vector<int> index);
int scanManualEntry(long freq, Modulation mode = FM);
int setDemodSquelch(int level);
int setDemodGain(int level);
int getScannerContext();

View File

@ -152,7 +152,7 @@ void Demodulator::stop(){
LOG_F(1, "Demodulator stopped");
}
bool Demodulator::setFrequency(uint32_t freq) {
bool Demodulator::setFrequency(long long freq) {
/*if(freq == _demodMgr.getCurrentModem()->getFrequency()){
DLOG_F(9, "Frequency already set");
return true;
@ -169,7 +169,7 @@ bool Demodulator::setFrequency(uint32_t freq) {
_demodMgr.getCurrentModem()->setFrequency(freq);
//this is totally arbitrary
usleep(1000);
usleep(7000);
_currentFreq = freq;
@ -178,6 +178,13 @@ bool Demodulator::setFrequency(uint32_t freq) {
return true;
}
bool Demodulator::setTunerFrequency(long long freq){
_cubic->setFrequency(freq);
_demodMgr.getCurrentModem()->setFrequency(freq);
usleep(200000);
return true;
}
float Demodulator::getSignalLevel() {
return _demodMgr.getActiveContextModem()->getSignalLevel();
}

View File

@ -15,7 +15,7 @@
#include "DemodulatorMgr.h"
#include "SDRDeviceInfo.h"
#define DEFAULT_SQUELCH -60
#define DEFAULT_SQUELCH 0
namespace piscan {
@ -29,7 +29,8 @@ class DemodInterface {
public:
virtual ~DemodInterface() {};
virtual bool setFrequency(uint32_t freq) = 0;
virtual bool setFrequency(long long freq) = 0;
virtual bool setTunerFrequency(long long freq) = 0;
virtual float getSignalLevel() = 0;
virtual float getDecodedPL() = 0;
virtual unsigned int getDecodedDC() = 0;
@ -53,7 +54,7 @@ private:
MessageReceiver& _centralQueue;
Modulation _currentModem = NFM;
float _squelchLevel = DEFAULT_SQUELCH;
uint32_t _currentFreq = 0;
long long _currentFreq = 0;
float _gain = AUTO_GAIN;
std::shared_ptr<CubicSDR> _cubic;
@ -62,7 +63,8 @@ private:
std::map<Modulation, DemodulatorInstancePtr> _demods;
void giveMessage(std::shared_ptr<Message> message);
bool setFrequency(uint32_t freq);
bool setFrequency(long long freq);
bool setTunerFrequency(long long freq);
float getSignalLevel();
float getDecodedPL();
unsigned int getDecodedDC();