util.cpp 8.8 KB

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