qrcode_app.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. QRCode* qrcode;
  50. bool loading;
  51. bool too_long;
  52. bool show_stats;
  53. } QRCodeApp;
  54. /**
  55. * @param ecc ECC number
  56. * @returns a character corresponding to the ecc level
  57. */
  58. static char get_ecc_char(uint8_t ecc) {
  59. switch (ecc) {
  60. case 0: return 'L';
  61. case 1: return 'M';
  62. case 2: return 'Q';
  63. case 3: return 'H';
  64. default: return '?';
  65. }
  66. }
  67. /**
  68. * @param mode qrcode mode
  69. * @returns a character corresponding to the mode
  70. */
  71. static char get_mode_char(uint8_t mode) {
  72. switch (mode) {
  73. case 0: return 'N';
  74. case 1: return 'A';
  75. case 2: return 'B';
  76. case 3: return 'K';
  77. default: return '?';
  78. }
  79. }
  80. /**
  81. * Render
  82. * @param canvas The canvas to render to
  83. * @param ctx Context provided to the callback by view_port_draw_callback_set
  84. */
  85. static void render_callback(Canvas* canvas, void* ctx) {
  86. furi_assert(canvas);
  87. furi_assert(ctx);
  88. QRCodeApp* instance = ctx;
  89. furi_check(furi_mutex_acquire(instance->mutex, FuriWaitForever) == FuriStatusOk);
  90. canvas_clear(canvas);
  91. canvas_set_color(canvas, ColorBlack);
  92. canvas_set_font(canvas, FontPrimary);
  93. uint8_t font_height = canvas_current_font_height(canvas);
  94. uint8_t width = canvas_width(canvas);
  95. uint8_t height = canvas_height(canvas);
  96. if (instance->qrcode) {
  97. uint8_t size = instance->qrcode->size;
  98. uint8_t pixel_size = height / size;
  99. uint8_t top = (height - pixel_size * size) / 2;
  100. uint8_t left = instance->show_stats ? top : (width - pixel_size * size) / 2;
  101. for (uint8_t y = 0; y < size; y++) {
  102. for (uint8_t x = 0; x < size; x++) {
  103. if (qrcode_getModule(instance->qrcode, x, y)) {
  104. if (pixel_size == 1) {
  105. canvas_draw_dot(canvas, left + x * pixel_size, top + y * pixel_size);
  106. } else {
  107. canvas_draw_box(canvas, left + x * pixel_size, top + y * pixel_size, pixel_size, pixel_size);
  108. }
  109. }
  110. }
  111. }
  112. if (instance->show_stats) {
  113. if (top < 2) top = 2;
  114. left += size * pixel_size + top;
  115. FuriString* str = furi_string_alloc();
  116. furi_string_printf(str, "Ver: %i", instance->qrcode->version);
  117. canvas_draw_str(canvas, left, top + font_height, furi_string_get_cstr(str));
  118. furi_string_printf(str, "ECC: %c", get_ecc_char(instance->qrcode->ecc));
  119. canvas_draw_str(canvas, left, 2 * (top + font_height), furi_string_get_cstr(str));
  120. furi_string_printf(str, "Mod: %c", get_mode_char(instance->qrcode->mode));
  121. canvas_draw_str(canvas, left, 3 * (top + font_height), furi_string_get_cstr(str));
  122. furi_string_free(str);
  123. }
  124. } else if (instance->loading) {
  125. canvas_draw_str_aligned(canvas, width / 2, height / 2, AlignCenter, AlignCenter, "Loading...");
  126. } else {
  127. uint8_t margin = (height - font_height * 2) / 3;
  128. canvas_draw_str_aligned(canvas, width / 2, margin, AlignCenter, AlignTop, "Could not load qrcode.");
  129. if (instance->too_long) {
  130. canvas_set_font(canvas, FontSecondary);
  131. canvas_draw_str(canvas, width / 2, margin * 2 + font_height, "Message is too long.");
  132. }
  133. }
  134. furi_mutex_release(instance->mutex);
  135. }
  136. /**
  137. * Handle input
  138. * @param input_event The received input event
  139. * @param ctx Context provided to the callback by view_port_input_callback_set
  140. */
  141. static void input_callback(InputEvent* input_event, void* ctx) {
  142. furi_assert(input_event);
  143. furi_assert(ctx);
  144. if (input_event->type == InputTypeShort) {
  145. QRCodeApp* instance = ctx;
  146. furi_message_queue_put(instance->input_queue, input_event, 0);
  147. }
  148. }
  149. /**
  150. * Determine if the given string is all numeric
  151. * @param str The string to test
  152. * @returns true if the string is all numeric
  153. */
  154. static bool is_numeric(const char* str, uint16_t len) {
  155. furi_assert(str);
  156. while (len > 0) {
  157. char c = str[--len];
  158. if (c < '0' || c > '9') return false;
  159. }
  160. return true;
  161. }
  162. /**
  163. * Determine if the given string is alphanumeric
  164. * @param str The string to test
  165. * @returns true if the string is alphanumeric
  166. */
  167. static bool is_alphanumeric(const char* str, uint16_t len) {
  168. furi_assert(str);
  169. while (len > 0) {
  170. char c = str[--len];
  171. if (c >= '0' && c <= '9') continue;
  172. if (c >= 'A' && c <= 'Z') continue;
  173. if (c == ' '
  174. || c == '$'
  175. || c == '%'
  176. || c == '*'
  177. || c == '+'
  178. || c == '-'
  179. || c == '.'
  180. || c == '/'
  181. || c == ':')
  182. continue;
  183. return false;
  184. }
  185. return true;
  186. }
  187. /**
  188. * Allocate a qrcode
  189. * @param version qrcode version
  190. * @returns an allocated QRCode
  191. */
  192. static QRCode* qrcode_alloc(uint8_t version) {
  193. QRCode* qrcode = malloc(sizeof(QRCode));
  194. qrcode->modules = malloc(qrcode_getBufferSize(version));
  195. return qrcode;
  196. }
  197. /**
  198. * Free a QRCode
  199. * @param qrcode The QRCode to free
  200. */
  201. static void qrcode_free(QRCode* qrcode) {
  202. furi_assert(qrcode);
  203. free(qrcode->modules);
  204. free(qrcode);
  205. }
  206. /**
  207. * Load a qrcode from a string
  208. * @param instance The qrcode app instance
  209. * @param str The message to encode as a qrcode
  210. * @returns true if the string was successfully loaded
  211. */
  212. static bool qrcode_load_string(QRCodeApp* instance, FuriString* str) {
  213. furi_assert(instance);
  214. furi_assert(str);
  215. furi_check(furi_mutex_acquire(instance->mutex, FuriWaitForever) == FuriStatusOk);
  216. if (instance->qrcode) {
  217. qrcode_free(instance->qrcode);
  218. instance->qrcode = NULL;
  219. }
  220. instance->too_long = false;
  221. instance->show_stats = false;
  222. bool result = false;
  223. do {
  224. const char* cstr = furi_string_get_cstr(str);
  225. uint16_t len = strlen(cstr);
  226. // figure out the qrcode "mode"
  227. uint8_t mode = MODE_BYTE;
  228. if (is_numeric(cstr, len)) mode = MODE_NUMERIC;
  229. else if (is_alphanumeric(cstr, len)) mode = MODE_ALPHANUMERIC;
  230. // Figure out the smallest qrcode version that'll fit all of the data -
  231. // we prefer the smallest version to maximize the pixel size of each
  232. // module to improve reader performance. Here, version is the 0-based
  233. // index. The qrcode_initBytes function will want a 1-based version
  234. // number, so we'll add one later.
  235. uint8_t ecc = ECC_LOW;
  236. uint8_t version = 0;
  237. while (version < MAX_QRCODE_VERSION && MAX_LENGTH[mode][ecc][version] < len) {
  238. version++;
  239. }
  240. if (version == MAX_QRCODE_VERSION) {
  241. instance->too_long = true;
  242. break;
  243. }
  244. // Figure out the maximum ECC we can use. I shouldn't need to
  245. // bounds-check ecc in this loop because I already know from the loop
  246. // above that ECC_LOW (0) works... don't forget to add one to that
  247. // version number...
  248. ecc = ECC_HIGH;
  249. while (MAX_LENGTH[mode][ecc][version] < len) {
  250. ecc--;
  251. }
  252. version++;
  253. // Build the qrcode
  254. instance->qrcode = qrcode_alloc(version);
  255. int8_t res = qrcode_initBytes(instance->qrcode, instance->qrcode->modules, version, ecc, (uint8_t*)cstr, len);
  256. if (res != 0) {
  257. FURI_LOG_E(TAG, "Could not create qrcode");
  258. qrcode_free(instance->qrcode);
  259. instance->qrcode = NULL;
  260. break;
  261. }
  262. result = true;
  263. } while (false);
  264. instance->loading = false;
  265. furi_mutex_release(instance->mutex);
  266. return result;
  267. }
  268. /**
  269. * Load a qrcode from a file
  270. * @param instance The qrcode app instance
  271. * @param file_path Path to the file to read
  272. * @returns true if the file was successfully loaded
  273. */
  274. static bool qrcode_load_file(QRCodeApp* instance, const char* file_path) {
  275. furi_assert(instance);
  276. furi_assert(file_path);
  277. FuriString* temp_str = furi_string_alloc();
  278. bool result = false;
  279. Storage* storage = furi_record_open(RECORD_STORAGE);
  280. FlipperFormat* file = flipper_format_file_alloc(storage);
  281. do {
  282. if (!flipper_format_file_open_existing(file, file_path)) break;
  283. uint32_t version = 0;
  284. if (!flipper_format_read_header(file, temp_str, &version)) break;
  285. if (furi_string_cmp_str(temp_str, QRCODE_FILETYPE)
  286. || version != QRCODE_FILE_VERSION) {
  287. FURI_LOG_E(TAG, "Incorrect file format or version");
  288. break;
  289. }
  290. if (!flipper_format_read_string(file, "Message", temp_str)) {
  291. FURI_LOG_E(TAG, "Message is missing");
  292. break;
  293. }
  294. if (!qrcode_load_string(instance, temp_str)) {
  295. break;
  296. }
  297. result = true;
  298. } while (false);
  299. furi_record_close(RECORD_STORAGE);
  300. flipper_format_free(file);
  301. furi_string_free(temp_str);
  302. return result;
  303. }
  304. /**
  305. * Allocate the qrcode app
  306. * @returns a qrcode app instance
  307. */
  308. static QRCodeApp* qrcode_app_alloc() {
  309. QRCodeApp* instance = malloc(sizeof(QRCodeApp));
  310. instance->input_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  311. instance->view_port = view_port_alloc();
  312. view_port_draw_callback_set(instance->view_port, render_callback, instance);
  313. view_port_input_callback_set(instance->view_port, input_callback, instance);
  314. instance->gui = furi_record_open(RECORD_GUI);
  315. gui_add_view_port(instance->gui, instance->view_port, GuiLayerFullscreen);
  316. instance->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  317. instance->qrcode = NULL;
  318. instance->loading = true;
  319. instance->too_long = false;
  320. instance->show_stats = false;
  321. return instance;
  322. }
  323. /**
  324. * Free the qrcode app
  325. * @param qrcode_app The app to free
  326. */
  327. static void qrcode_app_free(QRCodeApp* instance) {
  328. if (instance->qrcode) qrcode_free(instance->qrcode);
  329. gui_remove_view_port(instance->gui, instance->view_port);
  330. furi_record_close(RECORD_GUI);
  331. view_port_free(instance->view_port);
  332. furi_message_queue_free(instance->input_queue);
  333. furi_mutex_free(instance->mutex);
  334. free(instance);
  335. }
  336. /** App entrypoint */
  337. int32_t qrcode_app(void* p) {
  338. QRCodeApp* instance = qrcode_app_alloc();
  339. FuriString* file_path = furi_string_alloc();
  340. do {
  341. if (p && strlen(p)) {
  342. furi_string_set(file_path, (const char*)p);
  343. } else {
  344. furi_string_set(file_path, QRCODE_FOLDER);
  345. DialogsFileBrowserOptions browser_options;
  346. dialog_file_browser_set_basic_options(
  347. &browser_options, QRCODE_EXTENSION, &I_qrcode_10px);
  348. browser_options.hide_ext = true;
  349. browser_options.base_path = QRCODE_FOLDER;
  350. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  351. bool res = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
  352. furi_record_close(RECORD_DIALOGS);
  353. if (!res) {
  354. FURI_LOG_E(TAG, "No file selected");
  355. break;
  356. }
  357. }
  358. if (!qrcode_load_file(instance, furi_string_get_cstr(file_path))) {
  359. FURI_LOG_E(TAG, "Unable to load file");
  360. }
  361. InputEvent input;
  362. while (furi_message_queue_get(instance->input_queue, &input, FuriWaitForever) == FuriStatusOk) {
  363. furi_check(furi_mutex_acquire(instance->mutex, FuriWaitForever) == FuriStatusOk);
  364. if (input.key == InputKeyBack) {
  365. if (instance->qrcode) {
  366. qrcode_free(instance->qrcode);
  367. instance->qrcode = NULL;
  368. }
  369. instance->loading = true;
  370. furi_mutex_release(instance->mutex);
  371. break;
  372. } else if (input.key == InputKeyRight) {
  373. instance->show_stats = true;
  374. } else if (input.key == InputKeyLeft) {
  375. instance->show_stats = false;
  376. }
  377. furi_mutex_release(instance->mutex);
  378. view_port_update(instance->view_port);
  379. }
  380. if (p && strlen(p)) {
  381. // if started with an arg, exit instead
  382. // of looping back to the browser
  383. break;
  384. }
  385. } while (true);
  386. furi_string_free(file_path);
  387. qrcode_app_free(instance);
  388. return 0;
  389. }