iothub_ll_telemetry_sample

Committer:
AzureIoTClient
Date:
Tue Mar 20 10:31:58 2018 -0700
Revision:
1:589bbd7948f3
Parent:
0:c979e2f5511a
Child:
2:2b31d7ad244c
1.2.1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 0:c979e2f5511a 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 0:c979e2f5511a 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 0:c979e2f5511a 3 #include <stdio.h>
AzureIoTClient 0:c979e2f5511a 4 #include <stdlib.h>
AzureIoTClient 0:c979e2f5511a 5
AzureIoTClient 0:c979e2f5511a 6 #include "iothub_client.h"
AzureIoTClient 0:c979e2f5511a 7 #include "iothub_message.h"
AzureIoTClient 0:c979e2f5511a 8 #include "azure_c_shared_utility/threadapi.h"
AzureIoTClient 0:c979e2f5511a 9 #include "azure_c_shared_utility/crt_abstractions.h"
AzureIoTClient 0:c979e2f5511a 10 #include "azure_c_shared_utility/platform.h"
AzureIoTClient 0:c979e2f5511a 11 #include "azure_c_shared_utility/shared_util_options.h"
AzureIoTClient 0:c979e2f5511a 12
AzureIoTClient 1:589bbd7948f3 13 // USE_MQTT, USE_AMQP, and/or USE_HTTP are set in .\CMakeLists.txt based/
AzureIoTClient 1:589bbd7948f3 14 // on which protocols the IoT C SDK has been set to include during cmake
AzureIoTClient 1:589bbd7948f3 15 // generation time.
AzureIoTClient 1:589bbd7948f3 16
AzureIoTClient 0:c979e2f5511a 17 #ifdef USE_MQTT
AzureIoTClient 0:c979e2f5511a 18 #include "iothubtransportmqtt.h"
AzureIoTClient 0:c979e2f5511a 19 #ifdef USE_WEBSOCKETS
AzureIoTClient 0:c979e2f5511a 20 #include "iothubtransportmqtt_websockets.h"
AzureIoTClient 0:c979e2f5511a 21 #endif
AzureIoTClient 0:c979e2f5511a 22 #endif
AzureIoTClient 0:c979e2f5511a 23 #ifdef USE_AMQP
AzureIoTClient 0:c979e2f5511a 24 #include "iothubtransportamqp.h"
AzureIoTClient 0:c979e2f5511a 25 #ifdef USE_WEBSOCKETS
AzureIoTClient 0:c979e2f5511a 26 #include "iothubtransportamqp_websockets.h"
AzureIoTClient 0:c979e2f5511a 27 #endif
AzureIoTClient 0:c979e2f5511a 28 #endif
AzureIoTClient 0:c979e2f5511a 29 #ifdef USE_HTTP
AzureIoTClient 0:c979e2f5511a 30 #include "iothubtransporthttp.h"
AzureIoTClient 0:c979e2f5511a 31 #endif
AzureIoTClient 0:c979e2f5511a 32
AzureIoTClient 0:c979e2f5511a 33 #include "iothub_client_options.h"
AzureIoTClient 0:c979e2f5511a 34 #include "certs.h"
AzureIoTClient 0:c979e2f5511a 35
AzureIoTClient 0:c979e2f5511a 36 /* String containing Hostname, Device Id & Device Key in the format: */
AzureIoTClient 0:c979e2f5511a 37 /* Paste in the your iothub connection string */
AzureIoTClient 0:c979e2f5511a 38 static const char* connectionString = "[device connection string]";
AzureIoTClient 0:c979e2f5511a 39
AzureIoTClient 0:c979e2f5511a 40 #define MESSAGE_COUNT 5
AzureIoTClient 0:c979e2f5511a 41 static bool g_continueRunning = true;
AzureIoTClient 0:c979e2f5511a 42 static size_t g_message_count_send_confirmations = 0;
AzureIoTClient 0:c979e2f5511a 43
AzureIoTClient 0:c979e2f5511a 44 static void send_confirm_callback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
AzureIoTClient 0:c979e2f5511a 45 {
AzureIoTClient 0:c979e2f5511a 46 (void)userContextCallback;
AzureIoTClient 0:c979e2f5511a 47 // When a message is sent this callback will get envoked
AzureIoTClient 0:c979e2f5511a 48 g_message_count_send_confirmations++;
AzureIoTClient 0:c979e2f5511a 49 (void)printf("Confirmation callback received for message %zu with result %s\r\n", g_message_count_send_confirmations, ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
AzureIoTClient 0:c979e2f5511a 50 }
AzureIoTClient 0:c979e2f5511a 51
AzureIoTClient 0:c979e2f5511a 52 int main(void)
AzureIoTClient 0:c979e2f5511a 53 {
AzureIoTClient 0:c979e2f5511a 54 IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol;
AzureIoTClient 0:c979e2f5511a 55 IOTHUB_MESSAGE_HANDLE message_handle;
AzureIoTClient 0:c979e2f5511a 56 size_t messages_sent = 0;
AzureIoTClient 0:c979e2f5511a 57 const char* telemetry_msg = "test_message";
AzureIoTClient 0:c979e2f5511a 58
AzureIoTClient 1:589bbd7948f3 59 // Select the Protocol to use with the connection.
AzureIoTClient 1:589bbd7948f3 60 #ifdef USE_HTTP
AzureIoTClient 1:589bbd7948f3 61 protocol = HTTP_Protocol;
AzureIoTClient 1:589bbd7948f3 62 #endif
AzureIoTClient 0:c979e2f5511a 63 #ifdef USE_AMQP
AzureIoTClient 0:c979e2f5511a 64 //protocol = AMQP_Protocol_over_WebSocketsTls;
AzureIoTClient 0:c979e2f5511a 65 protocol = AMQP_Protocol;
AzureIoTClient 0:c979e2f5511a 66 #endif
AzureIoTClient 0:c979e2f5511a 67 #ifdef USE_MQTT
AzureIoTClient 1:589bbd7948f3 68 protocol = MQTT_Protocol;
AzureIoTClient 0:c979e2f5511a 69 //protocol = MQTT_WebSocket_Protocol;
AzureIoTClient 0:c979e2f5511a 70 #endif
AzureIoTClient 0:c979e2f5511a 71
AzureIoTClient 0:c979e2f5511a 72 IOTHUB_CLIENT_LL_HANDLE iothub_ll_handle;
AzureIoTClient 0:c979e2f5511a 73
AzureIoTClient 0:c979e2f5511a 74 // Used to initialize IoTHub SDK subsystem
AzureIoTClient 0:c979e2f5511a 75 (void)platform_init();
AzureIoTClient 0:c979e2f5511a 76
AzureIoTClient 0:c979e2f5511a 77 (void)printf("Creating IoTHub handle\r\n");
AzureIoTClient 0:c979e2f5511a 78 // Create the iothub handle here
AzureIoTClient 0:c979e2f5511a 79 iothub_ll_handle = IoTHubClient_LL_CreateFromConnectionString(connectionString, protocol);
AzureIoTClient 0:c979e2f5511a 80
AzureIoTClient 0:c979e2f5511a 81 // Set any option that are neccessary.
AzureIoTClient 0:c979e2f5511a 82 // For available options please see the iothub_sdk_options.md documentation
AzureIoTClient 0:c979e2f5511a 83 //bool traceOn = true;
AzureIoTClient 0:c979e2f5511a 84 //IoTHubClient_LL_SetOption(iothub_ll_handle, OPTION_LOG_TRACE, &traceOn);
AzureIoTClient 0:c979e2f5511a 85 // Setting the Trusted Certificate. This is only necessary on system with without
AzureIoTClient 0:c979e2f5511a 86 // built in certificate stores.
AzureIoTClient 0:c979e2f5511a 87 IoTHubClient_LL_SetOption(iothub_ll_handle, OPTION_TRUSTED_CERT, certificates);
AzureIoTClient 0:c979e2f5511a 88
AzureIoTClient 0:c979e2f5511a 89 do
AzureIoTClient 0:c979e2f5511a 90 {
AzureIoTClient 0:c979e2f5511a 91 if (messages_sent < MESSAGE_COUNT)
AzureIoTClient 0:c979e2f5511a 92 {
AzureIoTClient 0:c979e2f5511a 93 // Construct the iothub message from a string or a byte array
AzureIoTClient 0:c979e2f5511a 94 message_handle = IoTHubMessage_CreateFromString(telemetry_msg);
AzureIoTClient 0:c979e2f5511a 95 //message_handle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText)));
AzureIoTClient 0:c979e2f5511a 96
AzureIoTClient 0:c979e2f5511a 97 // Set Message property
AzureIoTClient 0:c979e2f5511a 98 (void)IoTHubMessage_SetMessageId(message_handle, "MSG_ID");
AzureIoTClient 0:c979e2f5511a 99 (void)IoTHubMessage_SetCorrelationId(message_handle, "CORE_ID");
AzureIoTClient 0:c979e2f5511a 100 (void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2Fjson");
AzureIoTClient 0:c979e2f5511a 101 (void)IoTHubMessage_SetContentEncodingSystemProperty(message_handle, "utf-8");
AzureIoTClient 0:c979e2f5511a 102
AzureIoTClient 0:c979e2f5511a 103 // Add custom properties to message
AzureIoTClient 0:c979e2f5511a 104 MAP_HANDLE propMap = IoTHubMessage_Properties(message_handle);
AzureIoTClient 0:c979e2f5511a 105 Map_AddOrUpdate(propMap, "property_key", "property_value");
AzureIoTClient 0:c979e2f5511a 106
AzureIoTClient 0:c979e2f5511a 107 (void)printf("Sending message %d to IoTHub\r\n", (int)(messages_sent+1) );
AzureIoTClient 0:c979e2f5511a 108 IoTHubClient_LL_SendEventAsync(iothub_ll_handle, message_handle, send_confirm_callback, NULL);
AzureIoTClient 0:c979e2f5511a 109
AzureIoTClient 0:c979e2f5511a 110 // The message is copied to the sdk so the we can destroy it
AzureIoTClient 0:c979e2f5511a 111 IoTHubMessage_Destroy(message_handle);
AzureIoTClient 0:c979e2f5511a 112
AzureIoTClient 0:c979e2f5511a 113 messages_sent++;
AzureIoTClient 0:c979e2f5511a 114 }
AzureIoTClient 0:c979e2f5511a 115 else if (g_message_count_send_confirmations >= MESSAGE_COUNT)
AzureIoTClient 0:c979e2f5511a 116 {
AzureIoTClient 0:c979e2f5511a 117 // After all messages are all received stop running
AzureIoTClient 0:c979e2f5511a 118 g_continueRunning = false;
AzureIoTClient 0:c979e2f5511a 119 }
AzureIoTClient 0:c979e2f5511a 120
AzureIoTClient 0:c979e2f5511a 121 IoTHubClient_LL_DoWork(iothub_ll_handle);
AzureIoTClient 0:c979e2f5511a 122 ThreadAPI_Sleep(1);
AzureIoTClient 0:c979e2f5511a 123
AzureIoTClient 0:c979e2f5511a 124 } while (g_continueRunning);
AzureIoTClient 0:c979e2f5511a 125
AzureIoTClient 0:c979e2f5511a 126 // Clean up the iothub sdk handle
AzureIoTClient 0:c979e2f5511a 127 IoTHubClient_LL_Destroy(iothub_ll_handle);
AzureIoTClient 0:c979e2f5511a 128
AzureIoTClient 0:c979e2f5511a 129 // Free all the sdk subsystem
AzureIoTClient 0:c979e2f5511a 130 platform_deinit();
AzureIoTClient 0:c979e2f5511a 131
AzureIoTClient 0:c979e2f5511a 132 printf("Press any key to continue");
AzureIoTClient 0:c979e2f5511a 133 getchar();
AzureIoTClient 0:c979e2f5511a 134
AzureIoTClient 0:c979e2f5511a 135 return 0;
AzureIoTClient 0:c979e2f5511a 136 }