weebo.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "weebo_i.h"
  2. #define TAG "weebo"
  3. #define WEEBO_KEY_RETAIL_FILENAME "key_retail"
  4. uint8_t original[NTAG215_SIZE];
  5. uint8_t plain_base[NFC3D_AMIIBO_SIZE];
  6. uint8_t modified[NTAG215_SIZE];
  7. void calculate_pwd(uint8_t* uid, uint8_t* pwd) {
  8. pwd[0] = uid[1] ^ uid[3] ^ 0xAA;
  9. pwd[1] = uid[2] ^ uid[4] ^ 0x55;
  10. pwd[2] = uid[3] ^ uid[5] ^ 0xAA;
  11. pwd[3] = uid[4] ^ uid[6] ^ 0x55;
  12. }
  13. bool weebo_load_key_retail(Weebo* weebo) {
  14. FuriString* path = furi_string_alloc();
  15. bool parsed = false;
  16. uint8_t buffer[160];
  17. memset(buffer, 0, sizeof(buffer));
  18. Storage* storage = furi_record_open(RECORD_STORAGE);
  19. Stream* stream = file_stream_alloc(storage);
  20. do {
  21. furi_string_printf(
  22. path, "%s/%s%s", STORAGE_APP_DATA_PATH_PREFIX, WEEBO_KEY_RETAIL_FILENAME, ".bin");
  23. bool opened =
  24. file_stream_open(stream, furi_string_get_cstr(path), FSAM_READ, FSOM_OPEN_EXISTING);
  25. if(!opened) {
  26. FURI_LOG_E(TAG, "Failed to open file");
  27. break;
  28. }
  29. size_t bytes_read = stream_read(stream, buffer, sizeof(buffer));
  30. if(bytes_read != sizeof(buffer)) {
  31. FURI_LOG_E(TAG, "Insufficient data");
  32. break;
  33. }
  34. memcpy(&weebo->amiiboKeys, buffer, bytes_read);
  35. // TODO: compare SHA1
  36. parsed = true;
  37. } while(false);
  38. file_stream_close(stream);
  39. furi_record_close(RECORD_STORAGE);
  40. furi_string_free(path);
  41. return parsed;
  42. }
  43. bool weebo_custom_event_callback(void* context, uint32_t event) {
  44. furi_assert(context);
  45. Weebo* weebo = context;
  46. return scene_manager_handle_custom_event(weebo->scene_manager, event);
  47. }
  48. bool weebo_back_event_callback(void* context) {
  49. furi_assert(context);
  50. Weebo* weebo = context;
  51. return scene_manager_handle_back_event(weebo->scene_manager);
  52. }
  53. void weebo_tick_event_callback(void* context) {
  54. furi_assert(context);
  55. Weebo* weebo = context;
  56. scene_manager_handle_tick_event(weebo->scene_manager);
  57. }
  58. Weebo* weebo_alloc() {
  59. Weebo* weebo = malloc(sizeof(Weebo));
  60. weebo->view_dispatcher = view_dispatcher_alloc();
  61. weebo->scene_manager = scene_manager_alloc(&weebo_scene_handlers, weebo);
  62. view_dispatcher_set_event_callback_context(weebo->view_dispatcher, weebo);
  63. view_dispatcher_set_custom_event_callback(weebo->view_dispatcher, weebo_custom_event_callback);
  64. view_dispatcher_set_navigation_event_callback(
  65. weebo->view_dispatcher, weebo_back_event_callback);
  66. view_dispatcher_set_tick_event_callback(
  67. weebo->view_dispatcher, weebo_tick_event_callback, 100);
  68. weebo->nfc = nfc_alloc();
  69. // Nfc device
  70. weebo->nfc_device = nfc_device_alloc();
  71. nfc_device_set_loading_callback(weebo->nfc_device, weebo_show_loading_popup, weebo);
  72. // Open GUI record
  73. weebo->gui = furi_record_open(RECORD_GUI);
  74. view_dispatcher_attach_to_gui(
  75. weebo->view_dispatcher, weebo->gui, ViewDispatcherTypeFullscreen);
  76. // Open Notification record
  77. weebo->notifications = furi_record_open(RECORD_NOTIFICATION);
  78. // Submenu
  79. weebo->submenu = submenu_alloc();
  80. view_dispatcher_add_view(
  81. weebo->view_dispatcher, WeeboViewMenu, submenu_get_view(weebo->submenu));
  82. // Popup
  83. weebo->popup = popup_alloc();
  84. view_dispatcher_add_view(weebo->view_dispatcher, WeeboViewPopup, popup_get_view(weebo->popup));
  85. // Loading
  86. weebo->loading = loading_alloc();
  87. view_dispatcher_add_view(
  88. weebo->view_dispatcher, WeeboViewLoading, loading_get_view(weebo->loading));
  89. // Text Input
  90. weebo->text_input = text_input_alloc();
  91. view_dispatcher_add_view(
  92. weebo->view_dispatcher, WeeboViewTextInput, text_input_get_view(weebo->text_input));
  93. // Number Input
  94. weebo->number_input = number_input_alloc();
  95. view_dispatcher_add_view(
  96. weebo->view_dispatcher, WeeboViewNumberInput, number_input_get_view(weebo->number_input));
  97. // TextBox
  98. weebo->text_box = text_box_alloc();
  99. view_dispatcher_add_view(
  100. weebo->view_dispatcher, WeeboViewTextBox, text_box_get_view(weebo->text_box));
  101. weebo->text_box_store = furi_string_alloc();
  102. // Custom Widget
  103. weebo->widget = widget_alloc();
  104. view_dispatcher_add_view(
  105. weebo->view_dispatcher, WeeboViewWidget, widget_get_view(weebo->widget));
  106. weebo->storage = furi_record_open(RECORD_STORAGE);
  107. weebo->dialogs = furi_record_open(RECORD_DIALOGS);
  108. weebo->load_path = furi_string_alloc();
  109. weebo->keys_loaded = false;
  110. return weebo;
  111. }
  112. void weebo_free(Weebo* weebo) {
  113. furi_assert(weebo);
  114. nfc_free(weebo->nfc);
  115. // Nfc device
  116. nfc_device_free(weebo->nfc_device);
  117. // Submenu
  118. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewMenu);
  119. submenu_free(weebo->submenu);
  120. // Popup
  121. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewPopup);
  122. popup_free(weebo->popup);
  123. // Loading
  124. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewLoading);
  125. loading_free(weebo->loading);
  126. // TextInput
  127. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewTextInput);
  128. text_input_free(weebo->text_input);
  129. // NumberInput
  130. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewNumberInput);
  131. number_input_free(weebo->number_input);
  132. // TextBox
  133. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewTextBox);
  134. text_box_free(weebo->text_box);
  135. furi_string_free(weebo->text_box_store);
  136. // Custom Widget
  137. view_dispatcher_remove_view(weebo->view_dispatcher, WeeboViewWidget);
  138. widget_free(weebo->widget);
  139. // View Dispatcher
  140. view_dispatcher_free(weebo->view_dispatcher);
  141. // Scene Manager
  142. scene_manager_free(weebo->scene_manager);
  143. // GUI
  144. furi_record_close(RECORD_GUI);
  145. weebo->gui = NULL;
  146. // Notifications
  147. furi_record_close(RECORD_NOTIFICATION);
  148. weebo->notifications = NULL;
  149. furi_string_free(weebo->load_path);
  150. furi_record_close(RECORD_STORAGE);
  151. furi_record_close(RECORD_DIALOGS);
  152. free(weebo);
  153. }
  154. void weebo_text_store_set(Weebo* weebo, const char* text, ...) {
  155. va_list args;
  156. va_start(args, text);
  157. vsnprintf(weebo->text_store, sizeof(weebo->text_store), text, args);
  158. va_end(args);
  159. }
  160. void weebo_text_store_clear(Weebo* weebo) {
  161. memset(weebo->text_store, 0, sizeof(weebo->text_store));
  162. }
  163. static const NotificationSequence weebo_sequence_blink_start_blue = {
  164. &message_blink_start_10,
  165. &message_blink_set_color_blue,
  166. &message_do_not_reset,
  167. NULL,
  168. };
  169. static const NotificationSequence weebo_sequence_blink_stop = {
  170. &message_blink_stop,
  171. NULL,
  172. };
  173. void weebo_blink_start(Weebo* weebo) {
  174. notification_message(weebo->notifications, &weebo_sequence_blink_start_blue);
  175. }
  176. void weebo_blink_stop(Weebo* weebo) {
  177. notification_message(weebo->notifications, &weebo_sequence_blink_stop);
  178. }
  179. void weebo_show_loading_popup(void* context, bool show) {
  180. Weebo* weebo = context;
  181. if(show) {
  182. // Raise timer priority so that animations can play
  183. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  184. view_dispatcher_switch_to_view(weebo->view_dispatcher, WeeboViewLoading);
  185. } else {
  186. // Restore default timer priority
  187. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  188. }
  189. }
  190. int32_t weebo_app(void* p) {
  191. UNUSED(p);
  192. Weebo* weebo = weebo_alloc();
  193. weebo->keys_loaded = weebo_load_key_retail(weebo);
  194. if(weebo->keys_loaded) {
  195. scene_manager_next_scene(weebo->scene_manager, WeeboSceneMainMenu);
  196. } else {
  197. scene_manager_next_scene(weebo->scene_manager, WeeboSceneKeysMissing);
  198. }
  199. view_dispatcher_run(weebo->view_dispatcher);
  200. weebo_free(weebo);
  201. return 0;
  202. }