EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
EnigmaIOTNodeClass Class Reference

Main node class. Manages communication with gateway and allows sending and receiving user data. More...

#include <EnigmaIOTNode.h>

Public Member Functions

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. More...
 
void stop ()
 Stops EnigmaIoT protocol. More...
 
void restart (restartReason_t reason, bool reboot=true)
 Sets connection as unregistered to force a resyncrhonisation after boot. More...
 
void setSleepTime (uint32_t sleepTime, bool forceSleepForever=false)
 Allows to configure a new sleep time period from user code. More...
 
bool setNodeAddress (uint8_t address[ENIGMAIOT_ADDR_LEN])
 Set node address to be used in EnigmaIOT communication. More...
 
uint32_t getSleepTime ()
 Returns sleep period in seconds. More...
 
bool broadcastIsEnabled ()
 Returns if node broadcast mode is enabled. In that case, node is able to send and receive encrypted broadcast messages. If this is enabled this will be notified to gateway so that it sends broadcast key. Notice this mode is optional and does not disable the ability to send normal messages. More...
 
void enableBroadcast (bool broadcast=true)
 Enables node broadcast mode. Node will request broadcast key to Gateway. When it is received node will be able to send and receive encrypted broadcast messages. More...
 
void handle ()
 This method should be called periodically for instance inside loop() function. It is used for internal node maintenance tasks. More...
 
void enableClockSync (bool clockSync=true)
 Controls clock synchronization function. More...
 
void setLed (uint8_t led, time_t onTime=FLASH_LED_TIME)
 Sets a LED to be flashed every time a message is transmitted. More...
 
void setResetPin (int pin)
 Sets a pin to be used to reset configuration it it is connected to ground during startup. More...
 
bool sendData (const uint8_t *data, size_t len, nodePayloadEncoding_t payloadEncoding=CAYENNELPP)
 Starts a data message transmission. More...
 
bool sendHADiscoveryMessage (const uint8_t *data, size_t len)
 Builds, encrypts and sends a HomeAssistant discovery message. More...
 
bool sendUnencryptedData (const uint8_t *data, size_t len, nodePayloadEncoding_t payloadEncoding=CAYENNELPP)
 Starts a data message transmission. More...
 
void onDataRx (onNodeDataRx_t handler)
 Defines a function callback that will be called on every downlink data message that is received from gateway. More...
 
void onConnected (onConnected_t handler)
 Defines a function callback that will be called everytime node is registered on gateway. More...
 
void onDisconnected (onDisconnected_t handler)
 Defines a function callback that will be called everytime node is disconnected from gateway. More...
 
void onWiFiManagerExit (onWiFiManagerExit_t handle)
 Register callback to be called on wifi manager exit. More...
 
void onWiFiManagerStarted (simpleEventHandler_t handle)
 Register callback to be called on wifi manager start. More...
 
void addWiFiManagerParameter (AsyncWiFiManagerParameter *p)
 Adds a parameter to configuration portal. More...
 
void sleep ()
 Requests transition to sleep mode (low energy state) More...
 
int64_t clock ()
 Gets current clock counter. millis() + offset. More...
 
time_t unixtime ()
 Gets current time in seconds from 1970, if time is synchronized. More...
 
bool hasClockSync ()
 Checks if internal clock is synchronized to gateway. More...
 
bool isRegistered ()
 Checks if node is registered. More...
 
int8_t getRSSI ()
 Gets latest RSSI measurement. It is updated during start up or in case of transmission errors. More...
 
void resetConfig ()
 Deletes configuration file stored on flash. It makes neccessary to configure it again using WiFi Portal. More...
 
bool getOTArunning ()
 Checks if OTA is running. More...
 
NodegetNode ()
 Gets Node instance. More...
 

Protected Member Functions

bool checkCRC (const uint8_t *buf, size_t count, uint32_t *crc)
 Check that a given CRC matches to calulated value from a buffer. More...
 
void startIdentifying (time_t period)
 Starts node identification by flashing led. More...
 
void stopIdentifying ()
 Stops node identification. More...
 
bool loadRTCData ()
 Loads configuration from RTC data. Uses a CRC to check data integrity. More...
 
bool loadFlashData ()
 Loads configuration from flash memory. More...
 
bool saveFlashData (bool fsOpen=false)
 Saves configuration to flash memory. More...
 
bool configWiFiManager (rtcmem_data_t *data)
 Starts configuration AP and web server and gets settings from it. More...
 
void sendRestart ()
 Sends a restart notification control message. More...
 
bool clientHello ()
 Build a ClientHello messange and send it to gateway. More...
 
bool clockRequest ()
 Build a ClockRequest messange and send it to gateway. More...
 
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 clock difference against gateway and synchronize to it. More...
 
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 shared key using Diffie Hellman algorithm. More...
 
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 start. More...
 
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 broadcast messages. More...
 
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. More...
 
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 need more performance. More...
 
bool processOTACommand (const uint8_t *mac, const uint8_t *data, uint8_t len)
 Processes a single OTA update command or data. More...
 
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. More...
 
bool processDownstreamData (const uint8_t *mac, const uint8_t *buf, size_t count, bool control=false)
 Processes downstream data from gateway. More...
 
void manageMessage (const uint8_t *mac, const uint8_t *buf, uint8_t count)
 Process every received message. More...
 
void getStatus (uint8_t *mac_addr, uint8_t status)
 Functrion to debug send status. More...
 
bool processGetSleepTimeCommand (const uint8_t *mac, const uint8_t *buf, uint8_t len)
 Processes a request of sleep time configuration. More...
 
bool processSetSleepTimeCommand (const uint8_t *mac, const uint8_t *buf, uint8_t len)
 Processes a request to set new sleep time configuration. More...
 
bool processSetIdentifyCommand (const uint8_t *mac, const uint8_t *buf, uint8_t len)
 Processes a request to start indicate to identify a node visually. More...
 
