Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit e33b699

Browse files
committed
Merge branch 'raul360-usbstick-ok' into Andrea_review
2 parents 0c2c694 + 02624a9 commit e33b699

File tree

6 files changed

+38
-243
lines changed

6 files changed

+38
-243
lines changed

src/arduino-rfm/LoRaMAC.cpp

Lines changed: 21 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
*****************************************************************************************
6464
*/
6565
void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, sLoRa_Session *Session_Data,
66-
sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message_Rx, sSettings *LoRa_Settings, msg_t *upMsg_Type)
66+
sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message_Rx, sSettings *LoRa_Settings)
6767
{
6868
static const unsigned int Receive_Delay_1 = LoRa_Settings->Rx1_Delay;
6969
static const unsigned int Receive_Delay_2 = LoRa_Settings->Rx2_Delay;
@@ -78,8 +78,8 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
7878
unsigned char rx1_dr = LoRa_Settings->Datarate_Tx+10;
7979
#elif defined(EU_868)
8080
unsigned char rx1_dr = LoRa_Settings->Datarate_Tx;
81-
#elif defined(IN_865)
82-
unsigned char rx1_dr = LoRa_Settings->Datarate_Tx;
81+
#elif defined(IN_865)
82+
unsigned char rx1_dr = LoRa_Settings->Datarate_Tx;
8383
#else // AS_923 and AS_923_2
8484
unsigned char rx1_dr = LoRa_Settings->Datarate_Tx;
8585
#endif
@@ -90,12 +90,8 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
9090
pinMode(RFM_SWITCH,OUTPUT);
9191
digitalWrite(RFM_SWITCH,0); //Rf switch inside RAK module change to Tx
9292
#endif
93-
94-
//Lora send data & ack
95-
if(*upMsg_Type == MSG_UP) LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings);
96-
else if(*upMsg_Type == MSG_ACK) LORA_Send_ACK(Data_Tx, Session_Data, LoRa_Settings);
97-
98-
93+
//Lora send data
94+
LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings);
9995
prevTime = millis();
10096

10197
#if (SAMR34)
@@ -110,9 +106,9 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
110106
#elif defined(EU_868)
111107
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 923.3 MHZ
112108
LoRa_Settings->Datarate_Rx = SF12BW125; //set RX2 datarate 12
113-
#elif defined(IN_865)
114-
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 866.550 MHZ
115-
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
109+
#elif defined(IN_865)
110+
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 866.550 MHZ
111+
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
116112
#elif defined(AS_923) || defined(AS_923_2)
117113
LoRa_Settings->Channel_Rx = 0x00; // set Rx2 channel 923.2 (AS_923) or 921.4 (AS_923_2)
118114
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
@@ -123,7 +119,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
123119
#endif
124120
LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); //BUG DETECT SENDED PACKET ALWAYS (IT DOES UPDATE)
125121
}
126-
122+
127123
//LoRaWAN Link Layer Specification v1.0.4 line 375
128124
//Wait rx1 window delay, TO TEST check if class c receives anything
129125
do{
@@ -143,11 +139,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
143139
return;
144140
}
145141

146-
//
147-
#ifdef _CLASS_C_
148-
return;
149-
#endif
150-
151142
// Class C open RX2 immediately after first rx window
152143
if(LoRa_Settings->Mote_Class == CLASS_C){
153144
#ifdef US_915
@@ -156,9 +147,9 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
156147
#elif defined(EU_868)
157148
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 923.3 MHZ
158149
LoRa_Settings->Datarate_Rx = SF12BW125; //set RX2 datarate 12
159-
#elif defined(IN_865)
160-
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 866.550 MHZ
161-
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
150+
#elif defined(IN_865)
151+
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 866.550 MHZ
152+
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
162153
#elif defined(AS_923) || defined(AS_923_2)
163154
LoRa_Settings->Channel_Rx = 0x00; // set Rx2 channel 923.2 (AS_923) or 921.4 (AS_923_2)
164155
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
@@ -184,9 +175,9 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
184175
#elif defined(EU_868)
185176
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 923.3 MHZ
186177
LoRa_Settings->Datarate_Rx = SF12BW125; //set RX2 datarate 12
187-
#elif defined(IN_865)
188-
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 866.550 MHZ
189-
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
178+
#elif defined(IN_865)
179+
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 866.550 MHZ
180+
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
190181
#elif defined(AS_923) || defined(AS_923_2)
191182
LoRa_Settings->Channel_Rx = 0x00; // set Rx2 channel 923.2 (AS_923) or 921.4 (AS_923_2)
192183
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
@@ -204,11 +195,10 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
204195
if (Data_Rx->Counter>0){
205196
return;
206197
}
198+
207199
}
208200
}
209201

