subbrute_attack_view.c 12 KB

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