-
Notifications
You must be signed in to change notification settings - Fork 13
/
auto-pickup.cpp
132 lines (113 loc) · 3.44 KB
/
auto-pickup.cpp
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
#include <iostream>
#include <string>
#include <cstdint>
#include <algorithm>
using namespace std;
#define ID101 5
#define HDR_LENGTH 3
#define ITM_LENGTH 4
class CG_PACKET
{
private:
uint8_t m_length; /*!< Length of the packet */
string m_str; /*!< String representation */
private:
void parseHeader(uint8_t& p_pos,
bool& p_okHdr,
string& p_txt) const
{
uint8_t l_ret(0);
if ( p_pos+1 >= HDR_LENGTH )
{
l_ret = ( ( ( (m_str[p_pos ] == '1') & 1) << 2 ) +
( ( (m_str[p_pos -1] == '1') & 1) << 1 ) +
( ( (m_str[p_pos -2] == '1') & 1) << 0 ) );
p_pos = (p_pos+1 == HDR_LENGTH) ? 0 : (p_pos - HDR_LENGTH);
}
else
{
p_pos = 0;
}
p_okHdr = (l_ret == ID101);
if ( p_okHdr )
{
p_txt += "001";
}
}
void parseLength(uint8_t& p_pos,
uint8_t& p_length,
bool p_okHdr,
string& p_txt) const
{
if ( p_pos+1 >= ITM_LENGTH )
{
p_length = ( ( ( (m_str[p_pos ] == '1') & 1) << 3 ) +
( ( (m_str[p_pos -1] == '1') & 1) << 2 ) +
( ( (m_str[p_pos -2] == '1') & 1) << 1 ) +
( ( (m_str[p_pos -3] == '1') & 1) << 0 ) );
p_pos =(p_pos+1 == ITM_LENGTH) ? 0 : (p_pos - ITM_LENGTH);
}
else
{
p_pos = 0;
}
if ( p_okHdr )
{
for ( int i = 0; i < ITM_LENGTH; i++ )
{
p_txt += ( p_length & (1 << (ITM_LENGTH -1 -i)) ) ? '1' : '0';
}
}
}
void parsePayLoad(uint8_t& p_pos,
uint8_t p_length,
bool p_okHdr,
string& p_txt) const
{
if ( p_pos+1 >= p_length )
{
if ( p_okHdr )
{
for ( uint8_t i = 0; i < p_length; i++ )
{
p_txt += m_str[p_pos -i];
}
}
p_pos = (p_pos+1 == p_length) ? 0 : (p_pos - p_length);
}
else
{
p_pos = 0;
}
}
public:
CG_PACKET(): m_length(0), m_str("") {}
// Receive function
void rcv(void)
{
int l_length;
cin >> l_length >> m_str; cin.ignore();
reverse(m_str.begin(), m_str.end());
m_length = static_cast<uint8_t>(l_length);
}
// Decode function
string dcd(void)
{
string l_ret;
uint8_t l_curPos(m_length - 1), l_length(0);
bool l_okHdr (false);
while( l_curPos > 0 )
{
parseHeader (l_curPos, l_okHdr, l_ret);
parseLength (l_curPos, l_length, l_okHdr, l_ret);
parsePayLoad(l_curPos, l_length, l_okHdr, l_ret);
}
return l_ret;
}
};
int main()
{
CG_PACKET packet;
packet.rcv();
cout << packet.dcd() << endl;
}