weebo.c 13 KB

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