ibutton_scene_emulate.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "../ibutton_i.h"
  2. #include <core/log.h>
  3. #include <dolphin/dolphin.h>
  4. #include <toolbox/path.h>
  5. #define EMULATE_TIMEOUT_TICKS 10
  6. static void ibutton_scene_emulate_callback(void* context, bool emulated) {
  7. iButton* ibutton = context;
  8. if(emulated) {
  9. view_dispatcher_send_custom_event(
  10. ibutton->view_dispatcher, iButtonCustomEventWorkerEmulated);
  11. }
  12. }
  13. void ibutton_scene_emulate_on_enter(void* context) {
  14. iButton* ibutton = context;
  15. Widget* widget = ibutton->widget;
  16. iButtonKey* key = ibutton->key;
  17. const uint8_t* key_data = ibutton_key_get_data_p(key);
  18. FuriString* key_name;
  19. key_name = furi_string_alloc();
  20. if(furi_string_end_with(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
  21. path_extract_filename(ibutton->file_path, key_name, true);
  22. }
  23. DOLPHIN_DEED(DolphinDeedIbuttonEmulate);
  24. // check that stored key has name
  25. if(!furi_string_empty(key_name)) {
  26. ibutton_text_store_set(ibutton, "%s", furi_string_get_cstr(key_name));
  27. } else {
  28. // if not, show key data
  29. switch(ibutton_key_get_type(key)) {
  30. case iButtonKeyDS1990:
  31. ibutton_text_store_set(
  32. ibutton,
  33. "%02X %02X %02X %02X\n%02X %02X %02X %02X",
  34. key_data[0],
  35. key_data[1],
  36. key_data[2],
  37. key_data[3],
  38. key_data[4],
  39. key_data[5],
  40. key_data[6],
  41. key_data[7]);
  42. break;
  43. case iButtonKeyCyfral:
  44. ibutton_text_store_set(ibutton, "%02X %02X", key_data[0], key_data[1]);
  45. break;
  46. case iButtonKeyMetakom:
  47. ibutton_text_store_set(
  48. ibutton, "%02X %02X %02X %02X", key_data[0], key_data[1], key_data[2], key_data[3]);
  49. break;
  50. }
  51. }
  52. widget_add_string_multiline_element(
  53. widget, 90, 10, AlignCenter, AlignTop, FontPrimary, "iButton\nemulating");
  54. widget_add_icon_element(widget, 3, 10, &I_iButtonKey_49x44);
  55. widget_add_text_box_element(
  56. widget, 54, 39, 75, 22, AlignCenter, AlignCenter, ibutton->text_store, true);
  57. view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewWidget);
  58. ibutton_worker_emulate_set_callback(
  59. ibutton->key_worker, ibutton_scene_emulate_callback, ibutton);
  60. ibutton_worker_emulate_start(ibutton->key_worker, key);
  61. furi_string_free(key_name);
  62. ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateStart);
  63. }
  64. bool ibutton_scene_emulate_on_event(void* context, SceneManagerEvent event) {
  65. iButton* ibutton = context;
  66. bool consumed = false;
  67. if(event.type == SceneManagerEventTypeTick) {
  68. uint32_t cnt = scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneEmulate);
  69. if(cnt > 0) {
  70. cnt--;
  71. if(cnt == 0) {
  72. ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateBlink);
  73. }
  74. scene_manager_set_scene_state(ibutton->scene_manager, iButtonSceneEmulate, cnt);
  75. }
  76. consumed = true;
  77. } else if(event.type == SceneManagerEventTypeCustom) {
  78. consumed = true;
  79. if(event.event == iButtonCustomEventWorkerEmulated) {
  80. if(scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneEmulate) == 0) {
  81. ibutton_notification_message(ibutton, iButtonNotificationMessageYellowBlink);
  82. }
  83. scene_manager_set_scene_state(
  84. ibutton->scene_manager, iButtonSceneEmulate, EMULATE_TIMEOUT_TICKS);
  85. }
  86. }
  87. return consumed;
  88. }
  89. void ibutton_scene_emulate_on_exit(void* context) {
  90. iButton* ibutton = context;
  91. ibutton_worker_stop(ibutton->key_worker);
  92. widget_reset(ibutton->widget);
  93. ibutton_notification_message(ibutton, iButtonNotificationMessageBlinkStop);
  94. }