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:
Mon May 09 14:38:01 2016 -0700
Revision:
40:548ef685e177
Parent:
39:87752a84df87
Child:
43:a4b2614eb860
1.0.6

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"
AzureIoTClient 17:d01a2794d5d7 8 #include "iothub_client.h"
AzureIoTClient 17:d01a2794d5d7 9 #include "iothub_message.h"
AzureIoTClient 39:87752a84df87 10 #include "azure_c_shared_utility/threadapi.h"
AzureIoTClient 39:87752a84df87 11 #include "azure_c_shared_utility/crt_abstractions.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 17:d01a2794d5d7 24
AzureIoTClient 17:d01a2794d5d7 25 DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES);
AzureIoTClient 17:d01a2794d5d7 26
AzureIoTClient 17:d01a2794d5d7 27 typedef struct EVENT_INSTANCE_TAG
AzureIoTClient 17:d01a2794d5d7 28 {
AzureIoTClient 17:d01a2794d5d7 29 IOTHUB_MESSAGE_HANDLE messageHandle;
AzureIoTClient 17:d01a2794d5d7 30 int messageTrackingId; // For tracking the messages within the user callback.
AzureIoTClient 17:d01a2794d5d7 31 } EVENT_INSTANCE;
AzureIoTClient 17:d01a2794d5d7 32
AzureIoTClient 17:d01a2794d5d7 33 static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
AzureIoTClient 17:d01a2794d5d7 34 {
AzureIoTClient 17:d01a2794d5d7 35 int* counter = (int*)userContextCallback;
Azure.IoT Build 31:7a8aed41e109 36 const unsigned char* buffer = NULL;
Azure.IoT Build 31:7a8aed41e109 37 size_t size = 0;
AzureIoTClient 40:548ef685e177 38 const char* messageId;
AzureIoTClient 40:548ef685e177 39 const char* correlationId;
AzureIoTClient 40:548ef685e177 40
AzureIoTClient 40:548ef685e177 41 // AMQP message properties
AzureIoTClient 40:548ef685e177 42 if ((messageId = IoTHubMessage_GetMessageId(message)) == NULL)
AzureIoTClient 40:548ef685e177 43 {
AzureIoTClient 40:548ef685e177 44 messageId = "<null>";
AzureIoTClient 40:548ef685e177 45 }
AzureIoTClient 40:548ef685e177 46
AzureIoTClient 40:548ef685e177 47 if ((correlationId = IoTHubMessage_GetCorrelationId(message)) == NULL)
AzureIoTClient 40:548ef685e177 48 {
AzureIoTClient 40:548ef685e177 49 correlationId = "<null>";
AzureIoTClient 40:548ef685e177 50 }
AzureIoTClient 40:548ef685e177 51
AzureIoTClient 40:548ef685e177 52 // AMQP message content.
Azure.IoT Build 31:7a8aed41e109 53 IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message);
AzureIoTClient 19:de28463ff3cc 54
Azure.IoT Build 31:7a8aed41e109 55 if (contentType == IOTHUBMESSAGE_BYTEARRAY)
Azure.IoT Build 31:7a8aed41e109 56 {
Azure.IoT Build 31:7a8aed41e109 57 if (IoTHubMessage_GetByteArray(message, &buffer, &size) == IOTHUB_MESSAGE_OK)
Azure.IoT Build 31:7a8aed41e109 58 {
AzureIoTClient 40:548ef685e177 59 (void)printf("Received Message [%d] (message-id: %s, correlation-id: %s) with BINARY Data: <<<%.*s>>> & Size=%d\r\n", *counter, messageId, correlationId,(int)size, buffer, (int)size);
Azure.IoT Build 31:7a8aed41e109 60 }
Azure.IoT Build 31:7a8aed41e109 61 else
Azure.IoT Build 31:7a8aed41e109 62 {
Azure.IoT Build 31:7a8aed41e109 63 (void)printf("Failed getting the BINARY body of the message received.\r\n");
Azure.IoT Build 31:7a8aed41e109 64 }
Azure.IoT Build 31:7a8aed41e109 65 }
Azure.IoT Build 31:7a8aed41e109 66 else if (contentType == IOTHUBMESSAGE_STRING)
AzureIoTClient 17:d01a2794d5d7 67 {
Azure.IoT Build 31:7a8aed41e109 68 if ((buffer = IoTHubMessage_GetString(message)) != NULL && (size = strlen(buffer)) > 0)
Azure.IoT Build 31:7a8aed41e109 69 {
AzureIoTClient 40:548ef685e177 70 (void)printf("Received Message [%d] (message-id: %s, correlation-id: %s) with STRING Data: <<<%.*s>>> & Size=%d\r\n", *counter, messageId, correlationId, (int)size, buffer, (int)size);
Azure.IoT Build 31:7a8aed41e109 71 }
Azure.IoT Build 31:7a8aed41e109 72 else
Azure.IoT Build 31:7a8aed41e109 73 {
Azure.IoT Build 31:7a8aed41e109 74 (void)printf("Failed getting the STRING body of the message received.\r\n");
Azure.IoT Build 31:7a8aed41e109 75 }
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 body of the message received (type %i).\r\n", contentType);
AzureIoTClient 17:d01a2794d5d7 80 }
AzureIoTClient 17:d01a2794d5d7 81
AzureIoTClient 17:d01a2794d5d7 82 // Retrieve properties from the message
AzureIoTClient 17:d01a2794d5d7 83 MAP_HANDLE mapProperties = IoTHubMessage_Properties(message);
AzureIoTClient 17:d01a2794d5d7 84 if (mapProperties != NULL)
AzureIoTClient 17:d01a2794d5d7 85 {
AzureIoTClient 17:d01a2794d5d7 86 const char*const* keys;
AzureIoTClient 17:d01a2794d5d7 87 const char*const* values;
AzureIoTClient 17:d01a2794d5d7 88 size_t propertyCount = 0;
AzureIoTClient 17:d01a2794d5d7 89 if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK)
AzureIoTClient 17:d01a2794d5d7 90 {
AzureIoTClient 17:d01a2794d5d7 91 if (propertyCount > 0)
AzureIoTClient 17:d01a2794d5d7 92 {
Azure.IoT Build 31:7a8aed41e109 93 size_t index;
Azure.IoT Build 31:7a8aed41e109 94
AzureIoTClient 17:d01a2794d5d7 95 printf("Message Properties:\r\n");
Azure.IoT Build 31:7a8aed41e109 96 for (index = 0; index < propertyCount; index++)
AzureIoTClient 17:d01a2794d5d7 97 {
AzureIoTClient 17:d01a2794d5d7 98 printf("\tKey: %s Value: %s\r\n", keys[index], values[index]);
AzureIoTClient 17:d01a2794d5d7 99 }
AzureIoTClient 17:d01a2794d5d7 100 printf("\r\n");
AzureIoTClient 17:d01a2794d5d7 101 }
AzureIoTClient 17:d01a2794d5d7 102 }
AzureIoTClient 17:d01a2794d5d7 103 }
AzureIoTClient 17:d01a2794d5d7 104
AzureIoTClient 17:d01a2794d5d7 105 /* Some device specific action code goes here... */
AzureIoTClient 17:d01a2794d5d7 106 (*counter)++;
AzureIoTClient 17:d01a2794d5d7 107 return IOTHUBMESSAGE_ACCEPTED;
AzureIoTClient 17:d01a2794d5d7 108 }
AzureIoTClient 17:d01a2794d5d7 109
AzureIoTClient 17:d01a2794d5d7 110 static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
AzureIoTClient 17:d01a2794d5d7 111 {
AzureIoTClient 17:d01a2794d5d7 112 EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
AzureIoTClient 17:d01a2794d5d7 113 (void)printf("Confirmation[%d] received for message tracking id = %d with result = %s\r\n", callbackCounter, eventInstance->messageTrackingId, ENUM_TO_STRING(IOTHUB_CLIENT_CONFIRMATION_RESULT, result));
AzureIoTClient 17:d01a2794d5d7 114 /* Some device specific action code goes here... */
AzureIoTClient 17:d01a2794d5d7 115 callbackCounter++;
AzureIoTClient 17:d01a2794d5d7 116 IoTHubMessage_Destroy(eventInstance->messageHandle);
AzureIoTClient 17:d01a2794d5d7 117 }
AzureIoTClient 17:d01a2794d5d7 118
AzureIoTClient 17:d01a2794d5d7 119 static char msgText[1024];
AzureIoTClient 17:d01a2794d5d7 120 static char propText[1024];
AzureIoTClient 17:d01a2794d5d7 121 #define MESSAGE_COUNT 5
AzureIoTClient 17:d01a2794d5d7 122
AzureIoTClient 17:d01a2794d5d7 123 void iothub_client_sample_amqp_run(void)
Azure.IoT Build 31:7a8aed41e109 124 {
AzureIoTClient 17:d01a2794d5d7 125 IOTHUB_CLIENT_HANDLE iotHubClientHandle;
AzureIoTClient 17:d01a2794d5d7 126
AzureIoTClient 17:d01a2794d5d7 127 EVENT_INSTANCE messages[MESSAGE_COUNT];
AzureIoTClient 17:d01a2794d5d7 128
AzureIoTClient 17:d01a2794d5d7 129 srand((unsigned int)time(NULL));
AzureIoTClient 17:d01a2794d5d7 130 double avgWindSpeed = 10.0;
AzureIoTClient 17:d01a2794d5d7 131
AzureIoTClient 17:d01a2794d5d7 132 callbackCounter = 0;
AzureIoTClient 17:d01a2794d5d7 133 int receiveContext = 0;
AzureIoTClient 17:d01a2794d5d7 134
AzureIoTClient 17:d01a2794d5d7 135 (void)printf("Starting the IoTHub client sample AMQP...\r\n");
Azure.IoT Build 34:b7f31511a8ad 136
Azure.IoT Build 34:b7f31511a8ad 137 if (platform_init() != 0)
AzureIoTClient 17:d01a2794d5d7 138 {
Azure.IoT Build 34:b7f31511a8ad 139 printf("Failed to initialize the platform.\r\n");
AzureIoTClient 17:d01a2794d5d7 140 }
AzureIoTClient 17:d01a2794d5d7 141 else
AzureIoTClient 17:d01a2794d5d7 142 {
Azure.IoT Build 34:b7f31511a8ad 143 if ((iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, AMQP_Protocol)) == NULL)
AzureIoTClient 17:d01a2794d5d7 144 {
Azure.IoT Build 34:b7f31511a8ad 145 (void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
AzureIoTClient 17:d01a2794d5d7 146 }
AzureIoTClient 17:d01a2794d5d7 147 else
AzureIoTClient 17:d01a2794d5d7 148 {
AzureIoTClient 17:d01a2794d5d7 149
Azure.IoT Build 34:b7f31511a8ad 150 #ifdef MBED_BUILD_TIMESTAMP
Azure.IoT Build 34:b7f31511a8ad 151 // For mbed add the certificate information
Azure.IoT Build 34:b7f31511a8ad 152 if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
AzureIoTClient 17:d01a2794d5d7 153 {
Azure.IoT Build 34:b7f31511a8ad 154 printf("failure to set option \"TrustedCerts\"\r\n");
Azure.IoT Build 34:b7f31511a8ad 155 }
Azure.IoT Build 34:b7f31511a8ad 156 #endif // MBED_BUILD_TIMESTAMP
Azure.IoT Build 34:b7f31511a8ad 157
Azure.IoT Build 34:b7f31511a8ad 158 /* Setting Message call back, so we can receive Commands. */
Azure.IoT Build 34:b7f31511a8ad 159 if (IoTHubClient_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK)
Azure.IoT Build 34:b7f31511a8ad 160 {
Azure.IoT Build 34:b7f31511a8ad 161 (void)printf("ERROR: IoTHubClient_SetMessageCallback..........FAILED!\r\n");
Azure.IoT Build 34:b7f31511a8ad 162 }
Azure.IoT Build 34:b7f31511a8ad 163 else
Azure.IoT Build 34:b7f31511a8ad 164 {
Azure.IoT Build 34:b7f31511a8ad 165 (void)printf("IoTHubClient_SetMessageCallback...successful.\r\n");
Azure.IoT Build 34:b7f31511a8ad 166
Azure.IoT Build 34:b7f31511a8ad 167 /* Now that we are ready to receive commands, let's send some messages */
Azure.IoT Build 34:b7f31511a8ad 168 for (size_t i = 0; i < MESSAGE_COUNT; i++)
AzureIoTClient 17:d01a2794d5d7 169 {
Azure.IoT Build 34:b7f31511a8ad 170 sprintf_s(msgText, sizeof(msgText), "{\"deviceId\":\"myFirstDevice\",\"windSpeed\":%.2f}", avgWindSpeed + (rand() % 4 + 2));
Azure.IoT Build 34:b7f31511a8ad 171 if ((messages[i].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
AzureIoTClient 17:d01a2794d5d7 172 {
Azure.IoT Build 34:b7f31511a8ad 173 (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
AzureIoTClient 17:d01a2794d5d7 174 }
AzureIoTClient 17:d01a2794d5d7 175 else
AzureIoTClient 17:d01a2794d5d7 176 {
Azure.IoT Build 34:b7f31511a8ad 177 messages[i].messageTrackingId = i;
Azure.IoT Build 34:b7f31511a8ad 178
Azure.IoT Build 34:b7f31511a8ad 179 MAP_HANDLE propMap = IoTHubMessage_Properties(messages[i].messageHandle);
Azure.IoT Build 34:b7f31511a8ad 180 sprintf_s(propText, sizeof(propText), "PropMsg_%d", i);
Azure.IoT Build 34:b7f31511a8ad 181 if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
Azure.IoT Build 34:b7f31511a8ad 182 {
Azure.IoT Build 34:b7f31511a8ad 183 (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
Azure.IoT Build 34:b7f31511a8ad 184 }
Azure.IoT Build 34:b7f31511a8ad 185
Azure.IoT Build 34:b7f31511a8ad 186 if (IoTHubClient_SendEventAsync(iotHubClientHandle, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK)
Azure.IoT Build 34:b7f31511a8ad 187 {
Azure.IoT Build 34:b7f31511a8ad 188 (void)printf("ERROR: IoTHubClient_SendEventAsync..........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_SendEventAsync accepted data for transmission to IoT Hub.\r\n");
Azure.IoT Build 34:b7f31511a8ad 193 }
AzureIoTClient 17:d01a2794d5d7 194 }
AzureIoTClient 17:d01a2794d5d7 195 }
Azure.IoT Build 34:b7f31511a8ad 196
Azure.IoT Build 34:b7f31511a8ad 197 /* Wait for Commands. */
Azure.IoT Build 34:b7f31511a8ad 198 (void)printf("Press any key to exit the application. \r\n");
Azure.IoT Build 34:b7f31511a8ad 199 (void)getchar();
AzureIoTClient 17:d01a2794d5d7 200 }
AzureIoTClient 17:d01a2794d5d7 201
Azure.IoT Build 34:b7f31511a8ad 202 IoTHubClient_Destroy(iotHubClientHandle);
AzureIoTClient 17:d01a2794d5d7 203 }
Azure.IoT Build 34:b7f31511a8ad 204 platform_deinit();
AzureIoTClient 17:d01a2794d5d7 205 }
AzureIoTClient 17:d01a2794d5d7 206 }