bool processSetResetConfigCommand (const uint8_t *mac, const uint8_t *buf, uint8_t len)
 Processes a request to reset node configuration. More...
 
bool processSetRestartCommand (const uint8_t *mac, const uint8_t *buf, uint8_t len)
 Processes a request to restar node mcu. More...
 
bool processVersionCommand (const uint8_t *mac, const uint8_t *buf, uint8_t len)
 Processes a request firmware version. More...
 
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. More...
 
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 subsequent use. More...
 
void clearRTC ()
 Clears configuration stored in RTC memory to recover factory state. More...
 
void clearFlash ()
 Clears configuration stored in flash to recover factory state. More...
 
bool saveRTCData ()
 Save configuration to RTC to store current status and recover it after deep sleep. More...
 
void checkResetButton ()
 Checks reset button status during startup. More...
 
bool reportRSSI ()
 Sends RSSI value and channel to Gateway. More...
 
bool processGetRSSICommand (const uint8_t *mac, const uint8_t *data, uint8_t len)
 Processes a request to measure RSSI. More...
 
bool processGetNameCommand (const uint8_t *mac, const uint8_t *data, uint8_t len)
 Processes a request to get Node name and address. More...
 
bool processSetNameResponse (const uint8_t *mac, const uint8_t *data, uint8_t len)
 Processes a response to set Node name. More...
 
bool processSetNameCommand (const uint8_t *mac, const uint8_t *data, uint8_t len)
 Processes a request to set Node name. More...
 
bool sendNodeNameSet (const char *name)
 Informs Gateway about custom node name. More...
 

Static Protected Member Functions

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. More...
 
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 process. More...
 

Protected Attributes

Node node
 Node abstraction to store context. More...
 
bool flashBlue = false
 If true Tx LED will be flashed. More...
 
int8_t led = -1
 IO Pin that corresponds to Tx LED. Default value disables LED. It is initialized with setLed method. More...
 
unsigned int ledOnTime
 Time that LED is On during flash. Initalized on setLed More...
 
Comms_halClasscomm
 Comms abstraction layer. More...
 
onNodeDataRx_t notifyData
 Callback that will be called on every message reception. More...
 
onConnected_t notifyConnection
 Callback that will be called anytime a new node is registered. More...
 
onDisconnected_t notifyDisconnection
 Callback that will be called anytime a node is disconnected. More...
 
bool useCounter = true
 true means that data message counter will be used to mark message order More...
 
rtcmem_data_t rtcmem_data
 Context data to be stored on persistent storage. More...
 
bool sleepRequested = false
 true means that this node will sleep as soon a message is sent and downlink wait time has passed More...
 
uint64_t sleepTime
 Time in microseconds that this node will be slept between measurements. More...
 
uint8_t dataMessageSent [MAX_MESSAGE_LENGTH]
 Buffer where sent message is stored in case of retransmission is needed. More...
 
uint8_t dataMessageSentLength = 0
 Message length stored for use in case of message retransmission is needed. More...
 
bool dataMessageSendPending = false
 True in case of message retransmission is needed. More...
 
nodePayloadEncoding_t dataMessageSendEncoding = RAW
 Encoding of the message pending to be sent. More...
 
bool dataMessageEncrypt = true
 Message encryption enabled. Stored for use in case of message retransmission is needed. More...
 
nodeInvalidateReason_t invalidateReason = UNKNOWN_ERROR
 Last key invalidation reason. More...
 
bool otaRunning = false
 True if OTA update has started. More...
 
bool otaError = false
 True if OTA update has failed. This normally produces a restart. More...
 
bool protectOTA = false
 True if OTA update was launched. OTA flag is stored on RTC so this disables writting. More...
 
time_t lastOTAmsg
 Time when last OTA update message has received. This is used to control timeout. More...
 
boolean indentifying = false
 True if node has its led flashing to be identified. More...
 
time_t identifyStart
 Time when identification started flashing. Used to control identification timeout. More...
 
clock_t timeSyncPeriod = QUICK_SYNC_TIME
 Clock synchronization period. More...
 
bool clockSyncEnabled = false
 If true clock is synchronized with Gateway. More...
 
bool shouldRestart = false
 Triggers a restart if true. More...
 
restartReason_t restartReason
 Reason of restart (OTA, restart requested, configuration reset) More...
 
bool gatewaySearchStarted = false
 Avoids start a new gateway scan if it already started. More...
 
bool requestSearchGateway = false
 Flag to control updating gateway address, RSSI and channel. More...
 
bool requestReportRSSI = false
 Flag to control RSSI reporting. More...
 
bool configCleared = false
 This flag disables asy configuration save after triggering a factory reset. More...
 
int resetPin = -1
 Pin used to reset configuration if it is connected to ground during startup. More...
 
AsyncWiFiManager * wifiManager
 Wifi configuration portal. More...
 
onWiFiManagerExit_t notifyWiFiManagerExit
 Function called when configuration portal exits. More...
 
simpleEventHandler_t notifyWiFiManagerStarted
 Function called when configuration portal is started. More...
 
time_t cycleStartedTime
 Used to calculate exact sleep time by substracting awake time. More...
 
int16_t lastBroadcastMsgCounter
 Counter for broadcast messages from gateway *‍/. More...
 

Detailed Description

Main node class. Manages communication with gateway and allows sending and receiving user data.

Definition at line 134 of file EnigmaIOTNode.h.

Member Function Documentation

◆ addWiFiManagerParameter()

void EnigmaIOTNodeClass::addWiFiManagerParameter ( AsyncWiFiManagerParameter *  p)
inline

Adds a parameter to configuration portal.

Parameters
pConfiguration parameter

Definition at line 735 of file EnigmaIOTNode.h.

◆ begin()

void EnigmaIOTNodeClass::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.

Parameters
commPhysical layer to be used on this node network
gatewayGateway address
networkKeyNetwork key to protect shared key agreement
useCounterIndicates if a counter has to be added to every message data to check message sequence. true by default
sleepyIndicates if this node changes to low energy mode (sleep mode) after sending a data message. true by default

