-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPTPP.cpp
178 lines (154 loc) · 3.59 KB
/
SPTPP.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
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
#include "SPTPP.h"
SPTPP::SPTPP(uint8_t* obj, int size, unsigned long timeout)
{
this->obj = obj;
this->size = size;
this->timeout = timeout;
buffer = new uint8_t[size + 5];
writeBuffer = new uint8_t[size + 5];
pos = 0;
}
void SPTPP::GetedByte(uint8_t b)
{
unsigned long act = millis();
if (pos >= size + 5 || act - lastTime > timeout)
pos = 0;
lastTime = act;
if (pos == 0)
{
if (b == 0xf0 || b == 0xaa)
{
buffer[0] = b;
pos = 1;
}
}
else if (buffer[0] == 0xaa)
{
buffer[pos++] = b;
if (pos >= 5)
RunRead();
}
else if (buffer[0] == 0xf0)
{
buffer[pos++] = b;
if (pos == 2)
actSize = b + 5;
else if (pos >= actSize)
RunWrite();
}
}
int SPTPP::CheckFletcher16( uint8_t *data, int count )
{
uint16_t sum1 = 0;
uint16_t sum2 = 0;
for(int index = 0; index < count; ++index )
{
sum1 = (sum1 + data[index]) % 255;
sum2 = (sum2 + sum1) % 255;
}
return data[count] == (uint8_t)sum1 && data[count + 1] == (uint8_t)sum2;
}
uint8_t* SPTPP::Fletcher16( uint8_t *data, int count )
{
uint16_t sum1 = 0;
uint16_t sum2 = 0;
for(int index = 0; index < count; ++index )
{
sum1 = (sum1 + data[index]) % 255;
sum2 = (sum2 + sum1) % 255;
}
data[count] = (uint8_t)sum1;
data[count + 1] = (uint8_t)sum2;
}
void SPTPP::RunRead()
{
if (CheckFletcher16(buffer, 3))
{
int adr = buffer[1];
int count = buffer[2];
if (adr + count < size)
{
writeBuffer[0] = 0x55;
writeBuffer[1] = buffer[2];
for (int i = 0; i < count; ++i)
writeBuffer[2 + i] = obj[adr + i];
Fletcher16(writeBuffer, count + 2);
Serial.write(writeBuffer, count + 4);
Serial.flush();
}
else
SendReadError();
SetNewBuffer(5);
}
else
CheckNextBuffPosition();
}
void SPTPP::RunWrite()
{
if (CheckFletcher16(buffer, buffer[1] + 3))
{
int adr = buffer[1];
int count = buffer[2];
if (adr + count < size)
{
for (int i = 0; i < count; ++i)
obj[adr + i] = buffer[3 + i];
writeBuffer[0] = 0x0f;
writeBuffer[1] = buffer[2];
Fletcher16(writeBuffer, 2);
Serial.write(writeBuffer, 4);
Serial.flush();
}
else
SendWriteError();
SetNewBuffer(buffer[1] + 5);
}
else
CheckNextBuffPosition();
}
void SPTPP::CheckNextBuffPosition()
{
for (int i = 1; i < pos; ++i)
{
if (buffer[i] == 0xf0)
{
SetNewBuffer(i);
if (pos > 1 && buffer[1] + 5 >= pos)
RunWrite();
return;
}
else if (i >= pos - 5 && buffer[i] == 0xaa)
{
SetNewBuffer(i);
if (pos == 5)
RunRead();
return;
}
}
}
void SPTPP::SetNewBuffer(int p)
{
for (int i = 0; i < pos - p; ++i)
buffer[i] = buffer[p + i];
pos -= p;
if (pos < 0)
pos = 0;
}
void SPTPP::SendReadError()
{
writeBuffer[0] = 0x55;
writeBuffer[1] = 0x00;
writeBuffer[2] = 0x55;
writeBuffer[3] = 0xAA;
Serial.write(writeBuffer, 4);
Serial.flush();
}
void SPTPP::SendWriteError()
{
writeBuffer[0] = 0x0F;
writeBuffer[1] = 0x00;
writeBuffer[2] = 0x0F;
writeBuffer[3] = 0x1E;
Serial.write(writeBuffer, 4);
Serial.flush();
}