MeshProtocolSimulator  1.0.0
Public Member Functions | Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
NodeSimulator Class Reference

Structure, that runs simulation for one node in network and contains some info about that node. More...

#include <Emulator_helpers.h>

Public Member Functions

 NodeSimulator ()
 Constructor.
 
 NodeSimulator (Emulator *emulator, int index, MeshMAC nodeMACAddress)
 Constructor. More...
 
 NodeSimulator (Emulator *emulator, int index)
 Constructor. More...
 
 NodeSimulator (const NodeSimulator &)=default
 
 ~NodeSimulator ()
 Destructor.
 
NodeSimulatoroperator= (const NodeSimulator &)=default
 
MeshNet * Node ()
 Gets pointer to node, which is simulated by current NodeSimulator.
 
EmulatorgetParentPtr ()
 Gets pointer to emulator, which owns this instance.
 
int getIndex ()
 Gets index of node in emulator array of nodes.
 
uint64_t getCyclesCount ()
 Gets count of cycles, that were done in infinity loop, where is MeshNet code executed.
 
void ExecuteCycle ()
 Executes current node's one cycle.
 
bool BeginSimulation (NodeSettings settings_, std::chrono::nanoseconds simulationTimeOffset=std::chrono::nanoseconds::zero())
 Turns on simulation for current node. More...
 
bool BeginSimulation (std::chrono::nanoseconds simulationTimeOffset=std::chrono::nanoseconds::zero())
 Turns on simulation for current node with old or default settings. More...
 
void EndSimulation ()
 Turns off simulation for current node.
 
void SleepFor (std::chrono::nanoseconds sleepDelay)
 Makes current node sleep for specified delay. More...
 
bool IsSimulationRunning ()
 Checks if simulation is running for current node. More...
 
bool IsSimulationEnabled ()
 Checks if simulation has been started, but it does not mean it is currently running. More...
 
std::chrono::nanoseconds getSimulationTime ()
 Gets current node simulation time. More...
 
std::chrono::nanoseconds getNodeWorkTime ()
 Gets work time of current node. More...
 
std::chrono::nanoseconds getLastSentTime ()
 Gets simulation time, when last sent occoured. More...
 
const std::vector< NeighborNode > & GetNeighborNodes ()
 Gets reference to vector, which has pointers to all neighbor nodes. This return valid value only when simulation is running.
 
NodeSettings GetSettings ()
 Gets node settings.
 

Public Attributes

std::thread Thread
 
std::mutex Mutex
 
std::function< void(void)> Interrupt = NULL
 Function done at interrupt.
 

Protected Member Functions

bool IsFree (std::chrono::nanoseconds time)
 Check if node is not receiving at current simulation time. Use this from inside in simulation. More...
 
bool IsFreeEx (std::chrono::nanoseconds time)
 Check if node is not receiving at current simulation time. Use this from outside of simulation. More...
 
uint8_t Available ()
 Checks how many bytes are available to receive. More...
 
uint16_t GetLastAvailableBytes ()
 Gets last available bytes in one frame from Available() method.
 
bool Receive (uint8_t *message, uint8_t &messageLength, float &RSSI)
 Receives message when available. More...
 
void Send (uint8_t *message, uint8_t messageLength)
 Sends data to the environment. More...
 
void OnScanResponse (const MeshNetScanResult &result)
 This method is called on every scan response by node. More...
 
void CreateRandomInterference (std::chrono::microseconds interferingDuration)
 
void PauseSimulationTimer ()
 Call this if cycle is going to be paused. It stops counting instructions done on current thread.
 
void ResumeSimulationTimer ()
 Call this if cycle is going to be resumed. It restarts counting instructions done on current thread.
 
int CheckForSentMessages ()
 Checks received messages, puts them in FIFO and clears FIFO from interference and overflow messages. More...
 
void ReportInterference (RawMessageDescriptor &msg1, RawMessageDescriptor &msg2, MeshMAC currentNode)
 Reports Hidden terminal interference of two messages. More...
 
