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:
Fri Jun 17 17:04:01 2016 -0700
Revision:
42:289de245ba1d
Parent:
41:7a930aa6dc01
Child:
46:91bd03862871
1.0.9

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