Skip to content

Commit

Permalink
add user database storage
Browse files Browse the repository at this point in the history
simply added UserDB* source code and modified Makefile.
others are untouched.
  • Loading branch information
jg1uaa committed Mar 4, 2020
1 parent ab13f35 commit 8ff3913
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ OBJECTS = \
NetworkInfo.o Nextion.o NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o NXDNCRC.o NXDNFACCH1.o NXDNLayer3.o NXDNLICH.o \
NXDNLookup.o NXDNNetwork.o NXDNSACCH.o NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o \
POCSAGControl.o POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o SerialController.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o \
TFTSurenoo.o Thread.o Timer.o UDPSocket.o UMP.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o
TFTSurenoo.o Thread.o Timer.o UDPSocket.o UMP.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o

all: MMDVMHost RemoteCommand

Expand Down
2 changes: 1 addition & 1 deletion Makefile.Pi
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ OBJECTS = \
NetworkInfo.o Nextion.o NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o NXDNCRC.o NXDNFACCH1.o NXDNLayer3.o NXDNLICH.o NXDNLookup.o \
NXDNNetwork.o NXDNSACCH.o NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o POCSAGControl.o \
POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o SerialController.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o \
UDPSocket.o UMP.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o
UDPSocket.o UMP.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o

all: MMDVMHost RemoteCommand

Expand Down
2 changes: 1 addition & 1 deletion Makefile.Pi.Adafruit
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ OBJECTS = \
NetworkInfo.o Nextion.o NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o NXDNCRC.o NXDNFACCH1.o NXDNLayer3.o NXDNLICH.o NXDNLookup.o \
NXDNNetwork.o NXDNSACCH.o NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o POCSAGControl.o \
POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o SerialController.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o \
Timer.o UDPSocket.o UMP.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o
Timer.o UDPSocket.o UMP.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o

all: MMDVMHost RemoteCommand

Expand Down
2 changes: 1 addition & 1 deletion Makefile.Pi.HD44780
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ OBJECTS = \
NetworkInfo.o Nextion.o NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o NXDNCRC.o NXDNFACCH1.o NXDNLayer3.o NXDNLICH.o NXDNLookup.o \
NXDNNetwork.o NXDNSACCH.o NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o POCSAGControl.o \
POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o SerialController.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o \
Timer.o UDPSocket.o UMP.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o
Timer.o UDPSocket.o UMP.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o

all: MMDVMHost RemoteCommand

Expand Down
2 changes: 1 addition & 1 deletion Makefile.Pi.OLED
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ OBJECTS = \
NetworkInfo.o Nextion.o NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o NXDNCRC.o NXDNFACCH1.o NXDNLayer3.o NXDNLICH.o NXDNLookup.o \
NXDNNetwork.o NXDNSACCH.o NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o POCSAGControl.o \
POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o SerialController.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o \
Timer.o UDPSocket.o UMP.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o
Timer.o UDPSocket.o UMP.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o

all: MMDVMHost RemoteCommand

Expand Down
2 changes: 1 addition & 1 deletion Makefile.Pi.PCF8574
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ OBJECTS = \
NetworkInfo.o Nextion.o NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o NXDNCRC.o NXDNFACCH1.o NXDNLayer3.o NXDNLICH.o NXDNLookup.o \
NXDNNetwork.o NXDNSACCH.o NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o POCSAGControl.o \
POCSAGNetwork.o QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o SerialController.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o \
Timer.o UDPSocket.o UMP.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o
Timer.o UDPSocket.o UMP.o UserDB.o UserDBentry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o

all: MMDVMHost RemoteCommand

Expand Down
2 changes: 1 addition & 1 deletion Makefile.Solaris
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ OBJECTS = \
NullDisplay.o NullModem.o NXDNAudio.o NXDNControl.o NXDNConvolution.o NXDNCRC.o NXDNFACCH1.o NXDNLayer3.o NXDNLICH.o NXDNLookup.o NXDNNetwork.o \
NXDNSACCH.o NXDNUDCH.o P25Audio.o P25Control.o P25Data.o P25LowSpeedData.o P25Network.o P25NID.o P25Trellis.o P25Utils.o POCSAGControl.o POCSAGNetwork.o \
QR1676.o RemoteControl.o RS129.o RS241213.o RSSIInterpolator.o SerialController.o SerialPort.o SHA256.o StopWatch.o Sync.o TFTSerial.o TFTSurenoo.o Thread.o Timer.o UDPSocket.o \
UMP.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o
UMP.o UserDB.o UserDBebtry.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFNetwork.o YSFPayload.o

all: MMDVMHost RemoteCommand

Expand Down
184 changes: 184 additions & 0 deletions UserDB.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* Copyright (C) 2020 by SASANO Takayoshi JG1UAA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "UserDB.h"
#include "Log.h"

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

CUserDB::CUserDB() :
m_table(),
m_mutex()
{
}

CUserDB::~CUserDB()
{
}

bool CUserDB::lookup(unsigned int id, class CUserDBentry *entry)
{
bool rv;

m_mutex.lock();

try {
if (entry != NULL)
*entry = m_table.at(id);
else
m_table.at(id);

rv = true;
} catch (...) {
rv = false;
}

m_mutex.unlock();

return rv;
}

