forked from MagicalTux/dlockd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDLockPacket.cpp
More file actions
66 lines (53 loc) · 1.36 KB
/
DLockPacket.cpp
File metadata and controls
66 lines (53 loc) · 1.36 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
#include "DLockPacket.hpp"
DLockPacket::DLockPacket(char opcode, QByteArray data) {
this->setOpcode(opcode);
this->setData(data);
// 2 nibbles = 1 byte :)
char flags = (version & 0xf) << 4 | (opcode & 0xf);
packet.append(flags);
// setting crc16 field to zero
quint8 empty = 0x00;
packet.append(empty);
packet.append(empty);
packet.append(data);
this->updateCrc();
}
DLockPacket DLockPacket::setOpcode(char opcode, bool updateCrc) {
this->opcode = opcode;
char flags = (version & 0xf) << 4 | (opcode & 0xf);
packet.replace(0, 1, &flags, 1);
if (updateCrc == true) {
this->updateCrc();
}
return *this;
}
DLockPacket DLockPacket::setData(QByteArray data, bool updateCrc) {
this->data = data;
packet.replace(3, data.length(), data.constData());
if (updateCrc == true) {
this->updateCrc();
}
return *this;
}
char DLockPacket::getOpcode() {
return this->opcode;
}
QByteArray DLockPacket::getData() {
return this->data;
}
void DLockPacket::updateCrc() {
// setting back to 0 first
char empty = 0x00;
packet.replace(1, 1, &empty, 1);
packet.replace(2, 1, &empty, 1);
// then generating the crc
quint16 crc = qChecksum(packet.constData(), packet.length());
char low = crc;
char high = (crc >> 8) & 0xff;
// finally updating it
packet.replace(1, 1, &high, 1);
packet.replace(2, 1, &low, 1);
}
QByteArray DLockPacket::getPacket() {
return packet;
}