MeshNet  1.0.0
Public Member Functions | Protected Member Functions | Protected Attributes
MeshRouteTable< BufferSize > Class Template Reference

Mesh route table contains all known routes. Source address column value has to be unique. Size of table can be defined using template like this: More...

#include <meshTables.h>

Inheritance diagram for MeshRouteTable< BufferSize >:
Inheritance graph
[legend]
Collaboration diagram for MeshRouteTable< BufferSize >:
Collaboration graph
[legend]

Public Member Functions

 MeshRouteTable (const uint32_t rowLife)
 Constructor. More...
 
bool set (int16_t index, uint8_t source, uint8_t neighbor, uint8_t hop, int16_t valid=-1)
 Sets row values in table. More...
 
int16_t set (uint8_t source, uint8_t neighbor, uint8_t hop, int16_t valid=-1)
 Sets row values in table. Row index is established using getIndexOfSource() method. If no row with specified source was found. New row is created in blank space. More...
 
int16_t set (const MeshRouteTableRow &row, int16_t valid=-1)
 Sets row values in table. Row index is established using getIndexOfSource() method. If no row with specified source was found. New row is created in blank space. More...
 
bool add (uint8_t source, uint8_t neighbor, uint8_t hop)
 Adds or overwrites data in row. This method works slightly different, because it implements part of mesh routing logic. More...
 
bool add (const MeshRouteTableRow &val)
 Adds or overwrites data in row. This method works slightly different as basic add method, because it implements part of mesh routing logic. If row with specified source exists and better metric is received, row is updated. If val is direct neighbor, row is always prolonged. If no valid row with specified source was found, new row is created. If table is full, oldset row which is not gateway will be overwritten. More...
 
void operator+= (const MeshRouteTableRow &val)
 
bool removeBySource (uint8_t source)
 Removes row, which contains specified source address. More...
 
int16_t removeByNeighbor (uint8_t neighbor)
 Removes all rows with specified neighbor address. More...
 
int16_t removeByAddress (uint8_t address)
 Removes all rows which contains specified address in "Source" or "Neighbor" field. More...
 
bool isRowDirectNeighbor (int16_t index) const
 Checks if row contains record of direct neighbor (source number and neighbor number are equal). More...
 
bool sourceExists (uint8_t source) const
 Checks if source node exists in table. More...
 
bool neighborExists (uint8_t neighbor) const
 Checks if neighbor node exists in table. More...
 
int16_t getIndexOfSource (uint8_t source) const
 Gets index of source address. More...
 
int16_t getNeighborFromSource (int8_t source) const
 Gets neighbor, which is connected (directly or indirectly) with source node. More...
 
uint8_t getDirectNeighborCount () const
 Gets number direct neighbors in buffer. More...
 
void setNewCallback (void(*callback)(MeshRouteTable< BufferSize > *, int16_t, const MeshRouteTableRow &, uint8_t))
 Sets callback function, which is called when new record was inserted to table. This callback is not called if row value is set. More...
 
- Public Member Functions inherited from MicroTable< MeshRouteTableRow, BufferSize >
 MicroTable (const uint32_t rowLife)
 Constructor. More...
 
const MeshRouteTableRowget (int16_t index) const
 Gets row from table by index. More...
 
MeshRouteTableRow get (int16_t index)
 Gets row from table by index. More...
 
const MeshRouteTableRowoperator[] (int16_t index) const
 Gets row from table by index. More...
 
MeshRouteTableRow operator[] (int16_t index)
 Gets row from table by index. More...
 
bool set (int16_t index, const MeshRouteTableRow &row)
 Sets row values in table. Row life time is set to maximum. More...
 
bool set (int16_t index, const MeshRouteTableRow &row, int16_t rawLifeTime_)
 Sets row values in table. More...
 
bool remove (const MeshRouteTableRow &rowRef)
 Removes first row, which is equal to rowRef. More...
 
