Repository navigation
Expand file tree
/
Copy pathDV3000.cpp
More file actions
160 lines (141 loc) · 4.22 KB
/
Copy pathDV3000.cpp
File metadata and controls
160 lines (141 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef SW_MODES_ONLY
/*
* Copyright (C) 2014 by Jonathan Naylor G4KLX and John Hays K7VE
* Copyright 2016 by Jeremy McDermond (NH6Z)
* Copyright 2021 by Thomas Early N7TAE
*
* Adapted by K7VE from G4KLX dv3000d
*/
// tcd - a hybid transcoder using DVSI hardware and Codec2 software
// Copyright © 2022 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 <sys/select.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <cerrno>
#include <thread>
#include "DV3000.h"
#include "Configure.h"
#include "Controller.h"
extern CController g_Cont;
CDV3000::CDV3000(Encoding t) : CDVDevice(t) {}
CDV3000::~CDV3000()
{
waiting_packet.Shutdown();
}
void CDV3000::PushWaitingPacket(unsigned int /* channel */, std::shared_ptr<CTranscoderPacket> packet)
{
waiting_packet.push(packet);
}
std::shared_ptr<CTranscoderPacket> CDV3000::PopWaitingPacket(unsigned int /* channel */)
{
return waiting_packet.pop();
}
bool CDV3000::SendAudio(const uint8_t /*channel*/, const int16_t *audio) const
{
// Create Audio packet based on input int8_ts
SDV_Packet p;
p.start_byte = PKT_HEADER;
p.header.payload_length = htons(1 + sizeof(p.payload.audio3k));
p.header.packet_type = PKT_SPEECH;
p.field_id = PKT_SPEECHD;
p.payload.audio3k.num_samples = 160U;
for (int i=0; i<160; i++)
p.payload.audio3k.samples[i] = htons(audio[i]);
// send audio packet to DV3000
const DWORD size = packet_size(p);
DWORD written;
auto status = FT_Write(ftHandle, &p, size, &written);
if (FT_OK != status)
{
FTDI_Error("Error writing audio packet", status);
return true;
}
else if (size != written)
{
std::cerr << "Incomplete Speech Packet write on " << description << std::endl;
return true;
}
return false;
}
bool CDV3000::SendData(const uint8_t /* channel */, const uint8_t *data) const
{
// Create data packet
SDV_Packet p;
p.start_byte = PKT_HEADER;
p.header.payload_length = htons(1 + sizeof(p.payload.ambe3k));
p.header.packet_type = PKT_CHANNEL;
p.field_id = PKT_CHAND;
p.payload.ambe3k.num_bits = 72U;
memcpy(p.payload.ambe3k.data, data, 9);
// send data packet to DV3000
const DWORD size = packet_size(p);
DWORD written;
auto status = FT_Write(ftHandle, &p, size, &written);
if (FT_OK != status)
{
FTDI_Error("Error writing AMBE Packet", status);
return true;
}
else if (size != written)
{
std::cerr << "Incomplete AMBE Packet write on " << description << std::endl;
return true;
}
return false;
}
void CDV3000::ProcessPacket(const SDV_Packet &p)
{
auto packet = PopWaitingPacket(PKT_CHANNEL0);
if (packet)
{
if (PKT_CHANNEL == p.header.packet_type)
{
if (11!=ntohs(p.header.payload_length) || PKT_CHAND!=p.field_id || 72!=p.payload.ambe3k.num_bits)
dump("Improper ambe packet:", &p, packet_size(p));
buffer_depth--;
if (Encoding::dstar == type)
packet->SetDStarData(p.payload.ambe3k.data);
else
packet->SetDMRData(p.payload.ambe3k.data);
}
else if (PKT_SPEECH == p.header.packet_type)
{
if (322!=ntohs(p.header.payload_length) || PKT_SPEECHD!=p.field_id || 160!=p.payload.audio3k.num_samples)
dump("Improper audio packet:", &p, packet_size(p));
buffer_depth--;
packet->SetAudioSamples(p.payload.audio3k.samples, true);
}
else
{
dump("ReadDevice() ERROR: Read an unexpected device packet:", &p, packet_size(p));
return;
}
if (Encoding::dstar == type) // is this a DMR or a DStar device?
{
g_Cont.dstar_mux.lock();
g_Cont.RouteDstPacket(packet);
g_Cont.dstar_mux.unlock();
}
else
{
g_Cont.dmrst_mux.lock();
g_Cont.RouteDmrPacket(packet);
g_Cont.dmrst_mux.unlock();
}
}
}
#endif