util.cpp 8.2 KB

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