Dieter Graef / iothub_client

Dependents:   STM32F746_iothub_client_sample_mqtt

Fork of iothub_client by Azure IoT

Committer:
AzureIoTClient
Date:
Tue Sep 15 21:47:12 2015 -0700
Revision:
0:e393db310d89
Automatic build commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 0:e393db310d89 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 0:e393db310d89 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 0:e393db310d89 3
AzureIoTClient 0:e393db310d89 4 #include <stdlib.h>
AzureIoTClient 0:e393db310d89 5 #ifdef _CRTDBG_MAP_ALLOC
AzureIoTClient 0:e393db310d89 6 #include <crtdbg.h>
AzureIoTClient 0:e393db310d89 7 #endif
AzureIoTClient 0:e393db310d89 8 #include "gballoc.h"
AzureIoTClient 0:e393db310d89 9
AzureIoTClient 0:e393db310d89 10 #include <time.h>
AzureIoTClient 0:e393db310d89 11 #include "iothub_client_private.h"
AzureIoTClient 0:e393db310d89 12 #include "iothubtransporthttp.h"
AzureIoTClient 0:e393db310d89 13
AzureIoTClient 0:e393db310d89 14 #include "httpapiexsas.h"
AzureIoTClient 0:e393db310d89 15 #include "urlencode.h"
AzureIoTClient 0:e393db310d89 16 #include "iot_logging.h"
AzureIoTClient 0:e393db310d89 17 #include "httpapiex.h"
AzureIoTClient 0:e393db310d89 18 #include "httpapiexsas.h"
AzureIoTClient 0:e393db310d89 19 #include "strings.h"
AzureIoTClient 0:e393db310d89 20 #include "base64.h"
AzureIoTClient 0:e393db310d89 21 #include "doublylinkedlist.h"
AzureIoTClient 0:e393db310d89 22 #include "httpheaders.h"
AzureIoTClient 0:e393db310d89 23 #include "agenttime.h"
AzureIoTClient 0:e393db310d89 24
AzureIoTClient 0:e393db310d89 25
AzureIoTClient 0:e393db310d89 26 #define IOTHUB_APP_PREFIX "iothub-app-"
AzureIoTClient 0:e393db310d89 27
AzureIoTClient 0:e393db310d89 28 #define CONTENT_TYPE "Content-Type"
AzureIoTClient 0:e393db310d89 29 #define APPLICATION_OCTET_STREAM "application/octet-stream"
AzureIoTClient 0:e393db310d89 30 #define APPLICATION_VND_MICROSOFT_IOTHUB_JSON "application/vnd.microsoft.iothub.json"
AzureIoTClient 0:e393db310d89 31
AzureIoTClient 0:e393db310d89 32 /*DEFAULT_GETMINIMUMPOLLINGTIME is the minimum time in seconds allowed between 2 consecutive GET issues to the service (GET=fetch notifications)*/
AzureIoTClient 0:e393db310d89 33 /*the default is 25 minutes*/
AzureIoTClient 0:e393db310d89 34 #define DEFAULT_GETMINIMUMPOLLINGTIME ((unsigned int)25*60)
AzureIoTClient 0:e393db310d89 35
AzureIoTClient 0:e393db310d89 36 /*forward declaration*/
AzureIoTClient 0:e393db310d89 37 static int appendMapToJSON(STRING_HANDLE existing, const char* const* keys, const char* const* values, size_t count);
AzureIoTClient 0:e393db310d89 38
AzureIoTClient 0:e393db310d89 39 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_069: [This function shall return a pointer to a structure of type TRANSPORT_PROVIDER having the following values for its fields:] */
AzureIoTClient 0:e393db310d89 40 static TRANSPORT_PROVIDER thisTransportProvider =
AzureIoTClient 0:e393db310d89 41 {
AzureIoTClient 0:e393db310d89 42 IoTHubTransportHttp_SetOption, /*pfIoTHubTransport_SetOption IoTHubTransport_SetOption; */
AzureIoTClient 0:e393db310d89 43 IoTHubTransportHttp_Create, /*pfIoTHubTransport_Create IoTHubTransport_Create; */
AzureIoTClient 0:e393db310d89 44 IoTHubTransportHttp_Destroy, /*pfIoTHubTransport_Destroy IoTHubTransport_Destroy; */
AzureIoTClient 0:e393db310d89 45 IoTHubTransportHttp_Subscribe, /*pfIoTHubTransport_Subscribe IoTHubTransport_Subscribe; */
AzureIoTClient 0:e393db310d89 46 IoTHubTransportHttp_Unsubscribe, /*pfIoTHubTransport_Unsubscribe IoTHubTransport_Unsubscribe; */
AzureIoTClient 0:e393db310d89 47 IoTHubTransportHttp_DoWork, /*pfIoTHubTransport_DoWork IoTHubTransport_DoWork; */
AzureIoTClient 0:e393db310d89 48 IoTHubTransportHttp_GetSendStatus /* pfIoTHubTransport_GetSendStatus IoTHubTransport_GetSendStatus */
AzureIoTClient 0:e393db310d89 49 };
AzureIoTClient 0:e393db310d89 50
AzureIoTClient 0:e393db310d89 51 const void* IoTHubTransportHttp_ProvideTransportInterface(void)
AzureIoTClient 0:e393db310d89 52 {
AzureIoTClient 0:e393db310d89 53 return &thisTransportProvider;
AzureIoTClient 0:e393db310d89 54 }
AzureIoTClient 0:e393db310d89 55
AzureIoTClient 0:e393db310d89 56 typedef struct HTTPTRANSPORT_HANDLE_DATA_TAG
AzureIoTClient 0:e393db310d89 57 {
AzureIoTClient 0:e393db310d89 58 STRING_HANDLE eventHTTPrelativePath;
AzureIoTClient 0:e393db310d89 59 STRING_HANDLE notificationHTTPrelativePath;
AzureIoTClient 0:e393db310d89 60 HTTP_HEADERS_HANDLE eventHTTPrequestHeaders;
AzureIoTClient 0:e393db310d89 61 STRING_HANDLE hostName;
AzureIoTClient 0:e393db310d89 62 HTTPAPIEX_HANDLE httpApiExHandle;
AzureIoTClient 0:e393db310d89 63 HTTP_HEADERS_HANDLE notificationHTTPrequestHeaders;
AzureIoTClient 0:e393db310d89 64 STRING_HANDLE abandonHTTPrelativePathBegin;
AzureIoTClient 0:e393db310d89 65 HTTPAPIEX_SAS_HANDLE sasObject;
AzureIoTClient 0:e393db310d89 66 bool DoWork_PullNotification;
AzureIoTClient 0:e393db310d89 67 bool doBatchedTransfers;
AzureIoTClient 0:e393db310d89 68 bool isFirstPoll;
AzureIoTClient 0:e393db310d89 69 unsigned int getMinimumPollingTime;
AzureIoTClient 0:e393db310d89 70 time_t lastPollTime;
AzureIoTClient 0:e393db310d89 71 PDLIST_ENTRY waitingToSend;
AzureIoTClient 0:e393db310d89 72 DLIST_ENTRY eventConfirmations; /*holds items for event confirmations*/
AzureIoTClient 0:e393db310d89 73 }HTTPTRANSPORT_HANDLE_DATA;
AzureIoTClient 0:e393db310d89 74
AzureIoTClient 0:e393db310d89 75
AzureIoTClient 0:e393db310d89 76 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_003: [Otherwise IoTHubTransportHttp_Create shall create an immutable string (further called "event HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events?api-version=2015-08-15-preview".]*/
AzureIoTClient 0:e393db310d89 77 static void destroy_eventHTTPrelativePath(HTTPTRANSPORT_HANDLE_DATA* handleData)
AzureIoTClient 0:e393db310d89 78 {
AzureIoTClient 0:e393db310d89 79 STRING_delete(handleData->eventHTTPrelativePath);
AzureIoTClient 0:e393db310d89 80 handleData->eventHTTPrelativePath = NULL;
AzureIoTClient 0:e393db310d89 81 }
AzureIoTClient 0:e393db310d89 82
AzureIoTClient 0:e393db310d89 83 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_003: [Otherwise IoTHubTransportHttp_Create shall create an immutable string (further called "event HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events?api-version=2015-08-15-preview".]*/
AzureIoTClient 0:e393db310d89 84 static bool create_eventHTTPrelativePath(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 0:e393db310d89 85 {
AzureIoTClient 0:e393db310d89 86 bool result;
AzureIoTClient 0:e393db310d89 87 STRING_HANDLE urlEncodedDeviceId = NULL;
AzureIoTClient 0:e393db310d89 88 handleData->eventHTTPrelativePath = STRING_construct("/devices/");
AzureIoTClient 0:e393db310d89 89 if (handleData->eventHTTPrelativePath == NULL)
AzureIoTClient 0:e393db310d89 90 {
AzureIoTClient 0:e393db310d89 91 result = false;
AzureIoTClient 0:e393db310d89 92 }
AzureIoTClient 0:e393db310d89 93 else if (!(
AzureIoTClient 0:e393db310d89 94 ((urlEncodedDeviceId = URL_EncodeString(config->upperConfig->deviceId)) != NULL) &&
AzureIoTClient 0:e393db310d89 95 (STRING_concat_with_STRING(handleData->eventHTTPrelativePath, urlEncodedDeviceId) == 0) &&
AzureIoTClient 0:e393db310d89 96 (STRING_concat(handleData->eventHTTPrelativePath, EVENT_ENDPOINT API_VERSION) == 0)
AzureIoTClient 0:e393db310d89 97 ))
AzureIoTClient 0:e393db310d89 98 {
AzureIoTClient 0:e393db310d89 99 destroy_eventHTTPrelativePath(handleData);
AzureIoTClient 0:e393db310d89 100 result = false;
AzureIoTClient 0:e393db310d89 101 }
AzureIoTClient 0:e393db310d89 102 else
AzureIoTClient 0:e393db310d89 103 {
AzureIoTClient 0:e393db310d89 104 result = true;
AzureIoTClient 0:e393db310d89 105 }
AzureIoTClient 0:e393db310d89 106 STRING_delete(urlEncodedDeviceId);
AzureIoTClient 0:e393db310d89 107 return result;
AzureIoTClient 0:e393db310d89 108 }
AzureIoTClient 0:e393db310d89 109
AzureIoTClient 0:e393db310d89 110 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_034: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called "notification HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/devicebound?api-version=2015-08-15-preview".]*/
AzureIoTClient 0:e393db310d89 111 static void destroy_notificationHTTPrelativePath(HTTPTRANSPORT_HANDLE_DATA* handleData)
AzureIoTClient 0:e393db310d89 112 {
AzureIoTClient 0:e393db310d89 113 STRING_delete(handleData->notificationHTTPrelativePath);
AzureIoTClient 0:e393db310d89 114 handleData->notificationHTTPrelativePath = NULL;
AzureIoTClient 0:e393db310d89 115 }
AzureIoTClient 0:e393db310d89 116
AzureIoTClient 0:e393db310d89 117 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_034: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called "notification HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/devicebound?api-version=2015-08-15-preview".]*/
AzureIoTClient 0:e393db310d89 118 static bool create_notificationHTTPrelativePath(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 0:e393db310d89 119 {
AzureIoTClient 0:e393db310d89 120 bool result;
AzureIoTClient 0:e393db310d89 121 handleData->notificationHTTPrelativePath = STRING_construct("/devices/");
AzureIoTClient 0:e393db310d89 122 if (handleData->notificationHTTPrelativePath == NULL)
AzureIoTClient 0:e393db310d89 123 {
AzureIoTClient 0:e393db310d89 124 result = false;
AzureIoTClient 0:e393db310d89 125 }
AzureIoTClient 0:e393db310d89 126 else
AzureIoTClient 0:e393db310d89 127 {
AzureIoTClient 0:e393db310d89 128 STRING_HANDLE urlEncodedDeviceId = NULL;
AzureIoTClient 0:e393db310d89 129 if (!(
AzureIoTClient 0:e393db310d89 130 ((urlEncodedDeviceId = URL_EncodeString(config->upperConfig->deviceId)) != NULL) &&
AzureIoTClient 0:e393db310d89 131 (STRING_concat_with_STRING(handleData->notificationHTTPrelativePath, urlEncodedDeviceId) == 0) &&
AzureIoTClient 0:e393db310d89 132 (STRING_concat(handleData->notificationHTTPrelativePath, NOTIFICATION_ENDPOINT_HTTP API_VERSION) == 0)
AzureIoTClient 0:e393db310d89 133 ))
AzureIoTClient 0:e393db310d89 134 {
AzureIoTClient 0:e393db310d89 135 result = false;
AzureIoTClient 0:e393db310d89 136 destroy_notificationHTTPrelativePath(handleData);
AzureIoTClient 0:e393db310d89 137 }
AzureIoTClient 0:e393db310d89 138 else
AzureIoTClient 0:e393db310d89 139 {
AzureIoTClient 0:e393db310d89 140 result = true;
AzureIoTClient 0:e393db310d89 141 }
AzureIoTClient 0:e393db310d89 142 STRING_delete(urlEncodedDeviceId);
AzureIoTClient 0:e393db310d89 143 }
AzureIoTClient 0:e393db310d89 144
AzureIoTClient 0:e393db310d89 145 return result;
AzureIoTClient 0:e393db310d89 146 }
AzureIoTClient 0:e393db310d89 147
AzureIoTClient 0:e393db310d89 148 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_005: [Otherwise, IoTHubTransportHttp_Create shall create a set of HTTP headers (further on called "event HTTP request headers") consisting of the following fixed field names and values:
AzureIoTClient 0:e393db310d89 149 "iothub-to":"/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events";
AzureIoTClient 0:e393db310d89 150 "Authorization":" "
AzureIoTClient 0:e393db310d89 151 "Content-Type":"application/vnd.microsoft.iothub.json"
AzureIoTClient 0:e393db310d89 152 "Accept":"application/json"
AzureIoTClient 0:e393db310d89 153 "Connection":"Keep-Alive"]*/
AzureIoTClient 0:e393db310d89 154 static void destroy_eventHTTPrequestHeaders(HTTPTRANSPORT_HANDLE_DATA* handleData)
AzureIoTClient 0:e393db310d89 155 {
AzureIoTClient 0:e393db310d89 156 HTTPHeaders_Free(handleData->eventHTTPrequestHeaders);
AzureIoTClient 0:e393db310d89 157 handleData->eventHTTPrequestHeaders = NULL;
AzureIoTClient 0:e393db310d89 158 }
AzureIoTClient 0:e393db310d89 159
AzureIoTClient 0:e393db310d89 160 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_005: [Otherwise, IoTHubTransportHttp_Create shall create a set of HTTP headers (further on called "event HTTP request headers") consisting of the following fixed field names and values:
AzureIoTClient 0:e393db310d89 161 "iothub-to":"/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events";
AzureIoTClient 0:e393db310d89 162 "Authorization":" "
AzureIoTClient 0:e393db310d89 163 "Content-Type":"application/vnd.microsoft.iothub.json"
AzureIoTClient 0:e393db310d89 164 "Accept":"application/json"
AzureIoTClient 0:e393db310d89 165 "Connection":"Keep-Alive"]*/
AzureIoTClient 0:e393db310d89 166 static bool create_eventHTTPrequestHeaders(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 0:e393db310d89 167 {
AzureIoTClient 0:e393db310d89 168 bool result;
AzureIoTClient 0:e393db310d89 169 handleData->eventHTTPrequestHeaders = HTTPHeaders_Alloc();
AzureIoTClient 0:e393db310d89 170 if (handleData->eventHTTPrequestHeaders == NULL)
AzureIoTClient 0:e393db310d89 171 {
AzureIoTClient 0:e393db310d89 172 result = false;
AzureIoTClient 0:e393db310d89 173 }
AzureIoTClient 0:e393db310d89 174 else
AzureIoTClient 0:e393db310d89 175 {
AzureIoTClient 0:e393db310d89 176 STRING_HANDLE temp = STRING_construct("/devices/");
AzureIoTClient 0:e393db310d89 177 if (temp == NULL)
AzureIoTClient 0:e393db310d89 178 {
AzureIoTClient 0:e393db310d89 179 result = false;
AzureIoTClient 0:e393db310d89 180 destroy_eventHTTPrequestHeaders(handleData);
AzureIoTClient 0:e393db310d89 181 }
AzureIoTClient 0:e393db310d89 182 else
AzureIoTClient 0:e393db310d89 183 {
AzureIoTClient 0:e393db310d89 184 STRING_HANDLE urlEncodedDeviceId = NULL;
AzureIoTClient 0:e393db310d89 185 if (!(
AzureIoTClient 0:e393db310d89 186 ((urlEncodedDeviceId = URL_EncodeString(config->upperConfig->deviceId)) != NULL) &&
AzureIoTClient 0:e393db310d89 187 (STRING_concat_with_STRING(temp, urlEncodedDeviceId) == 0) &&
AzureIoTClient 0:e393db310d89 188 (STRING_concat(temp, EVENT_ENDPOINT) == 0)
AzureIoTClient 0:e393db310d89 189 ))
AzureIoTClient 0:e393db310d89 190 {
AzureIoTClient 0:e393db310d89 191 result = false;
AzureIoTClient 0:e393db310d89 192 destroy_eventHTTPrequestHeaders(handleData);
AzureIoTClient 0:e393db310d89 193 }
AzureIoTClient 0:e393db310d89 194 else
AzureIoTClient 0:e393db310d89 195 {
AzureIoTClient 0:e393db310d89 196 if (!(
AzureIoTClient 0:e393db310d89 197 (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "iothub-to", STRING_c_str(temp)) == HTTP_HEADERS_OK) &&
AzureIoTClient 0:e393db310d89 198 (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "Authorization", " ") == HTTP_HEADERS_OK) &&
AzureIoTClient 0:e393db310d89 199 (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "Accept", "application/json") == HTTP_HEADERS_OK) &&
AzureIoTClient 0:e393db310d89 200 (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "Connection", "Keep-Alive") == HTTP_HEADERS_OK)
AzureIoTClient 0:e393db310d89 201 ))
AzureIoTClient 0:e393db310d89 202 {
AzureIoTClient 0:e393db310d89 203 result = false;
AzureIoTClient 0:e393db310d89 204 destroy_eventHTTPrequestHeaders(handleData);
AzureIoTClient 0:e393db310d89 205 }
AzureIoTClient 0:e393db310d89 206 else
AzureIoTClient 0:e393db310d89 207 {
AzureIoTClient 0:e393db310d89 208 result = true;
AzureIoTClient 0:e393db310d89 209 }
AzureIoTClient 0:e393db310d89 210 }
AzureIoTClient 0:e393db310d89 211 STRING_delete(temp);
AzureIoTClient 0:e393db310d89 212 STRING_delete(urlEncodedDeviceId);
AzureIoTClient 0:e393db310d89 213 }
AzureIoTClient 0:e393db310d89 214 }
AzureIoTClient 0:e393db310d89 215 return result;
AzureIoTClient 0:e393db310d89 216 }
AzureIoTClient 0:e393db310d89 217
AzureIoTClient 0:e393db310d89 218 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_007: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called hostname) containing config->upperConfig->iotHubName + config->upperConfig->iotHubSuffix.]*/
AzureIoTClient 0:e393db310d89 219 static void destroy_hostName(HTTPTRANSPORT_HANDLE_DATA* handleData)
AzureIoTClient 0:e393db310d89 220 {
AzureIoTClient 0:e393db310d89 221 STRING_delete(handleData->hostName);
AzureIoTClient 0:e393db310d89 222 handleData->hostName = NULL;
AzureIoTClient 0:e393db310d89 223 }
AzureIoTClient 0:e393db310d89 224
AzureIoTClient 0:e393db310d89 225 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_007: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called hostname) containing config->upperConfig->iotHubName + config->upperConfig->iotHubSuffix.]*/
AzureIoTClient 0:e393db310d89 226 static bool create_hostName(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 0:e393db310d89 227 {
AzureIoTClient 0:e393db310d89 228 bool result;
AzureIoTClient 0:e393db310d89 229 handleData->hostName = STRING_construct(config->upperConfig->iotHubName);
AzureIoTClient 0:e393db310d89 230 if (handleData->hostName == NULL)
AzureIoTClient 0:e393db310d89 231 {
AzureIoTClient 0:e393db310d89 232 result = false;
AzureIoTClient 0:e393db310d89 233 }
AzureIoTClient 0:e393db310d89 234 else
AzureIoTClient 0:e393db310d89 235 {
AzureIoTClient 0:e393db310d89 236 if ((STRING_concat(handleData->hostName, ".") != 0) ||
AzureIoTClient 0:e393db310d89 237 (STRING_concat(handleData->hostName, config->upperConfig->iotHubSuffix) != 0))
AzureIoTClient 0:e393db310d89 238 {
AzureIoTClient 0:e393db310d89 239 destroy_hostName(handleData);
AzureIoTClient 0:e393db310d89 240 result = false;
AzureIoTClient 0:e393db310d89 241 }
AzureIoTClient 0:e393db310d89 242 else
AzureIoTClient 0:e393db310d89 243 {
AzureIoTClient 0:e393db310d89 244 result = true;
AzureIoTClient 0:e393db310d89 245 }
AzureIoTClient 0:e393db310d89 246 }
AzureIoTClient 0:e393db310d89 247 return result;
AzureIoTClient 0:e393db310d89 248 }
AzureIoTClient 0:e393db310d89 249
AzureIoTClient 0:e393db310d89 250 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_009: [Otherwise, IoTHubTransportHttp_Create shall create a HTTPAPIEX_HANDLE by a call to HTTPAPIEX_Create passing for hostName the hostname so far constructed by IoTHubTransportHttp_Create.]*/
AzureIoTClient 0:e393db310d89 251 static void destroy_httpApiExHandle(HTTPTRANSPORT_HANDLE_DATA* handleData)
AzureIoTClient 0:e393db310d89 252 {
AzureIoTClient 0:e393db310d89 253 HTTPAPIEX_Destroy(handleData->httpApiExHandle);
AzureIoTClient 0:e393db310d89 254 handleData->httpApiExHandle = NULL;
AzureIoTClient 0:e393db310d89 255 }
AzureIoTClient 0:e393db310d89 256
AzureIoTClient 0:e393db310d89 257 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_007: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called hostname) containing config->upperConfig->iotHubName + config->upperConfig->iotHubSuffix.]*/
AzureIoTClient 0:e393db310d89 258 static bool create_httpApiExHandle(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 0:e393db310d89 259 {
AzureIoTClient 0:e393db310d89 260 bool result;
AzureIoTClient 0:e393db310d89 261 (void)config;
AzureIoTClient 0:e393db310d89 262 handleData->httpApiExHandle = HTTPAPIEX_Create(STRING_c_str(handleData->hostName));
AzureIoTClient 0:e393db310d89 263 if (handleData->httpApiExHandle == NULL)
AzureIoTClient 0:e393db310d89 264 {
AzureIoTClient 0:e393db310d89 265 result = false;
AzureIoTClient 0:e393db310d89 266 }
AzureIoTClient 0:e393db310d89 267 else
AzureIoTClient 0:e393db310d89 268 {
AzureIoTClient 0:e393db310d89 269 result = true;
AzureIoTClient 0:e393db310d89 270 }
AzureIoTClient 0:e393db310d89 271 return result;
AzureIoTClient 0:e393db310d89 272 }
AzureIoTClient 0:e393db310d89 273
AzureIoTClient 0:e393db310d89 274 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_059: [Otherwise, IoTHubTransportHttp_Create shall create a set of HTTP headers (further on called "notification HTTP request headers") consisting of the following fixed field names and values:
AzureIoTClient 0:e393db310d89 275 "Authorization": " "]*/
AzureIoTClient 0:e393db310d89 276 static void destroy_notificationHTTPrequestHeaders(HTTPTRANSPORT_HANDLE_DATA* handleData)
AzureIoTClient 0:e393db310d89 277 {
AzureIoTClient 0:e393db310d89 278 HTTPHeaders_Free(handleData->notificationHTTPrequestHeaders);
AzureIoTClient 0:e393db310d89 279 handleData->notificationHTTPrequestHeaders = NULL;
AzureIoTClient 0:e393db310d89 280 }
AzureIoTClient 0:e393db310d89 281
AzureIoTClient 0:e393db310d89 282 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_059: [Otherwise, IoTHubTransportHttp_Create shall create a set of HTTP headers (further on called "notification HTTP request headers") consisting of the following fixed field names and values:
AzureIoTClient 0:e393db310d89 283 "Authorization": " "]*/
AzureIoTClient 0:e393db310d89 284 static bool create_notificationHTTPrequestHeaders(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 0:e393db310d89 285 {
AzureIoTClient 0:e393db310d89 286 bool result;
AzureIoTClient 0:e393db310d89 287 (void)config;
AzureIoTClient 0:e393db310d89 288 handleData->notificationHTTPrequestHeaders = HTTPHeaders_Alloc();
AzureIoTClient 0:e393db310d89 289 if (handleData->notificationHTTPrequestHeaders == NULL)
AzureIoTClient 0:e393db310d89 290 {
AzureIoTClient 0:e393db310d89 291 result = false;
AzureIoTClient 0:e393db310d89 292 }
AzureIoTClient 0:e393db310d89 293 else
AzureIoTClient 0:e393db310d89 294 {
AzureIoTClient 0:e393db310d89 295 if (HTTPHeaders_AddHeaderNameValuePair(handleData->notificationHTTPrequestHeaders, "Authorization", " ") != HTTP_HEADERS_OK)
AzureIoTClient 0:e393db310d89 296 {
AzureIoTClient 0:e393db310d89 297 destroy_notificationHTTPrequestHeaders(handleData);
AzureIoTClient 0:e393db310d89 298 result = false;
AzureIoTClient 0:e393db310d89 299 }
AzureIoTClient 0:e393db310d89 300 else
AzureIoTClient 0:e393db310d89 301 {
AzureIoTClient 0:e393db310d89 302 result = true;
AzureIoTClient 0:e393db310d89 303 }
AzureIoTClient 0:e393db310d89 304 }
AzureIoTClient 0:e393db310d89 305 return result;
AzureIoTClient 0:e393db310d89 306 }
AzureIoTClient 0:e393db310d89 307
AzureIoTClient 0:e393db310d89 308 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_061: [Otherwise, IoTHubTransportHttp_Create shall create a STRING containing: "/devices/" + URL_ENCODED(device id) +"/messages/deviceBound/" called abandonHTTPrelativePathBegin.] */
AzureIoTClient 0:e393db310d89 309 static void destroy_abandonHTTPrelativePathBegin(HTTPTRANSPORT_HANDLE_DATA* handleData)
AzureIoTClient 0:e393db310d89 310 {
AzureIoTClient 0:e393db310d89 311 STRING_delete(handleData->abandonHTTPrelativePathBegin);
AzureIoTClient 0:e393db310d89 312 handleData->abandonHTTPrelativePathBegin = NULL;
AzureIoTClient 0:e393db310d89 313 }
AzureIoTClient 0:e393db310d89 314
AzureIoTClient 0:e393db310d89 315 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_061: [Otherwise, IoTHubTransportHttp_Create shall create a STRING containing: "/devices/" + URL_ENCODED(device id) +"/messages/deviceBound/" called abandonHTTPrelativePathBegin.] */
AzureIoTClient 0:e393db310d89 316 static bool create_abandonHTTPrelativePathBegin(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 0:e393db310d89 317 {
AzureIoTClient 0:e393db310d89 318 bool result;
AzureIoTClient 0:e393db310d89 319 handleData->abandonHTTPrelativePathBegin = STRING_construct("/devices/");
AzureIoTClient 0:e393db310d89 320 if (handleData->abandonHTTPrelativePathBegin == NULL)
AzureIoTClient 0:e393db310d89 321 {
AzureIoTClient 0:e393db310d89 322 result = false;
AzureIoTClient 0:e393db310d89 323 }
AzureIoTClient 0:e393db310d89 324 else
AzureIoTClient 0:e393db310d89 325 {
AzureIoTClient 0:e393db310d89 326 STRING_HANDLE urlEncodedDeviceId = NULL;
AzureIoTClient 0:e393db310d89 327 if (!(
AzureIoTClient 0:e393db310d89 328 ((urlEncodedDeviceId = URL_EncodeString(config->upperConfig->deviceId)) != NULL) &&
AzureIoTClient 0:e393db310d89 329 (STRING_concat_with_STRING(handleData->abandonHTTPrelativePathBegin, urlEncodedDeviceId) == 0) &&
AzureIoTClient 0:e393db310d89 330 (STRING_concat(handleData->abandonHTTPrelativePathBegin, NOTIFICATION_ENDPOINT_HTTP_ETAG) == 0)
AzureIoTClient 0:e393db310d89 331 ))
AzureIoTClient 0:e393db310d89 332 {
AzureIoTClient 0:e393db310d89 333 LogError("unable to STRING_concat\r\n");
AzureIoTClient 0:e393db310d89 334 STRING_delete(handleData->abandonHTTPrelativePathBegin);
AzureIoTClient 0:e393db310d89 335 result = false;
AzureIoTClient 0:e393db310d89 336 }
AzureIoTClient 0:e393db310d89 337 else
AzureIoTClient 0:e393db310d89 338 {
AzureIoTClient 0:e393db310d89 339 result = true;
AzureIoTClient 0:e393db310d89 340 }
AzureIoTClient 0:e393db310d89 341 STRING_delete(urlEncodedDeviceId);
AzureIoTClient 0:e393db310d89 342 }
AzureIoTClient 0:e393db310d89 343 return result;
AzureIoTClient 0:e393db310d89 344 }
AzureIoTClient 0:e393db310d89 345
AzureIoTClient 0:e393db310d89 346 static void destroy_SASObject(HTTPTRANSPORT_HANDLE_DATA* handleData)
AzureIoTClient 0:e393db310d89 347 {
AzureIoTClient 0:e393db310d89 348 HTTPAPIEX_SAS_Destroy(handleData->sasObject);
AzureIoTClient 0:e393db310d89 349 handleData->sasObject = NULL;
AzureIoTClient 0:e393db310d89 350 }
AzureIoTClient 0:e393db310d89 351
AzureIoTClient 0:e393db310d89 352 static bool create_deviceSASObject(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 0:e393db310d89 353 {
AzureIoTClient 0:e393db310d89 354 STRING_HANDLE keyName;
AzureIoTClient 0:e393db310d89 355 bool result;
AzureIoTClient 0:e393db310d89 356 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_001: [IoTHubTransportHttp_Create shall invoke URL_EncodeString with an argument of device id.]*/
AzureIoTClient 0:e393db310d89 357 keyName = URL_EncodeString(config->upperConfig->deviceId);
AzureIoTClient 0:e393db310d89 358 if (keyName == NULL)
AzureIoTClient 0:e393db310d89 359 {
AzureIoTClient 0:e393db310d89 360 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_002: [If the encode fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
AzureIoTClient 0:e393db310d89 361 result = false;
AzureIoTClient 0:e393db310d89 362 }
AzureIoTClient 0:e393db310d89 363 else
AzureIoTClient 0:e393db310d89 364 {
AzureIoTClient 0:e393db310d89 365 STRING_HANDLE uriResource;
AzureIoTClient 0:e393db310d89 366 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_003: [IoTHubTransportHttp_Create shall invoke STRING_clone using the previously created hostname.]*/
AzureIoTClient 0:e393db310d89 367 uriResource = STRING_clone(handleData->hostName);
AzureIoTClient 0:e393db310d89 368 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_004: [If the clone fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
AzureIoTClient 0:e393db310d89 369 if (uriResource != NULL)
AzureIoTClient 0:e393db310d89 370 {
AzureIoTClient 0:e393db310d89 371 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_005: [IoTHubTransportHttp_Create shall invoke STRING_concat with arguments uriResource and the string "/devices/".]*/
AzureIoTClient 0:e393db310d89 372 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_006: [If the concat fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
AzureIoTClient 0:e393db310d89 373 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_007: [IoTHubTransportHttp_Create shall invoke STRING_concat_with_STRING with arguments uriResource and keyName.]*/
AzureIoTClient 0:e393db310d89 374 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_008: [If the STRING_concat_with_STRING fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
AzureIoTClient 0:e393db310d89 375 if ((STRING_concat(uriResource, "/devices/") == 0) &&
AzureIoTClient 0:e393db310d89 376 (STRING_concat_with_STRING(uriResource, keyName) == 0))
AzureIoTClient 0:e393db310d89 377 {
AzureIoTClient 0:e393db310d89 378 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_009: [IoTHubTransportHttp_Create shall invoke STRING_construct with an argument of config->upperConfig->deviceKey.]*/
AzureIoTClient 0:e393db310d89 379 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_010: [If the STRING_construct fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
AzureIoTClient 0:e393db310d89 380 STRING_HANDLE key = STRING_construct(config->upperConfig->deviceKey);
AzureIoTClient 0:e393db310d89 381 if (key != NULL)
AzureIoTClient 0:e393db310d89 382 {
AzureIoTClient 0:e393db310d89 383 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_013: [The keyName is shortened to zero length, if that fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
AzureIoTClient 0:e393db310d89 384 if (STRING_empty(keyName) != 0)
AzureIoTClient 0:e393db310d89 385 {
AzureIoTClient 0:e393db310d89 386 LogError("Unable to form the device key name for the SAS\r\n");
AzureIoTClient 0:e393db310d89 387 result = false;
AzureIoTClient 0:e393db310d89 388 }
AzureIoTClient 0:e393db310d89 389 else
AzureIoTClient 0:e393db310d89 390 {
AzureIoTClient 0:e393db310d89 391 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_011: [IoTHubTransportHttp_Create shall invoke HTTPAPIEX_SAS_Create with arguments key, uriResource, and zero length keyName.]*/
AzureIoTClient 0:e393db310d89 392 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_012: [If the HTTPAPIEX_SAS_Create fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
AzureIoTClient 0:e393db310d89 393 handleData->sasObject = HTTPAPIEX_SAS_Create(key, uriResource, keyName);
AzureIoTClient 0:e393db310d89 394 result = (handleData->sasObject != NULL) ? (true) : (false);
AzureIoTClient 0:e393db310d89 395 }
AzureIoTClient 0:e393db310d89 396 STRING_delete(key);
AzureIoTClient 0:e393db310d89 397 }
AzureIoTClient 0:e393db310d89 398 else
AzureIoTClient 0:e393db310d89 399 {
AzureIoTClient 0:e393db310d89 400 result = false;
AzureIoTClient 0:e393db310d89 401 }
AzureIoTClient 0:e393db310d89 402 }
AzureIoTClient 0:e393db310d89 403 else
AzureIoTClient 0:e393db310d89 404 {
AzureIoTClient 0:e393db310d89 405 result = false;
AzureIoTClient 0:e393db310d89 406 }
AzureIoTClient 0:e393db310d89 407 STRING_delete(uriResource);
AzureIoTClient 0:e393db310d89 408 }
AzureIoTClient 0:e393db310d89 409 else
AzureIoTClient 0:e393db310d89 410 {
AzureIoTClient 0:e393db310d89 411 result = false;
AzureIoTClient 0:e393db310d89 412 }
AzureIoTClient 0:e393db310d89 413 STRING_delete(keyName);
AzureIoTClient 0:e393db310d89 414 }
AzureIoTClient 0:e393db310d89 415 return result;
AzureIoTClient 0:e393db310d89 416 }
AzureIoTClient 0:e393db310d89 417
AzureIoTClient 0:e393db310d89 418 TRANSPORT_HANDLE IoTHubTransportHttp_Create(const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 0:e393db310d89 419 {
AzureIoTClient 0:e393db310d89 420 HTTPTRANSPORT_HANDLE_DATA* result;
AzureIoTClient 0:e393db310d89 421 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_001: [If parameter config is NULL then IoTHubTransportHttp_Create shall fail and return NULL.] */
AzureIoTClient 0:e393db310d89 422 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_002: [IoTHubTransportHttp_Create shall fail and return NULL if any fields of the config structure are NULL.] */
AzureIoTClient 0:e393db310d89 423 if (config == NULL)
AzureIoTClient 0:e393db310d89 424 {
AzureIoTClient 0:e393db310d89 425 LogError("invalid arg (configuration is missing)\r\n");
AzureIoTClient 0:e393db310d89 426 result = NULL;
AzureIoTClient 0:e393db310d89 427 }
AzureIoTClient 0:e393db310d89 428 else if (config->upperConfig == NULL)
AzureIoTClient 0:e393db310d89 429 {
AzureIoTClient 0:e393db310d89 430 LogError("invalid arg (upperConfig is NULL)\r\n");
AzureIoTClient 0:e393db310d89 431 result = NULL;
AzureIoTClient 0:e393db310d89 432 }
AzureIoTClient 0:e393db310d89 433 else if (config->waitingToSend == NULL)
AzureIoTClient 0:e393db310d89 434 {
AzureIoTClient 0:e393db310d89 435 LogError("invalid arg (waitingToSend is NULL)\r\n");
AzureIoTClient 0:e393db310d89 436 result = NULL;
AzureIoTClient 0:e393db310d89 437 }
AzureIoTClient 0:e393db310d89 438 else if (config->upperConfig->protocol == NULL)
AzureIoTClient 0:e393db310d89 439 {
AzureIoTClient 0:e393db310d89 440 LogError("invalid arg (protocol is NULL)\r\n");
AzureIoTClient 0:e393db310d89 441 result = NULL;
AzureIoTClient 0:e393db310d89 442 }
AzureIoTClient 0:e393db310d89 443 else if (config->upperConfig->deviceId == NULL)
AzureIoTClient 0:e393db310d89 444 {
AzureIoTClient 0:e393db310d89 445 LogError("invalid arg (deviceId is NULL)\r\n");
AzureIoTClient 0:e393db310d89 446 result = NULL;
AzureIoTClient 0:e393db310d89 447 }
AzureIoTClient 0:e393db310d89 448 else if (config->upperConfig->iotHubName == NULL)
AzureIoTClient 0:e393db310d89 449 {
AzureIoTClient 0:e393db310d89 450 LogError("invalid arg (iotHubName is NULL)\r\n");
AzureIoTClient 0:e393db310d89 451 result = NULL;
AzureIoTClient 0:e393db310d89 452 }
AzureIoTClient 0:e393db310d89 453 else if (config->upperConfig->iotHubSuffix == NULL)
AzureIoTClient 0:e393db310d89 454 {
AzureIoTClient 0:e393db310d89 455 LogError("invalid arg (iotHubSuffix is NULL)\r\n");
AzureIoTClient 0:e393db310d89 456 result = NULL;
AzureIoTClient 0:e393db310d89 457 }
AzureIoTClient 0:e393db310d89 458 else if (config->upperConfig->deviceKey == NULL)
AzureIoTClient 0:e393db310d89 459 {
AzureIoTClient 0:e393db310d89 460 LogError("invalid arg (deviceKey is NULL)\r\n");
AzureIoTClient 0:e393db310d89 461 result = NULL;
AzureIoTClient 0:e393db310d89 462 }
AzureIoTClient 0:e393db310d89 463 else
AzureIoTClient 0:e393db310d89 464 {
AzureIoTClient 0:e393db310d89 465 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_003: [Otherwise IoTHubTransportHttp_Create shall create an immutable string (further called "event HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events?api-version=2015-08-15-preview".]*/
AzureIoTClient 0:e393db310d89 466 result = (HTTPTRANSPORT_HANDLE_DATA*)malloc(sizeof(HTTPTRANSPORT_HANDLE_DATA));
AzureIoTClient 0:e393db310d89 467 if (result == NULL)
AzureIoTClient 0:e393db310d89 468 {
AzureIoTClient 0:e393db310d89 469 LogError("unable to malloc\r\n");
AzureIoTClient 0:e393db310d89 470 }
AzureIoTClient 0:e393db310d89 471 else
AzureIoTClient 0:e393db310d89 472 {
AzureIoTClient 0:e393db310d89 473 bool was_eventHTTPrelativePath_ok = create_eventHTTPrelativePath(result, config);
AzureIoTClient 0:e393db310d89 474 bool was_notificationHTTPrelativePath_ok = was_eventHTTPrelativePath_ok && create_notificationHTTPrelativePath(result, config);
AzureIoTClient 0:e393db310d89 475 bool was_eventHTTPrequestHeaders_ok = was_notificationHTTPrelativePath_ok && create_eventHTTPrequestHeaders(result, config);
AzureIoTClient 0:e393db310d89 476 bool was_hostName_ok = was_eventHTTPrequestHeaders_ok && create_hostName(result, config);
AzureIoTClient 0:e393db310d89 477 bool was_httpApiExHandle_ok = was_hostName_ok && create_httpApiExHandle(result, config);
AzureIoTClient 0:e393db310d89 478 bool was_notificationHTTPrequestHeaders_ok = was_httpApiExHandle_ok && create_notificationHTTPrequestHeaders(result, config);
AzureIoTClient 0:e393db310d89 479 bool was_abandonHTTPrelativePathBegin_ok = was_notificationHTTPrequestHeaders_ok && create_abandonHTTPrelativePathBegin(result, config);
AzureIoTClient 0:e393db310d89 480 bool was_sasObject_ok = was_abandonHTTPrelativePathBegin_ok && create_deviceSASObject(result, config);
AzureIoTClient 0:e393db310d89 481
AzureIoTClient 0:e393db310d89 482 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_011: [Otherwise, IoTHubTransportHttp_Create shall set a flag called "DoWork_PullNotification" to false, succeed and return a non-NULL value.]*/
AzureIoTClient 0:e393db310d89 483 if (was_sasObject_ok)
AzureIoTClient 0:e393db310d89 484 {
AzureIoTClient 0:e393db310d89 485 result->DoWork_PullNotification = false;
AzureIoTClient 0:e393db310d89 486 result->doBatchedTransfers = false;
AzureIoTClient 0:e393db310d89 487 result->isFirstPoll = true;
AzureIoTClient 0:e393db310d89 488 result->getMinimumPollingTime = DEFAULT_GETMINIMUMPOLLINGTIME;
AzureIoTClient 0:e393db310d89 489 result->waitingToSend = config->waitingToSend;
AzureIoTClient 0:e393db310d89 490 DList_InitializeListHead(&(result->eventConfirmations));
AzureIoTClient 0:e393db310d89 491 }
AzureIoTClient 0:e393db310d89 492 else
AzureIoTClient 0:e393db310d89 493 {
AzureIoTClient 0:e393db310d89 494 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_004: [If creating the string fail for any reason then IoTHubTransportHttp_Create shall fail and return NULL.] */
AzureIoTClient 0:e393db310d89 495 if (was_eventHTTPrelativePath_ok) destroy_eventHTTPrelativePath(result);
AzureIoTClient 0:e393db310d89 496 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_035: [If creating the notification HTTP relative path fails, then IoTHubTransportHttp_Create shall fail and return NULL.] */
AzureIoTClient 0:e393db310d89 497 if (was_notificationHTTPrelativePath_ok) destroy_notificationHTTPrelativePath(result);
AzureIoTClient 0:e393db310d89 498 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_006: [If creating the event HTTP request headers fails, then IoTHubTransportHttp_Create shall fail and return NULL.] */
AzureIoTClient 0:e393db310d89 499 if (was_eventHTTPrequestHeaders_ok) destroy_eventHTTPrequestHeaders(result);
AzureIoTClient 0:e393db310d89 500 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_008: [If creating the hostname fails then IoTHubTransportHttp_Create shall fail and return NULL.] */
AzureIoTClient 0:e393db310d89 501 if (was_hostName_ok) destroy_hostName(result);
AzureIoTClient 0:e393db310d89 502 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_010: [If creating the HTTPAPIEX_HANDLE fails then IoTHubTransportHttp_Create shall fail and return NULL.] */
AzureIoTClient 0:e393db310d89 503 if (was_httpApiExHandle_ok) destroy_httpApiExHandle(result);
AzureIoTClient 0:e393db310d89 504 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_060: [If creating notification HTTP request headers then IoTHubTransportHttp_Create shall fail and return NULL.]*/
AzureIoTClient 0:e393db310d89 505 if (was_notificationHTTPrequestHeaders_ok) destroy_notificationHTTPrequestHeaders(result);
AzureIoTClient 0:e393db310d89 506 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_062: [If creating the abandonHTTPrelativePathBegin fails then IoTHubTransportHttp_Create shall fail and return NULL] */
AzureIoTClient 0:e393db310d89 507 if (was_abandonHTTPrelativePathBegin_ok) destroy_abandonHTTPrelativePathBegin(result);
AzureIoTClient 0:e393db310d89 508
AzureIoTClient 0:e393db310d89 509 free(result);
AzureIoTClient 0:e393db310d89 510 result = NULL;
AzureIoTClient 0:e393db310d89 511 }
AzureIoTClient 0:e393db310d89 512 }
AzureIoTClient 0:e393db310d89 513 }
AzureIoTClient 0:e393db310d89 514 return result;
AzureIoTClient 0:e393db310d89 515 }
AzureIoTClient 0:e393db310d89 516
AzureIoTClient 0:e393db310d89 517 void IoTHubTransportHttp_Destroy(TRANSPORT_HANDLE handle)
AzureIoTClient 0:e393db310d89 518 {
AzureIoTClient 0:e393db310d89 519 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_012: [IoTHubTransportHttp_Destroy shall do nothing if parameter handle is NULL.]*/
AzureIoTClient 0:e393db310d89 520 if (handle != NULL)
AzureIoTClient 0:e393db310d89 521 {
AzureIoTClient 0:e393db310d89 522 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_013: [Otherwise IoTHubTransportHttp_Destroy shall free all the resources currently in use.] */
AzureIoTClient 0:e393db310d89 523 destroy_eventHTTPrelativePath(handle);
AzureIoTClient 0:e393db310d89 524 destroy_notificationHTTPrelativePath(handle);
AzureIoTClient 0:e393db310d89 525 destroy_eventHTTPrequestHeaders(handle);
AzureIoTClient 0:e393db310d89 526 destroy_hostName(handle);
AzureIoTClient 0:e393db310d89 527 destroy_httpApiExHandle(handle);
AzureIoTClient 0:e393db310d89 528 destroy_notificationHTTPrequestHeaders(handle);
AzureIoTClient 0:e393db310d89 529 destroy_abandonHTTPrelativePathBegin(handle);
AzureIoTClient 0:e393db310d89 530 destroy_SASObject(handle);
AzureIoTClient 0:e393db310d89 531 free(handle);
AzureIoTClient 0:e393db310d89 532 }
AzureIoTClient 0:e393db310d89 533 }
AzureIoTClient 0:e393db310d89 534
AzureIoTClient 0:e393db310d89 535 int IoTHubTransportHttp_Subscribe(TRANSPORT_HANDLE handle)
AzureIoTClient 0:e393db310d89 536 {
AzureIoTClient 0:e393db310d89 537 int result;
AzureIoTClient 0:e393db310d89 538 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_014: [If parameter handle is NULL then IoTHubTransportHttp_Subscribe shall fail and return a non-zero value.] */
AzureIoTClient 0:e393db310d89 539 if (handle == NULL)
AzureIoTClient 0:e393db310d89 540 {
AzureIoTClient 0:e393db310d89 541 LogError("invalid arg passed to IoTHubTransportHttp_Subscribe\r\n");
AzureIoTClient 0:e393db310d89 542 result = __LINE__;
AzureIoTClient 0:e393db310d89 543 }
AzureIoTClient 0:e393db310d89 544 else
AzureIoTClient 0:e393db310d89 545 {
AzureIoTClient 0:e393db310d89 546 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_056: [Otherwise, IoTHubTransportHttp_Subscribe shall set the flag called DoWork_PullNotifications to true and succeed.] */
AzureIoTClient 0:e393db310d89 547 HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 0:e393db310d89 548 handleData->DoWork_PullNotification = true;
AzureIoTClient 0:e393db310d89 549 result = 0;
AzureIoTClient 0:e393db310d89 550 }
AzureIoTClient 0:e393db310d89 551 return result;
AzureIoTClient 0:e393db310d89 552 }
AzureIoTClient 0:e393db310d89 553
AzureIoTClient 0:e393db310d89 554 void IoTHubTransportHttp_Unsubscribe(TRANSPORT_HANDLE handle)
AzureIoTClient 0:e393db310d89 555 {
AzureIoTClient 0:e393db310d89 556 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_016: [If parameter handle is NULL then IoTHubTransportHttp_Unsubscribe shall do nothing.] */
AzureIoTClient 0:e393db310d89 557 if (handle != NULL)
AzureIoTClient 0:e393db310d89 558 {
AzureIoTClient 0:e393db310d89 559 HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 0:e393db310d89 560 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_058: [Otherwise it shall set the flag DoWork_PullNotification to false.] */
AzureIoTClient 0:e393db310d89 561 handleData->DoWork_PullNotification = false;
AzureIoTClient 0:e393db310d89 562 }
AzureIoTClient 0:e393db310d89 563 }
AzureIoTClient 0:e393db310d89 564
AzureIoTClient 0:e393db310d89 565 /*produces a representation of the properties, if they exist*/
AzureIoTClient 0:e393db310d89 566 /*if they do not exist, produces ""*/
AzureIoTClient 0:e393db310d89 567 static int concat_Properties(STRING_HANDLE existing, MAP_HANDLE map)
AzureIoTClient 0:e393db310d89 568 {
AzureIoTClient 0:e393db310d89 569 int result;
AzureIoTClient 0:e393db310d89 570 const char*const* keys;
AzureIoTClient 0:e393db310d89 571 const char*const* values;
AzureIoTClient 0:e393db310d89 572 size_t count;
AzureIoTClient 0:e393db310d89 573 if (Map_GetInternals(map, &keys, &values, &count) != MAP_OK)
AzureIoTClient 0:e393db310d89 574 {
AzureIoTClient 0:e393db310d89 575 result = __LINE__;
AzureIoTClient 0:e393db310d89 576 LogError("error while Map_GetInternals\r\n");
AzureIoTClient 0:e393db310d89 577 }
AzureIoTClient 0:e393db310d89 578 else
AzureIoTClient 0:e393db310d89 579 {
AzureIoTClient 0:e393db310d89 580
AzureIoTClient 0:e393db310d89 581 if (count == 0)
AzureIoTClient 0:e393db310d89 582 {
AzureIoTClient 0:e393db310d89 583 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_076: [If IoTHubMessage does not have properties, then "properties":{...} shall be missing from the payload*/
AzureIoTClient 0:e393db310d89 584 /*no properties - do nothing with existing*/
AzureIoTClient 0:e393db310d89 585 result = 0;
AzureIoTClient 0:e393db310d89 586 }
AzureIoTClient 0:e393db310d89 587 else
AzureIoTClient 0:e393db310d89 588 {
AzureIoTClient 0:e393db310d89 589 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_075: [If IoTHubMessage has properties, then they shall be serialized at the same level as "body" using the following pattern: "properties":{"iothub-app-name1":"value1","iothub-app-name2":"value2*/
AzureIoTClient 0:e393db310d89 590 if (STRING_concat(existing, ",\"properties\":") != 0)
AzureIoTClient 0:e393db310d89 591 {
AzureIoTClient 0:e393db310d89 592 /*go ahead and return it*/
AzureIoTClient 0:e393db310d89 593 result = __LINE__;
AzureIoTClient 0:e393db310d89 594 LogError("failed STRING_concat\r\n");
AzureIoTClient 0:e393db310d89 595 }
AzureIoTClient 0:e393db310d89 596 else if (appendMapToJSON(existing, keys, values, count) != 0)
AzureIoTClient 0:e393db310d89 597 {
AzureIoTClient 0:e393db310d89 598 result = __LINE__;
AzureIoTClient 0:e393db310d89 599 LogError("unable to append the properties\r\n");
AzureIoTClient 0:e393db310d89 600 }
AzureIoTClient 0:e393db310d89 601 else
AzureIoTClient 0:e393db310d89 602 {
AzureIoTClient 0:e393db310d89 603 /*all is fine*/
AzureIoTClient 0:e393db310d89 604 result = 0;
AzureIoTClient 0:e393db310d89 605 }
AzureIoTClient 0:e393db310d89 606 }
AzureIoTClient 0:e393db310d89 607 }
AzureIoTClient 0:e393db310d89 608 return result;
AzureIoTClient 0:e393db310d89 609 }
AzureIoTClient 0:e393db310d89 610
AzureIoTClient 0:e393db310d89 611 /*produces a JSON representation of the map : {"a": "value_of_a","b":"value_of_b"}*/
AzureIoTClient 0:e393db310d89 612 static int appendMapToJSON(STRING_HANDLE existing, const char* const* keys, const char* const* values, size_t count) /*under consideration: move to MAP module when it has more than 1 user*/
AzureIoTClient 0:e393db310d89 613 {
AzureIoTClient 0:e393db310d89 614 int result;
AzureIoTClient 0:e393db310d89 615 if (STRING_concat(existing, "{") != 0)
AzureIoTClient 0:e393db310d89 616 {
AzureIoTClient 0:e393db310d89 617 /*go on and return it*/
AzureIoTClient 0:e393db310d89 618 result = __LINE__;
AzureIoTClient 0:e393db310d89 619 LogError("STRING_construct failed\r\n");
AzureIoTClient 0:e393db310d89 620 }
AzureIoTClient 0:e393db310d89 621 else
AzureIoTClient 0:e393db310d89 622 {
AzureIoTClient 0:e393db310d89 623 size_t i;
AzureIoTClient 0:e393db310d89 624 for (i = 0; i < count; i++)
AzureIoTClient 0:e393db310d89 625 {
AzureIoTClient 0:e393db310d89 626 if (!(
AzureIoTClient 0:e393db310d89 627 (STRING_concat(existing, (i == 0) ? "\"" IOTHUB_APP_PREFIX : ",\"" IOTHUB_APP_PREFIX) == 0) &&
AzureIoTClient 0:e393db310d89 628 (STRING_concat(existing, keys[i]) == 0) &&
AzureIoTClient 0:e393db310d89 629 (STRING_concat(existing, "\":\"") == 0) &&
AzureIoTClient 0:e393db310d89 630 (STRING_concat(existing, values[i]) == 0) &&
AzureIoTClient 0:e393db310d89 631 (STRING_concat(existing, "\"") == 0)
AzureIoTClient 0:e393db310d89 632 ))
AzureIoTClient 0:e393db310d89 633 {
AzureIoTClient 0:e393db310d89 634 LogError("unable to STRING_concat\r\n");
AzureIoTClient 0:e393db310d89 635 break;
AzureIoTClient 0:e393db310d89 636 }
AzureIoTClient 0:e393db310d89 637 }
AzureIoTClient 0:e393db310d89 638
AzureIoTClient 0:e393db310d89 639 if (i < count)
AzureIoTClient 0:e393db310d89 640 {
AzureIoTClient 0:e393db310d89 641 result = __LINE__;
AzureIoTClient 0:e393db310d89 642 /*error, let it go through*/
AzureIoTClient 0:e393db310d89 643 }
AzureIoTClient 0:e393db310d89 644 else if (STRING_concat(existing, "}") != 0)
AzureIoTClient 0:e393db310d89 645 {
AzureIoTClient 0:e393db310d89 646 result = __LINE__;
AzureIoTClient 0:e393db310d89 647 LogError("unable to STRING_concat\r\n");
AzureIoTClient 0:e393db310d89 648 }
AzureIoTClient 0:e393db310d89 649 else
AzureIoTClient 0:e393db310d89 650 {
AzureIoTClient 0:e393db310d89 651 /*all is fine*/
AzureIoTClient 0:e393db310d89 652 result = 0;
AzureIoTClient 0:e393db310d89 653 }
AzureIoTClient 0:e393db310d89 654 }
AzureIoTClient 0:e393db310d89 655 return result;
AzureIoTClient 0:e393db310d89 656 }
AzureIoTClient 0:e393db310d89 657
AzureIoTClient 0:e393db310d89 658 /*makes the following string:{"body":"base64 encoding of the message content"[,"properties":{"a":"valueOfA"}]}*/
AzureIoTClient 0:e393db310d89 659 /*return NULL if there was a failure, or a non-NULL STRING_HANDLE that contains the intended data*/
AzureIoTClient 0:e393db310d89 660 static STRING_HANDLE make1EventJSONitem(PDLIST_ENTRY item)
AzureIoTClient 0:e393db310d89 661 {
AzureIoTClient 0:e393db310d89 662 STRING_HANDLE result; /*temp wants to contain :{"body":"base64 encoding of the message content"[,"properties":{"a":"valueOfA"}]}*/
AzureIoTClient 0:e393db310d89 663 IOTHUB_MESSAGE_LIST* message = containingRecord(item, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 0:e393db310d89 664 IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message->messageHandle);
AzureIoTClient 0:e393db310d89 665 switch (contentType)
AzureIoTClient 0:e393db310d89 666 {
AzureIoTClient 0:e393db310d89 667 case IOTHUBMESSAGE_BYTEARRAY:
AzureIoTClient 0:e393db310d89 668 {
AzureIoTClient 0:e393db310d89 669 result = STRING_construct("{\"body\":\"");
AzureIoTClient 0:e393db310d89 670 if (result == NULL)
AzureIoTClient 0:e393db310d89 671 {
AzureIoTClient 0:e393db310d89 672 LogError("unable to STRING_construct\r\n");
AzureIoTClient 0:e393db310d89 673 }
AzureIoTClient 0:e393db310d89 674 else
AzureIoTClient 0:e393db310d89 675 {
AzureIoTClient 0:e393db310d89 676 const unsigned char* source;
AzureIoTClient 0:e393db310d89 677 size_t size;
AzureIoTClient 0:e393db310d89 678
AzureIoTClient 0:e393db310d89 679 if (IoTHubMessage_GetByteArray(message->messageHandle, &source, &size) != IOTHUB_MESSAGE_OK)
AzureIoTClient 0:e393db310d89 680 {
AzureIoTClient 0:e393db310d89 681 LogError("unable to get the data for the message.\r\n");
AzureIoTClient 0:e393db310d89 682 STRING_delete(result);
AzureIoTClient 0:e393db310d89 683 result = NULL;
AzureIoTClient 0:e393db310d89 684 }
AzureIoTClient 0:e393db310d89 685 else
AzureIoTClient 0:e393db310d89 686 {
AzureIoTClient 0:e393db310d89 687 STRING_HANDLE encoded = Base64_Encode_Bytes(source, size);
AzureIoTClient 0:e393db310d89 688 if (encoded == NULL)
AzureIoTClient 0:e393db310d89 689 {
AzureIoTClient 0:e393db310d89 690 LogError("unable to Base64_Encode_Bytes.\r\n");
AzureIoTClient 0:e393db310d89 691 STRING_delete(result);
AzureIoTClient 0:e393db310d89 692 result = NULL;
AzureIoTClient 0:e393db310d89 693 }
AzureIoTClient 0:e393db310d89 694 else
AzureIoTClient 0:e393db310d89 695 {
AzureIoTClient 0:e393db310d89 696 if (!(
AzureIoTClient 0:e393db310d89 697 (STRING_concat_with_STRING(result, encoded) == 0) &&
AzureIoTClient 0:e393db310d89 698 (STRING_concat(result, "\"") == 0) && /*\" because closing value*/
AzureIoTClient 0:e393db310d89 699 (concat_Properties(result, IoTHubMessage_Properties(message->messageHandle)) == 0) &&
AzureIoTClient 0:e393db310d89 700 (STRING_concat(result, "},") == 0) /*the last comma shall be replaced by a ']' by DaCr's suggestion (which is awesome enough to receive credits in the source code)*/
AzureIoTClient 0:e393db310d89 701 ))
AzureIoTClient 0:e393db310d89 702 {
AzureIoTClient 0:e393db310d89 703 STRING_delete(result);
AzureIoTClient 0:e393db310d89 704 result = NULL;
AzureIoTClient 0:e393db310d89 705 LogError("unable to STRING_concat_with_STRING.\r\n");
AzureIoTClient 0:e393db310d89 706 }
AzureIoTClient 0:e393db310d89 707 else
AzureIoTClient 0:e393db310d89 708 {
AzureIoTClient 0:e393db310d89 709 /*all is fine... */
AzureIoTClient 0:e393db310d89 710 }
AzureIoTClient 0:e393db310d89 711 STRING_delete(encoded);
AzureIoTClient 0:e393db310d89 712 }
AzureIoTClient 0:e393db310d89 713 }
AzureIoTClient 0:e393db310d89 714 }
AzureIoTClient 0:e393db310d89 715 break;
AzureIoTClient 0:e393db310d89 716 }
AzureIoTClient 0:e393db310d89 717 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_113: [If a messages to be send has type IOTHUBMESSAGE_STRING, then its serialization shall be {"body":"JSON encoding of the string", "base64Encoded":false}] */
AzureIoTClient 0:e393db310d89 718 case IOTHUBMESSAGE_STRING:
AzureIoTClient 0:e393db310d89 719 {
AzureIoTClient 0:e393db310d89 720 result = STRING_construct("{\"body\":");
AzureIoTClient 0:e393db310d89 721 if (result == NULL)
AzureIoTClient 0:e393db310d89 722 {
AzureIoTClient 0:e393db310d89 723 LogError("unable to STRING_construct\r\n");
AzureIoTClient 0:e393db310d89 724 }
AzureIoTClient 0:e393db310d89 725 else
AzureIoTClient 0:e393db310d89 726 {
AzureIoTClient 0:e393db310d89 727 const char* source = IoTHubMessage_GetString(message->messageHandle);
AzureIoTClient 0:e393db310d89 728 if (source == NULL)
AzureIoTClient 0:e393db310d89 729 {
AzureIoTClient 0:e393db310d89 730 LogError("unable to IoTHubMessage_GetString\r\n");
AzureIoTClient 0:e393db310d89 731 STRING_delete(result);
AzureIoTClient 0:e393db310d89 732 result = NULL;
AzureIoTClient 0:e393db310d89 733 }
AzureIoTClient 0:e393db310d89 734 else
AzureIoTClient 0:e393db310d89 735 {
AzureIoTClient 0:e393db310d89 736 STRING_HANDLE asJson = STRING_new_JSON(source);
AzureIoTClient 0:e393db310d89 737 if (asJson == NULL)
AzureIoTClient 0:e393db310d89 738 {
AzureIoTClient 0:e393db310d89 739 LogError("unable to STRING_new_JSON\r\n");
AzureIoTClient 0:e393db310d89 740 STRING_delete(result);
AzureIoTClient 0:e393db310d89 741 result = NULL;
AzureIoTClient 0:e393db310d89 742 }
AzureIoTClient 0:e393db310d89 743 else
AzureIoTClient 0:e393db310d89 744 {
AzureIoTClient 0:e393db310d89 745 if (!(
AzureIoTClient 0:e393db310d89 746 (STRING_concat_with_STRING(result, asJson) == 0) &&
AzureIoTClient 0:e393db310d89 747 (STRING_concat(result, ",\"base64Encoded\":false") == 0) &&
AzureIoTClient 0:e393db310d89 748 (concat_Properties(result, IoTHubMessage_Properties(message->messageHandle)) == 0) &&
AzureIoTClient 0:e393db310d89 749 (STRING_concat(result, "},") == 0) /*the last comma shall be replaced by a ']' by DaCr's suggestion (which is awesome enough to receive credits in the source code)*/
AzureIoTClient 0:e393db310d89 750 ))
AzureIoTClient 0:e393db310d89 751 {
AzureIoTClient 0:e393db310d89 752 LogError("unable to STRING_concat_with_STRING");
AzureIoTClient 0:e393db310d89 753 STRING_delete(result);
AzureIoTClient 0:e393db310d89 754 result = NULL;
AzureIoTClient 0:e393db310d89 755 }
AzureIoTClient 0:e393db310d89 756 else
AzureIoTClient 0:e393db310d89 757 {
AzureIoTClient 0:e393db310d89 758 /*result has the intended content*/
AzureIoTClient 0:e393db310d89 759 }
AzureIoTClient 0:e393db310d89 760 STRING_delete(asJson);
AzureIoTClient 0:e393db310d89 761 }
AzureIoTClient 0:e393db310d89 762 }
AzureIoTClient 0:e393db310d89 763 }
AzureIoTClient 0:e393db310d89 764 break;
AzureIoTClient 0:e393db310d89 765 }
AzureIoTClient 0:e393db310d89 766 default:
AzureIoTClient 0:e393db310d89 767 {
AzureIoTClient 0:e393db310d89 768 LogError("an unknown message type was encountered (%d)\r\n", contentType);
AzureIoTClient 0:e393db310d89 769 result = NULL; /*unknown message type*/
AzureIoTClient 0:e393db310d89 770 break;
AzureIoTClient 0:e393db310d89 771 }
AzureIoTClient 0:e393db310d89 772 }
AzureIoTClient 0:e393db310d89 773 return result;
AzureIoTClient 0:e393db310d89 774 }
AzureIoTClient 0:e393db310d89 775
AzureIoTClient 0:e393db310d89 776 #define MAKE_PAYLOAD_RESULT_VALUES \
AzureIoTClient 0:e393db310d89 777 MAKE_PAYLOAD_OK, /*returned when there is a payload to be later send by HTTP*/ \
AzureIoTClient 0:e393db310d89 778 MAKE_PAYLOAD_NO_ITEMS, /*returned when there are no items to be send*/ \
AzureIoTClient 0:e393db310d89 779 MAKE_PAYLOAD_ERROR, /*returned when there were errors*/ \
AzureIoTClient 0:e393db310d89 780 MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT /*returned when the first item doesn't fit*/
AzureIoTClient 0:e393db310d89 781
AzureIoTClient 0:e393db310d89 782 DEFINE_ENUM(MAKE_PAYLOAD_RESULT, MAKE_PAYLOAD_RESULT_VALUES);
AzureIoTClient 0:e393db310d89 783
AzureIoTClient 0:e393db310d89 784 /*this function assembles several {"body":"base64 encoding of the message content"," base64Encoded": true} into 1 payload*/
AzureIoTClient 0:e393db310d89 785 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_070: [IoTHubTransportHttp_DoWork shall build the following string:[{"body":"base64 encoding of the message1 content"},{"body":"base64 encoding of the message2 content"}...]]*/
AzureIoTClient 0:e393db310d89 786 static MAKE_PAYLOAD_RESULT makePayload(HTTPTRANSPORT_HANDLE_DATA* handleData, STRING_HANDLE* payload)
AzureIoTClient 0:e393db310d89 787 {
AzureIoTClient 0:e393db310d89 788 MAKE_PAYLOAD_RESULT result;
AzureIoTClient 0:e393db310d89 789
AzureIoTClient 0:e393db310d89 790 *payload = STRING_construct("[");
AzureIoTClient 0:e393db310d89 791 if (*payload == NULL)
AzureIoTClient 0:e393db310d89 792 {
AzureIoTClient 0:e393db310d89 793 LogError("unable to STRING_construct\r\n");
AzureIoTClient 0:e393db310d89 794 result = MAKE_PAYLOAD_ERROR;
AzureIoTClient 0:e393db310d89 795 }
AzureIoTClient 0:e393db310d89 796 else
AzureIoTClient 0:e393db310d89 797 {
AzureIoTClient 0:e393db310d89 798 bool isFirst = true;
AzureIoTClient 0:e393db310d89 799 PDLIST_ENTRY actual;
AzureIoTClient 0:e393db310d89 800 /*either all the items enter the list or only some*/
AzureIoTClient 0:e393db310d89 801 result = MAKE_PAYLOAD_OK; /*optimistically initializing it*/
AzureIoTClient 0:e393db310d89 802 bool keepGoing = true; /*keepGoing gets sometimes to false from within the loop*/
AzureIoTClient 0:e393db310d89 803 while (keepGoing && ((actual = handleData->waitingToSend->Flink) != handleData->waitingToSend))
AzureIoTClient 0:e393db310d89 804 {
AzureIoTClient 0:e393db310d89 805 STRING_HANDLE temp = make1EventJSONitem(actual);
AzureIoTClient 0:e393db310d89 806 if (isFirst)
AzureIoTClient 0:e393db310d89 807 {
AzureIoTClient 0:e393db310d89 808 isFirst = false;
AzureIoTClient 0:e393db310d89 809 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_073: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/
AzureIoTClient 0:e393db310d89 810 if (temp == NULL) /*first item failed to create, nothing to send*/
AzureIoTClient 0:e393db310d89 811 {
AzureIoTClient 0:e393db310d89 812 result = MAKE_PAYLOAD_ERROR;
AzureIoTClient 0:e393db310d89 813 STRING_delete(*payload);
AzureIoTClient 0:e393db310d89 814 *payload = NULL;
AzureIoTClient 0:e393db310d89 815 keepGoing = false;
AzureIoTClient 0:e393db310d89 816 }
AzureIoTClient 0:e393db310d89 817 else
AzureIoTClient 0:e393db310d89 818 {
AzureIoTClient 0:e393db310d89 819 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_071: [If the oldest message in waitingToSend causes the message to exceed 256K bytes then it shall be removed from waitingToSend, and IoTHubClient_LL_SendComplete shall be called. Parameter PDLIST_ENTRY completed shall point to a list containing only the oldest item, and parameter IOTHUB_BATCHSTATE result shall be set to IOTHUB_BATCHSTATE_FAILED.] */
AzureIoTClient 0:e393db310d89 820 if (STRING_length(temp) >= 256 * 1024)
AzureIoTClient 0:e393db310d89 821 {
AzureIoTClient 0:e393db310d89 822 PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/
AzureIoTClient 0:e393db310d89 823 DList_InsertTailList(&(handleData->eventConfirmations), head);
AzureIoTClient 0:e393db310d89 824 result = MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT;
AzureIoTClient 0:e393db310d89 825 STRING_delete(*payload);
AzureIoTClient 0:e393db310d89 826 *payload = NULL;
AzureIoTClient 0:e393db310d89 827 keepGoing = false;
AzureIoTClient 0:e393db310d89 828 }
AzureIoTClient 0:e393db310d89 829 else
AzureIoTClient 0:e393db310d89 830 {
AzureIoTClient 0:e393db310d89 831 if (STRING_concat_with_STRING(*payload, temp) != 0)
AzureIoTClient 0:e393db310d89 832 {
AzureIoTClient 0:e393db310d89 833 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_073: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/
AzureIoTClient 0:e393db310d89 834 result = MAKE_PAYLOAD_ERROR;
AzureIoTClient 0:e393db310d89 835 STRING_delete(*payload);
AzureIoTClient 0:e393db310d89 836 *payload = NULL;
AzureIoTClient 0:e393db310d89 837 keepGoing = false;
AzureIoTClient 0:e393db310d89 838 }
AzureIoTClient 0:e393db310d89 839 else
AzureIoTClient 0:e393db310d89 840 {
AzureIoTClient 0:e393db310d89 841 /*first item was put nicely in the payload*/
AzureIoTClient 0:e393db310d89 842 PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/
AzureIoTClient 0:e393db310d89 843 DList_InsertTailList(&(handleData->eventConfirmations), head);
AzureIoTClient 0:e393db310d89 844 }
AzureIoTClient 0:e393db310d89 845 }
AzureIoTClient 0:e393db310d89 846 STRING_delete(temp);
AzureIoTClient 0:e393db310d89 847 }
AzureIoTClient 0:e393db310d89 848 }
AzureIoTClient 0:e393db310d89 849 else
AzureIoTClient 0:e393db310d89 850 {
AzureIoTClient 0:e393db310d89 851 /*there is at least 1 item already in the payload*/
AzureIoTClient 0:e393db310d89 852 if (temp == NULL)
AzureIoTClient 0:e393db310d89 853 {
AzureIoTClient 0:e393db310d89 854 /*there are multiple payloads encoded, the last one had an internal error, just go with those - closing the payload happens "after the loop"*/
AzureIoTClient 0:e393db310d89 855 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_072: [If at any point during construction of the string there are errors, IoTHubTransportHttp_DoWork shall use the so far constructed string as payload.]*/
AzureIoTClient 0:e393db310d89 856 result = MAKE_PAYLOAD_OK;
AzureIoTClient 0:e393db310d89 857 keepGoing = false;
AzureIoTClient 0:e393db310d89 858 }
AzureIoTClient 0:e393db310d89 859 else
AzureIoTClient 0:e393db310d89 860 {
AzureIoTClient 0:e393db310d89 861 if (STRING_length(*payload) + STRING_length(temp) > 256 * 1024)
AzureIoTClient 0:e393db310d89 862 {
AzureIoTClient 0:e393db310d89 863 /*this item doesn't make it to the payload, but the payload is valid so far*/
AzureIoTClient 0:e393db310d89 864 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_072: [If at any point during construction of the string there are errors, IoTHubTransportHttp_DoWork shall use the so far constructed string as payload.]*/
AzureIoTClient 0:e393db310d89 865 result = MAKE_PAYLOAD_OK;
AzureIoTClient 0:e393db310d89 866 keepGoing = false;
AzureIoTClient 0:e393db310d89 867 }
AzureIoTClient 0:e393db310d89 868 else if (STRING_concat_with_STRING(*payload, temp) != 0)
AzureIoTClient 0:e393db310d89 869 {
AzureIoTClient 0:e393db310d89 870 /*should still send what there is so far...*/
AzureIoTClient 0:e393db310d89 871 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_072: [If at any point during construction of the string there are errors, IoTHubTransportHttp_DoWork shall use the so far constructed string as payload.]*/
AzureIoTClient 0:e393db310d89 872 result = MAKE_PAYLOAD_OK;
AzureIoTClient 0:e393db310d89 873 keepGoing = false;
AzureIoTClient 0:e393db310d89 874 }
AzureIoTClient 0:e393db310d89 875 else
AzureIoTClient 0:e393db310d89 876 {
AzureIoTClient 0:e393db310d89 877 /*cool, the payload made it there, let's continue... */
AzureIoTClient 0:e393db310d89 878 PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/
AzureIoTClient 0:e393db310d89 879 DList_InsertTailList(&(handleData->eventConfirmations), head);
AzureIoTClient 0:e393db310d89 880 }
AzureIoTClient 0:e393db310d89 881 STRING_delete(temp);
AzureIoTClient 0:e393db310d89 882 }
AzureIoTClient 0:e393db310d89 883 }
AzureIoTClient 0:e393db310d89 884 }
AzureIoTClient 0:e393db310d89 885
AzureIoTClient 0:e393db310d89 886 /*closing the payload*/
AzureIoTClient 0:e393db310d89 887 if (result == MAKE_PAYLOAD_OK)
AzureIoTClient 0:e393db310d89 888 {
AzureIoTClient 0:e393db310d89 889 ((char*)STRING_c_str(*payload))[STRING_length(*payload) - 1] = ']'; /*TODO - do this in STRING_HANDLE*/
AzureIoTClient 0:e393db310d89 890 }
AzureIoTClient 0:e393db310d89 891 else
AzureIoTClient 0:e393db310d89 892 {
AzureIoTClient 0:e393db310d89 893 /*no need to close anything*/
AzureIoTClient 0:e393db310d89 894 }
AzureIoTClient 0:e393db310d89 895 }
AzureIoTClient 0:e393db310d89 896 return result;
AzureIoTClient 0:e393db310d89 897 }
AzureIoTClient 0:e393db310d89 898
AzureIoTClient 0:e393db310d89 899 static void reversePutListBackIn(PDLIST_ENTRY source, PDLIST_ENTRY destination)
AzureIoTClient 0:e393db310d89 900 {
AzureIoTClient 0:e393db310d89 901 /*this function takes a list, and inserts it in another list. When done in the context of this file, it reverses the effects of a not-able-to-send situation*/
AzureIoTClient 0:e393db310d89 902 PDLIST_ENTRY afterHead;
AzureIoTClient 0:e393db310d89 903 while ((afterHead = DList_RemoveHeadList(source)) != source)
AzureIoTClient 0:e393db310d89 904 {
AzureIoTClient 0:e393db310d89 905 DList_InsertHeadList(destination, afterHead);
AzureIoTClient 0:e393db310d89 906 }
AzureIoTClient 0:e393db310d89 907 }
AzureIoTClient 0:e393db310d89 908
AzureIoTClient 0:e393db310d89 909 static void DoEvent(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 0:e393db310d89 910 {
AzureIoTClient 0:e393db310d89 911 HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 0:e393db310d89 912 if (DList_IsListEmpty(handleData->waitingToSend))
AzureIoTClient 0:e393db310d89 913 {
AzureIoTClient 0:e393db310d89 914 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_019: [If the list is empty then IoTHubTransportHttp_DoWork shall proceed to the following action.] */
AzureIoTClient 0:e393db310d89 915 }
AzureIoTClient 0:e393db310d89 916 else
AzureIoTClient 0:e393db310d89 917 {
AzureIoTClient 0:e393db310d89 918 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_101: [If option SetBatching is true then _Dowork shall send batched event message as specced below.] */
AzureIoTClient 0:e393db310d89 919 if (handleData->doBatchedTransfers)
AzureIoTClient 0:e393db310d89 920 {
AzureIoTClient 0:e393db310d89 921 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_102: [Request HTTP headers shall have the value of "Content-Type" created or updated to "application/vnd.microsoft.iothub.json" by a call to HTTPHeaders_ReplaceHeaderNameValuePair.] */
AzureIoTClient 0:e393db310d89 922 if (HTTPHeaders_ReplaceHeaderNameValuePair(handleData->eventHTTPrequestHeaders, CONTENT_TYPE, APPLICATION_VND_MICROSOFT_IOTHUB_JSON) != HTTP_HEADERS_OK)
AzureIoTClient 0:e393db310d89 923 {
AzureIoTClient 0:e393db310d89 924 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_103: [If updating Content-Type fails for any reason, then _DoWork shall advance to the next action.] */
AzureIoTClient 0:e393db310d89 925 LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair\r\n");
AzureIoTClient 0:e393db310d89 926 }
AzureIoTClient 0:e393db310d89 927 else
AzureIoTClient 0:e393db310d89 928 {
AzureIoTClient 0:e393db310d89 929 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_018: [It shall inspect the "waitingToSend" DLIST passed in config structure.] */
AzureIoTClient 0:e393db310d89 930 STRING_HANDLE payload;
AzureIoTClient 0:e393db310d89 931 switch (makePayload(handleData, &payload))
AzureIoTClient 0:e393db310d89 932 {
AzureIoTClient 0:e393db310d89 933 case MAKE_PAYLOAD_OK:
AzureIoTClient 0:e393db310d89 934 {
AzureIoTClient 0:e393db310d89 935 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_031: [Once a final payload has been obtained, IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters:] */
AzureIoTClient 0:e393db310d89 936 BUFFER_HANDLE temp = BUFFER_new();
AzureIoTClient 0:e393db310d89 937 if (temp == NULL)
AzureIoTClient 0:e393db310d89 938 {
AzureIoTClient 0:e393db310d89 939 LogError("unable to BUFFER_new\r\n");
AzureIoTClient 0:e393db310d89 940 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_073: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/
AzureIoTClient 0:e393db310d89 941 reversePutListBackIn(&(handleData->eventConfirmations), handleData->waitingToSend);
AzureIoTClient 0:e393db310d89 942 }
AzureIoTClient 0:e393db310d89 943 else
AzureIoTClient 0:e393db310d89 944 {
AzureIoTClient 0:e393db310d89 945 if (BUFFER_build(temp, (const unsigned char*)STRING_c_str(payload), STRING_length(payload)) != 0)
AzureIoTClient 0:e393db310d89 946 {
AzureIoTClient 0:e393db310d89 947 LogError("unable to BUFFER_build\r\n");
AzureIoTClient 0:e393db310d89 948 //items go back to waitingToSend
AzureIoTClient 0:e393db310d89 949 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_073: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/
AzureIoTClient 0:e393db310d89 950 reversePutListBackIn(&(handleData->eventConfirmations), handleData->waitingToSend);
AzureIoTClient 0:e393db310d89 951 }
AzureIoTClient 0:e393db310d89 952 else
AzureIoTClient 0:e393db310d89 953 {
AzureIoTClient 0:e393db310d89 954 unsigned int statusCode;
AzureIoTClient 0:e393db310d89 955 HTTPAPIEX_RESULT r;
AzureIoTClient 0:e393db310d89 956 if ((r = HTTPAPIEX_SAS_ExecuteRequest(
AzureIoTClient 0:e393db310d89 957 handleData->sasObject,
AzureIoTClient 0:e393db310d89 958 handleData->httpApiExHandle,
AzureIoTClient 0:e393db310d89 959 HTTPAPI_REQUEST_POST,
AzureIoTClient 0:e393db310d89 960 STRING_c_str(handleData->eventHTTPrelativePath),
AzureIoTClient 0:e393db310d89 961 handleData->eventHTTPrequestHeaders,
AzureIoTClient 0:e393db310d89 962 temp,
AzureIoTClient 0:e393db310d89 963 &statusCode,
AzureIoTClient 0:e393db310d89 964 NULL,
AzureIoTClient 0:e393db310d89 965 NULL
AzureIoTClient 0:e393db310d89 966 )) != HTTPAPIEX_OK)
AzureIoTClient 0:e393db310d89 967 {
AzureIoTClient 0:e393db310d89 968 LogError("unable to HTTPAPIEX_ExecuteRequest\r\n");
AzureIoTClient 0:e393db310d89 969 //items go back to waitingToSend
AzureIoTClient 0:e393db310d89 970 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_065: [if HTTPAPIEX_SAS_ExecuteRequest fails or the http status code >=300 then IoTHubTransportHttp_DoWork shall not do any other action (it is assumed at the next _DoWork it shall be retried).] */
AzureIoTClient 0:e393db310d89 971 reversePutListBackIn(&(handleData->eventConfirmations), handleData->waitingToSend);
AzureIoTClient 0:e393db310d89 972 }
AzureIoTClient 0:e393db310d89 973 else
AzureIoTClient 0:e393db310d89 974 {
AzureIoTClient 0:e393db310d89 975 if (statusCode < 300)
AzureIoTClient 0:e393db310d89 976 {
AzureIoTClient 0:e393db310d89 977 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_067: [If HTTPAPIEX_SAS_ExecuteRequest does not fail and http status code <300 then IoTHubTransportHttp_DoWork shall call IoTHubClient_LL_SendComplete. Parameter PDLIST_ENTRY completed shall point to a list containing all the items batched, and parameter IOTHUB_BATCHSTATE result shall be set to IOTHUB_BATCHSTATE_SUCESS. The batched items shall be removed from waitingToSend.] */
AzureIoTClient 0:e393db310d89 978 IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_SUCCESS);
AzureIoTClient 0:e393db310d89 979 }
AzureIoTClient 0:e393db310d89 980 else
AzureIoTClient 0:e393db310d89 981 {
AzureIoTClient 0:e393db310d89 982 //items go back to waitingToSend
AzureIoTClient 0:e393db310d89 983 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_065: [if HTTPAPIEX_SAS_ExecuteRequest fails or the http status code >=300 then IoTHubTransportHttp_DoWork shall not do any other action (it is assumed at the next _DoWork it shall be retried).] */
AzureIoTClient 0:e393db310d89 984 LogError("unexpected HTTP status code (%u)\r\n", statusCode);
AzureIoTClient 0:e393db310d89 985 reversePutListBackIn(&(handleData->eventConfirmations), handleData->waitingToSend);
AzureIoTClient 0:e393db310d89 986 }
AzureIoTClient 0:e393db310d89 987 }
AzureIoTClient 0:e393db310d89 988 }
AzureIoTClient 0:e393db310d89 989 BUFFER_delete(temp);
AzureIoTClient 0:e393db310d89 990 }
AzureIoTClient 0:e393db310d89 991 STRING_delete(payload);
AzureIoTClient 0:e393db310d89 992 break;
AzureIoTClient 0:e393db310d89 993 }
AzureIoTClient 0:e393db310d89 994 case MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT:
AzureIoTClient 0:e393db310d89 995 {
AzureIoTClient 0:e393db310d89 996 IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_FAILED); /*takes care of emptying the list too*/
AzureIoTClient 0:e393db310d89 997 break;
AzureIoTClient 0:e393db310d89 998 }
AzureIoTClient 0:e393db310d89 999 case MAKE_PAYLOAD_ERROR:
AzureIoTClient 0:e393db310d89 1000 {
AzureIoTClient 0:e393db310d89 1001 LogError("unrecoverable errors while building a batch message\r\n");
AzureIoTClient 0:e393db310d89 1002 break;
AzureIoTClient 0:e393db310d89 1003 }
AzureIoTClient 0:e393db310d89 1004 case MAKE_PAYLOAD_NO_ITEMS:
AzureIoTClient 0:e393db310d89 1005 {
AzureIoTClient 0:e393db310d89 1006 /*do nothing*/
AzureIoTClient 0:e393db310d89 1007 break;
AzureIoTClient 0:e393db310d89 1008 }
AzureIoTClient 0:e393db310d89 1009 default:
AzureIoTClient 0:e393db310d89 1010 {
AzureIoTClient 0:e393db310d89 1011 LogError("internal error: switch's default branch reached when never intended\r\n");
AzureIoTClient 0:e393db310d89 1012 break;
AzureIoTClient 0:e393db310d89 1013 }
AzureIoTClient 0:e393db310d89 1014 }
AzureIoTClient 0:e393db310d89 1015 }
AzureIoTClient 0:e393db310d89 1016 }
AzureIoTClient 0:e393db310d89 1017 else
AzureIoTClient 0:e393db310d89 1018 {
AzureIoTClient 0:e393db310d89 1019 const unsigned char* messageContent;
AzureIoTClient 0:e393db310d89 1020 size_t messageSize;
AzureIoTClient 0:e393db310d89 1021 IOTHUB_MESSAGE_LIST* message = containingRecord(handleData->waitingToSend->Flink, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 0:e393db310d89 1022 IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message->messageHandle);
AzureIoTClient 0:e393db310d89 1023
AzureIoTClient 0:e393db310d89 1024 if (!(
AzureIoTClient 0:e393db310d89 1025 ((contentType == IOTHUBMESSAGE_BYTEARRAY) && (IoTHubMessage_GetByteArray(message->messageHandle, &messageContent, &messageSize)==IOTHUB_MESSAGE_OK)) ||
AzureIoTClient 0:e393db310d89 1026 ((contentType == IOTHUBMESSAGE_STRING) && (
AzureIoTClient 0:e393db310d89 1027 messageContent = (const unsigned char*)IoTHubMessage_GetString(message->messageHandle),
AzureIoTClient 0:e393db310d89 1028 (messageSize = (messageContent == NULL)?0:strlen((const char*)messageContent)),
AzureIoTClient 0:e393db310d89 1029 messageContent!=NULL)
AzureIoTClient 0:e393db310d89 1030 )
AzureIoTClient 0:e393db310d89 1031 ))
AzureIoTClient 0:e393db310d89 1032 {
AzureIoTClient 0:e393db310d89 1033 LogError("unable to get the message content\r\n");
AzureIoTClient 0:e393db310d89 1034 /*go on...*/
AzureIoTClient 0:e393db310d89 1035 }
AzureIoTClient 0:e393db310d89 1036 else
AzureIoTClient 0:e393db310d89 1037 {
AzureIoTClient 0:e393db310d89 1038 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_109: [If the oldest message in waitingToSend causes the message to exceed 256K bytes then it shall be removed from waitingToSend, and IoTHubClient_LL_SendComplete shall be called. Parameter PDLIST_ENTRY completed shall point to a list containing only the oldest item, and parameter IOTHUB_BATCHSTATE result shall be set to IOTHUB_BATCHSTATE_FAILED.] */
AzureIoTClient 0:e393db310d89 1039 if (messageSize >= 256 * 1024)
AzureIoTClient 0:e393db310d89 1040 {
AzureIoTClient 0:e393db310d89 1041 PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/
AzureIoTClient 0:e393db310d89 1042 DList_InsertTailList(&(handleData->eventConfirmations), head);
AzureIoTClient 0:e393db310d89 1043 IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_FAILED); /*takes care of emptying the list too*/
AzureIoTClient 0:e393db310d89 1044 }
AzureIoTClient 0:e393db310d89 1045 else
AzureIoTClient 0:e393db310d89 1046 {
AzureIoTClient 0:e393db310d89 1047 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_104: [If option SetBatching is false then _Dowork shall send individual event message as specced below.] */
AzureIoTClient 0:e393db310d89 1048 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_105: [A clone of the event HTTP request headers shall be created.]*/
AzureIoTClient 0:e393db310d89 1049 HTTP_HEADERS_HANDLE clonedEventHTTPrequestHeaders = HTTPHeaders_Clone(handleData->eventHTTPrequestHeaders);
AzureIoTClient 0:e393db310d89 1050 if (clonedEventHTTPrequestHeaders == NULL)
AzureIoTClient 0:e393db310d89 1051 {
AzureIoTClient 0:e393db310d89 1052 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 0:e393db310d89 1053 LogError("HTTPHeaders_Clone failed\r\n");
AzureIoTClient 0:e393db310d89 1054 }
AzureIoTClient 0:e393db310d89 1055 else
AzureIoTClient 0:e393db310d89 1056 {
AzureIoTClient 0:e393db310d89 1057 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_106: [The cloned HTTP headers shall have the HTTP header "Content-Type" set to "application/octet-stream".] */
AzureIoTClient 0:e393db310d89 1058 if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, CONTENT_TYPE, APPLICATION_OCTET_STREAM) != HTTP_HEADERS_OK)
AzureIoTClient 0:e393db310d89 1059 {
AzureIoTClient 0:e393db310d89 1060 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 0:e393db310d89 1061 LogError("HTTPHeaders_ReplaceHeaderNameValuePair failed\r\n");
AzureIoTClient 0:e393db310d89 1062 }
AzureIoTClient 0:e393db310d89 1063 else
AzureIoTClient 0:e393db310d89 1064 {
AzureIoTClient 0:e393db310d89 1065 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_107: [Every message property "property":"value" shall be added to the HTTP headers as an individual header "iothub-app-property":"value".] */
AzureIoTClient 0:e393db310d89 1066 MAP_HANDLE map = IoTHubMessage_Properties(message->messageHandle);
AzureIoTClient 0:e393db310d89 1067 const char*const* keys;
AzureIoTClient 0:e393db310d89 1068 const char*const* values;
AzureIoTClient 0:e393db310d89 1069 size_t count;
AzureIoTClient 0:e393db310d89 1070 if (Map_GetInternals(map, &keys, &values, &count) != MAP_OK)
AzureIoTClient 0:e393db310d89 1071 {
AzureIoTClient 0:e393db310d89 1072 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 0:e393db310d89 1073 LogError("unable to Map_GetInternals\r\n");
AzureIoTClient 0:e393db310d89 1074 }
AzureIoTClient 0:e393db310d89 1075 else
AzureIoTClient 0:e393db310d89 1076 {
AzureIoTClient 0:e393db310d89 1077 size_t i;
AzureIoTClient 0:e393db310d89 1078 bool goOn = true;
AzureIoTClient 0:e393db310d89 1079 for (i = 0; (i < count) && goOn; i++)
AzureIoTClient 0:e393db310d89 1080 {
AzureIoTClient 0:e393db310d89 1081
AzureIoTClient 0:e393db310d89 1082 STRING_HANDLE temp = STRING_construct(IOTHUB_APP_PREFIX);
AzureIoTClient 0:e393db310d89 1083 if (temp == NULL)
AzureIoTClient 0:e393db310d89 1084 {
AzureIoTClient 0:e393db310d89 1085 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 0:e393db310d89 1086 LogError("unable to STRING_construct\r\n");
AzureIoTClient 0:e393db310d89 1087 goOn = false;
AzureIoTClient 0:e393db310d89 1088 }
AzureIoTClient 0:e393db310d89 1089 else
AzureIoTClient 0:e393db310d89 1090 {
AzureIoTClient 0:e393db310d89 1091 if (STRING_concat(temp, keys[i]) != 0)
AzureIoTClient 0:e393db310d89 1092 {
AzureIoTClient 0:e393db310d89 1093 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 0:e393db310d89 1094 LogError("unable to STRING_concat\r\n");
AzureIoTClient 0:e393db310d89 1095 goOn = false;
AzureIoTClient 0:e393db310d89 1096 }
AzureIoTClient 0:e393db310d89 1097 else
AzureIoTClient 0:e393db310d89 1098 {
AzureIoTClient 0:e393db310d89 1099 if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, STRING_c_str(temp), values[i]) != HTTP_HEADERS_OK)
AzureIoTClient 0:e393db310d89 1100 {
AzureIoTClient 0:e393db310d89 1101 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 0:e393db310d89 1102 LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair\r\n");
AzureIoTClient 0:e393db310d89 1103 goOn = false;
AzureIoTClient 0:e393db310d89 1104 }
AzureIoTClient 0:e393db310d89 1105 }
AzureIoTClient 0:e393db310d89 1106 STRING_delete(temp);
AzureIoTClient 0:e393db310d89 1107 }
AzureIoTClient 0:e393db310d89 1108 }
AzureIoTClient 0:e393db310d89 1109
AzureIoTClient 0:e393db310d89 1110 if (!goOn)
AzureIoTClient 0:e393db310d89 1111 {
AzureIoTClient 0:e393db310d89 1112 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 0:e393db310d89 1113 }
AzureIoTClient 0:e393db310d89 1114 else
AzureIoTClient 0:e393db310d89 1115 {
AzureIoTClient 0:e393db310d89 1116 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_110: [IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters] */
AzureIoTClient 0:e393db310d89 1117 BUFFER_HANDLE toBeSend = BUFFER_new();
AzureIoTClient 0:e393db310d89 1118 if (toBeSend == NULL)
AzureIoTClient 0:e393db310d89 1119 {
AzureIoTClient 0:e393db310d89 1120 LogError("unable to BUFFER_new\r\n");
AzureIoTClient 0:e393db310d89 1121 }
AzureIoTClient 0:e393db310d89 1122 else
AzureIoTClient 0:e393db310d89 1123 {
AzureIoTClient 0:e393db310d89 1124 if (BUFFER_build(toBeSend, messageContent, messageSize) != 0)
AzureIoTClient 0:e393db310d89 1125 {
AzureIoTClient 0:e393db310d89 1126 LogError("unable to BUFFER_build\r\n");
AzureIoTClient 0:e393db310d89 1127 }
AzureIoTClient 0:e393db310d89 1128 else
AzureIoTClient 0:e393db310d89 1129 {
AzureIoTClient 0:e393db310d89 1130 unsigned int statusCode;
AzureIoTClient 0:e393db310d89 1131 HTTPAPIEX_RESULT r;
AzureIoTClient 0:e393db310d89 1132 if ((r = HTTPAPIEX_SAS_ExecuteRequest(
AzureIoTClient 0:e393db310d89 1133 handleData->sasObject,
AzureIoTClient 0:e393db310d89 1134 handleData->httpApiExHandle,
AzureIoTClient 0:e393db310d89 1135 HTTPAPI_REQUEST_POST,
AzureIoTClient 0:e393db310d89 1136 STRING_c_str(handleData->eventHTTPrelativePath),
AzureIoTClient 0:e393db310d89 1137 clonedEventHTTPrequestHeaders,
AzureIoTClient 0:e393db310d89 1138 toBeSend,
AzureIoTClient 0:e393db310d89 1139 &statusCode,
AzureIoTClient 0:e393db310d89 1140 NULL,
AzureIoTClient 0:e393db310d89 1141 NULL
AzureIoTClient 0:e393db310d89 1142 )) != HTTPAPIEX_OK)
AzureIoTClient 0:e393db310d89 1143 {
AzureIoTClient 0:e393db310d89 1144 LogError("unable to HTTPAPIEX_ExecuteRequest\r\n");
AzureIoTClient 0:e393db310d89 1145 }
AzureIoTClient 0:e393db310d89 1146 else
AzureIoTClient 0:e393db310d89 1147 {
AzureIoTClient 0:e393db310d89 1148 if (statusCode < 300)
AzureIoTClient 0:e393db310d89 1149 {
AzureIoTClient 0:e393db310d89 1150 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_112: [If HTTPAPIEX_SAS_ExecuteRequest does not fail and http status code <300 then IoTHubTransportHttp_DoWork shall call IoTHubClient_LL_SendComplete. Parameter PDLIST_ENTRY completed shall point to a list the item send, and parameter IOTHUB_BATCHSTATE result shall be set to IOTHUB_BATCHSTATE_SUCCESS. The item shall be removed from waitingToSend.] */
AzureIoTClient 0:e393db310d89 1151 PDLIST_ENTRY justSent = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/
AzureIoTClient 0:e393db310d89 1152 DList_InsertTailList(&(handleData->eventConfirmations), justSent);
AzureIoTClient 0:e393db310d89 1153 IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_SUCCESS); /*takes care of emptying the list too*/
AzureIoTClient 0:e393db310d89 1154 }
AzureIoTClient 0:e393db310d89 1155 else
AzureIoTClient 0:e393db310d89 1156 {
AzureIoTClient 0:e393db310d89 1157 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_111: [If HTTPAPIEX_SAS_ExecuteRequest fails or the http status code >=300 then IoTHubTransportHttp_DoWork shall not do any other action (it is assumed at the next _DoWork it shall be retried).] */
AzureIoTClient 0:e393db310d89 1158 LogError("unexpected HTTP status code (%u)\r\n", statusCode);
AzureIoTClient 0:e393db310d89 1159 }
AzureIoTClient 0:e393db310d89 1160 }
AzureIoTClient 0:e393db310d89 1161 }
AzureIoTClient 0:e393db310d89 1162 BUFFER_delete(toBeSend);
AzureIoTClient 0:e393db310d89 1163 }
AzureIoTClient 0:e393db310d89 1164 }
AzureIoTClient 0:e393db310d89 1165 }
AzureIoTClient 0:e393db310d89 1166 }
AzureIoTClient 0:e393db310d89 1167 HTTPHeaders_Free(clonedEventHTTPrequestHeaders);
AzureIoTClient 0:e393db310d89 1168 }
AzureIoTClient 0:e393db310d89 1169 }
AzureIoTClient 0:e393db310d89 1170 }
AzureIoTClient 0:e393db310d89 1171 }
AzureIoTClient 0:e393db310d89 1172 }
AzureIoTClient 0:e393db310d89 1173 }
AzureIoTClient 0:e393db310d89 1174
AzureIoTClient 0:e393db310d89 1175 #define ACTION_VALUES \
AzureIoTClient 0:e393db310d89 1176 ABANDON, \
AzureIoTClient 0:e393db310d89 1177 REJECT, \
AzureIoTClient 0:e393db310d89 1178 ACCEPT
AzureIoTClient 0:e393db310d89 1179 DEFINE_ENUM(ACTION, ACTION_VALUES);
AzureIoTClient 0:e393db310d89 1180
AzureIoTClient 0:e393db310d89 1181 static void abandonOrAcceptMessage(HTTPTRANSPORT_HANDLE_DATA* handleData, const char* ETag, ACTION action)
AzureIoTClient 0:e393db310d89 1182 {
AzureIoTClient 0:e393db310d89 1183 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_050: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters:
AzureIoTClient 0:e393db310d89 1184 -requestType: POST
AzureIoTClient 0:e393db310d89 1185 -relativePath: abandon relative path begin (as created by _Create) + value of ETag + "/abandon?api-version=2015-08-15-preview"
AzureIoTClient 0:e393db310d89 1186 - requestHttpHeadersHandle: an HTTP headers instance containing the following
AzureIoTClient 0:e393db310d89 1187 Authorization: " "
AzureIoTClient 0:e393db310d89 1188 If-Match: value of ETag
AzureIoTClient 0:e393db310d89 1189 - requestContent: NULL
AzureIoTClient 0:e393db310d89 1190 - statusCode: a pointer to unsigned int which might be examined for logging
AzureIoTClient 0:e393db310d89 1191 - responseHeadearsHandle: NULL
AzureIoTClient 0:e393db310d89 1192 - responseContent: NULL]*/
AzureIoTClient 0:e393db310d89 1193 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_051: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters:
AzureIoTClient 0:e393db310d89 1194 -requestType: DELETE
AzureIoTClient 0:e393db310d89 1195 -relativePath: abandon relative path begin + value of ETag + "?api-version=2015-08-15-preview"
AzureIoTClient 0:e393db310d89 1196 - requestHttpHeadersHandle: an HTTP headers instance containing the following
AzureIoTClient 0:e393db310d89 1197 Authorization: " "
AzureIoTClient 0:e393db310d89 1198 If-Match: value of ETag
AzureIoTClient 0:e393db310d89 1199 - requestContent: NULL
AzureIoTClient 0:e393db310d89 1200 - statusCode: a pointer to unsigned int which might be used by logging
AzureIoTClient 0:e393db310d89 1201 - responseHeadearsHandle: NULL
AzureIoTClient 0:e393db310d89 1202 - responseContent: NULL]*/
AzureIoTClient 0:e393db310d89 1203 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_077: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters:
AzureIoTClient 0:e393db310d89 1204 -requestType: DELETE
AzureIoTClient 0:e393db310d89 1205 -relativePath: abandon relative path begin + value of ETag +"?api-version=2015-08-15-preview" + "&reject"
AzureIoTClient 0:e393db310d89 1206 - requestHttpHeadersHandle: an HTTP headers instance containing the following
AzureIoTClient 0:e393db310d89 1207 Authorization: " "
AzureIoTClient 0:e393db310d89 1208 If-Match: value of ETag
AzureIoTClient 0:e393db310d89 1209 - requestContent: NULL
AzureIoTClient 0:e393db310d89 1210 - statusCode: a pointer to unsigned int which might be used by logging
AzureIoTClient 0:e393db310d89 1211 - responseHeadearsHandle: NULL
AzureIoTClient 0:e393db310d89 1212 - responseContent: NULL]*/
AzureIoTClient 0:e393db310d89 1213
AzureIoTClient 0:e393db310d89 1214 STRING_HANDLE fullAbandonRelativePath = STRING_clone(handleData->abandonHTTPrelativePathBegin);
AzureIoTClient 0:e393db310d89 1215 if (fullAbandonRelativePath == NULL)
AzureIoTClient 0:e393db310d89 1216 {
AzureIoTClient 0:e393db310d89 1217 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
AzureIoTClient 0:e393db310d89 1218 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1219 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1220 LogError("unable to STRING_clone\r\n");
AzureIoTClient 0:e393db310d89 1221 }
AzureIoTClient 0:e393db310d89 1222 else
AzureIoTClient 0:e393db310d89 1223 {
AzureIoTClient 0:e393db310d89 1224 STRING_HANDLE ETagUnquoted = STRING_construct_n(ETag + 1, strlen(ETag) - 2); /*skip first character which is '"' and the last one (which is also '"')*/
AzureIoTClient 0:e393db310d89 1225 if (ETagUnquoted == NULL)
AzureIoTClient 0:e393db310d89 1226 {
AzureIoTClient 0:e393db310d89 1227 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
AzureIoTClient 0:e393db310d89 1228 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1229 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1230 LogError("unable to STRING_construct_n\r\n");
AzureIoTClient 0:e393db310d89 1231 }
AzureIoTClient 0:e393db310d89 1232 else
AzureIoTClient 0:e393db310d89 1233 {
AzureIoTClient 0:e393db310d89 1234 if (!(
AzureIoTClient 0:e393db310d89 1235 (STRING_concat_with_STRING(fullAbandonRelativePath, ETagUnquoted) == 0) &&
AzureIoTClient 0:e393db310d89 1236 (STRING_concat(fullAbandonRelativePath, (action == ABANDON) ? "/abandon" API_VERSION : ((action == REJECT) ? API_VERSION "&reject" : API_VERSION)) == 0)
AzureIoTClient 0:e393db310d89 1237 ))
AzureIoTClient 0:e393db310d89 1238 {
AzureIoTClient 0:e393db310d89 1239 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
AzureIoTClient 0:e393db310d89 1240 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1241 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1242 LogError("unable to STRING_concat\r\n");
AzureIoTClient 0:e393db310d89 1243 }
AzureIoTClient 0:e393db310d89 1244 else
AzureIoTClient 0:e393db310d89 1245 {
AzureIoTClient 0:e393db310d89 1246 HTTP_HEADERS_HANDLE abandonRequestHttpHeaders = HTTPHeaders_Alloc();
AzureIoTClient 0:e393db310d89 1247 if (abandonRequestHttpHeaders == NULL)
AzureIoTClient 0:e393db310d89 1248 {
AzureIoTClient 0:e393db310d89 1249 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
AzureIoTClient 0:e393db310d89 1250 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1251 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1252 LogError("unable to HTTPHeaders_Alloc\r\n");
AzureIoTClient 0:e393db310d89 1253 }
AzureIoTClient 0:e393db310d89 1254 else
AzureIoTClient 0:e393db310d89 1255 {
AzureIoTClient 0:e393db310d89 1256 if (!(
AzureIoTClient 0:e393db310d89 1257 (HTTPHeaders_AddHeaderNameValuePair(abandonRequestHttpHeaders, "Authorization", " ") == HTTP_HEADERS_OK) &&
AzureIoTClient 0:e393db310d89 1258 (HTTPHeaders_AddHeaderNameValuePair(abandonRequestHttpHeaders, "If-Match", ETag) == HTTP_HEADERS_OK)
AzureIoTClient 0:e393db310d89 1259 ))
AzureIoTClient 0:e393db310d89 1260 {
AzureIoTClient 0:e393db310d89 1261 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
AzureIoTClient 0:e393db310d89 1262 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1263 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1264 LogError("unable to HTTPHeaders_AddHeaderNameValuePair\r\n");
AzureIoTClient 0:e393db310d89 1265 }
AzureIoTClient 0:e393db310d89 1266 else
AzureIoTClient 0:e393db310d89 1267 {
AzureIoTClient 0:e393db310d89 1268 unsigned int statusCode;
AzureIoTClient 0:e393db310d89 1269 if (HTTPAPIEX_SAS_ExecuteRequest(
AzureIoTClient 0:e393db310d89 1270 handleData->sasObject,
AzureIoTClient 0:e393db310d89 1271 handleData->httpApiExHandle,
AzureIoTClient 0:e393db310d89 1272 (action == ABANDON) ? HTTPAPI_REQUEST_POST : HTTPAPI_REQUEST_DELETE, /*-requestType: POST */
AzureIoTClient 0:e393db310d89 1273 STRING_c_str(fullAbandonRelativePath), /*-relativePath: abandon relative path begin (as created by _Create) + value of ETag + "/abandon?api-version=2015-08-15-preview" */
AzureIoTClient 0:e393db310d89 1274 abandonRequestHttpHeaders, /*- requestHttpHeadersHandle: an HTTP headers instance containing the following */
AzureIoTClient 0:e393db310d89 1275 NULL, /*- requestContent: NULL */
AzureIoTClient 0:e393db310d89 1276 &statusCode, /*- statusCode: a pointer to unsigned int which might be examined for logging */
AzureIoTClient 0:e393db310d89 1277 NULL, /*- responseHeadearsHandle: NULL */
AzureIoTClient 0:e393db310d89 1278 NULL /*- responseContent: NULL] */
AzureIoTClient 0:e393db310d89 1279 ) != HTTPAPIEX_OK)
AzureIoTClient 0:e393db310d89 1280 {
AzureIoTClient 0:e393db310d89 1281 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
AzureIoTClient 0:e393db310d89 1282 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1283 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1284 LogError("unable to HTTPAPIEX_ExecuteRequest\r\n");
AzureIoTClient 0:e393db310d89 1285 }
AzureIoTClient 0:e393db310d89 1286 else
AzureIoTClient 0:e393db310d89 1287 {
AzureIoTClient 0:e393db310d89 1288 if (statusCode != 204)
AzureIoTClient 0:e393db310d89 1289 {
AzureIoTClient 0:e393db310d89 1290 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
AzureIoTClient 0:e393db310d89 1291 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1292 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1293 LogError("unexpected status code returned %u (was expecting 204)\r\n", statusCode);
AzureIoTClient 0:e393db310d89 1294 }
AzureIoTClient 0:e393db310d89 1295 else
AzureIoTClient 0:e393db310d89 1296 {
AzureIoTClient 0:e393db310d89 1297 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
AzureIoTClient 0:e393db310d89 1298 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1299 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 0:e393db310d89 1300 /*all is fine*/
AzureIoTClient 0:e393db310d89 1301 }
AzureIoTClient 0:e393db310d89 1302 }
AzureIoTClient 0:e393db310d89 1303 }
AzureIoTClient 0:e393db310d89 1304 HTTPHeaders_Free(abandonRequestHttpHeaders);
AzureIoTClient 0:e393db310d89 1305 }
AzureIoTClient 0:e393db310d89 1306 }
AzureIoTClient 0:e393db310d89 1307 STRING_delete(ETagUnquoted);
AzureIoTClient 0:e393db310d89 1308 }
AzureIoTClient 0:e393db310d89 1309 STRING_delete(fullAbandonRelativePath);
AzureIoTClient 0:e393db310d89 1310 }
AzureIoTClient 0:e393db310d89 1311 }
AzureIoTClient 0:e393db310d89 1312
AzureIoTClient 0:e393db310d89 1313 static void DoNotifications(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 0:e393db310d89 1314 {
AzureIoTClient 0:e393db310d89 1315 HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 0:e393db310d89 1316 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_057: [If flag DoWork_PullNotification is set to false then _DoWork shall advance to the next action.] */
AzureIoTClient 0:e393db310d89 1317 if (handleData->DoWork_PullNotification)
AzureIoTClient 0:e393db310d89 1318 {
AzureIoTClient 0:e393db310d89 1319 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_116: [After client creation, the first GET shall be allowed no matter what the value of GetMinimumPollingTime.] */
AzureIoTClient 0:e393db310d89 1320 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_117: [If time is not available then all calls shall be treated as if they are the first one.] */
AzureIoTClient 0:e393db310d89 1321 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_115: [A GET request that happens earlier than GetMinimumPollingTime shall be ignored.] */
AzureIoTClient 0:e393db310d89 1322 time_t timeNow = get_time(NULL);
AzureIoTClient 0:e393db310d89 1323 bool isPollingAllowed = handleData->isFirstPoll || (timeNow == (time_t)(-1)) || (get_difftime(timeNow, handleData->lastPollTime) > handleData->getMinimumPollingTime);
AzureIoTClient 0:e393db310d89 1324 if (isPollingAllowed)
AzureIoTClient 0:e393db310d89 1325 {
AzureIoTClient 0:e393db310d89 1326 HTTP_HEADERS_HANDLE responseHTTPHeaders = HTTPHeaders_Alloc();
AzureIoTClient 0:e393db310d89 1327 if (responseHTTPHeaders == NULL)
AzureIoTClient 0:e393db310d89 1328 {
AzureIoTClient 0:e393db310d89 1329 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_037: [If the call to HTTPAPIEX_SAS_ExecuteRequest did not executed successfully or building any part of the prerequisites of the call fails, then _DoWork shall advance to the next action in this description.] */
AzureIoTClient 0:e393db310d89 1330 LogError("unable to HTTPHeaders_Alloc\r\n");
AzureIoTClient 0:e393db310d89 1331 }
AzureIoTClient 0:e393db310d89 1332 else
AzureIoTClient 0:e393db310d89 1333 {
AzureIoTClient 0:e393db310d89 1334 BUFFER_HANDLE responseContent = BUFFER_new();
AzureIoTClient 0:e393db310d89 1335 if (responseContent == NULL)
AzureIoTClient 0:e393db310d89 1336 {
AzureIoTClient 0:e393db310d89 1337 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_037: [If the call to HTTPAPIEX_SAS_ExecuteRequest did not executed successfully or building any part of the prerequisites of the call fails, then _DoWork shall advance to the next action in this description.] */
AzureIoTClient 0:e393db310d89 1338 LogError("unable to BUFFER_new\r\n");
AzureIoTClient 0:e393db310d89 1339 }
AzureIoTClient 0:e393db310d89 1340 else
AzureIoTClient 0:e393db310d89 1341 {
AzureIoTClient 0:e393db310d89 1342 unsigned int statusCode;
AzureIoTClient 0:e393db310d89 1343 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_036: [Otherwise, IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters
AzureIoTClient 0:e393db310d89 1344 requestType: GET
AzureIoTClient 0:e393db310d89 1345 relativePath: the notification HTTP relative path
AzureIoTClient 0:e393db310d89 1346 requestHttpHeadersHandle: notification HTTP request headers created by _Create
AzureIoTClient 0:e393db310d89 1347 requestContent: NULL
AzureIoTClient 0:e393db310d89 1348 statusCode: a pointer to unsigned int which shall be later examined
AzureIoTClient 0:e393db310d89 1349 responseHeadearsHandle: a new instance of HTTP headers
AzureIoTClient 0:e393db310d89 1350 responseContent: a new instance of buffer]
AzureIoTClient 0:e393db310d89 1351 */
AzureIoTClient 0:e393db310d89 1352 if (HTTPAPIEX_SAS_ExecuteRequest(
AzureIoTClient 0:e393db310d89 1353 handleData->sasObject,
AzureIoTClient 0:e393db310d89 1354 handleData->httpApiExHandle,
AzureIoTClient 0:e393db310d89 1355 HTTPAPI_REQUEST_GET, /*requestType: GET*/
AzureIoTClient 0:e393db310d89 1356 STRING_c_str(handleData->notificationHTTPrelativePath), /*relativePath: the notification HTTP relative path*/
AzureIoTClient 0:e393db310d89 1357 handleData->notificationHTTPrequestHeaders, /*requestHttpHeadersHandle: notification HTTP request headers created by _Create*/
AzureIoTClient 0:e393db310d89 1358 NULL, /*requestContent: NULL*/
AzureIoTClient 0:e393db310d89 1359 &statusCode, /*statusCode: a pointer to unsigned int which shall be later examined*/
AzureIoTClient 0:e393db310d89 1360 responseHTTPHeaders, /*responseHeadearsHandle: a new instance of HTTP headers*/
AzureIoTClient 0:e393db310d89 1361 responseContent /*responseContent: a new instance of buffer*/
AzureIoTClient 0:e393db310d89 1362 )
AzureIoTClient 0:e393db310d89 1363 != HTTPAPIEX_OK)
AzureIoTClient 0:e393db310d89 1364 {
AzureIoTClient 0:e393db310d89 1365 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_037: [If the call to HTTPAPIEX_SAS_ExecuteRequest did not executed successfully or building any part of the prerequisites of the call fails, then _DoWork shall advance to the next action in this description.] */
AzureIoTClient 0:e393db310d89 1366 LogError("unable to HTTPAPIEX_ExecuteRequest\r\n");
AzureIoTClient 0:e393db310d89 1367 }
AzureIoTClient 0:e393db310d89 1368 else
AzureIoTClient 0:e393db310d89 1369 {
AzureIoTClient 0:e393db310d89 1370 /*HTTP dialogue was succesfull*/
AzureIoTClient 0:e393db310d89 1371 if (timeNow == (time_t)(-1))
AzureIoTClient 0:e393db310d89 1372 {
AzureIoTClient 0:e393db310d89 1373 handleData->isFirstPoll = true;
AzureIoTClient 0:e393db310d89 1374 }
AzureIoTClient 0:e393db310d89 1375 else
AzureIoTClient 0:e393db310d89 1376 {
AzureIoTClient 0:e393db310d89 1377 handleData->isFirstPoll = false;
AzureIoTClient 0:e393db310d89 1378 handleData->lastPollTime = timeNow;
AzureIoTClient 0:e393db310d89 1379 }
AzureIoTClient 0:e393db310d89 1380 if (statusCode == 204)
AzureIoTClient 0:e393db310d89 1381 {
AzureIoTClient 0:e393db310d89 1382 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_038: [If the HTTPAPIEX_SAS_ExecuteRequest executed successfully then status code shall be examined. Any status code different than 200 causes _DoWork to advance to the next action.] */
AzureIoTClient 0:e393db310d89 1383 /*this is an expected status code, means "no commands", but logging that creates panic*/
AzureIoTClient 0:e393db310d89 1384
AzureIoTClient 0:e393db310d89 1385 /*do nothing, advance to next action*/
AzureIoTClient 0:e393db310d89 1386 }
AzureIoTClient 0:e393db310d89 1387 else if (statusCode != 200)
AzureIoTClient 0:e393db310d89 1388 {
AzureIoTClient 0:e393db310d89 1389 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_038: [If the HTTPAPIEX_SAS_ExecuteRequest executed successfully then status code shall be examined. Any status code different than 200 causes _DoWork to advance to the next action.] */
AzureIoTClient 0:e393db310d89 1390 LogError("expected status code was 200, but actually was received %u... moving on\r\n", statusCode);
AzureIoTClient 0:e393db310d89 1391 }
AzureIoTClient 0:e393db310d89 1392 else
AzureIoTClient 0:e393db310d89 1393 {
AzureIoTClient 0:e393db310d89 1394 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_039: [If status code is 200, then _DoWork shall make a copy of the value of the "ETag" http header.]*/
AzureIoTClient 0:e393db310d89 1395 const char* etagValue = HTTPHeaders_FindHeaderValue(responseHTTPHeaders, "ETag");
AzureIoTClient 0:e393db310d89 1396 if (etagValue == NULL)
AzureIoTClient 0:e393db310d89 1397 {
AzureIoTClient 0:e393db310d89 1398 LogError("unable to find a received header called \"E-Tag\"\r\n");
AzureIoTClient 0:e393db310d89 1399 }
AzureIoTClient 0:e393db310d89 1400 else
AzureIoTClient 0:e393db310d89 1401 {
AzureIoTClient 0:e393db310d89 1402 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_040: [If no such header is found or is invalid, then _DoWork shall advance to the next action.]*/
AzureIoTClient 0:e393db310d89 1403 size_t etagsize = strlen(etagValue);
AzureIoTClient 0:e393db310d89 1404 if (
AzureIoTClient 0:e393db310d89 1405 (etagsize < 2) ||
AzureIoTClient 0:e393db310d89 1406 (etagValue[0] != '"') ||
AzureIoTClient 0:e393db310d89 1407 (etagValue[etagsize - 1] != '"')
AzureIoTClient 0:e393db310d89 1408 )
AzureIoTClient 0:e393db310d89 1409 {
AzureIoTClient 0:e393db310d89 1410 LogError("ETag is not a valid quoted string\r\n");
AzureIoTClient 0:e393db310d89 1411 }
AzureIoTClient 0:e393db310d89 1412 else
AzureIoTClient 0:e393db310d89 1413 {
AzureIoTClient 0:e393db310d89 1414 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_041: [_DoWork shall assemble an IOTHUBMESSAGE_HANDLE from the received HTTP content (using the responseContent buffer).] */
AzureIoTClient 0:e393db310d89 1415 IOTHUB_MESSAGE_HANDLE receivedMessage = IoTHubMessage_CreateFromByteArray(BUFFER_u_char(responseContent), BUFFER_length(responseContent));
AzureIoTClient 0:e393db310d89 1416 if (receivedMessage == NULL)
AzureIoTClient 0:e393db310d89 1417 {
AzureIoTClient 0:e393db310d89 1418 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_042: [If assembling the message fails in any way, then _DoWork shall "abandon" the message.]*/
AzureIoTClient 0:e393db310d89 1419 LogError("unable to IoTHubMessage_CreateFromByteArray, trying to abandon the message... \r\n");
AzureIoTClient 0:e393db310d89 1420 abandonOrAcceptMessage(handle, etagValue, ABANDON);
AzureIoTClient 0:e393db310d89 1421 }
AzureIoTClient 0:e393db310d89 1422 else
AzureIoTClient 0:e393db310d89 1423 {
AzureIoTClient 0:e393db310d89 1424 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_087: [All the HTTP headers of the form iothub-app-name:somecontent shall be transformed in message properties {name, somecontent}.]*/
AzureIoTClient 0:e393db310d89 1425 size_t nHeaders;
AzureIoTClient 0:e393db310d89 1426 if (HTTPHeaders_GetHeaderCount(responseHTTPHeaders, &nHeaders) != HTTP_HEADERS_OK)
AzureIoTClient 0:e393db310d89 1427 {
AzureIoTClient 0:e393db310d89 1428 LogError("unable to get the count of HTTP headers\r\n");
AzureIoTClient 0:e393db310d89 1429 abandonOrAcceptMessage(handle, etagValue, ABANDON);
AzureIoTClient 0:e393db310d89 1430 }
AzureIoTClient 0:e393db310d89 1431 else
AzureIoTClient 0:e393db310d89 1432 {
AzureIoTClient 0:e393db310d89 1433 size_t i;
AzureIoTClient 0:e393db310d89 1434 MAP_HANDLE properties = (nHeaders > 0) ? IoTHubMessage_Properties(receivedMessage) : NULL;
AzureIoTClient 0:e393db310d89 1435 for (i = 0; i < nHeaders; i++)
AzureIoTClient 0:e393db310d89 1436 {
AzureIoTClient 0:e393db310d89 1437 char* completeHeader;
AzureIoTClient 0:e393db310d89 1438 if (HTTPHeaders_GetHeader(responseHTTPHeaders, i, &completeHeader) != HTTP_HEADERS_OK)
AzureIoTClient 0:e393db310d89 1439 {
AzureIoTClient 0:e393db310d89 1440 break;
AzureIoTClient 0:e393db310d89 1441 }
AzureIoTClient 0:e393db310d89 1442 else
AzureIoTClient 0:e393db310d89 1443 {
AzureIoTClient 0:e393db310d89 1444 if (strncmp(IOTHUB_APP_PREFIX, completeHeader, strlen(IOTHUB_APP_PREFIX)) == 0)
AzureIoTClient 0:e393db310d89 1445 {
AzureIoTClient 0:e393db310d89 1446 /*looks like a property headers*/
AzureIoTClient 0:e393db310d89 1447 /*there's a guaranteed ':' in the completeHeader, by HTTP_HEADERS module*/
AzureIoTClient 0:e393db310d89 1448 char* whereIsColon = strchr(completeHeader, ':');
AzureIoTClient 0:e393db310d89 1449 *whereIsColon = '\0'; /*cut it down*/
AzureIoTClient 0:e393db310d89 1450 if (Map_AddOrUpdate(properties, completeHeader + strlen(IOTHUB_APP_PREFIX), whereIsColon + 2) != MAP_OK) /*whereIsColon+1 is a space because HTTPEHADERS outputs a ": " between name and value*/
AzureIoTClient 0:e393db310d89 1451 {
AzureIoTClient 0:e393db310d89 1452 free(completeHeader);
AzureIoTClient 0:e393db310d89 1453 break;
AzureIoTClient 0:e393db310d89 1454 }
AzureIoTClient 0:e393db310d89 1455 }
AzureIoTClient 0:e393db310d89 1456 free(completeHeader);
AzureIoTClient 0:e393db310d89 1457 }
AzureIoTClient 0:e393db310d89 1458 }
AzureIoTClient 0:e393db310d89 1459
AzureIoTClient 0:e393db310d89 1460 if (i < nHeaders)
AzureIoTClient 0:e393db310d89 1461 {
AzureIoTClient 0:e393db310d89 1462 abandonOrAcceptMessage(handle, etagValue, ABANDON);
AzureIoTClient 0:e393db310d89 1463 }
AzureIoTClient 0:e393db310d89 1464 else
AzureIoTClient 0:e393db310d89 1465 {
AzureIoTClient 0:e393db310d89 1466 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_043: [Otherwise, _DoWork shall call IoTHubClient_LL_NotificationCallback with parameters handle = iotHubClientHandle and notificationMessage = newly created message.]*/
AzureIoTClient 0:e393db310d89 1467 IOTHUBMESSAGE_DISPOSITION_RESULT notificationResult = IoTHubClient_LL_NotificationCallback(iotHubClientHandle, receivedMessage);
AzureIoTClient 0:e393db310d89 1468 if (notificationResult == IOTHUBMESSAGE_ACCEPTED)
AzureIoTClient 0:e393db310d89 1469 {
AzureIoTClient 0:e393db310d89 1470 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_044: [If IoTHubClient_LL_NotificationCallback returns IOTHUBMESSAGE_ACCEPTED then _DoWork shall "accept" the message.]*/
AzureIoTClient 0:e393db310d89 1471 abandonOrAcceptMessage(handle, etagValue, ACCEPT);
AzureIoTClient 0:e393db310d89 1472 }
AzureIoTClient 0:e393db310d89 1473 else if (notificationResult == IOTHUBMESSAGE_REJECTED)
AzureIoTClient 0:e393db310d89 1474 {
AzureIoTClient 0:e393db310d89 1475 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_074: [If IoTHubClient_LL_NotificationCallback returns IOTHUBMESSAGE_REJECTED then _DoWork shall "reject" the message.]*/
AzureIoTClient 0:e393db310d89 1476 abandonOrAcceptMessage(handle, etagValue, REJECT);
AzureIoTClient 0:e393db310d89 1477 }
AzureIoTClient 0:e393db310d89 1478 else
AzureIoTClient 0:e393db310d89 1479 {
AzureIoTClient 0:e393db310d89 1480 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_079: [If IoTHubClient_LL_NotificationCallback returns IOTHUBMESSAGE_ABANDONED then _DoWork shall "abandon" the message.] */
AzureIoTClient 0:e393db310d89 1481 abandonOrAcceptMessage(handle, etagValue, ABANDON);
AzureIoTClient 0:e393db310d89 1482 }
AzureIoTClient 0:e393db310d89 1483 }
AzureIoTClient 0:e393db310d89 1484 }
AzureIoTClient 0:e393db310d89 1485 IoTHubMessage_Destroy(receivedMessage);
AzureIoTClient 0:e393db310d89 1486 }
AzureIoTClient 0:e393db310d89 1487 }
AzureIoTClient 0:e393db310d89 1488
AzureIoTClient 0:e393db310d89 1489 }
AzureIoTClient 0:e393db310d89 1490 }
AzureIoTClient 0:e393db310d89 1491 }
AzureIoTClient 0:e393db310d89 1492 BUFFER_delete(responseContent);
AzureIoTClient 0:e393db310d89 1493 }
AzureIoTClient 0:e393db310d89 1494 HTTPHeaders_Free(responseHTTPHeaders);
AzureIoTClient 0:e393db310d89 1495 }
AzureIoTClient 0:e393db310d89 1496 }
AzureIoTClient 0:e393db310d89 1497 else
AzureIoTClient 0:e393db310d89 1498 {
AzureIoTClient 0:e393db310d89 1499 /*isPollingAllowed is false... */
AzureIoTClient 0:e393db310d89 1500 /*do nothing "shall be ignored*/
AzureIoTClient 0:e393db310d89 1501 }
AzureIoTClient 0:e393db310d89 1502 }
AzureIoTClient 0:e393db310d89 1503 }
AzureIoTClient 0:e393db310d89 1504
AzureIoTClient 0:e393db310d89 1505 void IoTHubTransportHttp_DoWork(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 0:e393db310d89 1506 {
AzureIoTClient 0:e393db310d89 1507 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_017: [If parameter handle is NULL or parameter iotHubClientHandle then IoTHubTransportHttp_DoWork shall immeditely return.]*/
AzureIoTClient 0:e393db310d89 1508 if ((handle != NULL) && (iotHubClientHandle != NULL))
AzureIoTClient 0:e393db310d89 1509 {
AzureIoTClient 0:e393db310d89 1510 DoEvent(handle, iotHubClientHandle);
AzureIoTClient 0:e393db310d89 1511 DoNotifications(handle, iotHubClientHandle);
AzureIoTClient 0:e393db310d89 1512 }
AzureIoTClient 0:e393db310d89 1513 }
AzureIoTClient 0:e393db310d89 1514
AzureIoTClient 0:e393db310d89 1515 IOTHUB_CLIENT_RESULT IoTHubTransportHttp_GetSendStatus(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 0:e393db310d89 1516 {
AzureIoTClient 0:e393db310d89 1517 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 0:e393db310d89 1518
AzureIoTClient 0:e393db310d89 1519 /* Codes_SRS_IOTHUBTRANSPORTTHTTP_09_001: [IoTHubTransportHttp_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter] */
AzureIoTClient 0:e393db310d89 1520 if (handle == NULL)
AzureIoTClient 0:e393db310d89 1521 {
AzureIoTClient 0:e393db310d89 1522 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 0:e393db310d89 1523 LogError("Invalid handle to IoTHubClient HTTP transport instance.\r\n");
AzureIoTClient 0:e393db310d89 1524 }
AzureIoTClient 0:e393db310d89 1525 else if (iotHubClientStatus == NULL)
AzureIoTClient 0:e393db310d89 1526 {
AzureIoTClient 0:e393db310d89 1527 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 0:e393db310d89 1528 LogError("Invalid pointer to output parameter IOTHUB_CLIENT_STATUS.\r\n");
AzureIoTClient 0:e393db310d89 1529 }
AzureIoTClient 0:e393db310d89 1530 else
AzureIoTClient 0:e393db310d89 1531 {
AzureIoTClient 0:e393db310d89 1532 HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 0:e393db310d89 1533
AzureIoTClient 0:e393db310d89 1534 /* Codes_SRS_IOTHUBTRANSPORTTHTTP_09_002: [IoTHubTransportHttp_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_IDLE if there are currently no event items to be sent or being sent] */
AzureIoTClient 0:e393db310d89 1535 if (!DList_IsListEmpty(handleData->waitingToSend))
AzureIoTClient 0:e393db310d89 1536 {
AzureIoTClient 0:e393db310d89 1537 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY;
AzureIoTClient 0:e393db310d89 1538 }
AzureIoTClient 0:e393db310d89 1539 /* Codes_SRS_IOTHUBTRANSPORTTHTTP_09_003: [IoTHubTransportHttp_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_BUSY if there are currently event items to be sent or being sent] */
AzureIoTClient 0:e393db310d89 1540 else
AzureIoTClient 0:e393db310d89 1541 {
AzureIoTClient 0:e393db310d89 1542 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE;
AzureIoTClient 0:e393db310d89 1543 }
AzureIoTClient 0:e393db310d89 1544
AzureIoTClient 0:e393db310d89 1545 result = IOTHUB_CLIENT_OK;
AzureIoTClient 0:e393db310d89 1546 }
AzureIoTClient 0:e393db310d89 1547
AzureIoTClient 0:e393db310d89 1548 return result;
AzureIoTClient 0:e393db310d89 1549 }
AzureIoTClient 0:e393db310d89 1550
AzureIoTClient 0:e393db310d89 1551 IOTHUB_CLIENT_RESULT IoTHubTransportHttp_SetOption(TRANSPORT_HANDLE handle, const char* option, const void* value)
AzureIoTClient 0:e393db310d89 1552 {
AzureIoTClient 0:e393db310d89 1553 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 0:e393db310d89 1554 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_080: [If handle parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 0:e393db310d89 1555 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_081: [If option parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 0:e393db310d89 1556 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_082: [If value parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 0:e393db310d89 1557 if (
AzureIoTClient 0:e393db310d89 1558 (handle == NULL) ||
AzureIoTClient 0:e393db310d89 1559 (option == NULL) ||
AzureIoTClient 0:e393db310d89 1560 (value == NULL)
AzureIoTClient 0:e393db310d89 1561 )
AzureIoTClient 0:e393db310d89 1562 {
AzureIoTClient 0:e393db310d89 1563 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 0:e393db310d89 1564 LogError("invalid parameter (NULL) passed to IoTHubTransportHttp_SetOption\r\n");
AzureIoTClient 0:e393db310d89 1565 }
AzureIoTClient 0:e393db310d89 1566 else
AzureIoTClient 0:e393db310d89 1567 {
AzureIoTClient 0:e393db310d89 1568 HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle;
AzureIoTClient 0:e393db310d89 1569 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_097: ["SetBatching"] */
AzureIoTClient 0:e393db310d89 1570 if (strcmp("SetBatching", option) == 0)
AzureIoTClient 0:e393db310d89 1571 {
AzureIoTClient 0:e393db310d89 1572 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_083: [If optionName is an option handled by IoTHubTransportHttp then it shall be set.] */
AzureIoTClient 0:e393db310d89 1573 handleData->doBatchedTransfers = *(bool*)value;
AzureIoTClient 0:e393db310d89 1574 result = IOTHUB_CLIENT_OK;
AzureIoTClient 0:e393db310d89 1575 }
AzureIoTClient 0:e393db310d89 1576 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_114: ["GetMinimumPollingTime"] */
AzureIoTClient 0:e393db310d89 1577 else if (strcmp("GetMinimumPollingTime", option) == 0)
AzureIoTClient 0:e393db310d89 1578 {
AzureIoTClient 0:e393db310d89 1579 handleData->getMinimumPollingTime = *(unsigned int*)value;
AzureIoTClient 0:e393db310d89 1580 result = IOTHUB_CLIENT_OK;
AzureIoTClient 0:e393db310d89 1581 }
AzureIoTClient 0:e393db310d89 1582 else
AzureIoTClient 0:e393db310d89 1583 {
AzureIoTClient 0:e393db310d89 1584 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_084: [Otherwise, IoTHubTransport_Http shall call HTTPAPIEX_SetOption with the same parameters and return the translated code.] */
AzureIoTClient 0:e393db310d89 1585 HTTPAPIEX_RESULT HTTPAPIEX_result = HTTPAPIEX_SetOption(handleData->httpApiExHandle, option, value);
AzureIoTClient 0:e393db310d89 1586 /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_086: [The following table translates HTTPAPIEX return codes to IOTHUB_CLIENT_RESULT return codes:] */
AzureIoTClient 0:e393db310d89 1587 if (HTTPAPIEX_result == HTTPAPIEX_OK)
AzureIoTClient 0:e393db310d89 1588 {
AzureIoTClient 0:e393db310d89 1589 result = IOTHUB_CLIENT_OK;
AzureIoTClient 0:e393db310d89 1590 }
AzureIoTClient 0:e393db310d89 1591 else if (HTTPAPIEX_result == HTTPAPIEX_INVALID_ARG)
AzureIoTClient 0:e393db310d89 1592 {
AzureIoTClient 0:e393db310d89 1593 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 0:e393db310d89 1594 LogError("HTTPAPIEX_SetOption failed\r\n");
AzureIoTClient 0:e393db310d89 1595 }
AzureIoTClient 0:e393db310d89 1596 else
AzureIoTClient 0:e393db310d89 1597 {
AzureIoTClient 0:e393db310d89 1598 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 0:e393db310d89 1599 LogError("HTTPAPIEX_SetOption failed\r\n");
AzureIoTClient 0:e393db310d89 1600 }
AzureIoTClient 0:e393db310d89 1601 }
AzureIoTClient 0:e393db310d89 1602 }
AzureIoTClient 0:e393db310d89 1603 return result;
AzureIoTClient 0:e393db310d89 1604 }