ibutton_scene_emulate.c 3.7 KB

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