EnigmaIOT
0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
|
Main gateway class. Manages communication with nodes and sends data to upper layer. More...
#include <EnigmaIOTGateway.h>
Public Member Functions | |
bool | getShouldSave () |
Gets flag that indicates if configuration should be saved. More... | |
char * | getNetworkName () |
Gets EnigmaIOT network name. More... | |
char * | getNetworkKey (bool plain=false) |
Gets hashed EnigmaIOT network key. More... | |
void | addWiFiManagerParameter (AsyncWiFiManagerParameter *p) |
Adds a parameter to configuration portal. 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... | |
bool | configWiFiManager () |
Starts configuration AP and web server and gets settings from it. More... | |
void | begin (Comms_halClass *comm, uint8_t *networkKey=NULL, bool useDataCounter=true) |
Initalizes communication basic data and starts accepting node registration. More... | |
void | handle () |
This method should be called periodically for instance inside loop() function. It is used for internal gateway maintenance tasks. More... | |
void | setTxLed (uint8_t led, time_t onTime=FLASH_LED_TIME) |
Sets a LED to be flashed every time a message is transmitted. More... | |
void | setRxLed (uint8_t led, time_t onTime=FLASH_LED_TIME) |
Sets a LED to be flashed every time a message is received. More... | |
void | onDataRx (onGwDataRx_t handler) |
Defines a function callback that will be called on every downlink data message that is received from a node. More... | |
void | onHADiscovery (onHADiscovery_t handler) |
Defines a function callback that will be called when a Home Assistant discovery message is received from a node. More... | |
double | getPER (uint8_t *address) |
Gets packet error rate of node that has a specific address. More... | |
uint32_t | getTotalPackets (uint8_t *address) |
Gets total packets sent by node that has a specific address. More... | |
uint32_t | getErrorPackets (uint8_t *address) |
Gets number of errored packets of node that has a specific address. More... | |
double | getPacketsHour (uint8_t *address) |
Gets packet rate sent by node that has a specific address, in packets per hour. More... | |
bool | sendDownstream (uint8_t *mac, const uint8_t *data, size_t len, control_message_type_t controlData, gatewayPayloadEncoding_t payload_type=RAW, char *nodeName=NULL) |
Starts a downstream data message transmission. More... | |
void | onNewNode (onNewNode_t handler) |
Defines a function callback that will be called every time a node gets connected or reconnected. More... | |
void | onNodeDisconnected (onNodeDisconnected_t handler) |
Defines a function callback that will be called every time a node is disconnected. More... | |
void | onGatewayRestartRequested (simpleEventHandler_t handler) |
Defines a function callback that will process a gateway restart request. More... | |
bool | addInputMsgQueue (const uint8_t *addr, const uint8_t *msg, size_t len) |
Add message to input queue. More... | |
msg_queue_item_t * | getInputMsgQueue (msg_queue_item_t *buffer) |
Gets next item in the queue. More... | |
void | popInputMsgQueue () |
Deletes next item in the queue. More... | |
int | getActiveNodesNumber () |
Gets number of active nodes. More... | |
NodeList * | getNodes () |
Gets nodes data structure. More... | |
Protected Member Functions | |
bool | serverHello (const uint8_t *key, Node *node) |
Build a ServerHello message and send it to node. More... | |
bool | sendBroadcastKey (Node *node) |
Sends broadcast key to node if it has requested it explicitly or it has notified during handshake. More... | |
bool | processClientHello (const uint8_t mac[ENIGMAIOT_ADDR_LEN], const uint8_t *buf, size_t count, Node *node) |
Gets a buffer containing a ClientHello message and process it. This carries node public key to be used on Diffie Hellman algorithm. More... | |
bool | processClockRequest (const uint8_t mac[ENIGMAIOT_ADDR_LEN], const uint8_t *buf, size_t count, Node *node) |
Starts clock sync procedure from node to gateway. More... | |
bool | clockResponse (Node *node, uint64_t t1, uint64_t t2) |
Returns timestaps needed so that node can calculate time difference. More... | |
bool | invalidateKey (Node *node, gwInvalidateReason_t reason) |
Creates an InvalidateKey message and sned it. This trigger a new key agreement to start on related node. More... | |
bool | processDataMessage (const uint8_t mac[ENIGMAIOT_ADDR_LEN], uint8_t *buf, size_t count, Node *node, bool encrypted=true) |
Processes data message from node. More... | |
bool | processUnencryptedDataMessage (const uint8_t mac[ENIGMAIOT_ADDR_LEN], uint8_t *buf, size_t count, Node *node) |
Processes unencrypted data message from node. More... | |
bool | downstreamDataMessage (Node *node, const uint8_t *data, size_t len, control_message_type_t controlData, gatewayPayloadEncoding_t encoding=ENIGMAIOT) |
Builds, encrypts and sends a DownstreamData message. More... | |
bool | processControlMessage (const uint8_t mac[ENIGMAIOT_ADDR_LEN], uint8_t *buf, size_t count, Node *node) |
Processes control message from node. More... | |
bool | processNodeNameSet (const uint8_t mac[ENIGMAIOT_ADDR_LEN], uint8_t *buf, size_t count, Node *node) |
Processes new node name request fromn node. More... | |
bool | nodeNameSetRespose (Node *node, int8_t error) |
Send back set name response. More... | |
void | manageMessage (const uint8_t *mac, 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 | loadFlashData () |
Loads configuration from flash memory. More... | |
bool | saveFlashData () |
Saves configuration to flash memory. More... | |
bool | sendHADiscoveryJSON (uint8_t *address, uint8_t *data, size_t len, const char *networkName, const char *nodeName) |
Sends a Home Assistant discovery message after receiving it from node. More... | |
Static Protected Member Functions | |
static void | doSave (void) |
Activates a flag that signals that configuration has to be saved. More... | |
static void | doResetConfig (void) |
Activates a flag that signals that configuration has to be saved. More... | |
static void | rx_cb (uint8_t *mac_addr, uint8_t *data, uint8_t len) |
Function that will be called anytime this gateway receives a message. More... | |
static void | tx_cb (uint8_t *mac_addr, uint8_t status) |
Function that will be called anytime this gateway sends a message to indicate status result of sending process. More... | |
Protected Attributes | |
uint8_t | myPublicKey [KEY_LENGTH] |
Temporary public key store used during key agreement. More... | |
bool | flashTx = false |
true if Tx LED should flash More... | |
volatile bool | flashRx = false |
true if Rx LED should flash More... | |
node_t | node |
temporary store to keep node data while processing a message More... | |
NodeList | nodelist |
Node database that keeps status and shared keys. More... | |
Comms_halClass * | comm |
Instance of physical communication layer. More... | |
int8_t | txled = -1 |
I/O pin to connect a led that flashes when gateway transmits data. More... | |
int8_t | rxled = -1 |
I/O pin to connect a led that flashes when gateway receives data. More... | |
unsigned long | txLedOnTime |
Flash duration for Tx LED. More... | |
unsigned long | rxLedOnTime |
Flash duration for Rx LED. More... | |
onGwDataRx_t | notifyData |
Callback function that will be invoked when data is received from a node. More... | |
onHADiscovery_t | notifyHADiscovery |
Callback function that will be invoked when HomeAssistant discovery message is received from a node. More... | |
onNewNode_t | notifyNewNode |
Callback function that will be invoked when a new node is connected. More... | |
onNodeDisconnected_t | notifyNodeDisconnection |
Callback function that will be invoked when a node gets disconnected. More... | |
simpleEventHandler_t | notifyRestartRequested |
Callback function that will be invoked when a hardware restart is requested. More... | |
bool | useCounter = true |
true if counter is used to check data messages order More... | |
gateway_config_t | gwConfig |
Gateway specific configuration to be stored on flash memory. More... | |
char | plainNetKey [KEY_LENGTH] |
msg_queue_item_t | tempBuffer |
Temporary storage for input message got from buffer. More... | |
EnigmaIOTRingBuffer< msg_queue_item_t > * | input_queue |
Input messages buffer. It acts as a FIFO queue. More... | |
AsyncWebServer * | server |
WebServer that holds configuration portal. More... | |
DNSServer * | dns |
DNS server used by configuration portal. 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... | |
Friends | |
class | GatewayAPI |
Main gateway class. Manages communication with nodes and sends data to upper layer.
Definition at line 120 of file EnigmaIOTGateway.h.
bool EnigmaIOTGatewayClass::addInputMsgQueue | ( | const uint8_t * | addr, |
const uint8_t * | msg, | ||
size_t | len | ||
) |
Add message to input queue.
addr | Origin address |
msg | EnigmaIoT message |
len | Message length |
Definition at line 792 of file EnigmaIOTGateway.cpp.
|
inline |
Adds a parameter to configuration portal.
p | Configuration parameter |
Definition at line 368 of file EnigmaIOTGateway.h.
void EnigmaIOTGatewayClass::begin | ( | Comms_halClass * | comm, |
uint8_t * | networkKey = NULL , |
||
bool | useDataCounter = true |
||
) |
Initalizes communication basic data and starts accepting node registration.
comm | Physical layer to be used on this network |
networkKey | Network key to protect shared key agreement |
useDataCounter | Indicates if a counter is going to be added to every message data to check message sequence. true by default |
Definition at line 736 of file EnigmaIOTGateway.cpp.
|
protected |
Returns timestaps needed so that node can calculate time difference.
node | Pointer to data that corresponds to originating node |
t1 | Origin clock |
t2 | Received clock |
true
if message could be correcly processed Definition at line 1822 of file EnigmaIOTGateway.cpp.
bool EnigmaIOTGatewayClass::configWiFiManager | ( | ) |
Starts configuration AP and web server and gets settings from it.
true
if data was been correctly configured. false
otherwise Definition at line 494 of file EnigmaIOTGateway.cpp.
|
staticprotected |
Activates a flag that signals that configuration has to be saved.
Definition at line 47 of file EnigmaIOTGateway.cpp.
|
staticprotected |
Activates a flag that signals that configuration has to be saved.
Definition at line 42 of file EnigmaIOTGateway.cpp.
|
protected |
Builds, encrypts and sends a DownstreamData message.
node | Node that downstream data message is going to |
data | Buffer to store payload to be sent |
len | Length of payload data |
controlData | Content data type if control data |
encoding | Identifies data encoding of payload. It can be RAW, CAYENNELPP, MSGPACK |
true
if message could be correcly sent or scheduled Definition at line 1489 of file EnigmaIOTGateway.cpp.
|
inline |
Gets number of active nodes.
Definition at line 592 of file EnigmaIOTGateway.h.
uint32_t EnigmaIOTGatewayClass::getErrorPackets | ( | uint8_t * | address | ) |
Gets number of errored packets of node that has a specific address.
address | Node address |
Definition at line 1476 of file EnigmaIOTGateway.cpp.
msg_queue_item_t * EnigmaIOTGatewayClass::getInputMsgQueue | ( | msg_queue_item_t * | buffer | ) |
Gets next item in the queue.
Definition at line 815 of file EnigmaIOTGateway.cpp.
|
inline |
Gets hashed EnigmaIOT network key.
Definition at line 357 of file EnigmaIOTGateway.h.
|
inline |
Gets EnigmaIOT network name.
Definition at line 349 of file EnigmaIOTGateway.h.
|
inline |
Gets nodes data structure.
Definition at line 600 of file EnigmaIOTGateway.h.
double EnigmaIOTGatewayClass::getPacketsHour | ( | uint8_t * | address | ) |
Gets packet rate sent by node that has a specific address, in packets per hour.
address | Node address |
Definition at line 1482 of file EnigmaIOTGateway.cpp.
double EnigmaIOTGatewayClass::getPER | ( | uint8_t * | address | ) |
Gets packet error rate of node that has a specific address.
address | Node address |
Definition at line 1460 of file EnigmaIOTGateway.cpp.
bool EnigmaIOTGatewayClass::getShouldSave | ( | ) |
Gets flag that indicates if configuration should be saved.
true
if config data should be saved. false
otherwise Definition at line 55 of file EnigmaIOTGateway.cpp.
|
protected |
Functrion to debug send status.
mac_addr | Address of message sender |
status | Result status code |
Definition at line 858 of file EnigmaIOTGateway.cpp.
uint32_t EnigmaIOTGatewayClass::getTotalPackets | ( | uint8_t * | address | ) |
Gets total packets sent by node that has a specific address.
address | Node address |
Definition at line 1470 of file EnigmaIOTGateway.cpp.
void EnigmaIOTGatewayClass::handle | ( | ) |
This method should be called periodically for instance inside loop()
function. It is used for internal gateway maintenance tasks.
digitalRead (rxled) &&
digitalRead (txled) &&
Definition at line 867 of file EnigmaIOTGateway.cpp.
|
protected |
Creates an InvalidateKey message and sned it. This trigger a new key agreement to start on related node.
node | Node to send Invalidate Key message to |
reason | Reason that produced key invalidation in gwInvalidateReason_t format |
true
if message could be correcly sent Definition at line 1624 of file EnigmaIOTGateway.cpp.
|
protected |
Loads configuration from flash memory.
true
if data was read successfuly. false
otherwise Definition at line 611 of file EnigmaIOTGateway.cpp.
|
protected |
Process every received message.
It starts clasiffying message usint the first byte. After that it passes it to the corresponding method for decoding
mac | Address of message sender |
buf | Buffer that stores message bytes |
count | Length of message in number of bytes |
Definition at line 935 of file EnigmaIOTGateway.cpp.
|
protected |
Send back set name response.
node | Pointer to data that corresponds to originating node |
error | Result code of name set procedure (0: OK, -1: Already used, -2: Too long, -3: Empty name, -4: Message error) |
true
if message could be correcly processed Definition at line 1106 of file EnigmaIOTGateway.cpp.
|
inline |
Defines a function callback that will be called on every downlink data message that is received from a node.
Use example:
handler | Pointer to the function |
Definition at line 449 of file EnigmaIOTGateway.h.
|
inline |
Defines a function callback that will process a gateway restart request.
handler | Pointer to the function |
Definition at line 565 of file EnigmaIOTGateway.h.
|
inline |
Defines a function callback that will be called when a Home Assistant discovery message is received from a node.
handler | Pointer to the function |
Definition at line 458 of file EnigmaIOTGateway.h.
|
inline |
Defines a function callback that will be called every time a node gets connected or reconnected.
Use example:
handler | Pointer to the function |
Definition at line 528 of file EnigmaIOTGateway.h.
|
inline |
Defines a function callback that will be called every time a node is disconnected.
Use example:
handler | Pointer to the function |
Definition at line 557 of file EnigmaIOTGateway.h.
|
inline |
Register callback to be called on wifi manager exit.
handle | Callback function pointer |
Definition at line 378 of file EnigmaIOTGateway.h.
|
inline |
Register callback to be called on wifi manager start.
handle | Callback function pointer |
Definition at line 386 of file EnigmaIOTGateway.h.
void EnigmaIOTGatewayClass::popInputMsgQueue | ( | ) |
Deletes next item in the queue.
Definition at line 843 of file EnigmaIOTGateway.cpp.
|
protected |
Gets a buffer containing a ClientHello message and process it. This carries node public key to be used on Diffie Hellman algorithm.
mac | Address where this message was received from |
buf | Pointer to the buffer that contains the message |
count | Message length in number of bytes of ClientHello message |
node | Node entry that Client Hello message comes from |
true
if message could be correcly processed Definition at line 1660 of file EnigmaIOTGateway.cpp.
|
protected |
Starts clock sync procedure from node to gateway.
mac | Address where this message was received from |
buf | Pointer to the buffer that contains the message |
count | Message length in number of bytes of ClockRequest message |
node | Node entry that Client Hello message comes from |
true
if message could be correcly processed Definition at line 1739 of file EnigmaIOTGateway.cpp.
|
protected |
Processes control message from node.
mac | Node address |
buf | Buffer that stores received message |
count | Length of received data |
node | Node where data message comes from |
true
if message could be correcly decoded Definition at line 1250 of file EnigmaIOTGateway.cpp.
|
protected |
Processes data message from node.
mac | Node address |
buf | Buffer that stores received message |
count | Length of received data |
node | Node where data message comes from |
encrypted | true if received message is encrypted |
true
if message could be correcly decoded Definition at line 1377 of file EnigmaIOTGateway.cpp.
|
protected |
Processes new node name request fromn node.
mac | Node address |
buf | Buffer that stores received message |
count | Length of received data |
node | Node where data message comes from |
true
if message could be correcly decoded Definition at line 1175 of file EnigmaIOTGateway.cpp.
|
protected |
Processes unencrypted data message from node.
mac | Node address |
buf | Buffer that stores received message |
count | Length of received data |
node | Node where data message comes from |
true
if message could be correcly decoded Definition at line 1324 of file EnigmaIOTGateway.cpp.
|
staticprotected |
Function that will be called anytime this gateway receives a message.
mac_addr | Address of message sender |
data | Buffer that stores message bytes |
len | Length of message in number of bytes |
Definition at line 849 of file EnigmaIOTGateway.cpp.
|
protected |
Saves configuration to flash memory.
true
if data could be written successfuly. false
otherwise Definition at line 692 of file EnigmaIOTGateway.cpp.
|
protected |
Sends broadcast key to node if it has requested it explicitly or it has notified during handshake.
node | Entry in node list database to get destination address |
true
if message was successfully sent. false
otherwise Definition at line 1903 of file EnigmaIOTGateway.cpp.
bool EnigmaIOTGatewayClass::sendDownstream | ( | uint8_t * | mac, |
const uint8_t * | data, | ||
size_t | len, | ||
control_message_type_t | controlData, | ||
gatewayPayloadEncoding_t | payload_type = RAW , |
||
char * | nodeName = NULL |
||
) |
Starts a downstream data message transmission.
mac | Node address |
data | Payload buffer |
len | Payload length |
controlData | Indicates if data is control data and its class |
payload_type | Identifies data encoding of payload. It can be RAW, CAYENNELPP, MSGPACK |
nodeName | Causes data to be sent to a node with this name instead of numeric address |
Definition at line 364 of file EnigmaIOTGateway.cpp.
|
protected |
Sends a Home Assistant discovery message after receiving it from node.
address | Node physical address |
data | MsgPack input buffer |
len | Input buffer length |
networkName | EnigmaIOT network name |
nodeName | Node name. Can be NULL |
true
if data could be written successfuly. false
otherwise Definition at line 1989 of file EnigmaIOTGateway.cpp.
|
protected |
Build a ServerHello message and send it to node.
key | Node public key to be used on Diffie Hellman algorithm |
node | Entry in node list database where node will be registered |
true
if ServerHello message was successfully sent. false
otherwise Definition at line 1910 of file EnigmaIOTGateway.cpp.
void EnigmaIOTGatewayClass::setRxLed | ( | uint8_t | led, |
time_t | onTime = FLASH_LED_TIME |
||
) |
Sets a LED to be flashed every time a message is received.
led | LED I/O pin |
onTime | Flash duration. 100ms by default. |
Definition at line 66 of file EnigmaIOTGateway.cpp.
void EnigmaIOTGatewayClass::setTxLed | ( | uint8_t | led, |
time_t | onTime = FLASH_LED_TIME |
||
) |
Sets a LED to be flashed every time a message is transmitted.
led | LED I/O pin |
onTime | Flash duration. 100ms by default. |
Definition at line 59 of file EnigmaIOTGateway.cpp.
|
staticprotected |
Function that will be called anytime this gateway sends a message to indicate status result of sending process.
mac_addr | Address of message destination |
status | Result of sending process |
Definition at line 854 of file EnigmaIOTGateway.cpp.
|
friend |
Definition at line 155 of file EnigmaIOTGateway.h.
|
protected |
Instance of physical communication layer.
Definition at line 127 of file EnigmaIOTGateway.h.
|
protected |
DNS server used by configuration portal.
Definition at line 150 of file EnigmaIOTGateway.h.
|
protected |
true
if Rx LED should flash
Definition at line 124 of file EnigmaIOTGateway.h.
|
protected |
true
if Tx LED should flash
Definition at line 123 of file EnigmaIOTGateway.h.
|
protected |
Gateway specific configuration to be stored on flash memory.
Definition at line 140 of file EnigmaIOTGateway.h.
|
protected |
Input messages buffer. It acts as a FIFO queue.
Definition at line 147 of file EnigmaIOTGateway.h.
|
protected |
Temporary public key store used during key agreement.
Definition at line 122 of file EnigmaIOTGateway.h.
|
protected |
temporary store to keep node data while processing a message
Definition at line 125 of file EnigmaIOTGateway.h.
|
protected |
Node database that keeps status and shared keys.
Definition at line 126 of file EnigmaIOTGateway.h.
|
protected |
Callback function that will be invoked when data is received from a node.
Definition at line 132 of file EnigmaIOTGateway.h.
|
protected |
Callback function that will be invoked when HomeAssistant discovery message is received from a node.
Definition at line 134 of file EnigmaIOTGateway.h.
|
protected |
Callback function that will be invoked when a new node is connected.
Definition at line 136 of file EnigmaIOTGateway.h.
|
protected |
Callback function that will be invoked when a node gets disconnected.
Definition at line 137 of file EnigmaIOTGateway.h.
|
protected |
Callback function that will be invoked when a hardware restart is requested.
Definition at line 138 of file EnigmaIOTGateway.h.
|
protected |
Function called when configuration portal exits.
Definition at line 152 of file EnigmaIOTGateway.h.
|
protected |
Function called when configuration portal is started.
Definition at line 153 of file EnigmaIOTGateway.h.
|
protected |
Definition at line 141 of file EnigmaIOTGateway.h.
|
protected |
I/O pin to connect a led that flashes when gateway receives data.
Definition at line 129 of file EnigmaIOTGateway.h.
|
protected |
Flash duration for Rx LED.
Definition at line 131 of file EnigmaIOTGateway.h.
|
protected |
WebServer that holds configuration portal.
Definition at line 149 of file EnigmaIOTGateway.h.
|
protected |
Temporary storage for input message got from buffer.
Definition at line 145 of file EnigmaIOTGateway.h.
|
protected |
I/O pin to connect a led that flashes when gateway transmits data.
Definition at line 128 of file EnigmaIOTGateway.h.
|
protected |
Flash duration for Tx LED.
Definition at line 130 of file EnigmaIOTGateway.h.
|
protected |
true
if counter is used to check data messages order
Definition at line 139 of file EnigmaIOTGateway.h.
|
protected |
Wifi configuration portal.
Definition at line 151 of file EnigmaIOTGateway.h.