subbrute_attack_view.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #include "subbrute_attack_view.h"
  2. #include "../subbrute_i.h"
  3. #include "../subbrute_protocols.h"
  4. #include "../helpers/gui_top_buttons.h"
  5. #include <input/input.h>
  6. #include <gui/elements.h>
  7. #include <gui/icon.h>
  8. #include <gui/icon_animation.h>
  9. #include <assets_icons.h>
  10. #define TAG "SubBruteAttackView"
  11. struct SubBruteAttackView {
  12. View* view;
  13. SubBruteAttackViewCallback callback;
  14. void* context;
  15. SubBruteAttacks attack_type;
  16. uint64_t max_value;
  17. uint64_t current_step;
  18. bool is_attacking;
  19. uint8_t extra_repeats;
  20. };
  21. typedef struct {
  22. SubBruteAttacks attack_type;
  23. uint64_t max_value;
  24. uint64_t current_step;
  25. uint8_t extra_repeats;
  26. bool is_attacking;
  27. IconAnimation* icon;
  28. } SubBruteAttackViewModel;
  29. void subbrute_attack_view_set_callback(
  30. SubBruteAttackView* instance,
  31. SubBruteAttackViewCallback callback,
  32. void* context) {
  33. furi_assert(instance);
  34. furi_assert(callback);
  35. instance->callback = callback;
  36. instance->context = context;
  37. }
  38. bool subbrute_attack_view_input(InputEvent* event, void* context) {
  39. furi_assert(event);
  40. furi_assert(context);
  41. SubBruteAttackView* instance = context;
  42. #ifdef FURI_DEBUG
  43. FURI_LOG_D(TAG, "InputKey: %d", event->key);
  44. #endif
  45. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  46. instance->is_attacking = false;
  47. with_view_model(
  48. instance->view,
  49. SubBruteAttackViewModel * model,
  50. { model->is_attacking = false; },
  51. true);
  52. instance->callback(SubBruteCustomEventTypeBackPressed, instance->context);
  53. return true;
  54. }
  55. bool update = false;
  56. if(!instance->is_attacking) {
  57. if(event->type == InputTypeShort && event->key == InputKeyOk) {
  58. #ifdef FURI_DEBUG
  59. FURI_LOG_D(TAG, "InputKey: %d OK", event->key);
  60. #endif
  61. instance->is_attacking = true;
  62. instance->callback(SubBruteCustomEventTypeTransmitStarted, instance->context);
  63. update = true;
  64. } else if(event->key == InputKeyUp) {
  65. instance->callback(SubBruteCustomEventTypeSaveFile, instance->context);
  66. update = true;
  67. } else if(event->key == InputKeyDown) {
  68. instance->callback(SubBruteCustomEventTypeTransmitCustom, instance->context);
  69. update = true;
  70. } else if(event->type == InputTypeShort) {
  71. if(event->key == InputKeyLeft) {
  72. instance->callback(SubBruteCustomEventTypeChangeStepDown, instance->context);
  73. } else if(event->key == InputKeyRight) {
  74. instance->callback(SubBruteCustomEventTypeChangeStepUp, instance->context);
  75. }
  76. update = true;
  77. } else if(event->type == InputTypeRepeat) {
  78. if(event->key == InputKeyLeft) {
  79. instance->callback(SubBruteCustomEventTypeChangeStepDownMore, instance->context);
  80. } else if(event->key == InputKeyRight) {
  81. instance->callback(SubBruteCustomEventTypeChangeStepUpMore, instance->context);
  82. }
  83. update = true;
  84. }
  85. } else {
  86. // ATTACK Mode!
  87. if((event->type == InputTypeShort || event->type == InputTypeRepeat) &&
  88. (event->key == InputKeyOk || event->key == InputKeyBack)) {
  89. instance->is_attacking = false;
  90. instance->callback(SubBruteCustomEventTypeTransmitNotStarted, instance->context);
  91. update = true;
  92. }
  93. }
  94. if(update) {
  95. with_view_model(
  96. instance->view,
  97. SubBruteAttackViewModel * model,
  98. {
  99. if(model->is_attacking != instance->is_attacking) {
  100. if(instance->is_attacking) {
  101. icon_animation_stop(model->icon);
  102. icon_animation_start(model->icon);
  103. } else {
  104. icon_animation_stop(model->icon);
  105. }
  106. }
  107. model->attack_type = instance->attack_type;
  108. model->max_value = instance->max_value;
  109. model->current_step = instance->current_step;
  110. model->is_attacking = instance->is_attacking;
  111. },
  112. true);
  113. }
  114. return true;
  115. }
  116. SubBruteAttackView* subbrute_attack_view_alloc() {
  117. SubBruteAttackView* instance = malloc(sizeof(SubBruteAttackView));
  118. instance->view = view_alloc();
  119. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubBruteAttackViewModel));
  120. view_set_context(instance->view, instance);
  121. with_view_model(
  122. instance->view,
  123. SubBruteAttackViewModel * model,
  124. {
  125. model->icon = icon_animation_alloc(&A_Sub1ghz_14);
  126. view_tie_icon_animation(instance->view, model->icon);
  127. },
  128. true);
  129. view_set_draw_callback(instance->view, (ViewDrawCallback)subbrute_attack_view_draw);
  130. view_set_input_callback(instance->view, subbrute_attack_view_input);
  131. view_set_enter_callback(instance->view, subbrute_attack_view_enter);
  132. view_set_exit_callback(instance->view, subbrute_attack_view_exit);
  133. instance->attack_type = SubBruteAttackTotalCount;
  134. instance->max_value = 0x00;
  135. instance->current_step = 0;
  136. instance->is_attacking = false;
  137. return instance;
  138. }
  139. void subbrute_attack_view_enter(void* context) {
  140. furi_assert(context);
  141. #ifdef FURI_DEBUG
  142. FURI_LOG_D(TAG, "subbrute_attack_view_enter");
  143. #endif
  144. }
  145. void subbrute_attack_view_free(SubBruteAttackView* instance) {
  146. furi_assert(instance);
  147. #ifdef FURI_DEBUG
  148. FURI_LOG_D(TAG, "subbrute_attack_view_free");
  149. #endif
  150. with_view_model(
  151. instance->view,
  152. SubBruteAttackViewModel * model,
  153. { icon_animation_free(model->icon); },
  154. false);
  155. view_free(instance->view);
  156. free(instance);
  157. }
  158. View* subbrute_attack_view_get_view(SubBruteAttackView* instance) {
  159. furi_assert(instance);
  160. return instance->view;
  161. }
  162. void subbrute_attack_view_set_current_step(SubBruteAttackView* instance, uint64_t current_step) {
  163. furi_assert(instance);
  164. #ifdef FURI_DEBUG
  165. //FURI_LOG_D(TAG, "Set step: %d", current_step);
  166. #endif
  167. instance->current_step = current_step;
  168. with_view_model(
  169. instance->view,
  170. SubBruteAttackViewModel * model,
  171. { model->current_step = current_step; },
  172. true);
  173. }
  174. // We need to call init every time, because not every time we calls enter
  175. // normally, call enter only once
  176. void subbrute_attack_view_init_values(
  177. SubBruteAttackView* instance,
  178. uint8_t index,
  179. uint64_t max_value,
  180. uint64_t current_step,
  181. bool is_attacking,
  182. uint8_t extra_repeats) {
  183. #ifdef FURI_DEBUG
  184. FURI_LOG_I(
  185. TAG,
  186. "INIT, attack_type: %d, max_value: %lld, current_step: %lld, extra_repeats: %d",
  187. index,
  188. max_value,
  189. current_step,
  190. extra_repeats);
  191. #endif
  192. instance->attack_type = index;
  193. instance->max_value = max_value;
  194. instance->current_step = current_step;
  195. instance->is_attacking = is_attacking;
  196. instance->extra_repeats = extra_repeats;
  197. with_view_model(
  198. instance->view,
  199. SubBruteAttackViewModel * model,
  200. {
  201. model->max_value = max_value;
  202. model->attack_type = index;
  203. model->current_step = current_step;
  204. model->is_attacking = is_attacking;
  205. model->extra_repeats = extra_repeats;
  206. if(is_attacking) {
  207. icon_animation_start(model->icon);
  208. } else {
  209. icon_animation_stop(model->icon);
  210. }
  211. },
  212. true);
  213. }
  214. void subbrute_attack_view_exit(void* context) {
  215. furi_assert(context);
  216. SubBruteAttackView* instance = context;
  217. #ifdef FURI_DEBUG
  218. FURI_LOG_D(TAG, "subbrute_attack_view_exit");
  219. #endif
  220. with_view_model(
  221. instance->view,
  222. SubBruteAttackViewModel * model,
  223. { icon_animation_stop(model->icon); },
  224. false);
  225. }
  226. void subbrute_attack_view_draw(Canvas* canvas, void* context) {
  227. furi_assert(context);
  228. SubBruteAttackViewModel* model = (SubBruteAttackViewModel*)context;
  229. char buffer[64];
  230. const char* attack_name = NULL;
  231. attack_name = subbrute_protocol_name(model->attack_type);
  232. // Title
  233. if(model->is_attacking) {
  234. canvas_set_color(canvas, ColorBlack);
  235. canvas_set_font(canvas, FontSecondary);
  236. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, attack_name);
  237. }
  238. // Current Step / Max value
  239. const uint8_t y_frequency = 17;
  240. if(model->max_value > 9999) {
  241. canvas_set_font(canvas, FontBigNumbers);
  242. snprintf(buffer, sizeof(buffer), "%05d/", (int)model->current_step);
  243. canvas_draw_str_aligned(canvas, 5, y_frequency, AlignLeft, AlignTop, buffer);
  244. // Second part with another font, because BigNumber is out of screen bounds
  245. canvas_set_font(canvas, FontPrimary);
  246. snprintf(buffer, sizeof(buffer), "%05d", (int)model->max_value);
  247. canvas_draw_str_aligned(canvas, 107, y_frequency + 13, AlignRight, AlignBottom, buffer);
  248. } else if(model->max_value <= 0xFF) {
  249. canvas_set_font(canvas, FontBigNumbers);
  250. snprintf(
  251. buffer, sizeof(buffer), "%03d/%03d", (int)model->current_step, (int)model->max_value);
  252. canvas_draw_str_aligned(canvas, 64, y_frequency, AlignCenter, AlignTop, buffer);
  253. } else {
  254. canvas_set_font(canvas, FontBigNumbers);
  255. snprintf(
  256. buffer, sizeof(buffer), "%04d/%04d", (int)model->current_step, (int)model->max_value);
  257. canvas_draw_str_aligned(canvas, 64, y_frequency, AlignCenter, AlignTop, buffer);
  258. }
  259. canvas_set_font(canvas, FontSecondary);
  260. memset(buffer, 0, sizeof(buffer));
  261. if(!model->is_attacking) {
  262. canvas_set_color(canvas, ColorBlack);
  263. canvas_set_font(canvas, FontSecondary);
  264. canvas_draw_str_aligned(canvas, 64, 44, AlignCenter, AlignBottom, attack_name);
  265. snprintf(
  266. buffer,
  267. sizeof(buffer),
  268. "x%d",
  269. model->extra_repeats + subbrute_protocol_repeats_count(model->attack_type));
  270. canvas_draw_str_aligned(canvas, 60, 6, AlignCenter, AlignCenter, buffer);
  271. elements_button_left(canvas, "-1");
  272. elements_button_right(canvas, "+1");
  273. elements_button_center(canvas, "Start");
  274. elements_button_top_left(canvas, "Save");
  275. elements_button_top_right(canvas, "Resend");
  276. } else {
  277. // canvas_draw_icon_animation
  278. const uint8_t icon_h_offset = 0;
  279. const uint8_t icon_width_with_offset =
  280. icon_animation_get_width(model->icon) + icon_h_offset;
  281. const uint8_t icon_v_offset = icon_animation_get_height(model->icon); // + vertical_offset;
  282. const uint8_t x = canvas_width(canvas);
  283. const uint8_t y = canvas_height(canvas);
  284. canvas_draw_icon_animation(
  285. canvas, x - icon_width_with_offset, y - icon_v_offset, model->icon);
  286. // Progress bar
  287. // Resolution: 128x64 px
  288. float progress_value = (float)model->current_step / model->max_value;
  289. elements_progress_bar(canvas, 8, 37, 110, progress_value > 1 ? 1 : progress_value);
  290. snprintf(
  291. buffer,
  292. sizeof(buffer),
  293. "x%d",
  294. model->extra_repeats + subbrute_protocol_repeats_count(model->attack_type));
  295. canvas_draw_str(canvas, 4, y - 8, buffer);
  296. canvas_draw_str(canvas, 4, y - 1, "repeats");
  297. elements_button_center(canvas, "Stop");
  298. }
  299. }