void ReportCollision (RawMessageDescriptor &msg, RawMessage &msg2)
 Reports half-duplex collision of message, which was received during sending. More...
 
void ReportMessageLost (RawMessageDescriptor &msg, MeshMAC receiverNode)
 Reports, that sent message was lost due to bad link quality. More...
 
void OnNodeConnected ()
 This method is called when node is connected to new network. More...
 
void OnNodeDisconnected (MeshDisconnectReason reason, MeshMAC oldBSSID, uint8_t oldAddress)
 This method is called when node is disconnected from any network. More...
 
void OnReceive (MeshPacket &packet)
 This method is called when node has received data packet. More...
 
void OnPingResp (uint8_t pingID, uint8_t pingAddr, MeshPacketError pingStat, uint8_t hops, uint32_t duration, const uint8_t *testData, int16_t testDataLength)
 This method is called when ping response is received. More...
 

Static Protected Member Functions

static void OnNodeConnectedStatic (MeshNet *mesh)
 Static function, which can be used as callback. In this function correct OnNodeConnected() method is called.
 
static void OnNodeDisconnectedStatic (MeshNet *mesh, MeshDisconnectReason reason, MeshMAC oldBSSID, uint8_t oldAddress)
 Static function, which can be used as callback. In this function correct OnNodeDisconnected() method is called.
 
static void OnReceiveStatic (MeshNet *mesh, MeshPacket &packet)
 Static function, which can be used as callback. In this function correct OnReceive() method is called.
 
static void OnPingRespStatic (MeshNet *mesh, uint8_t pingID, uint8_t pingAddr, MeshPacketError pingStat, uint8_t hops, uint32_t duration, const uint8_t *testData, int16_t testDataLength)
 Static function, which can be used as callback. In this function correct OnPingResp() method is called.
 

Protected Attributes

NodeSettings settings
 Node settings.
 
Emulatorem = NULL
 Pointer to emulator, which owns this instance.
 
int index = 0
 Index of node in emulator array of nodes.
 
uint64_t numberOfCycles = 0
 Number of cycles done from start of the simulation.
 
std::vector< NeighborNodeneighborNodes
 Vector with pointers to all neighbor nodes.
 
std::chrono::nanoseconds FreeFromTime = std::chrono::nanoseconds::zero()
 
std::chrono::nanoseconds FreeSenderFromTime = std::chrono::nanoseconds::zero()
 
std::deque< RawMessageDescriptorinputFIFO
 FIFO, that acts as FIFO on receiver module.
 
std::list< RawMessageDescriptorreceivedMessagesList
 List of received messages, that was not processed yet.
 
uint64_t lastCycles = 0
 Last count of processor cycles, which are used to calculate properly timing of simulation.
 
std::chrono::nanoseconds simulationTime = std::chrono::nanoseconds::zero()
 Simulation time elapsed in nanoseconds.
 
std::chrono::nanoseconds lastSentTime = std::chrono::nanoseconds::zero()
 Simulation time, when last message was sent.
 
std::chrono::nanoseconds simulationOffset = std::chrono::nanoseconds::zero()
 Simulation start offset.
 
std::atomic< bool > CycleLock { true }
 True when current cycle is locked. Cycle can be executed only when it is unlocked.
 
std::condition_variable_any CycleLockCV
 Conditional variable for cycle lock.
 
bool simulationStarted = false
 True if BeginSimulation() was called.
 
int lastAvailableBytes = 0
 Count of bytes, that was returned last time by Available() method.
 
bool simulationTimerPaused = true
 True if current simulation timer is running (ResumeSimulationTimer() was called) and false if simulation timer is not running (PauseSimulationTimer() was called).
 
MeshNet * currentNode = NULL
 Pointer to speficied node.
 
Simulation_RFI * currentRFI = NULL
 Pointer to speficied RF interface.
 
friend Emulator
 
friend Simulation_RFI
 

Detailed Description

Structure, that runs simulation for one node in network and contains some info about that node.

