-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_builder.cpp
More file actions
51 lines (40 loc) · 1.25 KB
/
Copy pathcommand_builder.cpp
File metadata and controls
51 lines (40 loc) · 1.25 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
#include "command_builder.h"
#include "crc.h"
#include <vector>
#include <ctime>
#define SYNC_CMD 0xAA
#define TYPE_CMD 0x41
void append_uint16_be(std::vector<unsigned char>& buffer, uint16_t value)
{
buffer.push_back((value >> 8) & 0xFF);
buffer.push_back(value & 0xFF);
}
void append_uint32_be(std::vector<unsigned char>& buffer, uint32_t value)
{
buffer.push_back((value >> 24) & 0xFF);
buffer.push_back((value >> 16) & 0xFF);
buffer.push_back((value >> 8) & 0xFF);
buffer.push_back(value & 0xFF);
}
std::vector<unsigned char> build_command_frame(
uint16_t pmuId,
uint16_t commandCode)
{
std::vector<unsigned char> frame;
frame.push_back(SYNC_CMD);
frame.push_back(TYPE_CMD);
append_uint16_be(frame, 0); // placeholder frame size
append_uint16_be(frame, pmuId);
// SOC + FRACSEC
time_t now = time(nullptr);
append_uint32_be(frame, static_cast<uint32_t>(now));
append_uint32_be(frame, 0);
append_uint16_be(frame, commandCode);
// Now update frame size
uint16_t frameSize = frame.size() + 2; // + CRC
frame[2] = (frameSize >> 8) & 0xFF;
frame[3] = frameSize & 0xFF;
uint16_t crc = calculate_crc(frame.data(), frame.size());
append_uint16_be(frame, crc);
return frame;
}