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