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