From ab5bff499d160f795b5de051c667ac6f01822bb1 Mon Sep 17 00:00:00 2001 From: Ewerton Scaboro da Silva Date: Mon, 12 Sep 2022 14:37:28 -0700 Subject: [PATCH] Change ADU client update id to match service specification (#242) * Change ADU client update id to match service specification Please refer to Azure/azure-sdk-for-c#2331 for details. * Code style fixes * Fix NXP and simulated examples * Code style fixes --- .../main/azure_iot_freertos_esp32_pnp_model.c | 40 +++++++++++-------- .../sample_azure_iot_adu.c | 22 +++++----- .../sample_azure_iot_pnp_simulated_data.c | 12 ++---- libs/azure-iot-middleware-freertos | 2 +- 4 files changed, 37 insertions(+), 39 deletions(-) diff --git a/demos/projects/ESPRESSIF/adu/main/azure_iot_freertos_esp32_pnp_model.c b/demos/projects/ESPRESSIF/adu/main/azure_iot_freertos_esp32_pnp_model.c index 378ab194..6757e082 100644 --- a/demos/projects/ESPRESSIF/adu/main/azure_iot_freertos_esp32_pnp_model.c +++ b/demos/projects/ESPRESSIF/adu/main/azure_iot_freertos_esp32_pnp_model.c @@ -14,7 +14,9 @@ #include "sample_adu_jws.h" +#include "demo_config.h" #include "sample_azure_iot_pnp_data_if.h" + /*-----------------------------------------------------------*/ #define INDEFINITE_TIME ( ( time_t ) -1 ) @@ -128,23 +130,26 @@ static void prvSkipPropertyAndValue( AzureIoTJSONReader_t * pxReader ) } /*-----------------------------------------------------------*/ -static bool prvIsUpdateAlreadyInstalled( const AzureIoTADUUpdateRequest_t * pxAduUpdateRequest ) +/** + * @brief Verifies if the current image version matches the "installedCriteria" version in the + * installation step of the ADU Update Manifest. + * + * @param pxAduUpdateRequest Parsed update request, with the ADU update manifest. + * @return true If the current image version matches the installedCriteria. + * @return false If the current image version does not match the installedCriteria. + */ +static bool prvDoesInstalledCriteriaMatchCurrentVersion( const AzureIoTADUUpdateRequest_t * pxAduUpdateRequest ) { - if( ( pxAduUpdateRequest->xUpdateManifest.xUpdateId.ulNameLength == - xADUDeviceProperties.xCurrentUpdateId.ulNameLength ) && - ( strncmp( ( const char * ) pxAduUpdateRequest->xUpdateManifest.xUpdateId.pucName, - ( const char * ) xADUDeviceProperties.xCurrentUpdateId.ucName, - ( size_t ) xADUDeviceProperties.xCurrentUpdateId.ulNameLength ) == 0 ) && - ( pxAduUpdateRequest->xUpdateManifest.xUpdateId.ulProviderLength == - xADUDeviceProperties.xCurrentUpdateId.ulProviderLength ) && - ( strncmp( ( const char * ) pxAduUpdateRequest->xUpdateManifest.xUpdateId.pucProvider, - ( const char * ) xADUDeviceProperties.xCurrentUpdateId.ucProvider, - ( size_t ) xADUDeviceProperties.xCurrentUpdateId.ulProviderLength ) == 0 ) && - ( pxAduUpdateRequest->xUpdateManifest.xUpdateId.ulVersionLength == - xADUDeviceProperties.xCurrentUpdateId.ulVersionLength ) && - ( strncmp( ( const char * ) pxAduUpdateRequest->xUpdateManifest.xUpdateId.pucVersion, - ( const char * ) xADUDeviceProperties.xCurrentUpdateId.ucVersion, - ( size_t ) xADUDeviceProperties.xCurrentUpdateId.ulVersionLength ) == 0 ) ) + /* + * In a production solution, each step should be validated against the version of + * each component the update step applies to (matching through the `handler` name). + */ + if( ( ( sizeof( democonfigADU_UPDATE_VERSION ) - 1 ) == + pxAduUpdateRequest->xUpdateManifest.xInstructions.pxSteps[ 0 ].ulInstalledCriteriaLength ) && + ( strncmp( + ( const char * ) democonfigADU_UPDATE_VERSION, + ( const char * ) pxAduUpdateRequest->xUpdateManifest.xInstructions.pxSteps[ 0 ].pucInstalledCriteria, + ( size_t ) pxAduUpdateRequest->xUpdateManifest.xInstructions.pxSteps[ 0 ].ulInstalledCriteriaLength ) == 0 ) ) { return true; } @@ -153,6 +158,7 @@ static bool prvIsUpdateAlreadyInstalled( const AzureIoTADUUpdateRequest_t * pxAd return false; } } + /*-----------------------------------------------------------*/ /** @@ -169,7 +175,7 @@ static bool prvIsUpdateAlreadyInstalled( const AzureIoTADUUpdateRequest_t * pxAd */ static AzureIoTADURequestDecision_t prvUserDecideShouldStartUpdate( AzureIoTADUUpdateRequest_t * pxAduUpdateRequest ) { - if( prvIsUpdateAlreadyInstalled( pxAduUpdateRequest ) ) + if( prvDoesInstalledCriteriaMatchCurrentVersion( pxAduUpdateRequest ) ) { LogInfo( ( "[ADU] Rejecting update request (current version is up-to-date)" ) ); return eAzureIoTADURequestDecisionReject; diff --git a/demos/sample_azure_iot_adu/sample_azure_iot_adu.c b/demos/sample_azure_iot_adu/sample_azure_iot_adu.c index 4ec8a798..26e0dfb2 100644 --- a/demos/sample_azure_iot_adu/sample_azure_iot_adu.c +++ b/demos/sample_azure_iot_adu/sample_azure_iot_adu.c @@ -128,6 +128,11 @@ */ #define ADU_HEADER_BUFFER_SIZE 512 +#define democonfigADU_UPDATE_ID "{\"provider\":\"" democonfigADU_UPDATE_PROVIDER "\",\"name\":\"" democonfigADU_UPDATE_NAME "\",\"version\":\"" democonfigADU_UPDATE_VERSION "\"}" + +#ifdef democonfigADU_UPDATE_NEW_VERSION + #define democonfigADU_SPOOFED_UPDATE_ID "{\"provider\":\"" democonfigADU_UPDATE_PROVIDER "\",\"name\":\"" democonfigADU_UPDATE_NAME "\",\"version\":\"" democonfigADU_UPDATE_NEW_VERSION "\"}" +#endif /*-----------------------------------------------------------*/ /** @@ -162,15 +167,8 @@ AzureIoTADUClientDeviceProperties_t xADUDeviceProperties = .ulManufacturerLength = sizeof( democonfigADU_DEVICE_MANUFACTURER ) - 1, .ucModel = ( const uint8_t * ) democonfigADU_DEVICE_MODEL, .ulModelLength = sizeof( democonfigADU_DEVICE_MODEL ) - 1, - .xCurrentUpdateId = - { - .ucProvider = ( const uint8_t * ) democonfigADU_UPDATE_PROVIDER, - .ulProviderLength = sizeof( democonfigADU_UPDATE_PROVIDER ) - 1, - .ucName = ( const uint8_t * ) democonfigADU_UPDATE_NAME, - .ulNameLength = sizeof( democonfigADU_UPDATE_NAME ) - 1, - .ucVersion = ( const uint8_t * ) democonfigADU_UPDATE_VERSION, - .ulVersionLength = sizeof( democonfigADU_UPDATE_VERSION ) - 1 - }, + .ucCurrentUpdateId = ( const uint8_t * ) democonfigADU_UPDATE_ID, + .ulCurrentUpdateIdLength = sizeof( democonfigADU_UPDATE_ID ) - 1, .ucDeliveryOptimizationAgentVersion = NULL, .ulDeliveryOptimizationAgentVersionLength = 0 }; @@ -639,13 +637,13 @@ static AzureIoTResult_t prvEnableImageAndResetDevice() static AzureIoTResult_t prvSpoofNewVersion( void ) { #ifdef democonfigADU_UPDATE_NEW_VERSION - xADUDeviceProperties.xCurrentUpdateId.ucVersion = ( const uint8_t * ) democonfigADU_UPDATE_NEW_VERSION; - xADUDeviceProperties.xCurrentUpdateId.ulVersionLength = strlen( democonfigADU_UPDATE_NEW_VERSION ); + xADUDeviceProperties.ucCurrentUpdateId = ( const uint8_t * ) democonfigADU_SPOOFED_UPDATE_ID; + xADUDeviceProperties.ulCurrentUpdateIdLength = strlen( democonfigADU_SPOOFED_UPDATE_ID ); #else LogError( ( "[ADU] New ADU update version for simulator not given." ) ); #endif LogInfo( ( "[ADU] Device Version %.*s", - xADUDeviceProperties.xCurrentUpdateId.ulVersionLength, xADUDeviceProperties.xCurrentUpdateId.ucVersion ) ); + xADUDeviceProperties.ulCurrentUpdateIdLength, xADUDeviceProperties.ucCurrentUpdateId ) ); return AzureIoTADUClient_SendAgentState( &xAzureIoTADUClient, &xAzureIoTHubClient, &xADUDeviceProperties, diff --git a/demos/sample_azure_iot_adu/sample_azure_iot_pnp_simulated_data.c b/demos/sample_azure_iot_adu/sample_azure_iot_pnp_simulated_data.c index 431281d5..e622970c 100644 --- a/demos/sample_azure_iot_adu/sample_azure_iot_pnp_simulated_data.c +++ b/demos/sample_azure_iot_adu/sample_azure_iot_pnp_simulated_data.c @@ -20,12 +20,6 @@ #include "FreeRTOS.h" #include "task.h" -/* - * TODO: In future improvement, compare sampleazureiotMODEL_ID macro definition - * and make sure that it is "dtmi:com:example:Thermostat;1", - * and fail compilation otherwise. - */ - /*-----------------------------------------------------------*/ /** @@ -256,12 +250,12 @@ static bool prvDoesInstalledCriteriaMatchCurrentVersion( const AzureIoTADUUpdate * In a production solution, each step should be validated against the version of * each component the update step applies to (matching through the `handler` name). */ - if( ( xADUDeviceProperties.xCurrentUpdateId.ulVersionLength == + if( ( ( sizeof( democonfigADU_UPDATE_VERSION ) - 1 ) == pxAduUpdateRequest->xUpdateManifest.xInstructions.pxSteps[ 0 ].ulInstalledCriteriaLength ) && ( strncmp( - ( const char * ) xADUDeviceProperties.xCurrentUpdateId.ucVersion, + ( const char * ) democonfigADU_UPDATE_VERSION, ( const char * ) pxAduUpdateRequest->xUpdateManifest.xInstructions.pxSteps[ 0 ].pucInstalledCriteria, - ( size_t ) xADUDeviceProperties.xCurrentUpdateId.ulVersionLength ) == 0 ) ) + ( size_t ) pxAduUpdateRequest->xUpdateManifest.xInstructions.pxSteps[ 0 ].ulInstalledCriteriaLength ) == 0 ) ) { return true; } diff --git a/libs/azure-iot-middleware-freertos b/libs/azure-iot-middleware-freertos index fe9c9fe1..546d2200 160000 --- a/libs/azure-iot-middleware-freertos +++ b/libs/azure-iot-middleware-freertos @@ -1 +1 @@ -Subproject commit fe9c9fe1311e35c64ecd8b0d1461d10983bb9962 +Subproject commit 546d22007ff4b6eac3fa67ee7f089a97312723e8