Skip to content

Commit

Permalink
Sample Updates: SAS Telemetry and Twin; Logging (Azure#1148)
Browse files Browse the repository at this point in the history
- Changes name of Logging Macros to match C Guidelines. Updates in all sample files.
- Updates Sas Telemetry and Twin samples to match prior sample updates.
  • Loading branch information
momuno committed Aug 27, 2020
1 parent fb41576 commit f4531ea
Show file tree
Hide file tree
Showing 13 changed files with 601 additions and 531 deletions.
42 changes: 23 additions & 19 deletions sdk/samples/iot/iot_sample_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
#include <openssl/evp.h>
#include <openssl/hmac.h>

#define IOT_SAMPLE_PRECONDITION_NOT_NULL(arg) \
do { \
if (arg == NULL) \
{ \
LOG_ERROR("Pointer is NULL."); \
exit(1); \
} \
#define IOT_SAMPLE_PRECONDITION_NOT_NULL(arg) \
do { \
if (arg == NULL) \
{ \
IOT_SAMPLE_LOG_ERROR("Pointer is NULL."); \
exit(1); \
} \
} while (0)

//
Expand Down Expand Up @@ -87,7 +87,7 @@ static az_result read_configuration_entry(
}
else
{
LOG_ERROR("(missing) Please set the %s environment variable.", env_name);
IOT_SAMPLE_LOG_ERROR("(missing) Please set the %s environment variable.", env_name);
return AZ_ERROR_ARG;
}

Expand Down Expand Up @@ -162,7 +162,7 @@ az_result iot_sample_read_environment_variables(
break;

default:
LOG_ERROR("Hub sample name undefined.");
IOT_SAMPLE_LOG_ERROR("Hub sample name undefined.");
return AZ_ERROR_ARG;
}
}
Expand Down Expand Up @@ -226,13 +226,13 @@ az_result iot_sample_read_environment_variables(
break;

default:
LOG_ERROR("Provisioning sample name undefined.");
IOT_SAMPLE_LOG_ERROR("Provisioning sample name undefined.");
return AZ_ERROR_ARG;
}
}
else
{
LOG_ERROR("Sample type undefined.");
IOT_SAMPLE_LOG_ERROR("Sample type undefined.");
return AZ_ERROR_ARG;
}

Expand All @@ -245,7 +245,7 @@ az_result iot_sample_read_environment_variables(
out_env_vars->x509_trust_pem_file_path,
&(out_env_vars->x509_trust_pem_file_path)));

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

Expand Down Expand Up @@ -292,11 +292,11 @@ az_result iot_sample_create_mqtt_endpoint(
}
else
{
LOG_ERROR("Sample type undefined.");
IOT_SAMPLE_LOG_ERROR("Sample type undefined.");
return AZ_ERROR_ARG;
}

LOG_SUCCESS("MQTT endpoint created at \"%s\".", out_endpoint);
IOT_SAMPLE_LOG_SUCCESS("MQTT endpoint created at \"%s\".", out_endpoint);

return AZ_OK;
}
Expand Down Expand Up @@ -492,9 +492,10 @@ void iot_sample_generate_sas_base64_encoded_signed_signature(
// 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_failed(rc = decode_base64_bytes(sas_base64_encoded_key, sas_decoded_key, &sas_decoded_key)))
if (az_failed(
rc = decode_base64_bytes(sas_base64_encoded_key, sas_decoded_key, &sas_decoded_key)))
{
LOG_ERROR("Could not decode the SAS key: az_result return code 0x%04x.", rc);
IOT_SAMPLE_LOG_ERROR("Could not decode the SAS key: az_result return code 0x%04x.", rc);
exit(rc);
}

Expand All @@ -508,15 +509,18 @@ void iot_sample_generate_sas_base64_encoded_signed_signature(
sas_hmac256_signed_signature,
&sas_hmac256_signed_signature)))
{
LOG_ERROR("Could not sign the signature: az_result return code 0x%04x.", 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_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)))
{
LOG_ERROR("Could not base64 encode the password: az_result return code 0x%04x.", rc);
IOT_SAMPLE_LOG_ERROR("Could not base64 encode the password: az_result return code 0x%04x.", rc);
exit(rc);
}

