diff --git a/.github/workflows/check-arduino.yml b/.github/workflows/check-arduino.yml index ed673ea6d..35658bf6c 100644 --- a/.github/workflows/check-arduino.yml +++ b/.github/workflows/check-arduino.yml @@ -16,7 +16,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@v5 - name: Arduino Lint uses: arduino/arduino-lint-action@v2 diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index c914adc14..33ce58527 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -26,12 +26,12 @@ jobs: - name: Arduino_DebugUtils - name: ArduinoMqttClient - name: Arduino_SecureElement + - name: Arduino_CloudUtils # sketch paths to compile (recursive) for all boards UNIVERSAL_SKETCH_PATHS: | - examples/ArduinoIoTCloud-Advanced - examples/ArduinoIoTCloud-Basic - examples/ArduinoIoTCloud-Callbacks - - examples/ArduinoIoTCloud-Schedule - examples/utility/ArduinoIoTCloud_Travis_CI SKETCHES_REPORTS_PATH: sketches-reports @@ -91,6 +91,9 @@ jobs: - fqbn: arduino:mbed_edge:edge_control type: mbed_edge artifact-name-suffix: arduino-mbed_edge-edge_control + - fqbn: "rp2040:rp2040:rpipicow" + type: rp2040 + artifact-name-suffix: rp2040-rp2040-rpipicow # make board type-specific customizations to the matrix jobs @@ -102,6 +105,7 @@ jobs: # Install samd platform via Boards Manager - name: arduino:samd libraries: | + - name: ArduinoBearSSL - name: ArduinoECCX08 - name: Blues Wireless Notecard - name: RTCZero @@ -109,6 +113,7 @@ jobs: - source-url: https://github.com/adafruit/Adafruit_SleepyDog.git sketch-paths: | - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule - examples/utility/Provisioning # MKR WiFi 1010, Nano 33 IoT, Nano RP2040 Connect - board: @@ -118,6 +123,7 @@ jobs: - name: arduino:samd - name: arduino:mbed_nano libraries: | + - name: ArduinoBearSSL - name: ArduinoECCX08 - name: Blues Wireless Notecard - name: RTCZero @@ -127,6 +133,7 @@ jobs: sketch-paths: | - examples/ArduinoIoTCloud-DeferredOTA - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule - examples/utility/Provisioning - examples/utility/SelfProvisioning - board: @@ -149,6 +156,7 @@ jobs: # Install samd platform via Boards Manager - name: arduino:samd libraries: | + - name: ArduinoBearSSL - name: ArduinoECCX08 - name: Blues Wireless Notecard - name: RTCZero @@ -156,6 +164,7 @@ jobs: - source-url: https://github.com/adafruit/Adafruit_SleepyDog.git sketch-paths: | - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule - examples/utility/Provisioning # NB boards - board: @@ -164,6 +173,7 @@ jobs: # Install samd platform via Boards Manager - name: arduino:samd libraries: | + - name: ArduinoBearSSL - name: ArduinoECCX08 - name: Blues Wireless Notecard - name: RTCZero @@ -171,6 +181,7 @@ jobs: - source-url: https://github.com/adafruit/Adafruit_SleepyDog.git sketch-paths: | - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule - examples/utility/Provisioning # Portenta - board: @@ -179,12 +190,18 @@ jobs: # Install mbed_portenta platform via Boards Manager - name: arduino:mbed_portenta libraries: | + - name: ArduinoBearSSL - name: ArduinoECCX08 - name: Arduino_Cellular - name: Blues Wireless Notecard + - name: ArduinoBLE + - name: Arduino_KVStore + - name: Arduino_NetworkConfigurator sketch-paths: | + - examples/ArduinoIoTCloud-NetConfig - examples/ArduinoIoTCloud-DeferredOTA - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule - examples/utility/Provisioning # Nicla Vision - board: @@ -194,9 +211,14 @@ jobs: - name: arduino:mbed_nicla libraries: | - name: Blues Wireless Notecard + - name: ArduinoBLE + - name: Arduino_KVStore + - name: Arduino_NetworkConfigurator sketch-paths: | + - examples/ArduinoIoTCloud-NetConfig - examples/ArduinoIoTCloud-DeferredOTA - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule - examples/utility/Provisioning # Opta - board: @@ -205,11 +227,17 @@ jobs: # Install mbed_opta platform via Boards Manager - name: arduino:mbed_opta libraries: | + - name: ArduinoBearSSL - name: ArduinoECCX08 - name: Blues Wireless Notecard + - name: ArduinoBLE + - name: Arduino_KVStore + - name: Arduino_NetworkConfigurator sketch-paths: | + - examples/ArduinoIoTCloud-NetConfig - examples/ArduinoIoTCloud-DeferredOTA - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule - examples/utility/Provisioning # GIGA - board: @@ -218,11 +246,17 @@ jobs: # Install mbed_giga platform via Boards Manager - name: arduino:mbed_giga libraries: | + - name: ArduinoBearSSL - name: ArduinoECCX08 - name: Blues Wireless Notecard + - name: ArduinoBLE + - name: Arduino_KVStore + - name: Arduino_NetworkConfigurator sketch-paths: | + - examples/ArduinoIoTCloud-NetConfig - examples/ArduinoIoTCloud-DeferredOTA - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule - examples/utility/Provisioning # Portenta C33 - board: @@ -233,8 +267,13 @@ jobs: libraries: | - name: Arduino_Cellular - name: Blues Wireless Notecard + - name: ArduinoBLE + - name: Arduino_KVStore + - name: Arduino_NetworkConfigurator sketch-paths: | + - examples/ArduinoIoTCloud-NetConfig - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule - examples/utility/Provisioning # UNO R4 WiFi - board: @@ -244,8 +283,13 @@ jobs: - name: arduino:renesas_uno libraries: | - name: Blues Wireless Notecard + - name: ArduinoBLE + - name: Arduino_KVStore + - name: Arduino_NetworkConfigurator sketch-paths: | + - examples/ArduinoIoTCloud-NetConfig - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule # Nano ESP32 - board: type: arduino_esp32 @@ -257,6 +301,7 @@ jobs: sketch-paths: | - examples/ArduinoIoTCloud-DeferredOTA - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule # Edge Control - board: type: mbed_edge @@ -267,6 +312,7 @@ jobs: - name: Blues Wireless Notecard sketch-paths: | - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule # ESP8266 boards - board: type: esp8266 @@ -277,7 +323,8 @@ jobs: # Use the version currently installed in Arduino Cloud version: 2.5.0 libraries: - sketch-paths: + sketch-paths: | + - examples/ArduinoIoTCloud-Schedule # ESP32 boards - board: type: esp32 @@ -290,10 +337,18 @@ jobs: sketch-paths: | - examples/ArduinoIoTCloud-DeferredOTA - examples/ArduinoIoTCloud-Notecard + - examples/ArduinoIoTCloud-Schedule + # PicoW + - board: + type: rp2040 + platforms: | + # Install rp2040 platform via Boards Manager + - name: rp2040:rp2040 + source-url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v5 - name: Install ESP32 platform dependencies if: matrix.board.type == 'esp32' diff --git a/.github/workflows/spell-check.yml b/.github/workflows/spell-check.yml index 4253ed878..4a701170b 100644 --- a/.github/workflows/spell-check.yml +++ b/.github/workflows/spell-check.yml @@ -10,7 +10,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v5 - name: Spell check uses: codespell-project/actions-codespell@master diff --git a/.github/workflows/sync-labels.yml b/.github/workflows/sync-labels.yml index 8c422de0f..62c898a4a 100644 --- a/.github/workflows/sync-labels.yml +++ b/.github/workflows/sync-labels.yml @@ -27,11 +27,11 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@v5 - name: Download JSON schema for labels configuration file id: download-schema - uses: carlosperate/download-file-action@v1 + uses: carlosperate/download-file-action@v2 with: file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/arduino-tooling-gh-label-configuration-schema.json location: ${{ runner.temp }}/label-configuration-schema @@ -65,7 +65,7 @@ jobs: steps: - name: Download - uses: carlosperate/download-file-action@v1 + uses: carlosperate/download-file-action@v2 with: file-url: https://raw.githubusercontent.com/arduino/tooling-project-assets/main/workflow-templates/assets/sync-labels/${{ matrix.filename }} @@ -105,10 +105,10 @@ jobs: echo "::set-output name=flag::--dry-run" - name: Checkout repository - uses: actions/checkout@v4 + uses: actions/checkout@v5 - name: Download configuration files artifact - uses: actions/download-artifact@v4 + uses: actions/download-artifact@v5 with: name: ${{ env.CONFIGURATIONS_ARTIFACT }} path: ${{ env.CONFIGURATIONS_FOLDER }} diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 10d06cf27..95063c7cb 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -23,7 +23,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v4 + uses: actions/checkout@v5 - uses: arduino/cpp-test-action@main with: @@ -32,7 +32,6 @@ jobs: coverage-exclude-paths: | - '*/extras/test/*' - '/usr/*' - - '*/src/cbor/lib/*' coverage-data-path: ${{ env.COVERAGE_DATA_PATH }} # A token is used to avoid intermittent spurious job failures caused by rate limiting. diff --git a/.gitignore b/.gitignore index 7704f8888..c71e1ae86 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,21 @@ .vs build .idea/ +### CMake ### +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps + +### CMake Patch ### +CMakeUserPresets.json + +# External projects +*-prefix/ diff --git a/LICENSE b/LICENSE index 4a1b324fd..f288702d2 100644 --- a/LICENSE +++ b/LICENSE @@ -1,17 +1,3 @@ -This file includes licensing information for ArduinoIoTCloud - -Copyright (c) 2019 ARDUINO SA (www.arduino.cc) - -The software is released under the GNU General Public License, which covers the main body -of the ArduinoIoTCloud code. The terms of this license can be found at: -https://www.gnu.org/licenses/gpl-3.0.en.html - -You can be released from the requirements of the above licenses by purchasing -a commercial license. Buying such a license is mandatory if you want to modify or -otherwise use the software for commercial activities involving the Arduino -software without disclosing the source code of your own applications. To purchase -a commercial license, send an email to license@arduino.cc - GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 diff --git a/README.md b/README.md index 4b248e784..93a0829a8 100644 --- a/README.md +++ b/README.md @@ -96,3 +96,9 @@ Boards can authenticate to the ArduinoIoTCloud servers using 3 methods: * `DEVICE_CERTIFICATE` and `PRIVATE_KEY`. This values are stored inside the board secure element during the device provisioning phase. Boards that are using this method are: [`MKR 1000`](https://store.arduino.cc/arduino-mkr1000-wifi), [`MKR WiFi 1010`](https://store.arduino.cc/arduino-mkr-wifi-1010), [`MKR GSM 1400`](https://store.arduino.cc/arduino-mkr-gsm-1400-1415), [`MKR NB 1500`](https://store.arduino.cc/arduino-mkr-nb-1500-1413), [`Nano 33 IoT`](https://store.arduino.cc/arduino-nano-33-iot), [`Portenta H7`](https://store.arduino.cc/portenta-h7), [`Nano RP2040 Connect`](https://store.arduino.cc/products/arduino-nano-rp2040-connect), [`Nicla Vision`](https://store.arduino.cc/products/nicla-vision), [`OPTA WiFi`](https://store.arduino.cc/products/opta-wifi), [`OPTA RS485`](https://store.arduino.cc/products/opta-rs485), [`OPTA Lite`](https://store.arduino.cc/products/opta-lite), [`GIGA R1 WiFi`](https://store.arduino.cc/products/giga-r1-wifi), [`Portenta C33`](https://store.arduino.cc/products/portenta-c33) * `APP_EUI` and `APP_KEY`. This values are defined in the `thingProperties.h` file and included in the Sketch. Boards that are using this method are: [`MKR WAN 1300/1310`](https://store.arduino.cc/mkr-wan-1310) + +### License + +The ArduinoIoTCloud library is licensed under the GNU General Public License v3.0. + +You can be released from the requirements of the above license by purchasing a commercial license. Buying such a license is mandatory if you want to modify or otherwise use the software for commercial activities involving the Arduino software without disclosing the source code of your own applications. To purchase a commercial license, send an email to license@arduino.cc diff --git a/examples/ArduinoIoTCloud-AWS-Basic/ArduinoIoTCloud-AWS-Basic.ino b/examples/ArduinoIoTCloud-AWS-Basic/ArduinoIoTCloud-AWS-Basic.ino new file mode 100644 index 000000000..3213a3bf8 --- /dev/null +++ b/examples/ArduinoIoTCloud-AWS-Basic/ArduinoIoTCloud-AWS-Basic.ino @@ -0,0 +1,151 @@ +#include "arduino_secrets.h" +/* + This sketch demonstrates how to connect to ArduinoIoTCloud and AWS IoT core. + + The full list of compatible boards can be found here: + - https://github.com/arduino-libraries/ArduinoIoTCloud#what +*/ + +#include "thingProperties.h" +#include "aws_secrets.h" + +Client& getDefaultClient() { + switch(ArduinoIoTPreferredConnection.getInterface()) { + +#ifdef BOARD_HAS_WIFI + case NetworkAdapter::WIFI: + static WiFiClient wclient; + return wclient; +#endif + +#ifdef BOARD_HAS_ETHERNET + case NetworkAdapter::ETHERNET: + static EthernetClient eclient; + return eclient; +#endif + + default: + Serial.println("Error: could not create default AWS client"); + break; + } +} + +unsigned long publishMillis = 0; +unsigned long connectMillis = 0; + +BearSSLClient sslClientAWS(getDefaultClient()); +MqttClient mqttClientAWS(sslClientAWS); + +void setup() { + /* Initialize serial and wait up to 5 seconds for port to open */ + Serial.begin(9600); + + /* Configure LED pin as an output */ + pinMode(LED_BUILTIN, OUTPUT); + + /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ + initProperties(); + + /* Initialize Arduino IoT Cloud library */ + ArduinoCloud.begin(ArduinoIoTPreferredConnection, true, "iot.arduino.cc"); + + setDebugMessageLevel(5); + ArduinoCloud.printDebugInfo(); + + /* Initialize AWS Client */ + ArduinoBearSSL.onGetTime(getTime); + sslClientAWS.setEccSlot(AWS_SLOT, AWS_CERTIFICATE); + + mqttClientAWS.setId("ArduinoAWSClient"); + mqttClientAWS.onMessage(onMessageReceived); + mqttClientAWS.setConnectionTimeout(10 * 1000); + mqttClientAWS.setKeepAliveInterval(30 * 1000); + mqttClientAWS.setCleanSession(false); +} + +void loop() { + ArduinoCloud.update(); + potentiometer = analogRead(A0); + seconds = millis() / 1000; + + if (!ArduinoCloud.connected()) { + return; + } + + if (!mqttClientAWS.connected()) { + if (millis() - connectMillis > 5000) { + connectMillis = millis(); + // MQTT client is disconnected, connect + if (!connectMQTT()) { + return; + } + } else { + return; + } + } + + // poll for new MQTT messages and send keep alive + mqttClientAWS.poll(); + + // publish a message roughly every 5 seconds. + if (millis() - publishMillis > 5000) { + publishMillis = millis(); + + publishMessage(); + } +} + +/* + * 'onLedChange' is called when the "led" property of your Thing changes + */ +void onLedChange() { + Serial.print("LED set to "); + Serial.println(led); + digitalWrite(LED_BUILTIN, led); +} + +void onMessageReceived(int messageSize) +{ + // we received a message, print out the topic and contents + Serial.print("Received a message with topic '"); + Serial.print(mqttClientAWS.messageTopic()); + Serial.print("', length "); + Serial.print(messageSize); + Serial.println(" bytes:"); + + for (int i = 0; i < messageSize; i++) { + const char c = mqttClientAWS.read(); + Serial.print(c); + } + Serial.println(); +} + +int connectMQTT() { + Serial.print("Attempting to connect to MQTT broker: "); + Serial.print(AWS_BROKER); + Serial.println(" "); + + if (!mqttClientAWS.connect(AWS_BROKER, 8883)) { + // failed, retry + Serial.print("."); + return 0; + } + Serial.println(); + + Serial.println("You're connected to the MQTT broker"); + Serial.println(); + + // subscribe to a topic + mqttClientAWS.subscribe("arduino/incoming"); + return 1; +} + +void publishMessage() { + Serial.println("Publishing message"); + + // send message, the Print interface can be used to set the message contents + mqttClientAWS.beginMessage("arduino/outgoing"); + mqttClientAWS.print("hello "); + mqttClientAWS.print(millis()); + mqttClientAWS.endMessage(); +} diff --git a/examples/ArduinoIoTCloud-AWS-Basic/arduino_secrets.h b/examples/ArduinoIoTCloud-AWS-Basic/arduino_secrets.h new file mode 100644 index 000000000..dee3e7210 --- /dev/null +++ b/examples/ArduinoIoTCloud-AWS-Basic/arduino_secrets.h @@ -0,0 +1,2 @@ +#define SECRET_SSID "" +#define SECRET_OPTIONAL_PASS "" diff --git a/examples/ArduinoIoTCloud-AWS-Basic/aws_secrets.h b/examples/ArduinoIoTCloud-AWS-Basic/aws_secrets.h new file mode 100644 index 000000000..07131d1da --- /dev/null +++ b/examples/ArduinoIoTCloud-AWS-Basic/aws_secrets.h @@ -0,0 +1,10 @@ +/* Fill in the hostname of your AWS IoT broker */ +#define AWS_BROKER "" + +#define AWS_SLOT 4 + +/* Fill in the boards public certificate */ +const char AWS_CERTIFICATE[] = R"( +-----BEGIN CERTIFICATE----- +-----END CERTIFICATE----- +)"; diff --git a/examples/ArduinoIoTCloud-AWS-Basic/thingProperties.h b/examples/ArduinoIoTCloud-AWS-Basic/thingProperties.h new file mode 100644 index 000000000..6717ea377 --- /dev/null +++ b/examples/ArduinoIoTCloud-AWS-Basic/thingProperties.h @@ -0,0 +1,21 @@ +// Code generated by Arduino IoT Cloud, DO NOT EDIT. + +#include +#include + +const char SSID[] = SECRET_SSID; // Network SSID (name) +const char PASS[] = SECRET_OPTIONAL_PASS; // Network password (use for WPA, or use as key for WEP) + +void onLedChange(); + +bool led; +int potentiometer; +int seconds; + +void initProperties() { + ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); + ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); + ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1); +} + +WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_SSID, SECRET_OPTIONAL_PASS); diff --git a/examples/ArduinoIoTCloud-Advanced/arduino_secrets.h b/examples/ArduinoIoTCloud-Advanced/arduino_secrets.h index 0be56888a..4134e9377 100644 --- a/examples/ArduinoIoTCloud-Advanced/arduino_secrets.h +++ b/examples/ArduinoIoTCloud-Advanced/arduino_secrets.h @@ -9,7 +9,7 @@ #endif /* ESP8266 ESP32 */ -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define SECRET_DEVICE_KEY "my-device-password" #endif diff --git a/examples/ArduinoIoTCloud-Advanced/thingProperties.h b/examples/ArduinoIoTCloud-Advanced/thingProperties.h index 640405507..482b2991e 100644 --- a/examples/ArduinoIoTCloud-Advanced/thingProperties.h +++ b/examples/ArduinoIoTCloud-Advanced/thingProperties.h @@ -6,7 +6,7 @@ #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" #endif -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #endif @@ -22,14 +22,15 @@ CloudLocation location; CloudColor color; void initProperties() { -#if defined(BOARD_HAS_SECRET_KEY) - ArduinoCloud.setBoardId(BOARD_ID); - ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); -#endif #if defined(HAS_TCP) ArduinoCloud.addProperty(switchButton, Permission::Write).onUpdate(onSwitchButtonChange); ArduinoCloud.addProperty(location, Permission::Read).publishOnChange(0.0f); ArduinoCloud.addProperty(color, Permission::ReadWrite).onUpdate(onColorChange); + +#if !defined(BOARD_HAS_SECURE_ELEMENT) + ArduinoCloud.setBoardId(BOARD_ID); + ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); +#endif #elif defined(HAS_LORA) ArduinoCloud.addProperty(switchButton, 1, Permission::Write).onUpdate(onSwitchButtonChange); ArduinoCloud.addProperty(location, 2, Permission::Read).publishOnChange(0.0f); diff --git a/examples/ArduinoIoTCloud-Basic/arduino_secrets.h b/examples/ArduinoIoTCloud-Basic/arduino_secrets.h index 0be56888a..4134e9377 100644 --- a/examples/ArduinoIoTCloud-Basic/arduino_secrets.h +++ b/examples/ArduinoIoTCloud-Basic/arduino_secrets.h @@ -9,7 +9,7 @@ #endif /* ESP8266 ESP32 */ -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define SECRET_DEVICE_KEY "my-device-password" #endif diff --git a/examples/ArduinoIoTCloud-Basic/thingProperties.h b/examples/ArduinoIoTCloud-Basic/thingProperties.h index a354b38f7..8ad84c419 100644 --- a/examples/ArduinoIoTCloud-Basic/thingProperties.h +++ b/examples/ArduinoIoTCloud-Basic/thingProperties.h @@ -6,7 +6,7 @@ #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" #endif -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #endif @@ -21,14 +21,15 @@ int potentiometer; int seconds; void initProperties() { -#if defined(BOARD_HAS_SECRET_KEY) - ArduinoCloud.setBoardId(BOARD_ID); - ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); -#endif #if defined(HAS_TCP) ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1); + +#if !defined(BOARD_HAS_SECURE_ELEMENT) + ArduinoCloud.setBoardId(BOARD_ID); + ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); +#endif #elif defined(HAS_LORA) ArduinoCloud.addProperty(led, 1, Permission::ReadWrite).onUpdate(onLedChange); ArduinoCloud.addProperty(potentiometer, 2, Permission::Read).publishOnChange(10); diff --git a/examples/ArduinoIoTCloud-BlockForOTA/arduino_secrets.h b/examples/ArduinoIoTCloud-BlockForOTA/arduino_secrets.h index 0be56888a..4134e9377 100644 --- a/examples/ArduinoIoTCloud-BlockForOTA/arduino_secrets.h +++ b/examples/ArduinoIoTCloud-BlockForOTA/arduino_secrets.h @@ -9,7 +9,7 @@ #endif /* ESP8266 ESP32 */ -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define SECRET_DEVICE_KEY "my-device-password" #endif diff --git a/examples/ArduinoIoTCloud-BlockForOTA/thingProperties.h b/examples/ArduinoIoTCloud-BlockForOTA/thingProperties.h index a354b38f7..8ad84c419 100644 --- a/examples/ArduinoIoTCloud-BlockForOTA/thingProperties.h +++ b/examples/ArduinoIoTCloud-BlockForOTA/thingProperties.h @@ -6,7 +6,7 @@ #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" #endif -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #endif @@ -21,14 +21,15 @@ int potentiometer; int seconds; void initProperties() { -#if defined(BOARD_HAS_SECRET_KEY) - ArduinoCloud.setBoardId(BOARD_ID); - ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); -#endif #if defined(HAS_TCP) ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1); + +#if !defined(BOARD_HAS_SECURE_ELEMENT) + ArduinoCloud.setBoardId(BOARD_ID); + ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); +#endif #elif defined(HAS_LORA) ArduinoCloud.addProperty(led, 1, Permission::ReadWrite).onUpdate(onLedChange); ArduinoCloud.addProperty(potentiometer, 2, Permission::Read).publishOnChange(10); diff --git a/examples/ArduinoIoTCloud-Callbacks/arduino_secrets.h b/examples/ArduinoIoTCloud-Callbacks/arduino_secrets.h index 0be56888a..4134e9377 100644 --- a/examples/ArduinoIoTCloud-Callbacks/arduino_secrets.h +++ b/examples/ArduinoIoTCloud-Callbacks/arduino_secrets.h @@ -9,7 +9,7 @@ #endif /* ESP8266 ESP32 */ -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define SECRET_DEVICE_KEY "my-device-password" #endif diff --git a/examples/ArduinoIoTCloud-Callbacks/thingProperties.h b/examples/ArduinoIoTCloud-Callbacks/thingProperties.h index 69f0ea0b3..e5e89e2ee 100644 --- a/examples/ArduinoIoTCloud-Callbacks/thingProperties.h +++ b/examples/ArduinoIoTCloud-Callbacks/thingProperties.h @@ -6,7 +6,7 @@ #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" #endif -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #endif @@ -15,11 +15,12 @@ #endif void initProperties() { -#if defined(BOARD_HAS_SECRET_KEY) +#if defined(HAS_TCP) +#if !defined(BOARD_HAS_SECURE_ELEMENT) ArduinoCloud.setBoardId(BOARD_ID); ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); #endif -#if defined(BOARD_HAS_LORA) +#elif defined(HAS_LORA) ArduinoCloud.setThingId(THING_ID); #endif } diff --git a/examples/ArduinoIoTCloud-DeferredOTA/arduino_secrets.h b/examples/ArduinoIoTCloud-DeferredOTA/arduino_secrets.h index e784c4413..d0762b4c8 100644 --- a/examples/ArduinoIoTCloud-DeferredOTA/arduino_secrets.h +++ b/examples/ArduinoIoTCloud-DeferredOTA/arduino_secrets.h @@ -9,7 +9,7 @@ #endif /* ESP8266 ESP32 */ -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define SECRET_DEVICE_KEY "my-device-password" #endif diff --git a/examples/ArduinoIoTCloud-DeferredOTA/thingProperties.h b/examples/ArduinoIoTCloud-DeferredOTA/thingProperties.h index cb89dabac..1feeee877 100644 --- a/examples/ArduinoIoTCloud-DeferredOTA/thingProperties.h +++ b/examples/ArduinoIoTCloud-DeferredOTA/thingProperties.h @@ -6,7 +6,7 @@ #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" #endif -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #endif @@ -15,11 +15,12 @@ void onLedChange(); bool led; void initProperties() { -#if defined(BOARD_HAS_SECRET_KEY) +#if defined(HAS_TCP) +#if !defined(BOARD_HAS_SECURE_ELEMENT) ArduinoCloud.setBoardId(BOARD_ID); ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); #endif - +#endif ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); } diff --git a/examples/ArduinoIoTCloud-NetConfig/ArduinoIoTCloud-NetConfig.ino b/examples/ArduinoIoTCloud-NetConfig/ArduinoIoTCloud-NetConfig.ino new file mode 100644 index 000000000..238b59f55 --- /dev/null +++ b/examples/ArduinoIoTCloud-NetConfig/ArduinoIoTCloud-NetConfig.ino @@ -0,0 +1,62 @@ +/* + This sketch demonstrates how to exchange data between your board and the Arduino IoT Cloud. + + * Connect a potentiometer (or other analog sensor) to A0. + * When the potentiometer (or sensor) value changes the data is sent to the Cloud. + * When you flip the switch in the Cloud dashboard the onboard LED lights gets turned ON or OFF. + + IMPORTANT: + This sketch works with WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud. + On a LoRa board, if it is configured as a class A device (default and preferred option), + values from Cloud dashboard are received only after a value is sent to Cloud. + + The full list of compatible boards can be found here: + - https://github.com/arduino-libraries/ArduinoIoTCloud#what +*/ + +#include "thingProperties.h" + +#if !defined(LED_BUILTIN) && !defined(ARDUINO_NANO_ESP32) +static int const LED_BUILTIN = 2; +#endif + +void setup() { + /* Initialize serial and wait up to 5 seconds for port to open */ + Serial.begin(9600); + for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime <= 5000); ) { } + + /* Set the debug message level: + * - DBG_ERROR: Only show error messages + * - DBG_WARNING: Show warning and error messages + * - DBG_INFO: Show info, warning, and error messages + * - DBG_DEBUG: Show debug, info, warning, and error messages + * - DBG_VERBOSE: Show all messages + */ + setDebugMessageLevel(DBG_INFO); + + /* Configure LED pin as an output */ + pinMode(LED_BUILTIN, OUTPUT); + + /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */ + initProperties(); + + /* Initialize Arduino IoT Cloud library */ + ArduinoCloud.begin(ArduinoIoTPreferredConnection); + + ArduinoCloud.printDebugInfo(); +} + +void loop() { + ArduinoCloud.update(); + potentiometer = analogRead(A0); + seconds = millis() / 1000; +} + +/* + * 'onLedChange' is called when the "led" property of your Thing changes + */ +void onLedChange() { + Serial.print("LED set to "); + Serial.println(led); + digitalWrite(LED_BUILTIN, led); +} diff --git a/examples/ArduinoIoTCloud-NetConfig/thingProperties.h b/examples/ArduinoIoTCloud-NetConfig/thingProperties.h new file mode 100644 index 000000000..4eba7e49c --- /dev/null +++ b/examples/ArduinoIoTCloud-NetConfig/thingProperties.h @@ -0,0 +1,49 @@ +#if !defined(ARDUINO_SAMD_MKRWIFI1010) && !defined(ARDUINO_SAMD_NANO_33_IOT) && !defined(ARDUINO_NANO_RP2040_CONNECT) \ + && !defined(ARDUINO_PORTENTA_H7_M7) && !defined(ARDUINO_NICLA_VISION) && !defined(ARDUINO_OPTA) && !defined(ARDUINO_GIGA) \ + && !defined(ARDUINO_UNOR4_WIFI) && !defined(ARDUINO_PORTENTA_C33) +#error "This example is not compatible with this board." +#endif +#include +#include +#include +#include +#include +#include + +void onLedChange(); + +bool led; +int potentiometer; +int seconds; + +GenericConnectionHandler ArduinoIoTPreferredConnection; +KVStore kvStore; +NetworkConfiguratorClass NetworkConfigurator(ArduinoIoTPreferredConnection); +BLEAgentClass BLEAgent; +SerialAgentClass SerialAgent; + +void initProperties() { + NetworkConfigurator.addAgent(BLEAgent); + NetworkConfigurator.addAgent(SerialAgent); + NetworkConfigurator.setStorage(kvStore); + + /* For changing the default reset pin uncomment and set your preferred pin. + * Use DISABLE_PIN for disabling the reset procedure. + * The pin must be in the list of digital pins usable for interrupts. + * Please refer to the Arduino documentation for more details: + * https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/ + */ + //NetworkConfigurator.setReconfigurePin(your_pin); + + /* Otherwise if you need to monitor the pin status changes + * you can set a custom callback function that is fired on every change + */ + //NetworkConfigurator.setPinChangedCallback(your_callback); + + ArduinoCloud.setConfigurator(NetworkConfigurator); + + ArduinoCloud.addProperty(led, Permission::Write).onUpdate(onLedChange); + ArduinoCloud.addProperty(potentiometer, Permission::Read).publishOnChange(10); + ArduinoCloud.addProperty(seconds, Permission::Read).publishOnChange(1); + +} diff --git a/examples/ArduinoIoTCloud-Schedule/arduino_secrets.h b/examples/ArduinoIoTCloud-Schedule/arduino_secrets.h index 0be56888a..7a9190b5d 100644 --- a/examples/ArduinoIoTCloud-Schedule/arduino_secrets.h +++ b/examples/ArduinoIoTCloud-Schedule/arduino_secrets.h @@ -9,7 +9,7 @@ #endif /* ESP8266 ESP32 */ -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define SECRET_DEVICE_KEY "my-device-password" #endif @@ -23,12 +23,6 @@ #define SECRET_PASS "" #endif -/* MKR WAN 1300/1310 */ -#if defined(BOARD_HAS_LORA) - #define SECRET_APP_EUI "" - #define SECRET_APP_KEY "" -#endif - /* Portenta H7 + Ethernet shield */ #if defined(BOARD_HAS_ETHERNET) #define SECRET_OPTIONAL_IP "" diff --git a/examples/ArduinoIoTCloud-Schedule/thingProperties.h b/examples/ArduinoIoTCloud-Schedule/thingProperties.h index d9958c6d0..290feb8bd 100644 --- a/examples/ArduinoIoTCloud-Schedule/thingProperties.h +++ b/examples/ArduinoIoTCloud-Schedule/thingProperties.h @@ -2,18 +2,14 @@ #include #include "arduino_secrets.h" -#if !(defined(HAS_TCP) || defined(HAS_LORA)) +#if !defined(HAS_TCP) #error "Please check Arduino IoT Cloud supported boards list: https://github.com/arduino-libraries/ArduinoIoTCloud/#what" #endif -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #endif -#if defined(HAS_LORA) - #define THING_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" -#endif - void onSwitchButtonChange(); bool switchButton; @@ -26,10 +22,6 @@ CloudSchedule monthly; CloudSchedule yearly; void initProperties() { -#if defined(BOARD_HAS_SECRET_KEY) - ArduinoCloud.setBoardId(BOARD_ID); - ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); -#endif #if defined(HAS_TCP) ArduinoCloud.addProperty(switchButton, Permission::Write); ArduinoCloud.addProperty(oneShot, Permission::ReadWrite); @@ -39,10 +31,11 @@ void initProperties() { ArduinoCloud.addProperty(weekly, Permission::ReadWrite); ArduinoCloud.addProperty(monthly, Permission::ReadWrite); ArduinoCloud.addProperty(yearly, Permission::ReadWrite); -#elif defined(HAS_LORA) - ArduinoCloud.addProperty(switchButton, 1, Permission::Write); - ArduinoCloud.setThingId(THING_ID); +#if !defined(BOARD_HAS_SECURE_ELEMENT) + ArduinoCloud.setBoardId(BOARD_ID); + ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); +#endif #endif } @@ -50,8 +43,6 @@ void initProperties() { WiFiConnectionHandler ArduinoIoTPreferredConnection(SECRET_WIFI_SSID, SECRET_WIFI_PASS); #elif defined(BOARD_HAS_GSM) GSMConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); -#elif defined(BOARD_HAS_LORA) - LoRaConnectionHandler ArduinoIoTPreferredConnection(SECRET_APP_EUI, SECRET_APP_KEY, _lora_band::EU868, NULL, _lora_class::CLASS_A); #elif defined(BOARD_HAS_NB) NBConnectionHandler ArduinoIoTPreferredConnection(SECRET_PIN, SECRET_APN, SECRET_LOGIN, SECRET_PASS); #elif defined(BOARD_HAS_CATM1_NBIOT) diff --git a/examples/utility/ArduinoIoTCloud_Travis_CI/arduino_secrets.h b/examples/utility/ArduinoIoTCloud_Travis_CI/arduino_secrets.h index a3fd8a7e8..f4bceabb6 100644 --- a/examples/utility/ArduinoIoTCloud_Travis_CI/arduino_secrets.h +++ b/examples/utility/ArduinoIoTCloud_Travis_CI/arduino_secrets.h @@ -9,7 +9,7 @@ #endif /* ESP8266 ESP32*/ -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define SECRET_DEVICE_KEY "my-device-password" #endif diff --git a/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h b/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h index 2574220f4..eb3c29258 100644 --- a/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h +++ b/examples/utility/ArduinoIoTCloud_Travis_CI/thingProperties.h @@ -10,7 +10,7 @@ DEFINES ******************************************************************************/ -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) #define BOARD_ID "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" #endif @@ -86,7 +86,7 @@ void onStringPropertyChange(); ******************************************************************************/ #if defined(HAS_TCP) void initProperties() { -#if defined(BOARD_HAS_SECRET_KEY) +#if !defined(BOARD_HAS_SECURE_ELEMENT) ArduinoCloud.setBoardId(BOARD_ID); ArduinoCloud.setSecretDeviceKey(SECRET_DEVICE_KEY); #endif diff --git a/examples/utility/Provisioning_2.0/CSRHandler.cpp b/examples/utility/Provisioning_2.0/CSRHandler.cpp new file mode 100644 index 000000000..1a6101e4b --- /dev/null +++ b/examples/utility/Provisioning_2.0/CSRHandler.cpp @@ -0,0 +1,428 @@ +/* + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#include "CSRHandler.h" +#include +#include +#include +#include +#include +#include +#include +#include "Arduino_DebugUtils.h" +#include + +#define RESPONSE_TIMEOUT 5000 +#define CONNECTION_RETRY_TIMEOUT_MS 2000 +#define BACKEND_INTERVAL_s 12 +#define MAX_CSR_REQUEST_INTERVAL 180000 +#define MAX_CSR_REQUEST_INTERVAL_ATTEMPTS 15 + +#ifdef COMPILE_TEST +constexpr char *server = "boards-v2.oniudra.cc"; +#else +constexpr char *server = "boards-v2.arduino.cc"; +#endif + +CSRHandlerClass::CSRHandlerClass() : + _ledFeedback{LEDFeedbackClass::getInstance()}, + _state{CSRHandlerStates::END}, + _nextRequestAt{0}, + _requestAttempt{0}, + _startWaitingResponse{0}, + _uhwid{nullptr}, + _certForCSR{nullptr}, + _connectionHandler{nullptr}, + _secureElement{nullptr}, + _tlsClient{nullptr}, + _client{nullptr}, + _fw_version{""}, + _deviceId{""}, + _issueYear{0}, + _issueMonth{0}, + _issueDay{0}, + _issueHour{0} { + memset(_serialNumber, 0, sizeof(_serialNumber)); + memset(_authorityKeyIdentifier, 0, sizeof(_authorityKeyIdentifier)); + memset(_signature, 0, sizeof(_signature)); +} + +CSRHandlerClass::~CSRHandlerClass() { + if (_certForCSR) { + delete _certForCSR; + _certForCSR = nullptr; + } + if (_client) { + delete _client; + _client = nullptr; + } + + if(_tlsClient){ + delete _tlsClient; + _tlsClient = nullptr; + } +} + +bool CSRHandlerClass::begin(ConnectionHandler &connectionHandler, SecureElement &secureElement, String &uhwid) { + if(_state != CSRHandlerStates::END) { + return true; + } + + if(uhwid == "") { + return false; + } + + _connectionHandler = &connectionHandler; + _secureElement = &secureElement; + _uhwid = &uhwid; + +#ifdef BOARD_HAS_WIFI + _fw_version = WiFi.firmwareVersion(); +#endif + if(!_tlsClient){ + _tlsClient = new TLSClientMqtt(); + } + _tlsClient->begin(*_connectionHandler); + _tlsClient->setTimeout(RESPONSE_TIMEOUT); + _client = new HttpClient(*_tlsClient, server, 443); + TimeService.begin(_connectionHandler); + _requestAttempt = 0; + _nextRequestAt = 0; + _startWaitingResponse = 0; + _state = CSRHandlerStates::BUILD_CSR; +} + +void CSRHandlerClass::end() { + if (_client) { + _client->stop(); + delete _client; + _client = nullptr; + } + + if (_certForCSR) { + delete _certForCSR; + _certForCSR = nullptr; + } + + if(_tlsClient){ + delete _tlsClient; + _tlsClient = nullptr; + } + _fw_version = ""; + _deviceId = ""; + _state = CSRHandlerStates::END; +} + +CSRHandlerClass::CSRHandlerStates CSRHandlerClass::poll() { + switch (_state) { + case CSRHandlerStates::BUILD_CSR: _state = handleBuildCSR (); break; + case CSRHandlerStates::REQUEST_SIGNATURE: _state = handleRequestSignature (); break; + case CSRHandlerStates::WAITING_RESPONSE: _state = handleWaitingResponse (); break; + case CSRHandlerStates::PARSE_RESPONSE: _state = handleParseResponse (); break; + case CSRHandlerStates::BUILD_CERTIFICATE: _state = handleBuildCertificate (); break; + case CSRHandlerStates::CERT_CREATED: _state = handleCertCreated (); break; + case CSRHandlerStates::WAITING_COMPLETE_RES: _state = handleWaitingCompleteRes(); break; + case CSRHandlerStates::COMPLETED: break; + case CSRHandlerStates::ERROR: handleError (); break; + case CSRHandlerStates::END: break; + } + + return _state; +} + +void CSRHandlerClass::updateNextRequestAt() { + uint32_t delay; + if(_requestAttempt <= MAX_CSR_REQUEST_INTERVAL_ATTEMPTS) { + delay = BACKEND_INTERVAL_s * _requestAttempt * 1000; // use linear backoff since backend has a rate limit + }else { + delay = MAX_CSR_REQUEST_INTERVAL; + } + + _nextRequestAt = millis() + delay + jitter(); +} + +uint32_t CSRHandlerClass::jitter(uint32_t base, uint32_t max) { + srand(millis()); + return base + rand() % (max - base); +} + +bool CSRHandlerClass::postRequest(const char *url, String &postData) { + if(!_client){ + _client = new HttpClient(*_tlsClient, server, 443); + } + + uint32_t ts = getTimestamp(); + if(ts == 0){ + DEBUG_WARNING("CSRH::%s Failed getting timestamp", __FUNCTION__); + return false; + } + + String token = getAIoTCloudJWT(*_secureElement, *_uhwid, ts, 1); + + _requestAttempt++; + _client->beginRequest(); + + if(_client->post(url) == 0){ + _client->sendHeader("Host", server); + _client->sendHeader("Connection", "close"); + _client->sendHeader("Content-Type", "application/json;charset=UTF-8"); + _client->sendHeader("Authorization", "Bearer " + token); + _client->sendHeader("Content-Length", postData.length()); + _client->beginBody(); + _client->print(postData); + _startWaitingResponse = millis(); + return true; + } + return false; +} + +uint32_t CSRHandlerClass::getTimestamp() { + uint8_t getTsAttempt = 0; + uint32_t ts = 0; + do{ + TimeService.sync(); + ts = TimeService.getTime(); + getTsAttempt++; + }while(ts == 0 && getTsAttempt < 3); + + return ts; +} + +CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleBuildCSR() { + if (!_certForCSR) { + _certForCSR = new ECP256Certificate(); + } + _certForCSR->begin(); + + _certForCSR->setSubjectCommonName(*_uhwid); + + if (!SElementCSR::build(*_secureElement, *_certForCSR, static_cast(SElementArduinoCloudSlot::Key), true)) { + DEBUG_ERROR("CSRH::%s Error generating CSR!", __FUNCTION__); + _ledFeedback.setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR); + return CSRHandlerStates::ERROR; + } + return CSRHandlerStates::REQUEST_SIGNATURE; +} + +CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleRequestSignature() { + CSRHandlerStates nextState = _state; + + if(millis() < _nextRequestAt) { + return nextState; + } + + NetworkConnectionState connectionRes = _connectionHandler->check(); + if (connectionRes != NetworkConnectionState::CONNECTED) { + nextNetworkRetry(); + return nextState; + } + + if(!_certForCSR){ + return CSRHandlerStates::BUILD_CSR; + } + + String csr = _certForCSR->getCSRPEM(); + csr.replace("\n", "\\n"); + + String PostData = "{\"csr\":\""; + PostData += csr; + PostData += "\"}"; + DEBUG_INFO("CSRH Downloading certificate..."); + + if(postRequest("/provisioning/v1/onboarding/provision/csr", PostData)){ + nextState = CSRHandlerStates::WAITING_RESPONSE; + } else { + updateNextRequestAt(); + DEBUG_WARNING("CSRH::%s Failed sending request, retrying in %d ms", __FUNCTION__, _nextRequestAt - millis()); + } + + return nextState; +} + +CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleWaitingResponse() { + CSRHandlerStates nextState = _state; + NetworkConnectionState connectionRes = _connectionHandler->check(); + if (connectionRes != NetworkConnectionState::CONNECTED) { + nextNetworkRetry(); + _client->stop(); + return CSRHandlerStates::REQUEST_SIGNATURE; + } + + if (millis() - _startWaitingResponse > RESPONSE_TIMEOUT) { + _client->stop(); + updateNextRequestAt(); + DEBUG_WARNING("CSRH::%s CSR request timeout, retrying in %d ms", __FUNCTION__, _nextRequestAt - millis()); + nextState = CSRHandlerStates::REQUEST_SIGNATURE; + } + + + int statusCode = _client->responseStatusCode(); + if(statusCode == 200){ + nextState = CSRHandlerStates::PARSE_RESPONSE; + } else { + _client->stop(); + updateNextRequestAt(); + DEBUG_WARNING("CSRH::%s CSR request error code %d, retrying in %d ms", __FUNCTION__, statusCode ,_nextRequestAt - millis()); + nextState = CSRHandlerStates::REQUEST_SIGNATURE; + } + + return nextState; +} + +CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleParseResponse() { + String certResponse = _client->responseBody(); + _client->stop(); + + /* Parse the response in format: + * device_id|authority_key_identifier|not_before|serial|signature_asn1_x|signature_asn1_y + */ + char *response = (char *)certResponse.c_str(); + char *token[6]; + int i = 1; + token[0] = strtok(response, "|"); + for (; i < 6; i++) { + char *tok = strtok(NULL, "|"); + if(tok == NULL){ + break; + } + token[i] = tok; + } + + if(i < 6 || strlen(token[0]) != 36 || strlen(token[1]) != 40 + || strlen(token[2]) < 10 || strlen(token[3]) != 32 + || strlen(token[4]) != 64 || strlen(token[5]) != 64 + || sscanf(token[2], "%4d-%2d-%2dT%2d", &_issueYear, &_issueMonth, &_issueDay, &_issueHour) != 4){ + updateNextRequestAt(); + DEBUG_ERROR("CSRH::%s Error parsing response, retrying in %d ms", __FUNCTION__, _nextRequestAt - millis()); + return CSRHandlerStates::REQUEST_SIGNATURE; + } + + _deviceId = token[0]; + hex::decode(token[1], _authorityKeyIdentifier, sizeof(_authorityKeyIdentifier)); + hex::decode(token[3], _serialNumber, sizeof(_serialNumber)); + hex::decode(token[4], _signature, sizeof(_signature)); + hex::decode(token[5], &_signature[32], sizeof(_signature) - 32); + + return CSRHandlerStates::BUILD_CERTIFICATE; +} + +CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleBuildCertificate() { + int expireYears = 31; + + if (!SElementArduinoCloudDeviceId::write(*_secureElement, _deviceId, SElementArduinoCloudSlot::DeviceId)) { + DEBUG_ERROR("CSRH::%s Error storing device id!", __FUNCTION__); + _ledFeedback.setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR); + return CSRHandlerStates::ERROR; + } + + ECP256Certificate cert; + cert.begin(); + + cert.setSubjectCommonName(_deviceId); + cert.setIssuerCountryName("US"); + cert.setIssuerOrganizationName("Arduino LLC US"); + cert.setIssuerOrganizationalUnitName("IT"); + cert.setIssuerCommonName("Arduino"); + cert.setSignature(_signature, sizeof(_signature)); + cert.setAuthorityKeyId(_authorityKeyIdentifier, sizeof(_authorityKeyIdentifier)); + cert.setSerialNumber(_serialNumber, sizeof(_serialNumber)); + cert.setIssueYear(_issueYear); + cert.setIssueMonth(_issueMonth); + cert.setIssueDay(_issueDay); + cert.setIssueHour(_issueHour); + cert.setExpireYears(expireYears); + + if (!SElementArduinoCloudCertificate::build(*_secureElement, cert, static_cast(SElementArduinoCloudSlot::Key))) { + DEBUG_ERROR("CSRH::%s Error building secureElement compressed cert!", __FUNCTION__); + _ledFeedback.setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR); + return CSRHandlerStates::ERROR; + } + + if (!SElementArduinoCloudCertificate::write(*_secureElement, cert, SElementArduinoCloudSlot::CompressedCertificate)) { + DEBUG_ERROR("CSRH::%s Error storing cert!" , __FUNCTION__); + _ledFeedback.setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR); + return CSRHandlerStates::ERROR; + } + + DEBUG_INFO("CSRH Certificate created!"); + _nextRequestAt = 0; + _requestAttempt = 0; + return CSRHandlerStates::CERT_CREATED; +} + +CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleCertCreated() { + CSRHandlerStates nextState = _state; + + if(millis() < _nextRequestAt) { + return nextState; + } + + NetworkConnectionState connectionRes = _connectionHandler->check(); + if (connectionRes != NetworkConnectionState::CONNECTED) { + nextNetworkRetry(); + return nextState; + } + + String PostData = "{\"wifi_fw_version\":\""; + PostData += _fw_version; + PostData += "\"}"; + if(postRequest("/provisioning/v1/onboarding/provision/complete", PostData)){ + nextState = CSRHandlerStates::WAITING_COMPLETE_RES; + } else { + updateNextRequestAt(); + DEBUG_WARNING("CSRH::%s Error sending complete request, retrying in %d ms", __FUNCTION__, _nextRequestAt - millis()); + } + + return nextState; +} + +CSRHandlerClass::CSRHandlerStates CSRHandlerClass::handleWaitingCompleteRes() { + CSRHandlerStates nextState = _state; + NetworkConnectionState connectionRes = _connectionHandler->check(); + if (connectionRes != NetworkConnectionState::CONNECTED) { + nextNetworkRetry(); + _client->stop(); + return CSRHandlerStates::CERT_CREATED; + } + + if (millis() - _startWaitingResponse > RESPONSE_TIMEOUT) { + _client->stop(); + updateNextRequestAt(); + DEBUG_WARNING("CSRH::%s Complete request timeout, retrying in %d ms", __FUNCTION__, _nextRequestAt - millis()); + nextState = CSRHandlerStates::CERT_CREATED; + } + + int statusCode = _client->responseStatusCode(); + if(statusCode == 200){ + if (_certForCSR) { + delete _certForCSR; + _certForCSR = nullptr; + } + DEBUG_INFO("CSRH Provisioning completed!"); + nextState = CSRHandlerStates::COMPLETED; + } else if (statusCode == 429 || statusCode == 503) { + updateNextRequestAt(); + nextState = CSRHandlerStates::CERT_CREATED; + } else { + DEBUG_WARNING("CSRH::%s Complete request error code %d, retrying in %d ms", __FUNCTION__, statusCode ,_nextRequestAt - millis()); + _requestAttempt = 0; + _nextRequestAt = 0; + nextState = CSRHandlerStates::REQUEST_SIGNATURE; + } + _client->stop(); + + return nextState; +} + +void CSRHandlerClass::nextNetworkRetry() { + _nextRequestAt = millis() + CONNECTION_RETRY_TIMEOUT_MS; +} + +void CSRHandlerClass::handleError() { + _ledFeedback.update(); +} diff --git a/examples/utility/Provisioning_2.0/CSRHandler.h b/examples/utility/Provisioning_2.0/CSRHandler.h new file mode 100644 index 000000000..ae5956b7e --- /dev/null +++ b/examples/utility/Provisioning_2.0/CSRHandler.h @@ -0,0 +1,74 @@ +/* + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#pragma once +#include +#include +#include +#include +#include +#include "utility/LEDFeedback.h" +#define JITTER_BASE 0 +#define JITTER_MAX 1000 + +class CSRHandlerClass { +public: + CSRHandlerClass(); + ~CSRHandlerClass(); + enum class CSRHandlerStates { + BUILD_CSR, + REQUEST_SIGNATURE, + WAITING_RESPONSE, + PARSE_RESPONSE, + BUILD_CERTIFICATE, + CERT_CREATED, + WAITING_COMPLETE_RES, + COMPLETED, + ERROR, + END + }; + bool begin(ConnectionHandler &connectionHandler, SecureElement &secureElement, String &uhwid); + void end(); + CSRHandlerStates poll(); +private: + CSRHandlerStates _state; + unsigned long _nextRequestAt; + uint32_t _requestAttempt; + uint32_t _startWaitingResponse; + String *_uhwid; + String _fw_version; + + int _issueYear; + uint8_t _issueMonth; + uint8_t _issueDay; + uint8_t _issueHour; + byte _serialNumber[16]; + byte _authorityKeyIdentifier[20]; + byte _signature[64]; + String _deviceId; + + ECP256Certificate *_certForCSR; + ConnectionHandler *_connectionHandler; + SecureElement *_secureElement; + TLSClientMqtt *_tlsClient; + HttpClient *_client; + LEDFeedbackClass &_ledFeedback; + void updateNextRequestAt(); + void nextNetworkRetry(); + uint32_t jitter(uint32_t base = JITTER_BASE, uint32_t max = JITTER_MAX); + bool postRequest(const char *url, String &postData); + uint32_t getTimestamp(); + CSRHandlerStates handleBuildCSR(); + CSRHandlerStates handleRequestSignature(); + CSRHandlerStates handleWaitingResponse(); + CSRHandlerStates handleParseResponse(); + CSRHandlerStates handleBuildCertificate(); + CSRHandlerStates handleCertCreated(); + CSRHandlerStates handleWaitingCompleteRes(); + void handleError(); +}; diff --git a/examples/utility/Provisioning_2.0/ClaimingHandler.cpp b/examples/utility/Provisioning_2.0/ClaimingHandler.cpp new file mode 100644 index 000000000..e2567e084 --- /dev/null +++ b/examples/utility/Provisioning_2.0/ClaimingHandler.cpp @@ -0,0 +1,235 @@ +/* + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#include "ClaimingHandler.h" +#include +#include "Arduino_DebugUtils.h" +#include +#include "utility/HCI.h" +#include +#include "ANetworkConfigurator_Config.h" + +#define SLOT_BOARD_PRIVATE_KEY 1 + +extern const char *SKETCH_VERSION; + +ClaimingHandlerClass::ClaimingHandlerClass(): + _uhwid {nullptr}, + _state {ClaimingHandlerStates::END}, + _secureElement {nullptr}, + _clearStoredCredentials {nullptr}, + _agentManager { AgentsManagerClass::getInstance()}, + _ledFeedback {LEDFeedbackClass::getInstance()} { + _receivedEvent = ClaimingReqEvents::NONE; + _ts = 0; +} + +bool ClaimingHandlerClass::begin(SecureElement &secureElement, String &uhwid, ClearStoredCredentialsHandler clearStoredCredentials) { + if(_state != ClaimingHandlerStates::END) { + return true; + } + + if(uhwid == "" || clearStoredCredentials == nullptr) { + return false; + } + + if (!_agentManager.addRequestHandler(RequestType::GET_ID, getIdRequestCb)) { + return false; + } + + if (!_agentManager.addRequestHandler(RequestType::RESET, resetStoredCredRequestCb)) { + return false; + } + + if(!_agentManager.addRequestHandler(RequestType::GET_BLE_MAC_ADDRESS, getBLEMacAddressRequestCb)) { + return false; + } + + if(!_agentManager.addRequestHandler(RequestType::GET_PROVISIONING_SKETCH_VERSION, getProvSketchVersionRequestCb)) { + return false; + } + + if (!_agentManager.addReturnTimestampCallback(setTimestamp)) { + return false; + } + + _agentManager.begin(); + _uhwid = &uhwid; + _secureElement = &secureElement; + _clearStoredCredentials = clearStoredCredentials; + _state = ClaimingHandlerStates::INIT; +} + +void ClaimingHandlerClass::end() { + if(_state == ClaimingHandlerStates::END) { + return; + } + + _agentManager.removeReturnTimestampCallback(); + _agentManager.removeRequestHandler(RequestType::GET_ID); + _agentManager.removeRequestHandler(RequestType::RESET); + _agentManager.end(); + _state = ClaimingHandlerStates::END; +} + +void ClaimingHandlerClass::poll() { + if(_state == ClaimingHandlerStates::END) { + return; + } + _ledFeedback.update(); + _agentManager.update(); + + switch (_receivedEvent) { + case ClaimingReqEvents::GET_ID: getIdReqHandler (); break; + case ClaimingReqEvents::RESET: resetStoredCredReqHandler (); break; + case ClaimingReqEvents::GET_BLE_MAC_ADDRESS: getBLEMacAddressReqHandler (); break; + case ClaimingReqEvents::GET_PROV_SKETCH_VERSION: getProvSketchVersionReqHandler(); break; + } + _receivedEvent = ClaimingReqEvents::NONE; + return; +} + +void ClaimingHandlerClass::getIdReqHandler() { + if (_ts == 0) { + DEBUG_ERROR("CH::%s Error: timestamp not provided" , __FUNCTION__); + sendStatus(StatusMessage::PARAMS_NOT_FOUND); + return; + } + + byte _uhwidBytes[32]; + hex::decode(_uhwid->c_str(), _uhwidBytes, _uhwid->length()); + + String token = generateToken(); + if (token == "") { + DEBUG_ERROR("CH::%s Error: token not created", __FUNCTION__); + sendStatus(StatusMessage::ERROR); + return; + } + + SElementJWS sejws; + String publicKey = sejws.publicKey(*_secureElement, SLOT_BOARD_PRIVATE_KEY, false); + if (publicKey == "") { + DEBUG_ERROR("CH::%s Error: public key not created", __FUNCTION__); + sendStatus(StatusMessage::ERROR); + return; + } + + //Send public key + ProvisioningOutputMessage publicKeyMsg = {MessageOutputType::PROV_PUBLIC_KEY}; + publicKeyMsg.m.provPublicKey = publicKey.c_str(); + _agentManager.sendMsg(publicKeyMsg); + + + //Send UHWID + ProvisioningOutputMessage idMsg = {MessageOutputType::UHWID}; + idMsg.m.uhwid = _uhwidBytes; + _agentManager.sendMsg(idMsg); + + //Send JWT + ProvisioningOutputMessage jwtMsg = {MessageOutputType::JWT}; + jwtMsg.m.jwt = token.c_str(); + _agentManager.sendMsg(jwtMsg); + _ts = 0; + +} + +void ClaimingHandlerClass::resetStoredCredReqHandler() { + if( !_clearStoredCredentials()){ + DEBUG_ERROR("CH::%s Error: reset stored credentials failed", __FUNCTION__); + sendStatus(StatusMessage::ERROR); + } else { + sendStatus(StatusMessage::RESET_COMPLETED); + } + +} + +void ClaimingHandlerClass::getBLEMacAddressReqHandler() { + /* Set the default MAC address as ff:ff:ff:ff:ff:ff for compatibility + * with the Arduino IoT Cloud WebUI + */ + uint8_t mac[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; + +#ifdef ARDUINO_OPTA + if(_getPid_() == OPTA_WIFI_PID) { +#endif + bool activated = false; + ConfiguratorAgent * connectedAgent = _agentManager.getConnectedAgent(); + if(!_agentManager.isAgentEnabled(ConfiguratorAgent::AgentTypes::BLE) || (connectedAgent != nullptr && + connectedAgent->getAgentType() != ConfiguratorAgent::AgentTypes::BLE)) { + activated = true; + BLE.begin(); + } + + HCI.readBdAddr(mac); + + for(int i = 0; i < 3; i++){ + uint8_t byte = mac[i]; + mac[i] = mac[5-i]; + mac[5-i] = byte; + } + if (activated) { + BLE.end(); + } +#ifdef ARDUINO_OPTA + } +#endif + ProvisioningOutputMessage outputMsg; + outputMsg.type = MessageOutputType::BLE_MAC_ADDRESS; + outputMsg.m.BLEMacAddress = mac; + _agentManager.sendMsg(outputMsg); +} + +void ClaimingHandlerClass::getProvSketchVersionReqHandler() { + ProvisioningOutputMessage outputMsg; + outputMsg.type = MessageOutputType::PROV_SKETCH_VERSION; + outputMsg.m.provSketchVersion = SKETCH_VERSION; + _agentManager.sendMsg(outputMsg); +} + +void ClaimingHandlerClass::getIdRequestCb() { + DEBUG_VERBOSE("CH Get ID request received"); + _receivedEvent = ClaimingReqEvents::GET_ID; +} +void ClaimingHandlerClass::setTimestamp(uint64_t ts) { + _ts = ts; +} + +void ClaimingHandlerClass::resetStoredCredRequestCb() { + DEBUG_VERBOSE("CH Reset stored credentials request received"); + _receivedEvent = ClaimingReqEvents::RESET; +} + +void ClaimingHandlerClass::getBLEMacAddressRequestCb() { + DEBUG_VERBOSE("CH Get BLE MAC address request received"); + _receivedEvent = ClaimingReqEvents::GET_BLE_MAC_ADDRESS; +} + +void ClaimingHandlerClass::getProvSketchVersionRequestCb() { + DEBUG_VERBOSE("CH Get provisioning sketch version request received"); + _receivedEvent = ClaimingReqEvents::GET_PROV_SKETCH_VERSION; +} + +String ClaimingHandlerClass::generateToken() { + String token = getAIoTCloudJWT(*_secureElement, *_uhwid, _ts, SLOT_BOARD_PRIVATE_KEY); + if(token == "") { + byte publicKey[64]; + DEBUG_INFO("Generating private key"); + if(!_secureElement->generatePrivateKey(SLOT_BOARD_PRIVATE_KEY, publicKey)){ + DEBUG_ERROR("CH::%s Error: private key generation failed", __FUNCTION__); + return ""; + } + token = getAIoTCloudJWT(*_secureElement, *_uhwid, _ts, SLOT_BOARD_PRIVATE_KEY); + } + + return token; +} + +bool ClaimingHandlerClass::sendStatus(StatusMessage msg) { + ProvisioningOutputMessage statusMsg = {MessageOutputType::STATUS, {msg}}; + return _agentManager.sendMsg(statusMsg); +} diff --git a/examples/utility/Provisioning_2.0/ClaimingHandler.h b/examples/utility/Provisioning_2.0/ClaimingHandler.h new file mode 100644 index 000000000..7b8693b36 --- /dev/null +++ b/examples/utility/Provisioning_2.0/ClaimingHandler.h @@ -0,0 +1,54 @@ +/* + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#pragma once +#include "Arduino.h" +#include "configuratorAgents/AgentsManager.h" +#include +#include "utility/LEDFeedback.h" + +typedef bool (*ClearStoredCredentialsHandler)(); +class ClaimingHandlerClass { +public: + ClaimingHandlerClass(); + bool begin(SecureElement &secureElement, String &uhwid, ClearStoredCredentialsHandler clearStoredCredentials); + void end(); + void poll(); +private: + String *_uhwid; + enum class ClaimingHandlerStates { + INIT, + END + }; + enum class ClaimingReqEvents { NONE, + GET_ID, + RESET, + GET_BLE_MAC_ADDRESS, + GET_PROV_SKETCH_VERSION}; + static inline ClaimingReqEvents _receivedEvent; + ClaimingHandlerStates _state; + AgentsManagerClass &_agentManager; + LEDFeedbackClass &_ledFeedback; + static inline uint64_t _ts; + SecureElement *_secureElement; + String generateToken(); + + bool sendStatus(StatusMessage msg); + /* Commands handlers */ + void getIdReqHandler(); + void resetStoredCredReqHandler(); + void getBLEMacAddressReqHandler(); + void getProvSketchVersionReqHandler(); + ClearStoredCredentialsHandler _clearStoredCredentials; + /* Callbacks for receiving commands */ + static void getIdRequestCb(); + static void setTimestamp(uint64_t ts); + static void resetStoredCredRequestCb(); + static void getBLEMacAddressRequestCb(); + static void getProvSketchVersionRequestCb(); +}; diff --git a/examples/utility/Provisioning_2.0/Provisioning_2.0.ino b/examples/utility/Provisioning_2.0/Provisioning_2.0.ino new file mode 100644 index 000000000..7f8f44ceb --- /dev/null +++ b/examples/utility/Provisioning_2.0/Provisioning_2.0.ino @@ -0,0 +1,228 @@ +/* + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ +#include "thingProperties.h" +#include "CSRHandler.h" +#include "ClaimingHandler.h" +#include "SecretsHelper.h" +#include +#include +#include +#include +#include "utility/LEDFeedback.h" + +const char *SKETCH_VERSION = "0.3.3"; + +enum class DeviceState { + HARDWARE_CHECK, + BEGIN, + NETWORK_CONFIG, + CSR, + BEGIN_CLOUD, + RUN, + ERROR + }; + +DeviceState _state = DeviceState::HARDWARE_CHECK; +SecureElement secureElement; + +String uhwid = ""; +bool resetEvent = false; + +CSRHandlerClass CSRHandler; +ClaimingHandlerClass ClaimingHandler; + +bool clearStoredCredentials() { + const uint8_t empty[4] = {0x00,0x00,0x00,0x00}; + if(!NetworkConfigurator.resetStoredConfiguration() || \ + !secureElement.writeSlot(static_cast(SElementArduinoCloudSlot::DeviceId), (byte*)empty, sizeof(empty)) || \ + !secureElement.writeSlot(static_cast(SElementArduinoCloudSlot::CompressedCertificate), (byte*)empty, sizeof(empty))) { + return false; + } + + ArduinoCloud.disconnect(); + resetEvent = true; + return true; + } + +void setup() { + Serial.begin(9600); + + delay(1500); + + setDebugMessageLevel(4); + + initProperties(); + AgentsManagerClass::getInstance().begin(); + LEDFeedbackClass::getInstance().begin(); + DEBUG_INFO("Starting Provisioning version %s", SKETCH_VERSION); +} + +void sendStatus(StatusMessage msg) { + ProvisioningOutputMessage outMsg = { MessageOutputType::STATUS, { msg } }; + AgentsManagerClass::getInstance().sendMsg(outMsg); +} + +DeviceState handleHardwareCheck() { + // Init the secure element + if(!secureElement.begin()) { + DEBUG_ERROR("Sketch: Error during secureElement begin!"); + LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR); + sendStatus(StatusMessage::HW_ERROR_SE_BEGIN); + return DeviceState::ERROR; + } + + if (!secureElement.locked()) { + if (!secureElement.writeConfiguration()) { + DEBUG_ERROR("Sketch: Writing secureElement configuration failed!"); + LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR); + sendStatus(StatusMessage::HW_ERROR_SE_CONFIG); + return DeviceState::ERROR; + } + + if (!secureElement.lock()) { + DEBUG_ERROR("Sketch: Locking secureElement configuration failed!"); + LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR); + sendStatus(StatusMessage::HW_ERROR_SE_LOCK); + return DeviceState::ERROR; + } + DEBUG_INFO("secureElement locked successfully"); + } + + FlashFormatter flashFormatter; + // Check if the board storage is properly formatted + if(!flashFormatter.checkAndFormatPartition()) { + DEBUG_ERROR("Sketch: Error partitioning storage"); + LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR); + sendStatus(StatusMessage::FAIL_TO_PARTITION_STORAGE); + return DeviceState::ERROR; + } + + return DeviceState::BEGIN; +} + +DeviceState handleBegin() { + uhwid = GetUHWID(); + if(uhwid == ""){ + DEBUG_ERROR("Sketch: Error getting UHWID"); + LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::ERROR); + sendStatus(StatusMessage::ERROR_GENERATING_UHWID); + return DeviceState::ERROR; + } + // Scan the network options + NetworkConfigurator.scanNetworkOptions(); + NetworkConfigurator.begin(); + ClaimingHandler.begin(secureElement, uhwid, clearStoredCredentials); + DEBUG_INFO("BLE Available"); + return DeviceState::NETWORK_CONFIG; +} + +DeviceState handleNetworkConfig() { + ClaimingHandler.poll(); + if(resetEvent){ + resetEvent = false; + } + NetworkConfiguratorStates s = NetworkConfigurator.update(); + + DeviceState nextState = _state; + if (s == NetworkConfiguratorStates::CONFIGURED) { + String deviceId = ""; + SElementArduinoCloudDeviceId::read(secureElement, deviceId, SElementArduinoCloudSlot::DeviceId); + + if (deviceId == "") { + CSRHandler.begin(ArduinoIoTPreferredConnection, secureElement, uhwid); + nextState = DeviceState::CSR; + } else { + nextState = DeviceState::BEGIN_CLOUD; + } + } + return nextState; +} + +DeviceState handleCSR() { + NetworkConfigurator.update(); + ClaimingHandler.poll(); + if(resetEvent) { + resetEvent = false; + CSRHandler.end(); + return DeviceState::NETWORK_CONFIG; + } + + DeviceState nextState = _state; + + CSRHandlerClass::CSRHandlerStates res = CSRHandler.poll(); + if (res == CSRHandlerClass::CSRHandlerStates::COMPLETED) { + CSRHandler.end(); + nextState = DeviceState::BEGIN_CLOUD; + } + + return nextState; +} + +DeviceState handleBeginCloud() { + // Close the connection to the peer (App mobile, FE, etc) + NetworkConfigurator.disconnectAgent(); + // Close the BLE connectivity + if (NetworkConfigurator.isAgentEnabled(ConfiguratorAgent::AgentTypes::BLE)) { + NetworkConfigurator.enableAgent(ConfiguratorAgent::AgentTypes::BLE, false); + } + // Connect to Arduino IoT Cloud +#ifdef COMPILE_TEST + ArduinoCloud.begin(ArduinoIoTPreferredConnection, false, "mqtts-sa.iot.oniudra.cc"); +#else + ArduinoCloud.begin(ArduinoIoTPreferredConnection); +#endif + ArduinoCloud.printDebugInfo(); + + return DeviceState::RUN; +} + +void cloudConnectedHandler(bool connected) { + static bool _status = false; + if(connected != _status){ + _status = connected; + if(connected){ + LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::CONNECTED_TO_CLOUD); + } else { + LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::NONE); + } + } +} + +DeviceState handleRun() { + ClaimingHandler.poll(); + if(resetEvent) { + resetEvent = false; + return DeviceState::NETWORK_CONFIG; + } + + DeviceState nextState = _state; + ArduinoCloud.update(); + + cloudConnectedHandler(ArduinoCloud.connected()); + + return nextState; +} + +DeviceState handleError() { + LEDFeedbackClass::getInstance().update(); + AgentsManagerClass::getInstance().update(); + return DeviceState::ERROR; +} + +void loop() { + switch (_state) { + case DeviceState::HARDWARE_CHECK: _state = handleHardwareCheck(); break; + case DeviceState::BEGIN: _state = handleBegin (); break; + case DeviceState::NETWORK_CONFIG : _state = handleNetworkConfig(); break; + case DeviceState::CSR: _state = handleCSR (); break; + case DeviceState::BEGIN_CLOUD: _state = handleBeginCloud (); break; + case DeviceState::RUN: _state = handleRun (); break; + case DeviceState::ERROR: _state = handleError (); break; + default: break; + } +} diff --git a/examples/utility/Provisioning_2.0/SecretsHelper.h b/examples/utility/Provisioning_2.0/SecretsHelper.h new file mode 100644 index 000000000..bdb6354d1 --- /dev/null +++ b/examples/utility/Provisioning_2.0/SecretsHelper.h @@ -0,0 +1,20 @@ +/* + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#pragma once + +#include +#include + +inline String GetUHWID() { + UniqueHWId Id; + if (Id.begin()) { + return Id.get(); + } + return ""; +} diff --git a/examples/utility/Provisioning_2.0/thingProperties.h b/examples/utility/Provisioning_2.0/thingProperties.h new file mode 100644 index 000000000..7f51fa6ca --- /dev/null +++ b/examples/utility/Provisioning_2.0/thingProperties.h @@ -0,0 +1,32 @@ +/* + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ +#if defined(ARDUINO_SAMD_MKRGSM1400) || defined(ARDUINO_SAMD_MKRNB1500) || defined(ARDUINO_SAMD_MKRWAN1300) || defined(ARDUINO_SAMD_MKRWAN1310) +#error "Board not supported for Provisioning 2.0" +#endif + +#include +#include +#include +#include "configuratorAgents/agents/BLEAgent.h" +#include "configuratorAgents/agents/SerialAgent.h" + +GenericConnectionHandler ArduinoIoTPreferredConnection; +KVStore kvStore; +BLEAgentClass BLEAgent; +SerialAgentClass SerialAgent; +NetworkConfiguratorClass NetworkConfigurator(ArduinoIoTPreferredConnection); + +void initProperties() { + + NetworkConfigurator.addAgent(BLEAgent); + NetworkConfigurator.addAgent(SerialAgent); + NetworkConfigurator.setStorage(kvStore); + ArduinoCloud.setConfigurator(NetworkConfigurator); +} + + diff --git a/extras/test/CMakeLists.txt b/extras/test/CMakeLists.txt index 527fd7558..f9c13c8c0 100644 --- a/extras/test/CMakeLists.txt +++ b/extras/test/CMakeLists.txt @@ -1,20 +1,79 @@ ########################################################################## -cmake_minimum_required(VERSION 2.8) +cmake_minimum_required(VERSION 3.5) ########################################################################## project(testArduinoIoTCloud) +Include(FetchContent) + +FetchContent_Declare( + Catch2 + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG v3.4.0 +) + +FetchContent_Declare( + cloudutils + GIT_REPOSITORY https://github.com/arduino-libraries/Arduino_CloudUtils.git + GIT_TAG main +) + +FetchContent_Declare( + connectionhandler + GIT_REPOSITORY https://github.com/arduino-libraries/Arduino_ConnectionHandler.git + GIT_TAG master +) + +FetchContent_MakeAvailable(Catch2) + +FetchContent_MakeAvailable(cloudutils) + +FetchContent_MakeAvailable(connectionhandler) ########################################################################## include_directories(include) include_directories(../../src) +include_directories(../../src/message) include_directories(../../src/cbor) include_directories(../../src/property) include_directories(../../src/utility/time) -include_directories(external/catch/v2.13.10/include) -include_directories(external/fakeit/v2.0.5/include) + +# add_library(cloudutils STATIC IMPORTED GLOBAL) +add_library(cloudutils INTERFACE) + +target_include_directories( + cloudutils INTERFACE + ${cloudutils_SOURCE_DIR}/src/ +) + +target_include_directories( + cloudutils INTERFACE + ${cloudutils_SOURCE_DIR}/src/cbor +) + +target_include_directories( + cloudutils INTERFACE + ${cloudutils_SOURCE_DIR}/src/cbor/utils +) + +target_include_directories( + cloudutils INTERFACE + ${cloudutils_SOURCE_DIR}/src/interfaces +) + +add_library(connectionhandler INTERFACE) + +target_include_directories( + connectionhandler INTERFACE + ${connectionhandler_SOURCE_DIR}/src/ +) + +target_include_directories( + connectionhandler INTERFACE + ${connectionhandler_SOURCE_DIR}/src/connectionHandlerModels +) ########################################################################## @@ -32,6 +91,8 @@ set(TEST_SRCS src/test_addPropertyReal.cpp src/test_callback.cpp src/test_CloudColor.cpp + src/test_CloudFloat.cpp + src/test_CloudWrapperFloat.cpp src/test_CloudLocation.cpp src/test_CloudSchedule.cpp src/test_decode.cpp @@ -45,7 +106,6 @@ set(TEST_SRCS src/test_writeOnly.cpp src/test_writeOnDemand.cpp src/test_writeOnChange.cpp - src/test_TimedAttempt.cpp ) set(TEST_UTIL_SRCS @@ -54,26 +114,26 @@ set(TEST_UTIL_SRCS ) set(TEST_DUT_SRCS - ../../src/utility/time/TimedAttempt.cpp ../../src/property/Property.cpp ../../src/property/PropertyContainer.cpp ../../src/cbor/CBORDecoder.cpp ../../src/cbor/CBOREncoder.cpp - ../../src/cbor/MessageDecoder.cpp - ../../src/cbor/MessageEncoder.cpp - ../../src/cbor/CBOR.cpp - ../../src/cbor/lib/tinycbor/src/cborencoder.c - ../../src/cbor/lib/tinycbor/src/cborencoder_close_container_checked.c - ../../src/cbor/lib/tinycbor/src/cborerrorstrings.c - ../../src/cbor/lib/tinycbor/src/cborparser.c - ../../src/cbor/lib/tinycbor/src/cborparser_dup_string.c - ../../src/cbor/lib/tinycbor/src/cborpretty.c - ../../src/cbor/lib/tinycbor/src/cborpretty_stdio.c - ../../src/cbor/lib/tinycbor/src/cbortojson.c - ../../src/cbor/lib/tinycbor/src/cborvalidation.c - ../../src/cbor/lib/tinycbor/src/open_memstream.c + ../../src/cbor/IoTCloudMessageDecoder.cpp + ../../src/cbor/IoTCloudMessageEncoder.cpp + + ${cloudutils_SOURCE_DIR}/src/cbor/tinycbor/src/cborencoder.c + ${cloudutils_SOURCE_DIR}/src/cbor/tinycbor/src/cborencoder_close_container_checked.c + ${cloudutils_SOURCE_DIR}/src/cbor/tinycbor/src/cborerrorstrings.c + ${cloudutils_SOURCE_DIR}/src/cbor/tinycbor/src/cborparser.c + ${cloudutils_SOURCE_DIR}/src/cbor/tinycbor/src/cborparser_dup_string.c + ${cloudutils_SOURCE_DIR}/src/cbor/tinycbor/src/cborpretty.c + ${cloudutils_SOURCE_DIR}/src/cbor/tinycbor/src/cborpretty_stdio.c + ${cloudutils_SOURCE_DIR}/src/cbor/tinycbor/src/cbortojson.c + ${cloudutils_SOURCE_DIR}/src/cbor/tinycbor/src/cborvalidation.c + ${cloudutils_SOURCE_DIR}/src/cbor/tinycbor/src/open_memstream.c + ${cloudutils_SOURCE_DIR}/src/cbor/MessageDecoder.cpp + ${cloudutils_SOURCE_DIR}/src/cbor/MessageEncoder.cpp ) - ########################################################################## set(TEST_TARGET_SRCS @@ -86,12 +146,13 @@ set(TEST_TARGET_SRCS ########################################################################## +add_compile_definitions(BOARD_HAS_LORA BOARD_HAS_CATM1_NBIOT BOARD_HAS_WIFI BOARD_HAS_ETHERNET BOARD_HAS_CELLULAR BOARD_HAS_NB BOARD_HAS_GSM) add_compile_definitions(HOST HAS_TCP) add_compile_options(-Wall -Wextra -Wpedantic -Werror) add_compile_options(-Wno-cast-function-type) set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} "--coverage") -set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "--coverage -Wno-deprecated-copy") +set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "--coverage -Wno-deprecated-copy -Wno-missing-field-initializers") ########################################################################## @@ -100,5 +161,9 @@ add_executable( ${TEST_TARGET_SRCS} ) +target_link_libraries( ${TEST_TARGET} connectionhandler) +target_link_libraries( ${TEST_TARGET} cloudutils) +target_link_libraries( ${TEST_TARGET} Catch2WithMain ) + ########################################################################## diff --git a/extras/test/external/catch/v2.13.10/include/catch.hpp b/extras/test/external/catch/v2.13.10/include/catch.hpp deleted file mode 100644 index 9b309bddc..000000000 --- a/extras/test/external/catch/v2.13.10/include/catch.hpp +++ /dev/null @@ -1,17976 +0,0 @@ -/* - * Catch v2.13.10 - * Generated: 2022-10-16 11:01:23.452308 - * ---------------------------------------------------------- - * This file has been merged from multiple headers. Please don't edit it directly - * Copyright (c) 2022 Two Blue Cubes Ltd. All rights reserved. - * - * Distributed under the Boost Software License, Version 1.0. (See accompanying - * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - */ -#ifndef TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED -#define TWOBLUECUBES_SINGLE_INCLUDE_CATCH_HPP_INCLUDED -// start catch.hpp - - -#define CATCH_VERSION_MAJOR 2 -#define CATCH_VERSION_MINOR 13 -#define CATCH_VERSION_PATCH 10 - -#ifdef __clang__ -# pragma clang system_header -#elif defined __GNUC__ -# pragma GCC system_header -#endif - -// start catch_suppress_warnings.h - -#ifdef __clang__ -# ifdef __ICC // icpc defines the __clang__ macro -# pragma warning(push) -# pragma warning(disable: 161 1682) -# else // __ICC -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wpadded" -# pragma clang diagnostic ignored "-Wswitch-enum" -# pragma clang diagnostic ignored "-Wcovered-switch-default" -# endif -#elif defined __GNUC__ - // Because REQUIREs trigger GCC's -Wparentheses, and because still - // supported version of g++ have only buggy support for _Pragmas, - // Wparentheses have to be suppressed globally. -# pragma GCC diagnostic ignored "-Wparentheses" // See #674 for details - -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-variable" -# pragma GCC diagnostic ignored "-Wpadded" -#endif -// end catch_suppress_warnings.h -#if defined(CATCH_CONFIG_MAIN) || defined(CATCH_CONFIG_RUNNER) -# define CATCH_IMPL -# define CATCH_CONFIG_ALL_PARTS -#endif - -// In the impl file, we want to have access to all parts of the headers -// Can also be used to sanely support PCHs -#if defined(CATCH_CONFIG_ALL_PARTS) -# define CATCH_CONFIG_EXTERNAL_INTERFACES -# if defined(CATCH_CONFIG_DISABLE_MATCHERS) -# undef CATCH_CONFIG_DISABLE_MATCHERS -# endif -# if !defined(CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER) -# define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER -# endif -#endif - -#if !defined(CATCH_CONFIG_IMPL_ONLY) -// start catch_platform.h - -// See e.g.: -// https://opensource.apple.com/source/CarbonHeaders/CarbonHeaders-18.1/TargetConditionals.h.auto.html -#ifdef __APPLE__ -# include -# if (defined(TARGET_OS_OSX) && TARGET_OS_OSX == 1) || \ - (defined(TARGET_OS_MAC) && TARGET_OS_MAC == 1) -# define CATCH_PLATFORM_MAC -# elif (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE == 1) -# define CATCH_PLATFORM_IPHONE -# endif - -#elif defined(linux) || defined(__linux) || defined(__linux__) -# define CATCH_PLATFORM_LINUX - -#elif defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || defined(__MINGW32__) -# define CATCH_PLATFORM_WINDOWS -#endif - -// end catch_platform.h - -#ifdef CATCH_IMPL -# ifndef CLARA_CONFIG_MAIN -# define CLARA_CONFIG_MAIN_NOT_DEFINED -# define CLARA_CONFIG_MAIN -# endif -#endif - -// start catch_user_interfaces.h - -namespace Catch { - unsigned int rngSeed(); -} - -// end catch_user_interfaces.h -// start catch_tag_alias_autoregistrar.h - -// start catch_common.h - -// start catch_compiler_capabilities.h - -// Detect a number of compiler features - by compiler -// The following features are defined: -// -// CATCH_CONFIG_COUNTER : is the __COUNTER__ macro supported? -// CATCH_CONFIG_WINDOWS_SEH : is Windows SEH supported? -// CATCH_CONFIG_POSIX_SIGNALS : are POSIX signals supported? -// CATCH_CONFIG_DISABLE_EXCEPTIONS : Are exceptions enabled? -// **************** -// Note to maintainers: if new toggles are added please document them -// in configuration.md, too -// **************** - -// In general each macro has a _NO_ form -// (e.g. CATCH_CONFIG_NO_POSIX_SIGNALS) which disables the feature. -// Many features, at point of detection, define an _INTERNAL_ macro, so they -// can be combined, en-mass, with the _NO_ forms later. - -#ifdef __cplusplus - -# if (__cplusplus >= 201402L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L) -# define CATCH_CPP14_OR_GREATER -# endif - -# if (__cplusplus >= 201703L) || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) -# define CATCH_CPP17_OR_GREATER -# endif - -#endif - -// Only GCC compiler should be used in this block, so other compilers trying to -// mask themselves as GCC should be ignored. -#if defined(__GNUC__) && !defined(__clang__) && !defined(__ICC) && !defined(__CUDACC__) && !defined(__LCC__) -# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION _Pragma( "GCC diagnostic push" ) -# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION _Pragma( "GCC diagnostic pop" ) - -# define CATCH_INTERNAL_IGNORE_BUT_WARN(...) (void)__builtin_constant_p(__VA_ARGS__) - -#endif - -#if defined(__clang__) - -# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION _Pragma( "clang diagnostic push" ) -# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION _Pragma( "clang diagnostic pop" ) - -// As of this writing, IBM XL's implementation of __builtin_constant_p has a bug -// which results in calls to destructors being emitted for each temporary, -// without a matching initialization. In practice, this can result in something -// like `std::string::~string` being called on an uninitialized value. -// -// For example, this code will likely segfault under IBM XL: -// ``` -// REQUIRE(std::string("12") + "34" == "1234") -// ``` -// -// Therefore, `CATCH_INTERNAL_IGNORE_BUT_WARN` is not implemented. -# if !defined(__ibmxl__) && !defined(__CUDACC__) -# define CATCH_INTERNAL_IGNORE_BUT_WARN(...) (void)__builtin_constant_p(__VA_ARGS__) /* NOLINT(cppcoreguidelines-pro-type-vararg, hicpp-vararg) */ -# endif - -# define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ - _Pragma( "clang diagnostic ignored \"-Wexit-time-destructors\"" ) \ - _Pragma( "clang diagnostic ignored \"-Wglobal-constructors\"") - -# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS \ - _Pragma( "clang diagnostic ignored \"-Wparentheses\"" ) - -# define CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS \ - _Pragma( "clang diagnostic ignored \"-Wunused-variable\"" ) - -# define CATCH_INTERNAL_SUPPRESS_ZERO_VARIADIC_WARNINGS \ - _Pragma( "clang diagnostic ignored \"-Wgnu-zero-variadic-macro-arguments\"" ) - -# define CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS \ - _Pragma( "clang diagnostic ignored \"-Wunused-template\"" ) - -#endif // __clang__ - -//////////////////////////////////////////////////////////////////////////////// -// Assume that non-Windows platforms support posix signals by default -#if !defined(CATCH_PLATFORM_WINDOWS) - #define CATCH_INTERNAL_CONFIG_POSIX_SIGNALS -#endif - -//////////////////////////////////////////////////////////////////////////////// -// We know some environments not to support full POSIX signals -#if defined(__CYGWIN__) || defined(__QNX__) || defined(__EMSCRIPTEN__) || defined(__DJGPP__) - #define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS -#endif - -#ifdef __OS400__ -# define CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS -# define CATCH_CONFIG_COLOUR_NONE -#endif - -//////////////////////////////////////////////////////////////////////////////// -// Android somehow still does not support std::to_string -#if defined(__ANDROID__) -# define CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING -# define CATCH_INTERNAL_CONFIG_ANDROID_LOGWRITE -#endif - -//////////////////////////////////////////////////////////////////////////////// -// Not all Windows environments support SEH properly -#if defined(__MINGW32__) -# define CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH -#endif - -//////////////////////////////////////////////////////////////////////////////// -// PS4 -#if defined(__ORBIS__) -# define CATCH_INTERNAL_CONFIG_NO_NEW_CAPTURE -#endif - -//////////////////////////////////////////////////////////////////////////////// -// Cygwin -#ifdef __CYGWIN__ - -// Required for some versions of Cygwin to declare gettimeofday -// see: http://stackoverflow.com/questions/36901803/gettimeofday-not-declared-in-this-scope-cygwin -# define _BSD_SOURCE -// some versions of cygwin (most) do not support std::to_string. Use the libstd check. -// https://gcc.gnu.org/onlinedocs/gcc-4.8.2/libstdc++/api/a01053_source.html line 2812-2813 -# if !((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \ - && !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF)) - -# define CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING - -# endif -#endif // __CYGWIN__ - -//////////////////////////////////////////////////////////////////////////////// -// Visual C++ -#if defined(_MSC_VER) - -// Universal Windows platform does not support SEH -// Or console colours (or console at all...) -# if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) -# define CATCH_CONFIG_COLOUR_NONE -# else -# define CATCH_INTERNAL_CONFIG_WINDOWS_SEH -# endif - -# if !defined(__clang__) // Handle Clang masquerading for msvc - -// MSVC traditional preprocessor needs some workaround for __VA_ARGS__ -// _MSVC_TRADITIONAL == 0 means new conformant preprocessor -// _MSVC_TRADITIONAL == 1 means old traditional non-conformant preprocessor -# if !defined(_MSVC_TRADITIONAL) || (defined(_MSVC_TRADITIONAL) && _MSVC_TRADITIONAL) -# define CATCH_INTERNAL_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR -# endif // MSVC_TRADITIONAL - -// Only do this if we're not using clang on Windows, which uses `diagnostic push` & `diagnostic pop` -# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION __pragma( warning(push) ) -# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION __pragma( warning(pop) ) -# endif // __clang__ - -#endif // _MSC_VER - -#if defined(_REENTRANT) || defined(_MSC_VER) -// Enable async processing, as -pthread is specified or no additional linking is required -# define CATCH_INTERNAL_CONFIG_USE_ASYNC -#endif // _MSC_VER - -//////////////////////////////////////////////////////////////////////////////// -// Check if we are compiled with -fno-exceptions or equivalent -#if defined(__EXCEPTIONS) || defined(__cpp_exceptions) || defined(_CPPUNWIND) -# define CATCH_INTERNAL_CONFIG_EXCEPTIONS_ENABLED -#endif - -//////////////////////////////////////////////////////////////////////////////// -// DJGPP -#ifdef __DJGPP__ -# define CATCH_INTERNAL_CONFIG_NO_WCHAR -#endif // __DJGPP__ - -//////////////////////////////////////////////////////////////////////////////// -// Embarcadero C++Build -#if defined(__BORLANDC__) - #define CATCH_INTERNAL_CONFIG_POLYFILL_ISNAN -#endif - -//////////////////////////////////////////////////////////////////////////////// - -// Use of __COUNTER__ is suppressed during code analysis in -// CLion/AppCode 2017.2.x and former, because __COUNTER__ is not properly -// handled by it. -// Otherwise all supported compilers support COUNTER macro, -// but user still might want to turn it off -#if ( !defined(__JETBRAINS_IDE__) || __JETBRAINS_IDE__ >= 20170300L ) - #define CATCH_INTERNAL_CONFIG_COUNTER -#endif - -//////////////////////////////////////////////////////////////////////////////// - -// RTX is a special version of Windows that is real time. -// This means that it is detected as Windows, but does not provide -// the same set of capabilities as real Windows does. -#if defined(UNDER_RTSS) || defined(RTX64_BUILD) - #define CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH - #define CATCH_INTERNAL_CONFIG_NO_ASYNC - #define CATCH_CONFIG_COLOUR_NONE -#endif - -#if !defined(_GLIBCXX_USE_C99_MATH_TR1) -#define CATCH_INTERNAL_CONFIG_GLOBAL_NEXTAFTER -#endif - -// Various stdlib support checks that require __has_include -#if defined(__has_include) - // Check if string_view is available and usable - #if __has_include() && defined(CATCH_CPP17_OR_GREATER) - # define CATCH_INTERNAL_CONFIG_CPP17_STRING_VIEW - #endif - - // Check if optional is available and usable - # if __has_include() && defined(CATCH_CPP17_OR_GREATER) - # define CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL - # endif // __has_include() && defined(CATCH_CPP17_OR_GREATER) - - // Check if byte is available and usable - # if __has_include() && defined(CATCH_CPP17_OR_GREATER) - # include - # if defined(__cpp_lib_byte) && (__cpp_lib_byte > 0) - # define CATCH_INTERNAL_CONFIG_CPP17_BYTE - # endif - # endif // __has_include() && defined(CATCH_CPP17_OR_GREATER) - - // Check if variant is available and usable - # if __has_include() && defined(CATCH_CPP17_OR_GREATER) - # if defined(__clang__) && (__clang_major__ < 8) - // work around clang bug with libstdc++ https://bugs.llvm.org/show_bug.cgi?id=31852 - // fix should be in clang 8, workaround in libstdc++ 8.2 - # include - # if defined(__GLIBCXX__) && defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE < 9) - # define CATCH_CONFIG_NO_CPP17_VARIANT - # else - # define CATCH_INTERNAL_CONFIG_CPP17_VARIANT - # endif // defined(__GLIBCXX__) && defined(_GLIBCXX_RELEASE) && (_GLIBCXX_RELEASE < 9) - # else - # define CATCH_INTERNAL_CONFIG_CPP17_VARIANT - # endif // defined(__clang__) && (__clang_major__ < 8) - # endif // __has_include() && defined(CATCH_CPP17_OR_GREATER) -#endif // defined(__has_include) - -#if defined(CATCH_INTERNAL_CONFIG_COUNTER) && !defined(CATCH_CONFIG_NO_COUNTER) && !defined(CATCH_CONFIG_COUNTER) -# define CATCH_CONFIG_COUNTER -#endif -#if defined(CATCH_INTERNAL_CONFIG_WINDOWS_SEH) && !defined(CATCH_CONFIG_NO_WINDOWS_SEH) && !defined(CATCH_CONFIG_WINDOWS_SEH) && !defined(CATCH_INTERNAL_CONFIG_NO_WINDOWS_SEH) -# define CATCH_CONFIG_WINDOWS_SEH -#endif -// This is set by default, because we assume that unix compilers are posix-signal-compatible by default. -#if defined(CATCH_INTERNAL_CONFIG_POSIX_SIGNALS) && !defined(CATCH_INTERNAL_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_NO_POSIX_SIGNALS) && !defined(CATCH_CONFIG_POSIX_SIGNALS) -# define CATCH_CONFIG_POSIX_SIGNALS -#endif -// This is set by default, because we assume that compilers with no wchar_t support are just rare exceptions. -#if !defined(CATCH_INTERNAL_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_NO_WCHAR) && !defined(CATCH_CONFIG_WCHAR) -# define CATCH_CONFIG_WCHAR -#endif - -#if !defined(CATCH_INTERNAL_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_NO_CPP11_TO_STRING) && !defined(CATCH_CONFIG_CPP11_TO_STRING) -# define CATCH_CONFIG_CPP11_TO_STRING -#endif - -#if defined(CATCH_INTERNAL_CONFIG_CPP17_OPTIONAL) && !defined(CATCH_CONFIG_NO_CPP17_OPTIONAL) && !defined(CATCH_CONFIG_CPP17_OPTIONAL) -# define CATCH_CONFIG_CPP17_OPTIONAL -#endif - -#if defined(CATCH_INTERNAL_CONFIG_CPP17_STRING_VIEW) && !defined(CATCH_CONFIG_NO_CPP17_STRING_VIEW) && !defined(CATCH_CONFIG_CPP17_STRING_VIEW) -# define CATCH_CONFIG_CPP17_STRING_VIEW -#endif - -#if defined(CATCH_INTERNAL_CONFIG_CPP17_VARIANT) && !defined(CATCH_CONFIG_NO_CPP17_VARIANT) && !defined(CATCH_CONFIG_CPP17_VARIANT) -# define CATCH_CONFIG_CPP17_VARIANT -#endif - -#if defined(CATCH_INTERNAL_CONFIG_CPP17_BYTE) && !defined(CATCH_CONFIG_NO_CPP17_BYTE) && !defined(CATCH_CONFIG_CPP17_BYTE) -# define CATCH_CONFIG_CPP17_BYTE -#endif - -#if defined(CATCH_CONFIG_EXPERIMENTAL_REDIRECT) -# define CATCH_INTERNAL_CONFIG_NEW_CAPTURE -#endif - -#if defined(CATCH_INTERNAL_CONFIG_NEW_CAPTURE) && !defined(CATCH_INTERNAL_CONFIG_NO_NEW_CAPTURE) && !defined(CATCH_CONFIG_NO_NEW_CAPTURE) && !defined(CATCH_CONFIG_NEW_CAPTURE) -# define CATCH_CONFIG_NEW_CAPTURE -#endif - -#if !defined(CATCH_INTERNAL_CONFIG_EXCEPTIONS_ENABLED) && !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) -# define CATCH_CONFIG_DISABLE_EXCEPTIONS -#endif - -#if defined(CATCH_INTERNAL_CONFIG_POLYFILL_ISNAN) && !defined(CATCH_CONFIG_NO_POLYFILL_ISNAN) && !defined(CATCH_CONFIG_POLYFILL_ISNAN) -# define CATCH_CONFIG_POLYFILL_ISNAN -#endif - -#if defined(CATCH_INTERNAL_CONFIG_USE_ASYNC) && !defined(CATCH_INTERNAL_CONFIG_NO_ASYNC) && !defined(CATCH_CONFIG_NO_USE_ASYNC) && !defined(CATCH_CONFIG_USE_ASYNC) -# define CATCH_CONFIG_USE_ASYNC -#endif - -#if defined(CATCH_INTERNAL_CONFIG_ANDROID_LOGWRITE) && !defined(CATCH_CONFIG_NO_ANDROID_LOGWRITE) && !defined(CATCH_CONFIG_ANDROID_LOGWRITE) -# define CATCH_CONFIG_ANDROID_LOGWRITE -#endif - -#if defined(CATCH_INTERNAL_CONFIG_GLOBAL_NEXTAFTER) && !defined(CATCH_CONFIG_NO_GLOBAL_NEXTAFTER) && !defined(CATCH_CONFIG_GLOBAL_NEXTAFTER) -# define CATCH_CONFIG_GLOBAL_NEXTAFTER -#endif - -// Even if we do not think the compiler has that warning, we still have -// to provide a macro that can be used by the code. -#if !defined(CATCH_INTERNAL_START_WARNINGS_SUPPRESSION) -# define CATCH_INTERNAL_START_WARNINGS_SUPPRESSION -#endif -#if !defined(CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION) -# define CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION -#endif -#if !defined(CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS) -# define CATCH_INTERNAL_SUPPRESS_PARENTHESES_WARNINGS -#endif -#if !defined(CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS) -# define CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS -#endif -#if !defined(CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS) -# define CATCH_INTERNAL_SUPPRESS_UNUSED_WARNINGS -#endif -#if !defined(CATCH_INTERNAL_SUPPRESS_ZERO_VARIADIC_WARNINGS) -# define CATCH_INTERNAL_SUPPRESS_ZERO_VARIADIC_WARNINGS -#endif - -// The goal of this macro is to avoid evaluation of the arguments, but -// still have the compiler warn on problems inside... -#if !defined(CATCH_INTERNAL_IGNORE_BUT_WARN) -# define CATCH_INTERNAL_IGNORE_BUT_WARN(...) -#endif - -#if defined(__APPLE__) && defined(__apple_build_version__) && (__clang_major__ < 10) -# undef CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS -#elif defined(__clang__) && (__clang_major__ < 5) -# undef CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS -#endif - -#if !defined(CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS) -# define CATCH_INTERNAL_SUPPRESS_UNUSED_TEMPLATE_WARNINGS -#endif - -#if defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) -#define CATCH_TRY if ((true)) -#define CATCH_CATCH_ALL if ((false)) -#define CATCH_CATCH_ANON(type) if ((false)) -#else -#define CATCH_TRY try -#define CATCH_CATCH_ALL catch (...) -#define CATCH_CATCH_ANON(type) catch (type) -#endif - -#if defined(CATCH_INTERNAL_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR) && !defined(CATCH_CONFIG_NO_TRADITIONAL_MSVC_PREPROCESSOR) && !defined(CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR) -#define CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR -#endif - -// end catch_compiler_capabilities.h -#define INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) name##line -#define INTERNAL_CATCH_UNIQUE_NAME_LINE( name, line ) INTERNAL_CATCH_UNIQUE_NAME_LINE2( name, line ) -#ifdef CATCH_CONFIG_COUNTER -# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __COUNTER__ ) -#else -# define INTERNAL_CATCH_UNIQUE_NAME( name ) INTERNAL_CATCH_UNIQUE_NAME_LINE( name, __LINE__ ) -#endif - -#include -#include -#include - -// We need a dummy global operator<< so we can bring it into Catch namespace later -struct Catch_global_namespace_dummy {}; -std::ostream& operator<<(std::ostream&, Catch_global_namespace_dummy); - -namespace Catch { - - struct CaseSensitive { enum Choice { - Yes, - No - }; }; - - class NonCopyable { - NonCopyable( NonCopyable const& ) = delete; - NonCopyable( NonCopyable && ) = delete; - NonCopyable& operator = ( NonCopyable const& ) = delete; - NonCopyable& operator = ( NonCopyable && ) = delete; - - protected: - NonCopyable(); - virtual ~NonCopyable(); - }; - - struct SourceLineInfo { - - SourceLineInfo() = delete; - SourceLineInfo( char const* _file, std::size_t _line ) noexcept - : file( _file ), - line( _line ) - {} - - SourceLineInfo( SourceLineInfo const& other ) = default; - SourceLineInfo& operator = ( SourceLineInfo const& ) = default; - SourceLineInfo( SourceLineInfo&& ) noexcept = default; - SourceLineInfo& operator = ( SourceLineInfo&& ) noexcept = default; - - bool empty() const noexcept { return file[0] == '\0'; } - bool operator == ( SourceLineInfo const& other ) const noexcept; - bool operator < ( SourceLineInfo const& other ) const noexcept; - - char const* file; - std::size_t line; - }; - - std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ); - - // Bring in operator<< from global namespace into Catch namespace - // This is necessary because the overload of operator<< above makes - // lookup stop at namespace Catch - using ::operator<<; - - // Use this in variadic streaming macros to allow - // >> +StreamEndStop - // as well as - // >> stuff +StreamEndStop - struct StreamEndStop { - std::string operator+() const; - }; - template - T const& operator + ( T const& value, StreamEndStop ) { - return value; - } -} - -#define CATCH_INTERNAL_LINEINFO \ - ::Catch::SourceLineInfo( __FILE__, static_cast( __LINE__ ) ) - -// end catch_common.h -namespace Catch { - - struct RegistrarForTagAliases { - RegistrarForTagAliases( char const* alias, char const* tag, SourceLineInfo const& lineInfo ); - }; - -} // end namespace Catch - -#define CATCH_REGISTER_TAG_ALIAS( alias, spec ) \ - CATCH_INTERNAL_START_WARNINGS_SUPPRESSION \ - CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS \ - namespace{ Catch::RegistrarForTagAliases INTERNAL_CATCH_UNIQUE_NAME( AutoRegisterTagAlias )( alias, spec, CATCH_INTERNAL_LINEINFO ); } \ - CATCH_INTERNAL_STOP_WARNINGS_SUPPRESSION - -// end catch_tag_alias_autoregistrar.h -// start catch_test_registry.h - -// start catch_interfaces_testcase.h - -#include - -namespace Catch { - - class TestSpec; - - struct ITestInvoker { - virtual void invoke () const = 0; - virtual ~ITestInvoker(); - }; - - class TestCase; - struct IConfig; - - struct ITestCaseRegistry { - virtual ~ITestCaseRegistry(); - virtual std::vector const& getAllTests() const = 0; - virtual std::vector const& getAllTestsSorted( IConfig const& config ) const = 0; - }; - - bool isThrowSafe( TestCase const& testCase, IConfig const& config ); - bool matchTest( TestCase const& testCase, TestSpec const& testSpec, IConfig const& config ); - std::vector filterTests( std::vector const& testCases, TestSpec const& testSpec, IConfig const& config ); - std::vector const& getAllTestCasesSorted( IConfig const& config ); - -} - -// end catch_interfaces_testcase.h -// start catch_stringref.h - -#include -#include -#include -#include - -namespace Catch { - - /// A non-owning string class (similar to the forthcoming std::string_view) - /// Note that, because a StringRef may be a substring of another string, - /// it may not be null terminated. - class StringRef { - public: - using size_type = std::size_t; - using const_iterator = const char*; - - private: - static constexpr char const* const s_empty = ""; - - char const* m_start = s_empty; - size_type m_size = 0; - - public: // construction - constexpr StringRef() noexcept = default; - - StringRef( char const* rawChars ) noexcept; - - constexpr StringRef( char const* rawChars, size_type size ) noexcept - : m_start( rawChars ), - m_size( size ) - {} - - StringRef( std::string const& stdString ) noexcept - : m_start( stdString.c_str() ), - m_size( stdString.size() ) - {} - - explicit operator std::string() const { - return std::string(m_start, m_size); - } - - public: // operators - auto operator == ( StringRef const& other ) const noexcept -> bool; - auto operator != (StringRef const& other) const noexcept -> bool { - return !(*this == other); - } - - auto operator[] ( size_type index ) const noexcept -> char { - assert(index < m_size); - return m_start[index]; - } - - public: // named queries - constexpr auto empty() const noexcept -> bool { - return m_size == 0; - } - constexpr auto size() const noexcept -> size_type { - return m_size; - } - - // Returns the current start pointer. If the StringRef is not - // null-terminated, throws std::domain_exception - auto c_str() const -> char const*; - - public: // substrings and searches - // Returns a substring of [start, start + length). - // If start + length > size(), then the substring is [start, size()). - // If start > size(), then the substring is empty. - auto substr( size_type start, size_type length ) const noexcept -> StringRef; - - // Returns the current start pointer. May not be null-terminated. - auto data() const noexcept -> char const*; - - constexpr auto isNullTerminated() const noexcept -> bool { - return m_start[m_size] == '\0'; - } - - public: // iterators - constexpr const_iterator begin() const { return m_start; } - constexpr const_iterator end() const { return m_start + m_size; } - }; - - auto operator += ( std::string& lhs, StringRef const& sr ) -> std::string&; - auto operator << ( std::ostream& os, StringRef const& sr ) -> std::ostream&; - - constexpr auto operator "" _sr( char const* rawChars, std::size_t size ) noexcept -> StringRef { - return StringRef( rawChars, size ); - } -} // namespace Catch - -constexpr auto operator "" _catch_sr( char const* rawChars, std::size_t size ) noexcept -> Catch::StringRef { - return Catch::StringRef( rawChars, size ); -} - -// end catch_stringref.h -// start catch_preprocessor.hpp - - -#define CATCH_RECURSION_LEVEL0(...) __VA_ARGS__ -#define CATCH_RECURSION_LEVEL1(...) CATCH_RECURSION_LEVEL0(CATCH_RECURSION_LEVEL0(CATCH_RECURSION_LEVEL0(__VA_ARGS__))) -#define CATCH_RECURSION_LEVEL2(...) CATCH_RECURSION_LEVEL1(CATCH_RECURSION_LEVEL1(CATCH_RECURSION_LEVEL1(__VA_ARGS__))) -#define CATCH_RECURSION_LEVEL3(...) CATCH_RECURSION_LEVEL2(CATCH_RECURSION_LEVEL2(CATCH_RECURSION_LEVEL2(__VA_ARGS__))) -#define CATCH_RECURSION_LEVEL4(...) CATCH_RECURSION_LEVEL3(CATCH_RECURSION_LEVEL3(CATCH_RECURSION_LEVEL3(__VA_ARGS__))) -#define CATCH_RECURSION_LEVEL5(...) CATCH_RECURSION_LEVEL4(CATCH_RECURSION_LEVEL4(CATCH_RECURSION_LEVEL4(__VA_ARGS__))) - -#ifdef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR -#define INTERNAL_CATCH_EXPAND_VARGS(...) __VA_ARGS__ -// MSVC needs more evaluations -#define CATCH_RECURSION_LEVEL6(...) CATCH_RECURSION_LEVEL5(CATCH_RECURSION_LEVEL5(CATCH_RECURSION_LEVEL5(__VA_ARGS__))) -#define CATCH_RECURSE(...) CATCH_RECURSION_LEVEL6(CATCH_RECURSION_LEVEL6(__VA_ARGS__)) -#else -#define CATCH_RECURSE(...) CATCH_RECURSION_LEVEL5(__VA_ARGS__) -#endif - -#define CATCH_REC_END(...) -#define CATCH_REC_OUT - -#define CATCH_EMPTY() -#define CATCH_DEFER(id) id CATCH_EMPTY() - -#define CATCH_REC_GET_END2() 0, CATCH_REC_END -#define CATCH_REC_GET_END1(...) CATCH_REC_GET_END2 -#define CATCH_REC_GET_END(...) CATCH_REC_GET_END1 -#define CATCH_REC_NEXT0(test, next, ...) next CATCH_REC_OUT -#define CATCH_REC_NEXT1(test, next) CATCH_DEFER ( CATCH_REC_NEXT0 ) ( test, next, 0) -#define CATCH_REC_NEXT(test, next) CATCH_REC_NEXT1(CATCH_REC_GET_END test, next) - -#define CATCH_REC_LIST0(f, x, peek, ...) , f(x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1) ) ( f, peek, __VA_ARGS__ ) -#define CATCH_REC_LIST1(f, x, peek, ...) , f(x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST0) ) ( f, peek, __VA_ARGS__ ) -#define CATCH_REC_LIST2(f, x, peek, ...) f(x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1) ) ( f, peek, __VA_ARGS__ ) - -#define CATCH_REC_LIST0_UD(f, userdata, x, peek, ...) , f(userdata, x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1_UD) ) ( f, userdata, peek, __VA_ARGS__ ) -#define CATCH_REC_LIST1_UD(f, userdata, x, peek, ...) , f(userdata, x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST0_UD) ) ( f, userdata, peek, __VA_ARGS__ ) -#define CATCH_REC_LIST2_UD(f, userdata, x, peek, ...) f(userdata, x) CATCH_DEFER ( CATCH_REC_NEXT(peek, CATCH_REC_LIST1_UD) ) ( f, userdata, peek, __VA_ARGS__ ) - -// Applies the function macro `f` to each of the remaining parameters, inserts commas between the results, -// and passes userdata as the first parameter to each invocation, -// e.g. CATCH_REC_LIST_UD(f, x, a, b, c) evaluates to f(x, a), f(x, b), f(x, c) -#define CATCH_REC_LIST_UD(f, userdata, ...) CATCH_RECURSE(CATCH_REC_LIST2_UD(f, userdata, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) - -#define CATCH_REC_LIST(f, ...) CATCH_RECURSE(CATCH_REC_LIST2(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) - -#define INTERNAL_CATCH_EXPAND1(param) INTERNAL_CATCH_EXPAND2(param) -#define INTERNAL_CATCH_EXPAND2(...) INTERNAL_CATCH_NO## __VA_ARGS__ -#define INTERNAL_CATCH_DEF(...) INTERNAL_CATCH_DEF __VA_ARGS__ -#define INTERNAL_CATCH_NOINTERNAL_CATCH_DEF -#define INTERNAL_CATCH_STRINGIZE(...) INTERNAL_CATCH_STRINGIZE2(__VA_ARGS__) -#ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR -#define INTERNAL_CATCH_STRINGIZE2(...) #__VA_ARGS__ -#define INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS(param) INTERNAL_CATCH_STRINGIZE(INTERNAL_CATCH_REMOVE_PARENS(param)) -#else -// MSVC is adding extra space and needs another indirection to expand INTERNAL_CATCH_NOINTERNAL_CATCH_DEF -#define INTERNAL_CATCH_STRINGIZE2(...) INTERNAL_CATCH_STRINGIZE3(__VA_ARGS__) -#define INTERNAL_CATCH_STRINGIZE3(...) #__VA_ARGS__ -#define INTERNAL_CATCH_STRINGIZE_WITHOUT_PARENS(param) (INTERNAL_CATCH_STRINGIZE(INTERNAL_CATCH_REMOVE_PARENS(param)) + 1) -#endif - -#define INTERNAL_CATCH_MAKE_NAMESPACE2(...) ns_##__VA_ARGS__ -#define INTERNAL_CATCH_MAKE_NAMESPACE(name) INTERNAL_CATCH_MAKE_NAMESPACE2(name) - -#define INTERNAL_CATCH_REMOVE_PARENS(...) INTERNAL_CATCH_EXPAND1(INTERNAL_CATCH_DEF __VA_ARGS__) - -#ifndef CATCH_CONFIG_TRADITIONAL_MSVC_PREPROCESSOR -#define INTERNAL_CATCH_MAKE_TYPE_LIST2(...) decltype(get_wrapper()) -#define INTERNAL_CATCH_MAKE_TYPE_LIST(...) INTERNAL_CATCH_MAKE_TYPE_LIST2(INTERNAL_CATCH_REMOVE_PARENS(__VA_ARGS__)) -#else -#define INTERNAL_CATCH_MAKE_TYPE_LIST2(...) INTERNAL_CATCH_EXPAND_VARGS(decltype(get_wrapper())) -#define INTERNAL_CATCH_MAKE_TYPE_LIST(...) INTERNAL_CATCH_EXPAND_VARGS(INTERNAL_CATCH_MAKE_TYPE_LIST2(INTERNAL_CATCH_REMOVE_PARENS(__VA_ARGS__))) -#endif - -#define INTERNAL_CATCH_MAKE_TYPE_LISTS_FROM_TYPES(...)\ - CATCH_REC_LIST(INTERNAL_CATCH_MAKE_TYPE_LIST,__VA_ARGS__) - -#define INTERNAL_CATCH_REMOVE_PARENS_1_ARG(_0) INTERNAL_CATCH_REMOVE_PARENS(_0) -#define INTERNAL_CATCH_REMOVE_PARENS_2_ARG(_0, _1) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_1_ARG(_1) -#define INTERNAL_CATCH_REMOVE_PARENS_3_ARG(_0, _1, _2) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_2_ARG(_1, _2) -#define INTERNAL_CATCH_REMOVE_PARENS_4_ARG(_0, _1, _2, _3) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_3_ARG(_1, _2, _3) -#define INTERNAL_CATCH_REMOVE_PARENS_5_ARG(_0, _1, _2, _3, _4) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_4_ARG(_1, _2, _3, _4) -#define INTERNAL_CATCH_REMOVE_PARENS_6_ARG(_0, _1, _2, _3, _4, _5) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_5_ARG(_1, _2, _3, _4, _5) -#define INTERNAL_CATCH_REMOVE_PARENS_7_ARG(_0, _1, _2, _3, _4, _5, _6) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_6_ARG(_1, _2, _3, _4, _5, _6) -#define INTERNAL_CATCH_REMOVE_PARENS_8_ARG(_0, _1, _2, _3, _4, _5, _6, _7) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_7_ARG(_1, _2, _3, _4, _5, _6, _7) -#define INTERNAL_CATCH_REMOVE_PARENS_9_ARG(_0, _1, _2, _3, _4, _5, _6, _7, _8) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_8_ARG(_1, _2, _3, _4, _5, _6, _7, _8) -#define INTERNAL_CATCH_REMOVE_PARENS_10_ARG(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_9_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9) -#define INTERNAL_CATCH_REMOVE_PARENS_11_ARG(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10) INTERNAL_CATCH_REMOVE_PARENS(_0), INTERNAL_CATCH_REMOVE_PARENS_10_ARG(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10) - -#define INTERNAL_CATCH_VA_NARGS_IMPL(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N - -#define INTERNAL_CATCH_TYPE_GEN\ - template struct TypeList {};\ - template\ - constexpr auto get_wrapper() noexcept -> TypeList { return {}; }\ - template class...> struct TemplateTypeList{};\ - template class...Cs>\ - constexpr auto get_wrapper() noexcept -> TemplateTypeList { return {}; }\ - template\ - struct append;\ - template\ - struct rewrap;\ - template class, typename...>\ - struct create;\ - template class, typename>\ - struct convert;\ - \ - template \ - struct append { using type = T; };\ - template< template class L1, typename...E1, template class L2, typename...E2, typename...Rest>\ - struct append, L2, Rest...> { using type = typename append, Rest...>::type; };\ - template< template class L1, typename...E1, typename...Rest>\ - struct append, TypeList, Rest...> { using type = L1; };\ - \ - template< template class Container, template class List, typename...elems>\ - struct rewrap, List> { using type = TypeList>; };\ - template< template class Container, template class List, class...Elems, typename...Elements>\ - struct rewrap, List, Elements...> { using type = typename append>, typename rewrap, Elements...>::type>::type; };\ - \ - template