Constructor & Destructor Documentation

◆ NodeSimulator() [1/2]

NodeSimulator::NodeSimulator ( Emulator emulator,
int  index,
MeshMAC  nodeMACAddress 
)

Constructor.

Parameters
emulatorEmulator, which owns this instance.
indexIndex of node in emulator array of nodes.
nodeMACAddressMAC address of node.

◆ NodeSimulator() [2/2]

NodeSimulator::NodeSimulator ( Emulator emulator,
int  index 
)

Constructor.

Parameters
emulatorEmulator, which owns this instance.
indexIndex of node in emulator array of nodes.

Member Function Documentation

◆ Available()

uint8_t NodeSimulator::Available ( )
protected

Checks how many bytes are available to receive.

Warning
Can be called only inside of current simulation thread.
Note
This method pauses cycle.
Returns
Returns number of bytes available to receive.

◆ BeginSimulation() [1/2]

bool NodeSimulator::BeginSimulation ( NodeSettings  settings_,
std::chrono::nanoseconds  simulationTimeOffset = std::chrono::nanoseconds::zero() 
)
inline

Turns on simulation for current node.

Parameters
[in]settings_Settings of current node.
simulationTimeOffsetOffset time of current node simulation start.
Warning
When simulation is reenabled, simulation time has to be greather than minimum simulation time in other nodes.

Returns true when simulation has started correctly.

◆ BeginSimulation() [2/2]

bool NodeSimulator::BeginSimulation ( std::chrono::nanoseconds  simulationTimeOffset = std::chrono::nanoseconds::zero())

Turns on simulation for current node with old or default settings.

Parameters
[in]settings_Settings of current node.
simulationTimeOffsetOffset time of current node simulation start.
Warning
When simulation is reenabled, simulation time has to be greather than minimum simulation time in other nodes.

Returns true when simulation has started correctly.

◆ CheckForSentMessages()

int NodeSimulator::CheckForSentMessages ( )
protected

Checks received messages, puts them in FIFO and clears FIFO from interference and overflow messages.

Returns
Returns number of available bytes in last message or 0 when no message is available.

◆ getLastSentTime()

std::chrono::nanoseconds NodeSimulator::getLastSentTime ( )
inline

Gets simulation time, when last sent occoured.

Returns
Returns simulation time, when last sent occoured.

◆ getNodeWorkTime()

std::chrono::nanoseconds NodeSimulator::getNodeWorkTime ( )
inline

Gets work time of current node.

Returns
Returns work time of current node.

◆ getSimulationTime()

std::chrono::nanoseconds NodeSimulator::getSimulationTime ( )

Gets current node simulation time.

Returns
Returns current node simulation time.

◆ IsFree()

bool NodeSimulator::IsFree ( std::chrono::nanoseconds  time)
protected

Check if node is not receiving at current simulation time. Use this from inside in simulation.

Parameters
timeSimulation time, when to check. This value can be greather or equal to curren node simulation time.
Note
Simulation is paused during checking free state and is incremented by interferenceCheckCyclesDelay and interferenceCheckDelay.
Returns
True when node is not receiving.

◆ IsFreeEx()

bool NodeSimulator::IsFreeEx ( std::chrono::nanoseconds  time)
protected

Check if node is not receiving at current simulation time. Use this from outside of simulation.

Parameters
timeSimulation time, when to check. This value can be greather or equal to curren node simulation time.
Note
Simulation is paused during checking free state and is not incremented.
Returns
True when node is not receiving.

◆ IsSimulationEnabled()

bool NodeSimulator::IsSimulationEnabled ( )
inline

Checks if simulation has been started, but it does not mean it is currently running.

Returns
True when simulation has been started by calling SimulationBegin() method.

◆ IsSimulationRunning()

bool NodeSimulator::IsSimulationRunning ( )
inline

Checks if simulation is running for current node.

Returns
True when simulation is running for current node.

◆ OnNodeConnected()

void NodeSimulator::OnNodeConnected ( )
protected

