blob: 216b82913a5177c6823e0ac7d3e976c14a22abc7 (
plain)
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
198
|
#include <SimplePacket.h>
#include <avr/io.h>
SimplePacket::SimplePacket(txFuncPtr myPtr)
{
txFunc = myPtr;
init();
}
void SimplePacket::init()
{
//zero out our data arrays.
for (rx_length = 0; rx_length < MAX_PACKET_LENGTH; rx_length++)
rx_data[rx_length] = 0;
for (tx_length = 0; tx_length < MAX_PACKET_LENGTH; tx_length++)
tx_data[tx_length] = 0;
//init our other variables.
state = PS_START;
response_code = RC_OK;
target_length = 0;
rx_length = 0;
rx_crc = 0;
tx_length = 0;
tx_crc = 0;
}
//process a byte from our packet
void SimplePacket::process_byte(uint8_t b)
{
if (state == PS_START) // process start byte
{
//cool! its the start of a packet.
if (b == START_BYTE)
{
init();
state = PS_LEN;
}
else
{
// throw an error message?
// nah, ignore it as garbage.
// FIXME: Consider reporting such error in a special debug mode
}
}
else if (state == PS_LEN) // process length byte
{
target_length = b;
rx_length = 0;
state = PS_PAYLOAD;
if (target_length > MAX_PACKET_LENGTH)
response_code = RC_PACKET_TOO_BIG;
}
else if (state == PS_PAYLOAD) // process payload byte
{
//just keep reading bytes while we got them.
if (rx_length < target_length)
{
//keep track of CRC.
rx_crc = _crc_ibutton_update(rx_crc, b);
//will it fit?
if (rx_length < MAX_PACKET_LENGTH)
rx_data[rx_length] = b;
//keep track.
rx_length++;
}
//are we done?
if (rx_length >= target_length)
state = PS_CRC;
}
else if (state == PS_CRC) // check crc
{
// did the packet check out?
if (rx_crc != b)
response_code = RC_CRC_MISMATCH;
//okay, the packet is done.
state = PS_LAST;
}
}
bool SimplePacket::isFinished()
{
return (state == PS_LAST);
}
uint8_t SimplePacket::getLength()
{
return rx_length;
}
PacketState SimplePacket::getState()
{
return state;
}
ResponseCode SimplePacket::getResponseCode()
{
return response_code;
}
void SimplePacket::unsupported()
{
response_code = RC_CMD_UNSUPPORTED;
}
void SimplePacket::overflow()
{
response_code = RC_BUFFER_OVERFLOW;
}
void SimplePacket::sendReply()
{
//initialize our response CRC
tx_crc = 0;
tx_crc = _crc_ibutton_update(tx_crc, response_code);
//actually send our response.
transmit(START_BYTE);
transmit(tx_length+1);
transmit(response_code);
//loop through our reply packet payload and send it.
for (uint8_t i=0; i<tx_length; i++)
{
transmit(tx_data[i]);
tx_crc = _crc_ibutton_update(tx_crc, tx_data[i]);
}
//okay, send our CRC.
transmit(tx_crc);
//okay, now reset.
init();
}
void SimplePacket::sendPacket()
{
tx_crc = 0;
transmit(START_BYTE);
transmit(tx_length);
//loop through our reply packet payload and send it.
for (uint8_t i=0; i<tx_length; i++)
{
transmit(tx_data[i]);
tx_crc = _crc_ibutton_update(tx_crc, tx_data[i]);
}
//okay, send our CRC.
transmit(tx_crc);
}
void SimplePacket::transmit(uint8_t d)
{
txFunc(d);
}
//add a four byte chunk of data to our reply
void SimplePacket::add_32(uint32_t d)
{
add_16(d);
add_16(d >> 16);
}
//add a two byte chunk of data to our reply
void SimplePacket::add_16(uint16_t d)
{
add_8(d & 0xff);
add_8(d >> 8);
}
//add a byte to our reply.
void SimplePacket::add_8(uint8_t d)
{
//only add it if it will fit.
if (tx_length < MAX_PACKET_LENGTH)
tx_data[tx_length++] = d;
}
uint8_t SimplePacket::get_8(uint8_t idx)
{
return rx_data[idx];
}
uint16_t SimplePacket::get_16(uint8_t idx)
{
return (((int16_t)get_8(idx+1)) << 8) | get_8(idx);
}
uint32_t SimplePacket::get_32(uint8_t idx)
{
return (((uint32_t)get_16(idx+2)) << 16) | get_16(idx);
}
|