u2f_view.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "u2f_view.h"
  2. #include <gui/elements.h>
  3. struct U2fView {
  4. View* view;
  5. U2fOkCallback callback;
  6. void* context;
  7. };
  8. typedef struct {
  9. U2fViewMsg display_msg;
  10. } U2fModel;
  11. static void u2f_view_draw_callback(Canvas* canvas, void* _model) {
  12. U2fModel* model = _model;
  13. canvas_set_font(canvas, FontPrimary);
  14. canvas_draw_str_aligned(canvas, 0, 0, AlignLeft, AlignTop, "U2F Demo");
  15. if(model->display_msg == U2fMsgRegister) {
  16. canvas_set_font(canvas, FontPrimary);
  17. canvas_draw_str_aligned(canvas, 0, 45, AlignLeft, AlignBottom, "Registration");
  18. canvas_set_font(canvas, FontSecondary);
  19. canvas_draw_str_aligned(canvas, 0, 63, AlignLeft, AlignBottom, "Press [OK] to confirm");
  20. } else if(model->display_msg == U2fMsgAuth) {
  21. canvas_set_font(canvas, FontPrimary);
  22. canvas_draw_str_aligned(canvas, 0, 45, AlignLeft, AlignBottom, "Authentication");
  23. canvas_set_font(canvas, FontSecondary);
  24. canvas_draw_str_aligned(canvas, 0, 63, AlignLeft, AlignBottom, "Press [OK] to confirm");
  25. } else if(model->display_msg == U2fMsgError) {
  26. canvas_set_font(canvas, FontPrimary);
  27. canvas_draw_str_aligned(canvas, 64, 40, AlignCenter, AlignCenter, "U2F data missing");
  28. }
  29. }
  30. static bool u2f_view_input_callback(InputEvent* event, void* context) {
  31. furi_assert(context);
  32. U2fView* u2f = context;
  33. bool consumed = false;
  34. if(event->type == InputTypeShort) {
  35. if(event->key == InputKeyOk) {
  36. consumed = true;
  37. if(u2f->callback != NULL) u2f->callback(InputTypeShort, u2f->context);
  38. }
  39. }
  40. return consumed;
  41. }
  42. U2fView* u2f_view_alloc() {
  43. U2fView* u2f = furi_alloc(sizeof(U2fView));
  44. u2f->view = view_alloc();
  45. view_allocate_model(u2f->view, ViewModelTypeLocking, sizeof(U2fModel));
  46. view_set_context(u2f->view, u2f);
  47. view_set_draw_callback(u2f->view, u2f_view_draw_callback);
  48. view_set_input_callback(u2f->view, u2f_view_input_callback);
  49. return u2f;
  50. }
  51. void u2f_view_free(U2fView* u2f) {
  52. furi_assert(u2f);
  53. view_free(u2f->view);
  54. free(u2f);
  55. }
  56. View* u2f_view_get_view(U2fView* u2f) {
  57. furi_assert(u2f);
  58. return u2f->view;
  59. }
  60. void u2f_view_set_ok_callback(U2fView* u2f, U2fOkCallback callback, void* context) {
  61. furi_assert(u2f);
  62. furi_assert(callback);
  63. with_view_model(
  64. u2f->view, (U2fModel * model) {
  65. u2f->callback = callback;
  66. u2f->context = context;
  67. return false;
  68. });
  69. }
  70. void u2f_view_set_state(U2fView* u2f, U2fViewMsg msg) {
  71. with_view_model(
  72. u2f->view, (U2fModel * model) {
  73. model->display_msg = msg;
  74. return false;
  75. });
  76. }