This method is called when node is connected to new network.

Warning
Do not call this method. It will be automatically called as callback method in MeshNet.

◆ OnNodeDisconnected()

void NodeSimulator::OnNodeDisconnected ( MeshDisconnectReason  reason,
MeshMAC  oldBSSID,
uint8_t  oldAddress 
)
protected

This method is called when node is disconnected from any network.

Parameters
reasonDisconnect reason.
oldMACBSSID of network the node was disconnected from.
oldAddressOld address assigned to current node.
Warning
Do not call this method. It will be automatically called as callback method in MeshNet.

◆ OnPingResp()

void NodeSimulator::OnPingResp ( uint8_t  pingID,
uint8_t  pingAddr,
MeshPacketError  pingStat,
uint8_t  hops,
uint32_t  duration,
const uint8_t *  testData,
int16_t  testDataLength 
)
protected

This method is called when ping response is received.

Parameters
pingIDID od ping packet.
pingAddrAddress of pinged node.
pingStatError which happened to ping packet.
hopsResponse packet hops.
durationPing duration in ms.
testDataPointer to test data sent with ping.
testDataLengthLength of test data in bytes.

◆ OnReceive()

void NodeSimulator::OnReceive ( MeshPacket &  packet)
protected

This method is called when node has received data packet.

Parameters
packetReceived packet.

◆ OnScanResponse()

void NodeSimulator::OnScanResponse ( const MeshNetScanResult &  result)
protected

This method is called on every scan response by node.

Parameters
resultScan response result.
Warning
Do not call this method. It will be automatically called as callback method in MeshNet.

◆ Receive()

bool NodeSimulator::Receive ( uint8_t *  message,
uint8_t &  messageLength,
float &  RSSI 
)
protected

Receives message when available.

Parameters
[out]messageBuffer for message.
[out]messageLengthLength of received message.
[out]RSSIvalue of received message.
Warning
Can be called only inside of current simulation thread.
Returns
Returns true when receiving was successfull.

◆ ReportCollision()

void NodeSimulator::ReportCollision ( RawMessageDescriptor msg,
RawMessage msg2 
)
protected

Reports half-duplex collision of message, which was received during sending.

    ┌───┐--X->┌───┐
    | A |     | B |
    └───┘<-X--└───┘
    A - Current node, which sent message to B
    B - Node, which sent message to A at same time
* 
Parameters
msgReceived message during sending message.
msg2Message that is going to be sent, but cannot.

◆ ReportInterference()

void NodeSimulator::ReportInterference ( RawMessageDescriptor msg1,
RawMessageDescriptor msg2,
MeshMAC  currentNode 
)
protected

Reports Hidden terminal interference of two messages.

    ┌───┐     ┌───┐     ┌───┐
    | A |---->| B |<----| C |
    └───┘     └───┘     └───┘
    A - Current node, which sent message to B at same time as C
    B - Node, which received messages from both nodes A and C
    C - Node, which sent message to B at same time as A
* 
Parameters
msg1One of the interfered messages.
msg2Second of the interfered messages.
currentNodeMAC address of node, where interference happened.

◆ ReportMessageLost()

void NodeSimulator::ReportMessageLost ( RawMessageDescriptor msg,
MeshMAC  receiverNode 
)
protected

Reports, that sent message was lost due to bad link quality.

Parameters
[in]msgMessage, that is lost.
receiverNodeNode, which should receive message.

◆ Send()

void NodeSimulator::Send ( uint8_t *  message,
uint8_t  messageLength 
)
protected

Sends data to the environment.

Parameters
messageRaw bytes of message to send.
Warning
Can be called only inside of current simulation thread.
Note
This method pauses cycle.
Parameters
messageLengthNumber of bytes to send.

◆ SleepFor()

void NodeSimulator::SleepFor ( std::chrono::nanoseconds  sleepDelay)

Makes current node sleep for specified delay.

Parameters
Delay,whichwill be current node sleepeng for.

The documentation for this class was generated from the following files: