diff --git a/sdk/inc/azure/iot/az_iot_adu_client.h b/sdk/inc/azure/iot/az_iot_adu_client.h index 8785ef2e7a..29244dbeaf 100644 --- a/sdk/inc/azure/iot/az_iot_adu_client.h +++ b/sdk/inc/azure/iot/az_iot_adu_client.h @@ -119,7 +119,7 @@ typedef struct * The version for the update. */ az_span version; -} az_iot_adu_client_update_id; +} az_iot_adu_update_id; /** * @brief Holds any user-defined custom properties of the device. @@ -179,7 +179,7 @@ typedef struct /** * An ID of the update that is currently installed. */ - az_iot_adu_client_update_id update_id; + az_span update_id; } az_iot_adu_client_device_properties; /** @@ -417,7 +417,7 @@ typedef struct /** * User-defined identity of the update manifest. */ - az_iot_adu_client_update_id update_id; + az_iot_adu_update_id update_id; /** * Instructions of the update manifest. */ diff --git a/sdk/samples/iot/paho_iot_adu_sample_common.c b/sdk/samples/iot/paho_iot_adu_sample_common.c index 1a582536c9..b163b52a49 100644 --- a/sdk/samples/iot/paho_iot_adu_sample_common.c +++ b/sdk/samples/iot/paho_iot_adu_sample_common.c @@ -76,14 +76,16 @@ static bool did_parse_update = false; static bool did_update = false; static char adu_scratch_buffer[10000]; +#define ADU_DEVICE_UPDATE_ID \ + "{ \"provider\": \"" ADU_DEVICE_MANUFACTURER "\", \"name\": \"" ADU_DEVICE_MODEL \ + "\", \"version\": \"" ADU_DEVICE_VERSION "\" }" + az_iot_adu_device_properties adu_device_properties = { .manufacturer = AZ_SPAN_LITERAL_FROM_STR(ADU_DEVICE_MANUFACTURER), .model = AZ_SPAN_LITERAL_FROM_STR(ADU_DEVICE_MODEL), .adu_version = AZ_SPAN_LITERAL_FROM_STR(AZ_IOT_ADU_CLIENT_AGENT_VERSION), .delivery_optimization_agent_version = AZ_SPAN_EMPTY, - .update_id = { .name = AZ_SPAN_LITERAL_FROM_STR(ADU_DEVICE_MODEL), - .provider = AZ_SPAN_LITERAL_FROM_STR(ADU_DEVICE_MANUFACTURER), - .version = AZ_SPAN_LITERAL_FROM_STR(ADU_DEVICE_VERSION) } }; + .update_id = AZ_SPAN_LITERAL_FROM_STR(ADU_DEVICE_UPDATE_ID) }; // // Functions diff --git a/sdk/src/azure/iot/az_iot_adu_client.c b/sdk/src/azure/iot/az_iot_adu_client.c index dd4de913a6..a1a1b8114e 100644 --- a/sdk/src/azure/iot/az_iot_adu_client.c +++ b/sdk/src/azure/iot/az_iot_adu_client.c @@ -72,7 +72,10 @@ #define AZ_IOT_ADU_CLIENT_AGENT_PROPERTY_NAME_CREATED_DATE_TIME "createdDateTime" #define NULL_TERM_CHAR_SIZE 1 -#define UPDATE_ID_ESCAPING_CHARS_LENGTH 24 + +#define RESULT_STEP_ID_PREFIX "step_" +#define MAX_UINT32_NUMBER_OF_DIGITS 10 +#define RESULT_STEP_ID_MAX_SIZE (sizeof(RESULT_STEP_ID_PREFIX) - 1 + MAX_UINT32_NUMBER_OF_DIGITS) #define RETURN_IF_JSON_TOKEN_NOT_TYPE(jr_ptr, json_token_type) \ if (jr_ptr->token.kind != json_token_type) \ @@ -119,28 +122,6 @@ AZ_NODISCARD bool az_iot_adu_client_is_component_device_update( AZ_SPAN_FROM_STR(AZ_IOT_ADU_CLIENT_AGENT_COMPONENT_NAME), component_name); } -static az_span generate_update_id_string( - az_iot_adu_client_update_id update_id, - az_span update_id_string) -{ - // TODO: Investigate a way to leverage azure SDK core for this. - az_span remainder = update_id_string; - remainder = az_span_copy(remainder, AZ_SPAN_FROM_STR("\"{\\\"provider\\\":\\\"")); - remainder = az_span_copy(remainder, update_id.provider); - remainder = az_span_copy(remainder, AZ_SPAN_FROM_STR("\\\",\\\"name\\\":\\\"")); - remainder = az_span_copy(remainder, update_id.name); - remainder = az_span_copy(remainder, AZ_SPAN_FROM_STR("\\\",\\\"version\\\":\\\"")); - remainder = az_span_copy(remainder, update_id.version); - remainder = az_span_copy(remainder, AZ_SPAN_FROM_STR("\\\"}\"")); - - return az_span_slice( - update_id_string, 0, az_span_size(update_id_string) - az_span_size(remainder)); -} - -#define RESULT_STEP_ID_PREFIX "step_" -#define MAX_UINT32_NUMBER_OF_DIGITS 10 -#define RESULT_STEP_ID_MAX_SIZE (sizeof(RESULT_STEP_ID_PREFIX) + MAX_UINT32_NUMBER_OF_DIGITS) - static az_span get_json_writer_remaining_buffer(az_json_writer* jw) { return az_span_slice_to_end( @@ -174,9 +155,7 @@ AZ_NODISCARD az_result az_iot_adu_client_get_agent_state_payload( _az_PRECONDITION_NOT_NULL(device_properties); _az_PRECONDITION_VALID_SPAN(device_properties->manufacturer, 1, false); _az_PRECONDITION_VALID_SPAN(device_properties->model, 1, false); - _az_PRECONDITION_VALID_SPAN(device_properties->update_id.provider, 1, false); - _az_PRECONDITION_VALID_SPAN(device_properties->update_id.name, 1, false); - _az_PRECONDITION_VALID_SPAN(device_properties->update_id.version, 1, false); + _az_PRECONDITION_VALID_SPAN(device_properties->update_id, 1, false); _az_PRECONDITION_VALID_SPAN(device_properties->adu_version, 1, false); _az_PRECONDITION_NOT_NULL(ref_json_writer); @@ -350,23 +329,11 @@ AZ_NODISCARD az_result az_iot_adu_client_get_agent_state_payload( _az_RETURN_IF_FAILED(az_json_writer_append_end_object(ref_json_writer)); } - /* Fill installed update id. */ - // TODO: Find way to not use internal field - az_span update_id_string = az_span_slice_to_end( - ref_json_writer->_internal.destination_buffer, - az_span_size(az_json_writer_get_bytes_used_in_destination(ref_json_writer)) - + UPDATE_ID_ESCAPING_CHARS_LENGTH); - - if (az_span_is_content_equal(update_id_string, AZ_SPAN_EMPTY)) - { - return AZ_ERROR_NOT_ENOUGH_SPACE; - } - + /* Fill installed update id. */ _az_RETURN_IF_FAILED(az_json_writer_append_property_name( ref_json_writer, AZ_SPAN_FROM_STR(AZ_IOT_ADU_CLIENT_AGENT_PROPERTY_NAME_INSTALLED_UPDATE_ID))); - _az_RETURN_IF_FAILED(az_json_writer_append_json_text( - ref_json_writer, generate_update_id_string(device_properties->update_id, update_id_string))); + _az_RETURN_IF_FAILED(az_json_writer_append_string(ref_json_writer, device_properties->update_id)); _az_RETURN_IF_FAILED(az_json_writer_append_end_object(ref_json_writer)); diff --git a/sdk/tests/iot/adu/test_az_iot_adu.c b/sdk/tests/iot/adu/test_az_iot_adu.c index 1a7225dbeb..d897855e7b 100644 --- a/sdk/tests/iot/adu/test_az_iot_adu.c +++ b/sdk/tests/iot/adu/test_az_iot_adu.c @@ -23,6 +23,9 @@ #define TEST_ADU_DEVICE_MODEL "Foobar" #define TEST_AZ_IOT_ADU_CLIENT_AGENT_VERSION AZ_IOT_ADU_CLIENT_AGENT_VERSION #define TEST_ADU_DEVICE_VERSION "1.0" +#define TEST_ADU_DEVICE_UPDATE_ID \ + "{\"provider\":\"" TEST_ADU_DEVICE_MANUFACTURER "\",\"name\":\"" TEST_ADU_DEVICE_MODEL \ + "\",\"version\":\"" TEST_ADU_DEVICE_VERSION "\"}" static uint8_t expected_agent_state_payload[] = "{\"deviceUpdate\":{\"__t\":\"c\",\"agent\":{\"deviceProperties\":{\"manufacturer\":" @@ -47,9 +50,7 @@ az_iot_adu_client_device_properties adu_device_properties .model = AZ_SPAN_LITERAL_FROM_STR(TEST_ADU_DEVICE_MODEL), .adu_version = AZ_SPAN_LITERAL_FROM_STR(TEST_AZ_IOT_ADU_CLIENT_AGENT_VERSION), .delivery_optimization_agent_version = AZ_SPAN_LITERAL_EMPTY, - .update_id = { .provider = AZ_SPAN_LITERAL_FROM_STR(TEST_ADU_DEVICE_MANUFACTURER), - .name = AZ_SPAN_LITERAL_FROM_STR(TEST_ADU_DEVICE_MODEL), - .version = AZ_SPAN_LITERAL_FROM_STR(TEST_ADU_DEVICE_VERSION) } }; + .update_id = AZ_SPAN_LITERAL_FROM_STR(TEST_ADU_DEVICE_UPDATE_ID) }; static uint8_t send_response_valid_payload[] = "{\"deviceUpdate\":{\"__t\":\"c\",\"service\":{\"ac\":200,\"av\":1,\"value\":{}}}}";