Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raul360 usbstick ok #181

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
125 changes: 0 additions & 125 deletions examples/class_c_otaa_ack/class_c_otaa_ack.ino

This file was deleted.

177 changes: 23 additions & 154 deletions src/arduino-rfm/LoRaMAC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@
*****************************************************************************************
*/
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)
sLoRa_OTAA *OTAA_Data, sLoRa_Message *Message_Rx, sSettings *LoRa_Settings)
{
static const unsigned int Receive_Delay_1 = 1000;
static const unsigned int Receive_Delay_2 = 2000; // Receive_Delay_2 >= Receive_Delay_1 + RX1_Window
static const unsigned int RX1_Window = 1000;
static const unsigned int Receive_Delay_2 = 2000; // Receive_Delay_2 >= Receive_Delay_1 + RX1_Window
static const unsigned int RX1_Window = 1000;
static const unsigned int RX2_Window = 1000;

unsigned long prevTime = 0;
Expand All @@ -78,8 +78,8 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
unsigned char rx1_dr = LoRa_Settings->Datarate_Tx+10;
#elif defined(EU_868)
unsigned char rx1_dr = LoRa_Settings->Datarate_Tx;
#elif defined(IN_865)
unsigned char rx1_dr = LoRa_Settings->Datarate_Tx;
#elif defined(IN_865)
unsigned char rx1_dr = LoRa_Settings->Datarate_Tx;
#else // AS_923 and AS_923_2
unsigned char rx1_dr = LoRa_Settings->Datarate_Tx;
#endif
Expand All @@ -90,12 +90,8 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
pinMode(RFM_SWITCH,OUTPUT);
digitalWrite(RFM_SWITCH,0); //Rf switch inside RAK module change to Tx
#endif

//Lora send data & ack
if(*upMsg_Type == MSG_UP) LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings);
else if(*upMsg_Type == MSG_ACK) LORA_Send_ACK(Data_Tx, Session_Data, LoRa_Settings);


//Lora send data
LORA_Send_Data(Data_Tx, Session_Data, LoRa_Settings);
prevTime = millis();

#if (SAMR34)
Expand All @@ -110,9 +106,9 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
#elif defined(EU_868)
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 923.3 MHZ
LoRa_Settings->Datarate_Rx = SF12BW125; //set RX2 datarate 12
#elif defined(IN_865)
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 866.550 MHZ
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
#elif defined(IN_865)
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 866.550 MHZ
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
#elif defined(AS_923) || defined(AS_923_2)
LoRa_Settings->Channel_Rx = 0x00; // set Rx2 channel 923.2 (AS_923) or 921.4 (AS_923_2)
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
Expand All @@ -123,7 +119,7 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
#endif
LORA_Receive_Data(Data_Rx, Session_Data, OTAA_Data, Message_Rx, LoRa_Settings); //BUG DETECT SENDED PACKET ALWAYS (IT DOES UPDATE)
}

//LoRaWAN Link Layer Specification v1.0.4 line 375
//Wait rx1 window delay, TO TEST check if class c receives anything
do{
Expand All @@ -143,11 +139,6 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
return;
}

//
#ifdef _CLASS_C_
return;
#endif

// Class C open RX2 immediately after first rx window
if(LoRa_Settings->Mote_Class == CLASS_C){
#ifdef US_915
Expand All @@ -156,9 +147,9 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
#elif defined(EU_868)
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 923.3 MHZ
LoRa_Settings->Datarate_Rx = SF12BW125; //set RX2 datarate 12
#elif defined(IN_865)
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 866.550 MHZ
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
#elif defined(IN_865)
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 866.550 MHZ
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
#elif defined(AS_923) || defined(AS_923_2)
LoRa_Settings->Channel_Rx = 0x00; // set Rx2 channel 923.2 (AS_923) or 921.4 (AS_923_2)
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
Expand All @@ -184,9 +175,9 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
#elif defined(EU_868)
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 923.3 MHZ
LoRa_Settings->Datarate_Rx = SF12BW125; //set RX2 datarate 12
#elif defined(IN_865)
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 866.550 MHZ
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
#elif defined(IN_865)
LoRa_Settings->Channel_Rx = CHRX2; // set Rx2 channel 866.550 MHZ
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
#elif defined(AS_923) || defined(AS_923_2)
LoRa_Settings->Channel_Rx = 0x00; // set Rx2 channel 923.2 (AS_923) or 921.4 (AS_923_2)
LoRa_Settings->Datarate_Rx = SF10BW125; //set RX2 datarate 10
Expand All @@ -204,11 +195,10 @@ void LORA_Cycle(sBuffer *Data_Tx, sBuffer *Data_Rx, RFM_command_t *RFM_Command,
if (Data_Rx->Counter>0){
return;
}

}
}



