ibutton.c 10 KB

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