ibutton_scene_emulate.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. Popup* popup = ibutton->popup;
  16. iButtonKey* key = ibutton->key;
  17. const uint8_t* key_data = ibutton_key_get_data_p(key);
  18. string_t key_name;
  19. string_init(key_name);
  20. if(string_end_with_str_p(ibutton->file_path, IBUTTON_APP_EXTENSION)) {
  21. path_extract_filename(ibutton->file_path, key_name, true);
  22. }
  23. uint8_t line_count = 2;
  24. DOLPHIN_DEED(DolphinDeedIbuttonEmulate);
  25. // check that stored key has name
  26. if(!string_empty_p(key_name)) {
  27. ibutton_text_store_set(ibutton, "emulating\n%s", string_get_cstr(key_name));
  28. line_count = 2;
  29. } else {
  30. // if not, show key data
  31. switch(ibutton_key_get_type(key)) {
  32. case iButtonKeyDS1990:
  33. ibutton_text_store_set(
  34. ibutton,
  35. "emulating\n%02X %02X %02X %02X\n%02X %02X %02X %02X",
  36. key_data[0],
  37. key_data[1],
  38. key_data[2],
  39. key_data[3],
  40. key_data[4],
  41. key_data[5],
  42. key_data[6],
  43. key_data[7]);
  44. line_count = 3;
  45. break;
  46. case iButtonKeyCyfral:
  47. ibutton_text_store_set(ibutton, "emulating\n%02X %02X", key_data[0], key_data[1]);
  48. line_count = 2;
  49. break;
  50. case iButtonKeyMetakom:
  51. ibutton_text_store_set(
  52. ibutton,
  53. "emulating\n%02X %02X %02X %02X",
  54. key_data[0],
  55. key_data[1],
  56. key_data[2],
  57. key_data[3]);
  58. line_count = 2;
  59. break;
  60. }
  61. }
  62. switch(line_count) {
  63. case 3:
  64. popup_set_header(popup, "iButton", 82, 18, AlignCenter, AlignBottom);
  65. popup_set_text(popup, ibutton->text_store, 82, 22, AlignCenter, AlignTop);
  66. break;
  67. default:
  68. popup_set_header(popup, "iButton", 82, 24, AlignCenter, AlignBottom);
  69. popup_set_text(popup, ibutton->text_store, 82, 28, AlignCenter, AlignTop);
  70. break;
  71. }
  72. popup_set_icon(popup, 2, 10, &I_iButtonKey_49x44);
  73. view_dispatcher_switch_to_view(ibutton->view_dispatcher, iButtonViewPopup);
  74. ibutton_worker_emulate_set_callback(
  75. ibutton->key_worker, ibutton_scene_emulate_callback, ibutton);
  76. ibutton_worker_emulate_start(ibutton->key_worker, key);
  77. string_clear(key_name);
  78. ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateStart);
  79. }
  80. bool ibutton_scene_emulate_on_event(void* context, SceneManagerEvent event) {
  81. iButton* ibutton = context;
  82. bool consumed = false;
  83. if(event.type == SceneManagerEventTypeTick) {
  84. uint32_t cnt = scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneEmulate);
  85. if(cnt > 0) {
  86. cnt--;
  87. if(cnt == 0) {
  88. ibutton_notification_message(ibutton, iButtonNotificationMessageEmulateBlink);
  89. }
  90. scene_manager_set_scene_state(ibutton->scene_manager, iButtonSceneEmulate, cnt);
  91. }
  92. consumed = true;
  93. } else if(event.type == SceneManagerEventTypeCustom) {
  94. consumed = true;
  95. if(event.event == iButtonCustomEventWorkerEmulated) {
  96. if(scene_manager_get_scene_state(ibutton->scene_manager, iButtonSceneEmulate) == 0) {
  97. ibutton_notification_message(ibutton, iButtonNotificationMessageYellowBlink);
  98. }
  99. scene_manager_set_scene_state(
  100. ibutton->scene_manager, iButtonSceneEmulate, EMULATE_TIMEOUT_TICKS);
  101. }
  102. }
  103. return consumed;
  104. }
  105. void ibutton_scene_emulate_on_exit(void* context) {
  106. iButton* ibutton = context;
  107. Popup* popup = ibutton->popup;
  108. ibutton_worker_stop(ibutton->key_worker);
  109. popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
  110. popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
  111. popup_set_icon(popup, 0, 0, NULL);
  112. ibutton_notification_message(ibutton, iButtonNotificationMessageBlinkStop);
  113. }