From c3b11261df0c6420abada755541439e783b647d7 Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Wed, 11 Dec 2019 18:44:16 +0000 Subject: [PATCH 01/13] Setup PlatformIO and TravisCI (#65) --- .gitignore | 1 + .travis.yml | 30 ++++++++++++++++++++++++++++++ platformio.ini | 28 ++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 .travis.yml create mode 100644 platformio.ini diff --git a/.gitignore b/.gitignore index db03415..7558808 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .pioenvs +.pio .clang_complete .gcc-flags.json diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..785345f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,30 @@ +# Continuous Integration (CI) is the practice, in software +# engineering, of merging all developer working copies with a shared mainline +# several times a day < https://docs.platformio.org/page/ci/index.html > +# +# Documentation: +# +# * Travis CI Embedded Builds with PlatformIO +# < https://docs.travis-ci.com/user/integration/platformio/ > +# +# * PlatformIO integration with Travis CI +# < https://docs.platformio.org/page/ci/travis.html > +# +# * User Guide for `platformio ci` command +# < https://docs.platformio.org/page/userguide/cmd_ci.html > +# +language: python +python: + - "3.7.5" + +sudo: false +cache: + directories: + - "~/.platformio" + +install: + - pip install -U platformio + - platformio update + +script: + - platformio ci --lib="./src" -c platformio.ini ./examples/simple/ diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..e52187c --- /dev/null +++ b/platformio.ini @@ -0,0 +1,28 @@ +[platformio] +default_envs = nodemcuv2 + +[common] +lib_deps = + ArduinoJson@5.13.4 + +[env:nodemcuv2] +platform = https://github.com/platformio/platform-espressif8266.git +board = d1_mini +framework = arduino +monitor_speed = 115200 +build_flags = -std=c++11, -Wl,-Teagle.flash.4m3m.ld, -D PIO_FRAMEWORK_ARDUINO_LWIP_HIGHER_BANDWIDTH, -DARDUINO_ARCH_ESP8266 +lib_ldf_mode = deep +board_build.f_flash = 80000000L +upload_speed = 115200 +lib_deps = ${common.lib_deps} +test_filter = embedded + +[env:esp32] +platform = https://github.com/platformio/platform-espressif32.git +board = esp-wrover-kit +framework = arduino +board_build.partitions = noota_3g.csv +build_flags = -std=c++11, -DARDUINO_ARCH_ESP32 +lib_ldf_mode = deep +lib_deps = ${common.lib_deps} +test_filter = embedded From e6b5761b9560802a3d9bd6db518c5fa188fde13f Mon Sep 17 00:00:00 2001 From: Nicholas Wiersma Date: Thu, 12 Dec 2019 06:55:30 +0200 Subject: [PATCH 02/13] Fix ConfigManager Bugs (#66) --- .gitignore | 1 + .travis.yml | 25 ++++++++----------------- README.md | 5 +++-- src/ConfigManager.cpp | 24 ++++++++++++------------ src/ConfigManager.h | 12 +++++++++--- 5 files changed, 33 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 7558808..cc2351c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/.pio .pioenvs .pio diff --git a/.travis.yml b/.travis.yml index 785345f..1222d6c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,23 +1,9 @@ -# Continuous Integration (CI) is the practice, in software -# engineering, of merging all developer working copies with a shared mainline -# several times a day < https://docs.platformio.org/page/ci/index.html > -# -# Documentation: -# -# * Travis CI Embedded Builds with PlatformIO -# < https://docs.travis-ci.com/user/integration/platformio/ > -# -# * PlatformIO integration with Travis CI -# < https://docs.platformio.org/page/ci/travis.html > -# -# * User Guide for `platformio ci` command -# < https://docs.platformio.org/page/userguide/cmd_ci.html > -# +sudo: false + language: python python: - - "3.7.5" + - "2.7" -sudo: false cache: directories: - "~/.platformio" @@ -28,3 +14,8 @@ install: script: - platformio ci --lib="./src" -c platformio.ini ./examples/simple/ + +notifications: + email: + on_success: never + on_failure: always diff --git a/README.md b/README.md index fcbf443..ee7d96e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ ![Logo](http://svg.wiersma.co.za/github/project?lang=cpp&title=ConfigManager&tag=wifi%20configuration%20manager) +[![Build Status](https://travis-ci.com/nrwiersma/ConfigManager.svg?branch=master)](https://travis-ci.com/nrwiersma/ConfigManager) [![arduino-library-badge](http://www.ardu-badge.com/badge/ConfigManager.svg)](http://www.ardu-badge.com/ConfigManager) Wifi connection and configuration manager for ESP8266 and ESP32. @@ -109,7 +110,7 @@ on the most common IDEs: ### Enabling -By default, ConfigManager runs in `DEBUG_MODE` off. This is to allow the serial iterface to communicate as needed. +By default, ConfigManager runs in `DEBUG_MODE` off. This is to allow the serial iterface to communicate as needed. To turn on debugging, add the following line inside your `setup` routine: ``` @@ -295,7 +296,7 @@ ssid=access point&password=some password ```json { "ssid": "access point name", - "strength": *int*, + "strength": *int*, "security": *bool* } ``` diff --git a/src/ConfigManager.cpp b/src/ConfigManager.cpp index ff024c6..c56f07f 100644 --- a/src/ConfigManager.cpp +++ b/src/ConfigManager.cpp @@ -2,7 +2,7 @@ const byte DNS_PORT = 53; const char magicBytes[MAGIC_LENGTH] = {'C', 'M'}; -const char magicBytesEmpty[MAGIC_LENGTH] = {NULL, NULL}; +const char magicBytesEmpty[MAGIC_LENGTH] = {'\0', '\0'}; const char mimeHTML[] PROGMEM = "text/html"; const char mimeJSON[] PROGMEM = "application/json"; @@ -128,7 +128,7 @@ void ConfigManager::handleAPPost() { storeWifiSettings(ssid, password, false); server->send(204, FPSTR(mimePlain), F("Saved. Will attempt to reboot.")); - + ESP.restart(); } @@ -148,7 +148,7 @@ void ConfigManager::handleScanGet() { for (int i = 0; i < n; ++i) { String ssid = WiFi.SSID(i); int rssi = WiFi.RSSI(i); - String security = WiFi.encryptionType(i) == ENC_TYPE_NONE ? "none" : "enabled"; + String security = WiFi.encryptionType(i) == WIFI_OPEN ? "none" : "enabled"; DebugPrint("Name: "); DebugPrint(ssid); @@ -215,12 +215,12 @@ void ConfigManager::handleRESTPut() { void ConfigManager::handleNotFound() { String URI = toStringIP(server->client().localIP()) + String(":") + String(webPort); String header = server->hostHeader(); - + if ( !isIp(header) && header != URI) { DebugPrint(F("Unknown URL: ")); DebugPrintln(header); server->sendHeader("Location", String("http://") + URI, true); - server->send(302, FPSTR(mimePlain), ""); + server->send(302, FPSTR(mimePlain), ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. server->client().stop(); return; @@ -298,13 +298,13 @@ void ConfigManager::startAP() { WiFi.mode(WIFI_AP); WiFi.softAP(apName, apPassword); - + delay(500); // Need to wait to get IP - + IPAddress ip(192, 168, 1, 1); IPAddress NMask(255, 255, 255, 0); WiFi.softAPConfig(ip, ip, NMask); - + DebugPrint("AP Name: "); DebugPrintln(apName); @@ -348,7 +348,7 @@ void ConfigManager::createBaseWebServer() { server.reset(new WebServer(this->webPort)); DebugPrint(F("Webserver enabled on port: ")); DebugPrintln(webPort); - + server->collectHeaders(headerKeys, headerKeysSize); server->on("/", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleAPGet, this)); @@ -361,8 +361,8 @@ void ConfigManager::createBaseWebServer() { void ConfigManager::clearWifiSettings(bool reboot) { char ssid[SSID_LENGTH]; char password[PASSWORD_LENGTH]; - memset(ssid, NULL, SSID_LENGTH); - memset(password, NULL, PASSWORD_LENGTH); + memset(ssid, 0, SSID_LENGTH); + memset(password, 0, PASSWORD_LENGTH); DebugPrintln(F("Clearing WiFi connection.")); storeWifiSettings(ssid, password, true); @@ -429,7 +429,7 @@ void ConfigManager::writeConfig() { } boolean ConfigManager::isIp(String str) { - for (int i = 0; i < str.length(); i++) { + for (uint i = 0; i < str.length(); i++) { int c = str.charAt(i); if (c != '.' && (c < '0' || c > '9')) { return false; diff --git a/src/ConfigManager.h b/src/ConfigManager.h index 25aa2d7..5363a51 100644 --- a/src/ConfigManager.h +++ b/src/ConfigManager.h @@ -18,6 +18,12 @@ #include #include "ArduinoJson.h" +#if defined(ARDUINO_ARCH_ESP8266) //ESP8266 + #define WIFI_OPEN ENC_TYPE_NONE +#elif defined(ARDUINO_ARCH_ESP32) //ESP32 + #define WIFI_OPEN WIFI_AUTH_OPEN +#endif + #define MAGIC_LENGTH 2 #define SSID_LENGTH 32 #define PASSWORD_LENGTH 64 @@ -111,7 +117,7 @@ class ConfigStringParameter : public BaseParameter { if (json->containsKey(name) && json->is(name)) { const char * value = json->get(name); - memset(ptr, NULL, length); + memset(ptr, 0, length); strncpy(ptr, const_cast(value), length - 1); } } @@ -123,9 +129,9 @@ class ConfigStringParameter : public BaseParameter { void clearData() { DebugPrint("Clearing: "); DebugPrintln(name); - memset(ptr, NULL, length); + memset(ptr, 0, length); } - + private: const char *name; From 73744440971d23b7cd6f2e44cfa6584d6f72857d Mon Sep 17 00:00:00 2001 From: Nicholas Wiersma Date: Thu, 12 Dec 2019 07:06:11 +0200 Subject: [PATCH 03/13] Bump version --- library.json | 2 +- library.properties | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/library.json b/library.json index 2fbe62c..ec62ace 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ConfigManager", - "version": "1.5", + "version": "1.5.1", "keywords": "wifi, wi-fi, config", "description": "WiFi connection manager for ESP8266 and ESP32", "repository": diff --git a/library.properties b/library.properties index d63a1ae..43373f9 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ConfigManager -version=1.5 +version=1.5.1 author=Nick Wiersma maintainer=Nick Wiersma sentence=WiFi connection manager for ESP8266 and ESP32 From 9f1439f7d2f5b93bdddc466b919f9df37ed33c33 Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Fri, 6 Dec 2019 17:36:13 +0000 Subject: [PATCH 04/13] Extract debugging a parameter based classes to separate files --- lib/ConfigParameter/BaseParameter.h | 18 ++++ lib/ConfigParameter/ConfigParameter.h | 45 +++++++++ lib/ConfigParameter/ConfigStringParameter.h | 50 ++++++++++ lib/DebugPrint/DebugPrint.h | 9 ++ src/ConfigManager.h | 105 +------------------- 5 files changed, 126 insertions(+), 101 deletions(-) create mode 100644 lib/ConfigParameter/BaseParameter.h create mode 100644 lib/ConfigParameter/ConfigParameter.h create mode 100644 lib/ConfigParameter/ConfigStringParameter.h create mode 100644 lib/DebugPrint/DebugPrint.h diff --git a/lib/ConfigParameter/BaseParameter.h b/lib/ConfigParameter/BaseParameter.h new file mode 100644 index 0000000..22aae77 --- /dev/null +++ b/lib/ConfigParameter/BaseParameter.h @@ -0,0 +1,18 @@ +#ifndef __BASE_PARAMETER_H__ +#define __BASE_PARAMETER_H__ + +enum Mode {ap, api}; +enum ParameterMode { get, set, both}; + +/** + * Base Parameter + */ +class BaseParameter { +public: + virtual ParameterMode getMode() = 0; + virtual void fromJson(JsonObject *json) = 0; + virtual void toJson(JsonObject *json) = 0; + virtual void clearData() = 0; +}; + +#endif /* __BASE_PARAMETER_H__ */ diff --git a/lib/ConfigParameter/ConfigParameter.h b/lib/ConfigParameter/ConfigParameter.h new file mode 100644 index 0000000..9ee2095 --- /dev/null +++ b/lib/ConfigParameter/ConfigParameter.h @@ -0,0 +1,45 @@ +#ifndef __CONFIG_PARAMETER_H__ +#define __CONFIG_PARAMETER_H_ + +#include "BaseParameter.h" + +/** + * Config Parameter + */ +template +class ConfigParameter : public BaseParameter { +public: + ConfigParameter(const char *name, T *ptr, ParameterMode mode = both) { + this->name = name; + this->ptr = ptr; + this->mode = mode; + } + + ParameterMode getMode() { + return this->mode; + } + + void fromJson(JsonObject *json) { + if (json->containsKey(name) && json->is(name)) { + *ptr = json->get(name); + } + } + + void toJson(JsonObject *json) { + json->set(name, *ptr); + } + + void clearData() { + DebugPrint("Clearing: "); + DebugPrintln(name); + *ptr = T(); + } + +private: + const char *name; + T *ptr; + std::function cb; + ParameterMode mode; +}; + +#endif /* __CONFIG_PARAMETER_H__ */ diff --git a/lib/ConfigParameter/ConfigStringParameter.h b/lib/ConfigParameter/ConfigStringParameter.h new file mode 100644 index 0000000..09f72cd --- /dev/null +++ b/lib/ConfigParameter/ConfigStringParameter.h @@ -0,0 +1,50 @@ +#ifndef CONFIG_STRING_PARAMETER_H__ +#define __CONFIG_STRING_PARAMETER_H__ + +#include "BaseParameter.h" + +/** + * Config String Parameter + */ +class ConfigStringParameter : public BaseParameter { +public: + ConfigStringParameter(const char *name, char *ptr, size_t length, ParameterMode mode = both) { + this->name = name; + this->ptr = ptr; + this->length = length; + this->mode = mode; + } + + ParameterMode getMode() { + return this->mode; + } + + void fromJson(JsonObject *json) { + if (json->containsKey(name) && json->is(name)) { + const char * value = json->get(name); + + memset(ptr, NULL, length); + strncpy(ptr, const_cast(value), length - 1); + } + } + + void toJson(JsonObject *json) { + json->set(name, ptr); + } + + void clearData() { + DebugPrint("Clearing: "); + DebugPrintln(name); + memset(ptr, NULL, length); + } + + +private: + const char *name; + char *ptr; + size_t length; + ParameterMode mode; +}; + + +#endif /* __CONFIG_STRING_PARAMETER_H__ */ diff --git a/lib/DebugPrint/DebugPrint.h b/lib/DebugPrint/DebugPrint.h new file mode 100644 index 0000000..b7c60ab --- /dev/null +++ b/lib/DebugPrint/DebugPrint.h @@ -0,0 +1,9 @@ +#ifndef __DEBUG_PRINT_H__ +#define __DEBUG_PRINT_H__ + +extern bool DEBUG_MODE; + +#define DebugPrintln(a) (DEBUG_MODE ? Serial.println(a) : false) +#define DebugPrint(a) (DEBUG_MODE ? Serial.print(a) : false) + +#endif /* __DEBUG_PRINT_H__ */ diff --git a/src/ConfigManager.h b/src/ConfigManager.h index 5363a51..33ec108 100644 --- a/src/ConfigManager.h +++ b/src/ConfigManager.h @@ -24,6 +24,10 @@ #define WIFI_OPEN WIFI_AUTH_OPEN #endif +#include "DebugPrint.h" +#include "ConfigParameter.h" +#include "ConfigStringParameter.h" + #define MAGIC_LENGTH 2 #define SSID_LENGTH 32 #define PASSWORD_LENGTH 64 @@ -33,113 +37,12 @@ using WebServer = ESP8266WebServer; #endif -extern bool DEBUG_MODE; - -#define DebugPrintln(a) (DEBUG_MODE ? Serial.println(a) : false) -#define DebugPrint(a) (DEBUG_MODE ? Serial.print(a) : false) - extern const char mimeHTML[]; extern const char mimeJSON[]; extern const char mimePlain[]; extern const char mimeCSS[]; extern const char mimeJS[]; -enum Mode {ap, api}; -enum ParameterMode { get, set, both}; - -/** - * Base Parameter - */ -class BaseParameter { -public: - virtual ParameterMode getMode() = 0; - virtual void fromJson(JsonObject *json) = 0; - virtual void toJson(JsonObject *json) = 0; - virtual void clearData() = 0; -}; - -/** - * Config Parameter - */ -template -class ConfigParameter : public BaseParameter { -public: - ConfigParameter(const char *name, T *ptr, ParameterMode mode = both) { - this->name = name; - this->ptr = ptr; - this->mode = mode; - } - - ParameterMode getMode() { - return this->mode; - } - - void fromJson(JsonObject *json) { - if (json->containsKey(name) && json->is(name)) { - *ptr = json->get(name); - } - } - - void toJson(JsonObject *json) { - json->set(name, *ptr); - } - - void clearData() { - DebugPrint("Clearing: "); - DebugPrintln(name); - *ptr = T(); - } - -private: - const char *name; - T *ptr; - std::function cb; - ParameterMode mode; -}; - -/** - * Config String Parameter - */ -class ConfigStringParameter : public BaseParameter { -public: - ConfigStringParameter(const char *name, char *ptr, size_t length, ParameterMode mode = both) { - this->name = name; - this->ptr = ptr; - this->length = length; - this->mode = mode; - } - - ParameterMode getMode() { - return this->mode; - } - - void fromJson(JsonObject *json) { - if (json->containsKey(name) && json->is(name)) { - const char * value = json->get(name); - - memset(ptr, 0, length); - strncpy(ptr, const_cast(value), length - 1); - } - } - - void toJson(JsonObject *json) { - json->set(name, ptr); - } - - void clearData() { - DebugPrint("Clearing: "); - DebugPrintln(name); - memset(ptr, 0, length); - } - - -private: - const char *name; - char *ptr; - size_t length; - ParameterMode mode; -}; - /** * Config Manager */ From c163535d3425434adf16952c4f88370ad8c6b7dd Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Thu, 12 Dec 2019 02:40:10 +0000 Subject: [PATCH 05/13] Update CI to include private library files --- .travis.yml | 2 +- lib/ConfigParameter/BaseParameter.h | 2 ++ lib/ConfigParameter/ConfigParameter.h | 2 +- lib/ConfigParameter/ConfigStringParameter.h | 2 +- src/ConfigManager.h | 1 - 5 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1222d6c..84062ca 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ install: - platformio update script: - - platformio ci --lib="./src" -c platformio.ini ./examples/simple/ + - platformio ci -c platformio.ini --lib="./src" --lib="./lib/*" ./examples/simple/ notifications: email: diff --git a/lib/ConfigParameter/BaseParameter.h b/lib/ConfigParameter/BaseParameter.h index 22aae77..40cc640 100644 --- a/lib/ConfigParameter/BaseParameter.h +++ b/lib/ConfigParameter/BaseParameter.h @@ -1,6 +1,8 @@ #ifndef __BASE_PARAMETER_H__ #define __BASE_PARAMETER_H__ +#include + enum Mode {ap, api}; enum ParameterMode { get, set, both}; diff --git a/lib/ConfigParameter/ConfigParameter.h b/lib/ConfigParameter/ConfigParameter.h index 9ee2095..f41cf72 100644 --- a/lib/ConfigParameter/ConfigParameter.h +++ b/lib/ConfigParameter/ConfigParameter.h @@ -1,7 +1,7 @@ #ifndef __CONFIG_PARAMETER_H__ #define __CONFIG_PARAMETER_H_ -#include "BaseParameter.h" +#include /** * Config Parameter diff --git a/lib/ConfigParameter/ConfigStringParameter.h b/lib/ConfigParameter/ConfigStringParameter.h index 09f72cd..666ce5a 100644 --- a/lib/ConfigParameter/ConfigStringParameter.h +++ b/lib/ConfigParameter/ConfigStringParameter.h @@ -1,7 +1,7 @@ #ifndef CONFIG_STRING_PARAMETER_H__ #define __CONFIG_STRING_PARAMETER_H__ -#include "BaseParameter.h" +#include /** * Config String Parameter diff --git a/src/ConfigManager.h b/src/ConfigManager.h index 33ec108..3d13fb7 100644 --- a/src/ConfigManager.h +++ b/src/ConfigManager.h @@ -24,7 +24,6 @@ #define WIFI_OPEN WIFI_AUTH_OPEN #endif -#include "DebugPrint.h" #include "ConfigParameter.h" #include "ConfigStringParameter.h" From 1aa3ca7f74270f1cf9cf305438ae3f82fb16eefd Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Thu, 12 Dec 2019 18:10:00 +0000 Subject: [PATCH 06/13] Fix header --- lib/ConfigParameter/ConfigStringParameter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ConfigParameter/ConfigStringParameter.h b/lib/ConfigParameter/ConfigStringParameter.h index 666ce5a..3680724 100644 --- a/lib/ConfigParameter/ConfigStringParameter.h +++ b/lib/ConfigParameter/ConfigStringParameter.h @@ -1,4 +1,4 @@ -#ifndef CONFIG_STRING_PARAMETER_H__ +#ifndef __CONFIG_STRING_PARAMETER_H__ #define __CONFIG_STRING_PARAMETER_H__ #include From 1ca3a564bfa8aeea2d06887504068ac357c8f101 Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Thu, 12 Dec 2019 18:10:40 +0000 Subject: [PATCH 07/13] Make sure we include the ArduinoJson header --- lib/ConfigParameter/BaseParameter.h | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ConfigParameter/BaseParameter.h b/lib/ConfigParameter/BaseParameter.h index 40cc640..6ff6720 100644 --- a/lib/ConfigParameter/BaseParameter.h +++ b/lib/ConfigParameter/BaseParameter.h @@ -1,6 +1,7 @@ #ifndef __BASE_PARAMETER_H__ #define __BASE_PARAMETER_H__ +#include #include enum Mode {ap, api}; From 02369699d447475255772f5e954a6f0edb588eec Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Thu, 12 Dec 2019 18:10:59 +0000 Subject: [PATCH 08/13] Add basic embedded test --- test/embedded/ConfigParameterTest.h | 14 ++++++++++++++ test/embedded/main.cpp | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 test/embedded/ConfigParameterTest.h create mode 100644 test/embedded/main.cpp diff --git a/test/embedded/ConfigParameterTest.h b/test/embedded/ConfigParameterTest.h new file mode 100644 index 0000000..ce955a4 --- /dev/null +++ b/test/embedded/ConfigParameterTest.h @@ -0,0 +1,14 @@ +#include + +#include +#include + +bool DEBUG_MODE = false; + +void test_sets_config_parameter_mode() { + const char *variable = "baphled"; + + ConfigParameter configParameters = ConfigParameter("username", &variable, both); + + TEST_ASSERT_EQUAL(configParameters.getMode(), both); +} diff --git a/test/embedded/main.cpp b/test/embedded/main.cpp new file mode 100644 index 0000000..626b98a --- /dev/null +++ b/test/embedded/main.cpp @@ -0,0 +1,18 @@ +#if defined(ARDUINO) && defined(UNIT_TEST) +#include "Arduino.h" + +#include + +#include "ConfigParameterTest.h" + +void setup() { + delay(2000); + UNITY_BEGIN(); + + RUN_TEST(test_sets_config_parameter_mode); + + UNITY_END(); +} + +void loop() {} +#endif From 1d5fbe4f65a582f276515782fc4b5e5e9e1cc8b8 Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Sun, 15 Dec 2019 05:19:23 +0000 Subject: [PATCH 09/13] Add basic tests to test ConfigParameter --- lib/ConfigParameter/ConfigParameter.h | 1 - test/embedded/ConfigParameterTest.h | 55 ++++++++++++++++++++++++++- test/embedded/main.cpp | 3 ++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/lib/ConfigParameter/ConfigParameter.h b/lib/ConfigParameter/ConfigParameter.h index f41cf72..dc05cc5 100644 --- a/lib/ConfigParameter/ConfigParameter.h +++ b/lib/ConfigParameter/ConfigParameter.h @@ -38,7 +38,6 @@ class ConfigParameter : public BaseParameter { private: const char *name; T *ptr; - std::function cb; ParameterMode mode; }; diff --git a/test/embedded/ConfigParameterTest.h b/test/embedded/ConfigParameterTest.h index ce955a4..5f628d6 100644 --- a/test/embedded/ConfigParameterTest.h +++ b/test/embedded/ConfigParameterTest.h @@ -7,8 +7,59 @@ bool DEBUG_MODE = false; void test_sets_config_parameter_mode() { const char *variable = "baphled"; + ConfigParameter parameter = ConfigParameter("username", &variable, both); - ConfigParameter configParameters = ConfigParameter("username", &variable, both); + TEST_ASSERT_EQUAL(both, parameter.getMode()); +} + +void test_config_parameter_stored_as_json() { + const char *variable = "baphled"; + const char *expected = "{\"username\":\"baphled\"}"; + DynamicJsonBuffer jsonBuffer; + JsonObject& obj = jsonBuffer.createObject(); + + char body[50] = "\n"; + + ConfigParameter parameter = ConfigParameter("username", &variable, get); + + parameter.toJson(&obj); + obj.printTo(body); + + TEST_ASSERT_EQUAL_STRING(expected, body); +} + +void test_config_parameter_stored_from_json() { + const char *variable = "foo"; + const char *expected = "{\"username\":\"baphled\"}"; + char body[50] = "\n"; + + DynamicJsonBuffer jsonBuffer; + JsonObject& obj = jsonBuffer.parseObject(expected); + + ConfigParameter parameter = ConfigParameter("username", &variable, set); + + parameter.fromJson(&obj); + + parameter.toJson(&obj); + obj.printTo(body); + + TEST_ASSERT_EQUAL_STRING(expected, body); +} + +void test_config_parameter_clear_data() { + const char *variable = "foo"; + const char *expected = "{\"username\":null}"; + char body[50] = "\n"; + + DynamicJsonBuffer jsonBuffer; + JsonObject& obj = jsonBuffer.createObject(); + + ConfigParameter parameter = ConfigParameter("username", &variable, set); + + parameter.clearData(); + + parameter.toJson(&obj); + obj.printTo(body); - TEST_ASSERT_EQUAL(configParameters.getMode(), both); + TEST_ASSERT_EQUAL_STRING(expected, body); } diff --git a/test/embedded/main.cpp b/test/embedded/main.cpp index 626b98a..effa49b 100644 --- a/test/embedded/main.cpp +++ b/test/embedded/main.cpp @@ -10,6 +10,9 @@ void setup() { UNITY_BEGIN(); RUN_TEST(test_sets_config_parameter_mode); + RUN_TEST(test_config_parameter_stored_as_json); + RUN_TEST(test_config_parameter_stored_from_json); + RUN_TEST(test_config_parameter_clear_data); UNITY_END(); } From 008dac8a34d0960dafecd35906cdfdef49bcfe0b Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Sun, 15 Dec 2019 05:58:12 +0000 Subject: [PATCH 10/13] Move enum to the appropriate place --- lib/ConfigParameter/BaseParameter.h | 1 - src/ConfigManager.h | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/ConfigParameter/BaseParameter.h b/lib/ConfigParameter/BaseParameter.h index 6ff6720..ab8ff0c 100644 --- a/lib/ConfigParameter/BaseParameter.h +++ b/lib/ConfigParameter/BaseParameter.h @@ -4,7 +4,6 @@ #include #include -enum Mode {ap, api}; enum ParameterMode { get, set, both}; /** diff --git a/src/ConfigManager.h b/src/ConfigManager.h index 3d13fb7..df97467 100644 --- a/src/ConfigManager.h +++ b/src/ConfigManager.h @@ -42,6 +42,8 @@ extern const char mimePlain[]; extern const char mimeCSS[]; extern const char mimeJS[]; +enum Mode {ap, api}; + /** * Config Manager */ From 46505b10dd05adff24cdbd6f7f28ef1e0373e422 Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Sun, 15 Dec 2019 05:58:40 +0000 Subject: [PATCH 11/13] Add tests for ConfigStringParameter class --- test/embedded/ConfigStringParameterTest.h | 67 +++++++++++++++++++++++ test/embedded/main.cpp | 8 +++ 2 files changed, 75 insertions(+) create mode 100644 test/embedded/ConfigStringParameterTest.h diff --git a/test/embedded/ConfigStringParameterTest.h b/test/embedded/ConfigStringParameterTest.h new file mode 100644 index 0000000..3e63717 --- /dev/null +++ b/test/embedded/ConfigStringParameterTest.h @@ -0,0 +1,67 @@ +#include + +#include +#include + +void test_sets_config_string_parameter_mode() { + char *variable = "baphled"; + size_t length = 10; + ConfigStringParameter parameter = ConfigStringParameter("username", variable, length, both); + + TEST_ASSERT_EQUAL(both, parameter.getMode()); +} + +void test_config_string_parameter_stored_as_json() { + char *variable = "baphled"; + const char *expected = "{\"username\":\"baphled\"}"; + DynamicJsonBuffer jsonBuffer; + JsonObject& obj = jsonBuffer.createObject(); + + char body[50] = "\n"; + size_t length = 10; + + ConfigStringParameter parameter = ConfigStringParameter("username", variable, length, set); + + parameter.toJson(&obj); + obj.printTo(body); + + TEST_ASSERT_EQUAL_STRING(expected, body); +} + +void test_config_string_parameter_stored_from_json() { + char *variable = "foo"; + const char *expected = "{\"username\":\"baphled\"}"; + char body[50] = "\n"; + size_t length = 10; + + DynamicJsonBuffer jsonBuffer; + JsonObject& obj = jsonBuffer.parseObject(expected); + + ConfigStringParameter parameter = ConfigStringParameter("username", variable, length, set); + + parameter.fromJson(&obj); + + parameter.toJson(&obj); + obj.printTo(body); + + TEST_ASSERT_EQUAL_STRING(expected, body); +} + +void test_config_string_parameter_clear_data() { + char *variable = "foo"; + const char *expected = "{\"username\":\"\"}"; + char body[50] = "\n"; + size_t length = 10; + + DynamicJsonBuffer jsonBuffer; + JsonObject& obj = jsonBuffer.createObject(); + + ConfigStringParameter parameter = ConfigStringParameter("username", variable, length, set); + + parameter.clearData(); + + parameter.toJson(&obj); + obj.printTo(body); + + TEST_ASSERT_EQUAL_STRING(expected, body); +} diff --git a/test/embedded/main.cpp b/test/embedded/main.cpp index effa49b..a0f0b4c 100644 --- a/test/embedded/main.cpp +++ b/test/embedded/main.cpp @@ -4,16 +4,24 @@ #include #include "ConfigParameterTest.h" +#include "ConfigStringParameterTest.h" void setup() { delay(2000); UNITY_BEGIN(); + // ConfigParameterTest RUN_TEST(test_sets_config_parameter_mode); RUN_TEST(test_config_parameter_stored_as_json); RUN_TEST(test_config_parameter_stored_from_json); RUN_TEST(test_config_parameter_clear_data); + // ConfigStringParameterTest + RUN_TEST(test_sets_config_string_parameter_mode); + RUN_TEST(test_config_string_parameter_stored_as_json); + RUN_TEST(test_config_string_parameter_stored_from_json); + RUN_TEST(test_config_string_parameter_clear_data); + UNITY_END(); } From 027dbc621fdce6270411dc27a367d8b83e844c0e Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Sun, 15 Dec 2019 06:19:59 +0000 Subject: [PATCH 12/13] Move DEBUG_MODE setup --- test/embedded/ConfigParameterTest.h | 2 -- test/embedded/main.cpp | 3 +++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/embedded/ConfigParameterTest.h b/test/embedded/ConfigParameterTest.h index 5f628d6..bed52ed 100644 --- a/test/embedded/ConfigParameterTest.h +++ b/test/embedded/ConfigParameterTest.h @@ -3,8 +3,6 @@ #include #include -bool DEBUG_MODE = false; - void test_sets_config_parameter_mode() { const char *variable = "baphled"; ConfigParameter parameter = ConfigParameter("username", &variable, both); diff --git a/test/embedded/main.cpp b/test/embedded/main.cpp index a0f0b4c..909a025 100644 --- a/test/embedded/main.cpp +++ b/test/embedded/main.cpp @@ -6,8 +6,11 @@ #include "ConfigParameterTest.h" #include "ConfigStringParameterTest.h" +bool DEBUG_MODE = false; + void setup() { delay(2000); + UNITY_BEGIN(); // ConfigParameterTest From 29c8762fca835d1ffc680b70a19da1a5a18deae1 Mon Sep 17 00:00:00 2001 From: Yomi Colledge Date: Sun, 15 Dec 2019 06:20:14 +0000 Subject: [PATCH 13/13] Fix header definition --- lib/ConfigParameter/ConfigParameter.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ConfigParameter/ConfigParameter.h b/lib/ConfigParameter/ConfigParameter.h index dc05cc5..513932f 100644 --- a/lib/ConfigParameter/ConfigParameter.h +++ b/lib/ConfigParameter/ConfigParameter.h @@ -1,5 +1,5 @@ #ifndef __CONFIG_PARAMETER_H__ -#define __CONFIG_PARAMETER_H_ +#define __CONFIG_PARAMETER_H__ #include