Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 44 additions & 26 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,11 @@ void OnGetConfig()

void OnGetInfo()
{
// read the serial number and generate if 1st start up, was before in ResetBoard()
// moved to this position as the time to generate a serial number in ResetBoard() is always the same
// OnGetInfo() is called from the connector and the time is very likely always different
// Therefore millis() can be used for randomSeed
generateSerial(false);
setLastCommandMillis();
cmdMessenger.sendCmdStart(kInfo);
cmdMessenger.sendCmdArg(F(MOBIFLIGHT_TYPE));
Expand All @@ -448,45 +453,58 @@ bool getStatusConfig()
// ************************************************************
// serial number handling
// ************************************************************
void generateRandomSerial()
{
randomSeed(millis());
sprintf(serial, "SN-%03x-%03x", (unsigned int)random(4095), (unsigned int)random(4095));
MFeeprom.write_block(MEM_OFFSET_SERIAL, serial, MEM_LEN_SERIAL);
}

void generateUniqueSerial()
{
sprintf(serial, "SN-");
for (size_t i = 0; i < UniqueIDsize; i++) {
sprintf(&serial[3 + i * 2], "%02X", UniqueID[i]);
}
}

void generateSerial(bool force)
{
if (force) {
#if defined(ARDUINO_ARCH_AVR)
// A serial number is forced to generated from the user
// It is very likely that the reason is a double serial number as the UniqueID for AVR's must not be Unique
// so generate one acc. the old style and use millis() for seed
randomSeed(millis());
sprintf(serial, "SN-%03x-", (unsigned int)random(4095));
sprintf(&serial[7], "%03x", (unsigned int)random(4095));
MFeeprom.write_block(MEM_OFFSET_SERIAL, serial, MEM_LEN_SERIAL);
#endif
// A serial number is forced to generate
// generate a serial number acc. the old style also for the Pico
generateRandomSerial();
return;
}

// A serial number according old style is already generated and saved to the eeprom
if (MFeeprom.read_byte(MEM_OFFSET_SERIAL) == 'S' && MFeeprom.read_byte(MEM_OFFSET_SERIAL + 1) == 'N') {
// A serial number according old style is already generated and saved to the eeprom
// So keep it to avoid a connector message with orphaned board
MFeeprom.read_block(MEM_OFFSET_SERIAL, serial, MEM_LEN_SERIAL);
return;
}

// Read the uniqueID and use it as serial numnber
sprintf(serial, "SN-");
for (size_t i = 0; i < UniqueIDsize; i++) {
if (UniqueID[i] < 0x10) {
sprintf(&serial[3 + i * 2], "0%X", UniqueID[i]);
} else {
sprintf(&serial[3 + i * 2], "%X", UniqueID[i]);
}
// A uniqueID is already generated and saved to the eeprom
if (MFeeprom.read_byte(MEM_OFFSET_SERIAL) == 'I' && MFeeprom.read_byte(MEM_OFFSET_SERIAL + 1) == 'D') {
generateUniqueSerial();
return;
}

if (MFeeprom.read_byte(MEM_OFFSET_SERIAL) != 'I' && MFeeprom.read_byte(MEM_OFFSET_SERIAL + 1) != 'D') {
// Coming here it's the first start up of the board and no serial number or UniqueID is available
// mark this in the eeprom that a UniqueID is used on first start up
MFeeprom.write_block(MEM_OFFSET_SERIAL, "ID", 2);
// Set first byte of config to 0x00 to ensure with empty config on 1st start up
MFeeprom.write_byte(MEM_OFFSET_CONFIG, 0x00);
}
// Coming here no UniqueID and no serial number is available, so it's the first start up of a board
#if defined(ARDUINO_ARCH_AVR)
// Generate a serial number for AVR's
// To have not always the same starting point for the random generator, millis() are
// used as starting point. It is very unlikely that the time between flashing the firmware
// and getting the command to send the info's to the connector is always the same.
generateRandomSerial();
#elif defined(ARDUINO_ARCH_RP2040)
// Read the uniqueID for Pico's and use it as serial number
generateUniqueSerial();
// mark this in the eeprom that a UniqueID is used on first start up for Pico's
MFeeprom.write_block(MEM_OFFSET_SERIAL, "ID", 2);
#endif
// Set first byte of config to 0x00 to ensure empty config on 1st start up
// Otherwise the complete length of the config will be send with 0xFF (empty EEPROM)
MFeeprom.write_byte(MEM_OFFSET_CONFIG, 0x00);
}

void OnGenNewSerial()
Expand Down
1 change: 0 additions & 1 deletion src/mobiflight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ void updatePowerSaving()
// ************************************************************
void ResetBoard()
{
generateSerial(false);
setLastCommandMillis();
restoreName();
loadConfig();
Expand Down