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 Apr 08 13:24:07 2016 -0700
Revision:
15:52602fffff8d
Parent:
14:fae5c6b2cce3
Child:
16:46f0f4f95c07
1.0.4

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