ibutton.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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_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, string_t key_path, bool show_dialog) {
  37. FlipperFormat* file = flipper_format_file_alloc(ibutton->storage);
  38. bool result = false;
  39. string_t data;
  40. string_init(data);
  41. do {
  42. if(!flipper_format_file_open_existing(file, string_get_cstr(key_path))) break;
  43. // header
  44. uint32_t version;
  45. if(!flipper_format_read_header(file, data, &version)) break;
  46. if(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(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. string_clear(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. string_init(ibutton->file_path);
  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. string_clear(ibutton->file_path);
  166. free(ibutton);
  167. }
  168. bool ibutton_file_select(iButton* ibutton) {
  169. bool success = dialog_file_browser_show(
  170. ibutton->dialogs,
  171. ibutton->file_path,
  172. ibutton->file_path,
  173. IBUTTON_APP_EXTENSION,
  174. true,
  175. &I_ibutt_10px,
  176. true);
  177. if(success) {
  178. success = ibutton_load_key_data(ibutton, ibutton->file_path, true);
  179. }
  180. return success;
  181. }
  182. bool ibutton_save_key(iButton* ibutton, const char* key_name) {
  183. // Create ibutton directory if necessary
  184. ibutton_make_app_folder(ibutton);
  185. FlipperFormat* file = flipper_format_file_alloc(ibutton->storage);
  186. iButtonKey* key = ibutton->key;
  187. bool result = false;
  188. do {
  189. // Check if we has old key
  190. if(string_end_with_str_p(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
  191. // First remove old key
  192. ibutton_delete_key(ibutton);
  193. // Remove old key name from path
  194. size_t filename_start = string_search_rchar(ibutton->file_path, '/');
  195. string_left(ibutton->file_path, filename_start);
  196. }
  197. string_cat_printf(ibutton->file_path, "/%s%s", key_name, IBUTTON_APP_EXTENSION);
  198. // Open file for write
  199. if(!flipper_format_file_open_always(file, string_get_cstr(ibutton->file_path))) break;
  200. // Write header
  201. if(!flipper_format_write_header_cstr(file, IBUTTON_APP_FILE_TYPE, 1)) break;
  202. // Write key type
  203. if(!flipper_format_write_comment_cstr(file, "Key type can be Cyfral, Dallas or Metakom"))
  204. break;
  205. const char* key_type = ibutton_key_get_string_by_type(ibutton_key_get_type(key));
  206. if(!flipper_format_write_string_cstr(file, "Key type", key_type)) break;
  207. // Write data
  208. if(!flipper_format_write_comment_cstr(
  209. file, "Data size for Cyfral is 2, for Metakom is 4, for Dallas is 8"))
  210. break;
  211. if(!flipper_format_write_hex(
  212. file, "Data", ibutton_key_get_data_p(key), ibutton_key_get_data_size(key)))
  213. break;
  214. result = true;
  215. } while(false);
  216. flipper_format_free(file);
  217. if(!result) {
  218. dialog_message_show_storage_error(ibutton->dialogs, "Cannot save\nkey file");
  219. }
  220. return result;
  221. }
  222. bool ibutton_delete_key(iButton* ibutton) {
  223. bool result = false;
  224. result = storage_simply_remove(ibutton->storage, string_get_cstr(ibutton->file_path));
  225. return result;
  226. }
  227. void ibutton_text_store_set(iButton* ibutton, const char* text, ...) {
  228. va_list args;
  229. va_start(args, text);
  230. vsnprintf(ibutton->text_store, IBUTTON_TEXT_STORE_SIZE, text, args);
  231. va_end(args);
  232. }
  233. void ibutton_text_store_clear(iButton* ibutton) {
  234. memset(ibutton->text_store, 0, IBUTTON_TEXT_STORE_SIZE);
  235. }
  236. void ibutton_notification_message(iButton* ibutton, uint32_t message) {
  237. furi_assert(message < sizeof(ibutton_notification_sequences) / sizeof(NotificationSequence*));
  238. notification_message(ibutton->notifications, ibutton_notification_sequences[message]);
  239. }
  240. int32_t ibutton_app(void* p) {
  241. iButton* ibutton = ibutton_alloc();
  242. ibutton_make_app_folder(ibutton);
  243. bool key_loaded = false;
  244. bool rpc_mode = false;
  245. if(p && strlen(p)) {
  246. uint32_t rpc_ctx = 0;
  247. if(sscanf(p, "RPC %lX", &rpc_ctx) == 1) {
  248. FURI_LOG_D(TAG, "Running in RPC mode");
  249. ibutton->rpc_ctx = (void*)rpc_ctx;
  250. rpc_mode = true;
  251. rpc_system_app_set_callback(ibutton->rpc_ctx, ibutton_rpc_command_callback, ibutton);
  252. rpc_system_app_send_started(ibutton->rpc_ctx);
  253. } else {
  254. string_set_str(ibutton->file_path, (const char*)p);
  255. if(ibutton_load_key_data(ibutton, ibutton->file_path, true)) {
  256. key_loaded = true;
  257. // TODO: Display an error if the key from p could not be loaded
  258. }
  259. }
  260. }
  261. if(rpc_mode) {
  262. view_dispatcher_attach_to_gui(
  263. ibutton->view_dispatcher, ibutton->gui, ViewDispatcherTypeDesktop);
  264. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneRpc);
  265. } else {
  266. view_dispatcher_attach_to_gui(
  267. ibutton->view_dispatcher, ibutton->gui, ViewDispatcherTypeFullscreen);
  268. if(key_loaded) {
  269. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneEmulate);
  270. } else {
  271. scene_manager_next_scene(ibutton->scene_manager, iButtonSceneStart);
  272. }
  273. }
  274. view_dispatcher_run(ibutton->view_dispatcher);
  275. if(ibutton->rpc_ctx) {
  276. rpc_system_app_set_callback(ibutton->rpc_ctx, NULL, NULL);
  277. rpc_system_app_send_exited(ibutton->rpc_ctx);
  278. }
  279. ibutton_free(ibutton);
  280. return 0;
  281. }