subbrute_main_view.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. 3,
  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. uint8_t temp_x_offset_repeats = 18;
  164. if(model->extra_repeats + subbrute_protocol_repeats_count(model->index) <
  165. 10) {
  166. temp_x_offset_repeats = 15;
  167. }
  168. canvas_draw_str_aligned(
  169. canvas,
  170. screen_width - temp_x_offset_repeats,
  171. 9 + (item_position * item_height) + STATUS_BAR_Y_SHIFT,
  172. AlignLeft,
  173. AlignCenter,
  174. buffer);
  175. canvas_set_font(canvas, FontSecondary);
  176. }
  177. elements_frame(
  178. canvas, 1, 1 + (item_position * item_height) + STATUS_BAR_Y_SHIFT, 124, 15);
  179. } else {
  180. canvas_draw_str_aligned(
  181. canvas,
  182. 4,
  183. 9 + (item_position * item_height) + STATUS_BAR_Y_SHIFT,
  184. AlignLeft,
  185. AlignCenter,
  186. subbrute_protocol_name(position));
  187. }
  188. }
  189. }
  190. elements_scrollbar_pos(
  191. canvas,
  192. screen_width,
  193. STATUS_BAR_Y_SHIFT + 2,
  194. screen_height - STATUS_BAR_Y_SHIFT,
  195. model->index,
  196. SubBruteAttackTotalCount);
  197. }
  198. }
  199. bool subbrute_main_view_input(InputEvent* event, void* context) {
  200. furi_assert(event);
  201. furi_assert(context);
  202. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  203. #ifdef FURI_DEBUG
  204. FURI_LOG_I(TAG, "InputKey: BACK");
  205. #endif
  206. return false;
  207. }
  208. SubBruteMainView* instance = context;
  209. #ifdef FURI_DEBUG
  210. FURI_LOG_D(TAG, "InputKey: %d, extra_repeats: %d", event->key, instance->extra_repeats);
  211. #endif
  212. const uint8_t min_value = 0;
  213. const uint8_t correct_total = SubBruteAttackTotalCount - 1;
  214. uint8_t max_repeats = 14 - subbrute_protocol_repeats_count(instance->index);
  215. bool updated = false;
  216. bool consumed = false;
  217. bool is_short = (event->type == InputTypeShort) || (event->type == InputTypeRepeat);
  218. if(!instance->is_select_byte) {
  219. if(event->key == InputKeyUp && is_short) {
  220. if(instance->index == min_value) {
  221. instance->index = correct_total;
  222. } else {
  223. instance->index = CLAMP(instance->index - 1, correct_total, min_value);
  224. }
  225. instance->extra_repeats = 0;
  226. updated = true;
  227. consumed = true;
  228. } else if(event->key == InputKeyDown && is_short) {
  229. if(instance->index == correct_total) {
  230. instance->index = min_value;
  231. } else {
  232. instance->index = CLAMP(instance->index + 1, correct_total, min_value);
  233. }
  234. instance->extra_repeats = 0;
  235. updated = true;
  236. consumed = true;
  237. } else if(event->key == InputKeyLeft && is_short) {
  238. instance->extra_repeats = CLAMP(instance->extra_repeats - 1, max_repeats, 0);
  239. updated = true;
  240. consumed = true;
  241. } else if(event->key == InputKeyRight && is_short) {
  242. instance->extra_repeats = CLAMP(instance->extra_repeats + 1, max_repeats, 0);
  243. updated = true;
  244. consumed = true;
  245. } else if(event->key == InputKeyOk && is_short) {
  246. if(instance->index == SubBruteAttackLoadFile) {
  247. instance->callback(SubBruteCustomEventTypeLoadFile, instance->context);
  248. } else {
  249. instance->callback(SubBruteCustomEventTypeMenuSelected, instance->context);
  250. }
  251. consumed = true;
  252. updated = true;
  253. }
  254. if(updated) {
  255. instance->window_position = instance->index;
  256. if(instance->window_position > 0) {
  257. instance->window_position -= 1;
  258. }
  259. if(SubBruteAttackTotalCount <= ITEMS_ON_SCREEN) {
  260. instance->window_position = 0;
  261. } else {
  262. if(instance->window_position >= (SubBruteAttackTotalCount - ITEMS_ON_SCREEN)) {
  263. instance->window_position = (SubBruteAttackTotalCount - ITEMS_ON_SCREEN);
  264. }
  265. }
  266. }
  267. } else if(is_short) {
  268. if(event->key == InputKeyLeft) {
  269. if((instance->index > 0 && !instance->two_bytes) ||
  270. (instance->two_bytes && instance->index > 1)) {
  271. instance->index--;
  272. }
  273. updated = true;
  274. consumed = true;
  275. } else if(event->key == InputKeyRight) {
  276. if(instance->index < 7) {
  277. instance->index++;
  278. }
  279. updated = true;
  280. consumed = true;
  281. } else if(event->key == InputKeyUp) {
  282. instance->two_bytes = !instance->two_bytes;
  283. // Because index is changing
  284. if(instance->two_bytes && instance->index < 7) {
  285. instance->index++;
  286. }
  287. // instance->callback(
  288. // instance->two_bytes ? SubBruteCustomEventTypeChangeStepUp :
  289. // SubBruteCustomEventTypeChangeStepDown,
  290. // instance->context);
  291. updated = true;
  292. consumed = true;
  293. } else if(event->key == InputKeyOk) {
  294. instance->callback(SubBruteCustomEventTypeIndexSelected, instance->context);
  295. consumed = true;
  296. updated = true;
  297. }
  298. }
  299. if(updated) {
  300. with_view_model(
  301. instance->view,
  302. SubBruteMainViewModel * model,
  303. {
  304. model->index = instance->index;
  305. model->window_position = instance->window_position;
  306. model->key_from_file = instance->key_from_file;
  307. model->is_select_byte = instance->is_select_byte;
  308. model->two_bytes = instance->two_bytes;
  309. model->extra_repeats = instance->extra_repeats;
  310. },
  311. true);
  312. }
  313. return consumed;
  314. }
  315. void subbrute_main_view_enter(void* context) {
  316. furi_assert(context);
  317. #ifdef FURI_DEBUG
  318. FURI_LOG_D(TAG, "subbrute_main_view_enter");
  319. #endif
  320. }
  321. void subbrute_main_view_exit(void* context) {
  322. furi_assert(context);
  323. #ifdef FURI_DEBUG
  324. FURI_LOG_D(TAG, "subbrute_main_view_exit");
  325. #endif
  326. }
  327. SubBruteMainView* subbrute_main_view_alloc() {
  328. SubBruteMainView* instance = malloc(sizeof(SubBruteMainView));
  329. instance->view = view_alloc();
  330. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(SubBruteMainViewModel));
  331. view_set_context(instance->view, instance);
  332. view_set_draw_callback(instance->view, (ViewDrawCallback)subbrute_main_view_draw);
  333. view_set_input_callback(instance->view, subbrute_main_view_input);
  334. view_set_enter_callback(instance->view, subbrute_main_view_enter);
  335. view_set_exit_callback(instance->view, subbrute_main_view_exit);
  336. instance->index = 0;
  337. instance->window_position = 0;
  338. instance->key_from_file = 0;
  339. instance->is_select_byte = false;
  340. instance->two_bytes = false;
  341. instance->extra_repeats = 0;
  342. with_view_model(
  343. instance->view,
  344. SubBruteMainViewModel * model,
  345. {
  346. model->index = instance->index;
  347. model->window_position = instance->window_position;
  348. model->key_from_file = instance->key_from_file;
  349. model->is_select_byte = instance->is_select_byte;
  350. model->two_bytes = instance->two_bytes;
  351. model->extra_repeats = instance->extra_repeats;
  352. },
  353. true);
  354. return instance;
  355. }
  356. void subbrute_main_view_free(SubBruteMainView* instance) {
  357. furi_assert(instance);
  358. view_free(instance->view);
  359. free(instance);
  360. }
  361. View* subbrute_main_view_get_view(SubBruteMainView* instance) {
  362. furi_assert(instance);
  363. return instance->view;
  364. }
  365. void subbrute_main_view_set_index(
  366. SubBruteMainView* instance,
  367. uint8_t idx,
  368. bool is_select_byte,
  369. bool two_bytes,
  370. uint64_t key_from_file) {
  371. furi_assert(instance);
  372. furi_assert(idx < SubBruteAttackTotalCount);
  373. #ifdef FURI_DEBUG
  374. FURI_LOG_I(TAG, "Set index: %d, is_select_byte: %d", idx, is_select_byte);
  375. #endif
  376. instance->is_select_byte = is_select_byte;
  377. instance->two_bytes = two_bytes;
  378. instance->key_from_file = key_from_file;
  379. instance->index = idx;
  380. instance->window_position = idx;
  381. if(!is_select_byte) {
  382. if(instance->window_position > 0) {
  383. instance->window_position -= 1;
  384. }
  385. if(SubBruteAttackTotalCount <= ITEMS_ON_SCREEN) {
  386. instance->window_position = 0;
  387. } else {
  388. if(instance->window_position >= (SubBruteAttackTotalCount - ITEMS_ON_SCREEN)) {
  389. instance->window_position = (SubBruteAttackTotalCount - ITEMS_ON_SCREEN);
  390. }
  391. }
  392. }
  393. with_view_model(
  394. instance->view,
  395. SubBruteMainViewModel * model,
  396. {
  397. model->index = instance->index;
  398. model->window_position = instance->window_position;
  399. model->key_from_file = instance->key_from_file;
  400. model->is_select_byte = instance->is_select_byte;
  401. model->two_bytes = instance->two_bytes;
  402. model->extra_repeats = instance->extra_repeats;
  403. },
  404. true);
  405. }
  406. SubBruteAttacks subbrute_main_view_get_index(SubBruteMainView* instance) {
  407. furi_assert(instance);
  408. return instance->index;
  409. }
  410. uint8_t subbrute_main_view_get_extra_repeats(SubBruteMainView* instance) {
  411. furi_assert(instance);
  412. return instance->extra_repeats;
  413. }
  414. bool subbrute_main_view_get_two_bytes(SubBruteMainView* instance) {
  415. furi_assert(instance);
  416. return instance->two_bytes;
  417. }