-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathprotocol-helpers.hpp
197 lines (181 loc) · 4.78 KB
/
protocol-helpers.hpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* Pan Tilt Zoom camera instance
*
* Copyright 2020 Grant Likely <[email protected]>
*
* SPDX-License-Identifier: GPLv2
*/
#pragma once
#include <QObject>
#include <QTimer>
#include <QSerialPort>
#include <obs.hpp>
/*
* Datagram field encoding helpers
*/
class datagram_field {
public:
const char *name;
int offset;
datagram_field(const char *name, int offset) : name(name), offset(offset) { }
virtual void encode(QByteArray &msg, int val) = 0;
virtual bool decode(OBSData data, QByteArray &msg) = 0;
};
class bool_field : public datagram_field {
public:
const unsigned int mask;
bool_field(const char *name, unsigned offset, unsigned int mask) :
datagram_field(name, offset), mask(mask) { }
void encode(QByteArray &msg, int val) {
unsigned int encoded = 0;
unsigned int current_bit = 0;
if (msg.size() < offset + 1)
return;
msg[offset] = (msg[offset] & ~mask) | (val ? mask : 0);
}
bool decode(OBSData data, QByteArray &msg) {
if (msg.size() < offset + 1)
return false;
obs_data_set_bool(data, name, (msg[offset] & mask) != 0);
return true;
}
};
class int_field : public datagram_field {
public:
const unsigned int mask;
int size, extend_mask = 0;
int_field(const char *name, unsigned offset, unsigned int mask, bool signextend = false) :
datagram_field(name, offset), mask(mask)
{
// Calculate number of bytes in the value
unsigned int wm = mask;
size = 0;
while (wm) {
wm >>= 8;
size++;
}
// Calculate the mask for sign extending
if (signextend) {
int bitcount = 0;
wm = mask;
while (wm) {
wm &= wm - 1;
bitcount++;
}
extend_mask = 1U << (bitcount - 1);
}
}
void encode(QByteArray &msg, int val) {
unsigned int encoded = 0;
unsigned int current_bit = 0;
unsigned int wm;
if (msg.size() < offset + size)
return;
for (wm = mask; wm; wm = wm >> 1, current_bit++) {
if (wm & 1) {
encoded |= (val & 1) << current_bit;
val = val >> 1;
}
}
for (int i = size - 1, wm = mask; i >= 0; i--) {
msg[offset + i] = 0xff & ((~wm & msg[offset + i]) | encoded);
wm >>= 8;
encoded >>= 8;
}
}
bool decode_int(int *val_, QByteArray &msg) {
unsigned int encoded = 0;
int val = 0;
unsigned int current_bit = 0;
if (msg.size() < offset + size)
return false;
for (int i = 0; i < size; i++)
encoded = encoded << 8 | msg[offset+i];
for (unsigned int wm = mask; wm; wm >>= 1, encoded >>= 1) {
if (wm & 1) {
val |= (encoded & 1) << current_bit;
current_bit++;
}
}
*val_ = (val ^ extend_mask) - extend_mask;
return true;
}
bool decode(OBSData data, QByteArray &msg) {
int val;
bool rc = decode_int(&val, msg);
if (rc)
obs_data_set_int(data, name, val);
return rc;
}
};
class string_lookup_field : public int_field {
public:
const QMap<int, std::string> *lookup;
string_lookup_field(const char *name, const QMap<int, std::string> &lookuptable,
unsigned offset, unsigned int mask, bool signextend = false) :
int_field(name, offset, mask, signextend)
{
lookup = &lookuptable;
}
bool decode(OBSData data, QByteArray &msg) {
int val;
bool rc = decode_int(&val, msg);
if (!rc)
return false;
obs_data_set_string(data, name, lookup->value(val, "Unknown").c_str());
return true;
}
};
class PTZCmd {
public:
QByteArray cmd;
QList<datagram_field*> args;
QList<datagram_field*> results;
PTZCmd(const char *cmd_hex) : cmd(QByteArray::fromHex(cmd_hex)) { }
PTZCmd(const char *cmd_hex, QList<datagram_field*> args) :
cmd(QByteArray::fromHex(cmd_hex)), args(args) { }
PTZCmd(const char *cmd_hex, QList<datagram_field*> args, QList<datagram_field*> rslts) :
cmd(QByteArray::fromHex(cmd_hex)), args(args), results(rslts) { }
void encode(QList<int> arglist) {
for (int i = 0; i < arglist.size() && i < args.size(); i++)
args[i]->encode(cmd, arglist[i]);
}
obs_data_t *decode(QByteArray msg) {
obs_data_t *data = obs_data_create();
for (auto field : results)
field->decode(data, msg);
return data;
}
};
class PTZInq : public PTZCmd {
public:
PTZInq(const char *cmd_hex) : PTZCmd(cmd_hex) { }
PTZInq(const char *cmd_hex, QList<datagram_field*> rslts) :
PTZCmd(cmd_hex, {}, rslts) {}
};
/*
* Protocol UART wrapper abstract base class
*/
class PTZUARTWrapper : public QObject {
Q_OBJECT
protected:
QString port_name;
QSerialPort uart;
QByteArray rxbuffer;
signals:
void receive(const QByteArray &packet);
void reset();
public:
PTZUARTWrapper(QString &port_name);
virtual bool open();
void close();
void setBaudRate(int baudRate);
int baudRate();
virtual void setConfig(OBSData config);
virtual OBSData getConfig();
virtual void addOBSProperties(obs_properties_t *props);
virtual void send(const QByteArray &packet);
virtual void receiveBytes(const QByteArray &bytes) = 0;
QString portName() { return port_name; }
public slots:
void poll();
};