Microsoft Azure IoTHub client HTTP transport

Dependents:   iothub_client_sample_http simplesample_http temp_sensor_anomaly

This library implements the HTTP transport for Microsoft Azure IoTHub client. The code is replicated from https://github.com/Azure/azure-iot-sdks

Committer:
Azure.IoT Build
Date:
Fri Mar 25 15:59:57 2016 -0700
Revision:
14:fae5c6b2cce3
Parent:
13:848d52f93daf
Child:
15:52602fffff8d
Release 1.0.3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 6:73793bae15ba 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 6:73793bae15ba 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 6:73793bae15ba 3
AzureIoTClient 6:73793bae15ba 4 #include <stdlib.h>
AzureIoTClient 6:73793bae15ba 5 #ifdef _CRTDBG_MAP_ALLOC
AzureIoTClient 6:73793bae15ba 6 #include <crtdbg.h>
AzureIoTClient 6:73793bae15ba 7 #endif
AzureIoTClient 6:73793bae15ba 8 #include "gballoc.h"
AzureIoTClient 6:73793bae15ba 9
AzureIoTClient 6:73793bae15ba 10 #include <time.h>
AzureIoTClient 11:495deb847d9c 11 #include "iothub_client_version.h"
AzureIoTClient 6:73793bae15ba 12 #include "iothub_client_private.h"
AzureIoTClient 6:73793bae15ba 13 #include "iothubtransporthttp.h"
AzureIoTClient 6:73793bae15ba 14
AzureIoTClient 6:73793bae15ba 15 #include "httpapiexsas.h"
AzureIoTClient 6:73793bae15ba 16 #include "urlencode.h"
AzureIoTClient 6:73793bae15ba 17 #include "iot_logging.h"
AzureIoTClient 6:73793bae15ba 18 #include "httpapiex.h"
AzureIoTClient 6:73793bae15ba 19 #include "httpapiexsas.h"
AzureIoTClient 6:73793bae15ba 20 #include "strings.h"
AzureIoTClient 6:73793bae15ba 21 #include "base64.h"
AzureIoTClient 6:73793bae15ba 22 #include "doublylinkedlist.h"
Azure.IoT Build 13:848d52f93daf 23 #include "vector.h"
AzureIoTClient 6:73793bae15ba 24 #include "httpheaders.h"
AzureIoTClient 6:73793bae15ba 25 #include "agenttime.h"
AzureIoTClient 6:73793bae15ba 26
AzureIoTClient 6:73793bae15ba 27 #define IOTHUB_APP_PREFIX "iothub-app-"
AzureIoTClient 7:48f0f78cd3ef 28 const char* IOTHUB_MESSAGE_ID = "iothub-messageid";
AzureIoTClient 7:48f0f78cd3ef 29 const char* IOTHUB_CORRELATION_ID = "iothub-correlationid";
AzureIoTClient 6:73793bae15ba 30
AzureIoTClient 6:73793bae15ba 31 #define CONTENT_TYPE "Content-Type"
AzureIoTClient 6:73793bae15ba 32 #define APPLICATION_OCTET_STREAM "application/octet-stream"
AzureIoTClient 6:73793bae15ba 33 #define APPLICATION_VND_MICROSOFT_IOTHUB_JSON "application/vnd.microsoft.iothub.json"
AzureIoTClient 6:73793bae15ba 34
AzureIoTClient 6:73793bae15ba 35 /*DEFAULT_GETMINIMUMPOLLINGTIME is the minimum time in seconds allowed between 2 consecutive GET issues to the service (GET=fetch messages)*/
AzureIoTClient 6:73793bae15ba 36 /*the default is 25 minutes*/
AzureIoTClient 6:73793bae15ba 37 #define DEFAULT_GETMINIMUMPOLLINGTIME ((unsigned int)25*60)
AzureIoTClient 6:73793bae15ba 38
AzureIoTClient 6:73793bae15ba 39 #define MAXIMUM_MESSAGE_SIZE (255*1024-1)
AzureIoTClient 6:73793bae15ba 40 #define MAXIMUM_PAYLOAD_OVERHEAD 384
AzureIoTClient 6:73793bae15ba 41 #define MAXIMUM_PROPERTY_OVERHEAD 16
AzureIoTClient 6:73793bae15ba 42
AzureIoTClient 6:73793bae15ba 43 /*forward declaration*/
AzureIoTClient 6:73793bae15ba 44 static int appendMapToJSON(STRING_HANDLE existing, const char* const* keys, const char* const* values, size_t count);
AzureIoTClient 6:73793bae15ba 45
Azure.IoT Build 13:848d52f93daf 46 /*Codes_SRS_TRANSPORTMULTITHTTP_17_125: [This function shall return a pointer to a structure of type TRANSPORT_PROVIDER having the following values for its fields:] */
AzureIoTClient 6:73793bae15ba 47 static TRANSPORT_PROVIDER thisTransportProvider =
AzureIoTClient 6:73793bae15ba 48 {
AzureIoTClient 6:73793bae15ba 49 IoTHubTransportHttp_SetOption, /*pfIoTHubTransport_SetOption IoTHubTransport_SetOption; */
AzureIoTClient 6:73793bae15ba 50 IoTHubTransportHttp_Create, /*pfIoTHubTransport_Create IoTHubTransport_Create; */
AzureIoTClient 6:73793bae15ba 51 IoTHubTransportHttp_Destroy, /*pfIoTHubTransport_Destroy IoTHubTransport_Destroy; */
Azure.IoT Build 13:848d52f93daf 52 IoTHubTransportHttp_Register, /* pfIotHubTransport_Register IoTHubTransport_Register; */
Azure.IoT Build 13:848d52f93daf 53 IoTHubTransportHttp_Unregister, /* pfIotHubTransport_Unregister IoTHubTransport_Unegister; */
AzureIoTClient 6:73793bae15ba 54 IoTHubTransportHttp_Subscribe, /*pfIoTHubTransport_Subscribe IoTHubTransport_Subscribe; */
AzureIoTClient 6:73793bae15ba 55 IoTHubTransportHttp_Unsubscribe, /*pfIoTHubTransport_Unsubscribe IoTHubTransport_Unsubscribe; */
AzureIoTClient 6:73793bae15ba 56 IoTHubTransportHttp_DoWork, /*pfIoTHubTransport_DoWork IoTHubTransport_DoWork; */
AzureIoTClient 6:73793bae15ba 57 IoTHubTransportHttp_GetSendStatus /* pfIoTHubTransport_GetSendStatus IoTHubTransport_GetSendStatus */
AzureIoTClient 6:73793bae15ba 58 };
AzureIoTClient 6:73793bae15ba 59
AzureIoTClient 6:73793bae15ba 60 const void* HTTP_Protocol(void)
AzureIoTClient 6:73793bae15ba 61 {
AzureIoTClient 6:73793bae15ba 62 return &thisTransportProvider;
AzureIoTClient 6:73793bae15ba 63 }
AzureIoTClient 6:73793bae15ba 64
Azure.IoT Build 13:848d52f93daf 65
AzureIoTClient 6:73793bae15ba 66 typedef struct HTTPTRANSPORT_HANDLE_DATA_TAG
AzureIoTClient 6:73793bae15ba 67 {
Azure.IoT Build 13:848d52f93daf 68 STRING_HANDLE hostName;
Azure.IoT Build 13:848d52f93daf 69 HTTPAPIEX_HANDLE httpApiExHandle;
Azure.IoT Build 13:848d52f93daf 70 bool doBatchedTransfers;
Azure.IoT Build 13:848d52f93daf 71 unsigned int getMinimumPollingTime;
Azure.IoT Build 13:848d52f93daf 72 VECTOR_HANDLE perDeviceList;
Azure.IoT Build 13:848d52f93daf 73 }HTTPTRANSPORT_HANDLE_DATA;
Azure.IoT Build 13:848d52f93daf 74
Azure.IoT Build 13:848d52f93daf 75 typedef struct HTTPTRANSPORT_PERDEVICE_DATA_TAG
Azure.IoT Build 13:848d52f93daf 76 {
Azure.IoT Build 13:848d52f93daf 77 HTTPTRANSPORT_HANDLE_DATA* transportHandle;
Azure.IoT Build 13:848d52f93daf 78
Azure.IoT Build 13:848d52f93daf 79 STRING_HANDLE deviceId;
Azure.IoT Build 13:848d52f93daf 80 STRING_HANDLE deviceKey;
AzureIoTClient 6:73793bae15ba 81 STRING_HANDLE eventHTTPrelativePath;
AzureIoTClient 6:73793bae15ba 82 STRING_HANDLE messageHTTPrelativePath;
AzureIoTClient 6:73793bae15ba 83 HTTP_HEADERS_HANDLE eventHTTPrequestHeaders;
AzureIoTClient 6:73793bae15ba 84 HTTP_HEADERS_HANDLE messageHTTPrequestHeaders;
AzureIoTClient 6:73793bae15ba 85 STRING_HANDLE abandonHTTPrelativePathBegin;
AzureIoTClient 6:73793bae15ba 86 HTTPAPIEX_SAS_HANDLE sasObject;
AzureIoTClient 6:73793bae15ba 87 bool DoWork_PullMessage;
AzureIoTClient 6:73793bae15ba 88 time_t lastPollTime;
Azure.IoT Build 13:848d52f93daf 89 bool isFirstPoll;
Azure.IoT Build 13:848d52f93daf 90
Azure.IoT Build 13:848d52f93daf 91 IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle;
AzureIoTClient 6:73793bae15ba 92 PDLIST_ENTRY waitingToSend;
AzureIoTClient 6:73793bae15ba 93 DLIST_ENTRY eventConfirmations; /*holds items for event confirmations*/
Azure.IoT Build 13:848d52f93daf 94 } HTTPTRANSPORT_PERDEVICE_DATA;
AzureIoTClient 6:73793bae15ba 95
Azure.IoT Build 13:848d52f93daf 96 static void destroy_eventHTTPrelativePath(HTTPTRANSPORT_PERDEVICE_DATA* handleData)
AzureIoTClient 6:73793bae15ba 97 {
AzureIoTClient 6:73793bae15ba 98 STRING_delete(handleData->eventHTTPrelativePath);
AzureIoTClient 6:73793bae15ba 99 handleData->eventHTTPrelativePath = NULL;
AzureIoTClient 6:73793bae15ba 100 }
AzureIoTClient 6:73793bae15ba 101
Azure.IoT Build 13:848d52f93daf 102 static bool create_eventHTTPrelativePath(HTTPTRANSPORT_PERDEVICE_DATA* handleData, const char * deviceId)
AzureIoTClient 6:73793bae15ba 103 {
Azure.IoT Build 13:848d52f93daf 104 /*Codes_SRS_TRANSPORTMULTITHTTP_17_017: [ IoTHubTransportHttp_Register shall create an immutable string (further called "event HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(deviceId) + "/messages/events" + APIVERSION. ]*/
AzureIoTClient 6:73793bae15ba 105 bool result;
Azure.IoT Build 13:848d52f93daf 106 STRING_HANDLE urlEncodedDeviceId;
AzureIoTClient 6:73793bae15ba 107 handleData->eventHTTPrelativePath = STRING_construct("/devices/");
AzureIoTClient 6:73793bae15ba 108 if (handleData->eventHTTPrelativePath == NULL)
AzureIoTClient 6:73793bae15ba 109 {
Azure.IoT Build 13:848d52f93daf 110 LogError("STRING_construct failed.");
AzureIoTClient 6:73793bae15ba 111 result = false;
AzureIoTClient 6:73793bae15ba 112 }
Azure.IoT Build 13:848d52f93daf 113 else
Azure.IoT Build 13:848d52f93daf 114 {
Azure.IoT Build 13:848d52f93daf 115 if (!(
Azure.IoT Build 13:848d52f93daf 116 ((urlEncodedDeviceId = URL_EncodeString(deviceId)) != NULL) &&
AzureIoTClient 6:73793bae15ba 117 (STRING_concat_with_STRING(handleData->eventHTTPrelativePath, urlEncodedDeviceId) == 0) &&
AzureIoTClient 6:73793bae15ba 118 (STRING_concat(handleData->eventHTTPrelativePath, EVENT_ENDPOINT API_VERSION) == 0)
AzureIoTClient 6:73793bae15ba 119 ))
AzureIoTClient 6:73793bae15ba 120 {
Azure.IoT Build 13:848d52f93daf 121 /*Codes_SRS_TRANSPORTMULTITHTTP_17_018: [ If creating the string fail for any reason then IoTHubTransportHttp_Register shall fail and return NULL. ]*/
AzureIoTClient 6:73793bae15ba 122 destroy_eventHTTPrelativePath(handleData);
Azure.IoT Build 13:848d52f93daf 123 LogError("Creating HTTP event relative path failed.");
AzureIoTClient 6:73793bae15ba 124 result = false;
AzureIoTClient 6:73793bae15ba 125 }
AzureIoTClient 6:73793bae15ba 126 else
AzureIoTClient 6:73793bae15ba 127 {
AzureIoTClient 6:73793bae15ba 128 result = true;
AzureIoTClient 6:73793bae15ba 129 }
AzureIoTClient 6:73793bae15ba 130 STRING_delete(urlEncodedDeviceId);
Azure.IoT Build 13:848d52f93daf 131 }
AzureIoTClient 6:73793bae15ba 132 return result;
AzureIoTClient 6:73793bae15ba 133 }
AzureIoTClient 6:73793bae15ba 134
Azure.IoT Build 13:848d52f93daf 135 static void destroy_messageHTTPrelativePath(HTTPTRANSPORT_PERDEVICE_DATA* handleData)
AzureIoTClient 6:73793bae15ba 136 {
AzureIoTClient 6:73793bae15ba 137 STRING_delete(handleData->messageHTTPrelativePath);
AzureIoTClient 6:73793bae15ba 138 handleData->messageHTTPrelativePath = NULL;
AzureIoTClient 6:73793bae15ba 139 }
AzureIoTClient 6:73793bae15ba 140
Azure.IoT Build 13:848d52f93daf 141 static bool create_messageHTTPrelativePath(HTTPTRANSPORT_PERDEVICE_DATA* handleData, const char * deviceId)
AzureIoTClient 6:73793bae15ba 142 {
AzureIoTClient 6:73793bae15ba 143 bool result;
AzureIoTClient 6:73793bae15ba 144 handleData->messageHTTPrelativePath = STRING_construct("/devices/");
AzureIoTClient 6:73793bae15ba 145 if (handleData->messageHTTPrelativePath == NULL)
AzureIoTClient 6:73793bae15ba 146 {
Azure.IoT Build 13:848d52f93daf 147 LogError("STRING_construct failed.");
AzureIoTClient 6:73793bae15ba 148 result = false;
AzureIoTClient 6:73793bae15ba 149 }
AzureIoTClient 6:73793bae15ba 150 else
AzureIoTClient 6:73793bae15ba 151 {
Azure.IoT Build 13:848d52f93daf 152 STRING_HANDLE urlEncodedDeviceId;
Azure.IoT Build 13:848d52f93daf 153 /*Codes_SRS_TRANSPORTMULTITHTTP_17_019: [ IoTHubTransportHttp_Register shall create an immutable string (further called "message HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(deviceId) + "/messages/devicebound" + APIVERSION. ]*/
AzureIoTClient 6:73793bae15ba 154 if (!(
Azure.IoT Build 13:848d52f93daf 155 ((urlEncodedDeviceId = URL_EncodeString(deviceId)) != NULL) &&
AzureIoTClient 6:73793bae15ba 156 (STRING_concat_with_STRING(handleData->messageHTTPrelativePath, urlEncodedDeviceId) == 0) &&
AzureIoTClient 6:73793bae15ba 157 (STRING_concat(handleData->messageHTTPrelativePath, MESSAGE_ENDPOINT_HTTP API_VERSION) == 0)
AzureIoTClient 6:73793bae15ba 158 ))
AzureIoTClient 6:73793bae15ba 159 {
Azure.IoT Build 13:848d52f93daf 160 /*Codes_SRS_TRANSPORTMULTITHTTP_17_020: [ If creating the message HTTP relative path fails, then IoTHubTransportHttp_Register shall fail and return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 161 LogError("Creating HTTP message relative path failed.");
AzureIoTClient 6:73793bae15ba 162 result = false;
AzureIoTClient 6:73793bae15ba 163 destroy_messageHTTPrelativePath(handleData);
AzureIoTClient 6:73793bae15ba 164 }
AzureIoTClient 6:73793bae15ba 165 else
AzureIoTClient 6:73793bae15ba 166 {
AzureIoTClient 6:73793bae15ba 167 result = true;
AzureIoTClient 6:73793bae15ba 168 }
AzureIoTClient 6:73793bae15ba 169 STRING_delete(urlEncodedDeviceId);
AzureIoTClient 6:73793bae15ba 170 }
AzureIoTClient 6:73793bae15ba 171
AzureIoTClient 6:73793bae15ba 172 return result;
AzureIoTClient 6:73793bae15ba 173 }
AzureIoTClient 6:73793bae15ba 174
Azure.IoT Build 13:848d52f93daf 175 static void destroy_eventHTTPrequestHeaders(HTTPTRANSPORT_PERDEVICE_DATA* handleData)
AzureIoTClient 6:73793bae15ba 176 {
AzureIoTClient 6:73793bae15ba 177 HTTPHeaders_Free(handleData->eventHTTPrequestHeaders);
AzureIoTClient 6:73793bae15ba 178 handleData->eventHTTPrequestHeaders = NULL;
AzureIoTClient 6:73793bae15ba 179 }
AzureIoTClient 6:73793bae15ba 180
Azure.IoT Build 13:848d52f93daf 181 static bool create_eventHTTPrequestHeaders(HTTPTRANSPORT_PERDEVICE_DATA* handleData, const char * deviceId)
Azure.IoT Build 13:848d52f93daf 182 {
Azure.IoT Build 13:848d52f93daf 183 /*Codes_SRS_TRANSPORTMULTITHTTP_17_021: [ IoTHubTransportHttp_Register shall create a set of HTTP headers (further called "event HTTP request headers") consisting of the following fixed field names and values: "iothub-to":"/devices/" + URL_ENCODED(deviceId) + "/messages/events"; "Authorization":""
AzureIoTClient 6:73793bae15ba 184 "Accept":"application/json"
Azure.IoT Build 13:848d52f93daf 185 "Connection":"Keep-Alive" ]*/
AzureIoTClient 6:73793bae15ba 186 bool result;
AzureIoTClient 6:73793bae15ba 187 handleData->eventHTTPrequestHeaders = HTTPHeaders_Alloc();
AzureIoTClient 6:73793bae15ba 188 if (handleData->eventHTTPrequestHeaders == NULL)
AzureIoTClient 6:73793bae15ba 189 {
Azure.IoT Build 13:848d52f93daf 190 LogError("HTTPHeaders_Alloc failed.");
AzureIoTClient 6:73793bae15ba 191 result = false;
AzureIoTClient 6:73793bae15ba 192 }
AzureIoTClient 6:73793bae15ba 193 else
AzureIoTClient 6:73793bae15ba 194 {
AzureIoTClient 6:73793bae15ba 195 STRING_HANDLE temp = STRING_construct("/devices/");
AzureIoTClient 6:73793bae15ba 196 if (temp == NULL)
AzureIoTClient 6:73793bae15ba 197 {
Azure.IoT Build 13:848d52f93daf 198 LogError("STRING_construct failed.");
AzureIoTClient 6:73793bae15ba 199 result = false;
AzureIoTClient 6:73793bae15ba 200 destroy_eventHTTPrequestHeaders(handleData);
AzureIoTClient 6:73793bae15ba 201 }
AzureIoTClient 6:73793bae15ba 202 else
AzureIoTClient 6:73793bae15ba 203 {
Azure.IoT Build 13:848d52f93daf 204 STRING_HANDLE urlEncodedDeviceId;
AzureIoTClient 6:73793bae15ba 205 if (!(
Azure.IoT Build 13:848d52f93daf 206 ((urlEncodedDeviceId = URL_EncodeString(deviceId)) != NULL) &&
AzureIoTClient 6:73793bae15ba 207 (STRING_concat_with_STRING(temp, urlEncodedDeviceId) == 0) &&
AzureIoTClient 6:73793bae15ba 208 (STRING_concat(temp, EVENT_ENDPOINT) == 0)
AzureIoTClient 6:73793bae15ba 209 ))
AzureIoTClient 6:73793bae15ba 210 {
Azure.IoT Build 13:848d52f93daf 211 LogError("deviceId construction failed.");
AzureIoTClient 6:73793bae15ba 212 result = false;
AzureIoTClient 6:73793bae15ba 213 destroy_eventHTTPrequestHeaders(handleData);
AzureIoTClient 6:73793bae15ba 214 }
AzureIoTClient 6:73793bae15ba 215 else
AzureIoTClient 6:73793bae15ba 216 {
AzureIoTClient 6:73793bae15ba 217 if (!(
AzureIoTClient 6:73793bae15ba 218 (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "iothub-to", STRING_c_str(temp)) == HTTP_HEADERS_OK) &&
AzureIoTClient 6:73793bae15ba 219 (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "Authorization", " ") == HTTP_HEADERS_OK) &&
AzureIoTClient 6:73793bae15ba 220 (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "Accept", "application/json") == HTTP_HEADERS_OK) &&
AzureIoTClient 9:0b2cab6cfc60 221 (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "Connection", "Keep-Alive") == HTTP_HEADERS_OK) &&
AzureIoTClient 10:b012891d0a54 222 (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "User-Agent", CLIENT_DEVICE_TYPE_PREFIX CLIENT_DEVICE_BACKSLASH IOTHUB_SDK_VERSION) == HTTP_HEADERS_OK)
AzureIoTClient 6:73793bae15ba 223 ))
AzureIoTClient 6:73793bae15ba 224 {
Azure.IoT Build 13:848d52f93daf 225 /*Codes_SRS_TRANSPORTMULTITHTTP_17_022: [ If creating the event HTTP request headers fails, then IoTHubTransportHttp_Register shall fail and return NULL.] */
Azure.IoT Build 13:848d52f93daf 226 LogError("adding header properties failed.");
AzureIoTClient 6:73793bae15ba 227 result = false;
AzureIoTClient 6:73793bae15ba 228 destroy_eventHTTPrequestHeaders(handleData);
AzureIoTClient 6:73793bae15ba 229 }
AzureIoTClient 6:73793bae15ba 230 else
AzureIoTClient 6:73793bae15ba 231 {
AzureIoTClient 6:73793bae15ba 232 result = true;
AzureIoTClient 6:73793bae15ba 233 }
AzureIoTClient 6:73793bae15ba 234 }
Azure.IoT Build 13:848d52f93daf 235 STRING_delete(urlEncodedDeviceId);
AzureIoTClient 6:73793bae15ba 236 STRING_delete(temp);
Azure.IoT Build 13:848d52f93daf 237 }
Azure.IoT Build 13:848d52f93daf 238 }
Azure.IoT Build 13:848d52f93daf 239 return result;
Azure.IoT Build 13:848d52f93daf 240 }
Azure.IoT Build 13:848d52f93daf 241
Azure.IoT Build 13:848d52f93daf 242 static void destroy_messageHTTPrequestHeaders(HTTPTRANSPORT_PERDEVICE_DATA* handleData)
Azure.IoT Build 13:848d52f93daf 243 {
Azure.IoT Build 13:848d52f93daf 244 HTTPHeaders_Free(handleData->messageHTTPrequestHeaders);
Azure.IoT Build 13:848d52f93daf 245 handleData->messageHTTPrequestHeaders = NULL;
Azure.IoT Build 13:848d52f93daf 246 }
Azure.IoT Build 13:848d52f93daf 247
Azure.IoT Build 13:848d52f93daf 248 static bool create_messageHTTPrequestHeaders(HTTPTRANSPORT_PERDEVICE_DATA* handleData)
Azure.IoT Build 13:848d52f93daf 249 {
Azure.IoT Build 13:848d52f93daf 250 /*Codes_SRS_TRANSPORTMULTITHTTP_17_132: [ IoTHubTransportHttp_Register shall create a set of HTTP headers (further called "message HTTP request headers") consisting of the following fixed field names and values:
Azure.IoT Build 13:848d52f93daf 251 "Authorization": "" ]*/
Azure.IoT Build 13:848d52f93daf 252 bool result;
Azure.IoT Build 13:848d52f93daf 253 handleData->messageHTTPrequestHeaders = HTTPHeaders_Alloc();
Azure.IoT Build 13:848d52f93daf 254 if (handleData->messageHTTPrequestHeaders == NULL)
Azure.IoT Build 13:848d52f93daf 255 {
Azure.IoT Build 13:848d52f93daf 256 LogError("HTTPHeaders_Alloc failed.");
Azure.IoT Build 13:848d52f93daf 257 result = false;
Azure.IoT Build 13:848d52f93daf 258 }
Azure.IoT Build 13:848d52f93daf 259 else
Azure.IoT Build 13:848d52f93daf 260 {
Azure.IoT Build 13:848d52f93daf 261 if (!(
Azure.IoT Build 13:848d52f93daf 262 (HTTPHeaders_AddHeaderNameValuePair(handleData->messageHTTPrequestHeaders, "User-Agent", CLIENT_DEVICE_TYPE_PREFIX CLIENT_DEVICE_BACKSLASH IOTHUB_SDK_VERSION) == HTTP_HEADERS_OK) &&
Azure.IoT Build 13:848d52f93daf 263 (HTTPHeaders_AddHeaderNameValuePair(handleData->messageHTTPrequestHeaders, "Authorization", " ") == HTTP_HEADERS_OK)
Azure.IoT Build 13:848d52f93daf 264 ))
Azure.IoT Build 13:848d52f93daf 265 {
Azure.IoT Build 13:848d52f93daf 266 /*Codes_SRS_TRANSPORTMULTITHTTP_17_023: [ If creating message HTTP request headers then IoTHubTransportHttp_Register shall fail and return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 267 destroy_messageHTTPrequestHeaders(handleData);
Azure.IoT Build 13:848d52f93daf 268 LogError("adding header properties failed.");
Azure.IoT Build 13:848d52f93daf 269 result = false;
Azure.IoT Build 13:848d52f93daf 270 }
Azure.IoT Build 13:848d52f93daf 271 else
Azure.IoT Build 13:848d52f93daf 272 {
Azure.IoT Build 13:848d52f93daf 273 result = true;
AzureIoTClient 6:73793bae15ba 274 }
AzureIoTClient 6:73793bae15ba 275 }
AzureIoTClient 6:73793bae15ba 276 return result;
AzureIoTClient 6:73793bae15ba 277 }
AzureIoTClient 6:73793bae15ba 278
Azure.IoT Build 13:848d52f93daf 279 static void destroy_abandonHTTPrelativePathBegin(HTTPTRANSPORT_PERDEVICE_DATA* handleData)
Azure.IoT Build 13:848d52f93daf 280 {
Azure.IoT Build 13:848d52f93daf 281 STRING_delete(handleData->abandonHTTPrelativePathBegin);
Azure.IoT Build 13:848d52f93daf 282 handleData->abandonHTTPrelativePathBegin = NULL;
Azure.IoT Build 13:848d52f93daf 283 }
Azure.IoT Build 13:848d52f93daf 284
Azure.IoT Build 13:848d52f93daf 285 static bool create_abandonHTTPrelativePathBegin(HTTPTRANSPORT_PERDEVICE_DATA* handleData, const char * deviceId)
Azure.IoT Build 13:848d52f93daf 286 {
Azure.IoT Build 13:848d52f93daf 287 /*Codes_SRS_TRANSPORTMULTITHTTP_17_024: [ IoTHubTransportHttp_Register shall create a STRING containing: "/devices/" + URL_ENCODED(device id) +"/messages/deviceBound/" called abandonHTTPrelativePathBegin. ]*/
Azure.IoT Build 13:848d52f93daf 288 bool result;
Azure.IoT Build 13:848d52f93daf 289 handleData->abandonHTTPrelativePathBegin = STRING_construct("/devices/");
Azure.IoT Build 13:848d52f93daf 290 if (handleData->abandonHTTPrelativePathBegin == NULL)
Azure.IoT Build 13:848d52f93daf 291 {
Azure.IoT Build 13:848d52f93daf 292 result = false;
Azure.IoT Build 13:848d52f93daf 293 }
Azure.IoT Build 13:848d52f93daf 294 else
Azure.IoT Build 13:848d52f93daf 295 {
Azure.IoT Build 13:848d52f93daf 296 STRING_HANDLE urlEncodedDeviceId;
Azure.IoT Build 13:848d52f93daf 297 if (!(
Azure.IoT Build 13:848d52f93daf 298 ((urlEncodedDeviceId = URL_EncodeString(deviceId)) != NULL) &&
Azure.IoT Build 13:848d52f93daf 299 (STRING_concat_with_STRING(handleData->abandonHTTPrelativePathBegin, urlEncodedDeviceId) == 0) &&
Azure.IoT Build 13:848d52f93daf 300 (STRING_concat(handleData->abandonHTTPrelativePathBegin, MESSAGE_ENDPOINT_HTTP_ETAG) == 0)
Azure.IoT Build 13:848d52f93daf 301 ))
Azure.IoT Build 13:848d52f93daf 302 {
Azure.IoT Build 13:848d52f93daf 303 /*Codes_SRS_TRANSPORTMULTITHTTP_17_025: [ If creating the abandonHTTPrelativePathBegin fails then IoTHubTransportHttp_Register shall fail and return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 304 LogError("unable to create abandon path string.\r\n");
Azure.IoT Build 13:848d52f93daf 305 STRING_delete(handleData->abandonHTTPrelativePathBegin);
Azure.IoT Build 13:848d52f93daf 306 result = false;
Azure.IoT Build 13:848d52f93daf 307 }
Azure.IoT Build 13:848d52f93daf 308 else
Azure.IoT Build 13:848d52f93daf 309 {
Azure.IoT Build 13:848d52f93daf 310 result = true;
Azure.IoT Build 13:848d52f93daf 311 }
Azure.IoT Build 13:848d52f93daf 312 STRING_delete(urlEncodedDeviceId);
Azure.IoT Build 13:848d52f93daf 313 }
Azure.IoT Build 13:848d52f93daf 314 return result;
Azure.IoT Build 13:848d52f93daf 315 }
Azure.IoT Build 13:848d52f93daf 316
Azure.IoT Build 13:848d52f93daf 317 static void destroy_SASObject(HTTPTRANSPORT_PERDEVICE_DATA* handleData)
Azure.IoT Build 13:848d52f93daf 318 {
Azure.IoT Build 13:848d52f93daf 319 HTTPAPIEX_SAS_Destroy(handleData->sasObject);
Azure.IoT Build 13:848d52f93daf 320 handleData->sasObject = NULL;
Azure.IoT Build 13:848d52f93daf 321 }
Azure.IoT Build 13:848d52f93daf 322
Azure.IoT Build 13:848d52f93daf 323 static bool create_deviceSASObject(HTTPTRANSPORT_PERDEVICE_DATA* handleData, STRING_HANDLE hostName, const char * deviceId, const char * deviceKey)
Azure.IoT Build 13:848d52f93daf 324 {
Azure.IoT Build 13:848d52f93daf 325 STRING_HANDLE keyName;
Azure.IoT Build 13:848d52f93daf 326 bool result;
Azure.IoT Build 13:848d52f93daf 327 /*Codes_SRS_TRANSPORTMULTITHTTP_17_026: [IoTHubTransportHttp_Create shall invoke URL_EncodeString with an argument of device id.]*/
Azure.IoT Build 13:848d52f93daf 328 keyName = URL_EncodeString(deviceId);
Azure.IoT Build 13:848d52f93daf 329 if (keyName == NULL)
Azure.IoT Build 13:848d52f93daf 330 {
Azure.IoT Build 13:848d52f93daf 331 /*Codes_SRS_TRANSPORTMULTITHTTP_17_027: [If the encode fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
Azure.IoT Build 13:848d52f93daf 332 LogError("URL_EncodeString keyname failed");
Azure.IoT Build 13:848d52f93daf 333 result = false;
Azure.IoT Build 13:848d52f93daf 334 }
Azure.IoT Build 13:848d52f93daf 335 else
Azure.IoT Build 13:848d52f93daf 336 {
Azure.IoT Build 13:848d52f93daf 337 STRING_HANDLE uriResource;
Azure.IoT Build 13:848d52f93daf 338 /*Codes_SRS_TRANSPORTMULTITHTTP_17_028: [IoTHubTransportHttp_Create shall invoke STRING_clone using the previously created hostname.]*/
Azure.IoT Build 13:848d52f93daf 339 uriResource = STRING_clone(hostName);
Azure.IoT Build 13:848d52f93daf 340 /*Codes_SRS_TRANSPORTMULTITHTTP_17_029: [If the clone fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
Azure.IoT Build 13:848d52f93daf 341 if (uriResource != NULL)
Azure.IoT Build 13:848d52f93daf 342 {
Azure.IoT Build 13:848d52f93daf 343 /*Codes_SRS_TRANSPORTMULTITHTTP_17_030: [IoTHubTransportHttp_Create shall invoke STRING_concat with arguments uriResource and the string "/devices/".]*/
Azure.IoT Build 13:848d52f93daf 344 /*Codes_SRS_TRANSPORTMULTITHTTP_17_141: [If the concat fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
Azure.IoT Build 13:848d52f93daf 345 /*Codes_SRS_TRANSPORTMULTITHTTP_17_031: [IoTHubTransportHttp_Create shall invoke STRING_concat_with_STRING with arguments uriResource and keyName.]*/
Azure.IoT Build 13:848d52f93daf 346 /*Codes_SRS_TRANSPORTMULTITHTTP_17_032: [If the STRING_concat_with_STRING fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
Azure.IoT Build 13:848d52f93daf 347 if ((STRING_concat(uriResource, "/devices/") == 0) &&
Azure.IoT Build 13:848d52f93daf 348 (STRING_concat_with_STRING(uriResource, keyName) == 0))
Azure.IoT Build 13:848d52f93daf 349 {
Azure.IoT Build 13:848d52f93daf 350 /*Codes_SRS_TRANSPORTMULTITHTTP_17_033: [IoTHubTransportHttp_Create shall invoke STRING_construct with an argument of config->upperConfig->deviceKey.]*/
Azure.IoT Build 13:848d52f93daf 351 /*Codes_SRS_TRANSPORTMULTITHTTP_17_034: [If the STRING_construct fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
Azure.IoT Build 13:848d52f93daf 352 STRING_HANDLE key = STRING_construct(deviceKey);
Azure.IoT Build 13:848d52f93daf 353 if (key != NULL)
Azure.IoT Build 13:848d52f93daf 354 {
Azure.IoT Build 13:848d52f93daf 355 /*Codes_SRS_TRANSPORTMULTITHTTP_17_035: [The keyName is shortened to zero length, if that fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
Azure.IoT Build 13:848d52f93daf 356 if (STRING_empty(keyName) != 0)
Azure.IoT Build 13:848d52f93daf 357 {
Azure.IoT Build 13:848d52f93daf 358 LogError("Unable to form the device key name for the SAS\r\n");
Azure.IoT Build 13:848d52f93daf 359 result = false;
Azure.IoT Build 13:848d52f93daf 360 }
Azure.IoT Build 13:848d52f93daf 361 else
Azure.IoT Build 13:848d52f93daf 362 {
Azure.IoT Build 13:848d52f93daf 363 /*Codes_SRS_TRANSPORTMULTITHTTP_17_036: [IoTHubTransportHttp_Create shall invoke HTTPAPIEX_SAS_Create with arguments key, uriResource, and zero length keyName.]*/
Azure.IoT Build 13:848d52f93daf 364 /*Codes_SRS_TRANSPORTMULTITHTTP_17_037: [If the HTTPAPIEX_SAS_Create fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/
Azure.IoT Build 13:848d52f93daf 365 handleData->sasObject = HTTPAPIEX_SAS_Create(key, uriResource, keyName);
Azure.IoT Build 13:848d52f93daf 366 result = (handleData->sasObject != NULL) ? (true) : (false);
Azure.IoT Build 13:848d52f93daf 367 }
Azure.IoT Build 13:848d52f93daf 368 STRING_delete(key);
Azure.IoT Build 13:848d52f93daf 369 }
Azure.IoT Build 13:848d52f93daf 370 else
Azure.IoT Build 13:848d52f93daf 371 {
Azure.IoT Build 13:848d52f93daf 372 LogError("STRING_construct Key failed");
Azure.IoT Build 13:848d52f93daf 373 result = false;
Azure.IoT Build 13:848d52f93daf 374 }
Azure.IoT Build 13:848d52f93daf 375 }
Azure.IoT Build 13:848d52f93daf 376 else
Azure.IoT Build 13:848d52f93daf 377 {
Azure.IoT Build 13:848d52f93daf 378 LogError("STRING_concat uri resource failed");
Azure.IoT Build 13:848d52f93daf 379 result = false;
Azure.IoT Build 13:848d52f93daf 380 }
Azure.IoT Build 13:848d52f93daf 381 STRING_delete(uriResource);
Azure.IoT Build 13:848d52f93daf 382 }
Azure.IoT Build 13:848d52f93daf 383 else
Azure.IoT Build 13:848d52f93daf 384 {
Azure.IoT Build 13:848d52f93daf 385 LogError("STRING_staticclone uri resource failed");
Azure.IoT Build 13:848d52f93daf 386 result = false;
Azure.IoT Build 13:848d52f93daf 387 }
Azure.IoT Build 13:848d52f93daf 388 STRING_delete(keyName);
Azure.IoT Build 13:848d52f93daf 389 }
Azure.IoT Build 13:848d52f93daf 390 return result;
Azure.IoT Build 13:848d52f93daf 391 }
Azure.IoT Build 13:848d52f93daf 392
Azure.IoT Build 13:848d52f93daf 393 static void destroy_deviceId(HTTPTRANSPORT_PERDEVICE_DATA* handleData)
Azure.IoT Build 13:848d52f93daf 394 {
Azure.IoT Build 13:848d52f93daf 395 STRING_delete(handleData->deviceId);
Azure.IoT Build 13:848d52f93daf 396 handleData->deviceId = NULL;
Azure.IoT Build 13:848d52f93daf 397 }
Azure.IoT Build 13:848d52f93daf 398
Azure.IoT Build 13:848d52f93daf 399 static bool create_deviceId(HTTPTRANSPORT_PERDEVICE_DATA* handleData, const char * deviceId)
Azure.IoT Build 13:848d52f93daf 400 {
Azure.IoT Build 13:848d52f93daf 401 /*Codes_SRS_TRANSPORTMULTITHTTP_17_133: [ IoTHubTransportHttp_Register shall create an immutable string (further called "deviceId") from config->deviceConfig->deviceId. ]*/
Azure.IoT Build 13:848d52f93daf 402 bool result;
Azure.IoT Build 13:848d52f93daf 403 handleData->deviceId = STRING_construct(deviceId);
Azure.IoT Build 13:848d52f93daf 404 if (handleData->deviceId == NULL)
Azure.IoT Build 13:848d52f93daf 405 {
Azure.IoT Build 13:848d52f93daf 406 /*Codes_SRS_TRANSPORTMULTITHTTP_17_134: [ If deviceId is not created, then IoTHubTransportHttp_Register shall fail and return NULL. */
Azure.IoT Build 13:848d52f93daf 407 LogError("STRING_construct deviceId failed");
Azure.IoT Build 13:848d52f93daf 408 result = false;
Azure.IoT Build 13:848d52f93daf 409 }
Azure.IoT Build 13:848d52f93daf 410 else
Azure.IoT Build 13:848d52f93daf 411 {
Azure.IoT Build 13:848d52f93daf 412 result = true;
Azure.IoT Build 13:848d52f93daf 413 }
Azure.IoT Build 13:848d52f93daf 414 return result;
Azure.IoT Build 13:848d52f93daf 415 }
Azure.IoT Build 13:848d52f93daf 416
Azure.IoT Build 13:848d52f93daf 417 static void destroy_deviceKey(HTTPTRANSPORT_PERDEVICE_DATA* handleData)
Azure.IoT Build 13:848d52f93daf 418 {
Azure.IoT Build 13:848d52f93daf 419 STRING_delete(handleData->deviceKey);
Azure.IoT Build 13:848d52f93daf 420 handleData->deviceKey = NULL;
Azure.IoT Build 13:848d52f93daf 421 }
Azure.IoT Build 13:848d52f93daf 422
Azure.IoT Build 13:848d52f93daf 423 static bool create_deviceKey(HTTPTRANSPORT_PERDEVICE_DATA* handleData, const char * deviceKey)
Azure.IoT Build 13:848d52f93daf 424 {
Azure.IoT Build 13:848d52f93daf 425 /*Codes_SRS_TRANSPORTMULTITHTTP_17_135: [ IoTHubTransportHttp_Register shall create an immutable string (further called "deviceKey") from deviceKey. ]*/
Azure.IoT Build 13:848d52f93daf 426 bool result;
Azure.IoT Build 13:848d52f93daf 427 handleData->deviceKey = STRING_construct(deviceKey);
Azure.IoT Build 13:848d52f93daf 428 if (handleData->deviceKey == NULL)
Azure.IoT Build 13:848d52f93daf 429 {
Azure.IoT Build 13:848d52f93daf 430 /*Codes_SRS_TRANSPORTMULTITHTTP_17_136: [ If deviceKey is not created, then IoTHubTransportHttp_Register shall fail and return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 431 LogError("STRING_construct deviceKey failed");
Azure.IoT Build 13:848d52f93daf 432 result = false;
Azure.IoT Build 13:848d52f93daf 433 }
Azure.IoT Build 13:848d52f93daf 434 else
Azure.IoT Build 13:848d52f93daf 435 {
Azure.IoT Build 13:848d52f93daf 436 result = true;
Azure.IoT Build 13:848d52f93daf 437 }
Azure.IoT Build 13:848d52f93daf 438 return result;
Azure.IoT Build 13:848d52f93daf 439 }
Azure.IoT Build 13:848d52f93daf 440
Azure.IoT Build 13:848d52f93daf 441 /*
Azure.IoT Build 13:848d52f93daf 442 * List queries Find by handle and find by device name
Azure.IoT Build 13:848d52f93daf 443 */
Azure.IoT Build 13:848d52f93daf 444
Azure.IoT Build 13:848d52f93daf 445 /*Codes_SRS_TRANSPORTMULTITHTTP_17_137: [ IoTHubTransportHttp_Register shall search the devices list for any device matching name deviceId. If deviceId is found it shall return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 446 bool findDeviceHandle(const void* element, const void* value)
Azure.IoT Build 13:848d52f93daf 447 {
Azure.IoT Build 13:848d52f93daf 448 bool result;
Azure.IoT Build 13:848d52f93daf 449 /* data stored at element is device handle */
Azure.IoT Build 13:848d52f93daf 450 const IOTHUB_DEVICE_HANDLE * guess = (const IOTHUB_DEVICE_HANDLE *)element;
Azure.IoT Build 13:848d52f93daf 451 const IOTHUB_DEVICE_HANDLE match = (const IOTHUB_DEVICE_HANDLE)value;
Azure.IoT Build 13:848d52f93daf 452 result = (*guess == match) ? true : false;
Azure.IoT Build 13:848d52f93daf 453 return result;
Azure.IoT Build 13:848d52f93daf 454 }
Azure.IoT Build 13:848d52f93daf 455
Azure.IoT Build 13:848d52f93daf 456 static bool findDeviceById(const void* element, const void* value)
Azure.IoT Build 13:848d52f93daf 457 {
Azure.IoT Build 13:848d52f93daf 458 bool result;
Azure.IoT Build 13:848d52f93daf 459 const char* deviceId = (const char *)value;
Azure.IoT Build 13:848d52f93daf 460 const HTTPTRANSPORT_PERDEVICE_DATA * perDeviceElement = *(const HTTPTRANSPORT_PERDEVICE_DATA **)element;
Azure.IoT Build 13:848d52f93daf 461
Azure.IoT Build 13:848d52f93daf 462 result = (strcmp(STRING_c_str(perDeviceElement->deviceId), deviceId) == 0);
Azure.IoT Build 13:848d52f93daf 463
Azure.IoT Build 13:848d52f93daf 464 return result;
Azure.IoT Build 13:848d52f93daf 465 }
Azure.IoT Build 13:848d52f93daf 466
Azure.IoT Build 14:fae5c6b2cce3 467 IOTHUB_DEVICE_HANDLE IoTHubTransportHttp_Register(TRANSPORT_LL_HANDLE handle, const char* deviceId, const char* deviceKey, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle, PDLIST_ENTRY waitingToSend)
Azure.IoT Build 13:848d52f93daf 468 {
Azure.IoT Build 13:848d52f93daf 469 HTTPTRANSPORT_PERDEVICE_DATA* result;
Azure.IoT Build 13:848d52f93daf 470 if (handle == NULL)
Azure.IoT Build 13:848d52f93daf 471 {
Azure.IoT Build 13:848d52f93daf 472 /*Codes_SRS_TRANSPORTMULTITHTTP_17_142: [ If handle is NULL, then IoTHubTransportHttp_Register shall return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 473 LogError("Transport handle is NULL");
Azure.IoT Build 13:848d52f93daf 474 result = NULL;
Azure.IoT Build 13:848d52f93daf 475 }
Azure.IoT Build 13:848d52f93daf 476 else if (deviceId == NULL || deviceKey == NULL || waitingToSend == NULL || iotHubClientHandle == NULL)
Azure.IoT Build 13:848d52f93daf 477 {
Azure.IoT Build 13:848d52f93daf 478 /*Codes_SRS_TRANSPORTMULTITHTTP_17_015: [ If parameter deviceKey is NULL, then IoTHubTransportHttp_Register shall return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 479 /*Codes_SRS_TRANSPORTMULTITHTTP_17_014: [ If parameter deviceId is NULL, then IoTHubTransportHttp_Register shall return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 480 /*Codes_SRS_TRANSPORTMULTITHTTP_17_016: [ If parameter waitingToSend is NULL, then IoTHubTransportHttp_Register shall return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 481 /*Codes_SRS_TRANSPORTMULTITHTTP_17_143: [ If parameter iotHubClientHandle is NULL, then IoTHubTransportHttp_Register shall return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 482 LogError("All parameters must be non-NULL");
Azure.IoT Build 13:848d52f93daf 483 result = NULL;
Azure.IoT Build 13:848d52f93daf 484 }
Azure.IoT Build 13:848d52f93daf 485 else
Azure.IoT Build 13:848d52f93daf 486 {
Azure.IoT Build 13:848d52f93daf 487 HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle;
Azure.IoT Build 13:848d52f93daf 488 /*Codes_SRS_TRANSPORTMULTITHTTP_17_137: [ IoTHubTransportHttp_Register shall search the devices list for any device matching name deviceId. If deviceId is found it shall return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 489 void* listItem = VECTOR_find_if(handleData->perDeviceList, findDeviceById, deviceId);
Azure.IoT Build 13:848d52f93daf 490 if (listItem != NULL)
Azure.IoT Build 13:848d52f93daf 491 {
Azure.IoT Build 13:848d52f93daf 492 /*Codes_SRS_TRANSPORTMULTITHTTP_17_137: [ IoTHubTransportHttp_Register shall search the devices list for any device matching name deviceId. If deviceId is found it shall return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 493 LogError("Transport already has device registered by id: [%s]", deviceId);
Azure.IoT Build 13:848d52f93daf 494 result = NULL;
Azure.IoT Build 13:848d52f93daf 495 }
Azure.IoT Build 13:848d52f93daf 496 else
Azure.IoT Build 13:848d52f93daf 497 {
Azure.IoT Build 13:848d52f93daf 498 /*Codes_SRS_TRANSPORTMULTITHTTP_17_038: [ Otherwise, IoTHubTransportHttp_Register shall allocate the IOTHUB_DEVICE_HANDLE structure. ]*/
Azure.IoT Build 13:848d52f93daf 499 bool was_resultCreated_ok = ((result = malloc(sizeof(HTTPTRANSPORT_PERDEVICE_DATA))) != NULL);
Azure.IoT Build 13:848d52f93daf 500 bool was_create_deviceId_ok = was_resultCreated_ok && create_deviceId(result, deviceId);
Azure.IoT Build 13:848d52f93daf 501 bool was_create_deviceKey_ok = was_create_deviceId_ok && create_deviceKey(result, deviceKey);
Azure.IoT Build 13:848d52f93daf 502 bool was_eventHTTPrelativePath_ok = was_create_deviceKey_ok && create_eventHTTPrelativePath(result, deviceId);
Azure.IoT Build 13:848d52f93daf 503 bool was_messageHTTPrelativePath_ok = was_eventHTTPrelativePath_ok && create_messageHTTPrelativePath(result, deviceId);
Azure.IoT Build 13:848d52f93daf 504 bool was_eventHTTPrequestHeaders_ok = was_messageHTTPrelativePath_ok && create_eventHTTPrequestHeaders(result, deviceId);
Azure.IoT Build 13:848d52f93daf 505 bool was_messageHTTPrequestHeaders_ok = was_eventHTTPrequestHeaders_ok && create_messageHTTPrequestHeaders(result);
Azure.IoT Build 13:848d52f93daf 506 bool was_abandonHTTPrelativePathBegin_ok = was_messageHTTPrequestHeaders_ok && create_abandonHTTPrelativePathBegin(result, deviceId);
Azure.IoT Build 13:848d52f93daf 507 bool was_sasObject_ok = was_abandonHTTPrelativePathBegin_ok && create_deviceSASObject(result, handleData->hostName, deviceId, deviceKey);
Azure.IoT Build 13:848d52f93daf 508 /*Codes_SRS_TRANSPORTMULTITHTTP_17_041: [ IoTHubTransportHttp_Register shall call VECTOR_push_back to store the new device information. ]*/
Azure.IoT Build 13:848d52f93daf 509 bool was_list_add_ok = was_sasObject_ok && (VECTOR_push_back(handleData->perDeviceList, &result, 1) == 0);
Azure.IoT Build 13:848d52f93daf 510
Azure.IoT Build 13:848d52f93daf 511 if (was_list_add_ok)
Azure.IoT Build 13:848d52f93daf 512 {
Azure.IoT Build 13:848d52f93daf 513 /*Codes_SRS_TRANSPORTMULTITHTTP_17_043: [ Upon success, IoTHubTransportHttp_Register shall store the transport handle, iotHubClientHandle, and the waitingToSend queue in the device handle return a non-NULL value. ]*/
Azure.IoT Build 13:848d52f93daf 514 /*Codes_SRS_TRANSPORTMULTITHTTP_17_040: [ IoTHubTransportHttp_Register shall put event HTTP relative path, message HTTP relative path, event HTTP request headers, message HTTP request headers, abandonHTTPrelativePathBegin, HTTPAPIEX_SAS_HANDLE, and the device handle into a device structure. ]*/
Azure.IoT Build 13:848d52f93daf 515 /*Codes_SRS_TRANSPORTMULTITHTTP_17_128: [ IoTHubTransportHttp_Register shall mark this device as unsubscribed. ]*/
Azure.IoT Build 13:848d52f93daf 516 result->DoWork_PullMessage = false;
Azure.IoT Build 13:848d52f93daf 517 result->isFirstPoll = true;
Azure.IoT Build 13:848d52f93daf 518 result->iotHubClientHandle = iotHubClientHandle;
Azure.IoT Build 13:848d52f93daf 519 result->waitingToSend = waitingToSend;
Azure.IoT Build 13:848d52f93daf 520 DList_InitializeListHead(&(result->eventConfirmations));
Azure.IoT Build 13:848d52f93daf 521 result->transportHandle = handle;
Azure.IoT Build 13:848d52f93daf 522 }
Azure.IoT Build 13:848d52f93daf 523 else
Azure.IoT Build 13:848d52f93daf 524 {
Azure.IoT Build 13:848d52f93daf 525 /*Codes_SRS_TRANSPORTMULTITHTTP_17_042: [ If the list_add fails then IoTHubTransportHttp_Register shall fail and return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 526 if (was_sasObject_ok) destroy_SASObject(result);
Azure.IoT Build 13:848d52f93daf 527 if (was_abandonHTTPrelativePathBegin_ok) destroy_abandonHTTPrelativePathBegin(result);
Azure.IoT Build 13:848d52f93daf 528 if (was_messageHTTPrelativePath_ok) destroy_messageHTTPrelativePath(result);
Azure.IoT Build 13:848d52f93daf 529 if (was_eventHTTPrequestHeaders_ok) destroy_eventHTTPrequestHeaders(result);
Azure.IoT Build 13:848d52f93daf 530 if (was_messageHTTPrequestHeaders_ok) destroy_messageHTTPrequestHeaders(result);
Azure.IoT Build 13:848d52f93daf 531 if (was_eventHTTPrelativePath_ok) destroy_eventHTTPrelativePath(result);
Azure.IoT Build 13:848d52f93daf 532 if (was_create_deviceId_ok) destroy_deviceId(result);
Azure.IoT Build 13:848d52f93daf 533 if (was_create_deviceKey_ok) destroy_deviceKey(result);
Azure.IoT Build 13:848d52f93daf 534 /*Codes_SRS_TRANSPORTMULTITHTTP_17_039: [ If the allocating the device handle fails then IoTHubTransportHttp_Register shall fail and return NULL. ] */
Azure.IoT Build 13:848d52f93daf 535 if (was_resultCreated_ok) free(result);
Azure.IoT Build 13:848d52f93daf 536 result = NULL;
Azure.IoT Build 13:848d52f93daf 537 }
Azure.IoT Build 13:848d52f93daf 538 }
Azure.IoT Build 13:848d52f93daf 539
Azure.IoT Build 13:848d52f93daf 540 }
Azure.IoT Build 13:848d52f93daf 541 return (IOTHUB_DEVICE_HANDLE)result;
Azure.IoT Build 13:848d52f93daf 542 }
Azure.IoT Build 13:848d52f93daf 543
Azure.IoT Build 13:848d52f93daf 544
Azure.IoT Build 13:848d52f93daf 545 static void destroy_perDeviceData(HTTPTRANSPORT_PERDEVICE_DATA * perDeviceItem)
Azure.IoT Build 13:848d52f93daf 546 {
Azure.IoT Build 13:848d52f93daf 547 destroy_deviceId(perDeviceItem);
Azure.IoT Build 13:848d52f93daf 548 destroy_deviceKey(perDeviceItem);
Azure.IoT Build 13:848d52f93daf 549 destroy_eventHTTPrelativePath(perDeviceItem);
Azure.IoT Build 13:848d52f93daf 550 destroy_messageHTTPrelativePath(perDeviceItem);
Azure.IoT Build 13:848d52f93daf 551 destroy_eventHTTPrequestHeaders(perDeviceItem);
Azure.IoT Build 13:848d52f93daf 552 destroy_messageHTTPrequestHeaders(perDeviceItem);
Azure.IoT Build 13:848d52f93daf 553 destroy_abandonHTTPrelativePathBegin(perDeviceItem);
Azure.IoT Build 13:848d52f93daf 554 destroy_SASObject(perDeviceItem);
Azure.IoT Build 13:848d52f93daf 555 }
Azure.IoT Build 13:848d52f93daf 556
Azure.IoT Build 13:848d52f93daf 557 static IOTHUB_DEVICE_HANDLE* get_perDeviceDataItem(IOTHUB_DEVICE_HANDLE deviceHandle)
Azure.IoT Build 13:848d52f93daf 558 {
Azure.IoT Build 13:848d52f93daf 559 HTTPTRANSPORT_PERDEVICE_DATA* deviceHandleData = (HTTPTRANSPORT_PERDEVICE_DATA*)deviceHandle;
Azure.IoT Build 13:848d52f93daf 560 IOTHUB_DEVICE_HANDLE* listItem;
Azure.IoT Build 13:848d52f93daf 561
Azure.IoT Build 13:848d52f93daf 562 HTTPTRANSPORT_HANDLE_DATA* handleData = deviceHandleData->transportHandle;
Azure.IoT Build 13:848d52f93daf 563
Azure.IoT Build 13:848d52f93daf 564 listItem = VECTOR_find_if(handleData->perDeviceList, findDeviceHandle, deviceHandle);
Azure.IoT Build 13:848d52f93daf 565 if (listItem == NULL)
Azure.IoT Build 13:848d52f93daf 566 {
Azure.IoT Build 13:848d52f93daf 567 LogError("device handle not found in transport device list");
Azure.IoT Build 13:848d52f93daf 568 listItem = NULL;
Azure.IoT Build 13:848d52f93daf 569 }
Azure.IoT Build 13:848d52f93daf 570 else
Azure.IoT Build 13:848d52f93daf 571 {
Azure.IoT Build 13:848d52f93daf 572 /* sucessfully found device in list. */
Azure.IoT Build 13:848d52f93daf 573 }
Azure.IoT Build 13:848d52f93daf 574
Azure.IoT Build 13:848d52f93daf 575 return listItem;
Azure.IoT Build 13:848d52f93daf 576 }
Azure.IoT Build 13:848d52f93daf 577
Azure.IoT Build 13:848d52f93daf 578 void IoTHubTransportHttp_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle)
Azure.IoT Build 13:848d52f93daf 579 {
Azure.IoT Build 13:848d52f93daf 580 if (deviceHandle == NULL)
Azure.IoT Build 13:848d52f93daf 581 {
Azure.IoT Build 13:848d52f93daf 582 /*Codes_SRS_TRANSPORTMULTITHTTP_17_044: [ If deviceHandle is NULL, then IoTHubTransportHttp_Unregister shall do nothing. ]*/
Azure.IoT Build 13:848d52f93daf 583 LogError("Unregister a NULL device handle");
Azure.IoT Build 13:848d52f93daf 584 }
Azure.IoT Build 13:848d52f93daf 585 else
Azure.IoT Build 13:848d52f93daf 586 {
Azure.IoT Build 13:848d52f93daf 587 HTTPTRANSPORT_PERDEVICE_DATA* deviceHandleData = (HTTPTRANSPORT_PERDEVICE_DATA*)deviceHandle;
Azure.IoT Build 13:848d52f93daf 588 HTTPTRANSPORT_HANDLE_DATA* handleData = deviceHandleData->transportHandle;
Azure.IoT Build 13:848d52f93daf 589 /*Codes_SRS_TRANSPORTMULTITHTTP_17_045: [ IoTHubTransportHttp_Unregister shall locate deviceHandle in the transport device list by calling list_find_if. ]*/
Azure.IoT Build 13:848d52f93daf 590 IOTHUB_DEVICE_HANDLE* listItem = get_perDeviceDataItem(deviceHandle);
Azure.IoT Build 13:848d52f93daf 591 if (listItem == NULL)
Azure.IoT Build 13:848d52f93daf 592 {
Azure.IoT Build 13:848d52f93daf 593 /*Codes_SRS_TRANSPORTMULTITHTTP_17_046: [ If the device structure is not found, then this function shall fail and do nothing. ]*/
Azure.IoT Build 13:848d52f93daf 594 LogError("Device Handle [%p] not found in transport", deviceHandle);
Azure.IoT Build 13:848d52f93daf 595 }
Azure.IoT Build 13:848d52f93daf 596 else
Azure.IoT Build 13:848d52f93daf 597 {
Azure.IoT Build 13:848d52f93daf 598 HTTPTRANSPORT_PERDEVICE_DATA * perDeviceItem = (HTTPTRANSPORT_PERDEVICE_DATA *)(*listItem);
Azure.IoT Build 13:848d52f93daf 599
Azure.IoT Build 13:848d52f93daf 600 /*Codes_SRS_TRANSPORTMULTITHTTP_17_047: [ IoTHubTransportHttp_Unregister shall free all the resources used in the device structure. ]*/
Azure.IoT Build 13:848d52f93daf 601 destroy_perDeviceData(perDeviceItem);
Azure.IoT Build 13:848d52f93daf 602 /*Codes_SRS_TRANSPORTMULTITHTTP_17_048: [ IoTHubTransportHttp_Unregister shall call list_remove to remove device from devices list. ]*/
Azure.IoT Build 13:848d52f93daf 603 VECTOR_erase(handleData->perDeviceList, listItem, 1);
Azure.IoT Build 13:848d52f93daf 604 free(deviceHandleData);
Azure.IoT Build 13:848d52f93daf 605 }
Azure.IoT Build 13:848d52f93daf 606 }
Azure.IoT Build 13:848d52f93daf 607
Azure.IoT Build 13:848d52f93daf 608 return;
Azure.IoT Build 13:848d52f93daf 609 }
Azure.IoT Build 13:848d52f93daf 610
Azure.IoT Build 13:848d52f93daf 611
Azure.IoT Build 13:848d52f93daf 612 /*Codes_SRS_TRANSPORTMULTITHTTP_17_005: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called hostname) containing config->upperConfig->iotHubName + config->upperConfig->iotHubSuffix.]*/
AzureIoTClient 6:73793bae15ba 613 static void destroy_hostName(HTTPTRANSPORT_HANDLE_DATA* handleData)
AzureIoTClient 6:73793bae15ba 614 {
Azure.IoT Build 13:848d52f93daf 615 STRING_delete(handleData->hostName);
Azure.IoT Build 13:848d52f93daf 616 handleData->hostName = NULL;
AzureIoTClient 6:73793bae15ba 617 }
AzureIoTClient 6:73793bae15ba 618
Azure.IoT Build 13:848d52f93daf 619 /*Codes_SRS_TRANSPORTMULTITHTTP_17_005: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called hostname) containing config->upperConfig->iotHubName + config->upperConfig->iotHubSuffix.]*/
AzureIoTClient 6:73793bae15ba 620 static bool create_hostName(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 6:73793bae15ba 621 {
AzureIoTClient 6:73793bae15ba 622 bool result;
AzureIoTClient 6:73793bae15ba 623 handleData->hostName = STRING_construct(config->upperConfig->iotHubName);
AzureIoTClient 6:73793bae15ba 624 if (handleData->hostName == NULL)
AzureIoTClient 6:73793bae15ba 625 {
AzureIoTClient 6:73793bae15ba 626 result = false;
AzureIoTClient 6:73793bae15ba 627 }
AzureIoTClient 6:73793bae15ba 628 else
AzureIoTClient 6:73793bae15ba 629 {
AzureIoTClient 6:73793bae15ba 630 if ((STRING_concat(handleData->hostName, ".") != 0) ||
AzureIoTClient 6:73793bae15ba 631 (STRING_concat(handleData->hostName, config->upperConfig->iotHubSuffix) != 0))
AzureIoTClient 6:73793bae15ba 632 {
Azure.IoT Build 13:848d52f93daf 633 /*Codes_SRS_TRANSPORTMULTITHTTP_17_006: [ If creating the hostname fails then IoTHubTransportHttp_Create shall fail and return NULL. ] */
AzureIoTClient 6:73793bae15ba 634 destroy_hostName(handleData);
AzureIoTClient 6:73793bae15ba 635 result = false;
AzureIoTClient 6:73793bae15ba 636 }
AzureIoTClient 6:73793bae15ba 637 else
AzureIoTClient 6:73793bae15ba 638 {
AzureIoTClient 6:73793bae15ba 639 result = true;
AzureIoTClient 6:73793bae15ba 640 }
AzureIoTClient 6:73793bae15ba 641 }
AzureIoTClient 6:73793bae15ba 642 return result;
AzureIoTClient 6:73793bae15ba 643 }
AzureIoTClient 6:73793bae15ba 644
Azure.IoT Build 13:848d52f93daf 645 /*Codes_SRS_TRANSPORTMULTITHTTP_17_007: [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 6:73793bae15ba 646 static void destroy_httpApiExHandle(HTTPTRANSPORT_HANDLE_DATA* handleData)
AzureIoTClient 6:73793bae15ba 647 {
Azure.IoT Build 13:848d52f93daf 648 HTTPAPIEX_Destroy(handleData->httpApiExHandle);
Azure.IoT Build 13:848d52f93daf 649 handleData->httpApiExHandle = NULL;
AzureIoTClient 6:73793bae15ba 650 }
AzureIoTClient 6:73793bae15ba 651
Azure.IoT Build 13:848d52f93daf 652 /*Codes_SRS_TRANSPORTMULTITHTTP_17_007: [ 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 6:73793bae15ba 653 static bool create_httpApiExHandle(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 6:73793bae15ba 654 {
AzureIoTClient 6:73793bae15ba 655 bool result;
AzureIoTClient 6:73793bae15ba 656 (void)config;
Azure.IoT Build 13:848d52f93daf 657 handleData->httpApiExHandle = HTTPAPIEX_Create(STRING_c_str(handleData->hostName));
Azure.IoT Build 13:848d52f93daf 658 if (handleData->httpApiExHandle == NULL)
Azure.IoT Build 13:848d52f93daf 659 {
Azure.IoT Build 13:848d52f93daf 660 /*Codes_SRS_TRANSPORTMULTITHTTP_17_008: [ If creating the HTTPAPIEX_HANDLE fails then IoTHubTransportHttp_Create shall fail and return NULL. ] */
Azure.IoT Build 13:848d52f93daf 661 result = false;
Azure.IoT Build 13:848d52f93daf 662 }
Azure.IoT Build 13:848d52f93daf 663 else
Azure.IoT Build 13:848d52f93daf 664 {
Azure.IoT Build 13:848d52f93daf 665 result = true;
Azure.IoT Build 13:848d52f93daf 666 }
Azure.IoT Build 13:848d52f93daf 667 return result;
AzureIoTClient 6:73793bae15ba 668 }
AzureIoTClient 6:73793bae15ba 669
Azure.IoT Build 13:848d52f93daf 670 static void destroy_perDeviceList(HTTPTRANSPORT_HANDLE_DATA* handleData)
AzureIoTClient 6:73793bae15ba 671 {
Azure.IoT Build 13:848d52f93daf 672 VECTOR_destroy(handleData->perDeviceList);
Azure.IoT Build 13:848d52f93daf 673 handleData->perDeviceList = NULL;
AzureIoTClient 6:73793bae15ba 674 }
AzureIoTClient 6:73793bae15ba 675
Azure.IoT Build 13:848d52f93daf 676 /*Codes_SRS_TRANSPORTMULTITHTTP_17_009: [ IoTHubTransportHttp_Create shall call list_create to create a list of registered devices. ]*/
Azure.IoT Build 13:848d52f93daf 677 static bool create_perDeviceList(HTTPTRANSPORT_HANDLE_DATA* handleData)
AzureIoTClient 6:73793bae15ba 678 {
Azure.IoT Build 13:848d52f93daf 679 bool result;
Azure.IoT Build 13:848d52f93daf 680 handleData->perDeviceList = VECTOR_create(sizeof(IOTHUB_DEVICE_HANDLE));
Azure.IoT Build 13:848d52f93daf 681 if (handleData == NULL || handleData->perDeviceList == NULL)
Azure.IoT Build 13:848d52f93daf 682 {
Azure.IoT Build 13:848d52f93daf 683 /*Codes_SRS_TRANSPORTMULTITHTTP_17_010: [ If creating the list fails, then IoTHubTransportHttp_Create shall fail and return NULL. ]*/
Azure.IoT Build 13:848d52f93daf 684 result = false;
Azure.IoT Build 13:848d52f93daf 685 }
Azure.IoT Build 13:848d52f93daf 686 else
Azure.IoT Build 13:848d52f93daf 687 {
Azure.IoT Build 13:848d52f93daf 688 result = true;
Azure.IoT Build 13:848d52f93daf 689 }
Azure.IoT Build 13:848d52f93daf 690 return result;
AzureIoTClient 6:73793bae15ba 691 }
AzureIoTClient 6:73793bae15ba 692
Azure.IoT Build 13:848d52f93daf 693
Azure.IoT Build 14:fae5c6b2cce3 694 TRANSPORT_LL_HANDLE IoTHubTransportHttp_Create(const IOTHUBTRANSPORT_CONFIG* config)
AzureIoTClient 6:73793bae15ba 695 {
AzureIoTClient 6:73793bae15ba 696 HTTPTRANSPORT_HANDLE_DATA* result;
AzureIoTClient 6:73793bae15ba 697 if (config == NULL)
AzureIoTClient 6:73793bae15ba 698 {
Azure.IoT Build 13:848d52f93daf 699 /*Codes_SRS_TRANSPORTMULTITHTTP_17_001: [If parameter config is NULL, then IoTHubTransportHttp_Create shall return NULL.]*/
AzureIoTClient 6:73793bae15ba 700 LogError("invalid arg (configuration is missing)\r\n");
AzureIoTClient 6:73793bae15ba 701 result = NULL;
AzureIoTClient 6:73793bae15ba 702 }
AzureIoTClient 6:73793bae15ba 703 else if (config->upperConfig == NULL)
AzureIoTClient 6:73793bae15ba 704 {
Azure.IoT Build 13:848d52f93daf 705 /*Codes_SRS_TRANSPORTMULTITHTTP_17_002: [ If field transportConfig is NULL, then IoTHubTransportHttp_Create shall return NULL. ]*/
AzureIoTClient 6:73793bae15ba 706 LogError("invalid arg (upperConfig is NULL)\r\n");
AzureIoTClient 6:73793bae15ba 707 result = NULL;
AzureIoTClient 6:73793bae15ba 708 }
AzureIoTClient 6:73793bae15ba 709 else if (config->upperConfig->protocol == NULL)
AzureIoTClient 6:73793bae15ba 710 {
Azure.IoT Build 13:848d52f93daf 711 /*Codes_SRS_TRANSPORTMULTITHTTP_17_003: [ If fields protocol, iotHubName or iotHubSuffix in transportConfig are NULL, then IoTHubTransportHttp_Create shall return NULL. ]*/
AzureIoTClient 6:73793bae15ba 712 LogError("invalid arg (protocol is NULL)\r\n");
AzureIoTClient 6:73793bae15ba 713 result = NULL;
AzureIoTClient 6:73793bae15ba 714 }
AzureIoTClient 6:73793bae15ba 715 else if (config->upperConfig->iotHubName == NULL)
AzureIoTClient 6:73793bae15ba 716 {
Azure.IoT Build 13:848d52f93daf 717 /*Codes_SRS_TRANSPORTMULTITHTTP_17_003: [ If fields protocol, iotHubName or iotHubSuffix in transportConfig are NULL, then IoTHubTransportHttp_Create shall return NULL. ]*/
AzureIoTClient 6:73793bae15ba 718 LogError("invalid arg (iotHubName is NULL)\r\n");
AzureIoTClient 6:73793bae15ba 719 result = NULL;
AzureIoTClient 6:73793bae15ba 720 }
AzureIoTClient 6:73793bae15ba 721 else if (config->upperConfig->iotHubSuffix == NULL)
AzureIoTClient 6:73793bae15ba 722 {
Azure.IoT Build 13:848d52f93daf 723 /*Codes_SRS_TRANSPORTMULTITHTTP_17_003: [ If fields protocol, iotHubName or iotHubSuffix in transportConfig are NULL, then IoTHubTransportHttp_Create shall return NULL. ]*/
AzureIoTClient 6:73793bae15ba 724 LogError("invalid arg (iotHubSuffix is NULL)\r\n");
AzureIoTClient 6:73793bae15ba 725 result = NULL;
AzureIoTClient 6:73793bae15ba 726 }
AzureIoTClient 6:73793bae15ba 727 else
AzureIoTClient 6:73793bae15ba 728 {
Azure.IoT Build 13:848d52f93daf 729 /*Codes_SRS_TRANSPORTMULTITHTTP_17_130: [ IoTHubTransportHttp_Create shall allocate memory for the handle. ]*/
AzureIoTClient 6:73793bae15ba 730 result = (HTTPTRANSPORT_HANDLE_DATA*)malloc(sizeof(HTTPTRANSPORT_HANDLE_DATA));
AzureIoTClient 6:73793bae15ba 731 if (result == NULL)
AzureIoTClient 6:73793bae15ba 732 {
Azure.IoT Build 13:848d52f93daf 733 /*Codes_SRS_TRANSPORTMULTITHTTP_17_131: [ If allocation fails, IoTHubTransportHttp_Create shall fail and return NULL. ]*/
AzureIoTClient 6:73793bae15ba 734 LogError("unable to malloc\r\n");
AzureIoTClient 6:73793bae15ba 735 }
AzureIoTClient 6:73793bae15ba 736 else
AzureIoTClient 6:73793bae15ba 737 {
Azure.IoT Build 13:848d52f93daf 738 bool was_hostName_ok = create_hostName(result, config);
AzureIoTClient 6:73793bae15ba 739 bool was_httpApiExHandle_ok = was_hostName_ok && create_httpApiExHandle(result, config);
Azure.IoT Build 13:848d52f93daf 740 bool was_perDeviceList_ok = was_httpApiExHandle_ok && create_perDeviceList(result);
Azure.IoT Build 13:848d52f93daf 741
AzureIoTClient 6:73793bae15ba 742
Azure.IoT Build 13:848d52f93daf 743 if (was_perDeviceList_ok)
AzureIoTClient 6:73793bae15ba 744 {
Azure.IoT Build 13:848d52f93daf 745 /*Codes_SRS_TRANSPORTMULTITHTTP_17_011: [ Otherwise, IoTHubTransportHttp_Create shall succeed and return a non-NULL value. ]*/
AzureIoTClient 6:73793bae15ba 746 result->doBatchedTransfers = false;
AzureIoTClient 6:73793bae15ba 747 result->getMinimumPollingTime = DEFAULT_GETMINIMUMPOLLINGTIME;
AzureIoTClient 6:73793bae15ba 748 }
AzureIoTClient 6:73793bae15ba 749 else
AzureIoTClient 6:73793bae15ba 750 {
AzureIoTClient 6:73793bae15ba 751 if (was_httpApiExHandle_ok) destroy_httpApiExHandle(result);
Azure.IoT Build 13:848d52f93daf 752 if (was_hostName_ok) destroy_hostName(result);
AzureIoTClient 6:73793bae15ba 753
AzureIoTClient 6:73793bae15ba 754 free(result);
AzureIoTClient 6:73793bae15ba 755 result = NULL;
AzureIoTClient 6:73793bae15ba 756 }
AzureIoTClient 6:73793bae15ba 757 }
AzureIoTClient 6:73793bae15ba 758 }
AzureIoTClient 6:73793bae15ba 759 return result;
AzureIoTClient 6:73793bae15ba 760 }
AzureIoTClient 6:73793bae15ba 761
Azure.IoT Build 14:fae5c6b2cce3 762 void IoTHubTransportHttp_Destroy(TRANSPORT_LL_HANDLE handle)
AzureIoTClient 6:73793bae15ba 763 {
Azure.IoT Build 13:848d52f93daf 764 /*Codes_SRS_TRANSPORTMULTITHTTP_17_012: [ IoTHubTransportHttp_Destroy shall do nothing is handle is NULL. ]*/
AzureIoTClient 6:73793bae15ba 765 if (handle != NULL)
AzureIoTClient 6:73793bae15ba 766 {
Azure.IoT Build 13:848d52f93daf 767 HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle;
Azure.IoT Build 13:848d52f93daf 768 IOTHUB_DEVICE_HANDLE* listItem;
Azure.IoT Build 13:848d52f93daf 769
Azure.IoT Build 13:848d52f93daf 770 size_t deviceListSize = VECTOR_size(handleData->perDeviceList);
Azure.IoT Build 13:848d52f93daf 771
Azure.IoT Build 13:848d52f93daf 772 /*Codes_SRS_TRANSPORTMULTITHTTP_17_013: [ Otherwise, IoTHubTransportHttp_Destroy shall free all the resources currently in use. ]*/
Azure.IoT Build 13:848d52f93daf 773 for (size_t i = 0; i < deviceListSize; i++)
Azure.IoT Build 13:848d52f93daf 774 {
Azure.IoT Build 13:848d52f93daf 775 listItem = VECTOR_element(handleData->perDeviceList, i);
Azure.IoT Build 13:848d52f93daf 776 HTTPTRANSPORT_PERDEVICE_DATA* perDeviceItem = (HTTPTRANSPORT_PERDEVICE_DATA*)(*listItem);
Azure.IoT Build 13:848d52f93daf 777 destroy_perDeviceData(perDeviceItem);
Azure.IoT Build 13:848d52f93daf 778 free(perDeviceItem);
Azure.IoT Build 13:848d52f93daf 779 }
Azure.IoT Build 13:848d52f93daf 780
AzureIoTClient 6:73793bae15ba 781 destroy_hostName(handle);
AzureIoTClient 6:73793bae15ba 782 destroy_httpApiExHandle(handle);
Azure.IoT Build 13:848d52f93daf 783 destroy_perDeviceList(handle);
AzureIoTClient 6:73793bae15ba 784 free(handle);
AzureIoTClient 6:73793bae15ba 785 }
AzureIoTClient 6:73793bae15ba 786 }
AzureIoTClient 6:73793bae15ba 787
Azure.IoT Build 13:848d52f93daf 788 int IoTHubTransportHttp_Subscribe(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 6:73793bae15ba 789 {
AzureIoTClient 6:73793bae15ba 790 int result;
AzureIoTClient 6:73793bae15ba 791 if (handle == NULL)
AzureIoTClient 6:73793bae15ba 792 {
Azure.IoT Build 13:848d52f93daf 793 /*Codes_SRS_TRANSPORTMULTITHTTP_17_103: [ If parameter deviceHandle is NULL then IoTHubTransportHttp_Subscribe shall fail and return a non-zero value. ]*/
AzureIoTClient 6:73793bae15ba 794 LogError("invalid arg passed to IoTHubTransportHttp_Subscribe\r\n");
AzureIoTClient 6:73793bae15ba 795 result = __LINE__;
AzureIoTClient 6:73793bae15ba 796 }
AzureIoTClient 6:73793bae15ba 797 else
AzureIoTClient 6:73793bae15ba 798 {
Azure.IoT Build 13:848d52f93daf 799 /*Codes_SRS_TRANSPORTMULTITHTTP_17_104: [ IoTHubTransportHttp_Subscribe shall locate deviceHandle in the transport device list by calling list_find_if. ]*/
Azure.IoT Build 13:848d52f93daf 800 IOTHUB_DEVICE_HANDLE* listItem = get_perDeviceDataItem(handle);
Azure.IoT Build 13:848d52f93daf 801
Azure.IoT Build 13:848d52f93daf 802 if (listItem == NULL)
Azure.IoT Build 13:848d52f93daf 803 {
Azure.IoT Build 13:848d52f93daf 804 /*Codes_SRS_TRANSPORTMULTITHTTP_17_105: [ If the device structure is not found, then this function shall fail and return a non-zero value. ]*/
Azure.IoT Build 13:848d52f93daf 805 LogError("did not find device in transport handle");
Azure.IoT Build 13:848d52f93daf 806 result = __LINE__;
Azure.IoT Build 13:848d52f93daf 807 }
Azure.IoT Build 13:848d52f93daf 808 else
Azure.IoT Build 13:848d52f93daf 809 {
Azure.IoT Build 13:848d52f93daf 810 HTTPTRANSPORT_PERDEVICE_DATA * perDeviceItem;
Azure.IoT Build 13:848d52f93daf 811
Azure.IoT Build 13:848d52f93daf 812 perDeviceItem = (HTTPTRANSPORT_PERDEVICE_DATA *)(*listItem);
Azure.IoT Build 13:848d52f93daf 813 /*Codes_SRS_TRANSPORTMULTITHTTP_17_106: [ Otherwise, IoTHubTransportHttp_Subscribe shall set the device so that subsequent calls to DoWork should execute HTTP requests. ]*/
Azure.IoT Build 13:848d52f93daf 814 perDeviceItem->DoWork_PullMessage = true;
Azure.IoT Build 13:848d52f93daf 815 }
AzureIoTClient 6:73793bae15ba 816 result = 0;
AzureIoTClient 6:73793bae15ba 817 }
AzureIoTClient 6:73793bae15ba 818 return result;
AzureIoTClient 6:73793bae15ba 819 }
AzureIoTClient 6:73793bae15ba 820
Azure.IoT Build 13:848d52f93daf 821 void IoTHubTransportHttp_Unsubscribe(IOTHUB_DEVICE_HANDLE handle)
AzureIoTClient 6:73793bae15ba 822 {
Azure.IoT Build 13:848d52f93daf 823 /*Codes_SRS_TRANSPORTMULTITHTTP_17_107: [ If parameter deviceHandle is NULL then IoTHubTransportHttp_Unsubscribe shall fail do nothing. ]*/
AzureIoTClient 6:73793bae15ba 824 if (handle != NULL)
AzureIoTClient 6:73793bae15ba 825 {
Azure.IoT Build 13:848d52f93daf 826 /*Codes_SRS_TRANSPORTMULTITHTTP_17_108: [ IoTHubTransportHttp_Unsubscribe shall locate deviceHandle in the transport device list by calling list_find_if. ]*/
Azure.IoT Build 13:848d52f93daf 827 IOTHUB_DEVICE_HANDLE* listItem = get_perDeviceDataItem(handle);
Azure.IoT Build 13:848d52f93daf 828 /*Codes_SRS_TRANSPORTMULTITHTTP_17_109: [ If the device structure is not found, then this function shall fail and do nothing. ]*/
Azure.IoT Build 13:848d52f93daf 829 if (listItem != NULL)
Azure.IoT Build 13:848d52f93daf 830 {
Azure.IoT Build 13:848d52f93daf 831 HTTPTRANSPORT_PERDEVICE_DATA * perDeviceItem = (HTTPTRANSPORT_PERDEVICE_DATA *)(*listItem);
Azure.IoT Build 13:848d52f93daf 832 /*Codes_SRS_TRANSPORTMULTITHTTP_17_110: [ Otherwise, IoTHubTransportHttp_Subscribe shall set the device so that subsequent calls to DoWork shall not execute HTTP requests. ]*/
Azure.IoT Build 13:848d52f93daf 833 perDeviceItem->DoWork_PullMessage = false;
AzureIoTClient 6:73793bae15ba 834 }
Azure.IoT Build 13:848d52f93daf 835 else
Azure.IoT Build 13:848d52f93daf 836 {
Azure.IoT Build 13:848d52f93daf 837 LogError("Device not found to unsuscribe.\r\n");
Azure.IoT Build 13:848d52f93daf 838 }
Azure.IoT Build 13:848d52f93daf 839 }
Azure.IoT Build 13:848d52f93daf 840 else
Azure.IoT Build 13:848d52f93daf 841 {
Azure.IoT Build 13:848d52f93daf 842 LogError("Null handle passed to Unsuscribe.\r\n");
Azure.IoT Build 13:848d52f93daf 843 }
AzureIoTClient 6:73793bae15ba 844 }
AzureIoTClient 6:73793bae15ba 845
AzureIoTClient 6:73793bae15ba 846 /*produces a representation of the properties, if they exist*/
AzureIoTClient 6:73793bae15ba 847 /*if they do not exist, produces ""*/
AzureIoTClient 6:73793bae15ba 848 static int concat_Properties(STRING_HANDLE existing, MAP_HANDLE map, size_t* propertiesMessageSizeContribution)
AzureIoTClient 6:73793bae15ba 849 {
AzureIoTClient 6:73793bae15ba 850 int result;
AzureIoTClient 6:73793bae15ba 851 const char*const* keys;
AzureIoTClient 6:73793bae15ba 852 const char*const* values;
AzureIoTClient 6:73793bae15ba 853 size_t count;
AzureIoTClient 6:73793bae15ba 854 if (Map_GetInternals(map, &keys, &values, &count) != MAP_OK)
AzureIoTClient 6:73793bae15ba 855 {
AzureIoTClient 6:73793bae15ba 856 result = __LINE__;
AzureIoTClient 6:73793bae15ba 857 LogError("error while Map_GetInternals\r\n");
AzureIoTClient 6:73793bae15ba 858 }
AzureIoTClient 6:73793bae15ba 859 else
AzureIoTClient 6:73793bae15ba 860 {
AzureIoTClient 6:73793bae15ba 861
AzureIoTClient 6:73793bae15ba 862 if (count == 0)
AzureIoTClient 6:73793bae15ba 863 {
Azure.IoT Build 13:848d52f93daf 864 /*Codes_SRS_TRANSPORTMULTITHTTP_17_064: [If IoTHubMessage does not have properties, then "properties":{...} shall be missing from the payload*/
AzureIoTClient 6:73793bae15ba 865 /*no properties - do nothing with existing*/
AzureIoTClient 6:73793bae15ba 866 result = 0;
AzureIoTClient 6:73793bae15ba 867 *propertiesMessageSizeContribution = 0;
AzureIoTClient 6:73793bae15ba 868 }
AzureIoTClient 6:73793bae15ba 869 else
AzureIoTClient 6:73793bae15ba 870 {
Azure.IoT Build 13:848d52f93daf 871 /*Codes_SRS_TRANSPORTMULTITHTTP_17_058: [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 6:73793bae15ba 872 if (STRING_concat(existing, ",\"properties\":") != 0)
AzureIoTClient 6:73793bae15ba 873 {
AzureIoTClient 6:73793bae15ba 874 /*go ahead and return it*/
AzureIoTClient 6:73793bae15ba 875 result = __LINE__;
AzureIoTClient 6:73793bae15ba 876 LogError("failed STRING_concat\r\n");
AzureIoTClient 6:73793bae15ba 877 }
AzureIoTClient 6:73793bae15ba 878 else if (appendMapToJSON(existing, keys, values, count) != 0)
AzureIoTClient 6:73793bae15ba 879 {
AzureIoTClient 6:73793bae15ba 880 result = __LINE__;
AzureIoTClient 6:73793bae15ba 881 LogError("unable to append the properties\r\n");
AzureIoTClient 6:73793bae15ba 882 }
AzureIoTClient 6:73793bae15ba 883 else
AzureIoTClient 6:73793bae15ba 884 {
AzureIoTClient 6:73793bae15ba 885 /*all is fine*/
AzureIoTClient 6:73793bae15ba 886 size_t i;
AzureIoTClient 6:73793bae15ba 887 *propertiesMessageSizeContribution = 0;
AzureIoTClient 6:73793bae15ba 888 for (i = 0;i < count;i++)
AzureIoTClient 6:73793bae15ba 889 {
Azure.IoT Build 13:848d52f93daf 890 /*Codes_SRS_TRANSPORTMULTITHTTP_17_063: [Every property name shall add to the message size the length of the property name + the length of the property value + 16 bytes.] */
AzureIoTClient 6:73793bae15ba 891 *propertiesMessageSizeContribution += (strlen(keys[i]) + strlen(values[i]) + MAXIMUM_PROPERTY_OVERHEAD);
AzureIoTClient 6:73793bae15ba 892 }
AzureIoTClient 6:73793bae15ba 893 result = 0;
AzureIoTClient 6:73793bae15ba 894 }
AzureIoTClient 6:73793bae15ba 895 }
AzureIoTClient 6:73793bae15ba 896 }
AzureIoTClient 6:73793bae15ba 897 return result;
AzureIoTClient 6:73793bae15ba 898 }
AzureIoTClient 6:73793bae15ba 899
AzureIoTClient 6:73793bae15ba 900 /*produces a JSON representation of the map : {"a": "value_of_a","b":"value_of_b"}*/
AzureIoTClient 6:73793bae15ba 901 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 6:73793bae15ba 902 {
AzureIoTClient 6:73793bae15ba 903 int result;
AzureIoTClient 6:73793bae15ba 904 if (STRING_concat(existing, "{") != 0)
AzureIoTClient 6:73793bae15ba 905 {
AzureIoTClient 6:73793bae15ba 906 /*go on and return it*/
AzureIoTClient 6:73793bae15ba 907 result = __LINE__;
AzureIoTClient 6:73793bae15ba 908 LogError("STRING_construct failed\r\n");
AzureIoTClient 6:73793bae15ba 909 }
AzureIoTClient 6:73793bae15ba 910 else
AzureIoTClient 6:73793bae15ba 911 {
AzureIoTClient 6:73793bae15ba 912 size_t i;
AzureIoTClient 6:73793bae15ba 913 for (i = 0; i < count; i++)
AzureIoTClient 6:73793bae15ba 914 {
AzureIoTClient 6:73793bae15ba 915 if (!(
AzureIoTClient 6:73793bae15ba 916 (STRING_concat(existing, (i == 0) ? "\"" IOTHUB_APP_PREFIX : ",\"" IOTHUB_APP_PREFIX) == 0) &&
AzureIoTClient 6:73793bae15ba 917 (STRING_concat(existing, keys[i]) == 0) &&
AzureIoTClient 6:73793bae15ba 918 (STRING_concat(existing, "\":\"") == 0) &&
AzureIoTClient 6:73793bae15ba 919 (STRING_concat(existing, values[i]) == 0) &&
AzureIoTClient 6:73793bae15ba 920 (STRING_concat(existing, "\"") == 0)
AzureIoTClient 6:73793bae15ba 921 ))
AzureIoTClient 6:73793bae15ba 922 {
AzureIoTClient 6:73793bae15ba 923 LogError("unable to STRING_concat\r\n");
AzureIoTClient 6:73793bae15ba 924 break;
AzureIoTClient 6:73793bae15ba 925 }
AzureIoTClient 6:73793bae15ba 926 }
AzureIoTClient 6:73793bae15ba 927
AzureIoTClient 6:73793bae15ba 928 if (i < count)
AzureIoTClient 6:73793bae15ba 929 {
AzureIoTClient 6:73793bae15ba 930 result = __LINE__;
AzureIoTClient 6:73793bae15ba 931 /*error, let it go through*/
AzureIoTClient 6:73793bae15ba 932 }
AzureIoTClient 6:73793bae15ba 933 else if (STRING_concat(existing, "}") != 0)
AzureIoTClient 6:73793bae15ba 934 {
AzureIoTClient 6:73793bae15ba 935 result = __LINE__;
AzureIoTClient 6:73793bae15ba 936 LogError("unable to STRING_concat\r\n");
AzureIoTClient 6:73793bae15ba 937 }
AzureIoTClient 6:73793bae15ba 938 else
AzureIoTClient 6:73793bae15ba 939 {
AzureIoTClient 6:73793bae15ba 940 /*all is fine*/
AzureIoTClient 6:73793bae15ba 941 result = 0;
AzureIoTClient 6:73793bae15ba 942 }
AzureIoTClient 6:73793bae15ba 943 }
AzureIoTClient 6:73793bae15ba 944 return result;
AzureIoTClient 6:73793bae15ba 945 }
AzureIoTClient 6:73793bae15ba 946
AzureIoTClient 6:73793bae15ba 947 /*makes the following string:{"body":"base64 encoding of the message content"[,"properties":{"a":"valueOfA"}]}*/
AzureIoTClient 6:73793bae15ba 948 /*return NULL if there was a failure, or a non-NULL STRING_HANDLE that contains the intended data*/
AzureIoTClient 6:73793bae15ba 949 static STRING_HANDLE make1EventJSONitem(PDLIST_ENTRY item, size_t *messageSizeContribution)
AzureIoTClient 6:73793bae15ba 950 {
AzureIoTClient 6:73793bae15ba 951 STRING_HANDLE result; /*temp wants to contain :{"body":"base64 encoding of the message content"[,"properties":{"a":"valueOfA"}]}*/
AzureIoTClient 6:73793bae15ba 952 IOTHUB_MESSAGE_LIST* message = containingRecord(item, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 6:73793bae15ba 953 IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message->messageHandle);
AzureIoTClient 6:73793bae15ba 954
AzureIoTClient 6:73793bae15ba 955 switch (contentType)
AzureIoTClient 6:73793bae15ba 956 {
AzureIoTClient 6:73793bae15ba 957 case IOTHUBMESSAGE_BYTEARRAY:
AzureIoTClient 6:73793bae15ba 958 {
AzureIoTClient 6:73793bae15ba 959 result = STRING_construct("{\"body\":\"");
AzureIoTClient 6:73793bae15ba 960 if (result == NULL)
AzureIoTClient 6:73793bae15ba 961 {
AzureIoTClient 6:73793bae15ba 962 LogError("unable to STRING_construct\r\n");
AzureIoTClient 6:73793bae15ba 963 }
AzureIoTClient 6:73793bae15ba 964 else
AzureIoTClient 6:73793bae15ba 965 {
AzureIoTClient 6:73793bae15ba 966 const unsigned char* source;
AzureIoTClient 6:73793bae15ba 967 size_t size;
AzureIoTClient 6:73793bae15ba 968
AzureIoTClient 6:73793bae15ba 969 if (IoTHubMessage_GetByteArray(message->messageHandle, &source, &size) != IOTHUB_MESSAGE_OK)
AzureIoTClient 6:73793bae15ba 970 {
AzureIoTClient 6:73793bae15ba 971 LogError("unable to get the data for the message.\r\n");
AzureIoTClient 6:73793bae15ba 972 STRING_delete(result);
AzureIoTClient 6:73793bae15ba 973 result = NULL;
AzureIoTClient 6:73793bae15ba 974 }
AzureIoTClient 6:73793bae15ba 975 else
AzureIoTClient 6:73793bae15ba 976 {
AzureIoTClient 6:73793bae15ba 977 STRING_HANDLE encoded = Base64_Encode_Bytes(source, size);
AzureIoTClient 6:73793bae15ba 978 if (encoded == NULL)
AzureIoTClient 6:73793bae15ba 979 {
AzureIoTClient 6:73793bae15ba 980 LogError("unable to Base64_Encode_Bytes.\r\n");
AzureIoTClient 6:73793bae15ba 981 STRING_delete(result);
AzureIoTClient 6:73793bae15ba 982 result = NULL;
AzureIoTClient 6:73793bae15ba 983 }
AzureIoTClient 6:73793bae15ba 984 else
AzureIoTClient 6:73793bae15ba 985 {
AzureIoTClient 6:73793bae15ba 986 size_t propertiesSize;
AzureIoTClient 6:73793bae15ba 987 if (!(
AzureIoTClient 6:73793bae15ba 988 (STRING_concat_with_STRING(result, encoded) == 0) &&
AzureIoTClient 6:73793bae15ba 989 (STRING_concat(result, "\"") == 0) && /*\" because closing value*/
AzureIoTClient 6:73793bae15ba 990 (concat_Properties(result, IoTHubMessage_Properties(message->messageHandle), &propertiesSize) == 0) &&
AzureIoTClient 6:73793bae15ba 991 (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 6:73793bae15ba 992 ))
AzureIoTClient 6:73793bae15ba 993 {
AzureIoTClient 6:73793bae15ba 994 STRING_delete(result);
AzureIoTClient 6:73793bae15ba 995 result = NULL;
AzureIoTClient 6:73793bae15ba 996 LogError("unable to STRING_concat_with_STRING.\r\n");
AzureIoTClient 6:73793bae15ba 997 }
AzureIoTClient 6:73793bae15ba 998 else
AzureIoTClient 6:73793bae15ba 999 {
AzureIoTClient 6:73793bae15ba 1000 /*all is fine... */
Azure.IoT Build 13:848d52f93daf 1001 /*Codes_SRS_TRANSPORTMULTITHTTP_17_062: [The message size is computed from the length of the payload + 384.] */
AzureIoTClient 6:73793bae15ba 1002 *messageSizeContribution = size + MAXIMUM_PAYLOAD_OVERHEAD + propertiesSize;
AzureIoTClient 6:73793bae15ba 1003 }
AzureIoTClient 6:73793bae15ba 1004 STRING_delete(encoded);
AzureIoTClient 6:73793bae15ba 1005 }
AzureIoTClient 6:73793bae15ba 1006 }
AzureIoTClient 6:73793bae15ba 1007 }
AzureIoTClient 6:73793bae15ba 1008 break;
AzureIoTClient 6:73793bae15ba 1009 }
Azure.IoT Build 13:848d52f93daf 1010 /*Codes_SRS_TRANSPORTMULTITHTTP_17_057: [If a messages to be send has type IOTHUBMESSAGE_STRING, then its serialization shall be {"body":"JSON encoding of the string", "base64Encoded":false}] */
AzureIoTClient 6:73793bae15ba 1011 case IOTHUBMESSAGE_STRING:
AzureIoTClient 6:73793bae15ba 1012 {
AzureIoTClient 6:73793bae15ba 1013 result = STRING_construct("{\"body\":");
AzureIoTClient 6:73793bae15ba 1014 if (result == NULL)
AzureIoTClient 6:73793bae15ba 1015 {
AzureIoTClient 6:73793bae15ba 1016 LogError("unable to STRING_construct\r\n");
AzureIoTClient 6:73793bae15ba 1017 }
AzureIoTClient 6:73793bae15ba 1018 else
AzureIoTClient 6:73793bae15ba 1019 {
AzureIoTClient 6:73793bae15ba 1020 const char* source = IoTHubMessage_GetString(message->messageHandle);
AzureIoTClient 6:73793bae15ba 1021 if (source == NULL)
AzureIoTClient 6:73793bae15ba 1022 {
AzureIoTClient 6:73793bae15ba 1023 LogError("unable to IoTHubMessage_GetString\r\n");
AzureIoTClient 6:73793bae15ba 1024 STRING_delete(result);
AzureIoTClient 6:73793bae15ba 1025 result = NULL;
AzureIoTClient 6:73793bae15ba 1026 }
AzureIoTClient 6:73793bae15ba 1027 else
AzureIoTClient 6:73793bae15ba 1028 {
AzureIoTClient 6:73793bae15ba 1029 STRING_HANDLE asJson = STRING_new_JSON(source);
AzureIoTClient 6:73793bae15ba 1030 if (asJson == NULL)
AzureIoTClient 6:73793bae15ba 1031 {
AzureIoTClient 6:73793bae15ba 1032 LogError("unable to STRING_new_JSON\r\n");
AzureIoTClient 6:73793bae15ba 1033 STRING_delete(result);
AzureIoTClient 6:73793bae15ba 1034 result = NULL;
AzureIoTClient 6:73793bae15ba 1035 }
AzureIoTClient 6:73793bae15ba 1036 else
AzureIoTClient 6:73793bae15ba 1037 {
AzureIoTClient 6:73793bae15ba 1038 size_t propertiesSize;
AzureIoTClient 6:73793bae15ba 1039 if (!(
AzureIoTClient 6:73793bae15ba 1040 (STRING_concat_with_STRING(result, asJson) == 0) &&
AzureIoTClient 6:73793bae15ba 1041 (STRING_concat(result, ",\"base64Encoded\":false") == 0) &&
AzureIoTClient 6:73793bae15ba 1042 (concat_Properties(result, IoTHubMessage_Properties(message->messageHandle), &propertiesSize) == 0) &&
AzureIoTClient 6:73793bae15ba 1043 (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 6:73793bae15ba 1044 ))
AzureIoTClient 6:73793bae15ba 1045 {
AzureIoTClient 6:73793bae15ba 1046 LogError("unable to STRING_concat_with_STRING");
AzureIoTClient 6:73793bae15ba 1047 STRING_delete(result);
AzureIoTClient 6:73793bae15ba 1048 result = NULL;
AzureIoTClient 6:73793bae15ba 1049 }
AzureIoTClient 6:73793bae15ba 1050 else
AzureIoTClient 6:73793bae15ba 1051 {
AzureIoTClient 6:73793bae15ba 1052 /*result has the intended content*/
Azure.IoT Build 13:848d52f93daf 1053 /*Codes_SRS_TRANSPORTMULTITHTTP_17_062: [The message size is computed from the length of the payload + 384.] */
AzureIoTClient 6:73793bae15ba 1054 *messageSizeContribution = strlen(source) + MAXIMUM_PAYLOAD_OVERHEAD + propertiesSize;
AzureIoTClient 6:73793bae15ba 1055 }
AzureIoTClient 6:73793bae15ba 1056 STRING_delete(asJson);
AzureIoTClient 6:73793bae15ba 1057 }
AzureIoTClient 6:73793bae15ba 1058 }
AzureIoTClient 6:73793bae15ba 1059 }
AzureIoTClient 6:73793bae15ba 1060 break;
AzureIoTClient 6:73793bae15ba 1061 }
AzureIoTClient 6:73793bae15ba 1062 default:
AzureIoTClient 6:73793bae15ba 1063 {
AzureIoTClient 6:73793bae15ba 1064 LogError("an unknown message type was encountered (%d)\r\n", contentType);
AzureIoTClient 6:73793bae15ba 1065 result = NULL; /*unknown message type*/
AzureIoTClient 6:73793bae15ba 1066 break;
AzureIoTClient 6:73793bae15ba 1067 }
AzureIoTClient 6:73793bae15ba 1068 }
AzureIoTClient 6:73793bae15ba 1069 return result;
AzureIoTClient 6:73793bae15ba 1070 }
AzureIoTClient 6:73793bae15ba 1071
AzureIoTClient 6:73793bae15ba 1072 #define MAKE_PAYLOAD_RESULT_VALUES \
AzureIoTClient 6:73793bae15ba 1073 MAKE_PAYLOAD_OK, /*returned when there is a payload to be later send by HTTP*/ \
AzureIoTClient 6:73793bae15ba 1074 MAKE_PAYLOAD_NO_ITEMS, /*returned when there are no items to be send*/ \
AzureIoTClient 6:73793bae15ba 1075 MAKE_PAYLOAD_ERROR, /*returned when there were errors*/ \
AzureIoTClient 6:73793bae15ba 1076 MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT /*returned when the first item doesn't fit*/
AzureIoTClient 6:73793bae15ba 1077
AzureIoTClient 6:73793bae15ba 1078 DEFINE_ENUM(MAKE_PAYLOAD_RESULT, MAKE_PAYLOAD_RESULT_VALUES);
AzureIoTClient 6:73793bae15ba 1079
AzureIoTClient 6:73793bae15ba 1080 /*this function assembles several {"body":"base64 encoding of the message content"," base64Encoded": true} into 1 payload*/
Azure.IoT Build 13:848d52f93daf 1081 /*Codes_SRS_TRANSPORTMULTITHTTP_17_056: [IoTHubTransportHttp_DoWork shall build the following string:[{"body":"base64 encoding of the message1 content"},{"body":"base64 encoding of the message2 content"}...]]*/
Azure.IoT Build 13:848d52f93daf 1082 static MAKE_PAYLOAD_RESULT makePayload(HTTPTRANSPORT_PERDEVICE_DATA* deviceData, STRING_HANDLE* payload)
AzureIoTClient 6:73793bae15ba 1083 {
AzureIoTClient 6:73793bae15ba 1084 MAKE_PAYLOAD_RESULT result;
AzureIoTClient 6:73793bae15ba 1085 size_t allMessagesSize = 0;
AzureIoTClient 6:73793bae15ba 1086 *payload = STRING_construct("[");
AzureIoTClient 6:73793bae15ba 1087 if (*payload == NULL)
AzureIoTClient 6:73793bae15ba 1088 {
AzureIoTClient 6:73793bae15ba 1089 LogError("unable to STRING_construct\r\n");
AzureIoTClient 6:73793bae15ba 1090 result = MAKE_PAYLOAD_ERROR;
AzureIoTClient 6:73793bae15ba 1091 }
AzureIoTClient 6:73793bae15ba 1092 else
AzureIoTClient 6:73793bae15ba 1093 {
AzureIoTClient 6:73793bae15ba 1094 bool isFirst = true;
AzureIoTClient 6:73793bae15ba 1095 PDLIST_ENTRY actual;
AzureIoTClient 10:b012891d0a54 1096 bool keepGoing = true; /*keepGoing gets sometimes to false from within the loop*/
AzureIoTClient 6:73793bae15ba 1097 /*either all the items enter the list or only some*/
AzureIoTClient 6:73793bae15ba 1098 result = MAKE_PAYLOAD_OK; /*optimistically initializing it*/
Azure.IoT Build 13:848d52f93daf 1099 while (keepGoing && ((actual = deviceData->waitingToSend->Flink) != deviceData->waitingToSend))
AzureIoTClient 6:73793bae15ba 1100 {
AzureIoTClient 6:73793bae15ba 1101 size_t messageSize;
AzureIoTClient 6:73793bae15ba 1102 STRING_HANDLE temp = make1EventJSONitem(actual, &messageSize);
AzureIoTClient 6:73793bae15ba 1103 if (isFirst)
AzureIoTClient 6:73793bae15ba 1104 {
AzureIoTClient 6:73793bae15ba 1105 isFirst = false;
Azure.IoT Build 13:848d52f93daf 1106 /*Codes_SRS_TRANSPORTMULTITHTTP_17_067: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/
AzureIoTClient 6:73793bae15ba 1107 if (temp == NULL) /*first item failed to create, nothing to send*/
AzureIoTClient 6:73793bae15ba 1108 {
AzureIoTClient 6:73793bae15ba 1109 result = MAKE_PAYLOAD_ERROR;
AzureIoTClient 6:73793bae15ba 1110 STRING_delete(*payload);
AzureIoTClient 6:73793bae15ba 1111 *payload = NULL;
AzureIoTClient 6:73793bae15ba 1112 keepGoing = false;
AzureIoTClient 6:73793bae15ba 1113 }
AzureIoTClient 6:73793bae15ba 1114 else
AzureIoTClient 6:73793bae15ba 1115 {
Azure.IoT Build 13:848d52f93daf 1116 /*Codes_SRS_TRANSPORTMULTITHTTP_17_065: [If the oldest message in waitingToSend causes the message size to exceed the message size limit 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.]*/
Azure.IoT Build 13:848d52f93daf 1117 /*Codes_SRS_TRANSPORTMULTITHTTP_17_061: [The message size shall be limited to 255KB - 1 byte.]*/
AzureIoTClient 6:73793bae15ba 1118 if (messageSize > MAXIMUM_MESSAGE_SIZE)
AzureIoTClient 6:73793bae15ba 1119 {
Azure.IoT Build 13:848d52f93daf 1120 PDLIST_ENTRY head = DList_RemoveHeadList(deviceData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/
Azure.IoT Build 13:848d52f93daf 1121 DList_InsertTailList(&(deviceData->eventConfirmations), head);
AzureIoTClient 6:73793bae15ba 1122 result = MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT;
AzureIoTClient 6:73793bae15ba 1123 STRING_delete(*payload);
AzureIoTClient 6:73793bae15ba 1124 *payload = NULL;
AzureIoTClient 6:73793bae15ba 1125 keepGoing = false;
AzureIoTClient 6:73793bae15ba 1126 }
AzureIoTClient 6:73793bae15ba 1127 else
AzureIoTClient 6:73793bae15ba 1128 {
AzureIoTClient 6:73793bae15ba 1129 if (STRING_concat_with_STRING(*payload, temp) != 0)
AzureIoTClient 6:73793bae15ba 1130 {
Azure.IoT Build 13:848d52f93daf 1131 /*Codes_SRS_TRANSPORTMULTITHTTP_17_067: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/
AzureIoTClient 6:73793bae15ba 1132 result = MAKE_PAYLOAD_ERROR;
AzureIoTClient 6:73793bae15ba 1133 STRING_delete(*payload);
AzureIoTClient 6:73793bae15ba 1134 *payload = NULL;
AzureIoTClient 6:73793bae15ba 1135 keepGoing = false;
AzureIoTClient 6:73793bae15ba 1136 }
AzureIoTClient 6:73793bae15ba 1137 else
AzureIoTClient 6:73793bae15ba 1138 {
AzureIoTClient 6:73793bae15ba 1139 /*first item was put nicely in the payload*/
Azure.IoT Build 13:848d52f93daf 1140 PDLIST_ENTRY head = DList_RemoveHeadList(deviceData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/
Azure.IoT Build 13:848d52f93daf 1141 DList_InsertTailList(&(deviceData->eventConfirmations), head);
AzureIoTClient 6:73793bae15ba 1142 allMessagesSize += messageSize;
AzureIoTClient 6:73793bae15ba 1143 }
AzureIoTClient 6:73793bae15ba 1144 }
AzureIoTClient 6:73793bae15ba 1145 STRING_delete(temp);
AzureIoTClient 6:73793bae15ba 1146 }
AzureIoTClient 6:73793bae15ba 1147 }
AzureIoTClient 6:73793bae15ba 1148 else
AzureIoTClient 6:73793bae15ba 1149 {
AzureIoTClient 6:73793bae15ba 1150 /*there is at least 1 item already in the payload*/
AzureIoTClient 6:73793bae15ba 1151 if (temp == NULL)
AzureIoTClient 6:73793bae15ba 1152 {
AzureIoTClient 6:73793bae15ba 1153 /*there are multiple payloads encoded, the last one had an internal error, just go with those - closing the payload happens "after the loop"*/
Azure.IoT Build 13:848d52f93daf 1154 /*Codes_SRS_TRANSPORTMULTITHTTP_17_066: [If at any point during construction of the string there are errors, IoTHubTransportHttp_DoWork shall use the so far constructed string as payload.]*/
AzureIoTClient 6:73793bae15ba 1155 result = MAKE_PAYLOAD_OK;
AzureIoTClient 6:73793bae15ba 1156 keepGoing = false;
AzureIoTClient 6:73793bae15ba 1157 }
AzureIoTClient 6:73793bae15ba 1158 else
AzureIoTClient 6:73793bae15ba 1159 {
AzureIoTClient 6:73793bae15ba 1160 if (allMessagesSize + messageSize > MAXIMUM_MESSAGE_SIZE)
AzureIoTClient 6:73793bae15ba 1161 {
AzureIoTClient 6:73793bae15ba 1162 /*this item doesn't make it to the payload, but the payload is valid so far*/
Azure.IoT Build 13:848d52f93daf 1163 /*Codes_SRS_TRANSPORTMULTITHTTP_17_066: [If at any point during construction of the string there are errors, IoTHubTransportHttp_DoWork shall use the so far constructed string as payload.]*/
AzureIoTClient 6:73793bae15ba 1164 result = MAKE_PAYLOAD_OK;
AzureIoTClient 6:73793bae15ba 1165 keepGoing = false;
AzureIoTClient 6:73793bae15ba 1166 }
AzureIoTClient 6:73793bae15ba 1167 else if (STRING_concat_with_STRING(*payload, temp) != 0)
AzureIoTClient 6:73793bae15ba 1168 {
AzureIoTClient 6:73793bae15ba 1169 /*should still send what there is so far...*/
Azure.IoT Build 13:848d52f93daf 1170 /*Codes_SRS_TRANSPORTMULTITHTTP_17_066: [If at any point during construction of the string there are errors, IoTHubTransportHttp_DoWork shall use the so far constructed string as payload.]*/
AzureIoTClient 6:73793bae15ba 1171 result = MAKE_PAYLOAD_OK;
AzureIoTClient 6:73793bae15ba 1172 keepGoing = false;
AzureIoTClient 6:73793bae15ba 1173 }
AzureIoTClient 6:73793bae15ba 1174 else
AzureIoTClient 6:73793bae15ba 1175 {
AzureIoTClient 6:73793bae15ba 1176 /*cool, the payload made it there, let's continue... */
Azure.IoT Build 13:848d52f93daf 1177 PDLIST_ENTRY head = DList_RemoveHeadList(deviceData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/
Azure.IoT Build 13:848d52f93daf 1178 DList_InsertTailList(&(deviceData->eventConfirmations), head);
AzureIoTClient 6:73793bae15ba 1179 allMessagesSize += messageSize;
AzureIoTClient 6:73793bae15ba 1180 }
AzureIoTClient 6:73793bae15ba 1181 STRING_delete(temp);
AzureIoTClient 6:73793bae15ba 1182 }
AzureIoTClient 6:73793bae15ba 1183 }
AzureIoTClient 6:73793bae15ba 1184 }
AzureIoTClient 6:73793bae15ba 1185
AzureIoTClient 6:73793bae15ba 1186 /*closing the payload*/
AzureIoTClient 6:73793bae15ba 1187 if (result == MAKE_PAYLOAD_OK)
AzureIoTClient 6:73793bae15ba 1188 {
AzureIoTClient 6:73793bae15ba 1189 ((char*)STRING_c_str(*payload))[STRING_length(*payload) - 1] = ']'; /*TODO - do this in STRING_HANDLE*/
AzureIoTClient 6:73793bae15ba 1190 }
AzureIoTClient 6:73793bae15ba 1191 else
AzureIoTClient 6:73793bae15ba 1192 {
AzureIoTClient 6:73793bae15ba 1193 /*no need to close anything*/
AzureIoTClient 6:73793bae15ba 1194 }
AzureIoTClient 6:73793bae15ba 1195 }
AzureIoTClient 6:73793bae15ba 1196 return result;
AzureIoTClient 6:73793bae15ba 1197 }
AzureIoTClient 6:73793bae15ba 1198
AzureIoTClient 6:73793bae15ba 1199 static void reversePutListBackIn(PDLIST_ENTRY source, PDLIST_ENTRY destination)
AzureIoTClient 6:73793bae15ba 1200 {
AzureIoTClient 6:73793bae15ba 1201 /*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 6:73793bae15ba 1202 DList_AppendTailList(destination->Flink, source);
AzureIoTClient 6:73793bae15ba 1203 DList_RemoveEntryList(source);
AzureIoTClient 6:73793bae15ba 1204 DList_InitializeListHead(source);
AzureIoTClient 6:73793bae15ba 1205 }
AzureIoTClient 6:73793bae15ba 1206
Azure.IoT Build 13:848d52f93daf 1207 static void DoEvent(HTTPTRANSPORT_HANDLE_DATA* handleData, HTTPTRANSPORT_PERDEVICE_DATA* deviceData, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 6:73793bae15ba 1208 {
Azure.IoT Build 13:848d52f93daf 1209
Azure.IoT Build 13:848d52f93daf 1210 if (DList_IsListEmpty(deviceData->waitingToSend))
AzureIoTClient 6:73793bae15ba 1211 {
Azure.IoT Build 13:848d52f93daf 1212 /*Codes_SRS_TRANSPORTMULTITHTTP_17_060: [If the list is empty then IoTHubTransportHttp_DoWork shall proceed to the following action.] */
AzureIoTClient 6:73793bae15ba 1213 }
AzureIoTClient 6:73793bae15ba 1214 else
AzureIoTClient 6:73793bae15ba 1215 {
Azure.IoT Build 13:848d52f93daf 1216 /*Codes_SRS_TRANSPORTMULTITHTTP_17_053: [If option SetBatching is true then _Dowork shall send batched event message as specced below.] */
AzureIoTClient 6:73793bae15ba 1217 if (handleData->doBatchedTransfers)
AzureIoTClient 6:73793bae15ba 1218 {
Azure.IoT Build 13:848d52f93daf 1219 /*Codes_SRS_TRANSPORTMULTITHTTP_17_054: [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.] */
Azure.IoT Build 13:848d52f93daf 1220 if (HTTPHeaders_ReplaceHeaderNameValuePair(deviceData->eventHTTPrequestHeaders, CONTENT_TYPE, APPLICATION_VND_MICROSOFT_IOTHUB_JSON) != HTTP_HEADERS_OK)
AzureIoTClient 6:73793bae15ba 1221 {
Azure.IoT Build 13:848d52f93daf 1222 /*Codes_SRS_TRANSPORTMULTITHTTP_17_055: [If updating Content-Type fails for any reason, then _DoWork shall advance to the next action.] */
AzureIoTClient 6:73793bae15ba 1223 LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair\r\n");
AzureIoTClient 6:73793bae15ba 1224 }
AzureIoTClient 6:73793bae15ba 1225 else
AzureIoTClient 6:73793bae15ba 1226 {
Azure.IoT Build 13:848d52f93daf 1227 /*Codes_SRS_TRANSPORTMULTITHTTP_17_059: [It shall inspect the "waitingToSend" DLIST passed in config structure.] */
AzureIoTClient 6:73793bae15ba 1228 STRING_HANDLE payload;
Azure.IoT Build 13:848d52f93daf 1229 switch (makePayload(deviceData, &payload))
AzureIoTClient 6:73793bae15ba 1230 {
AzureIoTClient 6:73793bae15ba 1231 case MAKE_PAYLOAD_OK:
AzureIoTClient 6:73793bae15ba 1232 {
Azure.IoT Build 13:848d52f93daf 1233 /*Codes_SRS_TRANSPORTMULTITHTTP_17_068: [Once a final payload has been obtained, IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters:] */
AzureIoTClient 6:73793bae15ba 1234 BUFFER_HANDLE temp = BUFFER_new();
AzureIoTClient 6:73793bae15ba 1235 if (temp == NULL)
AzureIoTClient 6:73793bae15ba 1236 {
AzureIoTClient 6:73793bae15ba 1237 LogError("unable to BUFFER_new\r\n");
Azure.IoT Build 13:848d52f93daf 1238 /*Codes_SRS_TRANSPORTMULTITHTTP_17_067: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/
Azure.IoT Build 13:848d52f93daf 1239 reversePutListBackIn(&(deviceData->eventConfirmations), deviceData->waitingToSend);
AzureIoTClient 6:73793bae15ba 1240 }
AzureIoTClient 6:73793bae15ba 1241 else
AzureIoTClient 6:73793bae15ba 1242 {
AzureIoTClient 6:73793bae15ba 1243 if (BUFFER_build(temp, (const unsigned char*)STRING_c_str(payload), STRING_length(payload)) != 0)
AzureIoTClient 6:73793bae15ba 1244 {
AzureIoTClient 6:73793bae15ba 1245 LogError("unable to BUFFER_build\r\n");
AzureIoTClient 6:73793bae15ba 1246 //items go back to waitingToSend
Azure.IoT Build 13:848d52f93daf 1247 /*Codes_SRS_TRANSPORTMULTITHTTP_17_067: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/
Azure.IoT Build 13:848d52f93daf 1248 reversePutListBackIn(&(deviceData->eventConfirmations), deviceData->waitingToSend);
AzureIoTClient 6:73793bae15ba 1249 }
AzureIoTClient 6:73793bae15ba 1250 else
AzureIoTClient 6:73793bae15ba 1251 {
AzureIoTClient 6:73793bae15ba 1252 unsigned int statusCode;
AzureIoTClient 6:73793bae15ba 1253 HTTPAPIEX_RESULT r;
AzureIoTClient 6:73793bae15ba 1254 if ((r = HTTPAPIEX_SAS_ExecuteRequest(
Azure.IoT Build 13:848d52f93daf 1255 deviceData->sasObject,
AzureIoTClient 6:73793bae15ba 1256 handleData->httpApiExHandle,
AzureIoTClient 6:73793bae15ba 1257 HTTPAPI_REQUEST_POST,
Azure.IoT Build 13:848d52f93daf 1258 STRING_c_str(deviceData->eventHTTPrelativePath),
Azure.IoT Build 13:848d52f93daf 1259 deviceData->eventHTTPrequestHeaders,
AzureIoTClient 6:73793bae15ba 1260 temp,
AzureIoTClient 6:73793bae15ba 1261 &statusCode,
AzureIoTClient 6:73793bae15ba 1262 NULL,
AzureIoTClient 6:73793bae15ba 1263 NULL
AzureIoTClient 6:73793bae15ba 1264 )) != HTTPAPIEX_OK)
AzureIoTClient 6:73793bae15ba 1265 {
AzureIoTClient 6:73793bae15ba 1266 LogError("unable to HTTPAPIEX_ExecuteRequest\r\n");
AzureIoTClient 6:73793bae15ba 1267 //items go back to waitingToSend
Azure.IoT Build 13:848d52f93daf 1268 /*Codes_SRS_TRANSPORTMULTITHTTP_17_069: [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).] */
Azure.IoT Build 13:848d52f93daf 1269 reversePutListBackIn(&(deviceData->eventConfirmations), deviceData->waitingToSend);
AzureIoTClient 6:73793bae15ba 1270 }
AzureIoTClient 6:73793bae15ba 1271 else
AzureIoTClient 6:73793bae15ba 1272 {
AzureIoTClient 6:73793bae15ba 1273 if (statusCode < 300)
AzureIoTClient 6:73793bae15ba 1274 {
Azure.IoT Build 13:848d52f93daf 1275 /*Codes_SRS_TRANSPORTMULTITHTTP_17_070: [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.] */
Azure.IoT Build 13:848d52f93daf 1276 IoTHubClient_LL_SendComplete(iotHubClientHandle, &(deviceData->eventConfirmations), IOTHUB_BATCHSTATE_SUCCESS);
AzureIoTClient 6:73793bae15ba 1277 }
AzureIoTClient 6:73793bae15ba 1278 else
AzureIoTClient 6:73793bae15ba 1279 {
AzureIoTClient 6:73793bae15ba 1280 //items go back to waitingToSend
Azure.IoT Build 13:848d52f93daf 1281 /*Codes_SRS_TRANSPORTMULTITHTTP_17_069: [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 6:73793bae15ba 1282 LogError("unexpected HTTP status code (%u)\r\n", statusCode);
Azure.IoT Build 13:848d52f93daf 1283 reversePutListBackIn(&(deviceData->eventConfirmations), deviceData->waitingToSend);
AzureIoTClient 6:73793bae15ba 1284 }
AzureIoTClient 6:73793bae15ba 1285 }
AzureIoTClient 6:73793bae15ba 1286 }
AzureIoTClient 6:73793bae15ba 1287 BUFFER_delete(temp);
AzureIoTClient 6:73793bae15ba 1288 }
AzureIoTClient 6:73793bae15ba 1289 STRING_delete(payload);
AzureIoTClient 6:73793bae15ba 1290 break;
AzureIoTClient 6:73793bae15ba 1291 }
AzureIoTClient 6:73793bae15ba 1292 case MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT:
AzureIoTClient 6:73793bae15ba 1293 {
Azure.IoT Build 13:848d52f93daf 1294 IoTHubClient_LL_SendComplete(iotHubClientHandle, &(deviceData->eventConfirmations), IOTHUB_BATCHSTATE_FAILED); /*takes care of emptying the list too*/
AzureIoTClient 6:73793bae15ba 1295 break;
AzureIoTClient 6:73793bae15ba 1296 }
AzureIoTClient 6:73793bae15ba 1297 case MAKE_PAYLOAD_ERROR:
AzureIoTClient 6:73793bae15ba 1298 {
AzureIoTClient 6:73793bae15ba 1299 LogError("unrecoverable errors while building a batch message\r\n");
AzureIoTClient 6:73793bae15ba 1300 break;
AzureIoTClient 6:73793bae15ba 1301 }
AzureIoTClient 6:73793bae15ba 1302 case MAKE_PAYLOAD_NO_ITEMS:
AzureIoTClient 6:73793bae15ba 1303 {
AzureIoTClient 6:73793bae15ba 1304 /*do nothing*/
AzureIoTClient 6:73793bae15ba 1305 break;
AzureIoTClient 6:73793bae15ba 1306 }
AzureIoTClient 6:73793bae15ba 1307 default:
AzureIoTClient 6:73793bae15ba 1308 {
AzureIoTClient 6:73793bae15ba 1309 LogError("internal error: switch's default branch reached when never intended\r\n");
AzureIoTClient 6:73793bae15ba 1310 break;
AzureIoTClient 6:73793bae15ba 1311 }
AzureIoTClient 6:73793bae15ba 1312 }
AzureIoTClient 6:73793bae15ba 1313 }
AzureIoTClient 6:73793bae15ba 1314 }
AzureIoTClient 6:73793bae15ba 1315 else
AzureIoTClient 6:73793bae15ba 1316 {
AzureIoTClient 6:73793bae15ba 1317 const unsigned char* messageContent=NULL;
AzureIoTClient 6:73793bae15ba 1318 size_t messageSize=0;
AzureIoTClient 6:73793bae15ba 1319 size_t originalMessageSize=0;
Azure.IoT Build 13:848d52f93daf 1320 IOTHUB_MESSAGE_LIST* message = containingRecord(deviceData->waitingToSend->Flink, IOTHUB_MESSAGE_LIST, entry);
AzureIoTClient 6:73793bae15ba 1321 IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message->messageHandle);
AzureIoTClient 6:73793bae15ba 1322
Azure.IoT Build 13:848d52f93daf 1323 /*Codes_SRS_TRANSPORTMULTITHTTP_17_073: [The message size is computed from the length of the payload + 384.]*/
AzureIoTClient 6:73793bae15ba 1324 if (!(
AzureIoTClient 6:73793bae15ba 1325 (((contentType == IOTHUBMESSAGE_BYTEARRAY) &&
AzureIoTClient 6:73793bae15ba 1326 (IoTHubMessage_GetByteArray(message->messageHandle, &messageContent, &originalMessageSize)==IOTHUB_MESSAGE_OK)) ? (messageSize= originalMessageSize + MAXIMUM_PAYLOAD_OVERHEAD, 1): 0)
AzureIoTClient 6:73793bae15ba 1327
AzureIoTClient 6:73793bae15ba 1328 ||
AzureIoTClient 6:73793bae15ba 1329
AzureIoTClient 6:73793bae15ba 1330 ((contentType == IOTHUBMESSAGE_STRING) && (
AzureIoTClient 6:73793bae15ba 1331 messageContent = (const unsigned char*)IoTHubMessage_GetString(message->messageHandle),
AzureIoTClient 6:73793bae15ba 1332 (messageSize = MAXIMUM_PAYLOAD_OVERHEAD + (originalMessageSize = ((messageContent == NULL)?0:strlen((const char*)messageContent)))),
AzureIoTClient 6:73793bae15ba 1333 messageContent!=NULL)
AzureIoTClient 6:73793bae15ba 1334 )
AzureIoTClient 6:73793bae15ba 1335 ))
AzureIoTClient 6:73793bae15ba 1336 {
AzureIoTClient 6:73793bae15ba 1337 LogError("unable to get the message content\r\n");
AzureIoTClient 6:73793bae15ba 1338 /*go on...*/
AzureIoTClient 6:73793bae15ba 1339 }
AzureIoTClient 6:73793bae15ba 1340 else
AzureIoTClient 6:73793bae15ba 1341 {
Azure.IoT Build 13:848d52f93daf 1342 /*Codes_SRS_TRANSPORTMULTITHTTP_17_075: [If the oldest message in waitingToSend causes the message to exceed the message size limit 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.]*/
Azure.IoT Build 13:848d52f93daf 1343 /*Codes_SRS_TRANSPORTMULTITHTTP_17_072: [The message size shall be limited to 255KB -1 bytes.] */
AzureIoTClient 6:73793bae15ba 1344 if (messageSize > MAXIMUM_MESSAGE_SIZE)
AzureIoTClient 6:73793bae15ba 1345 {
Azure.IoT Build 13:848d52f93daf 1346 PDLIST_ENTRY head = DList_RemoveHeadList(deviceData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/
Azure.IoT Build 13:848d52f93daf 1347 DList_InsertTailList(&(deviceData->eventConfirmations), head);
Azure.IoT Build 13:848d52f93daf 1348 IoTHubClient_LL_SendComplete(iotHubClientHandle, &(deviceData->eventConfirmations), IOTHUB_BATCHSTATE_FAILED); /*takes care of emptying the list too*/
AzureIoTClient 6:73793bae15ba 1349 }
AzureIoTClient 6:73793bae15ba 1350 else
AzureIoTClient 6:73793bae15ba 1351 {
Azure.IoT Build 13:848d52f93daf 1352 /*Codes_SRS_TRANSPORTMULTITHTTP_17_071: [If option SetBatching is false then _Dowork shall send individual event message as specced below.] */
Azure.IoT Build 13:848d52f93daf 1353 /*Codes_SRS_TRANSPORTMULTITHTTP_17_076: [A clone of the event HTTP request headers shall be created.]*/
Azure.IoT Build 13:848d52f93daf 1354 HTTP_HEADERS_HANDLE clonedEventHTTPrequestHeaders = HTTPHeaders_Clone(deviceData->eventHTTPrequestHeaders);
AzureIoTClient 6:73793bae15ba 1355 if (clonedEventHTTPrequestHeaders == NULL)
AzureIoTClient 6:73793bae15ba 1356 {
Azure.IoT Build 13:848d52f93daf 1357 /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 6:73793bae15ba 1358 LogError("HTTPHeaders_Clone failed\r\n");
AzureIoTClient 6:73793bae15ba 1359 }
AzureIoTClient 6:73793bae15ba 1360 else
AzureIoTClient 6:73793bae15ba 1361 {
Azure.IoT Build 13:848d52f93daf 1362 /*Codes_SRS_TRANSPORTMULTITHTTP_17_077: [The cloned HTTP headers shall have the HTTP header "Content-Type" set to "application/octet-stream".] */
AzureIoTClient 6:73793bae15ba 1363 if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, CONTENT_TYPE, APPLICATION_OCTET_STREAM) != HTTP_HEADERS_OK)
AzureIoTClient 6:73793bae15ba 1364 {
Azure.IoT Build 13:848d52f93daf 1365 /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 6:73793bae15ba 1366 LogError("HTTPHeaders_ReplaceHeaderNameValuePair failed\r\n");
AzureIoTClient 6:73793bae15ba 1367 }
AzureIoTClient 6:73793bae15ba 1368 else
AzureIoTClient 6:73793bae15ba 1369 {
Azure.IoT Build 13:848d52f93daf 1370 /*Codes_SRS_TRANSPORTMULTITHTTP_17_078: [Every message property "property":"value" shall be added to the HTTP headers as an individual header "iothub-app-property":"value".] */
AzureIoTClient 6:73793bae15ba 1371 MAP_HANDLE map = IoTHubMessage_Properties(message->messageHandle);
AzureIoTClient 6:73793bae15ba 1372 const char*const* keys;
AzureIoTClient 6:73793bae15ba 1373 const char*const* values;
AzureIoTClient 6:73793bae15ba 1374 size_t count;
AzureIoTClient 6:73793bae15ba 1375 if (Map_GetInternals(map, &keys, &values, &count) != MAP_OK)
AzureIoTClient 6:73793bae15ba 1376 {
Azure.IoT Build 13:848d52f93daf 1377 /*Codes_SRS_TRANSPORTMULTITHTTP_17_078: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 6:73793bae15ba 1378 LogError("unable to Map_GetInternals\r\n");
AzureIoTClient 6:73793bae15ba 1379 }
AzureIoTClient 6:73793bae15ba 1380 else
AzureIoTClient 6:73793bae15ba 1381 {
AzureIoTClient 6:73793bae15ba 1382 size_t i;
AzureIoTClient 6:73793bae15ba 1383 bool goOn = true;
AzureIoTClient 8:1b71bf027eb5 1384 const char* msgId;
AzureIoTClient 8:1b71bf027eb5 1385 const char* corrId;
AzureIoTClient 8:1b71bf027eb5 1386
AzureIoTClient 6:73793bae15ba 1387 for (i = 0; (i < count) && goOn; i++)
AzureIoTClient 6:73793bae15ba 1388 {
Azure.IoT Build 13:848d52f93daf 1389 /*Codes_SRS_TRANSPORTMULTITHTTP_17_074: [Every property name shall add to the message size the length of the property name + the length of the property value + 16 bytes.] */
AzureIoTClient 6:73793bae15ba 1390 messageSize += (strlen(values[i]) + strlen(keys[i]) + MAXIMUM_PROPERTY_OVERHEAD);
AzureIoTClient 6:73793bae15ba 1391 if (messageSize > MAXIMUM_MESSAGE_SIZE)
AzureIoTClient 6:73793bae15ba 1392 {
Azure.IoT Build 13:848d52f93daf 1393 /*Codes_SRS_TRANSPORTMULTITHTTP_17_072: [The message size shall be limited to 255KB -1 bytes.] */
Azure.IoT Build 13:848d52f93daf 1394 PDLIST_ENTRY head = DList_RemoveHeadList(deviceData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/
Azure.IoT Build 13:848d52f93daf 1395 DList_InsertTailList(&(deviceData->eventConfirmations), head);
Azure.IoT Build 13:848d52f93daf 1396 IoTHubClient_LL_SendComplete(iotHubClientHandle, &(deviceData->eventConfirmations), IOTHUB_BATCHSTATE_FAILED); /*takes care of emptying the list too*/
AzureIoTClient 6:73793bae15ba 1397 goOn = false;
AzureIoTClient 6:73793bae15ba 1398 }
AzureIoTClient 6:73793bae15ba 1399 else
AzureIoTClient 6:73793bae15ba 1400 {
AzureIoTClient 6:73793bae15ba 1401 STRING_HANDLE temp = STRING_construct(IOTHUB_APP_PREFIX);
AzureIoTClient 6:73793bae15ba 1402 if (temp == NULL)
AzureIoTClient 6:73793bae15ba 1403 {
Azure.IoT Build 13:848d52f93daf 1404 /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 6:73793bae15ba 1405 LogError("unable to STRING_construct\r\n");
AzureIoTClient 6:73793bae15ba 1406 goOn = false;
AzureIoTClient 6:73793bae15ba 1407 }
AzureIoTClient 6:73793bae15ba 1408 else
AzureIoTClient 6:73793bae15ba 1409 {
AzureIoTClient 6:73793bae15ba 1410 if (STRING_concat(temp, keys[i]) != 0)
AzureIoTClient 6:73793bae15ba 1411 {
Azure.IoT Build 13:848d52f93daf 1412 /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 6:73793bae15ba 1413 LogError("unable to STRING_concat\r\n");
AzureIoTClient 6:73793bae15ba 1414 goOn = false;
AzureIoTClient 6:73793bae15ba 1415 }
AzureIoTClient 6:73793bae15ba 1416 else
AzureIoTClient 6:73793bae15ba 1417 {
AzureIoTClient 6:73793bae15ba 1418 if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, STRING_c_str(temp), values[i]) != HTTP_HEADERS_OK)
AzureIoTClient 6:73793bae15ba 1419 {
Azure.IoT Build 13:848d52f93daf 1420 /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 6:73793bae15ba 1421 LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair\r\n");
AzureIoTClient 6:73793bae15ba 1422 goOn = false;
AzureIoTClient 6:73793bae15ba 1423 }
AzureIoTClient 6:73793bae15ba 1424 }
AzureIoTClient 6:73793bae15ba 1425 STRING_delete(temp);
AzureIoTClient 6:73793bae15ba 1426 }
AzureIoTClient 6:73793bae15ba 1427 }
AzureIoTClient 6:73793bae15ba 1428 }
AzureIoTClient 6:73793bae15ba 1429
AzureIoTClient 7:48f0f78cd3ef 1430 // Add the Message Id and the Correlation Id
AzureIoTClient 8:1b71bf027eb5 1431 msgId = IoTHubMessage_GetMessageId(message->messageHandle);
AzureIoTClient 7:48f0f78cd3ef 1432 if (goOn && msgId != NULL)
AzureIoTClient 7:48f0f78cd3ef 1433 {
AzureIoTClient 7:48f0f78cd3ef 1434 if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, IOTHUB_MESSAGE_ID, msgId) != HTTP_HEADERS_OK)
AzureIoTClient 7:48f0f78cd3ef 1435 {
AzureIoTClient 7:48f0f78cd3ef 1436 LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair\r\n");
AzureIoTClient 7:48f0f78cd3ef 1437 goOn = false;
AzureIoTClient 7:48f0f78cd3ef 1438 }
AzureIoTClient 7:48f0f78cd3ef 1439 }
AzureIoTClient 7:48f0f78cd3ef 1440
AzureIoTClient 8:1b71bf027eb5 1441 corrId = IoTHubMessage_GetCorrelationId(message->messageHandle);
AzureIoTClient 7:48f0f78cd3ef 1442 if (goOn && corrId != NULL)
AzureIoTClient 7:48f0f78cd3ef 1443 {
AzureIoTClient 7:48f0f78cd3ef 1444 if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, IOTHUB_CORRELATION_ID, corrId) != HTTP_HEADERS_OK)
AzureIoTClient 7:48f0f78cd3ef 1445 {
AzureIoTClient 7:48f0f78cd3ef 1446 LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair\r\n");
AzureIoTClient 7:48f0f78cd3ef 1447 goOn = false;
AzureIoTClient 7:48f0f78cd3ef 1448 }
AzureIoTClient 7:48f0f78cd3ef 1449 }
AzureIoTClient 7:48f0f78cd3ef 1450
AzureIoTClient 6:73793bae15ba 1451 if (!goOn)
AzureIoTClient 6:73793bae15ba 1452 {
Azure.IoT Build 13:848d52f93daf 1453 /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */
AzureIoTClient 6:73793bae15ba 1454 }
AzureIoTClient 6:73793bae15ba 1455 else
AzureIoTClient 6:73793bae15ba 1456 {
Azure.IoT Build 13:848d52f93daf 1457 /*Codes_SRS_TRANSPORTMULTITHTTP_17_080: [IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters] */
AzureIoTClient 6:73793bae15ba 1458 BUFFER_HANDLE toBeSend = BUFFER_new();
AzureIoTClient 6:73793bae15ba 1459 if (toBeSend == NULL)
AzureIoTClient 6:73793bae15ba 1460 {
AzureIoTClient 6:73793bae15ba 1461 LogError("unable to BUFFER_new\r\n");
AzureIoTClient 6:73793bae15ba 1462 }
AzureIoTClient 6:73793bae15ba 1463 else
AzureIoTClient 6:73793bae15ba 1464 {
AzureIoTClient 6:73793bae15ba 1465 if (BUFFER_build(toBeSend, messageContent, originalMessageSize) != 0)
AzureIoTClient 6:73793bae15ba 1466 {
AzureIoTClient 6:73793bae15ba 1467 LogError("unable to BUFFER_build\r\n");
AzureIoTClient 6:73793bae15ba 1468 }
AzureIoTClient 6:73793bae15ba 1469 else
AzureIoTClient 6:73793bae15ba 1470 {
AzureIoTClient 6:73793bae15ba 1471 unsigned int statusCode;
AzureIoTClient 6:73793bae15ba 1472 HTTPAPIEX_RESULT r;
AzureIoTClient 6:73793bae15ba 1473 if ((r = HTTPAPIEX_SAS_ExecuteRequest(
Azure.IoT Build 13:848d52f93daf 1474 deviceData->sasObject,
AzureIoTClient 6:73793bae15ba 1475 handleData->httpApiExHandle,
AzureIoTClient 6:73793bae15ba 1476 HTTPAPI_REQUEST_POST,
Azure.IoT Build 13:848d52f93daf 1477 STRING_c_str(deviceData->eventHTTPrelativePath),
AzureIoTClient 6:73793bae15ba 1478 clonedEventHTTPrequestHeaders,
AzureIoTClient 6:73793bae15ba 1479 toBeSend,
AzureIoTClient 6:73793bae15ba 1480 &statusCode,
AzureIoTClient 6:73793bae15ba 1481 NULL,
AzureIoTClient 6:73793bae15ba 1482 NULL
AzureIoTClient 6:73793bae15ba 1483 )) != HTTPAPIEX_OK)
AzureIoTClient 6:73793bae15ba 1484 {
AzureIoTClient 6:73793bae15ba 1485 LogError("unable to HTTPAPIEX_ExecuteRequest\r\n");
AzureIoTClient 6:73793bae15ba 1486 }
AzureIoTClient 6:73793bae15ba 1487 else
AzureIoTClient 6:73793bae15ba 1488 {
AzureIoTClient 6:73793bae15ba 1489 if (statusCode < 300)
AzureIoTClient 6:73793bae15ba 1490 {
Azure.IoT Build 13:848d52f93daf 1491 /*Codes_SRS_TRANSPORTMULTITHTTP_17_082: [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.] */
Azure.IoT Build 13:848d52f93daf 1492 PDLIST_ENTRY justSent = DList_RemoveHeadList(deviceData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/
Azure.IoT Build 13:848d52f93daf 1493 DList_InsertTailList(&(deviceData->eventConfirmations), justSent);
Azure.IoT Build 13:848d52f93daf 1494 IoTHubClient_LL_SendComplete(iotHubClientHandle, &(deviceData->eventConfirmations), IOTHUB_BATCHSTATE_SUCCESS); /*takes care of emptying the list too*/
AzureIoTClient 6:73793bae15ba 1495 }
AzureIoTClient 6:73793bae15ba 1496 else
AzureIoTClient 6:73793bae15ba 1497 {
Azure.IoT Build 13:848d52f93daf 1498 /*Codes_SRS_TRANSPORTMULTITHTTP_17_081: [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 6:73793bae15ba 1499 LogError("unexpected HTTP status code (%u)\r\n", statusCode);
AzureIoTClient 6:73793bae15ba 1500 }
AzureIoTClient 6:73793bae15ba 1501 }
AzureIoTClient 6:73793bae15ba 1502 }
AzureIoTClient 6:73793bae15ba 1503 BUFFER_delete(toBeSend);
AzureIoTClient 6:73793bae15ba 1504 }
AzureIoTClient 6:73793bae15ba 1505 }
AzureIoTClient 6:73793bae15ba 1506 }
AzureIoTClient 6:73793bae15ba 1507 }
AzureIoTClient 6:73793bae15ba 1508 HTTPHeaders_Free(clonedEventHTTPrequestHeaders);
AzureIoTClient 6:73793bae15ba 1509 }
AzureIoTClient 6:73793bae15ba 1510 }
AzureIoTClient 6:73793bae15ba 1511 }
AzureIoTClient 6:73793bae15ba 1512 }
AzureIoTClient 6:73793bae15ba 1513 }
AzureIoTClient 6:73793bae15ba 1514 }
AzureIoTClient 6:73793bae15ba 1515
AzureIoTClient 6:73793bae15ba 1516 #define ACTION_VALUES \
AzureIoTClient 6:73793bae15ba 1517 ABANDON, \
AzureIoTClient 6:73793bae15ba 1518 REJECT, \
AzureIoTClient 6:73793bae15ba 1519 ACCEPT
AzureIoTClient 6:73793bae15ba 1520 DEFINE_ENUM(ACTION, ACTION_VALUES);
AzureIoTClient 6:73793bae15ba 1521
Azure.IoT Build 13:848d52f93daf 1522 static void abandonOrAcceptMessage(HTTPTRANSPORT_HANDLE_DATA* handleData, HTTPTRANSPORT_PERDEVICE_DATA* deviceData, const char* ETag, ACTION action)
AzureIoTClient 6:73793bae15ba 1523 {
Azure.IoT Build 13:848d52f93daf 1524 /*Codes_SRS_TRANSPORTMULTITHTTP_17_097: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters:
AzureIoTClient 6:73793bae15ba 1525 -requestType: POST
Azure.IoT Build 12:9e2531d86009 1526 -relativePath: abandon relative path begin (as created by _Create) + value of ETag + "/abandon?api-version=2016-02-03"
AzureIoTClient 6:73793bae15ba 1527 - requestHttpHeadersHandle: an HTTP headers instance containing the following
AzureIoTClient 6:73793bae15ba 1528 Authorization: " "
AzureIoTClient 6:73793bae15ba 1529 If-Match: value of ETag
AzureIoTClient 6:73793bae15ba 1530 - requestContent: NULL
AzureIoTClient 6:73793bae15ba 1531 - statusCode: a pointer to unsigned int which might be examined for logging
AzureIoTClient 6:73793bae15ba 1532 - responseHeadearsHandle: NULL
AzureIoTClient 6:73793bae15ba 1533 - responseContent: NULL]*/
Azure.IoT Build 13:848d52f93daf 1534 /*Codes_SRS_TRANSPORTMULTITHTTP_17_099: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters:
AzureIoTClient 6:73793bae15ba 1535 -requestType: DELETE
Azure.IoT Build 12:9e2531d86009 1536 -relativePath: abandon relative path begin + value of ETag + "?api-version=2016-02-03"
AzureIoTClient 6:73793bae15ba 1537 - requestHttpHeadersHandle: an HTTP headers instance containing the following
AzureIoTClient 6:73793bae15ba 1538 Authorization: " "
AzureIoTClient 6:73793bae15ba 1539 If-Match: value of ETag
AzureIoTClient 6:73793bae15ba 1540 - requestContent: NULL
AzureIoTClient 6:73793bae15ba 1541 - statusCode: a pointer to unsigned int which might be used by logging
AzureIoTClient 6:73793bae15ba 1542 - responseHeadearsHandle: NULL
AzureIoTClient 6:73793bae15ba 1543 - responseContent: NULL]*/
Azure.IoT Build 13:848d52f93daf 1544 /*Codes_SRS_TRANSPORTMULTITHTTP_17_101: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters:
AzureIoTClient 6:73793bae15ba 1545 -requestType: DELETE
Azure.IoT Build 12:9e2531d86009 1546 -relativePath: abandon relative path begin + value of ETag +"?api-version=2016-02-03" + "&reject"
AzureIoTClient 6:73793bae15ba 1547 - requestHttpHeadersHandle: an HTTP headers instance containing the following
AzureIoTClient 6:73793bae15ba 1548 Authorization: " "
AzureIoTClient 6:73793bae15ba 1549 If-Match: value of ETag
AzureIoTClient 6:73793bae15ba 1550 - requestContent: NULL
AzureIoTClient 6:73793bae15ba 1551 - statusCode: a pointer to unsigned int which might be used by logging
AzureIoTClient 6:73793bae15ba 1552 - responseHeadearsHandle: NULL
AzureIoTClient 6:73793bae15ba 1553 - responseContent: NULL]*/
AzureIoTClient 6:73793bae15ba 1554
Azure.IoT Build 13:848d52f93daf 1555 STRING_HANDLE fullAbandonRelativePath = STRING_clone(deviceData->abandonHTTPrelativePathBegin);
AzureIoTClient 6:73793bae15ba 1556 if (fullAbandonRelativePath == NULL)
AzureIoTClient 6:73793bae15ba 1557 {
Azure.IoT Build 13:848d52f93daf 1558 /*Codes_SRS_TRANSPORTMULTITHTTP_17_098: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
Azure.IoT Build 13:848d52f93daf 1559 /*Codes_SRS_TRANSPORTMULTITHTTP_17_100: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
Azure.IoT Build 13:848d52f93daf 1560 /*Codes_SRS_TRANSPORTMULTITHTTP_17_102: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 6:73793bae15ba 1561 LogError("unable to STRING_clone\r\n");
AzureIoTClient 6:73793bae15ba 1562 }
AzureIoTClient 6:73793bae15ba 1563 else
AzureIoTClient 6:73793bae15ba 1564 {
AzureIoTClient 6:73793bae15ba 1565 STRING_HANDLE ETagUnquoted = STRING_construct_n(ETag + 1, strlen(ETag) - 2); /*skip first character which is '"' and the last one (which is also '"')*/
AzureIoTClient 6:73793bae15ba 1566 if (ETagUnquoted == NULL)
AzureIoTClient 6:73793bae15ba 1567 {
Azure.IoT Build 13:848d52f93daf 1568 /*Codes_SRS_TRANSPORTMULTITHTTP_17_098: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
Azure.IoT Build 13:848d52f93daf 1569 /*Codes_SRS_TRANSPORTMULTITHTTP_17_100: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
Azure.IoT Build 13:848d52f93daf 1570 /*Codes_SRS_TRANSPORTMULTITHTTP_17_102: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 6:73793bae15ba 1571 LogError("unable to STRING_construct_n\r\n");
AzureIoTClient 6:73793bae15ba 1572 }
AzureIoTClient 6:73793bae15ba 1573 else
AzureIoTClient 6:73793bae15ba 1574 {
AzureIoTClient 6:73793bae15ba 1575 if (!(
AzureIoTClient 6:73793bae15ba 1576 (STRING_concat_with_STRING(fullAbandonRelativePath, ETagUnquoted) == 0) &&
AzureIoTClient 6:73793bae15ba 1577 (STRING_concat(fullAbandonRelativePath, (action == ABANDON) ? "/abandon" API_VERSION : ((action == REJECT) ? API_VERSION "&reject" : API_VERSION)) == 0)
AzureIoTClient 6:73793bae15ba 1578 ))
AzureIoTClient 6:73793bae15ba 1579 {
Azure.IoT Build 13:848d52f93daf 1580 /*Codes_SRS_TRANSPORTMULTITHTTP_17_098: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
Azure.IoT Build 13:848d52f93daf 1581 /*Codes_SRS_TRANSPORTMULTITHTTP_17_100: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
Azure.IoT Build 13:848d52f93daf 1582 /*Codes_SRS_TRANSPORTMULTITHTTP_17_102: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 6:73793bae15ba 1583 LogError("unable to STRING_concat\r\n");
AzureIoTClient 6:73793bae15ba 1584 }
AzureIoTClient 6:73793bae15ba 1585 else
AzureIoTClient 6:73793bae15ba 1586 {
AzureIoTClient 6:73793bae15ba 1587 HTTP_HEADERS_HANDLE abandonRequestHttpHeaders = HTTPHeaders_Alloc();
AzureIoTClient 6:73793bae15ba 1588 if (abandonRequestHttpHeaders == NULL)
AzureIoTClient 6:73793bae15ba 1589 {
Azure.IoT Build 13:848d52f93daf 1590 /*Codes_SRS_TRANSPORTMULTITHTTP_17_098: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
Azure.IoT Build 13:848d52f93daf 1591 /*Codes_SRS_TRANSPORTMULTITHTTP_17_100: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
Azure.IoT Build 13:848d52f93daf 1592 /*Codes_SRS_TRANSPORTMULTITHTTP_17_102: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 6:73793bae15ba 1593 LogError("unable to HTTPHeaders_Alloc\r\n");
AzureIoTClient 6:73793bae15ba 1594 }
AzureIoTClient 6:73793bae15ba 1595 else
AzureIoTClient 6:73793bae15ba 1596 {
AzureIoTClient 6:73793bae15ba 1597 if (!(
Azure.IoT Build 13:848d52f93daf 1598 (HTTPHeaders_AddHeaderNameValuePair(abandonRequestHttpHeaders, "User-Agent", CLIENT_DEVICE_TYPE_PREFIX CLIENT_DEVICE_BACKSLASH IOTHUB_SDK_VERSION) == HTTP_HEADERS_OK) &&
AzureIoTClient 6:73793bae15ba 1599 (HTTPHeaders_AddHeaderNameValuePair(abandonRequestHttpHeaders, "Authorization", " ") == HTTP_HEADERS_OK) &&
AzureIoTClient 6:73793bae15ba 1600 (HTTPHeaders_AddHeaderNameValuePair(abandonRequestHttpHeaders, "If-Match", ETag) == HTTP_HEADERS_OK)
AzureIoTClient 6:73793bae15ba 1601 ))
AzureIoTClient 6:73793bae15ba 1602 {
Azure.IoT Build 13:848d52f93daf 1603 /*Codes_SRS_TRANSPORTMULTITHTTP_17_098: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
Azure.IoT Build 13:848d52f93daf 1604 /*Codes_SRS_TRANSPORTMULTITHTTP_17_100: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
Azure.IoT Build 13:848d52f93daf 1605 /*Codes_SRS_TRANSPORTMULTITHTTP_17_102: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 6:73793bae15ba 1606 LogError("unable to HTTPHeaders_AddHeaderNameValuePair\r\n");
AzureIoTClient 6:73793bae15ba 1607 }
AzureIoTClient 6:73793bae15ba 1608 else
AzureIoTClient 6:73793bae15ba 1609 {
AzureIoTClient 6:73793bae15ba 1610 unsigned int statusCode;
AzureIoTClient 6:73793bae15ba 1611 if (HTTPAPIEX_SAS_ExecuteRequest(
Azure.IoT Build 13:848d52f93daf 1612 deviceData->sasObject,
AzureIoTClient 6:73793bae15ba 1613 handleData->httpApiExHandle,
AzureIoTClient 6:73793bae15ba 1614 (action == ABANDON) ? HTTPAPI_REQUEST_POST : HTTPAPI_REQUEST_DELETE, /*-requestType: POST */
Azure.IoT Build 12:9e2531d86009 1615 STRING_c_str(fullAbandonRelativePath), /*-relativePath: abandon relative path begin (as created by _Create) + value of ETag + "/abandon?api-version=2016-02-03" */
AzureIoTClient 6:73793bae15ba 1616 abandonRequestHttpHeaders, /*- requestHttpHeadersHandle: an HTTP headers instance containing the following */
AzureIoTClient 6:73793bae15ba 1617 NULL, /*- requestContent: NULL */
AzureIoTClient 6:73793bae15ba 1618 &statusCode, /*- statusCode: a pointer to unsigned int which might be examined for logging */
AzureIoTClient 6:73793bae15ba 1619 NULL, /*- responseHeadearsHandle: NULL */
AzureIoTClient 6:73793bae15ba 1620 NULL /*- responseContent: NULL] */
AzureIoTClient 6:73793bae15ba 1621 ) != HTTPAPIEX_OK)
AzureIoTClient 6:73793bae15ba 1622 {
Azure.IoT Build 13:848d52f93daf 1623 /*Codes_SRS_TRANSPORTMULTITHTTP_17_098: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
Azure.IoT Build 13:848d52f93daf 1624 /*Codes_SRS_TRANSPORTMULTITHTTP_17_100: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
Azure.IoT Build 13:848d52f93daf 1625 /*Codes_SRS_TRANSPORTMULTITHTTP_17_102: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 6:73793bae15ba 1626 LogError("unable to HTTPAPIEX_ExecuteRequest\r\n");
AzureIoTClient 6:73793bae15ba 1627 }
AzureIoTClient 6:73793bae15ba 1628 else
AzureIoTClient 6:73793bae15ba 1629 {
AzureIoTClient 6:73793bae15ba 1630 if (statusCode != 204)
AzureIoTClient 6:73793bae15ba 1631 {
Azure.IoT Build 13:848d52f93daf 1632 /*Codes_SRS_TRANSPORTMULTITHTTP_17_098: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
Azure.IoT Build 13:848d52f93daf 1633 /*Codes_SRS_TRANSPORTMULTITHTTP_17_100: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
Azure.IoT Build 13:848d52f93daf 1634 /*Codes_SRS_TRANSPORTMULTITHTTP_17_102: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 6:73793bae15ba 1635 LogError("unexpected status code returned %u (was expecting 204)\r\n", statusCode);
AzureIoTClient 6:73793bae15ba 1636 }
AzureIoTClient 6:73793bae15ba 1637 else
AzureIoTClient 6:73793bae15ba 1638 {
Azure.IoT Build 13:848d52f93daf 1639 /*Codes_SRS_TRANSPORTMULTITHTTP_17_098: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/
Azure.IoT Build 13:848d52f93daf 1640 /*Codes_SRS_TRANSPORTMULTITHTTP_17_100: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
Azure.IoT Build 13:848d52f93daf 1641 /*Codes_SRS_TRANSPORTMULTITHTTP_17_102: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */
AzureIoTClient 6:73793bae15ba 1642 /*all is fine*/
AzureIoTClient 6:73793bae15ba 1643 }
AzureIoTClient 6:73793bae15ba 1644 }
AzureIoTClient 6:73793bae15ba 1645 }
AzureIoTClient 6:73793bae15ba 1646 HTTPHeaders_Free(abandonRequestHttpHeaders);
AzureIoTClient 6:73793bae15ba 1647 }
AzureIoTClient 6:73793bae15ba 1648 }
AzureIoTClient 6:73793bae15ba 1649 STRING_delete(ETagUnquoted);
AzureIoTClient 6:73793bae15ba 1650 }
AzureIoTClient 6:73793bae15ba 1651 STRING_delete(fullAbandonRelativePath);
AzureIoTClient 6:73793bae15ba 1652 }
AzureIoTClient 6:73793bae15ba 1653 }
AzureIoTClient 6:73793bae15ba 1654
Azure.IoT Build 13:848d52f93daf 1655 static void DoMessages(HTTPTRANSPORT_HANDLE_DATA* handleData, HTTPTRANSPORT_PERDEVICE_DATA* deviceData, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 6:73793bae15ba 1656 {
Azure.IoT Build 13:848d52f93daf 1657 /*Codes_SRS_TRANSPORTMULTITHTTP_17_083: [ If device is not subscribed then _DoWork shall advance to the next action. ] */
Azure.IoT Build 13:848d52f93daf 1658 if (deviceData->DoWork_PullMessage)
AzureIoTClient 6:73793bae15ba 1659 {
Azure.IoT Build 13:848d52f93daf 1660 /*Codes_SRS_TRANSPORTMULTITHTTP_17_123: [After client creation, the first GET shall be allowed no matter what the value of GetMinimumPollingTime.] */
Azure.IoT Build 13:848d52f93daf 1661 /*Codes_SRS_TRANSPORTMULTITHTTP_17_124: [If time is not available then all calls shall be treated as if they are the first one.] */
Azure.IoT Build 13:848d52f93daf 1662 /*Codes_SRS_TRANSPORTMULTITHTTP_17_122: [A GET request that happens earlier than GetMinimumPollingTime shall be ignored.] */
AzureIoTClient 6:73793bae15ba 1663 time_t timeNow = get_time(NULL);
Azure.IoT Build 13:848d52f93daf 1664 bool isPollingAllowed = deviceData->isFirstPoll || (timeNow == (time_t)(-1)) || (get_difftime(timeNow, deviceData->lastPollTime) > handleData->getMinimumPollingTime);
AzureIoTClient 6:73793bae15ba 1665 if (isPollingAllowed)
AzureIoTClient 6:73793bae15ba 1666 {
AzureIoTClient 6:73793bae15ba 1667 HTTP_HEADERS_HANDLE responseHTTPHeaders = HTTPHeaders_Alloc();
AzureIoTClient 6:73793bae15ba 1668 if (responseHTTPHeaders == NULL)
AzureIoTClient 6:73793bae15ba 1669 {
Azure.IoT Build 13:848d52f93daf 1670 /*Codes_SRS_TRANSPORTMULTITHTTP_17_085: [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 6:73793bae15ba 1671 LogError("unable to HTTPHeaders_Alloc\r\n");
AzureIoTClient 6:73793bae15ba 1672 }
AzureIoTClient 6:73793bae15ba 1673 else
AzureIoTClient 6:73793bae15ba 1674 {
AzureIoTClient 6:73793bae15ba 1675 BUFFER_HANDLE responseContent = BUFFER_new();
AzureIoTClient 6:73793bae15ba 1676 if (responseContent == NULL)
AzureIoTClient 6:73793bae15ba 1677 {
Azure.IoT Build 13:848d52f93daf 1678 /*Codes_SRS_TRANSPORTMULTITHTTP_17_085: [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 6:73793bae15ba 1679 LogError("unable to BUFFER_new\r\n");
AzureIoTClient 6:73793bae15ba 1680 }
AzureIoTClient 6:73793bae15ba 1681 else
AzureIoTClient 6:73793bae15ba 1682 {
AzureIoTClient 6:73793bae15ba 1683 unsigned int statusCode;
Azure.IoT Build 13:848d52f93daf 1684 /*Codes_SRS_TRANSPORTMULTITHTTP_17_084: [Otherwise, IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters
AzureIoTClient 6:73793bae15ba 1685 requestType: GET
AzureIoTClient 6:73793bae15ba 1686 relativePath: the message HTTP relative path
AzureIoTClient 6:73793bae15ba 1687 requestHttpHeadersHandle: message HTTP request headers created by _Create
AzureIoTClient 6:73793bae15ba 1688 requestContent: NULL
AzureIoTClient 6:73793bae15ba 1689 statusCode: a pointer to unsigned int which shall be later examined
AzureIoTClient 6:73793bae15ba 1690 responseHeadearsHandle: a new instance of HTTP headers
AzureIoTClient 6:73793bae15ba 1691 responseContent: a new instance of buffer]
AzureIoTClient 6:73793bae15ba 1692 */
AzureIoTClient 6:73793bae15ba 1693 if (HTTPAPIEX_SAS_ExecuteRequest(
Azure.IoT Build 13:848d52f93daf 1694 deviceData->sasObject,
AzureIoTClient 6:73793bae15ba 1695 handleData->httpApiExHandle,
AzureIoTClient 6:73793bae15ba 1696 HTTPAPI_REQUEST_GET, /*requestType: GET*/
Azure.IoT Build 13:848d52f93daf 1697 STRING_c_str(deviceData->messageHTTPrelativePath), /*relativePath: the message HTTP relative path*/
Azure.IoT Build 13:848d52f93daf 1698 deviceData->messageHTTPrequestHeaders, /*requestHttpHeadersHandle: message HTTP request headers created by _Create*/
AzureIoTClient 6:73793bae15ba 1699 NULL, /*requestContent: NULL*/
AzureIoTClient 6:73793bae15ba 1700 &statusCode, /*statusCode: a pointer to unsigned int which shall be later examined*/
AzureIoTClient 6:73793bae15ba 1701 responseHTTPHeaders, /*responseHeadearsHandle: a new instance of HTTP headers*/
AzureIoTClient 6:73793bae15ba 1702 responseContent /*responseContent: a new instance of buffer*/
AzureIoTClient 6:73793bae15ba 1703 )
AzureIoTClient 6:73793bae15ba 1704 != HTTPAPIEX_OK)
AzureIoTClient 6:73793bae15ba 1705 {
Azure.IoT Build 13:848d52f93daf 1706 /*Codes_SRS_TRANSPORTMULTITHTTP_17_085: [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 6:73793bae15ba 1707 LogError("unable to HTTPAPIEX_ExecuteRequest\r\n");
AzureIoTClient 6:73793bae15ba 1708 }
AzureIoTClient 6:73793bae15ba 1709 else
AzureIoTClient 6:73793bae15ba 1710 {
AzureIoTClient 6:73793bae15ba 1711 /*HTTP dialogue was succesfull*/
AzureIoTClient 6:73793bae15ba 1712 if (timeNow == (time_t)(-1))
AzureIoTClient 6:73793bae15ba 1713 {
Azure.IoT Build 13:848d52f93daf 1714 deviceData->isFirstPoll = true;
AzureIoTClient 6:73793bae15ba 1715 }
AzureIoTClient 6:73793bae15ba 1716 else
AzureIoTClient 6:73793bae15ba 1717 {
Azure.IoT Build 13:848d52f93daf 1718 deviceData->isFirstPoll = false;
Azure.IoT Build 13:848d52f93daf 1719 deviceData->lastPollTime = timeNow;
AzureIoTClient 6:73793bae15ba 1720 }
AzureIoTClient 6:73793bae15ba 1721 if (statusCode == 204)
AzureIoTClient 6:73793bae15ba 1722 {
Azure.IoT Build 13:848d52f93daf 1723 /*Codes_SRS_TRANSPORTMULTITHTTP_17_086: [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 6:73793bae15ba 1724 /*this is an expected status code, means "no commands", but logging that creates panic*/
AzureIoTClient 6:73793bae15ba 1725
AzureIoTClient 6:73793bae15ba 1726 /*do nothing, advance to next action*/
AzureIoTClient 6:73793bae15ba 1727 }
AzureIoTClient 6:73793bae15ba 1728 else if (statusCode != 200)
AzureIoTClient 6:73793bae15ba 1729 {
Azure.IoT Build 13:848d52f93daf 1730 /*Codes_SRS_TRANSPORTMULTITHTTP_17_086: [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 6:73793bae15ba 1731 LogError("expected status code was 200, but actually was received %u... moving on\r\n", statusCode);
AzureIoTClient 6:73793bae15ba 1732 }
AzureIoTClient 6:73793bae15ba 1733 else
AzureIoTClient 6:73793bae15ba 1734 {
Azure.IoT Build 13:848d52f93daf 1735 /*Codes_SRS_TRANSPORTMULTITHTTP_17_087: [If status code is 200, then _DoWork shall make a copy of the value of the "ETag" http header.]*/
AzureIoTClient 6:73793bae15ba 1736 const char* etagValue = HTTPHeaders_FindHeaderValue(responseHTTPHeaders, "ETag");
AzureIoTClient 6:73793bae15ba 1737 if (etagValue == NULL)
AzureIoTClient 6:73793bae15ba 1738 {
AzureIoTClient 6:73793bae15ba 1739 LogError("unable to find a received header called \"E-Tag\"\r\n");
AzureIoTClient 6:73793bae15ba 1740 }
AzureIoTClient 6:73793bae15ba 1741 else
AzureIoTClient 6:73793bae15ba 1742 {
Azure.IoT Build 13:848d52f93daf 1743 /*Codes_SRS_TRANSPORTMULTITHTTP_17_088: [If no such header is found or is invalid, then _DoWork shall advance to the next action.]*/
AzureIoTClient 6:73793bae15ba 1744 size_t etagsize = strlen(etagValue);
AzureIoTClient 6:73793bae15ba 1745 if (
AzureIoTClient 6:73793bae15ba 1746 (etagsize < 2) ||
AzureIoTClient 6:73793bae15ba 1747 (etagValue[0] != '"') ||
AzureIoTClient 6:73793bae15ba 1748 (etagValue[etagsize - 1] != '"')
AzureIoTClient 6:73793bae15ba 1749 )
AzureIoTClient 6:73793bae15ba 1750 {
AzureIoTClient 6:73793bae15ba 1751 LogError("ETag is not a valid quoted string\r\n");
AzureIoTClient 6:73793bae15ba 1752 }
AzureIoTClient 6:73793bae15ba 1753 else
AzureIoTClient 6:73793bae15ba 1754 {
Azure.IoT Build 13:848d52f93daf 1755 /*Codes_SRS_TRANSPORTMULTITHTTP_17_089: [_DoWork shall assemble an IOTHUBMESSAGE_HANDLE from the received HTTP content (using the responseContent buffer).] */
AzureIoTClient 6:73793bae15ba 1756 IOTHUB_MESSAGE_HANDLE receivedMessage = IoTHubMessage_CreateFromByteArray(BUFFER_u_char(responseContent), BUFFER_length(responseContent));
AzureIoTClient 6:73793bae15ba 1757 if (receivedMessage == NULL)
AzureIoTClient 6:73793bae15ba 1758 {
Azure.IoT Build 13:848d52f93daf 1759 /*Codes_SRS_TRANSPORTMULTITHTTP_17_092: [If assembling the message fails in any way, then _DoWork shall "abandon" the message.]*/
AzureIoTClient 6:73793bae15ba 1760 LogError("unable to IoTHubMessage_CreateFromByteArray, trying to abandon the message... \r\n");
Azure.IoT Build 13:848d52f93daf 1761 abandonOrAcceptMessage(handleData, deviceData, etagValue, ABANDON);
AzureIoTClient 6:73793bae15ba 1762 }
AzureIoTClient 6:73793bae15ba 1763 else
AzureIoTClient 6:73793bae15ba 1764 {
Azure.IoT Build 13:848d52f93daf 1765 /*Codes_SRS_TRANSPORTMULTITHTTP_17_090: [All the HTTP headers of the form iothub-app-name:somecontent shall be transformed in message properties {name, somecontent}.]*/
Azure.IoT Build 13:848d52f93daf 1766 /*Codes_SRS_TRANSPORTMULTITHTTP_17_091: [The HTTP header of iothub-messageid shall be set in the MessageId.]*/
AzureIoTClient 6:73793bae15ba 1767 size_t nHeaders;
AzureIoTClient 6:73793bae15ba 1768 if (HTTPHeaders_GetHeaderCount(responseHTTPHeaders, &nHeaders) != HTTP_HEADERS_OK)
AzureIoTClient 6:73793bae15ba 1769 {
AzureIoTClient 6:73793bae15ba 1770 LogError("unable to get the count of HTTP headers\r\n");
Azure.IoT Build 13:848d52f93daf 1771 abandonOrAcceptMessage(handleData, deviceData, etagValue, ABANDON);
AzureIoTClient 6:73793bae15ba 1772 }
AzureIoTClient 6:73793bae15ba 1773 else
AzureIoTClient 6:73793bae15ba 1774 {
AzureIoTClient 6:73793bae15ba 1775 size_t i;
AzureIoTClient 6:73793bae15ba 1776 MAP_HANDLE properties = (nHeaders > 0) ? IoTHubMessage_Properties(receivedMessage) : NULL;
AzureIoTClient 6:73793bae15ba 1777 for (i = 0; i < nHeaders; i++)
AzureIoTClient 6:73793bae15ba 1778 {
AzureIoTClient 6:73793bae15ba 1779 char* completeHeader;
AzureIoTClient 6:73793bae15ba 1780 if (HTTPHeaders_GetHeader(responseHTTPHeaders, i, &completeHeader) != HTTP_HEADERS_OK)
AzureIoTClient 6:73793bae15ba 1781 {
AzureIoTClient 6:73793bae15ba 1782 break;
AzureIoTClient 6:73793bae15ba 1783 }
AzureIoTClient 6:73793bae15ba 1784 else
AzureIoTClient 6:73793bae15ba 1785 {
AzureIoTClient 6:73793bae15ba 1786 if (strncmp(IOTHUB_APP_PREFIX, completeHeader, strlen(IOTHUB_APP_PREFIX)) == 0)
AzureIoTClient 6:73793bae15ba 1787 {
AzureIoTClient 6:73793bae15ba 1788 /*looks like a property headers*/
AzureIoTClient 6:73793bae15ba 1789 /*there's a guaranteed ':' in the completeHeader, by HTTP_HEADERS module*/
AzureIoTClient 6:73793bae15ba 1790 char* whereIsColon = strchr(completeHeader, ':');
AzureIoTClient 7:48f0f78cd3ef 1791 if (whereIsColon != NULL)
AzureIoTClient 7:48f0f78cd3ef 1792 {
AzureIoTClient 7:48f0f78cd3ef 1793 *whereIsColon = '\0'; /*cut it down*/
AzureIoTClient 7:48f0f78cd3ef 1794 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 7:48f0f78cd3ef 1795 {
AzureIoTClient 7:48f0f78cd3ef 1796 free(completeHeader);
AzureIoTClient 7:48f0f78cd3ef 1797 break;
AzureIoTClient 7:48f0f78cd3ef 1798 }
AzureIoTClient 7:48f0f78cd3ef 1799 }
AzureIoTClient 7:48f0f78cd3ef 1800 }
AzureIoTClient 7:48f0f78cd3ef 1801 else if (strncmp(IOTHUB_MESSAGE_ID, completeHeader, strlen(IOTHUB_MESSAGE_ID)) == 0)
AzureIoTClient 7:48f0f78cd3ef 1802 {
AzureIoTClient 7:48f0f78cd3ef 1803 char* whereIsColon = strchr(completeHeader, ':');
AzureIoTClient 7:48f0f78cd3ef 1804 if (whereIsColon != NULL)
AzureIoTClient 6:73793bae15ba 1805 {
AzureIoTClient 7:48f0f78cd3ef 1806 *whereIsColon = '\0'; /*cut it down*/
AzureIoTClient 7:48f0f78cd3ef 1807 if (IoTHubMessage_SetMessageId(receivedMessage, whereIsColon + 2) != IOTHUB_MESSAGE_OK)
AzureIoTClient 7:48f0f78cd3ef 1808 {
AzureIoTClient 7:48f0f78cd3ef 1809 free(completeHeader);
AzureIoTClient 7:48f0f78cd3ef 1810 break;
AzureIoTClient 7:48f0f78cd3ef 1811 }
AzureIoTClient 7:48f0f78cd3ef 1812 }
AzureIoTClient 7:48f0f78cd3ef 1813 }
AzureIoTClient 7:48f0f78cd3ef 1814 else if (strncmp(IOTHUB_CORRELATION_ID, completeHeader, strlen(IOTHUB_CORRELATION_ID)) == 0)
AzureIoTClient 7:48f0f78cd3ef 1815 {
AzureIoTClient 7:48f0f78cd3ef 1816 char* whereIsColon = strchr(completeHeader, ':');
AzureIoTClient 7:48f0f78cd3ef 1817 if (whereIsColon != NULL)
AzureIoTClient 7:48f0f78cd3ef 1818 {
AzureIoTClient 7:48f0f78cd3ef 1819 *whereIsColon = '\0'; /*cut it down*/
AzureIoTClient 7:48f0f78cd3ef 1820 if (IoTHubMessage_SetCorrelationId(receivedMessage, whereIsColon + 2) != IOTHUB_MESSAGE_OK)
AzureIoTClient 7:48f0f78cd3ef 1821 {
AzureIoTClient 7:48f0f78cd3ef 1822 free(completeHeader);
AzureIoTClient 7:48f0f78cd3ef 1823 break;
AzureIoTClient 7:48f0f78cd3ef 1824 }
AzureIoTClient 6:73793bae15ba 1825 }
AzureIoTClient 6:73793bae15ba 1826 }
AzureIoTClient 6:73793bae15ba 1827 free(completeHeader);
AzureIoTClient 6:73793bae15ba 1828 }
AzureIoTClient 6:73793bae15ba 1829 }
AzureIoTClient 6:73793bae15ba 1830
AzureIoTClient 6:73793bae15ba 1831 if (i < nHeaders)
AzureIoTClient 6:73793bae15ba 1832 {
Azure.IoT Build 13:848d52f93daf 1833 abandonOrAcceptMessage(handleData, deviceData, etagValue, ABANDON);
AzureIoTClient 6:73793bae15ba 1834 }
AzureIoTClient 6:73793bae15ba 1835 else
AzureIoTClient 6:73793bae15ba 1836 {
Azure.IoT Build 13:848d52f93daf 1837 /*Codes_SRS_TRANSPORTMULTITHTTP_17_093: [Otherwise, _DoWork shall call IoTHubClient_LL_MessageCallback with parameters handle = iotHubClientHandle and message = newly created message.]*/
AzureIoTClient 6:73793bae15ba 1838 IOTHUBMESSAGE_DISPOSITION_RESULT messageResult = IoTHubClient_LL_MessageCallback(iotHubClientHandle, receivedMessage);
AzureIoTClient 6:73793bae15ba 1839 if (messageResult == IOTHUBMESSAGE_ACCEPTED)
AzureIoTClient 6:73793bae15ba 1840 {
Azure.IoT Build 13:848d52f93daf 1841 /*Codes_SRS_TRANSPORTMULTITHTTP_17_094: [If IoTHubClient_LL_MessageCallback returns IOTHUBMESSAGE_ACCEPTED then _DoWork shall "accept" the message.]*/
Azure.IoT Build 13:848d52f93daf 1842 abandonOrAcceptMessage(handleData, deviceData, etagValue, ACCEPT);
AzureIoTClient 6:73793bae15ba 1843 }
AzureIoTClient 6:73793bae15ba 1844 else if (messageResult == IOTHUBMESSAGE_REJECTED)
AzureIoTClient 6:73793bae15ba 1845 {
Azure.IoT Build 13:848d52f93daf 1846 /*Codes_SRS_TRANSPORTMULTITHTTP_17_095: [If IoTHubClient_LL_MessageCallback returns IOTHUBMESSAGE_REJECTED then _DoWork shall "reject" the message.]*/
Azure.IoT Build 13:848d52f93daf 1847 abandonOrAcceptMessage(handleData, deviceData, etagValue, REJECT);
AzureIoTClient 6:73793bae15ba 1848 }
AzureIoTClient 6:73793bae15ba 1849 else
AzureIoTClient 6:73793bae15ba 1850 {
Azure.IoT Build 13:848d52f93daf 1851 /*Codes_SRS_TRANSPORTMULTITHTTP_17_096: [If IoTHubClient_LL_MessageCallback returns IOTHUBMESSAGE_ABANDONED then _DoWork shall "abandon" the message.] */
Azure.IoT Build 13:848d52f93daf 1852 abandonOrAcceptMessage(handleData, deviceData, etagValue, ABANDON);
AzureIoTClient 6:73793bae15ba 1853 }
AzureIoTClient 6:73793bae15ba 1854 }
AzureIoTClient 6:73793bae15ba 1855 }
AzureIoTClient 6:73793bae15ba 1856 IoTHubMessage_Destroy(receivedMessage);
AzureIoTClient 6:73793bae15ba 1857 }
AzureIoTClient 6:73793bae15ba 1858 }
AzureIoTClient 6:73793bae15ba 1859
AzureIoTClient 6:73793bae15ba 1860 }
AzureIoTClient 6:73793bae15ba 1861 }
AzureIoTClient 6:73793bae15ba 1862 }
AzureIoTClient 6:73793bae15ba 1863 BUFFER_delete(responseContent);
AzureIoTClient 6:73793bae15ba 1864 }
AzureIoTClient 6:73793bae15ba 1865 HTTPHeaders_Free(responseHTTPHeaders);
AzureIoTClient 6:73793bae15ba 1866 }
AzureIoTClient 6:73793bae15ba 1867 }
AzureIoTClient 6:73793bae15ba 1868 else
AzureIoTClient 6:73793bae15ba 1869 {
AzureIoTClient 6:73793bae15ba 1870 /*isPollingAllowed is false... */
AzureIoTClient 6:73793bae15ba 1871 /*do nothing "shall be ignored*/
AzureIoTClient 6:73793bae15ba 1872 }
AzureIoTClient 6:73793bae15ba 1873 }
AzureIoTClient 6:73793bae15ba 1874 }
AzureIoTClient 6:73793bae15ba 1875
Azure.IoT Build 14:fae5c6b2cce3 1876 void IoTHubTransportHttp_DoWork(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle)
AzureIoTClient 6:73793bae15ba 1877 {
Azure.IoT Build 13:848d52f93daf 1878 /*Codes_SRS_TRANSPORTMULTITHTTP_17_049: [ If handle is NULL, then IoTHubTransportHttp_DoWork shall do nothing. ]*/
Azure.IoT Build 13:848d52f93daf 1879 /*Codes_SRS_TRANSPORTMULTITHTTP_17_140: [ If iotHubClientHandle is NULL, then IoTHubTransportHttp_DoWork shall do nothing. ]*/
Azure.IoT Build 13:848d52f93daf 1880
Azure.IoT Build 13:848d52f93daf 1881 (void)iotHubClientHandle; // use the perDevice handle.
Azure.IoT Build 14:fae5c6b2cce3 1882 if (handle != NULL)
AzureIoTClient 6:73793bae15ba 1883 {
Azure.IoT Build 13:848d52f93daf 1884 HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle;
Azure.IoT Build 13:848d52f93daf 1885 IOTHUB_DEVICE_HANDLE* listItem;
Azure.IoT Build 13:848d52f93daf 1886 size_t deviceListSize = VECTOR_size(handleData->perDeviceList);
Azure.IoT Build 13:848d52f93daf 1887 /*Codes_SRS_TRANSPORTMULTITHTTP_17_052: [ IoTHubTransportHttp_DoWork shall perform a round-robin loop through every deviceHandle in the transport device list, using the iotHubClientHandle field saved in the IOTHUB_DEVICE_HANDLE. ]*/
Azure.IoT Build 13:848d52f93daf 1888 /*Codes_SRS_TRANSPORTMULTITHTTP_17_050: [ IoTHubTransportHttp_DoWork shall call loop through the device list. ] */
Azure.IoT Build 13:848d52f93daf 1889 /*Codes_SRS_TRANSPORTMULTITHTTP_17_051: [ IF the list is empty, then IoTHubTransportHttp_DoWork shall do nothing. ]*/
Azure.IoT Build 13:848d52f93daf 1890 for (size_t i = 0; i < deviceListSize; i++)
Azure.IoT Build 13:848d52f93daf 1891 {
Azure.IoT Build 13:848d52f93daf 1892 listItem = VECTOR_element(handleData->perDeviceList, i);
Azure.IoT Build 13:848d52f93daf 1893 HTTPTRANSPORT_PERDEVICE_DATA* perDeviceItem = *(HTTPTRANSPORT_PERDEVICE_DATA**)(listItem);
Azure.IoT Build 13:848d52f93daf 1894 DoEvent(handleData, perDeviceItem, perDeviceItem->iotHubClientHandle);
Azure.IoT Build 13:848d52f93daf 1895 DoMessages(handleData, perDeviceItem, perDeviceItem->iotHubClientHandle);
Azure.IoT Build 13:848d52f93daf 1896
Azure.IoT Build 13:848d52f93daf 1897 }
Azure.IoT Build 13:848d52f93daf 1898 }
Azure.IoT Build 13:848d52f93daf 1899 else
Azure.IoT Build 13:848d52f93daf 1900 {
Azure.IoT Build 13:848d52f93daf 1901 LogError("Invalid Argument NULL call on DoWork.\r\n");
AzureIoTClient 6:73793bae15ba 1902 }
AzureIoTClient 6:73793bae15ba 1903 }
AzureIoTClient 6:73793bae15ba 1904
Azure.IoT Build 13:848d52f93daf 1905 IOTHUB_CLIENT_RESULT IoTHubTransportHttp_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus)
AzureIoTClient 6:73793bae15ba 1906 {
AzureIoTClient 6:73793bae15ba 1907 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 6:73793bae15ba 1908
AzureIoTClient 6:73793bae15ba 1909 if (handle == NULL)
AzureIoTClient 6:73793bae15ba 1910 {
Azure.IoT Build 13:848d52f93daf 1911 /*Codes_SRS_TRANSPORTMULTITHTTP_17_111: [ IoTHubTransportHttp_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter. ]*/
AzureIoTClient 6:73793bae15ba 1912 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 6:73793bae15ba 1913 LogError("Invalid handle to IoTHubClient HTTP transport instance.\r\n");
AzureIoTClient 6:73793bae15ba 1914 }
AzureIoTClient 6:73793bae15ba 1915 else if (iotHubClientStatus == NULL)
AzureIoTClient 6:73793bae15ba 1916 {
AzureIoTClient 6:73793bae15ba 1917 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 6:73793bae15ba 1918 LogError("Invalid pointer to output parameter IOTHUB_CLIENT_STATUS.\r\n");
AzureIoTClient 6:73793bae15ba 1919 }
AzureIoTClient 6:73793bae15ba 1920 else
AzureIoTClient 6:73793bae15ba 1921 {
Azure.IoT Build 13:848d52f93daf 1922 /*Codes_SRS_TRANSPORTMULTITHTTP_17_138: [ IoTHubTransportHttp_GetSendStatus shall locate deviceHandle in the transport device list by calling list_find_if. ]*/
Azure.IoT Build 13:848d52f93daf 1923 IOTHUB_DEVICE_HANDLE* listItem = get_perDeviceDataItem(handle);
Azure.IoT Build 13:848d52f93daf 1924 if (listItem == NULL)
Azure.IoT Build 13:848d52f93daf 1925 {
Azure.IoT Build 13:848d52f93daf 1926 /*Codes_SRS_TRANSPORTMULTITHTTP_17_139: [ If the device structure is not found, then this function shall fail and return with IOTHUB_CLIENT_INVALID_ARG. ]*/
Azure.IoT Build 13:848d52f93daf 1927 result = IOTHUB_CLIENT_INVALID_ARG;
Azure.IoT Build 13:848d52f93daf 1928 LogError("Device not found in transport list.\r\n");
Azure.IoT Build 13:848d52f93daf 1929 }
Azure.IoT Build 13:848d52f93daf 1930 else
Azure.IoT Build 13:848d52f93daf 1931 {
Azure.IoT Build 13:848d52f93daf 1932 HTTPTRANSPORT_PERDEVICE_DATA* deviceData = (HTTPTRANSPORT_PERDEVICE_DATA*)(*listItem);
Azure.IoT Build 13:848d52f93daf 1933 /* Codes_SRS_TRANSPORTMULTITHTTP_17_113: [ 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. ] */
Azure.IoT Build 13:848d52f93daf 1934 if (!DList_IsListEmpty(deviceData->waitingToSend))
AzureIoTClient 6:73793bae15ba 1935 {
AzureIoTClient 6:73793bae15ba 1936 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY;
AzureIoTClient 6:73793bae15ba 1937 }
Azure.IoT Build 13:848d52f93daf 1938 /* Codes_SRS_TRANSPORTMULTITHTTP_17_112: [ 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 6:73793bae15ba 1939 else
AzureIoTClient 6:73793bae15ba 1940 {
AzureIoTClient 6:73793bae15ba 1941 *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE;
AzureIoTClient 6:73793bae15ba 1942 }
AzureIoTClient 6:73793bae15ba 1943 result = IOTHUB_CLIENT_OK;
AzureIoTClient 6:73793bae15ba 1944 }
Azure.IoT Build 13:848d52f93daf 1945 }
AzureIoTClient 6:73793bae15ba 1946
AzureIoTClient 6:73793bae15ba 1947 return result;
AzureIoTClient 6:73793bae15ba 1948 }
AzureIoTClient 6:73793bae15ba 1949
Azure.IoT Build 14:fae5c6b2cce3 1950 IOTHUB_CLIENT_RESULT IoTHubTransportHttp_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value)
AzureIoTClient 6:73793bae15ba 1951 {
AzureIoTClient 6:73793bae15ba 1952 IOTHUB_CLIENT_RESULT result;
Azure.IoT Build 13:848d52f93daf 1953 /*Codes_SRS_TRANSPORTMULTITHTTP_17_114: [If handle parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
Azure.IoT Build 13:848d52f93daf 1954 /*Codes_SRS_TRANSPORTMULTITHTTP_17_115: [If option parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
Azure.IoT Build 13:848d52f93daf 1955 /*Codes_SRS_TRANSPORTMULTITHTTP_17_116: [If value parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */
AzureIoTClient 6:73793bae15ba 1956 if (
AzureIoTClient 6:73793bae15ba 1957 (handle == NULL) ||
AzureIoTClient 6:73793bae15ba 1958 (option == NULL) ||
AzureIoTClient 6:73793bae15ba 1959 (value == NULL)
AzureIoTClient 6:73793bae15ba 1960 )
AzureIoTClient 6:73793bae15ba 1961 {
AzureIoTClient 6:73793bae15ba 1962 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 6:73793bae15ba 1963 LogError("invalid parameter (NULL) passed to IoTHubTransportHttp_SetOption\r\n");
AzureIoTClient 6:73793bae15ba 1964 }
AzureIoTClient 6:73793bae15ba 1965 else
AzureIoTClient 6:73793bae15ba 1966 {
AzureIoTClient 6:73793bae15ba 1967 HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle;
Azure.IoT Build 13:848d52f93daf 1968 /*Codes_SRS_TRANSPORTMULTITHTTP_17_120: ["Batching"] */
AzureIoTClient 6:73793bae15ba 1969 if (strcmp("Batching", option) == 0)
AzureIoTClient 6:73793bae15ba 1970 {
Azure.IoT Build 13:848d52f93daf 1971 /*Codes_SRS_TRANSPORTMULTITHTTP_17_117: [If optionName is an option handled by IoTHubTransportHttp then it shall be set.] */
AzureIoTClient 6:73793bae15ba 1972 handleData->doBatchedTransfers = *(bool*)value;
AzureIoTClient 6:73793bae15ba 1973 result = IOTHUB_CLIENT_OK;
AzureIoTClient 6:73793bae15ba 1974 }
Azure.IoT Build 13:848d52f93daf 1975 /*Codes_SRS_TRANSPORTMULTITHTTP_17_121: ["MinimumPollingTime"] */
AzureIoTClient 6:73793bae15ba 1976 else if (strcmp("MinimumPollingTime", option) == 0)
AzureIoTClient 6:73793bae15ba 1977 {
AzureIoTClient 6:73793bae15ba 1978 handleData->getMinimumPollingTime = *(unsigned int*)value;
AzureIoTClient 6:73793bae15ba 1979 result = IOTHUB_CLIENT_OK;
AzureIoTClient 6:73793bae15ba 1980 }
AzureIoTClient 6:73793bae15ba 1981 else
AzureIoTClient 6:73793bae15ba 1982 {
Azure.IoT Build 13:848d52f93daf 1983 /*Codes_SRS_TRANSPORTMULTITHTTP_17_126: [ "TrustedCerts"] */
Azure.IoT Build 13:848d52f93daf 1984 /*Codes_SRS_TRANSPORTMULTITHTTP_17_127: [ NULL shall be allowed. ]*/
Azure.IoT Build 13:848d52f93daf 1985 /*Codes_SRS_TRANSPORTMULTITHTTP_17_129: [ This option shall passed down to the lower layer by calling HTTPAPIEX_SetOption. ]*/
Azure.IoT Build 13:848d52f93daf 1986 /*Codes_SRS_TRANSPORTMULTITHTTP_17_118: [Otherwise, IoTHubTransport_Http shall call HTTPAPIEX_SetOption with the same parameters and return the translated code.] */
AzureIoTClient 6:73793bae15ba 1987 HTTPAPIEX_RESULT HTTPAPIEX_result = HTTPAPIEX_SetOption(handleData->httpApiExHandle, option, value);
Azure.IoT Build 13:848d52f93daf 1988 /*Codes_SRS_TRANSPORTMULTITHTTP_17_119: [The following table translates HTTPAPIEX return codes to IOTHUB_CLIENT_RESULT return codes:] */
AzureIoTClient 6:73793bae15ba 1989 if (HTTPAPIEX_result == HTTPAPIEX_OK)
AzureIoTClient 6:73793bae15ba 1990 {
AzureIoTClient 6:73793bae15ba 1991 result = IOTHUB_CLIENT_OK;
AzureIoTClient 6:73793bae15ba 1992 }
AzureIoTClient 6:73793bae15ba 1993 else if (HTTPAPIEX_result == HTTPAPIEX_INVALID_ARG)
AzureIoTClient 6:73793bae15ba 1994 {
AzureIoTClient 6:73793bae15ba 1995 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 6:73793bae15ba 1996 LogError("HTTPAPIEX_SetOption failed\r\n");
AzureIoTClient 6:73793bae15ba 1997 }
AzureIoTClient 6:73793bae15ba 1998 else
AzureIoTClient 6:73793bae15ba 1999 {
AzureIoTClient 6:73793bae15ba 2000 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 6:73793bae15ba 2001 LogError("HTTPAPIEX_SetOption failed\r\n");
AzureIoTClient 6:73793bae15ba 2002 }
AzureIoTClient 6:73793bae15ba 2003 }
AzureIoTClient 6:73793bae15ba 2004 }
AzureIoTClient 6:73793bae15ba 2005 return result;
AzureIoTClient 6:73793bae15ba 2006 }