forked from IntelRealSense/RealSenseID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialPacket.cc
111 lines (95 loc) · 2.79 KB
/
SerialPacket.cc
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
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2020-2021 Intel Corporation. All Rights Reserved.
#include "SerialPacket.h"
#include <stdexcept>
#include <string.h>
#include <cassert>
namespace RealSenseID
{
namespace PacketManager
{
SerialPacket::SerialPacket()
{
// zero all bytes before filling the relevant parts
::memset(this, 0, sizeof(SerialPacket));
// fill sync bytes
header.sync1 = SyncByte::Sync1;
header.sync2 = SyncByte::Sync2;
header.protocol_ver = ProtocolVer;
header.id = MsgId::None;
header.payload_size = 0;
}
static int AlignTo32Bytes(int size)
{
int mod = size % 32;
if (mod)
return size + (32 - size % 32);
return size;
}
//
// FaPacket impl
//
FaPacket::FaPacket(MsgId id, const char* user_id, char status)
{
header.id = id;
header.payload_size = static_cast<uint16_t>(AlignTo32Bytes(sizeof(payload.sequence_number) + sizeof(FaMessage)));
auto& fa_msg = payload.message.fa_msg;
constexpr size_t buffer_size = sizeof(fa_msg.user_id);
static_assert(buffer_size == (PacketManager::MaxUserIdSize + 1), "sizeof(fa_msg.user_id) != (MaxUserIdSize + 1)");
if (user_id != nullptr)
{
// store the user_id in a 31 bytes buffer (max 30 ascii chars + zero terminating byte(s))
::strncpy(fa_msg.user_id, user_id, buffer_size - 1);
fa_msg.user_id[buffer_size - 1] = '\0'; // always null terminated
}
fa_msg.fa_status = '0' + status;
assert(IsFaPacket(*this));
}
FaPacket::FaPacket(MsgId id) : FaPacket(id, nullptr, 0)
{
}
// translate to null terminated user id
const char* FaPacket::GetUserId() const
{
return payload.message.fa_msg.user_id;
}
char FaPacket::GetStatusCode()
{
return payload.message.fa_msg.fa_status - '0';
}
//
// DataPacket impl
//
DataPacket::DataPacket(MsgId id, char* data, size_t data_size)
{
header.id = id;
header.payload_size = static_cast<uint16_t>(AlignTo32Bytes(sizeof(payload.sequence_number) + (int)data_size));
uint32_t target_size = sizeof(payload.sequence_number) + sizeof(payload.message.data_msg.data);
if (header.payload_size > target_size)
{
throw std::runtime_error("DataPacket ctor: given size exceeds max allowed");
}
auto* target_ptr = payload.message.data_msg.data;
if (data != nullptr)
{
::memcpy(target_ptr, data, data_size);
}
assert(IsDataPacket(*this));
}
DataPacket::DataPacket(MsgId id) : DataPacket(id, nullptr, 0)
{
}
const DataMessage& DataPacket::Data() const
{
return payload.message.data_msg;
}
bool IsFaPacket(const SerialPacket& packet)
{
return packet.header.id >= MsgId::MinFa && packet.header.id <= MsgId::MaxFa;
}
bool IsDataPacket(const SerialPacket& packet)
{
return !IsFaPacket(packet);
}
} // namespace PacketManager
} // namespace RealSenseID