Skip to content

Commit 4a2fe73

Browse files
committed
and some more...
1 parent 22d1d51 commit 4a2fe73

File tree

6 files changed

+20
-16
lines changed

6 files changed

+20
-16
lines changed

Documentation/espnow-v2.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ I [EspNowService] ESP-NOW version: 2.0
4343

4444
### Larger Messages
4545

46-
- Message size increased from **127 to 1416 characters**
46+
- Message size increased from **127 to 1417 characters**
4747
- Variable-length packet transmission (only sends actual message length)
4848
- Backwards compatible: messages < 197 chars still work with v1.0 devices
4949

@@ -69,7 +69,7 @@ Offset Size Field
6969
| `Tactility/Private/Tactility/app/chat/ChatProtocol.h` | `MESSAGE_SIZE` = 1417, added header constants |
7070
| `Tactility/Source/app/chat/ChatProtocol.cpp` | Variable-length serialize/deserialize |
7171
| `Tactility/Source/app/chat/ChatApp.cpp` | Send actual packet size |
72-
| `Tactility/Source/app/chat/ChatView.cpp` | Input field max length = 1416 |
72+
| `Tactility/Source/app/chat/ChatView.cpp` | Input field max length = 1417 |
7373
| `Documentation/chat.md` | Updated protocol documentation |
7474

7575
## Compatibility

Tactility/Include/Tactility/service/espnow/EspNow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void unsubscribeReceiver(ReceiverSubscription subscription);
6161
/** Get the ESP-NOW protocol version (1 for v1.0, 2 for v2.0). Returns 0 if service not running. */
6262
uint32_t getVersion();
6363

64-
/** Get the maximum data length for current ESP-NOW version (250 for v1.0, 1470 for v2.0). */
64+
/** Get the maximum data length for current ESP-NOW version (250 for v1.0, 1470 for v2.0). Returns 0 if service not running. */
6565
size_t getMaxDataLength();
6666

6767
}

