Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ADU client update id to match service specification #2331

Merged
merged 5 commits into from
Sep 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sdk/inc/azure/iot/az_iot_adu_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
ewertons marked this conversation as resolved.
Show resolved Hide resolved

/**
* @brief Holds any user-defined custom properties of the device.
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.
*/
Expand Down
8 changes: 5 additions & 3 deletions sdk/samples/iot/paho_iot_adu_sample_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 7 additions & 40 deletions sdk/src/azure/iot/az_iot_adu_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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));

Expand Down
7 changes: 4 additions & 3 deletions sdk/tests/iot/adu/test_az_iot_adu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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\":"
Expand All @@ -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\":{}}}}";
Expand Down