Microsoft Azure IoTHub client HTTP transport

Dependents:   iothub_client_sample_http simplesample_http temp_sensor_anomaly

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

Committer:
AzureIoTClient
Date:
Mon May 22 10:34:45 2017 -0700
Revision:
31:c889be99d3f7
Parent:
30:655054f86a6e
Child:
32:4d4a226b072b
1.1.15

Who changed what in which revision?

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