Tactility/Source/app/chat/ChatApp.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ void ChatApp::sendMessage(const std::string& text) {
122122
void ChatApp::applySettings(const std::string& nickname, const std::string& keyHex) {
123123
bool needRestart = false;
124124

125-
settings.nickname = nickname;
125+
// Trim nickname to protocol limit
126+
settings.nickname = nickname.substr(0, SENDER_NAME_SIZE - 1);
126127

127128
// Parse hex key
128129
if (keyHex.size() == ESP_NOW_KEY_LEN * 2) {
@@ -133,7 +134,9 @@ void ChatApp::applySettings(const std::string& nickname, const std::string& keyH
133134
char hex[3] = { keyHex[i * 2], keyHex[i * 2 + 1], 0 };
134135
newKey[i] = static_cast<uint8_t>(strtoul(hex, nullptr, 16));
135136
}
136-
if (!std::equal(newKey, newKey + ESP_NOW_KEY_LEN, settings.encryptionKey.begin())) {
137+
// Restart if key changed OR if encryption is being enabled
138+
bool wasEnabled = settings.hasEncryptionKey;
139+
if (!wasEnabled || !std::equal(newKey, newKey + ESP_NOW_KEY_LEN, settings.encryptionKey.begin())) {
137140
std::copy(newKey, newKey + ESP_NOW_KEY_LEN, settings.encryptionKey.begin());
138141
needRestart = true;
139142
}
@@ -151,7 +154,7 @@ void ChatApp::applySettings(const std::string& nickname, const std::string& keyH
151154
LOGGER.warn("Key must be exactly {} hex characters, got {}", ESP_NOW_KEY_LEN * 2, keyHex.size());
152155
}
153156

154-
state.setLocalNickname(nickname);
157+
state.setLocalNickname(settings.nickname);
155158
saveSettings(settings);
156159

157160
if (needRestart) {
@@ -161,8 +164,9 @@ void ChatApp::applySettings(const std::string& nickname, const std::string& keyH
161164
}
162165

163166
void ChatApp::switchChannel(const std::string& chatChannel) {
164-
state.setCurrentChannel(chatChannel);
165-
settings.chatChannel = chatChannel;
167+
const auto trimmedChannel = chatChannel.substr(0, TARGET_SIZE - 1);
168+
state.setCurrentChannel(trimmedChannel);
169+
settings.chatChannel = trimmedChannel;
166170
saveSettings(settings);
167171

168172
{

Tactility/Source/app/chat/ChatProtocol.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ bool deserializeMessage(const uint8_t* data, int length, ParsedMessage& out) {
5454
// Calculate actual message length from packet size and ensure null termination
5555
size_t msgLen = length - MESSAGE_HEADER_SIZE;
5656
if (msgLen > 0 && msgLen < MESSAGE_SIZE) {
57-
msg.message[msgLen] = '\0'; // Ensure null at end of received data
57+
msg.message[msgLen] = '\0'; // Handle malformed packets missing null terminator
5858
}
5959
msg.message[MESSAGE_SIZE - 1] = '\0'; // Safety: ensure buffer is always terminated
6060

Tactility/Source/app/chat/ChatSettings.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <iomanip>
1717
#include <map>
1818
#include <sstream>
19-
#include <string>
2019
#include <unistd.h>
2120

2221
namespace tt::app::chat {
@@ -63,7 +62,7 @@ static bool readHex(const std::string& input, uint8_t* buffer, size_t length) {
6362

6463
static bool encryptKey(const uint8_t key[ESP_NOW_KEY_LEN], std::string& hexOutput) {
6564
uint8_t iv[16];
66-
crypt::getIv(IV_SEED, sizeof(IV_SEED) - 1, iv);
65+
crypt::getIv(IV_SEED, std::strlen(IV_SEED), iv);
6766

6867
uint8_t encrypted[ESP_NOW_KEY_LEN];
6968
if (crypt::encrypt(iv, key, encrypted, ESP_NOW_KEY_LEN) != 0) {
@@ -86,7 +85,7 @@ static bool decryptKey(const std::string& hexInput, uint8_t key[ESP_NOW_KEY_LEN]
8685
}
8786

8887
uint8_t iv[16];
89-
crypt::getIv(IV_SEED, sizeof(IV_SEED) - 1, iv);
88+
crypt::getIv(IV_SEED, std::strlen(IV_SEED), iv);
9089

9190
if (crypt::decrypt(iv, encrypted, key, ESP_NOW_KEY_LEN) != 0) {
9291
LOGGER.error("Failed to decrypt key");

Tactility/Source/service/espnow/EspNowService.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,17 @@ void EspNowService::enableFromDispatcher(const EspNowConfig& config) {
7474
return;
7575
}
7676

77-
//#if CONFIG_ESPNOW_ENABLE_POWER_SAVE
78-
// ESP_ERROR_CHECK( esp_now_set_wake_window(CONFIG_ESPNOW_WAKE_WINDOW) );
79-
// ESP_ERROR_CHECK( esp_wifi_connectionless_module_set_wake_interval(CONFIG_ESPNOW_WAKE_INTERVAL) );
80-
//#endif
77+
//#if CONFIG_ESPNOW_ENABLE_POWER_SAVE
78+
// ESP_ERROR_CHECK( esp_now_set_wake_window(CONFIG_ESPNOW_WAKE_WINDOW) );
79+
// ESP_ERROR_CHECK( esp_wifi_connectionless_module_set_wake_interval(CONFIG_ESPNOW_WAKE_INTERVAL) );
80+
//#endif
8181

8282
if (esp_now_set_pmk(config.masterKey) != ESP_OK) {
8383
LOGGER.error("esp_now_set_pmk() failed");
8484
return;
8585
}
8686

87+
espnowVersion = 0;
8788
if (esp_now_get_version(&espnowVersion) == ESP_OK) {
8889
LOGGER.info("ESP-NOW version: {}.0", espnowVersion);
8990
} else {

0 commit comments

Comments
 (0)