Skip to content

Commit 7389a69

Browse files
committed
Move training into separate file
1 parent ef7da4c commit 7389a69

File tree

2 files changed

+146
-146
lines changed

2 files changed

+146
-146
lines changed

Firmware/LoRaSerial_Firmware/Radio.ino

Lines changed: 0 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -681,149 +681,3 @@ bool receiveInProcess()
681681
//if ((radioStatus & 0b1) == 0) return (false); //If bit 0 is cleared, there is no receive in progress
682682
//return (true); //If bit 0 is set, forget the other bits, there is a receive in progress
683683
}
684-
685-
void beginTraining()
686-
{
687-
originalSettings = settings; //Make copy of current settings
688-
689-
moveToTrainingFreq();
690-
}
691-
692-
void beginDefaultTraining()
693-
{
694-
Settings defaultSettings;
695-
originalSettings = defaultSettings; //Upon completion we will return to default settings
696-
697-
moveToTrainingFreq();
698-
}
699-
700-
//Upon successful exchange of keys, go back to original settings
701-
void endTraining(bool newTrainingAvailable)
702-
{
703-
settings = originalSettings; //Return to original radio settings
704-
705-
//Apply new netID and AES if available
706-
if (newTrainingAvailable)
707-
{
708-
if (lastPacketSize == sizeof(settings.encryptionKey) + 1) //Error check, should be AES key + NetID
709-
{
710-
//Move training data into settings
711-
for (int x = 0 ; x < sizeof(settings.encryptionKey); x++)
712-
settings.encryptionKey[x] = lastPacket[x];
713-
714-
settings.netID = lastPacket[lastPacketSize - 1]; //Last spot in array is netID
715-
716-
if (settings.debug == true)
717-
{
718-
systemPrint("New Key: ");
719-
for (uint8_t i = 0 ; i < 16 ; i++)
720-
{
721-
if (settings.encryptionKey[i] < 0x10) systemPrint("0");
722-
systemPrint(settings.encryptionKey[i], HEX);
723-
systemPrint(" ");
724-
}
725-
systemPrintln();
726-
727-
systemPrint("New ID: ");
728-
systemPrintln(settings.netID);
729-
}
730-
}
731-
else
732-
{
733-
//If the packet was marked as training but was not valid training data, then give up. Return to normal radio mode with pre-existing settings.
734-
}
735-
}
736-
else
737-
{
738-
//We transmitted the training data, move the local training data into settings
739-
for (int x = 0 ; x < sizeof(settings.encryptionKey); x++)
740-
settings.encryptionKey[x] = trainEncryptionKey[x];
741-
742-
settings.netID = trainNetID; //Last spot in array is netID
743-
}
744-
745-
recordSystemSettings();
746-
747-
generateHopTable(); //Generate frequency table based on current settings
748-
749-
configureRadio(); //Setup radio with settings
750-
751-
returnToReceiving();
752-
753-
if (settings.pointToPoint == true)
754-
changeState(RADIO_NO_LINK_RECEIVING_STANDBY);
755-
else
756-
changeState(RADIO_BROADCASTING_RECEIVING_STANDBY);
757-
758-
systemPrintln("LINK TRAINED");
759-
}
760-
761-
//Change to known training frequency based on available freq and current major firmware version
762-
//This will allow different minor versions to continue to train to each other
763-
//Send special packet with train = 1, then wait for response
764-
void moveToTrainingFreq()
765-
{
766-
//During training use default radio settings. This ensures both radios are at known good settings.
767-
Settings defaultSettings;
768-
settings = defaultSettings; //Move to default settings
769-
770-
//Disable hopping
771-
settings.frequencyHop = false;
772-
773-
//Disable NetID checking
774-
settings.pointToPoint = false;
775-
776-
generateHopTable(); //Generate frequency table based on current settings
777-
778-
configureRadio(); //Setup radio with settings
779-
780-
//Move to frequency that is not part of the hop table
781-
//In normal operation we move 1/2 a channel away from min. In training, we move a full channel away + major firmware version.
782-
float channelSpacing = (settings.frequencyMax - settings.frequencyMin) / (float)(settings.numberOfChannels + 2);
783-
float trainFrequency = settings.frequencyMin + (channelSpacing * (FIRMWARE_VERSION_MAJOR % settings.numberOfChannels));
784-
785-
channels[0] = trainFrequency; //Inject this frequency into the channel table
786-
787-
//Transmit general ping packet to see if anyone else is sitting on the training channel
788-
sendTrainingPingPacket();
789-
790-
//Recalculate packetAirTime because we need to wait not for a 2-byte response, but a 19 byte response
791-
packetAirTime = calcAirTime(sizeof(trainEncryptionKey) + sizeof(trainNetID) + 2);
792-
793-
changeState(RADIO_TRAINING_TRANSMITTING);
794-
}
795-
796-
//Generate new netID/AES key to share
797-
//We assume the user needs to maintain their settings (airSpeed, numberOfChannels, freq min/max, bandwidth/spread/hop)
798-
//but need to be on a different netID/AES key.
799-
void generateTrainingSettings()
800-
{
801-
LRS_DEBUG_PRINTLN("Generate New Training Settings");
802-
803-
//Seed random number based on RF noise. We use Arduino random() because platform specific generation does not matter
804-
randomSeed(radio.randomByte());
805-
806-
//Generate new NetID
807-
trainNetID = random(0, 256); //Inclusive, exclusive
808-
809-
//Generate new AES Key. User may not be using AES but we still need both radios to have the same key in case they do enable AES.
810-
for (int x = 0 ; x < 16 ; x++)
811-
trainEncryptionKey[x] = random(0, 256); //Inclusive, exclusive
812-
813-
//We do not generate new AES Initial Values here. Those are generated during generateHopTable() based on the unit's settings.
814-
815-
if (settings.debug == true)
816-
{
817-
systemPrint(F("trainNetID: "));
818-
systemPrintln(trainNetID);
819-
820-
systemPrint(F("trainEncryptionKey:"));
821-
for (uint8_t i = 0 ; i < 16 ; i++)
822-
{
823-
systemPrint(" 0x");
824-
if (trainEncryptionKey[i] < 0x10) systemPrint("0");
825-
systemPrint(trainEncryptionKey[i], HEX);
826-
}
827-
systemPrintln();
828-
}
829-
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
2+
void beginTraining()
3+
{
4+
originalSettings = settings; //Make copy of current settings
5+
6+
moveToTrainingFreq();
7+
}
8+
9+
void beginDefaultTraining()
10+
{
11+
Settings defaultSettings;
12+
originalSettings = defaultSettings; //Upon completion we will return to default settings
13+
14+
moveToTrainingFreq();
15+
}
16+
17+
//Upon successful exchange of keys, go back to original settings
18+
void endTraining(bool newTrainingAvailable)
19+
{
20+
settings = originalSettings; //Return to original radio settings
21+
22+
//Apply new netID and AES if available
23+
if (newTrainingAvailable)
24+
{
25+
if (lastPacketSize == sizeof(settings.encryptionKey) + 1) //Error check, should be AES key + NetID
26+
{
27+
//Move training data into settings
28+
for (int x = 0 ; x < sizeof(settings.encryptionKey); x++)
29+
settings.encryptionKey[x] = lastPacket[x];
30+
31+
settings.netID = lastPacket[lastPacketSize - 1]; //Last spot in array is netID
32+
33+
if (settings.debug == true)
34+
{
35+
systemPrint("New Key: ");
36+
for (uint8_t i = 0 ; i < 16 ; i++)
37+
{
38+
if (settings.encryptionKey[i] < 0x10) systemPrint("0");
39+
systemPrint(settings.encryptionKey[i], HEX);
40+
systemPrint(" ");
41+
}
42+
systemPrintln();
43+
44+
systemPrint("New ID: ");
45+
systemPrintln(settings.netID);
46+
}
47+
}
48+
else
49+
{
50+
//If the packet was marked as training but was not valid training data, then give up. Return to normal radio mode with pre-existing settings.
51+
}
52+
}
53+
else
54+
{
55+
//We transmitted the training data, move the local training data into settings
56+
for (int x = 0 ; x < sizeof(settings.encryptionKey); x++)
57+
settings.encryptionKey[x] = trainEncryptionKey[x];
58+
59+
settings.netID = trainNetID; //Last spot in array is netID
60+
}
61+
62+
recordSystemSettings();
63+
64+
generateHopTable(); //Generate frequency table based on current settings
65+
66+
configureRadio(); //Setup radio with settings
67+
68+
returnToReceiving();
69+
70+
if (settings.pointToPoint == true)
71+
changeState(RADIO_NO_LINK_RECEIVING_STANDBY);
72+
else
73+
changeState(RADIO_BROADCASTING_RECEIVING_STANDBY);
74+
75+
systemPrintln("LINK TRAINED");
76+
}
77+
78+
//Change to known training frequency based on available freq and current major firmware version
79+
//This will allow different minor versions to continue to train to each other
80+
//Send special packet with train = 1, then wait for response
81+
void moveToTrainingFreq()
82+
{
83+
//During training use default radio settings. This ensures both radios are at known good settings.
84+
Settings defaultSettings;
85+
settings = defaultSettings; //Move to default settings
86+
87+
//Disable hopping
88+
settings.frequencyHop = false;
89+
90+
//Disable NetID checking
91+
settings.pointToPoint = false;
92+
93+
generateHopTable(); //Generate frequency table based on current settings
94+
95+
configureRadio(); //Setup radio with settings
96+
97+
//Move to frequency that is not part of the hop table
98+
//In normal operation we move 1/2 a channel away from min. In training, we move a full channel away + major firmware version.
99+
float channelSpacing = (settings.frequencyMax - settings.frequencyMin) / (float)(settings.numberOfChannels + 2);
100+
float trainFrequency = settings.frequencyMin + (channelSpacing * (FIRMWARE_VERSION_MAJOR % settings.numberOfChannels));
101+
102+
channels[0] = trainFrequency; //Inject this frequency into the channel table
103+
104+
//Transmit general ping packet to see if anyone else is sitting on the training channel
105+
sendTrainingPingPacket();
106+
107+
//Recalculate packetAirTime because we need to wait not for a 2-byte response, but a 19 byte response
108+
packetAirTime = calcAirTime(sizeof(trainEncryptionKey) + sizeof(trainNetID) + 2);
109+
110+
changeState(RADIO_TRAINING_TRANSMITTING);
111+
}
112+
113+
//Generate new netID/AES key to share
114+
//We assume the user needs to maintain their settings (airSpeed, numberOfChannels, freq min/max, bandwidth/spread/hop)
115+
//but need to be on a different netID/AES key.
116+
void generateTrainingSettings()
117+
{
118+
LRS_DEBUG_PRINTLN("Generate New Training Settings");
119+
120+
//Seed random number based on RF noise. We use Arduino random() because platform specific generation does not matter
121+
randomSeed(radio.randomByte());
122+
123+
//Generate new NetID
124+
trainNetID = random(0, 256); //Inclusive, exclusive
125+
126+
//Generate new AES Key. User may not be using AES but we still need both radios to have the same key in case they do enable AES.
127+
for (int x = 0 ; x < 16 ; x++)
128+
trainEncryptionKey[x] = random(0, 256); //Inclusive, exclusive
129+
130+
//We do not generate new AES Initial Values here. Those are generated during generateHopTable() based on the unit's settings.
131+
132+
if (settings.debug == true)
133+
{
134+
systemPrint(F("trainNetID: "));
135+
systemPrintln(trainNetID);
136+
137+
systemPrint(F("trainEncryptionKey:"));
138+
for (uint8_t i = 0 ; i < 16 ; i++)
139+
{
140+
systemPrint(" 0x");
141+
if (trainEncryptionKey[i] < 0x10) systemPrint("0");
142+
systemPrint(trainEncryptionKey[i], HEX);
143+
}
144+
systemPrintln();
145+
}
146+
}

0 commit comments

Comments
 (0)