subbrute_attack_view.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #include "subbrute_attack_view.h"
  2. #include "../subbrute_i.h"
  3. #include "../subbrute_protocols.h"
  4. #include <input/input.h>
  5. #include <gui/elements.h>
  6. #include <gui/icon.h>
  7. #include <gui/icon_animation.h>
  8. #include <assets_icons.h>
  9. #define TAG "SubBruteAttackView"
  10. struct SubBruteAttackView {
  11. View* view;
  12. SubBruteAttackViewCallback callback;
  13. void* context;
  14. };
  15. typedef struct {
  16. SubBruteAttacks index;
  17. uint64_t max_value;
  18. uint64_t current_step;
  19. bool is_attacking;
  20. bool is_continuous_worker;
  21. IconAnimation* icon;
  22. } SubBruteAttackViewModel;
  23. void subbrute_attack_view_set_callback(
  24. SubBruteAttackView* instance,
  25. SubBruteAttackViewCallback callback,
  26. void* context) {
  27. furi_assert(instance);
  28. furi_assert(callback);
  29. instance->callback = callback;
  30. instance->context = context;
  31. }
  32. bool subbrute_attack_view_input(InputEvent* event, void* context) {
  33. furi_assert(event);
  34. furi_assert(context);
  35. #ifdef FURI_DEBUG
  36. FURI_LOG_D(TAG, "InputKey: %d", event->key);
  37. #endif
  38. SubBruteAttackView* instance = context;
  39. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  40. instance->callback(SubBruteCustomEventTypeBackPressed, instance->context);
  41. with_view_model(
  42. instance->view,
  43. SubBruteAttackViewModel * model,
  44. {
  45. model->is_attacking = false;
  46. model->is_continuous_worker = false;
  47. },
  48. true);
  49. return true;
  50. }
  51. bool is_attacking = false;
  52. with_view_model(
  53. instance->view,
  54. SubBruteAttackViewModel * model,
  55. { is_attacking = model->is_attacking; },
  56. false);
  57. // if(!is_attacking) {
  58. // instance->callback(SubBruteCustomEventTypeTransmitNotStarted, instance->context);
  59. // } else {
  60. // instance->callback(SubBruteCustomEventTypeTransmitStarted, instance->context);
  61. // }
  62. if(!is_attacking) {
  63. if(event->type == InputTypeShort && event->key == InputKeyOk) {
  64. #ifdef FURI_DEBUG
  65. FURI_LOG_D(TAG, "InputKey: %d OK", event->key);
  66. #endif
  67. with_view_model(
  68. instance->view,
  69. SubBruteAttackViewModel * model,
  70. {
  71. model->is_attacking = true;
  72. model->is_continuous_worker = false;
  73. icon_animation_stop(model->icon);
  74. icon_animation_start(model->icon);
  75. },
  76. true);
  77. instance->callback(SubBruteCustomEventTypeTransmitStarted, instance->context);
  78. /*if(event->type == InputTypeRepeat && event->key == InputKeyOk) {
  79. #ifdef FURI_DEBUG
  80. FURI_LOG_D(TAG, "InputKey: %d OK. SubBruteCustomEventTypeTransmitContinuousStarted", event->key);
  81. #endif
  82. with_view_model(
  83. instance->view, (SubBruteAttackViewModel * model) {
  84. model->is_attacking = true;
  85. model->is_continuous_worker = true;
  86. icon_animation_stop(model->icon);
  87. icon_animation_start(model->icon);
  88. return true;
  89. });
  90. instance->callback(SubBruteCustomEventTypeTransmitContinuousStarted, instance->context);
  91. } else if(event->type == InputTypeShort && event->key == InputKeyOk) {
  92. #ifdef FURI_DEBUG
  93. FURI_LOG_D(TAG, "InputKey: %d OK", event->key);
  94. #endif
  95. with_view_model(
  96. instance->view, (SubBruteAttackViewModel * model) {
  97. model->is_attacking = true;
  98. model->is_continuous_worker = false;
  99. icon_animation_stop(model->icon);
  100. icon_animation_start(model->icon);
  101. return true;
  102. });
  103. instance->callback(SubBruteCustomEventTypeTransmitStarted, instance->context);*/
  104. } else if(event->key == InputKeyUp) {
  105. instance->callback(SubBruteCustomEventTypeSaveFile, instance->context);
  106. } else if(event->key == InputKeyDown) {
  107. instance->callback(SubBruteCustomEventTypeTransmitCustom, instance->context);
  108. } else if(event->type == InputTypeShort) {
  109. if(event->key == InputKeyLeft) {
  110. instance->callback(SubBruteCustomEventTypeChangeStepDown, instance->context);
  111. } else if(event->key == InputKeyRight) {
  112. instance->callback(SubBruteCustomEventTypeChangeStepUp, instance->context);
  113. }
  114. // with_view_model(
  115. // instance->view, (SubBruteAttackViewModel * model) {
  116. // if(event->key == InputKeyLeft) {
  117. // model->current_step =
  118. // ((model->current_step - 1) + model->max_value) % model->max_value;
  119. // } else if(event->key == InputKeyRight) {
  120. // model->current_step = (model->current_step + 1) % model->max_value;
  121. // }
  122. // return true;
  123. // });
  124. // instance->callback(SubBruteCustomEventTypeChangeStep, instance->context);
  125. } else if(event->type == InputTypeRepeat) {
  126. if(event->key == InputKeyLeft) {
  127. instance->callback(SubBruteCustomEventTypeChangeStepDownMore, instance->context);
  128. } else if(event->key == InputKeyRight) {
  129. instance->callback(SubBruteCustomEventTypeChangeStepUpMore, instance->context);
  130. }
  131. /*with_view_model(
  132. instance->view, (SubBruteAttackViewModel * model) {
  133. if(event->key == InputKeyLeft) {
  134. model->current_step =
  135. ((model->current_step - 100) + model->max_value) % model->max_value;
  136. } else if(event->key == InputKeyRight) {
  137. model->current_step = (model->current_step + 100) % model->max_value;
  138. }
  139. return true;
  140. });
  141. instance->callback(SubBruteCustomEventTypeChangeStep, instance->context);*/
  142. }
  143. } else {
  144. if((event->type == InputTypeShort || event->type == InputTypeRepeat) &&
  145. (event->key == InputKeyOk || event->key == InputKeyBack)) {
  146. with_view_model(
  147. instance->view,
  148. SubBruteAttackViewModel * model,
  149. {
  150. model->is_attacking = false;
  151. model->is_continuous_worker = false;
  152. icon_animation_stop(model->icon);
  153. icon_animation_start(model->icon);
  154. },
  155. true);
  156. instance->callback(SubBruteCustomEventTypeTransmitNotStarted, instance->context);
  157. }
  158. }
  159. return true;
  160. }
  161. SubBruteAttackView* subbrute_attack_view_alloc() {
  162. SubBruteAttackView* instance = malloc(sizeof(SubBruteAttackView));
  163. instance->view = view_alloc();
  164. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubBruteAttackViewModel));
  165. view_set_context(instance->view, instance);
  166. with_view_model(
  167. instance->view,
  168. SubBruteAttackViewModel * model,
  169. {
  170. model->icon = icon_animation_alloc(&A_Sub1ghz_14);
  171. view_tie_icon_animation(instance->view, model->icon);
  172. },
  173. false);
  174. view_set_draw_callback(instance->view, (ViewDrawCallback)subbrute_attack_view_draw);
  175. view_set_input_callback(instance->view, subbrute_attack_view_input);
  176. view_set_enter_callback(instance->view, subbrute_attack_view_enter);
  177. view_set_exit_callback(instance->view, subbrute_attack_view_exit);
  178. return instance;
  179. }
  180. void subbrute_attack_view_enter(void* context) {
  181. furi_assert(context);
  182. #ifdef FURI_DEBUG
  183. FURI_LOG_D(TAG, "subbrute_attack_view_enter");
  184. #endif
  185. }
  186. void subbrute_attack_view_free(SubBruteAttackView* instance) {
  187. furi_assert(instance);
  188. #ifdef FURI_DEBUG
  189. FURI_LOG_D(TAG, "subbrute_attack_view_free");
  190. #endif
  191. with_view_model(
  192. instance->view,
  193. SubBruteAttackViewModel * model,
  194. { icon_animation_free(model->icon); },
  195. false);
  196. view_free(instance->view);
  197. free(instance);
  198. }
  199. View* subbrute_attack_view_get_view(SubBruteAttackView* instance) {
  200. furi_assert(instance);
  201. return instance->view;
  202. }
  203. void subbrute_attack_view_set_current_step(SubBruteAttackView* instance, uint64_t current_step) {
  204. furi_assert(instance);
  205. #ifdef FURI_DEBUG
  206. //FURI_LOG_D(TAG, "Set step: %d", current_step);
  207. #endif
  208. with_view_model(
  209. instance->view,
  210. SubBruteAttackViewModel * model,
  211. { model->current_step = current_step; },
  212. true);
  213. }
  214. void subbrute_attack_view_set_worker_type(SubBruteAttackView* instance, bool is_continuous_worker) {
  215. furi_assert(instance);
  216. with_view_model(
  217. instance->view,
  218. SubBruteAttackViewModel * model,
  219. { model->is_continuous_worker = is_continuous_worker; },
  220. true);
  221. }
  222. // We need to call init every time, because not every time we calls enter
  223. // normally, call enter only once
  224. void subbrute_attack_view_init_values(
  225. SubBruteAttackView* instance,
  226. uint8_t index,
  227. uint64_t max_value,
  228. uint64_t current_step,
  229. bool is_attacking) {
  230. #ifdef FURI_DEBUG
  231. FURI_LOG_D(
  232. TAG,
  233. "init, index: %d, max_value: %lld, current_step: %lld",
  234. index,
  235. max_value,
  236. current_step);
  237. #endif
  238. with_view_model(
  239. instance->view,
  240. SubBruteAttackViewModel * model,
  241. {
  242. model->max_value = max_value;
  243. model->index = index;
  244. model->current_step = current_step;
  245. model->is_attacking = is_attacking;
  246. if(is_attacking) {
  247. icon_animation_start(model->icon);
  248. } else {
  249. icon_animation_stop(model->icon);
  250. }
  251. },
  252. true);
  253. }
  254. void subbrute_attack_view_exit(void* context) {
  255. furi_assert(context);
  256. SubBruteAttackView* instance = context;
  257. #ifdef FURI_DEBUG
  258. FURI_LOG_D(TAG, "subbrute_attack_view_exit");
  259. #endif
  260. with_view_model(
  261. instance->view,
  262. SubBruteAttackViewModel * model,
  263. { icon_animation_stop(model->icon); },
  264. false);
  265. }
  266. /**
  267. * Thanks to the author of metronome
  268. * @param canvas
  269. * @param str
  270. */
  271. void elements_button_top_left(Canvas* canvas, const char* str) {
  272. const Icon* icon = &I_ButtonUp_7x4;
  273. const uint8_t button_height = 12;
  274. const uint8_t vertical_offset = 3;
  275. const uint8_t horizontal_offset = 3;
  276. const uint8_t string_width = canvas_string_width(canvas, str);
  277. const uint8_t icon_h_offset = 3;
  278. const uint8_t icon_width_with_offset = icon_get_width(icon) + icon_h_offset;
  279. const uint8_t icon_v_offset = icon_get_height(icon) + vertical_offset;
  280. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  281. const uint8_t x = 0;
  282. const uint8_t y = 0 + button_height;
  283. canvas_draw_box(canvas, x, y - button_height, button_width, button_height);
  284. canvas_draw_line(canvas, x + button_width + 0, y - button_height, x + button_width + 0, y - 1);
  285. canvas_draw_line(canvas, x + button_width + 1, y - button_height, x + button_width + 1, y - 2);
  286. canvas_draw_line(canvas, x + button_width + 2, y - button_height, x + button_width + 2, y - 3);
  287. canvas_invert_color(canvas);
  288. canvas_draw_icon(canvas, x + horizontal_offset, y - icon_v_offset, icon);
  289. canvas_draw_str(
  290. canvas, x + horizontal_offset + icon_width_with_offset, y - vertical_offset, str);
  291. canvas_invert_color(canvas);
  292. }
  293. /**
  294. * Thanks to the author of metronome
  295. * @param canvas
  296. * @param str
  297. */
  298. void elements_button_top_right(Canvas* canvas, const char* str) {
  299. const Icon* icon = &I_ButtonDown_7x4;
  300. const uint8_t button_height = 12;
  301. const uint8_t vertical_offset = 3;
  302. const uint8_t horizontal_offset = 3;
  303. const uint8_t string_width = canvas_string_width(canvas, str);
  304. const uint8_t icon_h_offset = 3;
  305. const uint8_t icon_width_with_offset = icon_get_width(icon) + icon_h_offset;
  306. const uint8_t icon_v_offset = icon_get_height(icon) + vertical_offset + 1;
  307. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset;
  308. const uint8_t x = canvas_width(canvas);
  309. const uint8_t y = 0 + button_height;
  310. canvas_draw_box(canvas, x - button_width, y - button_height, button_width, button_height);
  311. canvas_draw_line(canvas, x - button_width - 1, y - button_height, x - button_width - 1, y - 1);
  312. canvas_draw_line(canvas, x - button_width - 2, y - button_height, x - button_width - 2, y - 2);
  313. canvas_draw_line(canvas, x - button_width - 3, y - button_height, x - button_width - 3, y - 3);
  314. canvas_invert_color(canvas);
  315. canvas_draw_str(canvas, x - button_width + horizontal_offset, y - vertical_offset, str);
  316. canvas_draw_icon(
  317. canvas, x - horizontal_offset - icon_get_width(icon), y - icon_v_offset, icon);
  318. canvas_invert_color(canvas);
  319. }
  320. void subbrute_attack_view_draw(Canvas* canvas, void* context) {
  321. furi_assert(context);
  322. SubBruteAttackViewModel* model = (SubBruteAttackViewModel*)context;
  323. char buffer[26];
  324. const char* attack_name = NULL;
  325. attack_name = subbrute_protocol_name(model->index);
  326. // Title
  327. if(model->is_attacking) {
  328. canvas_set_color(canvas, ColorBlack);
  329. canvas_set_font(canvas, FontSecondary);
  330. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, attack_name);
  331. }
  332. // Value
  333. canvas_set_font(canvas, FontBigNumbers);
  334. snprintf(buffer, sizeof(buffer), "%04d/%04d", (int)model->current_step, (int)model->max_value);
  335. canvas_draw_str_aligned(canvas, 64, 17, AlignCenter, AlignTop, buffer);
  336. canvas_set_font(canvas, FontSecondary);
  337. if(!model->is_attacking) {
  338. canvas_set_color(canvas, ColorBlack);
  339. canvas_set_font(canvas, FontSecondary);
  340. canvas_draw_str_aligned(canvas, 64, 44, AlignCenter, AlignBottom, attack_name);
  341. elements_button_left(canvas, "-1");
  342. elements_button_right(canvas, "+1");
  343. elements_button_center(canvas, "Start");
  344. elements_button_top_left(canvas, "Save");
  345. elements_button_top_right(canvas, "Resend");
  346. } else {
  347. if(model->is_continuous_worker) {
  348. canvas_invert_color(canvas);
  349. }
  350. // canvas_draw_icon_animation
  351. const uint8_t icon_h_offset = 0;
  352. const uint8_t icon_width_with_offset =
  353. icon_animation_get_width(model->icon) + icon_h_offset;
  354. const uint8_t icon_v_offset = icon_animation_get_height(model->icon); // + vertical_offset;
  355. const uint8_t x = canvas_width(canvas);
  356. const uint8_t y = canvas_height(canvas);
  357. canvas_draw_icon_animation(
  358. canvas, x - icon_width_with_offset, y - icon_v_offset, model->icon);
  359. // Progress bar
  360. // Resolution: 128x64 px
  361. float progress_value = (float)model->current_step / model->max_value;
  362. elements_progress_bar(canvas, 8, 37, 110, progress_value > 1 ? 1 : progress_value);
  363. elements_button_center(canvas, "Stop");
  364. if(model->is_continuous_worker) {
  365. canvas_invert_color(canvas);
  366. }
  367. }
  368. }