subbrute_attack_view.c 12 KB

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