ibutton.c 11 KB

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