weebo.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. #include "weebo_i.h"
  2. #define TAG "weebo"
  3. #define WEEBO_KEY_RETAIL_FILENAME "key_retail"
  4. #define FIGURE_ID_LIST APP_ASSETS_PATH("figure_ids.nfc")
  5. #define UNPACKED_FIGURE_ID 0x1dc
  6. #define NFC_APP_EXTENSION ".nfc"
  7. #define NFC_APP_PATH_PREFIX "/ext/nfc"
  8. static const char* nfc_resources_header = "Flipper NFC resources";
  9. static const uint32_t nfc_resources_file_version = 1;
  10. bool weebo_load_key_retail(Weebo* weebo) {
  11. FuriString* path = furi_string_alloc();
  12. bool parsed = false;
  13. uint8_t buffer[160];
  14. memset(buffer, 0, sizeof(buffer));
  15. Storage* storage = furi_record_open(RECORD_STORAGE);
  16. Stream* stream = file_stream_alloc(storage);
  17. do {
  18. furi_string_printf(
  19. path, "%s/%s%s", STORAGE_APP_DATA_PATH_PREFIX, WEEBO_KEY_RETAIL_FILENAME, ".bin");
  20. bool opened =
  21. file_stream_open(stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING);
  22. if(!opened) {
  23. FURI_LOG_E(TAG, "Failed to open file");
  24. break;
  25. }
  26. size_t bytes_read = stream_read(stream, buffer, sizeof(buffer));
  27. if(bytes_read != sizeof(buffer)) {
  28. FURI_LOG_E(TAG, "Insufficient data");
  29. break;
  30. }
  31. memcpy(&weebo->keys, buffer, bytes_read);
  32. // TODO: compare SHA1
  33. parsed = true;
  34. } while(false);
  35. file_stream_close(stream);
  36. furi_record_close(RECORD_STORAGE);
  37. furi_string_free(path);
  38. return parsed;
  39. }
  40. bool weebo_load_figure(Weebo* weebo, FuriString* path, bool show_dialog) {
  41. bool parsed = false;
  42. FuriString* reason = furi_string_alloc_set("Couldn't load file");
  43. uint8_t buffer[NTAG215_SIZE];
  44. memset(buffer, 0, sizeof(buffer));
  45. if(weebo->loading_cb) {
  46. weebo->loading_cb(weebo->loading_cb_ctx, true);
  47. }
  48. do {
  49. NfcDevice* nfc_device = weebo->nfc_device;
  50. if(!nfc_device_load(nfc_device, furi_string_get_cstr(path))) break;
  51. NfcProtocol protocol = nfc_device_get_protocol(nfc_device);
  52. if(protocol != NfcProtocolMfUltralight) {
  53. furi_string_printf(reason, "Not Ultralight protocol");
  54. break;
  55. }
  56. const MfUltralightData* data = nfc_device_get_data(nfc_device, NfcProtocolMfUltralight);
  57. if(data->type != MfUltralightTypeNTAG215) {
  58. furi_string_printf(reason, "Not NTAG215");
  59. break;
  60. }
  61. if(!mf_ultralight_is_all_data_read(data)) {
  62. furi_string_printf(reason, "Incomplete data");
  63. break;
  64. }
  65. uint8_t* uid = data->iso14443_3a_data->uid;
  66. uint8_t pwd[4];
  67. weebo_calculate_pwd(uid, pwd);
  68. if(memcmp(data->page[133].data, pwd, sizeof(pwd)) != 0) {
  69. furi_string_printf(reason, "Wrong password");
  70. break;
  71. }
  72. for(size_t i = 0; i < 135; i++) {
  73. memcpy(
  74. buffer + i * MF_ULTRALIGHT_PAGE_SIZE, data->page[i].data, MF_ULTRALIGHT_PAGE_SIZE);
  75. }
  76. if(!nfc3d_amiibo_unpack(&weebo->keys, buffer, weebo->figure)) {
  77. FURI_LOG_E(TAG, "Failed to unpack");
  78. break;
  79. }
  80. parsed = true;
  81. } while(false);
  82. if(weebo->loading_cb) {
  83. weebo->loading_cb(weebo->loading_cb_ctx, false);
  84. }
  85. if((!parsed) && (show_dialog)) {
  86. dialog_message_show_storage_error(weebo->dialogs, furi_string_get_cstr(reason));
  87. }
  88. furi_string_free(reason);
  89. return parsed;
  90. }
  91. static bool
  92. weebo_search_data(Storage* storage, const char* file_name, FuriString* key, FuriString* data) {
  93. bool parsed = false;
  94. FlipperFormat* file = flipper_format_file_alloc(storage);
  95. FuriString* temp_str;
  96. temp_str = furi_string_alloc();
  97. do {
  98. // Open file
  99. if(!flipper_format_file_open_existing(file, file_name)) break;
  100. // Read file header and version
  101. uint32_t version = 0;
  102. if(!flipper_format_read_header(file, temp_str, &version)) break;
  103. if(furi_string_cmp_str(temp_str, nfc_resources_header) ||
  104. (version != nfc_resources_file_version))
  105. break;
  106. if(!flipper_format_read_string(file, furi_string_get_cstr(key), data)) break;
  107. parsed = true;
  108. } while(false);
  109. furi_string_free(temp_str);
  110. flipper_format_free(file);
  111. return parsed;
  112. }
  113. uint16_t weebo_get_figure_id(Weebo* weebo) {
  114. uint16_t id = 0;
  115. id |= weebo->figure[UNPACKED_FIGURE_ID + 0] << 8;
  116. id |= weebo->figure[UNPACKED_FIGURE_ID + 1] << 0;
  117. FURI_LOG_D(TAG, "id = %04x", id);
  118. return id;
  119. }
  120. bool weebo_get_figure_form(Weebo* weebo, FuriString* name) {
  121. bool parsed = false;
  122. uint8_t form = weebo->figure[UNPACKED_FIGURE_ID + 3];
  123. FURI_LOG_D(TAG, "form = %02x", form);
  124. switch(form) {
  125. case 0x00:
  126. furi_string_set_str(name, "Figure");
  127. parsed = true;
  128. break;
  129. case 0x01:
  130. furi_string_set_str(name, "Card");
  131. parsed = true;
  132. break;
  133. case 0x02:
  134. furi_string_set_str(name, "Yarn");
  135. parsed = true;
  136. break;
  137. default:
  138. break;
  139. }
  140. return parsed;
  141. }
  142. bool weebo_get_figure_series(Weebo* weebo, FuriString* name) {
  143. bool parsed = false;
  144. uint8_t series_id = weebo->figure[UNPACKED_FIGURE_ID + 6];
  145. switch(series_id) {
  146. case 0x00:
  147. furi_string_set_str(name, "Smash Bros");
  148. parsed = true;
  149. break;
  150. case 0x01:
  151. furi_string_set_str(name, "Mario Bros");
  152. parsed = true;
  153. break;
  154. case 0x02:
  155. furi_string_set_str(name, "Chibi Robo");
  156. parsed = true;
  157. break;
  158. case 0x03:
  159. furi_string_set_str(name, "Yarn Yoshi");
  160. parsed = true;
  161. break;
  162. case 0x04:
  163. furi_string_set_str(name, "Splatoon");
  164. parsed = true;
  165. break;
  166. case 0x05:
  167. furi_string_set_str(name, "Animal Crossing");
  168. parsed = true;
  169. break;
  170. case 0x06:
  171. furi_string_set_str(name, "8-bit Mario");
  172. parsed = true;
  173. break;
  174. case 0x07:
  175. furi_string_set_str(name, "Skylanders");
  176. parsed = true;
  177. break;
  178. case 0x09:
  179. furi_string_set_str(name, "Legend of Zelda");
  180. parsed = true;
  181. break;
  182. case 0x0A:
  183. furi_string_set_str(name, "Shovel Knight");
  184. parsed = true;
  185. break;
  186. case 0x0C:
  187. furi_string_set_str(name, "Kirby");
  188. parsed = true;
  189. break;
  190. case 0x0D:
  191. furi_string_set_str(name, "Pokken");
  192. parsed = true;
  193. break;
  194. case 0x0F:
  195. furi_string_set_str(name, "Monster Hunter");
  196. parsed = true;
  197. break;
  198. default:
  199. break;
  200. }
  201. return parsed;
  202. }
  203. bool weebo_get_figure_name(Weebo* weebo, FuriString* name) {
  204. bool parsed = false;
  205. uint16_t id = weebo_get_figure_id(weebo);
  206. FuriString* key = furi_string_alloc_printf("%04x", id);
  207. if(weebo_search_data(weebo->storage, FIGURE_ID_LIST, key, name)) {
  208. parsed = true;
  209. }
  210. furi_string_free(key);
  211. return parsed;
  212. }
  213. void weebo_set_loading_callback(Weebo* weebo, WeeboLoadingCallback callback, void* context) {
  214. furi_assert(weebo);
  215. weebo->loading_cb = callback;
  216. weebo->loading_cb_ctx = context;
  217. }
  218. bool weebo_file_select(Weebo* weebo) {
  219. furi_assert(weebo);
  220. bool res = false;
  221. FuriString* weebo_app_folder;
  222. if(storage_dir_exists(weebo->storage, "/ext/nfc/SmashAmiibo")) {
  223. weebo_app_folder = furi_string_alloc_set("/ext/nfc/SmashAmiibo");
  224. } else if(storage_dir_exists(weebo->storage, "/ext/nfc/Amiibo")) {
  225. weebo_app_folder = furi_string_alloc_set("/ext/nfc/Amiibo");
  226. } else if(storage_dir_exists(weebo->storage, "/ext/nfc/Amiibos")) {
  227. weebo_app_folder = furi_string_alloc_set("/ext/nfc/Amiibos");
  228. } else if(storage_dir_exists(weebo->storage, "/ext/nfc/amiibo")) {
  229. weebo_app_folder = furi_string_alloc_set("/ext/nfc/amiibo");
  230. } else if(storage_dir_exists(weebo->storage, "/ext/nfc/amiibos")) {
  231. weebo_app_folder = furi_string_alloc_set("/ext/nfc/amiibos");
  232. } else {
  233. weebo_app_folder = furi_string_alloc_set(NFC_APP_PATH_PREFIX);
  234. }
  235. DialogsFileBrowserOptions browser_options;
  236. dialog_file_browser_set_basic_options(&browser_options, NFC_APP_EXTENSION, &I_Nfc_10px);
  237. browser_options.base_path = NFC_APP_PATH_PREFIX;
  238. res = dialog_file_browser_show(
  239. weebo->dialogs, weebo->load_path, weebo_app_folder, &browser_options);
  240. furi_string_free(weebo_app_folder);
  241. if(res) {
  242. FuriString* filename;
  243. filename = furi_string_alloc();
  244. path_extract_filename(weebo->load_path, filename, true);
  245. strncpy(weebo->file_name, furi_string_get_cstr(filename), WEEBO_FILE_NAME_MAX_LENGTH);
  246. res = weebo_load_figure(weebo, weebo->load_path, true);
  247. furi_string_free(filename);
  248. }
  249. return res;
  250. }
  251. bool weebo_custom_event_callback(void* context, uint32_t event) {
  252. furi_assert(context);
  253. Weebo* weebo = context;
  254. return scene_manager_handle_custom_event(weebo->scene_manager, event);
  255. }
  256. bool weebo_back_event_callback(void* context) {
  257. furi_assert(context);
  258. Weebo* weebo = context;
  259. return scene_manager_handle_back_event(weebo->scene_manager);
  260. }
  261. void weebo_tick_event_callback(void* context) {
  262. furi_assert(context);
  263. Weebo* weebo = context;
  264. scene_manager_handle_tick_event(weebo->scene_manager);
  265. }
  266. Weebo* weebo_alloc() {
  267. Weebo* weebo = malloc(sizeof(Weebo));
  268. weebo->view_dispatcher = view_dispatcher_alloc();
  269. weebo->scene_manager = scene_manager_alloc(&weebo_scene_handlers, weebo);
  270. view_dispatcher_set_event_callback_context(weebo->view_dispatcher, weebo);
  271. view_dispatcher_set_custom_event_callback(weebo->view_dispatcher, weebo_custom_event_callback);
  272. view_dispatcher_set_navigation_event_callback(
  273. weebo->view_dispatcher, weebo_back_event_callback);
  274. view_dispatcher_set_tick_event_callback(
  275. weebo->view_dispatcher, weebo_tick_event_callback, 100);
  276. weebo->nfc = nfc_alloc();
  277. // Nfc device
  278. weebo->nfc_device = nfc_device_alloc();
  279. nfc_device_set_loading_callback(weebo->nfc_device, weebo_show_loading_popup, weebo);
  280. // Open GUI record
  281. weebo->gui = furi_record_open(RECORD_GUI);
  282. view_dispatcher_attach_to_gui(
  283. weebo->view_dispatcher, weebo->gui, ViewDispatcherTypeFullscreen);
  284. // Open Notification record
  285. weebo->notifications = furi_record_open(RECORD_NOTIFICATION);
  286. // Submenu
  287. weebo->submenu = submenu_alloc();
  288. view_dispatcher_add_view(
  289. weebo->view_dispatcher, WeeboViewMenu, submenu_get_view(weebo->submenu));
  290. // Popup
  291. weebo->popup = popup_alloc();
  292. view_dispatcher_add_view(weebo->view_dispatcher, WeeboViewPopup, popup_get_view(weebo->popup));
  293. // Loading
  294. weebo->loading = loading_alloc();
  295. view_dispatcher_add_view(
  296. weebo->view_dispatcher, WeeboViewLoading, loading_get_view(weebo->loading));
  297. // Text Input
  298. weebo->text_input = text_input_alloc();
  299. view_dispatcher_add_view(
  300. weebo->view_dispatcher, WeeboViewTextInput, text_input_get_view(weebo->text_input));
  301. // Number Input
  302. weebo->number_input = number_input_alloc();
  303. view_dispatcher_add_view(
  304. weebo->view_dispatcher, WeeboViewNumberInput, number_input_get_view(weebo->number_input));
  305. // TextBox
  306. weebo->text_box = text_box_alloc();
  307. view_dispatcher_add_view(
  308. weebo->view_dispatcher, WeeboViewTextBox, text_box_get_view(weebo->text_box));
  309. weebo->text_box_store = furi_string_alloc();
  310. // Custom Widget
  311. weebo->widget = widget_alloc();
  312. view_dispatcher_add_view(
  313. weebo->view_dispatcher, WeeboViewWidget, widget_get_view(weebo->widget));
  314. weebo->storage = furi_record_open(RECORD_STORAGE);
  315. weebo->dialogs = furi_record_open(RECORD_DIALOGS);
  316. weebo->load_path = furi_string_alloc();
  317. weebo->keys_loaded = false;
  318. return weebo;
  319. }
  320. void weebo_free(Weebo* weebo) {
  321. furi_assert(weebo);
  322. nfc_free(weebo->nfc);
  323. // Nfc device
  324. nfc_device_free(weebo->nfc_device);
  325. // Submenu
  326. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewMenu);
  327. submenu_free(weebo->submenu);
  328. // Popup
  329. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewPopup);
  330. popup_free(weebo->popup);
  331. // Loading
  332. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewLoading);
  333. loading_free(weebo->loading);
  334. // TextInput
  335. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewTextInput);
  336. text_input_free(weebo->text_input);
  337. // NumberInput
  338. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewNumberInput);
  339. number_input_free(weebo->number_input);
  340. // TextBox
  341. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewTextBox);
  342. text_box_free(weebo->text_box);
  343. furi_string_free(weebo->text_box_store);
  344. // Custom Widget
  345. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewWidget);
  346. widget_free(weebo->widget);
  347. // View Dispatcher
  348. view_dispatcher_free(weebo->view_dispatcher);
  349. // Scene Manager
  350. scene_manager_free(weebo->scene_manager);
  351. // GUI
  352. furi_record_close(RECORD_GUI);
  353. weebo->gui = NULL;
  354. // Notifications
  355. furi_record_close(RECORD_NOTIFICATION);
  356. weebo->notifications = NULL;
  357. furi_string_free(weebo->load_path);
  358. furi_record_close(RECORD_STORAGE);
  359. furi_record_close(RECORD_DIALOGS);
  360. free(weebo);
  361. }
  362. void weebo_text_store_set(Weebo* weebo, const char* text, ...) {
  363. va_list args;
  364. va_start(args, text);
  365. vsnprintf(weebo->text_store, sizeof(weebo->text_store), text, args);
  366. va_end(args);
  367. }
  368. void weebo_text_store_clear(Weebo* weebo) {
  369. memset(weebo->text_store, 0, sizeof(weebo->text_store));
  370. }
  371. static const NotificationSequence weebo_sequence_blink_start_blue = {
  372. &message_blink_start_10,
  373. &message_blink_set_color_blue,
  374. &message_do_not_reset,
  375. NULL,
  376. };
  377. static const NotificationSequence weebo_sequence_blink_stop = {
  378. &message_blink_stop,
  379. NULL,
  380. };
  381. void weebo_blink_start(Weebo* weebo) {
  382. notification_message(weebo->notifications, &weebo_sequence_blink_start_blue);
  383. }
  384. void weebo_blink_stop(Weebo* weebo) {
  385. notification_message(weebo->notifications, &weebo_sequence_blink_stop);
  386. }
  387. void weebo_show_loading_popup(void* context, bool show) {
  388. Weebo* weebo = context;
  389. if(show) {
  390. // Raise timer priority so that animations can play
  391. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  392. view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewLoading);
  393. } else {
  394. // Restore default timer priority
  395. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  396. }
  397. }
  398. int32_t weebo_app(void* p) {
  399. UNUSED(p);
  400. Weebo* weebo = weebo_alloc();
  401. weebo->keys_loaded = weebo_load_key_retail(weebo);
  402. if(weebo->keys_loaded) {
  403. scene_manager_next_scene(weebo->scene_manager, WeeboSceneMainMenu);
  404. } else {
  405. scene_manager_next_scene(weebo->scene_manager, WeeboSceneKeysMissing);
  406. }
  407. view_dispatcher_run(weebo->view_dispatcher);
  408. weebo_free(weebo);
  409. return 0;
  410. }