subbrute_attack_view.c 12 KB

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