added a packet queue

main
Tom Early 4 years ago
parent 3d3ff08eda
commit 08ca65ec2f

@ -20,7 +20,7 @@
bool CController::Start()
{
if (socket.Open("urfd2tcd", this))
if (reader.Open("urfd2tcd"))
{
keep_running = false;
return true;
@ -33,7 +33,7 @@ void CController::Stop()
{
keep_running = false;
future.get();
socket.Close();
reader.Close();
}
void CController::Processing()

@ -24,7 +24,7 @@
#include "DV3003.h"
#include "configure.h"
#include "UnixPacketSocket.h"
#include "UnixDgramSocket.h"
class CController
{
@ -39,7 +39,8 @@ private:
std::atomic<bool> keep_running;
std::future<void> future;
std::vector<std::shared_ptr<CDV3003>> dmr_devices, dstar_devices;
CUnixPacketClient socket;
CUnixDgramReader reader;
CUnixDgramWriter writer;
void Processing();

@ -403,7 +403,7 @@ bool CDV3003::GetAudio(int16_t *audio)
p.start_byte = 0U;
if (getresponse(p))
return true;
if (p.start_byte!=PKT_HEADER || htons(p.header.payload_length)!=322 ||
if (p.start_byte!=PKT_HEADER || htons(p.header.payload_length)!=323 ||
p.header.packet_type!=PKT_SPEECH || p.payload.audio.speechd!=0U ||
p.payload.audio.num_samples!=160U) {
std::cerr << "GetAudio: unexpected audio packet response" << std::endl;

@ -50,8 +50,7 @@
#define packet_size(a) int(1 + sizeof((a).header) + ntohs((a).header.payload_length))
#pragma pack(push, 1)
struct dv3003_packet {
using SDV3003_Packet = struct __attribute__ ((__packed__)) dv3003_packet {
uint8_t start_byte;
struct {
uint16_t payload_length;
@ -83,9 +82,7 @@ struct dv3003_packet {
} ambe;
} payload;
};
#pragma pack(pop)
using SDV3003_Packet = struct dv3003_packet;
enum class Encoding { dstar, dmr };
class CDV3003 {

@ -1,4 +1,3 @@
// tcd - a hybid transcoder using DVSI hardware and Codec2 software
// Copyright © 2021 Thomas A. Early N7TAE
@ -17,76 +16,52 @@
#include "TranscoderPacket.h"
CTranscoderPacket::CTranscoderPacket() : module(' '), codec_in(ECodecType::none), dstar_set(false), dmr_set(false), m17_set(false)
CTranscoderPacket::CTranscoderPacket(char mod) : dstar_set(false), dmr_set(false), m17_set(false)
{
tcpacket.module = mod;
}
char CTranscoderPacket::GetModule() const
{
return module;
}
void CTranscoderPacket::SetModule(char mod)
{
module = mod;
return tcpacket.module;
}
const uint8_t *CTranscoderPacket::GetDStarData()
{
return data.dstar;
return tcpacket.dstar;
}
const uint8_t *CTranscoderPacket::GetDMRData()
{
return data.dmr;
return tcpacket.dmr;
}
const uint8_t *CTranscoderPacket::GetM17Data()
{
return data.m17;
return tcpacket.m17;
}
void CTranscoderPacket::SetDStarData(const uint8_t *dstar)
{
memcpy(data.dstar, dstar, 9);
memcpy(tcpacket.dstar, dstar, 9);
dstar_set = true;
}
void CTranscoderPacket::SetDMRData(const uint8_t *dmr )
{
memcpy(data.dmr, dmr, 9);
memcpy(tcpacket.dmr, dmr, 9);
dmr_set = true;
}
void CTranscoderPacket::SetM17Data(const uint8_t *m17, bool is_3200)
{
memcpy(data.m17, m17, is_3200 ? 16 : 8);
memcpy(tcpacket.m17, m17, is_3200 ? 16 : 8);
m17_set = true;
m17_is_3200 = is_3200;
}
void CTranscoderPacket::SetCodecIn(ECodecType type, uint8_t *data)
{
switch (type) {
case ECodecType::dstar:
SetDStarData(data);
break;
case ECodecType::dmr:
SetDMRData(data);
break;
case ECodecType::m17_1600:
SetM17Data(data, false);
break;
case ECodecType::m17_3200:
SetM17Data(data, true);
true;
}
if (type != ECodecType::none)
codec_in = type;
}
ECodecType CTranscoderPacket::GetCodecIn() const
{
return codec_in;
return tcpacket.codec_in;
}
bool CTranscoderPacket::DStarIsSet() const

@ -20,23 +20,16 @@
#include <cstring>
#include <stdint.h>
using SCodecData = struct codecdata_tag {
uint8_t dstar[9];
uint8_t dmr[9];
uint8_t m17[16];
};
enum class ECodecType { none, dstar, dmr, m17_1600, m17_3200 };
#include "TCPacketDef.h"
class CTranscoderPacket
{
public:
// constructor
CTranscoderPacket();
CTranscoderPacket(char mod);
// this packet's refector module;
char GetModule() const;
void SetModule(char mod);
// codec
const uint8_t *GetDStarData();
@ -46,9 +39,6 @@ public:
void SetDMRData(const uint8_t *dmr );
void SetM17Data(const uint8_t *m17, bool is_3200);
// first time load
void SetCodecIn(ECodecType type, uint8_t *data);
// state of packet
ECodecType GetCodecIn() const;
bool DStarIsSet() const;
@ -58,8 +48,7 @@ public:
bool AllAreSet() const;
private:
char module;
ECodecType codec_in;
STCPacket tcpacket;
uint16_t audio[320];
bool m17_is_3200, dstar_set, dmr_set, m17_set;
SCodecData data;
};

@ -1,219 +0,0 @@
// tcd - a hybid transcoder using DVSI hardware and Codec2 software
// Copyright © 2021 Thomas A. Early N7TAE
// 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 3 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, see <https://www.gnu.org/licenses/>.
#include <iostream>
#include <string>
#include <thread>
#include <chrono>
#include <unistd.h>
#include <sys/un.h>
#include <sys/socket.h>
#include "UnixPacketSocket.h"
CUnixPacket::CUnixPacket() : m_fd(-1), m_host(NULL) {}
bool CUnixPacket::Receive(std::vector<uint8_t> &Buffer, unsigned timeout)
{
// socket valid ?
if ( 0 > m_fd )
return false;
// control socket
fd_set FdSet;
FD_ZERO(&FdSet);
FD_SET(m_fd, &FdSet);
struct timeval tv;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
auto rval = select(m_fd + 1, &FdSet, 0, 0, &tv);
if (rval < 0)
{
std::cerr << "select error on Unix socket " << m_name << ": " << strerror(errno) << std::endl;
}
if (rval > 0)
{
uint8_t buf[USB3XXX_MAXPACKETSIZE];
auto len = read(m_fd, buf, USB3XXX_MAXPACKETSIZE);
if (len <= 0)
return true;
Buffer.resize(len);
memcpy(Buffer.data(), buf, len);
return false;
}
Restart();
return true;
}
bool CUnixPacket::Write(const void *buffer, const ssize_t size)
{
if (0 > m_fd)
return true;
ssize_t written = write(m_fd, buffer, size);
if (written != size)
{
if (-1 == written)
{
std::cerr << "Write error on '" << m_name << "': " << strerror(errno) << std::endl;
}
else
{
std::cout << "Write error on '" << m_name << "': Only wrote " << written << " of " << size << " bytes" << std::endl;
}
return Restart();
}
return false;
}
bool CUnixPacket::Restart()
{
if (! m_host->IsRunning())
return true;
std::cout << "Restarting '" << m_name << "'... " << std::endl;
Close();
std::string name(m_name);
return Open(name.c_str(), m_host);
}
int CUnixPacket::GetFD()
{
return m_fd;
}
CUnixPacketServer::CUnixPacketServer() : m_server(-1) {}
CUnixPacketServer::~CUnixPacketServer()
{
Close();
}
bool CUnixPacketServer::Open(const char *name, CController *host)
{
m_server = socket(AF_UNIX, SOCK_SEQPACKET, 0);
m_host = host;
if (m_server < 0)
{
std::cerr << "Cannot open '" << name << "' socket: " << strerror(errno) << std::endl;
return true;
}
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
memcpy(addr.sun_path+1, name, strlen(name));
if (-1 == bind(m_server, (struct sockaddr *)&addr, sizeof(addr)))
{
std::cerr << "Cannot bind '" << name << "' socket: " << strerror(errno) << std::endl;
Close();
return true;
}
if (-1 == listen(m_server, 1))
{
std::cerr << "Cannot listen on '" << name << "' socket: " << strerror(errno) << std::endl;
Close();
return true;
}
m_fd = accept(m_server, nullptr, 0);
if (m_fd < 0)
{
std::cerr << "Cannot accept on '" << name << "' socket: " << strerror(errno) << std::endl;
Close();
return true;
}
strncpy(m_name, name, 108);
return false;
}
void CUnixPacketServer::Close()
{
if (m_server >= 0)
{
close(m_server);
m_server = -1;
}
if (m_fd >= 0)
{
close(m_fd);
m_fd = -1;
}
}
CUnixPacketClient::~CUnixPacketClient()
{
Close();
}
bool CUnixPacketClient::Open(const char *name, CController *host)
{
m_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (m_fd < 0)
{
std::cerr << "Cannot open unix client socket " << name << std::endl;
return true;
}
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
memcpy(addr.sun_path+1, name, strlen(name));
int rval = -1;
int tries = 0;
while (rval < 0)
{
rval = connect(m_fd, (struct sockaddr *)&addr, sizeof(addr));
if (rval < 0)
{
if (ECONNREFUSED == errno)
{
if (0 == tries++ % 20)
std::cout << "Waiting for " << name << " server to start..." << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
else
{
std::cerr << "Cannot connect '" << name << "' socket: " << strerror(errno) << std::endl;
Close();
return true;
}
}
if (! m_host->IsRunning())
{
Close();
return true;
}
}
m_host = host;
strncpy(m_name, name, 108);
return false;
}
void CUnixPacketClient::Close()
{
if (m_fd >= 0)
{
close(m_fd);
m_fd = -1;
}
}

@ -1,58 +0,0 @@
#pragma once
// tcd - a hybid transcoder using DVSI hardware and Codec2 software
// Copyright © 2021 Thomas A. Early N7TAE
// 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 3 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, see <https://www.gnu.org/licenses/>.
#include <vector>
#include <sys/types.h>
#include "Controller.h"
class CUnixPacket
{
public:
CUnixPacket();
virtual bool Open(const char *name, CController *host) = 0;
virtual void Close() = 0;
bool Write(const void *buffer, const ssize_t size);
bool Receive(std::vector<uint8_t> &buf, unsigned timeout);
int GetFD();
protected:
bool Restart();
int m_fd;
CController *m_host;
char m_name[108];
};
class CUnixPacketServer : public CUnixPacket
{
public:
CUnixPacketServer();
~CUnixPacketServer();
bool Open(const char *name, CController *host);
void Close();
protected:
int m_server;
};
class CUnixPacketClient : public CUnixPacket
{
public:
~CUnixPacketClient();
bool Open(const char *name, CController *host);
void Close();
};
Loading…
Cancel
Save

Powered by TurnKey Linux.