This condition is signalled to Gateway during registration so that downlink messages are managed diferently. On non sleepy nodes a downlink data message can be sent on any moment as node will be always awake. But for nodes that sleep, normally those that are powered with batteries, downlink message will be queued on gateway and sent just after an uplink data message from node has been sent

Definition at line 696 of file EnigmaIOTNode.cpp.

◆ broadcastIsEnabled()

bool EnigmaIOTNodeClass::broadcastIsEnabled ( )
inline

Returns if node broadcast mode is enabled. In that case, node is able to send and receive encrypted broadcast messages. If this is enabled this will be notified to gateway so that it sends broadcast key. Notice this mode is optional and does not disable the ability to send normal messages.

Returns
true if node has broadcast mode enabled.

Definition at line 557 of file EnigmaIOTNode.h.

◆ checkCRC()

bool EnigmaIOTNodeClass::checkCRC ( const uint8_t *  buf,
size_t  count,
uint32_t *  crc 
)
protected

Check that a given CRC matches to calulated value from a buffer.

Parameters
bufPointer to the buffer that contains the stream to calculate CRC
countBuffer length in number of bytes
crcReceived CRC to check against calculation
Returns
Returns true if CRC check was successful. false otherwise

Definition at line 1182 of file EnigmaIOTNode.cpp.

◆ checkResetButton()

void EnigmaIOTNodeClass::checkResetButton ( )
protected

Checks reset button status during startup.

Definition at line 674 of file EnigmaIOTNode.cpp.

◆ clearFlash()

void EnigmaIOTNodeClass::clearFlash ( )
protected

Clears configuration stored in flash to recover factory state.

Definition at line 464 of file EnigmaIOTNode.cpp.

◆ clearRTC()

void EnigmaIOTNodeClass::clearRTC ( )
protected

Clears configuration stored in RTC memory to recover factory state.

Definition at line 2093 of file EnigmaIOTNode.cpp.

◆ clientHello()

bool EnigmaIOTNodeClass::clientHello ( )
protected

Build a ClientHello messange and send it to gateway.

Returns
Returns true if ClientHello message was successfully sent. false otherwise

Definition at line 1192 of file EnigmaIOTNode.cpp.

◆ clock()

int64_t EnigmaIOTNodeClass::clock ( )

Gets current clock counter. millis() + offset.

Returns
Clock value in int64_t format

Definition at line 1455 of file EnigmaIOTNode.cpp.

◆ clockRequest()

bool EnigmaIOTNodeClass::clockRequest ( )
protected

Build a ClockRequest messange and send it to gateway.

Returns
Returns true if ClockRequest message was successfully sent. false otherwise

Definition at line 1291 of file EnigmaIOTNode.cpp.

◆ configWiFiManager()

bool EnigmaIOTNodeClass::configWiFiManager ( rtcmem_data_t data)
protected

Starts configuration AP and web server and gets settings from it.

Parameters
dataPointer to configuration data to be stored on RTC memory to keep status along sleep cycles
Returns
Returns true if data was been correctly configured. false otherwise

Definition at line 481 of file EnigmaIOTNode.cpp.

◆ dataMessage()

bool EnigmaIOTNodeClass::dataMessage ( const uint8_t *  data,
size_t  len,
dataMessageType_t  dataMsgType = DATA_TYPE,
bool  encrypt = true,
nodePayloadEncoding_t  payloadEncoding = CAYENNELPP 
)
protected

Builds, encrypts and sends a Data message.

Parameters
dataBuffer to store payload to be sent
lenLength of payload data
dataMsgTypeSignals if this message is a special EnigmaIoT message or that should not be passed to higher layers
payloadEncodingDetermine payload data encoding as nodePayloadEncoding_t. It can be RAW, CAYENNELPP, MSGPACK
encryptIndicates if message should be encrypted. True by default. Not recommended to set to false, use it only if you absolutely need more performance
Returns
Returns true if message could be correcly sent

Definition at line 1635 of file EnigmaIOTNode.cpp.

◆ enableBroadcast()

void EnigmaIOTNodeClass::enableBroadcast ( bool  broadcast = true)
inline

Enables node broadcast mode. Node will request broadcast key to Gateway. When it is received node will be able to send and receive encrypted broadcast messages.

Parameters
broadcasttrue to enable broadcast mode on this node.

Definition at line 566 of file EnigmaIOTNode.h.

◆ enableClockSync()

void EnigmaIOTNodeClass::enableClockSync ( bool  clockSync = true)
inline

Controls clock synchronization function.

Parameters
clockSyncIf true clock will be synchronized with gateway

Definition at line 584 of file EnigmaIOTNode.h.

◆ getNode()

Node* EnigmaIOTNodeClass::getNode ( )
inline

Gets Node instance.

Returns
Node instance pointer

Definition at line 797 of file EnigmaIOTNode.h.

◆ getOTArunning()

bool EnigmaIOTNodeClass::getOTArunning ( )
inline

Checks if OTA is running.

Returns
OTA running state

Definition at line 789 of file EnigmaIOTNode.h.

◆ getRSSI()

int8_t EnigmaIOTNodeClass::getRSSI ( )

Gets latest RSSI measurement. It is updated during start up or in case of transmission errors.

Returns
RSSI value

Definition at line 85 of file EnigmaIOTNode.cpp.

◆ getSleepTime()

uint32_t EnigmaIOTNodeClass::getSleepTime ( )

Returns sleep period in seconds.

Returns
Sleep period in seconds

Definition at line 77 of file EnigmaIOTNode.cpp.

◆ getStatus()

void EnigmaIOTNodeClass::getStatus ( uint8_t *  mac_addr,
uint8_t  status 
)
protected

Functrion to debug send status.

Parameters
mac_addrAddress of message sender
statusResult status code

