subbrute_main_view.c 16 KB

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