Skip to content

Commit

Permalink
Update return code structure for all samples. (#1248)
Browse files Browse the repository at this point in the history
Separates receiving the return code from a function call, and the actual check of the return code.
Commenting and typo fixes.
  • Loading branch information
momuno authored Sep 10, 2020
1 parent 8d31f22 commit fbac414
Show file tree
Hide file tree
Showing 14 changed files with 758 additions and 786 deletions.
69 changes: 32 additions & 37 deletions sdk/samples/iot/iot_sample_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ az_result iot_sample_read_environment_variables(
out_env_vars->x509_trust_pem_file_path,
&(out_env_vars->x509_trust_pem_file_path)));

IOT_SAMPLE_LOG(" "); // Formatting.
IOT_SAMPLE_LOG(" "); // Formatting
return AZ_OK;
}

Expand Down Expand Up @@ -311,7 +311,6 @@ void iot_sample_sleep_for_seconds(uint32_t seconds)
#else
sleep(seconds);
#endif
return;
}

uint32_t iot_sample_get_epoch_expiration_time_from_minutes(uint32_t minutes)
Expand All @@ -324,20 +323,19 @@ static az_result decode_base64_bytes(
az_span decoded_bytes,
az_span* out_decoded_bytes)
{
az_result rc;
BIO* base64_decoder;
BIO* source_mem_bio;

memset(az_span_ptr(decoded_bytes), 0, (size_t)az_span_size(decoded_bytes));

// Create a BIO filter to process the bytes
// Create a BIO filter to process the bytes.
base64_decoder = BIO_new(BIO_f_base64());
if (base64_decoder == NULL)
{
return AZ_ERROR_OUT_OF_MEMORY;
}

// Get the source BIO to push through the filter
// Get the source BIO to push through the filter.
source_mem_bio
= BIO_new_mem_buf(az_span_ptr(base64_encoded_bytes), (int)az_span_size(base64_encoded_bytes));
if (source_mem_bio == NULL)
Expand All @@ -346,7 +344,7 @@ static az_result decode_base64_bytes(
return AZ_ERROR_OUT_OF_MEMORY;
}

// Push the memory through the filter
// Push the memory through the filter.
source_mem_bio = BIO_push(base64_decoder, source_mem_bio);
if (source_mem_bio == NULL)
{
Expand All @@ -355,14 +353,15 @@ static az_result decode_base64_bytes(
return AZ_ERROR_OUT_OF_MEMORY;
}

// Set flags to not have a newline and close the BIO
// Set flags to not have a newline and close the BIO.
BIO_set_flags(source_mem_bio, BIO_FLAGS_BASE64_NO_NL);
BIO_set_close(source_mem_bio, BIO_CLOSE);

// Read the memory which was pushed through the filter
// Read the memory which was pushed through the filter.
int read_data = BIO_read(source_mem_bio, az_span_ptr(decoded_bytes), az_span_size(decoded_bytes));

// Set the output span
// Set the output span.
az_result rc;
if (read_data > 0)
{
*out_decoded_bytes = az_span_create(az_span_ptr(decoded_bytes), (int32_t)read_data);
Expand All @@ -373,7 +372,7 @@ static az_result decode_base64_bytes(
rc = AZ_ERROR_NOT_ENOUGH_SPACE;
}

// Free the BIO chain
// Free the BIO chain.
BIO_free_all(source_mem_bio);

return rc;
Expand All @@ -385,8 +384,6 @@ static az_result hmac_sha256_sign_signature(
az_span signed_signature,
az_span* out_signed_signature)
{
az_result rc;

unsigned int hmac_encode_len;
unsigned char const* hmac = HMAC(
EVP_sha256(),
Expand All @@ -397,6 +394,7 @@ static az_result hmac_sha256_sign_signature(
az_span_ptr(signed_signature),
&hmac_encode_len);

az_result rc;
if (hmac != NULL)
{
*out_signed_signature = az_span_create(az_span_ptr(signed_signature), (int32_t)hmac_encode_len);
Expand All @@ -415,27 +413,26 @@ static az_result base64_encode_bytes(
az_span base64_encoded_bytes,
az_span* out_base64_encoded_bytes)
{
az_result rc;
BIO* base64_encoder;
BIO* sink_mem_bio;
BUF_MEM* encoded_mem_ptr;

// Create a BIO filter to process the bytes
// Create a BIO filter to process the bytes.
base64_encoder = BIO_new(BIO_f_base64());
if (base64_encoder == NULL)
{
return AZ_ERROR_OUT_OF_MEMORY;
}

// Create a memory sink BIO to process bytes to
// Create a memory sink BIO to process bytes to.
sink_mem_bio = BIO_new(BIO_s_mem());
if (sink_mem_bio == NULL)
{
BIO_free(base64_encoder);
return AZ_ERROR_OUT_OF_MEMORY;
}

// Push the sink to the encoder
// Push the sink to the encoder.
base64_encoder = BIO_push(base64_encoder, sink_mem_bio);
if (base64_encoder == NULL)
{
Expand All @@ -444,10 +441,10 @@ static az_result base64_encode_bytes(
return AZ_ERROR_OUT_OF_MEMORY;
}

// Set no newline flag for the encoder
// Set no newline flag for the encoder.
BIO_set_flags(base64_encoder, BIO_FLAGS_BASE64_NO_NL);

// Write the bytes to be encoded
// Write the bytes to be encoded.
int const bytes_written
= BIO_write(base64_encoder, az_span_ptr(decoded_bytes), (int)az_span_size(decoded_bytes));
if (bytes_written < 1)
Expand All @@ -460,12 +457,13 @@ static az_result base64_encode_bytes(
// Flush the BIO
BIO_flush(base64_encoder);

// Get the pointer to the encoded bytes
// Get the pointer to the encoded bytes.
BIO_get_mem_ptr(base64_encoder, &encoded_mem_ptr);

az_result rc;
if ((size_t)az_span_size(base64_encoded_bytes) >= encoded_mem_ptr->length)
{
// Copy the bytes to the output and initialize output span
// Copy the bytes to the output and initialize output span.
memcpy(az_span_ptr(base64_encoded_bytes), encoded_mem_ptr->data, encoded_mem_ptr->length);
*out_base64_encoded_bytes
= az_span_create(az_span_ptr(base64_encoded_bytes), (int32_t)encoded_mem_ptr->length);
Expand All @@ -477,7 +475,7 @@ static az_result base64_encode_bytes(
rc = AZ_ERROR_NOT_ENOUGH_SPACE;
}

// Free the BIO chain
// Free the BIO chain.
BIO_free_all(base64_encoder);

return rc;
Expand All @@ -491,13 +489,14 @@ void iot_sample_generate_sas_base64_encoded_signed_signature(
{
IOT_SAMPLE_PRECONDITION_NOT_NULL(out_sas_base64_encoded_signed_signature);

int rc;
az_result rc;

// Decode the sas base64 encoded key to use for HMAC signing.
char sas_decoded_key_buffer[64];
az_span sas_decoded_key = AZ_SPAN_FROM_BUFFER(sas_decoded_key_buffer);
if (az_result_failed(
rc = decode_base64_bytes(sas_base64_encoded_key, sas_decoded_key, &sas_decoded_key)))

rc = decode_base64_bytes(sas_base64_encoded_key, sas_decoded_key, &sas_decoded_key);
if (az_result_failed(rc))
{
IOT_SAMPLE_LOG_ERROR("Could not decode the SAS key: az_result return code 0x%04x.", rc);
exit(rc);
Expand All @@ -506,27 +505,23 @@ void iot_sample_generate_sas_base64_encoded_signed_signature(
// HMAC-SHA256 sign the signature with the decoded key.
char sas_hmac256_signed_signature_buffer[128];
az_span sas_hmac256_signed_signature = AZ_SPAN_FROM_BUFFER(sas_hmac256_signed_signature_buffer);
if (az_result_failed(
rc = hmac_sha256_sign_signature(
sas_decoded_key,
sas_signature,
sas_hmac256_signed_signature,
&sas_hmac256_signed_signature)))

rc = hmac_sha256_sign_signature(
sas_decoded_key, sas_signature, sas_hmac256_signed_signature, &sas_hmac256_signed_signature);
if (az_result_failed(rc))
{
IOT_SAMPLE_LOG_ERROR("Could not sign the signature: az_result return code 0x%04x.", rc);
exit(rc);
}

// Base64 encode the result of the HMAC signing.
if (az_result_failed(
rc = base64_encode_bytes(
sas_hmac256_signed_signature,
sas_base64_encoded_signed_signature,
out_sas_base64_encoded_signed_signature)))
rc = base64_encode_bytes(
sas_hmac256_signed_signature,
sas_base64_encoded_signed_signature,
out_sas_base64_encoded_signed_signature);
if (az_result_failed(rc))
{
IOT_SAMPLE_LOG_ERROR("Could not base64 encode the password: az_result return code 0x%04x.", rc);
exit(rc);
}

return;
}
76 changes: 35 additions & 41 deletions sdk/samples/iot/paho_iot_hub_c2d_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ static void parse_c2d_message(
az_iot_hub_client_c2d_request* out_c2d_request);

/*
* This sample receives incoming cloud-to-device (C2D) messages sent from the Azure IoT Hub to
* the device. It will successfully receive up to MAX_C2D_MESSAGE_COUNT messages sent from the
* service. If a timeout occurs of TIMEOUT_MQTT_RECEIVE_MS while waiting for a message, the sample
* will exit. X509 self-certification is used.
* This sample receives incoming cloud-to-device (C2D) messages sent from the Azure IoT Hub to the
* device. It will successfully receive up to MAX_C2D_MESSAGE_COUNT messages sent from the service.
* If a timeout occurs of TIMEOUT_MQTT_RECEIVE_MS while waiting for a message, the sample will exit.
* X509 self-certification is used.
*
* To send a C2D message, select your device's Message to Device tab in the Azure Portal for your
* IoT Hub. Enter a message in the Message Body and select Send Message.
Expand Down Expand Up @@ -82,8 +82,8 @@ static void create_and_configure_mqtt_client(void)
int rc;

// Reads in environment variables set by user for purposes of running sample.
if (az_result_failed(
rc = iot_sample_read_environment_variables(SAMPLE_TYPE, SAMPLE_NAME, &env_vars)))
rc = iot_sample_read_environment_variables(SAMPLE_TYPE, SAMPLE_NAME, &env_vars);
if (az_result_failed(rc))
{
IOT_SAMPLE_LOG_ERROR(
"Failed to read configuration from environment variables: az_result return code 0x%08x.",
Expand All @@ -93,41 +93,36 @@ static void create_and_configure_mqtt_client(void)

// Build an MQTT endpoint c-string.
char mqtt_endpoint_buffer[128];
if (az_result_failed(
rc = iot_sample_create_mqtt_endpoint(
SAMPLE_TYPE, &env_vars, mqtt_endpoint_buffer, sizeof(mqtt_endpoint_buffer))))
rc = iot_sample_create_mqtt_endpoint(
SAMPLE_TYPE, &env_vars, mqtt_endpoint_buffer, sizeof(mqtt_endpoint_buffer));
if (az_result_failed(rc))
{
IOT_SAMPLE_LOG_ERROR("Failed to create MQTT endpoint: az_result return code 0x%08x.", rc);
exit(rc);
}

// Initialize the hub client with the default connection options.
if (az_result_failed(
rc = az_iot_hub_client_init(
&hub_client, env_vars.hub_hostname, env_vars.hub_device_id, NULL)))
rc = az_iot_hub_client_init(&hub_client, env_vars.hub_hostname, env_vars.hub_device_id, NULL);
if (az_result_failed(rc))
{
IOT_SAMPLE_LOG_ERROR("Failed to initialize hub client: az_result return code 0x%08x.", rc);
exit(rc);
}

// Get the MQTT client id used for the MQTT connection.
char mqtt_client_id_buffer[128];
if (az_result_failed(
rc = az_iot_hub_client_get_client_id(
&hub_client, mqtt_client_id_buffer, sizeof(mqtt_client_id_buffer), NULL)))
rc = az_iot_hub_client_get_client_id(
&hub_client, mqtt_client_id_buffer, sizeof(mqtt_client_id_buffer), NULL);
if (az_result_failed(rc))
{
IOT_SAMPLE_LOG_ERROR("Failed to get MQTT client id: az_result return code 0x%08x.", rc);
exit(rc);
}

// Create the Paho MQTT client.
if ((rc = MQTTClient_create(
&mqtt_client,
mqtt_endpoint_buffer,
mqtt_client_id_buffer,
MQTTCLIENT_PERSISTENCE_NONE,
NULL))
!= MQTTCLIENT_SUCCESS)
rc = MQTTClient_create(
&mqtt_client, mqtt_endpoint_buffer, mqtt_client_id_buffer, MQTTCLIENT_PERSISTENCE_NONE, NULL);
if (rc != MQTTCLIENT_SUCCESS)
{
IOT_SAMPLE_LOG_ERROR("Failed to create MQTT client: MQTTClient return code %d.", rc);
exit(rc);
Expand All @@ -139,9 +134,9 @@ static void connect_mqtt_client_to_iot_hub(void)
int rc;

// Get the MQTT client username.
if (az_result_failed(
rc = az_iot_hub_client_get_user_name(
&hub_client, mqtt_client_username_buffer, sizeof(mqtt_client_username_buffer), NULL)))
rc = az_iot_hub_client_get_user_name(
&hub_client, mqtt_client_username_buffer, sizeof(mqtt_client_username_buffer), NULL);
if (az_result_failed(rc))
{
IOT_SAMPLE_LOG_ERROR("Failed to get MQTT client username: az_result return code 0x%08x.", rc);
exit(rc);
Expand All @@ -163,7 +158,8 @@ static void connect_mqtt_client_to_iot_hub(void)
mqtt_connect_options.ssl = &mqtt_ssl_options;

// Connect MQTT client to the Azure IoT Hub.
if ((rc = MQTTClient_connect(mqtt_client, &mqtt_connect_options)) != MQTTCLIENT_SUCCESS)
rc = MQTTClient_connect(mqtt_client, &mqtt_connect_options);
if (rc != MQTTCLIENT_SUCCESS)
{
IOT_SAMPLE_LOG_ERROR(
"Failed to connect: MQTTClient return code %d.\n"
Expand All @@ -176,11 +172,9 @@ static void connect_mqtt_client_to_iot_hub(void)

static void subscribe_mqtt_client_to_iot_hub_topics(void)
{
int rc;

// Messages received on the C2D topic will be cloud-to-device messages.
if ((rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_C2D_SUBSCRIBE_TOPIC, 1))
!= MQTTCLIENT_SUCCESS)
int rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_C2D_SUBSCRIBE_TOPIC, 1);
if (rc != MQTTCLIENT_SUCCESS)
{
IOT_SAMPLE_LOG_ERROR("Failed to subscribe to the C2D topic: MQTTClient return code %d.", rc);
exit(rc);
Expand All @@ -200,10 +194,12 @@ static void receive_c2d_messages(void)
IOT_SAMPLE_LOG(" "); // Formatting
IOT_SAMPLE_LOG("Waiting for C2D message.\n");

if (((rc
= MQTTClient_receive(mqtt_client, &topic, &topic_len, &message, MQTT_TIMEOUT_RECEIVE_MS))
!= MQTTCLIENT_SUCCESS)
&& (rc != MQTTCLIENT_TOPICNAME_TRUNCATED))
// MQTTCLIENT_SUCCESS or MQTTCLIENT_TOPICNAME_TRUNCATED if a message is received.
// MQTTCLIENT_SUCCESS can also indicate that the timeout expired, in which case message is NULL.
// MQTTCLIENT_TOPICNAME_TRUNCATED if the topic contains embedded NULL characters.
// An error code is returned if there was a problem trying to receive a message.
rc = MQTTClient_receive(mqtt_client, &topic, &topic_len, &message, MQTT_TIMEOUT_RECEIVE_MS);
if ((rc != MQTTCLIENT_SUCCESS) && (rc != MQTTCLIENT_TOPICNAME_TRUNCATED))
{
IOT_SAMPLE_LOG_ERROR(
"Failed to receive message #%d: MQTTClient return code %d.", message_count + 1, rc);
Expand Down Expand Up @@ -236,9 +232,8 @@ static void receive_c2d_messages(void)

static void disconnect_mqtt_client_from_iot_hub(void)
{
int rc;

if ((rc = MQTTClient_disconnect(mqtt_client, MQTT_TIMEOUT_DISCONNECT_MS)) != MQTTCLIENT_SUCCESS)
int rc = MQTTClient_disconnect(mqtt_client, MQTT_TIMEOUT_DISCONNECT_MS);
if (rc != MQTTCLIENT_SUCCESS)
{
IOT_SAMPLE_LOG_ERROR("Failed to disconnect MQTT client: MQTTClient return code %d.", rc);
exit(rc);
Expand All @@ -253,14 +248,13 @@ static void parse_c2d_message(
MQTTClient_message const* message,
az_iot_hub_client_c2d_request* out_c2d_request)
{
az_result rc;
az_span const topic_span = az_span_create((uint8_t*)topic, topic_len);
az_span const message_span = az_span_create((uint8_t*)message->payload, message->payloadlen);

// Parse message and retrieve c2d_request info.
if (az_result_failed(
rc
= az_iot_hub_client_c2d_parse_received_topic(&hub_client, topic_span, out_c2d_request)))
az_result rc
= az_iot_hub_client_c2d_parse_received_topic(&hub_client, topic_span, out_c2d_request);
if (az_result_failed(rc))
{
IOT_SAMPLE_LOG_ERROR("Message from unknown topic: az_result return code 0x%08x.", rc);
IOT_SAMPLE_LOG_AZ_SPAN("Topic:", topic_span);
Expand Down
Loading

0 comments on commit fbac414

Please sign in to comment.