Skip to content

Commit

Permalink
Load the DMR Id lookup table periodically.
Browse files Browse the repository at this point in the history
  • Loading branch information
g4klx committed Oct 10, 2016
1 parent 878b4d6 commit 40c086b
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 47 deletions.
43 changes: 33 additions & 10 deletions Conf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum SECTION {
SECTION_INFO,
SECTION_LOG,
SECTION_CWID,
SECTION_DMRID_LOOKUP,
SECTION_MODEM,
SECTION_DSTAR,
SECTION_DMR,
Expand Down Expand Up @@ -73,6 +74,8 @@ m_logFilePath(),
m_logFileRoot(),
m_cwIdEnabled(false),
m_cwIdTime(10U),
m_dmrIdLookupFile(),
m_dmrIdLookupTime(0U),
m_modemPort(),
m_modemRXInvert(false),
m_modemTXInvert(false),
Expand Down Expand Up @@ -112,7 +115,6 @@ m_dmrDstIdBlacklistSlot1NET(),
m_dmrDstIdBlacklistSlot2NET(),
m_dmrDstIdWhitelistSlot1NET(),
m_dmrDstIdWhitelistSlot2NET(),
m_dmrLookupFile(),
m_dmrCallHang(3U),
m_dmrTXHang(4U),
m_fusionEnabled(false),
Expand Down Expand Up @@ -196,6 +198,8 @@ bool CConf::read()
section = SECTION_LOG;
else if (::strncmp(buffer, "[CW Id]", 7U) == 0)
section = SECTION_CWID;
else if (::strncmp(buffer, "[DMR Id Lookup]", 15U) == 0)
section = SECTION_DMRID_LOOKUP;
else if (::strncmp(buffer, "[Modem]", 7U) == 0)
section = SECTION_MODEM;
else if (::strncmp(buffer, "[D-Star]", 8U) == 0)
Expand Down Expand Up @@ -287,6 +291,11 @@ bool CConf::read()
m_cwIdEnabled = ::atoi(value) == 1;
else if (::strcmp(key, "Time") == 0)
m_cwIdTime = (unsigned int)::atoi(value);
} else if (section == SECTION_DMRID_LOOKUP) {
if (::strcmp(key, "File") == 0)
m_dmrIdLookupFile = value;
else if (::strcmp(key, "Time") == 0)
m_dmrIdLookupTime = (unsigned int)::atoi(value);
} else if (section == SECTION_MODEM) {
if (::strcmp(key, "Port") == 0)
m_modemPort = value;
Expand Down Expand Up @@ -436,9 +445,7 @@ bool CConf::read()
m_dmrDstIdWhitelistSlot2NET.push_back(id);
p = ::strtok(NULL, ",\r\n");
}
} else if (::strcmp(key, "LookupFile") == 0)
m_dmrLookupFile = value;
else if (::strcmp(key, "TXHang") == 0)
} else if (::strcmp(key, "TXHang") == 0)
m_dmrTXHang = (unsigned int)::atoi(value);
else if (::strcmp(key, "CallHang") == 0)
m_dmrCallHang = (unsigned int)::atoi(value);
Expand Down Expand Up @@ -686,6 +693,16 @@ unsigned int CConf::getCWIdTime() const
return m_cwIdTime;
}

std::string CConf::getDMRIdLookupFile() const
{
return m_dmrIdLookupFile;
}

unsigned int CConf::getDMRIdLookupTime() const
{
return m_dmrIdLookupTime;
}

std::string CConf::getModemPort() const
{
return m_modemPort;
Expand Down Expand Up @@ -840,39 +857,45 @@ std::vector<unsigned int> CConf::getDMRBlackList() const
{
return m_dmrBlackList;
}

std::vector<unsigned int> CConf::getDMRDstIdBlacklistSlot1RF() const
{
return m_dmrDstIdBlacklistSlot1RF;
}

std::vector<unsigned int> CConf::getDMRDstIdBlacklistSlot2RF() const
{
return m_dmrDstIdBlacklistSlot2RF;
}

std::vector<unsigned int> CConf::getDMRDstIdWhitelistSlot1RF() const
{
return m_dmrDstIdWhitelistSlot1RF;
}std::vector<unsigned int> CConf::getDMRDstIdWhitelistSlot2RF() const
}

std::vector<unsigned int> CConf::getDMRDstIdWhitelistSlot2RF() const
{
return m_dmrDstIdWhitelistSlot2RF;
}

std::vector<unsigned int> CConf::getDMRDstIdBlacklistSlot1NET() const
{
return m_dmrDstIdBlacklistSlot1NET;
}

std::vector<unsigned int> CConf::getDMRDstIdBlacklistSlot2NET() const
{
return m_dmrDstIdBlacklistSlot2NET;
}

std::vector<unsigned int> CConf::getDMRDstIdWhitelistSlot1NET() const
{
return m_dmrDstIdWhitelistSlot1NET;
}std::vector<unsigned int> CConf::getDMRDstIdWhitelistSlot2NET() const
{
return m_dmrDstIdWhitelistSlot2NET;
}
std::string CConf::getDMRLookupFile() const

std::vector<unsigned int> CConf::getDMRDstIdWhitelistSlot2NET() const
{
return m_dmrLookupFile;
return m_dmrDstIdWhitelistSlot2NET;
}

unsigned int CConf::getDMRCallHang() const
Expand Down
10 changes: 7 additions & 3 deletions Conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ class CConf
bool getCWIdEnabled() const;
unsigned int getCWIdTime() const;

// The DMR Id section
std::string getDMRIdLookupFile() const;
unsigned int getDMRIdLookupTime() const;