Expand Down
74 changes: 41 additions & 33 deletions sdk/samples/iot/iot_sample_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,47 @@
#include <azure/core/az_span.h>

#define IOT_SAMPLE_SAS_KEY_DURATION_TIME_DIGITS 4

//
// Logging
//
#define LOG_ERROR(...) \
{ \
#define IOT_SAMPLE_LOG_ERROR(...) \
do \
{ \
(void)fprintf(stderr, "ERROR:\t\t%s:%s():%d: ", __FILE__, __func__, __LINE__); \
(void)fprintf(stderr, __VA_ARGS__); \
(void)fprintf(stderr, "\n"); \
fflush(stdout); \
fflush(stderr); \
}

#define LOG_SUCCESS(...) \
{ \
(void)printf("SUCCESS:\t"); \
(void)printf(__VA_ARGS__); \
(void)printf("\n"); \
}

#define LOG(...) \
{ \
(void)printf("\t\t"); \
(void)fprintf(stderr, __VA_ARGS__); \
(void)fprintf(stderr, "\n"); \
fflush(stdout); \
fflush(stderr); \
} while (0)

#define IOT_SAMPLE_LOG_SUCCESS(...) \
do \
{ \
(void)printf("SUCCESS:\t"); \
(void)printf(__VA_ARGS__); \
(void)printf("\n"); \
} while (0)

#define IOT_SAMPLE_LOG(...) \
do \
{ \
(void)printf("\t\t"); \
(void)printf(__VA_ARGS__); \
(void)printf("\n"); \
}

#define LOG_AZ_SPAN(span_description, span) \
{ \
(void)printf("\t\t%s ", span_description); \
char* buffer = (char*)az_span_ptr(span); \
(void)printf("\n"); \
} while (0)

#define IOT_SAMPLE_LOG_AZ_SPAN(span_description, span) \
do \
{ \
(void)printf("\t\t%s ", span_description); \
char* buffer = (char*)az_span_ptr(span); \
for (int32_t az_span_i = 0; az_span_i < az_span_size(span); az_span_i++) \
{ \
putchar(*buffer++); \
} \
(void)printf("\n"); \
}
{ \
putchar(*buffer++); \
} \
(void)printf("\n"); \
} while (0)

