qrcode_app.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 0
  14. /**
  15. * Maximum version is 11 because the f0 screen is only 64 pixels high and
  16. * version 12 is 65x65. Version 11 is 61x61.
  17. */
  18. #define MAX_QRCODE_VERSION 11
  19. /** Maximum length by mode, ecc, and version */
  20. static const uint16_t MAX_LENGTH[3][4][MAX_QRCODE_VERSION] = {
  21. {
  22. // Numeric
  23. {41, 77, 127, 187, 255, 322, 370, 461, 552, 652, 772}, // Low
  24. {34, 63, 101, 149, 202, 255, 293, 365, 432, 513, 604}, // Medium
  25. {27, 48, 77, 111, 144, 178, 207, 259, 312, 364, 427}, // Quartile
  26. {17, 34, 58, 82, 106, 139, 154, 202, 235, 288, 331}, // High
  27. },
  28. {
  29. // Alphanumeric
  30. {25, 47, 77, 114, 154, 195, 224, 279, 335, 395, 468}, // Low
  31. {20, 38, 61, 90, 122, 154, 178, 221, 262, 311, 366}, // Medium
  32. {16, 29, 47, 67, 87, 108, 125, 157, 189, 221, 259}, // Quartile
  33. {10, 20, 35, 50, 64, 84, 93, 122, 143, 174, 200}, // High
  34. },
  35. {
  36. // Binary
  37. {17, 32, 53, 78, 106, 134, 154, 192, 230, 271, 321}, // Low
  38. {14, 26, 42, 62, 84, 106, 122, 152, 180, 213, 251}, // Medium
  39. {11, 20, 32, 46, 60, 74, 86, 108, 130, 151, 177}, // Quartile
  40. {7, 14, 24, 34, 44, 58, 64, 84, 98, 119, 137}, // High
  41. },
  42. };
  43. /** Main app instance */
  44. typedef struct {
  45. FuriMessageQueue* input_queue;
  46. Gui* gui;
  47. ViewPort* view_port;
  48. FuriMutex** mutex;
  49. FuriString* message;
  50. QRCode* qrcode;
  51. uint8_t min_version;
  52. uint8_t max_ecc_at_min_version;
  53. bool loading;
  54. bool too_long;
  55. bool show_stats;
  56. uint8_t selected_idx;
  57. bool edit;
  58. uint8_t set_version;
  59. uint8_t set_ecc;
  60. } QRCodeApp;
  61. /**
  62. * @param ecc ECC number
  63. * @returns a character corresponding to the ecc level
  64. */
  65. static char get_ecc_char(uint8_t ecc) {
  66. switch (ecc) {
  67. case 0: return 'L';
  68. case 1: return 'M';
  69. case 2: return 'Q';
  70. case 3: return 'H';
  71. default: return '?';
  72. }
  73. }
  74. /**
  75. * @param mode qrcode mode
  76. * @returns a character corresponding to the mode
  77. */
  78. static char get_mode_char(uint8_t mode) {
  79. switch (mode) {
  80. case 0: return 'N';
  81. case 1: return 'A';
  82. case 2: return 'B';
  83. case 3: return 'K';
  84. default: return '?';
  85. }
  86. }
  87. /**
  88. * Render
  89. * @param canvas The canvas to render to
  90. * @param ctx Context provided to the callback by view_port_draw_callback_set
  91. */
  92. static void render_callback(Canvas* canvas, void* ctx) {
  93. furi_assert(canvas);
  94. furi_assert(ctx);
  95. QRCodeApp* instance = ctx;
  96. furi_check(furi_mutex_acquire(instance->mutex, FuriWaitForever) == FuriStatusOk);
  97. canvas_clear(canvas);
  98. canvas_set_color(canvas, ColorBlack);
  99. canvas_set_font(canvas, FontPrimary);
  100. uint8_t font_height = canvas_current_font_height(canvas);
  101. uint8_t width = canvas_width(canvas);
  102. uint8_t height = canvas_height(canvas);
  103. if (instance->loading) {
  104. canvas_draw_str_aligned(canvas, width / 2, height / 2, AlignCenter, AlignCenter, "Loading...");
  105. } else if (instance->qrcode) {
  106. uint8_t size = instance->qrcode->size;
  107. uint8_t pixel_size = height / size;
  108. uint8_t top = (height - pixel_size * size) / 2;
  109. uint8_t left = ((instance->show_stats ? 65 : width) - pixel_size * size) / 2;
  110. for (uint8_t y = 0; y < size; y++) {
  111. for (uint8_t x = 0; x < size; x++) {
  112. if (qrcode_getModule(instance->qrcode, x, y)) {
  113. if (pixel_size == 1) {
  114. canvas_draw_dot(canvas, left + x * pixel_size, top + y * pixel_size);
  115. } else {
  116. canvas_draw_box(canvas, left + x * pixel_size, top + y * pixel_size, pixel_size, pixel_size);
  117. }
  118. }
  119. }
  120. }
  121. if (instance->show_stats) {
  122. top = 10;
  123. left = 66;
  124. FuriString* str = furi_string_alloc();
  125. if (!instance->edit || instance->selected_idx == 0) {
  126. furi_string_printf(str, "Ver: %i", instance->set_version);
  127. canvas_draw_str(canvas, left + 5, top + font_height, furi_string_get_cstr(str));
  128. if (instance->selected_idx == 0) {
  129. canvas_draw_triangle(canvas, left, top + font_height / 2, font_height - 4, 4, CanvasDirectionLeftToRight);
  130. }
  131. if (instance->edit) {
  132. uint8_t arrow_left = left + 5 + canvas_string_width(canvas, "Ver: 8") / 2;
  133. canvas_draw_triangle(canvas, arrow_left, top, font_height - 4, 4, CanvasDirectionBottomToTop);
  134. canvas_draw_triangle(canvas, arrow_left, top + font_height + 1, font_height - 4, 4, CanvasDirectionTopToBottom);
  135. }
  136. }
  137. if (!instance->edit || instance->selected_idx == 1) {
  138. furi_string_printf(str, "ECC: %c", get_ecc_char(instance->set_ecc));
  139. canvas_draw_str(canvas, left + 5, 2 * font_height + top + 2, furi_string_get_cstr(str));
  140. if (instance->selected_idx == 1) {
  141. canvas_draw_triangle(canvas, left, 3 * font_height / 2 + top + 2, font_height - 4, 4, CanvasDirectionLeftToRight);
  142. }
  143. if (instance->edit) {
  144. uint8_t arrow_left = left + 5 + canvas_string_width(canvas, "ECC: H") / 2;
  145. canvas_draw_triangle(canvas, arrow_left, font_height + top + 2, font_height - 4, 4, CanvasDirectionBottomToTop);
  146. canvas_draw_triangle(canvas, arrow_left, 2 * font_height + top + 3, font_height - 4, 4, CanvasDirectionTopToBottom);
  147. }
  148. }
  149. if (!instance->edit) {
  150. furi_string_printf(str, "Mod: %c", get_mode_char(instance->qrcode->mode));
  151. canvas_draw_str(canvas, left + 5, 3 * font_height + top + 4, furi_string_get_cstr(str));
  152. }
  153. furi_string_free(str);
  154. }
  155. } else {
  156. uint8_t margin = (height - font_height * 2) / 3;
  157. canvas_draw_str_aligned(canvas, width / 2, margin, AlignCenter, AlignTop, "Could not load qrcode.");
  158. if (instance->too_long) {
  159. canvas_set_font(canvas, FontSecondary);
  160. canvas_draw_str(canvas, width / 2, margin * 2 + font_height, "Message is too long.");
  161. }
  162. }
  163. furi_mutex_release(instance->mutex);
  164. }
  165. /**
  166. * Handle input
  167. * @param input_event The received input event
  168. * @param ctx Context provided to the callback by view_port_input_callback_set
  169. */
  170. static void input_callback(InputEvent* input_event, void* ctx) {
  171. furi_assert(input_event);
  172. furi_assert(ctx);
  173. if (input_event->type == InputTypeShort) {
  174. QRCodeApp* instance = ctx;
  175. furi_message_queue_put(instance->input_queue, input_event, 0);
  176. }
  177. }
  178. /**
  179. * Determine if the given string is all numeric
  180. * @param str The string to test
  181. * @returns true if the string is all numeric
  182. */
  183. static bool is_numeric(const char* str, uint16_t len) {
  184. furi_assert(str);
  185. while (len > 0) {
  186. char c = str[--len];
  187. if (c < '0' || c > '9') return false;
  188. }
  189. return true;
  190. }
  191. /**
  192. * Determine if the given string is alphanumeric
  193. * @param str The string to test
  194. * @returns true if the string is alphanumeric
  195. */
  196. static bool is_alphanumeric(const char* str, uint16_t len) {
  197. furi_assert(str);
  198. while (len > 0) {
  199. char c = str[--len];
  200. if (c >= '0' && c <= '9') continue;
  201. if (c >= 'A' && c <= 'Z') continue;
  202. if (c == ' '
  203. || c == '$'
  204. || c == '%'
  205. || c == '*'
  206. || c == '+'
  207. || c == '-'
  208. || c == '.'
  209. || c == '/'
  210. || c == ':')
  211. continue;
  212. return false;
  213. }
  214. return true;
  215. }
  216. /**
  217. * Allocate a qrcode
  218. * @param version qrcode version
  219. * @returns an allocated QRCode
  220. */
  221. static QRCode* qrcode_alloc(uint8_t version) {
  222. QRCode* qrcode = malloc(sizeof(QRCode));
  223. qrcode->modules = malloc(qrcode_getBufferSize(version));
  224. return qrcode;
  225. }
  226. /**
  227. * Free a QRCode
  228. * @param qrcode The QRCode to free
  229. */
  230. static void qrcode_free(QRCode* qrcode) {
  231. furi_assert(qrcode);
  232. free(qrcode->modules);
  233. free(qrcode);
  234. }
  235. /**
  236. * Rebuild the qrcode. Assumes that instance->message is the message to encode,
  237. * that the mutex has been acquired, and the specified version/ecc will be
  238. * sufficiently large enough to encode the full message. It is also assumed
  239. * that the old qrcode will be free'd by the caller.
  240. * @param instance The qrcode app instance
  241. * @param version The qrcode version to use
  242. * @param ecc The qrcode ECC level to use
  243. * @returns true if the qrcode was successfully created
  244. */
  245. static bool rebuild_qrcode(QRCodeApp* instance, uint8_t version, uint8_t ecc) {
  246. furi_assert(instance);
  247. furi_assert(instance->message);
  248. const char* cstr = furi_string_get_cstr(instance->message);
  249. uint16_t len = strlen(cstr);
  250. instance->qrcode = qrcode_alloc(version);
  251. int8_t res = qrcode_initBytes(instance->qrcode, instance->qrcode->modules, version, ecc, (uint8_t*)cstr, len);
  252. if (res != 0) {
  253. FURI_LOG_E(TAG, "Could not create qrcode");
  254. qrcode_free(instance->qrcode);
  255. instance->qrcode = NULL;
  256. return false;
  257. }
  258. return true;
  259. }
  260. /**
  261. * Load a qrcode from a string
  262. * @param instance The qrcode app instance
  263. * @param str The message to encode as a qrcode
  264. * @returns true if the string was successfully loaded
  265. */
  266. static bool qrcode_load_string(QRCodeApp* instance, FuriString* str) {
  267. furi_assert(instance);
  268. furi_assert(str);
  269. furi_check(furi_mutex_acquire(instance->mutex, FuriWaitForever) == FuriStatusOk);
  270. if (instance->message) {
  271. furi_string_free(instance->message);
  272. instance->message = NULL;
  273. }
  274. if (instance->qrcode) {
  275. qrcode_free(instance->qrcode);
  276. instance->qrcode = NULL;
  277. }
  278. instance->too_long = false;
  279. instance->show_stats = false;
  280. instance->selected_idx = 0;
  281. instance->edit = false;
  282. bool result = false;
  283. do {
  284. const char* cstr = furi_string_get_cstr(str);
  285. uint16_t len = strlen(cstr);
  286. instance->message = furi_string_alloc_set(str);
  287. if (!instance->message) {
  288. FURI_LOG_E(TAG, "Could not allocate message");
  289. break;
  290. }
  291. // figure out the qrcode "mode"
  292. uint8_t mode = MODE_BYTE;
  293. if (is_numeric(cstr, len)) mode = MODE_NUMERIC;
  294. else if (is_alphanumeric(cstr, len)) mode = MODE_ALPHANUMERIC;
  295. // Figure out the smallest qrcode version that'll fit all of the data -
  296. // we prefer the smallest version to maximize the pixel size of each
  297. // module to improve reader performance. Here, version is the 0-based
  298. // index. The qrcode_initBytes function will want a 1-based version
  299. // number, so we'll add one later.
  300. uint8_t ecc = ECC_LOW;
  301. uint8_t version = 0;
  302. while (version < MAX_QRCODE_VERSION && MAX_LENGTH[mode][ecc][version] < len) {
  303. version++;
  304. }
  305. if (version == MAX_QRCODE_VERSION) {
  306. instance->too_long = true;
  307. break;
  308. }
  309. // Figure out the maximum ECC we can use. I shouldn't need to
  310. // bounds-check ecc in this loop because I already know from the loop
  311. // above that ECC_LOW (0) works... don't forget to add one to that
  312. // version number...
  313. ecc = ECC_HIGH;
  314. while (MAX_LENGTH[mode][ecc][version] < len) {
  315. ecc--;
  316. }
  317. version++;
  318. // Build the qrcode
  319. if (!rebuild_qrcode(instance, version, ecc)) {
  320. furi_string_free(instance->message);
  321. instance->message = NULL;
  322. break;
  323. }
  324. instance->min_version = instance->set_version = version;
  325. instance->max_ecc_at_min_version = instance->set_ecc = ecc;
  326. result = true;
  327. } while (false);
  328. instance->loading = false;
  329. furi_mutex_release(instance->mutex);
  330. return result;
  331. }
  332. /**
  333. * Load a qrcode from a file
  334. * @param instance The qrcode app instance
  335. * @param file_path Path to the file to read
  336. * @returns true if the file was successfully loaded
  337. */
  338. static bool qrcode_load_file(QRCodeApp* instance, const char* file_path) {
  339. furi_assert(instance);
  340. furi_assert(file_path);
  341. FuriString* temp_str = furi_string_alloc();
  342. bool result = false;
  343. Storage* storage = furi_record_open(RECORD_STORAGE);
  344. FlipperFormat* file = flipper_format_file_alloc(storage);
  345. do {
  346. if (!flipper_format_file_open_existing(file, file_path)) break;
  347. uint32_t version = 0;
  348. if (!flipper_format_read_header(file, temp_str, &version)) break;
  349. if (furi_string_cmp_str(temp_str, QRCODE_FILETYPE)
  350. || version != QRCODE_FILE_VERSION) {
  351. FURI_LOG_E(TAG, "Incorrect file format or version");
  352. break;
  353. }
  354. if (!flipper_format_read_string(file, "Message", temp_str)) {
  355. FURI_LOG_E(TAG, "Message is missing");
  356. break;
  357. }
  358. if (!qrcode_load_string(instance, temp_str)) {
  359. break;
  360. }
  361. result = true;
  362. } while (false);
  363. furi_record_close(RECORD_STORAGE);
  364. flipper_format_free(file);
  365. furi_string_free(temp_str);
  366. return result;
  367. }
  368. /**
  369. * Allocate the qrcode app
  370. * @returns a qrcode app instance
  371. */
  372. static QRCodeApp* qrcode_app_alloc() {
  373. QRCodeApp* instance = malloc(sizeof(QRCodeApp));
  374. instance->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  375. instance->view_port = view_port_alloc();
  376. view_port_draw_callback_set(instance->view_port, render_callback, instance);
  377. view_port_input_callback_set(instance->view_port, input_callback, instance);
  378. instance->gui = furi_record_open(RECORD_GUI);
  379. gui_add_view_port(instance->gui, instance->view_port, GuiLayerFullscreen);
  380. instance->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  381. instance->message = NULL;
  382. instance->qrcode = NULL;
  383. instance->loading = true;
  384. instance->too_long = false;
  385. instance->show_stats = false;
  386. instance->selected_idx = 0;
  387. instance->edit = false;
  388. return instance;
  389. }
  390. /**
  391. * Free the qrcode app
  392. * @param qrcode_app The app to free
  393. */
  394. static void qrcode_app_free(QRCodeApp* instance) {
  395. if (instance->message) furi_string_free(instance->message);
  396. if (instance->qrcode) qrcode_free(instance->qrcode);
  397. gui_remove_view_port(instance->gui, instance->view_port);
  398. furi_record_close(RECORD_GUI);
  399. view_port_free(instance->view_port);
  400. furi_message_queue_free(instance->input_queue);
  401. furi_mutex_free(instance->mutex);
  402. free(instance);
  403. }
  404. /** App entrypoint */
  405. int32_t qrcode_app(void* p) {
  406. QRCodeApp* instance = qrcode_app_alloc();
  407. FuriString* file_path = furi_string_alloc();
  408. do {
  409. if (p && strlen(p)) {
  410. furi_string_set(file_path, (const char*)p);
  411. } else {
  412. furi_string_set(file_path, QRCODE_FOLDER);
  413. DialogsFileBrowserOptions browser_options;
  414. dialog_file_browser_set_basic_options(
  415. &browser_options, QRCODE_EXTENSION, &I_qrcode_10px);
  416. browser_options.hide_ext = true;
  417. browser_options.base_path = QRCODE_FOLDER;
  418. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  419. bool res = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
  420. furi_record_close(RECORD_DIALOGS);
  421. if (!res) {
  422. FURI_LOG_E(TAG, "No file selected");
  423. break;
  424. }
  425. }
  426. if (!qrcode_load_file(instance, furi_string_get_cstr(file_path))) {
  427. FURI_LOG_E(TAG, "Unable to load file");
  428. }
  429. InputEvent input;
  430. while (furi_message_queue_get(instance->input_queue, &input, FuriWaitForever) == FuriStatusOk) {
  431. furi_check(furi_mutex_acquire(instance->mutex, FuriWaitForever) == FuriStatusOk);
  432. if (input.key == InputKeyBack) {
  433. if (instance->message) {
  434. furi_string_free(instance->message);
  435. instance->message = NULL;
  436. }
  437. if (instance->qrcode) {
  438. qrcode_free(instance->qrcode);
  439. instance->qrcode = NULL;
  440. }
  441. instance->loading = true;
  442. instance->edit = false;
  443. furi_mutex_release(instance->mutex);
  444. break;
  445. } else if (input.key == InputKeyRight) {
  446. instance->show_stats = true;
  447. } else if (input.key == InputKeyLeft) {
  448. instance->show_stats = false;
  449. } else if (instance->show_stats && !instance->loading && instance->qrcode) {
  450. if (input.key == InputKeyUp) {
  451. if (!instance->edit) {
  452. instance->selected_idx = MAX(0, instance->selected_idx - 1);
  453. } else {
  454. if (instance->selected_idx == 0 && instance->set_version < MAX_QRCODE_VERSION) {
  455. instance->set_version++;
  456. } else if (instance->selected_idx == 1) {
  457. uint8_t max_ecc = instance->set_version == instance->min_version ? instance->max_ecc_at_min_version : ECC_HIGH;
  458. if (instance->set_ecc < max_ecc) {
  459. instance->set_ecc++;
  460. }
  461. }
  462. }
  463. } else if (input.key == InputKeyDown) {
  464. if (!instance->edit) {
  465. instance->selected_idx = MIN(1, instance->selected_idx + 1);
  466. } else {
  467. if (instance->selected_idx == 0 && instance->set_version > instance->min_version) {
  468. instance->set_version--;
  469. if (instance->set_version == instance->min_version) {
  470. instance->set_ecc = MAX(instance->set_ecc, instance->max_ecc_at_min_version);
  471. }
  472. } else if (instance->selected_idx == 1 && instance->set_ecc > 0) {
  473. instance->set_ecc--;
  474. }
  475. }
  476. } else if (input.key == InputKeyOk) {
  477. if (instance->edit && (instance->set_version != instance->qrcode->version || instance->set_ecc != instance->qrcode->ecc)) {
  478. QRCode* qrcode = instance->qrcode;
  479. instance->loading = true;
  480. if (rebuild_qrcode(instance, instance->set_version, instance->set_ecc)) {
  481. qrcode_free(qrcode);
  482. } else {
  483. FURI_LOG_E(TAG, "Could not rebuild qrcode");
  484. instance->qrcode = qrcode;
  485. instance->set_version = qrcode->version;
  486. instance->set_ecc = qrcode->ecc;
  487. }
  488. instance->loading = false;
  489. }
  490. instance->edit = !instance->edit;
  491. }
  492. }
  493. furi_mutex_release(instance->mutex);
  494. view_port_update(instance->view_port);
  495. }
  496. if (p && strlen(p)) {
  497. // if started with an arg, exit instead
  498. // of looping back to the browser
  499. break;
  500. }
  501. } while (true);
  502. furi_string_free(file_path);
  503. qrcode_app_free(instance);
  504. return 0;
  505. }