subbrute_attack_view.c 15 KB

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