bool remove (int16_t index)
 Removes row at index. More...
 
void clear ()
 Removes all rows data. More...
 
void validate ()
 Decreases all valid column values in specified interval (row lifetime). If valid value is 0, row will not be longer valid. More...
 
bool setRawRemainingLifeTime (int16_t index, uint8_t rawLifeTime_=255)
 Sets row life time. Valid counter for this rowcan be changed, only when row is still valid. More...
 
virtual int16_t getIndexOf (const MeshRouteTableRow &rowRef) const
 Gets index of row which is equal to rowRef. More...
 
int16_t getFirstBlankRowIndex (int16_t startPos=0) const
 Gets index of first blank row. More...
 
int16_t getOldestRowIndex () const
 Gets index of first oldest row. More...
 
bool exists (int16_t index) const
 Checks if row at specified index exists in table. More...
 
bool exists (const MeshRouteTableRow &rowRef) const
 Checks if any row, which is equal to rowRef exists in table. More...
 
bool isRowBlank (int16_t index) const
 Checks if row is blank or not. Row, that is not valid is blank. More...
 
int16_t size () const
 Gets capacity of table in rows. More...
 
int16_t count () const
 Gets number of used rows in buffer. More...
 
uint8_t getMaxRowLifeTime () const
 Gets the maximum row life time in millisseconds. More...
 
int16_t getRealUsedSize () const
 Gets real used count of rows in table. This value is calculated like this: last_used_row_index + 1. More...
 
uint8_t getFragmentation () const
 Calculates fragmentation in %. Fragmentation is calculated like this: (getRealUsedSize() - count())/getRealUsedSize() More...
 
void defragment ()
 Removes blank spaces between rows. Call this to speed up searching when buffer is fragmented. More...
 
uint32_t getRowRemainingLifeTime (int16_t index) const
 Gets selected row's remaining life time in milliseconds. More...
 
uint8_t getRawRowRemainingLifeTime (int16_t index) const
 Gets selected row's raw remaining life time. More...
 
uint32_t getLifeTimeResolution () const
 Gets life time resolution in milliseconds. More...
 
uint8_t convertLifeTimeToRaw (uint32_t lifeTime) const
 Converts life time in milliseconds to raw life time value. Value is rounded up. More...
 
uint32_t convertRawToLifeTime (uint8_t rawLifeTime) const
 Converts raw life time value to life time in milliseconds. Value is aproximate. To get accurate result, use getRowRemainingLifeTime() method. More...
 
virtual size_t getRawDataSize () const
 Calculates, how many bytes will be printed, when using command printRaw. More...
 
virtual size_t printRaw (OSTREAM &stream, const char tableIdentifier[4]) const
 Prints raw table data through stream. Those data can be decoded in PC or in another MCU. More...
 
size_t print (OSTREAM &stream, bool showIndexes=true, bool doNotPrintBlank=true, bool showRemainingRowLifetime=true) const
 Prints items from table to stream. More...
 
size_t printTo (Print &p) const override
 Makes this class printable. More...
 

Protected Member Functions

void onCreate (int16_t index, const MeshRouteTableRow &newValue, uint8_t newRawLifeTime) override
 This method is called when valid row is set to blank space. It is called after count calculation is done, but before buffer is updated. More...
 
void onRemove (int16_t index, const MeshRouteTableRow &oldValue, uint8_t oldRawLifeTime, bool removedByValidation=false) override
 This method is called when valid row is set to new value. It is called after count calculation is done, but before buffer is updated. More...
 
void onClear () override
 This method is called when table is cleared. It is called after cleaning is done, so all variables are fresh. More...
 
int16_t getOldestRowIndexNotGW () const
 Gets index of first oldest row, except gateway nodes. More...
 
