-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathpacket.cpp
More file actions
159 lines (135 loc) · 3.13 KB
/
packet.cpp
File metadata and controls
159 lines (135 loc) · 3.13 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
//
// Copyright © 2020-2025 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
// with this software. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
#include <arpa/inet.h>
#include "packet.h"
#include "crc.h"
static const CCRC CRC;
CPacket::CPacket()
{
memset(data, 0, MAX_PACKET_SIZE + 1);
size = 0;
}
const uint8_t *CPacket::GetCDstAddress() const
{
return data + (isstream ? 6u : 4u);
}
uint8_t *CPacket::GetDstAddress()
{
return data + (isstream ? 6u : 4u);
}
const uint8_t *CPacket::GetCSrcAddress() const
{
return data + (isstream ? 12u : 10u);
}
uint8_t *CPacket::GetSrcAddress()
{
return data + (isstream ? 12u : 10u);
}
// returns the StreamID in host byte order
uint16_t CPacket::GetStreamId() const
{
return isstream ? Get16At(4) : 0u;
}
void CPacket::SetStreamId(uint16_t sid)
{
if (isstream)
{
Set16At(4, sid);
}
}
uint8_t *CPacket::GetMetaData()
{
return data + (isstream ? 20 : 18);
}
const uint8_t *CPacket::GetCMetaData() const
{
return data + (isstream ? 20 : 18);
}
// returns LSD:TYPE in host byte order
uint16_t CPacket::GetFrameType() const
{
return Get16At(isstream ? 18 : 16);
}
void CPacket::SetFrameType(uint16_t ft)
{
Set16At(isstream ? 18 : 16, ft);
}
uint16_t CPacket::GetFrameNumber() const
{
return isstream ? Get16At(34) : 0;
}
void CPacket::SetFrameNumber(uint16_t fn)
{
if (isstream)
{
Set16At(34, fn);
}
}
bool CPacket::IsLastPacket() const
{
if (isstream and size)
return 0x8000u == (0x8000u & GetFrameNumber());
return true;
}
void CPacket::CalcCRC()
{
if (isstream)
{
Set16At(52, CRC.CalcCRC(data, 52));
}
else
{ // set the CRC for the LSF
Set16At(32, CRC.CalcCRC(data+4, 28));
// now for the payload
Set16At(size-2, CRC.CalcCRC(data+34, size-36));
}
}
const uint8_t *CPacket::GetCVoice() const
{
static uint8_t quiet[] { 0x01u, 0x00u, 0x09u, 0x43u, 0x9cu, 0xe4u, 0x21u, 0x08u, 0x01u, 0x00u, 0x09u, 0x43u, 0x9cu, 0xe4u, 0x21u, 0x08u };
return isstream ? data + 36 : quiet;
}
uint8_t *CPacket::GetVoice()
{
static uint8_t fake[16];
return isstream ? data+36 : fake;
}
uint16_t CPacket::GetCRC(bool first) const
{
uint16_t rv = 0u;
if (isstream)
rv = Get16At(52);
else
{
if (first)
rv = Get16At(32);
else
rv = Get16At(size-2);
}
return rv;
}
uint16_t CPacket::Get16At(size_t pos) const
{
return 0x100u * data[pos] + data[pos+1];
}
void CPacket::Set16At(size_t pos, uint16_t val)
{
data[pos++] = 0xffu & (val >> 8);
data[pos] = 0xffu & val;
}