qrcode_app.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. #include <furi.h>
  2. #include <dialogs/dialogs.h>
  3. #include <gui/gui.h>
  4. #include <storage/storage.h>
  5. #include <lib/flipper_format/flipper_format.h>
  6. // this file is generated by the build script
  7. #include <qrcode_icons.h>
  8. #include "qrcode.h"
  9. #define TAG "qrcode"
  10. #define QRCODE_FOLDER ANY_PATH("qrcodes")
  11. #define QRCODE_EXTENSION ".qrcode"
  12. #define QRCODE_FILETYPE "QRCode"
  13. #define QRCODE_FILE_VERSION 1
  14. /** Valid modes are Numeric (0), Alpha-Numeric (1), and Binary (2) */
  15. #define MAX_QRCODE_MODE 2
  16. /**
  17. * Maximum version is 11 because the f0 screen is only 64 pixels high and
  18. * version 12 is 65x65. Version 11 is 61x61.
  19. */
  20. #define MAX_QRCODE_VERSION 11
  21. /** Valid ECC levels are Low (0), Medium (1), Quartile (2), and High (3) */
  22. #define MAX_QRCODE_ECC 3
  23. /** Maximum length by mode, ecc, and version */
  24. static const uint16_t MAX_LENGTH[3][4][MAX_QRCODE_VERSION] = {
  25. {
  26. // Numeric
  27. {41, 77, 127, 187, 255, 322, 370, 461, 552, 652, 772}, // Low
  28. {34, 63, 101, 149, 202, 255, 293, 365, 432, 513, 604}, // Medium
  29. {27, 48, 77, 111, 144, 178, 207, 259, 312, 364, 427}, // Quartile
  30. {17, 34, 58, 82, 106, 139, 154, 202, 235, 288, 331}, // High
  31. },
  32. {
  33. // Alphanumeric
  34. {25, 47, 77, 114, 154, 195, 224, 279, 335, 395, 468}, // Low
  35. {20, 38, 61, 90, 122, 154, 178, 221, 262, 311, 366}, // Medium
  36. {16, 29, 47, 67, 87, 108, 125, 157, 189, 221, 259}, // Quartile
  37. {10, 20, 35, 50, 64, 84, 93, 122, 143, 174, 200}, // High
  38. },
  39. {
  40. // Binary
  41. {17, 32, 53, 78, 106, 134, 154, 192, 230, 271, 321}, // Low
  42. {14, 26, 42, 62, 84, 106, 122, 152, 180, 213, 251}, // Medium
  43. {11, 20, 32, 46, 60, 74, 86, 108, 130, 151, 177}, // Quartile
  44. {7, 14, 24, 34, 44, 58, 64, 84, 98, 119, 137}, // High
  45. },
  46. };
  47. /** Main app instance */
  48. typedef struct {
  49. FuriMessageQueue* input_queue;
  50. Gui* gui;
  51. ViewPort* view_port;
  52. FuriMutex** mutex;
  53. FuriString* message;
  54. QRCode* qrcode;
  55. uint8_t min_mode;
  56. uint8_t max_mode;
  57. uint8_t min_version;
  58. uint8_t max_ecc_at_min_version;
  59. bool loading;
  60. bool too_long;
  61. bool show_stats;
  62. uint8_t selected_idx;
  63. bool edit;
  64. uint8_t set_mode;
  65. uint8_t set_version;
  66. uint8_t set_ecc;
  67. } QRCodeApp;
  68. /**
  69. * @param ecc ECC number
  70. * @returns a character corresponding to the ecc level
  71. */
  72. static char get_ecc_char(uint8_t ecc) {
  73. switch (ecc) {
  74. case 0: return 'L';
  75. case 1: return 'M';
  76. case 2: return 'Q';
  77. case 3: return 'H';
  78. default: return '?';
  79. }
  80. }
  81. /**
  82. * @param ecc A character representing an ECC mode (L, M, Q, or H)
  83. * @returns the ecc level or 255 representing an unknown ECC mode
  84. */
  85. static uint8_t get_ecc_value(char ecc) {
  86. switch (ecc) {
  87. case 'L':
  88. case 'l':
  89. return 0;
  90. case 'M':
  91. case 'm':
  92. return 1;
  93. case 'Q':
  94. case 'q':
  95. return 2;
  96. case 'H':
  97. case 'h':
  98. return 3;
  99. default:
  100. return 255;
  101. }
  102. }
  103. /**
  104. * @param mode qrcode mode
  105. * @returns a character corresponding to the mode
  106. */
  107. static char get_mode_char(uint8_t mode) {
  108. switch (mode) {
  109. case 0: return 'N';
  110. case 1: return 'A';
  111. case 2: return 'B';
  112. case 3: return 'K';
  113. default: return '?';
  114. }
  115. }
  116. /**
  117. * @param mode A character representing a qrcode mode (N, A, or B)
  118. * @returns the mode or 255 representing an unknown mode
  119. */
  120. static uint8_t get_mode_value(char mode) {
  121. switch (mode) {
  122. case 'N':
  123. case 'n':
  124. return 0;
  125. case 'A':
  126. case 'a':
  127. return 1;
  128. case 'B':
  129. case 'b':
  130. return 2;
  131. default:
  132. return 255;
  133. }
  134. }
  135. /**
  136. * Render
  137. * @param canvas The canvas to render to
  138. * @param ctx Context provided to the callback by view_port_draw_callback_set
  139. */
  140. static void render_callback(Canvas* canvas, void* ctx) {
  141. furi_assert(canvas);
  142. furi_assert(ctx);
  143. QRCodeApp* instance = ctx;
  144. furi_check(furi_mutex_acquire(instance->mutex, FuriWaitForever) == FuriStatusOk);
  145. canvas_clear(canvas);
  146. canvas_set_color(canvas, ColorBlack);
  147. canvas_set_font(canvas, FontPrimary);
  148. uint8_t font_height = canvas_current_font_height(canvas);
  149. uint8_t width = canvas_width(canvas);
  150. uint8_t height = canvas_height(canvas);
  151. if (instance->loading) {
  152. canvas_draw_str_aligned(canvas, width / 2, height / 2, AlignCenter, AlignCenter, "Loading...");
  153. } else if (instance->qrcode) {
  154. uint8_t size = instance->qrcode->size;
  155. uint8_t pixel_size = height / size;
  156. uint8_t top = (height - pixel_size * size) / 2;
  157. uint8_t left = ((instance->show_stats ? 65 : width) - pixel_size * size) / 2;
  158. for (uint8_t y = 0; y < size; y++) {
  159. for (uint8_t x = 0; x < size; x++) {
  160. if (qrcode_getModule(instance->qrcode, x, y)) {
  161. if (pixel_size == 1) {
  162. canvas_draw_dot(canvas, left + x * pixel_size, top + y * pixel_size);
  163. } else {
  164. canvas_draw_box(canvas, left + x * pixel_size, top + y * pixel_size, pixel_size, pixel_size);
  165. }
  166. }
  167. }
  168. }
  169. if (instance->show_stats) {
  170. top = 10;
  171. left = 66;
  172. FuriString* str = furi_string_alloc();
  173. if (!instance->edit || instance->selected_idx == 0) {
  174. furi_string_printf(str, "Mod: %c", get_mode_char(instance->set_mode));
  175. canvas_draw_str(canvas, left + 5, font_height + top, furi_string_get_cstr(str));
  176. if (instance->selected_idx == 0) {
  177. canvas_draw_triangle(canvas, left, top + font_height / 2, font_height - 4, 4, CanvasDirectionLeftToRight);
  178. }
  179. if (instance->edit) {
  180. uint8_t arrow_left = left + 5 + canvas_string_width(canvas, "Mod: B") / 2;
  181. canvas_draw_triangle(canvas, arrow_left, top, font_height - 4, 4, CanvasDirectionBottomToTop);
  182. canvas_draw_triangle(canvas, arrow_left, top + font_height + 1, font_height - 4, 4, CanvasDirectionTopToBottom);
  183. }
  184. }
  185. if (!instance->edit || instance->selected_idx == 1) {
  186. furi_string_printf(str, "Ver: %i", instance->set_version);
  187. canvas_draw_str(canvas, left + 5, 2 * font_height + top + 2, furi_string_get_cstr(str));
  188. if (instance->selected_idx == 1) {
  189. canvas_draw_triangle(canvas, left, 3 * font_height / 2 + top + 2, font_height - 4, 4, CanvasDirectionLeftToRight);
  190. }
  191. if (instance->edit) {
  192. uint8_t arrow_left = left + 5 + canvas_string_width(canvas, "Ver: 8") / 2;
  193. canvas_draw_triangle(canvas, arrow_left, font_height + top + 2, font_height - 4, 4, CanvasDirectionBottomToTop);
  194. canvas_draw_triangle(canvas, arrow_left, 2 * font_height + top + 3, font_height - 4, 4, CanvasDirectionTopToBottom);
  195. }
  196. }
  197. if (!instance->edit || instance->selected_idx == 2) {
  198. furi_string_printf(str, "ECC: %c", get_ecc_char(instance->set_ecc));
  199. canvas_draw_str(canvas, left + 5, 3 * font_height + top + 4, furi_string_get_cstr(str));
  200. if (instance->selected_idx == 2) {
  201. canvas_draw_triangle(canvas, left, 5 * font_height / 2 + top + 4, font_height - 4, 4, CanvasDirectionLeftToRight);
  202. }
  203. if (instance->edit) {
  204. uint8_t arrow_left = left + 5 + canvas_string_width(canvas, "ECC: H") / 2;
  205. canvas_draw_triangle(canvas, arrow_left, 2 * font_height + top + 4, font_height - 4, 4, CanvasDirectionBottomToTop);
  206. canvas_draw_triangle(canvas, arrow_left, 3 * font_height + top + 5, font_height - 4, 4, CanvasDirectionTopToBottom);
  207. }
  208. }
  209. furi_string_free(str);
  210. }
  211. } else {
  212. uint8_t margin = (height - font_height * 2) / 3;
  213. canvas_draw_str_aligned(canvas, width / 2, margin, AlignCenter, AlignTop, "Could not load qrcode.");
  214. if (instance->too_long) {
  215. canvas_set_font(canvas, FontSecondary);
  216. canvas_draw_str(canvas, width / 2, margin * 2 + font_height, "Message is too long.");
  217. }
  218. }
  219. furi_mutex_release(instance->mutex);
  220. }
  221. /**
  222. * Handle input
  223. * @param input_event The received input event
  224. * @param ctx Context provided to the callback by view_port_input_callback_set
  225. */
  226. static void input_callback(InputEvent* input_event, void* ctx) {
  227. furi_assert(input_event);
  228. furi_assert(ctx);
  229. if (input_event->type == InputTypeShort) {
  230. QRCodeApp* instance = ctx;
  231. furi_message_queue_put(instance->input_queue, input_event, 0);
  232. }
  233. }
  234. /**
  235. * Determine if the given string is all numeric
  236. * @param str The string to test
  237. * @returns true if the string is all numeric
  238. */
  239. static bool is_numeric(const char* str, uint16_t len) {
  240. furi_assert(str);
  241. while (len > 0) {
  242. char c = str[--len];
  243. if (c < '0' || c > '9') return false;
  244. }
  245. return true;
  246. }
  247. /**
  248. * Determine if the given string is alphanumeric
  249. * @param str The string to test
  250. * @returns true if the string is alphanumeric
  251. */
  252. static bool is_alphanumeric(const char* str, uint16_t len) {
  253. furi_assert(str);
  254. while (len > 0) {
  255. char c = str[--len];
  256. if (c >= '0' && c <= '9') continue;
  257. if (c >= 'A' && c <= 'Z') continue;
  258. if (c == ' '
  259. || c == '$'
  260. || c == '%'
  261. || c == '*'
  262. || c == '+'
  263. || c == '-'
  264. || c == '.'
  265. || c == '/'
  266. || c == ':')
  267. continue;
  268. return false;
  269. }
  270. return true;
  271. }
  272. /**
  273. * Allocate a qrcode
  274. * @param version qrcode version
  275. * @returns an allocated QRCode
  276. */
  277. static QRCode* qrcode_alloc(uint8_t version) {
  278. QRCode* qrcode = malloc(sizeof(QRCode));
  279. qrcode->modules = malloc(qrcode_getBufferSize(version));
  280. return qrcode;
  281. }
  282. /**
  283. * Free a QRCode
  284. * @param qrcode The QRCode to free
  285. */
  286. static void qrcode_free(QRCode* qrcode) {
  287. furi_assert(qrcode);
  288. free(qrcode->modules);
  289. free(qrcode);
  290. }
  291. /**
  292. * Rebuild the qrcode. Assumes that instance->message is the message to encode,
  293. * that the mutex has been acquired, and the specified version/ecc will be
  294. * sufficiently large enough to encode the full message. It is also assumed
  295. * that the old qrcode will be free'd by the caller.
  296. * @param instance The qrcode app instance
  297. * @param mode The qrcode mode to use
  298. * @param version The qrcode version to use
  299. * @param ecc The qrcode ECC level to use
  300. * @returns true if the qrcode was successfully created
  301. */
  302. static bool rebuild_qrcode(QRCodeApp* instance, uint8_t mode, uint8_t version, uint8_t ecc) {
  303. furi_assert(instance);
  304. furi_assert(instance->message);
  305. const char* cstr = furi_string_get_cstr(instance->message);
  306. uint16_t len = (uint16_t)furi_string_size(instance->message);
  307. instance->qrcode = qrcode_alloc(version);
  308. int8_t res = qrcode_initBytes(instance->qrcode, instance->qrcode->modules, (int8_t)mode, version, ecc, (uint8_t*)cstr, len);
  309. if (res != 0) {
  310. FURI_LOG_E(TAG, "Could not create qrcode");
  311. qrcode_free(instance->qrcode);
  312. instance->qrcode = NULL;
  313. return false;
  314. }
  315. return true;
  316. }
  317. /**
  318. * Determine the minimum version and maximum ECC for a message of a given
  319. * length and mode.
  320. * @param len The length of the message
  321. * @param mode The mode of the encoded message
  322. * @param version Pointer to variable that will receive the minimum version
  323. * @param ecc Pointer to variable that will receive the maximum ECC
  324. * @returns false if the data is too long for the given mode, true otherwise.
  325. */
  326. static bool find_min_version_max_ecc(uint16_t len, uint8_t mode, uint8_t *version, uint8_t *ecc) {
  327. // Figure out the smallest qrcode version that'll fit all of the data - we
  328. // prefer the smallest version to maximize the pixel size of each module to
  329. // improve reader performance. Here, version is the 0-based index. The
  330. // qrcode_initBytes function will want a 1-based version number, so we'll
  331. // add one later.
  332. *ecc = ECC_LOW;
  333. *version = 0;
  334. while (*version < MAX_QRCODE_VERSION && MAX_LENGTH[mode][*ecc][*version] < len) {
  335. (*version)++;
  336. }
  337. if (*version == MAX_QRCODE_VERSION) {
  338. return false;
  339. }
  340. // Figure out the maximum ECC we can use. I shouldn't need to bounds-check
  341. // ecc in this loop because I already know from the loop above that ECC_LOW
  342. // (0) works... don't forget to add one to that version number, since we're
  343. // using it as a 0-based number here, but qrcode_initBytes will want a
  344. // 1-based number...
  345. *ecc = ECC_HIGH;
  346. while (MAX_LENGTH[mode][*ecc][*version] < len) {
  347. (*ecc)--;
  348. }
  349. (*version)++;
  350. return true;
  351. }
  352. /**
  353. * Load a qrcode from a string
  354. * @param instance The qrcode app instance
  355. * @param str The message to encode as a qrcode
  356. * @param desired_mode User selected mode, 255 = unset
  357. * @param desired_version User selected version, 255 = unset
  358. * @param desired_ecc User selected ECC, 255 = unset
  359. * @returns true if the string was successfully loaded
  360. */
  361. static bool qrcode_load_string(QRCodeApp* instance, FuriString* str, uint8_t desired_mode, uint8_t desired_version, uint8_t desired_ecc) {
  362. furi_assert(instance);
  363. furi_assert(str);
  364. furi_check(furi_mutex_acquire(instance->mutex, FuriWaitForever) == FuriStatusOk);
  365. if (instance->message) {
  366. furi_string_free(instance->message);
  367. instance->message = NULL;
  368. }
  369. if (instance->qrcode) {
  370. qrcode_free(instance->qrcode);
  371. instance->qrcode = NULL;
  372. }
  373. instance->too_long = false;
  374. instance->show_stats = false;
  375. instance->selected_idx = 0;
  376. instance->edit = false;
  377. bool result = false;
  378. do {
  379. const char* cstr = furi_string_get_cstr(str);
  380. uint16_t len = (uint16_t)furi_string_size(str);
  381. instance->message = furi_string_alloc_set(str);
  382. if (!instance->message) {
  383. FURI_LOG_E(TAG, "Could not allocate message");
  384. break;
  385. }
  386. // figure out the minimum qrcode "mode"
  387. int8_t min_mode = MODE_BYTE;
  388. if (is_numeric(cstr, len)) min_mode = MODE_NUMERIC;
  389. else if (is_alphanumeric(cstr, len)) min_mode = MODE_ALPHANUMERIC;
  390. // determine the maximum "mode"
  391. int8_t max_mode = MAX_QRCODE_MODE;
  392. uint8_t min_version = 0;
  393. uint8_t max_ecc_at_min_version = 0;
  394. while (max_mode >= min_mode && !find_min_version_max_ecc(len, (uint8_t)max_mode, &min_version, &max_ecc_at_min_version)) {
  395. max_mode--;
  396. }
  397. // if the max is less than the min, the message is too long
  398. if (max_mode < min_mode) {
  399. instance->too_long = true;
  400. break;
  401. }
  402. // pick a mode based on the min/max and desired mode
  403. if (desired_mode == 255 || desired_mode < (uint8_t)min_mode) {
  404. desired_mode = (uint8_t)min_mode;
  405. } else if (desired_mode > (uint8_t)max_mode) {
  406. desired_mode = (uint8_t)max_mode;
  407. }
  408. if (desired_mode != (uint8_t)max_mode) {
  409. // if the desired mode equals the max mode, then min_version and
  410. // max_ecc_at_min_version are already set appropriately by the max
  411. // mode loop above... otherwise, we need to calculate them... this
  412. // should always return true because we already know the desired
  413. // mode is appropriate for the data, but, just in case...
  414. if (!find_min_version_max_ecc(len, desired_mode, &min_version, &max_ecc_at_min_version)) {
  415. instance->too_long = true;
  416. break;
  417. }
  418. }
  419. // ensure desired version and ecc are appropriate
  420. if (desired_version == 255 || desired_version < min_version) {
  421. desired_version = min_version;
  422. } else if (desired_version > MAX_QRCODE_VERSION) {
  423. desired_version = MAX_QRCODE_VERSION;
  424. }
  425. if (desired_version == min_version) {
  426. if (desired_ecc > max_ecc_at_min_version) {
  427. desired_ecc = max_ecc_at_min_version;
  428. }
  429. } else if (desired_ecc > MAX_QRCODE_ECC) {
  430. desired_ecc = MAX_QRCODE_ECC;
  431. }
  432. // Build the qrcode
  433. if (!rebuild_qrcode(instance, desired_mode, desired_version, desired_ecc)) {
  434. break;
  435. }
  436. instance->min_mode = (uint8_t)min_mode;
  437. instance->max_mode = (uint8_t)max_mode;
  438. instance->set_mode = desired_mode;
  439. instance->min_version = min_version;
  440. instance->set_version = desired_version;
  441. instance->max_ecc_at_min_version = max_ecc_at_min_version;
  442. instance->set_ecc = desired_ecc;
  443. result = true;
  444. } while (false);
  445. if (!result) {
  446. if (instance->message) {
  447. furi_string_free(instance->message);
  448. instance->message = NULL;
  449. }
  450. if (instance->qrcode) {
  451. qrcode_free(instance->qrcode);
  452. instance->qrcode = NULL;
  453. }
  454. }
  455. instance->loading = false;
  456. furi_mutex_release(instance->mutex);
  457. return result;
  458. }
  459. /**
  460. * Load a qrcode from a file
  461. * @param instance The qrcode app instance
  462. * @param file_path Path to the file to read
  463. * @returns true if the file was successfully loaded
  464. */
  465. static bool qrcode_load_file(QRCodeApp* instance, const char* file_path) {
  466. furi_assert(instance);
  467. furi_assert(file_path);
  468. FuriString* temp_str = furi_string_alloc();
  469. bool result = false;
  470. Storage* storage = furi_record_open(RECORD_STORAGE);
  471. FlipperFormat* file = flipper_format_file_alloc(storage);
  472. do {
  473. if (!flipper_format_file_open_existing(file, file_path)) break;
  474. uint32_t file_version = 0;
  475. if (!flipper_format_read_header(file, temp_str, &file_version)) break;
  476. if (furi_string_cmp_str(temp_str, QRCODE_FILETYPE)
  477. || file_version > QRCODE_FILE_VERSION) {
  478. FURI_LOG_E(TAG, "Incorrect file format or version");
  479. break;
  480. }
  481. uint32_t desired_mode = 255;
  482. uint32_t desired_version = 255;
  483. uint32_t desired_ecc = 255;
  484. if (file_version > 0) {
  485. if (flipper_format_key_exist(file, "QRMode")) {
  486. if (flipper_format_read_string(file, "QRMode", temp_str)) {
  487. if (furi_string_size(temp_str) > 0) {
  488. desired_mode = get_mode_value(furi_string_get_char(temp_str, 0));
  489. }
  490. } else {
  491. FURI_LOG_E(TAG, "Could not read QRMode");
  492. desired_mode = 255;
  493. }
  494. }
  495. if (flipper_format_key_exist(file, "QRVersion")) {
  496. if (flipper_format_read_uint32(file, "QRVersion", &desired_version, 1)) {
  497. if (desired_version > MAX_QRCODE_VERSION) {
  498. FURI_LOG_E(TAG, "Invalid QRVersion");
  499. desired_version = 255;
  500. }
  501. } else {
  502. FURI_LOG_E(TAG, "Could not read QRVersion");
  503. desired_version = 255;
  504. }
  505. }
  506. if (flipper_format_key_exist(file, "QRECC")) {
  507. if (flipper_format_read_string(file, "QRECC", temp_str)) {
  508. if (furi_string_size(temp_str) > 0) {
  509. desired_ecc = get_ecc_value(furi_string_get_char(temp_str, 0));
  510. }
  511. } else {
  512. FURI_LOG_E(TAG, "Could not read QRECC");
  513. desired_ecc = 255;
  514. }
  515. }
  516. }
  517. if (!flipper_format_read_string(file, "Message", temp_str)) {
  518. FURI_LOG_E(TAG, "Message is missing");
  519. break;
  520. }
  521. if (file_version > 0) {
  522. FuriString* msg_cont = furi_string_alloc();
  523. while (flipper_format_key_exist(file, "Message")) {
  524. if (!flipper_format_read_string(file, "Message", msg_cont)) {
  525. FURI_LOG_E(TAG, "Could not read next Message");
  526. break;
  527. }
  528. furi_string_push_back(temp_str, '\n');
  529. furi_string_cat(temp_str, msg_cont);
  530. }
  531. furi_string_free(msg_cont);
  532. }
  533. if (!qrcode_load_string(instance, temp_str, (uint8_t)desired_mode, (uint8_t)desired_version, (uint8_t)desired_ecc)) {
  534. break;
  535. }
  536. result = true;
  537. } while (false);
  538. furi_record_close(RECORD_STORAGE);
  539. flipper_format_free(file);
  540. furi_string_free(temp_str);
  541. return result;
  542. }
  543. /**
  544. * Allocate the qrcode app
  545. * @returns a qrcode app instance
  546. */
  547. static QRCodeApp* qrcode_app_alloc() {
  548. QRCodeApp* instance = malloc(sizeof(QRCodeApp));
  549. instance->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  550. instance->view_port = view_port_alloc();
  551. view_port_draw_callback_set(instance->view_port, render_callback, instance);
  552. view_port_input_callback_set(instance->view_port, input_callback, instance);
  553. instance->gui = furi_record_open(RECORD_GUI);
  554. gui_add_view_port(instance->gui, instance->view_port, GuiLayerFullscreen);
  555. instance->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  556. instance->message = NULL;
  557. instance->qrcode = NULL;
  558. instance->loading = true;
  559. instance->too_long = false;
  560. instance->show_stats = false;
  561. instance->selected_idx = 0;
  562. instance->edit = false;
  563. return instance;
  564. }
  565. /**
  566. * Free the qrcode app
  567. * @param qrcode_app The app to free
  568. */
  569. static void qrcode_app_free(QRCodeApp* instance) {
  570. if (instance->message) furi_string_free(instance->message);
  571. if (instance->qrcode) qrcode_free(instance->qrcode);
  572. gui_remove_view_port(instance->gui, instance->view_port);
  573. furi_record_close(RECORD_GUI);
  574. view_port_free(instance->view_port);
  575. furi_message_queue_free(instance->input_queue);
  576. furi_mutex_free(instance->mutex);
  577. free(instance);
  578. }
  579. /** App entrypoint */
  580. int32_t qrcode_app(void* p) {
  581. QRCodeApp* instance = qrcode_app_alloc();
  582. FuriString* file_path = furi_string_alloc();
  583. do {
  584. if (p && strlen(p)) {
  585. furi_string_set(file_path, (const char*)p);
  586. } else {
  587. furi_string_set(file_path, QRCODE_FOLDER);
  588. DialogsFileBrowserOptions browser_options;
  589. dialog_file_browser_set_basic_options(
  590. &browser_options, QRCODE_EXTENSION, &I_qrcode_10px);
  591. browser_options.hide_ext = true;
  592. browser_options.base_path = QRCODE_FOLDER;
  593. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  594. bool res = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
  595. furi_record_close(RECORD_DIALOGS);
  596. if (!res) {
  597. FURI_LOG_E(TAG, "No file selected");
  598. break;
  599. }
  600. }
  601. if (!qrcode_load_file(instance, furi_string_get_cstr(file_path))) {
  602. FURI_LOG_E(TAG, "Unable to load file");
  603. }
  604. InputEvent input;
  605. while (furi_message_queue_get(instance->input_queue, &input, FuriWaitForever) == FuriStatusOk) {
  606. furi_check(furi_mutex_acquire(instance->mutex, FuriWaitForever) == FuriStatusOk);
  607. if (input.key == InputKeyBack) {
  608. if (instance->message) {
  609. furi_string_free(instance->message);
  610. instance->message = NULL;
  611. }
  612. if (instance->qrcode) {
  613. qrcode_free(instance->qrcode);
  614. instance->qrcode = NULL;
  615. }
  616. instance->loading = true;
  617. instance->edit = false;
  618. furi_mutex_release(instance->mutex);
  619. break;
  620. } else if (input.key == InputKeyRight) {
  621. instance->show_stats = true;
  622. } else if (input.key == InputKeyLeft) {
  623. instance->show_stats = false;
  624. } else if (instance->show_stats && !instance->loading && instance->qrcode) {
  625. if (input.key == InputKeyUp) {
  626. if (!instance->edit) {
  627. instance->selected_idx = MAX(0, instance->selected_idx - 1);
  628. } else {
  629. if (instance->selected_idx == 0 && instance->set_mode < instance->max_mode) {
  630. instance->set_mode++;
  631. } else if (instance->selected_idx == 1 && instance->set_version < MAX_QRCODE_VERSION) {
  632. instance->set_version++;
  633. } else if (instance->selected_idx == 2) {
  634. uint8_t max_ecc = instance->set_version == instance->min_version ? instance->max_ecc_at_min_version : ECC_HIGH;
  635. if (instance->set_ecc < max_ecc) {
  636. instance->set_ecc++;
  637. }
  638. }
  639. }
  640. } else if (input.key == InputKeyDown) {
  641. if (!instance->edit) {
  642. instance->selected_idx = MIN(2, instance->selected_idx + 1);
  643. } else {
  644. if (instance->selected_idx == 0 && instance->set_mode > instance->min_mode) {
  645. instance->set_mode--;
  646. } else if (instance->selected_idx == 1 && instance->set_version > instance->min_version) {
  647. instance->set_version--;
  648. if (instance->set_version == instance->min_version) {
  649. instance->set_ecc = MIN(instance->set_ecc, instance->max_ecc_at_min_version);
  650. }
  651. } else if (instance->selected_idx == 2 && instance->set_ecc > 0) {
  652. instance->set_ecc--;
  653. }
  654. }
  655. } else if (input.key == InputKeyOk) {
  656. if (
  657. instance->edit
  658. && (instance->set_mode != instance->qrcode->mode
  659. || instance->set_version != instance->qrcode->version
  660. || instance->set_ecc != instance->qrcode->ecc)) {
  661. uint8_t orig_min_version = instance->min_version;
  662. uint8_t orig_max_ecc_at_min_version = instance->max_ecc_at_min_version;
  663. if (instance->set_mode != instance->qrcode->mode) {
  664. uint16_t len = (uint16_t)furi_string_size(instance->message);
  665. uint8_t min_version = 0;
  666. uint8_t max_ecc_at_min_version = 0;
  667. if (find_min_version_max_ecc(len, instance->set_mode, &min_version, &max_ecc_at_min_version)) {
  668. if (instance->set_version < min_version) {
  669. instance->set_version = min_version;
  670. }
  671. if (instance->set_version == min_version && instance->set_ecc > max_ecc_at_min_version) {
  672. instance->set_ecc = max_ecc_at_min_version;
  673. }
  674. instance->min_version = min_version;
  675. instance->max_ecc_at_min_version = max_ecc_at_min_version;
  676. } else {
  677. instance->set_mode = instance->qrcode->mode;
  678. }
  679. }
  680. QRCode* qrcode = instance->qrcode;
  681. instance->loading = true;
  682. if (rebuild_qrcode(instance, instance->set_mode, instance->set_version, instance->set_ecc)) {
  683. qrcode_free(qrcode);
  684. } else {
  685. FURI_LOG_E(TAG, "Could not rebuild qrcode");
  686. instance->qrcode = qrcode;
  687. instance->set_mode = qrcode->mode;
  688. instance->set_version = qrcode->version;
  689. instance->set_ecc = qrcode->ecc;
  690. instance->min_version = orig_min_version;
  691. instance->max_ecc_at_min_version = orig_max_ecc_at_min_version;
  692. }
  693. instance->loading = false;
  694. }
  695. instance->edit = !instance->edit;
  696. }
  697. }
  698. furi_mutex_release(instance->mutex);
  699. view_port_update(instance->view_port);
  700. }
  701. if (p && strlen(p)) {
  702. // if started with an arg, exit instead
  703. // of looping back to the browser
  704. break;
  705. }
  706. } while (true);
  707. furi_string_free(file_path);
  708. qrcode_app_free(instance);
  709. return 0;
  710. }