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

Adding MIC Trailer (Issue #123) #126

Open
wants to merge 11 commits into
base: develop
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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ set(source_files
model/forwarder.cc
model/lorawan-mac-header.cc
model/lora-frame-header.cc
model/lorawan-mic-trailer.cc
model/mac-command.cc
model/lora-device-address.cc
model/lora-device-address-generator.cc
Expand Down Expand Up @@ -71,6 +72,7 @@ set(header_files
model/forwarder.h
model/lorawan-mac-header.h
model/lora-frame-header.h
model/lorawan-mic-trailer.h
model/mac-command.h
model/lora-device-address.h
model/lora-device-address-generator.h
Expand Down
12 changes: 6 additions & 6 deletions helper/lora-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ NS_LOG_COMPONENT_DEFINE ("LoraHelper");
}

NetDeviceContainer
LoraHelper::Install ( const LoraPhyHelper &phyHelper,
const LorawanMacHelper &macHelper,
NodeContainer c) const
LoraHelper::Install ( LoraPhyHelper &phyHelper,
LorawanMacHelper &macHelper,
NodeContainer c)
{
NS_LOG_FUNCTION_NOARGS ();

Expand Down Expand Up @@ -147,9 +147,9 @@ NS_LOG_COMPONENT_DEFINE ("LoraHelper");
}

NetDeviceContainer
LoraHelper::Install ( const LoraPhyHelper &phy,
const LorawanMacHelper &mac,
Ptr<Node> node) const
LoraHelper::Install ( LoraPhyHelper &phy,
LorawanMacHelper &mac,
Ptr<Node> node)
{
return Install (phy, mac, NodeContainer (node));
}
Expand Down
12 changes: 6 additions & 6 deletions helper/lora-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class LoraHelper
* \returns a device container which contains all the devices created by this
* method.
*/
virtual NetDeviceContainer Install (const LoraPhyHelper &phyHelper,
const LorawanMacHelper &macHelper,
NodeContainer c) const;
virtual NetDeviceContainer Install (LoraPhyHelper &phyHelper,
LorawanMacHelper &macHelper,
NodeContainer c);

/**
* Install LoraNetDevice on a single node
Expand All @@ -69,9 +69,9 @@ class LoraHelper
* \returns a device container which contains all the devices created by this
* method.
*/
virtual NetDeviceContainer Install (const LoraPhyHelper &phyHelper,
const LorawanMacHelper &macHelper,
Ptr<Node> node) const;
virtual NetDeviceContainer Install (LoraPhyHelper &phyHelper,
LorawanMacHelper &macHelper,
Ptr<Node> node);

/**
* Enable tracking of packets via trace sources.
Expand Down
62 changes: 61 additions & 1 deletion helper/lorawan-mac-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ NS_LOG_COMPONENT_DEFINE ("LorawanMacHelper");

LorawanMacHelper::LorawanMacHelper () : m_region (LorawanMacHelper::EU)
{
for (unsigned int i = 0;i < 16;i++)
{
m_CurNwkKey[i] = 0;
}

for (unsigned int i = 0;i < 8;i++)
{
m_CurJoinEUI[i] = 0;
}
}

void
Expand Down Expand Up @@ -71,7 +80,7 @@ LorawanMacHelper::SetRegion (enum LorawanMacHelper::Regions region)
}

Ptr<LorawanMac>
LorawanMacHelper::Create (Ptr<Node> node, Ptr<NetDevice> device) const
LorawanMacHelper::Create (Ptr<Node> node, Ptr<NetDevice> device)
{
Ptr<LorawanMac> mac = m_mac.Create<LorawanMac> ();
mac->SetDevice (device);
Expand Down Expand Up @@ -106,6 +115,11 @@ LorawanMacHelper::Create (Ptr<Node> node, Ptr<NetDevice> device) const
break;
}
}

edMac->SetNwkKey(m_CurNwkKey);
GenerateNwkKey();
edMac->SetJoinEUI(m_CurJoinEUI);
GenerateJoinEUI();
}
else
{
Expand Down Expand Up @@ -668,5 +682,51 @@ LorawanMacHelper::SetSpreadingFactorsGivenDistribution (NodeContainer endDevices

} // end function

void
LorawanMacHelper::GenerateNwkKey(void)
{
/* just increment from the last one */
int i;

for (i = 15;i >= 0;i--)
{
if (m_CurNwkKey[i] == 255)
{
/* carry over to next byte */
m_CurNwkKey[i] = 0;
}
else
{
m_CurNwkKey[i]++;
break;
}
}

return;
}

void
LorawanMacHelper::GenerateJoinEUI(void)
{
/* just increment from the last one */
int i;

for (i = 7;i >= 0;i--)
{
if (m_CurJoinEUI[i] == 255)
{
/* carry over to next byte */
m_CurJoinEUI[i] = 0;
}
else
{
m_CurJoinEUI[i]++;
break;
}
}

return;
}

} // namespace lorawan
} // namespace ns3
12 changes: 9 additions & 3 deletions helper/lorawan-mac-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class LorawanMacHelper
* \param device the device within which this MAC will be created.
* \returns a newly-created LorawanMac object.
*/
Ptr<LorawanMac> Create (Ptr<Node> node, Ptr<NetDevice> device) const;
Ptr<LorawanMac> Create (Ptr<Node> node, Ptr<NetDevice> device);

