From 6ee1650381b6b0d5ef30d4696fd3c0f072e2f3c1 Mon Sep 17 00:00:00 2001 From: Piotr Dobrowolski Date: Sun, 1 Oct 2017 23:24:41 +0200 Subject: [PATCH] Generic firmware proof of concept --- base-firmware/Makefile | 1 + base-firmware/Makefile-user.mk | 2 ++ base-firmware/app/application.cpp | 13 +++++++ base-firmware/files/config.json | 14 ++++++++ spejsiot/Endpoint.h | 27 +++------------ spejsiot/SpejsNode.cpp | 53 ++++++++++++++++++++++------- spejsiot/SpejsNode.h | 4 ++- spejsiot/endpoints/OutputEndpoint.h | 8 +++++ 8 files changed, 86 insertions(+), 36 deletions(-) create mode 120000 base-firmware/Makefile create mode 100644 base-firmware/Makefile-user.mk create mode 100644 base-firmware/app/application.cpp create mode 100644 base-firmware/files/config.json diff --git a/base-firmware/Makefile b/base-firmware/Makefile new file mode 120000 index 0000000..d0b0e8e --- /dev/null +++ b/base-firmware/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/base-firmware/Makefile-user.mk b/base-firmware/Makefile-user.mk new file mode 100644 index 0000000..73a4d8f --- /dev/null +++ b/base-firmware/Makefile-user.mk @@ -0,0 +1,2 @@ +SPIFF_SIZE = 196600 +DISABLE_SPIFFS = 0 diff --git a/base-firmware/app/application.cpp b/base-firmware/app/application.cpp new file mode 100644 index 0000000..291766e --- /dev/null +++ b/base-firmware/app/application.cpp @@ -0,0 +1,13 @@ +#include +#include +#include + +SpejsNode node("unconfigured-generic-device"); + +void init() { + node.init(); + node.loadJSON({ + &OutputEndpoint::fromJSON, + &DHTEndpoint::fromJSON, + }); +} diff --git a/base-firmware/files/config.json b/base-firmware/files/config.json new file mode 100644 index 0000000..df09c94 --- /dev/null +++ b/base-firmware/files/config.json @@ -0,0 +1,14 @@ +{ + "name": "testdev", + + "extras": { + "owner": "informatic", + "description": "This is just a testing device" + }, + + "endpoints": { + "out": {"type": "output", "gpio": 2}, + "testInput": {"type": "input", "gpio": 3}, + "testtemp": {"type": "dht"} + } +} diff --git a/spejsiot/Endpoint.h b/spejsiot/Endpoint.h index 2a3bb09..d145f2d 100644 --- a/spejsiot/Endpoint.h +++ b/spejsiot/Endpoint.h @@ -34,6 +34,10 @@ public: } virtual void onConnected(); + + static Endpoint* fromJSON(JsonObject& obj) { + return NULL; + } }; template class ValueEndpoint : public Endpoint { @@ -45,27 +49,6 @@ public: ValueEndpoint(String _type) : Endpoint(_type) {} }; -struct endpoint_def { - char* name; - Endpoint* (*constructor)(void); -}; - -//extern int __papiez_pedofil; - -extern struct endpoint_def* __stop_endpoints[]; -extern struct endpoint_def* __start_endpoints[]; - - -#define DECLARE_ENDPOINT(name, constructor) \ - struct endpoint_def name ## _def __attribute__ ((section (".endpoints"))) = {\ - (char*) #name, constructor \ - }; - -// static struct endpoint_def name ## _def = { \ -// (char*) #name, constructor \ -// }; \ -//& name##_def - -#define ENDPOINTS_COUNT (((uint8_t*) __stop_endpoints - (uint8_t*) __start_endpoints) / sizeof(struct endpoint_def)) +typedef Endpoint* (*EndpointInitializer) (JsonObject&); #endif diff --git a/spejsiot/SpejsNode.cpp b/spejsiot/SpejsNode.cpp index 237b93c..fbdcb76 100644 --- a/spejsiot/SpejsNode.cpp +++ b/spejsiot/SpejsNode.cpp @@ -4,6 +4,8 @@ #include +#define CONFIG_FILE "config.json" + void SpejsNode::init() { deviceID = WifiStation.getMAC().substring(6, 12); @@ -29,26 +31,51 @@ void SpejsNode::init() { // Keepalive Timer initialization keepaliveTimer.initializeMs(10000, TimerDelegate(&SpejsNode::keepAliveHandler, this)).start(); - loadJSON(); - statusLED.high(); } -//extern int __papiez_pedofil; +void SpejsNode::loadJSON(std::vector initializers) { +#ifdef RBOOT_SPIFFS_0 + debugf("trying to mount spiffs at 0x%08x, length %d", RBOOT_SPIFFS_0, SPIFF_SIZE); + spiffs_mount_manual(RBOOT_SPIFFS_0, SPIFF_SIZE); +#else + debugf("trying to mount spiffs at 0x%08x, length %d", 0x100000, SPIFF_SIZE); + spiffs_mount_manual(0x100000, SPIFF_SIZE); +#endif -void SpejsNode::loadJSON() { - /* - Serial.printf("start: %08x\n", __start_endpoints); - Serial.printf("stop: %08x\n", __stop_endpoints); - Serial.printf("Endpoints count: %d\n", ENDPOINTS_COUNT); + DynamicJsonBuffer jsonBuffer; + if (fileExist(CONFIG_FILE)) { + Serial.println("Found config file"); + int size = fileGetSize(CONFIG_FILE); + Serial.printf("%d bytes\n", size); + char* jsonString = new char[size + 1]; + fileGetContent(CONFIG_FILE, jsonString, size + 1); + JsonObject& root = jsonBuffer.parseObject(jsonString); + if (root.containsKey("name")) + deviceType = (root["name"]).asString(); - for(int i = 0; i < ENDPOINTS_COUNT; i++) { - struct endpoint_def* endpoint = __start_endpoints[i]; - Serial.printf(" -> %08x\r\n", endpoint); - Serial.printf(" -> name: %s\r\n", endpoint->name); + JsonObject& data = root["endpoints"].asObject(); + for (auto it: data) { + bool found = false; + + for(auto init: initializers) { + Endpoint* ep = init(it.value); + if (ep != NULL) { + Serial.printf("%s: got object\n", it.key); + registerEndpoint(it.key, ep); + found = true; + break; + } + } + + if (!found) { + Serial.printf("%s: nothing found\n", it.key); + } + } + } else { + Serial.println("No configuration"); } - */ } void SpejsNode::keepAliveHandler() { diff --git a/spejsiot/SpejsNode.h b/spejsiot/SpejsNode.h index 6e19969..a5cfec2 100644 --- a/spejsiot/SpejsNode.h +++ b/spejsiot/SpejsNode.h @@ -6,6 +6,8 @@ #include #include +#include + #define D0 16 #define D1 5 #define D2 4 @@ -95,7 +97,7 @@ public: String DEV_TOPIC(String t); - void loadJSON(); + void loadJSON(std::vector); }; #endif diff --git a/spejsiot/endpoints/OutputEndpoint.h b/spejsiot/endpoints/OutputEndpoint.h index 7ac85be..78b7ddc 100644 --- a/spejsiot/endpoints/OutputEndpoint.h +++ b/spejsiot/endpoints/OutputEndpoint.h @@ -17,6 +17,14 @@ public: } EndpointResult onValue(String property, String value); + + static Endpoint* fromJSON(JsonObject& obj) { + if (String(obj["type"].asString()) == "output" && obj.containsKey("gpio")) { + return new OutputEndpoint(obj["gpio"], obj["inverted"]); + } + + return NULL; + } }; #endif /* ifndef OUTPUTENDPOINT_H */