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 Jun 30 10:42:19 2017 -0700
Revision:
74:d20e4ff30068
Parent:
69:06d895ef6bfc
Child:
78:7acff4261af4
1.1.18

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 40:548ef685e177 43
AzureIoTClient 63:14e9684b52e3 44 // Message properties
AzureIoTClient 43:a4b2614eb860 45 if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL)
AzureIoTClient 43:a4b2614eb860 46 {
AzureIoTClient 43:a4b2614eb860 47 messageId = "<null>";
AzureIoTClient 43:a4b2614eb860 48 }
AzureIoTClient 40:548ef685e177 49
AzureIoTClient 43:a4b2614eb860 50 if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL)
AzureIoTClient 43:a4b2614eb860 51 {
AzureIoTClient 43:a4b2614eb860 52 correlationId = "<null>";
AzureIoTClient 43:a4b2614eb860 53 }
AzureIoTClient 40:548ef685e177 54
AzureIoTClient 63:14e9684b52e3 55 // Message content
Azure.IoT Build 31:7a8aed41e109 56 IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message);
AzureIoTClient 19:de28463ff3cc 57
Azure.IoT Build 31:7a8aed41e109 58 if (contentType == IOTHUBMESSAGE_BYTEARRAY)
Azure.IoT Build 31:7a8aed41e109 59 {
Azure.IoT Build 31:7a8aed41e109 60 if (IoTHubMessage_GetByteArray(message, &buffer, &size) == IOTHUB_MESSAGE_OK)
Azure.IoT Build 31:7a8aed41e109 61 {
AzureIoTClient 63:14e9684b52e3 62 (void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n BINARY Data: <<<%.*s>>> & Size=%d\r\n", *counter, messageId, correlationId, (int)size, buffer, (int)size);
Azure.IoT Build 31:7a8aed41e109 63 }
Azure.IoT Build 31:7a8aed41e109 64 else
Azure.IoT Build 31:7a8aed41e109 65 {
Azure.IoT Build 31:7a8aed41e109 66 (void)printf("Failed getting the BINARY body of the message received.\r\n");
Azure.IoT Build 31:7a8aed41e109 67 }
Azure.IoT Build 31:7a8aed41e109 68 }
Azure.IoT Build 31:7a8aed41e109 69 else if (contentType == IOTHUBMESSAGE_STRING)
AzureIoTClient 17:d01a2794d5d7 70 {
AzureIoTClient 48:ecd0964514f8 71 if ((buffer = (const unsigned char*)IoTHubMessage_GetString(message)) != NULL && (size = strlen((const char*)buffer)) > 0)
Azure.IoT Build 31:7a8aed41e109 72 {
AzureIoTClient 63:14e9684b52e3 73 (void)printf("Received Message [%d]\r\n Message ID: %s\r\n Correlation ID: %s\r\n STRING Data: <<<%.*s>>> & Size=%d\r\n", *counter, messageId, correlationId, (int)size, buffer, (int)size);
AzureIoTClient 43:a4b2614eb860 74
AzureIoTClient 43:a4b2614eb860 75 // If we receive the work 'quit' then we stop running
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 STRING 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
Azure.IoT Build 31:7a8aed41e109 83 {
Azure.IoT Build 31:7a8aed41e109 84 (void)printf("Failed getting the body of the message received (type %i).\r\n", contentType);
AzureIoTClient 17:d01a2794d5d7 85 }
AzureIoTClient 17:d01a2794d5d7 86
AzureIoTClient 17:d01a2794d5d7 87 // Retrieve properties from the message
AzureIoTClient 17:d01a2794d5d7 88 MAP_HANDLE mapProperties = IoTHubMessage_Properties(message);
AzureIoTClient 17:d01a2794d5d7 89 if (mapProperties != NULL)
AzureIoTClient 17:d01a2794d5d7 90 {
AzureIoTClient 17:d01a2794d5d7 91 const char*const* keys;
AzureIoTClient 17:d01a2794d5d7 92 const char*const* values;
AzureIoTClient 17:d01a2794d5d7 93 size_t propertyCount = 0;
AzureIoTClient 17:d01a2794d5d7 94 if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK)
AzureIoTClient 17:d01a2794d5d7 95 {
AzureIoTClient 17:d01a2794d5d7 96 if (propertyCount > 0)
AzureIoTClient 17:d01a2794d5d7 97 {
Azure.IoT Build 31:7a8aed41e109 98 size_t index;
Azure.IoT Build 31:7a8aed41e109 99
AzureIoTClient 63:14e9684b52e3 100 printf(" Message Properties:\r\n");
Azure.IoT Build 31:7a8aed41e109 101 for (index = 0; index < propertyCount; index++)
AzureIoTClient 17:d01a2794d5d7 102 {
AzureIoTClient 17:d01a2794d5d7 103 printf("\tKey: %s Value: %s\r\n", keys[index], values[index]);
AzureIoTClient 17:d01a2794d5d7 104 }
AzureIoTClient 17:d01a2794d5d7 105 printf("\r\n");
AzureIoTClient 17:d01a2794d5d7 106 }
AzureIoTClient 17:d01a2794d5d7 107 }
AzureIoTClient 17:d01a2794d5d7 108 }
AzureIoTClient 17:d01a2794d5d7 109
AzureIoTClient 69:06d895ef6bfc 110 if (size == (strlen("quit") * sizeof(char)) && memcmp(buffer, "quit", size) == 0)
AzureIoTClient 43:a4b2614eb860 111 {
AzureIoTClient 43:a4b2614eb860 112 g_continueRunning = false;
AzureIoTClient 43:a4b2614eb860 113 }
AzureIoTClient 43:a4b2614eb860 114
AzureIoTClient 17:d01a2794d5d7 115 /* Some device specific action code goes here... */
AzureIoTClient 17:d01a2794d5d7 116 (*counter)++;
AzureIoTClient 17:d01a2794d5d7 117 return IOTHUBMESSAGE_ACCEPTED;
AzureIoTClient 17:d01a2794d5d7 118 }
AzureIoTClient 17:d01a2794d5d7 119
AzureIoTClient 17:d01a2794d5d7 120 static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
AzureIoTClient 17:d01a2794d5d7 121 {
AzureIoTClient 17:d01a2794d5d7 122 EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
AzureIoTClient 48:ecd0964514f8 123 (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 124 /* Some device specific action code goes here... */
AzureIoTClient 17:d01a2794d5d7 125 callbackCounter++;
AzureIoTClient 17:d01a2794d5d7 126 IoTHubMessage_Destroy(eventInstance->messageHandle);
AzureIoTClient 17:d01a2794d5d7 127 }
AzureIoTClient 17:d01a2794d5d7 128
AzureIoTClient 17:d01a2794d5d7 129 void iothub_client_sample_amqp_run(void)
Azure.IoT Build 31:7a8aed41e109 130 {
AzureIoTClient 43:a4b2614eb860 131 IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle;
AzureIoTClient 17:d01a2794d5d7 132
AzureIoTClient 17:d01a2794d5d7 133 EVENT_INSTANCE messages[MESSAGE_COUNT];
AzureIoTClient 17:d01a2794d5d7 134
AzureIoTClient 43:a4b2614eb860 135 g_continueRunning = true;
AzureIoTClient 17:d01a2794d5d7 136 srand((unsigned int)time(NULL));
AzureIoTClient 17:d01a2794d5d7 137 double avgWindSpeed = 10.0;
AzureIoTClient 17:d01a2794d5d7 138
AzureIoTClient 17:d01a2794d5d7 139 callbackCounter = 0;
AzureIoTClient 17:d01a2794d5d7 140 int receiveContext = 0;
AzureIoTClient 17:d01a2794d5d7 141
AzureIoTClient 17:d01a2794d5d7 142 (void)printf("Starting the IoTHub client sample AMQP...\r\n");
Azure.IoT Build 34:b7f31511a8ad 143
Azure.IoT Build 34:b7f31511a8ad 144 if (platform_init() != 0)
AzureIoTClient 17:d01a2794d5d7 145 {
Azure.IoT Build 34:b7f31511a8ad 146 printf("Failed to initialize the platform.\r\n");
AzureIoTClient 17:d01a2794d5d7 147 }
AzureIoTClient 17:d01a2794d5d7 148 else
AzureIoTClient 17:d01a2794d5d7 149 {
AzureIoTClient 43:a4b2614eb860 150 if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, AMQP_Protocol)) == NULL)
AzureIoTClient 17:d01a2794d5d7 151 {
Azure.IoT Build 34:b7f31511a8ad 152 (void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
AzureIoTClient 17:d01a2794d5d7 153 }
AzureIoTClient 17:d01a2794d5d7 154 else
AzureIoTClient 17:d01a2794d5d7 155 {
AzureIoTClient 43:a4b2614eb860 156 bool traceOn = true;
AzureIoTClient 43:a4b2614eb860 157 IoTHubClient_LL_SetOption(iotHubClientHandle, "logtrace", &traceOn);
AzureIoTClient 17:d01a2794d5d7 158
AzureIoTClient 74:d20e4ff30068 159 // 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 160 uint32_t c2d_keep_alive_freq_secs = 120;
AzureIoTClient 74:d20e4ff30068 161 IoTHubClient_LL_SetOption(iotHubClientHandle, "c2d_keep_alive_freq_secs", &c2d_keep_alive_freq_secs);
AzureIoTClient 74:d20e4ff30068 162
Azure.IoT Build 34:b7f31511a8ad 163 #ifdef MBED_BUILD_TIMESTAMP
Azure.IoT Build 34:b7f31511a8ad 164 // For mbed add the certificate information
AzureIoTClient 43:a4b2614eb860 165 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
AzureIoTClient 17:d01a2794d5d7 166 {
Azure.IoT Build 34:b7f31511a8ad 167 printf("failure to set option \"TrustedCerts\"\r\n");
Azure.IoT Build 34:b7f31511a8ad 168 }
Azure.IoT Build 34:b7f31511a8ad 169 #endif // MBED_BUILD_TIMESTAMP
Azure.IoT Build 34:b7f31511a8ad 170
Azure.IoT Build 34:b7f31511a8ad 171 /* Setting Message call back, so we can receive Commands. */
AzureIoTClient 43:a4b2614eb860 172 if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK)
Azure.IoT Build 34:b7f31511a8ad 173 {
Azure.IoT Build 34:b7f31511a8ad 174 (void)printf("ERROR: IoTHubClient_SetMessageCallback..........FAILED!\r\n");
Azure.IoT Build 34:b7f31511a8ad 175 }
Azure.IoT Build 34:b7f31511a8ad 176 else
Azure.IoT Build 34:b7f31511a8ad 177 {
Azure.IoT Build 34:b7f31511a8ad 178 (void)printf("IoTHubClient_SetMessageCallback...successful.\r\n");
Azure.IoT Build 34:b7f31511a8ad 179
Azure.IoT Build 34:b7f31511a8ad 180 /* Now that we are ready to receive commands, let's send some messages */
AzureIoTClient 43:a4b2614eb860 181 size_t iterator = 0;
AzureIoTClient 43:a4b2614eb860 182 do
AzureIoTClient 17:d01a2794d5d7 183 {
AzureIoTClient 43:a4b2614eb860 184 if (iterator < MESSAGE_COUNT)
AzureIoTClient 17:d01a2794d5d7 185 {
AzureIoTClient 43:a4b2614eb860 186 sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"myFirstDevice\",\"windSpeed\":%.2f}", avgWindSpeed + (rand() % 4 + 2));
AzureIoTClient 43:a4b2614eb860 187 if ((messages[iterator].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
Azure.IoT Build 34:b7f31511a8ad 188 {
AzureIoTClient 43:a4b2614eb860 189 (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
Azure.IoT Build 34:b7f31511a8ad 190 }
Azure.IoT Build 34:b7f31511a8ad 191 else
Azure.IoT Build 34:b7f31511a8ad 192 {
AzureIoTClient 43:a4b2614eb860 193 messages[iterator].messageTrackingId = iterator;
AzureIoTClient 43:a4b2614eb860 194
AzureIoTClient 43:a4b2614eb860 195 MAP_HANDLE propMap = IoTHubMessage_Properties(messages[iterator].messageHandle);
AzureIoTClient 48:ecd0964514f8 196 (void)sprintf_s(propText, sizeof(propText), "PropMsg_%zu", iterator);
AzureIoTClient 43:a4b2614eb860 197 if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
AzureIoTClient 43:a4b2614eb860 198 {
AzureIoTClient 43:a4b2614eb860 199 (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
AzureIoTClient 43:a4b2614eb860 200 }
AzureIoTClient 43:a4b2614eb860 201
AzureIoTClient 43:a4b2614eb860 202 if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[iterator].messageHandle, SendConfirmationCallback, &messages[iterator]) != IOTHUB_CLIENT_OK)
AzureIoTClient 43:a4b2614eb860 203 {
AzureIoTClient 43:a4b2614eb860 204 (void)printf("ERROR: IoTHubClient_SendEventAsync..........FAILED!\r\n");
AzureIoTClient 43:a4b2614eb860 205 }
AzureIoTClient 43:a4b2614eb860 206 else
AzureIoTClient 43:a4b2614eb860 207 {
AzureIoTClient 43:a4b2614eb860 208 (void)printf("IoTHubClient_SendEventAsync accepted data for transmission to IoT Hub.\r\n");
AzureIoTClient 43:a4b2614eb860 209 }
Azure.IoT Build 34:b7f31511a8ad 210 }
AzureIoTClient 17:d01a2794d5d7 211 }
AzureIoTClient 43:a4b2614eb860 212 IoTHubClient_LL_DoWork(iotHubClientHandle);
AzureIoTClient 43:a4b2614eb860 213 ThreadAPI_Sleep(1);
Azure.IoT Build 34:b7f31511a8ad 214
AzureIoTClient 43:a4b2614eb860 215 iterator++;
AzureIoTClient 43:a4b2614eb860 216 } while (g_continueRunning);
Azure.IoT Build 47:d87920c7f211 217
AzureIoTClient 60:ae07786385f9 218 (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 219 for (size_t index = 0; index < DOWORK_LOOP_NUM; index++)
Azure.IoT Build 47:d87920c7f211 220 {
Azure.IoT Build 47:d87920c7f211 221 IoTHubClient_LL_DoWork(iotHubClientHandle);
Azure.IoT Build 47:d87920c7f211 222 ThreadAPI_Sleep(1);
Azure.IoT Build 47:d87920c7f211 223 }
AzureIoTClient 17:d01a2794d5d7 224 }
AzureIoTClient 43:a4b2614eb860 225 IoTHubClient_LL_Destroy(iotHubClientHandle);
AzureIoTClient 17:d01a2794d5d7 226 }
Azure.IoT Build 34:b7f31511a8ad 227 platform_deinit();
AzureIoTClient 17:d01a2794d5d7 228 }
AzureIoTClient 17:d01a2794d5d7 229 }
AzureIoTClient 43:a4b2614eb860 230
AzureIoTClient 43:a4b2614eb860 231 int main(void)
AzureIoTClient 43:a4b2614eb860 232 {
AzureIoTClient 43:a4b2614eb860 233 iothub_client_sample_amqp_run();
AzureIoTClient 43:a4b2614eb860 234 return 0;
AzureIoTClient 43:a4b2614eb860 235 }