210-
211-
212202
/*
213203
*****************************************************************************************
214204
* Description : Function that is used to build a LoRaWAN data message and then tranmit it.
@@ -338,130 +328,6 @@ void LORA_Send_Data(sBuffer *Data_Tx, sLoRa_Session *Session_Data, sSettings *Lo
338328
}
339329
}
340330

341-
// send uplink message including ACK
342-
343-
void LORA_Send_ACK(sBuffer *Data_Tx, sLoRa_Session *Session_Data, sSettings *LoRa_Settings)
344-
{
345-
Serial.println("LoraMac send ack");
346-
//Define variables
347-
unsigned char i;
348-
349-
//Initialise RFM buffer
350-
unsigned char RFM_Data[MAX_UPLINK_PAYLOAD_SIZE+65];
351-
sBuffer RFM_Package = {&RFM_Data[0], 0x00};
352-
353-
//Initialise Message struct for a transmit message
354-
sLoRa_Message Message;
355-
356-
Message.MAC_Header = 0x00;
357-
Message.Frame_Port = 0x00; //set as MAC command
358-
Message.Frame_Control = 0x00;
359-
360-
//Load device address from session data into the message
361-
Message.DevAddr[0] = Session_Data->DevAddr[0];
362-
Message.DevAddr[1] = Session_Data->DevAddr[1];
363-
Message.DevAddr[2] = Session_Data->DevAddr[2];
364-
Message.DevAddr[3] = Session_Data->DevAddr[3];
365-
366-
//Set up direction
367-
Message.Direction = 0x00;
368-
369-
//Load the frame counter from the session data into the message
370-
Message.Frame_Counter = *Session_Data->Frame_Counter;
371-
372-
//Set confirmation
373-
//Unconfirmed
374-
// if(LoRa_Settings->Confirm == 0x00)
375-
// {
376-
// Message.MAC_Header = Message.MAC_Header | 0x40;
377-
// }
378-
// //Confirmed
379-
// else
380-
// {
381-
// Message.MAC_Header = Message.MAC_Header | 0x80;
382-
// }
383-
Message.MAC_Header = Message.MAC_Header | 0x40;
384-
385-
//Build the Radio Package
386-
//Load mac header
387-
RFM_Package.Data[0] = Message.MAC_Header;
388-
389-
//Load device address
390-
RFM_Package.Data[1] = Message.DevAddr[3];
391-
RFM_Package.Data[2] = Message.DevAddr[2];
392-
RFM_Package.Data[3] = Message.DevAddr[1];
393-
RFM_Package.Data[4] = Message.DevAddr[0];
394-
395-
//Load frame control
396-
RFM_Package.Data[5] = (Message.Frame_Control | 0x20);
397-
398-
//Load frame counter
399-
RFM_Package.Data[6] = (*Session_Data->Frame_Counter & 0x00FF);
400-
RFM_Package.Data[7] = ((*Session_Data->Frame_Counter >> 8) & 0x00FF);
401-
402-
//Set data counter to 8
403-
RFM_Package.Counter = 8;
404-
405-
//If there is data load the Frame_Port field
406-
//Encrypt the data and load the data
407-
if(Data_Tx->Counter > 0x00)
408-
{
409-
//Load Frame port field
410-
//RFM_Data[8] = Message.Frame_Port;
411-
RFM_Package.Data[8] = 0;
412-
413-
//Raise package counter
414-
RFM_Package.Counter++;
415-
416-
//Encrypt the data
417-
Encrypt_Payload(Data_Tx, Session_Data->AppSKey, &Message);
418-
419-
//Load Data
420-
for(i = 0; i < Data_Tx->Counter; i++)
421-
{
422-
RFM_Package.Data[RFM_Package.Counter++] = Data_Tx->Data[i];
423-
}
424-
425-
426-
}
427-
428-
//Calculate MIC
429-
Construct_Data_MIC(&RFM_Package, Session_Data, &Message);
430-
431-
//Load MIC in package
432-
for(i = 0; i < 4; i++)
433-
{
434-
RFM_Package.Data[RFM_Package.Counter++] = Message.MIC[i];
435-
}
436-
437-
//Send Package
438-
RFM_Send_Package(&RFM_Package, LoRa_Settings);
439-
440-
//Raise Frame counter
441-
if(*Session_Data->Frame_Counter != 0xFFFF)
442-
{
443-
//Raise frame counter
444-
*Session_Data->Frame_Counter = *Session_Data->Frame_Counter + 1;
445-
}
446-
else
447-
{
448-
*Session_Data->Frame_Counter = 0x0000;
449-
}
450-
451-
//Change channel for next message if hopping is activated
452-
if(LoRa_Settings->Channel_Hopping == 0x01)
453-
{
454-
if(LoRa_Settings->Channel_Tx < 0x07)
455-
{
456-
LoRa_Settings->Channel_Tx++;
457-
}
458-
else
459-
{
460-
LoRa_Settings->Channel_Tx = 0x00;
461-
}
462-
}
463-
}
464-
465331
/*
466332
*****************************************************************************************
467333
* Description : Function that is used to receive a LoRaWAN message and retrieve the data from the RFM
@@ -655,12 +521,15 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA
655521
*/
656522
static void Generate_DevNonce(unsigned char *DevNonce)
657523
{
524+
#ifdef ESP32
525+
// Use the built-in random number generator of ESP32 to obtain a random value
526+
esp_fill_random(DevNonce, 2); // Fill the first 2 bytes of DevNonce with random values
527+
#else
658528
unsigned int RandNumber;
659-
660529
RandNumber = random(0xFFFF);
661-
662530
DevNonce[0] = RandNumber & 0x00FF;
663531
DevNonce[1] = (RandNumber >> 8) & 0x00FF;
532+
#endif
664533
}
665534
/*
666535
*****************************************************************************************

src/arduino-rfm/LoRaMAC.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,16 @@
4343
********************************************************************************************
4444
*/
4545

