weebo.c 12 KB

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