Skip to content

Commit

Permalink
PnP Component Sample convert sample style (Azure#1092)
Browse files Browse the repository at this point in the history
Updates to includes, defines, logging, function names, etc.
Move MQTT message initialization into init function.
  • Loading branch information
momuno authored Aug 25, 2020
1 parent d375d08 commit 332102c
Show file tree
Hide file tree
Showing 20 changed files with 1,176 additions and 1,275 deletions.
12 changes: 2 additions & 10 deletions sdk/samples/iot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ target_link_libraries(paho_iot_hub_pnp_sample

# PnP Component Sample
add_executable (paho_iot_hub_pnp_component_sample
${CMAKE_CURRENT_LIST_DIR}/sample_pnp_component_mqtt.c
${CMAKE_CURRENT_LIST_DIR}/sample_pnp_mqtt_component.c
${CMAKE_CURRENT_LIST_DIR}/sample_pnp_device_info_component.c
${CMAKE_CURRENT_LIST_DIR}/sample_pnp_thermostat_component.c
${CMAKE_CURRENT_LIST_DIR}/paho_iot_hub_pnp_component_sample.c
Expand All @@ -96,15 +96,7 @@ add_executable (paho_iot_hub_pnp_component_sample
# SDK deps
target_link_libraries(paho_iot_hub_pnp_component_sample
PRIVATE
az::iot::hub
)

# External deps
target_link_libraries(paho_iot_hub_pnp_component_sample
PRIVATE
eclipse-paho-mqtt-c::paho-mqtt3cs-static
OpenSSL::SSL
OpenSSL::Crypto
az::iot::samples::common
)

# Telemetry (Certificates) Sample
Expand Down
3 changes: 2 additions & 1 deletion sdk/samples/iot/iot_samples_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ az_result read_environment_variables(
{
case PAHO_IOT_HUB_C2D_SAMPLE:
case PAHO_IOT_HUB_METHODS_SAMPLE:
case PAHO_IOT_HUB_PNP_COMPONENT_SAMPLE:
case PAHO_IOT_HUB_PNP_SAMPLE:
case PAHO_IOT_HUB_TELEMETRY_SAMPLE:
case PAHO_IOT_HUB_TWIN_SAMPLE:
Expand Down Expand Up @@ -230,7 +231,7 @@ az_result read_environment_variables(
env_vars->x509_trust_pem_file_path,
&(env_vars->x509_trust_pem_file_path)));

LOG(" "); // Log formatting
LOG(" "); // Formatting.
return AZ_OK;
}

Expand Down
1 change: 1 addition & 0 deletions sdk/samples/iot/iot_samples_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ typedef enum
{
PAHO_IOT_HUB_C2D_SAMPLE,
PAHO_IOT_HUB_METHODS_SAMPLE,
PAHO_IOT_HUB_PNP_COMPONENT_SAMPLE,
PAHO_IOT_HUB_PNP_SAMPLE,
PAHO_IOT_HUB_SAS_TELEMETRY_SAMPLE,
PAHO_IOT_HUB_TELEMETRY_SAMPLE,
Expand Down
21 changes: 11 additions & 10 deletions sdk/samples/iot/paho_iot_hub_c2d_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// SPDX-License-Identifier: MIT

#ifdef _MSC_VER
#pragma warning(push)
// warning C4201: nonstandard extension used: nameless struct/union
#pragma warning(disable : 4201)
#endif
#include <paho-mqtt/MQTTClient.h>
#ifdef _MSC_VER
#pragma warning(default : 4201)
#pragma warning(pop)
#endif

#include "iot_samples_common.h"
Expand Down Expand Up @@ -84,7 +85,7 @@ static void create_and_configure_mqtt_client(void)
if (az_failed(rc = read_environment_variables(SAMPLE_TYPE, SAMPLE_NAME, &env_vars)))
{
LOG_ERROR(
"Failed to read configuration from environment variables: az_result return code 0x%04x.",
"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 = 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%04x.", rc);
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%04x.", rc);
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%04x.", rc);
LOG_ERROR("Failed to get MQTT client id: az_result return code 0x%08x.", 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%04x.", rc);
LOG_ERROR("Failed to get MQTT client username: az_result return code 0x%08x.", rc);
exit(rc);
}

Expand Down Expand Up @@ -214,14 +215,14 @@ static void receive_c2d_messages(void)
{
topic_len = (int)strlen(topic);
}
LOG_SUCCESS("Message #%d: Client received message from the service.", message_count + 1);
LOG_SUCCESS("Message #%d: Client received a 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.");

LOG(" "); // formatting
LOG(" "); // formatting.

MQTTClient_freeMessage(&message);
MQTTClient_free(topic);
Expand All @@ -247,15 +248,15 @@ static void parse_c2d_message(
const MQTTClient_message* message,
az_iot_hub_client_c2d_request* c2d_request)
{
int rc;
az_result rc;
az_span topic_span = az_span_create((uint8_t*)topic, topic_len);
az_span message_span = az_span_create((uint8_t*)message->payload, message->payloadlen);

// Parse message and retrieve c2d_request info.
if (az_failed(
rc = az_iot_hub_client_c2d_parse_received_topic(&hub_client, topic_span, c2d_request)))
{
LOG_ERROR("Message from unknown topic: az_result return code 0x%04x.", rc);
LOG_ERROR("Message from unknown topic: az_result return code 0x%08x.", rc);
LOG_AZ_SPAN("Topic:", topic_span);
exit(rc);
}
Expand Down
23 changes: 12 additions & 11 deletions sdk/samples/iot/paho_iot_hub_methods_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// SPDX-License-Identifier: MIT

#ifdef _MSC_VER
#pragma warning(push)
// warning C4201: nonstandard extension used: nameless struct/union
#pragma warning(disable : 4201)
#endif
#include <paho-mqtt/MQTTClient.h>
#ifdef _MSC_VER
#pragma warning(default : 4201)
#pragma warning(pop)
#endif

#include "iot_samples_common.h"
Expand Down Expand Up @@ -100,7 +101,7 @@ static void create_and_configure_mqtt_client(void)
if (az_failed(rc = read_environment_variables(SAMPLE_TYPE, SAMPLE_NAME, &env_vars)))
{
LOG_ERROR(
"Failed to read configuration from environment variables: az_result return code 0x%04x.",
"Failed to read configuration from environment variables: az_result return code 0x%08x.",
rc);
exit(rc);
}
Expand All @@ -111,7 +112,7 @@ static void create_and_configure_mqtt_client(void)
rc = 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%04x.", rc);
LOG_ERROR("Failed to create MQTT endpoint: az_result return code 0x%08x.", rc);
exit(rc);
}

Expand All @@ -120,7 +121,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%04x.", rc);
LOG_ERROR("Failed to initialize hub client: az_result return code 0x%08x.", rc);
exit(rc);
}

Expand All @@ -130,7 +131,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%04x.", rc);
LOG_ERROR("Failed to get MQTT client id: az_result return code 0x%08x.", rc);
exit(rc);
}

Expand All @@ -157,7 +158,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%04x.", rc);
LOG_ERROR("Failed to get MQTT client username: az_result return code 0x%08x.", rc);
exit(rc);
}

Expand Down Expand Up @@ -230,15 +231,15 @@ static void receive_method_messages(void)
{
topic_len = (int)strlen(topic);
}
LOG_SUCCESS("Message #%d: Client received message from the service.", message_count + 1);
LOG_SUCCESS("Message #%d: Client received a message from the service.", message_count + 1);

// Parse method message and invoke method.
az_iot_hub_client_method_request method_request;
parse_method_message(topic, topic_len, message, &method_request);
LOG_SUCCESS("Client parsed message.");

invoke_method(&method_request);
LOG(" "); // formatting
LOG(" "); // formatting.

MQTTClient_freeMessage(&message);
MQTTClient_free(topic);
Expand All @@ -264,7 +265,7 @@ static void parse_method_message(
const MQTTClient_message* message,
az_iot_hub_client_method_request* method_request)
{
int rc;
az_result rc;
az_span topic_span = az_span_create((uint8_t*)topic, topic_len);
az_span message_span = az_span_create((uint8_t*)message->payload, message->payloadlen);

Expand All @@ -273,7 +274,7 @@ static void parse_method_message(
rc = az_iot_hub_client_methods_parse_received_topic(
&hub_client, topic_span, method_request)))
{
LOG_ERROR("Message from unknown topic: az_result return code 0x%04x.", rc);
LOG_ERROR("Message from unknown topic: az_result return code 0x%08x.", rc);
LOG_AZ_SPAN("Topic:", topic_span);
exit(rc);
}
Expand Down Expand Up @@ -322,7 +323,7 @@ static void send_method_response(
sizeof(methods_response_topic_buffer),
NULL)))
{
LOG_ERROR("Failed to get Methods response publish topic: az_result return code 0x%04x.", rc);
LOG_ERROR("Failed to get Methods response publish topic: az_result return code 0x%08x.", rc);
exit(rc);
}

Expand Down
Loading

0 comments on commit 332102c

Please sign in to comment.