Skip to content

Commit 7398b54

Browse files
committed
Fix odd binding behaviour
1 parent be1601d commit 7398b54

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

include/common.h

+2
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ typedef enum
1111

1212
extern connectionState_e connectionState;
1313
extern unsigned long bindingStart;
14+
15+
#define UID_SIZE 6

src/Tx_main.cpp

+21-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
/////////// GLOBALS ///////////
2626

27-
uint8_t bindingAddress[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
27+
uint8_t bindingAddress[UID_SIZE] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2828

2929
const uint8_t version[] = {LATEST_VERSION};
3030

@@ -144,19 +144,30 @@ void ProcessMSPPacketFromTX(mspPacket_t *packet)
144144
{
145145
if (packet->function == MSP_ELRS_BIND)
146146
{
147-
config.SetGroupAddress(packet->payload);
147+
// If the backpack was flashed with passphrase then we override the packet with the flashed UID,
148+
// if not then store the given UID in the config, send it and reboot to use it.
149+
if (firmwareOptions.hasUID)
150+
{
151+
if (memcmp(packet->payload, firmwareOptions.uid, UID_SIZE) != 0)
152+
{
153+
ERRLN("Mismatched bind-phrase UIDs, flash the TX backpack with the same bind-phrase as the TX module.");
154+
return;
155+
}
156+
}
157+
else if (memcmp(packet->payload, config.GetGroupAddress(), UID_SIZE) != 0)
158+
{
159+
config.SetGroupAddress(packet->payload);
160+
config.Commit();
161+
rebootTime = 100 + millis(); // restart to set SetSoftMACAddress
162+
}
148163
DBG("MSP_ELRS_BIND = ");
149-
for (int i = 0; i < 6; i++)
164+
for (int i = 0; i < UID_SIZE; i++)
150165
{
151166
DBG("%x", packet->payload[i]); // Debug prints
152167
DBG(",");
153168
}
154169
DBG(""); // Extra line for serial output readability
155-
config.Commit();
156-
// delay(500); // delay may not be required
157170
sendMSPViaEspnow(packet);
158-
// delay(500); // delay may not be required
159-
rebootTime = millis(); // restart to set SetSoftMACAddress
160171
}
161172

162173
switch (packet->function)
@@ -240,10 +251,10 @@ void SetSoftMACAddress()
240251
{
241252
if (!firmwareOptions.hasUID)
242253
{
243-
memcpy(firmwareOptions.uid, config.GetGroupAddress(), 6);
254+
memcpy(firmwareOptions.uid, config.GetGroupAddress(), UID_SIZE);
244255
}
245256
DBG("EEPROM MAC = ");
246-
for (int i = 0; i < 6; i++)
257+
for (int i = 0; i < UID_SIZE; i++)
247258
{
248259
DBG("%x", firmwareOptions.uid[i]); // Debug prints
249260
DBG(",");
@@ -328,7 +339,7 @@ void setup()
328339
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
329340
esp_now_add_peer(firmwareOptions.uid, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
330341
#elif defined(PLATFORM_ESP32)
331-
memcpy(peerInfo.peer_addr, firmwareOptions.uid, 6);
342+
memcpy(peerInfo.peer_addr, firmwareOptions.uid, UID_SIZE);
332343
peerInfo.channel = 0;
333344
peerInfo.encrypt = false;
334345
if (esp_now_add_peer(&peerInfo) != ESP_OK)

0 commit comments

Comments
 (0)