iothub_ll_telemetry_sample

Committer:
AzureIoTClient
Date:
Mon Apr 16 14:28:30 2018 -0700
Revision:
2:2b31d7ad244c
Parent:
1:589bbd7948f3
Child:
3:c88858e5d52c
1.2.3

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 2:2b31d7ad244c 7 #include "iothub_client_options.h"
AzureIoTClient 0:c979e2f5511a 8 #include "iothub_message.h"
AzureIoTClient 0:c979e2f5511a 9 #include "azure_c_shared_utility/threadapi.h"
AzureIoTClient 0:c979e2f5511a 10 #include "azure_c_shared_utility/crt_abstractions.h"
AzureIoTClient 0:c979e2f5511a 11 #include "azure_c_shared_utility/platform.h"
AzureIoTClient 0:c979e2f5511a 12 #include "azure_c_shared_utility/shared_util_options.h"
AzureIoTClient 0:c979e2f5511a 13
AzureIoTClient 2:2b31d7ad244c 14 /* This sample uses the _LL APIs of iothub_client for example purposes.
AzureIoTClient 2:2b31d7ad244c 15 Simply changing the using the convenience layer (functions not having _LL)
AzureIoTClient 2:2b31d7ad244c 16 and removing calls to _DoWork will yield the same results. */
AzureIoTClient 1:589bbd7948f3 17
AzureIoTClient 2:2b31d7ad244c 18 // The protocol you wish to use should be uncommented
AzureIoTClient 2:2b31d7ad244c 19 //
AzureIoTClient 2:2b31d7ad244c 20 #define SAMPLE_MQTT
AzureIoTClient 2:2b31d7ad244c 21 //#define SAMPLE_MQTT_OVER_WEBSOCKETS
AzureIoTClient 2:2b31d7ad244c 22 //#define SAMPLE_AMQP
AzureIoTClient 2:2b31d7ad244c 23 //#define SAMPLE_AMQP_OVER_WEBSOCKETS
AzureIoTClient 2:2b31d7ad244c 24 //#define SAMPLE_HTTP
AzureIoTClient 2:2b31d7ad244c 25
AzureIoTClient 2:2b31d7ad244c 26 #ifdef SAMPLE_MQTT
AzureIoTClient 0:c979e2f5511a 27 #include "iothubtransportmqtt.h"
AzureIoTClient 2:2b31d7ad244c 28 #endif // SAMPLE_MQTT
AzureIoTClient 2:2b31d7ad244c 29 #ifdef SAMPLE_MQTT_OVER_WEBSOCKETS
AzureIoTClient 2:2b31d7ad244c 30 #include "iothubtransportmqtt_websockets.h"
AzureIoTClient 2:2b31d7ad244c 31 #endif // SAMPLE_MQTT_OVER_WEBSOCKETS
AzureIoTClient 2:2b31d7ad244c 32 #ifdef SAMPLE_AMQP
AzureIoTClient 0:c979e2f5511a 33 #include "iothubtransportamqp.h"
AzureIoTClient 2:2b31d7ad244c 34 #endif // SAMPLE_AMQP
AzureIoTClient 2:2b31d7ad244c 35 #ifdef SAMPLE_AMQP_OVER_WEBSOCKETS
AzureIoTClient 2:2b31d7ad244c 36 #include "iothubtransportamqp_websockets.h"
AzureIoTClient 2:2b31d7ad244c 37 #endif // SAMPLE_AMQP_OVER_WEBSOCKETS
AzureIoTClient 2:2b31d7ad244c 38 #ifdef SAMPLE_HTTP
AzureIoTClient 0:c979e2f5511a 39 #include "iothubtransporthttp.h"
AzureIoTClient 2:2b31d7ad244c 40 #endif // SAMPLE_HTTP
AzureIoTClient 0:c979e2f5511a 41
AzureIoTClient 2:2b31d7ad244c 42 #ifdef SET_TRUSTED_CERT_IN_SAMPLES
AzureIoTClient 0:c979e2f5511a 43 #include "certs.h"
AzureIoTClient 2:2b31d7ad244c 44 #endif // SET_TRUSTED_CERT_IN_SAMPLES
AzureIoTClient 0:c979e2f5511a 45
AzureIoTClient 0:c979e2f5511a 46 /* Paste in the your iothub connection string */
AzureIoTClient 0:c979e2f5511a 47 static const char* connectionString = "[device connection string]";
AzureIoTClient 0:c979e2f5511a 48 #define MESSAGE_COUNT 5
AzureIoTClient 0:c979e2f5511a 49 static bool g_continueRunning = true;
AzureIoTClient 0:c979e2f5511a 50 static size_t g_message_count_send_confirmations = 0;
AzureIoTClient 0:c979e2f5511a 51
AzureIoTClient 0:c979e2f5511a 52 static void send_confirm_callback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
AzureIoTClient 0:c979e2f5511a 53 {
AzureIoTClient 0:c979e2f5511a 54 (void)userContextCallback;
AzureIoTClient 0:c979e2f5511a 55 // When a message is sent this callback will get envoked
AzureIoTClient 0:c979e2f5511a 56 g_message_count_send_confirmations++;
AzureIoTClient 0:c979e2f5511a 57 (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 58 }
AzureIoTClient 0:c979e2f5511a 59
AzureIoTClient 0:c979e2f5511a 60 int main(void)
AzureIoTClient 0:c979e2f5511a 61 {
AzureIoTClient 0:c979e2f5511a 62 IOTHUB_CLIENT_TRANSPORT_PROVIDER protocol;
AzureIoTClient 0:c979e2f5511a 63 IOTHUB_MESSAGE_HANDLE message_handle;
AzureIoTClient 0:c979e2f5511a 64 size_t messages_sent = 0;
AzureIoTClient 0:c979e2f5511a 65 const char* telemetry_msg = "test_message";
AzureIoTClient 0:c979e2f5511a 66
AzureIoTClient 2:2b31d7ad244c 67 // Select the Protocol to use with the connection
AzureIoTClient 2:2b31d7ad244c 68 #ifdef SAMPLE_MQTT
AzureIoTClient 2:2b31d7ad244c 69 protocol = MQTT_Protocol;
AzureIoTClient 2:2b31d7ad244c 70 #endif // SAMPLE_MQTT
AzureIoTClient 2:2b31d7ad244c 71 #ifdef SAMPLE_MQTT_OVER_WEBSOCKETS
AzureIoTClient 2:2b31d7ad244c 72 protocol = MQTT_WebSocket_Protocol;
AzureIoTClient 2:2b31d7ad244c 73 #endif // SAMPLE_MQTT_OVER_WEBSOCKETS
AzureIoTClient 2:2b31d7ad244c 74 #ifdef SAMPLE_AMQP
AzureIoTClient 2:2b31d7ad244c 75 protocol = AMQP_Protocol;
AzureIoTClient 2:2b31d7ad244c 76 #endif // SAMPLE_AMQP
AzureIoTClient 2:2b31d7ad244c 77 #ifdef SAMPLE_AMQP_OVER_WEBSOCKETS
AzureIoTClient 2:2b31d7ad244c 78 protocol = AMQP_Protocol_over_WebSocketsTls;
AzureIoTClient 2:2b31d7ad244c 79 #endif // SAMPLE_AMQP_OVER_WEBSOCKETS
AzureIoTClient 2:2b31d7ad244c 80 #ifdef SAMPLE_HTTP
AzureIoTClient 1:589bbd7948f3 81 protocol = HTTP_Protocol;
AzureIoTClient 2:2b31d7ad244c 82 #endif // SAMPLE_HTTP
AzureIoTClient 0:c979e2f5511a 83
AzureIoTClient 0:c979e2f5511a 84 IOTHUB_CLIENT_LL_HANDLE iothub_ll_handle;
AzureIoTClient 0:c979e2f5511a 85
AzureIoTClient 0:c979e2f5511a 86 // Used to initialize IoTHub SDK subsystem
AzureIoTClient 0:c979e2f5511a 87 (void)platform_init();
AzureIoTClient 0:c979e2f5511a 88
AzureIoTClient 0:c979e2f5511a 89 (void)printf("Creating IoTHub handle\r\n");
AzureIoTClient 0:c979e2f5511a 90 // Create the iothub handle here
AzureIoTClient 0:c979e2f5511a 91 iothub_ll_handle = IoTHubClient_LL_CreateFromConnectionString(connectionString, protocol);
AzureIoTClient 0:c979e2f5511a 92
AzureIoTClient 0:c979e2f5511a 93 // Set any option that are neccessary.
AzureIoTClient 0:c979e2f5511a 94 // For available options please see the iothub_sdk_options.md documentation
AzureIoTClient 2:2b31d7ad244c 95
AzureIoTClient 0:c979e2f5511a 96 //bool traceOn = true;
AzureIoTClient 0:c979e2f5511a 97 //IoTHubClient_LL_SetOption(iothub_ll_handle, OPTION_LOG_TRACE, &traceOn);
AzureIoTClient 2:2b31d7ad244c 98
AzureIoTClient 2:2b31d7ad244c 99 #ifdef SET_TRUSTED_CERT_IN_SAMPLES
AzureIoTClient 0:c979e2f5511a 100 // Setting the Trusted Certificate. This is only necessary on system with without
AzureIoTClient 0:c979e2f5511a 101 // built in certificate stores.
AzureIoTClient 0:c979e2f5511a 102 IoTHubClient_LL_SetOption(iothub_ll_handle, OPTION_TRUSTED_CERT, certificates);
AzureIoTClient 2:2b31d7ad244c 103 #endif // SET_TRUSTED_CERT_IN_SAMPLES
AzureIoTClient 2:2b31d7ad244c 104
AzureIoTClient 2:2b31d7ad244c 105 #if defined SAMPLE_MQTT || defined SAMPLE_MQTT_WS
AzureIoTClient 2:2b31d7ad244c 106 //Setting the auto URL Encoder (recommended for MQTT). Please use this option unless
AzureIoTClient 2:2b31d7ad244c 107 //you are URL Encoding inputs yourself.
AzureIoTClient 2:2b31d7ad244c 108 //ONLY valid for use with MQTT
AzureIoTClient 2:2b31d7ad244c 109 //bool urlEncodeOn = true;
AzureIoTClient 2:2b31d7ad244c 110 //IoTHubClient_LL_SetOption(iothub_ll_handle, OPTION_AUTO_URL_ENCODE_DECODE, &urlEncodeOn);
AzureIoTClient 2:2b31d7ad244c 111 #endif
AzureIoTClient 0:c979e2f5511a 112
AzureIoTClient 0:c979e2f5511a 113 do
AzureIoTClient 0:c979e2f5511a 114 {
AzureIoTClient 0:c979e2f5511a 115 if (messages_sent < MESSAGE_COUNT)
AzureIoTClient 0:c979e2f5511a 116 {
AzureIoTClient 0:c979e2f5511a 117 // Construct the iothub message from a string or a byte array
AzureIoTClient 0:c979e2f5511a 118 message_handle = IoTHubMessage_CreateFromString(telemetry_msg);
AzureIoTClient 0:c979e2f5511a 119 //message_handle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText)));
AzureIoTClient 0:c979e2f5511a 120
AzureIoTClient 0:c979e2f5511a 121 // Set Message property
AzureIoTClient 0:c979e2f5511a 122 (void)IoTHubMessage_SetMessageId(message_handle, "MSG_ID");
AzureIoTClient 0:c979e2f5511a 123 (void)IoTHubMessage_SetCorrelationId(message_handle, "CORE_ID");
AzureIoTClient 2:2b31d7ad244c 124 (void)IoTHubMessage_SetContentTypeSystemProperty(message_handle, "application%2fjson");
AzureIoTClient 0:c979e2f5511a 125 (void)IoTHubMessage_SetContentEncodingSystemProperty(message_handle, "utf-8");
AzureIoTClient 0:c979e2f5511a 126
AzureIoTClient 0:c979e2f5511a 127 // Add custom properties to message
AzureIoTClient 0:c979e2f5511a 128 MAP_HANDLE propMap = IoTHubMessage_Properties(message_handle);
AzureIoTClient 0:c979e2f5511a 129 Map_AddOrUpdate(propMap, "property_key", "property_value");
AzureIoTClient 0:c979e2f5511a 130
AzureIoTClient 2:2b31d7ad244c 131 (void)printf("Sending message %d to IoTHub\r\n", (int)(messages_sent + 1));
AzureIoTClient 0:c979e2f5511a 132 IoTHubClient_LL_SendEventAsync(iothub_ll_handle, message_handle, send_confirm_callback, NULL);
AzureIoTClient 0:c979e2f5511a 133
AzureIoTClient 0:c979e2f5511a 134 // The message is copied to the sdk so the we can destroy it
AzureIoTClient 0:c979e2f5511a 135 IoTHubMessage_Destroy(message_handle);
AzureIoTClient 0:c979e2f5511a 136
AzureIoTClient 0:c979e2f5511a 137 messages_sent++;
AzureIoTClient 0:c979e2f5511a 138 }
AzureIoTClient 0:c979e2f5511a 139 else if (g_message_count_send_confirmations >= MESSAGE_COUNT)
AzureIoTClient 0:c979e2f5511a 140 {
AzureIoTClient 0:c979e2f5511a 141 // After all messages are all received stop running
AzureIoTClient 0:c979e2f5511a 142 g_continueRunning = false;
AzureIoTClient 0:c979e2f5511a 143 }
AzureIoTClient 0:c979e2f5511a 144
AzureIoTClient 0:c979e2f5511a 145 IoTHubClient_LL_DoWork(iothub_ll_handle);
AzureIoTClient 0:c979e2f5511a 146 ThreadAPI_Sleep(1);
AzureIoTClient 0:c979e2f5511a 147
AzureIoTClient 0:c979e2f5511a 148 } while (g_continueRunning);
AzureIoTClient 0:c979e2f5511a 149
AzureIoTClient 0:c979e2f5511a 150 // Clean up the iothub sdk handle
AzureIoTClient 0:c979e2f5511a 151 IoTHubClient_LL_Destroy(iothub_ll_handle);
AzureIoTClient 0:c979e2f5511a 152
AzureIoTClient 0:c979e2f5511a 153 // Free all the sdk subsystem
AzureIoTClient 0:c979e2f5511a 154 platform_deinit();
AzureIoTClient 0:c979e2f5511a 155
AzureIoTClient 0:c979e2f5511a 156 printf("Press any key to continue");
AzureIoTClient 0:c979e2f5511a 157 getchar();
AzureIoTClient 0:c979e2f5511a 158
AzureIoTClient 0:c979e2f5511a 159 return 0;
AzureIoTClient 0:c979e2f5511a 160 }