weebo.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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_name(Weebo* weebo, FuriString* name) {
  143. bool parsed = false;
  144. uint16_t id = weebo_get_figure_id(weebo);
  145. FuriString* key = furi_string_alloc_printf("%04x", id);
  146. if(weebo_search_data(weebo->storage, FIGURE_ID_LIST, key, name)) {
  147. parsed = true;
  148. }
  149. furi_string_free(key);
  150. return parsed;
  151. }
  152. void weebo_set_loading_callback(Weebo* weebo, WeeboLoadingCallback callback, void* context) {
  153. furi_assert(weebo);
  154. weebo->loading_cb = callback;
  155. weebo->loading_cb_ctx = context;
  156. }
  157. bool weebo_file_select(Weebo* weebo) {
  158. furi_assert(weebo);
  159. bool res = false;
  160. FuriString* weebo_app_folder;
  161. if(storage_dir_exists(weebo->storage, "/ext/nfc/SmashAmiibo")) {
  162. weebo_app_folder = furi_string_alloc_set("/ext/nfc/SmashAmiibo");
  163. } else if(storage_dir_exists(weebo->storage, "/ext/nfc/Amiibo")) {
  164. weebo_app_folder = furi_string_alloc_set("/ext/nfc/Amiibo");
  165. } else if(storage_dir_exists(weebo->storage, "/ext/nfc/Amiibos")) {
  166. weebo_app_folder = furi_string_alloc_set("/ext/nfc/Amiibos");
  167. } else if(storage_dir_exists(weebo->storage, "/ext/nfc/amiibo")) {
  168. weebo_app_folder = furi_string_alloc_set("/ext/nfc/amiibo");
  169. } else if(storage_dir_exists(weebo->storage, "/ext/nfc/amiibos")) {
  170. weebo_app_folder = furi_string_alloc_set("/ext/nfc/amiibos");
  171. } else {
  172. weebo_app_folder = furi_string_alloc_set(NFC_APP_PATH_PREFIX);
  173. }
  174. DialogsFileBrowserOptions browser_options;
  175. dialog_file_browser_set_basic_options(&browser_options, NFC_APP_EXTENSION, &I_Nfc_10px);
  176. browser_options.base_path = NFC_APP_PATH_PREFIX;
  177. res = dialog_file_browser_show(
  178. weebo->dialogs, weebo->load_path, weebo_app_folder, &browser_options);
  179. furi_string_free(weebo_app_folder);
  180. if(res) {
  181. FuriString* filename;
  182. filename = furi_string_alloc();
  183. path_extract_filename(weebo->load_path, filename, true);
  184. strncpy(weebo->file_name, furi_string_get_cstr(filename), WEEBO_FILE_NAME_MAX_LENGTH);
  185. res = weebo_load_figure(weebo, weebo->load_path, true);
  186. furi_string_free(filename);
  187. }
  188. return res;
  189. }
  190. bool weebo_custom_event_callback(void* context, uint32_t event) {
  191. furi_assert(context);
  192. Weebo* weebo = context;
  193. return scene_manager_handle_custom_event(weebo->scene_manager, event);
  194. }
  195. bool weebo_back_event_callback(void* context) {
  196. furi_assert(context);
  197. Weebo* weebo = context;
  198. return scene_manager_handle_back_event(weebo->scene_manager);
  199. }
  200. void weebo_tick_event_callback(void* context) {
  201. furi_assert(context);
  202. Weebo* weebo = context;
  203. scene_manager_handle_tick_event(weebo->scene_manager);
  204. }
  205. Weebo* weebo_alloc() {
  206. Weebo* weebo = malloc(sizeof(Weebo));
  207. weebo->view_dispatcher = view_dispatcher_alloc();
  208. weebo->scene_manager = scene_manager_alloc(&weebo_scene_handlers, weebo);
  209. view_dispatcher_set_event_callback_context(weebo->view_dispatcher, weebo);
  210. view_dispatcher_set_custom_event_callback(weebo->view_dispatcher, weebo_custom_event_callback);
  211. view_dispatcher_set_navigation_event_callback(
  212. weebo->view_dispatcher, weebo_back_event_callback);
  213. view_dispatcher_set_tick_event_callback(
  214. weebo->view_dispatcher, weebo_tick_event_callback, 100);
  215. weebo->nfc = nfc_alloc();
  216. // Nfc device
  217. weebo->nfc_device = nfc_device_alloc();
  218. nfc_device_set_loading_callback(weebo->nfc_device, weebo_show_loading_popup, weebo);
  219. // Open GUI record
  220. weebo->gui = furi_record_open(RECORD_GUI);
  221. view_dispatcher_attach_to_gui(
  222. weebo->view_dispatcher, weebo->gui, ViewDispatcherTypeFullscreen);
  223. // Open Notification record
  224. weebo->notifications = furi_record_open(RECORD_NOTIFICATION);
  225. // Submenu
  226. weebo->submenu = submenu_alloc();
  227. view_dispatcher_add_view(
  228. weebo->view_dispatcher, WeeboViewMenu, submenu_get_view(weebo->submenu));
  229. // Popup
  230. weebo->popup = popup_alloc();
  231. view_dispatcher_add_view(weebo->view_dispatcher, WeeboViewPopup, popup_get_view(weebo->popup));
  232. // Loading
  233. weebo->loading = loading_alloc();
  234. view_dispatcher_add_view(
  235. weebo->view_dispatcher, WeeboViewLoading, loading_get_view(weebo->loading));
  236. // Text Input
  237. weebo->text_input = text_input_alloc();
  238. view_dispatcher_add_view(
  239. weebo->view_dispatcher, WeeboViewTextInput, text_input_get_view(weebo->text_input));
  240. // Number Input
  241. weebo->number_input = number_input_alloc();
  242. view_dispatcher_add_view(
  243. weebo->view_dispatcher, WeeboViewNumberInput, number_input_get_view(weebo->number_input));
  244. // TextBox
  245. weebo->text_box = text_box_alloc();
  246. view_dispatcher_add_view(
  247. weebo->view_dispatcher, WeeboViewTextBox, text_box_get_view(weebo->text_box));
  248. weebo->text_box_store = furi_string_alloc();
  249. // Custom Widget
  250. weebo->widget = widget_alloc();
  251. view_dispatcher_add_view(
  252. weebo->view_dispatcher, WeeboViewWidget, widget_get_view(weebo->widget));
  253. weebo->storage = furi_record_open(RECORD_STORAGE);
  254. weebo->dialogs = furi_record_open(RECORD_DIALOGS);
  255. weebo->load_path = furi_string_alloc();
  256. weebo->keys_loaded = false;
  257. return weebo;
  258. }
  259. void weebo_free(Weebo* weebo) {
  260. furi_assert(weebo);
  261. nfc_free(weebo->nfc);
  262. // Nfc device
  263. nfc_device_free(weebo->nfc_device);
  264. // Submenu
  265. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewMenu);
  266. submenu_free(weebo->submenu);
  267. // Popup
  268. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewPopup);
  269. popup_free(weebo->popup);
  270. // Loading
  271. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewLoading);
  272. loading_free(weebo->loading);
  273. // TextInput
  274. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewTextInput);
  275. text_input_free(weebo->text_input);
  276. // NumberInput
  277. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewNumberInput);
  278. number_input_free(weebo->number_input);
  279. // TextBox
  280. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewTextBox);
  281. text_box_free(weebo->text_box);
  282. furi_string_free(weebo->text_box_store);
  283. // Custom Widget
  284. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewWidget);
  285. widget_free(weebo->widget);
  286. // View Dispatcher
  287. view_dispatcher_free(weebo->view_dispatcher);
  288. // Scene Manager
  289. scene_manager_free(weebo->scene_manager);
  290. // GUI
  291. furi_record_close(RECORD_GUI);
  292. weebo->gui = NULL;
  293. // Notifications
  294. furi_record_close(RECORD_NOTIFICATION);
  295. weebo->notifications = NULL;
  296. furi_string_free(weebo->load_path);
  297. furi_record_close(RECORD_STORAGE);
  298. furi_record_close(RECORD_DIALOGS);
  299. free(weebo);
  300. }
  301. void weebo_text_store_set(Weebo* weebo, const char* text, ...) {
  302. va_list args;
  303. va_start(args, text);
  304. vsnprintf(weebo->text_store, sizeof(weebo->text_store), text, args);
  305. va_end(args);
  306. }
  307. void weebo_text_store_clear(Weebo* weebo) {
  308. memset(weebo->text_store, 0, sizeof(weebo->text_store));
  309. }
  310. static const NotificationSequence weebo_sequence_blink_start_blue = {
  311. &message_blink_start_10,
  312. &message_blink_set_color_blue,
  313. &message_do_not_reset,
  314. NULL,
  315. };
  316. static const NotificationSequence weebo_sequence_blink_stop = {
  317. &message_blink_stop,
  318. NULL,
  319. };
  320. void weebo_blink_start(Weebo* weebo) {
  321. notification_message(weebo->notifications, &weebo_sequence_blink_start_blue);
  322. }
  323. void weebo_blink_stop(Weebo* weebo) {
  324. notification_message(weebo->notifications, &weebo_sequence_blink_stop);
  325. }
  326. void weebo_show_loading_popup(void* context, bool show) {
  327. Weebo* weebo = context;
  328. if(show) {
  329. // Raise timer priority so that animations can play
  330. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  331. view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewLoading);
  332. } else {
  333. // Restore default timer priority
  334. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  335. }
  336. }
  337. int32_t weebo_app(void* p) {
  338. UNUSED(p);
  339. Weebo* weebo = weebo_alloc();
  340. weebo->keys_loaded = weebo_load_key_retail(weebo);
  341. if(weebo->keys_loaded) {
  342. scene_manager_next_scene(weebo->scene_manager, WeeboSceneMainMenu);
  343. } else {
  344. scene_manager_next_scene(weebo->scene_manager, WeeboSceneKeysMissing);
  345. }
  346. view_dispatcher_run(weebo->view_dispatcher);
  347. weebo_free(weebo);
  348. return 0;
  349. }