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:
AzureIoTClient
Date:
Mon May 09 14:36:41 2016 -0700
Revision:
17:7ef88cde801f
Parent:
16:46f0f4f95c07
Child:
18:ab990f6aa61f
1.0.6

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