From 3a665aece16a96ae594f515622b20c56393eb1f5 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Tue, 20 Oct 2020 16:41:41 +0200 Subject: [PATCH 01/26] Offload SSL Client w/ crypto on Nina modules Temporarily remove OTA support since it depends on bearssl's SHA256 APIs --- src/AIoTC_Config.h | 10 ++++++++-- src/ArduinoIoTCloudTCP.cpp | 13 ++++++++++++- src/ArduinoIoTCloudTCP.h | 12 ++++++++++-- src/tls/utility/CryptoUtil.cpp | 2 +- src/tls/utility/CryptoUtil.h | 2 +- src/tls/utility/ECCX08Cert.cpp | 2 +- src/tls/utility/ECCX08Cert.h | 2 +- 7 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index 2515575c9..40d1f3114 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -72,14 +72,20 @@ #define OTA_ENABLED (0) #endif -#if defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRWIFI1010) || \ - defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_NANO_33_IOT) || \ +#if defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKR1000) || \ defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_PORTENTA_H7_M7) || \ defined(ARDUINO_PORTENTA_H7_M4) #define BOARD_HAS_ECCX08 #define HAS_TCP #endif +#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) + #define BOARD_HAS_OFFLOADED_ECCX08 + #define HAS_TCP + #undef OTA_ENABLED + #define OTA_ENABLED (0) +#endif + #if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) #define HAS_LORA #endif diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 29d2b042a..607af597a 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -28,6 +28,11 @@ #include "tls/utility/CryptoUtil.h" #endif +#ifdef BOARD_HAS_OFFLOADED_ECCX08 +#include +#include "tls/utility/CryptoUtil.h" +#endif + #include "utility/ota/OTA.h" #include "utility/ota/FlashSHA256.h" @@ -112,6 +117,12 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) _ota_img_sha256 = FlashSHA256::calc(0x2000, 0x40000 - 0x2000); #endif /* OTA_ENABLED */ + #ifdef BOARD_HAS_OFFLOADED_ECCX08 + if (!ECCX08.begin()) { DBG_ERROR(F("Cryptography processor failure. Make sure you have a compatible board.")); return 0; } + if (!CryptoUtil::readDeviceId(ECCX08, getDeviceId(), ECCX08Slot::DeviceId)) { DBG_ERROR(F("Cryptography processor read failure.")); return 0; } + ECCX08.end(); + #endif + #ifdef BOARD_HAS_ECCX08 if (!ECCX08.begin()) { DBG_ERROR(F("Cryptography processor failure. Make sure you have a compatible board.")); return 0; } if (!CryptoUtil::readDeviceId(ECCX08, getDeviceId(), ECCX08Slot::DeviceId)) { DBG_ERROR(F("Cryptography processor read failure.")); return 0; } @@ -146,7 +157,7 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) addPropertyReal(_ota_req, "OTA_REQ", Permission::ReadWrite).onSync(DEVICE_WINS); #endif /* OTA_ENABLED */ -#if OTA_STORAGE_SNU +#if OTA_STORAGE_SNU && OTA_ENABLED String const nina_fw_version = WiFi.firmwareVersion(); if (nina_fw_version < "1.4.1") { _ota_cap = false; diff --git a/src/ArduinoIoTCloudTCP.h b/src/ArduinoIoTCloudTCP.h index 78eab72b1..c26e08ec3 100644 --- a/src/ArduinoIoTCloudTCP.h +++ b/src/ArduinoIoTCloudTCP.h @@ -33,6 +33,11 @@ #include #endif +#ifdef BOARD_HAS_OFFLOADED_ECCX08 +#include "tls/utility/ECCX08Cert.h" +#include +#endif + #include /****************************************************************************** @@ -60,7 +65,7 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass virtual int connected () override; virtual void printDebugInfo() override; - #ifdef BOARD_HAS_ECCX08 + #if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) int begin(ConnectionHandler & connection, String brokerAddress = DEFAULT_BROKER_ADDRESS_SECURE_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_SECURE_AUTH); #else int begin(ConnectionHandler & connection, String brokerAddress = DEFAULT_BROKER_ADDRESS_USER_PASS_AUTH, uint16_t brokerPort = DEFAULT_BROKER_PORT_USER_PASS_AUTH); @@ -98,9 +103,12 @@ class ArduinoIoTCloudTCP: public ArduinoIoTCloudClass int _mqtt_data_len; bool _mqtt_data_request_retransmit; - #ifdef BOARD_HAS_ECCX08 + #if defined(BOARD_HAS_ECCX08) ECCX08CertClass _eccx08_cert; BearSSLClient _sslClient; + #elif defined(BOARD_HAS_OFFLOADED_ECCX08) + ECCX08CertClass _eccx08_cert; + WiFiBearSSLClient _sslClient; #elif defined(BOARD_ESP) WiFiClientSecure _sslClient; String _password; diff --git a/src/tls/utility/CryptoUtil.cpp b/src/tls/utility/CryptoUtil.cpp index 248cc2d79..4348bc38d 100644 --- a/src/tls/utility/CryptoUtil.cpp +++ b/src/tls/utility/CryptoUtil.cpp @@ -21,7 +21,7 @@ #include "CryptoUtil.h" -#ifdef BOARD_HAS_ECCX08 +#if defined(BOARD_HAS_ECCX08) || defined (BOARD_HAS_OFFLOADED_ECCX08) /****************************************************************************** * PUBLIC MEMBER FUNCTIONS diff --git a/src/tls/utility/CryptoUtil.h b/src/tls/utility/CryptoUtil.h index 68e4ee5b3..32239a486 100644 --- a/src/tls/utility/CryptoUtil.h +++ b/src/tls/utility/CryptoUtil.h @@ -24,7 +24,7 @@ #include -#ifdef BOARD_HAS_ECCX08 +#if defined(BOARD_HAS_ECCX08) || defined (BOARD_HAS_OFFLOADED_ECCX08) #include #include diff --git a/src/tls/utility/ECCX08Cert.cpp b/src/tls/utility/ECCX08Cert.cpp index 0a232be00..164309e9a 100644 --- a/src/tls/utility/ECCX08Cert.cpp +++ b/src/tls/utility/ECCX08Cert.cpp @@ -21,7 +21,7 @@ #include -#ifdef BOARD_HAS_ECCX08 +#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) #include "../../tls/bearssl/bearssl_hash.h" #include diff --git a/src/tls/utility/ECCX08Cert.h b/src/tls/utility/ECCX08Cert.h index 95437b352..4e1ef6209 100644 --- a/src/tls/utility/ECCX08Cert.h +++ b/src/tls/utility/ECCX08Cert.h @@ -24,7 +24,7 @@ #include -#ifdef BOARD_HAS_ECCX08 +#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) #include From 8482a1a47f3323ee0154952d4b5efbee4aed7f65 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Tue, 20 Oct 2020 18:56:41 +0200 Subject: [PATCH 02/26] Initial support for Uno WiFi rev2 Need to add a patch to Arduino_ConnectionHandler: diff --git a/src/Arduino_ConnectionHandler.h b/src/Arduino_ConnectionHandler.h index 195ab13..8d80f2e 100644 --- a/src/Arduino_ConnectionHandler.h +++ b/src/Arduino_ConnectionHandler.h @@ -29,7 +29,8 @@ #define WIFI_FIRMWARE_VERSION_REQUIRED WIFI_FIRMWARE_REQUIRED #endif -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) +#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || \ + defined(ARDUINO_AVR_UNO_WIFI_REV2) #include #include --- src/AIoTC_Config.h | 3 +- src/cbor/CBORDecoder.h | 4 + src/cbor/CBOREncoder.h | 5 + src/cbor/lib/tinycbor/src/cbor.h | 5 + src/cbor/lib/tinycbor/src/cborpretty.c | 4 + src/cbor/lib/tinycbor/src/cbortojson.c | 4 + src/cbor/lib/tinycbor/src/open_memstream.c | 4 + src/nonstd/nonstd.h | 235 +++++++++++++++++++++ src/property/Property.cpp | 11 +- src/property/Property.h | 11 +- 10 files changed, 283 insertions(+), 3 deletions(-) create mode 100644 src/nonstd/nonstd.h diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index 40d1f3114..aa3b43f56 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -79,7 +79,8 @@ #define HAS_TCP #endif -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) +#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || \ + defined(ARDUINO_AVR_UNO_WIFI_REV2) #define BOARD_HAS_OFFLOADED_ECCX08 #define HAS_TCP #undef OTA_ENABLED diff --git a/src/cbor/CBORDecoder.h b/src/cbor/CBORDecoder.h index a0f9d7360..f14d51314 100644 --- a/src/cbor/CBORDecoder.h +++ b/src/cbor/CBORDecoder.h @@ -21,6 +21,10 @@ /****************************************************************************** INCLUDE ******************************************************************************/ +#ifdef __AVR__ +#include +#include +#endif #undef max #undef min diff --git a/src/cbor/CBOREncoder.h b/src/cbor/CBOREncoder.h index abaa9d4b7..0d614c770 100644 --- a/src/cbor/CBOREncoder.h +++ b/src/cbor/CBOREncoder.h @@ -22,6 +22,11 @@ * INCLUDE ******************************************************************************/ +#ifdef __AVR__ +#include +#include +#endif + #include "../property/PropertyContainer.h" /****************************************************************************** diff --git a/src/cbor/lib/tinycbor/src/cbor.h b/src/cbor/lib/tinycbor/src/cbor.h index e81df0ec3..cb759fd5b 100644 --- a/src/cbor/lib/tinycbor/src/cbor.h +++ b/src/cbor/lib/tinycbor/src/cbor.h @@ -25,6 +25,11 @@ #ifndef CBOR_H #define CBOR_H +#ifdef __AVR__ +#define FP_NAN NAN +#define FP_INFINITE INFINITY +#endif + #ifndef assert #include #endif diff --git a/src/cbor/lib/tinycbor/src/cborpretty.c b/src/cbor/lib/tinycbor/src/cborpretty.c index 2db4b253a..b82b985ac 100644 --- a/src/cbor/lib/tinycbor/src/cborpretty.c +++ b/src/cbor/lib/tinycbor/src/cborpretty.c @@ -28,6 +28,8 @@ # define __STDC_LIMIT_MACROS 1 #endif +#ifndef __AVR__ + #include "cbor.h" #include "cborinternal_p.h" #include "compilersupport_p.h" @@ -580,4 +582,6 @@ CborError cbor_value_to_pretty_stream(CborStreamFunction streamFunction, void *t #pragma GCC diagnostic pop +#endif // __AVR__ + /** @} */ diff --git a/src/cbor/lib/tinycbor/src/cbortojson.c b/src/cbor/lib/tinycbor/src/cbortojson.c index ed6a16666..0e669f27e 100644 --- a/src/cbor/lib/tinycbor/src/cbortojson.c +++ b/src/cbor/lib/tinycbor/src/cbortojson.c @@ -30,6 +30,8 @@ # define __STDC_LIMIT_MACROS 1 #endif +#ifndef __AVR__ + #include "cbor.h" #include "cborjson.h" #include "cborinternal_p.h" @@ -701,4 +703,6 @@ CborError cbor_value_to_json_advance(FILE *out, CborValue *value, int flags) #pragma GCC diagnostic pop +#endif // __AVR__ + /** @} */ diff --git a/src/cbor/lib/tinycbor/src/open_memstream.c b/src/cbor/lib/tinycbor/src/open_memstream.c index e535c9c79..707dbb1c4 100644 --- a/src/cbor/lib/tinycbor/src/open_memstream.c +++ b/src/cbor/lib/tinycbor/src/open_memstream.c @@ -34,6 +34,10 @@ #include +#ifdef __AVR__ +typedef size_t ssize_t; +#endif + typedef ssize_t RetType; typedef size_t LenType; diff --git a/src/nonstd/nonstd.h b/src/nonstd/nonstd.h new file mode 100644 index 000000000..b591919c6 --- /dev/null +++ b/src/nonstd/nonstd.h @@ -0,0 +1,235 @@ +#pragma once +#ifdef __AVR__ +#include +extern void * operator new(size_t size, void * ptr); +namespace nonstd{ + + templatestruct tag{using type=T;}; + templateusing type_t=typename Tag::type; + + using size_t=decltype(sizeof(int)); + + //move + + template + T&& move(T&t){return static_cast(t);} + + //forward + + template + struct remove_reference:tag{}; + template + struct remove_reference:tag{}; + templateusing remove_reference_t=type_t>; + + template + T&& forward( remove_reference_t& t ) { + return static_cast(t); + } + template + T&& forward( remove_reference_t&& t ) { + return static_cast(t); + } + + //decay + + template + struct remove_const:tag{}; + template + struct remove_const:tag{}; + + template + struct remove_volatile:tag{}; + template + struct remove_volatile:tag{}; + + template + struct remove_cv:remove_const>>{}; + + + template + struct decay3:remove_cv{}; + template + struct decay3:tag{}; + template + struct decay2:decay3{}; + template + struct decay2:tag{}; + + template + struct decay:decay2>{}; + + template + using decay_t=type_t>; + + //is_convertible + + template + T declval(); // no implementation + + template + struct integral_constant{ + static constexpr T value=t; + constexpr integral_constant() {}; + constexpr operator T()const{ return value; } + constexpr T operator()()const{ return value; } + }; + template + using bool_t=integral_constant; + using true_type=bool_t; + using false_type=bool_t; + + templatestruct voider:tag{}; + templateusing void_t=type_t>; + + namespace details { + templateclass Z, class, class...Ts> + struct can_apply:false_type{}; + templateclass Z, class...Ts> + struct can_apply>, Ts...>:true_type{}; + } + templateclass Z, class...Ts> + using can_apply = details::can_apply; + + namespace details { + template + using try_convert = decltype( To{declval()} ); + } + template + struct is_convertible : can_apply< details::try_convert, From, To > {}; + template<> + struct is_convertible:true_type{}; + + //enable_if + + template + struct enable_if {}; + template + struct enable_if:tag{}; + template + using enable_if_t=type_t>; + + //res_of + + namespace details { + template + using invoke_t = decltype( declval()(declval()...) ); + + template + struct res_of {}; + template + struct res_of>>: + tag> + {}; + } + template + using res_of = details::res_of; + template + using res_of_t=type_t>; + + //aligned_storage + + template + struct alignas(align) aligned_storage_t { + char buff[size]; + }; + + //is_same + + template + struct is_same:false_type{}; + template + struct is_same:true_type{}; + + template + struct small_task; + + template + struct small_task{ + struct vtable_t { + void(*mover)(void* src, void* dest); + void(*destroyer)(void*); + R(*invoke)(void const* t, Args&&...args); + template + static vtable_t const* get() { + static const vtable_t table = { + [](void* src, void*dest) { + new(dest) T(move(*static_cast(src))); + }, + [](void* t){ static_cast(t)->~T(); }, + [](void const* t, Args&&...args)->R { + return (*static_cast(t))(forward(args)...); + } + }; + return &table; + } + }; + vtable_t const* table = nullptr; + aligned_storage_t data; + template, + enable_if_t{}>* = nullptr, + enable_if_t, R >{}>* = nullptr + > + small_task( F&& f ): + table( vtable_t::template get() ) + { + static_assert( sizeof(dF) <= sz, "object too large" ); + static_assert( alignof(dF) <= algn, "object too aligned" ); + new(&data) dF(forward(f)); + } + ~small_task() { + if (table) + table->destroyer(&data); + } + small_task(const small_task& o): + table(o.table) + { + data = o.data; + } + small_task(small_task&& o): + table(o.table) + { + if (table) + table->mover(&o.data, &data); + } + small_task(){} + small_task& operator=(const small_task& o){ + this->~small_task(); + new(this) small_task( move(o) ); + return *this; + } + small_task& operator=(small_task&& o){ + this->~small_task(); + new(this) small_task( move(o) ); + return *this; + } + explicit operator bool()const{return table;} + R operator()(Args...args)const{ + return table->invoke(&data, forward(args)...); + } + }; + + template + inline bool operator==(const small_task& __f, nullptr_t) + { return !static_cast(__f); } + + /// @overload + template + inline bool operator==(nullptr_t, const small_task& __f) + { return !static_cast(__f); } + + template + inline bool operator!=(const small_task& __f, nullptr_t) + { return static_cast(__f); } + + /// @overload + template + inline bool operator!=(nullptr_t, const small_task& __f) + { return static_cast(__f); } + + template + using function = small_task; +} + +#endif \ No newline at end of file diff --git a/src/property/Property.cpp b/src/property/Property.cpp index bccbae1b8..20be6e35b 100644 --- a/src/property/Property.cpp +++ b/src/property/Property.cpp @@ -189,7 +189,12 @@ CborError Property::appendAttributeReal(String value, String attributeName, Cbor }, encoder); } -CborError Property::appendAttributeName(String attributeName, std::functionappendValue, CborEncoder *encoder) { +#ifdef __AVR__ +CborError Property::appendAttributeName(String attributeName, nonstd::functionappendValue, CborEncoder *encoder) +#else +CborError Property::appendAttributeName(String attributeName, std::functionappendValue, CborEncoder *encoder) +#endif +{ if (attributeName != "") { // when the attribute name string is not empty, the attribute identifier is incremented in order to be encoded in the message if the _lightPayload flag is set _attributeIdentifier++; @@ -271,7 +276,11 @@ void Property::setAttributeReal(String& value, String attributeName) { }); } +#ifdef __AVR__ +void Property::setAttributeReal(String attributeName, nonstd::functionsetValue) +#else void Property::setAttributeReal(String attributeName, std::functionsetValue) +#endif { if (attributeName != "") { _attributeIdentifier++; diff --git a/src/property/Property.h b/src/property/Property.h index 4b5900782..f8db8a948 100644 --- a/src/property/Property.h +++ b/src/property/Property.h @@ -133,6 +133,10 @@ typedef void(*OnSyncCallbackFunc)(Property &); CLASS DECLARATION ******************************************************************************/ +#ifdef __AVR__ +#include "nonstd/nonstd.h" +#endif + class Property { public: @@ -177,13 +181,18 @@ class Property CborError appendAttributeReal(int value, String attributeName = "", CborEncoder *encoder = nullptr); CborError appendAttributeReal(float value, String attributeName = "", CborEncoder *encoder = nullptr); CborError appendAttributeReal(String value, String attributeName = "", CborEncoder *encoder = nullptr); +#ifndef __AVR__ CborError appendAttributeName(String attributeName, std::functionf, CborEncoder *encoder); + void setAttributeReal(String attributeName, std::functionsetValue); +#else + CborError appendAttributeName(String attributeName, nonstd::functionf, CborEncoder *encoder); + void setAttributeReal(String attributeName, nonstd::functionsetValue); +#endif void setAttributesFromCloud(std::list * map_data_list); void setAttributeReal(bool& value, String attributeName = ""); void setAttributeReal(int& value, String attributeName = ""); void setAttributeReal(float& value, String attributeName = ""); void setAttributeReal(String& value, String attributeName = ""); - void setAttributeReal(String attributeName, std::functionsetValue); String getAttributeName(String propertyName, char separator); virtual bool isDifferentFromCloud() = 0; From 6bf3889d3a8beee26aa8dcbbe935924873840aa3 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 16 Nov 2020 19:52:45 +0100 Subject: [PATCH 03/26] RFC: calculate SHA256 using ECCx08 --- src/tls/utility/SHA256.cpp | 12 +++++++++--- src/tls/utility/SHA256.h | 7 +++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/tls/utility/SHA256.cpp b/src/tls/utility/SHA256.cpp index ffed6e587..37d5dbafd 100644 --- a/src/tls/utility/SHA256.cpp +++ b/src/tls/utility/SHA256.cpp @@ -33,15 +33,21 @@ constexpr size_t SHA256::HASH_SIZE; void SHA256::begin() { - br_sha256_init(&_ctx); + spare_len = 0; + ECCX08.beginSHA256(); } void SHA256::update(uint8_t const * data, size_t const len) { - br_sha256_update(&_ctx, data, len); + if (len == 64) { + ECCX08.updateSHA256(data); + } else { + memcpy(spare_buf, data, len); + spare_len = len; + } } void SHA256::finalize(uint8_t * hash) { - br_sha256_out(&_ctx, hash); + ECCX08.endSHA256(spare_buf, spare_len, hash); } diff --git a/src/tls/utility/SHA256.h b/src/tls/utility/SHA256.h index ed0ce1d5a..4091c3eeb 100644 --- a/src/tls/utility/SHA256.h +++ b/src/tls/utility/SHA256.h @@ -22,7 +22,7 @@ * INCLUDE ******************************************************************************/ -#include "../bearssl/bearssl_hash.h" +#include "ArduinoECCX08.h" /****************************************************************************** * CLASS DECLARATION @@ -40,9 +40,8 @@ class SHA256 void finalize(uint8_t * hash); private: - - br_sha256_context _ctx; - + uint8_t spare_buf[64]; + size_t spare_len = 0; }; #endif /* ARDUINO_TLS_UTILITY_SHA256_H_ */ From 3a143a78b61ef2c1bd867ca09f8cd938822c46d3 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 16 Nov 2020 19:53:28 +0100 Subject: [PATCH 04/26] Reenable OTA for SAMD Nina targets --- src/AIoTC_Config.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index aa3b43f56..ec2aebbf5 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -83,8 +83,6 @@ defined(ARDUINO_AVR_UNO_WIFI_REV2) #define BOARD_HAS_OFFLOADED_ECCX08 #define HAS_TCP - #undef OTA_ENABLED - #define OTA_ENABLED (0) #endif #if defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) From f087d046f4c48337261d3436fd2dd3ae85b1232c Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 16 Nov 2020 19:53:55 +0100 Subject: [PATCH 05/26] OTA: prepare for UNO WiFi rev2 --- src/AIoTC_Config.h | 15 +++++++++++++-- src/ArduinoIoTCloud.cpp | 4 ++++ src/ArduinoIoTCloud.h | 3 +++ src/ArduinoIoTCloudTCP.cpp | 4 +++- src/utility/ota/FlashSHA256.cpp | 2 +- src/utility/ota/OTA.h | 2 +- 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index ec2aebbf5..e79145d2b 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -30,6 +30,16 @@ #define NTP_USE_RANDOM_PORT (1) #endif +#if defined(ARDUINO_AVR_UNO_WIFI_REV2) +#define DBG_ERROR +#define DBG_WARNING +#define DBG_INFO +#define DBG_DEBUG +#define DBG_VERBOSE +// avoid including debugutils +#define ARDUINO_DEBUG_UTILS_H_ +#endif + #ifndef DBG_ERROR #define DBG_ERROR(fmt, ...) Debug.print(DBG_ERROR, fmt, ## __VA_ARGS__) #endif @@ -54,7 +64,8 @@ * AUTOMATICALLY CONFIGURED DEFINES ******************************************************************************/ -#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) +#if defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_SAMD_NANO_33_IOT) || \ + defined(ARDUINO_AVR_UNO_WIFI_REV2) #define OTA_STORAGE_SNU (1) #else #define OTA_STORAGE_SNU (0) @@ -66,7 +77,7 @@ #define OTA_STORAGE_SSU (0) #endif -#if OTA_STORAGE_SFU || OTA_STORAGE_SSU || OTA_STORAGE_SNU +#if OTA_STORAGE_SFU || OTA_STORAGE_SSU || OTA_STORAGE_SNU && !defined(ARDUINO_AVR_UNO_WIFI_REV2) #define OTA_ENABLED (1) #else #define OTA_ENABLED (0) diff --git a/src/ArduinoIoTCloud.cpp b/src/ArduinoIoTCloud.cpp index cf7557e5a..4dfe9c326 100644 --- a/src/ArduinoIoTCloud.cpp +++ b/src/ArduinoIoTCloud.cpp @@ -179,3 +179,7 @@ void ArduinoIoTCloudClass::execCloudEventCallback(ArduinoIoTCloudEvent const eve (*callback)(); } } + +__attribute__((weak)) void setDebugMessageLevel(int const level) { + //do nothing +} \ No newline at end of file diff --git a/src/ArduinoIoTCloud.h b/src/ArduinoIoTCloud.h index 1be41e9e8..4c5948b76 100644 --- a/src/ArduinoIoTCloud.h +++ b/src/ArduinoIoTCloud.h @@ -154,4 +154,7 @@ class ArduinoIoTCloudClass #include "ArduinoIoTCloudLPWAN.h" #endif +// declaration for boards without debug library +void setDebugMessageLevel(int const level); + #endif diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 607af597a..064ad74d0 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -103,7 +103,7 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) _brokerAddress = brokerAddress; _brokerPort = brokerPort; -#if OTA_ENABLED +#if OTA_ENABLED && !defined(__AVR__) /* Calculate the SHA256 checksum over the firmware stored in the flash of the * MCU. Note: As we don't know the length per-se we read chunks of the flash * until we detect one containing only 0xFF (= flash erased). This only works @@ -433,9 +433,11 @@ void ArduinoIoTCloudTCP::onOTARequest() ota_download_success = true; #endif /* OTA_STORAGE_SNU */ +#ifndef __AVR__ /* Perform the reset to reboot to SxU. */ if (ota_download_success) NVIC_SystemReset(); +#endif } #endif diff --git a/src/utility/ota/FlashSHA256.cpp b/src/utility/ota/FlashSHA256.cpp index 9f908a8c8..2be6a6066 100644 --- a/src/utility/ota/FlashSHA256.cpp +++ b/src/utility/ota/FlashSHA256.cpp @@ -20,7 +20,7 @@ ******************************************************************************/ #include -#if OTA_ENABLED +#if OTA_ENABLED && !defined(__AVR__) #include "FlashSHA256.h" diff --git a/src/utility/ota/OTA.h b/src/utility/ota/OTA.h index 208d5e616..bd1476fba 100644 --- a/src/utility/ota/OTA.h +++ b/src/utility/ota/OTA.h @@ -25,7 +25,7 @@ #include #if OTA_ENABLED -#if OTA_STORAGE_SNU +#if OTA_STORAGE_SNU && !defined(ARDUINO_AVR_UNO_WIFI_REV2) #include #endif /* OTA_STORAGE_SNU */ From cbb745ba3ffff30a4af31260f95e20484d826c91 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Tue, 17 Nov 2020 10:11:21 +0100 Subject: [PATCH 06/26] Restore SHA256 via bearSSL Loses 2KB of flash compared with https://github.com/arduino-libraries/ArduinoIoTCloud/commit/46aa5f6b9566f472065245e8f346d3f4d037848f but it's surely faster --- src/tls/bearssl/dec32be.c | 2 +- src/tls/bearssl/enc32be.c | 2 +- src/tls/bearssl/sha2small.c | 2 +- src/tls/utility/SHA256.cpp | 12 +++--------- src/tls/utility/SHA256.h | 7 ++++--- 5 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/tls/bearssl/dec32be.c b/src/tls/bearssl/dec32be.c index fb1924fe6..1ba0968e4 100644 --- a/src/tls/bearssl/dec32be.c +++ b/src/tls/bearssl/dec32be.c @@ -23,7 +23,7 @@ */ #include -#ifdef BOARD_HAS_ECCX08 +#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) #include "inner.h" diff --git a/src/tls/bearssl/enc32be.c b/src/tls/bearssl/enc32be.c index 471cddcac..dde2295d1 100644 --- a/src/tls/bearssl/enc32be.c +++ b/src/tls/bearssl/enc32be.c @@ -23,7 +23,7 @@ */ #include -#ifdef BOARD_HAS_ECCX08 +#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) #include "inner.h" diff --git a/src/tls/bearssl/sha2small.c b/src/tls/bearssl/sha2small.c index fb33c9b16..c17638706 100644 --- a/src/tls/bearssl/sha2small.c +++ b/src/tls/bearssl/sha2small.c @@ -23,7 +23,7 @@ */ #include -#ifdef BOARD_HAS_ECCX08 +#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) #include "inner.h" diff --git a/src/tls/utility/SHA256.cpp b/src/tls/utility/SHA256.cpp index 37d5dbafd..ffed6e587 100644 --- a/src/tls/utility/SHA256.cpp +++ b/src/tls/utility/SHA256.cpp @@ -33,21 +33,15 @@ constexpr size_t SHA256::HASH_SIZE; void SHA256::begin() { - spare_len = 0; - ECCX08.beginSHA256(); + br_sha256_init(&_ctx); } void SHA256::update(uint8_t const * data, size_t const len) { - if (len == 64) { - ECCX08.updateSHA256(data); - } else { - memcpy(spare_buf, data, len); - spare_len = len; - } + br_sha256_update(&_ctx, data, len); } void SHA256::finalize(uint8_t * hash) { - ECCX08.endSHA256(spare_buf, spare_len, hash); + br_sha256_out(&_ctx, hash); } diff --git a/src/tls/utility/SHA256.h b/src/tls/utility/SHA256.h index 4091c3eeb..ed0ce1d5a 100644 --- a/src/tls/utility/SHA256.h +++ b/src/tls/utility/SHA256.h @@ -22,7 +22,7 @@ * INCLUDE ******************************************************************************/ -#include "ArduinoECCX08.h" +#include "../bearssl/bearssl_hash.h" /****************************************************************************** * CLASS DECLARATION @@ -40,8 +40,9 @@ class SHA256 void finalize(uint8_t * hash); private: - uint8_t spare_buf[64]; - size_t spare_len = 0; + + br_sha256_context _ctx; + }; #endif /* ARDUINO_TLS_UTILITY_SHA256_H_ */ From 2b14646254f46800ac009a73a43ae10eac76e8dd Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 18 Nov 2020 10:38:46 +0100 Subject: [PATCH 07/26] Prevent inclusion of 'Arduino_DebugUtils' on AVR in a more future-proof and maintainable way. --- src/AIoTC_Config.h | 2 -- src/ArduinoIoTCloud.h | 5 ++++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index e79145d2b..e9373eb20 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -36,8 +36,6 @@ #define DBG_INFO #define DBG_DEBUG #define DBG_VERBOSE -// avoid including debugutils -#define ARDUINO_DEBUG_UTILS_H_ #endif #ifndef DBG_ERROR diff --git a/src/ArduinoIoTCloud.h b/src/ArduinoIoTCloud.h index 4c5948b76..bf70679e4 100644 --- a/src/ArduinoIoTCloud.h +++ b/src/ArduinoIoTCloud.h @@ -25,7 +25,10 @@ #include #include -#include + +#if !defined(__AVR__) +# include +#endif #include "AIoTC_Const.h" From 41e2392e86560d3e77302d0dcb53008329a3f027 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 18 Nov 2020 10:44:59 +0100 Subject: [PATCH 08/26] =?UTF-8?q?Renaming=20all=20'DBG=5FERROR/WARNING/INF?= =?UTF-8?q?O/DEBUG/VERBOSE=20macros=20to=20DEBUG=5FERROR/WARNING/...=20to?= =?UTF-8?q?=20avoid=20name=20clashes=20with=20=C3=ADdentically=20named=20c?= =?UTF-8?q?onstants=20within=20Arduino=5FDebugUtils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/AIoTC_Config.h | 30 +++++++++++++++--------------- src/ArduinoIoTCloudLPWAN.cpp | 10 +++++----- src/utility/ota/FlashSHA256.cpp | 4 ++-- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index e9373eb20..89ef47d31 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -31,31 +31,31 @@ #endif #if defined(ARDUINO_AVR_UNO_WIFI_REV2) -#define DBG_ERROR -#define DBG_WARNING -#define DBG_INFO -#define DBG_DEBUG -#define DBG_VERBOSE +#define DEBUG_ERROR +#define DEBUG_WARNING +#define DEBUG_INFO +#define DEBUG_DEBUG +#define DEBUG_VERBOSE #endif -#ifndef DBG_ERROR - #define DBG_ERROR(fmt, ...) Debug.print(DBG_ERROR, fmt, ## __VA_ARGS__) +#ifndef DEBUG_ERROR + #define DEBUG_ERROR(fmt, ...) Debug.print(DBG_ERROR, fmt, ## __VA_ARGS__) #endif -#ifndef DBG_WARNING - #define DBG_WARNING(fmt, ...) Debug.print(DBG_WARNING, fmt, ## __VA_ARGS__) +#ifndef DEBUG_WARNING + #define DEBUG_WARNING(fmt, ...) Debug.print(DBG_WARNING, fmt, ## __VA_ARGS__) #endif -#ifndef DBG_INFO - #define DBG_INFO(fmt, ...) Debug.print(DBG_INFO, fmt, ## __VA_ARGS__) +#ifndef DEBUG_INFO + #define DEBUG_INFO(fmt, ...) Debug.print(DBG_INFO, fmt, ## __VA_ARGS__) #endif -#ifndef DBG_DEBUG - #define DBG_DEBUG(fmt, ...) Debug.print(DBG_DEBUG, fmt, ## __VA_ARGS__) +#ifndef DEBUG_DEBUG + #define DEBUG_DEBUG(fmt, ...) Debug.print(DBG_DEBUG, fmt, ## __VA_ARGS__) #endif -#ifndef DBG_VERBOSE - #define DBG_VERBOSE(fmt, ...) //Debug.print(DBG_VERBOSE, fmt, ## __VA_ARGS__) +#ifndef DEBUG_VERBOSE + #define DEBUG_VERBOSE(fmt, ...) //Debug.print(DBG_VERBOSE, fmt, ## __VA_ARGS__) #endif /****************************************************************************** diff --git a/src/ArduinoIoTCloudLPWAN.cpp b/src/ArduinoIoTCloudLPWAN.cpp index 9f8bfd9b2..73ae68c61 100644 --- a/src/ArduinoIoTCloudLPWAN.cpp +++ b/src/ArduinoIoTCloudLPWAN.cpp @@ -87,8 +87,8 @@ void ArduinoIoTCloudLPWAN::update() void ArduinoIoTCloudLPWAN::printDebugInfo() { - DBG_INFO(F("***** Arduino IoT Cloud LPWAN - configuration info *****")); - DBG_INFO(F("Thing ID: %s"), getThingId().c_str()); + DEBUG_INFO(F("***** Arduino IoT Cloud LPWAN - configuration info *****")); + DEBUG_INFO(F("Thing ID: %s"), getThingId().c_str()); } /****************************************************************************** @@ -106,8 +106,8 @@ ArduinoIoTCloudLPWAN::State ArduinoIoTCloudLPWAN::handle_ConnectPhy() ArduinoIoTCloudLPWAN::State ArduinoIoTCloudLPWAN::handle_SyncTime() { unsigned long const internal_posix_time = _time_service.getTime(); - DBG_VERBOSE(F("ArduinoIoTCloudLPWAN::%s internal clock configured to posix timestamp %d"), __FUNCTION__, internal_posix_time); - DBG_INFO(F("Connected to Arduino IoT Cloud")); + DEBUG_VERBOSE(F("ArduinoIoTCloudLPWAN::%s internal clock configured to posix timestamp %d"), __FUNCTION__, internal_posix_time); + DEBUG_INFO(F("Connected to Arduino IoT Cloud")); return State::Connected; } @@ -115,7 +115,7 @@ ArduinoIoTCloudLPWAN::State ArduinoIoTCloudLPWAN::handle_Connected() { if (!connected()) { - DBG_ERROR(F("ArduinoIoTCloudLPWAN::%s connection to gateway lost"), __FUNCTION__); + DEBUG_ERROR(F("ArduinoIoTCloudLPWAN::%s connection to gateway lost"), __FUNCTION__); return State::ConnectPhy; } diff --git a/src/utility/ota/FlashSHA256.cpp b/src/utility/ota/FlashSHA256.cpp index 2be6a6066..12290a612 100644 --- a/src/utility/ota/FlashSHA256.cpp +++ b/src/utility/ota/FlashSHA256.cpp @@ -100,8 +100,8 @@ String FlashSHA256::calc(uint32_t const start_addr, uint32_t const max_flash_siz sha256_str += buf; }); /* Do some debug printout. */ - DBG_VERBOSE("SHA256: %d bytes read", flash_addr); - DBG_VERBOSE("SHA256: HASH(%d) = %s", strlen(sha256_str.c_str()), sha256_str.c_str()); + DEBUG_VERBOSE("SHA256: %d bytes read", flash_addr); + DEBUG_VERBOSE("SHA256: HASH(%d) = %s", strlen(sha256_str.c_str()), sha256_str.c_str()); return sha256_str; } From 3640d4dd931fc357fe93fdbb7472cce4034f87ad Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 18 Nov 2020 10:58:22 +0100 Subject: [PATCH 09/26] Provide DBG_NONE/ERROR/ etc. values in case ArduinoDebugUtils is not compiled in (which is the case when working with Uno WiFi Rev. 2 --- src/AIoTC_Config.h | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index 89ef47d31..d6f60843b 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -31,11 +31,24 @@ #endif #if defined(ARDUINO_AVR_UNO_WIFI_REV2) -#define DEBUG_ERROR -#define DEBUG_WARNING -#define DEBUG_INFO -#define DEBUG_DEBUG -#define DEBUG_VERBOSE +/* Define debug macros which effectively removes them from the + * the codebase for Arduino Uno WiFi. Rev. 2. + */ +# define DEBUG_ERROR +# define DEBUG_WARNING +# define DEBUG_INFO +# define DEBUG_DEBUG +# define DEBUG_VERBOSE +/* Provide defines for constants provided within Arduino_DebugUtils + * in order to allow older sketches using those constants to still + * compile. + */ +# define DBG_NONE -1 +# define DBG_ERROR 0 +# define DBG_WARNING 1 +# define DBG_INFO 2 +# define DBG_DEBUG 3 +# define DBG_VERBOSE 4 #endif #ifndef DEBUG_ERROR From ad7407476bbed5b57c9228f1b8ab82e710f2797e Mon Sep 17 00:00:00 2001 From: per1234 Date: Tue, 17 Nov 2020 02:25:11 -0800 Subject: [PATCH 10/26] Add Uno WiFi Rev2 to "Compile Examples" CI workflow --- .github/workflows/compile-examples.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index c4d902eb9..6700631bf 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -41,6 +41,7 @@ jobs: {"fqbn": "arduino:samd:mkr1000", "type": "mkr1000"}, {"fqbn": "arduino:samd:mkrwifi1010", "type": "nina"}, {"fqbn": "arduino:samd:nano_33_iot", "type": "nina"}, + {"fqbn": "arduino:megaavr:uno2018", "type": "megaavr"}, {"fqbn": "arduino:samd:mkrwan1300", "type": "wan"}, {"fqbn": "arduino:samd:mkrgsm1400", "type": "gsm"}, {"fqbn": "arduino:samd:mkrnb1500", "type": "nb"}, @@ -79,6 +80,20 @@ jobs: sketch-paths: | - examples/utility/Provisioning - examples/utility/SelfProvisioning + # Uno WiFi Rev2 + - board: + type: "megaavr" + platforms: | + - name: arduino:megaavr + libraries: | + - name: ArduinoECCX08 + - name: Arduino_AVRSTL + - name: WiFiNINA + - name: Arduino_JSON + - name: ArduinoBearSSL + sketch-paths: | + - examples/utility/Provisioning + - examples/utility/SelfProvisioning # LoRaWAN boards - board: type: "wan" From 7ed85b088aa4608eeec873db0b8d289b0e8c7060 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Wed, 18 Nov 2020 17:02:14 +0100 Subject: [PATCH 11/26] Force inclusion of Arduino_AVRSTL. --- src/cbor/CBORDecoder.h | 2 +- src/cbor/CBOREncoder.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cbor/CBORDecoder.h b/src/cbor/CBORDecoder.h index f14d51314..c1cdd8330 100644 --- a/src/cbor/CBORDecoder.h +++ b/src/cbor/CBORDecoder.h @@ -23,7 +23,7 @@ ******************************************************************************/ #ifdef __AVR__ #include -#include +#include #endif #undef max diff --git a/src/cbor/CBOREncoder.h b/src/cbor/CBOREncoder.h index 0d614c770..af66bdbfe 100644 --- a/src/cbor/CBOREncoder.h +++ b/src/cbor/CBOREncoder.h @@ -24,7 +24,7 @@ #ifdef __AVR__ #include -#include +#include #endif #include "../property/PropertyContainer.h" From 1656ab0870b04120d11c61bf61f2b742467fb3a1 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 27 Jan 2021 06:23:26 +0100 Subject: [PATCH 12/26] Comment parameter out to prevent unused parameter warning. This function anyway only servers as a stub to allow compilation for megaavr architecture. --- src/ArduinoIoTCloud.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ArduinoIoTCloud.cpp b/src/ArduinoIoTCloud.cpp index 4dfe9c326..0f117e993 100644 --- a/src/ArduinoIoTCloud.cpp +++ b/src/ArduinoIoTCloud.cpp @@ -180,6 +180,7 @@ void ArduinoIoTCloudClass::execCloudEventCallback(ArduinoIoTCloudEvent const eve } } -__attribute__((weak)) void setDebugMessageLevel(int const level) { - //do nothing -} \ No newline at end of file +__attribute__((weak)) void setDebugMessageLevel(int const /* level */) +{ + /* do nothing */ +} From 573ce66c09289c1726dc15193683b80bbb1cd846 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 27 Jan 2021 06:24:56 +0100 Subject: [PATCH 13/26] Clearly disabling DEBUG macros for megaavr architecture. --- src/AIoTC_Config.h | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index d6f60843b..f41fc5519 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -31,14 +31,6 @@ #endif #if defined(ARDUINO_AVR_UNO_WIFI_REV2) -/* Define debug macros which effectively removes them from the - * the codebase for Arduino Uno WiFi. Rev. 2. - */ -# define DEBUG_ERROR -# define DEBUG_WARNING -# define DEBUG_INFO -# define DEBUG_DEBUG -# define DEBUG_VERBOSE /* Provide defines for constants provided within Arduino_DebugUtils * in order to allow older sketches using those constants to still * compile. @@ -52,23 +44,43 @@ #endif #ifndef DEBUG_ERROR - #define DEBUG_ERROR(fmt, ...) Debug.print(DBG_ERROR, fmt, ## __VA_ARGS__) +# if defined(ARDUINO_AVR_UNO_WIFI_REV2) +# define DEBUG_ERROR(fmt, ...) +# else +# define DEBUG_ERROR(fmt, ...) Debug.print(DBG_ERROR, fmt, ## __VA_ARGS__) +# endif #endif #ifndef DEBUG_WARNING - #define DEBUG_WARNING(fmt, ...) Debug.print(DBG_WARNING, fmt, ## __VA_ARGS__) +# if defined(ARDUINO_AVR_UNO_WIFI_REV2) +# define DEBUG_WARNING(fmt, ...) +# else +# define DEBUG_WARNING(fmt, ...) Debug.print(DBG_WARNING, fmt, ## __VA_ARGS__) +# endif #endif #ifndef DEBUG_INFO - #define DEBUG_INFO(fmt, ...) Debug.print(DBG_INFO, fmt, ## __VA_ARGS__) +# if defined(ARDUINO_AVR_UNO_WIFI_REV2) +# define DEBUG_INFO(fmt, ...) +# else +# define DEBUG_INFO(fmt, ...) Debug.print(DBG_INFO, fmt, ## __VA_ARGS__) +# endif #endif #ifndef DEBUG_DEBUG - #define DEBUG_DEBUG(fmt, ...) Debug.print(DBG_DEBUG, fmt, ## __VA_ARGS__) +# if defined(ARDUINO_AVR_UNO_WIFI_REV2) +# define DEBUG_DEBUG(fmt, ...) +# else +# define DEBUG_DEBUG(fmt, ...) Debug.print(DBG_DEBUG, fmt, ## __VA_ARGS__) +# endif #endif #ifndef DEBUG_VERBOSE - #define DEBUG_VERBOSE(fmt, ...) //Debug.print(DBG_VERBOSE, fmt, ## __VA_ARGS__) +# if defined(ARDUINO_AVR_UNO_WIFI_REV2) +# define DEBUG_VERBOSE(fmt, ...) +# else +# define DEBUG_VERBOSE(fmt, ...) //Debug.print(DBG_VERBOSE, fmt, ## __VA_ARGS__) +# endif #endif /****************************************************************************** From 956cd772b154f6616f7d44b70f8a53b0e51e7d1c Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 27 Jan 2021 06:28:37 +0100 Subject: [PATCH 14/26] Correcting debug macros to allow code to compile. --- src/ArduinoIoTCloudTCP.cpp | 64 +++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 064ad74d0..f51c96f4a 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -118,15 +118,35 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) #endif /* OTA_ENABLED */ #ifdef BOARD_HAS_OFFLOADED_ECCX08 - if (!ECCX08.begin()) { DBG_ERROR(F("Cryptography processor failure. Make sure you have a compatible board.")); return 0; } - if (!CryptoUtil::readDeviceId(ECCX08, getDeviceId(), ECCX08Slot::DeviceId)) { DBG_ERROR(F("Cryptography processor read failure.")); return 0; } + if (!ECCX08.begin()) + { + DEBUG_ERROR("Cryptography processor failure. Make sure you have a compatible board."); + return 0; + } + if (!CryptoUtil::readDeviceId(ECCX08, getDeviceId(), ECCX08Slot::DeviceId)) + { + DEBUG_ERROR("Cryptography processor read failure."); + return 0; + } ECCX08.end(); #endif #ifdef BOARD_HAS_ECCX08 - if (!ECCX08.begin()) { DBG_ERROR(F("Cryptography processor failure. Make sure you have a compatible board.")); return 0; } - if (!CryptoUtil::readDeviceId(ECCX08, getDeviceId(), ECCX08Slot::DeviceId)) { DBG_ERROR(F("Cryptography processor read failure.")); return 0; } - if (!CryptoUtil::reconstructCertificate(_eccx08_cert, getDeviceId(), ECCX08Slot::Key, ECCX08Slot::CompressedCertificate, ECCX08Slot::SerialNumberAndAuthorityKeyIdentifier)) { DBG_ERROR(F("Cryptography certificate reconstruction failure.")); return 0; } + if (!ECCX08.begin()) + { + DEBUG_ERROR(F("Cryptography processor failure. Make sure you have a compatible board.")); + return 0; + } + if (!CryptoUtil::readDeviceId(ECCX08, getDeviceId(), ECCX08Slot::DeviceId)) + { + DEBUG_ERROR(F("Cryptography processor read failure.")); + return 0; + } + if (!CryptoUtil::reconstructCertificate(_eccx08_cert, getDeviceId(), ECCX08Slot::Key, ECCX08Slot::CompressedCertificate, ECCX08Slot::SerialNumberAndAuthorityKeyIdentifier)) + { + DEBUG_ERROR(F("Cryptography certificate reconstruction failure.")); + return 0; + } _sslClient.setClient(_connection->getClient()); _sslClient.setEccSlot(static_cast(ECCX08Slot::Key), _eccx08_cert.bytes(), _eccx08_cert.length()); #elif defined(BOARD_ESP) @@ -161,7 +181,7 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) String const nina_fw_version = WiFi.firmwareVersion(); if (nina_fw_version < "1.4.1") { _ota_cap = false; - DBG_WARNING(F("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s"), __FUNCTION__, nina_fw_version.c_str()); + DEBUG_WARNING(F("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s"), __FUNCTION__, nina_fw_version.c_str()); } else { _ota_cap = true; @@ -198,10 +218,10 @@ int ArduinoIoTCloudTCP::connected() void ArduinoIoTCloudTCP::printDebugInfo() { - DBG_INFO(F("***** Arduino IoT Cloud - configuration info *****")); - DBG_INFO(F("Device ID: %s"), getDeviceId().c_str()); - DBG_INFO(F("Thing ID: %s"), getThingId().c_str()); - DBG_INFO(F("MQTT Broker: %s:%d"), _brokerAddress.c_str(), _brokerPort); + DEBUG_INFO(F("***** Arduino IoT Cloud - configuration info *****")); + DEBUG_INFO(F("Device ID: %s"), getDeviceId().c_str()); + DEBUG_INFO(F("Thing ID: %s"), getThingId().c_str()); + DEBUG_INFO(F("MQTT Broker: %s:%d"), _brokerAddress.c_str(), _brokerPort); } /****************************************************************************** @@ -219,7 +239,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy() ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime() { unsigned long const internal_posix_time = _time_service.getTime(); - DBG_VERBOSE(F("ArduinoIoTCloudTCP::%s internal clock configured to posix timestamp %d"), __FUNCTION__, internal_posix_time); + DEBUG_VERBOSE(F("ArduinoIoTCloudTCP::%s internal clock configured to posix timestamp %d"), __FUNCTION__, internal_posix_time); return State::ConnectMqttBroker; } @@ -228,7 +248,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker() if (_mqttClient.connect(_brokerAddress.c_str(), _brokerPort)) return State::SubscribeMqttTopics; - DBG_ERROR(F("ArduinoIoTCloudTCP::%s could not connect to %s:%d"), __FUNCTION__, _brokerAddress.c_str(), _brokerPort); + DEBUG_ERROR(F("ArduinoIoTCloudTCP::%s could not connect to %s:%d"), __FUNCTION__, _brokerAddress.c_str(), _brokerPort); return State::ConnectPhy; } @@ -236,8 +256,8 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics() { if (!_mqttClient.subscribe(_dataTopicIn)) { - DBG_ERROR(F("ArduinoIoTCloudTCP::%s could not subscribe to %s"), __FUNCTION__, _dataTopicIn.c_str()); - DBG_ERROR(F("Check your thing configuration, and press the reset button on your board.")); + DEBUG_ERROR(F("ArduinoIoTCloudTCP::%s could not subscribe to %s"), __FUNCTION__, _dataTopicIn.c_str()); + DEBUG_ERROR(F("Check your thing configuration, and press the reset button on your board.")); return State::SubscribeMqttTopics; } @@ -245,13 +265,13 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics() { if (!_mqttClient.subscribe(_shadowTopicIn)) { - DBG_ERROR(F("ArduinoIoTCloudTCP::%s could not subscribe to %s"), __FUNCTION__, _shadowTopicIn.c_str()); - DBG_ERROR(F("Check your thing configuration, and press the reset button on your board.")); + DEBUG_ERROR(F("ArduinoIoTCloudTCP::%s could not subscribe to %s"), __FUNCTION__, _shadowTopicIn.c_str()); + DEBUG_ERROR(F("Check your thing configuration, and press the reset button on your board.")); return State::SubscribeMqttTopics; } } - DBG_INFO(F("Connected to Arduino IoT Cloud")); + DEBUG_INFO(F("Connected to Arduino IoT Cloud")); execCloudEventCallback(ArduinoIoTCloudEvent::CONNECT); if (_shadowTopicIn != "") @@ -266,7 +286,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues() unsigned long const now = millis(); if ((now - _lastSyncRequestTickTime) > TIMEOUT_FOR_LASTVALUES_SYNC) { - DBG_VERBOSE(F("ArduinoIoTCloudTCP::%s [%d] last values requested"), __FUNCTION__, now); + DEBUG_VERBOSE(F("ArduinoIoTCloudTCP::%s [%d] last values requested"), __FUNCTION__, now); requestLastValue(); _lastSyncRequestTickTime = now; } @@ -278,7 +298,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected() { if (!_mqttClient.connected()) { - DBG_ERROR(F("ArduinoIoTCloudTCP::%s MQTT client connection lost"), __FUNCTION__); + DEBUG_ERROR(F("ArduinoIoTCloudTCP::%s MQTT client connection lost"), __FUNCTION__); /* Forcefully disconnect MQTT client and trigger a reconnection. */ _mqttClient.stop(); @@ -358,7 +378,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length) if ((_shadowTopicIn == topic) && (_state == State::RequestLastValues)) { - DBG_VERBOSE(F("ArduinoIoTCloudTCP::%s [%d] last values received"), __FUNCTION__, millis()); + DEBUG_VERBOSE(F("ArduinoIoTCloudTCP::%s [%d] last values received"), __FUNCTION__, millis()); CBORDecoder::decode(_property_container, (uint8_t*)bytes, length, true); sendPropertiesToCloud(); execCloudEventCallback(ArduinoIoTCloudEvent::SYNC); @@ -408,7 +428,7 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l #if OTA_ENABLED void ArduinoIoTCloudTCP::onOTARequest() { - DBG_VERBOSE(F("ArduinoIoTCloudTCP::%s _ota_url = %s"), __FUNCTION__, _ota_url.c_str()); + DEBUG_VERBOSE(F("ArduinoIoTCloudTCP::%s _ota_url = %s"), __FUNCTION__, _ota_url.c_str()); /* Status flag to prevent the reset from being executed * when HTTPS download is not supported. @@ -424,7 +444,7 @@ void ArduinoIoTCloudTCP::onOTARequest() uint8_t nina_ota_err_code = 0; if (!WiFiStorage.downloadOTA(_ota_url.c_str(), &nina_ota_err_code)) { - DBG_ERROR(F("ArduinoIoTCloudTCP::%s error download to nina: %d"), __FUNCTION__, nina_ota_err_code); + DEBUG_ERROR(F("ArduinoIoTCloudTCP::%s error download to nina: %d"), __FUNCTION__, nina_ota_err_code); _ota_error = static_cast(OTAError::DownloadFailed); return; } From 958f73e9bbbbe92276f8ea01ae92045f8edba925 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 27 Jan 2021 06:36:16 +0100 Subject: [PATCH 15/26] Eliminating 'F(expr)' macro since it's of no use due to the Neumann architecture in samd, mbed and megaavr (all currently supported arduino boards. --- src/ArduinoIoTCloudLPWAN.cpp | 12 +++++------ src/ArduinoIoTCloudTCP.cpp | 40 ++++++++++++++++++------------------ 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/ArduinoIoTCloudLPWAN.cpp b/src/ArduinoIoTCloudLPWAN.cpp index 73ae68c61..29458f21a 100644 --- a/src/ArduinoIoTCloudLPWAN.cpp +++ b/src/ArduinoIoTCloudLPWAN.cpp @@ -87,8 +87,8 @@ void ArduinoIoTCloudLPWAN::update() void ArduinoIoTCloudLPWAN::printDebugInfo() { - DEBUG_INFO(F("***** Arduino IoT Cloud LPWAN - configuration info *****")); - DEBUG_INFO(F("Thing ID: %s"), getThingId().c_str()); + DEBUG_INFO("***** Arduino IoT Cloud LPWAN - configuration info *****"); + DEBUG_INFO("Thing ID: %s", getThingId().c_str()); } /****************************************************************************** @@ -106,8 +106,8 @@ ArduinoIoTCloudLPWAN::State ArduinoIoTCloudLPWAN::handle_ConnectPhy() ArduinoIoTCloudLPWAN::State ArduinoIoTCloudLPWAN::handle_SyncTime() { unsigned long const internal_posix_time = _time_service.getTime(); - DEBUG_VERBOSE(F("ArduinoIoTCloudLPWAN::%s internal clock configured to posix timestamp %d"), __FUNCTION__, internal_posix_time); - DEBUG_INFO(F("Connected to Arduino IoT Cloud")); + DEBUG_VERBOSE("ArduinoIoTCloudLPWAN::%s internal clock configured to posix timestamp %d", __FUNCTION__, internal_posix_time); + DEBUG_INFO("Connected to Arduino IoT Cloud"); return State::Connected; } @@ -115,7 +115,7 @@ ArduinoIoTCloudLPWAN::State ArduinoIoTCloudLPWAN::handle_Connected() { if (!connected()) { - DEBUG_ERROR(F("ArduinoIoTCloudLPWAN::%s connection to gateway lost"), __FUNCTION__); + DEBUG_ERROR("ArduinoIoTCloudLPWAN::%s connection to gateway lost", __FUNCTION__); return State::ConnectPhy; } @@ -175,4 +175,4 @@ int ArduinoIoTCloudLPWAN::writeProperties(const byte data[], int length) ArduinoIoTCloudLPWAN ArduinoCloud; -#endif \ No newline at end of file +#endif diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index f51c96f4a..05385ef06 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -134,17 +134,17 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) #ifdef BOARD_HAS_ECCX08 if (!ECCX08.begin()) { - DEBUG_ERROR(F("Cryptography processor failure. Make sure you have a compatible board.")); + DEBUG_ERROR("Cryptography processor failure. Make sure you have a compatible board."); return 0; } if (!CryptoUtil::readDeviceId(ECCX08, getDeviceId(), ECCX08Slot::DeviceId)) { - DEBUG_ERROR(F("Cryptography processor read failure.")); + DEBUG_ERROR("Cryptography processor read failure."); return 0; } if (!CryptoUtil::reconstructCertificate(_eccx08_cert, getDeviceId(), ECCX08Slot::Key, ECCX08Slot::CompressedCertificate, ECCX08Slot::SerialNumberAndAuthorityKeyIdentifier)) { - DEBUG_ERROR(F("Cryptography certificate reconstruction failure.")); + DEBUG_ERROR("Cryptography certificate reconstruction failure."); return 0; } _sslClient.setClient(_connection->getClient()); @@ -181,7 +181,7 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) String const nina_fw_version = WiFi.firmwareVersion(); if (nina_fw_version < "1.4.1") { _ota_cap = false; - DEBUG_WARNING(F("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s"), __FUNCTION__, nina_fw_version.c_str()); + DEBUG_WARNING("ArduinoIoTCloudTCP::%s In order to be ready for cloud OTA, NINA firmware needs to be >= 1.4.1, current %s", __FUNCTION__, nina_fw_version.c_str()); } else { _ota_cap = true; @@ -218,10 +218,10 @@ int ArduinoIoTCloudTCP::connected() void ArduinoIoTCloudTCP::printDebugInfo() { - DEBUG_INFO(F("***** Arduino IoT Cloud - configuration info *****")); - DEBUG_INFO(F("Device ID: %s"), getDeviceId().c_str()); - DEBUG_INFO(F("Thing ID: %s"), getThingId().c_str()); - DEBUG_INFO(F("MQTT Broker: %s:%d"), _brokerAddress.c_str(), _brokerPort); + DEBUG_INFO("***** Arduino IoT Cloud - configuration info *****"); + DEBUG_INFO("Device ID: %s", getDeviceId().c_str()); + DEBUG_INFO("Thing ID: %s", getThingId().c_str()); + DEBUG_INFO("MQTT Broker: %s:%d", _brokerAddress.c_str(), _brokerPort); } /****************************************************************************** @@ -239,7 +239,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectPhy() ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SyncTime() { unsigned long const internal_posix_time = _time_service.getTime(); - DEBUG_VERBOSE(F("ArduinoIoTCloudTCP::%s internal clock configured to posix timestamp %d"), __FUNCTION__, internal_posix_time); + DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s internal clock configured to posix timestamp %d", __FUNCTION__, internal_posix_time); return State::ConnectMqttBroker; } @@ -248,7 +248,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_ConnectMqttBroker() if (_mqttClient.connect(_brokerAddress.c_str(), _brokerPort)) return State::SubscribeMqttTopics; - DEBUG_ERROR(F("ArduinoIoTCloudTCP::%s could not connect to %s:%d"), __FUNCTION__, _brokerAddress.c_str(), _brokerPort); + DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not connect to %s:%d", __FUNCTION__, _brokerAddress.c_str(), _brokerPort); return State::ConnectPhy; } @@ -256,8 +256,8 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics() { if (!_mqttClient.subscribe(_dataTopicIn)) { - DEBUG_ERROR(F("ArduinoIoTCloudTCP::%s could not subscribe to %s"), __FUNCTION__, _dataTopicIn.c_str()); - DEBUG_ERROR(F("Check your thing configuration, and press the reset button on your board.")); + DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not subscribe to %s", __FUNCTION__, _dataTopicIn.c_str()); + DEBUG_ERROR("Check your thing configuration, and press the reset button on your board."); return State::SubscribeMqttTopics; } @@ -265,13 +265,13 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics() { if (!_mqttClient.subscribe(_shadowTopicIn)) { - DEBUG_ERROR(F("ArduinoIoTCloudTCP::%s could not subscribe to %s"), __FUNCTION__, _shadowTopicIn.c_str()); - DEBUG_ERROR(F("Check your thing configuration, and press the reset button on your board.")); + DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not subscribe to %s", __FUNCTION__, _shadowTopicIn.c_str()); + DEBUG_ERROR("Check your thing configuration, and press the reset button on your board."); return State::SubscribeMqttTopics; } } - DEBUG_INFO(F("Connected to Arduino IoT Cloud")); + DEBUG_INFO("Connected to Arduino IoT Cloud"); execCloudEventCallback(ArduinoIoTCloudEvent::CONNECT); if (_shadowTopicIn != "") @@ -286,7 +286,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_RequestLastValues() unsigned long const now = millis(); if ((now - _lastSyncRequestTickTime) > TIMEOUT_FOR_LASTVALUES_SYNC) { - DEBUG_VERBOSE(F("ArduinoIoTCloudTCP::%s [%d] last values requested"), __FUNCTION__, now); + DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values requested", __FUNCTION__, now); requestLastValue(); _lastSyncRequestTickTime = now; } @@ -298,7 +298,7 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_Connected() { if (!_mqttClient.connected()) { - DEBUG_ERROR(F("ArduinoIoTCloudTCP::%s MQTT client connection lost"), __FUNCTION__); + DEBUG_ERROR("ArduinoIoTCloudTCP::%s MQTT client connection lost", __FUNCTION__); /* Forcefully disconnect MQTT client and trigger a reconnection. */ _mqttClient.stop(); @@ -378,7 +378,7 @@ void ArduinoIoTCloudTCP::handleMessage(int length) if ((_shadowTopicIn == topic) && (_state == State::RequestLastValues)) { - DEBUG_VERBOSE(F("ArduinoIoTCloudTCP::%s [%d] last values received"), __FUNCTION__, millis()); + DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s [%d] last values received", __FUNCTION__, millis()); CBORDecoder::decode(_property_container, (uint8_t*)bytes, length, true); sendPropertiesToCloud(); execCloudEventCallback(ArduinoIoTCloudEvent::SYNC); @@ -428,7 +428,7 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l #if OTA_ENABLED void ArduinoIoTCloudTCP::onOTARequest() { - DEBUG_VERBOSE(F("ArduinoIoTCloudTCP::%s _ota_url = %s"), __FUNCTION__, _ota_url.c_str()); + DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_url = %s"), __FUNCTION__, _ota_url.c_str()); /* Status flag to prevent the reset from being executed * when HTTPS download is not supported. @@ -444,7 +444,7 @@ void ArduinoIoTCloudTCP::onOTARequest() uint8_t nina_ota_err_code = 0; if (!WiFiStorage.downloadOTA(_ota_url.c_str(), &nina_ota_err_code)) { - DEBUG_ERROR(F("ArduinoIoTCloudTCP::%s error download to nina: %d"), __FUNCTION__, nina_ota_err_code); + DEBUG_ERROR("ArduinoIoTCloudTCP::%s error download to nina: %d"), __FUNCTION__, nina_ota_err_code); _ota_error = static_cast(OTAError::DownloadFailed); return; } From d9d50663b36381d08aa2859278fd2e747de9aa08 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 27 Jan 2021 06:39:08 +0100 Subject: [PATCH 16/26] List 'megaavr' as supported platform. --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index 9b41bd40e..0b450496c 100644 --- a/library.properties +++ b/library.properties @@ -6,6 +6,6 @@ sentence=This library allows to connect to the Arduino IoT Cloud service. paragraph=It provides a ConnectionManager to handle connection/disconnection, property-change updates and events callbacks. The supported boards are MKRGSM, MKR1000 and WiFi101. category=Communication url=https://github.com/arduino-libraries/ArduinoIoTCloud -architectures=mbed,samd,esp8266 +architectures=mbed,samd,esp8266,megaavr includes=ArduinoIoTCloud.h depends=Arduino_ConnectionHandler,Arduino_DebugUtils,ArduinoMqttClient,ArduinoECCX08,RTCZero From 11217fd44bd6efcb6746652d189ab57a003b4a1b Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 27 Jan 2021 06:43:18 +0100 Subject: [PATCH 17/26] As Arduino_AVRSTL is not yet released directly link to the repository. --- .github/workflows/compile-examples.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index 6700631bf..22d4a42a4 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -87,7 +87,7 @@ jobs: - name: arduino:megaavr libraries: | - name: ArduinoECCX08 - - name: Arduino_AVRSTL + - source-url: https://github.com/arduino-libraries/Arduino_AVRSTL.git - name: WiFiNINA - name: Arduino_JSON - name: ArduinoBearSSL From 7e3e196edf61a2f09777105daafee204bef81b10 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 27 Jan 2021 06:48:14 +0100 Subject: [PATCH 18/26] Fixing as-of-yet-not-fixed wrong braces. --- src/ArduinoIoTCloudTCP.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 05385ef06..2c5870530 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -428,7 +428,7 @@ int ArduinoIoTCloudTCP::write(String const topic, byte const data[], int const l #if OTA_ENABLED void ArduinoIoTCloudTCP::onOTARequest() { - DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_url = %s"), __FUNCTION__, _ota_url.c_str()); + DEBUG_VERBOSE("ArduinoIoTCloudTCP::%s _ota_url = %s", __FUNCTION__, _ota_url.c_str()); /* Status flag to prevent the reset from being executed * when HTTPS download is not supported. @@ -444,7 +444,7 @@ void ArduinoIoTCloudTCP::onOTARequest() uint8_t nina_ota_err_code = 0; if (!WiFiStorage.downloadOTA(_ota_url.c_str(), &nina_ota_err_code)) { - DEBUG_ERROR("ArduinoIoTCloudTCP::%s error download to nina: %d"), __FUNCTION__, nina_ota_err_code); + DEBUG_ERROR("ArduinoIoTCloudTCP::%s error download to nina: %d", __FUNCTION__, nina_ota_err_code); _ota_error = static_cast(OTAError::DownloadFailed); return; } From f9e85405a4c0fd556131cc430b1d7f5a4e0fabd5 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 27 Jan 2021 09:52:55 +0100 Subject: [PATCH 19/26] Cleaning up include structure. --- src/cbor/CBORDecoder.h | 6 ++++-- src/cbor/CBOREncoder.h | 9 +++++++-- src/property/Property.h | 11 +++++++++-- src/property/PropertyContainer.h | 6 ++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/cbor/CBORDecoder.h b/src/cbor/CBORDecoder.h index c1cdd8330..d1bf91ca0 100644 --- a/src/cbor/CBORDecoder.h +++ b/src/cbor/CBORDecoder.h @@ -21,9 +21,11 @@ /****************************************************************************** INCLUDE ******************************************************************************/ -#ifdef __AVR__ + #include -#include + +#ifdef __AVR__ +# include #endif #undef max diff --git a/src/cbor/CBOREncoder.h b/src/cbor/CBOREncoder.h index af66bdbfe..2370a7b50 100644 --- a/src/cbor/CBOREncoder.h +++ b/src/cbor/CBOREncoder.h @@ -22,11 +22,16 @@ * INCLUDE ******************************************************************************/ -#ifdef __AVR__ #include -#include + +#ifdef __AVR__ +# include #endif +#undef max +#undef min +#include + #include "../property/PropertyContainer.h" /****************************************************************************** diff --git a/src/property/Property.h b/src/property/Property.h index f8db8a948..803d77049 100644 --- a/src/property/Property.h +++ b/src/property/Property.h @@ -28,11 +28,18 @@ ******************************************************************************/ #include -// in order to allow to define its own max and min functions + #undef max #undef min + +#ifdef __AVR__ +# include +# include +#else +# include +#endif + #include -#include #include "../cbor/lib/tinycbor/cbor-lib.h" diff --git a/src/property/PropertyContainer.h b/src/property/PropertyContainer.h index 3fd807a34..effa7d966 100644 --- a/src/property/PropertyContainer.h +++ b/src/property/PropertyContainer.h @@ -22,8 +22,14 @@ INCLUDE ******************************************************************************/ +#include + #include "Property.h" +#ifdef __AVR__ +# include +#endif + #undef max #undef min #include From b57d900e04a16034d4fc7f38345288abe7f5c637 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Wed, 27 Jan 2021 17:28:09 +0100 Subject: [PATCH 20/26] megaAVR: don't use NTP time fallback It's required by the offloaded ecc anyway, so it will be ok --- src/utility/time/TimeService.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index b5e35dcd7..07d50fec6 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -105,6 +105,7 @@ unsigned long TimeService::getRemoteTime() return connection_time; } +#ifndef __AVR__ /* If no valid network time is available try to obtain the * time via NTP next. */ @@ -112,6 +113,7 @@ unsigned long TimeService::getRemoteTime() if(isTimeValid(ntp_time)) { return ntp_time; } +#endif #endif /* ifndef HAS_LORA */ From 4865f508c4de17eafc2b2eaf40738eb5bf020fb4 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 28 Jan 2021 07:38:39 +0100 Subject: [PATCH 21/26] Add braces to ensure correct order of operant execution. --- src/AIoTC_Config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index f41fc5519..09052bf58 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -100,7 +100,7 @@ #define OTA_STORAGE_SSU (0) #endif -#if OTA_STORAGE_SFU || OTA_STORAGE_SSU || OTA_STORAGE_SNU && !defined(ARDUINO_AVR_UNO_WIFI_REV2) +#if (OTA_STORAGE_SFU || OTA_STORAGE_SSU || OTA_STORAGE_SNU) && !defined(ARDUINO_AVR_UNO_WIFI_REV2) #define OTA_ENABLED (1) #else #define OTA_ENABLED (0) From 86b117026d9d13d8cd5b9bc4c03a4bc6a0132271 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 28 Jan 2021 07:54:54 +0100 Subject: [PATCH 22/26] Prevent execution of cloud firmware if a nina firmware < 1.4.2 is present. --- src/ArduinoIoTCloudTCP.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 2c5870530..2fa5eb039 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -103,6 +103,15 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) _brokerAddress = brokerAddress; _brokerPort = brokerPort; +#if defined(__AVR__) + String const nina_fw_version = WiFi.firmwareVersion(); + if (nina_fw_version < "1.4.2") + { + DEBUG_ERROR("ArduinoIoTCloudTCP::%s NINA firmware needs to be >= 1.4.2 to support cloud on Uno WiFi Rev. 2, current %s", __FUNCTION__, nina_fw_version.c_str()); + for(;;) { } + } +#endif /* AVR */ + #if OTA_ENABLED && !defined(__AVR__) /* Calculate the SHA256 checksum over the firmware stored in the flash of the * MCU. Note: As we don't know the length per-se we read chunks of the flash From 6e0e75f2fda047406b290210283c52d8950d4c27 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 28 Jan 2021 07:58:59 +0100 Subject: [PATCH 23/26] Automatically include Arduino_DebugUtils if any of the debug macros is actually defined, i.e. if any debug output is enabled on the platform. --- src/AIoTC_Config.h | 26 +++++++++++++------------- src/ArduinoIoTCloud.h | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index 09052bf58..7188a9447 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -30,19 +30,6 @@ #define NTP_USE_RANDOM_PORT (1) #endif -#if defined(ARDUINO_AVR_UNO_WIFI_REV2) -/* Provide defines for constants provided within Arduino_DebugUtils - * in order to allow older sketches using those constants to still - * compile. - */ -# define DBG_NONE -1 -# define DBG_ERROR 0 -# define DBG_WARNING 1 -# define DBG_INFO 2 -# define DBG_DEBUG 3 -# define DBG_VERBOSE 4 -#endif - #ifndef DEBUG_ERROR # if defined(ARDUINO_AVR_UNO_WIFI_REV2) # define DEBUG_ERROR(fmt, ...) @@ -83,6 +70,19 @@ # endif #endif +#if defined(ARDUINO_AVR_UNO_WIFI_REV2) && !(defined(DEBUG_ERROR) || defined(DEBUG_WARNING) || defined(DEBUG_INFO) || defined(DEBUG_DEBUG) || defined(DEBUG_VERBOSE)) +/* Provide defines for constants provided within Arduino_DebugUtils + * in order to allow older sketches using those constants to still + * compile. + */ +# define DBG_NONE -1 +# define DBG_ERROR 0 +# define DBG_WARNING 1 +# define DBG_INFO 2 +# define DBG_DEBUG 3 +# define DBG_VERBOSE 4 +#endif + /****************************************************************************** * AUTOMATICALLY CONFIGURED DEFINES ******************************************************************************/ diff --git a/src/ArduinoIoTCloud.h b/src/ArduinoIoTCloud.h index bf70679e4..0130e3efc 100644 --- a/src/ArduinoIoTCloud.h +++ b/src/ArduinoIoTCloud.h @@ -26,7 +26,7 @@ #include -#if !defined(__AVR__) +#if defined(DEBUG_ERROR) || defined(DEBUG_WARNING) || defined(DEBUG_INFO) || defined(DEBUG_DEBUG) || defined(DEBUG_VERBOSE) # include #endif From 9b49af2ffd7ca7fa663c3254457b60854b4a3242 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 28 Jan 2021 07:59:57 +0100 Subject: [PATCH 24/26] Enable ERROR message output for Uno WiFi Rev. 2 (how should you be able to see errors otherwise)? --- src/AIoTC_Config.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/AIoTC_Config.h b/src/AIoTC_Config.h index 7188a9447..59292db11 100644 --- a/src/AIoTC_Config.h +++ b/src/AIoTC_Config.h @@ -32,7 +32,7 @@ #ifndef DEBUG_ERROR # if defined(ARDUINO_AVR_UNO_WIFI_REV2) -# define DEBUG_ERROR(fmt, ...) +# define DEBUG_ERROR(fmt, ...) Debug.print(DBG_ERROR, fmt, ## __VA_ARGS__) # else # define DEBUG_ERROR(fmt, ...) Debug.print(DBG_ERROR, fmt, ## __VA_ARGS__) # endif From 577f305e7150849a9a357d3be4f04d59299f9ddd Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 28 Jan 2021 08:04:10 +0100 Subject: [PATCH 25/26] Bugfix: Don't loop forever, just return 0. --- src/ArduinoIoTCloudTCP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index 2fa5eb039..d1e886eee 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -108,7 +108,7 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) if (nina_fw_version < "1.4.2") { DEBUG_ERROR("ArduinoIoTCloudTCP::%s NINA firmware needs to be >= 1.4.2 to support cloud on Uno WiFi Rev. 2, current %s", __FUNCTION__, nina_fw_version.c_str()); - for(;;) { } + return 0; } #endif /* AVR */ From 8383de1271633a79378a3e666a8dd9547ec65bac Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Thu, 28 Jan 2021 08:06:18 +0100 Subject: [PATCH 26/26] Shortening error messages to conserve flash and eliminate unneccsary error messages. --- src/ArduinoIoTCloudTCP.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ArduinoIoTCloudTCP.cpp b/src/ArduinoIoTCloudTCP.cpp index d1e886eee..4a68ac7ae 100644 --- a/src/ArduinoIoTCloudTCP.cpp +++ b/src/ArduinoIoTCloudTCP.cpp @@ -129,12 +129,12 @@ int ArduinoIoTCloudTCP::begin(String brokerAddress, uint16_t brokerPort) #ifdef BOARD_HAS_OFFLOADED_ECCX08 if (!ECCX08.begin()) { - DEBUG_ERROR("Cryptography processor failure. Make sure you have a compatible board."); + DEBUG_ERROR("ECCX08.begin() failed."); return 0; } if (!CryptoUtil::readDeviceId(ECCX08, getDeviceId(), ECCX08Slot::DeviceId)) { - DEBUG_ERROR("Cryptography processor read failure."); + DEBUG_ERROR("CryptoUtil::readDeviceId(...) failed."); return 0; } ECCX08.end(); @@ -266,7 +266,9 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics() if (!_mqttClient.subscribe(_dataTopicIn)) { DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not subscribe to %s", __FUNCTION__, _dataTopicIn.c_str()); +#if !defined(__AVR__) DEBUG_ERROR("Check your thing configuration, and press the reset button on your board."); +#endif return State::SubscribeMqttTopics; } @@ -275,7 +277,9 @@ ArduinoIoTCloudTCP::State ArduinoIoTCloudTCP::handle_SubscribeMqttTopics() if (!_mqttClient.subscribe(_shadowTopicIn)) { DEBUG_ERROR("ArduinoIoTCloudTCP::%s could not subscribe to %s", __FUNCTION__, _shadowTopicIn.c_str()); +#if !defined(__AVR__) DEBUG_ERROR("Check your thing configuration, and press the reset button on your board."); +#endif return State::SubscribeMqttTopics; } }