subbrute_attack_view.c 12 KB

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