- Protected Member Functions inherited from MicroTable< MeshRouteTableRow, BufferSize >
virtual void onSet (int16_t index, const MeshRouteTableRow &oldValue, uint8_t oldRawLifeTime, const MeshRouteTableRow &newValue, uint8_t newRawLifeTime)
 This method is called when valid row is set to new value. It is called after count calculation is done, but before buffer is updated. More...
 
virtual void onRowLifeChanged (int16_t index, uint8_t old_raw, uint8_t new_raw)
 This method is called when table is cleared. It is called after cleaning is done, so all variables are fresh. More...
 

Protected Attributes

uint8_t directNeighborCount = 0
 
- Protected Attributes inherited from MicroTable< MeshRouteTableRow, BufferSize >
MeshRouteTableRow buffer [RowsCount]
 
uint8_t lifeTime [RowsCount]
 
uint32_t lastValidationTime
 
int16_t usedBufferSize
 
int16_t realUsedSize
 
const uint32_t ValidationInterval
 
const uint32_t RowLife
 

Detailed Description

template<int16_t BufferSize>
class MeshRouteTable< BufferSize >

Mesh route table contains all known routes. Source address column value has to be unique. Size of table can be defined using template like this:

MeshRouteTable<255> routeTable(60000);

Size of route table in example is 255 rows and lifetime of one row is 60 seconds.

How data are stored:

index (not physically stored) Source [uint8_t] Neighbor [uint8_t] Hops [uint8_t] Valid [uint8_t]
0 XXX XXX XXX XXX
1 XXX XXX XXX XXX
... ... ... ... ...
255 - depends on BufferSize XXX XXX XXX XXX

Constructor & Destructor Documentation

◆ MeshRouteTable()

template<int16_t BufferSize>
MeshRouteTable< BufferSize >::MeshRouteTable ( const uint32_t  rowLife)
inline

Constructor.

Parameters
rowLifeLife time of each row in milliseconds. Value has to be higher than 0, but it is recommended to set value to higher than 100 seconds.

Member Function Documentation

◆ add() [1/2]

template<int16_t BufferSize>
bool MeshRouteTable< BufferSize >::add ( const MeshRouteTableRow val)
inline

Adds or overwrites data in row. This method works slightly different as basic add method, because it implements part of mesh routing logic. If row with specified source exists and better metric is received, row is updated. If val is direct neighbor, row is always prolonged. If no valid row with specified source was found, new row is created. If table is full, oldset row which is not gateway will be overwritten.

Note
To only add row use set method without index parameter.
Parameters
rowRow with values, that will be added to specified row. (Valid property is ignored and is set to 255)
Returns
Returns when row was added or overwrote.

◆ add() [2/2]

template<int16_t BufferSize>
bool MeshRouteTable< BufferSize >::add ( uint8_t  source,
uint8_t  neighbor,
uint8_t  hop 
)
inline

Adds or overwrites data in row. This method works slightly different, because it implements part of mesh routing logic.

Note
To only add row use set method without index parameter.
Parameters
sourceSource node number of row, which to set.
neighborNeighbor, which is connected with source node.
hopNumber of hops from current node to source node.
Returns
Returns true when row was added or overwrote.

◆ getDirectNeighborCount()

template<int16_t BufferSize>
uint8_t MeshRouteTable< BufferSize >::getDirectNeighborCount ( ) const
inline

Gets number direct neighbors in buffer.

Note
It is recommended to call set() method instead direct setting Neighbor variable of row, because getDirectNeighborCount() can return wrong value.
Returns
Returns number direct neighbors in buffer.

◆ getIndexOfSource()

template<int16_t BufferSize>
int16_t MeshRouteTable< BufferSize >::getIndexOfSource ( uint8_t  source) const
inline

Gets index of source address.

Parameters
sourceSource node addres, which index has to be retrieved.
Note
Source address has to be unique in each row.
Returns
Returns index of row, which contains row with specified source node.

◆ getNeighborFromSource()

