diff --git a/.drone.yml b/.drone.yml index 293b48f..6694831 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,26 +8,34 @@ platform: arch: arm steps: +- name: static check + commands: + - cd gateway + - pio check -e pro-mini + when: + target: + exclude: + - production + - name: native tests commands: - cd gateway - pio test -e native - -- name: embedded tests - commands: - - cd gateway - - LD_LIBRARY_PATH=~/.platformio/packages/tool-simavr/lib/ pio test -e embedded --without-uploading + when: + target: + exclude: + - production - name: upload firmware commands: - cd gateway - service ser2net stop - pio run -e pro-mini - - echo -n 'reset' > /dev/ttyUSB0; sleep 1s; avrdude -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:.pio/build/pro-mini/firmware.hex:i -v + - echo 'reset' > /dev/ttyUSB0; sleep 1s; avrdude -patmega328p -carduino -P/dev/ttyUSB0 -b115200 -D -Uflash:w:.pio/build/pro-mini/firmware.hex:i -v - service ser2net start when: - branch: - - gateway + target: + - production trigger: branch: diff --git a/gateway/platformio.ini b/gateway/platformio.ini index 7c9ead9..75e972a 100644 --- a/gateway/platformio.ini +++ b/gateway/platformio.ini @@ -8,33 +8,46 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html +[platformio] +default_envs = pro-mini + [env:pro-mini] platform = atmelavr -board = miniatmega328 +board = pro16MHzatmega328 framework = arduino lib_extra_dirs = ../libraries lib_deps = sui77/rc-switch@^2.6.3 - bblanchon/ArduinoJson@6.16.1 + bblanchon/ArduinoJson@6.19.4 adafruit/Adafruit Unified Sensor@^1.1.4 adafruit/DHT sensor library@1.3.10 + https://git.hodos.ro/arduino/lib_serial-reader.git@^1.0.0 build_flags = -D DHT_SENSOR=0 upload_port = /dev/ttyUSB0 +check_tool = cppcheck +check_flags = --enable=all +check_skip_packages = yes +check_severity = medium, high [env:native] platform = native -test_filter = test_native +test_filter = native/* +lib_extra_dirs = + ../libraries +lib_deps = + bblanchon/ArduinoJson@6.19.4 +build_flags = -std=c++11 [env:embedded] platform = atmelavr framework = arduino -board = miniatmega328 +board = pro16MHzatmega328 lib_extra_dirs = ../libraries lib_deps = sui77/rc-switch@^2.6.3 - bblanchon/ArduinoJson@6.16.1 + bblanchon/ArduinoJson@6.19.4 platform_packages = platformio/tool-simavr @@ -46,4 +59,4 @@ test_testing_command = -f 16000000L ${platformio.build_dir}/${this.__env__}/firmware.elf -test_filter = test_embedded +test_filter = embedded/* diff --git a/gateway/src/gateway.cpp b/gateway/src/gateway.cpp index 49228f4..a8936d7 100644 --- a/gateway/src/gateway.cpp +++ b/gateway/src/gateway.cpp @@ -3,13 +3,15 @@ #include "Dht.h" #include "Protocol_1.h" #include "Protocol_2.h" +#include #define RESET_PIN 10 #define SEND_PIN 11 #define RECEIVE_PIN 2 -RCSwitch mySwitch = RCSwitch(); +RCSwitch mySwitch; +SerialReader<200> serialReader; void setup() { digitalWrite(RESET_PIN, HIGH); @@ -49,7 +51,7 @@ void readRcSwitch() { unsigned long value = mySwitch.getReceivedValue(); mySwitch.resetAvailable(); - StaticJsonDocument<200> jsonDoc; + StaticJsonDocument<128> jsonDoc; Protocol* p = findProtocol(mySwitch.getReceivedProtocol()); p->toJson(value, jsonDoc); delete p; @@ -60,44 +62,42 @@ void readRcSwitch() { } } -void runJsonCommands(const char* cmd) { - String origCmd = String(cmd); - StaticJsonDocument<512> jsonArray; - DeserializationError err = deserializeJson(jsonArray, cmd); +void handleJsonError(DeserializationError err, const char* cmd) { + StaticJsonDocument<50> jsonError; + JsonObject error = jsonError.createNestedObject("error"); + error["msg"] = err.c_str(); + error["orig_cmd"] = cmd; + serializeJson(jsonError, Serial); + Serial.println(); +} + +void runJsonCommand(char* cmd) { + StaticJsonDocument<50> jsonDoc; + DeserializationError err = deserializeJson(jsonDoc, cmd); if (err == DeserializationError::Ok) { - JsonArray array = jsonArray.as(); - for (JsonVariant jsonDoc : array) { - if (jsonDoc.containsKey("rcSwitch")) { - JsonObjectConst rcSwitch = jsonDoc["rcSwitch"]; - Protocol* p = findProtocol(rcSwitch["protocol"]); - p->fromJson(rcSwitch, mySwitch); - delete p; - serializeJson(jsonDoc, Serial); - Serial.println(); - } + if (jsonDoc.containsKey("rcSwitch")) { + JsonObjectConst rcSwitch = jsonDoc["rcSwitch"]; + Protocol* p = findProtocol(rcSwitch["protocol"]); + p->fromJson(rcSwitch, mySwitch); + delete p; + serializeJson(jsonDoc, Serial); + Serial.println(); } } else { - Serial.print(err.c_str()); - Serial.print(": "); - Serial.println(origCmd); + handleJsonError(err, cmd); } } void readCommand() { - if (Serial.available() > 0) { - String cmd = Serial.readStringUntil('\n'); - if (cmd == "reset") { + if (serialReader.readLine(Serial) > 0) { + char* cmd = serialReader.getBuffer(); + if (strcmp("reset", cmd) == 0) { Serial.println("resetting..."); - delay(200); + delay(1200); digitalWrite(RESET_PIN, LOW); Serial.println("resetting failed"); } - if (cmd.endsWith(",")) { - cmd = cmd.substring(0, cmd.lastIndexOf(',')); - } - char buffer[cmd.length()+2]; - sprintf(buffer, "[%s]", cmd.c_str()); - runJsonCommands(buffer); + runJsonCommand(cmd); } } diff --git a/gateway/test/embedded/test_dummy/dummy.cpp b/gateway/test/embedded/test_dummy/dummy.cpp new file mode 100644 index 0000000..352dc1e --- /dev/null +++ b/gateway/test/embedded/test_dummy/dummy.cpp @@ -0,0 +1,18 @@ +#include +#include + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + +void setup() { + UNITY_BEGIN(); + UNITY_END(); +} + +void loop() { +} diff --git a/gateway/test/test_native/decoder.cpp b/gateway/test/native/test_decoder/decoder.cpp similarity index 100% rename from gateway/test/test_native/decoder.cpp rename to gateway/test/native/test_decoder/decoder.cpp diff --git a/gateway/test/test_embedded/sensor_builder.cpp b/gateway/test/native/test_sensor_builder/sensor_builder.cpp similarity index 98% rename from gateway/test/test_embedded/sensor_builder.cpp rename to gateway/test/native/test_sensor_builder/sensor_builder.cpp index 8ca3ca3..db130f0 100644 --- a/gateway/test/test_embedded/sensor_builder.cpp +++ b/gateway/test/native/test_sensor_builder/sensor_builder.cpp @@ -1,4 +1,3 @@ -#include #include #include "TinyComponent.h" @@ -98,7 +97,7 @@ void test_oil_sensor_with_voltage(void) { TEST_ASSERT_EQUAL(2.847, diagnostic["voltage"]); } -void setup() { +int main(int argc, char **argv) { UNITY_BEGIN(); RUN_TEST(test_unknown_sensor_type); RUN_TEST(test_max_temp); @@ -110,7 +109,6 @@ void setup() { RUN_TEST(test_oil_sensor); RUN_TEST(test_oil_sensor_with_voltage); UNITY_END(); -} -void loop() { + return 0; } diff --git a/libraries/RF24 b/libraries/RF24 deleted file mode 160000 index cb04b38..0000000 --- a/libraries/RF24 +++ /dev/null @@ -1 +0,0 @@ -Subproject commit cb04b381f9b68cf60cac7ecb18346fb3291a80a6