util.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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[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) < pgm_read_byte(p2))
  105. return -1;
  106. if (pgm_read_byte(p1) > pgm_read_byte(p2))
  107. return 1;
  108. if (pgm_read_byte(p1) == 0)
  109. return 0;
  110. }
  111. return 0;
  112. }
  113. // Parse a major.minor.revision version number.
  114. // Return true if valid.
  115. inline bool parse_version_P(const char *str, uint16_t version[4])
  116. {
  117. #if 0
  118. SERIAL_ECHOPGM("Parsing version string ");
  119. SERIAL_ECHORPGM(str);
  120. SERIAL_ECHOLNPGM("");
  121. #endif
  122. const char *major = str;
  123. const char *p = str;
  124. while (is_digit(char(pgm_read_byte(p)))) ++ p;
  125. if (pgm_read_byte(p) != '.')
  126. return false;
  127. const char *minor = ++ p;
  128. while (is_digit(char(pgm_read_byte(p)))) ++ p;
  129. if (pgm_read_byte(p) != '.')
  130. return false;
  131. const char *rev = ++ p;
  132. while (is_digit(char(pgm_read_byte(p)))) ++ p;
  133. if (! is_whitespace_or_nl_or_eol(char(pgm_read_byte(p))) && pgm_read_byte(p) != '-')
  134. return false;
  135. char buf[5];
  136. uint8_t n = minor - major - 1;
  137. if (n > 4)
  138. return false;
  139. memcpy_P(buf, major, n); buf[n] = 0;
  140. char *endptr = NULL;
  141. version[0] = strtol(buf, &endptr, 10);
  142. if (*endptr != 0)
  143. return false;
  144. n = rev - minor - 1;
  145. if (n > 4)
  146. return false;
  147. memcpy_P(buf, minor, n); buf[n] = 0;
  148. version[1] = strtol(buf, &endptr, 10);
  149. if (*endptr != 0)
  150. return false;
  151. n = p - rev;
  152. if (n > 4)
  153. return false;
  154. memcpy_P(buf, rev, n);
  155. buf[n] = 0;
  156. version[2] = strtol(buf, &endptr, 10);
  157. if (*endptr != 0)
  158. return false;
  159. version[3] = FIRMWARE_REVISION_RELEASED;
  160. if (pgm_read_byte(p ++) == '-') {
  161. const char *q = p;
  162. while (! is_whitespace_or_nl_or_eol(char(pgm_read_byte(q))))
  163. ++ q;
  164. n = q - p;
  165. if (n == strlen_P(STR_REVISION_DEV) && strncmp_PP(p, STR_REVISION_DEV, n) == 0)
  166. version[3] = FIRMWARE_REVISION_DEV;
  167. else if (n == strlen_P(STR_REVISION_ALPHA) && strncmp_PP(p, STR_REVISION_ALPHA, n) == 0)
  168. version[3] = FIRMWARE_REVISION_ALPHA;
  169. else if (n == strlen_P(STR_REVISION_BETA) && strncmp_PP(p, STR_REVISION_BETA, n) == 0)
  170. version[3] = FIRMWARE_REVISION_BETA;
  171. else if ((n == 2 || n == 3) && strncmp_PP(p, STR_REVISION_RC, 2) == 0) {
  172. if (n == 2)
  173. version[3] = FIRMWARE_REVISION_RC;
  174. else {
  175. p += 2;
  176. if (is_digit(pgm_read_byte(p)))
  177. version[3] = FIRMWARE_REVISION_RC + pgm_read_byte(p) - '1';
  178. else
  179. return false;
  180. }
  181. } else
  182. return false;
  183. }
  184. #if 0
  185. SERIAL_ECHOPGM("Version parsed, major: ");
  186. SERIAL_ECHO(version[0]);
  187. SERIAL_ECHOPGM(", minor: ");
  188. SERIAL_ECHO(version[1]);
  189. SERIAL_ECHOPGM(", revision: ");
  190. SERIAL_ECHO(version[2]);
  191. SERIAL_ECHOPGM(", flavor: ");
  192. SERIAL_ECHO(version[3]);
  193. SERIAL_ECHOLNPGM("");
  194. #endif
  195. return true;
  196. }
  197. // 1 - yes, 0 - false, -1 - error;
  198. inline int8_t is_provided_version_newer(const char *version_string)
  199. {
  200. uint16_t ver_gcode[3], ver_current[3];
  201. if (! parse_version(version_string, ver_gcode))
  202. return -1;
  203. if (! parse_version_P(FW_VERSION_STR, ver_current))
  204. return 0; // this shall not happen
  205. for (uint8_t i = 0; i < 3; ++ i)
  206. if (ver_gcode[i] > ver_current[i])
  207. return 1;
  208. return 0;
  209. }
  210. bool show_upgrade_dialog_if_version_newer(const char *version_string)
  211. {
  212. uint16_t ver_gcode[4], ver_current[4];
  213. if (! parse_version(version_string, ver_gcode)) {
  214. // SERIAL_PROTOCOLLNPGM("parse_version failed");
  215. return false;
  216. }
  217. if (! parse_version_P(FW_VERSION_STR, ver_current)) {
  218. // SERIAL_PROTOCOLLNPGM("parse_version_P failed");
  219. return false; // this shall not happen
  220. }
  221. // SERIAL_PROTOCOLLNPGM("versions parsed");
  222. bool upgrade = false;
  223. for (uint8_t i = 0; i < 4; ++ i) {
  224. if (ver_gcode[i] > ver_current[i]) {
  225. upgrade = true;
  226. break;
  227. } else if (ver_gcode[i] < ver_current[i])
  228. break;
  229. }
  230. if (upgrade) {
  231. lcd_display_message_fullscreen_P(MSG_NEW_FIRMWARE_AVAILABLE);
  232. lcd_print_at_PGM(0, 2, PSTR(""));
  233. for (const char *c = version_string; ! is_whitespace_or_nl_or_eol(*c); ++ c)
  234. lcd_implementation_write(*c);
  235. lcd_print_at_PGM(0, 3, MSG_NEW_FIRMWARE_PLEASE_UPGRADE);
  236. tone(BEEPER, 1000);
  237. delay_keep_alive(50);
  238. noTone(BEEPER);
  239. delay_keep_alive(500);
  240. tone(BEEPER, 1000);
  241. delay_keep_alive(50);
  242. noTone(BEEPER);
  243. lcd_wait_for_click();
  244. lcd_update_enable(true);
  245. lcd_implementation_clear();
  246. lcd_update();
  247. }
  248. // Succeeded.
  249. return true;
  250. }
  251. void update_current_firmware_version_to_eeprom()
  252. {
  253. for (int8_t i = 0; i < FW_PRUSA3D_MAGIC_LEN; ++ i)
  254. eeprom_update_byte((uint8_t*)(EEPROM_FIRMWARE_PRUSA_MAGIC+i), pgm_read_byte(FW_PRUSA3D_MAGIC_STR+i));
  255. uint16_t ver_current[4];
  256. if (parse_version_P(FW_VERSION_STR, ver_current)) {
  257. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MAJOR, ver_current[0]);
  258. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_MINOR, ver_current[1]);
  259. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_REVISION, ver_current[2]);
  260. // See FirmwareRevisionFlavorType for the definition of firmware flavors.
  261. eeprom_update_word((uint16_t*)EEPROM_FIRMWARE_VERSION_FLAVOR, ver_current[3]);
  262. }
  263. }