Definition at line 2704 of file EnigmaIOTNode.cpp.

◆ handle()

void EnigmaIOTNodeClass::handle ( )

This method should be called periodically for instance inside loop() function. It is used for internal node maintenance tasks.

digitalRead (led) &&

Definition at line 1003 of file EnigmaIOTNode.cpp.

◆ hasClockSync()

bool EnigmaIOTNodeClass::hasClockSync ( )

Checks if internal clock is synchronized to gateway.

Returns
True if clock is synchronized

Definition at line 1464 of file EnigmaIOTNode.cpp.

◆ isRegistered()

bool EnigmaIOTNodeClass::isRegistered ( )
inline

Checks if node is registered.

Returns
True if node is registered

Definition at line 770 of file EnigmaIOTNode.h.

◆ loadFlashData()

bool EnigmaIOTNodeClass::loadFlashData ( )
protected

Loads configuration from flash memory.

Returns
Returns true if data was read successfuly. false otherwise

Definition at line 270 of file EnigmaIOTNode.cpp.

◆ loadRTCData()

bool EnigmaIOTNodeClass::loadRTCData ( )
protected

Loads configuration from RTC data. Uses a CRC to check data integrity.

Returns
Returns true if data is valid. false otherwise

Definition at line 221 of file EnigmaIOTNode.cpp.

◆ manageMessage()

void EnigmaIOTNodeClass::manageMessage ( const uint8_t *  mac,
const uint8_t *  buf,
uint8_t  count 
)
protected

Process every received message.

It starts clasiffying message usint the first byte. After that it passes it to the corresponding method for decoding

Parameters
macAddress of message sender
bufBuffer that stores message bytes
countLength of message in number of bytes

Definition at line 2551 of file EnigmaIOTNode.cpp.

◆ onConnected()

void EnigmaIOTNodeClass::onConnected ( onConnected_t  handler)
inline

Defines a function callback that will be called everytime node is registered on gateway.

Use example:

// First define the callback function
// Do whatever you need to process disconnecion
}
void setup () {
.....
// Now register function as data message handler
.....
}
void loop {
.....
}
Parameters
handlerPointer to the function

Definition at line 682 of file EnigmaIOTNode.h.

◆ onDataRx()

void EnigmaIOTNodeClass::onDataRx ( onNodeDataRx_t  handler)
inline

Defines a function callback that will be called on every downlink data message that is received from gateway.

Use example:

// First define the callback function
void processRxData (const uint8_t* mac, const uint8_t* buffer, uint8_t length) {
// Do whatever you need with received data
}
void setup () {
.....
// Now register function as data message handler
.....
}
void loop {
.....
}
Parameters
handlerPointer to the function

Definition at line 655 of file EnigmaIOTNode.h.

◆ onDisconnected()

void EnigmaIOTNodeClass::onDisconnected ( onDisconnected_t  handler)
inline

Defines a function callback that will be called everytime node is disconnected from gateway.

Deregistration is always started by gateway due syntax or encryption error or in case of key validity is over.

Use example:

// First define the callback function
// Do whatever you need to process disconnecion
}
void setup () {
.....
// Now register function as data message handler
.....
}
void loop {
.....
}
Parameters
handlerPointer to the function

Definition at line 711 of file EnigmaIOTNode.h.

◆ onWiFiManagerExit()

void EnigmaIOTNodeClass::onWiFiManagerExit ( onWiFiManagerExit_t  handle)
inline

Register callback to be called on wifi manager exit.

Parameters
handleCallback function pointer

Definition at line 719 of file EnigmaIOTNode.h.

◆ onWiFiManagerStarted()

void EnigmaIOTNodeClass::onWiFiManagerStarted ( simpleEventHandler_t  handle)
inline

Register callback to be called on wifi manager start.

Parameters
handleCallback function pointer

Definition at line 727 of file EnigmaIOTNode.h.

◆ processBroadcastKeyMessage()

bool EnigmaIOTNodeClass::processBroadcastKeyMessage ( const uint8_t *  mac,
const uint8_t *  buf,
size_t  count 
)
protected

Gets a buffer containing a BroadcastKey message and process it. This key is used to send and receive broadcast messages.

Parameters
macAddress where this message was received from
bufPointer to the buffer that contains the message
countMessage length in number of bytes of ServerHello message
Returns
Returns true if message could be correcly processed

Definition at line 2408 of file EnigmaIOTNode.cpp.

◆ processClockResponse()

bool EnigmaIOTNodeClass::processClockResponse ( const uint8_t *  mac,
const uint8_t *  buf,
size_t  count 
)
protected

Gets a buffer containing a ClockResponse message and process it. It uses that message to calculate clock difference against gateway and synchronize to it.

Parameters
macAddress where this message was received from
bufPointer to the buffer that contains the message
countMessage length in number of bytes of ClockResponse message
Returns
Returns true if message could be correcly processed

Definition at line 1367 of file EnigmaIOTNode.cpp.

◆ processControlCommand()

bool EnigmaIOTNodeClass::processControlCommand ( const uint8_t *  mac,
const uint8_t *  data,
size_t  len,
bool  broadcast 
)
protected

Processes a control command. Does not propagate to user code.

Parameters
macGateway address
dataBuffer to store received message
lenLength of payload data
broadcasttrueif this is a broadcast addressed message
Returns
Returns true if message could be correcly decoded and processed

Definition at line 2366 of file EnigmaIOTNode.cpp.

◆ processDownstreamData()

bool EnigmaIOTNodeClass::processDownstreamData ( const uint8_t *  mac,
const uint8_t *  buf,
size_t  count,
bool  control = false 
)
protected

Processes downstream data from gateway.

Parameters
macGateway address
bufBuffer to store received payload
countLength of payload data
controlIdicates if downstream message is user or control data. If true it is a control message
Returns
Returns true if message could be correcly decoded

Definition at line 2425 of file EnigmaIOTNode.cpp.

