ibutton.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. static 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 bool ibutton_rpc_command_callback(RpcAppSystemEvent event, const char* arg, void* context) {
  84. furi_assert(context);
  85. iButton* ibutton = context;
  86. bool result = false;
  87. if(event == RpcAppEventSessionClose) {
  88. rpc_system_app_set_callback(ibutton->rpc_ctx, NULL, NULL);
  89. ibutton->rpc_ctx = NULL;
  90. view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventRpcExit);
  91. result = true;
  92. } else if(event == RpcAppEventAppExit) {
  93. view_dispatcher_send_custom_event(ibutton->view_dispatcher, iButtonCustomEventRpcExit);
  94. result = true;
  95. } else if(event == RpcAppEventLoadFile) {
  96. if(arg) {
  97. string_set_str(ibutton->file_path, arg);
  98. if(ibutton_load_key_data(ibutton, ibutton->file_path, false)) {
  99. ibutton_worker_emulate_start(ibutton->key_worker, ibutton->key);
  100. result = true;
  101. }
  102. }
  103. }
  104. return result;
  105. }
  106. bool ibutton_custom_event_callback(void* context, uint32_t event) {
  107. furi_assert(context);
  108. iButton* ibutton = context;
  109. return scene_manager_handle_custom_event(ibutton->scene_manager, event);
  110. }
  111. bool ibutton_back_event_callback(void* context) {
  112. furi_assert(context);
  113. iButton* ibutton = context;
  114. return scene_manager_handle_back_event(ibutton->scene_manager);
  115. }
  116. void ibutton_tick_event_callback(void* context) {
  117. furi_assert(context);
  118. iButton* ibutton = context;
  119. scene_manager_handle_tick_event(ibutton->scene_manager);
  120. }
  121. iButton* ibutton_alloc() {
  122. iButton* ibutton = malloc(sizeof(iButton));
  123. string_init(ibutton->file_path);
  124. ibutton->scene_manager = scene_manager_alloc(&ibutton_scene_handlers, ibutton);
  125. ibutton->view_dispatcher = view_dispatcher_alloc();
  126. view_dispatcher_enable_queue(ibutton->view_dispatcher);
  127. view_dispatcher_set_event_callback_context(ibutton->view_dispatcher, ibutton);
  128. view_dispatcher_set_custom_event_callback(
  129. ibutton->view_dispatcher, ibutton_custom_event_callback);
  130. view_dispatcher_set_navigation_event_callback(
  131. ibutton->view_dispatcher, ibutton_back_event_callback);
  132. view_dispatcher_set_tick_event_callback(
  133. ibutton->view_dispatcher, ibutton_tick_event_callback, 100);
  134. ibutton->gui = furi_record_open("gui");
  135. view_dispatcher_attach_to_gui(
  136. ibutton->view_dispatcher, ibutton->gui, ViewDispatcherTypeFullscreen);
  137. ibutton->storage = furi_record_open("storage");
  138. ibutton->dialogs = furi_record_open("dialogs");
  139. ibutton->notifications = furi_record_open("notification");
  140. ibutton->key = ibutton_key_alloc();
  141. ibutton->key_worker = ibutton_worker_alloc();
  142. ibutton_worker_start_thread(ibutton->key_worker);
  143. ibutton->submenu = submenu_alloc();
  144. view_dispatcher_add_view(
  145. ibutton->view_dispatcher, iButtonViewSubmenu, submenu_get_view(ibutton->submenu));
  146. ibutton->byte_input = byte_input_alloc();
  147. view_dispatcher_add_view(
  148. ibutton->view_dispatcher, iButtonViewByteInput, byte_input_get_view(ibutton->byte_input));
  149. ibutton->text_input = text_input_alloc();
  150. view_dispatcher_add_view(
  151. ibutton->view_dispatcher, iButtonViewTextInput, text_input_get_view(ibutton->text_input));
  152. ibutton->popup = popup_alloc();
  153. view_dispatcher_add_view(
  154. ibutton->view_dispatcher, iButtonViewPopup, popup_get_view(ibutton->popup));
  155. ibutton->widget = widget_alloc();
  156. view_dispatcher_add_view(
  157. ibutton->view_dispatcher, iButtonViewWidget, widget_get_view(ibutton->widget));
  158. ibutton->dialog_ex = dialog_ex_alloc();
  159. view_dispatcher_add_view(
  160. ibutton->view_dispatcher, iButtonViewDialogEx, dialog_ex_get_view(ibutton->dialog_ex));
  161. return ibutton;
  162. }
  163. void ibutton_free(iButton* ibutton) {
  164. furi_assert(ibutton);
  165. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewDialogEx);
  166. dialog_ex_free(ibutton->dialog_ex);
  167. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewWidget);
  168. widget_free(ibutton->widget);
  169. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewPopup);
  170. popup_free(ibutton->popup);
  171. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewTextInput);
  172. text_input_free(ibutton->text_input);
  173. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewByteInput);
  174. byte_input_free(ibutton->byte_input);
  175. view_dispatcher_remove_view(ibutton->view_dispatcher, iButtonViewSubmenu);
  176. submenu_free(ibutton->submenu);
  177. view_dispatcher_free(ibutton->view_dispatcher);
  178. scene_manager_free(ibutton->scene_manager);
  179. furi_record_close("storage");
  180. ibutton->storage = NULL;
  181. furi_record_close("notification");
  182. ibutton->notifications = NULL;
  183. furi_record_close("dialogs");
  184. ibutton->dialogs = NULL;
  185. furi_record_close("gui");
  186. ibutton->gui = NULL;
  187. ibutton_worker_stop_thread(ibutton->key_worker);
  188. ibutton_worker_free(ibutton->key_worker);
  189. ibutton_key_free(ibutton->key);
  190. string_clear(ibutton->file_path);
  191. free(ibutton);
  192. }
  193. bool ibutton_file_select(iButton* ibutton) {
  194. bool success = dialog_file_browser_show(
  195. ibutton->dialogs,
  196. ibutton->file_path,
  197. ibutton->file_path,
  198. IBUTTON_APP_EXTENSION,
  199. true,
  200. &I_ibutt_10px,
  201. true);
  202. if(success) {
  203. success = ibutton_load_key_data(ibutton, ibutton->file_path, true);
  204. }
  205. return success;
  206. }
  207. bool ibutton_save_key(iButton* ibutton, const char* key_name) {
  208. // Create ibutton directory if necessary
  209. ibutton_make_app_folder(ibutton);
  210. FlipperFormat* file = flipper_format_file_alloc(ibutton->storage);
  211. iButtonKey* key = ibutton->key;
  212. bool result = false;
  213. do {
  214. // Check if we has old key
  215. if(string_end_with_str_p(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
  216. // First remove old key
  217. ibutton_delete_key(ibutton);
  218. // Remove old key name from path
  219. size_t filename_start = string_search_rchar(ibutton->file_path, '/');
  220. string_left(ibutton->file_path, filename_start);
  221. }
  222. string_cat_printf(ibutton->file_path, "/%s%s", key_name, IBUTTON_APP_EXTENSION);
  223. // Open file for write
  224. if(!flipper_format_file_open_always(file, string_get_cstr(ibutton->file_path))) break;
  225. // Write header
  226. if(!flipper_format_write_header_cstr(file, IBUTTON_APP_FILE_TYPE, 1)) break;
  227. // Write key type
  228. if(!flipper_format_write_comment_cstr(file, "Key type can be Cyfral, Dallas or Metakom"))
  229. break;
  230. const char* key_type = ibutton_key_get_string_by_type(ibutton_key_get_type(key));
  231. if(!flipper_format_write_string_cstr(file, "Key type", key_type)) break;
  232. // Write data
  233. if(!flipper_format_write_comment_cstr(
  234. file, "Data size for Cyfral is 2, for Metakom is 4, for Dallas is 8"))
  235. break;
  236. if(!flipper_format_write_hex(
  237. file, "Data", ibutton_key_get_data_p(key), ibutton_key_get_data_size(key)))
  238. break;
  239. result = true;
  240. } while(false);
  241. flipper_format_free(file);
  242. if(!result) {
  243. dialog_message_show_storage_error(ibutton->dialogs, "Cannot save\nkey file");
  244. }
  245. return result;
  246. }
  247. bool ibutton_delete_key(iButton* ibutton) {
  248. bool result = false;
  249. result = storage_simply_remove(ibutton->storage, string_get_cstr(ibutton->file_path));
  250. return result;
  251. }
  252. void ibutton_text_store_set(iButton* ibutton, const char* text, ...) {
  253. va_list args;
  254. va_start(args, text);
  255. vsnprintf(ibutton->text_store, IBUTTON_TEXT_STORE_SIZE, text, args);
  256. va_end(args);
  257. }
  258. void ibutton_text_store_clear(iButton* ibutton) {
  259. memset(ibutton->text_store, 0, IBUTTON_TEXT_STORE_SIZE);
  260. }
  261. void ibutton_switch_to_previous_scene_one_of(
  262. iButton* ibutton,
  263. const uint32_t* scene_ids,
  264. size_t scene_ids_size) {
  265. furi_assert(scene_ids_size);
  266. SceneManager* scene_manager = ibutton->scene_manager;
  267. for(size_t i = 0; i < scene_ids_size; ++i) {
  268. const uint32_t scene_id = scene_ids[i];
  269. if(scene_manager_has_previous_scene(scene_manager, scene_id)) {
  270. scene_manager_search_and_switch_to_previous_scene(scene_manager, scene_id);
  271. return;
  272. }
  273. }
  274. }
  275. void ibutton_notification_message(iButton* ibutton, uint32_t message) {
  276. furi_assert(message < sizeof(ibutton_notification_sequences) / sizeof(NotificationSequence*));
  277. notification_message(ibutton->notifications, ibutton_notification_sequences[message]);
  278. }
  279. int32_t ibutton_app(void* p) {
  280. iButton* ibutton = ibutton_alloc();
  281. ibutton_make_app_folder(ibutton);
  282. bool key_loaded = false;
  283. bool rpc_mode = false;
  284. if(p) {
  285. uint32_t rpc_ctx = 0;
  286. if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
  287. FURI_LOG_D(TAG, "Running in RPC mode");
  288. ibutton->rpc_ctx = (void*)rpc_ctx;
  289. rpc_mode = true;
  290. rpc_system_app_set_callback(ibutton->rpc_ctx, ibutton_rpc_command_callback, ibutton);
  291. } else {
  292. string_set_str(ibutton->file_path, (const char*)p);
  293. if(ibutton_load_key_data(ibutton, ibutton->file_path, true)) {
  294. key_loaded = true;
  295. // TODO: Display an error if the key from p could not be loaded
  296. }
  297. }
  298. }
  299. if(rpc_mode) {
  300. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneRpc);
  301. } else if(key_loaded) {
  302. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneEmulate);
  303. } else {
  304. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneStart);
  305. }
  306. view_dispatcher_run(ibutton->view_dispatcher);
  307. if(ibutton->rpc_ctx) {
  308. rpc_system_app_set_callback(ibutton->rpc_ctx, NULL, NULL);
  309. }
  310. ibutton_free(ibutton);
  311. return 0;
  312. }