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:
Wed Sep 16 23:54:55 2015 -0700
Revision:
4:24cbfabe1b9d
Parent:
0:eb1c7bc1f4f1
Child:
8:5db3366b6f57
New release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 0:eb1c7bc1f4f1 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 0:eb1c7bc1f4f1 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 0:eb1c7bc1f4f1 3
AzureIoTClient 0:eb1c7bc1f4f1 4 #include <stdio.h>
AzureIoTClient 0:eb1c7bc1f4f1 5
AzureIoTClient 0:eb1c7bc1f4f1 6 #include "iothub_client.h"
AzureIoTClient 0:eb1c7bc1f4f1 7 #include "iothub_message.h"
AzureIoTClient 0:eb1c7bc1f4f1 8 #include "threadapi.h"
AzureIoTClient 0:eb1c7bc1f4f1 9 #include "crt_abstractions.h"
AzureIoTClient 0:eb1c7bc1f4f1 10 #include "iothubtransportamqp.h"
AzureIoTClient 0:eb1c7bc1f4f1 11
AzureIoTClient 4:24cbfabe1b9d 12 #ifdef MBED_BUILD_TIMESTAMP
AzureIoTClient 4:24cbfabe1b9d 13 #include "certs.h"
AzureIoTClient 4:24cbfabe1b9d 14 #endif // MBED_BUILD_TIMESTAMP
AzureIoTClient 4:24cbfabe1b9d 15
AzureIoTClient 0:eb1c7bc1f4f1 16
AzureIoTClient 0:eb1c7bc1f4f1 17 static const char* connectionString = "[device connection string]";
AzureIoTClient 0:eb1c7bc1f4f1 18 static int callbackCounter;
AzureIoTClient 0:eb1c7bc1f4f1 19
AzureIoTClient 0:eb1c7bc1f4f1 20 DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES);
AzureIoTClient 0:eb1c7bc1f4f1 21
AzureIoTClient 0:eb1c7bc1f4f1 22 typedef struct EVENT_INSTANCE_TAG
AzureIoTClient 0:eb1c7bc1f4f1 23 {
AzureIoTClient 0:eb1c7bc1f4f1 24 IOTHUB_MESSAGE_HANDLE messageHandle;
AzureIoTClient 0:eb1c7bc1f4f1 25 int messageTrackingId; // For tracking the messages within the user callback.
AzureIoTClient 0:eb1c7bc1f4f1 26 } EVENT_INSTANCE;
AzureIoTClient 0:eb1c7bc1f4f1 27
AzureIoTClient 0:eb1c7bc1f4f1 28 static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveNotificationCallback(IOTHUB_MESSAGE_HANDLE notificationMessage, void* userContextCallback)
AzureIoTClient 0:eb1c7bc1f4f1 29 {
AzureIoTClient 0:eb1c7bc1f4f1 30 int* counter = (int*)userContextCallback;
AzureIoTClient 0:eb1c7bc1f4f1 31 const char* buffer;
AzureIoTClient 0:eb1c7bc1f4f1 32 size_t size;
AzureIoTClient 0:eb1c7bc1f4f1 33 if (IoTHubMessage_GetByteArray(notificationMessage, (const unsigned char**)&buffer, &size) == IOTHUB_MESSAGE_OK)
AzureIoTClient 0:eb1c7bc1f4f1 34 {
AzureIoTClient 0:eb1c7bc1f4f1 35 (void)printf("Received Notification [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size);
AzureIoTClient 0:eb1c7bc1f4f1 36 }
AzureIoTClient 0:eb1c7bc1f4f1 37
AzureIoTClient 0:eb1c7bc1f4f1 38 // Retrieve properties from the message
AzureIoTClient 0:eb1c7bc1f4f1 39 MAP_HANDLE mapProperties = IoTHubMessage_Properties(notificationMessage);
AzureIoTClient 0:eb1c7bc1f4f1 40 if (mapProperties != NULL)
AzureIoTClient 0:eb1c7bc1f4f1 41 {
AzureIoTClient 0:eb1c7bc1f4f1 42 const char*const* keys;
AzureIoTClient 0:eb1c7bc1f4f1 43 const char*const* values;
AzureIoTClient 0:eb1c7bc1f4f1 44 size_t propertyCount = 0;
AzureIoTClient 0:eb1c7bc1f4f1 45 if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK)
AzureIoTClient 0:eb1c7bc1f4f1 46 {
AzureIoTClient 0:eb1c7bc1f4f1 47 if (propertyCount > 0)
AzureIoTClient 0:eb1c7bc1f4f1 48 {
AzureIoTClient 0:eb1c7bc1f4f1 49 printf("Message Properties:\r\n");
AzureIoTClient 0:eb1c7bc1f4f1 50 for (size_t index = 0; index < propertyCount; index++)
AzureIoTClient 0:eb1c7bc1f4f1 51 {
AzureIoTClient 0:eb1c7bc1f4f1 52 printf("\tKey: %s Value: %s\r\n", keys[index], values[index]);
AzureIoTClient 0:eb1c7bc1f4f1 53 }
AzureIoTClient 0:eb1c7bc1f4f1 54 printf("\r\n");
AzureIoTClient 0:eb1c7bc1f4f1 55 }
AzureIoTClient 0:eb1c7bc1f4f1 56 }
AzureIoTClient 0:eb1c7bc1f4f1 57 }
AzureIoTClient 0:eb1c7bc1f4f1 58
AzureIoTClient 0:eb1c7bc1f4f1 59 /* Some device specific action code goes here... */
AzureIoTClient 0:eb1c7bc1f4f1 60 (*counter)++;
AzureIoTClient 0:eb1c7bc1f4f1 61 return IOTHUBMESSAGE_ACCEPTED;
AzureIoTClient 0:eb1c7bc1f4f1 62 }
AzureIoTClient 0:eb1c7bc1f4f1 63
AzureIoTClient 0:eb1c7bc1f4f1 64 static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
AzureIoTClient 0:eb1c7bc1f4f1 65 {
AzureIoTClient 0:eb1c7bc1f4f1 66 EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
AzureIoTClient 0:eb1c7bc1f4f1 67 (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 0:eb1c7bc1f4f1 68 /* Some device specific action code goes here... */
AzureIoTClient 0:eb1c7bc1f4f1 69 callbackCounter++;
AzureIoTClient 0:eb1c7bc1f4f1 70 IoTHubMessage_Destroy(eventInstance->messageHandle);
AzureIoTClient 0:eb1c7bc1f4f1 71 }
AzureIoTClient 0:eb1c7bc1f4f1 72
AzureIoTClient 0:eb1c7bc1f4f1 73 static char msgText[1024];
AzureIoTClient 0:eb1c7bc1f4f1 74 static char propText[1024];
AzureIoTClient 0:eb1c7bc1f4f1 75 #define MESSAGE_COUNT 5
AzureIoTClient 0:eb1c7bc1f4f1 76
AzureIoTClient 0:eb1c7bc1f4f1 77 void iothub_client_sample_amqp_run(void)
AzureIoTClient 0:eb1c7bc1f4f1 78 {
AzureIoTClient 0:eb1c7bc1f4f1 79 IOTHUB_CLIENT_HANDLE iotHubClientHandle;
AzureIoTClient 0:eb1c7bc1f4f1 80
AzureIoTClient 0:eb1c7bc1f4f1 81 EVENT_INSTANCE messages[MESSAGE_COUNT];
AzureIoTClient 0:eb1c7bc1f4f1 82
AzureIoTClient 0:eb1c7bc1f4f1 83 callbackCounter = 0;
AzureIoTClient 0:eb1c7bc1f4f1 84 int receiveContext = 0;
AzureIoTClient 0:eb1c7bc1f4f1 85
AzureIoTClient 0:eb1c7bc1f4f1 86 (void)printf("Starting the IoTHub client sample AMQP...\r\n");
AzureIoTClient 0:eb1c7bc1f4f1 87
AzureIoTClient 0:eb1c7bc1f4f1 88 if ((iotHubClientHandle = IoTHubClient_CreateFromConnectionString(connectionString, IoTHubTransportAmqp_ProvideTransportInterface)) == NULL)
AzureIoTClient 0:eb1c7bc1f4f1 89 {
AzureIoTClient 0:eb1c7bc1f4f1 90 (void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
AzureIoTClient 0:eb1c7bc1f4f1 91 }
AzureIoTClient 0:eb1c7bc1f4f1 92 else
AzureIoTClient 0:eb1c7bc1f4f1 93 {
AzureIoTClient 4:24cbfabe1b9d 94 #ifdef MBED_BUILD_TIMESTAMP
AzureIoTClient 4:24cbfabe1b9d 95 // For mbed add the certificate information
AzureIoTClient 4:24cbfabe1b9d 96 if (IoTHubClient_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
AzureIoTClient 4:24cbfabe1b9d 97 {
AzureIoTClient 4:24cbfabe1b9d 98 printf("failure to set option \"TrustedCerts\"\r\n");
AzureIoTClient 4:24cbfabe1b9d 99 }
AzureIoTClient 4:24cbfabe1b9d 100 #endif // MBED_BUILD_TIMESTAMP
AzureIoTClient 0:eb1c7bc1f4f1 101
AzureIoTClient 0:eb1c7bc1f4f1 102 /* Setting Notification call back, so we can receive Commands. */
AzureIoTClient 0:eb1c7bc1f4f1 103 if (IoTHubClient_SetNotificationCallback(iotHubClientHandle, ReceiveNotificationCallback, &receiveContext) != IOTHUB_CLIENT_OK)
AzureIoTClient 0:eb1c7bc1f4f1 104 {
AzureIoTClient 0:eb1c7bc1f4f1 105 (void)printf("ERROR: IoTHubClient_SetNotificationCallback..........FAILED!\r\n");
AzureIoTClient 0:eb1c7bc1f4f1 106 }
AzureIoTClient 0:eb1c7bc1f4f1 107 else
AzureIoTClient 0:eb1c7bc1f4f1 108 {
AzureIoTClient 0:eb1c7bc1f4f1 109 (void)printf("IoTHubClient_SetNotificationCallback...successful.\r\n");
AzureIoTClient 0:eb1c7bc1f4f1 110
AzureIoTClient 0:eb1c7bc1f4f1 111 /* Now that we are ready to receive commands, let's send some messages */
AzureIoTClient 0:eb1c7bc1f4f1 112 for (int i = 0; i < MESSAGE_COUNT; i++)
AzureIoTClient 0:eb1c7bc1f4f1 113 {
AzureIoTClient 0:eb1c7bc1f4f1 114 sprintf_s(msgText, sizeof(msgText), "Message_%d_From_IoTHubClient_Over_AMQP", i);
AzureIoTClient 0:eb1c7bc1f4f1 115 if ((messages[i].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
AzureIoTClient 0:eb1c7bc1f4f1 116 {
AzureIoTClient 0:eb1c7bc1f4f1 117 (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
AzureIoTClient 0:eb1c7bc1f4f1 118 }
AzureIoTClient 0:eb1c7bc1f4f1 119 else
AzureIoTClient 0:eb1c7bc1f4f1 120 {
AzureIoTClient 0:eb1c7bc1f4f1 121 messages[i].messageTrackingId = i;
AzureIoTClient 0:eb1c7bc1f4f1 122
AzureIoTClient 0:eb1c7bc1f4f1 123 MAP_HANDLE propMap = IoTHubMessage_Properties(messages[i].messageHandle);
AzureIoTClient 0:eb1c7bc1f4f1 124 sprintf_s(propText, sizeof(propText), "PropMsg_%d", i);
AzureIoTClient 0:eb1c7bc1f4f1 125 if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
AzureIoTClient 0:eb1c7bc1f4f1 126 {
AzureIoTClient 0:eb1c7bc1f4f1 127 (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
AzureIoTClient 0:eb1c7bc1f4f1 128 }
AzureIoTClient 0:eb1c7bc1f4f1 129
AzureIoTClient 0:eb1c7bc1f4f1 130 if (IoTHubClient_SendEventAsync(iotHubClientHandle, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK)
AzureIoTClient 0:eb1c7bc1f4f1 131 {
AzureIoTClient 0:eb1c7bc1f4f1 132 (void)printf("ERROR: IoTHubClient_SendEventAsync..........FAILED!\r\n");
AzureIoTClient 0:eb1c7bc1f4f1 133 }
AzureIoTClient 0:eb1c7bc1f4f1 134 else
AzureIoTClient 0:eb1c7bc1f4f1 135 {
AzureIoTClient 0:eb1c7bc1f4f1 136 (void)printf("IoTHubClient_SendEventAsync accepted data for transmission to IoT Hub.\r\n");
AzureIoTClient 0:eb1c7bc1f4f1 137 }
AzureIoTClient 0:eb1c7bc1f4f1 138
AzureIoTClient 0:eb1c7bc1f4f1 139 }
AzureIoTClient 0:eb1c7bc1f4f1 140 }
AzureIoTClient 0:eb1c7bc1f4f1 141
AzureIoTClient 0:eb1c7bc1f4f1 142 /* Wait for Commands. */
AzureIoTClient 0:eb1c7bc1f4f1 143 (void)printf("Press any key to exit the application. \r\n");
AzureIoTClient 0:eb1c7bc1f4f1 144 (void)getchar();
AzureIoTClient 0:eb1c7bc1f4f1 145 }
AzureIoTClient 0:eb1c7bc1f4f1 146
AzureIoTClient 0:eb1c7bc1f4f1 147 IoTHubClient_Destroy(iotHubClientHandle);
AzureIoTClient 0:eb1c7bc1f4f1 148 }
AzureIoTClient 0:eb1c7bc1f4f1 149 }