◆ processGetNameCommand()

bool EnigmaIOTNodeClass::processGetNameCommand ( const uint8_t *  mac,
const uint8_t *  data,
uint8_t  len 
)
protected

Processes a request to get Node name and address.

Parameters
macGateway address
dataBuffer to store received message
lenLength of payload data
Returns
Returns true if message could be correcly decoded and processed

Definition at line 1795 of file EnigmaIOTNode.cpp.

◆ processGetRSSICommand()

bool EnigmaIOTNodeClass::processGetRSSICommand ( const uint8_t *  mac,
const uint8_t *  data,
uint8_t  len 
)
protected

Processes a request to measure RSSI.

Parameters
macGateway address
dataBuffer to store received message
lenLength of payload data
Returns
Returns true if message could be correcly decoded and processed

Definition at line 2046 of file EnigmaIOTNode.cpp.

◆ processGetSleepTimeCommand()

bool EnigmaIOTNodeClass::processGetSleepTimeCommand ( const uint8_t *  mac,
const uint8_t *  buf,
uint8_t  len 
)
protected

Processes a request of sleep time configuration.

Parameters
macGateway address
bufBuffer to store received message
lenLength of payload data
Returns
Returns true if message could be correcly decoded and processed

Definition at line 1772 of file EnigmaIOTNode.cpp.

◆ processInvalidateKey()

nodeInvalidateReason_t EnigmaIOTNodeClass::processInvalidateKey ( const uint8_t *  mac,
const uint8_t *  buf,
size_t  count 
)
protected

Gets a buffer containing an InvalidateKey message and process it. This trigger a new key agreement to start.

Parameters
macAddress where this message was received from
bufPointer to the buffer that contains the message
countMessage length in number of bytes of InvalidateKey message
Returns
Returns the reason because key is not valid anymore. Check possible values in nodeInvalidateReason_t

Definition at line 2527 of file EnigmaIOTNode.cpp.

◆ processOTACommand()

bool EnigmaIOTNodeClass::processOTACommand ( const uint8_t *  mac,
const uint8_t *  data,
uint8_t  len 
)
protected

Processes a single OTA update command or data.

Parameters
macGateway address
dataBuffer to store received message
lenLength of payload data
Returns
Returns true if message could be correcly decoded and processed

Definition at line 2181 of file EnigmaIOTNode.cpp.

◆ processServerHello()

bool EnigmaIOTNodeClass::processServerHello ( const uint8_t *  mac,
const uint8_t *  buf,
size_t  count 
)
protected

Gets a buffer containing a ServerHello message and process it. It uses that message to calculate a shared key using Diffie Hellman algorithm.

Parameters
macAddress where this message was received from
bufPointer to the buffer that contains the message
countMessage length in number of bytes of ServerHello message
Returns
Returns true if message could be correcly processed

Definition at line 1471 of file EnigmaIOTNode.cpp.

◆ processSetIdentifyCommand()

bool EnigmaIOTNodeClass::processSetIdentifyCommand ( const uint8_t *  mac,
const uint8_t *  buf,
uint8_t  len 
)
protected

Processes a request to start indicate to identify a node visually.

Parameters
macGateway address
bufBuffer to store received message
lenLength of payload data
Returns
Returns true if message could be correcly decoded and processed

Definition at line 2033 of file EnigmaIOTNode.cpp.

◆ processSetNameCommand()

bool EnigmaIOTNodeClass::processSetNameCommand ( const uint8_t *  mac,
const uint8_t *  data,
uint8_t  len 
)
protected

Processes a request to set Node name.

Parameters
macGateway address
dataBuffer to store received message
lenLength of payload data
Returns
Returns true if message could be correcly decoded and processed

Definition at line 1899 of file EnigmaIOTNode.cpp.

◆ processSetNameResponse()

bool EnigmaIOTNodeClass::processSetNameResponse ( const uint8_t *  mac,
const uint8_t *  data,
uint8_t  len 
)
protected

Processes a response to set Node name.

Parameters
macGateway address
dataBuffer to store received message
lenLength of payload data
Returns
Returns true if message could be correcly decoded and processed

Definition at line 1829 of file EnigmaIOTNode.cpp.

◆ processSetResetConfigCommand()

bool EnigmaIOTNodeClass::processSetResetConfigCommand ( const uint8_t *  mac,
const uint8_t *  buf,
uint8_t  len 
)
protected

Processes a request to reset node configuration.

Parameters
macGateway address
bufBuffer to store received message
lenLength of payload data
Returns
Returns true if message could be correcly decoded and processed

Definition at line 2059 of file EnigmaIOTNode.cpp.

◆ processSetRestartCommand()

bool EnigmaIOTNodeClass::processSetRestartCommand ( const uint8_t *  mac,
const uint8_t *  buf,
uint8_t  len 
)
protected

Processes a request to restar node mcu.

Parameters
macGateway address
bufBuffer to store received message
lenLength of payload data
Returns
Returns true if message could be correcly decoded and processed

Definition at line 2053 of file EnigmaIOTNode.cpp.

◆ processSetSleepTimeCommand()

bool EnigmaIOTNodeClass::processSetSleepTimeCommand ( const uint8_t *  mac,
const uint8_t *  buf,
uint8_t  len 
)
protected

Processes a request to set new sleep time configuration.

Parameters
macGateway address
bufBuffer to store received message
lenLength of payload data
Returns
Returns true if message could be correcly decoded and processed

Definition at line 2118 of file EnigmaIOTNode.cpp.

◆ processVersionCommand()

bool EnigmaIOTNodeClass::processVersionCommand ( const uint8_t *  mac,
const uint8_t *  buf,
uint8_t  len 
)
protected

Processes a request firmware version.

Parameters
macGateway address
bufBuffer to store received message
lenLength of payload data
Returns
Returns true if message could be correcly decoded and processed

Definition at line 2163 of file EnigmaIOTNode.cpp.

◆ reportRSSI()

