esubghz_chat_key_menu.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. state->name_prefix, furi_get_tick());
  35. /* cleanup */
  36. crypto_explicit_bzero(key, sizeof(key));
  37. if (!ret) {
  38. crypto_ctx_clear(state->crypto_ctx);
  39. return;
  40. }
  41. /* set encrypted flag and enter the chat */
  42. state->encrypted = true;
  43. enter_chat(state);
  44. view_dispatcher_send_custom_event(state->view_dispatcher,
  45. ESubGhzChatEvent_KeyMenuGenKey);
  46. break;
  47. case ESubGhzChatKeyMenuItems_ReadKeyFromNfc:
  48. view_dispatcher_send_custom_event(state->view_dispatcher,
  49. ESubGhzChatEvent_KeyMenuReadKeyFromNfc);
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. /* Prepares the key menu scene. */
  56. void scene_on_enter_key_menu(void* context)
  57. {
  58. FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_key_menu");
  59. furi_assert(context);
  60. ESubGhzChatState* state = context;
  61. menu_reset(state->menu);
  62. menu_add_item(
  63. state->menu,
  64. "No encryption",
  65. NULL,
  66. ESubGhzChatKeyMenuItems_NoEncryption,
  67. key_menu_cb,
  68. state
  69. );
  70. menu_add_item(
  71. state->menu,
  72. "Password",
  73. NULL,
  74. ESubGhzChatKeyMenuItems_Password,
  75. key_menu_cb,
  76. state
  77. );
  78. menu_add_item(
  79. state->menu,
  80. "Hex Key",
  81. NULL,
  82. ESubGhzChatKeyMenuItems_HexKey,
  83. key_menu_cb,
  84. state
  85. );
  86. menu_add_item(
  87. state->menu,
  88. "Generate Key",
  89. NULL,
  90. ESubGhzChatKeyMenuItems_GenKey,
  91. key_menu_cb,
  92. state
  93. );
  94. menu_add_item(
  95. state->menu,
  96. "Read Key from NFC",
  97. NULL,
  98. ESubGhzChatKeyMenuItems_ReadKeyFromNfc,
  99. key_menu_cb,
  100. state
  101. );
  102. view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Menu);
  103. }
  104. /* Handles scene manager events for the key menu scene. */
  105. bool scene_on_event_key_menu(void* context, SceneManagerEvent event)
  106. {
  107. FURI_LOG_T(APPLICATION_NAME, "scene_on_event_key_menu");
  108. furi_assert(context);
  109. ESubGhzChatState* state = context;
  110. bool consumed = false;
  111. switch(event.type) {
  112. case SceneManagerEventTypeCustom:
  113. switch(event.event) {
  114. /* switch to message input scene */
  115. case ESubGhzChatEvent_KeyMenuNoEncryption:
  116. case ESubGhzChatEvent_KeyMenuGenKey:
  117. scene_manager_next_scene(state->scene_manager,
  118. ESubGhzChatScene_ChatInput);
  119. consumed = true;
  120. break;
  121. /* switch to password input scene */
  122. case ESubGhzChatEvent_KeyMenuPassword:
  123. scene_manager_next_scene(state->scene_manager,
  124. ESubGhzChatScene_PassInput);
  125. consumed = true;
  126. break;
  127. /* switch to hex key input scene */
  128. case ESubGhzChatEvent_KeyMenuHexKey:
  129. scene_manager_next_scene(state->scene_manager,
  130. ESubGhzChatScene_HexKeyInput);
  131. consumed = true;
  132. break;
  133. /* switch to hex key read scene */
  134. case ESubGhzChatEvent_KeyMenuReadKeyFromNfc:
  135. scene_manager_next_scene(state->scene_manager,
  136. ESubGhzChatScene_KeyReadPopup);
  137. consumed = true;
  138. break;
  139. }
  140. break;
  141. case SceneManagerEventTypeBack:
  142. /* stop the application if the user presses back here */
  143. view_dispatcher_stop(state->view_dispatcher);
  144. consumed = true;
  145. break;
  146. default:
  147. consumed = false;
  148. break;
  149. }
  150. return consumed;
  151. }
  152. /* Cleans up the key menu scene. */
  153. void scene_on_exit_key_menu(void* context)
  154. {
  155. FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_key_menu");
  156. furi_assert(context);
  157. ESubGhzChatState* state = context;
  158. menu_reset(state->menu);
  159. }