ibutton.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #include "ibutton.h"
  2. #include "assets_icons.h"
  3. #include "ibutton_i.h"
  4. #include "ibutton/scenes/ibutton_scene.h"
  5. #include <toolbox/path.h>
  6. #include <flipper_format/flipper_format.h>
  7. #include <rpc/rpc_app.h>
  8. #include <dolphin/dolphin.h>
  9. #define TAG "iButtonApp"
  10. static const NotificationSequence sequence_blink_set_yellow = {
  11. &message_blink_set_color_yellow,
  12. NULL,
  13. };
  14. static const NotificationSequence sequence_blink_set_magenta = {
  15. &message_blink_set_color_magenta,
  16. NULL,
  17. };
  18. static const NotificationSequence* ibutton_notification_sequences[] = {
  19. &sequence_error,
  20. &sequence_success,
  21. &sequence_blink_start_cyan,
  22. &sequence_blink_start_magenta,
  23. &sequence_blink_set_yellow,
  24. &sequence_blink_set_magenta,
  25. &sequence_set_red_255,
  26. &sequence_reset_red,
  27. &sequence_set_green_255,
  28. &sequence_reset_green,
  29. &sequence_blink_stop,
  30. };
  31. static void ibutton_make_app_folder(iButton* ibutton) {
  32. if(!storage_simply_mkdir(ibutton->storage, IBUTTON_APP_FOLDER)) {
  33. dialog_message_show_storage_error(ibutton->dialogs, "Cannot create\napp folder");
  34. }
  35. }
  36. bool ibutton_load_key_data(iButton* ibutton, FuriString* key_path, bool show_dialog) {
  37. FlipperFormat* file = flipper_format_file_alloc(ibutton->storage);
  38. bool result = false;
  39. FuriString* data;
  40. data = furi_string_alloc();
  41. do {
  42. if(!flipper_format_file_open_existing(file, furi_string_get_cstr(key_path))) break;
  43. // header
  44. uint32_t version;
  45. if(!flipper_format_read_header(file, data, &version)) break;
  46. if(furi_string_cmp_str(data, IBUTTON_APP_FILE_TYPE) != 0) break;
  47. if(version != 1) break;
  48. // key type
  49. iButtonKeyType type;
  50. if(!flipper_format_read_string(file, "Key type", data)) break;
  51. if(!ibutton_key_get_type_by_string(furi_string_get_cstr(data), &type)) break;
  52. // key data
  53. uint8_t key_data[IBUTTON_KEY_DATA_SIZE] = {0};
  54. if(!flipper_format_read_hex(file, "Data", key_data, ibutton_key_get_size_by_type(type)))
  55. break;
  56. ibutton_key_set_type(ibutton->key, type);
  57. ibutton_key_set_data(ibutton->key, key_data, IBUTTON_KEY_DATA_SIZE);
  58. result = true;
  59. } while(false);
  60. flipper_format_free(file);
  61. furi_string_free(data);
  62. if((!result) && (show_dialog)) {
  63. dialog_message_show_storage_error(ibutton->dialogs, "Cannot load\nkey file");
  64. }
  65. return result;
  66. }
  67. static void ibutton_rpc_command_callback(RpcAppSystemEvent event, void* context) {
  68. furi_assert(context);
  69. iButton* ibutton = context;
  70. if(event == RpcAppEventSessionClose) {
  71. view_dispatcher_send_custom_event(
  72. ibutton->view_dispatcher, iButtonCustomEventRpcSessionClose);
  73. rpc_system_app_set_callback(ibutton->rpc_ctx, NULL, NULL);
  74. ibutton->rpc_ctx = NULL;
  75. } else if(event == RpcAppEventAppExit) {
  76. view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventRpcExit);
  77. } else if(event == RpcAppEventLoadFile) {
  78. view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventRpcLoad);
  79. } else {
  80. rpc_system_app_confirm(ibutton->rpc_ctx, event, false);
  81. }
  82. }
  83. bool ibutton_custom_event_callback(void* context, uint32_t event) {
  84. furi_assert(context);
  85. iButton* ibutton = context;
  86. return scene_manager_handle_custom_event(ibutton->scene_manager, event);
  87. }
  88. bool ibutton_back_event_callback(void* context) {
  89. furi_assert(context);
  90. iButton* ibutton = context;
  91. return scene_manager_handle_back_event(ibutton->scene_manager);
  92. }
  93. void ibutton_tick_event_callback(void* context) {
  94. furi_assert(context);
  95. iButton* ibutton = context;
  96. scene_manager_handle_tick_event(ibutton->scene_manager);
  97. }
  98. iButton* ibutton_alloc() {
  99. iButton* ibutton = malloc(sizeof(iButton));
  100. ibutton->file_path = furi_string_alloc();
  101. ibutton->scene_manager = scene_manager_alloc(&ibutton_scene_handlers, ibutton);
  102. ibutton->view_dispatcher = view_dispatcher_alloc();
  103. view_dispatcher_enable_queue(ibutton->view_dispatcher);
  104. view_dispatcher_set_event_callback_context(ibutton->view_dispatcher, ibutton);
  105. view_dispatcher_set_custom_event_callback(
  106. ibutton->view_dispatcher, ibutton_custom_event_callback);
  107. view_dispatcher_set_navigation_event_callback(
  108. ibutton->view_dispatcher, ibutton_back_event_callback);
  109. view_dispatcher_set_tick_event_callback(
  110. ibutton->view_dispatcher, ibutton_tick_event_callback, 100);
  111. ibutton->gui = furi_record_open(RECORD_GUI);
  112. ibutton->storage = furi_record_open(RECORD_STORAGE);
  113. ibutton->dialogs = furi_record_open(RECORD_DIALOGS);
  114. ibutton->notifications = furi_record_open(RECORD_NOTIFICATION);
  115. ibutton->key = ibutton_key_alloc();
  116. ibutton->key_worker = ibutton_worker_alloc();
  117. ibutton_worker_start_thread(ibutton->key_worker);
  118. ibutton->submenu = submenu_alloc();
  119. view_dispatcher_add_view(
  120. ibutton->view_dispatcher, iButtonViewSubmenu, submenu_get_view(ibutton->submenu));
  121. ibutton->byte_input = byte_input_alloc();
  122. view_dispatcher_add_view(
  123. ibutton->view_dispatcher, iButtonViewByteInput, byte_input_get_view(ibutton->byte_input));
  124. ibutton->text_input = text_input_alloc();
  125. view_dispatcher_add_view(
  126. ibutton->view_dispatcher, iButtonViewTextInput, text_input_get_view(ibutton->text_input));
  127. ibutton->popup = popup_alloc();
  128. view_dispatcher_add_view(
  129. ibutton->view_dispatcher, iButtonViewPopup, popup_get_view(ibutton->popup));
  130. ibutton->widget = widget_alloc();
  131. view_dispatcher_add_view(
  132. ibutton->view_dispatcher, iButtonViewWidget, widget_get_view(ibutton->widget));
  133. ibutton->dialog_ex = dialog_ex_alloc();
  134. view_dispatcher_add_view(
  135. ibutton->view_dispatcher, iButtonViewDialogEx, dialog_ex_get_view(ibutton->dialog_ex));
  136. return ibutton;
  137. }
  138. void ibutton_free(iButton* ibutton) {
  139. furi_assert(ibutton);
  140. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewDialogEx);
  141. dialog_ex_free(ibutton->dialog_ex);
  142. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewWidget);
  143. widget_free(ibutton->widget);
  144. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewPopup);
  145. popup_free(ibutton->popup);
  146. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewTextInput);
  147. text_input_free(ibutton->text_input);
  148. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewByteInput);
  149. byte_input_free(ibutton->byte_input);
  150. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewSubmenu);
  151. submenu_free(ibutton->submenu);
  152. view_dispatcher_free(ibutton->view_dispatcher);
  153. scene_manager_free(ibutton->scene_manager);
  154. furi_record_close(RECORD_STORAGE);
  155. ibutton->storage = NULL;
  156. furi_record_close(RECORD_NOTIFICATION);
  157. ibutton->notifications = NULL;
  158. furi_record_close(RECORD_DIALOGS);
  159. ibutton->dialogs = NULL;
  160. furi_record_close(RECORD_GUI);
  161. ibutton->gui = NULL;
  162. ibutton_worker_stop_thread(ibutton->key_worker);
  163. ibutton_worker_free(ibutton->key_worker);
  164. ibutton_key_free(ibutton->key);
  165. furi_string_free(ibutton->file_path);
  166. free(ibutton);
  167. }
  168. bool ibutton_file_select(iButton* ibutton) {
  169. DialogsFileBrowserOptions browser_options;
  170. dialog_file_browser_set_basic_options(&browser_options, IBUTTON_APP_EXTENSION, &I_ibutt_10px);
  171. bool success = dialog_file_browser_show(
  172. ibutton->dialogs, ibutton->file_path, ibutton->file_path, &browser_options);
  173. if(success) {
  174. success = ibutton_load_key_data(ibutton, ibutton->file_path, true);
  175. }
  176. return success;
  177. }
  178. bool ibutton_save_key(iButton* ibutton, const char* key_name) {
  179. // Create ibutton directory if necessary
  180. ibutton_make_app_folder(ibutton);
  181. FlipperFormat* file = flipper_format_file_alloc(ibutton->storage);
  182. iButtonKey* key = ibutton->key;
  183. bool result = false;
  184. do {
  185. // Check if we has old key
  186. if(furi_string_end_with(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
  187. // First remove old key
  188. ibutton_delete_key(ibutton);
  189. // Remove old key name from path
  190. size_t filename_start = furi_string_search_rchar(ibutton->file_path, '/');
  191. furi_string_left(ibutton->file_path, filename_start);
  192. }
  193. furi_string_cat_printf(ibutton->file_path, "/%s%s", key_name, IBUTTON_APP_EXTENSION);
  194. // Open file for write
  195. if(!flipper_format_file_open_always(file, furi_string_get_cstr(ibutton->file_path))) break;
  196. // Write header
  197. if(!flipper_format_write_header_cstr(file, IBUTTON_APP_FILE_TYPE, 1)) break;
  198. // Write key type
  199. if(!flipper_format_write_comment_cstr(file, "Key type can be Cyfral, Dallas or Metakom"))
  200. break;
  201. const char* key_type = ibutton_key_get_string_by_type(ibutton_key_get_type(key));
  202. if(!flipper_format_write_string_cstr(file, "Key type", key_type)) break;
  203. // Write data
  204. if(!flipper_format_write_comment_cstr(
  205. file, "Data size for Cyfral is 2, for Metakom is 4, for Dallas is 8"))
  206. break;
  207. if(!flipper_format_write_hex(
  208. file, "Data", ibutton_key_get_data_p(key), ibutton_key_get_data_size(key)))
  209. break;
  210. result = true;
  211. } while(false);
  212. flipper_format_free(file);
  213. if(!result) {
  214. dialog_message_show_storage_error(ibutton->dialogs, "Cannot save\nkey file");
  215. }
  216. return result;
  217. }
  218. bool ibutton_delete_key(iButton* ibutton) {
  219. bool result = false;
  220. result = storage_simply_remove(ibutton->storage, furi_string_get_cstr(ibutton->file_path));
  221. return result;
  222. }
  223. void ibutton_text_store_set(iButton* ibutton, const char* text, ...) {
  224. va_list args;
  225. va_start(args, text);
  226. vsnprintf(ibutton->text_store, IBUTTON_TEXT_STORE_SIZE, text, args);
  227. va_end(args);
  228. }
  229. void ibutton_text_store_clear(iButton* ibutton) {
  230. memset(ibutton->text_store, 0, IBUTTON_TEXT_STORE_SIZE);
  231. }
  232. void ibutton_notification_message(iButton* ibutton, uint32_t message) {
  233. furi_assert(message < sizeof(ibutton_notification_sequences) / sizeof(NotificationSequence*));
  234. notification_message(ibutton->notifications, ibutton_notification_sequences[message]);
  235. }
  236. int32_t ibutton_app(void* p) {
  237. iButton* ibutton = ibutton_alloc();
  238. ibutton_make_app_folder(ibutton);
  239. bool key_loaded = false;
  240. bool rpc_mode = false;
  241. if(p && strlen(p)) {
  242. uint32_t rpc_ctx = 0;
  243. if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
  244. FURI_LOG_D(TAG, "Running in RPC mode");
  245. ibutton->rpc_ctx = (void*)rpc_ctx;
  246. rpc_mode = true;
  247. rpc_system_app_set_callback(ibutton->rpc_ctx, ibutton_rpc_command_callback, ibutton);
  248. rpc_system_app_send_started(ibutton->rpc_ctx);
  249. } else {
  250. furi_string_set(ibutton->file_path, (const char*)p);
  251. if(ibutton_load_key_data(ibutton, ibutton->file_path, true)) {
  252. key_loaded = true;
  253. // TODO: Display an error if the key from p could not be loaded
  254. }
  255. }
  256. }
  257. if(rpc_mode) {
  258. view_dispatcher_attach_to_gui(
  259. ibutton->view_dispatcher, ibutton->gui, ViewDispatcherTypeDesktop);
  260. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneRpc);
  261. DOLPHIN_DEED(DolphinDeedIbuttonEmulate);
  262. } else {
  263. view_dispatcher_attach_to_gui(
  264. ibutton->view_dispatcher, ibutton->gui, ViewDispatcherTypeFullscreen);
  265. if(key_loaded) {
  266. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneEmulate);
  267. DOLPHIN_DEED(DolphinDeedIbuttonEmulate);
  268. } else {
  269. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneStart);
  270. }
  271. }
  272. view_dispatcher_run(ibutton->view_dispatcher);
  273. if(ibutton->rpc_ctx) {
  274. rpc_system_app_set_callback(ibutton->rpc_ctx, NULL, NULL);
  275. rpc_system_app_send_exited(ibutton->rpc_ctx);
  276. }
  277. ibutton_free(ibutton);
  278. return 0;
  279. }