//
// Environment Variables
Expand Down Expand Up @@ -176,10 +181,13 @@ uint32_t iot_sample_get_epoch_expiration_time_from_minutes(uint32_t minutes);
/*
* @brief Generate the base64 encoded and signed signature using HMAC-SHA256 signing.
*
* @param[in] sas_base64_encoded_key An #az_span containing the SAS key that will be used for signing.
* @param[in] sas_base64_encoded_key An #az_span containing the SAS key that will be used for
* signing.
* @param[in] sas_signature An #az_span containing the signature.
* @param sas_base64_encoded_signed_signature An #az_span with sufficient capacity to hold the encoded signed signature.
* @param[out] out_sas_base64_encoded_signed_signature A pointer to the #az_span containing the encoded signed signature.
* @param[out] sas_base64_encoded_signed_signature An #az_span with sufficient capacity to hold the
* encoded signed signature.
* @param[out] out_sas_base64_encoded_signed_signature A pointer to the #az_span containing the
* encoded signed signature.
*/
void iot_sample_generate_sas_base64_encoded_signed_signature(
az_span sas_base64_encoded_key,
Expand Down
55 changes: 29 additions & 26 deletions sdk/samples/iot/paho_iot_hub_c2d_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,19 @@ static void parse_c2d_message(
int main(void)
{
create_and_configure_mqtt_client();
LOG_SUCCESS("Client created and configured.");
IOT_SAMPLE_LOG_SUCCESS("Client created and configured.");

connect_mqtt_client_to_iot_hub();
LOG_SUCCESS("Client connected to IoT Hub.");
IOT_SAMPLE_LOG_SUCCESS("Client connected to IoT Hub.");

subscribe_mqtt_client_to_iot_hub_topics();
LOG_SUCCESS("Client subscribed to IoT Hub topics and is ready to receive C2D messages.");
IOT_SAMPLE_LOG_SUCCESS(
"Client subscribed to IoT Hub topics and is ready to receive C2D messages.");

receive_c2d_messages();

disconnect_mqtt_client_from_iot_hub();
LOG_SUCCESS("Client disconnected from IoT Hub.");
IOT_SAMPLE_LOG_SUCCESS("Client disconnected from IoT Hub.");

return 0;
}
Expand All @@ -83,7 +84,7 @@ static void create_and_configure_mqtt_client(void)
// Reads in environment variables set by user for purposes of running sample.
if (az_failed(rc = iot_sample_read_environment_variables(SAMPLE_TYPE, SAMPLE_NAME, &env_vars)))
{
LOG_ERROR(
IOT_SAMPLE_LOG_ERROR(
"Failed to read configuration from environment variables: az_result return code 0x%08x.",
rc);
exit(rc);
Expand All @@ -95,7 +96,7 @@ static void create_and_configure_mqtt_client(void)
rc = iot_sample_create_mqtt_endpoint(
SAMPLE_TYPE, &env_vars, mqtt_endpoint_buffer, sizeof(mqtt_endpoint_buffer))))
{
LOG_ERROR("Failed to create MQTT endpoint: az_result return code 0x%08x.", rc);
IOT_SAMPLE_LOG_ERROR("Failed to create MQTT endpoint: az_result return code 0x%08x.", rc);
exit(rc);
}

Expand All @@ -104,7 +105,7 @@ static void create_and_configure_mqtt_client(void)
rc = az_iot_hub_client_init(
&hub_client, env_vars.hub_hostname, env_vars.hub_device_id, NULL)))
{
LOG_ERROR("Failed to initialize hub client: az_result return code 0x%08x.", rc);
IOT_SAMPLE_LOG_ERROR("Failed to initialize hub client: az_result return code 0x%08x.", rc);
exit(rc);
}

Expand All @@ -114,7 +115,7 @@ static void create_and_configure_mqtt_client(void)
rc = az_iot_hub_client_get_client_id(
&hub_client, mqtt_client_id_buffer, sizeof(mqtt_client_id_buffer), NULL)))
{
LOG_ERROR("Failed to get MQTT client id: az_result return code 0x%08x.", rc);
IOT_SAMPLE_LOG_ERROR("Failed to get MQTT client id: az_result return code 0x%08x.", rc);
exit(rc);
}

Expand All @@ -127,7 +128,7 @@ static void create_and_configure_mqtt_client(void)
NULL))
!= MQTTCLIENT_SUCCESS)
{
LOG_ERROR("Failed to create MQTT client: MQTTClient return code %d.", rc);
IOT_SAMPLE_LOG_ERROR("Failed to create MQTT client: MQTTClient return code %d.", rc);
exit(rc);
}
}
Expand All @@ -141,7 +142,7 @@ static void connect_mqtt_client_to_iot_hub(void)
rc = az_iot_hub_client_get_user_name(
&hub_client, mqtt_client_username_buffer, sizeof(mqtt_client_username_buffer), NULL)))
{
LOG_ERROR("Failed to get MQTT client username: az_result return code 0x%08x.", rc);
IOT_SAMPLE_LOG_ERROR("Failed to get MQTT client username: az_result return code 0x%08x.", rc);
exit(rc);
}