/*
*****************************************************************************************
* Description : Function that is used to build a LoRaWAN data message and then tranmit it.
Expand Down Expand Up @@ -338,130 +328,6 @@ void LORA_Send_Data(sBuffer *Data_Tx, sLoRa_Session *Session_Data, sSettings *Lo
}
}

// send uplink message including ACK

void LORA_Send_ACK(sBuffer *Data_Tx, sLoRa_Session *Session_Data, sSettings *LoRa_Settings)
{
Serial.println("LoraMac send ack");
//Define variables
unsigned char i;

//Initialise RFM buffer
unsigned char RFM_Data[MAX_UPLINK_PAYLOAD_SIZE+65];
sBuffer RFM_Package = {&RFM_Data[0], 0x00};

//Initialise Message struct for a transmit message
sLoRa_Message Message;

Message.MAC_Header = 0x00;
Message.Frame_Port = 0x00; //set as MAC command
Message.Frame_Control = 0x00;

//Load device address from session data into the message
Message.DevAddr[0] = Session_Data->DevAddr[0];
Message.DevAddr[1] = Session_Data->DevAddr[1];
Message.DevAddr[2] = Session_Data->DevAddr[2];
Message.DevAddr[3] = Session_Data->DevAddr[3];

//Set up direction
Message.Direction = 0x00;

//Load the frame counter from the session data into the message
Message.Frame_Counter = *Session_Data->Frame_Counter;

//Set confirmation
//Unconfirmed
// if(LoRa_Settings->Confirm == 0x00)
// {
// Message.MAC_Header = Message.MAC_Header | 0x40;
// }
// //Confirmed
// else
// {
// Message.MAC_Header = Message.MAC_Header | 0x80;
// }
Message.MAC_Header = Message.MAC_Header | 0x40;

//Build the Radio Package
//Load mac header
RFM_Package.Data[0] = Message.MAC_Header;

//Load device address
RFM_Package.Data[1] = Message.DevAddr[3];
RFM_Package.Data[2] = Message.DevAddr[2];
RFM_Package.Data[3] = Message.DevAddr[1];
RFM_Package.Data[4] = Message.DevAddr[0];

//Load frame control
RFM_Package.Data[5] = (Message.Frame_Control | 0x20);

//Load frame counter
RFM_Package.Data[6] = (*Session_Data->Frame_Counter & 0x00FF);
RFM_Package.Data[7] = ((*Session_Data->Frame_Counter >> 8) & 0x00FF);

//Set data counter to 8
RFM_Package.Counter = 8;

//If there is data load the Frame_Port field
//Encrypt the data and load the data
if(Data_Tx->Counter > 0x00)
{
//Load Frame port field
//RFM_Data[8] = Message.Frame_Port;
RFM_Package.Data[8] = 0;

//Raise package counter
RFM_Package.Counter++;

//Encrypt the data
Encrypt_Payload(Data_Tx, Session_Data->AppSKey, &Message);

//Load Data
for(i = 0; i < Data_Tx->Counter; i++)
{
RFM_Package.Data[RFM_Package.Counter++] = Data_Tx->Data[i];
}


}

//Calculate MIC
Construct_Data_MIC(&RFM_Package, Session_Data, &Message);

//Load MIC in package
for(i = 0; i < 4; i++)
{
RFM_Package.Data[RFM_Package.Counter++] = Message.MIC[i];
}

//Send Package
RFM_Send_Package(&RFM_Package, LoRa_Settings);

//Raise Frame counter
if(*Session_Data->Frame_Counter != 0xFFFF)
{
//Raise frame counter
*Session_Data->Frame_Counter = *Session_Data->Frame_Counter + 1;
}
else
{
*Session_Data->Frame_Counter = 0x0000;
}

//Change channel for next message if hopping is activated
if(LoRa_Settings->Channel_Hopping == 0x01)
{
if(LoRa_Settings->Channel_Tx < 0x07)
{
LoRa_Settings->Channel_Tx++;
}
else
{
LoRa_Settings->Channel_Tx = 0x00;
}
}
}

/*
*****************************************************************************************
* Description : Function that is used to receive a LoRaWAN message and retrieve the data from the RFM
Expand Down Expand Up @@ -655,12 +521,15 @@ void LORA_Receive_Data(sBuffer *Data_Rx, sLoRa_Session *Session_Data, sLoRa_OTAA
*/
static void Generate_DevNonce(unsigned char *DevNonce)
{
#ifdef ESP32
// Use the built-in random number generator of ESP32 to obtain a random value
esp_fill_random(DevNonce, 2); // Fill the first 2 bytes of DevNonce with random values
#else
unsigned int RandNumber;

RandNumber = random(0xFFFF);

DevNonce[0] = RandNumber & 0x00FF;
DevNonce[1] = (RandNumber >> 8) & 0x00FF;
#endif
}
/*
*****************************************************************************************
Expand Down
Loading