Explorar o código

Update esubghz_chat

MX %!s(int64=2) %!d(string=hai) anos
pai
achega
1742421e21

+ 4 - 4
README.md

@@ -14,15 +14,15 @@ bugs. You have been warned.
 
 ## Usage
 
-Once opened the plugin will ask for a frequency to operate on which must be
-entered in HZ.
-
-On the next screen the plugin will ask for the method of deriving the key. If
+Once opened the plugin will ask for the method of deriving the key. If
 "No encryption" is selected, the encryption is disabled. If "Generate Key" is
 selected, a random key is generated. Otherwise, the plugin will ask for the
 selected input method. Currently a password and a hex key, as well as reading
 the key from another Flipper via NFC are supported.
 
+On the next screen the plugin will ask for a frequency to operate on which must
+be entered in HZ.
+
 Finally the a message can be input. After the message is confirmed, the plugin
 will switch to the chat view, where sent and received messages are displayed.
 To view the chat view without entering a message, enter nothing. To go back to

BIN=BIN
assets/Nfc_10px.png


BIN=BIN
assets/Nfc_14px.png


BIN=BIN
assets/chat_14px.png


BIN=BIN
assets/hex_10px.png


BIN=BIN
assets/hex_14px.png


BIN=BIN
assets/keyboard_10px.png


BIN=BIN
assets/keyboard_14px.png


BIN=BIN
assets/u2f_10px.png


BIN=BIN
assets/u2f_14px.png


+ 5 - 3
esubghz_chat.c

@@ -8,7 +8,6 @@
 #define CHAT_LEAVE_DELAY 10
 #define TICK_INTERVAL 50
 #define MESSAGE_COMPLETION_TIMEOUT 500
-#define TIMEOUT_BETWEEN_MESSAGES 500
 
 #define KBD_UNLOCK_CNT 3
 #define KBD_UNLOCK_TIMEOUT 1000
@@ -575,6 +574,9 @@ int32_t esubghz_chat(void)
 		goto err_alloc_crypto;
 	}
 
+	/* set the default frequency */
+	state->frequency = DEFAULT_FREQ;
+
 	/* set the have_read callback of the Sub-GHz worker */
 	subghz_tx_rx_worker_set_callback_have_read(state->subghz_worker,
 			have_read_cb, state);
@@ -642,8 +644,8 @@ int32_t esubghz_chat(void)
 	view_dispatcher_attach_to_gui(state->view_dispatcher, gui,
 			ViewDispatcherTypeFullscreen);
 
-	/* switch to the frequency input scene */
-	scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_FreqInput);
+	/* switch to the key menu scene */
+	scene_manager_next_scene(state->scene_manager, ESubGhzChatScene_KeyMenu);
 
 	/* run the view dispatcher, this call only returns when we close the
 	 * application */

+ 7 - 0
helpers/nfc_helpers.h

