field_editor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include "field_editor.h"
  2. #include "../fuzzer_i.h"
  3. #include <input/input.h>
  4. #include <gui/elements.h>
  5. #include <toolbox/hex.h>
  6. #define FIELD_EDITOR_V2
  7. #define GUI_DISPLAY_WIDTH 128
  8. #define GUI_DISPLAY_HEIGHT 64
  9. #define GUI_DISPLAY_HORIZONTAL_CENTER 64
  10. #define GUI_DISPLAY_VERTICAL_CENTER 32
  11. #define UID_STR_LENGTH 25
  12. #ifdef FIELD_EDITOR_V2
  13. #define EDITOR_STRING_Y 38
  14. #else
  15. #define EDITOR_STRING_Y 50
  16. #endif
  17. struct FuzzerViewFieldEditor {
  18. View* view;
  19. FuzzerViewFieldEditorCallback callback;
  20. void* context;
  21. };
  22. typedef struct {
  23. uint8_t* uid;
  24. uint8_t uid_size;
  25. FuriString* uid_str;
  26. uint8_t index;
  27. bool lo;
  28. bool allow_edit;
  29. } FuzzerViewFieldEditorModel;
  30. void fuzzer_view_field_editor_set_callback(
  31. FuzzerViewFieldEditor* view_edit,
  32. FuzzerViewFieldEditorCallback callback,
  33. void* context) {
  34. furi_assert(view_edit);
  35. view_edit->callback = callback;
  36. view_edit->context = context;
  37. }
  38. void fuzzer_view_field_editor_reset_data(
  39. FuzzerViewFieldEditor* view_edit,
  40. const FuzzerPayload* new_uid,
  41. bool allow_edit) {
  42. furi_assert(view_edit);
  43. furi_assert(new_uid->data);
  44. with_view_model(
  45. view_edit->view,
  46. FuzzerViewFieldEditorModel * model,
  47. {
  48. memcpy(model->uid, new_uid->data, new_uid->data_size);
  49. model->index = 0;
  50. model->lo = false;
  51. model->uid_size = new_uid->data_size;
  52. model->allow_edit = allow_edit;
  53. },
  54. true);
  55. }
  56. void fuzzer_view_field_editor_get_uid(FuzzerViewFieldEditor* view_edit, FuzzerPayload* output_uid) {
  57. furi_assert(view_edit);
  58. furi_assert(output_uid);
  59. with_view_model(
  60. view_edit->view,
  61. FuzzerViewFieldEditorModel * model,
  62. {
  63. output_uid->data_size = model->uid_size;
  64. memcpy(output_uid->data, model->uid, model->uid_size);
  65. },
  66. true);
  67. }
  68. uint8_t fuzzer_view_field_editor_get_index(FuzzerViewFieldEditor* view_edit) {
  69. furi_assert(view_edit);
  70. uint8_t index;
  71. with_view_model(
  72. view_edit->view, FuzzerViewFieldEditorModel * model, { index = model->index; }, true);
  73. return index;
  74. }
  75. void fuzzer_view_field_editor_draw(Canvas* canvas, FuzzerViewFieldEditorModel* model) {
  76. canvas_clear(canvas);
  77. canvas_set_color(canvas, ColorBlack);
  78. #ifdef FIELD_EDITOR_V2
  79. canvas_set_font(canvas, FontSecondary);
  80. if(model->allow_edit) {
  81. canvas_draw_icon(canvas, 2, 4, &I_ButtonLeft_4x7);
  82. canvas_draw_icon(canvas, 8, 4, &I_ButtonRight_4x7);
  83. canvas_draw_icon_ex(canvas, 62, 3, &I_Pin_arrow_up_7x9, IconRotation180);
  84. canvas_draw_icon(canvas, 69, 3, &I_Pin_arrow_up_7x9);
  85. canvas_draw_str(canvas, 14, 10, "select byte");
  86. canvas_draw_str(canvas, 79, 10, "adjust byte");
  87. } else {
  88. canvas_draw_icon(canvas, 35, 4, &I_ButtonLeft_4x7);
  89. canvas_draw_icon(canvas, 41, 4, &I_ButtonRight_4x7);
  90. canvas_draw_str(canvas, 49, 10, "select byte");
  91. }
  92. char msg_index[18];
  93. canvas_set_font(canvas, FontPrimary);
  94. snprintf(msg_index, sizeof(msg_index), "Field index : %d", model->index);
  95. canvas_draw_str_aligned(
  96. canvas, GUI_DISPLAY_HORIZONTAL_CENTER, 24, AlignCenter, AlignBottom, msg_index);
  97. canvas_set_font(canvas, FontSecondary);
  98. canvas_draw_icon(canvas, 4, 52, &I_Pin_back_arrow_10x8);
  99. canvas_draw_icon(canvas, 85, 52, &I_Ok_btn_9x9);
  100. canvas_draw_str(canvas, 16, 60, "Back");
  101. canvas_draw_str(canvas, 96, 60, "Attack");
  102. #else
  103. canvas_set_font(canvas, FontSecondary);
  104. canvas_draw_str_aligned(
  105. canvas,
  106. GUI_DISPLAY_HORIZONTAL_CENTER,
  107. 5,
  108. AlignCenter,
  109. AlignTop,
  110. "Left and right: select byte");
  111. canvas_draw_str_aligned(
  112. canvas,
  113. GUI_DISPLAY_HORIZONTAL_CENTER,
  114. 15,
  115. AlignCenter,
  116. AlignTop,
  117. "Up and down: adjust byte");
  118. char msg_index[18];
  119. canvas_set_font(canvas, FontPrimary);
  120. snprintf(msg_index, sizeof(msg_index), "Field index : %d", model->index);
  121. canvas_draw_str_aligned(
  122. canvas, GUI_DISPLAY_HORIZONTAL_CENTER, 28, AlignCenter, AlignTop, msg_index);
  123. #endif
  124. // ####### Editor #######
  125. FuriString* temp_s = model->uid_str;
  126. canvas_set_font(canvas, FontSecondary);
  127. furi_string_reset(temp_s);
  128. for(int i = -3; i != 0; i++) {
  129. if(0 <= (model->index + i)) {
  130. furi_string_cat_printf(temp_s, "%02X ", model->uid[model->index + i]);
  131. }
  132. }
  133. canvas_draw_str_aligned(
  134. canvas, 52, EDITOR_STRING_Y, AlignRight, AlignBottom, furi_string_get_cstr(temp_s));
  135. furi_string_reset(temp_s);
  136. for(int i = 1; i != 4; i++) {
  137. if((model->index + i) < model->uid_size) {
  138. furi_string_cat_printf(temp_s, " %02X", model->uid[model->index + i]);
  139. }
  140. }
  141. canvas_draw_str_aligned(
  142. canvas, 77, EDITOR_STRING_Y, AlignLeft, AlignBottom, furi_string_get_cstr(temp_s));
  143. canvas_set_font(canvas, FontPrimary);
  144. furi_string_reset(temp_s);
  145. furi_string_cat_printf(temp_s, "<%02X>", model->uid[model->index]);
  146. canvas_draw_str_aligned(
  147. canvas,
  148. GUI_DISPLAY_HORIZONTAL_CENTER,
  149. EDITOR_STRING_Y,
  150. AlignCenter,
  151. AlignBottom,
  152. furi_string_get_cstr(temp_s));
  153. uint16_t w = canvas_string_width(canvas, furi_string_get_cstr(temp_s));
  154. w -= 11; // '<' & '>'
  155. w /= 2;
  156. if(model->allow_edit) {
  157. if(model->lo) {
  158. canvas_draw_line(
  159. canvas,
  160. GUI_DISPLAY_HORIZONTAL_CENTER + 1,
  161. EDITOR_STRING_Y + 2,
  162. GUI_DISPLAY_HORIZONTAL_CENTER + w,
  163. EDITOR_STRING_Y + 2);
  164. } else {
  165. canvas_draw_line(
  166. canvas,
  167. GUI_DISPLAY_HORIZONTAL_CENTER - w,
  168. EDITOR_STRING_Y + 2,
  169. GUI_DISPLAY_HORIZONTAL_CENTER - 1,
  170. EDITOR_STRING_Y + 2);
  171. }
  172. } else {
  173. // canvas_draw_line(
  174. // canvas,
  175. // GUI_DISPLAY_HORIZONTAL_CENTER - w,
  176. // EDITOR_STRING_Y + 2,
  177. // GUI_DISPLAY_HORIZONTAL_CENTER + w,
  178. // EDITOR_STRING_Y + 2);
  179. }
  180. // ####### Editor #######
  181. }
  182. bool fuzzer_view_field_editor_input(InputEvent* event, void* context) {
  183. furi_assert(context);
  184. FuzzerViewFieldEditor* view_edit = context;
  185. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  186. view_edit->callback(FuzzerCustomEventViewFieldEditorBack, view_edit->context);
  187. return true;
  188. } else if(event->key == InputKeyOk && event->type == InputTypeShort) {
  189. view_edit->callback(FuzzerCustomEventViewFieldEditorOk, view_edit->context);
  190. return true;
  191. } else if(event->key == InputKeyLeft) {
  192. with_view_model(
  193. view_edit->view,
  194. FuzzerViewFieldEditorModel * model,
  195. {
  196. if(event->type == InputTypeShort) {
  197. if(!model->allow_edit) {
  198. model->lo = false;
  199. }
  200. if(model->index > 0 || model->lo) {
  201. if(!model->lo) {
  202. model->index--;
  203. }
  204. model->lo = !model->lo;
  205. }
  206. } else if(event->type == InputTypeLong) {
  207. model->index = 0;
  208. model->lo = false;
  209. }
  210. },
  211. true);
  212. return true;
  213. } else if(event->key == InputKeyRight) {
  214. with_view_model(
  215. view_edit->view,
  216. FuzzerViewFieldEditorModel * model,
  217. {
  218. if(event->type == InputTypeShort) {
  219. if(!model->allow_edit) {
  220. model->lo = true;
  221. }
  222. if(model->index < (model->uid_size - 1) || !model->lo) {
  223. if(model->lo) {
  224. model->index++;
  225. }
  226. model->lo = !model->lo;
  227. }
  228. } else if(event->type == InputTypeLong) {
  229. model->index = model->uid_size - 1;
  230. model->lo = true;
  231. }
  232. },
  233. true);
  234. return true;
  235. } else if(event->key == InputKeyUp) {
  236. with_view_model(
  237. view_edit->view,
  238. FuzzerViewFieldEditorModel * model,
  239. {
  240. if(event->type == InputTypeShort && model->allow_edit) {
  241. if(model->lo) {
  242. model->uid[model->index] = (model->uid[model->index] & 0xF0) |
  243. ((model->uid[model->index] + 1) & 0x0F);
  244. } else {
  245. model->uid[model->index] = ((model->uid[model->index] + 0x10) & 0xF0) |
  246. (model->uid[model->index] & 0x0F);
  247. }
  248. }
  249. },
  250. true);
  251. return true;
  252. } else if(event->key == InputKeyDown) {
  253. with_view_model(
  254. view_edit->view,
  255. FuzzerViewFieldEditorModel * model,
  256. {
  257. if(event->type == InputTypeShort && model->allow_edit) {
  258. if(model->lo) {
  259. model->uid[model->index] = (model->uid[model->index] & 0xF0) |
  260. ((model->uid[model->index] - 1) & 0x0F);
  261. } else {
  262. model->uid[model->index] = ((model->uid[model->index] - 0x10) & 0xF0) |
  263. (model->uid[model->index] & 0x0F);
  264. }
  265. }
  266. },
  267. true);
  268. return true;
  269. }
  270. return true;
  271. }
  272. void fuzzer_view_field_editor_enter(void* context) {
  273. furi_assert(context);
  274. }
  275. void fuzzer_view_field_editor_exit(void* context) {
  276. furi_assert(context);
  277. }
  278. FuzzerViewFieldEditor* fuzzer_view_field_editor_alloc() {
  279. FuzzerViewFieldEditor* view_edit = malloc(sizeof(FuzzerViewFieldEditor));
  280. // View allocation and configuration
  281. view_edit->view = view_alloc();
  282. view_allocate_model(view_edit->view, ViewModelTypeLocking, sizeof(FuzzerViewFieldEditorModel));
  283. view_set_context(view_edit->view, view_edit);
  284. view_set_draw_callback(view_edit->view, (ViewDrawCallback)fuzzer_view_field_editor_draw);
  285. view_set_input_callback(view_edit->view, fuzzer_view_field_editor_input);
  286. view_set_enter_callback(view_edit->view, fuzzer_view_field_editor_enter);
  287. view_set_exit_callback(view_edit->view, fuzzer_view_field_editor_exit);
  288. with_view_model(
  289. view_edit->view,
  290. FuzzerViewFieldEditorModel * model,
  291. {
  292. model->uid_str = furi_string_alloc();
  293. model->uid = malloc(fuzzer_proto_get_max_data_size());
  294. },
  295. true);
  296. return view_edit;
  297. }
  298. void fuzzer_view_field_editor_free(FuzzerViewFieldEditor* view_edit) {
  299. furi_assert(view_edit);
  300. with_view_model(
  301. view_edit->view,
  302. FuzzerViewFieldEditorModel * model,
  303. {
  304. furi_string_free(model->uid_str);
  305. free(model->uid);
  306. },
  307. true);
  308. view_free(view_edit->view);
  309. free(view_edit);
  310. }
  311. View* fuzzer_view_field_editor_get_view(FuzzerViewFieldEditor* view_edit) {
  312. furi_assert(view_edit);
  313. return view_edit->view;
  314. }