EnigmaIOT  0.9.3
Secure sensor and gateway platform based on ESP8266 and ESP32
NodeList.h
Go to the documentation of this file.
1 
9 #ifndef _NODELIST_h
10 #define _NODELIST_h
11 
12 #if defined(ARDUINO) && ARDUINO >= 100
13  #include "Arduino.h"
14 #else
15  #include "WProgram.h"
16 #endif
17 #include "EnigmaIoTconfig.h"
18 #include "Filter.h"
19 
25  INIT,
29  SLEEP
30 };
31 
32 typedef enum {
33  NAME_OK = 0,
35  EMPTY_NAME = -2,
36  TOO_LONG = -3
38 
39 typedef enum node_status status_t;
40 
41 typedef enum control_message_type {
42  VERSION = 0x01,
43  VERSION_ANS = 0x81,
44  SLEEP_GET = 0x02,
45  SLEEP_SET = 0x03,
46  SLEEP_ANS = 0x82,
47  IDENTIFY = 0x04,
48  RESET = 0x05,
49  RESET_ANS = 0x85,
50  RSSI_GET = 0x06,
51  RSSI_ANS = 0x86,
52  NAME_GET = 0x07,
53  NAME_ANS = 0x08,
54  NAME_SET = 0x87,
55  OTA = 0xEF,
56  OTA_ANS = 0xFF,
57  USERDATA_GET = 0x00,
58  USERDATA_SET = 0x20,
59  INVALID = 0xF0
60  //USERDATA_ANS = 0x90
62 
63 typedef enum ota_status {
70  OTA_FINISHED = 6
72 
76 struct node_instance {
78  uint16_t nodeId;
79  uint8_t key[32];
80  uint16_t lastMessageCounter;
81  time_t keyValidFrom;
82  time_t lastMessageTime;
84  bool keyValid = false;
85  bool sleepyNode = true;
86 };
87 
88 typedef struct node_instance node_t;
89 
93 class Node {
94 public:
99  Node ();
100 
106  Node (node_t nodeData);
107 
112  uint8_t *getMacAddress () {
113  return mac;
114  }
115 
120  uint16_t getNodeId () {
121  return nodeId;
122  }
123 
128  void setNodeId (uint16_t nodeId) {
129  this->nodeId = nodeId;
130  }
131 
136  char* getNodeName () {
137  if (strlen (nodeName)) {
138  return nodeName;
139  } else {
140  return NULL;
141  }
142  }
143 
148  void setNodeName (const char* name) {
149  memset (nodeName, 0, NODE_NAME_LENGTH);
150  strncpy (nodeName, name, NODE_NAME_LENGTH);
151  }
152 
157  uint8_t *getEncriptionKey () {
158  return key;
159  }
160 
165  void setEncryptionKey (const uint8_t* key);
166 
171  time_t getKeyValidFrom () {
172  return keyValidFrom;
173  }
174 
180  this->keyValidFrom = keyValidFrom;
181  }
182 
187  time_t getLastMessageTime () {
188  return lastMessageTime;
189  }
190 
195  lastMessageTime = millis ();
196  }
197 
202  uint16_t getLastMessageCounter () {
203  return lastMessageCounter;
204  }
205 
210  void setLastMessageCounter (uint16_t counter) {
211  lastMessageCounter = counter;
212  }
213 
218  void setMacAddress (uint8_t *macAddress) {
219  if (macAddress) {
220  memcpy (mac, macAddress, 6);
221  }
222  }
223 
228  bool isKeyValid () {
229  return keyValid;
230  }
231 
236  void setKeyValid (bool status) {
237  keyValid = status;
238  }
239 
244  bool isRegistered () {
245  return status == REGISTERED;
246  }
247 
253  return status;
254  }
255 
261  this->status = status;
262  }
263 
268  node_t getNodeData ();
269 
274  void printToSerial (Stream *port = &Serial);
275 
279  void reset ();
280 
286  void setSleepy (bool sleepy) {
287  if (initAsSleepy) {
288  sleepyNode = sleepy;
289  } else {
290  sleepyNode = false;
291  }
292  }
293 
298  void setInitAsSleepy (bool sleepy) {
299  initAsSleepy = sleepy;
300  }
301 
306  bool getInitAsSleepy () {
307  return initAsSleepy;
308  }
309 
315  bool getSleepy () {
316  return sleepyNode;
317  }
318 
323  void updatePacketsRate (float value);
324 
326  size_t qMessageLength;
327  bool qMessagePending = false;
328 
329  uint32_t packetNumber = 0;
330  uint32_t packetErrors = 0;
331  double per = 0;
332  double packetsHour = 0;
333  int64_t t1, t2, t3, t4;
334 
335 protected:
336 //#define KEYLENGTH 32
337  bool keyValid;
340  uint16_t nodeId;
341  timer_t keyValidFrom;
342  bool sleepyNode = true;
345  uint8_t key[KEY_LENGTH];
346  timer_t lastMessageTime;
349 
353  void initRateFilter ();
354 
355  friend class NodeList;
356 };
357 
358 
359 class NodeList {
360 public:
361 
365  NodeList ();
366 
372  Node *getNodeFromID (uint16_t nodeId);
373 
379  Node *getNodeFromMAC (const uint8_t* mac);
380 
386  Node* getNodeFromName (const char* name);
387 
394  int8_t checkNodeName (const char* name, const uint8_t* address);
395 
400  Node *findEmptyNode ();
401 
406  uint16_t countActiveNodes ();
407 
413  bool unregisterNode (uint16_t nodeId);
414 
420  bool unregisterNode (const uint8_t* mac);
421 
427  bool unregisterNode (Node *node);
428 
434  Node *getNextActiveNode (uint16_t nodeId);
435 
441  Node *getNextActiveNode (Node node);
442 
448  Node *getNewNode (const uint8_t* mac);
449 
454  void printToSerial (Stream *port);
455 
456 protected:
458 
459 };
460 
461 
462 #endif
463 
OTA_CHECK_OK
@ OTA_CHECK_OK
Definition: NodeList.h:66
Node::lastMessageCounter
uint16_t lastMessageCounter
Last message counter state for specific Node.
Definition: NodeList.h:339
VERSION_ANS
@ VERSION_ANS
Definition: NodeList.h:43
IDENTIFY
@ IDENTIFY
Definition: NodeList.h:47
node_instance::lastMessageCounter
uint16_t lastMessageCounter
Last message counter state for specific Node.
Definition: NodeList.h:80
Node::setLastMessageTime
void setLastMessageTime()
Sets current moment as last node message time.
Definition: NodeList.h:194
NodeList::findEmptyNode
Node * findEmptyNode()
Searches for a free place for a new Node instance.
Definition: NodeList.cpp:196
Node::packetNumber
uint32_t packetNumber
Number of packets received from node to gateway.
Definition: NodeList.h:329
SLEEP_SET
@ SLEEP_SET
Definition: NodeList.h:45
NodeList::getNodeFromName
Node * getNodeFromName(const char *name)
Gets node that correspond with given node name.
Definition: NodeList.cpp:148
EMPTY_NAME
@ EMPTY_NAME
Definition: NodeList.h:35
Node::lastMessageTime
timer_t lastMessageTime
Node state.
Definition: NodeList.h:346
USERDATA_SET
@ USERDATA_SET
Definition: NodeList.h:58
Node::setNodeName
void setNodeName(const char *name)
Sets Node name.
Definition: NodeList.h:148
NodeList::printToSerial
void printToSerial(Stream *port)
Dumps node list data to a Stream object.
Definition: NodeList.cpp:290
Node::sleepyNode
bool sleepyNode
Node sleepy definition.
Definition: NodeList.h:342
Node::setStatus
void setStatus(status_t status)
Sets status for finite state machine that represents node.
Definition: NodeList.h:260
NodeList::nodes
Node nodes[NUM_NODES]
Static Node array that holds maximum number of supported nodes.
Definition: NodeList.h:457
Node::packetErrors
uint32_t packetErrors
Number of errored packets.
Definition: NodeList.h:330
SLEEP
@ SLEEP
Definition: NodeList.h:29
Node::qMessagePending
bool qMessagePending
True if message should be sent just after next data message
Definition: NodeList.h:327
Node::isKeyValid
bool isKeyValid()
Gets shared key validity for this node.
Definition: NodeList.h:228
Node::setInitAsSleepy
void setInitAsSleepy(bool sleepy)
Records if node started as a sleepy node or not. If it did not started so it will never accept sleep ...
Definition: NodeList.h:298
NodeList::getNewNode
Node * getNewNode(const uint8_t *mac)
Finds a node that correspond with given address of creates a new one if it does not exist.
Definition: NodeList.cpp:275
Node::setNodeId
void setNodeId(uint16_t nodeId)
Sets a new Node identifier.
Definition: NodeList.h:128
Node::keyValid
bool keyValid
Node shared key valid.
Definition: NodeList.h:337
NodeList::getNodeFromMAC
Node * getNodeFromMAC(const uint8_t *mac)
Gets node that correspond with given address.
Definition: NodeList.cpp:133
EnigmaIoTconfig.h
Parameter configuration.
NodeList::NodeList
NodeList()
Node list constructor.
Definition: NodeList.cpp:120
ENIGMAIOT_ADDR_LEN
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
Definition: EnigmaIoTconfig.h:14
node_instance::key
uint8_t key[32]
Shared key.
Definition: NodeList.h:79
NODE_NAME_LENGTH
static const uint8_t NODE_NAME_LENGTH
Maximum number of characters of node name.
Definition: EnigmaIoTconfig.h:19
OTA
@ OTA
Definition: NodeList.h:55
Node::initAsSleepy
bool initAsSleepy
Stores initial sleepy node. If this is false, this node does not accept sleep time changes.
Definition: NodeList.h:343
Node::t4
int64_t t4
Timestaps to calculate clock offset.
Definition: NodeList.h:333
RESET
@ RESET
Definition: NodeList.h:48
Node::getSleepy
bool getSleepy()
Gets node working mode regarding battery saving strategy. If node is sleepy it will turn into deep sl...
Definition: NodeList.h:315
Node::setKeyValid
void setKeyValid(bool status)
Sets shared key validity for this node.
Definition: NodeList.h:236
Node::getNodeData
node_t getNodeData()
Gets a struct that represents node object. May be used for node serialization.
Definition: NodeList.cpp:17
OTA_TIMEOUT
@ OTA_TIMEOUT
Definition: NodeList.h:69
node_instance::lastMessageTime
time_t lastMessageTime
Last time a message was received by Node.
Definition: NodeList.h:82
node_instance::keyValid
bool keyValid
Node shared key valid.
Definition: NodeList.h:84
RSSI_GET
@ RSSI_GET
Definition: NodeList.h:50
status_t
enum node_status status_t
Node state.
Definition: NodeList.h:39
INVALID
@ INVALID
Definition: NodeList.h:59
Node::getStatus
status_t getStatus()
Gets status for finite state machine that represents node.
Definition: NodeList.h:252
OTA_START_ERROR
@ OTA_START_ERROR
Definition: NodeList.h:65
OTA_FINISHED
@ OTA_FINISHED
Definition: NodeList.h:70
Node::nodeId
uint16_t nodeId
Node identifier asigned by gateway.
Definition: NodeList.h:340
Node::getNodeId
uint16_t getNodeId()
Gets Node identifier.
Definition: NodeList.h:120
NodeList::unregisterNode
bool unregisterNode(uint16_t nodeId)
Frees up a node and marks it as available.
Definition: NodeList.cpp:220
Node::getLastMessageTime
time_t getLastMessageTime()
Gets last time that node sent a message.
Definition: NodeList.h:187
node_instance::nodeId
uint16_t nodeId
Node identifier asigned by gateway.
Definition: NodeList.h:78
Node::setKeyValidFrom
void setKeyValidFrom(time_t keyValidFrom)
Sets time when key was agreed with gateway.
Definition: NodeList.h:179
INIT
@ INIT
Definition: NodeList.h:25
node_instance::sleepyNode
bool sleepyNode
Node sleepy definition.
Definition: NodeList.h:85
RSSI_ANS
@ RSSI_ANS
Definition: NodeList.h:51
Node::setMacAddress
void setMacAddress(uint8_t *macAddress)
Sets node address.
Definition: NodeList.h:218
Node::keyValidFrom
timer_t keyValidFrom
Last time that Node and Gateway agreed a key.
Definition: NodeList.h:341
SLEEP_GET
@ SLEEP_GET
Definition: NodeList.h:44
NAME_OK
@ NAME_OK
Definition: NodeList.h:33
Node::t3
int64_t t3
Definition: NodeList.h:333
Node::setSleepy
void setSleepy(bool sleepy)
Sets node working mode regarding battery saving strategy. If node is sleepy it will turn into deep sl...
Definition: NodeList.h:286
Node::t2
int64_t t2
Definition: NodeList.h:333
Node::initRateFilter
void initRateFilter()
Starts smoothing filter.
Definition: NodeList.cpp:66
WAIT_FOR_SERVER_HELLO
@ WAIT_FOR_SERVER_HELLO
Definition: NodeList.h:26
Node::reset
void reset()
Resets all node fields to a default initial and not registered state.
Definition: NodeList.cpp:104
Node::getInitAsSleepy
bool getInitAsSleepy()
Gets initial sleepy mode.
Definition: NodeList.h:306
Node::setEncryptionKey
void setEncryptionKey(const uint8_t *key)
Sets encryption key.
Definition: NodeList.cpp:11
Node::status
status_t status
Current node status. See enum node_status
Definition: NodeList.h:338
Node::packetsHour
double packetsHour
Packet rate ffor a specific nope.
Definition: NodeList.h:332
control_message_type
control_message_type
Definition: NodeList.h:41
NodeList::getNextActiveNode
Node * getNextActiveNode(uint16_t nodeId)
Gets next active node by nodeId.
Definition: NodeList.cpp:257
Node::getEncriptionKey
uint8_t * getEncriptionKey()
Gets Node encryption key.
Definition: NodeList.h:157
USERDATA_GET
@ USERDATA_GET
Definition: NodeList.h:57
NUM_NODES
static const int NUM_NODES
Maximum number of nodes that this gateway can handle.
Definition: EnigmaIoTconfig.h:30
ota_status_t
enum ota_status ota_status_t
FilterClass
Definition: Filter.h:30
node_instance
Struct that define node fields. Used for long term storage needs.
Definition: NodeList.h:76
Node::key
uint8_t key[KEY_LENGTH]
Shared key.
Definition: NodeList.h:345
Node::getLastMessageCounter
uint16_t getLastMessageCounter()
Gets counter for last received message from node.
Definition: NodeList.h:202
WAIT_FOR_DOWNLINK
@ WAIT_FOR_DOWNLINK
Definition: NodeList.h:27
OTA_OUT_OF_SEQUENCE
@ OTA_OUT_OF_SEQUENCE
Definition: NodeList.h:68
NAME_ANS
@ NAME_ANS
Definition: NodeList.h:53
node_instance::status
status_t status
Node state.
Definition: NodeList.h:83
Node::mac
uint8_t mac[ENIGMAIOT_ADDR_LEN]
Node address.
Definition: NodeList.h:344
Node::getNodeName
char * getNodeName()
Gets Node name.
Definition: NodeList.h:136
MAX_MESSAGE_LENGTH
static const uint8_t MAX_MESSAGE_LENGTH
Maximum payload size on ESP-NOW.
Definition: EnigmaIoTconfig.h:13
Node::rateFilter
FilterClass * rateFilter
Filter for message rate smoothing.
Definition: NodeList.h:347
node_instance::keyValidFrom
time_t keyValidFrom
Last time that Node and Gateway agreed a key.
Definition: NodeList.h:81
Node::updatePacketsRate
void updatePacketsRate(float value)
Adds a new message rate value for filter calculation.
Definition: NodeList.cpp:99
Node
Class definition for a single sensor Node.
Definition: NodeList.h:93
Node::queuedMessage
uint8_t queuedMessage[MAX_MESSAGE_LENGTH]
Message queued for sending to node in case of sleepy mode.
Definition: NodeList.h:325
NodeList::checkNodeName
int8_t checkNodeName(const char *name, const uint8_t *address)
Check Node name for duplicate.
Definition: NodeList.cpp:163
TOO_LONG
@ TOO_LONG
Definition: NodeList.h:36
ALREADY_USED
@ ALREADY_USED
Definition: NodeList.h:34
UNREGISTERED
@ UNREGISTERED
Definition: NodeList.h:24
NodeList
Definition: NodeList.h:359
RESET_ANS
@ RESET_ANS
Definition: NodeList.h:49
Node::isRegistered
bool isRegistered()
Gets registration state of this node.
Definition: NodeList.h:244
Node::getMacAddress
uint8_t * getMacAddress()
Gets address from Node.
Definition: NodeList.h:112
VERSION
@ VERSION
Definition: NodeList.h:42
Node::getKeyValidFrom
time_t getKeyValidFrom()
Gets last time that key was agreed with gateway.
Definition: NodeList.h:171
Node::Node
Node()
Plain constructor.
Definition: NodeList.cpp:77
OTA_ANS
@ OTA_ANS
Definition: NodeList.h:56
Node::printToSerial
void printToSerial(Stream *port=&Serial)
Dumps node data to the given stream, Serial by default. This method may be used for debugging.
Definition: NodeList.cpp:31
NAME_GET
@ NAME_GET
Definition: NodeList.h:52
REGISTERED
@ REGISTERED
Definition: NodeList.h:28
node_instance::mac
uint8_t mac[ENIGMAIOT_ADDR_LEN]
Node address.
Definition: NodeList.h:77
nodeNameStatus_t
nodeNameStatus_t
Definition: NodeList.h:32
Node::per
double per
Current packet error rate of a specific node.
Definition: NodeList.h:331
NodeList::getNodeFromID
Node * getNodeFromID(uint16_t nodeId)
Gets node that correspond with given nodeId.
Definition: NodeList.cpp:126
Node::nodeName
char nodeName[NODE_NAME_LENGTH]
Node name. Use as a human friendly name to avoid use of numeric address*‍/.
Definition: NodeList.h:348
ota_status
ota_status
Definition: NodeList.h:63
OTA_CHECK_FAIL
@ OTA_CHECK_FAIL
Definition: NodeList.h:67
NodeList::countActiveNodes
uint16_t countActiveNodes()
Gets the number of active nodes (registered or registering)
Definition: NodeList.cpp:209
Node::t1
int64_t t1
Definition: NodeList.h:333
control_message_type_t
enum control_message_type control_message_type_t
NAME_SET
@ NAME_SET
Definition: NodeList.h:54
Node::qMessageLength
size_t qMessageLength
Queued message length.
Definition: NodeList.h:326
KEY_LENGTH
const uint8_t KEY_LENGTH
Key length used by selected crypto algorythm. The only tested value is 32. Change it only if you know...
Definition: EnigmaIoTconfig.h:58
node_status
node_status
State definition for nodes.
Definition: NodeList.h:23
Node::setLastMessageCounter
void setLastMessageCounter(uint16_t counter)
Sets counter for last received message from node.
Definition: NodeList.h:210
SLEEP_ANS
@ SLEEP_ANS
Definition: NodeList.h:46
OTA_STARTED
@ OTA_STARTED
Definition: NodeList.h:64
Filter.h
Filter to process message rate or other values.