subbrute_attack_view.c 13 KB

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