112 lines
2.3 KiB
C++
112 lines
2.3 KiB
C++
/*
|
|
* Copyright (C) 2018 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; version 2 of the License.
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
#include "POCSAGControl.h"
|
|
#include "Utils.h"
|
|
#include "Log.h"
|
|
|
|
#include <cstdio>
|
|
#include <cassert>
|
|
#include <cstring>
|
|
#include <ctime>
|
|
|
|
// #define DUMP_POCSAG
|
|
|
|
const unsigned char BIT_MASK_TABLE[] = { 0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U };
|
|
|
|
#define WRITE_BIT1(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE[(i)&7])
|
|
#define READ_BIT1(p,i) (p[(i)>>3] & BIT_MASK_TABLE[(i)&7])
|
|
|
|
CPOCSAGControl::CPOCSAGControl(CPOCSAGNetwork* network, CDisplay* display) :
|
|
m_network(network),
|
|
m_display(display),
|
|
m_queue(5000U, "POCSAG Control"),
|
|
m_fp(NULL)
|
|
{
|
|
assert(display != NULL);
|
|
}
|
|
|
|
CPOCSAGControl::~CPOCSAGControl()
|
|
{
|
|
}
|
|
|
|
unsigned int CPOCSAGControl::readModem(unsigned char* data)
|
|
{
|
|
assert(data != NULL);
|
|
|
|
if (m_queue.isEmpty())
|
|
return 0U;
|
|
|
|
unsigned char len = 0U;
|
|
m_queue.getData(&len, 1U);
|
|
|
|
m_queue.getData(data, len);
|
|
|
|
return len;
|
|
}
|
|
|
|
void CPOCSAGControl::writeNetwork()
|
|
{
|
|
unsigned char netData[40U];
|
|
unsigned int length = m_network->read(netData);
|
|
if (length == 0U)
|
|
return;
|
|
|
|
}
|
|
|
|
void CPOCSAGControl::clock(unsigned int ms)
|
|
{
|
|
if (m_network != NULL)
|
|
writeNetwork();
|
|
|
|
}
|
|
|
|
bool CPOCSAGControl::openFile()
|
|
{
|
|
if (m_fp != NULL)
|
|
return true;
|
|
|
|
time_t t;
|
|
::time(&t);
|
|
|
|
struct tm* tm = ::localtime(&t);
|
|
|
|
char name[100U];
|
|
::sprintf(name, "POCSAG_%04d%02d%02d_%02d%02d%02d.ambe", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
|
|
|
|
m_fp = ::fopen(name, "wb");
|
|
if (m_fp == NULL)
|
|
return false;
|
|
|
|
::fwrite("POCSAG", 1U, 3U, m_fp);
|
|
|
|
return true;
|
|
}
|
|
|
|
bool CPOCSAGControl::writeFile(const unsigned char* data)
|
|
{
|
|
if (m_fp == NULL)
|
|
return false;
|
|
|
|
::fwrite(data, 1U, POCSAG_FRAME_LENGTH_BYTES, m_fp);
|
|
|
|
return true;
|
|
}
|
|
|
|
void CPOCSAGControl::closeFile()
|
|
{
|
|
if (m_fp != NULL) {
|
|
::fclose(m_fp);
|
|
m_fp = NULL;
|
|
}
|
|
}
|