1
0
Fork 0

Add the APRS writer thread and support class.

ycs232-kbc
Jonathan Naylor 8 years ago
parent 9e71805129
commit d9c0bb27c9

@ -0,0 +1,267 @@
/*
* Copyright (C) 2010-2014,2016 by Jonathan Naylor G4KLX
*
* 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 "APRSWriterThread.h"
#include "Utils.h"
#include "Log.h"
#include <algorithm>
#include <functional>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cassert>
// #define DUMP_TX
const unsigned int CALLSIGN_LENGTH = 8U;
const unsigned int APRS_TIMEOUT = 10U;
CAPRSWriterThread::CAPRSWriterThread(const std::string& callsign, const std::string& address, const std::string& hostname, unsigned int port) :
CThread(),
m_username(callsign),
m_ssid(callsign),
m_socket(hostname, port, address),
m_queue(20U, "APRS Queue"),
m_exit(false),
m_connected(false),
m_APRSReadCallback(NULL),
m_filter(),
m_clientName("YSFGateway")
{
assert(!callsign.empty());
assert(!hostname.empty());
assert(port > 0U);
m_username.resize(CALLSIGN_LENGTH, ' ');
m_username.erase(std::find_if(m_username.rbegin(), m_username.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), m_username.end());
std::transform(m_username.begin(), m_username.end(), m_username.begin(), ::toupper);
m_ssid = m_ssid.substr(CALLSIGN_LENGTH - 1U, 1);
}
CAPRSWriterThread::CAPRSWriterThread(const std::string& callsign, const std::string& address, const std::string& hostname, unsigned int port, const std::string& filter, const std::string& clientName) :
CThread(),
m_username(callsign),
m_ssid(callsign),
m_socket(hostname, port, address),
m_queue(20U, "APRS Queue"),
m_exit(false),
m_connected(false),
m_APRSReadCallback(NULL),
m_filter(filter),
m_clientName(clientName)
{
assert(!callsign.empty());
assert(!hostname.empty());
assert(port > 0U);
m_username.resize(CALLSIGN_LENGTH, ' ');
m_username.erase(std::find_if(m_username.rbegin(), m_username.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), m_username.end());
std::transform(m_username.begin(), m_username.end(), m_username.begin(), ::toupper);
m_ssid = m_ssid.substr(CALLSIGN_LENGTH - 1U, 1);
}
CAPRSWriterThread::~CAPRSWriterThread()
{
m_username.clear();
}
bool CAPRSWriterThread::start()
{
run();
return true;
}
void CAPRSWriterThread::entry()
{
LogMessage("Starting the APRS Writer thread");
m_connected = connect();
try {
while (!m_exit) {
if (!m_connected) {
m_connected = connect();
if (!m_connected){
LogError("Reconnect attempt to the APRS server has failed");
Sleep(10000UL); // 10 secs
}
}
if (m_connected) {
if (!m_queue.isEmpty()){
char* p;
m_queue.getData(&p, 1U);
LogMessage("APRS ==> %s", p);
::strcat(p, "\r\n");
bool ret = m_socket.write((unsigned char*)p, ::strlen(p));
if (!ret) {
m_connected = false;
m_socket.close();
LogError("Connection to the APRS thread has failed");
}
delete[] p;
}
{
std::string line;
int length = m_socket.readLine(line, APRS_TIMEOUT);
if (length < 0) {
m_connected = false;
m_socket.close();
LogError("Error when reading from the APRS server");
}
if(length > 0 && line.at(0U) != '#'//check if we have something and if that something is an APRS frame
&& m_APRSReadCallback != NULL)//do we have someone wanting an APRS Frame?
{
//wxLogMessage(wxT("Received APRS Frame : ") + line);
m_APRSReadCallback(std::string(line));
}
}
}
}
if (m_connected)
m_socket.close();
while (!m_queue.isEmpty()) {
char* p;
m_queue.getData(&p, 1U);
delete[] p;
}
}
catch (std::exception& e) {
LogError("Exception raised in the APRS Writer thread - \"%s\"", e.what());
}
catch (...) {
LogError("Unknown exception raised in the APRS Writer thread");
}
LogMessage("Stopping the APRS Writer thread");
}
void CAPRSWriterThread::setReadAPRSCallback(ReadAPRSFrameCallback cb)
{
m_APRSReadCallback = cb;
}
void CAPRSWriterThread::write(const char* data)
{
assert(data != NULL);
if (!m_connected)
return;
unsigned int len = ::strlen(data);
char* p = new char[len + 5U];
::strcpy(p, data);
m_queue.addData(&p, 1U);
}
bool CAPRSWriterThread::isConnected() const
{
return m_connected;
}
void CAPRSWriterThread::stop()
{
m_exit = true;
wait();
}
bool CAPRSWriterThread::connect()
{
unsigned int password = getAPRSPassword(m_username);
bool ret = m_socket.open();
if (!ret)
return false;
//wait for lgin banner
int length;
std::string serverResponse;
length = m_socket.readLine(serverResponse, APRS_TIMEOUT);
if (length == 0) {
LogError("No reply from the APRS server after %u seconds", APRS_TIMEOUT);
m_socket.close();
return false;
}
LogMessage("Received login banner : %s", serverResponse.c_str());
std::string filter(m_filter);
if (filter.length() > 0)
filter.insert(0U, " filter ");
char connectString[200U];
::sprintf(connectString, "user %s-%s pass %u vers %s%s\n", m_username.c_str(), m_ssid.c_str(), password, (m_clientName.length() ? m_clientName : "YSFGateway").c_str(), filter.c_str());
ret = m_socket.writeLine(std::string(connectString));
if (!ret) {
m_socket.close();
return false;
}
length = m_socket.readLine(serverResponse, APRS_TIMEOUT);
if (length == 0) {
LogError("No reply from the APRS server after %u seconds", APRS_TIMEOUT);
m_socket.close();
return false;
}
if (length < 0) {
LogError("Error when reading from the APRS server");
m_socket.close();
return false;
}
LogMessage("Response from APRS server: %s", serverResponse.c_str());
LogMessage("Connected to the APRS server");
return true;
}
unsigned int CAPRSWriterThread::getAPRSPassword(std::string callsign) const
{
unsigned int len = callsign.length();
uint16_t hash = 0x73E2U;
for (unsigned int i = 0U; i < len; i += 2U) {
hash ^= (char)callsign.at(i) << 8;
if (i + 1 < len)
hash ^= (char)callsign.at(i + 1);
}
return hash & 0x7FFFU;
}

@ -0,0 +1,63 @@
/*
* Copyright (C) 2010,2011,2012,2016 by Jonathan Naylor G4KLX
*
* 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.
*/
#ifndef APRSWriterThread_H
#define APRSWriterThread_H
#include "TCPSocket.h"
#include "RingBuffer.h"
#include "Thread.h"
#include <string>
typedef void (*ReadAPRSFrameCallback)(const std::string&);
class CAPRSWriterThread : public CThread {
public:
CAPRSWriterThread(const std::string& callsign, const std::string& address, const std::string& hostname, unsigned int port);
CAPRSWriterThread(const std::string& callsign, const std::string& address, const std::string& hostname, unsigned int port, const std::string& filter, const std::string& clientName);
virtual ~CAPRSWriterThread();
virtual bool start();
virtual bool isConnected() const;
virtual void write(const char* data);
virtual void entry();
virtual void stop();
void setReadAPRSCallback(ReadAPRSFrameCallback cb);
private:
std::string m_username;
std::string m_ssid;
CTCPSocket m_socket;
CRingBuffer<char*> m_queue;
bool m_exit;
bool m_connected;
ReadAPRSFrameCallback m_APRSReadCallback;
std::string m_filter;
std::string m_clientName;
bool connect();
unsigned int getAPRSPassword(std::string username) const;
};
#endif

