subbrute_main_view.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #include "subbrute_main_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. #define STATUS_BAR_Y_SHIFT 14
  9. #define TAG "SubBruteMainView"
  10. #define ITEMS_ON_SCREEN 3
  11. #define ITEMS_INTERVAL 1
  12. #define ITEM_WIDTH 14
  13. #define ITEM_Y 27
  14. #define ITEM_HEIGHT 13
  15. #define TEXT_X 6
  16. #define TEXT_Y 37
  17. #define TEXT_INTERVAL 3
  18. #define TEXT_WIDTH 12
  19. #define ITEM_FRAME_RADIUS 2
  20. struct SubBruteMainView {
  21. View* view;
  22. SubBruteMainViewCallback callback;
  23. void* context;
  24. uint8_t index;
  25. bool is_select_byte;
  26. bool two_bytes;
  27. uint64_t key_from_file;
  28. uint8_t extra_repeats;
  29. uint8_t window_position;
  30. };
  31. typedef struct {
  32. uint8_t index;
  33. uint8_t extra_repeats;
  34. uint8_t window_position;
  35. bool is_select_byte;
  36. bool two_bytes;
  37. uint64_t key_from_file;
  38. } SubBruteMainViewModel;
  39. void subbrute_main_view_set_callback(
  40. SubBruteMainView* instance,
  41. SubBruteMainViewCallback callback,
  42. void* context) {
  43. furi_assert(instance);
  44. furi_assert(callback);
  45. instance->callback = callback;
  46. instance->context = context;
  47. }
  48. void subbrute_main_view_center_displayed_key(
  49. Canvas* canvas,
  50. uint64_t key,
  51. uint8_t index,
  52. bool two_bytes) {
  53. uint8_t text_x = TEXT_X;
  54. uint8_t item_x = TEXT_X - ITEMS_INTERVAL;
  55. canvas_set_font(canvas, FontSecondary);
  56. for(int i = 0; i < 8; i++) {
  57. char current_value[3] = {0};
  58. uint8_t byte_value = (uint8_t)(key >> 8 * (7 - i)) & 0xFF;
  59. snprintf(current_value, sizeof(current_value), "%02X", byte_value);
  60. // For two bytes we need to select prev location
  61. if(!two_bytes && i == index) {
  62. canvas_set_color(canvas, ColorBlack);
  63. canvas_draw_rbox(
  64. canvas, item_x - 1, ITEM_Y, ITEM_WIDTH + 1, ITEM_HEIGHT, ITEM_FRAME_RADIUS);
  65. canvas_set_color(canvas, ColorWhite);
  66. canvas_draw_str(canvas, text_x, TEXT_Y, current_value);
  67. } else if(two_bytes && (i == index || i == index - 1)) {
  68. if(i == index) {
  69. canvas_set_color(canvas, ColorBlack);
  70. canvas_draw_rbox(
  71. canvas,
  72. item_x - ITEMS_INTERVAL - ITEM_WIDTH - 1,
  73. ITEM_Y,
  74. ITEM_WIDTH * 2 + ITEMS_INTERVAL * 2 + 1,
  75. ITEM_HEIGHT,
  76. ITEM_FRAME_RADIUS);
  77. canvas_set_color(canvas, ColorWhite);
  78. canvas_draw_str(canvas, text_x, TEXT_Y, current_value);
  79. // Redraw prev element with white
  80. memset(current_value, 0, sizeof(current_value));
  81. byte_value = (uint8_t)(key >> 8 * (7 - i + 1)) & 0xFF;
  82. snprintf(current_value, sizeof(current_value), "%02X", byte_value);
  83. canvas_draw_str(
  84. canvas, text_x - (TEXT_WIDTH + TEXT_INTERVAL), TEXT_Y, current_value);
  85. } else {
  86. canvas_set_color(canvas, ColorWhite);
  87. canvas_draw_str(canvas, text_x, TEXT_Y, current_value);
  88. }
  89. } else {
  90. canvas_set_color(canvas, ColorBlack);
  91. canvas_draw_str(canvas, text_x, TEXT_Y, current_value);
  92. }
  93. text_x = text_x + TEXT_WIDTH + TEXT_INTERVAL;
  94. item_x = item_x + ITEM_WIDTH + ITEMS_INTERVAL;
  95. }
  96. // Return normal color
  97. canvas_set_color(canvas, ColorBlack);
  98. }
  99. void subbrute_main_view_draw(Canvas* canvas, SubBruteMainViewModel* model) {
  100. uint16_t screen_width = canvas_width(canvas);
  101. uint16_t screen_height = canvas_height(canvas);
  102. if(model->is_select_byte) {
  103. #ifdef FURI_DEBUG
  104. //FURI_LOG_D(TAG, "key_from_file: %s", model->key_from_file);
  105. #endif
  106. //char msg_index[18];
  107. //snprintf(msg_index, sizeof(msg_index), "Field index: %d", model->index);
  108. canvas_set_font(canvas, FontSecondary);
  109. canvas_draw_str_aligned(
  110. canvas, 64, 17, AlignCenter, AlignTop, "Please select values to calc:");
  111. subbrute_main_view_center_displayed_key(
  112. canvas, model->key_from_file, model->index, model->two_bytes);
  113. //const char* line = furi_string_get_cstr(menu_items);
  114. //canvas_set_font(canvas, FontSecondary);
  115. //canvas_draw_str_aligned(
  116. // canvas, 64, 37, AlignCenter, AlignTop, furi_string_get_cstr(menu_items));
  117. elements_button_center(canvas, "Select");
  118. if(model->index > 0) {
  119. elements_button_left(canvas, " ");
  120. }
  121. if(model->index < 7) {
  122. elements_button_right(canvas, " ");
  123. }
  124. // Switch to another mode
  125. if(model->two_bytes) {
  126. elements_button_top_left(canvas, "One byte");
  127. } else {
  128. elements_button_top_left(canvas, "Two bytes");
  129. }
  130. } else {
  131. // Title
  132. canvas_set_font(canvas, FontPrimary);
  133. canvas_draw_box(canvas, 0, 0, canvas_width(canvas), STATUS_BAR_Y_SHIFT);
  134. canvas_invert_color(canvas);
  135. canvas_draw_str_aligned(canvas, 64, 3, AlignCenter, AlignTop, SUBBRUTEFORCER_VER);
  136. canvas_invert_color(canvas);
  137. // Menu
  138. canvas_set_color(canvas, ColorBlack);
  139. canvas_set_font(canvas, FontSecondary);
  140. const uint8_t item_height = 16;
  141. #ifdef FURI_DEBUG
  142. //FURI_LOG_D(TAG, "window_position: %d, index: %d", model->window_position, model->index);
  143. #endif
  144. for(uint8_t position = 0; position < SubBruteAttackTotalCount; ++position) {
  145. uint8_t item_position = position - model->window_position;
  146. if(item_position < ITEMS_ON_SCREEN) {
  147. if(model->index == position) {
  148. canvas_draw_str_aligned(
  149. canvas,
  150. 4,
  151. 9 + (item_position * item_height) + STATUS_BAR_Y_SHIFT,
  152. AlignLeft,
  153. AlignCenter,
  154. subbrute_protocol_name(position));
  155. if(model->extra_repeats > 0) {
  156. canvas_set_font(canvas, FontBatteryPercent);
  157. char buffer[10];
  158. snprintf(
  159. buffer,
  160. sizeof(buffer),
  161. "x%d",
  162. model->extra_repeats + subbrute_protocol_repeats_count(model->index));
  163. canvas_draw_str_aligned(
  164. canvas,
  165. screen_width - 15,
  166. 9 + (item_position * item_height) + STATUS_BAR_Y_SHIFT,
  167. AlignLeft,
  168. AlignCenter,
  169. buffer);
  170. canvas_set_font(canvas, FontSecondary);
  171. }
  172. elements_frame(
  173. canvas, 1, 1 + (item_position * item_height) + STATUS_BAR_Y_SHIFT, 124, 15);
  174. } else {
  175. canvas_draw_str_aligned(
  176. canvas,
  177. 4,
  178. 9 + (item_position * item_height) + STATUS_BAR_Y_SHIFT,
  179. AlignLeft,
  180. AlignCenter,
  181. subbrute_protocol_name(position));
  182. }
  183. }
  184. }
  185. elements_scrollbar_pos(
  186. canvas,
  187. screen_width,
  188. STATUS_BAR_Y_SHIFT + 2,
  189. screen_height - STATUS_BAR_Y_SHIFT,
  190. model->index,
  191. SubBruteAttackTotalCount);
  192. }
  193. }
  194. bool subbrute_main_view_input(InputEvent* event, void* context) {
  195. furi_assert(event);
  196. furi_assert(context);
  197. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  198. #ifdef FURI_DEBUG
  199. FURI_LOG_I(TAG, "InputKey: BACK");
  200. #endif
  201. return false;
  202. }
  203. SubBruteMainView* instance = context;
  204. #ifdef FURI_DEBUG
  205. FURI_LOG_D(TAG, "InputKey: %d, extra_repeats: %d", event->key, instance->extra_repeats);
  206. #endif
  207. const uint8_t min_value = 0;
  208. const uint8_t correct_total = SubBruteAttackTotalCount - 1;
  209. uint8_t max_repeats = 9 - subbrute_protocol_repeats_count(instance->index);
  210. bool updated = false;
  211. bool consumed = false;
  212. bool is_short = (event->type == InputTypeShort) || (event->type == InputTypeRepeat);
  213. if(!instance->is_select_byte) {
  214. if(event->key == InputKeyUp && is_short) {
  215. if(instance->index == min_value) {
  216. instance->index = correct_total;
  217. } else {
  218. instance->index = CLAMP(instance->index - 1, correct_total, min_value);
  219. }
  220. instance->extra_repeats = 0;
  221. updated = true;
  222. consumed = true;
  223. } else if(event->key == InputKeyDown && is_short) {
  224. if(instance->index == correct_total) {
  225. instance->index = min_value;
  226. } else {
  227. instance->index = CLAMP(instance->index + 1, correct_total, min_value);
  228. }
  229. instance->extra_repeats = 0;
  230. updated = true;
  231. consumed = true;
  232. } else if(event->key == InputKeyLeft && is_short) {
  233. instance->extra_repeats = CLAMP(instance->extra_repeats - 1, max_repeats, 0);
  234. updated = true;
  235. consumed = true;
  236. } else if(event->key == InputKeyRight && is_short) {
  237. instance->extra_repeats = CLAMP(instance->extra_repeats + 1, max_repeats, 0);
  238. updated = true;
  239. consumed = true;
  240. } else if(event->key == InputKeyOk && is_short) {
  241. if(instance->index == SubBruteAttackLoadFile) {
  242. instance->callback(SubBruteCustomEventTypeLoadFile, instance->context);
  243. } else {
  244. instance->callback(SubBruteCustomEventTypeMenuSelected, instance->context);
  245. }
  246. consumed = true;
  247. updated = true;
  248. }
  249. if(updated) {
  250. instance->window_position = instance->index;
  251. if(instance->window_position > 0) {
  252. instance->window_position -= 1;
  253. }
  254. if(SubBruteAttackTotalCount <= ITEMS_ON_SCREEN) {
  255. instance->window_position = 0;
  256. } else {
  257. if(instance->window_position >= (SubBruteAttackTotalCount - ITEMS_ON_SCREEN)) {
  258. instance->window_position = (SubBruteAttackTotalCount - ITEMS_ON_SCREEN);
  259. }
  260. }
  261. }
  262. } else if(is_short) {
  263. if(event->key == InputKeyLeft) {
  264. if((instance->index > 0 && !instance->two_bytes) ||
  265. (instance->two_bytes && instance->index > 1)) {
  266. instance->index--;
  267. }
  268. updated = true;
  269. consumed = true;
  270. } else if(event->key == InputKeyRight) {
  271. if(instance->index < 7) {
  272. instance->index++;
  273. }
  274. updated = true;
  275. consumed = true;
  276. } else if(event->key == InputKeyUp) {
  277. instance->two_bytes = !instance->two_bytes;
  278. // Because index is changing
  279. if(instance->two_bytes && instance->index < 7) {
  280. instance->index++;
  281. }
  282. // instance->callback(
  283. // instance->two_bytes ? SubBruteCustomEventTypeChangeStepUp :
  284. // SubBruteCustomEventTypeChangeStepDown,
  285. // instance->context);
  286. updated = true;
  287. consumed = true;
  288. } else if(event->key == InputKeyOk) {
  289. instance->callback(SubBruteCustomEventTypeIndexSelected, instance->context);
  290. consumed = true;
  291. updated = true;
  292. }
  293. }
  294. if(updated) {
  295. with_view_model(
  296. instance->view,
  297. SubBruteMainViewModel * model,
  298. {
  299. model->index = instance->index;
  300. model->window_position = instance->window_position;
  301. model->key_from_file = instance->key_from_file;
  302. model->is_select_byte = instance->is_select_byte;
  303. model->two_bytes = instance->two_bytes;
  304. model->extra_repeats = instance->extra_repeats;
  305. },
  306. true);
  307. }
  308. return consumed;
  309. }
  310. void subbrute_main_view_enter(void* context) {
  311. furi_assert(context);
  312. #ifdef FURI_DEBUG
  313. FURI_LOG_D(TAG, "subbrute_main_view_enter");
  314. #endif
  315. }
  316. void subbrute_main_view_exit(void* context) {
  317. furi_assert(context);
  318. #ifdef FURI_DEBUG
  319. FURI_LOG_D(TAG, "subbrute_main_view_exit");
  320. #endif
  321. }
  322. SubBruteMainView* subbrute_main_view_alloc() {
  323. SubBruteMainView* instance = malloc(sizeof(SubBruteMainView));
  324. instance->view = view_alloc();
  325. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubBruteMainViewModel));
  326. view_set_context(instance->view, instance);
  327. view_set_draw_callback(instance->view, (ViewDrawCallback)subbrute_main_view_draw);
  328. view_set_input_callback(instance->view, subbrute_main_view_input);
  329. view_set_enter_callback(instance->view, subbrute_main_view_enter);
  330. view_set_exit_callback(instance->view, subbrute_main_view_exit);
  331. instance->index = 0;
  332. instance->window_position = 0;
  333. instance->key_from_file = 0;
  334. instance->is_select_byte = false;
  335. instance->two_bytes = false;
  336. instance->extra_repeats = 0;
  337. with_view_model(
  338. instance->view,
  339. SubBruteMainViewModel * model,
  340. {
  341. model->index = instance->index;
  342. model->window_position = instance->window_position;
  343. model->key_from_file = instance->key_from_file;
  344. model->is_select_byte = instance->is_select_byte;
  345. model->two_bytes = instance->two_bytes;
  346. model->extra_repeats = instance->extra_repeats;
  347. },
  348. true);
  349. return instance;
  350. }
  351. void subbrute_main_view_free(SubBruteMainView* instance) {
  352. furi_assert(instance);
  353. view_free(instance->view);
  354. free(instance);
  355. }
  356. View* subbrute_main_view_get_view(SubBruteMainView* instance) {
  357. furi_assert(instance);
  358. return instance->view;
  359. }
  360. void subbrute_main_view_set_index(
  361. SubBruteMainView* instance,
  362. uint8_t idx,
  363. bool is_select_byte,
  364. bool two_bytes,
  365. uint64_t key_from_file) {
  366. furi_assert(instance);
  367. furi_assert(idx < SubBruteAttackTotalCount);
  368. #ifdef FURI_DEBUG
  369. FURI_LOG_I(TAG, "Set index: %d, is_select_byte: %d", idx, is_select_byte);
  370. #endif
  371. instance->is_select_byte = is_select_byte;
  372. instance->two_bytes = two_bytes;
  373. instance->key_from_file = key_from_file;
  374. instance->index = idx;
  375. instance->window_position = idx;
  376. if(!is_select_byte) {
  377. if(instance->window_position > 0) {
  378. instance->window_position -= 1;
  379. }
  380. if(SubBruteAttackTotalCount <= ITEMS_ON_SCREEN) {
  381. instance->window_position = 0;
  382. } else {
  383. if(instance->window_position >= (SubBruteAttackTotalCount - ITEMS_ON_SCREEN)) {
  384. instance->window_position = (SubBruteAttackTotalCount - ITEMS_ON_SCREEN);
  385. }
  386. }
  387. }
  388. with_view_model(
  389. instance->view,
  390. SubBruteMainViewModel * model,
  391. {
  392. model->index = instance->index;
  393. model->window_position = instance->window_position;
  394. model->key_from_file = instance->key_from_file;
  395. model->is_select_byte = instance->is_select_byte;
  396. model->two_bytes = instance->two_bytes;
  397. model->extra_repeats = instance->extra_repeats;
  398. },
  399. true);
  400. }
  401. SubBruteAttacks subbrute_main_view_get_index(SubBruteMainView* instance) {
  402. furi_assert(instance);
  403. return instance->index;
  404. }
  405. uint8_t subbrute_main_view_get_extra_repeats(SubBruteMainView* instance) {
  406. furi_assert(instance);
  407. return instance->extra_repeats;
  408. }
  409. bool subbrute_main_view_get_two_bytes(SubBruteMainView* instance) {
  410. furi_assert(instance);
  411. return instance->two_bytes;
  412. }