diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index e69308f..3587d65 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -49,7 +49,7 @@ jobs: echo "CODECOV_TOKEN=$CODECOV_TOKEN" >> "$GITHUB_ENV" - name: Upload coverage report to Codecov - uses: codecov/codecov-action@v3 + uses: codecov/codecov-action@v5 with: file: "${{ env.COVERAGE_DATA_PATH }}" fail_ci_if_error: true diff --git a/README.md b/README.md index 0546307..57199a7 100644 --- a/README.md +++ b/README.md @@ -34,10 +34,26 @@ The procedure: * `Arduino MKR WiFi 1010`: short the pin 7 to GND until the led turns off * `Arduino GIGA R1 WiFi`: short the pin 7 to GND until the led turns off * `Arduino Nano RP2040 Connect`: short the pin 2 to 3.3V until the led turns off +* `Arduino Portenta H7`: short the pin 0 to GND until the led turns off +* `Arduino Portenta C33`: short the pin 0 to GND until the led turns off * Other boards: short the pin 2 to GND until the led turns off +* `Portenta Machine Control`: currently the reset procedure is not available + +### More on the reconfiguration pin +Internally, the pin indicated in the procedure is set as `INPUT_PULLUP` (except for `Arduino Opta` ) and it's attached to an ISR fired on every change of the pin's status. + +In order to be notified when the ISR is fired, it's possible to register a callback function using the function `NetworkConfigurator.addReconfigurePinCallback(callback)`. Please take the example as reference. + +### Change the reconfiguration pin +In order to change the default pin for resetting the board, it's possible to use the function `NetworkConfigurator.setReconfigurePin(your_pin)` specifying the new pin. +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/ + +### Disable the reconfiguration feature +In order to disable the reconfiguration procedure, use this function in the sketch `NetworkConfigurator.setReconfigurePin(DISABLE_PIN)` + ## Configurator Agents -The library provides a set of *Configurator Agents* that added as plug-in to the sketch handle the communication between the Arduino Network Configurator and an external client ([*Arduino IoT App*](https://cloud.arduino.cc/iot-remote-app/) and Arduino IoT Cloud) for configuring the board. +The library provides a set of *Configurator Agents* that added as plug-in to the sketch handle the communication between the Arduino Network Configurator and an external client ([*Arduino IoT App*](https://cloud.arduino.cc/iot-remote-app/), Arduino IoT Cloud, [*Arduino Cloud CLI from v0.3.4*](https://github.com/arduino/arduino-cloud-cli)) for configuring the board. Out-of-the box there are 2 Configurator Agents * `BLEAgent`: manage the BLE interface diff --git a/examples/NetworkConfiguratorDemo/NetworkConfiguratorDemo.ino b/examples/NetworkConfiguratorDemo/NetworkConfiguratorDemo.ino index 1bb8e6c..bc03b42 100644 --- a/examples/NetworkConfiguratorDemo/NetworkConfiguratorDemo.ino +++ b/examples/NetworkConfiguratorDemo/NetworkConfiguratorDemo.ino @@ -21,6 +21,9 @@ * - Arduino MKR WiFi 1010: short the pin 7 to GND until the led turns off * - Arduino GIGA R1 WiFi: short the pin 7 to GND until the led turns off * - Arduino Nano RP2040 Connect: short the pin 2 to 3.3V until the led turns off + * - Portenta H7: short the pin 0 to GND until the led turns off + * - Portenta C33: short the pin 0 to GND until the led turns off + * - Portenta Machine Control: the reset is not available * - Other boards: short the pin 2 to GND until the led turns off * * In this sketch the BLE and Serial interfaces are always enabled and ready for accepting diff --git a/extras/test/CMakeLists.txt b/extras/test/CMakeLists.txt index b4c0898..b3736e9 100644 --- a/extras/test/CMakeLists.txt +++ b/extras/test/CMakeLists.txt @@ -24,8 +24,8 @@ FetchContent_Declare( FetchContent_Declare( connectionhandler - GIT_REPOSITORY https://github.com/andreagilardoni/Arduino_ConnectionHandler.git - GIT_TAG connectionhandler-settings + GIT_REPOSITORY https://github.com/arduino-libraries/Arduino_ConnectionHandler.git + GIT_TAG master CONFIGURE_COMMAND "" BUILD_COMMAND "" ) diff --git a/extras/test/src/test_provisioning_command_encode.cpp b/extras/test/src/test_provisioning_command_encode.cpp index 64872fc..2211ee7 100644 --- a/extras/test/src/test_provisioning_command_encode.cpp +++ b/extras/test/src/test_provisioning_command_encode.cpp @@ -181,27 +181,43 @@ } } - WHEN("Encode a message with provisioning ble mac Address ") + WHEN("Encode a message with provisioning public key") { - BLEMacAddressProvisioningMessage command; - command.c.id = ProvisioningMessageId::BLEMacAddressProvisioningMessageId; - memset(command.macAddress, 0xAF, 6); + ProvPublicKeyProvisioningMessage command; + command.c.id = ProvisioningMessageId::ProvPublicKeyProvisioningMessageId; + command.provPublicKey = "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7JxCtXl5SvIrHmiasqyN4pyoXRlm44d5WXNpqmvJ\nk0tH8UpmIeHG7YPAkKLaqid95v/wLVoWeX5EbjxmlCkFtw==\n-----END PUBLIC KEY-----\n"; uint8_t buffer[512]; size_t bytes_encoded = sizeof(buffer); CBORMessageEncoder encoder; MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded); + // Test the encoding is + // DA 00012017 # tag(73751) + // 81 # array(1) + // 78 B4 # text(180) + // 2D 2D 2D 2D 2D 4245 47 49 4E 20 50 55 42 4C 49 43204B45592D2D2D2D2D0A0A4D466B77457759484B6F5A497A6A3043415159494B6F5A497A6A3044415163445167414537 + // 4A784374586C3553764972486D69617371794E3470796F58526C6D3434643557584E70716D764A0A6B3074483855706D49654847375950416B4B4C617169643935762F774C566F5765583545626A786D6C436B4674773D3D0A0A2D2D2D2D2D454E44205055424C4943204B45592D2D2D2D2D0A + uint8_t expected_result[] = { - 0xda, 0x00, 0x01, 0x20, 0x13, 0x81, 0x46, - 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF + 0xDA, 0x00, 0x01, 0x20, 0x17, 0x81, 0x78, 0xB2, + 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x42, 0x45, 0x47, 0x49, 0x4E, 0x20, 0x50, 0x55, 0x42, 0x4C, 0x49, + 0x43, 0x20, 0x4B, 0x45, 0x59, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x0A, 0x4D, 0x46, 0x6B, 0x77, + 0x45, 0x77, 0x59, 0x48, 0x4B, 0x6F, 0x5A, 0x49, + 0x7A, 0x6A, 0x30, 0x43, 0x41, 0x51, 0x59, 0x49, 0x4B, 0x6F, 0x5A, 0x49, + 0x7A, 0x6A, 0x30, 0x44, 0x41, 0x51, 0x63, 0x44, 0x51, 0x67, 0x41, 0x45, + 0x37, 0x4A, 0x78, 0x43, 0x74, 0x58, 0x6C, 0x35, 0x53, 0x76, 0x49, 0x72, + 0x48, 0x6D, 0x69, 0x61, 0x73, 0x71, 0x79, 0x4E, 0x34, 0x70, 0x79, 0x6F, + 0x58, 0x52, 0x6C, 0x6D, 0x34, 0x34, 0x64, 0x35, 0x57, 0x58, 0x4E, 0x70, + 0x71, 0x6D, 0x76, 0x4A, 0x0A, 0x6B, 0x30, 0x74, 0x48, 0x38, 0x55, 0x70, + 0x6D, 0x49, 0x65, 0x48, 0x47, 0x37, 0x59, 0x50, 0x41, 0x6B, 0x4B, 0x4C, + 0x61, 0x71, 0x69, 0x64, 0x39, 0x35, 0x76, 0x2F, 0x77, 0x4C, 0x56, 0x6F, + 0x57, 0x65, 0x58, 0x35, 0x45, 0x62, 0x6A, 0x78, 0x6D, 0x6C, 0x43, 0x6B, + 0x46, 0x74, 0x77, 0x3D, 0x3D, 0x0A, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, + 0x45, 0x4E, 0x44, 0x20, 0x50, 0x55, 0x42, 0x4C, 0x49, 0x43, 0x20, 0x4B, + 0x45, 0x59, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x0A }; - // Test the encoding is - //DA 00012013 # tag(73747) - //81 # array(1) - // 46 # bytes(6) - // AFAFAFAFAFA THEN("The encoding is successful") { REQUIRE(err == MessageEncoder::Status::Complete); REQUIRE(bytes_encoded == sizeof(expected_result)); @@ -209,11 +225,27 @@ } } - WHEN("Encode a message with provisioning wifi fw version ") + WHEN("Encode a message with provisioning public key") + { + ProvPublicKeyProvisioningMessage command; + command.c.id = ProvisioningMessageId::ProvPublicKeyProvisioningMessageId; + command.provPublicKey = "-----BEGIN PUBLIC KEY-----\nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE7JxCtXl5SvIrHmiasqyN4pyoXRlm44d5WXNpqmvJ\nk0tH8UpmIeHG7YPAkKLaqid95v/wLVoWeX5EbjxmlCkFtw==\n-----END PUBLIC KEY-----\n"; + uint8_t buffer[50]; + size_t bytes_encoded = sizeof(buffer); + + CBORMessageEncoder encoder; + MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded); + + THEN("The encoding is failing") { + REQUIRE(err == MessageEncoder::Status::Error); + } + } + + WHEN("Encode a message with provisioning ble mac Address ") { - WiFiFWVersionProvisioningMessage command; - command.c.id = ProvisioningMessageId::WiFiFWVersionProvisioningMessageId; - command.wifiFwVersion = "1.6.0"; + BLEMacAddressProvisioningMessage command; + command.c.id = ProvisioningMessageId::BLEMacAddressProvisioningMessageId; + memset(command.macAddress, 0xAF, 6); uint8_t buffer[512]; size_t bytes_encoded = sizeof(buffer); @@ -221,14 +253,15 @@ MessageEncoder::Status err = encoder.encode((Message*)&command, buffer, bytes_encoded); uint8_t expected_result[] = { - 0xda, 0x00, 0x01, 0x20, 0x14, 0x81, 0x65, 0x31, 0x2E, 0x36, 0x2E, 0x30 + 0xda, 0x00, 0x01, 0x20, 0x13, 0x81, 0x46, + 0xAF, 0xAF, 0xAF, 0xAF, 0xAF, 0xAF }; // Test the encoding is - //DA 00012014 # tag(73748) - // 81 # array(1) - // 65 # text(5) - // 312E362E30 # "1.6.0" + //DA 00012013 # tag(73747) + //81 # array(1) + // 46 # bytes(6) + // AFAFAFAFAFA THEN("The encoding is successful") { REQUIRE(err == MessageEncoder::Status::Complete); REQUIRE(bytes_encoded == sizeof(expected_result)); @@ -283,7 +316,6 @@ // 81 # array(1) // 65 # text(5) // 312E362E30 # "1.6.0" - printf("res %d\n", (int)err); THEN("The encoding is successful") { REQUIRE(err == MessageEncoder::Status::Complete); REQUIRE(bytes_encoded == sizeof(expected_result)); diff --git a/library.properties b/library.properties index a90f9ad..8e8e484 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Arduino_NetworkConfigurator -version=0.1.2 +version=0.3.1 author=Arduino maintainer=Arduino sentence=This library allows to configure and update the network settings of a ConnectionHandler instance. diff --git a/src/ANetworkConfigurator_Config.h b/src/ANetworkConfigurator_Config.h index 9b474ba..db539f2 100644 --- a/src/ANetworkConfigurator_Config.h +++ b/src/ANetworkConfigurator_Config.h @@ -7,7 +7,7 @@ */ #pragma once -#define ANetworkConfigurator_LIB_VERSION "0.1.2" +#define ANetworkConfigurator_LIB_VERSION "0.3.1" #if defined(ARDUINO_SAMD_MKRWIFI1010) #define NETWORK_CONFIGURATOR_COMPATIBLE 1 @@ -57,7 +57,9 @@ #if defined(ARDUINO_PORTENTA_H7_M7) #define NETWORK_CONFIGURATOR_COMPATIBLE 1 #define ZERO_TOUCH_ENABLED 1 - #define PIN_RECONFIGURE 2 + #define I2C_ADD_DETECT_MACHINE_CONTROL_1 0x23 + #define I2C_ADD_DETECT_MACHINE_CONTROL_2 0x22 + #define PIN_RECONFIGURE 0 #define LED_RECONFIGURE LED_BUILTIN #define BOARD_HAS_RGB #define GREEN_LED LEDG @@ -108,7 +110,7 @@ #if defined(ARDUINO_PORTENTA_C33) #define NETWORK_CONFIGURATOR_COMPATIBLE 1 #define ZERO_TOUCH_ENABLED 1 - #define PIN_RECONFIGURE 2 + #define PIN_RECONFIGURE 0 #define LED_RECONFIGURE LEDG #define BOARD_HAS_RGB #define GREEN_LED LEDG diff --git a/src/Arduino_NetworkConfigurator.cpp b/src/Arduino_NetworkConfigurator.cpp index 85bf4a0..e4e2bfd 100644 --- a/src/Arduino_NetworkConfigurator.cpp +++ b/src/Arduino_NetworkConfigurator.cpp @@ -33,6 +33,7 @@ NetworkConfiguratorClass::NetworkConfiguratorClass(ConnectionHandler &connection _state{ NetworkConfiguratorStates::END }, _connectionHandler{ &connectionHandler }, _connectionHandlerIstantiated{ false }, + _configInProgress{ false }, _kvstore{ nullptr }, _connectionTimeout{ NC_CONNECTION_TIMEOUT_ms, NC_CONNECTION_TIMEOUT_ms }, _connectionRetryTimer{ NC_CONNECTION_RETRY_TIMER_ms, NC_CONNECTION_RETRY_TIMER_ms }, @@ -48,19 +49,9 @@ bool NetworkConfiguratorClass::begin() { if(_state != NetworkConfiguratorStates::END) { return true; } - /* - * If the board is zero touch capable, starts with zero touch configuration mode - * In this state the board will try to connect to the network using a set of - * default network settings ex. Ethernet with DHCP - * This mode will fail if the provided ConnectionHandler is not GenericConnectionHandler type - * falling back to read the network settings from the storage - */ - #if ZERO_TOUCH_ENABLED - _state = NetworkConfiguratorStates::ZERO_TOUCH_CONFIG; - #else _state = NetworkConfiguratorStates::READ_STORED_CONFIG; - #endif + _connectionHandler->enableCheckInternetAvailability(true); memset(&_networkSetting, 0x00, sizeof(models::NetworkSetting)); @@ -85,7 +76,6 @@ bool NetworkConfiguratorClass::begin() { DEBUG_ERROR("NetworkConfiguratorClass::%s Failed to initialize the AgentsManagerClass", __FUNCTION__); } - _connectionTimeout.begin(NC_CONNECTION_TIMEOUT_ms); _connectionRetryTimer.begin(NC_CONNECTION_RETRY_TIMER_ms); _resetInput->begin(); @@ -97,10 +87,10 @@ NetworkConfiguratorStates NetworkConfiguratorClass::update() { _ledFeedback->update(); switch (_state) { + case NetworkConfiguratorStates::READ_STORED_CONFIG: nextState = handleReadStorage (); break; #if ZERO_TOUCH_ENABLED case NetworkConfiguratorStates::ZERO_TOUCH_CONFIG: nextState = handleZeroTouchConfig(); break; #endif - case NetworkConfiguratorStates::READ_STORED_CONFIG: nextState = handleReadStorage (); break; case NetworkConfiguratorStates::WAITING_FOR_CONFIG: nextState = handleWaitingForConf (); break; case NetworkConfiguratorStates::CONNECTING: nextState = handleConnecting (); break; case NetworkConfiguratorStates::CONFIGURED: nextState = handleConfigured (); break; @@ -111,7 +101,7 @@ NetworkConfiguratorStates NetworkConfiguratorClass::update() { if(_state != nextState){ if(nextState == NetworkConfiguratorStates::CONNECTING){ - _connectionTimeout.reload(); + setConnectionTimeoutTimer(); } _state = nextState; } @@ -121,6 +111,9 @@ NetworkConfiguratorStates NetworkConfiguratorClass::update() { * - Arduino MKR WiFi 1010: short the pin 7 to GND until the led turns off * - Arduino GIGA R1 WiFi: short the pin 7 to GND until the led turns off * - Arduino Nano RP2040 Connect: short the pin 2 to 3.3V until the led turns off + * - Portenta H7: short the pin 0 to GND until the led turns off + * - Portenta C33: short the pin 0 to GND until the led turns off + * - Portenta Machine Control: the reset is not available * - Other boards: short the pin 2 to GND until the led turns off */ @@ -391,6 +384,60 @@ bool NetworkConfiguratorClass::handleConnectRequest() { return true; } +void NetworkConfiguratorClass::setConnectionTimeoutTimer() { + uint32_t timeout = 0; + switch (_networkSetting.type) { +#if defined(BOARD_HAS_WIFI) + case NetworkAdapter::WIFI: + timeout = NC_CONNECTION_TIMEOUT_ms; // 15 seconds + break; +#endif + +#if defined(BOARD_HAS_ETHERNET) + case NetworkAdapter::ETHERNET: + timeout = NC_CONNECTION_TIMEOUT_ms; // 15 seconds + break; +#endif + +#if defined(BOARD_HAS_NB) + case NetworkAdapter::NB: + timeout = 2 * NC_CONNECTION_TIMEOUT_ms; // 30 seconds + break; +#endif + +#if defined(BOARD_HAS_GSM) + case NetworkAdapter::GSM: + timeout = 2 * NC_CONNECTION_TIMEOUT_ms; // 30 seconds + break; +#endif + +#if defined(BOARD_HAS_CATM1_NBIOT) + case NetworkAdapter::CATM1: + timeout = 2 * NC_CONNECTION_TIMEOUT_ms; // 30 seconds + break; +#endif + +#if defined(BOARD_HAS_CELLULAR) + case NetworkAdapter::CELL: + timeout = 2 * NC_CONNECTION_TIMEOUT_ms; // 30 seconds + break; +#endif + +#if defined(BOARD_HAS_LORA) + case NetworkAdapter::LORA: + timeout = NC_CONNECTION_TIMEOUT_ms; // 15 seconds + break; +#endif + default: + timeout = NC_CONNECTION_TIMEOUT_ms; // Default to 15 seconds for other adapters + break; + } + + _connectionTimeout.begin(timeout); + _connectionTimeout.reload(); + return; +} + String NetworkConfiguratorClass::decodeConnectionErrorMessage(NetworkConnectionState err, StatusMessage *errorCode) { switch (err) { case NetworkConnectionState::ERROR: @@ -455,10 +502,10 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleZeroTouchConfig() { defaultEthernetSettings(); #endif if (!_connectionHandler->updateSetting(_networkSetting)) { - return NetworkConfiguratorStates::READ_STORED_CONFIG; + return NetworkConfiguratorStates::WAITING_FOR_CONFIG; } _connectionHandlerIstantiated = true; - _connectionTimeout.reload(); + setConnectionTimeoutTimer(); } StatusMessage err; @@ -471,9 +518,10 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleZeroTouchConfig() { sendStatus(StatusMessage::ERROR); } _connectionHandlerIstantiated = false; - return NetworkConfiguratorStates::READ_STORED_CONFIG; + return NetworkConfiguratorStates::WAITING_FOR_CONFIG; } - return NetworkConfiguratorStates::ZERO_TOUCH_CONFIG; + + return handleWaitingForConf(); } #endif @@ -508,13 +556,26 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleReadStorage() { if(credFound && _connectionHandler->updateSetting(_networkSetting)) { _connectionHandlerIstantiated = true; + _configInProgress = _agentsManager->isConfigInProgress(); + if (_configInProgress) { + return NetworkConfiguratorStates::UPDATING_CONFIG; + } return NetworkConfiguratorStates::CONFIGURED; } if (_optionUpdateTimer.getWaitTime() == 0) { scanNetworkOptions(); } + /* + * If the board is zero touch capable and without network configuration, it starts the zero touch configuration mode + * In this state the board will try to connect to the network using a set of + * default network settings ex. Ethernet with DHCP + */ + #if ZERO_TOUCH_ENABLED + return NetworkConfiguratorStates::ZERO_TOUCH_CONFIG; + #else return NetworkConfiguratorStates::WAITING_FOR_CONFIG; + #endif } NetworkConfiguratorStates NetworkConfiguratorClass::handleWaitingForConf() { @@ -547,8 +608,11 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleConnecting() { _agentsManager->update(); //To keep alive the connection with the configurator StatusMessage err; ConnectionResult res = connectToNetwork(&err); - + _configInProgress = _agentsManager->isConfigInProgress(); if (res == ConnectionResult::SUCCESS) { + if (_configInProgress) { + _ledFeedback->setMode(LEDFeedbackClass::LEDFeedbackMode::PEER_CONNECTED); + } return NetworkConfiguratorStates::CONFIGURED; } else if (res == ConnectionResult::FAILED) { sendStatus(err); @@ -560,24 +624,24 @@ NetworkConfiguratorStates NetworkConfiguratorClass::handleConnecting() { } NetworkConfiguratorStates NetworkConfiguratorClass::handleConfigured() { - bool configInprogress = _agentsManager->isConfigInProgress(); - - if (configInprogress) { - _ledFeedback->setMode(LEDFeedbackClass::LEDFeedbackMode::PEER_CONNECTED); - } - _agentsManager->update(); + const bool updatedConfigInprogress = _agentsManager->isConfigInProgress(); + // If the agent manager changes state, it means that user is trying to configure the network, so the network configurator should change state - if (_agentsManager->isConfigInProgress() && !configInprogress) { - scanNetworkOptions(); - return NetworkConfiguratorStates::UPDATING_CONFIG; + if (_configInProgress != updatedConfigInprogress) { + _configInProgress = updatedConfigInprogress; + if (_configInProgress) { + scanNetworkOptions(); + return NetworkConfiguratorStates::UPDATING_CONFIG; + } } return NetworkConfiguratorStates::CONFIGURED; } NetworkConfiguratorStates NetworkConfiguratorClass::handleUpdatingConfig() { - if (_agentsManager->isConfigInProgress() == false) { + _configInProgress = _agentsManager->isConfigInProgress(); + if (_configInProgress == false) { //If peer disconnects without updating the network settings, go to connecting state for check the connection sendStatus(StatusMessage::CONNECTING); return NetworkConfiguratorStates::CONNECTING; diff --git a/src/Arduino_NetworkConfigurator.h b/src/Arduino_NetworkConfigurator.h index e22bf1c..207e5ea 100644 --- a/src/Arduino_NetworkConfigurator.h +++ b/src/Arduino_NetworkConfigurator.h @@ -58,6 +58,9 @@ enum class NetworkConfiguratorStates { ZERO_TOUCH_CONFIG, * - Arduino MKR WiFi 1010: short the pin 7 to GND until the led turns off * - Arduino GIGA R1 WiFi: short the pin 7 to GND until the led turns off * - Arduino Nano RP2040 Connect: short the pin 2 to 3.3V until the led turns off + * - Portenta H7: short the pin 0 to GND until the led turns off + * - Portenta C33: short the pin 0 to GND until the led turns off + * - Portenta Machine Control: the reset is not available * - Other boards: short the pin 2 to GND until the led turns off * */ @@ -162,6 +165,7 @@ class NetworkConfiguratorClass { ConnectionHandler *_connectionHandler; static inline models::NetworkSetting _networkSetting; bool _connectionHandlerIstantiated; + bool _configInProgress; ResetInput *_resetInput; LEDFeedbackClass *_ledFeedback; /* Timeout instances */ @@ -203,6 +207,9 @@ class NetworkConfiguratorClass { void startReconfigureProcedure(); + // Returns the connection timeout in milliseconds according to the set network type + void setConnectionTimeoutTimer(); + String decodeConnectionErrorMessage(NetworkConnectionState err, StatusMessage *errorCode); ConnectionResult connectToNetwork(StatusMessage *err); ConnectionResult disconnectFromNetwork(); diff --git a/src/configuratorAgents/AgentsManager.cpp b/src/configuratorAgents/AgentsManager.cpp index 71f8f34..edc3d14 100644 --- a/src/configuratorAgents/AgentsManager.cpp +++ b/src/configuratorAgents/AgentsManager.cpp @@ -239,7 +239,15 @@ AgentsManagerStates AgentsManagerClass::handleInit() { (*agent)->end(); } } - LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::PEER_CONNECTED); + + if(_selectedAgent->getAgentType() == ConfiguratorAgent::AgentTypes::BLE) { + LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::PEER_CONNECTED_BLE); + } else if(_selectedAgent->getAgentType() == ConfiguratorAgent::AgentTypes::USB_SERIAL) { + LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::PEER_CONNECTED_SERIAL); + } else { + LEDFeedbackClass::getInstance().setMode(LEDFeedbackClass::LEDFeedbackMode::PEER_CONNECTED); + } + } return nextState; } @@ -308,6 +316,7 @@ void AgentsManagerClass::updateProgressRequest(MessageOutputType type) { case MessageOutputType::WIFI_FW_VERSION: key = RequestType::GET_WIFI_FW_VERSION ; break; case MessageOutputType::PROV_SKETCH_VERSION: key = RequestType::GET_PROVISIONING_SKETCH_VERSION; break; case MessageOutputType::NETCONFIG_LIB_VERSION: key = RequestType::GET_NETCONFIG_LIB_VERSION ; break; + case MessageOutputType::PROV_PUBLIC_KEY: key = RequestType::GET_ID ; break; } if (key == RequestType::NONE) { @@ -316,7 +325,7 @@ void AgentsManagerClass::updateProgressRequest(MessageOutputType type) { if( key == RequestType::GET_ID && _statusRequest.key == key && _statusRequest.pending){ _statusRequest.completion++; - if(_statusRequest.completion == 2){ + if(_statusRequest.completion == 3){ _statusRequest.reset(); } return; diff --git a/src/configuratorAgents/MessagesDefinitions.h b/src/configuratorAgents/MessagesDefinitions.h index 2f2d460..de456c8 100644 --- a/src/configuratorAgents/MessagesDefinitions.h +++ b/src/configuratorAgents/MessagesDefinitions.h @@ -61,6 +61,7 @@ enum class MessageOutputType { STATUS, WIFI_FW_VERSION, PROV_SKETCH_VERSION, NETCONFIG_LIB_VERSION, + PROV_PUBLIC_KEY }; /* Types of ingoing messages */ @@ -86,6 +87,7 @@ struct ProvisioningOutputMessage { const char *wifiFwVersion; const char *provSketchVersion; const char *netConfigLibVersion; + const char *provPublicKey; } m; }; diff --git a/src/configuratorAgents/agents/boardConfigurationProtocol/BoardConfigurationProtocol.cpp b/src/configuratorAgents/agents/boardConfigurationProtocol/BoardConfigurationProtocol.cpp index e0d856f..764c5ce 100644 --- a/src/configuratorAgents/agents/boardConfigurationProtocol/BoardConfigurationProtocol.cpp +++ b/src/configuratorAgents/agents/boardConfigurationProtocol/BoardConfigurationProtocol.cpp @@ -75,6 +75,9 @@ bool BoardConfigurationProtocol::sendMsg(ProvisioningOutputMessage &msg) { case MessageOutputType::NETCONFIG_LIB_VERSION: res = sendVersion(msg.m.netConfigLibVersion, msg.type); break; + case MessageOutputType::PROV_PUBLIC_KEY: + res = sendProvPublicKey(msg.m.provPublicKey, strlen(msg.m.provPublicKey)); + break; default: break; } @@ -294,6 +297,23 @@ bool BoardConfigurationProtocol::sendJwt(const char *jwt, size_t len) { return res; } +bool BoardConfigurationProtocol::sendProvPublicKey(const char *provPublicKey, size_t len) { + + size_t cborDataLen = CBOR_MIN_PROV_PUBIC_KEY_LEN + len; + uint8_t data[cborDataLen]; + + if (!CBORAdapter::provPublicKeyToCBOR(provPublicKey, data, &cborDataLen)) { + return false; + } + + if (!sendData(PacketManager::MessageType::DATA, data, cborDataLen)) { + DEBUG_WARNING("BoardConfigurationProtocol::%s failed to send JWT", __FUNCTION__); + return false; + } + + return true; +} + bool BoardConfigurationProtocol::sendBleMacAddress(const uint8_t *mac, size_t len) { bool res = false; if (len != BLE_MAC_ADDRESS_SIZE) { diff --git a/src/configuratorAgents/agents/boardConfigurationProtocol/BoardConfigurationProtocol.h b/src/configuratorAgents/agents/boardConfigurationProtocol/BoardConfigurationProtocol.h index 3a82291..e206f62 100644 --- a/src/configuratorAgents/agents/boardConfigurationProtocol/BoardConfigurationProtocol.h +++ b/src/configuratorAgents/agents/boardConfigurationProtocol/BoardConfigurationProtocol.h @@ -96,6 +96,7 @@ class BoardConfigurationProtocol { bool sendNetworkOptions(const NetworkOptions *netOptions); bool sendUhwid(const byte *uhwid); bool sendJwt(const char *jwt, size_t len); + bool sendProvPublicKey(const char *provPublicKey, size_t len); bool sendBleMacAddress(const uint8_t *mac, size_t len); bool sendVersion(const char *version, MessageOutputType type); TransmissionResult transmitStream(); diff --git a/src/configuratorAgents/agents/boardConfigurationProtocol/CBORAdapter.cpp b/src/configuratorAgents/agents/boardConfigurationProtocol/CBORAdapter.cpp index 96fcbf5..0f7a5a4 100644 --- a/src/configuratorAgents/agents/boardConfigurationProtocol/CBORAdapter.cpp +++ b/src/configuratorAgents/agents/boardConfigurationProtocol/CBORAdapter.cpp @@ -68,6 +68,20 @@ bool CBORAdapter::BLEMacAddressToCBOR(const uint8_t *mac, uint8_t *data, size_t return status == MessageEncoder::Status::Complete ? true : false; } +bool CBORAdapter::provPublicKeyToCBOR(const char *provPublicKey, uint8_t *data, size_t *len) { + CBORMessageEncoder encoder; + if(*len < CBOR_MIN_PROV_PUBIC_KEY_LEN + strlen(provPublicKey)) { + return false; + } + ProvPublicKeyProvisioningMessage provPublicKeyMsg; + provPublicKeyMsg.c.id = ProvisioningMessageId::ProvPublicKeyProvisioningMessageId; + provPublicKeyMsg.provPublicKey = provPublicKey; + + MessageEncoder::Status status = encoder.encode((Message *)&provPublicKeyMsg, data, *len); + + return status == MessageEncoder::Status::Complete ? true : false; +} + bool CBORAdapter::statusToCBOR(StatusMessage msg, uint8_t *data, size_t *len) { bool result = false; @@ -87,9 +101,9 @@ bool CBORAdapter::wifiFWVersionToCBOR(const char *wifiFWVersion, uint8_t *data, if(*len < CBOR_MIN_WIFI_FW_VERSION_LEN + strlen(wifiFWVersion)) { return false; } - WiFiFWVersionProvisioningMessage wifiFWVersionMsg; - wifiFWVersionMsg.c.id = ProvisioningMessageId::WiFiFWVersionProvisioningMessageId; - wifiFWVersionMsg.wifiFwVersion = wifiFWVersion; + VersionMessage wifiFWVersionMsg; + wifiFWVersionMsg.c.id = StandardMessageId::WiFiFWVersionMessageId; + wifiFWVersionMsg.params.version = wifiFWVersion; MessageEncoder::Status status = encoder.encode((Message *)&wifiFWVersionMsg, data, *len); diff --git a/src/configuratorAgents/agents/boardConfigurationProtocol/CBORAdapter.h b/src/configuratorAgents/agents/boardConfigurationProtocol/CBORAdapter.h index 09aeef8..5ec177b 100644 --- a/src/configuratorAgents/agents/boardConfigurationProtocol/CBORAdapter.h +++ b/src/configuratorAgents/agents/boardConfigurationProtocol/CBORAdapter.h @@ -21,11 +21,13 @@ #define CBOR_MIN_WIFI_FW_VERSION_LEN CBOR_DATA_HEADER_LEN + 1 // CBOR_DATA_HEADER_LEN + 1 byte for the length of the string #define CBOR_MIN_PROV_SKETCH_VERSION_LEN CBOR_DATA_HEADER_LEN + 1 // CBOR_DATA_HEADER_LEN + 1 byte for the length of the string #define CBOR_MIN_NETCONFIG_LIB_VERSION_LEN CBOR_DATA_HEADER_LEN + 1 // CBOR_DATA_HEADER_LEN + 1 byte for the length of the string +#define CBOR_MIN_PROV_PUBIC_KEY_LEN CBOR_DATA_HEADER_LEN + 3 // CBOR_DATA_HEADER_LEN + 2 bytes for the length of the string + 1 byte for the type of the string class CBORAdapter { public: static bool uhwidToCBOR(const byte *uhwid, uint8_t *data, size_t *len); static bool jwtToCBOR(const char *jwt, uint8_t *data, size_t *len); + static bool provPublicKeyToCBOR(const char *provPublicKey, uint8_t *data, size_t *len); static bool BLEMacAddressToCBOR(const uint8_t *mac, uint8_t *data, size_t *len); static bool wifiFWVersionToCBOR(const char *wifiFWVersion, uint8_t *data, size_t *len); static bool provSketchVersionToCBOR(const char *provSketchVersion, uint8_t *data, size_t *len); diff --git a/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/CBORInstances.h b/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/CBORInstances.h index b726e0f..2442db1 100644 --- a/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/CBORInstances.h +++ b/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/CBORInstances.h @@ -7,8 +7,8 @@ static StatusProvisioningMessageEncoder statusProvisioningMessageEnc static ListWifiNetworksProvisioningMessageEncoder listWifiNetworksProvisioningMessageEncoder; static UniqueHardwareIdProvisioningMessageEncoder uniqueHardwareIdProvisioningMessageEncoder; static JWTProvisioningMessageEncoder jWTProvisioningMessageEncoder; +static ProvPublicKeyProvisioningMessageEncoder provPublicKeyProvisioningMessageEncoder; static BLEMacAddressProvisioningMessageEncoder bLEMacAddressProvisioningMessageEncoder; -static WiFiFWVersionProvisioningMessageEncoder wiFiFWVersionProvisioningMessageEncoder; static ProvSketchVersionProvisioningMessageEncoder provSketchVersionProvisioningMessageEncoder; static NetConfigLibVersProvisioningMessageEncoder netConfigLibVersProvisioningMessageEncoder; diff --git a/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/Decoder.cpp b/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/Decoder.cpp index bac0846..9bc5c40 100644 --- a/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/Decoder.cpp +++ b/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/Decoder.cpp @@ -10,35 +10,11 @@ #include "Decoder.h" #include +#include #if defined(BOARD_HAS_ETHERNET) #include #endif -// FIXME move this utility functions -static bool copyCBORStringToArray(CborValue * param, char * dest, size_t dest_size) { - if (cbor_value_is_text_string(param)) { - // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string - if(_cbor_value_copy_string(param, dest, &dest_size, NULL) == CborNoError) { - return true; - } - } - - return false; -} - -// FIXME dest_size should be also returned, the copied byte array can have a different size from the starting one -// for the time being we need this on SHA256 only -static bool copyCBORByteToArray(CborValue * param, uint8_t * dest, size_t dest_size) { - if (cbor_value_is_byte_string(param)) { - // NOTE: keep in mind that _cbor_value_copy_string tries to put a \0 at the end of the string - if(_cbor_value_copy_string(param, dest, &dest_size, NULL) == CborNoError) { - return true; - } - } - - return false; -} - MessageDecoder::Status TimestampProvisioningMessageDecoder::decode(CborValue* param, Message* message) { TimestampProvisioningMessage* ts = (TimestampProvisioningMessage*) message; @@ -56,8 +32,9 @@ MessageDecoder::Status TimestampProvisioningMessageDecoder::decode(CborValue* pa MessageDecoder::Status WifiConfigProvisioningMessageDecoder::decode(CborValue* param, Message* message) { NetworkConfigProvisioningMessage* provisioningNetworkConfig = (NetworkConfigProvisioningMessage*) message; memset(&provisioningNetworkConfig->networkSetting, 0x00, sizeof(models::NetworkSetting)); + size_t ssidSize = sizeof(provisioningNetworkConfig->networkSetting.wifi.ssid); // Message is composed of 2 parameters: ssid and password - if (!copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.wifi.ssid, sizeof(provisioningNetworkConfig->networkSetting.wifi.ssid))) { + if (cbor::utils::copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.wifi.ssid, ssidSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } @@ -66,7 +43,8 @@ MessageDecoder::Status WifiConfigProvisioningMessageDecoder::decode(CborValue* p return MessageDecoder::Status::Error; } - if (!copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.wifi.pwd, sizeof(provisioningNetworkConfig->networkSetting.wifi.pwd))) { + size_t pwdSize = sizeof(provisioningNetworkConfig->networkSetting.wifi.pwd); + if (cbor::utils::copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.wifi.pwd, pwdSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } provisioningNetworkConfig->networkSetting.type = NetworkAdapter::WIFI; @@ -92,8 +70,9 @@ MessageDecoder::Status CommandsProvisioningMessageDecoder::decode(CborValue* par MessageDecoder::Status LoRaConfigProvisioningMessageDecoder::decode(CborValue* param, Message* message) { NetworkConfigProvisioningMessage* provisioningNetworkConfig = (NetworkConfigProvisioningMessage*) message; memset(&provisioningNetworkConfig->networkSetting, 0x00, sizeof(models::NetworkSetting)); + size_t appeuiSize = sizeof(provisioningNetworkConfig->networkSetting.lora.appeui); // Message is composed of 5 parameters: app_eui, app_key, band, channel_mask, device_class - if (!copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.lora.appeui, sizeof(provisioningNetworkConfig->networkSetting.lora.appeui))) { + if (cbor::utils::copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.lora.appeui, appeuiSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } @@ -102,7 +81,8 @@ MessageDecoder::Status LoRaConfigProvisioningMessageDecoder::decode(CborValue* p return MessageDecoder::Status::Error; } - if (!copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.lora.appkey, sizeof(provisioningNetworkConfig->networkSetting.lora.appkey))) { + size_t appkeySize = sizeof(provisioningNetworkConfig->networkSetting.lora.appkey); + if (cbor::utils::copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.lora.appkey, appkeySize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } @@ -127,7 +107,8 @@ MessageDecoder::Status LoRaConfigProvisioningMessageDecoder::decode(CborValue* p return MessageDecoder::Status::Error; } - if(!copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.lora.channelMask, sizeof(provisioningNetworkConfig->networkSetting.lora.channelMask))) { + size_t channelMaskSize = sizeof(provisioningNetworkConfig->networkSetting.lora.channelMask); + if(cbor::utils::copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.lora.channelMask, channelMaskSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } @@ -138,7 +119,9 @@ MessageDecoder::Status LoRaConfigProvisioningMessageDecoder::decode(CborValue* p char deviceClass[LORA_DEVICE_CLASS_SIZE]; memset(deviceClass, 0x00, sizeof(deviceClass)); - if (!copyCBORStringToArray(param, deviceClass, sizeof(deviceClass))) { + + size_t deviceClassSize = sizeof(deviceClass); + if (cbor::utils::copyCBORStringToArray(param, deviceClass, deviceClassSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } @@ -159,8 +142,9 @@ MessageDecoder::Status CATM1ConfigProvisioningMessageDecoder::decode(CborValue* CborValue array_iter; size_t arrayLength = 0; + size_t pinSize = sizeof(provisioningNetworkConfig->networkSetting.catm1.pin); // Message is composed of 5 parameters: pin, band, apn, login and password - if (!copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.catm1.pin, sizeof(provisioningNetworkConfig->networkSetting.catm1.pin))) { + if (cbor::utils::copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.catm1.pin, pinSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } @@ -208,7 +192,8 @@ MessageDecoder::Status CATM1ConfigProvisioningMessageDecoder::decode(CborValue* return MessageDecoder::Status::Error; } - if (!copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.catm1.apn, sizeof(provisioningNetworkConfig->networkSetting.catm1.apn))) { + size_t apnSize = sizeof(provisioningNetworkConfig->networkSetting.catm1.apn); + if (cbor::utils::copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.catm1.apn, apnSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } @@ -217,7 +202,8 @@ MessageDecoder::Status CATM1ConfigProvisioningMessageDecoder::decode(CborValue* return MessageDecoder::Status::Error; } - if (!copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.catm1.login, sizeof(provisioningNetworkConfig->networkSetting.catm1.login))) { + size_t loginSize = sizeof(provisioningNetworkConfig->networkSetting.catm1.login); + if (cbor::utils::copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.catm1.login, loginSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } @@ -226,7 +212,8 @@ MessageDecoder::Status CATM1ConfigProvisioningMessageDecoder::decode(CborValue* return MessageDecoder::Status::Error; } - if (!copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.catm1.pass, sizeof(provisioningNetworkConfig->networkSetting.catm1.pass))) { + size_t passSize = sizeof(provisioningNetworkConfig->networkSetting.catm1.pass); + if (cbor::utils::copyCBORStringToArray(param, provisioningNetworkConfig->networkSetting.catm1.pass, passSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } @@ -343,8 +330,9 @@ MessageDecoder::Status EthernetConfigProvisioningMessageDecoder::decode(CborValu #if defined(BOARD_HAS_NB) || defined(BOARD_HAS_GSM) ||defined(BOARD_HAS_CELLULAR) static inline MessageDecoder::Status extractCellularFields(CborValue* param, models::CellularSetting* cellSetting) { + size_t pinSize = sizeof(cellSetting->pin); // Message is composed of 4 parameters: pin, apn, login and password - if (!copyCBORStringToArray(param, cellSetting->pin, sizeof(cellSetting->pin))) { + if (cbor::utils::copyCBORStringToArray(param, cellSetting->pin, pinSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } @@ -353,7 +341,8 @@ static inline MessageDecoder::Status extractCellularFields(CborValue* param, mod return MessageDecoder::Status::Error; } - if (!copyCBORStringToArray(param, cellSetting->apn, sizeof(cellSetting->apn))) { + size_t apnSize = sizeof(cellSetting->apn); + if (cbor::utils::copyCBORStringToArray(param, cellSetting->apn, apnSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } @@ -362,7 +351,8 @@ static inline MessageDecoder::Status extractCellularFields(CborValue* param, mod return MessageDecoder::Status::Error; } - if (!copyCBORStringToArray(param, cellSetting->login, sizeof(cellSetting->login))) { + size_t loginSize = sizeof(cellSetting->login); + if (cbor::utils::copyCBORStringToArray(param, cellSetting->login, loginSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } @@ -371,7 +361,8 @@ static inline MessageDecoder::Status extractCellularFields(CborValue* param, mod return MessageDecoder::Status::Error; } - if (!copyCBORStringToArray(param, cellSetting->pass, sizeof(cellSetting->pass))) { + size_t passSize = sizeof(cellSetting->pass); + if (cbor::utils::copyCBORStringToArray(param, cellSetting->pass, passSize) == MessageDecoder::Status::Error) { return MessageDecoder::Status::Error; } diff --git a/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/Encoder.cpp b/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/Encoder.cpp index fd0bb07..c688de2 100644 --- a/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/Encoder.cpp +++ b/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/Encoder.cpp @@ -116,15 +116,15 @@ MessageEncoder::Status BLEMacAddressProvisioningMessageEncoder::encode(CborEncod return MessageEncoder::Status::Complete; } -MessageEncoder::Status WiFiFWVersionProvisioningMessageEncoder::encode(CborEncoder* encoder, Message *msg) { - WiFiFWVersionProvisioningMessage * provisioningWiFiFWVersion = (WiFiFWVersionProvisioningMessage*) msg; +MessageEncoder::Status ProvSketchVersionProvisioningMessageEncoder::encode(CborEncoder* encoder, Message *msg) { + ProvSketchVersionProvisioningMessage * provisioningSketchVersion = (ProvSketchVersionProvisioningMessage*) msg; CborEncoder array_encoder; if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) { return MessageEncoder::Status::Error; } - if(cbor_encode_text_stringz(&array_encoder, provisioningWiFiFWVersion->wifiFwVersion) != CborNoError) { + if(cbor_encode_text_stringz(&array_encoder, provisioningSketchVersion->provSketchVersion) != CborNoError) { return MessageEncoder::Status::Error; } @@ -135,15 +135,15 @@ MessageEncoder::Status WiFiFWVersionProvisioningMessageEncoder::encode(CborEncod return MessageEncoder::Status::Complete; } -MessageEncoder::Status ProvSketchVersionProvisioningMessageEncoder::encode(CborEncoder* encoder, Message *msg) { - ProvSketchVersionProvisioningMessage * provisioningSketchVersion = (ProvSketchVersionProvisioningMessage*) msg; +MessageEncoder::Status NetConfigLibVersProvisioningMessageEncoder::encode(CborEncoder* encoder, Message *msg) { + NetConfigLibVersionProvisioningMessage * netConfigLibVersion = (NetConfigLibVersionProvisioningMessage*) msg; CborEncoder array_encoder; if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) { return MessageEncoder::Status::Error; } - if(cbor_encode_text_stringz(&array_encoder, provisioningSketchVersion->provSketchVersion) != CborNoError) { + if(cbor_encode_text_stringz(&array_encoder, netConfigLibVersion->netConfigLibVersion) != CborNoError) { return MessageEncoder::Status::Error; } @@ -154,15 +154,15 @@ MessageEncoder::Status ProvSketchVersionProvisioningMessageEncoder::encode(CborE return MessageEncoder::Status::Complete; } -MessageEncoder::Status NetConfigLibVersProvisioningMessageEncoder::encode(CborEncoder* encoder, Message *msg) { - NetConfigLibVersionProvisioningMessage * netConfigLibVersion = (NetConfigLibVersionProvisioningMessage*) msg; +MessageEncoder::Status ProvPublicKeyProvisioningMessageEncoder::encode(CborEncoder *encoder, Message *msg) { + ProvPublicKeyProvisioningMessage * provisioningProvPublicKey = (ProvPublicKeyProvisioningMessage*) msg; CborEncoder array_encoder; if(cbor_encoder_create_array(encoder, &array_encoder, 1) != CborNoError) { return MessageEncoder::Status::Error; } - if(cbor_encode_text_stringz(&array_encoder, netConfigLibVersion->netConfigLibVersion) != CborNoError) { + if(cbor_encode_text_stringz(&array_encoder, provisioningProvPublicKey->provPublicKey) != CborNoError) { return MessageEncoder::Status::Error; } diff --git a/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/Encoder.h b/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/Encoder.h index fd02dfa..03ca489 100644 --- a/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/Encoder.h +++ b/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/Encoder.h @@ -52,14 +52,6 @@ class BLEMacAddressProvisioningMessageEncoder: public CBORMessageEncoderInterfac MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override; }; -class WiFiFWVersionProvisioningMessageEncoder: public CBORMessageEncoderInterface { -public: - WiFiFWVersionProvisioningMessageEncoder() - : CBORMessageEncoderInterface(CBORWiFiFWVersionProvisioningMessage, WiFiFWVersionProvisioningMessageId) {} -protected: - MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override; -}; - class ProvSketchVersionProvisioningMessageEncoder: public CBORMessageEncoderInterface { public: ProvSketchVersionProvisioningMessageEncoder() @@ -75,3 +67,11 @@ class NetConfigLibVersProvisioningMessageEncoder: public CBORMessageEncoderInter protected: MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override; }; + +class ProvPublicKeyProvisioningMessageEncoder: public CBORMessageEncoderInterface { + public: + ProvPublicKeyProvisioningMessageEncoder() + : CBORMessageEncoderInterface(CBORProvPublicKeyProvisioningMessage, ProvPublicKeyProvisioningMessageId) {} + protected: + MessageEncoder::Status encode(CborEncoder* encoder, Message *msg) override; + }; diff --git a/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/ProvisioningMessage.h b/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/ProvisioningMessage.h index 240a7b5..51d2395 100644 --- a/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/ProvisioningMessage.h +++ b/src/configuratorAgents/agents/boardConfigurationProtocol/cbor/ProvisioningMessage.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -47,9 +48,9 @@ enum CBORProvisioningMessageTag: CBORTag { CBORUniqueHardwareIdProvisioningMessage = 0x012010, CBORJWTProvisioningMessage = 0x012011, CBORBLEMacAddressProvisioningMessage = 0x012013, - CBORWiFiFWVersionProvisioningMessage = 0x012014, CBORProvSketchVersionProvisioningMessage = 0x012015, CBORNetConfigLibVersProvisioningMessage = 0x012016, + CBORProvPublicKeyProvisioningMessage = 0x012017, }; enum ProvisioningMessageId: MessageId { @@ -58,10 +59,10 @@ enum ProvisioningMessageId: MessageId { ListWifiNetworksProvisioningMessageId, UniqueHardwareIdProvisioningMessageId, BLEMacAddressProvisioningMessageId, - WiFiFWVersionProvisioningMessageId, ProvSketchVersionProvisioningMessageId, NetConfigLibVersProvisioningMessageId, JWTProvisioningMessageId, + ProvPublicKeyProvisioningMessageId, TimestampProvisioningMessageId, CommandsProvisioningMessageId, WifiConfigProvisioningMessageId, @@ -109,17 +110,17 @@ struct JWTProvisioningMessage { }; }; -struct BLEMacAddressProvisioningMessage { +struct ProvPublicKeyProvisioningMessage { ProvisioningMessage c; struct { - uint8_t macAddress[BLE_MAC_ADDRESS_SIZE]; + const char *provPublicKey; //The payload is a string. }; }; -struct WiFiFWVersionProvisioningMessage { +struct BLEMacAddressProvisioningMessage { ProvisioningMessage c; struct { - const char *wifiFwVersion; //The payload is a string. + uint8_t macAddress[BLE_MAC_ADDRESS_SIZE]; }; }; diff --git a/src/utility/LEDFeedback.cpp b/src/utility/LEDFeedback.cpp index ca670b5..2ae9dc7 100644 --- a/src/utility/LEDFeedback.cpp +++ b/src/utility/LEDFeedback.cpp @@ -18,7 +18,15 @@ #ifdef BOARD_HAS_LED_MATRIX #include "Arduino_LED_Matrix.h" -ArduinoLEDMatrix matrix; +ArduinoLEDMatrix ledMatrixAnimationHandler; + +const uint32_t usb_image[3] = { + 0x3c, + 0x267fe0, + 0x160e0000, + +}; + const uint32_t bluetooth[3] = { 0x401600d, @@ -28,27 +36,21 @@ const uint32_t bluetooth[3] = { const uint32_t cloud[][4] = { { - 0xc013, - 0x82644424, - 0x24023fc, - 66 - }, - { - 0xc013, - 0x82644424, - 0x24023fc, - 66 + 0x1c023, + 0x4484044, + 0x43f8000, + 500 }, { - 0xc013, - 0x82644824, - 0x24023fc, - 66 + 0xe011, + 0x82242022, + 0x21fc000, + 500 } }; const uint32_t snake[][4] = { - { 0xffffffff, 0xffffffff, 0xffffffff, 1300 }, + { 0xffffffff, 0xffffffff, 0xffffffff, 2000 }, { 0x7fffffff, 0xffffffff, 0xfffff7ff, 66 }, { 0x3fe7ffff, 0xffffffff, 0xff7ff3fe, 66 }, { 0x1fc3fe7f, 0xfffffff7, 0xff3fe1fc, 66 }, @@ -158,7 +160,7 @@ void LEDFeedbackClass::begin() { #endif #ifdef BOARD_HAS_LED_MATRIX - matrix.begin(); + ledMatrixAnimationHandler.begin(); #endif } @@ -179,7 +181,7 @@ void LEDFeedbackClass::setMode(LEDFeedbackMode mode) { { _ledChangeInterval = 0; #ifdef BOARD_HAS_LED_MATRIX - matrix.clear(); + ledMatrixAnimationHandler.clear(); #endif } break; @@ -192,8 +194,11 @@ void LEDFeedbackClass::setMode(LEDFeedbackMode mode) { #endif #ifdef BOARD_HAS_LED_MATRIX _framePtr = nullptr; - matrix.loadSequence(snake); - matrix.play(true); + ledMatrixAnimationHandler.loadSequence(snake); + ledMatrixAnimationHandler.play(true); + /* For fixing the issue that the first + * frame of the first run is not shown */ + ledMatrixAnimationHandler.loadSequence(snake); #endif _ledChangeInterval = HEARTBEAT_INTERVAL; _count = 0; @@ -201,17 +206,25 @@ void LEDFeedbackClass::setMode(LEDFeedbackMode mode) { break; case LEDFeedbackMode::PEER_CONNECTED: { - #ifdef BOARD_HAS_RGB - turnOFF(); - _ledPin = BLUE_LED; - #else - _ledPin = GREEN_LED; - #endif + configurePeerConnectedMode(); + } + break; + case LEDFeedbackMode::PEER_CONNECTED_BLE: + { + configurePeerConnectedMode(); #ifdef BOARD_HAS_LED_MATRIX - matrix.loadFrame(bluetooth); + ledMatrixAnimationHandler.loadFrame(bluetooth); _framePtr = (uint32_t*)bluetooth; #endif - _ledChangeInterval = ALWAYS_ON_INTERVAL; + } + break; + case LEDFeedbackMode::PEER_CONNECTED_SERIAL: + { + configurePeerConnectedMode(); + #ifdef BOARD_HAS_LED_MATRIX + ledMatrixAnimationHandler.loadFrame(usb_image); + _framePtr = (uint32_t*)usb_image; + #endif } break; case LEDFeedbackMode::CONNECTING_TO_NETWORK: @@ -222,8 +235,8 @@ void LEDFeedbackClass::setMode(LEDFeedbackMode mode) { _ledPin = GREEN_LED; #ifdef BOARD_HAS_LED_MATRIX _framePtr = nullptr; - matrix.loadSequence(LEDMATRIX_ANIMATION_WIFI_SEARCH); - matrix.play(true); + ledMatrixAnimationHandler.loadSequence(LEDMATRIX_ANIMATION_WIFI_SEARCH); + ledMatrixAnimationHandler.play(true); #endif _ledChangeInterval = ALWAYS_ON_INTERVAL; @@ -236,8 +249,8 @@ void LEDFeedbackClass::setMode(LEDFeedbackMode mode) { #endif #ifdef BOARD_HAS_LED_MATRIX _framePtr = nullptr; - matrix.loadSequence(cloud); - matrix.play(true); + ledMatrixAnimationHandler.loadSequence(cloud); + ledMatrixAnimationHandler.play(true); #endif _ledPin = GREEN_LED; _ledChangeInterval = SLOWBLINK_INTERVAL; @@ -255,7 +268,7 @@ void LEDFeedbackClass::setMode(LEDFeedbackMode mode) { #endif #ifdef BOARD_HAS_LED_MATRIX _framePtr = (uint32_t*)LEDMATRIX_EMOJI_SAD; - matrix.loadFrame(LEDMATRIX_EMOJI_SAD); + ledMatrixAnimationHandler.loadFrame(LEDMATRIX_EMOJI_SAD); #endif } break; @@ -324,7 +337,7 @@ void LEDFeedbackClass::turnOFF() { #endif #ifdef BOARD_HAS_LED_MATRIX if(_framePtr != nullptr){ - matrix.clear(); + ledMatrixAnimationHandler.clear(); } #endif @@ -339,10 +352,20 @@ void LEDFeedbackClass::turnON() { #endif #ifdef BOARD_HAS_LED_MATRIX if(_framePtr != nullptr){ - matrix.loadFrame(_framePtr); + ledMatrixAnimationHandler.loadFrame(_framePtr); } #endif _ledState = true; } +void LEDFeedbackClass::configurePeerConnectedMode() { + #ifdef BOARD_HAS_RGB + turnOFF(); + _ledPin = BLUE_LED; + #else + _ledPin = GREEN_LED; + #endif + _ledChangeInterval = ALWAYS_ON_INTERVAL; +} + #endif // NETWORK_CONFIGURATOR_COMPATIBLE diff --git a/src/utility/LEDFeedback.h b/src/utility/LEDFeedback.h index 2360e04..46d08ba 100644 --- a/src/utility/LEDFeedback.h +++ b/src/utility/LEDFeedback.h @@ -16,6 +16,8 @@ class LEDFeedbackClass { NONE, BLE_AVAILABLE, PEER_CONNECTED, + PEER_CONNECTED_BLE, + PEER_CONNECTED_SERIAL, CONNECTING_TO_NETWORK, CONNECTED_TO_CLOUD, ERROR @@ -30,6 +32,7 @@ class LEDFeedbackClass { LEDFeedbackClass() {}; void turnON(); void turnOFF(); + void configurePeerConnectedMode(); LEDFeedbackMode _mode = LEDFeedbackMode::NONE; uint32_t _lastUpdate = 0; uint32_t _count = 0; diff --git a/src/utility/ResetInput.cpp b/src/utility/ResetInput.cpp index 0df5fc0..70d322e 100644 --- a/src/utility/ResetInput.cpp +++ b/src/utility/ResetInput.cpp @@ -11,6 +11,28 @@ #include "ResetInput.h" #include "utility/LEDFeedback.h" +#if defined(ARDUINO_PORTENTA_H7_M7) +#include + + +bool isPortentaMachineControlAttached() { + Wire.begin(); + Wire.beginTransmission(I2C_ADD_DETECT_MACHINE_CONTROL_1); + if (Wire.endTransmission() != 0) { + return false; + } + + Wire.beginTransmission(I2C_ADD_DETECT_MACHINE_CONTROL_2); + if (Wire.endTransmission() != 0) { + return false; + } + + Wire.end(); + return true; +} + +#endif + ResetInput &ResetInput::getInstance() { static ResetInput instance; return instance; @@ -25,6 +47,12 @@ ResetInput::ResetInput() { } void ResetInput::begin() { +#if defined(ARDUINO_PORTENTA_H7_M7) + if(isPortentaMachineControlAttached()) { + return; // Portenta Machine Control is not supported + } +#endif + if(_pin == DISABLE_PIN){ return; } @@ -35,7 +63,6 @@ void ResetInput::begin() { #endif pinMode(LED_RECONFIGURE, OUTPUT); digitalWrite(LED_RECONFIGURE, LED_OFF); - attachInterrupt(digitalPinToInterrupt(_pin),_pressedCallback, CHANGE); } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy