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:
Thu Oct 22 18:33:42 2015 -0700
Revision:
18:e3e29fc771c9
Child:
20:52f8298d7256
v1.0.0-preview.4

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 18:e3e29fc771c9 12 #include "iothub_client_ll.h"
AzureIoTClient 18:e3e29fc771c9 13 #include "iothub_message.h"
AzureIoTClient 18:e3e29fc771c9 14 #include "crt_abstractions.h"
AzureIoTClient 18:e3e29fc771c9 15 #include "iothubtransporthttp.h"
AzureIoTClient 18:e3e29fc771c9 16
AzureIoTClient 18:e3e29fc771c9 17 #ifdef MBED_BUILD_TIMESTAMP
AzureIoTClient 18:e3e29fc771c9 18 #include "certs.h"
AzureIoTClient 18:e3e29fc771c9 19 #endif // MBED_BUILD_TIMESTAMP
AzureIoTClient 18:e3e29fc771c9 20
AzureIoTClient 18:e3e29fc771c9 21 static const char* connectionString = "[device connection string]";
AzureIoTClient 18:e3e29fc771c9 22 static int callbackCounter;
AzureIoTClient 18:e3e29fc771c9 23
AzureIoTClient 18:e3e29fc771c9 24 DEFINE_ENUM_STRINGS(IOTHUB_CLIENT_CONFIRMATION_RESULT, IOTHUB_CLIENT_CONFIRMATION_RESULT_VALUES);
AzureIoTClient 18:e3e29fc771c9 25
AzureIoTClient 18:e3e29fc771c9 26 typedef struct EVENT_INSTANCE_TAG
AzureIoTClient 18:e3e29fc771c9 27 {
AzureIoTClient 18:e3e29fc771c9 28 IOTHUB_MESSAGE_HANDLE messageHandle;
AzureIoTClient 18:e3e29fc771c9 29 int messageTrackingId; // For tracking the messages within the user callback.
AzureIoTClient 18:e3e29fc771c9 30 } EVENT_INSTANCE;
AzureIoTClient 18:e3e29fc771c9 31
AzureIoTClient 18:e3e29fc771c9 32 static IOTHUBMESSAGE_DISPOSITION_RESULT ReceiveMessageCallback(IOTHUB_MESSAGE_HANDLE message, void* userContextCallback)
AzureIoTClient 18:e3e29fc771c9 33 {
AzureIoTClient 18:e3e29fc771c9 34 int* counter = (int*)userContextCallback;
AzureIoTClient 18:e3e29fc771c9 35 const char* buffer;
AzureIoTClient 18:e3e29fc771c9 36 size_t size;
AzureIoTClient 18:e3e29fc771c9 37 if (IoTHubMessage_GetByteArray(message, (const unsigned char**)&buffer, &size) != IOTHUB_MESSAGE_OK)
AzureIoTClient 18:e3e29fc771c9 38 {
AzureIoTClient 18:e3e29fc771c9 39 printf("unable to retrieve the message data\r\n");
AzureIoTClient 18:e3e29fc771c9 40 }
AzureIoTClient 18:e3e29fc771c9 41 else
AzureIoTClient 18:e3e29fc771c9 42 {
AzureIoTClient 18:e3e29fc771c9 43 (void)printf("Received Message [%d] with Data: <<<%.*s>>> & Size=%d\r\n", *counter, (int)size, buffer, (int)size);
AzureIoTClient 18:e3e29fc771c9 44 }
AzureIoTClient 18:e3e29fc771c9 45
AzureIoTClient 18:e3e29fc771c9 46 // Retrieve properties from the message
AzureIoTClient 18:e3e29fc771c9 47 MAP_HANDLE mapProperties = IoTHubMessage_Properties(message);
AzureIoTClient 18:e3e29fc771c9 48 if (mapProperties != NULL)
AzureIoTClient 18:e3e29fc771c9 49 {
AzureIoTClient 18:e3e29fc771c9 50 const char*const* keys;
AzureIoTClient 18:e3e29fc771c9 51 const char*const* values;
AzureIoTClient 18:e3e29fc771c9 52 size_t propertyCount = 0;
AzureIoTClient 18:e3e29fc771c9 53 if (Map_GetInternals(mapProperties, &keys, &values, &propertyCount) == MAP_OK)
AzureIoTClient 18:e3e29fc771c9 54 {
AzureIoTClient 18:e3e29fc771c9 55 if (propertyCount > 0)
AzureIoTClient 18:e3e29fc771c9 56 {
AzureIoTClient 18:e3e29fc771c9 57 printf("Message Properties:\r\n");
AzureIoTClient 18:e3e29fc771c9 58 for (size_t index = 0; index < propertyCount; index++)
AzureIoTClient 18:e3e29fc771c9 59 {
AzureIoTClient 18:e3e29fc771c9 60 printf("\tKey: %s Value: %s\r\n", keys[index], values[index]);
AzureIoTClient 18:e3e29fc771c9 61 }
AzureIoTClient 18:e3e29fc771c9 62 printf("\r\n");
AzureIoTClient 18:e3e29fc771c9 63 }
AzureIoTClient 18:e3e29fc771c9 64 }
AzureIoTClient 18:e3e29fc771c9 65 }
AzureIoTClient 18:e3e29fc771c9 66
AzureIoTClient 18:e3e29fc771c9 67 /* Some device specific action code goes here... */
AzureIoTClient 18:e3e29fc771c9 68 (*counter)++;
AzureIoTClient 18:e3e29fc771c9 69 return IOTHUBMESSAGE_ACCEPTED;
AzureIoTClient 18:e3e29fc771c9 70 }
AzureIoTClient 18:e3e29fc771c9 71
AzureIoTClient 18:e3e29fc771c9 72 static void SendConfirmationCallback(IOTHUB_CLIENT_CONFIRMATION_RESULT result, void* userContextCallback)
AzureIoTClient 18:e3e29fc771c9 73 {
AzureIoTClient 18:e3e29fc771c9 74 EVENT_INSTANCE* eventInstance = (EVENT_INSTANCE*)userContextCallback;
AzureIoTClient 18:e3e29fc771c9 75 (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 76 /* Some device specific action code goes here... */
AzureIoTClient 18:e3e29fc771c9 77 callbackCounter++;
AzureIoTClient 18:e3e29fc771c9 78 IoTHubMessage_Destroy(eventInstance->messageHandle);
AzureIoTClient 18:e3e29fc771c9 79 }
AzureIoTClient 18:e3e29fc771c9 80
AzureIoTClient 18:e3e29fc771c9 81 static char msgText[1024];
AzureIoTClient 18:e3e29fc771c9 82 static char propText[1024];
AzureIoTClient 18:e3e29fc771c9 83 #define MESSAGE_COUNT 5
AzureIoTClient 18:e3e29fc771c9 84
AzureIoTClient 18:e3e29fc771c9 85 void iothub_client_sample_http_run(void)
AzureIoTClient 18:e3e29fc771c9 86 {
AzureIoTClient 18:e3e29fc771c9 87 IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle;
AzureIoTClient 18:e3e29fc771c9 88
AzureIoTClient 18:e3e29fc771c9 89 EVENT_INSTANCE messages[MESSAGE_COUNT];
AzureIoTClient 18:e3e29fc771c9 90
AzureIoTClient 18:e3e29fc771c9 91 srand((unsigned int)time(NULL));
AzureIoTClient 18:e3e29fc771c9 92 double avgWindSpeed = 10.0;
AzureIoTClient 18:e3e29fc771c9 93
AzureIoTClient 18:e3e29fc771c9 94 callbackCounter = 0;
AzureIoTClient 18:e3e29fc771c9 95 int receiveContext = 0;
AzureIoTClient 18:e3e29fc771c9 96
AzureIoTClient 18:e3e29fc771c9 97 (void)printf("Starting the IoTHub client sample HTTP...\r\n");
AzureIoTClient 18:e3e29fc771c9 98
AzureIoTClient 18:e3e29fc771c9 99 if ((iotHubClientHandle = IoTHubClient_LL_CreateFromConnectionString(connectionString, HTTP_Protocol)) == NULL)
AzureIoTClient 18:e3e29fc771c9 100 {
AzureIoTClient 18:e3e29fc771c9 101 (void)printf("ERROR: iotHubClientHandle is NULL!\r\n");
AzureIoTClient 18:e3e29fc771c9 102 }
AzureIoTClient 18:e3e29fc771c9 103 else
AzureIoTClient 18:e3e29fc771c9 104 {
AzureIoTClient 18:e3e29fc771c9 105 unsigned int timeout = 241000;
AzureIoTClient 18:e3e29fc771c9 106 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "timeout", &timeout) != IOTHUB_CLIENT_OK)
AzureIoTClient 18:e3e29fc771c9 107 {
AzureIoTClient 18:e3e29fc771c9 108 printf("failure to set option \"timeout\"\r\n");
AzureIoTClient 18:e3e29fc771c9 109 }
AzureIoTClient 18:e3e29fc771c9 110
AzureIoTClient 18:e3e29fc771c9 111 unsigned int minimumPollingTime = 9; /*because it can poll "after 9 seconds" polls will happen effectively at ~10 seconds*/
AzureIoTClient 18:e3e29fc771c9 112 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "MinimumPollingTime", &minimumPollingTime) != IOTHUB_CLIENT_OK)
AzureIoTClient 18:e3e29fc771c9 113 {
AzureIoTClient 18:e3e29fc771c9 114 printf("failure to set option \"MinimumPollingTime\"\r\n");
AzureIoTClient 18:e3e29fc771c9 115 }
AzureIoTClient 18:e3e29fc771c9 116
AzureIoTClient 18:e3e29fc771c9 117 #ifdef MBED_BUILD_TIMESTAMP
AzureIoTClient 18:e3e29fc771c9 118 // For mbed add the certificate information
AzureIoTClient 18:e3e29fc771c9 119 if (IoTHubClient_LL_SetOption(iotHubClientHandle, "TrustedCerts", certificates) != IOTHUB_CLIENT_OK)
AzureIoTClient 18:e3e29fc771c9 120 {
AzureIoTClient 18:e3e29fc771c9 121 printf("failure to set option \"TrustedCerts\"\r\n");
AzureIoTClient 18:e3e29fc771c9 122 }
AzureIoTClient 18:e3e29fc771c9 123 #endif // MBED_BUILD_TIMESTAMP
AzureIoTClient 18:e3e29fc771c9 124
AzureIoTClient 18:e3e29fc771c9 125 /* Setting Message call back, so we can receive Commands. */
AzureIoTClient 18:e3e29fc771c9 126 if (IoTHubClient_LL_SetMessageCallback(iotHubClientHandle, ReceiveMessageCallback, &receiveContext) != IOTHUB_CLIENT_OK)
AzureIoTClient 18:e3e29fc771c9 127 {
AzureIoTClient 18:e3e29fc771c9 128 (void)printf("ERROR: IoTHubClient_LL_SetMessageCallback..........FAILED!\r\n");
AzureIoTClient 18:e3e29fc771c9 129 }
AzureIoTClient 18:e3e29fc771c9 130 else
AzureIoTClient 18:e3e29fc771c9 131 {
AzureIoTClient 18:e3e29fc771c9 132 (void)printf("IoTHubClient_LL_SetMessageCallback...successful.\r\n");
AzureIoTClient 18:e3e29fc771c9 133
AzureIoTClient 18:e3e29fc771c9 134 /* Now that we are ready to receive commands, let's send some messages */
AzureIoTClient 18:e3e29fc771c9 135 for (int i = 0; i < MESSAGE_COUNT; i++)
AzureIoTClient 18:e3e29fc771c9 136 {
AzureIoTClient 18:e3e29fc771c9 137 sprintf_s(msgText, sizeof(msgText), "{\"deviceId\": \"myFirstDevice\",\"windSpeed\": %.2f}", avgWindSpeed + (rand() % 4 + 2));
AzureIoTClient 18:e3e29fc771c9 138 if ((messages[i].messageHandle = IoTHubMessage_CreateFromByteArray((const unsigned char*)msgText, strlen(msgText))) == NULL)
AzureIoTClient 18:e3e29fc771c9 139 {
AzureIoTClient 18:e3e29fc771c9 140 (void)printf("ERROR: iotHubMessageHandle is NULL!\r\n");
AzureIoTClient 18:e3e29fc771c9 141 }
AzureIoTClient 18:e3e29fc771c9 142 else
AzureIoTClient 18:e3e29fc771c9 143 {
AzureIoTClient 18:e3e29fc771c9 144 messages[i].messageTrackingId = i;
AzureIoTClient 18:e3e29fc771c9 145
AzureIoTClient 18:e3e29fc771c9 146 MAP_HANDLE propMap = IoTHubMessage_Properties(messages[i].messageHandle);
AzureIoTClient 18:e3e29fc771c9 147 sprintf_s(propText, sizeof(propText), "PropMsg_%d", i);
AzureIoTClient 18:e3e29fc771c9 148 if (Map_AddOrUpdate(propMap, "PropName", propText) != MAP_OK)
AzureIoTClient 18:e3e29fc771c9 149 {
AzureIoTClient 18:e3e29fc771c9 150 (void)printf("ERROR: Map_AddOrUpdate Failed!\r\n");
AzureIoTClient 18:e3e29fc771c9 151 }
AzureIoTClient 18:e3e29fc771c9 152
AzureIoTClient 18:e3e29fc771c9 153 if (IoTHubClient_LL_SendEventAsync(iotHubClientHandle, messages[i].messageHandle, SendConfirmationCallback, &messages[i]) != IOTHUB_CLIENT_OK)
AzureIoTClient 18:e3e29fc771c9 154 {
AzureIoTClient 18:e3e29fc771c9 155 (void)printf("ERROR: IoTHubClient_LL_SendEventAsync..........FAILED!\r\n");
AzureIoTClient 18:e3e29fc771c9 156 }
AzureIoTClient 18:e3e29fc771c9 157 else
AzureIoTClient 18:e3e29fc771c9 158 {
AzureIoTClient 18:e3e29fc771c9 159 (void)printf("IoTHubClient_LL_SendEventAsync accepted message [%d] for transmission to IoT Hub.\r\n", i);
AzureIoTClient 18:e3e29fc771c9 160 }
AzureIoTClient 18:e3e29fc771c9 161
AzureIoTClient 18:e3e29fc771c9 162 }
AzureIoTClient 18:e3e29fc771c9 163 }
AzureIoTClient 18:e3e29fc771c9 164 }
AzureIoTClient 18:e3e29fc771c9 165
AzureIoTClient 18:e3e29fc771c9 166 /* Wait for Commands. */
AzureIoTClient 18:e3e29fc771c9 167 while (1)
AzureIoTClient 18:e3e29fc771c9 168 {
AzureIoTClient 18:e3e29fc771c9 169 IoTHubClient_LL_DoWork(iotHubClientHandle);
AzureIoTClient 18:e3e29fc771c9 170 }
AzureIoTClient 18:e3e29fc771c9 171
AzureIoTClient 18:e3e29fc771c9 172 IoTHubClient_LL_Destroy(iotHubClientHandle);
AzureIoTClient 18:e3e29fc771c9 173 }
AzureIoTClient 18:e3e29fc771c9 174 }