Quellcode durchsuchen

Add menu to select encryption method

twisted_pear vor 2 Jahren
Ursprung
Commit
4927e6886a

+ 57 - 0
esubghz_chat.c

@@ -7,6 +7,7 @@
 
 #include "esubghz_chat_i.h"
 
+#define CHAT_LEAVE_DELAY 10
 #define TICK_INTERVAL 50
 #define MESSAGE_COMPLETION_TIMEOUT 500
 #define TIMEOUT_BETWEEN_MESSAGES 500
@@ -115,6 +116,49 @@ void tx_msg_input(ESubGhzChatState *state)
 			tx_size);
 }
 
+/* Displays whether or not encryption has been enabled in the text box. Also
+ * clears the text input buffer to remove the password and starts the Sub-GHz
+ * worker. After starting the worker a join message is transmitted. */
+void enter_chat(ESubGhzChatState *state)
+{
+	furi_string_cat_printf(state->chat_box_store, "\nEncrypted: %s",
+			(state->encrypted ? "yes" : "no"));
+
+	/* clear the text input buffer to remove a password or key */
+	crypto_explicit_bzero(state->text_input_store,
+			sizeof(state->text_input_store));
+
+	subghz_tx_rx_worker_start(state->subghz_worker, state->subghz_device,
+			state->frequency);
+
+	/* concatenate the name prefix and join message */
+	furi_string_set(state->msg_input, state->name_prefix);
+	furi_string_cat_str(state->msg_input, " joined chat.");
+
+	/* encrypt and transmit message */
+	tx_msg_input(state);
+
+	/* clear message input buffer */
+	furi_string_set_char(state->msg_input, 0, 0);
+}
+
+/* Sends a leave message */
+void exit_chat(ESubGhzChatState *state)
+{
+	/* concatenate the name prefix and leave message */
+	furi_string_set(state->msg_input, state->name_prefix);
+	furi_string_cat_str(state->msg_input, " left chat.");
+
+	/* encrypt and transmit message */
+	tx_msg_input(state);
+
+	/* clear message input buffer */
+	furi_string_set_char(state->msg_input, 0, 0);
+
+	/* wait for leave message to be delivered */
+	furi_delay_ms(CHAT_LEAVE_DELAY);
+}
+
 /* Whether or not to display the locked message. */
 static bool kbd_lock_msg_display(ESubGhzChatState *state)
 {
@@ -401,6 +445,11 @@ int32_t esubghz_chat(void)
 		goto err_alloc_hs;
 	}
 
+	state->menu = menu_alloc();
+	if (state->menu == NULL) {
+		goto err_alloc_menu;
+	}
+
 	state->text_input = text_input_alloc();
 	if (state->text_input == NULL) {
 		goto err_alloc_ti;
@@ -463,6 +512,8 @@ int32_t esubghz_chat(void)
 			TICK_INTERVAL);
 
 	/* add our two views to the view dispatcher */