46-
typedef enum {NO_RFM_COMMAND, NEW_RFM_COMMAND, RFM_COMMAND_DONE, JOIN, NEW_ACK_COMMAND} RFM_command_t;
46+
typedef enum {NO_RFM_COMMAND, NEW_RFM_COMMAND, RFM_COMMAND_DONE, JOIN} RFM_command_t;
4747

4848
/*
4949
*****************************************************************************************
5050
* FUNCTION PROTOTYPES
5151
*****************************************************************************************
5252
*/
5353

54-
void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, sLoRa_Session *Session_Data, sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message_Rx, sSettings *LoRa_Settings, msg_t *upMsg_Type);
54+
void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command, sLoRa_Session *Session_Data, sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message_Rx, sSettings *LoRa_Settings);
5555
void LORA_Send_Data(sBuffer *Data_Tx, sLoRa_Session *Session_Data, sSettings *LoRa_Settings);
56-
void LORA_Send_ACK(sBuffer *Data_Tx, sLoRa_Session *Session_Data, sSettings *LoRa_Settings);
5756
void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message, sSettings *LoRa_Settings);
5857
bool LORA_join_Accept(sBuffer *Data_Rx,sLoRa_Session *Session_Data, sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message, sSettings *LoRa_Settings);
5958
void LoRa_Send_JoinReq(sLoRa_OTAA *OTAA_Data, sSettings *LoRa_Settings);

src/arduino-rfm/RFM95.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -780,23 +780,8 @@ void RFM_Continuous_Receive(sSettings *LoRa_Settings)
780780
RFM_Write(RFM_REG_INVERT_IQ2, 0x19);
781781

782782
//Change Datarate and channel.
783-
// This depends on regional parameters
784-
#if defined(EU_868)
785-
RFM_Change_Datarate(SF12BW125);
786-
RFM_Change_Channel(CHRX2);
787-
#elif defined(IN_865)
788-
RFM_Change_Datarate(SF10BW125);
789-
RFM_Change_Channel(CHRX2);
790-
#else
791-
//Datarate for downlink should be 8 but testing on 10
792-
//LoRa_Settings->Datarate_Rx=10;
793-
//LoRa_Settings->Channel_Rx=0;
794-
//Serial.println("DataRate Rx "+String(LoRa_Settings->Datarate_Rx));
795-
//Serial.println("Cannel Rx "+String(LoRa_Settings->Channel_Rx));
796-
797-
RFM_Change_Datarate(LoRa_Settings->Datarate_Rx);
798-
RFM_Change_Channel(LoRa_Settings->Channel_Rx);
799-
#endif
783+
RFM_Change_Datarate(LoRa_Settings->Datarate_Rx);
784+
RFM_Change_Channel(LoRa_Settings->Channel_Rx);
800785

801786
//Switch to continuous receive
802787
RFM_Switch_Mode(RFM_MODE_RXCONT);

src/arduino-rfm/Struct.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,4 @@ typedef enum {NO_RX, NEW_RX} rx_t;
182182

183183
typedef enum {NO_ACK, NEW_ACK} ack_t;
184184

185-
typedef enum {MSG_UP, MSG_ACK} msg_t;
186-
187185
#endif
188-
189-

0 commit comments

Comments
 (0)