 |
EnigmaIOT
0.9.3
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>
29 #define LED_OFF !LED_ON
73 #if defined ARDUINO_ARCH_ESP8266 || defined ARDUINO_ARCH_ESP32
76 typedef std::function<void (uint8_t* mac, uint16_t node_id,
char* nodeName)>
onNewNode_t;
82 typedef void (*
onNewNode_t)(uint8_t* mac, uint16_t node_id,
char* nodeName);
104 template <
typename Telement>
147 DEBUG_DBG (
"Add element. Buffer was %s\n", wasFull ?
"full" :
"not full");
172 bool wasEmpty =
empty ();
173 DEBUG_DBG (
"Remove element. Buffer was %s\n", wasEmpty ?
"empty" :
"not empty");
224 portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
239 static void doSave (
void);
352 void manageMessage (
const uint8_t* mac, uint8_t* buf, uint8_t count);
360 static void rx_cb (uint8_t* mac_addr, uint8_t*
data, uint8_t len);
368 static void tx_cb (uint8_t* mac_addr, uint8_t
status);
509 double getPER (uint8_t* address);
607 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.
int readIndex
Pointer to next item to be read.
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.
Telement * buffer
Actual buffer.
bool useCounter
true if counter is used to check data messages order
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.
Ring buffer class. Used to implement message buffer.
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.
int size()
Returns actual number of elements that buffer holds.
msg_queue_item_t tempBuffer
Temporary storage for input message got from buffer.
bool push(Telement *item)
Adds a new item to buffer, deleting older element if it is full.
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.
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]
void(* onWiFiManagerStarted_t)(void)
onGwDataRx_t notifyData
Callback function that will be invoked when data is received fron a node.
EnigmaIOTRingBuffer< msg_queue_item_t > * input_queue
Input messages buffer. It acts as a FIFO queue.
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
char * getNetworkName()
Gets EnigmaIOT network name.
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.
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.
bool empty()
Checks if buffer is empty.
bool clockResponse(Node *node)
Returns timestaps needed so that node can calculate time difference.
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)
bool pop()
Deletes older item from buffer, if buffer is not empty.
Telement * front()
Gets a pointer to older item in buffer, if buffer is not empty.
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.
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.
bool isFull()
Checks if buffer is full.
NodeList nodelist
Node database that keeps status and shared keys.
NodeList * getNodes()
Gets nodes data structure.
static const uint8_t MAX_MESSAGE_LENGTH
Maximum payload size on ESP-NOW.
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.
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.
onWiFiManagerStarted_t notifyWiFiManagerStarted
Function called when configuration portal is started.
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.
static const uint8_t NETWORK_NAME_LENGTH
Maximum number of characters of network name.
void(* onGwDataRx_t)(uint8_t *mac, uint8_t *data, uint8_t len, uint16_t lostMessages, bool control, gatewayPayloadEncoding_t payload_type, char *nodeName)
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.
int numElements
Number of elements that buffer currently has.
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 messange and send it to node.
bool loadFlashData()
Loads configuration from flash memory.
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.
const uint8_t KEY_LENGTH
Key length used by selected crypto algorythm. The only tested value is 32. Change it only if you know...
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.
void onWiFiManagerStarted(onWiFiManagerStarted_t handle)
Register callback to be called on wifi manager start.
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.
void popInputMsgQueue()
Deletes next item in the queue.
Filter to process message rate or other values.
int writeIndex
Pointer to next position to write onto.