EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
EnigmaIOTNode.h
Go to the documentation of this file.
1 
9 #ifndef _ENIGMAIOTNODE_h
10 #define _ENIGMAIOTNODE_h
11 //#ifdef ESP8266
12 
13 #if defined(ARDUINO) && ARDUINO >= 100
14 #include "Arduino.h"
15 #else
16 #include "WProgram.h"
17 #endif
18 
19 #include "EnigmaIoTconfig.h"
20 #include "cryptModule.h"
21 #include "helperFunctions.h"
22 #include "Comms_hal.h"
23 #include "NodeList.h"
24 #include <cstddef>
25 #include <cstdint>
26 #include <ESPAsyncWebServer.h>
27 #include <ESPAsyncWiFiManager.h>
28 
29 #define LED_ON LOW
30 #define LED_OFF !LED_ON
31 
36  SENSOR_DATA = 0x01,
43  CONTROL_DATA = 0x03,
47  CLOCK_REQUEST = 0x05,
48  CLOCK_RESPONSE = 0x06,
49  NODE_NAME_SET = 0x07,
53  CLIENT_HELLO = 0xFF,
54  SERVER_HELLO = 0xFE,
55  INVALIDATE_KEY = 0xFB
56 };
57 
59  RAW = 0x00,
60  CAYENNELPP = 0x81,
61  PROT_BUF = 0x82,
62  MSG_PACK = 0x83,
63  BSON = 0x84,
64  CBOR = 0x85,
65  SMILE = 0x86
66 };
67 
72 };
73 
74 
79  UNKNOWN_ERROR = 0x00,
81  WRONG_DATA = 0x03,
83  KEY_EXPIRED = 0x05
84 };
85 
89 typedef struct {
90  uint32_t crc32;
91  uint8_t nodeKey[KEY_LENGTH];
92  uint16_t nodeId;
93  uint8_t channel /*= DEFAULT_CHANNEL*/;
94  uint8_t gateway[ENIGMAIOT_ADDR_LEN];
95  int8_t rssi;
96  uint8_t networkKey[KEY_LENGTH];
97  char networkName[NETWORK_NAME_LENGTH];
98  bool sleepy;
99  uint32_t sleepTime /*= 0*/;
100  char nodeName[NODE_NAME_LENGTH + 1];
101  uint8_t commErrors /*= 0*/;
103  bool nodeKeyValid /* = false*/;
104  uint8_t broadcastKey[KEY_LENGTH];
105  bool broadcastKeyValid /* = false*/;
106  bool broadcastKeyRequested /* = false*/;
107  status_t nodeRegisterStatus /*= UNREGISTERED*/;
111 } rtcmem_data_t;
112 
114 
115 #if defined ARDUINO_ARCH_ESP8266 || defined ARDUINO_ARCH_ESP32
116 #include <functional>
117 typedef std::function<void (const uint8_t* mac, const uint8_t* buf, uint8_t len, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding)> onNodeDataRx_t;
118 typedef std::function<void ()> onConnected_t;
119 typedef std::function<void (nodeInvalidateReason_t reason)> onDisconnected_t;
120 typedef std::function<void (bool status)> onWiFiManagerExit_t;
121 typedef std::function<void (void)> simpleEventHandler_t;
122 #else
123 typedef void (*onNodeDataRx_t)(const uint8_t* mac, const uint8_t* buf, uint8_t len, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding);
124 typedef void (*onConnected_t)();
125 typedef void (*onDisconnected_t)(nodeInvalidateReason_t reason);
126 typedef void (*onWiFiManagerExit_t)(bool status);
127 typedef void (*onWiFiManagerStarted_t)(void);
128 #endif
129 
135 protected:
137  bool flashBlue = false;
138  int8_t led = -1;
139  unsigned int ledOnTime;
144  bool useCounter = true;
146  bool sleepRequested = false;
147  uint64_t sleepTime;
149  uint8_t dataMessageSentLength = 0;
150  bool dataMessageSendPending = false;
152  bool dataMessageEncrypt = true;
154  bool otaRunning = false;
155  bool otaError = false;
156  bool protectOTA = false;
157  time_t lastOTAmsg;
158  boolean indentifying = false;
159  time_t identifyStart;
161  bool clockSyncEnabled = false;
162  bool shouldRestart = false;
164  bool gatewaySearchStarted = false;
165  bool requestSearchGateway = false;
166  bool requestReportRSSI = false;
167  bool configCleared = false;
168  int resetPin = -1;
169  AsyncWiFiManager* wifiManager;
174 
182  bool checkCRC (const uint8_t* buf, size_t count, uint32_t* crc);
183 
188  void startIdentifying (time_t period);
189 
193  void stopIdentifying ();
194 
199  bool loadRTCData ();
200 
205  bool loadFlashData ();
206 
212  bool saveFlashData (bool fsOpen = false);
213 
221 
225  void sendRestart ();
226 
231  bool clientHello ();
232 
237  bool clockRequest ();
238 
247  bool processClockResponse (const uint8_t* mac, const uint8_t* buf, size_t count);
248 
256  bool processServerHello (const uint8_t* mac, const uint8_t* buf, size_t count);
257 
265  nodeInvalidateReason_t processInvalidateKey (const uint8_t* mac, const uint8_t* buf, size_t count);
266 
274  bool processBroadcastKeyMessage (const uint8_t* mac, const uint8_t* buf, size_t count);
275 
285  bool dataMessage (const uint8_t* data, size_t len, dataMessageType_t dataMsgType = DATA_TYPE, bool encrypt = true, nodePayloadEncoding_t payloadEncoding = CAYENNELPP);
286 
295  bool unencryptedDataMessage (const uint8_t* data, size_t len, dataMessageType_t dataMsgType = DATA_TYPE, nodePayloadEncoding_t payloadEncoding = CAYENNELPP);
296 
304  bool processOTACommand (const uint8_t* mac, const uint8_t* data, uint8_t len);
305 
314  bool processControlCommand (const uint8_t* mac, const uint8_t* data, size_t len, bool broadcast);
315 
324  bool processDownstreamData (const uint8_t* mac, const uint8_t* buf, size_t count, bool control = false);
325 
334  void manageMessage (const uint8_t* mac, const uint8_t* buf, uint8_t count);
335 
341  void getStatus (uint8_t* mac_addr, uint8_t status);
342 
349  static void rx_cb (uint8_t* mac_addr, uint8_t* data, uint8_t len);
350 
357  static void tx_cb (uint8_t* mac_addr, uint8_t status);
358 
366  bool processGetSleepTimeCommand (const uint8_t* mac, const uint8_t* buf, uint8_t len);
367 
375  bool processSetSleepTimeCommand (const uint8_t* mac, const uint8_t* buf, uint8_t len);
376 
384  bool processSetIdentifyCommand (const uint8_t* mac, const uint8_t* buf, uint8_t len);
385 
393  bool processSetResetConfigCommand (const uint8_t* mac, const uint8_t* buf, uint8_t len);
394 
402  bool processSetRestartCommand (const uint8_t* mac, const uint8_t* buf, uint8_t len);
403 
411  bool processVersionCommand (const uint8_t* mac, const uint8_t* buf, uint8_t len);
412 
422  bool sendData (const uint8_t* data, size_t len, dataMessageType_t dataMsgType, bool encrypt = true, nodePayloadEncoding_t payloadEncoding = CAYENNELPP);
423 
430  bool searchForGateway (rtcmem_data_t* data, bool shouldStoreData = false);
431 
435  void clearRTC ();
436 
440  void clearFlash ();
441 
446  bool saveRTCData ();
447 
451  void checkResetButton ();
452 
457  bool reportRSSI ();
458 
466  bool processGetRSSICommand (const uint8_t* mac, const uint8_t* data, uint8_t len);
467 
475  bool processGetNameCommand (const uint8_t* mac, const uint8_t* data, uint8_t len);
476 
484  bool processSetNameResponse (const uint8_t* mac, const uint8_t* data, uint8_t len);
485 
493  bool processSetNameCommand (const uint8_t* mac, const uint8_t* data, uint8_t len);
494 
500  bool sendNodeNameSet (const char* name);
501 
502 public:
516  void begin (Comms_halClass* comm, uint8_t* gateway = NULL, uint8_t* networkKey = NULL, bool useCounter = true, bool sleepy = true);
517 
521  void stop ();
522 
528  void restart (restartReason_t reason, bool reboot = true);
529 
536  void setSleepTime (uint32_t sleepTime, bool forceSleepForever = false);
537 
543  bool setNodeAddress (uint8_t address[ENIGMAIOT_ADDR_LEN]);
544 
549  uint32_t getSleepTime ();
550 
558  return node.broadcastIsEnabled ();
559  }
560 
566  void enableBroadcast (bool broadcast = true) {
567 #ifndef DISABLE_BRCAST
568  node.enableBroadcast (broadcast);
570  rtcmem_data.broadcastKeyRequested = false; // Key is not requested yet
571 #endif
572  }
573 
578  void handle ();
579 
584  void enableClockSync (bool clockSync = true) {
585  setenv ("TZ", TZINFO, 1);
586  tzset ();
587  clockSyncEnabled = clockSync;
588  }
589 
595  void setLed (uint8_t led, time_t onTime = FLASH_LED_TIME);
596 
601  void setResetPin (int pin);
602 
609  bool sendData (const uint8_t* data, size_t len, nodePayloadEncoding_t payloadEncoding = CAYENNELPP) {
610  return sendData (data, len, DATA_TYPE, true, payloadEncoding);
611  }
612 
619  bool sendHADiscoveryMessage (const uint8_t* data, size_t len);
620 
621 
628  bool sendUnencryptedData (const uint8_t* data, size_t len, nodePayloadEncoding_t payloadEncoding = CAYENNELPP) {
629  return sendData (data, len, DATA_TYPE, false, payloadEncoding);
630  }
631 
655  void onDataRx (onNodeDataRx_t handler) {
656  notifyData = handler;
657  }
658 
682  void onConnected (onConnected_t handler) {
683  notifyConnection = handler;
684  }
685 
712  notifyDisconnection = handler;
713  }
714 
721  }
722 
729  }
730 
735  void addWiFiManagerParameter (AsyncWiFiManagerParameter* p) {
736  if (wifiManager) {
737  wifiManager->addParameter (p);
738  }
739  }
740 
746  void sleep ();
747 
752  int64_t clock ();
753 
758  time_t unixtime ();
759 
764  bool hasClockSync ();
765 
770  bool isRegistered () {
771  return node.isRegistered ();
772  }
773 
778  int8_t getRSSI ();
779 
783  void resetConfig ();
784 
789  bool getOTArunning () {
790  return otaRunning;
791  }
792 
797  Node* getNode () {
798  return &node;
799  }
800 
801 };
802 
804 
805 //#endif // ESP8266
806 #endif // _ENIGMAIOTNODE_h
EnigmaIOTNodeClass::otaRunning
bool otaRunning
True if OTA update has started.
Definition: EnigmaIOTNode.h:154
EnigmaIOTNodeClass::notifyWiFiManagerExit
onWiFiManagerExit_t notifyWiFiManagerExit
Function called when configuration portal exits.
Definition: EnigmaIOTNode.h:170
EnigmaIOTNodeClass::otaError
bool otaError
True if OTA update has failed. This normally produces a restart.
Definition: EnigmaIOTNode.h:155
EnigmaIOTNodeClass::notifyData
onNodeDataRx_t notifyData
Callback that will be called on every message reception.
Definition: EnigmaIOTNode.h:141
WRONG_CLIENT_HELLO
@ WRONG_CLIENT_HELLO
Definition: EnigmaIOTNode.h:80
EnigmaIOTNodeClass::unixtime
time_t unixtime()
Gets current time in seconds from 1970, if time is synchronized.
Definition: EnigmaIOTNode.cpp:1460
onDisconnected_t
void(* onDisconnected_t)(nodeInvalidateReason_t reason)
Definition: EnigmaIOTNode.h:125
UNENCRYPTED_NODE_DATA
@ UNENCRYPTED_NODE_DATA
Definition: EnigmaIOTNode.h:38
EnigmaIOTNodeClass::protectOTA
bool protectOTA
True if OTA update was launched. OTA flag is stored on RTC so this disables writting.
Definition: EnigmaIOTNode.h:156
nodeMessageType_t
nodeMessageType nodeMessageType_t
Definition: EnigmaIOTNode.h:113
rtcmem_data_t::nodeId
uint16_t nodeId
Definition: EnigmaIOTNode.h:92
EnigmaIOTNodeClass::hasClockSync
bool hasClockSync()
Checks if internal clock is synchronized to gateway.
Definition: EnigmaIOTNode.cpp:1464
EnigmaIOTNodeClass::enableClockSync
void enableClockSync(bool clockSync=true)
Controls clock synchronization function.
Definition: EnigmaIOTNode.h:584
ENIGMAIOT_ADDR_LEN
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
Definition: EnigmaIoTconfigAdvanced.h:23
CONTROL_DATA
@ CONTROL_DATA
Definition: EnigmaIOTNode.h:43
SERVER_HELLO
@ SERVER_HELLO
Definition: EnigmaIOTNode.h:54
cryptModule.h
Crypto library that implements EnigmaIoT encryption, decryption and key agreement fuctions.
NodeList.h
EnigmaIoT sensor node management structures.
NODE_NAME_RESULT
@ NODE_NAME_RESULT
Definition: EnigmaIOTNode.h:50
EnigmaIOTNodeClass::clientHello
bool clientHello()
Build a ClientHello messange and send it to gateway.
Definition: EnigmaIOTNode.cpp:1192
nodeMessageType
nodeMessageType
Message code definition.
Definition: EnigmaIOTNode.h:35
MAX_MESSAGE_LENGTH
static const uint8_t MAX_MESSAGE_LENGTH
Maximum payload size on ESP-NOW.
Definition: EnigmaIoTconfigAdvanced.h:21
CBOR
@ CBOR
Definition: EnigmaIOTNode.h:64
EnigmaIOTNodeClass::searchForGateway
bool searchForGateway(rtcmem_data_t *data, bool shouldStoreData=false)
Starts searching for a gateway that it using configured Network Name as WiFi AP. Stores this info for...
Definition: EnigmaIOTNode.cpp:860
NODE_NAME_LENGTH
static const uint8_t NODE_NAME_LENGTH
Maximum number of characters of node name.
Definition: EnigmaIoTconfigAdvanced.h:25
EnigmaIOTNodeClass::setResetPin
void setResetPin(int pin)
Sets a pin to be used to reset configuration it it is connected to ground during startup.
Definition: EnigmaIOTNode.cpp:94
EnigmaIOTNodeClass::led
int8_t led
IO Pin that corresponds to Tx LED. Default value disables LED. It is initialized with setLed method.
Definition: EnigmaIOTNode.h:138
RAW
@ RAW
Definition: EnigmaIOTNode.h:59
HA_DISCOVERY_MESSAGE
@ HA_DISCOVERY_MESSAGE
Definition: EnigmaIOTNode.h:45
EnigmaIOTNodeClass::checkCRC
bool checkCRC(const uint8_t *buf, size_t count, uint32_t *crc)
Check that a given CRC matches to calulated value from a buffer.
Definition: EnigmaIOTNode.cpp:1182
EnigmaIOTNodeClass::sendData
bool sendData(const uint8_t *data, size_t len, nodePayloadEncoding_t payloadEncoding=CAYENNELPP)
Starts a data message transmission.
Definition: EnigmaIOTNode.h:609
Node::broadcastIsEnabled
bool broadcastIsEnabled()
Returns if node broadcast mode is enabled. In that case, node is able to send and receive encrypted b...
Definition: NodeList.h:373
rtcmem_data_t::rssi
int8_t rssi
Definition: EnigmaIOTNode.h:95
EnigmaIOTNodeClass::rx_cb
static void rx_cb(uint8_t *mac_addr, uint8_t *data, uint8_t len)
Function that will be called anytime this node receives a message.
Definition: EnigmaIOTNode.cpp:1174
EnigmaIOTNodeClass::processOTACommand
bool processOTACommand(const uint8_t *mac, const uint8_t *data, uint8_t len)
Processes a single OTA update command or data.
Definition: EnigmaIOTNode.cpp:2181
EnigmaIOTNodeClass::dataMessage
bool dataMessage(const uint8_t *data, size_t len, dataMessageType_t dataMsgType=DATA_TYPE, bool encrypt=true, nodePayloadEncoding_t payloadEncoding=CAYENNELPP)
Builds, encrypts and sends a Data message.
Definition: EnigmaIOTNode.cpp:1635
TZINFO
#define TZINFO
Time zone.
Definition: EnigmaIoTconfig.h:20
EnigmaIOTNodeClass::setSleepTime
void setSleepTime(uint32_t sleepTime, bool forceSleepForever=false)
Allows to configure a new sleep time period from user code.
Definition: EnigmaIOTNode.cpp:948
EnigmaIOTNodeClass::processSetSleepTimeCommand
bool processSetSleepTimeCommand(const uint8_t *mac, const uint8_t *buf, uint8_t len)
Processes a request to set new sleep time configuration.
Definition: EnigmaIOTNode.cpp:2118
EnigmaIOTNodeClass::processGetNameCommand
bool processGetNameCommand(const uint8_t *mac, const uint8_t *data, uint8_t len)
Processes a request to get Node name and address.
Definition: EnigmaIOTNode.cpp:1795
EnigmaIoTconfig.h
Parameter configuration.
rtcmem_data_t::crc32
uint32_t crc32
Definition: EnigmaIOTNode.h:90
rtcmem_data_t::broadcastKeyValid
bool broadcastKeyValid
Definition: EnigmaIOTNode.h:105
EnigmaIOTNodeClass::clearRTC
void clearRTC()
Clears configuration stored in RTC memory to recover factory state.
Definition: EnigmaIOTNode.cpp:2093
EnigmaIOTNodeClass::handle
void handle()
This method should be called periodically for instance inside loop() function. It is used for interna...
Definition: EnigmaIOTNode.cpp:1003
EnigmaIOTNodeClass::sendRestart
void sendRestart()
Sends a restart notification control message.
Definition: EnigmaIOTNode.cpp:56
EnigmaIOTNode
EnigmaIOTNodeClass EnigmaIOTNode
Definition: EnigmaIOTNode.cpp:2719
SMILE
@ SMILE
Definition: EnigmaIOTNode.h:65
DOWNSTREAM_DATA_SET
@ DOWNSTREAM_DATA_SET
Definition: EnigmaIOTNode.h:39
EnigmaIOTNodeClass::checkResetButton
void checkResetButton()
Checks reset button status during startup.
Definition: EnigmaIOTNode.cpp:674
NETWORK_NAME_LENGTH
static const uint8_t NETWORK_NAME_LENGTH
Maximum number of characters of network name.
Definition: EnigmaIoTconfigAdvanced.h:24
PROT_BUF
@ PROT_BUF
Definition: EnigmaIOTNode.h:61
EnigmaIOTNodeClass::resetPin
int resetPin
Pin used to reset configuration if it is connected to ground during startup.
Definition: EnigmaIOTNode.h:168
BROADCAST_KEY_REQUEST
@ BROADCAST_KEY_REQUEST
Definition: EnigmaIOTNode.h:51
rtcmem_data_t::lastDownlinkMsgCounter
uint16_t lastDownlinkMsgCounter
Definition: EnigmaIOTNode.h:110
EnigmaIOTNodeClass::notifyWiFiManagerStarted
simpleEventHandler_t notifyWiFiManagerStarted
Function called when configuration portal is started.
Definition: EnigmaIOTNode.h:171
EnigmaIOTNodeClass::getOTArunning
bool getOTArunning()
Checks if OTA is running.
Definition: EnigmaIOTNode.h:789
EnigmaIOTNodeClass::requestReportRSSI
bool requestReportRSSI
Flag to control RSSI reporting.
Definition: EnigmaIOTNode.h:166
UNREGISTERED_NODE
@ UNREGISTERED_NODE
Definition: EnigmaIOTNode.h:82
EnigmaIOTNodeClass::restartReason
restartReason_t restartReason
Reason of restart (OTA, restart requested, configuration reset)
Definition: EnigmaIOTNode.h:163
EnigmaIOTNodeClass::indentifying
boolean indentifying
True if node has its led flashing to be identified.
Definition: EnigmaIOTNode.h:158
EnigmaIOTNodeClass::processSetNameResponse
bool processSetNameResponse(const uint8_t *mac, const uint8_t *data, uint8_t len)
Processes a response to set Node name.
Definition: EnigmaIOTNode.cpp:1829
status_t
enum node_status status_t
Node state.
Definition: NodeList.h:39
EnigmaIOTNodeClass::flashBlue
bool flashBlue
If true Tx LED will be flashed.
Definition: EnigmaIOTNode.h:137
dataMessageType_t
dataMessageType_t
Definition: EnigmaIOTNode.h:68
EnigmaIOTNodeClass::getSleepTime
uint32_t getSleepTime()
Returns sleep period in seconds.
Definition: EnigmaIOTNode.cpp:77
EnigmaIOTNodeClass::sendUnencryptedData
bool sendUnencryptedData(const uint8_t *data, size_t len, nodePayloadEncoding_t payloadEncoding=CAYENNELPP)
Starts a data message transmission.
Definition: EnigmaIOTNode.h:628
EnigmaIOTNodeClass::broadcastIsEnabled
bool broadcastIsEnabled()
Returns if node broadcast mode is enabled. In that case, node is able to send and receive encrypted b...
Definition: EnigmaIOTNode.h:557
EnigmaIOTNodeClass::comm
Comms_halClass * comm
Comms abstraction layer.
Definition: EnigmaIOTNode.h:140
BSON
@ BSON
Definition: EnigmaIOTNode.h:63
DOWNSTREAM_BRCAST_CTRL_DATA
@ DOWNSTREAM_BRCAST_CTRL_DATA
Definition: EnigmaIOTNode.h:46
EnigmaIOTNodeClass::dataMessageSent
uint8_t dataMessageSent[MAX_MESSAGE_LENGTH]
Buffer where sent message is stored in case of retransmission is needed.
Definition: EnigmaIOTNode.h:148
DATA_TYPE
@ DATA_TYPE
Definition: EnigmaIOTNode.h:69
EnigmaIOTNodeClass::processSetIdentifyCommand
bool processSetIdentifyCommand(const uint8_t *mac, const uint8_t *buf, uint8_t len)
Processes a request to start indicate to identify a node visually.
Definition: EnigmaIOTNode.cpp:2033
EnigmaIOTNodeClass::timeSyncPeriod
clock_t timeSyncPeriod
Clock synchronization period.
Definition: EnigmaIOTNode.h:160
EnigmaIOTNodeClass::clockRequest
bool clockRequest()
Build a ClockRequest messange and send it to gateway.
Definition: EnigmaIOTNode.cpp:1291
EnigmaIOTNodeClass::enableBroadcast
void enableBroadcast(bool broadcast=true)
Enables node broadcast mode. Node will request broadcast key to Gateway. When it is received node wil...
Definition: EnigmaIOTNode.h:566
MSG_PACK
@ MSG_PACK
Definition: EnigmaIOTNode.h:62
EnigmaIOTNodeClass::sleep
void sleep()
Requests transition to sleep mode (low energy state)
Definition: EnigmaIOTNode.cpp:1564
simpleEventHandler_t
void(* simpleEventHandler_t)(void)
Definition: EnigmaIOTGateway.h:101
INVALIDATE_KEY
@ INVALIDATE_KEY
Definition: EnigmaIOTNode.h:55
EnigmaIOTNodeClass::processSetResetConfigCommand
bool processSetResetConfigCommand(const uint8_t *mac, const uint8_t *buf, uint8_t len)
Processes a request to reset node configuration.
Definition: EnigmaIOTNode.cpp:2059
WRONG_DATA
@ WRONG_DATA
Definition: EnigmaIOTNode.h:81
EnigmaIOTNodeClass::tx_cb
static void tx_cb(uint8_t *mac_addr, uint8_t status)
Function that will be called anytime this node sends a message to indicate status result of sending p...
Definition: EnigmaIOTNode.cpp:1178
EnigmaIOTNodeClass::processServerHello
bool processServerHello(const uint8_t *mac, const uint8_t *buf, size_t count)
Gets a buffer containing a ServerHello message and process it. It uses that message to calculate a sh...
Definition: EnigmaIOTNode.cpp:1471
QUICK_SYNC_TIME
static const unsigned int QUICK_SYNC_TIME
Period of clock synchronization request in case of resync is needed.
Definition: EnigmaIoTconfig.h:46
EnigmaIOTNodeClass::onWiFiManagerExit
void onWiFiManagerExit(onWiFiManagerExit_t handle)
Register callback to be called on wifi manager exit.
Definition: EnigmaIOTNode.h:719
rtcmem_data_t::lastMessageCounter
uint16_t lastMessageCounter
Definition: EnigmaIOTNode.h:108
EnigmaIOTNodeClass::clearFlash
void clearFlash()
Clears configuration stored in flash to recover factory state.
Definition: EnigmaIOTNode.cpp:464
EnigmaIOTNodeClass::lastBroadcastMsgCounter
int16_t lastBroadcastMsgCounter
Counter for broadcast messages from gateway *‍/.
Definition: EnigmaIOTNode.h:173
DOWNSTREAM_BRCAST_DATA_GET
@ DOWNSTREAM_BRCAST_DATA_GET
Definition: EnigmaIOTNode.h:42
EnigmaIOTNodeClass::restart
void restart(restartReason_t reason, bool reboot=true)
Sets connection as unregistered to force a resyncrhonisation after boot.
Definition: EnigmaIOTNode.cpp:2351
EnigmaIOTNodeClass::onDisconnected
void onDisconnected(onDisconnected_t handler)
Defines a function callback that will be called everytime node is disconnected from gateway.
Definition: EnigmaIOTNode.h:711
EnigmaIOTNodeClass::configWiFiManager
bool configWiFiManager(rtcmem_data_t *data)
Starts configuration AP and web server and gets settings from it.
Definition: EnigmaIOTNode.cpp:481
EnigmaIOTNodeClass::resetConfig
void resetConfig()
Deletes configuration file stored on flash. It makes neccessary to configure it again using WiFi Port...
Definition: EnigmaIOTNode.cpp:44
EnigmaIOTNodeClass::sleepTime
uint64_t sleepTime
Time in microseconds that this node will be slept between measurements.
Definition: EnigmaIOTNode.h:147
DOWNSTREAM_DATA_GET
@ DOWNSTREAM_DATA_GET
Definition: EnigmaIOTNode.h:41
EnigmaIOTNodeClass::stopIdentifying
void stopIdentifying()
Stops node identification.
Definition: EnigmaIOTNode.cpp:669
EnigmaIOTNodeClass::dataMessageSendPending
bool dataMessageSendPending
True in case of message retransmission is needed.
Definition: EnigmaIOTNode.h:150
EnigmaIOTNodeClass::notifyConnection
onConnected_t notifyConnection
Callback that will be called anytime a new node is registered.
Definition: EnigmaIOTNode.h:142
nodePayloadEncoding_t
nodePayloadEncoding_t
Definition: EnigmaIOTNode.h:58
BROADCAST_KEY_RESPONSE
@ BROADCAST_KEY_RESPONSE
Definition: EnigmaIOTNode.h:52
restartReason_t
restartReason_t
Definition: NodeList.h:41
EnigmaIOTNodeClass::useCounter
bool useCounter
true means that data message counter will be used to mark message order
Definition: EnigmaIOTNode.h:144
EnigmaIOTNodeClass::sendData
bool sendData(const uint8_t *data, size_t len, dataMessageType_t dataMsgType, bool encrypt=true, nodePayloadEncoding_t payloadEncoding=CAYENNELPP)
Initiades data transmission distinguissing if it is payload or control data.
Definition: EnigmaIOTNode.cpp:1535
EnigmaIOTNodeClass::processControlCommand
bool processControlCommand(const uint8_t *mac, const uint8_t *data, size_t len, bool broadcast)
Processes a control command. Does not propagate to user code.
Definition: EnigmaIOTNode.cpp:2366
UNKNOWN_ERROR
@ UNKNOWN_ERROR
Definition: EnigmaIOTNode.h:79
EnigmaIOTNodeClass::onWiFiManagerStarted
void onWiFiManagerStarted(simpleEventHandler_t handle)
Register callback to be called on wifi manager start.
Definition: EnigmaIOTNode.h:727
EnigmaIOTNodeClass::processSetNameCommand
bool processSetNameCommand(const uint8_t *mac, const uint8_t *data, uint8_t len)
Processes a request to set Node name.
Definition: EnigmaIOTNode.cpp:1899
rtcmem_data_t::lastControlCounter
uint16_t lastControlCounter
Definition: EnigmaIOTNode.h:109
EnigmaIOTNodeClass::isRegistered
bool isRegistered()
Checks if node is registered.
Definition: EnigmaIOTNode.h:770
EnigmaIOTNodeClass::ledOnTime
unsigned int ledOnTime
Time that LED is On during flash. Initalized on setLed
Definition: EnigmaIOTNode.h:139
EnigmaIOTNodeClass::node
Node node
Node abstraction to store context.
Definition: EnigmaIOTNode.h:136
EnigmaIOTNodeClass::sendNodeNameSet
bool sendNodeNameSet(const char *name)
Informs Gateway about custom node name.
Definition: EnigmaIOTNode.cpp:1936
EnigmaIOTNodeClass::addWiFiManagerParameter
void addWiFiManagerParameter(AsyncWiFiManagerParameter *p)
Adds a parameter to configuration portal.
Definition: EnigmaIOTNode.h:735
EnigmaIOTNodeClass::processSetRestartCommand
bool processSetRestartCommand(const uint8_t *mac, const uint8_t *buf, uint8_t len)
Processes a request to restar node mcu.
Definition: EnigmaIOTNode.cpp:2053
EnigmaIOTNodeClass::invalidateReason
nodeInvalidateReason_t invalidateReason
Last key invalidation reason.
Definition: EnigmaIOTNode.h:153
EnigmaIOTNodeClass::rtcmem_data
rtcmem_data_t rtcmem_data
Context data to be stored on persistent storage.
Definition: EnigmaIOTNode.h:145
EnigmaIOTNodeClass::loadRTCData
bool loadRTCData()
Loads configuration from RTC data. Uses a CRC to check data integrity.
Definition: EnigmaIOTNode.cpp:221
EnigmaIOTNodeClass::clockSyncEnabled
bool clockSyncEnabled
If true clock is synchronized with Gateway.
Definition: EnigmaIOTNode.h:161
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: EnigmaIoTconfigAdvanced.h:70
EnigmaIOTNodeClass::processBroadcastKeyMessage
bool processBroadcastKeyMessage(const uint8_t *mac, const uint8_t *buf, size_t count)
Gets a buffer containing a BroadcastKey message and process it. This key is used to send and receive ...
Definition: EnigmaIOTNode.cpp:2408
EnigmaIOTNodeClass::sleepRequested
bool sleepRequested
true means that this node will sleep as soon a message is sent and downlink wait time has passed
Definition: EnigmaIOTNode.h:146
NODE_NAME_SET
@ NODE_NAME_SET
Definition: EnigmaIOTNode.h:49
EnigmaIOTNodeClass::processVersionCommand
bool processVersionCommand(const uint8_t *mac, const uint8_t *buf, uint8_t len)
Processes a request firmware version.
Definition: EnigmaIOTNode.cpp:2163
SENSOR_BRCAST_DATA
@ SENSOR_BRCAST_DATA
Definition: EnigmaIOTNode.h:37
EnigmaIOTNodeClass::processDownstreamData
bool processDownstreamData(const uint8_t *mac, const uint8_t *buf, size_t count, bool control=false)
Processes downstream data from gateway.
Definition: EnigmaIOTNode.cpp:2425
EnigmaIOTNodeClass::wifiManager
AsyncWiFiManager * wifiManager
Wifi configuration portal.
Definition: EnigmaIOTNode.h:169
EnigmaIOTNodeClass::processClockResponse
bool processClockResponse(const uint8_t *mac, const uint8_t *buf, size_t count)
Gets a buffer containing a ClockResponse message and process it. It uses that message to calculate cl...
Definition: EnigmaIOTNode.cpp:1367
EnigmaIOTNodeClass::sendHADiscoveryMessage
bool sendHADiscoveryMessage(const uint8_t *data, size_t len)
Builds, encrypts and sends a HomeAssistant discovery message.
Definition: EnigmaIOTNode.cpp:1764
rtcmem_data_t::broadcastKeyRequested
bool broadcastKeyRequested
Definition: EnigmaIOTNode.h:106
EnigmaIOTNodeClass::dataMessageEncrypt
bool dataMessageEncrypt
Message encryption enabled. Stored for use in case of message retransmission is needed.
Definition: EnigmaIOTNode.h:152
EnigmaIOTNodeClass::onConnected
void onConnected(onConnected_t handler)
Defines a function callback that will be called everytime node is registered on gateway.
Definition: EnigmaIOTNode.h:682
Node
Class definition for a single sensor Node.
Definition: NodeList.h:109
CLOCK_REQUEST
@ CLOCK_REQUEST
Definition: EnigmaIOTNode.h:47
EnigmaIOTNodeClass::lastOTAmsg
time_t lastOTAmsg
Time when last OTA update message has received. This is used to control timeout.
Definition: EnigmaIOTNode.h:157
KEY_EXPIRED
@ KEY_EXPIRED
Definition: EnigmaIOTNode.h:83
EnigmaIOTNodeClass::begin
void begin(Comms_halClass *comm, uint8_t *gateway=NULL, uint8_t *networkKey=NULL, bool useCounter=true, bool sleepy=true)
Initalizes communication basic data and starts node registration.
Definition: EnigmaIOTNode.cpp:696
Node::enableBroadcast
void enableBroadcast(bool broadcast)
Enables node broadcast mode. Node will request broadcast key to Gateway. When it is received node wil...
Definition: NodeList.h:386
EnigmaIOTNodeClass::notifyDisconnection
onDisconnected_t notifyDisconnection
Callback that will be called anytime a node is disconnected.
Definition: EnigmaIOTNode.h:143
Node::isRegistered
bool isRegistered()
Gets registration state of this node.
Definition: NodeList.h:292
EnigmaIOTNodeClass::identifyStart
time_t identifyStart
Time when identification started flashing. Used to control identification timeout.
Definition: EnigmaIOTNode.h:159
EnigmaIOTNodeClass::processInvalidateKey
nodeInvalidateReason_t processInvalidateKey(const uint8_t *mac, const uint8_t *buf, size_t count)
Gets a buffer containing an InvalidateKey message and process it. This trigger a new key agreement to...
Definition: EnigmaIOTNode.cpp:2527
SENSOR_DATA
@ SENSOR_DATA
Definition: EnigmaIOTNode.h:36
EnigmaIOTNodeClass::setLed
void setLed(uint8_t led, time_t onTime=FLASH_LED_TIME)
Sets a LED to be flashed every time a message is transmitted.
Definition: EnigmaIOTNode.cpp:89
EnigmaIOTNodeClass::saveRTCData
bool saveRTCData()
Save configuration to RTC to store current status and recover it after deep sleep.
Definition: EnigmaIOTNode.cpp:433
EnigmaIOTNodeClass::loadFlashData
bool loadFlashData()
Loads configuration from flash memory.
Definition: EnigmaIOTNode.cpp:270
EnigmaIOTNodeClass::unencryptedDataMessage
bool unencryptedDataMessage(const uint8_t *data, size_t len, dataMessageType_t dataMsgType=DATA_TYPE, nodePayloadEncoding_t payloadEncoding=CAYENNELPP)
Builds and sends a Data message without encryption. Not recommended, use it only if you absolutely ne...
Definition: EnigmaIOTNode.cpp:1574
data
@ data
Definition: GwOutput_generic.h:23
CLOCK_RESPONSE
@ CLOCK_RESPONSE
Definition: EnigmaIOTNode.h:48
DOWNSTREAM_CTRL_DATA
@ DOWNSTREAM_CTRL_DATA
Definition: EnigmaIOTNode.h:44
EnigmaIOTNodeClass::reportRSSI
bool reportRSSI()
Sends RSSI value and channel to Gateway.
Definition: EnigmaIOTNode.cpp:982
EnigmaIOTNodeClass::shouldRestart
bool shouldRestart
Triggers a restart if true.
Definition: EnigmaIOTNode.h:162
EnigmaIOTNodeClass::getStatus
void getStatus(uint8_t *mac_addr, uint8_t status)
Functrion to debug send status.
Definition: EnigmaIOTNode.cpp:2704
CLIENT_HELLO
@ CLIENT_HELLO
Definition: EnigmaIOTNode.h:53
EnigmaIOTNodeClass
Main node class. Manages communication with gateway and allows sending and receiving user data.
Definition: EnigmaIOTNode.h:134
EnigmaIOTNodeClass::processGetSleepTimeCommand
bool processGetSleepTimeCommand(const uint8_t *mac, const uint8_t *buf, uint8_t len)
Processes a request of sleep time configuration.
Definition: EnigmaIOTNode.cpp:1772
CAYENNELPP
@ CAYENNELPP
Definition: EnigmaIOTNode.h:60
helperFunctions.h
Auxiliary function definition.
EnigmaIOTNodeClass::stop
void stop()
Stops EnigmaIoT protocol.
Definition: EnigmaIOTNode.cpp:938
EnigmaIOTNodeClass::processGetRSSICommand
bool processGetRSSICommand(const uint8_t *mac, const uint8_t *data, uint8_t len)
Processes a request to measure RSSI.
Definition: EnigmaIOTNode.cpp:2046
EnigmaIOTNodeClass::setNodeAddress
bool setNodeAddress(uint8_t address[ENIGMAIOT_ADDR_LEN])
Set node address to be used in EnigmaIOT communication.
Definition: EnigmaIOTNode.cpp:943
EnigmaIOTNodeClass::requestSearchGateway
bool requestSearchGateway
Flag to control updating gateway address, RSSI and channel.
Definition: EnigmaIOTNode.h:165
nodeInvalidateReason_t
nodeInvalidateReason_t
Key invalidation reason definition.
Definition: EnigmaIOTNode.h:78
EnigmaIOTNodeClass::saveFlashData
bool saveFlashData(bool fsOpen=false)
Saves configuration to flash memory.
Definition: EnigmaIOTNode.cpp:352
onWiFiManagerExit_t
void(* onWiFiManagerExit_t)(bool status)
Definition: EnigmaIOTNode.h:126
rtcmem_data_t::sleepy
bool sleepy
Definition: EnigmaIOTNode.h:98
FLASH_LED_TIME
static const uint32_t FLASH_LED_TIME
Time that led keeps on during flash in ms.
Definition: EnigmaIoTconfig.h:18
EnigmaIOTNodeClass::clock
int64_t clock()
Gets current clock counter. millis() + offset.
Definition: EnigmaIOTNode.cpp:1455
EnigmaIOTNodeClass::dataMessageSendEncoding
nodePayloadEncoding_t dataMessageSendEncoding
Encoding of the message pending to be sent.
Definition: EnigmaIOTNode.h:151
EnigmaIOTNodeClass::manageMessage
void manageMessage(const uint8_t *mac, const uint8_t *buf, uint8_t count)
Process every received message.
Definition: EnigmaIOTNode.cpp:2551
onNodeDataRx_t
void(* onNodeDataRx_t)(const uint8_t *mac, const uint8_t *buf, uint8_t len, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding)
Definition: EnigmaIOTNode.h:123
EnigmaIOTNodeClass::cycleStartedTime
time_t cycleStartedTime
Used to calculate exact sleep time by substracting awake time.
Definition: EnigmaIOTNode.h:172
HA_DISC_TYPE
@ HA_DISC_TYPE
Definition: EnigmaIOTNode.h:71
Comms_hal.h
Generic communication system abstraction layer.
EnigmaIOTNodeClass::gatewaySearchStarted
bool gatewaySearchStarted
Avoids start a new gateway scan if it already started.
Definition: EnigmaIOTNode.h:164
EnigmaIOTNodeClass::configCleared
bool configCleared
This flag disables asy configuration save after triggering a factory reset.
Definition: EnigmaIOTNode.h:167
rtcmem_data_t
Context data to be stored con persistent storage to be used after wake from sleep mode.
Definition: EnigmaIOTNode.h:89
onWiFiManagerStarted_t
void(* onWiFiManagerStarted_t)(void)
Definition: EnigmaIOTNode.h:127
CONTROL_TYPE
@ CONTROL_TYPE
Definition: EnigmaIOTNode.h:70
status
@ status
Definition: GwOutput_generic.h:25
EnigmaIOTNodeClass::getRSSI
int8_t getRSSI()
Gets latest RSSI measurement. It is updated during start up or in case of transmission errors.
Definition: EnigmaIOTNode.cpp:85
EnigmaIOTNodeClass::startIdentifying
void startIdentifying(time_t period)
Starts node identification by flashing led.
Definition: EnigmaIOTNode.cpp:663
EnigmaIOTNodeClass::getNode
Node * getNode()
Gets Node instance.
Definition: EnigmaIOTNode.h:797
Comms_halClass
Interface for communication subsystem abstraction layer definition.
Definition: Comms_hal.h:41
EnigmaIOTNodeClass::dataMessageSentLength
uint8_t dataMessageSentLength
Message length stored for use in case of message retransmission is needed.
Definition: EnigmaIOTNode.h:149
DOWNSTREAM_BRCAST_DATA_SET
@ DOWNSTREAM_BRCAST_DATA_SET
Definition: EnigmaIOTNode.h:40
onConnected_t
void(* onConnected_t)()
Definition: EnigmaIOTNode.h:124
EnigmaIOTNodeClass::onDataRx
void onDataRx(onNodeDataRx_t handler)
Defines a function callback that will be called on every downlink data message that is received from ...
Definition: EnigmaIOTNode.h:655