|
EnigmaIOT
0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
|
Go to the documentation of this file.
13 using namespace placeholders;
50 DEBUG_INFO (
"Response: %s", response);
51 request->send (200,
"application/json", response);
55 DEBUG_INFO (
"Build Gateway Info");
58 snprintf (gwInfo, len,
"{\"version\":\"%d.%d.%d\",\"network\":\"%s\",\"addresses\":{\"AP\":\"%s\",\"STA\":\"%s\"},"
59 "\"channel\":%d,\"ap\":\"%s\",\"bssid\":\"%s\",\"rssi\":%d,"
63 "\"dns\":\"%s\",\"mem\":%d}",
66 WiFi.macAddress ().c_str (), WiFi.softAPmacAddress ().c_str (),
67 WiFi.channel (), WiFi.SSID ().c_str (), WiFi.BSSIDstr ().c_str (), WiFi.RSSI (),
69 (
float)(WiFi.getTxPower ()) / 4,
71 WiFi.dnsIP ().toString ().c_str (),
74 DEBUG_DBG (
"GwInfo: %s", gwInfo);
85 if (resultCode == 404) {
86 snprintf (response,
RESPONSE_SIZE,
"{\"result\":\"not found\"}");
88 DEBUG_DBG (
"Response: %d --> %s", resultCode, response);
89 request->send (resultCode,
"application/json", response);
94 int params = request->params ();
97 for (
int i = 0; i < params; i++) {
98 AsyncWebParameter* p = request->getParam (i);
101 nodeIndex = atoi (p->value ().c_str ());
102 DEBUG_INFO (
"Node to process is %d", nodeIndex);
110 DEBUG_INFO (
"Node to process is %s", node ? node->
getNodeName () :
"NULL");
112 DEBUG_INFO (
"Wrong node name %s", p->value ().c_str ());
118 uint8_t* addrResult =
str2mac (p->value ().c_str (), addr);
122 DEBUG_INFO (
"Node to process is %s", p ? p->value ().c_str () :
"NULL");
127 DEBUG_DBG (
"Parameter %s = %s", p->name ().c_str (), p ? p->value ().c_str () :
"NULL");
130 DEBUG_DBG (
"NodeId = %d, node: %p", nodeIndex, node);
142 DEBUG_DBG (
"Node %d is %p", node->
getNodeId (), node);
144 DEBUG_INFO (
"Node %d is registered", node->
getNodeId ());
147 return "{\"result\":\"ok\"}";
149 DEBUG_INFO (
"Node %d is not registered", node->
getNodeId ());
157 DEBUG_DBG (
"Node %d is %p", node->
getNodeId (), node);
159 DEBUG_INFO (
"Node %d is registered", node->
getNodeId ());
161 time_t currentMillis = millis ();
164 index = snprintf (nodeInfo, len,
165 "{\"version\":\"%d.%d.%d\",\"node_id\":%d,\"address\":\"" MACSTR "\","\
166 "\"keyValidSince\":%lld,\"lastMessageTime\":%lld,\"sleepy\":%s,"\
167 "\"Broadcast\":%s,\"TimeSync\":%s,\"rssi\":%d,\"packetsHour\":%f,\"per\":%f",
168 version[0], version[1], version[2],
181 if (nodeName && strlen (nodeName)) {
182 index = index + snprintf (nodeInfo + index, len - index,
",\"Name\":\"%s\"", nodeName);
184 snprintf (nodeInfo + index, len - index,
"}");
185 DEBUG_DBG (
"NodeInfo: %s", nodeInfo);
188 DEBUG_INFO (
"Node %d is not registered", node->
getNodeId ());
200 int resultCode = 404;
203 node = getNodeFromParam (request);
205 DEBUG_WARN (
"Send restart command to node %p", node);
207 bool result = restartNodeRequest (node);
209 snprintf (response, 30,
"{\"node_restart\":\"processed\"}");
212 if (resultCode == 404) {
213 snprintf (response, 25,
"{\"result\":\"not found\"}");
215 DEBUG_WARN (
"Response: %d --> %s", resultCode, response);
216 request->send (resultCode,
"application/json", response);
221 int resultCode = 404;
224 node = getNodeFromParam (request);
226 WebRequestMethodComposite method = request->method ();
227 DEBUG_INFO (
"Method: %s",
methodToString (request->method ()).c_str ());
229 if (method == HTTP_DELETE) {
230 DEBUG_INFO (
"Delete node %p", node);
231 const char* strTemp = deleteNode (node, resultCode);
235 }
else if (method == HTTP_GET) {
236 DEBUG_INFO (
"Info node %p", node);
237 const char* strTemp = getNodeInfo (node, resultCode, response,
RESPONSE_SIZE);
238 DEBUG_DBG (
"strTemp = %p", strTemp);
244 if (resultCode == 404) {
245 snprintf (response, 25,
"{\"result\":\"not found\"}");
247 DEBUG_DBG (
"Response: %d --> %s", resultCode, response);
248 request->send (resultCode,
"application/json", response);
254 snprintf (response, 25,
"{\"maxNodes\":%d}",
NUM_NODES);
255 DEBUG_INFO (
"Response: %s", response);
256 request->send (200,
"application/json", response);
261 bool confirm =
false;
262 int resultCode = 404;
264 int params = request->params ();
266 for (
int i = 0; i < params; i++) {
267 AsyncWebParameter* p = request->getParam (i);
269 if (p->value () ==
"1") {
278 snprintf (response, 30,
"{\"gw_restart\":\"processed\"}");
279 request->send (resultCode,
"application/json", response);
281 snprintf (response, 25,
"{\"gw_restart\":\"fail\"}");
282 request->send (resultCode,
"application/json", response);
285 DEBUG_INFO (
"Response: %s", response);
294 bool confirm =
false;
295 int resultCode = 404;
297 int params = request->params ();
299 for (
int i = 0; i < params; i++) {
300 AsyncWebParameter* p = request->getParam (i);
302 if (p->value () ==
"1") {
311 snprintf (response, 30,
"{\"gw_reset\":\"processed\"}");
312 request->send (resultCode,
"application/json", response);
314 snprintf (response, 25,
"{\"gw_reset\":\"fail\"}");
315 request->send (resultCode,
"application/json", response);
318 DEBUG_INFO (
"Response: %s", response);
328 AsyncResponseStream* response = request->beginResponseStream (
"application/json");
329 response->setCode (200);
331 response->print (
"{\"nodes\":[");
333 bool first = (node == NULL);
335 if (node && !first) {
336 DEBUG_DBG (
"First is %s, node is %p", first ?
"true" :
"false", node);
337 response->print (
',');
340 DEBUG_DBG (
"LastNode: %u, node: %p", node->
getNodeId (), node);
343 DEBUG_DBG (
"Got node. NodeId -> %u", node->
getNodeId ());
344 response->printf (
"{\"nodeId\":%u,\"address\":\"" MACSTR "\"",
348 if (nodeName && strlen (nodeName)) {
349 response->printf (
", \"name\":\"%s\"", node->
getNodeName ());
351 response->print (
"}");
353 }
while (node != NULL);
354 response->print (
"]}");
355 request->send (response);
361 return String (
"GET");
363 return String (
"POST");
365 return String (
"DELETE");
367 return String (
"PUT");
369 return String (
"PATCH");
371 return String (
"HEAD");
373 return String (
"OPTIONS");
379 DEBUG_WARN (
"404 Not found: %s", request->url ().c_str ());
380 DEBUG_WARN (
"Method: %s",
methodToString (request->method ()).c_str ());
381 int params = request->params ();
382 for (
int i = 0; i < params; i++) {
383 #if DEBUG_LEVEL >= INFO
384 AsyncWebParameter* p = request->getParam (i);
385 DEBUG_INFO (
"Parameter %s = %s", p->name ().c_str (), p->value ().c_str ());
388 request->send (404,
"text/plain",
"Not Found");
void onNotFound(AsyncWebServerRequest *request)
Processes unknown entry points or methods.
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...
const char * getGwRestartUri
static void doResetConfig(void)
Activates a flag that signals that configuration has to be saved.
Node * getNodeFromName(const char *name)
Gets node that correspond with given node name.
static const size_t ENIGMAIOT_ADDR_LEN
Address size. Mac address = 6 bytes.
void getMaxNodes(AsyncWebServerRequest *request)
Processes max node number request.
uint8_t * getVersion()
Gets node EnigmaIOT version.
GatewayAPI GwAPI
API instance.
bool broadcastIsEnabled()
Returns if node broadcast mode is enabled. In that case, node is able to send and receive encrypted b...
const char * getGwInfoUri
uint8_t * str2mac(const char *macAddrString, uint8_t *macBytes)
Debug helper function that creates MAC address byte array from text representation.
Node * getNodeFromMAC(const uint8_t *mac)
Gets node that correspond with given address.
Node * getNodeFromParam(AsyncWebServerRequest *request)
Gets node reference from request parameters.
void resetGw(AsyncWebServerRequest *request)
Processes gateway configuration reset request.
static const uint8_t ENIGMAIOT_PROT_VERS[3]
EnitmaIoT Version.
char * getNetworkName()
Gets EnigmaIOT network name.
String methodToString(WebRequestMethodComposite method)
bool restartNodeRequest(Node *node)
Sends restart node message.
void getNodes(AsyncWebServerRequest *request)
Processes node list request.
const char * getGwResettUri
const char * nodeAddrParam
bool getSleepy()
Gets node working mode regarding battery saving strategy. If node is sleepy it will turn into deep sl...
simpleEventHandler_t notifyRestartRequested
Callback function that will be invoked when a hardware restart is requested.
void nodeOp(AsyncWebServerRequest *request)
Processes node information request.
API web server to control EnigmaIOT Gateway.
uint16_t getNodeId()
Gets Node identifier.
time_t getLastMessageTime()
Gets last time that node sent a message.
const size_t RESPONSE_SIZE
Maximum API response size.
const char * confirmParam
void begin()
Starts REST API web server.
double packetsHour
Packet rate for a specific nope.
void getNodeNumber(AsyncWebServerRequest *request)
Processes node number request.
Node * getNextActiveNode(uint16_t nodeId)
Gets next active node by nodeId.
static const int NUM_NODES
Maximum number of nodes that this gateway can handle.
const char * getMaxNodesUri
char * getNodeInfo(Node *node, int &resultCode, char *nodeInfo, size_t len)
Builds node info.
void restartNode(AsyncWebServerRequest *request)
Processes node information request.
const char * deleteNode(Node *node, int &resultCode)
Processes node deletion request.
NodeList nodelist
Node database that keeps status and shared keys.
EnigmaIOTGatewayClass EnigmaIOTGateway
static const char BROADCAST_NONE_NAME[]
Name to reference broadcast node.
char * getNodeName()
Gets Node name.
static const uint8_t BROADCAST_ADDRESS[]
Broadcast address.
Class definition for a single sensor Node.
const char * getNodeRestartUri
bool isRegistered()
Gets registration state of this node.
uint8_t * getMacAddress()
Gets address from Node.
time_t getKeyValidFrom()
Gets last time that key was agreed with gateway.
char * buildGwInfo(char *gwInfo, size_t len)
Builds gateway info.
const int WEB_API_PORT
TCP port where Web API will listen through.
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.
void getGwInfo(AsyncWebServerRequest *request)
Processes gateway information request.
const char * getNodeNumberUri
double per
Current packet error rate of a specific node.
Node * getNodeFromID(uint16_t nodeId)
Gets node that correspond with given nodeId.
const char * nodeNameParam
void restartGw(AsyncWebServerRequest *request)
Processes gateway restart request.
bool isNumber(const char *input)
Checks if input string is numeric.
bool useTimeSync()
Gets node info to check if it has requested time sync ever.
int8_t getRSSI()
Gets last RSSI measurement of Gateway.