util.cpp 8.6 KB

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