subbrute_attack_view.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "subbrute_attack_view.h"
  2. #include "../subbrute_i.h"
  3. #include <input/input.h>
  4. #include <gui/elements.h>
  5. #include <gui/icon_animation.h>
  6. #include <subghz_bruteforcer_icons.h>
  7. #define TAG "SubBruteAttackView"
  8. struct SubBruteAttackView {
  9. View* view;
  10. SubBruteAttackViewCallback callback;
  11. void* context;
  12. SubBruteAttacks attack_type;
  13. uint64_t max_value;
  14. uint64_t current_step;
  15. bool is_attacking;
  16. };
  17. typedef struct {
  18. SubBruteAttacks attack_type;
  19. uint64_t max_value;
  20. uint64_t current_step;
  21. uint8_t repeat_count;
  22. bool is_attacking;
  23. IconAnimation* icon;
  24. } SubBruteAttackViewModel;
  25. void subbrute_attack_view_set_callback(
  26. SubBruteAttackView* instance,
  27. SubBruteAttackViewCallback callback,
  28. void* context) {
  29. furi_assert(instance);
  30. furi_assert(callback);
  31. instance->callback = callback;
  32. instance->context = context;
  33. }
  34. bool subbrute_attack_view_input(InputEvent* event, void* context) {
  35. furi_assert(event);
  36. furi_assert(context);
  37. SubBruteAttackView* instance = context;
  38. #ifdef FURI_DEBUG
  39. FURI_LOG_D(TAG, "InputKey: %d", event->key);
  40. #endif
  41. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  42. instance->is_attacking = false;
  43. with_view_model(
  44. instance->view,
  45. SubBruteAttackViewModel * model,
  46. { model->is_attacking = false; },
  47. true);
  48. instance->callback(SubBruteCustomEventTypeBackPressed, instance->context);
  49. return true;
  50. }
  51. bool update = false;
  52. if(!instance->is_attacking) {
  53. if(event->type == InputTypeShort && event->key == InputKeyOk) {
  54. #ifdef FURI_DEBUG
  55. FURI_LOG_D(TAG, "InputKey: %d OK", event->key);
  56. #endif
  57. instance->is_attacking = true;
  58. instance->callback(SubBruteCustomEventTypeTransmitStarted, instance->context);
  59. update = true;
  60. } else if(event->key == InputKeyUp && event->type == InputTypeShort) {
  61. instance->callback(SubBruteCustomEventTypeSaveFile, instance->context);
  62. update = true;
  63. } else if(event->key == InputKeyUp && event->type == InputTypeLong) {
  64. instance->callback(SubBruteCustomEventTypeExtraSettings, 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. update);
  112. }
  113. return update;
  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. false);
  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. SubBruteAttackView* instance = (SubBruteAttackView*)context;
  141. with_view_model(
  142. instance->view,
  143. SubBruteAttackViewModel * model,
  144. {
  145. if(model->is_attacking) {
  146. icon_animation_start(model->icon);
  147. }
  148. },
  149. true);
  150. }
  151. void subbrute_attack_view_free(SubBruteAttackView* instance) {
  152. furi_assert(instance);
  153. with_view_model(
  154. instance->view,
  155. SubBruteAttackViewModel * model,
  156. { icon_animation_free(model->icon); },
  157. false);
  158. view_free(instance->view);
  159. free(instance);
  160. }
  161. View* subbrute_attack_view_get_view(SubBruteAttackView* instance) {
  162. furi_assert(instance);
  163. return instance->view;
  164. }
  165. void subbrute_attack_view_set_current_step(SubBruteAttackView* instance, uint64_t current_step) {
  166. furi_assert(instance);
  167. instance->current_step = current_step;
  168. with_view_model(
  169. instance->view,
  170. SubBruteAttackViewModel * model,
  171. { model->current_step = current_step; },
  172. true);
  173. }
  174. // We need to call init every time, because not every time we call "enter"
  175. // normally, so call "enter" only once
  176. void subbrute_attack_view_init_values(
  177. SubBruteAttackView* instance,
  178. uint8_t index,
  179. uint64_t max_value,
  180. uint64_t current_step,
  181. bool is_attacking,
  182. uint8_t extra_repeats) {
  183. #ifdef FURI_DEBUG
  184. FURI_LOG_I(
  185. TAG,
  186. "INIT, attack_type: %d, max_value: %lld, current_step: %lld, extra_repeats: %d",
  187. index,
  188. max_value,
  189. current_step,
  190. extra_repeats);
  191. #endif
  192. instance->attack_type = index;
  193. instance->max_value = max_value;
  194. instance->current_step = current_step;
  195. instance->is_attacking = is_attacking;
  196. with_view_model(
  197. instance->view,
  198. SubBruteAttackViewModel * model,
  199. {
  200. model->max_value = max_value;
  201. model->attack_type = index;
  202. model->current_step = current_step;
  203. model->is_attacking = is_attacking;
  204. model->repeat_count = extra_repeats;
  205. if(is_attacking) {
  206. icon_animation_start(model->icon);
  207. } else {
  208. icon_animation_stop(model->icon);
  209. }
  210. },
  211. true);
  212. }
  213. void subbrute_attack_view_exit(void* context) {
  214. furi_assert(context);
  215. SubBruteAttackView* instance = context;
  216. #ifdef FURI_DEBUG
  217. FURI_LOG_D(TAG, "subbrute_attack_view_exit");
  218. #endif
  219. with_view_model(
  220. instance->view,
  221. SubBruteAttackViewModel * model,
  222. { icon_animation_stop(model->icon); },
  223. false);
  224. }
  225. void subbrute_attack_view_draw(Canvas* canvas, void* context) {
  226. furi_assert(context);
  227. SubBruteAttackViewModel* model = (SubBruteAttackViewModel*)context;
  228. char buffer[64] = {0};
  229. const char* attack_name = NULL;
  230. attack_name = subbrute_protocol_name(model->attack_type);
  231. // Title
  232. if(model->is_attacking) {
  233. canvas_set_color(canvas, ColorBlack);
  234. canvas_set_font(canvas, FontSecondary);
  235. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, attack_name);
  236. }
  237. // Current Step / Max value
  238. const uint8_t y_frequency = 17;
  239. if(model->max_value > 9999) {
  240. canvas_set_font(canvas, FontBigNumbers);
  241. snprintf(buffer, sizeof(buffer), "%05d/", (int)model->current_step);
  242. canvas_draw_str_aligned(canvas, 5, y_frequency, AlignLeft, AlignTop, buffer);
  243. // Second part with another font, because BigNumber is out of screen bounds
  244. canvas_set_font(canvas, FontPrimary);
  245. snprintf(buffer, sizeof(buffer), "%05d", (int)model->max_value);
  246. canvas_draw_str_aligned(canvas, 107, y_frequency + 13, AlignRight, AlignBottom, buffer);
  247. } else if(model->max_value <= 0xFF) {
  248. canvas_set_font(canvas, FontBigNumbers);
  249. snprintf(
  250. buffer, sizeof(buffer), "%03d/%03d", (int)model->current_step, (int)model->max_value);
  251. canvas_draw_str_aligned(canvas, 64, y_frequency, AlignCenter, AlignTop, buffer);
  252. } else {
  253. canvas_set_font(canvas, FontBigNumbers);
  254. snprintf(
  255. buffer, sizeof(buffer), "%04d/%04d", (int)model->current_step, (int)model->max_value);
  256. canvas_draw_str_aligned(canvas, 64, y_frequency, AlignCenter, AlignTop, buffer);
  257. }
  258. canvas_set_font(canvas, FontSecondary);
  259. memset(buffer, 0, sizeof(buffer));
  260. if(!model->is_attacking) {
  261. canvas_set_color(canvas, ColorBlack);
  262. canvas_set_font(canvas, FontSecondary);
  263. canvas_draw_str_aligned(canvas, 64, 44, AlignCenter, AlignBottom, attack_name);
  264. snprintf(
  265. buffer,
  266. sizeof(buffer),
  267. "x%d",
  268. model->repeat_count); // + subbrute_protocol_repeats_count(model->attack_type));
  269. canvas_draw_str_aligned(canvas, 60, 6, AlignCenter, AlignCenter, buffer);
  270. elements_button_left(canvas, "-1");
  271. elements_button_right(canvas, "+1");
  272. elements_button_center(canvas, "Start");
  273. elements_button_up(canvas, "Save");
  274. elements_button_down(canvas, "Resend");
  275. } else {
  276. // canvas_draw_icon_animation
  277. const uint8_t icon_h_offset = 0;
  278. const uint8_t icon_width_with_offset =
  279. icon_animation_get_width(model->icon) + icon_h_offset;
  280. const uint8_t icon_v_offset = icon_animation_get_height(model->icon); // + vertical_offset;
  281. const uint8_t x = canvas_width(canvas);
  282. const uint8_t y = canvas_height(canvas);
  283. canvas_draw_icon_animation(
  284. canvas, x - icon_width_with_offset, y - icon_v_offset, model->icon);
  285. // Progress bar
  286. // Resolution: 128x64 px
  287. float progress_value = (float)model->current_step / (float)model->max_value;
  288. elements_progress_bar(canvas, 8, 37, 110, progress_value > 1 ? 1 : progress_value);
  289. snprintf(
  290. buffer,
  291. sizeof(buffer),
  292. "x%d",
  293. model->repeat_count); // + subbrute_protocol_repeats_count(model->attack_type));
  294. canvas_draw_str(canvas, 4, y - 8, buffer);
  295. canvas_draw_str(canvas, 4, y - 1, "repeats");
  296. elements_button_center(canvas, "Stop");
  297. }
  298. }