esubghz_chat_key_menu.c 4.1 KB

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