Expand All @@ -163,7 +164,7 @@ static void connect_mqtt_client_to_iot_hub(void)
// Connect MQTT client to the Azure IoT Hub.
if ((rc = MQTTClient_connect(mqtt_client, &mqtt_connect_options)) != MQTTCLIENT_SUCCESS)
{
LOG_ERROR(
IOT_SAMPLE_LOG_ERROR(
"Failed to connect: MQTTClient return code %d.\n"
"If on Windows, confirm the AZ_IOT_DEVICE_X509_TRUST_PEM_FILE_PATH environment variable is "
"set correctly.",
Expand All @@ -180,7 +181,7 @@ static void subscribe_mqtt_client_to_iot_hub_topics(void)
if ((rc = MQTTClient_subscribe(mqtt_client, AZ_IOT_HUB_CLIENT_C2D_SUBSCRIBE_TOPIC, 1))
!= MQTTCLIENT_SUCCESS)
{
LOG_ERROR("Failed to subscribe to the C2D topic: MQTTClient return code %d.", rc);
IOT_SAMPLE_LOG_ERROR("Failed to subscribe to the C2D topic: MQTTClient return code %d.", rc);
exit(rc);
}
}
Expand All @@ -195,39 +196,41 @@ static void receive_c2d_messages(void)
// Continue until max # messages received or timeout expires.
for (uint8_t message_count = 0; message_count < MAX_C2D_MESSAGE_COUNT; message_count++)
{
LOG(" "); // Formatting
LOG("Waiting for C2D message.\n");
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))
{
LOG_ERROR("Failed to receive message #%d: MQTTClient return code %d.", message_count + 1, rc);
IOT_SAMPLE_LOG_ERROR(
"Failed to receive message #%d: MQTTClient return code %d.", message_count + 1, rc);
exit(rc);
}
else if (message == NULL)
{
LOG_ERROR("Timeout expired: MQTTClient return code %d.", rc);
IOT_SAMPLE_LOG("Receive message timeout expired.");
return;
}
else if (rc == MQTTCLIENT_TOPICNAME_TRUNCATED)
{
topic_len = (int)strlen(topic);
}
LOG_SUCCESS("Message #%d: Client received a message from the service.", message_count + 1);
IOT_SAMPLE_LOG_SUCCESS(
"Message #%d: Client received a C2D message from the service.", message_count + 1);

// Parse c2d message.
az_iot_hub_client_c2d_request c2d_request;
parse_c2d_message(topic, topic_len, message, &c2d_request);
LOG_SUCCESS("Client parsed message.");
IOT_SAMPLE_LOG_SUCCESS("Client parsed C2D message.");

MQTTClient_freeMessage(&message);
MQTTClient_free(topic);
}

LOG(" "); // Formatting
LOG_SUCCESS("Client received messages.")
IOT_SAMPLE_LOG(" "); // Formatting
IOT_SAMPLE_LOG_SUCCESS("Client received messages.");
}

static void disconnect_mqtt_client_from_iot_hub(void)
Expand All @@ -236,7 +239,7 @@ static void disconnect_mqtt_client_from_iot_hub(void)

if ((rc = MQTTClient_disconnect(mqtt_client, MQTT_TIMEOUT_DISCONNECT_MS)) != MQTTCLIENT_SUCCESS)
{
LOG_ERROR("Failed to disconnect MQTT client: MQTTClient return code %d.", rc);
IOT_SAMPLE_LOG_ERROR("Failed to disconnect MQTT client: MQTTClient return code %d.", rc);
exit(rc);
}

Expand All @@ -258,11 +261,11 @@ static void parse_c2d_message(
rc
= az_iot_hub_client_c2d_parse_received_topic(&hub_client, topic_span, out_c2d_request)))
{
LOG_ERROR("Message from unknown topic: az_result return code 0x%08x.", rc);
LOG_AZ_SPAN("Topic:", topic_span);
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);
}
LOG_SUCCESS("Client received a valid topic response.");
LOG_AZ_SPAN("Topic:", topic_span);
LOG_AZ_SPAN("Payload:", message_span);
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);
}
Loading

0 comments on commit f4531ea

Please sign in to comment.