esubghz_chat_key_menu.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "../esubghz_chat_i.h"
  2. typedef enum {
  3. ESubGhzChatKeyMenuItems_NoEncryption,
  4. ESubGhzChatKeyMenuItems_Password,
  5. ESubGhzChatKeyMenuItems_HexKey,
  6. ESubGhzChatKeyMenuItems_GenKey,
  7. } ESubGhzChatKeyMenuItems;
  8. static void key_menu_cb(void* context, uint32_t index)
  9. {
  10. furi_assert(context);
  11. ESubGhzChatState* state = context;
  12. uint8_t key[KEY_BITS / 8];
  13. switch(index) {
  14. case ESubGhzChatKeyMenuItems_NoEncryption:
  15. state->encrypted = false;
  16. enter_chat(state);
  17. scene_manager_handle_custom_event(state->scene_manager,
  18. ESubGhzChatEvent_KeyMenuNoEncryption);
  19. break;
  20. case ESubGhzChatKeyMenuItems_Password:
  21. scene_manager_handle_custom_event(state->scene_manager,
  22. ESubGhzChatEvent_KeyMenuPassword);
  23. break;
  24. case ESubGhzChatKeyMenuItems_HexKey:
  25. scene_manager_handle_custom_event(state->scene_manager,
  26. ESubGhzChatEvent_KeyMenuHexKey);
  27. break;
  28. case ESubGhzChatKeyMenuItems_GenKey:
  29. /* generate a random key */
  30. furi_hal_random_fill_buf(key, KEY_BITS / 8);
  31. /* initiate the crypto context */
  32. bool ret = crypto_ctx_set_key(state->crypto_ctx, key);
  33. /* cleanup */
  34. crypto_explicit_bzero(key, sizeof(key));
  35. if (!ret) {
  36. crypto_ctx_clear(state->crypto_ctx);
  37. return;
  38. }
  39. /* set encrypted flag and enter the chat */
  40. state->encrypted = true;
  41. enter_chat(state);
  42. scene_manager_handle_custom_event(state->scene_manager,
  43. ESubGhzChatEvent_KeyMenuGenKey);
  44. break;
  45. default:
  46. break;
  47. }
  48. }
  49. /* Prepares the key menu scene. */
  50. void scene_on_enter_key_menu(void* context)
  51. {
  52. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_key_menu");
  53. furi_assert(context);
  54. ESubGhzChatState* state = context;
  55. menu_reset(state->menu);
  56. menu_add_item(
  57. state->menu,
  58. "No encryption",
  59. NULL,
  60. ESubGhzChatKeyMenuItems_NoEncryption,
  61. key_menu_cb,
  62. state
  63. );
  64. menu_add_item(
  65. state->menu,
  66. "Password",
  67. NULL,
  68. ESubGhzChatKeyMenuItems_Password,
  69. key_menu_cb,
  70. state
  71. );
  72. menu_add_item(
  73. state->menu,
  74. "Hex Key",
  75. NULL,
  76. ESubGhzChatKeyMenuItems_HexKey,
  77. key_menu_cb,
  78. state
  79. );
  80. menu_add_item(
  81. state->menu,
  82. "Generate Key",
  83. NULL,
  84. ESubGhzChatKeyMenuItems_GenKey,
  85. key_menu_cb,
  86. state
  87. );
  88. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Menu);
  89. }
  90. /* Handles scene manager events for the key menu scene. */
  91. bool scene_on_event_key_menu(void* context, SceneManagerEvent event)
  92. {
  93. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_key_menu");
  94. furi_assert(context);
  95. ESubGhzChatState* state = context;
  96. bool consumed = false;
  97. switch(event.type) {
  98. case SceneManagerEventTypeCustom:
  99. switch(event.event) {
  100. /* switch to message input scene */
  101. case ESubGhzChatEvent_KeyMenuNoEncryption:
  102. case ESubGhzChatEvent_KeyMenuGenKey:
  103. scene_manager_next_scene(state->scene_manager,
  104. ESubGhzChatScene_ChatInput);
  105. consumed = true;
  106. break;
  107. /* switch to password input scene */
  108. case ESubGhzChatEvent_KeyMenuPassword:
  109. scene_manager_next_scene(state->scene_manager,
  110. ESubGhzChatScene_PassInput);
  111. consumed = true;
  112. break;
  113. /* switch to hex key input scene */
  114. case ESubGhzChatEvent_KeyMenuHexKey:
  115. scene_manager_next_scene(state->scene_manager,
  116. ESubGhzChatScene_HexKeyInput);
  117. consumed = true;
  118. break;
  119. }
  120. break;
  121. case SceneManagerEventTypeBack:
  122. /* stop the application if the user presses back here */
  123. view_dispatcher_stop(state->view_dispatcher);
  124. consumed = true;
  125. break;
  126. default:
  127. consumed = false;
  128. break;
  129. }
  130. return consumed;
  131. }
  132. /* Cleans up the key menu scene. */
  133. void scene_on_exit_key_menu(void* context)
  134. {
  135. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_key_menu");
  136. furi_assert(context);
  137. ESubGhzChatState* state = context;
  138. menu_reset(state->menu);
  139. }