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:
Fri Jun 17 17:02:21 2016 -0700
Revision:
18:ab990f6aa61f
Parent:
17:7ef88cde801f
Child:
19:d4f9e872bf3e
1.0.9

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