config parsing

This commit is contained in:
2022-12-23 02:35:55 +01:00
parent 7643585856
commit 479942a4e7
11 changed files with 438 additions and 105 deletions

View File

@ -1,61 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "LoRaWan_APP.h"
#if 0
/* OTAA para*/
uint8_t devEui[] = { 0x22, 0x32, 0x33, 0x00, 0x00, 0x88, 0x88, 0x02 };
uint8_t appEui[] = { 0xa0, 0x57, 0x81, 0x00, 0x01, 0x12, 0xaa, 0xf3 };
uint8_t appKey[] = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88 };
/* ABP para*/
uint8_t nwkSKey[] = { 0x15, 0xb1, 0xd0, 0xef, 0xa4, 0x63, 0xdf, 0xbe, 0x3d, 0x11, 0x18, 0x1e, 0x1e, 0xc7, 0xda,0x85 };
uint8_t appSKey[] = { 0xd7, 0x2c, 0x78, 0x75, 0x8c, 0xdc, 0xca, 0xbf, 0x55, 0xee, 0x4a, 0x77, 0x8d, 0x16, 0xef,0x67 };
uint32_t devAddr = ( uint32_t )0x007e6ae1;
void configInit() {
}
#else
config_t myConfig = {
.devEui = { 0x22, 0x32, 0x33, 0x00, 0x00, 0x88, 0x88, 0x02 },
.appEui = { 0xa0, 0x57, 0x81, 0x00, 0x01, 0x12, 0xaa, 0xf3 },
.appKey = { 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88, 0x88 },
.nwkSKey = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
.appSKey = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 },
.devAddr = 0,
.overTheAirActivation = true,
.modbus_poll_slots = {
{ .typ = INPUT_REGISTERS, .id = 7, .address = 0x01 },
{ .typ = INPUT_REGISTERS, .id = 7, .address = 0x02 },
{ .typ = UNASSIGNED, .id = 0, .address = 0 },
}
};
// these variables are defined as externals in the LoRaWAN stack
uint8_t devEui[sizeof(myConfig.devEui)];
uint8_t appEui[sizeof(myConfig.appEui)];
uint8_t appKey[sizeof(myConfig.appKey)];
uint8_t nwkSKey[sizeof(myConfig.nwkSKey)];
uint8_t appSKey[sizeof(myConfig.appSKey)];
uint32_t devAddr;
LoRaMacRegion_t loraWanRegion;
bool overTheAirActivation;
void configInit() {
memcpy(devEui, myConfig.devEui, sizeof(devEui));
memcpy(appEui, myConfig.appEui, sizeof(appEui));
memcpy(appKey, myConfig.appKey, sizeof(appKey));
memcpy(nwkSKey, myConfig.nwkSKey, sizeof(nwkSKey));
memcpy(appSKey, myConfig.appSKey, sizeof(appSKey));
devAddr = myConfig.devAddr;
loraWanRegion = LORAMAC_REGION_EU868;
overTheAirActivation = myConfig.overTheAirActivation;
}
#endif

View File

@ -1,39 +0,0 @@
#ifndef _CONFIG_H_
#define _CONFIG_H_
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#define MAX_MODBUS_POLL_SLOTS 16
#define UNASSIGNED 0
#define COILS 1
#define DISCRETE_INPUTS 2
#define HOLDING_REGISTERS 3
#define INPUT_REGISTERS 4
typedef struct modbus_poll_s {
int typ;
int id;
int address;
} modbus_poll_t;
typedef struct config_s {
uint8_t devEui[8];
uint8_t appEui[8];
uint8_t appKey[16];
uint8_t nwkSKey[16];
uint8_t appSKey[16];
uint32_t devAddr;
bool overTheAirActivation;
modbus_poll_t modbus_poll_slots[MAX_MODBUS_POLL_SLOTS];
} config_t;
void configInit();
#endif /* _CONFIG_H_ */

View File