template<int16_t BufferSize>
int16_t MeshRouteTable< BufferSize >::getNeighborFromSource ( int8_t  source) const
inline

Gets neighbor, which is connected (directly or indirectly) with source node.

Parameters
sourceSource node number (index) of row, which to check.
Returns
Returns neighbour node number or -1 when there is no row with specified source node number.

◆ getOldestRowIndexNotGW()

template<int16_t BufferSize>
int16_t MeshRouteTable< BufferSize >::getOldestRowIndexNotGW ( ) const
inlineprotected

Gets index of first oldest row, except gateway nodes.

Returns
Returns index of first oldest row or -1 if table is full.

◆ isRowDirectNeighbor()

template<int16_t BufferSize>
bool MeshRouteTable< BufferSize >::isRowDirectNeighbor ( int16_t  index) const
inline

Checks if row contains record of direct neighbor (source number and neighbor number are equal).

Parameters
indexIndex of row to check
Returns
Returns true when row contains only record of neighbor.

◆ neighborExists()

template<int16_t BufferSize>
bool MeshRouteTable< BufferSize >::neighborExists ( uint8_t  neighbor) const
inline

Checks if neighbor node exists in table.

Parameters
neighborNeighbor node number to check.
Returns
Returns true when neighbor node exists in table.

◆ onClear()

template<int16_t BufferSize>
void MeshRouteTable< BufferSize >::onClear ( )
inlineoverrideprotectedvirtual

This method is called when table is cleared. It is called after cleaning is done, so all variables are fresh.

Reimplemented from MicroTable< MeshRouteTableRow, BufferSize >.

◆ onCreate()

template<int16_t BufferSize>
void MeshRouteTable< BufferSize >::onCreate ( int16_t  index,
const MeshRouteTableRow newValue,
uint8_t  newRawLifeTime 
)
inlineoverrideprotectedvirtual

This method is called when valid row is set to blank space. It is called after count calculation is done, but before buffer is updated.

Parameters
indexIndex of row.
newValueReference to new value, to be inserted to row.
newRawLifeTimeNew raw life time value.

Reimplemented from MicroTable< MeshRouteTableRow, BufferSize >.

◆ onRemove()

template<int16_t BufferSize>
void MeshRouteTable< BufferSize >::onRemove ( int16_t  index,
const MeshRouteTableRow oldValue,
uint8_t  oldRawLifeTime,
bool  removedByValidation = false 
)
inlineoverrideprotectedvirtual

This method is called when valid row is set to new value. It is called after count calculation is done, but before buffer is updated.

Parameters
indexIndex of row.
oldValueReference to old value at index.
oldRawLifeTimeOld raw life time value.
newValueReference to new value, to be inserted to row.
newRawLifeTimeNew raw life time value.

This method is called when valid row is removed. It is called after count calculation is done, but before buffer is updated.

Parameters
indexIndex of row.
oldValueReference to removed value.
oldRawLifeTimeOld raw life time value.

Reimplemented from MicroTable< MeshRouteTableRow, BufferSize >.

◆ operator+=()

template<int16_t BufferSize>
void MeshRouteTable< BufferSize >::operator+= ( const MeshRouteTableRow val)
inline

◆ removeByAddress()

template<int16_t BufferSize>
int16_t MeshRouteTable< BufferSize >::removeByAddress ( uint8_t  address)
inline

Removes all rows which contains specified address in "Source" or "Neighbor" field.

Note
Multiple rows can be removed.
Parameters
addressSpecified address to remove from route table.
Returns
Returns count of removed rows.

◆ removeByNeighbor()

template<int16_t BufferSize>
int16_t MeshRouteTable< BufferSize >::removeByNeighbor ( uint8_t  neighbor)
inline

Removes all rows with specified neighbor address.

Note
Multiple rows can be removed.
Parameters
neighborRows with this neighbor address will be removed.
Returns
Returns count of removed rows.

◆ removeBySource()

