ibutton.c 12 KB

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