+	view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Menu,
+			menu_get_view(state->menu));
 	view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_Input,
 			text_input_get_view(state->text_input));
 	view_dispatcher_add_view(state->view_dispatcher, ESubGhzChatView_ChatBox,
@@ -483,6 +534,7 @@ int32_t esubghz_chat(void)
 
 	/* if it is running, stop the Sub-GHz worker */
 	if (subghz_tx_rx_worker_is_running(state->subghz_worker)) {
+		exit_chat(state);
 		subghz_tx_rx_worker_stop(state->subghz_worker);
 	}
 
@@ -492,6 +544,8 @@ int32_t esubghz_chat(void)
 	furi_record_close(RECORD_GUI);
 
 	/* remove our two views from the view dispatcher */
+	view_dispatcher_remove_view(state->view_dispatcher,
+			ESubGhzChatView_Menu);
 	view_dispatcher_remove_view(state->view_dispatcher,
 			ESubGhzChatView_Input);
 	view_dispatcher_remove_view(state->view_dispatcher,
@@ -525,6 +579,9 @@ err_alloc_cb:
 	text_input_free(state->text_input);
 
 err_alloc_ti:
+	menu_free(state->menu);
+
+err_alloc_menu:
 	helper_strings_free(state);
 
 err_alloc_hs:

+ 6 - 2
esubghz_chat_i.h

@@ -4,6 +4,7 @@
 #include <gui/view_dispatcher_i.h>
 #include <gui/view_port_i.h>
 #include <gui/scene_manager.h>
+#include <gui/modules/menu.h>
 #include <gui/modules/text_box.h>
 #include <gui/modules/text_input.h>
 #include <notification/notification_messages.h>
@@ -17,8 +18,6 @@
 
 #define DEFAULT_FREQ 433920000
 
-#define CHAT_LEAVE_DELAY 10
-
 #define RX_TX_BUFFER_SIZE 1024
 
 #define CHAT_BOX_STORE_SIZE 4096
@@ -30,6 +29,7 @@ typedef struct {
 	NotificationApp *notification;
 
 	// UI elements
+	Menu *menu;
 	TextBox *chat_box;
 	FuriString *chat_box_store;
 	TextInput *text_input;
@@ -65,13 +65,17 @@ typedef struct {
 
 typedef enum {
 	ESubGhzChatEvent_FreqEntered,
+	ESubGhzChatEvent_KeyMenuNoEncryption,
+	ESubGhzChatEvent_KeyMenuPassword,
 	ESubGhzChatEvent_PassEntered,
 	ESubGhzChatEvent_MsgEntered
 } ESubGhzChatEvent;
 
 typedef enum {
+	ESubGhzChatView_Menu,
 	ESubGhzChatView_Input,
 	ESubGhzChatView_ChatBox,
 } ESubGhzChatView;
 
 void tx_msg_input(ESubGhzChatState *state);
+void enter_chat(ESubGhzChatState *state);

+ 1 - 16
scenes/esubghz_chat_chat_input.c

@@ -98,22 +98,7 @@ bool scene_on_event_chat_input(void* context, SceneManagerEvent event)
 		break;
 
 	case SceneManagerEventTypeBack:
-		/* stop the application and send a leave message if the user
-		 * presses back here */
-
-		/* concatenate the name prefix and leave message */
-		furi_string_set(state->msg_input, state->name_prefix);
-		furi_string_cat_str(state->msg_input, " left chat.");
-
-		/* encrypt and transmit message */
-		tx_msg_input(state);
-
-		/* clear message input buffer */
-		furi_string_set_char(state->msg_input, 0, 0);
-
-		/* wait for leave message to be delivered */
-                furi_delay_ms(CHAT_LEAVE_DELAY);
-
+		/* stop the application if the user presses back here */
 		view_dispatcher_stop(state->view_dispatcher);
 		consumed = true;
 		break;

+ 1 - 1
scenes/esubghz_chat_freq_input.c

@@ -95,7 +95,7 @@ bool scene_on_event_freq_input(void* context, SceneManagerEvent event)
 		/* switch to password input scene */
 		case ESubGhzChatEvent_FreqEntered:
 			scene_manager_next_scene(state->scene_manager,
-					ESubGhzChatScene_PassInput);
+					ESubGhzChatScene_KeyMenu);
 			consumed = true;
 			break;
 		}

+ 114 - 0
scenes/esubghz_chat_key_menu.c

@@ -0,0 +1,114 @@
+#include "../esubghz_chat_i.h"
+
+typedef enum {
+	ESubGhzChatKeyMenuItems_NoEncryption,
+	ESubGhzChatKeyMenuItems_Password,
+} ESubGhzChatKeyMenuItems;
+
+static void key_menu_cb(void* context, uint32_t index)
+{
+	furi_assert(context);
+	ESubGhzChatState* state = context;
+
+	switch(index) {
+	case ESubGhzChatKeyMenuItems_NoEncryption:
+		state->encrypted = false;
+		enter_chat(state);
+
+		scene_manager_handle_custom_event(state->scene_manager,
+				ESubGhzChatEvent_KeyMenuNoEncryption);
+		break;
+
+	case ESubGhzChatKeyMenuItems_Password:
+		scene_manager_handle_custom_event(state->scene_manager,
+				ESubGhzChatEvent_KeyMenuPassword);
+		break;
+
+	default:
+		break;
+	}
+}
+
+/* Prepares the key menu scene. */
+void scene_on_enter_key_menu(void* context)
+{
+	FURI_LOG_T(APPLICATION_NAME, "scene_on_enter_key_menu");
+
+	furi_assert(context);
+	ESubGhzChatState* state = context;
+
+	menu_reset(state->menu);
+
+	menu_add_item(
+		state->menu,
+		"No encryption",
+		NULL,
+		ESubGhzChatKeyMenuItems_NoEncryption,
+		key_menu_cb,
+		state
+	);
+	menu_add_item(
+		state->menu,
+		"Password",
+		NULL,
+		ESubGhzChatKeyMenuItems_Password,
+		key_menu_cb,
+		state
+	);
+
+	view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Menu);
+}
+
+/* Handles scene manager events for the key menu scene. */
+bool scene_on_event_key_menu(void* context, SceneManagerEvent event)
+{
+	FURI_LOG_T(APPLICATION_NAME, "scene_on_event_key_menu");
+
+	furi_assert(context);
+	ESubGhzChatState* state = context;
+
+	bool consumed = false;
+
+	switch(event.type) {
+	case SceneManagerEventTypeCustom:
+		switch(event.event) {
+		/* switch to message input scene */
+		case ESubGhzChatEvent_KeyMenuNoEncryption:
+			scene_manager_next_scene(state->scene_manager,
+					ESubGhzChatScene_ChatInput);
+			consumed = true;
+			break;
+		/* switch to password input scene */
+		case ESubGhzChatEvent_KeyMenuPassword:
+			scene_manager_next_scene(state->scene_manager,
+					ESubGhzChatScene_PassInput);
+			consumed = true;
+			break;
+		}
+		break;
+
+	case SceneManagerEventTypeBack:
+		/* stop the application if the user presses back here */
+		view_dispatcher_stop(state->view_dispatcher);
+		consumed = true;
+		break;
+
+	default:
+		consumed = false;
+		break;
+	}
+
+	return consumed;
+}
+
+/* Cleans up the key menu scene. */
+void scene_on_exit_key_menu(void* context)
+{
+	FURI_LOG_T(APPLICATION_NAME, "scene_on_exit_key_menu");
+
+	furi_assert(context);
+	ESubGhzChatState* state = context;
+
+	menu_reset(state->menu);
+}
+

+ 3 - 44
scenes/esubghz_chat_pass_input.c

@@ -1,33 +1,12 @@
 #include "../esubghz_chat_i.h"
 
-/* Sends PassEntered event to scene manager and displays whether or not
- * encryption has been enabled in the text box. Also clears the text input
- * buffer to remove the password and starts the Sub-GHz worker. After starting
- * the worker a join message is transmitted. */
+/* Sends PassEntered event to scene manager and enters the chat. */
 static void pass_input_cb(void *context)
 {
 	furi_assert(context);
 	ESubGhzChatState* state = context;
 
-	furi_string_cat_printf(state->chat_box_store, "\nEncrypted: %s",
-			(state->encrypted ? "yes" : "no"));
-
-	/* clear the text input buffer to remove the password */
-	crypto_explicit_bzero(state->text_input_store,
-			sizeof(state->text_input_store));
-
-	subghz_tx_rx_worker_start(state->subghz_worker, state->subghz_device,
-			state->frequency);
-
-	/* concatenate the name prefix and join message */
-	furi_string_set(state->msg_input, state->name_prefix);
-	furi_string_cat_str(state->msg_input, " joined chat.");
-
-	/* encrypt and transmit message */
-	tx_msg_input(state);
-
-	/* clear message input buffer */
-	furi_string_set_char(state->msg_input, 0, 0);
+	enter_chat(state);
 
 	scene_manager_handle_custom_event(state->scene_manager,
 			ESubGhzChatEvent_PassEntered);
@@ -45,20 +24,11 @@ static bool pass_input_validator(const char *text, FuriString *error,
 	furi_assert(context);
 	ESubGhzChatState* state = context;
 
-#ifdef FW_ORIGIN_Official
 	if (strlen(text) == 0) {
 		furi_string_printf(error, "Enter a\npassword!");
 		return false;
 	}
 
-	if (strcmp(text, " ") == 0) {
-#else /* FW_ORIGIN_Official */
-	if (strlen(text) == 0) {
-#endif /* FW_ORIGIN_Official */
-		state->encrypted = false;
-		return true;
-	}
-
 	unsigned char key[KEY_BITS / 8];
 
 	/* derive a key from the password */
@@ -104,12 +74,7 @@ void scene_on_enter_pass_input(void* context)
 			state);
 	text_input_set_header_text(
 			state->text_input,
-#ifdef FW_ORIGIN_Official
-			"Password (space for no encr.)");
-#else /* FW_ORIGIN_Official */
-			"Password (empty for no encr.)");
-	text_input_set_minimum_length(state->text_input, 0);
-#endif /* FW_ORIGIN_Official */
+			"Password");
 
 	view_dispatcher_switch_to_view(state->view_dispatcher, ESubGhzChatView_Input);
 }
@@ -136,12 +101,6 @@ bool scene_on_event_pass_input(void* context, SceneManagerEvent event)
 		}
 		break;
 
-	case SceneManagerEventTypeBack:
-		/* stop the application if the user presses back here */
-		view_dispatcher_stop(state->view_dispatcher);
-		consumed = true;
-		break;
-
 	default:
 		consumed = false;
 		break;

+ 1 - 0
scenes/esubghz_chat_scene_config.h

@@ -1,4 +1,5 @@
 ADD_SCENE(esubghz_chat, freq_input, FreqInput)
+ADD_SCENE(esubghz_chat, key_menu, KeyMenu)
 ADD_SCENE(esubghz_chat, pass_input, PassInput)
 ADD_SCENE(esubghz_chat, chat_input, ChatInput)
 ADD_SCENE(esubghz_chat, chat_box, ChatBox)