bool EnigmaIOTNodeClass::reportRSSI ( )
protected

Sends RSSI value and channel to Gateway.

Returns
True if report was sent successfuly

Definition at line 982 of file EnigmaIOTNode.cpp.

◆ resetConfig()

void EnigmaIOTNodeClass::resetConfig ( )

Deletes configuration file stored on flash. It makes neccessary to configure it again using WiFi Portal.

Definition at line 44 of file EnigmaIOTNode.cpp.

◆ restart()

void EnigmaIOTNodeClass::restart ( restartReason_t  reason,
bool  reboot = true 
)

Sets connection as unregistered to force a resyncrhonisation after boot.

Parameters
reasonReason of the reboot (OTA, restart requested, configuration reset)
rebootTrue if a reboot should be triggered after unregistration

Definition at line 2351 of file EnigmaIOTNode.cpp.

◆ rx_cb()

void EnigmaIOTNodeClass::rx_cb ( uint8_t *  mac_addr,
uint8_t *  data,
uint8_t  len 
)
staticprotected

Function that will be called anytime this node receives a message.

Parameters
mac_addrAddress of message sender
dataBuffer that stores message bytes
lenLength of message in number of bytes

Definition at line 1174 of file EnigmaIOTNode.cpp.

◆ saveFlashData()

bool EnigmaIOTNodeClass::saveFlashData ( bool  fsOpen = false)
protected

Saves configuration to flash memory.

Parameters
fsOpenTrue if FileSystem has is managed in outter code
Returns
Returns true if data could be written successfuly. false otherwise

Definition at line 352 of file EnigmaIOTNode.cpp.

◆ saveRTCData()

bool EnigmaIOTNodeClass::saveRTCData ( )
protected

Save configuration to RTC to store current status and recover it after deep sleep.

Returns
Returns true if result is successful. false otherwise

Definition at line 433 of file EnigmaIOTNode.cpp.

◆ searchForGateway()

bool EnigmaIOTNodeClass::searchForGateway ( rtcmem_data_t data,
bool  shouldStoreData = false 
)
protected

Starts searching for a gateway that it using configured Network Name as WiFi AP. Stores this info for subsequent use.

Parameters
dataNode context structure
shouldStoreDataTrue if this method should save context in flash
Returns
Returns true if gateway could be found. false otherwise

Definition at line 860 of file EnigmaIOTNode.cpp.

◆ sendData() [1/2]

bool EnigmaIOTNodeClass::sendData ( const uint8_t *  data,
size_t  len,
dataMessageType_t  dataMsgType,
bool  encrypt = true,
nodePayloadEncoding_t  payloadEncoding = CAYENNELPP 
)
protected

Initiades data transmission distinguissing if it is payload or control data.

Parameters
dataBuffer to store payload to be sent
lenLength of payload data
dataMsgTypeSignals if this message is a special EnigmaIoT message or that should not be passed to higher layers
encrypttrue if data should be encrypted. Default is true
payloadEncodingIdentifies data encoding of payload. It can be RAW, CAYENNELPP, MSGPACK
Returns
Returns true if message could be correcly sent

Definition at line 1535 of file EnigmaIOTNode.cpp.

◆ sendData() [2/2]

bool EnigmaIOTNodeClass::sendData ( const uint8_t *  data,
size_t  len,
nodePayloadEncoding_t  payloadEncoding = CAYENNELPP 
)
inline

Starts a data message transmission.

Parameters
dataPayload buffer
lenPayload length
payloadEncodingIdentifies data encoding of payload. It can be RAW, CAYENNELPP, MSGPACK

Definition at line 609 of file EnigmaIOTNode.h.

◆ sendHADiscoveryMessage()

bool EnigmaIOTNodeClass::sendHADiscoveryMessage ( const uint8_t *  data,
size_t  len 
)

Builds, encrypts and sends a HomeAssistant discovery message.

Parameters
dataBuffer to store payload to be sent
lenLength of payload data
Returns
Returns true if message could be correcly sent

Definition at line 1764 of file EnigmaIOTNode.cpp.

◆ sendNodeNameSet()

bool EnigmaIOTNodeClass::sendNodeNameSet ( const char *  name)
protected

Informs Gateway about custom node name.

Parameters
nameCustom node name
Returns
True if message was sent successfuly

Definition at line 1936 of file EnigmaIOTNode.cpp.

◆ sendRestart()

void EnigmaIOTNodeClass::sendRestart ( )
protected

Sends a restart notification control message.

Definition at line 56 of file EnigmaIOTNode.cpp.

◆ sendUnencryptedData()

bool EnigmaIOTNodeClass::sendUnencryptedData ( const uint8_t *  data,
size_t  len,
nodePayloadEncoding_t  payloadEncoding = CAYENNELPP 
)
inline

Starts a data message transmission.

Parameters
dataPayload buffer
lenPayload length
payloadEncodingIdentifies data encoding of payload. It can be RAW, CAYENNELPP, MSGPACK

Definition at line 628 of file EnigmaIOTNode.h.

◆ setLed()

void EnigmaIOTNodeClass::setLed ( uint8_t  led,
time_t  onTime = FLASH_LED_TIME 
)

Sets a LED to be flashed every time a message is transmitted.

Parameters
ledLED I/O pin
onTimeFlash duration. 100ms by default.

Definition at line 89 of file EnigmaIOTNode.cpp.

◆ setNodeAddress()

bool EnigmaIOTNodeClass::setNodeAddress ( uint8_t  address[ENIGMAIOT_ADDR_LEN])

Set node address to be used in EnigmaIOT communication.

Parameters
addressNode address
Returns
true if addres was set correctly

Definition at line 943 of file EnigmaIOTNode.cpp.

◆ setResetPin()

void EnigmaIOTNodeClass::setResetPin ( int  pin)

Sets a pin to be used to reset configuration it it is connected to ground during startup.

Parameters
pinReset pin

