util.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include "Configuration.h"
  2. #include "ultralcd.h"
  3. #include "language.h"
  4. #include "util.h"
  5. // Allocate the version string in the program memory. Otherwise the string lands either on the stack or in the global RAM.
  6. const char FW_VERSION_STR[] PROGMEM = FW_VERSION;
  7. const char* FW_VERSION_STR_P()
  8. {
  9. return FW_VERSION_STR;
  10. }
  11. const char FW_PRUSA3D_MAGIC_STR[] PROGMEM = FW_PRUSA3D_MAGIC;
  12. const char* FW_PRUSA3D_MAGIC_STR_P()
  13. {
  14. return FW_PRUSA3D_MAGIC_STR;
  15. }
  16. const char STR_REVISION_DEV [] PROGMEM = "dev";
  17. const char STR_REVISION_ALPHA[] PROGMEM = "alpha";
  18. const char STR_REVISION_BETA [] PROGMEM = "beta";
  19. const char STR_REVISION_RC [] PROGMEM = "rc";
  20. inline bool is_whitespace_or_nl(char c)
  21. {
  22. return c == ' ' || c == '\t' || c == '\n' || c == 'r';
  23. }
  24. inline bool is_whitespace_or_nl_or_eol(char c)
  25. {
  26. return c == 0 || c == ' ' || c == '\t' || c == '\n' || c == '\r';
  27. }
  28. inline bool is_digit(char c)
  29. {
  30. return c >= '0' && c <= '9';
  31. }
  32. // Parse a major.minor.revision version number.
  33. // Return true if valid.
  34. inline bool parse_version(const char *str, uint16_t version[4])
  35. {
  36. #if 0
  37. SERIAL_ECHOPGM("Parsing version string ");
  38. SERIAL_ECHO(str);
  39. SERIAL_ECHOLNPGM("");
  40. #endif
  41. const char *major = str;
  42. const char *p = str;
  43. while (is_digit(*p)) ++ p;
  44. if (*p != '.')
  45. return false;
  46. const char *minor = ++ p;
  47. while (is_digit(*p)) ++ p;
  48. if (*p != '.')
  49. return false;
  50. const char *rev = ++ p;
  51. while (is_digit(*p)) ++ p;
  52. if (! is_whitespace_or_nl_or_eol(*p) && *p != '-')
  53. return false;
  54. char *endptr = NULL;
  55. version[0] = strtol(major, &endptr, 10);
  56. if (endptr != minor - 1)
  57. return false;
  58. version[1] = strtol(minor, &endptr, 10);
  59. if (endptr != rev - 1)
  60. return false;
  61. version[2] = strtol(rev, &endptr, 10);
  62. if (endptr != p)
  63. return false;
  64. version[3] = FIRMWARE_REVISION_RELEASED;
  65. if (*p ++ == '-') {
  66. const char *q = p;
  67. while (! is_whitespace_or_nl_or_eol(*q))
  68. ++ q;
  69. uint8_t n = q - p;
  70. if (n == strlen_P(STR_REVISION_DEV) && strncmp_P(p, STR_REVISION_DEV, n) == 0)
  71. version[3] = FIRMWARE_REVISION_DEV;
  72. else if (n == strlen_P(STR_REVISION_ALPHA) && strncmp_P(p, STR_REVISION_ALPHA, n) == 0)
  73. version[3] = FIRMWARE_REVISION_ALPHA;
  74. else if (n == strlen_P(STR_REVISION_BETA) && strncmp_P(p, STR_REVISION_BETA, n) == 0)
  75. version[3] = FIRMWARE_REVISION_BETA;
  76. else if ((n == 2 || n == 3) && (p[0] == 'r' || p[0] == 'R') && (p[1] == 'c' || p[1] == 'C')) {
  77. if (n == 2)
  78. version[3] = FIRMWARE_REVISION_RC;
  79. else {
  80. if (is_digit(p[2]))
  81. version[3] = FIRMWARE_REVISION_RC + p[2] - '1';
  82. else
  83. return false;
  84. }
  85. } else
  86. return false;
  87. }
  88. #if 0
  89. SERIAL_ECHOPGM("Version parsed, major: ");
  90. SERIAL_ECHO(version[0]);
  91. SERIAL_ECHOPGM(", minor: ");
  92. SERIAL_ECHO(version[1]);
  93. SERIAL_ECHOPGM(", revision: ");
  94. SERIAL_ECHO(version[2]);
  95. SERIAL_ECHOPGM(", flavor: ");
  96. SERIAL_ECHO(version[3]);
  97. SERIAL_ECHOLNPGM("");
  98. #endif
  99. return true;
  100. }
  101. inline bool strncmp_PP(const char *p1, const char *p2, uint8_t n)
  102. {
  103. for (; n > 0; -- n, ++ p1, ++ p2) {
  104. if (pgm_read_byte(p1) >= 65 && pgm_read_byte(p1) <= 92) //p1 is upper case (p2 is always lowercase)
  105. {
  106. if ((pgm_read_byte(p1)+32) < pgm_read_byte(p2))
  107. return -1;
  108. if ((pgm_read_byte(p1)+32) > pgm_read_byte(p2))
  109. return 1;
  110. }
  111. else if (pgm_read_byte(p1) == 0) {
  112. return 0;
  113. }
  114. else { //p1 is lowercase
  115. if (pgm_read_byte(p1) < pgm_read_byte(p2))
  116. return -1;
  117. if (pgm_read_byte(p1) > pgm_read_byte(p2))
  118. return 1;
  119. }
  120. }
  121. return 0;
  122. }
  123. // Parse a major.minor.revision version number.
  124. // Return true if valid.
  125. inline bool parse_version_P(const char *str, uint16_t version[4])
  126. {
  127. #if 0
  128. SERIAL_ECHOPGM("Parsing version string ");
  129. SERIAL_ECHORPGM(str);
  130. SERIAL_ECHOLNPGM("");
  131. #endif
  132. const char *major = str;
  133. const char *p = str;
  134. while (is_digit(char(pgm_read_byte(p)))) ++ p;
  135. if (pgm_read_byte(p) != '.')
  136. return false;
  137. const char *minor = ++ p;
  138. while (is_digit(char(pgm_read_byte(p)))) ++ p;
  139. if (pgm_read_byte(p) != '.')
  140. return false;
  141. const char *rev = ++ p;
  142. while (is_digit(char(pgm_read_byte(p)))) ++ p;
  143. if (! is_whitespace_or_nl_or_eol(char(pgm_read_byte(p))) && pgm_read_byte(p) != '-')
  144. return false;
  145. char buf[5];
  146. uint8_t n = minor - major - 1;
  147. if (n > 4)
  148. return false;
  149. memcpy_P(buf, major, n); buf[n] = 0;
  150. char *endptr = NULL;
  151. version[0] = strtol(buf, &endptr, 10);
  152. if (*endptr != 0)
  153. return false;
  154. n = rev - minor - 1;
  155. if (n > 4)
  156. return false;
  157. memcpy_P(buf, minor, n); buf[n] = 0;
  158. version[1] = strtol(buf, &endptr, 10);
  159. if (*endptr != 0)
  160. return false;
  161. n = p - rev;
  162. if (n > 4)
  163. return false;
  164. memcpy_P(buf, rev, n);
  165. buf[n] = 0;
  166. version[2] = strtol(buf, &endptr, 10);
  167. if (*endptr != 0)
  168. return false;
  169. version[3] = FIRMWARE_REVISION_RELEASED;
  170. if (pgm_read_byte(p ++) == '-') {
  171. const char *q = p;
  172. while (! is_whitespace_or_nl_or_eol(char(pgm_read_byte(q))))
  173. ++ q;
  174. n = q - p;
  175. if (n == strlen_P(STR_REVISION_DEV) && strncmp_PP(p, STR_REVISION_DEV, n) == 0)
  176. version[3] = FIRMWARE_REVISION_DEV;
  177. else if (n == strlen_P(STR_REVISION_ALPHA) && strncmp_PP(p, STR_REVISION_ALPHA, n) == 0)
  178. version[3] = FIRMWARE_REVISION_ALPHA;
  179. else if (n == strlen_P(STR_REVISION_BETA) && strncmp_PP(p, STR_REVISION_BETA, n) == 0)
  180. version[3] = FIRMWARE_REVISION_BETA;
  181. else if ((n == 2 || n == 3) && strncmp_PP(p, STR_REVISION_RC, 2) == 0) {
  182. if (n == 2)
  183. version[3] = FIRMWARE_REVISION_RC;
  184. else {
  185. p += 2;
  186. if (is_digit(pgm_read_byte(p)))
  187. version[3] = FIRMWARE_REVISION_RC + pgm_read_byte(p) - '1';
  188. else
  189. return false;
  190. }
  191. } else
  192. return false;
  193. }
  194. #if 0
  195. SERIAL_ECHOPGM("Version parsed, major: ");
  196. SERIAL_ECHO(version[0]);
  197. SERIAL_ECHOPGM(", minor: ");
  198. SERIAL_ECHO(version[1]);
  199. SERIAL_ECHOPGM(", revision: ");
  200. SERIAL_ECHO(version[2]);
  201. SERIAL_ECHOPGM(", flavor: ");
  202. SERIAL_ECHO(version[3]);
  203. SERIAL_ECHOLNPGM("");
  204. #endif
  205. return true;
  206. }
  207. // 1 - yes, 0 - false, -1 - error;
  208. inline int8_t is_provided_version_newer(const char *version_string)
  209. {
  210. uint16_t ver_gcode[3], ver_current[3];
  211. if (! parse_version(version_string, ver_gcode))
  212. return -1;
  213. if (! parse_version_P(FW_VERSION_STR, ver_current))
  214. return 0; // this shall not happen
  215. for (uint8_t i = 0; i < 3; ++ i)
  216. if (ver_gcode[i] > ver_current[i])
  217. return 1;
  218. return 0;
  219. }
  220. bool force_selftest_if_fw_version()
  221. {
  222. //if fw version used before flashing new firmware (fw version currently stored in eeprom) is lower then 3.1.2-RC2, function returns true to force selftest
  223. uint16_t ver_eeprom[4];
  224. uint16_t ver_with_calibration[4] = {3, 1, 2, 4}; //hardcoded 3.1.2-RC2 version
  225. bool force_selftest = false;
  226. ver_eeprom[0] = eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MAJOR);
  227. ver_eeprom[1] = eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MINOR);
  228. ver_eeprom[2] = eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_REVISION);
  229. ver_eeprom[3] = eeprom_read_word((uint16_t*)EEPROM_FIRMWARE_VERSION_FLAVOR);
  230. for (uint8_t i = 0; i < 4; ++i) {
  231. if (ver_with_calibration[i] > ver_eeprom[i]) {
  232. force_selftest = true;
  233. break;
  234. }
  235. else if (ver_with_calibration[i] < ver_eeprom[i])
  236. break;
  237. }
  238. //force selftest also in case that version used before flashing new firmware was 3.2.0-RC1
  239. if ((ver_eeprom[0] == 3) && (ver_eeprom[1] == 2) && (ver_eeprom[2] == 0) && (ver_eeprom[3] == 3)) force_selftest = true;
  240. return force_selftest;
  241. }
  242. bool show_upgrade_dialog_if_version_newer(const char *version_string)
  243. {
  244. uint16_t ver_gcode[4], ver_current[4];
  245. if (! parse_version(version_string, ver_gcode)) {
  246. // SERIAL_PROTOCOLLNPGM("parse_version failed");
  247. return false;
  248. }
  249. if (! parse_version_P(FW_VERSION_STR, ver_current)) {
  250. // SERIAL_PROTOCOLLNPGM("parse_version_P failed");
  251. return false; // this shall not happen
  252. }
  253. // SERIAL_PROTOCOLLNPGM("versions parsed");
  254. bool upgrade = false;
  255. for (uint8_t i = 0; i < 4; ++ i) {
  256. if (ver_gcode[i] > ver_current[i]) {
  257. upgrade = true;
  258. break;
  259. } else if (ver_gcode[i] < ver_current[i])
  260. break;
  261. }
  262. if (upgrade) {
  263. lcd_display_message_fullscreen_P(MSG_NEW_FIRMWARE_AVAILABLE);
  264. lcd_print_at_PGM(0, 2, PSTR(""));
  265. for (const char *c = version_string; ! is_whitespace_or_nl_or_eol(*c); ++ c)
  266. lcd_implementation_write(*c);
  267. lcd_print_at_PGM(0, 3, MSG_NEW_FIRMWARE_PLEASE_UPGRADE);
  268. tone(BEEPER, 1000);
  269. delay_keep_alive(50);
  270. noTone(BEEPER);
  271. delay_keep_alive(500);
  272. tone(BEEPER, 1000);
  273. delay_keep_alive(50);
  274. noTone(BEEPER);
  275. lcd_wait_for_click();
  276. lcd_update_enable(true);
  277. lcd_implementation_clear();
  278. lcd_update();
  279. }
  280. // Succeeded.
  281. return true;
  282. }
  283. void update_current_firmware_version_to_eeprom()
  284. {
  285. for (int8_t i = 0; i < FW_PRUSA3D_MAGIC_LEN; ++ i)
  286. eeprom_update_byte((uint8_t*)(EEPROM_FIRMWARE_PRUSA_MAGIC+i), pgm_read_byte(FW_PRUSA3D_MAGIC_STR+i));
  287. uint16_t ver_current[4];
  288. if (parse_version_P(FW_VERSION_STR, ver_current)) {
  289. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MAJOR, ver_current[0]);
  290. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MINOR, ver_current[1]);
  291. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_REVISION, ver_current[2]);
  292. // See FirmwareRevisionFlavorType for the definition of firmware flavors.
  293. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_FLAVOR, ver_current[3]);
  294. }
  295. }