Microsoft Azure IoTHub client HTTP transport

Dependents:   iothub_client_sample_http simplesample_http temp_sensor_anomaly

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

Committer:
AzureIoTClient
Date:
Mon Mar 05 17:39:48 2018 -0800
Revision:
35:786954ce12e9
Parent:
34:5aa0b7670697
Child:
36:36c1e1ca5679
1.2.0

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