@ -0,0 +1,99 @@
/*
* Copyright (C) 2015,2016 by Jonathan Naylor G4KLX
*
* 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 "Thread.h"
#if defined(_WIN32) || defined(_WIN64)
CThread::CThread() :
m_handle()
{
}
CThread::~CThread()
{
}
bool CThread::run()
{
m_handle = ::CreateThread(NULL, 0, &helper, this, 0, NULL);
return m_handle != NULL;
}
void CThread::wait()
{
::WaitForSingleObject(m_handle, INFINITE);
::CloseHandle(m_handle);
}
DWORD CThread::helper(LPVOID arg)
{
CThread* p = (CThread*)arg;
p->entry();
return 0UL;
}
void CThread::sleep(unsigned int ms)
{
::Sleep(ms);
}
#else
CThread::CThread() :
m_thread()
{
}
CThread::~CThread()
{
}
bool CThread::run()
{
return ::pthread_create(&m_thread, NULL, helper, this) == 0;
}
void CThread::wait()
{
::pthread_join(m_thread, NULL);
}
void* CThread::helper(void* arg)
{
CThread* p = (CThread*)arg;
p->entry();
return NULL;
}
void CThread::sleep(unsigned int ms)
{
::usleep(ms * 1000);
}
#endif

@ -0,0 +1,56 @@
/*
* Copyright (C) 2015,2016 by Jonathan Naylor G4KLX
*
* 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(THREAD_H)
#define THREAD_H
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#else
#include <pthread.h>
#endif
class CThread
{
public:
CThread();
virtual ~CThread();
virtual bool run();
virtual void entry() = 0;
virtual void wait();
static void sleep(unsigned int ms);
private:
#if defined(_WIN32) || defined(_WIN64)
HANDLE m_handle;
#else
pthread_t m_thread;
#endif
#if defined(_WIN32) || defined(_WIN64)
static DWORD __stdcall helper(LPVOID arg);
#else
static void* helper(void* arg);
#endif
};
#endif

@ -20,6 +20,7 @@
#include "StopWatch.h" #include "StopWatch.h"
#include "Version.h" #include "Version.h"
#include "YSFFICH.h" #include "YSFFICH.h"
#include "Thread.h"
#include "Timer.h" #include "Timer.h"
#include "Log.h" #include "Log.h"
@ -261,13 +262,8 @@ int CYSFGateway::run()
watchdogTimer.stop(); watchdogTimer.stop();
} }
if (ms < 5U) { if (ms < 5U)
#if defined(_WIN32) || defined(_WIN64) CThread::sleep(5U);
::Sleep(5UL); // 5ms
#else
::usleep(5000); // 5ms
#endif
}
} }
rptNetwork.close(); rptNetwork.close();

@ -146,6 +146,7 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="APRSWriterThread.h" />
<ClInclude Include="Conf.h" /> <ClInclude Include="Conf.h" />
<ClInclude Include="CRC.h" /> <ClInclude Include="CRC.h" />
<ClInclude Include="Golay24128.h" /> <ClInclude Include="Golay24128.h" />
@ -156,6 +157,7 @@
<ClInclude Include="RingBuffer.h" /> <ClInclude Include="RingBuffer.h" />
<ClInclude Include="StopWatch.h" /> <ClInclude Include="StopWatch.h" />
<ClInclude Include="TCPSocket.h" /> <ClInclude Include="TCPSocket.h" />
<ClInclude Include="Thread.h" />
<ClInclude Include="Timer.h" /> <ClInclude Include="Timer.h" />
<ClInclude Include="UDPSocket.h" /> <ClInclude Include="UDPSocket.h" />
<ClInclude Include="Utils.h" /> <ClInclude Include="Utils.h" />
@ -168,6 +170,7 @@
<ClInclude Include="YSFPayload.h" /> <ClInclude Include="YSFPayload.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="APRSWriterThread.cpp" />
<ClCompile Include="Conf.cpp" /> <ClCompile Include="Conf.cpp" />
<ClCompile Include="CRC.cpp" /> <ClCompile Include="CRC.cpp" />
<ClCompile Include="Golay24128.cpp" /> <ClCompile Include="Golay24128.cpp" />
@ -177,6 +180,7 @@
<ClCompile Include="Network.cpp" /> <ClCompile Include="Network.cpp" />
<ClCompile Include="StopWatch.cpp" /> <ClCompile Include="StopWatch.cpp" />
<ClCompile Include="TCPSocket.cpp" /> <ClCompile Include="TCPSocket.cpp" />
<ClCompile Include="Thread.cpp" />
<ClCompile Include="Timer.cpp" /> <ClCompile Include="Timer.cpp" />
<ClCompile Include="UDPSocket.cpp" /> <ClCompile Include="UDPSocket.cpp" />
<ClCompile Include="Utils.cpp" /> <ClCompile Include="Utils.cpp" />

@ -71,6 +71,12 @@
<ClInclude Include="TCPSocket.h"> <ClInclude Include="TCPSocket.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="APRSWriterThread.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Thread.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="Network.cpp"> <ClCompile Include="Network.cpp">
@ -124,5 +130,11 @@
<ClCompile Include="TCPSocket.cpp"> <ClCompile Include="TCPSocket.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="APRSWriterThread.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Thread.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>
Loading…
Cancel
Save