subbrute_attack_view.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. true);
  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. #ifdef FURI_DEBUG
  143. FURI_LOG_D(TAG, "subbrute_attack_view_enter");
  144. #endif
  145. }
  146. void subbrute_attack_view_free(SubBruteAttackView* instance) {
  147. furi_assert(instance);
  148. #ifdef FURI_DEBUG
  149. FURI_LOG_D(TAG, "subbrute_attack_view_free");
  150. #endif
  151. with_view_model(
  152. instance->view,
  153. SubBruteAttackViewModel * model,
  154. { icon_animation_free(model->icon); },
  155. false);
  156. view_free(instance->view);
  157. free(instance);
  158. }
  159. View* subbrute_attack_view_get_view(SubBruteAttackView* instance) {
  160. furi_assert(instance);
  161. return instance->view;
  162. }
  163. void subbrute_attack_view_set_current_step(SubBruteAttackView* instance, uint64_t current_step) {
  164. furi_assert(instance);
  165. #ifdef FURI_DEBUG
  166. //FURI_LOG_D(TAG, "Set step: %d", current_step);
  167. #endif
  168. instance->current_step = current_step;
  169. with_view_model(
  170. instance->view,
  171. SubBruteAttackViewModel * model,
  172. { model->current_step = current_step; },
  173. true);
  174. }
  175. // We need to call init every time, because not every time we calls enter
  176. // normally, call enter only once
  177. void subbrute_attack_view_init_values(
  178. SubBruteAttackView* instance,
  179. uint8_t index,
  180. uint64_t max_value,
  181. uint64_t current_step,
  182. bool is_attacking,
  183. uint8_t extra_repeats) {
  184. #ifdef FURI_DEBUG
  185. FURI_LOG_I(
  186. TAG,
  187. "INIT, attack_type: %d, max_value: %lld, current_step: %lld, extra_repeats: %d",
  188. index,
  189. max_value,
  190. current_step,
  191. extra_repeats);
  192. #endif
  193. instance->attack_type = index;
  194. instance->max_value = max_value;
  195. instance->current_step = current_step;
  196. instance->is_attacking = is_attacking;
  197. // instance->extra_repeats = extra_repeats;
  198. with_view_model(
  199. instance->view,
  200. SubBruteAttackViewModel * model,
  201. {
  202. model->max_value = max_value;
  203. model->attack_type = index;
  204. model->current_step = current_step;
  205. model->is_attacking = is_attacking;
  206. model->repeat_count = extra_repeats;
  207. if(is_attacking) {
  208. icon_animation_start(model->icon);
  209. } else {
  210. icon_animation_stop(model->icon);
  211. }
  212. },
  213. true);
  214. }
  215. void subbrute_attack_view_exit(void* context) {
  216. furi_assert(context);
  217. SubBruteAttackView* instance = context;
  218. #ifdef FURI_DEBUG
  219. FURI_LOG_D(TAG, "subbrute_attack_view_exit");
  220. #endif
  221. with_view_model(
  222. instance->view,
  223. SubBruteAttackViewModel * model,
  224. { icon_animation_stop(model->icon); },
  225. false);
  226. }
  227. void subbrute_attack_view_draw(Canvas* canvas, void* context) {
  228. furi_assert(context);
  229. SubBruteAttackViewModel* model = (SubBruteAttackViewModel*)context;
  230. char buffer[64];
  231. const char* attack_name = NULL;
  232. attack_name = subbrute_protocol_name(model->attack_type);
  233. // Title
  234. if(model->is_attacking) {
  235. canvas_set_color(canvas, ColorBlack);
  236. canvas_set_font(canvas, FontSecondary);
  237. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, attack_name);
  238. }
  239. // Current Step / Max value
  240. const uint8_t y_frequency = 17;
  241. if(model->max_value > 9999) {
  242. canvas_set_font(canvas, FontBigNumbers);
  243. snprintf(buffer, sizeof(buffer), "%05d/", (int)model->current_step);
  244. canvas_draw_str_aligned(canvas, 5, y_frequency, AlignLeft, AlignTop, buffer);
  245. // Second part with another font, because BigNumber is out of screen bounds
  246. canvas_set_font(canvas, FontPrimary);
  247. snprintf(buffer, sizeof(buffer), "%05d", (int)model->max_value);
  248. canvas_draw_str_aligned(canvas, 107, y_frequency + 13, AlignRight, AlignBottom, buffer);
  249. } else if(model->max_value <= 0xFF) {
  250. canvas_set_font(canvas, FontBigNumbers);
  251. snprintf(
  252. buffer, sizeof(buffer), "%03d/%03d", (int)model->current_step, (int)model->max_value);
  253. canvas_draw_str_aligned(canvas, 64, y_frequency, AlignCenter, AlignTop, buffer);
  254. } else {
  255. canvas_set_font(canvas, FontBigNumbers);
  256. snprintf(
  257. buffer, sizeof(buffer), "%04d/%04d", (int)model->current_step, (int)model->max_value);
  258. canvas_draw_str_aligned(canvas, 64, y_frequency, AlignCenter, AlignTop, buffer);
  259. }
  260. canvas_set_font(canvas, FontSecondary);
  261. memset(buffer, 0, sizeof(buffer));
  262. if(!model->is_attacking) {
  263. canvas_set_color(canvas, ColorBlack);
  264. canvas_set_font(canvas, FontSecondary);
  265. canvas_draw_str_aligned(canvas, 64, 44, AlignCenter, AlignBottom, attack_name);
  266. snprintf(
  267. buffer,
  268. sizeof(buffer),
  269. "x%d",
  270. model->repeat_count); // + subbrute_protocol_repeats_count(model->attack_type));
  271. canvas_draw_str_aligned(canvas, 60, 6, AlignCenter, AlignCenter, buffer);
  272. elements_button_left(canvas, "-1");
  273. elements_button_right(canvas, "+1");
  274. elements_button_center(canvas, "Start");
  275. elements_button_top_left(canvas, "Save");
  276. elements_button_top_right(canvas, "Resend");
  277. } else {
  278. // canvas_draw_icon_animation
  279. const uint8_t icon_h_offset = 0;
  280. const uint8_t icon_width_with_offset =
  281. icon_animation_get_width(model->icon) + icon_h_offset;
  282. const uint8_t icon_v_offset = icon_animation_get_height(model->icon); // + vertical_offset;
  283. const uint8_t x = canvas_width(canvas);
  284. const uint8_t y = canvas_height(canvas);
  285. canvas_draw_icon_animation(
  286. canvas, x - icon_width_with_offset, y - icon_v_offset, model->icon);
  287. // Progress bar
  288. // Resolution: 128x64 px
  289. float progress_value = (float)model->current_step / model->max_value;
  290. elements_progress_bar(canvas, 8, 37, 110, progress_value > 1 ? 1 : progress_value);
  291. snprintf(
  292. buffer,
  293. sizeof(buffer),
  294. "x%d",
  295. model->repeat_count); // + subbrute_protocol_repeats_count(model->attack_type));
  296. canvas_draw_str(canvas, 4, y - 8, buffer);
  297. canvas_draw_str(canvas, 4, y - 1, "repeats");
  298. elements_button_center(canvas, "Stop");
  299. }
  300. }