Repository navigation
Expand file tree
/
Copy pathhtra_protocol.cpp
More file actions
180 lines (148 loc) · 5.01 KB
/
Copy pathhtra_protocol.cpp
File metadata and controls
180 lines (148 loc) · 5.01 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "htra_protocol.h"
#include "devices/i_htra_device.h"
#include <QDataStream>
#include <QIODevice>
namespace {
quint8 expectedPayloadLength(quint8 cmdId)
{
switch(cmdId) {
case HTRA_CMD::CENTER_FREQ:
case HTRA_CMD::LEVEL:
case HTRA_CMD::SPAN:
case HTRA_CMD::RBW:
case HTRA_CMD::VBW:
case HTRA_CMD::PICK_SEARCH_CENTER:
case HTRA_CMD::PICK_SEARCH_WIDTH:
return 4;
case HTRA_CMD::PICK:
case HTRA_CMD::PICK_SEARCH_TYPE:
case HTRA_CMD::PICK_SEARCH_FULL_SPAN:
case HTRA_CMD::RF1:
case HTRA_CMD::RF2:
case HTRA_CMD::RF3:
case HTRA_CMD::RF4:
case HTRA_CMD::RFoff:
return 1;
default:
return 0;
}
}
QByteArray floatResponse(quint8 cmdId, double value)
{
QByteArray response;
QDataStream out(&response, QIODevice::WriteOnly);
out.setByteOrder(QDataStream::LittleEndian);
out.setFloatingPointPrecision(QDataStream::SinglePrecision);
out << HTRA_HEADER << cmdId << quint8(4) << static_cast<float>(value);
return response;
}
QByteArray byteResponse(quint8 cmdId, quint8 value)
{
QByteArray response;
QDataStream out(&response, QIODevice::WriteOnly);
out.setByteOrder(QDataStream::LittleEndian);
out << HTRA_HEADER << cmdId << quint8(1) << value;
return response;
}
} // namespace
bool HtraProtocol::takePacket(QByteArray &buffer, QByteArray &packet)
{
while(true) {
const int headerIndex = buffer.indexOf(char(HTRA_HEADER));
if(headerIndex < 0) {
buffer.clear();
return false;
}
if(headerIndex > 0) {
buffer.remove(0, headerIndex);
}
if(buffer.size() < 3) {
return false;
}
const auto cmdId = static_cast<quint8>(buffer.at(1));
const auto payloadLength = static_cast<quint8>(buffer.at(2));
const quint8 expectedLength = expectedPayloadLength(cmdId);
if(expectedLength == 0 || payloadLength != expectedLength) {
buffer.remove(0, 1);
continue;
}
const int packetLength = 3 + payloadLength;
if(buffer.size() < packetLength) {
return false;
}
packet = buffer.left(packetLength);
buffer.remove(0, packetLength);
return true;
}
}
QByteArray HtraProtocol::processCommand(const QByteArray &packet,
IHtraDevice &device,
const RfCommandHandler &rfHandler)
{
if(packet.size() < 3 || static_cast<quint8>(packet.at(0)) != HTRA_HEADER) {
return {};
}
const auto cmdId = static_cast<quint8>(packet.at(1));
const auto payloadLength = static_cast<quint8>(packet.at(2));
const quint8 expectedLength = expectedPayloadLength(cmdId);
if(expectedLength == 0 || payloadLength != expectedLength ||
packet.size() != 3 + payloadLength) {
return {};
}
QByteArray payload = packet.mid(3);
QDataStream in(&payload, QIODevice::ReadOnly);
in.setByteOrder(QDataStream::LittleEndian);
quint32 value = 0;
if(payloadLength == 4) {
in >> value;
} else {
quint8 byteValue = 0;
in >> byteValue;
value = byteValue;
}
if(in.status() != QDataStream::Ok) {
return {};
}
switch(cmdId) {
case HTRA_CMD::CENTER_FREQ:
device.onSetCenter(value);
return floatResponse(cmdId, device.centerFreq());
case HTRA_CMD::LEVEL:
device.onSetLevel(value);
return floatResponse(cmdId, device.level());
case HTRA_CMD::SPAN:
device.onSetSpan(value);
return floatResponse(cmdId, device.span());
case HTRA_CMD::RBW:
device.onSetRbw(value);
return floatResponse(cmdId, device.rbw());
case HTRA_CMD::VBW:
device.onSetVbw(value);
return floatResponse(cmdId, device.vbw());
case HTRA_CMD::PICK:
return floatResponse(cmdId, device.pick());
case HTRA_CMD::PICK_SEARCH_TYPE:
device.onSetPickSearchType(static_cast<quint8>(value));
return floatResponse(cmdId, device.pickSearchType());
case HTRA_CMD::PICK_SEARCH_CENTER:
device.onSetPickSearchCenter(value);
return floatResponse(cmdId, device.pickSearchCenter());
case HTRA_CMD::PICK_SEARCH_WIDTH:
device.onSetPickSearchWidth(value);
return floatResponse(cmdId, device.pickSearchWidth());
case HTRA_CMD::PICK_SEARCH_FULL_SPAN:
device.onSetPickSearchFullSpan();
return byteResponse(cmdId, 1);
case HTRA_CMD::RF1:
case HTRA_CMD::RF2:
case HTRA_CMD::RF3:
case HTRA_CMD::RF4:
case HTRA_CMD::RFoff:
if(rfHandler) {
rfHandler(cmdId);
}
return byteResponse(cmdId,
cmdId == HTRA_CMD::RFoff ? 0 : cmdId - 0x10);
}
return {};
}