-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
288 lines (263 loc) · 8.67 KB
/
util.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "Configuration.h"
#include "ultralcd.h"
#include "language.h"
#include "util.h"
// Allocate the version string in the program memory. Otherwise the string lands either on the stack or in the global RAM.
const char FW_VERSION_STR[] PROGMEM = FW_version;
const char* FW_VERSION_STR_P()
{
return FW_VERSION_STR;
}
const char FW_PRUSA3D_MAGIC_STR[] PROGMEM = FW_PRUSA3D_MAGIC;
const char* FW_PRUSA3D_MAGIC_STR_P()
{
return FW_PRUSA3D_MAGIC_STR;
}
const char STR_REVISION_DEV [] PROGMEM = "dev";
const char STR_REVISION_ALPHA[] PROGMEM = "alpha";
const char STR_REVISION_BETA [] PROGMEM = "beta";
const char STR_REVISION_RC [] PROGMEM = "rc";
inline bool is_whitespace_or_nl(char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == 'r';
}
inline bool is_whitespace_or_nl_or_eol(char c)
{
return c == 0 || c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
inline bool is_digit(char c)
{
return c >= '0' && c <= '9';
}
// Parse a major.minor.revision version number.
// Return true if valid.
inline bool parse_version(const char *str, uint16_t version[4])
{
#if 0
SERIAL_ECHOPGM("Parsing version string ");
SERIAL_ECHO(str);
SERIAL_ECHOLNPGM("");
#endif
const char *major = str;
const char *p = str;
while (is_digit(*p)) ++ p;
if (*p != '.')
return false;
const char *minor = ++ p;
while (is_digit(*p)) ++ p;
if (*p != '.')
return false;
const char *rev = ++ p;
while (is_digit(*p)) ++ p;
if (! is_whitespace_or_nl_or_eol(*p) && *p != '-')
return false;
char *endptr = NULL;
version[0] = strtol(major, &endptr, 10);
if (endptr != minor - 1)
return false;
version[1] = strtol(minor, &endptr, 10);
if (endptr != rev - 1)
return false;
version[2] = strtol(rev, &endptr, 10);
if (endptr != p)
return false;
version[3] = FIRMWARE_REVISION_RELEASED;
if (*p ++ == '-') {
const char *q = p;
while (! is_whitespace_or_nl_or_eol(*q))
++ q;
uint8_t n = q - p;
if (n == strlen_P(STR_REVISION_DEV) && strncmp_P(p, STR_REVISION_DEV, n) == 0)
version[3] = FIRMWARE_REVISION_DEV;
else if (n == strlen_P(STR_REVISION_ALPHA) && strncmp_P(p, STR_REVISION_ALPHA, n) == 0)
version[3] = FIRMWARE_REVISION_ALPHA;
else if (n == strlen_P(STR_REVISION_BETA) && strncmp_P(p, STR_REVISION_BETA, n) == 0)
version[3] = FIRMWARE_REVISION_BETA;
else if ((n == 2 || n == 3) && p[0] == 'r' && p[1] == 'c') {
if (n == 2)
version[3] = FIRMWARE_REVISION_RC;
else {
if (is_digit(p[2]))
version[3] = FIRMWARE_REVISION_RC + p[2] - '1';
else
return false;
}
} else
return false;
}
#if 0
SERIAL_ECHOPGM("Version parsed, major: ");
SERIAL_ECHO(version[0]);
SERIAL_ECHOPGM(", minor: ");
SERIAL_ECHO(version[1]);
SERIAL_ECHOPGM(", revision: ");
SERIAL_ECHO(version[2]);
SERIAL_ECHOPGM(", flavor: ");
SERIAL_ECHO(version[3]);
SERIAL_ECHOLNPGM("");
#endif
return true;
}
inline bool strncmp_PP(const char *p1, const char *p2, uint8_t n)
{
for (; n > 0; -- n, ++ p1, ++ p2) {
if (pgm_read_byte(p1) < pgm_read_byte(p2))
return -1;
if (pgm_read_byte(p1) > pgm_read_byte(p2))
return 1;
if (pgm_read_byte(p1) == 0)
return 0;
}
return 0;
}
// Parse a major.minor.revision version number.
// Return true if valid.
inline bool parse_version_P(const char *str, uint16_t version[4])
{
#if 0
SERIAL_ECHOPGM("Parsing version string ");
SERIAL_ECHORPGM(str);
SERIAL_ECHOLNPGM("");
#endif
const char *major = str;
const char *p = str;
while (is_digit(char(pgm_read_byte(p)))) ++ p;
if (pgm_read_byte(p) != '.')
return false;
const char *minor = ++ p;
while (is_digit(char(pgm_read_byte(p)))) ++ p;
if (pgm_read_byte(p) != '.')
return false;
const char *rev = ++ p;
while (is_digit(char(pgm_read_byte(p)))) ++ p;
if (! is_whitespace_or_nl_or_eol(char(pgm_read_byte(p))) && pgm_read_byte(p) != '-')
return false;
char buf[5];
uint8_t n = minor - major - 1;
if (n > 4)
return false;
memcpy_P(buf, major, n); buf[n] = 0;
char *endptr = NULL;
version[0] = strtol(buf, &endptr, 10);
if (*endptr != 0)
return false;
n = rev - minor - 1;
if (n > 4)
return false;
memcpy_P(buf, minor, n); buf[n] = 0;
version[1] = strtol(buf, &endptr, 10);
if (*endptr != 0)
return false;
n = p - rev;
if (n > 4)
return false;
memcpy_P(buf, rev, n);
buf[n] = 0;
version[2] = strtol(buf, &endptr, 10);
if (*endptr != 0)
return false;
version[3] = FIRMWARE_REVISION_RELEASED;
if (pgm_read_byte(p ++) == '-') {
const char *q = p;
while (! is_whitespace_or_nl_or_eol(char(pgm_read_byte(q))))
++ q;
n = q - p;
if (n == strlen_P(STR_REVISION_DEV) && strncmp_PP(p, STR_REVISION_DEV, n) == 0)
version[3] = FIRMWARE_REVISION_DEV;
else if (n == strlen_P(STR_REVISION_ALPHA) && strncmp_PP(p, STR_REVISION_ALPHA, n) == 0)
version[3] = FIRMWARE_REVISION_ALPHA;
else if (n == strlen_P(STR_REVISION_BETA) && strncmp_PP(p, STR_REVISION_BETA, n) == 0)
version[3] = FIRMWARE_REVISION_BETA;
else if ((n == 2 || n == 3) && strncmp_PP(p, STR_REVISION_RC, 2) == 0) {
if (n == 2)
version[3] = FIRMWARE_REVISION_RC;
else {
p += 2;
if (is_digit(pgm_read_byte(p)))
version[3] = FIRMWARE_REVISION_RC + pgm_read_byte(p) - '1';
else
return false;
}
} else
return false;
}
#if 0
SERIAL_ECHOPGM("Version parsed, major: ");
SERIAL_ECHO(version[0]);
SERIAL_ECHOPGM(", minor: ");
SERIAL_ECHO(version[1]);
SERIAL_ECHOPGM(", revision: ");
SERIAL_ECHO(version[2]);
SERIAL_ECHOPGM(", flavor: ");
SERIAL_ECHO(version[3]);
SERIAL_ECHOLNPGM("");
#endif
return true;
}
// 1 - yes, 0 - false, -1 - error;
inline int8_t is_provided_version_newer(const char *version_string)
{
uint16_t ver_gcode[3], ver_current[3];
if (! parse_version(version_string, ver_gcode))
return -1;
if (! parse_version_P(FW_VERSION_STR, ver_current))
return 0; // this shall not happen
for (uint8_t i = 0; i < 3; ++ i)
if (ver_gcode[i] > ver_current[i])
return 1;
return 0;
}
bool show_upgrade_dialog_if_version_newer(const char *version_string)
{
uint16_t ver_gcode[4], ver_current[4];
if (! parse_version(version_string, ver_gcode)) {
// SERIAL_PROTOCOLLNPGM("parse_version failed");
return false;
}
if (! parse_version_P(FW_VERSION_STR, ver_current)) {
// SERIAL_PROTOCOLLNPGM("parse_version_P failed");
return false; // this shall not happen
}
// SERIAL_PROTOCOLLNPGM("versions parsed");
bool upgrade = false;
for (uint8_t i = 0; i < 4; ++ i) {
if (ver_gcode[i] > ver_current[i]) {
upgrade = true;
break;
} else if (ver_gcode[i] < ver_current[i])
break;
}
if (upgrade) {
lcd_display_message_fullscreen_P(MSG_NEW_FIRMWARE_AVAILABLE);
lcd_print_at_PGM(0, 2, PSTR(""));
for (const char *c = version_string; ! is_whitespace_or_nl_or_eol(*c); ++ c)
lcd_implementation_write(*c);
lcd_print_at_PGM(0, 3, MSG_NEW_FIRMWARE_PLEASE_UPGRADE);
tone(BEEPER, 1000);
delay_keep_alive(50);
noTone(BEEPER);
delay_keep_alive(500);
tone(BEEPER, 1000);
delay_keep_alive(50);
noTone(BEEPER);
lcd_wait_for_click();
lcd_update_enable(true);
lcd_implementation_clear();
lcd_update();
}
// Succeeded.
return true;
}
void update_current_firmware_version_to_eeprom()
{
for (int8_t i = 0; i < FW_PRUSA3D_MAGIC_LEN; ++ i)
eeprom_update_byte((uint8_t*)(EEPROM_FIRMWARE_PRUSA_MAGIC+i), pgm_read_byte(FW_PRUSA3D_MAGIC_STR+i));
uint16_t ver_current[4];
if (parse_version_P(FW_VERSION_STR, ver_current)) {
eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MAJOR, ver_current[0]);
eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MINOR, ver_current[1]);
eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_REVISION, ver_current[2]);
// See FirmwareRevisionFlavorType for the definition of firmware flavors.
eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_FLAVOR, ver_current[3]);
}
}