subbrute_attack_view.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. void elements_button_top_left(Canvas* canvas, const char* str) {
  267. const Icon* icon = &I_ButtonUp_7x4;
  268. const uint8_t button_height = 12;
  269. const uint8_t vertical_offset = 9; //
  270. const uint8_t horizontal_offset = 3;
  271. const uint8_t string_width = canvas_string_width(canvas, str);
  272. const uint8_t icon_h_offset = 3;
  273. const uint8_t icon_width_with_offset = icon_get_width(icon) + icon_h_offset;
  274. const uint8_t icon_v_offset = icon_get_height(icon); //
  275. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset + 1;
  276. const uint8_t x = 0;
  277. const uint8_t y = 0;
  278. canvas_draw_box(canvas, x, y, button_width, button_height);
  279. // canvas_draw_line(canvas, x + button_width + 0, y, x + button_width + 0, y + button_height - 0); //
  280. // canvas_draw_line(canvas, x + button_width + 1, y, x + button_width + 1, y + button_height - 1);
  281. // canvas_draw_line(canvas, x + button_width + 2, y, x + button_width + 2, y + button_height - 2);
  282. canvas_invert_color(canvas);
  283. canvas_draw_icon(canvas, x + horizontal_offset, y + icon_v_offset, icon);
  284. canvas_draw_str(
  285. canvas, x + horizontal_offset + icon_width_with_offset, y + vertical_offset, str);
  286. canvas_invert_color(canvas);
  287. }
  288. void elements_button_top_right(Canvas* canvas, const char* str) {
  289. const Icon* icon = &I_ButtonDown_7x4;
  290. const uint8_t button_height = 12;
  291. const uint8_t vertical_offset = 9;
  292. const uint8_t horizontal_offset = 3;
  293. const uint8_t string_width = canvas_string_width(canvas, str);
  294. const uint8_t icon_h_offset = 3;
  295. const uint8_t icon_width_with_offset = icon_get_width(icon) + icon_h_offset;
  296. const uint8_t icon_v_offset = icon_get_height(icon); // + vertical_offset;
  297. const uint8_t button_width = string_width + horizontal_offset * 2 + icon_width_with_offset + 1;
  298. const uint8_t x = canvas_width(canvas);
  299. const uint8_t y = 0;
  300. canvas_draw_box(canvas, x - button_width, y, button_width, button_height);
  301. // canvas_draw_line(canvas, x - button_width - 1, y, x + button_width - 1, y + button_height - 0);
  302. // canvas_draw_line(canvas, x - button_width - 2, y, x + button_width - 2, y + button_height - 1);
  303. // canvas_draw_line(canvas, x - button_width - 3, y, x + button_width - 3, y + button_height - 2);
  304. canvas_invert_color(canvas);
  305. canvas_draw_str(canvas, x - button_width + horizontal_offset, y + vertical_offset, str);
  306. canvas_draw_icon(canvas, x - horizontal_offset - icon_get_width(icon), y + icon_v_offset, icon);
  307. canvas_invert_color(canvas);
  308. }
  309. void subbrute_attack_view_draw(Canvas* canvas, void* context) {
  310. furi_assert(context);
  311. SubBruteAttackViewModel* model = (SubBruteAttackViewModel*)context;
  312. char buffer[26];
  313. const char* attack_name = NULL;
  314. attack_name = subbrute_protocol_name(model->index);
  315. // Title
  316. if(model->is_attacking) {
  317. canvas_set_color(canvas, ColorBlack);
  318. canvas_set_font(canvas, FontSecondary);
  319. canvas_draw_str_aligned(canvas, 64, 2, AlignCenter, AlignTop, attack_name);
  320. }
  321. // Value
  322. canvas_set_font(canvas, FontBigNumbers);
  323. snprintf(buffer, sizeof(buffer), "%04d/%04d", (int)model->current_step, (int)model->max_value);
  324. canvas_draw_str_aligned(canvas, 64, 17, AlignCenter, AlignTop, buffer);
  325. canvas_set_font(canvas, FontSecondary);
  326. if(!model->is_attacking) {
  327. canvas_set_color(canvas, ColorBlack);
  328. canvas_set_font(canvas, FontSecondary);
  329. canvas_draw_str_aligned(canvas, 64, 44, AlignCenter, AlignBottom, attack_name);
  330. elements_button_left(canvas, "-1");
  331. elements_button_right(canvas, "+1");
  332. elements_button_center(canvas, "Start");
  333. elements_button_top_left(canvas, "Save");
  334. elements_button_top_right(canvas, "Resend");
  335. } else {
  336. if(model->is_continuous_worker) {
  337. canvas_invert_color(canvas);
  338. }
  339. // canvas_draw_icon_animation
  340. const uint8_t icon_h_offset = 0;
  341. const uint8_t icon_width_with_offset = icon_animation_get_width(model->icon) + icon_h_offset;
  342. const uint8_t icon_v_offset = icon_animation_get_height(model->icon); // + vertical_offset;
  343. const uint8_t x = canvas_width(canvas);
  344. const uint8_t y = canvas_height(canvas);
  345. canvas_draw_icon_animation(
  346. canvas, x - icon_width_with_offset, y - icon_v_offset, model->icon);
  347. // Progress bar
  348. // Resolution: 128x64 px
  349. float progress_value = (float)model->current_step / model->max_value;
  350. elements_progress_bar(canvas, 8, 37, 110, progress_value > 1 ? 1 : progress_value);
  351. elements_button_center(canvas, "Stop");
  352. if(model->is_continuous_worker) {
  353. canvas_invert_color(canvas);
  354. }
  355. }
  356. }