IoTHub raw messaging client sample using AMQP

Dependencies:   iothub_client EthernetInterface NTPClient iothub_amqp_transport azure_c_shared_utility mbed-rtos mbed azure_uamqp_c wolfSSL

This sample showcases the usage of Azure IoT client libraries with the AMQP transport for sending/receiving raw messages from an IoT Hub.

Committer:
AzureIoTClient
Date:
Fri Aug 25 11:23:59 2017 -0700
Revision:
78:7acff4261af4
Parent:
74:d20e4ff30068
Child:
79:21537a6b9511
1.1.22

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 17:d01a2794d5d7 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 17:d01a2794d5d7 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 17:d01a2794d5d7 3
AzureIoTClient 17:d01a2794d5d7 4 #include <stdio.h>
AzureIoTClient 17:d01a2794d5d7 5 #include <stdlib.h>
AzureIoTClient 17:d01a2794d5d7 6
AzureIoTClient 39:87752a84df87 7 #include "azure_c_shared_utility/platform.h"
Azure.IoT Build 47:d87920c7f211 8 #include "azure_c_shared_utility/threadapi.h"
Azure.IoT Build 47:d87920c7f211 9 #include "azure_c_shared_utility/crt_abstractions.h"
AzureIoTClient 17:d01a2794d5d7 10 #include "iothub_client.h"
AzureIoTClient 17:d01a2794d5d7 11 #include "iothub_message.h"
AzureIoTClient 17:d01a2794d5d7 12 #include "iothubtransportamqp.h"
AzureIoTClient 17:d01a2794d5d7 13
AzureIoTClient 17:d01a2794d5d7 14 #ifdef MBED_BUILD_TIMESTAMP
AzureIoTClient 17:d01a2794d5d7 15 #include "certs.h"
AzureIoTClient 17:d01a2794d5d7 16 #endif // MBED_BUILD_TIMESTAMP
AzureIoTClient 17:d01a2794d5d7 17
AzureIoTClient 40:548ef685e177 18 /*String containing Hostname, Device Id & Device Key in the format: */
AzureIoTClient 40:548ef685e177 19 /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
AzureIoTClient 40:548ef685e177 20 /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>" */
AzureIoTClient 28:a1105bba4bb1 21 static const char* connectionString = "[device connection string]";
AzureIoTClient 40:548ef685e177 22
AzureIoTClient 17:d01a2794d5d7 23 static int callbackCounter;
AzureIoTClient 43:a4b2614eb860 24 static bool g_continueRunning;
Azure.IoT Build 47:d87920c7f211 25 static char msgText[1024];
Azure.IoT Build 47:d87920c7f211 26 static char propText[1024];
Azure.IoT Build 47:d87920c7f211 27 #define MESSAGE_COUNT 5
Azure.IoT Build 47:d87920c7f211 28 #define DOWORK_LOOP_NUM 3
AzureIoTClient 17:d01a2794d5d7 29
AzureIoTClient 17:d01a2794d5d7 30 typedef struct EVENT_INSTANCE_TAG
AzureIoTClient 17:d01a2794d5d7 31 {
AzureIoTClient 17:d01a2794d5d7 32 IOTHUB_MESSAGE_HANDLE messageHandle;
AzureIoTClient 48:ecd0964514f8 33 size_t messageTrackingId; // For tracking the messages within the user callback.
AzureIoTClient 17:d01a2794d5d7 34 } EVENT_INSTANCE;
AzureIoTClient 17:d01a2794d5d7 35
AzureIoTClient 17:d01a2794d5d7 36 static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
AzureIoTClient 17:d01a2794d5d7 37 {
AzureIoTClient 17:d01a2794d5d7 38 int* counter = (int*)userContextCallback;
Azure.IoT Build 31:7a8aed41e109 39 const unsigned char* buffer = NULL;
Azure.IoT Build 31:7a8aed41e109 40 size_t size = 0;
AzureIoTClient 43:a4b2614eb860 41 const char* messageId;
AzureIoTClient 43:a4b2614eb860 42 const char* correlationId;
AzureIoTClient 78:7acff4261af4 43 const char* userDefinedContentType;
AzureIoTClient 78:7acff4261af4 44 const char* userDefinedContentEncoding;
AzureIoTClient 40:548ef685e177 45
AzureIoTClient 63:14e9684b52e3 46 // Message properties
AzureIoTClient 43:a4b2614eb860 47 if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL)
AzureIoTClient 43:a4b2614eb860 48 {
AzureIoTClient 43:a4b2614eb860 49 messageId = "<null>";
AzureIoTClient 43:a4b2614eb860 50 }
AzureIoTClient 40:548ef685e177 51
AzureIoTClient 43:a4b2614eb860 52 if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL)
AzureIoTClient 43:a4b2614eb860 53 {
AzureIoTClient 43:a4b2614eb860 54 correlationId = "<null>";
AzureIoTClient 43:a4b2614eb860 55 }
AzureIoTClient 40:548ef685e177 56
AzureIoTClient 78:7acff4261af4 57 if ((userDefinedContentType = IoTHubMessage_GetContentTypeSystemProperty(message)) == NULL)
AzureIoTClient 78:7acff4261af4 58 {
AzureIoTClient 78:7acff4261af4 59 userDefinedContentType = "<null>";
AzureIoTClient 78:7acff4261af4 60 }
AzureIoTClient 78:7acff4261af4 61
AzureIoTClient 78:7acff4261af4 62 if ((userDefinedContentEncoding = IoTHubMessage_GetContentEncodingSystemProperty(message)) == NULL)
AzureIoTClient 78:7acff4261af4 63 {
AzureIoTClient 78:7acff4261af4 64 userDefinedContentEncoding = "<null>";
AzureIoTClient 78:7acff4261af4 65 }
AzureIoTClient 78:7acff4261af4 66
AzureIoTClient 63:14e9684b52e3 67 // Message content
Azure.IoT Build 31:7a8aed41e109 68 IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message);
AzureIoTClient 19:de28463ff3cc 69
Azure.IoT Build 31:7a8aed41e109 70 if (contentType == IOTHUBMESSAGE_BYTEARRAY)
Azure.IoT Build 31:7a8aed41e109 71 {
Azure.IoT Build 31:7a8aed41e109 72 if (IoTHubMessage_GetByteArray(message, &buffer, &size) == IOTHUB_MESSAGE_OK)
Azure.IoT Build 31:7a8aed41e109 73 {
AzureIoTClient 78:7acff4261af4 74 (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 BINARY Data: <<<%.*s>>> & Size=%d\r\n",
AzureIoTClient 78:7acff4261af4 75 *counter, messageId, correlationId, userDefinedContentType, userDefinedContentEncoding, (int)size, buffer, (int)size);
Azure.IoT Build 31:7a8aed41e109 76 }
Azure.IoT Build 31:7a8aed41e109 77 else
Azure.IoT Build 31:7a8aed41e109 78 {
Azure.IoT Build 31:7a8aed41e109 79 (void)printf("Failed getting the BINARY body of the message received.\r\n");
Azure.IoT Build 31:7a8aed41e109 80 }
Azure.IoT Build 31:7a8aed41e109 81 }
Azure.IoT Build 31:7a8aed41e109 82 else if (contentType == IOTHUBMESSAGE_STRING)
AzureIoTClient 17:d01a2794d5d7 83 {
AzureIoTClient 48:ecd0964514f8 84 if ((buffer = (const unsigned char*)IoTHubMessage_GetString(message)) != NULL && (size = strlen((const char*)buffer)) > 0)
Azure.IoT Build 31:7a8aed41e109 85 {
AzureIoTClient 78:7acff4261af4 86 (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 STRING Data: <<<%.*s>>> & Size=%d\r\n",
AzureIoTClient 78:7acff4261af4 87 *counter, messageId, correlationId, userDefinedContentType, userDefinedContentEncoding, (int)size, buffer, (int)size);
AzureIoTClient 43:a4b2614eb860 88
AzureIoTClient 43:a4b2614eb860 89 // If we receive the work 'quit' then we stop running
Azure.IoT Build 31:7a8aed41e109 90 }
Azure.IoT Build 31:7a8aed41e109 91 else
Azure.IoT Build 31:7a8aed41e109 92 {
Azure.IoT Build 31:7a8aed41e109 93 (void)printf("Failed getting the STRING body of the message received.\r\n");
Azure.IoT Build 31:7a8aed41e109 94 }
Azure.IoT Build 31:7a8aed41e109 95 }
Azure.IoT Build 31:7a8aed41e109 96 else
Azure.IoT Build 31:7a8aed41e109 97 {
Azure.IoT Build 31:7a8aed41e109 98 (void)printf("Failed getting the body of the message received (type %i).\r\n", contentType);
AzureIoTClient 17:d01a2794d5d7 99 }
AzureIoTClient 17:d01a2794d5d7 100
AzureIoTClient 17:d01a2794d5d7 101 // Retrieve properties from the message
AzureIoTClient 17:d01a2794d5d7 102 MAP_HANDLE mapProperties = IoTHubMessage_Properties(message);
AzureIoTClient 17:d01a2794d5d7 103 if (mapProperties != NULL)
AzureIoTClient 17:d01a2794d5d7 104 {
AzureIoTClient 17:d01a2794d5d7 105 const char*const* keys;
AzureIoTClient 17:d01a2794d5d7 106 const char*const* values;
AzureIoTClient 17:d01a2794d5d7 107 size_t propertyCount = 0;
AzureIoTClient 17:d01a2794d5d7 108 if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK)
AzureIoTClient 17:d01a2794d5d7 109 {
AzureIoTClient 17:d01a2794d5d7 110 if (propertyCount > 0)
AzureIoTClient 17:d01a2794d5d7 111 {
Azure.IoT Build 31:7a8aed41e109 112 size_t index;
Azure.IoT Build 31:7a8aed41e109 113
AzureIoTClient 63:14e9684b52e3 114 printf(" Message Properties:\r\n");
Azure.IoT Build 31:7a8aed41e109 115 for (index = 0; index < propertyCount; index++)
AzureIoTClient 17:d01a2794d5d7 116 {
AzureIoTClient 17:d01a2794d5d7 117 printf("\tKey: %s Value: %s\r\n", keys[index], values[index]);
AzureIoTClient 17:d01a2794d5d7 118 }
AzureIoTClient 17:d01a2794d5d7 119 printf("\r\n");
AzureIoTClient 17:d01a2794d5d7 120 }
AzureIoTClient 17:d01a2794d5d7 121 }
AzureIoTClient 17:d01a2794d5d7 122 }
AzureIoTClient 17:d01a2794d5d7 123
AzureIoTClient 69:06d895ef6bfc 124 if (size == (strlen("quit") * sizeof(char)) && memcmp(buffer, "quit", size) == 0)
AzureIoTClient 43:a4b2614eb860 125 {
AzureIoTClient 43:a4b2614eb860 126 g_continueRunning = false;
AzureIoTClient 43:a4b2614eb860 127 }
AzureIoTClient 43:a4b2614eb860 128
AzureIoTClient 17:d01a2794d5d7 129 /* Some device specific action code goes here... */
AzureIoTClient 17:d01a2794d5d7 130 (*counter)++;
AzureIoTClient 17:d01a2794d5d7 131 return IOTHUBMESSAGE_ACCEPTED;
AzureIoTClient 17:d01a2794d5d7 132 }
AzureIoTClient 17:d01a2794d5d7 133
AzureIoTClient 17:d01a2794d5d7 134 static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
AzureIoTClient 17:d01a2794d5d7 135 {
AzureIoTClient 17:d01a2794d5d7 136 EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
AzureIoTClient 48:ecd0964514f8 137 (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));
AzureIoTClient 17:d01a2794d5d7 138 /* Some device specific action code goes here... */
AzureIoTClient 17:d01a2794d5d7 139 callbackCounter++;
AzureIoTClient 17:d01a2794d5d7 140 IoTHubMessage_Destroy(eventInstance->messageHandle);
AzureIoTClient 17:d01a2794d5d7 141 }
AzureIoTClient 17:d01a2794d5d7 142
AzureIoTClient 17:d01a2794d5d7 143 void iothub_client_sample_amqp_run(void)
Azure.IoT Build 31:7a8aed41e109 144 {
AzureIoTClient 43:a4b2614eb860 145 IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle;
AzureIoTClient 17:d01a2794d5d7 146
AzureIoTClient 17:d01a2794d5d7 147 EVENT_INSTANCE messages[MESSAGE_COUNT];
AzureIoTClient 17:d01a2794d5d7 148
AzureIoTClient 43:a4b2614eb860 149 g_continueRunning = true;
AzureIoTClient 17:d01a2794d5d7 150 srand((unsigned int)time(NULL));
AzureIoTClient 17:d01a2794d5d7 151 double avgWindSpeed = 10.0;
AzureIoTClient 17:d01a2794d5d7 152
AzureIoTClient 17:d01a2794d5d7 153 callbackCounter = 0;
AzureIoTClient 17:d01a2794d5d7 154 int receiveContext = 0;
AzureIoTClient 17:d01a2794d5d7 155
AzureIoTClient 17:d01a2794d5d7 156 (void)printf("Starting the IoTHub client sample AMQP...\r\n");
Azure.IoT Build 34:b7f31511a8ad 157
Azure.IoT Build 34:b7f31511a8ad 158 if (platform_init() != 0)
AzureIoTClient 17:d01a2794d5d7 159 {
Azure.IoT Build 34:b7f31511a8ad 160 printf("Failed to initialize the platform.\r\n");
AzureIoTClient 17:d01a2794d5d7 161 }
AzureIoTClient 17:d01a2794d5d7 162 else
AzureIoTClient 17:d01a2794d5d7 163 {
AzureIoTClient 43:a4b2614eb860 164 if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, AMQP_Protocol)) == NULL)
AzureIoTClient 17:d01a2794d5d7 165 {
Azure.IoT Build 34:b7f31511a8ad 166 (void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
AzureIoTClient 17:d01a2794d5d7 167 }
AzureIoTClient 17:d01a2794d5d7 168 else
AzureIoTClient 17:d01a2794d5d7 169 {
AzureIoTClient 43:a4b2614eb860 170 bool traceOn = true;
AzureIoTClient 43:a4b2614eb860 171 IoTHubClient_LL_SetOption(iotHubClientHandle, "logtrace", &traceOn);
AzureIoTClient 17:d01a2794d5d7 172
AzureIoTClient 74:d20e4ff30068 173 // Set keep alive is optional. If it is not set the default (240 secs) will be used. If it is zero the service won't send keep alive messages.
AzureIoTClient 74:d20e4ff30068 174 uint32_t c2d_keep_alive_freq_secs = 120;
AzureIoTClient 74:d20e4ff30068 175 IoTHubClient_LL_SetOption(iotHubClientHandle, "c2d_keep_alive_freq_secs", &c2d_keep_alive_freq_secs);
AzureIoTClient 74:d20e4ff30068 176
Azure.IoT Build 34:b7f31511a8ad 177 #ifdef MBED_BUILD_TIMESTAMP
Azure.IoT Build 34:b7f31511a8ad 178 // For mbed add the certificate information
AzureIoTClient 43:a4b2614eb860 179 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
AzureIoTClient 17:d01a2794d5d7 180 {
Azure.IoT Build 34:b7f31511a8ad 181 printf("failure to set option \"TrustedCerts\"\r\n");
Azure.IoT Build 34:b7f31511a8ad 182 }
Azure.IoT Build 34:b7f31511a8ad 183 #endif // MBED_BUILD_TIMESTAMP
Azure.IoT Build 34:b7f31511a8ad 184
Azure.IoT Build 34:b7f31511a8ad 185 /* Setting Message call back, so we can receive Commands. */
AzureIoTClient 43:a4b2614eb860 186 if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK)
Azure.IoT Build 34:b7f31511a8ad 187 {
Azure.IoT Build 34:b7f31511a8ad 188 (void)printf("ERROR: IoTHubClient_SetMessageCallback..........FAILED!\r\n");
Azure.IoT Build 34:b7f31511a8ad 189 }
Azure.IoT Build 34:b7f31511a8ad 190 else
Azure.IoT Build 34:b7f31511a8ad 191 {
Azure.IoT Build 34:b7f31511a8ad 192 (void)printf("IoTHubClient_SetMessageCallback...successful.\r\n");
Azure.IoT Build 34:b7f31511a8ad 193
Azure.IoT Build 34:b7f31511a8ad 194 /* Now that we are ready to receive commands, let's send some messages */
AzureIoTClient 43:a4b2614eb860 195 size_t iterator = 0;
AzureIoTClient 43:a4b2614eb860 196 do
AzureIoTClient 17:d01a2794d5d7 197 {
AzureIoTClient 43:a4b2614eb860 198 if (iterator < MESSAGE_COUNT)
AzureIoTClient 17:d01a2794d5d7 199 {
AzureIoTClient 43:a4b2614eb860 200 sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"myFirstDevice\",\"windSpeed\":%.2f}", avgWindSpeed + (rand() % 4 + 2));
AzureIoTClient 43:a4b2614eb860 201 if ((messages[iterator].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
Azure.IoT Build 34:b7f31511a8ad 202 {
AzureIoTClient 43:a4b2614eb860 203 (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
Azure.IoT Build 34:b7f31511a8ad 204 }
Azure.IoT Build 34:b7f31511a8ad 205 else
Azure.IoT Build 34:b7f31511a8ad 206 {
AzureIoTClient 43:a4b2614eb860 207 messages[iterator].messageTrackingId = iterator;
AzureIoTClient 43:a4b2614eb860 208
AzureIoTClient 43:a4b2614eb860 209 MAP_HANDLE propMap = IoTHubMessage_Properties(messages[iterator].messageHandle);
AzureIoTClient 48:ecd0964514f8 210 (void)sprintf_s(propText, sizeof(propText), "PropMsg_%zu", iterator);
AzureIoTClient 43:a4b2614eb860 211 if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
AzureIoTClient 43:a4b2614eb860 212 {
AzureIoTClient 43:a4b2614eb860 213 (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
AzureIoTClient 43:a4b2614eb860 214 }
AzureIoTClient 43:a4b2614eb860 215
AzureIoTClient 78:7acff4261af4 216 (void)IoTHubMessage_SetContentTypeSystemProperty(messages[iterator].messageHandle, "application/json");
AzureIoTClient 78:7acff4261af4 217 (void)IoTHubMessage_SetContentEncodingSystemProperty(messages[iterator].messageHandle, "utf-8");
AzureIoTClient 78:7acff4261af4 218
AzureIoTClient 43:a4b2614eb860 219 if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator].messageHandle, SendConfirmationCallback, &messages[iterator]) != IOTHUB_CLIENT_OK)
AzureIoTClient 43:a4b2614eb860 220 {
AzureIoTClient 43:a4b2614eb860 221 (void)printf("ERROR: IoTHubClient_SendEventAsync..........FAILED!\r\n");
AzureIoTClient 43:a4b2614eb860 222 }
AzureIoTClient 43:a4b2614eb860 223 else
AzureIoTClient 43:a4b2614eb860 224 {
AzureIoTClient 43:a4b2614eb860 225 (void)printf("IoTHubClient_SendEventAsync accepted data for transmission to IoT Hub.\r\n");
AzureIoTClient 43:a4b2614eb860 226 }
Azure.IoT Build 34:b7f31511a8ad 227 }
AzureIoTClient 17:d01a2794d5d7 228 }
AzureIoTClient 43:a4b2614eb860 229 IoTHubClient_LL_DoWork(iotHubClientHandle);
AzureIoTClient 43:a4b2614eb860 230 ThreadAPI_Sleep(1);
Azure.IoT Build 34:b7f31511a8ad 231
AzureIoTClient 43:a4b2614eb860 232 iterator++;
AzureIoTClient 43:a4b2614eb860 233 } while (g_continueRunning);
Azure.IoT Build 47:d87920c7f211 234
AzureIoTClient 60:ae07786385f9 235 (void)printf("iothub_client_sample_amqp has gotten quit message, call DoWork %d more time to complete final sending...\r\n", DOWORK_LOOP_NUM);
Azure.IoT Build 47:d87920c7f211 236 for (size_t index = 0; index < DOWORK_LOOP_NUM; index++)
Azure.IoT Build 47:d87920c7f211 237 {
Azure.IoT Build 47:d87920c7f211 238 IoTHubClient_LL_DoWork(iotHubClientHandle);
Azure.IoT Build 47:d87920c7f211 239 ThreadAPI_Sleep(1);
Azure.IoT Build 47:d87920c7f211 240 }
AzureIoTClient 17:d01a2794d5d7 241 }
AzureIoTClient 43:a4b2614eb860 242 IoTHubClient_LL_Destroy(iotHubClientHandle);
AzureIoTClient 17:d01a2794d5d7 243 }
Azure.IoT Build 34:b7f31511a8ad 244 platform_deinit();
AzureIoTClient 17:d01a2794d5d7 245 }
AzureIoTClient 17:d01a2794d5d7 246 }
AzureIoTClient 43:a4b2614eb860 247
AzureIoTClient 43:a4b2614eb860 248 int main(void)
AzureIoTClient 43:a4b2614eb860 249 {
AzureIoTClient 43:a4b2614eb860 250 iothub_client_sample_amqp_run();
AzureIoTClient 43:a4b2614eb860 251 return 0;
AzureIoTClient 43:a4b2614eb860 252 }