From fbac414a2064cecf3788478fcfe9230a6f3b2525 Mon Sep 17 00:00:00 2001 From: Mollie Munoz Date: Wed, 9 Sep 2020 18:37:56 -0700 Subject: [PATCH] Update return code structure for all samples. (#1248) Separates receiving the return code from a function call, and the actual check of the return code. Commenting and typo fixes. --- sdk/samples/iot/iot_sample_common.c | 69 ++-- sdk/samples/iot/paho_iot_hub_c2d_sample.c | 76 ++-- sdk/samples/iot/paho_iot_hub_methods_sample.c | 104 +++-- .../iot/paho_iot_hub_pnp_component_sample.c | 373 ++++++++++-------- sdk/samples/iot/paho_iot_hub_pnp_sample.c | 227 ++++++----- .../iot/paho_iot_hub_sas_telemetry_sample.c | 82 ++-- .../iot/paho_iot_hub_telemetry_sample.c | 72 ++-- sdk/samples/iot/paho_iot_hub_twin_sample.c | 141 ++++--- .../iot/paho_iot_provisioning_sample.c | 137 +++---- .../iot/paho_iot_provisioning_sas_sample.c | 160 ++++---- .../iot/pnp/pnp_device_info_component.c | 2 +- sdk/samples/iot/pnp/pnp_mqtt_message.c | 4 +- sdk/samples/iot/pnp/pnp_protocol.c | 8 +- .../iot/pnp/pnp_thermostat_component.c | 89 +++-- 14 files changed, 758 insertions(+), 786 deletions(-) diff --git a/sdk/samples/iot/iot_sample_common.c b/sdk/samples/iot/iot_sample_common.c index 632132d75c..8df0513f97 100644 --- a/sdk/samples/iot/iot_sample_common.c +++ b/sdk/samples/iot/iot_sample_common.c @@ -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; } @@ -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) @@ -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) @@ -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) { @@ -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); @@ -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; @@ -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(), @@ -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); @@ -415,19 +413,18 @@ 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) { @@ -435,7 +432,7 @@ static az_result base64_encode_bytes( 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) { @@ -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) @@ -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); @@ -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; @@ -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); @@ -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; } diff --git a/sdk/samples/iot/paho_iot_hub_c2d_sample.c b/sdk/samples/iot/paho_iot_hub_c2d_sample.c index 15059c9a50..0c8a4bd328 100644 --- a/sdk/samples/iot/paho_iot_hub_c2d_sample.c +++ b/sdk/samples/iot/paho_iot_hub_c2d_sample.c @@ -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. @@ -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.", @@ -93,18 +93,17 @@ 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); @@ -112,22 +111,18 @@ static void create_and_configure_mqtt_client(void) // 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); @@ -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); @@ -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" @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/sdk/samples/iot/paho_iot_hub_methods_sample.c b/sdk/samples/iot/paho_iot_hub_methods_sample.c index 95cd009851..a7005c947c 100644 --- a/sdk/samples/iot/paho_iot_hub_methods_sample.c +++ b/sdk/samples/iot/paho_iot_hub_methods_sample.c @@ -98,8 +98,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.", @@ -109,18 +109,17 @@ 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); @@ -128,22 +127,18 @@ static void create_and_configure_mqtt_client(void) // 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) + // Create the Paho MQTT client. + 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); @@ -155,9 +150,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); @@ -179,7 +174,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" @@ -192,11 +188,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 Methods topic will be method commands to be invoked. - if ((rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC, 1)) - != MQTTCLIENT_SUCCESS) + int rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC, 1); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to subscribe to the Methods topic: MQTTClient return code %d.", rc); @@ -217,10 +211,12 @@ static void receive_method_messages(void) IOT_SAMPLE_LOG(" "); // Formatting IOT_SAMPLE_LOG("Waiting for method request.\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); @@ -255,9 +251,8 @@ static void receive_method_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); @@ -272,14 +267,13 @@ static void parse_method_message( MQTTClient_message const* message, az_iot_hub_client_method_request* out_method_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 method_request info. - if (az_result_failed( - rc = az_iot_hub_client_methods_parse_received_topic( - &hub_client, topic_span, out_method_request))) + az_result rc + = az_iot_hub_client_methods_parse_received_topic(&hub_client, topic_span, out_method_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); @@ -322,14 +316,14 @@ static void send_method_response( // Get the Methods Response topic to publish the method response. char methods_response_topic_buffer[128]; - if (az_result_failed( - rc = az_iot_hub_client_methods_response_get_publish_topic( - &hub_client, - method_request->request_id, - (uint16_t)status, - methods_response_topic_buffer, - sizeof(methods_response_topic_buffer), - NULL))) + rc = az_iot_hub_client_methods_response_get_publish_topic( + &hub_client, + method_request->request_id, + (uint16_t)status, + methods_response_topic_buffer, + sizeof(methods_response_topic_buffer), + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to get the Methods Response topic: az_result return code 0x%08x.", rc); @@ -337,15 +331,15 @@ static void send_method_response( } // Publish the method response. - if ((rc = MQTTClient_publish( - mqtt_client, - methods_response_topic_buffer, - az_span_size(response), - az_span_ptr(response), - IOT_SAMPLE_MQTT_PUBLISH_QOS, - 0, - NULL)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_publish( + mqtt_client, + methods_response_topic_buffer, + az_span_size(response), + az_span_ptr(response), + IOT_SAMPLE_MQTT_PUBLISH_QOS, + 0, + NULL); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR("Failed to publish the Methods response: MQTTClient return code %d.", rc); exit(rc); diff --git a/sdk/samples/iot/paho_iot_hub_pnp_component_sample.c b/sdk/samples/iot/paho_iot_hub_pnp_component_sample.c index 35bf5842a6..86ffd4539e 100644 --- a/sdk/samples/iot/paho_iot_hub_pnp_component_sample.c +++ b/sdk/samples/iot/paho_iot_hub_pnp_component_sample.c @@ -39,9 +39,9 @@ bool is_device_operational = true; // * PnP Values * -// The model id is the JSON document (also called the Digital Twins Model Identifier or DTMI) -// which defines the capability of your device. The functionality of the device should match what -// is described in the corresponding DTMI. Should you choose to program your own PnP capable device, +// The model id is the JSON document (also called the Digital Twins Model Identifier or DTMI) which +// defines the capability of your device. The functionality of the device should match what is +// described in the corresponding DTMI. Should you choose to program your own PnP capable device, // the functionality would need to match the DTMI and you would need to update the below 'model_id'. // Please see the sample README for more information on this DTMI. static az_span const model_id @@ -90,7 +90,7 @@ static void request_device_twin_document(void); static void receive_messages(void); static void disconnect_mqtt_client_from_iot_hub(void); -// General message building, sending, receiving functions +// General message sending and receiving functions static void publish_mqtt_message(char const* topic, az_span payload, int qos); static void receive_mqtt_message(void); static void on_message_received( @@ -99,7 +99,6 @@ static void on_message_received( MQTTClient_message const* receive_message); // Device twin, command request, telemetry functions - static void handle_device_twin_message( MQTTClient_message const* receive_message, az_iot_hub_client_twin_response const* twin_response); @@ -190,7 +189,7 @@ static az_result append_string_callback(az_json_writer* jw, void* value); * temperature reached since boot. * * On initial bootup of the device, the sample will send the Temperature Controller reported - * properties to the service. You will see the following in the device twin JSON. + * properties to the service. You will see the following in the device twin JSON. * { * "properties": { * "reported": { @@ -281,9 +280,9 @@ static az_result append_string_callback(az_json_writer* jw, void* value); * "endTime": "2020-08-18T17:24:32-0700" * } * - * Telemetry: The Temperature Controller sends a JSON message with the property name - * `workingSet` and a `double` value for the current working set of the device memory in KiB. Also, - * each Temperature Sensor sends a JSON message with the property name `temperature` and a `double` + * Telemetry: The Temperature Controller sends a JSON message with the property name `workingSet` + * and a `double` value for the current working set of the device memory in KiB. Also, each + * Temperature Sensor sends a JSON message with the property name `temperature` and a `double` * value for the current temperature. */ int main(void) @@ -298,8 +297,8 @@ int main(void) IOT_SAMPLE_LOG_SUCCESS("Client subscribed to IoT Hub topics."); // Initializations - int rc; - if (az_result_failed(rc = pnp_mqtt_message_init(&publish_message))) + int rc = pnp_mqtt_message_init(&publish_message); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to initialize pnp_mqtt_message: az_result return code 0x%08x.", rc); @@ -325,8 +324,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.", @@ -336,9 +335,9 @@ 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); @@ -347,9 +346,9 @@ static void create_and_configure_mqtt_client(void) // Initialize the hub client with the connection options. az_iot_hub_client_options options = az_iot_hub_client_options_default(); options.model_id = model_id; - if (az_result_failed( - rc = az_iot_hub_client_init( - &hub_client, env_vars.hub_hostname, env_vars.hub_device_id, &options))) + + rc = az_iot_hub_client_init(&hub_client, env_vars.hub_hostname, env_vars.hub_device_id, &options); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to initialize hub client: az_result return code 0x%08x.", rc); exit(rc); @@ -357,22 +356,18 @@ static void create_and_configure_mqtt_client(void) // 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); @@ -384,9 +379,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); @@ -408,7 +403,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" @@ -424,8 +420,8 @@ static void subscribe_mqtt_client_to_iot_hub_topics(void) int rc; // Messages received on the Methods topic will be commands to be invoked. - if ((rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC, 1)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC, 1); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to subscribe to the Methods topic: MQTTClient return code %d.", rc); @@ -433,8 +429,8 @@ static void subscribe_mqtt_client_to_iot_hub_topics(void) } // Messages received on the Twin Patch topic will be updates to the desired properties. - if ((rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC, 1)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC, 1); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to subscribe to the Twin Patch topic: MQTTClient return code %d.", rc); @@ -442,8 +438,8 @@ static void subscribe_mqtt_client_to_iot_hub_topics(void) } // Messages received on Twin Response topic will be response statuses from the server. - if ((rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC, 1)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC, 1); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to subscribe to the Twin Response topic: MQTTClient return code %d.", rc); @@ -456,16 +452,16 @@ static void initialize_components(void) az_result rc; // Initialize thermostats 1 and 2. - if (az_result_failed( - rc = pnp_thermostat_init(&thermostat_1, thermostat_1_name, DEFAULT_START_TEMP_CELSIUS))) + rc = pnp_thermostat_init(&thermostat_1, thermostat_1_name, DEFAULT_START_TEMP_CELSIUS); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to initialize Temperature Sensor 1: az_result return code 0x%08x.", rc); exit(rc); } - if (az_result_failed( - rc = pnp_thermostat_init(&thermostat_2, thermostat_2_name, DEFAULT_START_TEMP_CELSIUS))) + rc = pnp_thermostat_init(&thermostat_2, thermostat_2_name, DEFAULT_START_TEMP_CELSIUS); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to initialize Temperature Sensor 2: az_result return code 0x%08x.", rc); @@ -478,19 +474,22 @@ static void send_device_info(void) az_result rc; // Get the Twin Patch topic to send a reported property update. - if (az_result_failed( - rc = az_iot_hub_client_twin_patch_get_publish_topic( - &hub_client, pnp_mqtt_get_request_id(), publish_message.topic, publish_message.topic_length, NULL))) + rc = az_iot_hub_client_twin_patch_get_publish_topic( + &hub_client, + pnp_mqtt_get_request_id(), + publish_message.topic, + publish_message.topic_length, + NULL); + if (az_result_failed(rc)) { - IOT_SAMPLE_LOG_ERROR( - "Failed to get the Twin Patch topic: az_result return code 0x%08x.", rc); + IOT_SAMPLE_LOG_ERROR("Failed to get the Twin Patch topic: az_result return code 0x%08x.", rc); exit(rc); } // Build the device info reported property message. - if (az_result_failed( - rc = pnp_device_info_build_reported_property( - publish_message.payload, &publish_message.out_payload))) + rc = pnp_device_info_build_reported_property( + publish_message.payload, &publish_message.out_payload); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to build reported property payload for device info: az_result return code 0x%08x.", @@ -503,7 +502,7 @@ static void send_device_info(void) publish_message.topic, publish_message.out_payload, IOT_SAMPLE_MQTT_PUBLISH_QOS); IOT_SAMPLE_LOG_SUCCESS("Client sent reported property message for device info."); IOT_SAMPLE_LOG_AZ_SPAN("Payload:", publish_message.out_payload); - IOT_SAMPLE_LOG(" "); // Formatting. + IOT_SAMPLE_LOG(" "); // Formatting // Receive the response from the server. receive_mqtt_message(); @@ -514,12 +513,15 @@ static void send_serial_number(void) az_result rc; // Get the Twin Patch topic to send a reported property update. - if (az_result_failed( - rc = az_iot_hub_client_twin_patch_get_publish_topic( - &hub_client, pnp_mqtt_get_request_id(), publish_message.topic, publish_message.topic_length, NULL))) + rc = az_iot_hub_client_twin_patch_get_publish_topic( + &hub_client, + pnp_mqtt_get_request_id(), + publish_message.topic, + publish_message.topic_length, + NULL); + if (az_result_failed(rc)) { - IOT_SAMPLE_LOG_ERROR( - "Failed to get the Twin Patch topic: az_result return code 0x%08x.", rc); + IOT_SAMPLE_LOG_ERROR("Failed to get the Twin Patch topic: az_result return code 0x%08x.", rc); exit(rc); } @@ -535,7 +537,7 @@ static void send_serial_number(void) az_span_size(twin_reported_serial_number_property_name), az_span_ptr(twin_reported_serial_number_property_name)); IOT_SAMPLE_LOG_AZ_SPAN("Payload:", publish_message.out_payload); - IOT_SAMPLE_LOG(" "); // Formatting. + IOT_SAMPLE_LOG(" "); // Formatting // Receive the response from the server. receive_mqtt_message(); @@ -548,9 +550,13 @@ static void request_device_twin_document(void) IOT_SAMPLE_LOG("Client requesting device twin document from service."); // Set the Twin Document topic to publish the twin document request. - if (az_result_failed( - rc = az_iot_hub_client_twin_document_get_publish_topic( - &hub_client, pnp_mqtt_get_request_id(), publish_message.topic, publish_message.topic_length, NULL))) + rc = az_iot_hub_client_twin_document_get_publish_topic( + &hub_client, + pnp_mqtt_get_request_id(), + publish_message.topic, + publish_message.topic_length, + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to get the Twin Document topic: az_result return code 0x%08x.", rc); @@ -559,7 +565,7 @@ static void request_device_twin_document(void) // Publish the twin document request. publish_mqtt_message(publish_message.topic, AZ_SPAN_EMPTY, IOT_SAMPLE_MQTT_PUBLISH_QOS); - IOT_SAMPLE_LOG(" "); // Formatting. + IOT_SAMPLE_LOG(" "); // Formatting // Receive the response from the server. receive_mqtt_message(); @@ -576,9 +582,13 @@ static void receive_messages(void) if (thermostat_1.send_maximum_temperature_property) { // Get the Twin Patch topic to send a reported property update. - if (az_result_failed( - rc = az_iot_hub_client_twin_patch_get_publish_topic( - &hub_client, pnp_mqtt_get_request_id(), publish_message.topic, publish_message.topic_length, NULL))) + rc = az_iot_hub_client_twin_patch_get_publish_topic( + &hub_client, + pnp_mqtt_get_request_id(), + publish_message.topic, + publish_message.topic_length, + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to get the Twin Patch topic: az_result return code 0x%08x.", rc); @@ -598,7 +608,7 @@ static void receive_messages(void) az_span_size(property_name), az_span_ptr(property_name)); IOT_SAMPLE_LOG_AZ_SPAN("Payload:", publish_message.out_payload); - IOT_SAMPLE_LOG(" "); // Formatting. + IOT_SAMPLE_LOG(" "); // Formatting thermostat_1.send_maximum_temperature_property = false; // Only send again if new max set. @@ -609,9 +619,13 @@ static void receive_messages(void) if (thermostat_2.send_maximum_temperature_property) { // Get the Twin Patch topic to send a reported property update. - if (az_result_failed( - rc = az_iot_hub_client_twin_patch_get_publish_topic( - &hub_client, pnp_mqtt_get_request_id(), publish_message.topic, publish_message.topic_length, NULL))) + rc = az_iot_hub_client_twin_patch_get_publish_topic( + &hub_client, + pnp_mqtt_get_request_id(), + publish_message.topic, + publish_message.topic_length, + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to get the Twin Patch topic: az_result return code 0x%08x.", rc); @@ -632,7 +646,7 @@ static void receive_messages(void) az_span_size(property_name), az_span_ptr(property_name)); IOT_SAMPLE_LOG_AZ_SPAN("Payload:", publish_message.out_payload); - IOT_SAMPLE_LOG(" "); // Formatting. + IOT_SAMPLE_LOG(" "); // Formatting thermostat_2.send_maximum_temperature_property = false; // Only send again if new max set. @@ -640,7 +654,7 @@ static void receive_messages(void) receive_mqtt_message(); } - // Send telemetry messages. No response requested from server. + // Send telemetry messages. No response requested from server. send_telemetry_messages(); IOT_SAMPLE_LOG(" "); // Formatting. @@ -651,10 +665,8 @@ static void receive_messages(void) static void disconnect_mqtt_client_from_iot_hub(void) { - int rc; - - if ((rc = MQTTClient_disconnect(mqtt_client, PNP_MQTT_TIMEOUT_DISCONNECT_MS)) - != MQTTCLIENT_SUCCESS) + int rc = MQTTClient_disconnect(mqtt_client, PNP_MQTT_TIMEOUT_DISCONNECT_MS); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR("Failed to disconnect MQTT client: MQTTClient return code %d.", rc); exit(rc); @@ -665,11 +677,10 @@ static void disconnect_mqtt_client_from_iot_hub(void) static void publish_mqtt_message(char const* topic, az_span payload, int qos) { - int rc; + int rc = MQTTClient_publish( + mqtt_client, topic, az_span_size(payload), az_span_ptr(payload), qos, 0, NULL); - if ((rc = MQTTClient_publish( - mqtt_client, topic, az_span_size(payload), az_span_ptr(payload), qos, 0, NULL)) - != MQTTCLIENT_SUCCESS) + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR("Failed to publish message: MQTTClient return code %d", rc); exit(rc); @@ -686,10 +697,13 @@ static void receive_mqtt_message(void) IOT_SAMPLE_LOG("Waiting for command request or device twin message.\n"); - if (((rc = MQTTClient_receive( - mqtt_client, &topic, &topic_len, &receive_message, PNP_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, &receive_message, PNP_MQTT_TIMEOUT_RECEIVE_MS); + if ((rc != MQTTCLIENT_SUCCESS) && (rc != MQTTCLIENT_TOPICNAME_TRUNCATED)) { IOT_SAMPLE_LOG_ERROR("Failed to receive message: MQTTClient return code %d.", rc); exit(rc); @@ -708,7 +722,7 @@ static void receive_mqtt_message(void) else { IOT_SAMPLE_LOG_SUCCESS("Client received a message from the service."); - timeout_counter = 0; // Reset. + timeout_counter = 0; // Reset if (rc == MQTTCLIENT_TOPICNAME_TRUNCATED) { @@ -716,7 +730,7 @@ static void receive_mqtt_message(void) } on_message_received(topic, topic_len, receive_message); - IOT_SAMPLE_LOG(" "); // Formatting. + IOT_SAMPLE_LOG(" "); // Formatting MQTTClient_freeMessage(&receive_message); MQTTClient_free(topic); @@ -738,9 +752,8 @@ static void on_message_received( az_iot_hub_client_method_request command_request; // Parse the incoming message topic and handle appropriately. - if (az_result_succeeded( - rc - = az_iot_hub_client_twin_parse_received_topic(&hub_client, topic_span, &twin_response))) + rc = az_iot_hub_client_twin_parse_received_topic(&hub_client, topic_span, &twin_response); + if (az_result_succeeded(rc)) { IOT_SAMPLE_LOG_SUCCESS("Client received a valid topic response."); IOT_SAMPLE_LOG_AZ_SPAN("Topic:", topic_span); @@ -749,21 +762,23 @@ static void on_message_received( handle_device_twin_message(receive_message, &twin_response); } - else if (az_result_succeeded( - rc = az_iot_hub_client_methods_parse_received_topic( - &hub_client, topic_span, &command_request))) - { - IOT_SAMPLE_LOG_SUCCESS("Client received a valid topic response."); - IOT_SAMPLE_LOG_AZ_SPAN("Topic:", topic_span); - IOT_SAMPLE_LOG_AZ_SPAN("Payload:", message_span); - - handle_command_request(receive_message, &command_request); - } else { - IOT_SAMPLE_LOG_ERROR("Message from unknown topic: az_result return code 0x%08x.", rc); - IOT_SAMPLE_LOG_AZ_SPAN("Topic:", topic_span); - exit(rc); + rc = az_iot_hub_client_methods_parse_received_topic(&hub_client, topic_span, &command_request); + if (az_result_succeeded(rc)) + { + IOT_SAMPLE_LOG_SUCCESS("Client received a valid topic response."); + IOT_SAMPLE_LOG_AZ_SPAN("Topic:", topic_span); + IOT_SAMPLE_LOG_AZ_SPAN("Payload:", message_span); + + handle_command_request(receive_message, &command_request); + } + else + { + IOT_SAMPLE_LOG_ERROR("Message from unknown topic: az_result return code 0x%08x.", rc); + IOT_SAMPLE_LOG_AZ_SPAN("Topic:", topic_span); + exit(rc); + } } } @@ -856,12 +871,15 @@ static void handle_command_request( status = AZ_IOT_STATUS_NOT_FOUND; } - az_result rc; - // Get the Methods response topic to publish the command response. - if (az_result_failed( - rc = az_iot_hub_client_methods_response_get_publish_topic( - &hub_client, command_request->request_id, (uint16_t)status, publish_message.topic, publish_message.topic_length, NULL))) + az_result rc = az_iot_hub_client_methods_response_get_publish_topic( + &hub_client, + command_request->request_id, + (uint16_t)status, + publish_message.topic, + publish_message.topic_length, + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to get the Methods response topic: az_result return code 0x%08x.", rc); @@ -882,12 +900,16 @@ static void send_telemetry_messages(void) // Temperature Sensor 1 // Get the Telemetry topic to publish the telemetry message. - if (az_result_failed( - rc = pnp_telemetry_get_publish_topic( - &hub_client, NULL, thermostat_1.component_name, publish_message.topic, publish_message.topic_length, NULL))) + rc = pnp_telemetry_get_publish_topic( + &hub_client, + NULL, + thermostat_1.component_name, + publish_message.topic, + publish_message.topic_length, + NULL); + if (az_result_failed(rc)) { - IOT_SAMPLE_LOG_ERROR( - "Failed to get the Telemetry topic: az_result return code 0x%08x.", rc); + IOT_SAMPLE_LOG_ERROR("Failed to get the Telemetry topic: az_result return code 0x%08x.", rc); exit(rc); } @@ -903,12 +925,16 @@ static void send_telemetry_messages(void) // Temperature Sensor 2 // Get the Telemetry topic to publish the telemetry message. - if (az_result_failed( - rc = pnp_telemetry_get_publish_topic( - &hub_client, NULL, thermostat_2.component_name, publish_message.topic, publish_message.topic_length, NULL))) + rc = pnp_telemetry_get_publish_topic( + &hub_client, + NULL, + thermostat_2.component_name, + publish_message.topic, + publish_message.topic_length, + NULL); + if (az_result_failed(rc)) { - IOT_SAMPLE_LOG_ERROR( - "Failed to get the Telemetry topic: az_result return code 0x%08x.", rc); + IOT_SAMPLE_LOG_ERROR("Failed to get the Telemetry topic: az_result return code 0x%08x.", rc); exit(rc); } @@ -924,11 +950,11 @@ static void send_telemetry_messages(void) // Temperature Controller // Get the Telemetry topic to publish the telemetry message. - rc = pnp_telemetry_get_publish_topic(&hub_client, NULL, AZ_SPAN_EMPTY, publish_message.topic, publish_message.topic_length, NULL); + rc = pnp_telemetry_get_publish_topic( + &hub_client, NULL, AZ_SPAN_EMPTY, publish_message.topic, publish_message.topic_length, NULL); if (az_result_failed(rc)) { - IOT_SAMPLE_LOG_ERROR( - "Failed to get the Telemetry topic: az_result return code 0x%08x.", rc); + IOT_SAMPLE_LOG_ERROR("Failed to get the Telemetry topic: az_result return code 0x%08x.", rc); exit(rc); } @@ -948,13 +974,14 @@ static void temp_controller_build_telemetry_message(az_span payload, az_span* ou int32_t working_set_ram_in_kibibytes = rand() % 128; - if (az_result_failed( - rc = pnp_build_telemetry_message( - payload, - telemetry_working_set_name, - append_int32_callback, - (void*)&working_set_ram_in_kibibytes, - out_payload))) + rc = pnp_build_telemetry_message( + payload, + telemetry_working_set_name, + append_int32_callback, + (void*)&working_set_ram_in_kibibytes, + out_payload); + + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to build Telemetry message for Temperature Controller: az_result return code " @@ -968,9 +995,7 @@ static void temp_controller_build_serial_number_reported_property( az_span payload, az_span* out_payload) { - az_result rc; - - rc = pnp_build_reported_property( + az_result rc = pnp_build_reported_property( payload, AZ_SPAN_EMPTY, twin_reported_serial_number_property_name, @@ -998,19 +1023,18 @@ static void temp_controller_build_error_reported_property_with_status( az_span payload, az_span* out_payload) { - az_result rc; + az_result rc = pnp_build_reported_property_with_status( + payload, + component_name, + property_name, + append_json_token_callback, + (void*)property_value, + (int32_t)status, + version, + twin_response_failed, + out_payload); - if (az_result_failed( - rc = pnp_build_reported_property_with_status( - payload, - component_name, - property_name, - append_json_token_callback, - (void*)property_value, - (int32_t)status, - version, - twin_response_failed, - out_payload))) + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to build Temperature Controller error payload: az_result return code 0x%08x.", rc); @@ -1060,7 +1084,7 @@ static az_result temp_controller_process_command_request( *out_status = AZ_IOT_STATUS_NOT_FOUND; IOT_SAMPLE_LOG_AZ_SPAN("Command not supported on Temperature Controller:", command_name); - return AZ_ERROR_ITEM_NOT_FOUND; // Unsupported command or not this component's command + return AZ_ERROR_ITEM_NOT_FOUND; // Unsupported command } return AZ_OK; @@ -1084,8 +1108,8 @@ static void temp_controller_invoke_reboot(void) IOT_SAMPLE_LOG_SUCCESS("Client subscribed to IoT Hub topics."); // Initializations - int rc; - if (az_result_failed(rc = pnp_mqtt_message_init(&publish_message))) + int rc = pnp_mqtt_message_init(&publish_message); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to initialize pnp_mqtt_message: az_result return code 0x%08x.", rc); @@ -1112,26 +1136,29 @@ static void property_callback( (void)user_context_callback; // Get the Twin Patch topic to send a property update. - if (az_result_failed( - rc = az_iot_hub_client_twin_patch_get_publish_topic( - &hub_client, pnp_mqtt_get_request_id(), publish_message.topic, publish_message.topic_length, NULL))) + rc = az_iot_hub_client_twin_patch_get_publish_topic( + &hub_client, + pnp_mqtt_get_request_id(), + publish_message.topic, + publish_message.topic_length, + NULL); + if (az_result_failed(rc)) { - IOT_SAMPLE_LOG_ERROR( - "Failed to get the Twin Patch topic: az_result return code 0x%08x.", rc); + IOT_SAMPLE_LOG_ERROR("Failed to get the Twin Patch topic: az_result return code 0x%08x.", rc); exit(rc); } // Attempt to process property update per component until find success or exit on error. if (az_span_is_content_equal(thermostat_1.component_name, component_name)) { - if (az_result_failed( - rc = pnp_thermostat_process_property_update( - &thermostat_1, - property_name, - &property_value, - version, - publish_message.payload, - &publish_message.out_payload))) + rc = pnp_thermostat_process_property_update( + &thermostat_1, + property_name, + &property_value, + version, + publish_message.payload, + &publish_message.out_payload); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Temperature Sensor 1 does not support writeable property `%.*s`.", @@ -1151,14 +1178,14 @@ static void property_callback( } else if (az_span_is_content_equal(thermostat_2.component_name, component_name)) { - if (az_result_failed( - rc = pnp_thermostat_process_property_update( - &thermostat_2, - property_name, - &property_value, - version, - publish_message.payload, - &publish_message.out_payload))) + rc = pnp_thermostat_process_property_update( + &thermostat_2, + property_name, + &property_value, + version, + publish_message.payload, + &publish_message.out_payload); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Temperature Sensor 2 does not support writeable property `%.*s`.", @@ -1178,14 +1205,14 @@ static void property_callback( } else if (az_span_size(component_name) == 0) { - if (az_result_failed( - rc = temp_controller_process_property_update( - component_name, - property_name, - &property_value, - version, - publish_message.payload, - &publish_message.out_payload))) + rc = temp_controller_process_property_update( + component_name, + property_name, + &property_value, + version, + publish_message.payload, + &publish_message.out_payload); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Temperature Controller does not support writable property `%.*s`. All writeable " diff --git a/sdk/samples/iot/paho_iot_hub_pnp_sample.c b/sdk/samples/iot/paho_iot_hub_pnp_sample.c index d74338fe92..44ee787315 100644 --- a/sdk/samples/iot/paho_iot_hub_pnp_sample.c +++ b/sdk/samples/iot/paho_iot_hub_pnp_sample.c @@ -45,9 +45,9 @@ bool is_device_operational = true; static char const iso_spec_time_format[] = "%Y-%m-%dT%H:%M:%S%z"; // ISO8601 Time Format // * PnP Values * -// The model id is the JSON document (also called the Digital Twins Model Identifier or DTMI) -// which defines the capability of your device. The functionality of the device should match what -// is described in the corresponding DTMI. Should you choose to program your own PnP capable device, +// The model id is the JSON document (also called the Digital Twins Model Identifier or DTMI) which +// defines the capability of your device. The functionality of the device should match what is +// described in the corresponding DTMI. Should you choose to program your own PnP capable device, // the functionality would need to match the DTMI and you would need to update the below 'model_id'. // Please see the sample README for more information on this DTMI. static az_span const model_id = AZ_SPAN_LITERAL_FROM_STR("dtmi:com:example:Thermostat;1"); @@ -170,10 +170,10 @@ static az_result build_property_payload_with_status( * - A reported property named `maxTempSinceLastReboot` with a `double` value for the highest * temperature reached since device boot. * - * To send a device twin desired property message, select your device's Device Twin tab - * in the Azure IoT Explorer. Add the property targetTemperature along with a corresponding value to - * the desired section of the JSON. Select Save to update the twin document and send the twin - * message to the device. + * To send a device twin desired property message, select your device's Device Twin tab in the Azure + * IoT Explorer. Add the property targetTemperature along with a corresponding value to the desired + * section of the JSON. Select Save to update the twin document and send the twin message to the + * device. * { * "properties": { * "desired": { @@ -218,8 +218,8 @@ static az_result build_property_payload_with_status( * "endTime": "2020-08-18T17:24:32-0700" * } * - * Telemetry: Device sends a JSON message with the field name `temperature` and the `double` - * value of the current temperature. + * Telemetry: Device sends a JSON message with the field name `temperature` and the `double` value + * of the current temperature. */ int main(void) { @@ -246,8 +246,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.", @@ -257,9 +257,9 @@ 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); @@ -268,9 +268,9 @@ static void create_and_configure_mqtt_client(void) // Initialize the hub client with the connection options. az_iot_hub_client_options options = az_iot_hub_client_options_default(); options.model_id = model_id; - if (az_result_failed( - rc = az_iot_hub_client_init( - &hub_client, env_vars.hub_hostname, env_vars.hub_device_id, &options))) + + rc = az_iot_hub_client_init(&hub_client, env_vars.hub_hostname, env_vars.hub_device_id, &options); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to initialize hub client: az_result return code 0x%08x.", rc); exit(rc); @@ -278,22 +278,18 @@ static void create_and_configure_mqtt_client(void) // 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); @@ -305,9 +301,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); @@ -329,7 +325,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" @@ -345,8 +342,8 @@ static void subscribe_mqtt_client_to_iot_hub_topics(void) int rc; // Messages received on the Methods topic will be commands to be invoked. - if ((rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC, 1)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_METHODS_SUBSCRIBE_TOPIC, 1); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to subscribe to the Methods topic: MQTTClient return code %d.", rc); @@ -354,8 +351,8 @@ static void subscribe_mqtt_client_to_iot_hub_topics(void) } // Messages received on the Twin Patch topic will be updates to the desired properties. - if ((rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC, 1)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC, 1); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to subscribe to the Twin Patch topic: MQTTClient return code %d.", rc); @@ -363,8 +360,8 @@ static void subscribe_mqtt_client_to_iot_hub_topics(void) } // Messages received on Twin Response topic will be response statuses from the server. - if ((rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC, 1)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC, 1); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to subscribe to the Twin Response topic: MQTTClient return code %d.", rc); @@ -380,13 +377,14 @@ static void request_device_twin_document(void) // Get the Twin Document topic to publish the twin document request. char twin_document_topic_buffer[128]; - if (az_result_failed( - rc = az_iot_hub_client_twin_document_get_publish_topic( - &hub_client, - get_request_id(), - twin_document_topic_buffer, - sizeof(twin_document_topic_buffer), - NULL))) + rc = az_iot_hub_client_twin_document_get_publish_topic( + &hub_client, + get_request_id(), + twin_document_topic_buffer, + sizeof(twin_document_topic_buffer), + NULL); + + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to get the Twin Document topic: az_result return code %04x", rc); exit(rc); @@ -407,13 +405,15 @@ static void receive_messages(void) // Continue to receive commands or device twin messages while device is operational. while (is_device_operational) { - IOT_SAMPLE_LOG(" "); // Formatting. + IOT_SAMPLE_LOG(" "); // Formatting IOT_SAMPLE_LOG("Waiting for command request or device twin 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: MQTTClient return code %d.", rc); exit(rc); @@ -432,7 +432,7 @@ static void receive_messages(void) else { IOT_SAMPLE_LOG_SUCCESS("Client received a message from the service."); - timeout_counter = 0; // Reset. + timeout_counter = 0; // Reset if (rc == MQTTCLIENT_TOPICNAME_TRUNCATED) { @@ -440,7 +440,7 @@ static void receive_messages(void) } on_message_received(topic, topic_len, message); - IOT_SAMPLE_LOG(" "); // Formatting. + IOT_SAMPLE_LOG(" "); // Formatting MQTTClient_freeMessage(&message); MQTTClient_free(topic); @@ -452,9 +452,8 @@ static void receive_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); @@ -470,7 +469,8 @@ static az_span get_request_id(void) az_span out_span = az_span_create( (uint8_t*)connection_request_id_buffer, sizeof(connection_request_id_buffer)); - if (az_result_failed(rc = az_span_u32toa(out_span, connection_request_id_int++, &remainder))) + rc = az_span_u32toa(out_span, connection_request_id_int++, &remainder); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to get request id: az_result return code 0x%08x.", rc); exit(rc); @@ -483,9 +483,9 @@ static void publish_mqtt_message(const char* topic, az_span payload, int qos) { int rc; - if ((rc = MQTTClient_publish( - mqtt_client, topic, az_span_size(payload), az_span_ptr(payload), qos, 0, NULL)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_publish( + mqtt_client, topic, az_span_size(payload), az_span_ptr(payload), qos, 0, NULL); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR("Failed to publish message: MQTTClient return code %d", rc); exit(rc); @@ -503,9 +503,8 @@ static void on_message_received(char* topic, int topic_len, MQTTClient_message c az_iot_hub_client_method_request command_request; // Parse the incoming message topic and handle appropriately. - if (az_result_succeeded( - rc - = az_iot_hub_client_twin_parse_received_topic(&hub_client, topic_span, &twin_response))) + rc = az_iot_hub_client_twin_parse_received_topic(&hub_client, topic_span, &twin_response); + if (az_result_succeeded(rc)) { IOT_SAMPLE_LOG_SUCCESS("Client received a valid topic response."); IOT_SAMPLE_LOG_AZ_SPAN("Topic:", topic_span); @@ -514,21 +513,23 @@ static void on_message_received(char* topic, int topic_len, MQTTClient_message c handle_device_twin_message(message, &twin_response); } - else if (az_result_succeeded( - rc = az_iot_hub_client_methods_parse_received_topic( - &hub_client, topic_span, &command_request))) - { - IOT_SAMPLE_LOG_SUCCESS("Client received a valid topic response."); - IOT_SAMPLE_LOG_AZ_SPAN("Topic:", topic_span); - IOT_SAMPLE_LOG_AZ_SPAN("Payload:", message_span); - - handle_command_request(message, &command_request); - } else { - IOT_SAMPLE_LOG_ERROR("Message from unknown topic: az_result return code 0x%08x.", rc); - IOT_SAMPLE_LOG_AZ_SPAN("Topic:", topic_span); - exit(rc); + rc = az_iot_hub_client_methods_parse_received_topic(&hub_client, topic_span, &command_request); + if (az_result_succeeded(rc)) + { + IOT_SAMPLE_LOG_SUCCESS("Client received a valid topic response."); + IOT_SAMPLE_LOG_AZ_SPAN("Topic:", topic_span); + IOT_SAMPLE_LOG_AZ_SPAN("Payload:", message_span); + + handle_command_request(message, &command_request); + } + else + { + IOT_SAMPLE_LOG_ERROR("Message from unknown topic: az_result return code 0x%08x.", rc); + IOT_SAMPLE_LOG_AZ_SPAN("Topic:", topic_span); + exit(rc); + } } } @@ -570,9 +571,9 @@ static void process_device_twin_message(az_span message_span, bool is_twin_get) int32_t version_number; // Parse for the desired temperature property. - if (az_result_failed( - rc = parse_desired_temperature_property( - message_span, is_twin_get, &property_found, &desired_temperature, &version_number))) + rc = parse_desired_temperature_property( + message_span, is_twin_get, &property_found, &desired_temperature, &version_number); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to parse for desired temperature property: az_result return code 0x%08x.", rc); @@ -581,7 +582,7 @@ static void process_device_twin_message(az_span message_span, bool is_twin_get) if (property_found) { - IOT_SAMPLE_LOG(" "); // Formatting. + IOT_SAMPLE_LOG(" "); // Formatting bool confirm = true; bool is_max_temp_changed; @@ -731,13 +732,13 @@ static void send_reported_property(az_span name, double value, int32_t version, // Get the Twin Patch topic to send a reported property update. char twin_patch_topic_buffer[128]; - if (az_result_failed( - rc = az_iot_hub_client_twin_patch_get_publish_topic( - &hub_client, - get_request_id(), - twin_patch_topic_buffer, - sizeof(twin_patch_topic_buffer), - NULL))) + rc = az_iot_hub_client_twin_patch_get_publish_topic( + &hub_client, + get_request_id(), + twin_patch_topic_buffer, + sizeof(twin_patch_topic_buffer), + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to get the Twin Patch topic: az_result return code 0x%08x.", rc); exit(rc); @@ -748,15 +749,15 @@ static void send_reported_property(az_span name, double value, int32_t version, az_span reported_property_payload = AZ_SPAN_FROM_BUFFER(reported_property_payload_buffer); if (confirm) { - if (az_result_failed( - rc = build_property_payload_with_status( - name, - value, - AZ_IOT_STATUS_OK, - version, - twin_success_name, - reported_property_payload, - &reported_property_payload))) + rc = build_property_payload_with_status( + name, + value, + AZ_IOT_STATUS_OK, + version, + twin_success_name, + reported_property_payload, + &reported_property_payload); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to build reported property confirmed payload : az_result return code 0x%08x.", @@ -770,9 +771,9 @@ static void send_reported_property(az_span name, double value, int32_t version, az_span const names[1] = { name }; double const values[1] = { value }; - if (az_result_failed( - rc = build_property_payload( - count, names, values, NULL, reported_property_payload, &reported_property_payload))) + rc = build_property_payload( + count, names, values, NULL, reported_property_payload, &reported_property_payload); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to build reported property payload: az_result return code 0x%08x.", rc); @@ -828,14 +829,14 @@ static void send_command_response( // Get the Methods response topic to publish the command response. char methods_response_topic_buffer[128]; - if (az_result_failed( - rc = az_iot_hub_client_methods_response_get_publish_topic( - &hub_client, - command_request->request_id, - (uint16_t)status, - methods_response_topic_buffer, - sizeof(methods_response_topic_buffer), - NULL))) + rc = az_iot_hub_client_methods_response_get_publish_topic( + &hub_client, + command_request->request_id, + (uint16_t)status, + methods_response_topic_buffer, + sizeof(methods_response_topic_buffer), + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to get the Methods Response topic: az_result return code 0x%08x.", rc); @@ -896,9 +897,8 @@ static az_result invoke_getMaxMinReport(az_span payload, az_span response, az_sp = { device_maximum_temperature, device_minimum_temperature, device_average_temperature }; az_span const times[2] = { start_time_span, end_time_span }; - az_result rc; - if (az_result_failed( - rc = build_property_payload(count, names, values, times, response, out_response))) + az_result rc = build_property_payload(count, names, values, times, response, out_response); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to build the Command Response payload: az_result return code 0x%08x.", rc); @@ -914,9 +914,9 @@ static void send_telemetry_message(void) // Get the Telemetry topic to publish the telemetry message. char telemetry_topic_buffer[128]; - if (az_result_failed( - rc = az_iot_hub_client_telemetry_get_publish_topic( - &hub_client, NULL, telemetry_topic_buffer, sizeof(telemetry_topic_buffer), NULL))) + rc = az_iot_hub_client_telemetry_get_publish_topic( + &hub_client, NULL, telemetry_topic_buffer, sizeof(telemetry_topic_buffer), NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to get the Telemetry topic: az_result return code 0x%08x.", rc); exit(rc); @@ -929,9 +929,8 @@ static void send_telemetry_message(void) char telemetry_payload_buffer[128]; az_span telemetry_payload = AZ_SPAN_FROM_BUFFER(telemetry_payload_buffer); - if (az_result_failed( - rc = build_property_payload( - count, names, values, NULL, telemetry_payload, &telemetry_payload))) + rc = build_property_payload(count, names, values, NULL, telemetry_payload, &telemetry_payload); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to build the Telemetry payload: az_result return code 0x%08x.", rc); diff --git a/sdk/samples/iot/paho_iot_hub_sas_telemetry_sample.c b/sdk/samples/iot/paho_iot_hub_sas_telemetry_sample.c index fe5f98b9b5..d2bc1785a5 100644 --- a/sdk/samples/iot/paho_iot_hub_sas_telemetry_sample.c +++ b/sdk/samples/iot/paho_iot_hub_sas_telemetry_sample.c @@ -49,8 +49,7 @@ static void disconnect_mqtt_client_from_iot_hub(void); static void generate_sas_key(void); /* - * This sample sends five telemetry messages to the Azure IoT Hub. - * SAS certification is used. + * This sample sends five telemetry messages to the Azure IoT Hub. SAS certification is used. */ int main(void) { @@ -74,8 +73,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.", @@ -85,18 +84,17 @@ 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); @@ -104,22 +102,18 @@ static void create_and_configure_mqtt_client(void) // 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); @@ -134,9 +128,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); @@ -157,7 +151,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" @@ -174,9 +169,9 @@ static void send_telemetry_messages_to_iot_hub(void) // Get the Telemetry topic to publish the telemetry messages. char telemetry_topic_buffer[128]; - if (az_result_failed( - rc = az_iot_hub_client_telemetry_get_publish_topic( - &hub_client, NULL, telemetry_topic_buffer, sizeof(telemetry_topic_buffer), NULL))) + rc = az_iot_hub_client_telemetry_get_publish_topic( + &hub_client, NULL, telemetry_topic_buffer, sizeof(telemetry_topic_buffer), NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to get the Telemetry topic: az_result return code 0x%08x.", rc); exit(rc); @@ -190,15 +185,15 @@ static void send_telemetry_messages_to_iot_hub(void) // Publish # of telemetry messages. for (uint8_t message_count = 0; message_count < MAX_TELEMETRY_MESSAGE_COUNT; message_count++) { - if ((rc = MQTTClient_publish( - mqtt_client, - telemetry_topic_buffer, - (int)strlen(telemetry_message_payloads[message_count]), - telemetry_message_payloads[message_count], - IOT_SAMPLE_MQTT_PUBLISH_QOS, - 0, - NULL)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_publish( + mqtt_client, + telemetry_topic_buffer, + (int)strlen(telemetry_message_payloads[message_count]), + telemetry_message_payloads[message_count], + IOT_SAMPLE_MQTT_PUBLISH_QOS, + 0, + NULL); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to publish Telemetry message #%d: MQTTClient return code %d.", @@ -216,9 +211,8 @@ static void send_telemetry_messages_to_iot_hub(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); @@ -237,16 +231,16 @@ static void generate_sas_key(void) // Get the signature that will later be signed with the decoded key. az_span sas_signature = AZ_SPAN_FROM_BUFFER(sas_signature_buffer); - if (az_result_failed( - rc = az_iot_hub_client_sas_get_signature( - &hub_client, sas_duration, sas_signature, &sas_signature))) + rc = az_iot_hub_client_sas_get_signature( + &hub_client, sas_duration, sas_signature, &sas_signature); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Could not get the signature for SAS key: az_result return code 0x%08x.", rc); exit(rc); } - // Generate the encoded, signed signature (b64 encoded, HMAC-SHA256 signing) + // Generate the encoded, signed signature (b64 encoded, HMAC-SHA256 signing). az_span sas_base64_encoded_signed_signature = AZ_SPAN_FROM_BUFFER(sas_base64_encoded_signed_signature_buffer); iot_sample_generate_sas_base64_encoded_signed_signature( @@ -255,7 +249,7 @@ static void generate_sas_key(void) sas_base64_encoded_signed_signature, &sas_base64_encoded_signed_signature); - // Get the resulting MQTT password, passing the base64 encoded, HMAC signed bytes + // Get the resulting MQTT password, passing the base64 encoded, HMAC signed bytes. size_t mqtt_password_length; rc = az_iot_hub_client_sas_get_password( &hub_client, diff --git a/sdk/samples/iot/paho_iot_hub_telemetry_sample.c b/sdk/samples/iot/paho_iot_hub_telemetry_sample.c index 89651277dc..fc7d1de3a7 100644 --- a/sdk/samples/iot/paho_iot_hub_telemetry_sample.c +++ b/sdk/samples/iot/paho_iot_hub_telemetry_sample.c @@ -42,8 +42,7 @@ static void send_telemetry_messages_to_iot_hub(void); static void disconnect_mqtt_client_from_iot_hub(void); /* - * This sample sends five telemetry messages to the Azure IoT Hub. - * X509 self-certification is used. + * This sample sends five telemetry messages to the Azure IoT Hub. X509 self-certification is used. */ int main(void) { @@ -67,8 +66,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.", @@ -78,18 +77,17 @@ 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); @@ -97,22 +95,18 @@ static void create_and_configure_mqtt_client(void) // 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); @@ -124,9 +118,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); @@ -148,7 +142,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" @@ -165,9 +160,9 @@ static void send_telemetry_messages_to_iot_hub(void) // Get the Telemetry topic to publish the telemetry messages. char telemetry_topic_buffer[128]; - if (az_result_failed( - rc = az_iot_hub_client_telemetry_get_publish_topic( - &hub_client, NULL, telemetry_topic_buffer, sizeof(telemetry_topic_buffer), NULL))) + rc = az_iot_hub_client_telemetry_get_publish_topic( + &hub_client, NULL, telemetry_topic_buffer, sizeof(telemetry_topic_buffer), NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to get the Telemetry topic: az_result return code 0x%08x.", rc); exit(rc); @@ -181,15 +176,15 @@ static void send_telemetry_messages_to_iot_hub(void) // Publish # of telemetry messages. for (uint8_t message_count = 0; message_count < MAX_TELEMETRY_MESSAGE_COUNT; message_count++) { - if ((rc = MQTTClient_publish( - mqtt_client, - telemetry_topic_buffer, - (int)strlen(telemetry_message_payloads[message_count]), - telemetry_message_payloads[message_count], - IOT_SAMPLE_MQTT_PUBLISH_QOS, - 0, - NULL)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_publish( + mqtt_client, + telemetry_topic_buffer, + (int)strlen(telemetry_message_payloads[message_count]), + telemetry_message_payloads[message_count], + IOT_SAMPLE_MQTT_PUBLISH_QOS, + 0, + NULL); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to publish Telemetry message #%d: MQTTClient return code %d.", @@ -207,9 +202,8 @@ static void send_telemetry_messages_to_iot_hub(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); diff --git a/sdk/samples/iot/paho_iot_hub_twin_sample.c b/sdk/samples/iot/paho_iot_hub_twin_sample.c index edce447303..f27f219598 100644 --- a/sdk/samples/iot/paho_iot_hub_twin_sample.c +++ b/sdk/samples/iot/paho_iot_hub_twin_sample.c @@ -79,7 +79,7 @@ static az_result build_reported_property( * A property named `device_count` is supported for this sample. To send a device twin desired * property message, select your device's Device Twin tab in the Azure Portal of your IoT Hub. Add * the property `device_count` along with a corresponding value to the `desired` section of the - * JSON. Select Save to udate the twin document and send the twin message to the device. + * JSON. Select Save to update the twin document and send the twin message to the device. * * { * "properties": { @@ -116,8 +116,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.", @@ -127,18 +127,17 @@ 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); @@ -146,22 +145,18 @@ static void create_and_configure_mqtt_client(void) // 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); @@ -173,9 +168,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); @@ -197,7 +192,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" @@ -213,8 +209,8 @@ static void subscribe_mqtt_client_to_iot_hub_topics(void) int rc; // Messages received on the Twin Patch topic will be updates to the desired properties. - if ((rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC, 1)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_PATCH_SUBSCRIBE_TOPIC, 1); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to subscribe to the Twin Patch topic: MQTTClient return code %d.", rc); @@ -222,8 +218,8 @@ static void subscribe_mqtt_client_to_iot_hub_topics(void) } // Messages received on Twin Response topic will be response statuses from the server. - if ((rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC, 1)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_TWIN_RESPONSE_SUBSCRIBE_TOPIC, 1); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to subscribe to the Twin Response topic: MQTTClient return code %d.", rc); @@ -235,7 +231,7 @@ static void send_and_receive_device_twin_messages(void) { get_device_twin_document(); (void)receive_device_twin_message(); - IOT_SAMPLE_LOG(" "); // Formatting. + IOT_SAMPLE_LOG(" "); // Formatting send_reported_property(); (void)receive_device_twin_message(); @@ -254,9 +250,8 @@ static void send_and_receive_device_twin_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); @@ -273,13 +268,13 @@ static void get_device_twin_document(void) // Get the Twin Document topic to publish the twin document request. char twin_document_topic_buffer[128]; - if (az_result_failed( - rc = az_iot_hub_client_twin_document_get_publish_topic( - &hub_client, - twin_document_topic_request_id, - twin_document_topic_buffer, - sizeof(twin_document_topic_buffer), - NULL))) + rc = az_iot_hub_client_twin_document_get_publish_topic( + &hub_client, + twin_document_topic_request_id, + twin_document_topic_buffer, + sizeof(twin_document_topic_buffer), + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to get the Twin Document topic: az_result return code 0x%08x.", rc); @@ -287,9 +282,9 @@ static void get_device_twin_document(void) } // Publish the twin document request. - if ((rc = MQTTClient_publish( - mqtt_client, twin_document_topic_buffer, 0, NULL, IOT_SAMPLE_MQTT_PUBLISH_QOS, 0, NULL)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_publish( + mqtt_client, twin_document_topic_buffer, 0, NULL, IOT_SAMPLE_MQTT_PUBLISH_QOS, 0, NULL); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to publish the Twin Document request: MQTTClient return code %d.", rc); @@ -305,13 +300,13 @@ static void send_reported_property(void) // Get the Twin Patch topic to publish a reported property update. char twin_patch_topic_buffer[128]; - if (az_result_failed( - rc = az_iot_hub_client_twin_patch_get_publish_topic( - &hub_client, - twin_patch_topic_request_id, - twin_patch_topic_buffer, - sizeof(twin_patch_topic_buffer), - NULL))) + rc = az_iot_hub_client_twin_patch_get_publish_topic( + &hub_client, + twin_patch_topic_request_id, + twin_patch_topic_buffer, + sizeof(twin_patch_topic_buffer), + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to get the Twin Patch topic: az_result return code 0x%08x.", rc); exit(rc); @@ -320,8 +315,9 @@ static void send_reported_property(void) // Build the updated reported property message. char reported_property_payload_buffer[128]; az_span reported_property_payload = AZ_SPAN_FROM_BUFFER(reported_property_payload_buffer); - if (az_result_failed( - rc = build_reported_property(reported_property_payload, &reported_property_payload))) + + rc = build_reported_property(reported_property_payload, &reported_property_payload); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to build reported property payload: az_result return code 0x%08x.", rc); @@ -329,15 +325,15 @@ static void send_reported_property(void) } // Publish the reported property update. - if ((rc = MQTTClient_publish( - mqtt_client, - twin_patch_topic_buffer, - az_span_size(reported_property_payload), - az_span_ptr(reported_property_payload), - IOT_SAMPLE_MQTT_PUBLISH_QOS, - 0, - NULL)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_publish( + mqtt_client, + twin_patch_topic_buffer, + az_span_size(reported_property_payload), + az_span_ptr(reported_property_payload), + IOT_SAMPLE_MQTT_PUBLISH_QOS, + 0, + NULL); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to publish the Twin Patch reported property update: MQTTClient return code %d.", @@ -359,9 +355,12 @@ static bool receive_device_twin_message(void) IOT_SAMPLE_LOG(" "); // Formatting IOT_SAMPLE_LOG("Waiting for device twin 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: MQTTClient return code %d.", rc); exit(rc); @@ -396,14 +395,13 @@ static void parse_device_twin_message( MQTTClient_message const* message, az_iot_hub_client_twin_response* out_twin_response) { - 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 twin_response info. - if (az_result_failed( - rc = az_iot_hub_client_twin_parse_received_topic( - &hub_client, topic_span, out_twin_response))) + az_result rc + = az_iot_hub_client_twin_parse_received_topic(&hub_client, topic_span, out_twin_response); + 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); @@ -419,7 +417,6 @@ static void handle_device_twin_message( MQTTClient_message const* message, az_iot_hub_client_twin_response const* twin_response) { - az_result rc; az_span const message_span = az_span_create((uint8_t*)message->payload, message->payloadlen); // Invoke appropriate action per response type (3 types only). @@ -440,9 +437,9 @@ static void handle_device_twin_message( int32_t desired_device_count; // Parse for the device count property. - if (az_result_failed( - rc = parse_desired_device_count_property( - message_span, &property_found, &desired_device_count))) + az_result rc = parse_desired_device_count_property( + message_span, &property_found, &desired_device_count); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to parse for desired device_count property: az_result return code 0x%08x.", rc); @@ -451,12 +448,12 @@ static void handle_device_twin_message( if (property_found) { - IOT_SAMPLE_LOG(" "); // Formatting. + IOT_SAMPLE_LOG(" "); // Formatting // Update device count locally and report update to server. update_device_count_property(desired_device_count); send_reported_property(); - receive_device_twin_message(); + (void)receive_device_twin_message(); } break; } diff --git a/sdk/samples/iot/paho_iot_provisioning_sample.c b/sdk/samples/iot/paho_iot_provisioning_sample.c index a7826888c9..6af9d4064b 100644 --- a/sdk/samples/iot/paho_iot_provisioning_sample.c +++ b/sdk/samples/iot/paho_iot_provisioning_sample.c @@ -56,9 +56,8 @@ static void send_operation_query_message( az_iot_provisioning_client_register_response const* response); /* - * This sample registers a device with the Azure IoT Device Provisioning Service. - * It will wait to receive the registration status before disconnecting. - * X509 self-certification is used. + * This sample registers a device with the Azure IoT Device Provisioning Service. It will wait to + * receive the registration status before disconnecting. X509 self-certification is used. */ int main(void) { @@ -88,8 +87,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 environment variables: az_result return code 0x%08x.", rc); exit(rc); @@ -97,9 +96,9 @@ static void create_and_configure_mqtt_client(void) // Build an MQTT endpoint c-string. char mqtt_endpoint_buffer[256]; - 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); @@ -107,13 +106,13 @@ static void create_and_configure_mqtt_client(void) // Initialize the provisioning client with the provisioning global endpoint and the default // connection options. - if (az_result_failed( - rc = az_iot_provisioning_client_init( - &provisioning_client, - az_span_create_from_str(mqtt_endpoint_buffer), - env_vars.provisioning_id_scope, - env_vars.provisioning_registration_id, - NULL))) + rc = az_iot_provisioning_client_init( + &provisioning_client, + az_span_create_from_str(mqtt_endpoint_buffer), + env_vars.provisioning_id_scope, + env_vars.provisioning_registration_id, + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to initialize provisioning client: az_result return code 0x%08x.", rc); @@ -122,22 +121,18 @@ static void create_and_configure_mqtt_client(void) // Get the MQTT client id used for the MQTT connection. char mqtt_client_id_buffer[128]; - if (az_result_failed( - rc = az_iot_provisioning_client_get_client_id( - &provisioning_client, mqtt_client_id_buffer, sizeof(mqtt_client_id_buffer), NULL))) + rc = az_iot_provisioning_client_get_client_id( + &provisioning_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); @@ -146,15 +141,10 @@ static void create_and_configure_mqtt_client(void) static void connect_mqtt_client_to_provisioning_service(void) { - int rc; - // Get the MQTT client username. - if (az_result_failed( - rc = az_iot_provisioning_client_get_user_name( - &provisioning_client, - mqtt_client_username_buffer, - sizeof(mqtt_client_username_buffer), - NULL))) + int rc = az_iot_provisioning_client_get_user_name( + &provisioning_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); @@ -176,7 +166,8 @@ static void connect_mqtt_client_to_provisioning_service(void) mqtt_connect_options.ssl = &mqtt_ssl_options; // Connect MQTT client to the Azure IoT Device Provisioning Service. - 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" @@ -189,12 +180,10 @@ static void connect_mqtt_client_to_provisioning_service(void) static void subscribe_mqtt_client_to_provisioning_service_topics(void) { - int rc; - // Messages received on the Register topic will be registration responses from the server. - if ((rc - = MQTTClient_subscribe(mqtt_client, AZ_IOT_PROVISIONING_CLIENT_REGISTER_SUBSCRIBE_TOPIC, 1)) - != MQTTCLIENT_SUCCESS) + int rc + = MQTTClient_subscribe(mqtt_client, AZ_IOT_PROVISIONING_CLIENT_REGISTER_SUBSCRIBE_TOPIC, 1); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( "Failed to subscribe to the Register topic: MQTTClient return code %d.", rc); @@ -208,9 +197,9 @@ static void register_device_with_provisioning_service(void) // Get the Register topic to publish the register request. char register_topic_buffer[128]; - if (az_result_failed( - rc = az_iot_provisioning_client_register_get_publish_topic( - &provisioning_client, register_topic_buffer, sizeof(register_topic_buffer), NULL))) + rc = az_iot_provisioning_client_register_get_publish_topic( + &provisioning_client, register_topic_buffer, sizeof(register_topic_buffer), NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to get the Register topic: az_result return code 0x%08x.", rc); exit(rc); @@ -224,8 +213,8 @@ static void register_device_with_provisioning_service(void) pubmsg.retained = 0; // Publish the register request. - if ((rc = MQTTClient_publishMessage(mqtt_client, register_topic_buffer, &pubmsg, NULL)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_publishMessage(mqtt_client, register_topic_buffer, &pubmsg, NULL); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR("Failed to publish Register request: MQTTClient return code %d.", rc); exit(rc); @@ -240,27 +229,29 @@ static void receive_device_registration_status_message(void) MQTTClient_message* message = NULL; bool is_operation_complete = false; - // Continue to parse incoming responses from the provisioning service until the device - // has been successfully provisioned or an error occurs. + // Continue to parse incoming responses from the provisioning service until the device has been + // successfully provisioned or an error occurs. do { IOT_SAMPLE_LOG(" "); // Formatting IOT_SAMPLE_LOG("Waiting for registration status message.\n"); - if (((rc - = MQTTClient_receive(mqtt_client, &topic, &topic_len, &message, MQTT_TIMEOUT_RECEIVE_MS)) - != MQTTCLIENT_SUCCESS) - && (MQTTCLIENT_TOPICNAME_TRUNCATED != rc)) + // 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: MQTTClient return code %d.", rc); exit(rc); } - else if (NULL == message) + else if (message == NULL) { IOT_SAMPLE_LOG_ERROR("Receive message timeout expired: MQTTClient return code %d.", rc); exit(rc); } - else if (MQTTCLIENT_TOPICNAME_TRUNCATED == rc) + else if (rc == MQTTCLIENT_TOPICNAME_TRUNCATED) { topic_len = (int)strlen(topic); } @@ -284,9 +275,8 @@ static void receive_device_registration_status_message(void) static void disconnect_mqtt_client_from_provisioning_service(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); @@ -307,9 +297,9 @@ static void parse_device_registration_status_message( az_span const message_span = az_span_create((uint8_t*)message->payload, message->payloadlen); // Parse message and retrieve register_response info. - if (az_result_failed( - rc = az_iot_provisioning_client_parse_received_topic_and_payload( - &provisioning_client, topic_span, message_span, out_register_response))) + rc = az_iot_provisioning_client_parse_received_topic_and_payload( + &provisioning_client, topic_span, message_span, out_register_response); + 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); @@ -321,9 +311,9 @@ static void parse_device_registration_status_message( IOT_SAMPLE_LOG("Status: %d", out_register_response->status); // Retrieve operation_status. - if (az_result_failed( - rc = az_iot_provisioning_client_parse_operation_status( - out_register_response, out_operation_status))) + rc = az_iot_provisioning_client_parse_operation_status( + out_register_response, out_operation_status); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to parse operation_status: az_result return code 0x%08x.", rc); exit(rc); @@ -363,8 +353,7 @@ static void handle_device_registration_status_message( IOT_SAMPLE_LOG("Last operation status: %d", register_response->status); IOT_SAMPLE_LOG_AZ_SPAN("Operation ID:", register_response->operation_id); IOT_SAMPLE_LOG("Error code: %u", register_response->registration_state.extended_error_code); - IOT_SAMPLE_LOG_AZ_SPAN( - "Error message:", register_response->registration_state.error_message); + IOT_SAMPLE_LOG_AZ_SPAN("Error message:", register_response->registration_state.error_message); IOT_SAMPLE_LOG_AZ_SPAN( "Error timestamp:", register_response->registration_state.error_timestamp); IOT_SAMPLE_LOG_AZ_SPAN( @@ -381,27 +370,27 @@ static void send_operation_query_message( // Get the Query Status topic to publish the query status request. char query_topic_buffer[256]; - if (az_result_failed( - rc = az_iot_provisioning_client_query_status_get_publish_topic( - &provisioning_client, - register_response->operation_id, - query_topic_buffer, - sizeof(query_topic_buffer), - NULL))) + rc = az_iot_provisioning_client_query_status_get_publish_topic( + &provisioning_client, + register_response->operation_id, + query_topic_buffer, + sizeof(query_topic_buffer), + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Unable to get query status publish topic: az_result return code 0x%08x.", rc); exit(rc); } - // IMPORTANT: Wait the recommended retry-after number of seconds before query + // IMPORTANT: Wait the recommended retry-after number of seconds before query. IOT_SAMPLE_LOG("Querying after %u seconds...", register_response->retry_after_seconds); iot_sample_sleep_for_seconds(register_response->retry_after_seconds); // Publish the query status request. - if ((rc = MQTTClient_publish( - mqtt_client, query_topic_buffer, 0, NULL, IOT_SAMPLE_MQTT_PUBLISH_QOS, 0, NULL)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_publish( + mqtt_client, query_topic_buffer, 0, NULL, IOT_SAMPLE_MQTT_PUBLISH_QOS, 0, NULL); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR("Failed to publish query status request: MQTTClient return code %d.", rc); exit(rc); diff --git a/sdk/samples/iot/paho_iot_provisioning_sas_sample.c b/sdk/samples/iot/paho_iot_provisioning_sas_sample.c index 138e9e329d..a1dc35c7f6 100644 --- a/sdk/samples/iot/paho_iot_provisioning_sas_sample.c +++ b/sdk/samples/iot/paho_iot_provisioning_sas_sample.c @@ -62,9 +62,8 @@ static void send_operation_query_message( static void generate_sas_key(void); /* - * This sample registers a device with the Azure IoT Hub Device Provisioning Service. - * It will wait to receive the registration status before disconnecting. - * SAS certification is used. + * This sample registers a device with the Azure IoT Hub Device Provisioning Service. It will wait + * to receive the registration status before disconnecting. SAS certification is used. */ int main(void) { @@ -94,33 +93,32 @@ 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.", - rc); + IOT_SAMPLE_LOG_ERROR("Failed to read environment variables: az_result return code 0x%08x.", rc); exit(rc); } // Build an MQTT endpoint c-string. char mqtt_endpoint_buffer[256]; - 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 provisioning client with the mqtt endpoint and the default connection options. - if (az_result_failed( - rc = az_iot_provisioning_client_init( - &provisioning_client, - az_span_create_from_str(mqtt_endpoint_buffer), - env_vars.provisioning_id_scope, - env_vars.provisioning_registration_id, - NULL))) + // Initialize the provisioning client with the provisioning global endpoint and the default + // connection options. + rc = az_iot_provisioning_client_init( + &provisioning_client, + az_span_create_from_str(mqtt_endpoint_buffer), + env_vars.provisioning_id_scope, + env_vars.provisioning_registration_id, + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to initialize provisioning client: az_result return code 0x%08x.", rc); @@ -129,22 +127,18 @@ static void create_and_configure_mqtt_client(void) // Get the MQTT client id used for the MQTT connection. char mqtt_client_id_buffer[128]; - if (az_result_failed( - rc = az_iot_provisioning_client_get_client_id( - &provisioning_client, mqtt_client_id_buffer, sizeof(mqtt_client_id_buffer), NULL))) + rc = az_iot_provisioning_client_get_client_id( + &provisioning_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); @@ -159,12 +153,9 @@ static void connect_mqtt_client_to_provisioning_service(void) int rc; // Get the MQTT client username. - if (az_result_failed( - rc = az_iot_provisioning_client_get_user_name( - &provisioning_client, - mqtt_client_username_buffer, - sizeof(mqtt_client_username_buffer), - NULL))) + rc = az_iot_provisioning_client_get_user_name( + &provisioning_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); @@ -185,7 +176,8 @@ static void connect_mqtt_client_to_provisioning_service(void) mqtt_connect_options.ssl = &mqtt_ssl_options; // Connect MQTT client to the Azure IoT Device Provisioning Service. - 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" @@ -198,15 +190,13 @@ static void connect_mqtt_client_to_provisioning_service(void) static void subscribe_mqtt_client_to_provisioning_service_topics(void) { - int rc; - // Messages received on the Register topic will be registration responses from the server. - if ((rc - = MQTTClient_subscribe(mqtt_client, AZ_IOT_PROVISIONING_CLIENT_REGISTER_SUBSCRIBE_TOPIC, 1)) - != MQTTCLIENT_SUCCESS) + int rc + = MQTTClient_subscribe(mqtt_client, AZ_IOT_PROVISIONING_CLIENT_REGISTER_SUBSCRIBE_TOPIC, 1); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR( - "Failed to subscribe to the register topic: MQTTClient return code %d.", rc); + "Failed to subscribe to the Register topic: MQTTClient return code %d.", rc); exit(rc); } } @@ -217,9 +207,9 @@ static void register_device_with_provisioning_service(void) // Get the Register topic to publish the register request. char register_topic_buffer[128]; - if (az_result_failed( - rc = az_iot_provisioning_client_register_get_publish_topic( - &provisioning_client, register_topic_buffer, sizeof(register_topic_buffer), NULL))) + rc = az_iot_provisioning_client_register_get_publish_topic( + &provisioning_client, register_topic_buffer, sizeof(register_topic_buffer), NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to get the Register topic: az_result return code 0x%08x.", rc); exit(rc); @@ -233,8 +223,8 @@ static void register_device_with_provisioning_service(void) pubmsg.retained = 0; // Publish the register request. - if ((rc = MQTTClient_publishMessage(mqtt_client, register_topic_buffer, &pubmsg, NULL)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_publishMessage(mqtt_client, register_topic_buffer, &pubmsg, NULL); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR("Failed to publish Register request: MQTTClient return code %d.", rc); exit(rc); @@ -249,33 +239,35 @@ static void receive_device_registration_status_message(void) MQTTClient_message* message = NULL; bool is_operation_complete = false; - // Continue to parse incoming responses from the provisioning service until the device - // has been successfully provisioned or an error occurs. + // Continue to parse incoming responses from the provisioning service until the device has been + // successfully provisioned or an error occurs. do { IOT_SAMPLE_LOG(" "); // Formatting IOT_SAMPLE_LOG("Waiting for registration status message.\n"); - if (((rc - = MQTTClient_receive(mqtt_client, &topic, &topic_len, &message, MQTT_TIMEOUT_RECEIVE_MS)) - != MQTTCLIENT_SUCCESS) - && (MQTTCLIENT_TOPICNAME_TRUNCATED != rc)) + // 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: MQTTClient return code %d.", rc); exit(rc); } - else if (NULL == message) + else if (message == NULL) { IOT_SAMPLE_LOG_ERROR("Receive message timeout expired: MQTTClient return code %d.", rc); exit(rc); } - else if (MQTTCLIENT_TOPICNAME_TRUNCATED == rc) + else if (rc == MQTTCLIENT_TOPICNAME_TRUNCATED) { topic_len = (int)strlen(topic); } IOT_SAMPLE_LOG_SUCCESS("Client received a message from the provisioning service."); - // Parse registration message. + // Parse registration status message. az_iot_provisioning_client_register_response register_response; az_iot_provisioning_client_operation_status operation_status; parse_device_registration_status_message( @@ -293,9 +285,8 @@ static void receive_device_registration_status_message(void) static void disconnect_mqtt_client_from_provisioning_service(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); @@ -316,9 +307,9 @@ static void parse_device_registration_status_message( az_span const message_span = az_span_create((uint8_t*)message->payload, message->payloadlen); // Parse message and retrieve register_response info. - if (az_result_failed( - rc = az_iot_provisioning_client_parse_received_topic_and_payload( - &provisioning_client, topic_span, message_span, out_register_response))) + rc = az_iot_provisioning_client_parse_received_topic_and_payload( + &provisioning_client, topic_span, message_span, out_register_response); + 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); @@ -330,9 +321,9 @@ static void parse_device_registration_status_message( IOT_SAMPLE_LOG("Status: %d", out_register_response->status); // Retrieve operation_status. - if (az_result_failed( - rc = az_iot_provisioning_client_parse_operation_status( - out_register_response, out_operation_status))) + rc = az_iot_provisioning_client_parse_operation_status( + out_register_response, out_operation_status); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to parse operation_status: az_result return code 0x%08x.", rc); exit(rc); @@ -372,8 +363,7 @@ static void handle_device_registration_status_message( IOT_SAMPLE_LOG("Last operation status: %d", register_response->status); IOT_SAMPLE_LOG_AZ_SPAN("Operation ID:", register_response->operation_id); IOT_SAMPLE_LOG("Error code: %u", register_response->registration_state.extended_error_code); - IOT_SAMPLE_LOG_AZ_SPAN( - "Error message:", register_response->registration_state.error_message); + IOT_SAMPLE_LOG_AZ_SPAN("Error message:", register_response->registration_state.error_message); IOT_SAMPLE_LOG_AZ_SPAN( "Error timestamp:", register_response->registration_state.error_timestamp); IOT_SAMPLE_LOG_AZ_SPAN( @@ -384,33 +374,33 @@ static void handle_device_registration_status_message( } static void send_operation_query_message( - const az_iot_provisioning_client_register_response* register_response) + az_iot_provisioning_client_register_response const* register_response) { int rc; // Get the Query Status topic to publish the query status request. - char query_status_topic_buffer[256]; - if (az_result_failed( - rc = az_iot_provisioning_client_query_status_get_publish_topic( - &provisioning_client, - register_response->operation_id, - query_status_topic_buffer, - sizeof(query_status_topic_buffer), - NULL))) + char query_topic_buffer[256]; + rc = az_iot_provisioning_client_query_status_get_publish_topic( + &provisioning_client, + register_response->operation_id, + query_topic_buffer, + sizeof(query_topic_buffer), + NULL); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Unable to get query status publish topic: az_result return code 0x%08x.", rc); exit(rc); } - // IMPORTANT: Wait the recommended retry-after number of seconds before query + // IMPORTANT: Wait the recommended retry-after number of seconds before query. IOT_SAMPLE_LOG("Querying after %u seconds...", register_response->retry_after_seconds); iot_sample_sleep_for_seconds(register_response->retry_after_seconds); // Publish the query status request. - if ((rc = MQTTClient_publish( - mqtt_client, query_status_topic_buffer, 0, NULL, IOT_SAMPLE_MQTT_PUBLISH_QOS, 0, NULL)) - != MQTTCLIENT_SUCCESS) + rc = MQTTClient_publish( + mqtt_client, query_topic_buffer, 0, NULL, IOT_SAMPLE_MQTT_PUBLISH_QOS, 0, NULL); + if (rc != MQTTCLIENT_SUCCESS) { IOT_SAMPLE_LOG_ERROR("Failed to publish query status request: MQTTClient return code %d.", rc); exit(rc); @@ -427,16 +417,16 @@ static void generate_sas_key(void) // Get the signature which will be signed with the decoded key. az_span sas_signature = AZ_SPAN_FROM_BUFFER(sas_signature_buffer); - if (az_result_failed( - rc = az_iot_provisioning_client_sas_get_signature( - &provisioning_client, sas_duration, sas_signature, &sas_signature))) + rc = az_iot_provisioning_client_sas_get_signature( + &provisioning_client, sas_duration, sas_signature, &sas_signature); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Could not get the signature for SAS key: az_result return code 0x%08x.", rc); exit(rc); } - // Generate the encoded, signed signature (b64 encoded, HMAC-SHA256 signing) + // Generate the encoded, signed signature (b64 encoded, HMAC-SHA256 signing). az_span sas_base64_encoded_signed_signature = AZ_SPAN_FROM_BUFFER(sas_base64_encoded_signed_signature_buffer); iot_sample_generate_sas_base64_encoded_signed_signature( @@ -445,7 +435,7 @@ static void generate_sas_key(void) sas_base64_encoded_signed_signature, &sas_base64_encoded_signed_signature); - // Get the resulting MQTT password, passing the base64 encoded, HMAC signed bytes + // Get the resulting MQTT password, passing the base64 encoded, HMAC signed bytes. size_t mqtt_password_length; rc = az_iot_provisioning_client_sas_get_password( &provisioning_client, diff --git a/sdk/samples/iot/pnp/pnp_device_info_component.c b/sdk/samples/iot/pnp/pnp_device_info_component.c index 804554fc8d..a6628ef5d5 100644 --- a/sdk/samples/iot/pnp/pnp_device_info_component.c +++ b/sdk/samples/iot/pnp/pnp_device_info_component.c @@ -13,7 +13,7 @@ #define DOUBLE_DECIMAL_PLACE_DIGITS 2 -/* Reported property keys and values */ +// Reported property keys and values static az_span const software_version_property_name = AZ_SPAN_LITERAL_FROM_STR("swVersion"); static az_span const software_version_property_value = AZ_SPAN_LITERAL_FROM_STR("1.0.0.0"); static az_span const manufacturer_property_name = AZ_SPAN_LITERAL_FROM_STR("manufacturer"); diff --git a/sdk/samples/iot/pnp/pnp_mqtt_message.c b/sdk/samples/iot/pnp/pnp_mqtt_message.c index b080c1d6b5..cfc1fab5d5 100644 --- a/sdk/samples/iot/pnp/pnp_mqtt_message.c +++ b/sdk/samples/iot/pnp/pnp_mqtt_message.c @@ -35,12 +35,12 @@ az_result pnp_mqtt_message_init(pnp_mqtt_message* out_mqtt_message) az_span pnp_mqtt_get_request_id(void) { - az_result rc; az_span remainder; az_span out_span = az_span_create((uint8_t*)request_id_buffer, sizeof(request_id_buffer)); // Note that if left to run for a long time, this will overflow and reset back to 0. - if (az_result_failed(rc = az_span_u32toa(out_span, request_id_int++, &remainder))) + az_result rc = az_span_u32toa(out_span, request_id_int++, &remainder); + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR("Failed to get request id: az_result return code 0x%08x.", rc); exit(rc); diff --git a/sdk/samples/iot/pnp/pnp_protocol.c b/sdk/samples/iot/pnp/pnp_protocol.c index d10e20a898..d9bfd2e440 100644 --- a/sdk/samples/iot/pnp/pnp_protocol.c +++ b/sdk/samples/iot/pnp/pnp_protocol.c @@ -80,7 +80,7 @@ static az_result visit_component_properties( return AZ_OK; } -// Move reader to the value of property name +// Move reader to the value of property name. static az_result json_child_token_move(az_json_reader* jr, az_span property_name) { while (az_result_succeeded(az_json_reader_next_token(jr))) @@ -357,9 +357,9 @@ az_result pnp_process_device_twin_message( return AZ_ERROR_UNEXPECTED_CHAR; } - if (jr.token.kind == AZ_JSON_TOKEN_BEGIN_OBJECT && components_ptr != NULL - && (az_result_succeeded( - is_component_in_model(property_name.slice, components_ptr, components_num, &index)))) + if ((jr.token.kind == AZ_JSON_TOKEN_BEGIN_OBJECT) && (components_ptr != NULL) + && az_result_succeeded( + is_component_in_model(property_name.slice, components_ptr, components_num, &index))) { if (az_result_failed(visit_component_properties( *components_ptr[index], &jr, version, property_callback, context_ptr))) diff --git a/sdk/samples/iot/pnp/pnp_thermostat_component.c b/sdk/samples/iot/pnp/pnp_thermostat_component.c index 91d1c3f66e..315ad1d757 100644 --- a/sdk/samples/iot/pnp/pnp_thermostat_component.c +++ b/sdk/samples/iot/pnp/pnp_thermostat_component.c @@ -2,7 +2,7 @@ // SPDX-License-Identifier: MIT #ifdef _MSC_VER -// warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s +// warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s // instead. #pragma warning(disable : 4996) #endif @@ -164,15 +164,14 @@ void pnp_thermostat_build_telemetry_message( az_span payload, az_span* out_payload) { - az_result rc; - - if (az_result_failed( - rc = pnp_build_telemetry_message( - payload, - telemetry_temperature_name, - append_double_callback, - (void*)&thermostat_component->current_temperature, - out_payload))) + az_result rc = pnp_build_telemetry_message( + payload, + telemetry_temperature_name, + append_double_callback, + (void*)&thermostat_component->current_temperature, + out_payload); + + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to build Telemetry message for %.*s: az_result return code 0x%08x.", @@ -189,17 +188,17 @@ void pnp_thermostat_build_maximum_temperature_reported_property( az_span* out_payload, az_span* out_property_name) { - az_result rc; *out_property_name = twin_reported_maximum_temperature_property_name; - if (az_result_failed( - rc = pnp_build_reported_property( - payload, - thermostat_component->component_name, - twin_reported_maximum_temperature_property_name, - append_double_callback, - &thermostat_component->maximum_temperature, - out_payload))) + az_result rc = pnp_build_reported_property( + payload, + thermostat_component->component_name, + twin_reported_maximum_temperature_property_name, + append_double_callback, + &thermostat_component->maximum_temperature, + out_payload); + + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to build `%.*s` reported property payload: az_result return code 0x%08x.", @@ -219,19 +218,18 @@ void pnp_thermostat_build_error_reported_property_with_status( az_span payload, az_span* out_payload) { - az_result rc; - - if (az_result_failed( - rc = pnp_build_reported_property_with_status( - payload, - component_name, - property_name, - append_double_callback, - (void*)property_value, - (int32_t)status, - version, - twin_response_failed, - out_payload))) + az_result rc = pnp_build_reported_property_with_status( + payload, + component_name, + property_name, + append_double_callback, + (void*)property_value, + (int32_t)status, + version, + twin_response_failed, + out_payload); + + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to build Temperature Sensor error payload: az_result return code 0x%08x.", rc); @@ -247,7 +245,6 @@ az_result pnp_thermostat_process_property_update( az_span payload, az_span* out_payload) { - az_result rc; double parsed_property_value = 0; if (!az_json_token_is_text_equal(property_name, twin_desired_temperature_property_name)) @@ -288,17 +285,18 @@ az_result pnp_thermostat_process_property_update( IOT_SAMPLE_LOG("Average Temperature: %2f", ref_thermostat_component->average_temperature); // Build reported property message with status. - if (az_result_failed( - rc = pnp_build_reported_property_with_status( - payload, - ref_thermostat_component->component_name, - property_name->slice, - append_double_callback, - (void*)&parsed_property_value, - (int32_t)AZ_IOT_STATUS_OK, - version, - twin_response_success, - out_payload))) + az_result rc = pnp_build_reported_property_with_status( + payload, + ref_thermostat_component->component_name, + property_name->slice, + append_double_callback, + (void*)&parsed_property_value, + (int32_t)AZ_IOT_STATUS_OK, + version, + twin_response_success, + out_payload); + + if (az_result_failed(rc)) { IOT_SAMPLE_LOG_ERROR( "Failed to get reported property payload with status: az_result return code 0x%08x.", rc); @@ -324,6 +322,7 @@ az_result pnp_thermostat_process_command_request( // Invoke command. rc = invoke_getMaxMinReport( thermostat_component, command_received_payload, payload, out_payload); + if (az_result_failed(rc)) { *out_payload = command_empty_response_payload; @@ -343,7 +342,7 @@ az_result pnp_thermostat_process_command_request( *out_status = AZ_IOT_STATUS_NOT_FOUND; IOT_SAMPLE_LOG_AZ_SPAN("Command not supported on Thermostat Sensor component:", command_name); - rc = AZ_ERROR_ITEM_NOT_FOUND; // Unsupported command or not this component's command + rc = AZ_ERROR_ITEM_NOT_FOUND; // Unsupported command } return rc;