IoTHub raw messaging client sample using MQTT

Dependencies:   EthernetInterface NTPClient mbed-rtos mbed wolfSSL azure_c_shared_utility iothub_client azure_umqtt_c iothub_mqtt_transport

Committer:
AzureIoTClient
Date:
Wed Jan 17 09:00:26 2018 -0800
Revision:
51:5a977cdbf27a
Parent:
47:7a04f9462a19
1.1.30

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Azure.IoT Build 2:65c172c36cf5 1 // Copyright (c) Microsoft. All rights reserved.
Azure.IoT Build 2:65c172c36cf5 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
Azure.IoT Build 2:65c172c36cf5 3
Azure.IoT Build 2:65c172c36cf5 4 #include <stdio.h>
Azure.IoT Build 2:65c172c36cf5 5 #include <stdlib.h>
Azure.IoT Build 2:65c172c36cf5 6
Azure.IoT Build 2:65c172c36cf5 7 #include "iothub_client.h"
Azure.IoT Build 2:65c172c36cf5 8 #include "iothub_message.h"
AzureIoTClient 6:02304619a9fa 9 #include "azure_c_shared_utility/threadapi.h"
AzureIoTClient 6:02304619a9fa 10 #include "azure_c_shared_utility/crt_abstractions.h"
Azure.IoT Build 14:0d0177062483 11 #include "azure_c_shared_utility/platform.h"
AzureIoTClient 47:7a04f9462a19 12 #include "azure_c_shared_utility/shared_util_options.h"
Azure.IoT Build 2:65c172c36cf5 13 #include "iothubtransportmqtt.h"
AzureIoTClient 47:7a04f9462a19 14 #include "iothub_client_options.h"
Azure.IoT Build 2:65c172c36cf5 15
Azure.IoT Build 2:65c172c36cf5 16 #ifdef MBED_BUILD_TIMESTAMP
AzureIoTClient 45:c5727007e4f0 17 #define SET_TRUSTED_CERT_IN_SAMPLES
AzureIoTClient 45:c5727007e4f0 18 #endif // MBED_BUILD_TIMESTAMP
AzureIoTClient 45:c5727007e4f0 19
AzureIoTClient 45:c5727007e4f0 20 #ifdef SET_TRUSTED_CERT_IN_SAMPLES
Azure.IoT Build 2:65c172c36cf5 21 #include "certs.h"
AzureIoTClient 45:c5727007e4f0 22 #endif // SET_TRUSTED_CERT_IN_SAMPLES
Azure.IoT Build 2:65c172c36cf5 23
AzureIoTClient 7:02bc67ebbe23 24 /*String containing Hostname, Device Id & Device Key in the format: */
AzureIoTClient 7:02bc67ebbe23 25 /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
AzureIoTClient 7:02bc67ebbe23 26 /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>" */
Azure.IoT Build 2:65c172c36cf5 27 static const char* connectionString = "[device connection string]";
Azure.IoT Build 2:65c172c36cf5 28
Azure.IoT Build 2:65c172c36cf5 29 static int callbackCounter;
Azure.IoT Build 2:65c172c36cf5 30 static char msgText[1024];
AzureIoTClient 6:02304619a9fa 31 static char propText[1024];
Azure.IoT Build 14:0d0177062483 32 static bool g_continueRunning;
Azure.IoT Build 2:65c172c36cf5 33 #define MESSAGE_COUNT 5
Azure.IoT Build 14:0d0177062483 34 #define DOWORK_LOOP_NUM 3
Azure.IoT Build 2:65c172c36cf5 35
Azure.IoT Build 2:65c172c36cf5 36
Azure.IoT Build 2:65c172c36cf5 37 typedef struct EVENT_INSTANCE_TAG
Azure.IoT Build 2:65c172c36cf5 38 {
Azure.IoT Build 2:65c172c36cf5 39 IOTHUB_MESSAGE_HANDLE messageHandle;
AzureIoTClient 15:2f2b3dc3ecb4 40 size_t messageTrackingId; // For tracking the messages within the user callback.
Azure.IoT Build 2:65c172c36cf5 41 } EVENT_INSTANCE;
Azure.IoT Build 2:65c172c36cf5 42
Azure.IoT Build 2:65c172c36cf5 43 static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
Azure.IoT Build 2:65c172c36cf5 44 {
Azure.IoT Build 2:65c172c36cf5 45 int* counter = (int*)userContextCallback;
Azure.IoT Build 2:65c172c36cf5 46 const char* buffer;
Azure.IoT Build 2:65c172c36cf5 47 size_t size;
AzureIoTClient 30:0a147a179e7e 48 MAP_HANDLE mapProperties;
AzureIoTClient 30:0a147a179e7e 49 const char* messageId;
AzureIoTClient 30:0a147a179e7e 50 const char* correlationId;
AzureIoTClient 44:093816abb152 51 const char* userDefinedContentType;
AzureIoTClient 44:093816abb152 52 const char* userDefinedContentEncoding;
AzureIoTClient 10:f77bb0044e11 53
AzureIoTClient 30:0a147a179e7e 54 // Message properties
AzureIoTClient 30:0a147a179e7e 55 if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL)
AzureIoTClient 30:0a147a179e7e 56 {
AzureIoTClient 30:0a147a179e7e 57 messageId = "<null>";
AzureIoTClient 30:0a147a179e7e 58 }
AzureIoTClient 30:0a147a179e7e 59
AzureIoTClient 30:0a147a179e7e 60 if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL)
AzureIoTClient 30:0a147a179e7e 61 {
AzureIoTClient 30:0a147a179e7e 62 correlationId = "<null>";
AzureIoTClient 30:0a147a179e7e 63 }
AzureIoTClient 30:0a147a179e7e 64
AzureIoTClient 44:093816abb152 65 if ((userDefinedContentType = IoTHubMessage_GetContentTypeSystemProperty(message)) == NULL)
AzureIoTClient 44:093816abb152 66 {
AzureIoTClient 44:093816abb152 67 userDefinedContentType = "<null>";
AzureIoTClient 44:093816abb152 68 }
AzureIoTClient 44:093816abb152 69
AzureIoTClient 44:093816abb152 70 if ((userDefinedContentEncoding = IoTHubMessage_GetContentEncodingSystemProperty(message)) == NULL)
AzureIoTClient 44:093816abb152 71 {
AzureIoTClient 44:093816abb152 72 userDefinedContentEncoding = "<null>";
AzureIoTClient 44:093816abb152 73 }
AzureIoTClient 44:093816abb152 74
AzureIoTClient 30:0a147a179e7e 75 // Message content
Azure.IoT Build 2:65c172c36cf5 76 if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK)
Azure.IoT Build 2:65c172c36cf5 77 {
Azure.IoT Build 2:65c172c36cf5 78 (void)printf("unable to retrieve the message data\r\n");
Azure.IoT Build 2:65c172c36cf5 79 }
Azure.IoT Build 2:65c172c36cf5 80 else
Azure.IoT Build 2:65c172c36cf5 81 {
AzureIoTClient 44:093816abb152 82 (void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n Content-Type: %s\r\n Content-Encoding: %s\r\n Data: <<<%.*s>>> & Size=%d\r\n",
AzureIoTClient 44:093816abb152 83 *counter, messageId, correlationId, userDefinedContentType, userDefinedContentEncoding, (int)size, buffer, (int)size);
Azure.IoT Build 2:65c172c36cf5 84 // If we receive the work 'quit' then we stop running
AzureIoTClient 30:0a147a179e7e 85 if (size == (strlen("quit") * sizeof(char)) && memcmp(buffer, "quit", size) == 0)
Azure.IoT Build 2:65c172c36cf5 86 {
AzureIoTClient 10:f77bb0044e11 87 g_continueRunning = false;
Azure.IoT Build 2:65c172c36cf5 88 }
Azure.IoT Build 2:65c172c36cf5 89 }
Azure.IoT Build 2:65c172c36cf5 90
Azure.IoT Build 2:65c172c36cf5 91 // Retrieve properties from the message
AzureIoTClient 30:0a147a179e7e 92 mapProperties = IoTHubMessage_Properties(message);
Azure.IoT Build 2:65c172c36cf5 93 if (mapProperties != NULL)
Azure.IoT Build 2:65c172c36cf5 94 {
Azure.IoT Build 2:65c172c36cf5 95 const char*const* keys;
Azure.IoT Build 2:65c172c36cf5 96 const char*const* values;
Azure.IoT Build 2:65c172c36cf5 97 size_t propertyCount = 0;
Azure.IoT Build 2:65c172c36cf5 98 if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK)
Azure.IoT Build 2:65c172c36cf5 99 {
Azure.IoT Build 2:65c172c36cf5 100 if (propertyCount > 0)
Azure.IoT Build 2:65c172c36cf5 101 {
AzureIoTClient 30:0a147a179e7e 102 size_t index;
AzureIoTClient 30:0a147a179e7e 103
AzureIoTClient 30:0a147a179e7e 104 printf(" Message Properties:\r\n");
AzureIoTClient 27:7c908b91210a 105 for (index = 0; index < propertyCount; index++)
Azure.IoT Build 2:65c172c36cf5 106 {
Azure.IoT Build 2:65c172c36cf5 107 (void)printf("\tKey: %s Value: %s\r\n", keys[index], values[index]);
Azure.IoT Build 2:65c172c36cf5 108 }
Azure.IoT Build 2:65c172c36cf5 109 (void)printf("\r\n");
Azure.IoT Build 2:65c172c36cf5 110 }
Azure.IoT Build 2:65c172c36cf5 111 }
Azure.IoT Build 2:65c172c36cf5 112 }
Azure.IoT Build 2:65c172c36cf5 113
Azure.IoT Build 2:65c172c36cf5 114 /* Some device specific action code goes here... */
Azure.IoT Build 2:65c172c36cf5 115 (*counter)++;
Azure.IoT Build 2:65c172c36cf5 116 return IOTHUBMESSAGE_ACCEPTED;
Azure.IoT Build 2:65c172c36cf5 117 }
Azure.IoT Build 2:65c172c36cf5 118
Azure.IoT Build 2:65c172c36cf5 119 static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
Azure.IoT Build 2:65c172c36cf5 120 {
Azure.IoT Build 2:65c172c36cf5 121 EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
AzureIoTClient 15:2f2b3dc3ecb4 122 (void)printf("Confirmation[%d] received for message tracking id = %zu with result = %s\r\n", callbackCounter, eventInstance->messageTrackingId, ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
Azure.IoT Build 2:65c172c36cf5 123 /* Some device specific action code goes here... */
Azure.IoT Build 2:65c172c36cf5 124 callbackCounter++;
Azure.IoT Build 2:65c172c36cf5 125 IoTHubMessage_Destroy(eventInstance->messageHandle);
Azure.IoT Build 2:65c172c36cf5 126 }
Azure.IoT Build 2:65c172c36cf5 127
Azure.IoT Build 2:65c172c36cf5 128 void iothub_client_sample_mqtt_run(void)
Azure.IoT Build 2:65c172c36cf5 129 {
Azure.IoT Build 2:65c172c36cf5 130 IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle;
Azure.IoT Build 2:65c172c36cf5 131
Azure.IoT Build 2:65c172c36cf5 132 EVENT_INSTANCE messages[MESSAGE_COUNT];
Azure.IoT Build 2:65c172c36cf5 133
AzureIoTClient 10:f77bb0044e11 134 g_continueRunning = true;
Azure.IoT Build 2:65c172c36cf5 135 srand((unsigned int)time(NULL));
Azure.IoT Build 2:65c172c36cf5 136 double avgWindSpeed = 10.0;
AzureIoTClient 36:eb9d95ee6d63 137 double minTemperature = 20.0;
AzureIoTClient 36:eb9d95ee6d63 138 double minHumidity = 60.0;
Azure.IoT Build 2:65c172c36cf5 139
Azure.IoT Build 2:65c172c36cf5 140 callbackCounter = 0;
Azure.IoT Build 2:65c172c36cf5 141 int receiveContext = 0;
Azure.IoT Build 2:65c172c36cf5 142
Azure.IoT Build 2:65c172c36cf5 143 if (platform_init() != 0)
Azure.IoT Build 2:65c172c36cf5 144 {
Azure.IoT Build 2:65c172c36cf5 145 (void)printf("Failed to initialize the platform.\r\n");
Azure.IoT Build 2:65c172c36cf5 146 }
Azure.IoT Build 2:65c172c36cf5 147 else
Azure.IoT Build 2:65c172c36cf5 148 {
Azure.IoT Build 2:65c172c36cf5 149 if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, MQTT_Protocol)) == NULL)
Azure.IoT Build 2:65c172c36cf5 150 {
Azure.IoT Build 2:65c172c36cf5 151 (void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
Azure.IoT Build 2:65c172c36cf5 152 }
Azure.IoT Build 2:65c172c36cf5 153 else
Azure.IoT Build 2:65c172c36cf5 154 {
Azure.IoT Build 2:65c172c36cf5 155 bool traceOn = true;
AzureIoTClient 47:7a04f9462a19 156 IoTHubClient_LL_SetOption(iotHubClientHandle, OPTION_LOG_TRACE, &traceOn);
Azure.IoT Build 2:65c172c36cf5 157
AzureIoTClient 45:c5727007e4f0 158 #ifdef SET_TRUSTED_CERT_IN_SAMPLES
Azure.IoT Build 2:65c172c36cf5 159 // For mbed add the certificate information
AzureIoTClient 47:7a04f9462a19 160 if (IoTHubClient_LL_SetOption(iotHubClientHandle, OPTION_TRUSTED_CERT, certificates) != IOTHUB_CLIENT_OK)
Azure.IoT Build 2:65c172c36cf5 161 {
Azure.IoT Build 2:65c172c36cf5 162 printf("failure to set option \"TrustedCerts\"\r\n");
Azure.IoT Build 2:65c172c36cf5 163 }
AzureIoTClient 45:c5727007e4f0 164 #endif // SET_TRUSTED_CERT_IN_SAMPLES
Azure.IoT Build 2:65c172c36cf5 165
Azure.IoT Build 2:65c172c36cf5 166 /* Setting Message call back, so we can receive Commands. */
Azure.IoT Build 2:65c172c36cf5 167 if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK)
Azure.IoT Build 2:65c172c36cf5 168 {
Azure.IoT Build 2:65c172c36cf5 169 (void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n");
Azure.IoT Build 2:65c172c36cf5 170 }
Azure.IoT Build 2:65c172c36cf5 171 else
Azure.IoT Build 2:65c172c36cf5 172 {
Azure.IoT Build 2:65c172c36cf5 173 (void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n");
Azure.IoT Build 2:65c172c36cf5 174
Azure.IoT Build 2:65c172c36cf5 175 /* Now that we are ready to receive commands, let's send some messages */
AzureIoTClient 10:f77bb0044e11 176 size_t iterator = 0;
AzureIoTClient 36:eb9d95ee6d63 177 double temperature = 0;
AzureIoTClient 36:eb9d95ee6d63 178 double humidity = 0;
AzureIoTClient 10:f77bb0044e11 179 do
Azure.IoT Build 2:65c172c36cf5 180 {
AzureIoTClient 10:f77bb0044e11 181 if (iterator < MESSAGE_COUNT)
Azure.IoT Build 2:65c172c36cf5 182 {
AzureIoTClient 36:eb9d95ee6d63 183 temperature = minTemperature + (rand() % 10);
AzureIoTClient 36:eb9d95ee6d63 184 humidity = minHumidity + (rand() % 20);
AzureIoTClient 36:eb9d95ee6d63 185 sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"myFirstDevice\",\"windSpeed\":%.2f,\"temperature\":%.2f,\"humidity\":%.2f}", avgWindSpeed + (rand() % 4 + 2), temperature, humidity);
AzureIoTClient 10:f77bb0044e11 186 if ((messages[iterator].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
AzureIoTClient 6:02304619a9fa 187 {
AzureIoTClient 10:f77bb0044e11 188 (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
Azure.IoT Build 2:65c172c36cf5 189 }
Azure.IoT Build 2:65c172c36cf5 190 else
Azure.IoT Build 2:65c172c36cf5 191 {
AzureIoTClient 37:fdee5b96897d 192
AzureIoTClient 37:fdee5b96897d 193 (void)IoTHubMessage_SetMessageId(messages[iterator].messageHandle, "MSG_ID");
AzureIoTClient 37:fdee5b96897d 194 (void)IoTHubMessage_SetCorrelationId(messages[iterator].messageHandle, "CORE_ID");
AzureIoTClient 44:093816abb152 195 (void)IoTHubMessage_SetContentTypeSystemProperty(messages[iterator].messageHandle, "application%2Fjson");
AzureIoTClient 44:093816abb152 196 (void)IoTHubMessage_SetContentEncodingSystemProperty(messages[iterator].messageHandle, "utf-8");
AzureIoTClient 37:fdee5b96897d 197
AzureIoTClient 10:f77bb0044e11 198 messages[iterator].messageTrackingId = iterator;
AzureIoTClient 10:f77bb0044e11 199 MAP_HANDLE propMap = IoTHubMessage_Properties(messages[iterator].messageHandle);
AzureIoTClient 36:eb9d95ee6d63 200 (void)sprintf_s(propText, sizeof(propText), temperature > 28 ? "true" : "false");
AzureIoTClient 36:eb9d95ee6d63 201 if (Map_AddOrUpdate(propMap, "temperatureAlert", propText) != MAP_OK)
AzureIoTClient 10:f77bb0044e11 202 {
AzureIoTClient 10:f77bb0044e11 203 (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
AzureIoTClient 10:f77bb0044e11 204 }
AzureIoTClient 10:f77bb0044e11 205
AzureIoTClient 10:f77bb0044e11 206 if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator].messageHandle, SendConfirmationCallback, &messages[iterator]) != IOTHUB_CLIENT_OK)
AzureIoTClient 10:f77bb0044e11 207 {
AzureIoTClient 10:f77bb0044e11 208 (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
AzureIoTClient 10:f77bb0044e11 209 }
AzureIoTClient 10:f77bb0044e11 210 else
AzureIoTClient 10:f77bb0044e11 211 {
AzureIoTClient 10:f77bb0044e11 212 (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", (int)iterator);
AzureIoTClient 10:f77bb0044e11 213 }
Azure.IoT Build 2:65c172c36cf5 214 }
AzureIoTClient 37:fdee5b96897d 215
Azure.IoT Build 2:65c172c36cf5 216 }
AzureIoTClient 10:f77bb0044e11 217 IoTHubClient_LL_DoWork(iotHubClientHandle);
AzureIoTClient 10:f77bb0044e11 218 ThreadAPI_Sleep(1);
Azure.IoT Build 2:65c172c36cf5 219
AzureIoTClient 10:f77bb0044e11 220 iterator++;
AzureIoTClient 10:f77bb0044e11 221 } while (g_continueRunning);
Azure.IoT Build 14:0d0177062483 222
Azure.IoT Build 14:0d0177062483 223 (void)printf("iothub_client_sample_mqtt has gotten quit message, call DoWork %d more time to complete final sending...\r\n", DOWORK_LOOP_NUM);
AzureIoTClient 27:7c908b91210a 224 size_t index = 0;
AzureIoTClient 27:7c908b91210a 225 for (index = 0; index < DOWORK_LOOP_NUM; index++)
Azure.IoT Build 14:0d0177062483 226 {
Azure.IoT Build 14:0d0177062483 227 IoTHubClient_LL_DoWork(iotHubClientHandle);
Azure.IoT Build 14:0d0177062483 228 ThreadAPI_Sleep(1);
Azure.IoT Build 14:0d0177062483 229 }
Azure.IoT Build 2:65c172c36cf5 230 }
Azure.IoT Build 2:65c172c36cf5 231 IoTHubClient_LL_Destroy(iotHubClientHandle);
Azure.IoT Build 2:65c172c36cf5 232 }
Azure.IoT Build 2:65c172c36cf5 233 platform_deinit();
Azure.IoT Build 2:65c172c36cf5 234 }
Azure.IoT Build 2:65c172c36cf5 235 }
AzureIoTClient 10:f77bb0044e11 236
AzureIoTClient 10:f77bb0044e11 237 int main(void)
AzureIoTClient 10:f77bb0044e11 238 {
AzureIoTClient 10:f77bb0044e11 239 iothub_client_sample_mqtt_run();
AzureIoTClient 10:f77bb0044e11 240 return 0;
AzureIoTClient 10:f77bb0044e11 241 }