@ -1,18 +1,67 @@
#include "configuration.h"
#include "defines.h"
#include "LoRaWan_APP.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <Wire.h>
#include "HT_SSD1306Wire.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <WiFiAP.h>
#include <sstream>
#include <string>
#include <iomanip>
const uint32_t MAGIC = 0xaffe0001;
config_t myConfig = {
.magic = MAGIC,
.appEui = { 0, 0, 0, 0, 0, 0, 0, 0 },
.appKey = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
.modbus_poll_slots = {
{ .typ = INPUT_REGISTERS, .id = 7, .address = 0x01 },
{ .typ = INPUT_REGISTERS, .id = 7, .address = 0x02 },
{ .typ = UNASSIGNED, .id = 0, .address = 0 },
}
};
uint8_t devEui[8];
uint8_t appEui[8];
uint8_t appKey[16];
uint8_t nwkSKey[16];
uint8_t appSKey[16];
uint32_t devAddr;
LoRaMacRegion_t loraWanRegion;
bool overTheAirActivation;
SSD1306Wire confDisplay(0x3c, 500000, SDA_OLED, SCL_OLED, GEOMETRY_128_64, RST_OLED);; // addr , freq , i2c group , resolution , rst
WebServer server(80);
const char *ssid = "ModbusLoraConf";
#define TEST_PASSWORD
#ifndef TEST_PASSWORD
char password[12];
#else
const char *password = "test1234";
#endif
extern uint8_t devEui[8];
bool configSaved = false;
int configParsingFailed = 0;
enum { PARSING_OK, PARSING_INVALID, PARSING_TOO_MANY, PARSING_TOO_FEW } configParsingError;
static void displayStatus(uint8_t numOfStations) {
confDisplay.clear();
@ -43,17 +92,142 @@ static void displayInit() {
confDisplay.display();
}
static void handleRoot() {
// bool configValid = (configBlock.magic == MAGIC);
std::stringstream buffer;
buffer << "<!doctype html"
"<html lang=\"en\">"
" <head>"
" <title>Modbus LoRaWAN Gateway</title>"
" </head>"
" <body>"
" <h1>Modbus LoRaWAN Gateway - Configuration Page</h1>";
if (configSaved) {
configSaved = false;
buffer << "<h2>Configuration saved</h2>";
}
if (configParsingFailed) {
configParsingFailed = 0;
buffer << "<h2>Error when parsing field " << configParsingFailed << ", error " << configParsingError << "</h2>";
}
buffer << " <form action=\"config\" method=\"GET\">"
" <table>"
" <tr>"
" <td>"
" DevEui"
" </td><td>";
buffer << " <input type=\"text\" readonly name=\"DevEui\" id=\"DevEui\" size=\"23\" value=\"";
for (int i = 0; i < 8; i++) {
if (i != 0) {
buffer << ":";
}
buffer << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (int)devEui[i];
}
buffer << "\"> (readonly)";
buffer << " </td>"
" </tr><tr>"
" <td>"
" AppEui"
" </td><td>";
buffer << " <input type=\"text\" name=\"AppEui\" id=\"AppEui\" size=\"23\" value=\"";
for (int i = 0; i < 8; i++) {
if (i != 0) {
buffer << ":";
}
buffer << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (int)myConfig.appEui[i];
}
buffer << "\">";
buffer << " </td>"
" </tr><tr>"
" <td>"
" AppKey"
" </td><td>";
buffer << " <input type=\"text\" name=\"AppKey\" id=\"AppKey\" size=\"47\" value=\"";
for (int i = 0; i < 16; i++) {
if (i != 0) {
buffer << ":";
}
buffer << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (int)myConfig.appKey[i];
}
buffer << "\">";
buffer << " </td>"
" </tr>"
" <tr><td colspan=\"2\">"
" <button type=\"submit\">Save</button>"
" </td></tr>"
" </table>"
" </form>"
" </body"
"</html";
server.send(200, "text/html", buffer.str().c_str());
}
static void parseField(char* arg, int fieldNum, int reqTokens, uint8_t *dest) {
int i = 0;
for (char *part = strtok(arg, ":"); part; part = strtok(NULL, ":")) {
Serial.println(part);
char *errPtr;
long v = strtol(part, &errPtr, 16);
if (errPtr && *errPtr) {
Serial.printf("some error happened, %p %02x\n\r", errPtr, *errPtr);
configParsingFailed = fieldNum;
configParsingError = PARSING_INVALID;
break;
}
if (i >= reqTokens) {
Serial.println("too many tokens");
configParsingFailed = fieldNum;
configParsingError = PARSING_TOO_MANY;
break;
}
dest[i] = v;
i++;
}
if ((i != reqTokens) && (configParsingFailed == 0)) {
Serial.println("too few tokens");
configParsingFailed = fieldNum;
configParsingError = PARSING_TOO_FEW;
}
}
static void handleConfigSave() {
char *arg1 = (char*)server.arg("AppEui").c_str();
Serial.printf("AppEui: %s\n\r", arg1);
parseField(arg1, 1, 8, myConfig.appEui);
if (!configParsingFailed) {
char *arg2 = (char*)server.arg("AppKey").c_str();
Serial.printf("AppKey: %s\n\r", arg2);
parseField(arg2, 2, 16, myConfig.appKey);
}
configSaved = !configParsingFailed;
server.sendHeader("Location", String("/"), true);
server.send(302, "text/plain", "");
}
void configurationSetup() {
#ifndef TEST_PASSWORD
memset(password, 0, sizeof(password));
for (int i = 0; i < sizeof(password) - 1; i++) {
password[i] = random(65, 90);
}
#endif
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
displayInit();
displayStatus(0);
LoRaWAN.generateDeveuiByChipID();
server.on("/", handleRoot);
server.on("/config", handleConfigSave);
server.begin();
}
@ -65,4 +239,14 @@ void configurationLoop() {
numOfStations = currentNumOfStations;
}
server.handleClient();
}
void configLoad() {
memcpy(appEui, myConfig.appEui, sizeof(appEui));
memcpy(appKey, myConfig.appKey, sizeof(appKey));
loraWanRegion = LORAMAC_REGION_EU868;
overTheAirActivation = true;
}

View File

@ -2,8 +2,39 @@
#define _CONFIGURATION_H_
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#define MAX_MODBUS_POLL_SLOTS 16
#define UNASSIGNED 0
#define COILS 1
#define DISCRETE_INPUTS 2
#define HOLDING_REGISTERS 3
#define INPUT_REGISTERS 4
typedef struct modbus_poll_s {
int typ;
int id;
int address;
} modbus_poll_t;
typedef struct config_s {
uint32_t magic;
uint8_t appEui[8];
uint8_t appKey[16];
modbus_poll_t modbus_poll_slots[MAX_MODBUS_POLL_SLOTS];
} config_t;
void configurationSetup();
void configurationLoop();
void configLoad();
#endif // _CONFIGURATION_H_

View File

@ -2,7 +2,7 @@
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>
#include "defines.h"
#include "config.h"
#include "configuration.h"
#include <string.h>
@ -121,9 +121,7 @@ void productionLoop()
case DEVICE_STATE_INIT:
digitalWrite(LED_GREEN, LOW);
{
#if(LORAWAN_DEVEUI_AUTO)
LoRaWAN.generateDeveuiByChipID();
#endif
LoRaWAN.init(loraWanClass,loraWanRegion);
break;
}

View File

@ -20,7 +20,7 @@ void setup() {
Serial.begin(115200);
configInit();
configLoad();
if (productionMode) {
productionSetup();