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:
Tue Sep 15 22:45:03 2015 -0700
Revision:
0:0e4a5a208d47
Child:
10:e42076d83f79
New release

Who changed what in which revision?

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