/**
* Set up the end device's data rates
Expand All @@ -116,7 +116,7 @@ class LorawanMacHelper
static std::vector<int> SetSpreadingFactorsGivenDistribution (NodeContainer endDevices,
NodeContainer gateways,
std::vector<double> distribution);

private:
/**
* Perform region-specific configurations for the 868 MHz EU band.
Expand Down Expand Up @@ -165,11 +165,17 @@ class LorawanMacHelper
* ClassAEndDeviceLorawanMac classes.
*/
void ApplyCommonAlohaConfigurations (Ptr<LorawanMac> lorawanMac) const;

ObjectFactory m_mac;
Ptr<LoraDeviceAddressGenerator> m_addrGen; //!< Pointer to the address generator to use
enum DeviceType m_deviceType; //!< The kind of device to install
enum Regions m_region; //!< The region in which the device will operate

void GenerateNwkKey(void);
void GenerateJoinEUI(void);

uint8_t m_CurNwkKey[16];
uint8_t m_CurJoinEUI[8];
};

} // namespace lorawan
Expand Down
34 changes: 31 additions & 3 deletions model/class-a-end-device-lorawan-mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ ClassAEndDeviceLorawanMac::SendToPhy (Ptr<Packet> packetToSend)
m_dataRate = m_dataRate - 1;
}

Ptr<Packet> packetCopy = packetToSend->Copy();
LorawanMacHeader mHdr;
packetCopy->RemoveHeader(mHdr);

// if (mHdr.GetMType() == JOIN_REQUEST)
// {
// m_devNonce++;
// }

// Craft LoraTxParameters object
LoraTxParameters params;
params.sf = GetSfFromDataRate (m_dataRate);
Expand All @@ -102,7 +111,7 @@ ClassAEndDeviceLorawanMac::SendToPhy (Ptr<Packet> packetToSend)

// Wake up PHY layer and directly send the packet

Ptr<LogicalLoraChannel> txChannel = GetChannelForTx ();
Ptr<LogicalLoraChannel> txChannel = m_txCh;

NS_LOG_DEBUG ("PacketToSend: " << packetToSend);
m_phy->Send (packetToSend, params, txChannel->GetFrequency (), m_txPower);
Expand Down Expand Up @@ -150,7 +159,7 @@ ClassAEndDeviceLorawanMac::Receive (Ptr<Packet const> packet)
// Remove the Mac Header to get some information
LorawanMacHeader mHdr;
packetCopy->RemoveHeader (mHdr);

NS_LOG_DEBUG ("Mac Header: " << mHdr);

// Only keep analyzing the packet if it's downlink
Expand All @@ -176,7 +185,26 @@ ClassAEndDeviceLorawanMac::Receive (Ptr<Packet const> packet)
// THIS WILL BE GetReceiveWindow()
Simulator::Cancel (m_secondReceiveWindow);


// if (mHdr.GetMType() == JOIN_ACCEPT)
// {
// uint8_t temp[3];
// uint32_t newJoinNonce = 0;
// packetCopy->CopyData(temp, 3); /* get the new joinNonce value */
//
// for (i = 0;i < 3;i++)
// {
// newJoinNonce <<= 8;
// newJoinNonce |= temp[i];
// }
//
// if (newJoinNonce > m_joinNonce)
// {
// /* update is value is greater */
// m_joinNonce = newJoinNonce;
// }
//
// }

// Parse the MAC commands
ParseCommands (fHdr);

Expand Down
11 changes: 6 additions & 5 deletions model/class-a-end-device-lorawan-mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,18 @@
#include "ns3/lorawan-mac.h" // Packet
#include "ns3/end-device-lorawan-mac.h" // EndDeviceLorawanMac
#include "ns3/lora-frame-header.h" // RxParamSetupReq
// #include "ns3/random-variable-stream.h"
//#include "ns3/random-variable-stream.h"
#include "ns3/lora-device-address.h"
// #include "ns3/traced-value.h"
//#include "ns3/traced-value.h"

namespace ns3 {
namespace lorawan {

/**
* Class representing the MAC layer of a Class A LoRaWAN device.
*/
class ClassAEndDeviceLorawanMac : public EndDeviceLorawanMac

class ClassAEndDeviceLorawanMac : public EndDeviceLorawanMac
{
public:
static TypeId GetTypeId (void);
Expand Down Expand Up @@ -207,7 +208,7 @@ class ClassAEndDeviceLorawanMac : public EndDeviceLorawanMac
* The Data Rate to listen for during the second downlink transmission.
*/
uint8_t m_secondReceiveWindowDataRate;

/**
* The RX1DROffset parameter value
*/
Expand Down
Loading