ibutton.c 12 KB

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