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