Definition at line 94 of file EnigmaIOTNode.cpp.

◆ setSleepTime()

void EnigmaIOTNodeClass::setSleepTime ( uint32_t  sleepTime,
bool  forceSleepForever = false 
)

Allows to configure a new sleep time period from user code.

Parameters
sleepTimeTime in seconds. Final period is not espected to be exact. Its value depends on communication process. If it is zero, disables deep sleep.
forceSleepForeverIgnored if sleepTime is not zero. If it has zero value forces deepSleep command to sleep indifinitely.

Definition at line 948 of file EnigmaIOTNode.cpp.

◆ sleep()

void EnigmaIOTNodeClass::sleep ( )

Requests transition to sleep mode (low energy state)

Sleep can be requested in any moment and will be triggered inmediatelly except if node is doing registration or is waiting for downlink

Definition at line 1564 of file EnigmaIOTNode.cpp.

◆ startIdentifying()

void EnigmaIOTNodeClass::startIdentifying ( time_t  period)
protected

Starts node identification by flashing led.

Parameters
periodFlash led period in ms

Definition at line 663 of file EnigmaIOTNode.cpp.

◆ stop()

void EnigmaIOTNodeClass::stop ( )

Stops EnigmaIoT protocol.

Definition at line 938 of file EnigmaIOTNode.cpp.

◆ stopIdentifying()

void EnigmaIOTNodeClass::stopIdentifying ( )
protected

Stops node identification.

Definition at line 669 of file EnigmaIOTNode.cpp.

◆ tx_cb()

void EnigmaIOTNodeClass::tx_cb ( uint8_t *  mac_addr,
uint8_t  status 
)
staticprotected

Function that will be called anytime this node sends a message to indicate status result of sending process.

Parameters
mac_addrAddress of message destination
statusResult of sending process

Definition at line 1178 of file EnigmaIOTNode.cpp.

◆ unencryptedDataMessage()

bool EnigmaIOTNodeClass::unencryptedDataMessage ( const uint8_t *  data,
size_t  len,
dataMessageType_t  dataMsgType = DATA_TYPE,
nodePayloadEncoding_t  payloadEncoding = CAYENNELPP 
)
protected

Builds and sends a Data message without encryption. Not recommended, use it only if you absolutely need more performance.

Parameters
dataBuffer to store payload to be sent
lenLength of payload data
dataMsgTypeSignals if this message is a special EnigmaIoT message or that should not be passed to higher layers
payloadEncodingDetermine payload data encoding as nodePayloadEncoding_t. It can be RAW, CAYENNELPP, MSGPACK
Returns
Returns true if message could be correcly sent

Definition at line 1574 of file EnigmaIOTNode.cpp.

◆ unixtime()

time_t EnigmaIOTNodeClass::unixtime ( )

Gets current time in seconds from 1970, if time is synchronized.

Returns
Time value in time_t format

Definition at line 1460 of file EnigmaIOTNode.cpp.

Member Data Documentation

◆ clockSyncEnabled

bool EnigmaIOTNodeClass::clockSyncEnabled = false
protected

If true clock is synchronized with Gateway.

Definition at line 161 of file EnigmaIOTNode.h.

◆ comm

Comms_halClass* EnigmaIOTNodeClass::comm
protected

Comms abstraction layer.

Definition at line 140 of file EnigmaIOTNode.h.

◆ configCleared

bool EnigmaIOTNodeClass::configCleared = false
protected

This flag disables asy configuration save after triggering a factory reset.

Definition at line 167 of file EnigmaIOTNode.h.

◆ cycleStartedTime

time_t EnigmaIOTNodeClass::cycleStartedTime
protected

Used to calculate exact sleep time by substracting awake time.

Definition at line 172 of file EnigmaIOTNode.h.

◆ dataMessageEncrypt

bool EnigmaIOTNodeClass::dataMessageEncrypt = true
protected

Message encryption enabled. Stored for use in case of message retransmission is needed.

Definition at line 152 of file EnigmaIOTNode.h.

◆ dataMessageSendEncoding

nodePayloadEncoding_t EnigmaIOTNodeClass::dataMessageSendEncoding = RAW
protected

Encoding of the message pending to be sent.

Definition at line 151 of file EnigmaIOTNode.h.

◆ dataMessageSendPending

bool EnigmaIOTNodeClass::dataMessageSendPending = false
protected

True in case of message retransmission is needed.

Definition at line 150 of file EnigmaIOTNode.h.

◆ dataMessageSent

uint8_t EnigmaIOTNodeClass::dataMessageSent[MAX_MESSAGE_LENGTH]
protected

Buffer where sent message is stored in case of retransmission is needed.

Definition at line 148 of file EnigmaIOTNode.h.

◆ dataMessageSentLength

uint8_t EnigmaIOTNodeClass::dataMessageSentLength = 0
protected

Message length stored for use in case of message retransmission is needed.

Definition at line 149 of file EnigmaIOTNode.h.

◆ flashBlue

bool EnigmaIOTNodeClass::flashBlue = false
protected

If true Tx LED will be flashed.

Definition at line 137 of file EnigmaIOTNode.h.

◆ gatewaySearchStarted

bool EnigmaIOTNodeClass::gatewaySearchStarted = false
protected

Avoids start a new gateway scan if it already started.

Definition at line 164 of file EnigmaIOTNode.h.

◆ identifyStart

time_t EnigmaIOTNodeClass::identifyStart
protected

Time when identification started flashing. Used to control identification timeout.

Definition at line 159 of file EnigmaIOTNode.h.

◆ indentifying

boolean EnigmaIOTNodeClass::indentifying = false
protected

True if node has its led flashing to be identified.

Definition at line 158 of file EnigmaIOTNode.h.

◆ invalidateReason

nodeInvalidateReason_t EnigmaIOTNodeClass::invalidateReason = UNKNOWN_ERROR
protected

Last key invalidation reason.