@@ -7,6 +7,13 @@ extern "C" {
 #define NFC_MAX_BYTES 256
 #define NFC_CONFIG_PAGES 4
 
+struct FreqNfcEntry {
+	uint32_t frequency;
+	uint32_t unused1;
+	uint32_t unused2;
+	uint32_t unused3;
+} __attribute__ ((packed));
+
 struct ReplayDictNfcEntry {
 	uint64_t run_id;
 	uint32_t counter;

+ 6 - 4
scenes/esubghz_chat_freq_input.c

@@ -1,11 +1,13 @@
 #include "../esubghz_chat_i.h"
 
-/* Sends FreqEntered event to scene manager. */
+/* Sends FreqEntered event to scene manager and enters the chat. */
 static void freq_input_cb(void *context)
 {
 	furi_assert(context);
 	ESubGhzChatState* state = context;
 
+	enter_chat(state);
+
 	view_dispatcher_send_custom_event(state->view_dispatcher,
 			ESubGhzChatEvent_FreqEntered);
 }
@@ -55,7 +57,7 @@ void scene_on_enter_freq_input(void* context)
 	ESubGhzChatState* state = context;
 
 	snprintf(state->text_input_store, TEXT_INPUT_STORE_SIZE, "%lu",
-			(uint32_t) DEFAULT_FREQ);
+			state->frequency);
 	text_input_reset(state->text_input);
 	text_input_set_result_callback(
 			state->text_input,
@@ -88,10 +90,10 @@ bool scene_on_event_freq_input(void* context, SceneManagerEvent event)
 	switch(event.type) {
 	case SceneManagerEventTypeCustom:
 		switch(event.event) {
-		/* switch to password input scene */
+		/* switch to message input scene */
 		case ESubGhzChatEvent_FreqEntered:
 			scene_manager_next_scene(state->scene_manager,
-					ESubGhzChatScene_KeyMenu);
+					ESubGhzChatScene_ChatInput);
 			consumed = true;
 			break;
 		}

+ 4 - 6
scenes/esubghz_chat_hex_key_input.c

@@ -1,7 +1,7 @@
 #include "../esubghz_chat_i.h"
 
-/* Sets the entered bytes as the key, enters the chat and sends a HexKeyEntered
- * event to the scene manager. */
+/* Sets the entered bytes as the key and sends a HexKeyEntered event to the
+ * scene manager. */
 static void hex_key_input_cb(void* context)
 {
 	furi_assert(context);
@@ -23,8 +23,6 @@ static void hex_key_input_cb(void* context)
 
 	state->encrypted = true;
 
-	enter_chat(state);
-
 	view_dispatcher_send_custom_event(state->view_dispatcher,
 			ESubGhzChatEvent_HexKeyEntered);
 }
@@ -61,10 +59,10 @@ bool scene_on_event_hex_key_input(void* context, SceneManagerEvent event)
 	switch(event.type) {
 	case SceneManagerEventTypeCustom:
 		switch(event.event) {
-		/* switch to message input scene */
+		/* switch to frequency input scene */
 		case ESubGhzChatEvent_HexKeyEntered:
 			scene_manager_next_scene(state->scene_manager,
-					ESubGhzChatScene_ChatInput);
+					ESubGhzChatScene_FreqInput);
 			consumed = true;
 			break;
 		}

+ 11 - 9
scenes/esubghz_chat_key_menu.c

@@ -18,7 +18,6 @@ static void key_menu_cb(void* context, uint32_t index)
 	switch(index) {
 	case ESubGhzChatKeyMenuItems_NoEncryption:
 		state->encrypted = false;
-		enter_chat(state);
 
 		view_dispatcher_send_custom_event(state->view_dispatcher,
 				ESubGhzChatEvent_KeyMenuNoEncryption);
@@ -52,7 +51,6 @@ static void key_menu_cb(void* context, uint32_t index)
 
 		/* set encrypted flag and enter the chat */
 		state->encrypted = true;
-		enter_chat(state);
 
 		view_dispatcher_send_custom_event(state->view_dispatcher,
 				ESubGhzChatEvent_KeyMenuGenKey);
@@ -78,10 +76,14 @@ void scene_on_enter_key_menu(void* context)
 
 	menu_reset(state->menu);
 
+	/* clear the crypto CTX in case we got back from password or hex key
+	 * input */
+	crypto_ctx_clear(state->crypto_ctx);
+
 	menu_add_item(
 		state->menu,
 		"No encryption",
-		&I_chat_10px,
+		&I_chat_14px,
 		ESubGhzChatKeyMenuItems_NoEncryption,
 		key_menu_cb,
 		state
@@ -89,7 +91,7 @@ void scene_on_enter_key_menu(void* context)
 	menu_add_item(
 		state->menu,
 		"Password",
-		&I_keyboard_10px,
+		&I_keyboard_14px,
 		ESubGhzChatKeyMenuItems_Password,
 		key_menu_cb,
 		state
@@ -97,7 +99,7 @@ void scene_on_enter_key_menu(void* context)
 	menu_add_item(
 		state->menu,
 		"Hex Key",
-		&I_hex_10px,
+		&I_hex_14px,
 		ESubGhzChatKeyMenuItems_HexKey,
 		key_menu_cb,
 		state
@@ -105,7 +107,7 @@ void scene_on_enter_key_menu(void* context)
 	menu_add_item(
 		state->menu,
 		"Generate Key",
-		&I_u2f_10px,
+		&I_u2f_14px,
 		ESubGhzChatKeyMenuItems_GenKey,
 		key_menu_cb,
 		state
@@ -113,7 +115,7 @@ void scene_on_enter_key_menu(void* context)
 	menu_add_item(
 		state->menu,
 		"Read Key from NFC",
-		&I_Nfc_10px,
+		&I_Nfc_14px,
 		ESubGhzChatKeyMenuItems_ReadKeyFromNfc,
 		key_menu_cb,
 		state
@@ -135,11 +137,11 @@ bool scene_on_event_key_menu(void* context, SceneManagerEvent event)
 	switch(event.type) {
 	case SceneManagerEventTypeCustom:
 		switch(event.event) {
-		/* switch to message input scene */
+		/* switch to frequency input scene */
 		case ESubGhzChatEvent_KeyMenuNoEncryption:
 		case ESubGhzChatEvent_KeyMenuGenKey:
 			scene_manager_next_scene(state->scene_manager,
-					ESubGhzChatScene_ChatInput);
+					ESubGhzChatScene_FreqInput);
 			consumed = true;
 			break;
 

+ 12 - 5
scenes/esubghz_chat_key_read_popup.c

@@ -31,9 +31,8 @@ static void key_read_popup_timeout_cb(void* context)
 	if (cur_state == KeyReadPopupState_Fail) {
 		view_dispatcher_send_custom_event(state->view_dispatcher,
 				ESubGhzChatEvent_KeyReadPopupFailed);
-	/* done displaying our success, enter chat */
+	/* done displaying our success */
 	} else if (cur_state == KeyReadPopupState_Success) {
-		enter_chat(state);
 		view_dispatcher_send_custom_event(state->view_dispatcher,
 				ESubGhzChatEvent_KeyReadPopupSucceeded);
 	}
@@ -94,9 +93,17 @@ static bool key_read_popup_handle_key_read(ESubGhzChatState *state)
 		return false;
 	}
 
+	/* read the frequency */
+	if (data_read >= (KEY_BITS / 8) + sizeof(struct FreqNfcEntry)) {
+		struct FreqNfcEntry *freq_entry = (struct FreqNfcEntry *)
+			(dev_data->mf_ul_data.data + (KEY_BITS / 8));
+		state->frequency = __ntohl(freq_entry->frequency);
+	}
+
 	/* read the replay dict */
 	struct ReplayDictNfcReaderContext rd_ctx = {
-		.cur = dev_data->mf_ul_data.data + (KEY_BITS / 8),
+		.cur = dev_data->mf_ul_data.data + (KEY_BITS / 8) +
+			sizeof(struct FreqNfcEntry),
 		.max = dev_data->mf_ul_data.data + (data_read < NFC_MAX_BYTES ?
 				data_read : NFC_MAX_BYTES)
 	};
@@ -247,10 +254,10 @@ bool scene_on_event_key_read_popup(void* context, SceneManagerEvent event)
 			consumed = true;
 			break;
 
-		/* success, go to chat input */
+		/* success, go to frequency input */
 		case ESubGhzChatEvent_KeyReadPopupSucceeded:
 			scene_manager_next_scene(state->scene_manager,
-					ESubGhzChatScene_ChatInput);
+					ESubGhzChatScene_FreqInput);
 			consumed = true;
 			break;
 

+ 15 - 4
scenes/esubghz_chat_key_share_popup.c

@@ -49,22 +49,33 @@ static void prepare_nfc_dev_data(ESubGhzChatState *state)
 	dev_data->mf_ul_data.version.storage_size = 0x11;
 	dev_data->mf_ul_data.version.protocol_type = 0x03;
 
+	size_t data_written = 0;
+
 	/* write key */
 	crypto_ctx_get_key(state->crypto_ctx, dev_data->mf_ul_data.data);
+	data_written += (KEY_BITS / 8);
+
+	/* write frequency */
+	struct FreqNfcEntry *freq_entry = (struct FreqNfcEntry *)
+		(dev_data->mf_ul_data.data + data_written);
+	freq_entry->frequency = __htonl(state->frequency);
+	freq_entry->unused1 = 0;
+	freq_entry->unused2 = 0;
+	freq_entry->unused3 = 0;
+	data_written += sizeof(struct FreqNfcEntry);
 
 	/* write the replay dict */
 	struct ReplayDictNfcWriterContext wr_ctx = {
-		.cur = dev_data->mf_ul_data.data + (KEY_BITS / 8),
+		.cur = dev_data->mf_ul_data.data + data_written,
 		.max = dev_data->mf_ul_data.data + NFC_MAX_BYTES
 	};
 
 	size_t n_entries = crypto_ctx_dump_replay_dict(state->crypto_ctx,
 			replay_dict_nfc_writer, &wr_ctx);
+	data_written += n_entries * sizeof(struct ReplayDictNfcEntry);
 
 	/* calculate size of data, add 16 for config pages */
-	dev_data->mf_ul_data.data_size = (KEY_BITS / 8) + (n_entries *
-			sizeof(struct ReplayDictNfcEntry)) + (NFC_CONFIG_PAGES
-			* 4);
+	dev_data->mf_ul_data.data_size = data_written + (NFC_CONFIG_PAGES * 4);
 }
 
 /* Prepares the key share popup scene. */

+ 3 - 5
scenes/esubghz_chat_pass_input.c

@@ -1,6 +1,6 @@
 #include "../esubghz_chat_i.h"
 
-/* Sends PassEntered event to scene manager and enters the chat. */
+/* Sends PassEntered event to scene manager. */
 static void pass_input_cb(void *context)
 {
 	furi_assert(context);
@@ -9,8 +9,6 @@ static void pass_input_cb(void *context)
 	crypto_explicit_bzero(state->text_input_store,
 			sizeof(state->text_input_store));
 
-	enter_chat(state);
-
 	view_dispatcher_send_custom_event(state->view_dispatcher,
 			ESubGhzChatEvent_PassEntered);
 }
@@ -96,10 +94,10 @@ bool scene_on_event_pass_input(void* context, SceneManagerEvent event)
 	switch(event.type) {
 	case SceneManagerEventTypeCustom:
 		switch(event.event) {
-		/* switch to message input scene */
+		/* switch to frequency input scene */
 		case ESubGhzChatEvent_PassEntered:
 			scene_manager_next_scene(state->scene_manager,
-					ESubGhzChatScene_ChatInput);
+					ESubGhzChatScene_FreqInput);
 			consumed = true;
 			break;
 		}