bool CUserDB::load(std::string const& filename)
{
FILE* fp = ::fopen(filename.c_str(), "r");
if (fp == NULL) {
LogWarning("Cannot open ID lookup table - %s", filename.c_str());
return false;
}

m_mutex.lock();

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

// set index for entries
char buffer[256U];
if (::fgets(buffer, sizeof(buffer), fp) == NULL) {
LogWarning("ID lookup file has no entry - %s", filename.c_str());
m_mutex.unlock();
::fclose(fp);
return false;
}

// no index - set default
std::unordered_map<std::string, int> index;
if (!makeindex(buffer, index)) {
::strncpy(buffer, "RADIO_ID,CALLSIGN,FIRST_NAME", sizeof(buffer));
makeindex(buffer, index);
::rewind(fp);
}

while (::fgets(buffer, sizeof(buffer), fp) != NULL) {
if (buffer[0U] != '#')
parse(buffer, index);
}

::fclose(fp);

size_t size = m_table.size();
m_mutex.unlock();

LogInfo("Loaded %u IDs to lookup table - %s", size, filename.c_str());

return (size != 0U);
}

bool CUserDB::makeindex(char* buf, std::unordered_map<std::string, int>& index)
{
int i;
char *p1, *p2;

for (i = 0, p1 = tokenize(buf, &p2); p1 != NULL;
i++, p1 = tokenize(p2, &p2)) {

// create [column keyword] - [column number] table
if (CUserDBentry::isValidKey(p1))
index[p1] = i;
}

try {
index.at(keyRADIO_ID);
index.at(keyCALLSIGN);
return true;
} catch (...) {
return false;
}
}

void CUserDB::parse(char* buf, std::unordered_map<std::string, int>& index)
{
int i;
char *p1, *p2;
std::unordered_map<std::string, char*> ptr;
unsigned int id;

for (i = 0, p1 = tokenize(buf, &p2); p1 != NULL;
i++, p1 = tokenize(p2, &p2)) {

for (auto it = index.begin(); it != index.end(); it++) {
// first: column keyword, second: column number
if (it->second == i) {
ptr[it->first] = p1;
break;
}
}
}

try {
ptr.at(keyRADIO_ID);
ptr.at(keyCALLSIGN);
} catch (...) {
return;
}

id = (unsigned int)::atoi(ptr[keyRADIO_ID]);
toupper_string(ptr[keyCALLSIGN]);

for (auto it = ptr.begin(); it != ptr.end(); it++) {
// no need to regist radio ID
if (it->first == keyRADIO_ID)
continue;

m_table[id].set(it->first, std::string(it->second));
}
}

void CUserDB::toupper_string(char* str)
{
while (*str != '\0') {
*str = ::toupper(*str);
str++;
}
}

char* CUserDB::tokenize(char* str, char** next)
{
if (*str == '\0')
return NULL;

char* p = ::strpbrk(str, ",\t\r\n");
if (p == NULL) {
*next = str + ::strlen(str);
} else {
*p = '\0';
*next = p + 1;
}

return str;
}
43 changes: 43 additions & 0 deletions UserDB.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2020 by SASANO Takayoshi JG1UAA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#if !defined(USERDB_H)
#define USERDB_H

#include "UserDBentry.h"
#include "Mutex.h"

class CUserDB {
public:
CUserDB();
~CUserDB();

bool lookup(unsigned int id, class CUserDBentry *entry);
bool load(std::string const& filename);

private:
bool makeindex(char* buf, std::unordered_map<std::string, int>& index);
void parse(char* buf, std::unordered_map<std::string, int>& index);
void toupper_string(char* str);
char* tokenize(char* str, char** next);

std::unordered_map<unsigned int, class CUserDBentry> m_table;
CMutex m_mutex;
};

#endif
54 changes: 54 additions & 0 deletions UserDBentry.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2020 by SASANO Takayoshi JG1UAA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "UserDBentry.h"
#include <algorithm>

CUserDBentry::CUserDBentry() :
m_db()
{
}

CUserDBentry::~CUserDBentry()
{
}

const std::vector<std::string> CUserDBentry::keyList {
keyRADIO_ID, keyCALLSIGN, keyFIRST_NAME, keyLAST_NAME,
keyCITY, keySTATE, keyCOUNTRY,
};

bool CUserDBentry::isValidKey(const std::string key)
{
auto it = std::find(keyList.begin(), keyList.end(), key);
return it != keyList.end();
}

void CUserDBentry::set(const std::string key, const std::string value)
{
if (isValidKey(key))
m_db[key] = value;
}

std::string CUserDBentry::get(const std::string key)
{
try {
return m_db.at(key);
} catch (...) {
return "";
}
}
49 changes: 49 additions & 0 deletions UserDBentry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2020 by SASANO Takayoshi JG1UAA
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#if !defined(USERDBENTRY_H)
#define USERDBENTRY_H

#include <string>
#include <unordered_map>
#include <vector>

#define keyRADIO_ID "RADIO_ID"
#define keyCALLSIGN "CALLSIGN"
#define keyFIRST_NAME "FIRST_NAME"
#define keyLAST_NAME "LAST_NAME"
#define keyCITY "CITY"
#define keySTATE "STATE"
#define keyCOUNTRY "COUNTRY"

class CUserDBentry {
public:
CUserDBentry();
~CUserDBentry();

static const std::vector<std::string> keyList;
static bool isValidKey(const std::string key);

void set(const std::string key, const std::string value);
std::string get(const std::string key);

private:
std::unordered_map<std::string, std::string> m_db;
};

#endif

2 comments on commit 8ff3913

@shawnchain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
I would like to know why using key/value in UserDBentry for the CSV fields instead of using member fields?
a value object would be more straightforward here I think.

@jg1uaa
Copy link
Contributor Author

@jg1uaa jg1uaa commented on 8ff3913 Mar 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm new to C++ and STL, and I didn't have idea to write with value object pattern.
This blog (written in Japanese) shows first class collection and it might help to write clean code (https://blog.sojiro.me/blog/2017/08/25/value-object-and-collection-object/), but can this collection maintain same flexibility for CSV fields as current implementation with key/value?
First of all, to reduce XXXtable (XXX means any field name) in program I choose using associative array.

Please sign in to comment.