|
EnigmaIOT
0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
|
Go to the documentation of this file.
9 #ifndef _ENIGMAIOTGATEWAY_h
10 #define _ENIGMAIOTGATEWAY_h
12 #if defined(ARDUINO) && ARDUINO >= 100
21 #include <ESPAsyncWebServer.h>
22 #include <ESPAsyncWiFiManager.h>
23 #include <DNSServer.h>
28 #endif // ENABLE_REST_API
33 #define LED_OFF !LED_ON
85 #if defined ARDUINO_ARCH_ESP8266 || defined ARDUINO_ARCH_ESP32
88 #if SUPPORT_HA_DISCOVERY
89 typedef std::function<void (
const char* topic,
char *message,
size_t len)> onHADiscovery_t;
91 typedef std::function<void (uint8_t* mac, uint16_t node_id,
char* nodeName)>
onNewNode_t;
98 typedef void (*
onNewNode_t)(uint8_t* mac, uint16_t node_id,
char* nodeName);
133 #if SUPPORT_HA_DISCOVERY
143 portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
160 static void doSave (
void);
288 void manageMessage (
const uint8_t* mac, uint8_t* buf, uint8_t count);
296 static void rx_cb (uint8_t* mac_addr, uint8_t*
data, uint8_t len);
304 static void tx_cb (uint8_t* mac_addr, uint8_t
status);
325 #if SUPPORT_HA_DISCOVERY
335 bool sendHADiscoveryJSON (uint8_t* address, uint8_t*
data,
size_t len,
const char* networkName,
const char* nodeName);
453 #if SUPPORT_HA_DISCOVERY
468 double getPER (uint8_t* address);
575 bool addInputMsgQueue (
const uint8_t* addr,
const uint8_t* msg,
size_t len);
void onWiFiManagerExit(onWiFiManagerExit_t handle)
Register callback to be called on wifi manager exit.
volatile bool flashRx
true if Rx LED should flash
int getActiveNodesNumber()
Gets number of active nodes.
bool invalidateKey(Node *node, gwInvalidateReason_t reason)
Creates an InvalidateKey message and sned it. This trigger a new key agreement to start on related no...
gatewayMessageType_t
Message code definition.
Library to build a gateway for EnigmaIoT system.
bool useCounter
true if counter is used to check data messages order
static void doResetConfig(void)
Activates a flag that signals that configuration has to be saved.
uint8_t myPublicKey[KEY_LENGTH]
Temporary public key store used during key agreement.
bool addInputMsgQueue(const uint8_t *addr, const uint8_t *msg, size_t len)
Add message to input queue.
unsigned long rxLedOnTime
Flash duration for Rx LED.
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
void onNodeDisconnected(onNodeDisconnected_t handler)
Defines a function callback that will be called every time a node is disconnected.
EnigmaIoT sensor node management structures.
Main gateway class. Manages communication with nodes and sends data to upper layer.
void onDataRx(onGwDataRx_t handler)
Defines a function callback that will be called on every downlink data message that is received from ...
bool processControlMessage(const uint8_t mac[ENIGMAIOT_ADDR_LEN], uint8_t *buf, size_t count, Node *node)
Processes control message from node.
void onHADiscovery(onHADiscovery_t handler)
Defines a function callback that will be called when a Home Assistant discovery message is received f...
static const uint8_t MAX_MESSAGE_LENGTH
Maximum payload size on ESP-NOW.
msg_queue_item_t tempBuffer
Temporary storage for input message got from buffer.
void manageMessage(const uint8_t *mac, uint8_t *buf, uint8_t count)
Process every received message.
Comms_halClass * comm
Instance of physical communication layer.
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.
@ DOWNSTREAM_BRCAST_DATA_GET
int8_t txled
I/O pin to connect a led that flashes when gateway transmits data.
bool configWiFiManager()
Starts configuration AP and web server and gets settings from it.
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.
AsyncWebServer * server
WebServer that holds configuration portal.
uint8_t networkKey[KEY_LENGTH]
onGwDataRx_t notifyData
Callback function that will be invoked when data is received from a node.
EnigmaIOTRingBuffer< msg_queue_item_t > * input_queue
Input messages buffer. It acts as a FIFO queue.
char * getNetworkName()
Gets EnigmaIOT network name.
onHADiscovery_t notifyHADiscovery
Callback function that will be invoked when HomeAssistant discovery message is received from a node.
bool sendBroadcastKey(Node *node)
Sends broadcast key to node if it has requested it explicitly or it has notified during handshake.
static const uint8_t NETWORK_NAME_LENGTH
Maximum number of characters of network name.
simpleEventHandler_t notifyRestartRequested
Callback function that will be invoked when a hardware restart is requested.
EnigmaIOTGatewayClass EnigmaIOTGateway
void setRxLed(uint8_t led, time_t onTime=FLASH_LED_TIME)
Sets a LED to be flashed every time a message is received.
onWiFiManagerExit_t notifyWiFiManagerExit
Function called when configuration portal exits.
void handle()
This method should be called periodically for instance inside loop() function. It is used for interna...
void addWiFiManagerParameter(AsyncWiFiManagerParameter *p)
Adds a parameter to configuration portal.
API web server to control EnigmaIOT Gateway.
onNodeDisconnected_t notifyNodeDisconnection
Callback function that will be invoked when a node gets disconnected.
bool processUnencryptedDataMessage(const uint8_t mac[ENIGMAIOT_ADDR_LEN], uint8_t *buf, size_t count, Node *node)
Processes unencrypted data message from node.
void setTxLed(uint8_t led, time_t onTime=FLASH_LED_TIME)
Sets a LED to be flashed every time a message is transmitted.
void(* onNewNode_t)(uint8_t *mac, uint16_t node_id, char *nodeName)
void(* simpleEventHandler_t)(void)
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.
Struct that define node fields. Used for long term storage needs.
@ DOWNSTREAM_BRCAST_DATA_SET
uint32_t getTotalPackets(uint8_t *address)
Gets total packets sent by node that has a specific address.
bool flashTx
true if Tx LED should flash
DNSServer * dns
DNS server used by configuration portal.
void onNewNode(onNewNode_t handler)
Defines a function callback that will be called every time a node gets connected or reconnected.
void getStatus(uint8_t *mac_addr, uint8_t status)
Functrion to debug send status.
bool getShouldSave()
Gets flag that indicates if configuration should be saved.
NodeList nodelist
Node database that keeps status and shared keys.
bool clockResponse(Node *node, uint64_t t1, uint64_t t2)
Returns timestaps needed so that node can calculate time difference.
NodeList * getNodes()
Gets nodes data structure.
const uint8_t KEY_LENGTH
Key length used by selected crypto algorythm. The only tested value is 32. Change it only if you know...
void onWiFiManagerStarted(simpleEventHandler_t handle)
Register callback to be called on wifi manager start.
node_t node
temporary store to keep node data while processing a message
unsigned long txLedOnTime
Flash duration for Tx LED.
static void doSave(void)
Activates a flag that signals that configuration has to be saved.
simpleEventHandler_t notifyWiFiManagerStarted
Function called when configuration portal is started.
Class definition for a single sensor Node.
gateway_config_t gwConfig
Gateway specific configuration to be stored on flash memory.
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 use...
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 sendin...
bool saveFlashData()
Saves configuration to flash memory.
int8_t rxled
I/O pin to connect a led that flashes when gateway receives data.
bool processNodeNameSet(const uint8_t mac[ENIGMAIOT_ADDR_LEN], uint8_t *buf, size_t count, Node *node)
Processes new node name request fromn node.
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.
void(* onWiFiManagerExit_t)(boolean status)
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.
static const uint8_t DEFAULT_CHANNEL
WiFi channel to be used on ESP-NOW.
uint32_t getErrorPackets(uint8_t *address)
Gets number of errored packets of node that has a specific address.
void(* onGwDataRx_t)(uint8_t *mac, uint8_t *data, uint8_t len, uint16_t lostMessages, bool control, gatewayPayloadEncoding_t payload_type, char *nodeName)
void onGatewayRestartRequested(simpleEventHandler_t handler)
Defines a function callback that will process a gateway restart request.
onNewNode_t notifyNewNode
Callback function that will be invoked when a new node is connected.
double getPacketsHour(uint8_t *address)
Gets packet rate sent by node that has a specific address, in packets per hour.
Auxiliary function definition.
static const uint32_t FLASH_LED_TIME
Time that led keeps on during flash in ms.
gwInvalidateReason_t
Key invalidation reason definition.
bool serverHello(const uint8_t *key, Node *node)
Build a ServerHello message and send it to node.
bool loadFlashData()
Loads configuration from flash memory.
@ DOWNSTREAM_BRCAST_CTRL_DATA
uint16_t countActiveNodes()
Gets the number of active nodes (registered or registering)
enum control_message_type control_message_type_t
bool nodeNameSetRespose(Node *node, int8_t error)
Send back set name response.
double getPER(uint8_t *address)
Gets packet error rate of node that has a specific address.
char plainNetKey[KEY_LENGTH]
Generic communication system abstraction layer.
char * getNetworkKey(bool plain=false)
Gets hashed EnigmaIOT network key.
void begin(Comms_halClass *comm, uint8_t *networkKey=NULL, bool useDataCounter=true)
Initalizes communication basic data and starts accepting node registration.
char networkName[NETWORK_NAME_LENGTH]
void(* onNodeDisconnected_t)(uint8_t *mac, gwInvalidateReason_t reason)
AsyncWiFiManager * wifiManager
Wifi configuration portal.
msg_queue_item_t * getInputMsgQueue(msg_queue_item_t *buffer)
Gets next item in the queue.
Interface for communication subsystem abstraction layer definition.
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.
void popInputMsgQueue()
Deletes next item in the queue.
Filter to process message rate or other values.