template<int16_t BufferSize>
bool MeshRouteTable< BufferSize >::removeBySource ( uint8_t  source)
inline

Removes row, which contains specified source address.

Parameters
sourceRow with this source address will be removed.
Returns
Returns true when row was removed.

◆ set() [1/3]

template<int16_t BufferSize>
int16_t MeshRouteTable< BufferSize >::set ( const MeshRouteTableRow row,
int16_t  valid = -1 
)
inline

Sets row values in table. Row index is established using getIndexOfSource() method. If no row with specified source was found. New row is created in blank space.

Note
It is recommended to use this set method or int16_t set(uint8_t source, uint8_t neighbor, uint8_t hop, uint8_t metricRaw, int16_t valid = -1), because only those set methods checks if source is unique.
Parameters
rowRow to set.
validSet this value to 255 to reset row lifetime. Set it to -1, to keep old value. Accepted value (0-255).
Returns
Returns index of row, that was set or -1 of nothing was set.

◆ set() [2/3]

template<int16_t BufferSize>
bool MeshRouteTable< BufferSize >::set ( int16_t  index,
uint8_t  source,
uint8_t  neighbor,
uint8_t  hop,
int16_t  valid = -1 
)
inline

Sets row values in table.

Warning
It is not recommended to use this method, because it does not checks if source address is unique or not. Please use int16_t set(uint8_t source, uint8_t neighbor, uint8_t hop, uint8_t metricRaw, int16_t valid = -1) or int16_t set(const MeshRouteTableRow& row, int16_t valid = -1) method.
Parameters
indexIndex of row to set.
sourceSource node address of row, which to set.
neighborNeighbor node address, which is connected with source node.
hopNumber of hops from current node to source node.
validSet this value to 255 to reset row lifetime. Set it to -1, to keep old value. Accepted value (0-255).
Returns
Returns true when row was set.

◆ set() [3/3]

template<int16_t BufferSize>
int16_t MeshRouteTable< BufferSize >::set ( uint8_t  source,
uint8_t  neighbor,
uint8_t  hop,
int16_t  valid = -1 
)
inline

Sets row values in table. Row index is established using getIndexOfSource() method. If no row with specified source was found. New row is created in blank space.

Note
It is recommended to use this set method or int16_t set(const MeshRouteTableRow& row, int16_t valid = -1), because only those set methods checks if source is unique.
Parameters
sourceSource node address of row, which to set. Source address is used for getting index of row.
neighborNeighbor node address, which is connected with source node.
hopNumber of hops from current node to source node.
validSet this value to 255 to reset row lifetime. Set it to -1, to keep old value. Accepted value (0-255).
Returns
Returns index of row, that was set or -1 of nothing was set.

◆ setNewCallback()

template<int16_t BufferSize>
void MeshRouteTable< BufferSize >::setNewCallback ( void(*)(MeshRouteTable< BufferSize > *, int16_t, const MeshRouteTableRow &, uint8_t)  callback)
inline

Sets callback function, which is called when new record was inserted to table. This callback is not called if row value is set.

Parameters
callbackCallback function.

Callback function parameters

  • MeshRouteTable<BufferSize> Pointer to table which called this callback
  • int16_t - Index of item.
  • MeshRouteTableRow - Created row.
  • uint8_t - New raw life time value.

◆ sourceExists()

template<int16_t BufferSize>
bool MeshRouteTable< BufferSize >::sourceExists ( uint8_t  source) const
inline

Checks if source node exists in table.

Parameters
sourceSource node number to check.
Returns
Returns true when neighbor node exists in table.

Field Documentation

◆ directNeighborCount

template<int16_t BufferSize>
uint8_t MeshRouteTable< BufferSize >::directNeighborCount = 0
protected

The documentation for this class was generated from the following file:
MeshRouteTable
Mesh route table contains all known routes. Source address column value has to be unique....
Definition: meshTables.h:1329