Skip to content

Commit

Permalink
Change ADU client update id to match service specification (#242)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ewertons committed Sep 12, 2022
1 parent 35759c8 commit ab5bff4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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;
}
Expand All @@ -153,6 +158,7 @@ static bool prvIsUpdateAlreadyInstalled( const AzureIoTADUUpdateRequest_t * pxAd
return false;
}
}

/*-----------------------------------------------------------*/

/**
Expand All @@ -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;
Expand Down
22 changes: 10 additions & 12 deletions demos/sample_azure_iot_adu/sample_azure_iot_adu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
/*-----------------------------------------------------------*/

/**
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 3 additions & 9 deletions demos/sample_azure_iot_adu/sample_azure_iot_pnp_simulated_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/

/*-----------------------------------------------------------*/

/**
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit ab5bff4

Please sign in to comment.