ibutton.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include "ibutton_i.h"
  2. #include <toolbox/path.h>
  3. #include <dolphin/dolphin.h>
  4. #define TAG "iButtonApp"
  5. static const NotificationSequence sequence_blink_set_yellow = {
  6. &message_blink_set_color_yellow,
  7. NULL,
  8. };
  9. static const NotificationSequence sequence_blink_set_magenta = {
  10. &message_blink_set_color_magenta,
  11. NULL,
  12. };
  13. static const NotificationSequence* ibutton_notification_sequences[] = {
  14. &sequence_error,
  15. &sequence_success,
  16. &sequence_blink_start_cyan,
  17. &sequence_blink_start_magenta,
  18. &sequence_blink_set_yellow,
  19. &sequence_blink_set_magenta,
  20. &sequence_set_red_255,
  21. &sequence_reset_red,
  22. &sequence_set_green_255,
  23. &sequence_reset_green,
  24. &sequence_blink_stop,
  25. };
  26. static void ibutton_make_app_folder(iButton* ibutton) {
  27. Storage* storage = furi_record_open(RECORD_STORAGE);
  28. if(!storage_simply_mkdir(storage, IBUTTON_APP_FOLDER)) {
  29. dialog_message_show_storage_error(ibutton->dialogs, "Cannot create\napp folder");
  30. }
  31. furi_record_close(RECORD_STORAGE);
  32. }
  33. static void ibutton_rpc_command_callback(RpcAppSystemEvent event, void* context) {
  34. furi_assert(context);
  35. iButton* ibutton = context;
  36. if(event == RpcAppEventSessionClose) {
  37. view_dispatcher_send_custom_event(
  38. ibutton->view_dispatcher, iButtonCustomEventRpcSessionClose);
  39. rpc_system_app_set_callback(ibutton->rpc, NULL, NULL);
  40. ibutton->rpc = NULL;
  41. } else if(event == RpcAppEventAppExit) {
  42. view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventRpcExit);
  43. } else if(event == RpcAppEventLoadFile) {
  44. view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventRpcLoad);
  45. } else {
  46. rpc_system_app_confirm(ibutton->rpc, event, false);
  47. }
  48. }
  49. bool ibutton_custom_event_callback(void* context, uint32_t event) {
  50. furi_assert(context);
  51. iButton* ibutton = context;
  52. return scene_manager_handle_custom_event(ibutton->scene_manager, event);
  53. }
  54. bool ibutton_back_event_callback(void* context) {
  55. furi_assert(context);
  56. iButton* ibutton = context;
  57. return scene_manager_handle_back_event(ibutton->scene_manager);
  58. }
  59. void ibutton_tick_event_callback(void* context) {
  60. furi_assert(context);
  61. iButton* ibutton = context;
  62. scene_manager_handle_tick_event(ibutton->scene_manager);
  63. }
  64. iButton* ibutton_alloc() {
  65. iButton* ibutton = malloc(sizeof(iButton));
  66. ibutton->file_path = furi_string_alloc();
  67. ibutton->scene_manager = scene_manager_alloc(&ibutton_scene_handlers, ibutton);
  68. ibutton->view_dispatcher = view_dispatcher_alloc();
  69. view_dispatcher_enable_queue(ibutton->view_dispatcher);
  70. view_dispatcher_set_event_callback_context(ibutton->view_dispatcher, ibutton);
  71. view_dispatcher_set_custom_event_callback(
  72. ibutton->view_dispatcher, ibutton_custom_event_callback);
  73. view_dispatcher_set_navigation_event_callback(
  74. ibutton->view_dispatcher, ibutton_back_event_callback);
  75. view_dispatcher_set_tick_event_callback(
  76. ibutton->view_dispatcher, ibutton_tick_event_callback, 100);
  77. ibutton->gui = furi_record_open(RECORD_GUI);
  78. ibutton->dialogs = furi_record_open(RECORD_DIALOGS);
  79. ibutton->notifications = furi_record_open(RECORD_NOTIFICATION);
  80. ibutton->protocols = ibutton_protocols_alloc();
  81. ibutton->key = ibutton_key_alloc(ibutton_protocols_get_max_data_size(ibutton->protocols));
  82. ibutton->worker = ibutton_worker_alloc(ibutton->protocols);
  83. ibutton_worker_start_thread(ibutton->worker);
  84. ibutton->submenu = submenu_alloc();
  85. view_dispatcher_add_view(
  86. ibutton->view_dispatcher, iButtonViewSubmenu, submenu_get_view(ibutton->submenu));
  87. ibutton->byte_input = byte_input_alloc();
  88. view_dispatcher_add_view(
  89. ibutton->view_dispatcher, iButtonViewByteInput, byte_input_get_view(ibutton->byte_input));
  90. ibutton->text_input = text_input_alloc();
  91. view_dispatcher_add_view(
  92. ibutton->view_dispatcher, iButtonViewTextInput, text_input_get_view(ibutton->text_input));
  93. ibutton->popup = popup_alloc();
  94. view_dispatcher_add_view(
  95. ibutton->view_dispatcher, iButtonViewPopup, popup_get_view(ibutton->popup));
  96. ibutton->widget = widget_alloc();
  97. view_dispatcher_add_view(
  98. ibutton->view_dispatcher, iButtonViewWidget, widget_get_view(ibutton->widget));
  99. ibutton->loading = loading_alloc();
  100. view_dispatcher_add_view(
  101. ibutton->view_dispatcher, iButtonViewLoading, loading_get_view(ibutton->loading));
  102. return ibutton;
  103. }
  104. void ibutton_free(iButton* ibutton) {
  105. furi_assert(ibutton);
  106. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewLoading);
  107. loading_free(ibutton->loading);
  108. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewWidget);
  109. widget_free(ibutton->widget);
  110. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewPopup);
  111. popup_free(ibutton->popup);
  112. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewTextInput);
  113. text_input_free(ibutton->text_input);
  114. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewByteInput);
  115. byte_input_free(ibutton->byte_input);
  116. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewSubmenu);
  117. submenu_free(ibutton->submenu);
  118. view_dispatcher_free(ibutton->view_dispatcher);
  119. scene_manager_free(ibutton->scene_manager);
  120. furi_record_close(RECORD_NOTIFICATION);
  121. ibutton->notifications = NULL;
  122. furi_record_close(RECORD_DIALOGS);
  123. ibutton->dialogs = NULL;
  124. furi_record_close(RECORD_GUI);
  125. ibutton->gui = NULL;
  126. ibutton_worker_stop_thread(ibutton->worker);
  127. ibutton_worker_free(ibutton->worker);
  128. ibutton_key_free(ibutton->key);
  129. ibutton_protocols_free(ibutton->protocols);
  130. furi_string_free(ibutton->file_path);
  131. free(ibutton);
  132. }
  133. bool ibutton_load_key(iButton* ibutton) {
  134. view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewLoading);
  135. const bool success = ibutton_protocols_load(
  136. ibutton->protocols, ibutton->key, furi_string_get_cstr(ibutton->file_path));
  137. if(!success) {
  138. dialog_message_show_storage_error(ibutton->dialogs, "Cannot load\nkey file");
  139. } else {
  140. FuriString* tmp = furi_string_alloc();
  141. path_extract_filename(ibutton->file_path, tmp, true);
  142. strncpy(ibutton->key_name, furi_string_get_cstr(tmp), IBUTTON_KEY_NAME_SIZE);
  143. furi_string_free(tmp);
  144. }
  145. return success;
  146. }
  147. bool ibutton_select_and_load_key(iButton* ibutton) {
  148. DialogsFileBrowserOptions browser_options;
  149. dialog_file_browser_set_basic_options(&browser_options, IBUTTON_APP_EXTENSION, &I_ibutt_10px);
  150. browser_options.base_path = IBUTTON_APP_FOLDER;
  151. if(furi_string_empty(ibutton->file_path)) {
  152. furi_string_set(ibutton->file_path, browser_options.base_path);
  153. }
  154. return dialog_file_browser_show(
  155. ibutton->dialogs, ibutton->file_path, ibutton->file_path, &browser_options) &&
  156. ibutton_load_key(ibutton);
  157. }
  158. bool ibutton_save_key(iButton* ibutton) {
  159. view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewLoading);
  160. ibutton_make_app_folder(ibutton);
  161. iButtonKey* key = ibutton->key;
  162. const bool success =
  163. ibutton_protocols_save(ibutton->protocols, key, furi_string_get_cstr(ibutton->file_path));
  164. if(!success) {
  165. dialog_message_show_storage_error(ibutton->dialogs, "Cannot save\nkey file");
  166. }
  167. return success;
  168. }
  169. bool ibutton_delete_key(iButton* ibutton) {
  170. bool result = false;
  171. Storage* storage = furi_record_open(RECORD_STORAGE);
  172. result = storage_simply_remove(storage, furi_string_get_cstr(ibutton->file_path));
  173. furi_record_close(RECORD_STORAGE);
  174. ibutton_reset_key(ibutton);
  175. return result;
  176. }
  177. void ibutton_reset_key(iButton* ibutton) {
  178. memset(ibutton->key_name, 0, IBUTTON_KEY_NAME_SIZE + 1);
  179. furi_string_reset(ibutton->file_path);
  180. ibutton_key_reset(ibutton->key);
  181. }
  182. void ibutton_notification_message(iButton* ibutton, uint32_t message) {
  183. furi_assert(message < sizeof(ibutton_notification_sequences) / sizeof(NotificationSequence*));
  184. notification_message(ibutton->notifications, ibutton_notification_sequences[message]);
  185. }
  186. void ibutton_submenu_callback(void* context, uint32_t index) {
  187. iButton* ibutton = context;
  188. view_dispatcher_send_custom_event(ibutton->view_dispatcher, index);
  189. }
  190. void ibutton_widget_callback(GuiButtonType result, InputType type, void* context) {
  191. iButton* ibutton = context;
  192. if(type == InputTypeShort) {
  193. view_dispatcher_send_custom_event(ibutton->view_dispatcher, result);
  194. }
  195. }
  196. int32_t ibutton_app(void* arg) {
  197. iButton* ibutton = ibutton_alloc();
  198. ibutton_make_app_folder(ibutton);
  199. bool key_loaded = false;
  200. if((arg != NULL) && (strlen(arg) != 0)) {
  201. if(sscanf(arg, "RPC %lX", (uint32_t*)&ibutton->rpc) == 1) {
  202. FURI_LOG_D(TAG, "Running in RPC mode");
  203. rpc_system_app_set_callback(ibutton->rpc, ibutton_rpc_command_callback, ibutton);
  204. rpc_system_app_send_started(ibutton->rpc);
  205. } else {
  206. furi_string_set(ibutton->file_path, (const char*)arg);
  207. key_loaded = ibutton_load_key(ibutton);
  208. }
  209. }
  210. if(ibutton->rpc != NULL) {
  211. view_dispatcher_attach_to_gui(
  212. ibutton->view_dispatcher, ibutton->gui, ViewDispatcherTypeDesktop);
  213. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneRpc);
  214. DOLPHIN_DEED(DolphinDeedIbuttonEmulate);
  215. } else {
  216. view_dispatcher_attach_to_gui(
  217. ibutton->view_dispatcher, ibutton->gui, ViewDispatcherTypeFullscreen);
  218. if(key_loaded) { //-V547
  219. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneEmulate);
  220. DOLPHIN_DEED(DolphinDeedIbuttonEmulate);
  221. } else {
  222. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneStart);
  223. }
  224. }
  225. view_dispatcher_run(ibutton->view_dispatcher);
  226. if(ibutton->rpc) {
  227. rpc_system_app_set_callback(ibutton->rpc, NULL, NULL);
  228. rpc_system_app_send_exited(ibutton->rpc);
  229. }
  230. ibutton_free(ibutton);
  231. return 0;
  232. }