Azure IoT / Mbed 2 deprecated iothub_client_sample_http

Dependencies:   iothub_client EthernetInterface iothub_http_transport mbed-rtos mbed wolfSSL azure_c_shared_utility NTPClient

Committer:
AzureIoTClient
Date:
Mon May 09 14:38:17 2016 -0700
Revision:
39:405f9c637ba4
Parent:
38:eb426458564b
Child:
41:7a930aa6dc01
1.0.6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 18:e3e29fc771c9 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 18:e3e29fc771c9 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 18:e3e29fc771c9 3
AzureIoTClient 18:e3e29fc771c9 4 #include <stdio.h>
AzureIoTClient 18:e3e29fc771c9 5 #include <stdlib.h>
AzureIoTClient 18:e3e29fc771c9 6
AzureIoTClient 18:e3e29fc771c9 7 /* This sample uses the _LL APIs of iothub_client for example purposes.
AzureIoTClient 18:e3e29fc771c9 8 That does not mean that HTTP only works with the _LL APIs.
AzureIoTClient 18:e3e29fc771c9 9 Simply changing the using the convenience layer (functions not having _LL)
AzureIoTClient 18:e3e29fc771c9 10 and removing calls to _DoWork will yield the same results. */
AzureIoTClient 18:e3e29fc771c9 11
AzureIoTClient 20:52f8298d7256 12 #ifdef ARDUINO
AzureIoTClient 20:52f8298d7256 13 #include "AzureIoT.h"
AzureIoTClient 20:52f8298d7256 14 #else
AzureIoTClient 18:e3e29fc771c9 15 #include "iothub_client_ll.h"
AzureIoTClient 18:e3e29fc771c9 16 #include "iothub_message.h"
AzureIoTClient 38:eb426458564b 17 #include "azure_c_shared_utility/crt_abstractions.h"
AzureIoTClient 18:e3e29fc771c9 18 #include "iothubtransporthttp.h"
AzureIoTClient 38:eb426458564b 19 #include "azure_c_shared_utility/platform.h"
AzureIoTClient 20:52f8298d7256 20 #endif
AzureIoTClient 18:e3e29fc771c9 21
AzureIoTClient 18:e3e29fc771c9 22 #ifdef MBED_BUILD_TIMESTAMP
AzureIoTClient 18:e3e29fc771c9 23 #include "certs.h"
AzureIoTClient 18:e3e29fc771c9 24 #endif // MBED_BUILD_TIMESTAMP
AzureIoTClient 18:e3e29fc771c9 25
AzureIoTClient 39:405f9c637ba4 26 /*String containing Hostname, Device Id & Device Key in the format: */
AzureIoTClient 39:405f9c637ba4 27 /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>" */
AzureIoTClient 39:405f9c637ba4 28 /* "HostName=<host_name>;DeviceId=<device_id>;SharedAccessSignature=<device_sas_token>" */
AzureIoTClient 18:e3e29fc771c9 29 static const char* connectionString = "[device connection string]";
AzureIoTClient 39:405f9c637ba4 30
AzureIoTClient 18:e3e29fc771c9 31 static int callbackCounter;
AzureIoTClient 18:e3e29fc771c9 32
AzureIoTClient 18:e3e29fc771c9 33 DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES);
AzureIoTClient 18:e3e29fc771c9 34
AzureIoTClient 18:e3e29fc771c9 35 typedef struct EVENT_INSTANCE_TAG
AzureIoTClient 18:e3e29fc771c9 36 {
AzureIoTClient 18:e3e29fc771c9 37 IOTHUB_MESSAGE_HANDLE messageHandle;
AzureIoTClient 18:e3e29fc771c9 38 int messageTrackingId; // For tracking the messages within the user callback.
AzureIoTClient 18:e3e29fc771c9 39 } EVENT_INSTANCE;
AzureIoTClient 18:e3e29fc771c9 40
AzureIoTClient 18:e3e29fc771c9 41 static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
AzureIoTClient 18:e3e29fc771c9 42 {
AzureIoTClient 18:e3e29fc771c9 43 int* counter = (int*)userContextCallback;
AzureIoTClient 18:e3e29fc771c9 44 const char* buffer;
AzureIoTClient 18:e3e29fc771c9 45 size_t size;
AzureIoTClient 20:52f8298d7256 46 MAP_HANDLE mapProperties;
AzureIoTClient 20:52f8298d7256 47
AzureIoTClient 18:e3e29fc771c9 48 if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK)
AzureIoTClient 18:e3e29fc771c9 49 {
AzureIoTClient 18:e3e29fc771c9 50 printf("unable to retrieve the message data\r\n");
AzureIoTClient 18:e3e29fc771c9 51 }
AzureIoTClient 18:e3e29fc771c9 52 else
AzureIoTClient 18:e3e29fc771c9 53 {
AzureIoTClient 18:e3e29fc771c9 54 (void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size);
AzureIoTClient 18:e3e29fc771c9 55 }
AzureIoTClient 18:e3e29fc771c9 56
AzureIoTClient 18:e3e29fc771c9 57 // Retrieve properties from the message
AzureIoTClient 20:52f8298d7256 58 mapProperties = IoTHubMessage_Properties(message);
AzureIoTClient 18:e3e29fc771c9 59 if (mapProperties != NULL)
AzureIoTClient 18:e3e29fc771c9 60 {
AzureIoTClient 18:e3e29fc771c9 61 const char*const* keys;
AzureIoTClient 18:e3e29fc771c9 62 const char*const* values;
AzureIoTClient 18:e3e29fc771c9 63 size_t propertyCount = 0;
AzureIoTClient 18:e3e29fc771c9 64 if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK)
AzureIoTClient 18:e3e29fc771c9 65 {
AzureIoTClient 18:e3e29fc771c9 66 if (propertyCount > 0)
AzureIoTClient 18:e3e29fc771c9 67 {
AzureIoTClient 20:52f8298d7256 68 size_t index;
AzureIoTClient 20:52f8298d7256 69
AzureIoTClient 18:e3e29fc771c9 70 printf("Message Properties:\r\n");
AzureIoTClient 20:52f8298d7256 71 for (index = 0; index < propertyCount; index++)
AzureIoTClient 18:e3e29fc771c9 72 {
AzureIoTClient 18:e3e29fc771c9 73 printf("\tKey: %s Value: %s\r\n", keys[index], values[index]);
AzureIoTClient 18:e3e29fc771c9 74 }
AzureIoTClient 18:e3e29fc771c9 75 printf("\r\n");
AzureIoTClient 18:e3e29fc771c9 76 }
AzureIoTClient 18:e3e29fc771c9 77 }
AzureIoTClient 18:e3e29fc771c9 78 }
AzureIoTClient 18:e3e29fc771c9 79
AzureIoTClient 18:e3e29fc771c9 80 /* Some device specific action code goes here... */
AzureIoTClient 18:e3e29fc771c9 81 (*counter)++;
AzureIoTClient 18:e3e29fc771c9 82 return IOTHUBMESSAGE_ACCEPTED;
AzureIoTClient 18:e3e29fc771c9 83 }
AzureIoTClient 18:e3e29fc771c9 84
AzureIoTClient 18:e3e29fc771c9 85 static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
AzureIoTClient 18:e3e29fc771c9 86 {
AzureIoTClient 18:e3e29fc771c9 87 EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
AzureIoTClient 18:e3e29fc771c9 88 (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 18:e3e29fc771c9 89 /* Some device specific action code goes here... */
AzureIoTClient 18:e3e29fc771c9 90 callbackCounter++;
AzureIoTClient 18:e3e29fc771c9 91 IoTHubMessage_Destroy(eventInstance->messageHandle);
AzureIoTClient 18:e3e29fc771c9 92 }
AzureIoTClient 18:e3e29fc771c9 93
AzureIoTClient 18:e3e29fc771c9 94 static char msgText[1024];
AzureIoTClient 18:e3e29fc771c9 95 static char propText[1024];
AzureIoTClient 18:e3e29fc771c9 96 #define MESSAGE_COUNT 5
AzureIoTClient 18:e3e29fc771c9 97
AzureIoTClient 18:e3e29fc771c9 98 void iothub_client_sample_http_run(void)
AzureIoTClient 18:e3e29fc771c9 99 {
AzureIoTClient 18:e3e29fc771c9 100 IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle;
AzureIoTClient 18:e3e29fc771c9 101
AzureIoTClient 18:e3e29fc771c9 102 EVENT_INSTANCE messages[MESSAGE_COUNT];
AzureIoTClient 20:52f8298d7256 103 double avgWindSpeed = 10.0;
AzureIoTClient 20:52f8298d7256 104 int receiveContext = 0;
AzureIoTClient 18:e3e29fc771c9 105
AzureIoTClient 18:e3e29fc771c9 106 srand((unsigned int)time(NULL));
AzureIoTClient 18:e3e29fc771c9 107
AzureIoTClient 18:e3e29fc771c9 108 callbackCounter = 0;
AzureIoTClient 18:e3e29fc771c9 109
Azure.IoT Build 33:f8eb46ca453d 110 if (platform_init() != 0)
AzureIoTClient 18:e3e29fc771c9 111 {
Azure.IoT Build 33:f8eb46ca453d 112 printf("Failed to initialize the platform.\r\n");
AzureIoTClient 18:e3e29fc771c9 113 }
AzureIoTClient 18:e3e29fc771c9 114 else
AzureIoTClient 18:e3e29fc771c9 115 {
Azure.IoT Build 33:f8eb46ca453d 116 (void)printf("Starting the IoTHub client sample HTTP...\r\n");
AzureIoTClient 18:e3e29fc771c9 117
Azure.IoT Build 33:f8eb46ca453d 118 if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol)) == NULL)
AzureIoTClient 18:e3e29fc771c9 119 {
Azure.IoT Build 33:f8eb46ca453d 120 (void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
AzureIoTClient 18:e3e29fc771c9 121 }
AzureIoTClient 18:e3e29fc771c9 122 else
AzureIoTClient 18:e3e29fc771c9 123 {
Azure.IoT Build 33:f8eb46ca453d 124 unsigned int timeout = 241000;
Azure.IoT Build 33:f8eb46ca453d 125 // Because it can poll "after 9 seconds" polls will happen effectively // at ~10 seconds.
Azure.IoT Build 33:f8eb46ca453d 126 // Note that for scalabilty, the default value of minimumPollingTime
Azure.IoT Build 33:f8eb46ca453d 127 // is 25 minutes. For more information, see:
Azure.IoT Build 33:f8eb46ca453d 128 // https://azure.microsoft.com/documentation/articles/iot-hub-devguide/#messaging
Azure.IoT Build 33:f8eb46ca453d 129 unsigned int minimumPollingTime = 9;
Azure.IoT Build 33:f8eb46ca453d 130 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "timeout", &timeout) != IOTHUB_CLIENT_OK)
Azure.IoT Build 33:f8eb46ca453d 131 {
Azure.IoT Build 33:f8eb46ca453d 132 printf("failure to set option \"timeout\"\r\n");
Azure.IoT Build 33:f8eb46ca453d 133 }
AzureIoTClient 20:52f8298d7256 134
Azure.IoT Build 33:f8eb46ca453d 135 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK)
AzureIoTClient 18:e3e29fc771c9 136 {
Azure.IoT Build 33:f8eb46ca453d 137 printf("failure to set option \"MinimumPollingTime\"\r\n");
Azure.IoT Build 33:f8eb46ca453d 138 }
Azure.IoT Build 33:f8eb46ca453d 139
Azure.IoT Build 33:f8eb46ca453d 140 #ifdef MBED_BUILD_TIMESTAMP
Azure.IoT Build 33:f8eb46ca453d 141 // For mbed add the certificate information
Azure.IoT Build 33:f8eb46ca453d 142 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
Azure.IoT Build 33:f8eb46ca453d 143 {
Azure.IoT Build 33:f8eb46ca453d 144 printf("failure to set option \"TrustedCerts\"\r\n");
Azure.IoT Build 33:f8eb46ca453d 145 }
Azure.IoT Build 33:f8eb46ca453d 146 #endif // MBED_BUILD_TIMESTAMP
Azure.IoT Build 33:f8eb46ca453d 147
Azure.IoT Build 33:f8eb46ca453d 148 /* Setting Message call back, so we can receive Commands. */
Azure.IoT Build 33:f8eb46ca453d 149 if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK)
Azure.IoT Build 33:f8eb46ca453d 150 {
Azure.IoT Build 33:f8eb46ca453d 151 (void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n");
Azure.IoT Build 33:f8eb46ca453d 152 }
Azure.IoT Build 33:f8eb46ca453d 153 else
Azure.IoT Build 33:f8eb46ca453d 154 {
Azure.IoT Build 33:f8eb46ca453d 155 int i;
Azure.IoT Build 33:f8eb46ca453d 156
Azure.IoT Build 33:f8eb46ca453d 157 (void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n");
Azure.IoT Build 33:f8eb46ca453d 158
Azure.IoT Build 33:f8eb46ca453d 159 /* Now that we are ready to receive commands, let's send some messages */
Azure.IoT Build 33:f8eb46ca453d 160 for (i = 0; i < MESSAGE_COUNT; i++)
AzureIoTClient 18:e3e29fc771c9 161 {
Azure.IoT Build 33:f8eb46ca453d 162 sprintf_s(msgText, sizeof(msgText), "{\"deviceId\": \"myFirstDevice\",\"windSpeed\": %.2f}", avgWindSpeed + (rand() % 4 + 2));
Azure.IoT Build 33:f8eb46ca453d 163 if ((messages[i].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
AzureIoTClient 18:e3e29fc771c9 164 {
Azure.IoT Build 33:f8eb46ca453d 165 (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
AzureIoTClient 18:e3e29fc771c9 166 }
AzureIoTClient 18:e3e29fc771c9 167 else
AzureIoTClient 18:e3e29fc771c9 168 {
Azure.IoT Build 33:f8eb46ca453d 169 MAP_HANDLE propMap;
Azure.IoT Build 33:f8eb46ca453d 170
Azure.IoT Build 33:f8eb46ca453d 171 messages[i].messageTrackingId = i;
Azure.IoT Build 33:f8eb46ca453d 172
Azure.IoT Build 33:f8eb46ca453d 173 propMap = IoTHubMessage_Properties(messages[i].messageHandle);
Azure.IoT Build 33:f8eb46ca453d 174 sprintf_s(propText, sizeof(propText), "PropMsg_%d", i);
Azure.IoT Build 33:f8eb46ca453d 175 if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
Azure.IoT Build 33:f8eb46ca453d 176 {
Azure.IoT Build 33:f8eb46ca453d 177 (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
Azure.IoT Build 33:f8eb46ca453d 178 }
Azure.IoT Build 33:f8eb46ca453d 179
Azure.IoT Build 33:f8eb46ca453d 180 if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK)
Azure.IoT Build 33:f8eb46ca453d 181 {
Azure.IoT Build 33:f8eb46ca453d 182 (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
Azure.IoT Build 33:f8eb46ca453d 183 }
Azure.IoT Build 33:f8eb46ca453d 184 else
Azure.IoT Build 33:f8eb46ca453d 185 {
Azure.IoT Build 33:f8eb46ca453d 186 (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", i);
Azure.IoT Build 33:f8eb46ca453d 187 }
Azure.IoT Build 33:f8eb46ca453d 188
AzureIoTClient 18:e3e29fc771c9 189 }
AzureIoTClient 18:e3e29fc771c9 190 }
AzureIoTClient 18:e3e29fc771c9 191 }
AzureIoTClient 18:e3e29fc771c9 192
Azure.IoT Build 33:f8eb46ca453d 193 /* Wait for Commands. */
Azure.IoT Build 33:f8eb46ca453d 194 while (1)
Azure.IoT Build 33:f8eb46ca453d 195 {
Azure.IoT Build 33:f8eb46ca453d 196 IoTHubClient_LL_DoWork(iotHubClientHandle);
Azure.IoT Build 33:f8eb46ca453d 197 }
Azure.IoT Build 33:f8eb46ca453d 198
Azure.IoT Build 33:f8eb46ca453d 199 IoTHubClient_LL_Destroy(iotHubClientHandle);
AzureIoTClient 18:e3e29fc771c9 200 }
Azure.IoT Build 33:f8eb46ca453d 201 platform_deinit();
AzureIoTClient 18:e3e29fc771c9 202 }
AzureIoTClient 18:e3e29fc771c9 203 }