Definition at line 153 of file EnigmaIOTNode.h.

◆ lastBroadcastMsgCounter

int16_t EnigmaIOTNodeClass::lastBroadcastMsgCounter
protected

Counter for broadcast messages from gateway *‍/.

Definition at line 173 of file EnigmaIOTNode.h.

◆ lastOTAmsg

time_t EnigmaIOTNodeClass::lastOTAmsg
protected

Time when last OTA update message has received. This is used to control timeout.

Definition at line 157 of file EnigmaIOTNode.h.

◆ led

int8_t EnigmaIOTNodeClass::led = -1
protected

IO Pin that corresponds to Tx LED. Default value disables LED. It is initialized with setLed method.

Definition at line 138 of file EnigmaIOTNode.h.

◆ ledOnTime

unsigned int EnigmaIOTNodeClass::ledOnTime
protected

Time that LED is On during flash. Initalized on setLed

Definition at line 139 of file EnigmaIOTNode.h.

◆ node

Node EnigmaIOTNodeClass::node
protected

Node abstraction to store context.

Definition at line 136 of file EnigmaIOTNode.h.

◆ notifyConnection

onConnected_t EnigmaIOTNodeClass::notifyConnection
protected

Callback that will be called anytime a new node is registered.

Definition at line 142 of file EnigmaIOTNode.h.

◆ notifyData

onNodeDataRx_t EnigmaIOTNodeClass::notifyData
protected

Callback that will be called on every message reception.

Definition at line 141 of file EnigmaIOTNode.h.

◆ notifyDisconnection

onDisconnected_t EnigmaIOTNodeClass::notifyDisconnection
protected

Callback that will be called anytime a node is disconnected.

Definition at line 143 of file EnigmaIOTNode.h.

◆ notifyWiFiManagerExit

onWiFiManagerExit_t EnigmaIOTNodeClass::notifyWiFiManagerExit
protected

Function called when configuration portal exits.

Definition at line 170 of file EnigmaIOTNode.h.

◆ notifyWiFiManagerStarted

simpleEventHandler_t EnigmaIOTNodeClass::notifyWiFiManagerStarted
protected

Function called when configuration portal is started.

Definition at line 171 of file EnigmaIOTNode.h.

◆ otaError

bool EnigmaIOTNodeClass::otaError = false
protected

True if OTA update has failed. This normally produces a restart.

Definition at line 155 of file EnigmaIOTNode.h.

◆ otaRunning

bool EnigmaIOTNodeClass::otaRunning = false
protected

True if OTA update has started.

Definition at line 154 of file EnigmaIOTNode.h.

◆ protectOTA

bool EnigmaIOTNodeClass::protectOTA = false
protected

True if OTA update was launched. OTA flag is stored on RTC so this disables writting.

Definition at line 156 of file EnigmaIOTNode.h.

◆ requestReportRSSI

bool EnigmaIOTNodeClass::requestReportRSSI = false
protected

Flag to control RSSI reporting.

Definition at line 166 of file EnigmaIOTNode.h.

◆ requestSearchGateway

bool EnigmaIOTNodeClass::requestSearchGateway = false
protected

Flag to control updating gateway address, RSSI and channel.

Definition at line 165 of file EnigmaIOTNode.h.

◆ resetPin

int EnigmaIOTNodeClass::resetPin = -1
protected

Pin used to reset configuration if it is connected to ground during startup.

Definition at line 168 of file EnigmaIOTNode.h.

◆ restartReason

restartReason_t EnigmaIOTNodeClass::restartReason
protected

Reason of restart (OTA, restart requested, configuration reset)

Definition at line 163 of file EnigmaIOTNode.h.

◆ rtcmem_data

rtcmem_data_t EnigmaIOTNodeClass::rtcmem_data
protected

Context data to be stored on persistent storage.

Definition at line 145 of file EnigmaIOTNode.h.

◆ shouldRestart

bool EnigmaIOTNodeClass::shouldRestart = false
protected

Triggers a restart if true.

Definition at line 162 of file EnigmaIOTNode.h.

◆ sleepRequested

bool EnigmaIOTNodeClass::sleepRequested = false
protected

true means that this node will sleep as soon a message is sent and downlink wait time has passed

Definition at line 146 of file EnigmaIOTNode.h.

◆ sleepTime

uint64_t EnigmaIOTNodeClass::sleepTime
protected

Time in microseconds that this node will be slept between measurements.

Definition at line 147 of file EnigmaIOTNode.h.

◆ timeSyncPeriod

clock_t EnigmaIOTNodeClass::timeSyncPeriod = QUICK_SYNC_TIME
protected

Clock synchronization period.

Definition at line 160 of file EnigmaIOTNode.h.

◆ useCounter

bool EnigmaIOTNodeClass::useCounter = true
protected

true means that data message counter will be used to mark message order

Definition at line 144 of file EnigmaIOTNode.h.

◆ wifiManager

AsyncWiFiManager* EnigmaIOTNodeClass::wifiManager
protected

Wifi configuration portal.

Definition at line 169 of file EnigmaIOTNode.h.


The documentation for this class was generated from the following files:
disconnectEventHandler
void disconnectEventHandler(nodeInvalidateReason_t reason)
Definition: EnigmaIOT-Button-Controller.cpp:67
connectEventHandler
void connectEventHandler()
Definition: EnigmaIOT-Button-Controller.cpp:61
EnigmaIOTNode
EnigmaIOTNodeClass EnigmaIOTNode
Definition: EnigmaIOTNode.cpp:2719
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
loop
void loop()
Definition: EnigmaIOT-Button-Controller.cpp:157
setup
void setup()
Definition: EnigmaIOT-Button-Controller.cpp:101
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
processRxData
void processRxData(const uint8_t *mac, const uint8_t *buffer, uint8_t length, nodeMessageType_t command, nodePayloadEncoding_t payloadEncoding)
Definition: EnigmaIOT-Button-Controller.cpp:83
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