-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDVTOOLFileReader.cpp
183 lines (142 loc) · 4.01 KB
/
DVTOOLFileReader.cpp
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (C) 2009,2013,2014 by Jonathan Naylor G4KLX
* Copyright (c) 2017 by 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 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 <cstring>
#include <arpa/inet.h>
#include "DVTOOLFileReader.h"
#include "DStarDefines.h"
static const char DVTOOL_SIGNATURE[] = "DVTOOL";
static const unsigned int DVTOOL_SIGNATURE_LENGTH = 6U;
static const char DSVT_SIGNATURE[] = "DSVT";
static const unsigned int DSVT_SIGNATURE_LENGTH = 4U;
static const unsigned int FIXED_DATA_LENGTH = 9U;
static const unsigned char HEADER_FLAG = 0x10;
static const unsigned char DATA_FLAG = 0x20;
static const unsigned char HEADER_MASK = 0x80;
static const unsigned char TRAILER_MASK = 0x40;
const unsigned int BUFFER_LENGTH = 255U;
CDVTOOLFileReader::CDVTOOLFileReader() :
m_fileName(),
m_file(NULL),
m_records(0U),
m_type(DVTFR_NONE),
m_buffer(NULL),
m_length(0U),
m_seqNo(0U)
{
m_buffer = new unsigned char[BUFFER_LENGTH];
}
CDVTOOLFileReader::~CDVTOOLFileReader()
{
delete[] m_buffer;
}
std::string CDVTOOLFileReader::getFileName() const
{
return m_fileName;
}
unsigned int CDVTOOLFileReader::getRecords() const
{
return m_records;
}
bool CDVTOOLFileReader::open(const std::string& fileName)
{
m_fileName = fileName;
m_file = fopen(fileName.c_str(), "rb");
if (NULL == m_file)
return false;
unsigned char buffer[DVTOOL_SIGNATURE_LENGTH];
size_t n = fread(buffer, 1, DVTOOL_SIGNATURE_LENGTH, m_file);
if (n != DVTOOL_SIGNATURE_LENGTH) {
fclose(m_file);
return false;
}
if (::memcmp(buffer, DVTOOL_SIGNATURE, DVTOOL_SIGNATURE_LENGTH) != 0) {
fclose(m_file);
return false;
}
uint32_t uint32;
n = fread(&uint32, sizeof(uint32_t), 1, m_file);
if (n != 1) {
fclose(m_file);
return false;
}
m_records = ntohl(uint32);
m_seqNo = 0U;
return true;
}
DVTFR_TYPE CDVTOOLFileReader::read()
{
uint16_t uint16;
size_t n = fread(&uint16, sizeof(uint16_t), 1, m_file);
if (n != 1)
return DVTFR_NONE;
m_length = htons(uint16) - 15U;
unsigned char bytes[FIXED_DATA_LENGTH];
n = fread(bytes, 1, DSVT_SIGNATURE_LENGTH, m_file);
if (n != DSVT_SIGNATURE_LENGTH)
return DVTFR_NONE;
if (::memcmp(bytes, DSVT_SIGNATURE, DSVT_SIGNATURE_LENGTH) != 0)
return DVTFR_NONE;
char flag;
n = fread(&flag, 1, 1, m_file);
if (n != 1)
return DVTFR_NONE;
m_type = (flag == HEADER_FLAG) ? DVTFR_HEADER : DVTFR_DATA;
n = fread(bytes, 1, FIXED_DATA_LENGTH, m_file);
if (n != FIXED_DATA_LENGTH)
return DVTFR_NONE;
n = fread(&flag, 1, 1, m_file);
if (n != 1)
return DVTFR_NONE;
if (m_type == DVTFR_DATA)
m_seqNo = flag;
n = fread(m_buffer, 1, m_length, m_file);
if (n != m_length)
return DVTFR_NONE;
return m_type;
}
CHeaderData* CDVTOOLFileReader::readHeader()
{
if (m_type != DVTFR_HEADER)
return NULL;
CHeaderData* header = new CHeaderData;
if (m_buffer[39U] == 0xFFU && m_buffer[40U] == 0xFFU) {
header->setDVTOOLData(m_buffer, RADIO_HEADER_LENGTH_BYTES, false);
return header;
}
// Header checksum testing is enabled
bool valid = header->setDVTOOLData(m_buffer, RADIO_HEADER_LENGTH_BYTES, true);
if (!valid) {
delete header;
return NULL;
}
return header;
}
CAMBEData* CDVTOOLFileReader::readData()
{
if (m_type != DVTFR_DATA)
return NULL;
CAMBEData* data = new CAMBEData;
data->setData(m_buffer, m_length);
data->setSeq(m_seqNo);
return data;
}
void CDVTOOLFileReader::close()
{
fclose(m_file);
}