// The Modem section
std::string getModemPort() const;
bool getModemRXInvert() const;
Expand Down Expand Up @@ -104,7 +108,6 @@ class CConf
std::vector<unsigned int> getDMRDstIdBlacklistSlot2NET() const;
std::vector<unsigned int> getDMRDstIdWhitelistSlot1NET() const;
std::vector<unsigned int> getDMRDstIdWhitelistSlot2NET() const;
std::string getDMRLookupFile() const;
unsigned int getDMRCallHang() const;
unsigned int getDMRTXHang() const;

Expand Down Expand Up @@ -206,6 +209,9 @@ class CConf
bool m_cwIdEnabled;
unsigned int m_cwIdTime;

std::string m_dmrIdLookupFile;
unsigned int m_dmrIdLookupTime;

std::string m_modemPort;
bool m_modemRXInvert;
bool m_modemTXInvert;
Expand Down Expand Up @@ -237,7 +243,6 @@ class CConf
bool m_dmrTGRewriteSlot2;
bool m_dmrBMAutoRewrite;
bool m_dmrBMRewriteReflectorVoicePrompts;

std::vector<unsigned int> m_dmrPrefixes;
std::vector<unsigned int> m_dmrBlackList;
std::vector<unsigned int> m_dmrDstIdBlacklistSlot1RF;
Expand All @@ -248,7 +253,6 @@ class CConf
std::vector<unsigned int> m_dmrDstIdBlacklistSlot2NET;
std::vector<unsigned int> m_dmrDstIdWhitelistSlot1NET;
std::vector<unsigned int> m_dmrDstIdWhitelistSlot2NET;
std::string m_dmrLookupFile;
unsigned int m_dmrCallHang;
unsigned int m_dmrTXHang;

Expand Down
101 changes: 80 additions & 21 deletions DMRLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@
*/

#include "DMRLookup.h"
#include "Timer.h"
#include "Log.h"

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>

CDMRLookup::CDMRLookup(const std::string& filename) :
m_filename(filename)
CDMRLookup::CDMRLookup(const std::string& filename, unsigned int reloadTime) :
CThread(),
m_filename(filename),
m_reloadTime(reloadTime),
m_table(),
m_mutex(),
m_stop(false)
{
}

Expand All @@ -34,13 +40,82 @@ CDMRLookup::~CDMRLookup()
}

bool CDMRLookup::read()
{
bool ret = load();

if (m_reloadTime > 0U)
run();

return ret;
}

void CDMRLookup::entry()
{
LogInfo("Started the DMR Id lookup reload thread");

CTimer timer(1U, 3600U * m_reloadTime);
timer.start();

while (!m_stop) {
sleep(1000U);

timer.clock();
if (timer.hasExpired()) {
load();
timer.start();
}
}

LogInfo("Stopped the DMR Id lookup reload thread");
}

void CDMRLookup::stop()
{
if (m_reloadTime == 0U) {
delete this;
return;
}

m_stop = true;

wait();
}

std::string CDMRLookup::find(unsigned int id)
{
std::string callsign;

if (id == 0xFFFFFFU)
return std::string("ALL");

m_mutex.lock();

try {
callsign = m_table.at(id);
} catch (...) {
char text[10U];
::sprintf(text, "%u", id);
callsign = std::string(text);
}

m_mutex.unlock();

return callsign;
}

bool CDMRLookup::load()
{
FILE* fp = ::fopen(m_filename.c_str(), "rt");
if (fp == NULL) {
LogWarning("Cannot open the Id lookup file - %s", m_filename.c_str());
return false;
}

m_mutex.lock();

// Remove the old entries
m_table.clear();

char buffer[100U];
while (::fgets(buffer, 100U, fp) != NULL) {
if (buffer[0U] == '#')
Expand All @@ -58,6 +133,8 @@ bool CDMRLookup::read()
}
}

m_mutex.unlock();

::fclose(fp);

size_t size = m_table.size();
Expand All @@ -67,22 +144,4 @@ bool CDMRLookup::read()
LogInfo("Loaded %u Ids to the callsign lookup table", size);

return true;
}

std::string CDMRLookup::find(unsigned int id) const
{
std::string callsign;

if (id == 0xFFFFFFU)
return std::string("ALL");

try {
callsign = m_table.at(id);
} catch (...) {
char text[10U];
::sprintf(text, "%u", id);
callsign = std::string(text);
}

return callsign;
}
}
20 changes: 16 additions & 4 deletions DMRLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,33 @@
#ifndef DMRLookup_H
#define DMRLookup_H

#include "Thread.h"
#include "Mutex.h"

#include <string>
#include <unordered_map>

class CDMRLookup {
class CDMRLookup : public CThread {
public:
CDMRLookup(const std::string& filename);
~CDMRLookup();
CDMRLookup(const std::string& filename, unsigned int reloadTime);
virtual ~CDMRLookup();

bool read();

std::string find(unsigned int id) const;
virtual void entry();

std::string find(unsigned int id);

void stop();

private:
std::string m_filename;
unsigned int m_reloadTime;
std::unordered_map<unsigned int, std::string> m_table;
CMutex m_mutex;
bool m_stop;

bool load();
};

#endif
5 changes: 4 additions & 1 deletion MMDVM.ini
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ FileRoot=MMDVM
Enable=1
Time=10

[DMR Id Lookup]
File=DMRIds.dat
Time=24

[Modem]
# Port=/dev/ttyACM0
Port=\\.\COM3
Expand Down Expand Up @@ -62,7 +66,6 @@ Id=123456
ColorCode=1
SelfOnly=0
# Prefixes=234,235
LookupFile=DMRIds.dat
CallHang=3
TXHang=4
#Blacklist=
Expand Down
Loading

0 comments on commit 40c086b

Please sign in to comment.