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
iothubtransporthttp.c@6:73793bae15ba, 2015-10-22 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Thu Oct 22 18:33:19 2015 -0700
- Revision:
- 6:73793bae15ba
- Child:
- 7:48f0f78cd3ef
v1.0.0-preview.4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AzureIoTClient | 6:73793bae15ba | 1 | // Copyright (c) Microsoft. All rights reserved. |
AzureIoTClient | 6:73793bae15ba | 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
AzureIoTClient | 6:73793bae15ba | 3 | |
AzureIoTClient | 6:73793bae15ba | 4 | #include <stdlib.h> |
AzureIoTClient | 6:73793bae15ba | 5 | #ifdef _CRTDBG_MAP_ALLOC |
AzureIoTClient | 6:73793bae15ba | 6 | #include <crtdbg.h> |
AzureIoTClient | 6:73793bae15ba | 7 | #endif |
AzureIoTClient | 6:73793bae15ba | 8 | #include "gballoc.h" |
AzureIoTClient | 6:73793bae15ba | 9 | |
AzureIoTClient | 6:73793bae15ba | 10 | #include <time.h> |
AzureIoTClient | 6:73793bae15ba | 11 | #include "iothub_client_private.h" |
AzureIoTClient | 6:73793bae15ba | 12 | #include "iothubtransporthttp.h" |
AzureIoTClient | 6:73793bae15ba | 13 | |
AzureIoTClient | 6:73793bae15ba | 14 | #include "httpapiexsas.h" |
AzureIoTClient | 6:73793bae15ba | 15 | #include "urlencode.h" |
AzureIoTClient | 6:73793bae15ba | 16 | #include "iot_logging.h" |
AzureIoTClient | 6:73793bae15ba | 17 | #include "httpapiex.h" |
AzureIoTClient | 6:73793bae15ba | 18 | #include "httpapiexsas.h" |
AzureIoTClient | 6:73793bae15ba | 19 | #include "strings.h" |
AzureIoTClient | 6:73793bae15ba | 20 | #include "base64.h" |
AzureIoTClient | 6:73793bae15ba | 21 | #include "doublylinkedlist.h" |
AzureIoTClient | 6:73793bae15ba | 22 | #include "httpheaders.h" |
AzureIoTClient | 6:73793bae15ba | 23 | #include "agenttime.h" |
AzureIoTClient | 6:73793bae15ba | 24 | |
AzureIoTClient | 6:73793bae15ba | 25 | #define IOTHUB_APP_PREFIX "iothub-app-" |
AzureIoTClient | 6:73793bae15ba | 26 | |
AzureIoTClient | 6:73793bae15ba | 27 | #define CONTENT_TYPE "Content-Type" |
AzureIoTClient | 6:73793bae15ba | 28 | #define APPLICATION_OCTET_STREAM "application/octet-stream" |
AzureIoTClient | 6:73793bae15ba | 29 | #define APPLICATION_VND_MICROSOFT_IOTHUB_JSON "application/vnd.microsoft.iothub.json" |
AzureIoTClient | 6:73793bae15ba | 30 | |
AzureIoTClient | 6:73793bae15ba | 31 | /*DEFAULT_GETMINIMUMPOLLINGTIME is the minimum time in seconds allowed between 2 consecutive GET issues to the service (GET=fetch messages)*/ |
AzureIoTClient | 6:73793bae15ba | 32 | /*the default is 25 minutes*/ |
AzureIoTClient | 6:73793bae15ba | 33 | #define DEFAULT_GETMINIMUMPOLLINGTIME ((unsigned int)25*60) |
AzureIoTClient | 6:73793bae15ba | 34 | |
AzureIoTClient | 6:73793bae15ba | 35 | #define MAXIMUM_MESSAGE_SIZE (255*1024-1) |
AzureIoTClient | 6:73793bae15ba | 36 | #define MAXIMUM_PAYLOAD_OVERHEAD 384 |
AzureIoTClient | 6:73793bae15ba | 37 | #define MAXIMUM_PROPERTY_OVERHEAD 16 |
AzureIoTClient | 6:73793bae15ba | 38 | |
AzureIoTClient | 6:73793bae15ba | 39 | /*forward declaration*/ |
AzureIoTClient | 6:73793bae15ba | 40 | static int appendMapToJSON(STRING_HANDLE existing, const char* const* keys, const char* const* values, size_t count); |
AzureIoTClient | 6:73793bae15ba | 41 | |
AzureIoTClient | 6:73793bae15ba | 42 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_069: [This function shall return a pointer to a structure of type TRANSPORT_PROVIDER having the following values for its fields:] */ |
AzureIoTClient | 6:73793bae15ba | 43 | static TRANSPORT_PROVIDER thisTransportProvider = |
AzureIoTClient | 6:73793bae15ba | 44 | { |
AzureIoTClient | 6:73793bae15ba | 45 | IoTHubTransportHttp_SetOption, /*pfIoTHubTransport_SetOption IoTHubTransport_SetOption; */ |
AzureIoTClient | 6:73793bae15ba | 46 | IoTHubTransportHttp_Create, /*pfIoTHubTransport_Create IoTHubTransport_Create; */ |
AzureIoTClient | 6:73793bae15ba | 47 | IoTHubTransportHttp_Destroy, /*pfIoTHubTransport_Destroy IoTHubTransport_Destroy; */ |
AzureIoTClient | 6:73793bae15ba | 48 | IoTHubTransportHttp_Subscribe, /*pfIoTHubTransport_Subscribe IoTHubTransport_Subscribe; */ |
AzureIoTClient | 6:73793bae15ba | 49 | IoTHubTransportHttp_Unsubscribe, /*pfIoTHubTransport_Unsubscribe IoTHubTransport_Unsubscribe; */ |
AzureIoTClient | 6:73793bae15ba | 50 | IoTHubTransportHttp_DoWork, /*pfIoTHubTransport_DoWork IoTHubTransport_DoWork; */ |
AzureIoTClient | 6:73793bae15ba | 51 | IoTHubTransportHttp_GetSendStatus /* pfIoTHubTransport_GetSendStatus IoTHubTransport_GetSendStatus */ |
AzureIoTClient | 6:73793bae15ba | 52 | }; |
AzureIoTClient | 6:73793bae15ba | 53 | |
AzureIoTClient | 6:73793bae15ba | 54 | const void* HTTP_Protocol(void) |
AzureIoTClient | 6:73793bae15ba | 55 | { |
AzureIoTClient | 6:73793bae15ba | 56 | return &thisTransportProvider; |
AzureIoTClient | 6:73793bae15ba | 57 | } |
AzureIoTClient | 6:73793bae15ba | 58 | |
AzureIoTClient | 6:73793bae15ba | 59 | typedef struct HTTPTRANSPORT_HANDLE_DATA_TAG |
AzureIoTClient | 6:73793bae15ba | 60 | { |
AzureIoTClient | 6:73793bae15ba | 61 | STRING_HANDLE eventHTTPrelativePath; |
AzureIoTClient | 6:73793bae15ba | 62 | STRING_HANDLE messageHTTPrelativePath; |
AzureIoTClient | 6:73793bae15ba | 63 | HTTP_HEADERS_HANDLE eventHTTPrequestHeaders; |
AzureIoTClient | 6:73793bae15ba | 64 | STRING_HANDLE hostName; |
AzureIoTClient | 6:73793bae15ba | 65 | HTTPAPIEX_HANDLE httpApiExHandle; |
AzureIoTClient | 6:73793bae15ba | 66 | HTTP_HEADERS_HANDLE messageHTTPrequestHeaders; |
AzureIoTClient | 6:73793bae15ba | 67 | STRING_HANDLE abandonHTTPrelativePathBegin; |
AzureIoTClient | 6:73793bae15ba | 68 | HTTPAPIEX_SAS_HANDLE sasObject; |
AzureIoTClient | 6:73793bae15ba | 69 | bool DoWork_PullMessage; |
AzureIoTClient | 6:73793bae15ba | 70 | bool doBatchedTransfers; |
AzureIoTClient | 6:73793bae15ba | 71 | bool isFirstPoll; |
AzureIoTClient | 6:73793bae15ba | 72 | unsigned int getMinimumPollingTime; |
AzureIoTClient | 6:73793bae15ba | 73 | time_t lastPollTime; |
AzureIoTClient | 6:73793bae15ba | 74 | PDLIST_ENTRY waitingToSend; |
AzureIoTClient | 6:73793bae15ba | 75 | DLIST_ENTRY eventConfirmations; /*holds items for event confirmations*/ |
AzureIoTClient | 6:73793bae15ba | 76 | }HTTPTRANSPORT_HANDLE_DATA; |
AzureIoTClient | 6:73793bae15ba | 77 | |
AzureIoTClient | 6:73793bae15ba | 78 | |
AzureIoTClient | 6:73793bae15ba | 79 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_003: [Otherwise IoTHubTransportHttp_Create shall create an immutable string (further called "event HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events?api-version=2015-08-15-preview".]*/ |
AzureIoTClient | 6:73793bae15ba | 80 | static void destroy_eventHTTPrelativePath(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 81 | { |
AzureIoTClient | 6:73793bae15ba | 82 | STRING_delete(handleData->eventHTTPrelativePath); |
AzureIoTClient | 6:73793bae15ba | 83 | handleData->eventHTTPrelativePath = NULL; |
AzureIoTClient | 6:73793bae15ba | 84 | } |
AzureIoTClient | 6:73793bae15ba | 85 | |
AzureIoTClient | 6:73793bae15ba | 86 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_003: [Otherwise IoTHubTransportHttp_Create shall create an immutable string (further called "event HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events?api-version=2015-08-15-preview".]*/ |
AzureIoTClient | 6:73793bae15ba | 87 | static bool create_eventHTTPrelativePath(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 88 | { |
AzureIoTClient | 6:73793bae15ba | 89 | bool result; |
AzureIoTClient | 6:73793bae15ba | 90 | STRING_HANDLE urlEncodedDeviceId = NULL; |
AzureIoTClient | 6:73793bae15ba | 91 | handleData->eventHTTPrelativePath = STRING_construct("/devices/"); |
AzureIoTClient | 6:73793bae15ba | 92 | if (handleData->eventHTTPrelativePath == NULL) |
AzureIoTClient | 6:73793bae15ba | 93 | { |
AzureIoTClient | 6:73793bae15ba | 94 | result = false; |
AzureIoTClient | 6:73793bae15ba | 95 | } |
AzureIoTClient | 6:73793bae15ba | 96 | else if (!( |
AzureIoTClient | 6:73793bae15ba | 97 | ((urlEncodedDeviceId = URL_EncodeString(config->upperConfig->deviceId)) != NULL) && |
AzureIoTClient | 6:73793bae15ba | 98 | (STRING_concat_with_STRING(handleData->eventHTTPrelativePath, urlEncodedDeviceId) == 0) && |
AzureIoTClient | 6:73793bae15ba | 99 | (STRING_concat(handleData->eventHTTPrelativePath, EVENT_ENDPOINT API_VERSION) == 0) |
AzureIoTClient | 6:73793bae15ba | 100 | )) |
AzureIoTClient | 6:73793bae15ba | 101 | { |
AzureIoTClient | 6:73793bae15ba | 102 | destroy_eventHTTPrelativePath(handleData); |
AzureIoTClient | 6:73793bae15ba | 103 | result = false; |
AzureIoTClient | 6:73793bae15ba | 104 | } |
AzureIoTClient | 6:73793bae15ba | 105 | else |
AzureIoTClient | 6:73793bae15ba | 106 | { |
AzureIoTClient | 6:73793bae15ba | 107 | result = true; |
AzureIoTClient | 6:73793bae15ba | 108 | } |
AzureIoTClient | 6:73793bae15ba | 109 | STRING_delete(urlEncodedDeviceId); |
AzureIoTClient | 6:73793bae15ba | 110 | return result; |
AzureIoTClient | 6:73793bae15ba | 111 | } |
AzureIoTClient | 6:73793bae15ba | 112 | |
AzureIoTClient | 6:73793bae15ba | 113 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_034: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called "message HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/devicebound?api-version=2015-08-15-preview".]*/ |
AzureIoTClient | 6:73793bae15ba | 114 | static void destroy_messageHTTPrelativePath(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 115 | { |
AzureIoTClient | 6:73793bae15ba | 116 | STRING_delete(handleData->messageHTTPrelativePath); |
AzureIoTClient | 6:73793bae15ba | 117 | handleData->messageHTTPrelativePath = NULL; |
AzureIoTClient | 6:73793bae15ba | 118 | } |
AzureIoTClient | 6:73793bae15ba | 119 | |
AzureIoTClient | 6:73793bae15ba | 120 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_034: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called "message HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/devicebound?api-version=2015-08-15-preview".]*/ |
AzureIoTClient | 6:73793bae15ba | 121 | static bool create_messageHTTPrelativePath(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 122 | { |
AzureIoTClient | 6:73793bae15ba | 123 | bool result; |
AzureIoTClient | 6:73793bae15ba | 124 | handleData->messageHTTPrelativePath = STRING_construct("/devices/"); |
AzureIoTClient | 6:73793bae15ba | 125 | if (handleData->messageHTTPrelativePath == NULL) |
AzureIoTClient | 6:73793bae15ba | 126 | { |
AzureIoTClient | 6:73793bae15ba | 127 | result = false; |
AzureIoTClient | 6:73793bae15ba | 128 | } |
AzureIoTClient | 6:73793bae15ba | 129 | else |
AzureIoTClient | 6:73793bae15ba | 130 | { |
AzureIoTClient | 6:73793bae15ba | 131 | STRING_HANDLE urlEncodedDeviceId = NULL; |
AzureIoTClient | 6:73793bae15ba | 132 | if (!( |
AzureIoTClient | 6:73793bae15ba | 133 | ((urlEncodedDeviceId = URL_EncodeString(config->upperConfig->deviceId)) != NULL) && |
AzureIoTClient | 6:73793bae15ba | 134 | (STRING_concat_with_STRING(handleData->messageHTTPrelativePath, urlEncodedDeviceId) == 0) && |
AzureIoTClient | 6:73793bae15ba | 135 | (STRING_concat(handleData->messageHTTPrelativePath, MESSAGE_ENDPOINT_HTTP API_VERSION) == 0) |
AzureIoTClient | 6:73793bae15ba | 136 | )) |
AzureIoTClient | 6:73793bae15ba | 137 | { |
AzureIoTClient | 6:73793bae15ba | 138 | result = false; |
AzureIoTClient | 6:73793bae15ba | 139 | destroy_messageHTTPrelativePath(handleData); |
AzureIoTClient | 6:73793bae15ba | 140 | } |
AzureIoTClient | 6:73793bae15ba | 141 | else |
AzureIoTClient | 6:73793bae15ba | 142 | { |
AzureIoTClient | 6:73793bae15ba | 143 | result = true; |
AzureIoTClient | 6:73793bae15ba | 144 | } |
AzureIoTClient | 6:73793bae15ba | 145 | STRING_delete(urlEncodedDeviceId); |
AzureIoTClient | 6:73793bae15ba | 146 | } |
AzureIoTClient | 6:73793bae15ba | 147 | |
AzureIoTClient | 6:73793bae15ba | 148 | return result; |
AzureIoTClient | 6:73793bae15ba | 149 | } |
AzureIoTClient | 6:73793bae15ba | 150 | |
AzureIoTClient | 6:73793bae15ba | 151 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_005: [Otherwise, IoTHubTransportHttp_Create shall create a set of HTTP headers (further on called "event HTTP request headers") consisting of the following fixed field names and values: |
AzureIoTClient | 6:73793bae15ba | 152 | "iothub-to":"/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events"; |
AzureIoTClient | 6:73793bae15ba | 153 | "Authorization":" " |
AzureIoTClient | 6:73793bae15ba | 154 | "Content-Type":"application/vnd.microsoft.iothub.json" |
AzureIoTClient | 6:73793bae15ba | 155 | "Accept":"application/json" |
AzureIoTClient | 6:73793bae15ba | 156 | "Connection":"Keep-Alive"]*/ |
AzureIoTClient | 6:73793bae15ba | 157 | static void destroy_eventHTTPrequestHeaders(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 158 | { |
AzureIoTClient | 6:73793bae15ba | 159 | HTTPHeaders_Free(handleData->eventHTTPrequestHeaders); |
AzureIoTClient | 6:73793bae15ba | 160 | handleData->eventHTTPrequestHeaders = NULL; |
AzureIoTClient | 6:73793bae15ba | 161 | } |
AzureIoTClient | 6:73793bae15ba | 162 | |
AzureIoTClient | 6:73793bae15ba | 163 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_005: [Otherwise, IoTHubTransportHttp_Create shall create a set of HTTP headers (further on called "event HTTP request headers") consisting of the following fixed field names and values: |
AzureIoTClient | 6:73793bae15ba | 164 | "iothub-to":"/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events"; |
AzureIoTClient | 6:73793bae15ba | 165 | "Authorization":" " |
AzureIoTClient | 6:73793bae15ba | 166 | "Content-Type":"application/vnd.microsoft.iothub.json" |
AzureIoTClient | 6:73793bae15ba | 167 | "Accept":"application/json" |
AzureIoTClient | 6:73793bae15ba | 168 | "Connection":"Keep-Alive"]*/ |
AzureIoTClient | 6:73793bae15ba | 169 | static bool create_eventHTTPrequestHeaders(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 170 | { |
AzureIoTClient | 6:73793bae15ba | 171 | bool result; |
AzureIoTClient | 6:73793bae15ba | 172 | handleData->eventHTTPrequestHeaders = HTTPHeaders_Alloc(); |
AzureIoTClient | 6:73793bae15ba | 173 | if (handleData->eventHTTPrequestHeaders == NULL) |
AzureIoTClient | 6:73793bae15ba | 174 | { |
AzureIoTClient | 6:73793bae15ba | 175 | result = false; |
AzureIoTClient | 6:73793bae15ba | 176 | } |
AzureIoTClient | 6:73793bae15ba | 177 | else |
AzureIoTClient | 6:73793bae15ba | 178 | { |
AzureIoTClient | 6:73793bae15ba | 179 | STRING_HANDLE temp = STRING_construct("/devices/"); |
AzureIoTClient | 6:73793bae15ba | 180 | if (temp == NULL) |
AzureIoTClient | 6:73793bae15ba | 181 | { |
AzureIoTClient | 6:73793bae15ba | 182 | result = false; |
AzureIoTClient | 6:73793bae15ba | 183 | destroy_eventHTTPrequestHeaders(handleData); |
AzureIoTClient | 6:73793bae15ba | 184 | } |
AzureIoTClient | 6:73793bae15ba | 185 | else |
AzureIoTClient | 6:73793bae15ba | 186 | { |
AzureIoTClient | 6:73793bae15ba | 187 | STRING_HANDLE urlEncodedDeviceId = NULL; |
AzureIoTClient | 6:73793bae15ba | 188 | if (!( |
AzureIoTClient | 6:73793bae15ba | 189 | ((urlEncodedDeviceId = URL_EncodeString(config->upperConfig->deviceId)) != NULL) && |
AzureIoTClient | 6:73793bae15ba | 190 | (STRING_concat_with_STRING(temp, urlEncodedDeviceId) == 0) && |
AzureIoTClient | 6:73793bae15ba | 191 | (STRING_concat(temp, EVENT_ENDPOINT) == 0) |
AzureIoTClient | 6:73793bae15ba | 192 | )) |
AzureIoTClient | 6:73793bae15ba | 193 | { |
AzureIoTClient | 6:73793bae15ba | 194 | result = false; |
AzureIoTClient | 6:73793bae15ba | 195 | destroy_eventHTTPrequestHeaders(handleData); |
AzureIoTClient | 6:73793bae15ba | 196 | } |
AzureIoTClient | 6:73793bae15ba | 197 | else |
AzureIoTClient | 6:73793bae15ba | 198 | { |
AzureIoTClient | 6:73793bae15ba | 199 | if (!( |
AzureIoTClient | 6:73793bae15ba | 200 | (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "iothub-to", STRING_c_str(temp)) == HTTP_HEADERS_OK) && |
AzureIoTClient | 6:73793bae15ba | 201 | (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "Authorization", " ") == HTTP_HEADERS_OK) && |
AzureIoTClient | 6:73793bae15ba | 202 | (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "Accept", "application/json") == HTTP_HEADERS_OK) && |
AzureIoTClient | 6:73793bae15ba | 203 | (HTTPHeaders_AddHeaderNameValuePair(handleData->eventHTTPrequestHeaders, "Connection", "Keep-Alive") == HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 204 | )) |
AzureIoTClient | 6:73793bae15ba | 205 | { |
AzureIoTClient | 6:73793bae15ba | 206 | result = false; |
AzureIoTClient | 6:73793bae15ba | 207 | destroy_eventHTTPrequestHeaders(handleData); |
AzureIoTClient | 6:73793bae15ba | 208 | } |
AzureIoTClient | 6:73793bae15ba | 209 | else |
AzureIoTClient | 6:73793bae15ba | 210 | { |
AzureIoTClient | 6:73793bae15ba | 211 | result = true; |
AzureIoTClient | 6:73793bae15ba | 212 | } |
AzureIoTClient | 6:73793bae15ba | 213 | } |
AzureIoTClient | 6:73793bae15ba | 214 | STRING_delete(temp); |
AzureIoTClient | 6:73793bae15ba | 215 | STRING_delete(urlEncodedDeviceId); |
AzureIoTClient | 6:73793bae15ba | 216 | } |
AzureIoTClient | 6:73793bae15ba | 217 | } |
AzureIoTClient | 6:73793bae15ba | 218 | return result; |
AzureIoTClient | 6:73793bae15ba | 219 | } |
AzureIoTClient | 6:73793bae15ba | 220 | |
AzureIoTClient | 6:73793bae15ba | 221 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_007: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called hostname) containing config->upperConfig->iotHubName + config->upperConfig->iotHubSuffix.]*/ |
AzureIoTClient | 6:73793bae15ba | 222 | static void destroy_hostName(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 223 | { |
AzureIoTClient | 6:73793bae15ba | 224 | STRING_delete(handleData->hostName); |
AzureIoTClient | 6:73793bae15ba | 225 | handleData->hostName = NULL; |
AzureIoTClient | 6:73793bae15ba | 226 | } |
AzureIoTClient | 6:73793bae15ba | 227 | |
AzureIoTClient | 6:73793bae15ba | 228 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_007: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called hostname) containing config->upperConfig->iotHubName + config->upperConfig->iotHubSuffix.]*/ |
AzureIoTClient | 6:73793bae15ba | 229 | static bool create_hostName(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 230 | { |
AzureIoTClient | 6:73793bae15ba | 231 | bool result; |
AzureIoTClient | 6:73793bae15ba | 232 | handleData->hostName = STRING_construct(config->upperConfig->iotHubName); |
AzureIoTClient | 6:73793bae15ba | 233 | if (handleData->hostName == NULL) |
AzureIoTClient | 6:73793bae15ba | 234 | { |
AzureIoTClient | 6:73793bae15ba | 235 | result = false; |
AzureIoTClient | 6:73793bae15ba | 236 | } |
AzureIoTClient | 6:73793bae15ba | 237 | else |
AzureIoTClient | 6:73793bae15ba | 238 | { |
AzureIoTClient | 6:73793bae15ba | 239 | if ((STRING_concat(handleData->hostName, ".") != 0) || |
AzureIoTClient | 6:73793bae15ba | 240 | (STRING_concat(handleData->hostName, config->upperConfig->iotHubSuffix) != 0)) |
AzureIoTClient | 6:73793bae15ba | 241 | { |
AzureIoTClient | 6:73793bae15ba | 242 | destroy_hostName(handleData); |
AzureIoTClient | 6:73793bae15ba | 243 | result = false; |
AzureIoTClient | 6:73793bae15ba | 244 | } |
AzureIoTClient | 6:73793bae15ba | 245 | else |
AzureIoTClient | 6:73793bae15ba | 246 | { |
AzureIoTClient | 6:73793bae15ba | 247 | result = true; |
AzureIoTClient | 6:73793bae15ba | 248 | } |
AzureIoTClient | 6:73793bae15ba | 249 | } |
AzureIoTClient | 6:73793bae15ba | 250 | return result; |
AzureIoTClient | 6:73793bae15ba | 251 | } |
AzureIoTClient | 6:73793bae15ba | 252 | |
AzureIoTClient | 6:73793bae15ba | 253 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_009: [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 | 254 | static void destroy_httpApiExHandle(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 255 | { |
AzureIoTClient | 6:73793bae15ba | 256 | HTTPAPIEX_Destroy(handleData->httpApiExHandle); |
AzureIoTClient | 6:73793bae15ba | 257 | handleData->httpApiExHandle = NULL; |
AzureIoTClient | 6:73793bae15ba | 258 | } |
AzureIoTClient | 6:73793bae15ba | 259 | |
AzureIoTClient | 6:73793bae15ba | 260 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_007: [Otherwise, IoTHubTransportHttp_Create shall create an immutable string (further called hostname) containing config->upperConfig->iotHubName + config->upperConfig->iotHubSuffix.]*/ |
AzureIoTClient | 6:73793bae15ba | 261 | static bool create_httpApiExHandle(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 262 | { |
AzureIoTClient | 6:73793bae15ba | 263 | bool result; |
AzureIoTClient | 6:73793bae15ba | 264 | (void)config; |
AzureIoTClient | 6:73793bae15ba | 265 | handleData->httpApiExHandle = HTTPAPIEX_Create(STRING_c_str(handleData->hostName)); |
AzureIoTClient | 6:73793bae15ba | 266 | if (handleData->httpApiExHandle == NULL) |
AzureIoTClient | 6:73793bae15ba | 267 | { |
AzureIoTClient | 6:73793bae15ba | 268 | result = false; |
AzureIoTClient | 6:73793bae15ba | 269 | } |
AzureIoTClient | 6:73793bae15ba | 270 | else |
AzureIoTClient | 6:73793bae15ba | 271 | { |
AzureIoTClient | 6:73793bae15ba | 272 | result = true; |
AzureIoTClient | 6:73793bae15ba | 273 | } |
AzureIoTClient | 6:73793bae15ba | 274 | return result; |
AzureIoTClient | 6:73793bae15ba | 275 | } |
AzureIoTClient | 6:73793bae15ba | 276 | |
AzureIoTClient | 6:73793bae15ba | 277 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_059: [Otherwise, IoTHubTransportHttp_Create shall create a set of HTTP headers (further on called "message HTTP request headers") consisting of the following fixed field names and values: |
AzureIoTClient | 6:73793bae15ba | 278 | "Authorization": " "]*/ |
AzureIoTClient | 6:73793bae15ba | 279 | static void destroy_messageHTTPrequestHeaders(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 280 | { |
AzureIoTClient | 6:73793bae15ba | 281 | HTTPHeaders_Free(handleData->messageHTTPrequestHeaders); |
AzureIoTClient | 6:73793bae15ba | 282 | handleData->messageHTTPrequestHeaders = NULL; |
AzureIoTClient | 6:73793bae15ba | 283 | } |
AzureIoTClient | 6:73793bae15ba | 284 | |
AzureIoTClient | 6:73793bae15ba | 285 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_059: [Otherwise, IoTHubTransportHttp_Create shall create a set of HTTP headers (further on called "message HTTP request headers") consisting of the following fixed field names and values: |
AzureIoTClient | 6:73793bae15ba | 286 | "Authorization": " "]*/ |
AzureIoTClient | 6:73793bae15ba | 287 | static bool create_messageHTTPrequestHeaders(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 288 | { |
AzureIoTClient | 6:73793bae15ba | 289 | bool result; |
AzureIoTClient | 6:73793bae15ba | 290 | (void)config; |
AzureIoTClient | 6:73793bae15ba | 291 | handleData->messageHTTPrequestHeaders = HTTPHeaders_Alloc(); |
AzureIoTClient | 6:73793bae15ba | 292 | if (handleData->messageHTTPrequestHeaders == NULL) |
AzureIoTClient | 6:73793bae15ba | 293 | { |
AzureIoTClient | 6:73793bae15ba | 294 | result = false; |
AzureIoTClient | 6:73793bae15ba | 295 | } |
AzureIoTClient | 6:73793bae15ba | 296 | else |
AzureIoTClient | 6:73793bae15ba | 297 | { |
AzureIoTClient | 6:73793bae15ba | 298 | if (HTTPHeaders_AddHeaderNameValuePair(handleData->messageHTTPrequestHeaders, "Authorization", " ") != HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 299 | { |
AzureIoTClient | 6:73793bae15ba | 300 | destroy_messageHTTPrequestHeaders(handleData); |
AzureIoTClient | 6:73793bae15ba | 301 | result = false; |
AzureIoTClient | 6:73793bae15ba | 302 | } |
AzureIoTClient | 6:73793bae15ba | 303 | else |
AzureIoTClient | 6:73793bae15ba | 304 | { |
AzureIoTClient | 6:73793bae15ba | 305 | result = true; |
AzureIoTClient | 6:73793bae15ba | 306 | } |
AzureIoTClient | 6:73793bae15ba | 307 | } |
AzureIoTClient | 6:73793bae15ba | 308 | return result; |
AzureIoTClient | 6:73793bae15ba | 309 | } |
AzureIoTClient | 6:73793bae15ba | 310 | |
AzureIoTClient | 6:73793bae15ba | 311 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_061: [Otherwise, IoTHubTransportHttp_Create shall create a STRING containing: "/devices/" + URL_ENCODED(device id) +"/messages/deviceBound/" called abandonHTTPrelativePathBegin.] */ |
AzureIoTClient | 6:73793bae15ba | 312 | static void destroy_abandonHTTPrelativePathBegin(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 313 | { |
AzureIoTClient | 6:73793bae15ba | 314 | STRING_delete(handleData->abandonHTTPrelativePathBegin); |
AzureIoTClient | 6:73793bae15ba | 315 | handleData->abandonHTTPrelativePathBegin = NULL; |
AzureIoTClient | 6:73793bae15ba | 316 | } |
AzureIoTClient | 6:73793bae15ba | 317 | |
AzureIoTClient | 6:73793bae15ba | 318 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_061: [Otherwise, IoTHubTransportHttp_Create shall create a STRING containing: "/devices/" + URL_ENCODED(device id) +"/messages/deviceBound/" called abandonHTTPrelativePathBegin.] */ |
AzureIoTClient | 6:73793bae15ba | 319 | static bool create_abandonHTTPrelativePathBegin(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 320 | { |
AzureIoTClient | 6:73793bae15ba | 321 | bool result; |
AzureIoTClient | 6:73793bae15ba | 322 | handleData->abandonHTTPrelativePathBegin = STRING_construct("/devices/"); |
AzureIoTClient | 6:73793bae15ba | 323 | if (handleData->abandonHTTPrelativePathBegin == NULL) |
AzureIoTClient | 6:73793bae15ba | 324 | { |
AzureIoTClient | 6:73793bae15ba | 325 | result = false; |
AzureIoTClient | 6:73793bae15ba | 326 | } |
AzureIoTClient | 6:73793bae15ba | 327 | else |
AzureIoTClient | 6:73793bae15ba | 328 | { |
AzureIoTClient | 6:73793bae15ba | 329 | STRING_HANDLE urlEncodedDeviceId = NULL; |
AzureIoTClient | 6:73793bae15ba | 330 | if (!( |
AzureIoTClient | 6:73793bae15ba | 331 | ((urlEncodedDeviceId = URL_EncodeString(config->upperConfig->deviceId)) != NULL) && |
AzureIoTClient | 6:73793bae15ba | 332 | (STRING_concat_with_STRING(handleData->abandonHTTPrelativePathBegin, urlEncodedDeviceId) == 0) && |
AzureIoTClient | 6:73793bae15ba | 333 | (STRING_concat(handleData->abandonHTTPrelativePathBegin, MESSAGE_ENDPOINT_HTTP_ETAG) == 0) |
AzureIoTClient | 6:73793bae15ba | 334 | )) |
AzureIoTClient | 6:73793bae15ba | 335 | { |
AzureIoTClient | 6:73793bae15ba | 336 | LogError("unable to STRING_concat\r\n"); |
AzureIoTClient | 6:73793bae15ba | 337 | STRING_delete(handleData->abandonHTTPrelativePathBegin); |
AzureIoTClient | 6:73793bae15ba | 338 | result = false; |
AzureIoTClient | 6:73793bae15ba | 339 | } |
AzureIoTClient | 6:73793bae15ba | 340 | else |
AzureIoTClient | 6:73793bae15ba | 341 | { |
AzureIoTClient | 6:73793bae15ba | 342 | result = true; |
AzureIoTClient | 6:73793bae15ba | 343 | } |
AzureIoTClient | 6:73793bae15ba | 344 | STRING_delete(urlEncodedDeviceId); |
AzureIoTClient | 6:73793bae15ba | 345 | } |
AzureIoTClient | 6:73793bae15ba | 346 | return result; |
AzureIoTClient | 6:73793bae15ba | 347 | } |
AzureIoTClient | 6:73793bae15ba | 348 | |
AzureIoTClient | 6:73793bae15ba | 349 | static void destroy_SASObject(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 350 | { |
AzureIoTClient | 6:73793bae15ba | 351 | HTTPAPIEX_SAS_Destroy(handleData->sasObject); |
AzureIoTClient | 6:73793bae15ba | 352 | handleData->sasObject = NULL; |
AzureIoTClient | 6:73793bae15ba | 353 | } |
AzureIoTClient | 6:73793bae15ba | 354 | |
AzureIoTClient | 6:73793bae15ba | 355 | static bool create_deviceSASObject(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 356 | { |
AzureIoTClient | 6:73793bae15ba | 357 | STRING_HANDLE keyName; |
AzureIoTClient | 6:73793bae15ba | 358 | bool result; |
AzureIoTClient | 6:73793bae15ba | 359 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_001: [IoTHubTransportHttp_Create shall invoke URL_EncodeString with an argument of device id.]*/ |
AzureIoTClient | 6:73793bae15ba | 360 | keyName = URL_EncodeString(config->upperConfig->deviceId); |
AzureIoTClient | 6:73793bae15ba | 361 | if (keyName == NULL) |
AzureIoTClient | 6:73793bae15ba | 362 | { |
AzureIoTClient | 6:73793bae15ba | 363 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_002: [If the encode fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 364 | result = false; |
AzureIoTClient | 6:73793bae15ba | 365 | } |
AzureIoTClient | 6:73793bae15ba | 366 | else |
AzureIoTClient | 6:73793bae15ba | 367 | { |
AzureIoTClient | 6:73793bae15ba | 368 | STRING_HANDLE uriResource; |
AzureIoTClient | 6:73793bae15ba | 369 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_003: [IoTHubTransportHttp_Create shall invoke STRING_clone using the previously created hostname.]*/ |
AzureIoTClient | 6:73793bae15ba | 370 | uriResource = STRING_clone(handleData->hostName); |
AzureIoTClient | 6:73793bae15ba | 371 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_004: [If the clone fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 372 | if (uriResource != NULL) |
AzureIoTClient | 6:73793bae15ba | 373 | { |
AzureIoTClient | 6:73793bae15ba | 374 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_005: [IoTHubTransportHttp_Create shall invoke STRING_concat with arguments uriResource and the string "/devices/".]*/ |
AzureIoTClient | 6:73793bae15ba | 375 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_006: [If the concat fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 376 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_007: [IoTHubTransportHttp_Create shall invoke STRING_concat_with_STRING with arguments uriResource and keyName.]*/ |
AzureIoTClient | 6:73793bae15ba | 377 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_008: [If the STRING_concat_with_STRING fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 378 | if ((STRING_concat(uriResource, "/devices/") == 0) && |
AzureIoTClient | 6:73793bae15ba | 379 | (STRING_concat_with_STRING(uriResource, keyName) == 0)) |
AzureIoTClient | 6:73793bae15ba | 380 | { |
AzureIoTClient | 6:73793bae15ba | 381 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_009: [IoTHubTransportHttp_Create shall invoke STRING_construct with an argument of config->upperConfig->deviceKey.]*/ |
AzureIoTClient | 6:73793bae15ba | 382 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_010: [If the STRING_construct fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 383 | STRING_HANDLE key = STRING_construct(config->upperConfig->deviceKey); |
AzureIoTClient | 6:73793bae15ba | 384 | if (key != NULL) |
AzureIoTClient | 6:73793bae15ba | 385 | { |
AzureIoTClient | 6:73793bae15ba | 386 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_013: [The keyName is shortened to zero length, if that fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 387 | if (STRING_empty(keyName) != 0) |
AzureIoTClient | 6:73793bae15ba | 388 | { |
AzureIoTClient | 6:73793bae15ba | 389 | LogError("Unable to form the device key name for the SAS\r\n"); |
AzureIoTClient | 6:73793bae15ba | 390 | result = false; |
AzureIoTClient | 6:73793bae15ba | 391 | } |
AzureIoTClient | 6:73793bae15ba | 392 | else |
AzureIoTClient | 6:73793bae15ba | 393 | { |
AzureIoTClient | 6:73793bae15ba | 394 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_011: [IoTHubTransportHttp_Create shall invoke HTTPAPIEX_SAS_Create with arguments key, uriResource, and zero length keyName.]*/ |
AzureIoTClient | 6:73793bae15ba | 395 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_06_012: [If the HTTPAPIEX_SAS_Create fails then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 396 | handleData->sasObject = HTTPAPIEX_SAS_Create(key, uriResource, keyName); |
AzureIoTClient | 6:73793bae15ba | 397 | result = (handleData->sasObject != NULL) ? (true) : (false); |
AzureIoTClient | 6:73793bae15ba | 398 | } |
AzureIoTClient | 6:73793bae15ba | 399 | STRING_delete(key); |
AzureIoTClient | 6:73793bae15ba | 400 | } |
AzureIoTClient | 6:73793bae15ba | 401 | else |
AzureIoTClient | 6:73793bae15ba | 402 | { |
AzureIoTClient | 6:73793bae15ba | 403 | result = false; |
AzureIoTClient | 6:73793bae15ba | 404 | } |
AzureIoTClient | 6:73793bae15ba | 405 | } |
AzureIoTClient | 6:73793bae15ba | 406 | else |
AzureIoTClient | 6:73793bae15ba | 407 | { |
AzureIoTClient | 6:73793bae15ba | 408 | result = false; |
AzureIoTClient | 6:73793bae15ba | 409 | } |
AzureIoTClient | 6:73793bae15ba | 410 | STRING_delete(uriResource); |
AzureIoTClient | 6:73793bae15ba | 411 | } |
AzureIoTClient | 6:73793bae15ba | 412 | else |
AzureIoTClient | 6:73793bae15ba | 413 | { |
AzureIoTClient | 6:73793bae15ba | 414 | result = false; |
AzureIoTClient | 6:73793bae15ba | 415 | } |
AzureIoTClient | 6:73793bae15ba | 416 | STRING_delete(keyName); |
AzureIoTClient | 6:73793bae15ba | 417 | } |
AzureIoTClient | 6:73793bae15ba | 418 | return result; |
AzureIoTClient | 6:73793bae15ba | 419 | } |
AzureIoTClient | 6:73793bae15ba | 420 | |
AzureIoTClient | 6:73793bae15ba | 421 | TRANSPORT_HANDLE IoTHubTransportHttp_Create(const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 422 | { |
AzureIoTClient | 6:73793bae15ba | 423 | HTTPTRANSPORT_HANDLE_DATA* result; |
AzureIoTClient | 6:73793bae15ba | 424 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_001: [If parameter config is NULL then IoTHubTransportHttp_Create shall fail and return NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 425 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_002: [IoTHubTransportHttp_Create shall fail and return NULL if any fields of the config structure are NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 426 | if (config == NULL) |
AzureIoTClient | 6:73793bae15ba | 427 | { |
AzureIoTClient | 6:73793bae15ba | 428 | LogError("invalid arg (configuration is missing)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 429 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 430 | } |
AzureIoTClient | 6:73793bae15ba | 431 | else if (config->upperConfig == NULL) |
AzureIoTClient | 6:73793bae15ba | 432 | { |
AzureIoTClient | 6:73793bae15ba | 433 | LogError("invalid arg (upperConfig is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 434 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 435 | } |
AzureIoTClient | 6:73793bae15ba | 436 | else if (config->waitingToSend == NULL) |
AzureIoTClient | 6:73793bae15ba | 437 | { |
AzureIoTClient | 6:73793bae15ba | 438 | LogError("invalid arg (waitingToSend is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 439 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 440 | } |
AzureIoTClient | 6:73793bae15ba | 441 | else if (config->upperConfig->protocol == NULL) |
AzureIoTClient | 6:73793bae15ba | 442 | { |
AzureIoTClient | 6:73793bae15ba | 443 | LogError("invalid arg (protocol is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 444 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 445 | } |
AzureIoTClient | 6:73793bae15ba | 446 | else if (config->upperConfig->deviceId == NULL) |
AzureIoTClient | 6:73793bae15ba | 447 | { |
AzureIoTClient | 6:73793bae15ba | 448 | LogError("invalid arg (deviceId is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 449 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 450 | } |
AzureIoTClient | 6:73793bae15ba | 451 | else if (config->upperConfig->iotHubName == NULL) |
AzureIoTClient | 6:73793bae15ba | 452 | { |
AzureIoTClient | 6:73793bae15ba | 453 | LogError("invalid arg (iotHubName is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 454 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 455 | } |
AzureIoTClient | 6:73793bae15ba | 456 | else if (config->upperConfig->iotHubSuffix == NULL) |
AzureIoTClient | 6:73793bae15ba | 457 | { |
AzureIoTClient | 6:73793bae15ba | 458 | LogError("invalid arg (iotHubSuffix is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 459 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 460 | } |
AzureIoTClient | 6:73793bae15ba | 461 | else if (config->upperConfig->deviceKey == NULL) |
AzureIoTClient | 6:73793bae15ba | 462 | { |
AzureIoTClient | 6:73793bae15ba | 463 | LogError("invalid arg (deviceKey is NULL)\r\n"); |
AzureIoTClient | 6:73793bae15ba | 464 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 465 | } |
AzureIoTClient | 6:73793bae15ba | 466 | else |
AzureIoTClient | 6:73793bae15ba | 467 | { |
AzureIoTClient | 6:73793bae15ba | 468 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_003: [Otherwise IoTHubTransportHttp_Create shall create an immutable string (further called "event HTTP relative path") from the following pieces: "/devices/" + URL_ENCODED(config->upperConfig->deviceId) + "/messages/events?api-version=2015-08-15-preview".]*/ |
AzureIoTClient | 6:73793bae15ba | 469 | result = (HTTPTRANSPORT_HANDLE_DATA*)malloc(sizeof(HTTPTRANSPORT_HANDLE_DATA)); |
AzureIoTClient | 6:73793bae15ba | 470 | if (result == NULL) |
AzureIoTClient | 6:73793bae15ba | 471 | { |
AzureIoTClient | 6:73793bae15ba | 472 | LogError("unable to malloc\r\n"); |
AzureIoTClient | 6:73793bae15ba | 473 | } |
AzureIoTClient | 6:73793bae15ba | 474 | else |
AzureIoTClient | 6:73793bae15ba | 475 | { |
AzureIoTClient | 6:73793bae15ba | 476 | bool was_eventHTTPrelativePath_ok = create_eventHTTPrelativePath(result, config); |
AzureIoTClient | 6:73793bae15ba | 477 | bool was_messageHTTPrelativePath_ok = was_eventHTTPrelativePath_ok && create_messageHTTPrelativePath(result, config); |
AzureIoTClient | 6:73793bae15ba | 478 | bool was_eventHTTPrequestHeaders_ok = was_messageHTTPrelativePath_ok && create_eventHTTPrequestHeaders(result, config); |
AzureIoTClient | 6:73793bae15ba | 479 | bool was_hostName_ok = was_eventHTTPrequestHeaders_ok && create_hostName(result, config); |
AzureIoTClient | 6:73793bae15ba | 480 | bool was_httpApiExHandle_ok = was_hostName_ok && create_httpApiExHandle(result, config); |
AzureIoTClient | 6:73793bae15ba | 481 | bool was_messageHTTPrequestHeaders_ok = was_httpApiExHandle_ok && create_messageHTTPrequestHeaders(result, config); |
AzureIoTClient | 6:73793bae15ba | 482 | bool was_abandonHTTPrelativePathBegin_ok = was_messageHTTPrequestHeaders_ok && create_abandonHTTPrelativePathBegin(result, config); |
AzureIoTClient | 6:73793bae15ba | 483 | bool was_sasObject_ok = was_abandonHTTPrelativePathBegin_ok && create_deviceSASObject(result, config); |
AzureIoTClient | 6:73793bae15ba | 484 | |
AzureIoTClient | 6:73793bae15ba | 485 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_011: [Otherwise, IoTHubTransportHttp_Create shall set a flag called "DoWork_PullMessage" to false, succeed and return a non-NULL value.]*/ |
AzureIoTClient | 6:73793bae15ba | 486 | if (was_sasObject_ok) |
AzureIoTClient | 6:73793bae15ba | 487 | { |
AzureIoTClient | 6:73793bae15ba | 488 | result->DoWork_PullMessage = false; |
AzureIoTClient | 6:73793bae15ba | 489 | result->doBatchedTransfers = false; |
AzureIoTClient | 6:73793bae15ba | 490 | result->isFirstPoll = true; |
AzureIoTClient | 6:73793bae15ba | 491 | result->getMinimumPollingTime = DEFAULT_GETMINIMUMPOLLINGTIME; |
AzureIoTClient | 6:73793bae15ba | 492 | result->waitingToSend = config->waitingToSend; |
AzureIoTClient | 6:73793bae15ba | 493 | DList_InitializeListHead(&(result->eventConfirmations)); |
AzureIoTClient | 6:73793bae15ba | 494 | } |
AzureIoTClient | 6:73793bae15ba | 495 | else |
AzureIoTClient | 6:73793bae15ba | 496 | { |
AzureIoTClient | 6:73793bae15ba | 497 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_004: [If creating the string fail for any reason then IoTHubTransportHttp_Create shall fail and return NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 498 | if (was_eventHTTPrelativePath_ok) destroy_eventHTTPrelativePath(result); |
AzureIoTClient | 6:73793bae15ba | 499 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_035: [If creating the message HTTP relative path fails, then IoTHubTransportHttp_Create shall fail and return NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 500 | if (was_messageHTTPrelativePath_ok) destroy_messageHTTPrelativePath(result); |
AzureIoTClient | 6:73793bae15ba | 501 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_006: [If creating the event HTTP request headers fails, then IoTHubTransportHttp_Create shall fail and return NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 502 | if (was_eventHTTPrequestHeaders_ok) destroy_eventHTTPrequestHeaders(result); |
AzureIoTClient | 6:73793bae15ba | 503 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_008: [If creating the hostname fails then IoTHubTransportHttp_Create shall fail and return NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 504 | if (was_hostName_ok) destroy_hostName(result); |
AzureIoTClient | 6:73793bae15ba | 505 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_010: [If creating the HTTPAPIEX_HANDLE fails then IoTHubTransportHttp_Create shall fail and return NULL.] */ |
AzureIoTClient | 6:73793bae15ba | 506 | if (was_httpApiExHandle_ok) destroy_httpApiExHandle(result); |
AzureIoTClient | 6:73793bae15ba | 507 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_060: [If creating message HTTP request headers then IoTHubTransportHttp_Create shall fail and return NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 508 | if (was_messageHTTPrequestHeaders_ok) destroy_messageHTTPrequestHeaders(result); |
AzureIoTClient | 6:73793bae15ba | 509 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_062: [If creating the abandonHTTPrelativePathBegin fails then IoTHubTransportHttp_Create shall fail and return NULL] */ |
AzureIoTClient | 6:73793bae15ba | 510 | if (was_abandonHTTPrelativePathBegin_ok) destroy_abandonHTTPrelativePathBegin(result); |
AzureIoTClient | 6:73793bae15ba | 511 | |
AzureIoTClient | 6:73793bae15ba | 512 | free(result); |
AzureIoTClient | 6:73793bae15ba | 513 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 514 | } |
AzureIoTClient | 6:73793bae15ba | 515 | } |
AzureIoTClient | 6:73793bae15ba | 516 | } |
AzureIoTClient | 6:73793bae15ba | 517 | return result; |
AzureIoTClient | 6:73793bae15ba | 518 | } |
AzureIoTClient | 6:73793bae15ba | 519 | |
AzureIoTClient | 6:73793bae15ba | 520 | void IoTHubTransportHttp_Destroy(TRANSPORT_HANDLE handle) |
AzureIoTClient | 6:73793bae15ba | 521 | { |
AzureIoTClient | 6:73793bae15ba | 522 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_012: [IoTHubTransportHttp_Destroy shall do nothing if parameter handle is NULL.]*/ |
AzureIoTClient | 6:73793bae15ba | 523 | if (handle != NULL) |
AzureIoTClient | 6:73793bae15ba | 524 | { |
AzureIoTClient | 6:73793bae15ba | 525 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_013: [Otherwise IoTHubTransportHttp_Destroy shall free all the resources currently in use.] */ |
AzureIoTClient | 6:73793bae15ba | 526 | destroy_eventHTTPrelativePath(handle); |
AzureIoTClient | 6:73793bae15ba | 527 | destroy_messageHTTPrelativePath(handle); |
AzureIoTClient | 6:73793bae15ba | 528 | destroy_eventHTTPrequestHeaders(handle); |
AzureIoTClient | 6:73793bae15ba | 529 | destroy_hostName(handle); |
AzureIoTClient | 6:73793bae15ba | 530 | destroy_httpApiExHandle(handle); |
AzureIoTClient | 6:73793bae15ba | 531 | destroy_messageHTTPrequestHeaders(handle); |
AzureIoTClient | 6:73793bae15ba | 532 | destroy_abandonHTTPrelativePathBegin(handle); |
AzureIoTClient | 6:73793bae15ba | 533 | destroy_SASObject(handle); |
AzureIoTClient | 6:73793bae15ba | 534 | free(handle); |
AzureIoTClient | 6:73793bae15ba | 535 | } |
AzureIoTClient | 6:73793bae15ba | 536 | } |
AzureIoTClient | 6:73793bae15ba | 537 | |
AzureIoTClient | 6:73793bae15ba | 538 | int IoTHubTransportHttp_Subscribe(TRANSPORT_HANDLE handle) |
AzureIoTClient | 6:73793bae15ba | 539 | { |
AzureIoTClient | 6:73793bae15ba | 540 | int result; |
AzureIoTClient | 6:73793bae15ba | 541 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_014: [If parameter handle is NULL then IoTHubTransportHttp_Subscribe shall fail and return a non-zero value.] */ |
AzureIoTClient | 6:73793bae15ba | 542 | if (handle == NULL) |
AzureIoTClient | 6:73793bae15ba | 543 | { |
AzureIoTClient | 6:73793bae15ba | 544 | LogError("invalid arg passed to IoTHubTransportHttp_Subscribe\r\n"); |
AzureIoTClient | 6:73793bae15ba | 545 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 546 | } |
AzureIoTClient | 6:73793bae15ba | 547 | else |
AzureIoTClient | 6:73793bae15ba | 548 | { |
AzureIoTClient | 6:73793bae15ba | 549 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_056: [Otherwise, IoTHubTransportHttp_Subscribe shall set the flag called DoWork_PullMessages to true and succeed.] */ |
AzureIoTClient | 6:73793bae15ba | 550 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 6:73793bae15ba | 551 | handleData->DoWork_PullMessage = true; |
AzureIoTClient | 6:73793bae15ba | 552 | result = 0; |
AzureIoTClient | 6:73793bae15ba | 553 | } |
AzureIoTClient | 6:73793bae15ba | 554 | return result; |
AzureIoTClient | 6:73793bae15ba | 555 | } |
AzureIoTClient | 6:73793bae15ba | 556 | |
AzureIoTClient | 6:73793bae15ba | 557 | void IoTHubTransportHttp_Unsubscribe(TRANSPORT_HANDLE handle) |
AzureIoTClient | 6:73793bae15ba | 558 | { |
AzureIoTClient | 6:73793bae15ba | 559 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_016: [If parameter handle is NULL then IoTHubTransportHttp_Unsubscribe shall do nothing.] */ |
AzureIoTClient | 6:73793bae15ba | 560 | if (handle != NULL) |
AzureIoTClient | 6:73793bae15ba | 561 | { |
AzureIoTClient | 6:73793bae15ba | 562 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 6:73793bae15ba | 563 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_058: [Otherwise it shall set the flag DoWork_PullMessage to false.] */ |
AzureIoTClient | 6:73793bae15ba | 564 | handleData->DoWork_PullMessage = false; |
AzureIoTClient | 6:73793bae15ba | 565 | } |
AzureIoTClient | 6:73793bae15ba | 566 | } |
AzureIoTClient | 6:73793bae15ba | 567 | |
AzureIoTClient | 6:73793bae15ba | 568 | /*produces a representation of the properties, if they exist*/ |
AzureIoTClient | 6:73793bae15ba | 569 | /*if they do not exist, produces ""*/ |
AzureIoTClient | 6:73793bae15ba | 570 | static int concat_Properties(STRING_HANDLE existing, MAP_HANDLE map, size_t* propertiesMessageSizeContribution) |
AzureIoTClient | 6:73793bae15ba | 571 | { |
AzureIoTClient | 6:73793bae15ba | 572 | int result; |
AzureIoTClient | 6:73793bae15ba | 573 | const char*const* keys; |
AzureIoTClient | 6:73793bae15ba | 574 | const char*const* values; |
AzureIoTClient | 6:73793bae15ba | 575 | size_t count; |
AzureIoTClient | 6:73793bae15ba | 576 | if (Map_GetInternals(map, &keys, &values, &count) != MAP_OK) |
AzureIoTClient | 6:73793bae15ba | 577 | { |
AzureIoTClient | 6:73793bae15ba | 578 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 579 | LogError("error while Map_GetInternals\r\n"); |
AzureIoTClient | 6:73793bae15ba | 580 | } |
AzureIoTClient | 6:73793bae15ba | 581 | else |
AzureIoTClient | 6:73793bae15ba | 582 | { |
AzureIoTClient | 6:73793bae15ba | 583 | |
AzureIoTClient | 6:73793bae15ba | 584 | if (count == 0) |
AzureIoTClient | 6:73793bae15ba | 585 | { |
AzureIoTClient | 6:73793bae15ba | 586 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_076: [If IoTHubMessage does not have properties, then "properties":{...} shall be missing from the payload*/ |
AzureIoTClient | 6:73793bae15ba | 587 | /*no properties - do nothing with existing*/ |
AzureIoTClient | 6:73793bae15ba | 588 | result = 0; |
AzureIoTClient | 6:73793bae15ba | 589 | *propertiesMessageSizeContribution = 0; |
AzureIoTClient | 6:73793bae15ba | 590 | } |
AzureIoTClient | 6:73793bae15ba | 591 | else |
AzureIoTClient | 6:73793bae15ba | 592 | { |
AzureIoTClient | 6:73793bae15ba | 593 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_075: [If IoTHubMessage has properties, then they shall be serialized at the same level as "body" using the following pattern: "properties":{"iothub-app-name1":"value1","iothub-app-name2":"value2*/ |
AzureIoTClient | 6:73793bae15ba | 594 | if (STRING_concat(existing, ",\"properties\":") != 0) |
AzureIoTClient | 6:73793bae15ba | 595 | { |
AzureIoTClient | 6:73793bae15ba | 596 | /*go ahead and return it*/ |
AzureIoTClient | 6:73793bae15ba | 597 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 598 | LogError("failed STRING_concat\r\n"); |
AzureIoTClient | 6:73793bae15ba | 599 | } |
AzureIoTClient | 6:73793bae15ba | 600 | else if (appendMapToJSON(existing, keys, values, count) != 0) |
AzureIoTClient | 6:73793bae15ba | 601 | { |
AzureIoTClient | 6:73793bae15ba | 602 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 603 | LogError("unable to append the properties\r\n"); |
AzureIoTClient | 6:73793bae15ba | 604 | } |
AzureIoTClient | 6:73793bae15ba | 605 | else |
AzureIoTClient | 6:73793bae15ba | 606 | { |
AzureIoTClient | 6:73793bae15ba | 607 | /*all is fine*/ |
AzureIoTClient | 6:73793bae15ba | 608 | size_t i; |
AzureIoTClient | 6:73793bae15ba | 609 | *propertiesMessageSizeContribution = 0; |
AzureIoTClient | 6:73793bae15ba | 610 | for (i = 0;i < count;i++) |
AzureIoTClient | 6:73793bae15ba | 611 | { |
AzureIoTClient | 6:73793bae15ba | 612 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_120: [Every property name shall add to the message size the length of the property name + the length of the property value + 16 bytes.] */ |
AzureIoTClient | 6:73793bae15ba | 613 | *propertiesMessageSizeContribution += (strlen(keys[i]) + strlen(values[i]) + MAXIMUM_PROPERTY_OVERHEAD); |
AzureIoTClient | 6:73793bae15ba | 614 | } |
AzureIoTClient | 6:73793bae15ba | 615 | result = 0; |
AzureIoTClient | 6:73793bae15ba | 616 | } |
AzureIoTClient | 6:73793bae15ba | 617 | } |
AzureIoTClient | 6:73793bae15ba | 618 | } |
AzureIoTClient | 6:73793bae15ba | 619 | return result; |
AzureIoTClient | 6:73793bae15ba | 620 | } |
AzureIoTClient | 6:73793bae15ba | 621 | |
AzureIoTClient | 6:73793bae15ba | 622 | /*produces a JSON representation of the map : {"a": "value_of_a","b":"value_of_b"}*/ |
AzureIoTClient | 6:73793bae15ba | 623 | 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 | 624 | { |
AzureIoTClient | 6:73793bae15ba | 625 | int result; |
AzureIoTClient | 6:73793bae15ba | 626 | if (STRING_concat(existing, "{") != 0) |
AzureIoTClient | 6:73793bae15ba | 627 | { |
AzureIoTClient | 6:73793bae15ba | 628 | /*go on and return it*/ |
AzureIoTClient | 6:73793bae15ba | 629 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 630 | LogError("STRING_construct failed\r\n"); |
AzureIoTClient | 6:73793bae15ba | 631 | } |
AzureIoTClient | 6:73793bae15ba | 632 | else |
AzureIoTClient | 6:73793bae15ba | 633 | { |
AzureIoTClient | 6:73793bae15ba | 634 | size_t i; |
AzureIoTClient | 6:73793bae15ba | 635 | for (i = 0; i < count; i++) |
AzureIoTClient | 6:73793bae15ba | 636 | { |
AzureIoTClient | 6:73793bae15ba | 637 | if (!( |
AzureIoTClient | 6:73793bae15ba | 638 | (STRING_concat(existing, (i == 0) ? "\"" IOTHUB_APP_PREFIX : ",\"" IOTHUB_APP_PREFIX) == 0) && |
AzureIoTClient | 6:73793bae15ba | 639 | (STRING_concat(existing, keys[i]) == 0) && |
AzureIoTClient | 6:73793bae15ba | 640 | (STRING_concat(existing, "\":\"") == 0) && |
AzureIoTClient | 6:73793bae15ba | 641 | (STRING_concat(existing, values[i]) == 0) && |
AzureIoTClient | 6:73793bae15ba | 642 | (STRING_concat(existing, "\"") == 0) |
AzureIoTClient | 6:73793bae15ba | 643 | )) |
AzureIoTClient | 6:73793bae15ba | 644 | { |
AzureIoTClient | 6:73793bae15ba | 645 | LogError("unable to STRING_concat\r\n"); |
AzureIoTClient | 6:73793bae15ba | 646 | break; |
AzureIoTClient | 6:73793bae15ba | 647 | } |
AzureIoTClient | 6:73793bae15ba | 648 | } |
AzureIoTClient | 6:73793bae15ba | 649 | |
AzureIoTClient | 6:73793bae15ba | 650 | if (i < count) |
AzureIoTClient | 6:73793bae15ba | 651 | { |
AzureIoTClient | 6:73793bae15ba | 652 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 653 | /*error, let it go through*/ |
AzureIoTClient | 6:73793bae15ba | 654 | } |
AzureIoTClient | 6:73793bae15ba | 655 | else if (STRING_concat(existing, "}") != 0) |
AzureIoTClient | 6:73793bae15ba | 656 | { |
AzureIoTClient | 6:73793bae15ba | 657 | result = __LINE__; |
AzureIoTClient | 6:73793bae15ba | 658 | LogError("unable to STRING_concat\r\n"); |
AzureIoTClient | 6:73793bae15ba | 659 | } |
AzureIoTClient | 6:73793bae15ba | 660 | else |
AzureIoTClient | 6:73793bae15ba | 661 | { |
AzureIoTClient | 6:73793bae15ba | 662 | /*all is fine*/ |
AzureIoTClient | 6:73793bae15ba | 663 | result = 0; |
AzureIoTClient | 6:73793bae15ba | 664 | } |
AzureIoTClient | 6:73793bae15ba | 665 | } |
AzureIoTClient | 6:73793bae15ba | 666 | return result; |
AzureIoTClient | 6:73793bae15ba | 667 | } |
AzureIoTClient | 6:73793bae15ba | 668 | |
AzureIoTClient | 6:73793bae15ba | 669 | /*makes the following string:{"body":"base64 encoding of the message content"[,"properties":{"a":"valueOfA"}]}*/ |
AzureIoTClient | 6:73793bae15ba | 670 | /*return NULL if there was a failure, or a non-NULL STRING_HANDLE that contains the intended data*/ |
AzureIoTClient | 6:73793bae15ba | 671 | static STRING_HANDLE make1EventJSONitem(PDLIST_ENTRY item, size_t *messageSizeContribution) |
AzureIoTClient | 6:73793bae15ba | 672 | { |
AzureIoTClient | 6:73793bae15ba | 673 | STRING_HANDLE result; /*temp wants to contain :{"body":"base64 encoding of the message content"[,"properties":{"a":"valueOfA"}]}*/ |
AzureIoTClient | 6:73793bae15ba | 674 | IOTHUB_MESSAGE_LIST* message = containingRecord(item, IOTHUB_MESSAGE_LIST, entry); |
AzureIoTClient | 6:73793bae15ba | 675 | IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message->messageHandle); |
AzureIoTClient | 6:73793bae15ba | 676 | |
AzureIoTClient | 6:73793bae15ba | 677 | switch (contentType) |
AzureIoTClient | 6:73793bae15ba | 678 | { |
AzureIoTClient | 6:73793bae15ba | 679 | case IOTHUBMESSAGE_BYTEARRAY: |
AzureIoTClient | 6:73793bae15ba | 680 | { |
AzureIoTClient | 6:73793bae15ba | 681 | result = STRING_construct("{\"body\":\""); |
AzureIoTClient | 6:73793bae15ba | 682 | if (result == NULL) |
AzureIoTClient | 6:73793bae15ba | 683 | { |
AzureIoTClient | 6:73793bae15ba | 684 | LogError("unable to STRING_construct\r\n"); |
AzureIoTClient | 6:73793bae15ba | 685 | } |
AzureIoTClient | 6:73793bae15ba | 686 | else |
AzureIoTClient | 6:73793bae15ba | 687 | { |
AzureIoTClient | 6:73793bae15ba | 688 | const unsigned char* source; |
AzureIoTClient | 6:73793bae15ba | 689 | size_t size; |
AzureIoTClient | 6:73793bae15ba | 690 | |
AzureIoTClient | 6:73793bae15ba | 691 | if (IoTHubMessage_GetByteArray(message->messageHandle, &source, &size) != IOTHUB_MESSAGE_OK) |
AzureIoTClient | 6:73793bae15ba | 692 | { |
AzureIoTClient | 6:73793bae15ba | 693 | LogError("unable to get the data for the message.\r\n"); |
AzureIoTClient | 6:73793bae15ba | 694 | STRING_delete(result); |
AzureIoTClient | 6:73793bae15ba | 695 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 696 | } |
AzureIoTClient | 6:73793bae15ba | 697 | else |
AzureIoTClient | 6:73793bae15ba | 698 | { |
AzureIoTClient | 6:73793bae15ba | 699 | STRING_HANDLE encoded = Base64_Encode_Bytes(source, size); |
AzureIoTClient | 6:73793bae15ba | 700 | if (encoded == NULL) |
AzureIoTClient | 6:73793bae15ba | 701 | { |
AzureIoTClient | 6:73793bae15ba | 702 | LogError("unable to Base64_Encode_Bytes.\r\n"); |
AzureIoTClient | 6:73793bae15ba | 703 | STRING_delete(result); |
AzureIoTClient | 6:73793bae15ba | 704 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 705 | } |
AzureIoTClient | 6:73793bae15ba | 706 | else |
AzureIoTClient | 6:73793bae15ba | 707 | { |
AzureIoTClient | 6:73793bae15ba | 708 | size_t propertiesSize; |
AzureIoTClient | 6:73793bae15ba | 709 | if (!( |
AzureIoTClient | 6:73793bae15ba | 710 | (STRING_concat_with_STRING(result, encoded) == 0) && |
AzureIoTClient | 6:73793bae15ba | 711 | (STRING_concat(result, "\"") == 0) && /*\" because closing value*/ |
AzureIoTClient | 6:73793bae15ba | 712 | (concat_Properties(result, IoTHubMessage_Properties(message->messageHandle), &propertiesSize) == 0) && |
AzureIoTClient | 6:73793bae15ba | 713 | (STRING_concat(result, "},") == 0) /*the last comma shall be replaced by a ']' by DaCr's suggestion (which is awesome enough to receive credits in the source code)*/ |
AzureIoTClient | 6:73793bae15ba | 714 | )) |
AzureIoTClient | 6:73793bae15ba | 715 | { |
AzureIoTClient | 6:73793bae15ba | 716 | STRING_delete(result); |
AzureIoTClient | 6:73793bae15ba | 717 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 718 | LogError("unable to STRING_concat_with_STRING.\r\n"); |
AzureIoTClient | 6:73793bae15ba | 719 | } |
AzureIoTClient | 6:73793bae15ba | 720 | else |
AzureIoTClient | 6:73793bae15ba | 721 | { |
AzureIoTClient | 6:73793bae15ba | 722 | /*all is fine... */ |
AzureIoTClient | 6:73793bae15ba | 723 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_119: [The message size is computed from the length of the payload + 384.] */ |
AzureIoTClient | 6:73793bae15ba | 724 | *messageSizeContribution = size + MAXIMUM_PAYLOAD_OVERHEAD + propertiesSize; |
AzureIoTClient | 6:73793bae15ba | 725 | } |
AzureIoTClient | 6:73793bae15ba | 726 | STRING_delete(encoded); |
AzureIoTClient | 6:73793bae15ba | 727 | } |
AzureIoTClient | 6:73793bae15ba | 728 | } |
AzureIoTClient | 6:73793bae15ba | 729 | } |
AzureIoTClient | 6:73793bae15ba | 730 | break; |
AzureIoTClient | 6:73793bae15ba | 731 | } |
AzureIoTClient | 6:73793bae15ba | 732 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_113: [If a messages to be send has type IOTHUBMESSAGE_STRING, then its serialization shall be {"body":"JSON encoding of the string", "base64Encoded":false}] */ |
AzureIoTClient | 6:73793bae15ba | 733 | case IOTHUBMESSAGE_STRING: |
AzureIoTClient | 6:73793bae15ba | 734 | { |
AzureIoTClient | 6:73793bae15ba | 735 | result = STRING_construct("{\"body\":"); |
AzureIoTClient | 6:73793bae15ba | 736 | if (result == NULL) |
AzureIoTClient | 6:73793bae15ba | 737 | { |
AzureIoTClient | 6:73793bae15ba | 738 | LogError("unable to STRING_construct\r\n"); |
AzureIoTClient | 6:73793bae15ba | 739 | } |
AzureIoTClient | 6:73793bae15ba | 740 | else |
AzureIoTClient | 6:73793bae15ba | 741 | { |
AzureIoTClient | 6:73793bae15ba | 742 | const char* source = IoTHubMessage_GetString(message->messageHandle); |
AzureIoTClient | 6:73793bae15ba | 743 | if (source == NULL) |
AzureIoTClient | 6:73793bae15ba | 744 | { |
AzureIoTClient | 6:73793bae15ba | 745 | LogError("unable to IoTHubMessage_GetString\r\n"); |
AzureIoTClient | 6:73793bae15ba | 746 | STRING_delete(result); |
AzureIoTClient | 6:73793bae15ba | 747 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 748 | } |
AzureIoTClient | 6:73793bae15ba | 749 | else |
AzureIoTClient | 6:73793bae15ba | 750 | { |
AzureIoTClient | 6:73793bae15ba | 751 | STRING_HANDLE asJson = STRING_new_JSON(source); |
AzureIoTClient | 6:73793bae15ba | 752 | if (asJson == NULL) |
AzureIoTClient | 6:73793bae15ba | 753 | { |
AzureIoTClient | 6:73793bae15ba | 754 | LogError("unable to STRING_new_JSON\r\n"); |
AzureIoTClient | 6:73793bae15ba | 755 | STRING_delete(result); |
AzureIoTClient | 6:73793bae15ba | 756 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 757 | } |
AzureIoTClient | 6:73793bae15ba | 758 | else |
AzureIoTClient | 6:73793bae15ba | 759 | { |
AzureIoTClient | 6:73793bae15ba | 760 | size_t propertiesSize; |
AzureIoTClient | 6:73793bae15ba | 761 | if (!( |
AzureIoTClient | 6:73793bae15ba | 762 | (STRING_concat_with_STRING(result, asJson) == 0) && |
AzureIoTClient | 6:73793bae15ba | 763 | (STRING_concat(result, ",\"base64Encoded\":false") == 0) && |
AzureIoTClient | 6:73793bae15ba | 764 | (concat_Properties(result, IoTHubMessage_Properties(message->messageHandle), &propertiesSize) == 0) && |
AzureIoTClient | 6:73793bae15ba | 765 | (STRING_concat(result, "},") == 0) /*the last comma shall be replaced by a ']' by DaCr's suggestion (which is awesome enough to receive credits in the source code)*/ |
AzureIoTClient | 6:73793bae15ba | 766 | )) |
AzureIoTClient | 6:73793bae15ba | 767 | { |
AzureIoTClient | 6:73793bae15ba | 768 | LogError("unable to STRING_concat_with_STRING"); |
AzureIoTClient | 6:73793bae15ba | 769 | STRING_delete(result); |
AzureIoTClient | 6:73793bae15ba | 770 | result = NULL; |
AzureIoTClient | 6:73793bae15ba | 771 | } |
AzureIoTClient | 6:73793bae15ba | 772 | else |
AzureIoTClient | 6:73793bae15ba | 773 | { |
AzureIoTClient | 6:73793bae15ba | 774 | /*result has the intended content*/ |
AzureIoTClient | 6:73793bae15ba | 775 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_119: [The message size is computed from the length of the payload + 384.] */ |
AzureIoTClient | 6:73793bae15ba | 776 | *messageSizeContribution = strlen(source) + MAXIMUM_PAYLOAD_OVERHEAD + propertiesSize; |
AzureIoTClient | 6:73793bae15ba | 777 | } |
AzureIoTClient | 6:73793bae15ba | 778 | STRING_delete(asJson); |
AzureIoTClient | 6:73793bae15ba | 779 | } |
AzureIoTClient | 6:73793bae15ba | 780 | } |
AzureIoTClient | 6:73793bae15ba | 781 | } |
AzureIoTClient | 6:73793bae15ba | 782 | break; |
AzureIoTClient | 6:73793bae15ba | 783 | } |
AzureIoTClient | 6:73793bae15ba | 784 | default: |
AzureIoTClient | 6:73793bae15ba | 785 | { |
AzureIoTClient | 6:73793bae15ba | 786 | LogError("an unknown message type was encountered (%d)\r\n", contentType); |
AzureIoTClient | 6:73793bae15ba | 787 | result = NULL; /*unknown message type*/ |
AzureIoTClient | 6:73793bae15ba | 788 | break; |
AzureIoTClient | 6:73793bae15ba | 789 | } |
AzureIoTClient | 6:73793bae15ba | 790 | } |
AzureIoTClient | 6:73793bae15ba | 791 | return result; |
AzureIoTClient | 6:73793bae15ba | 792 | } |
AzureIoTClient | 6:73793bae15ba | 793 | |
AzureIoTClient | 6:73793bae15ba | 794 | #define MAKE_PAYLOAD_RESULT_VALUES \ |
AzureIoTClient | 6:73793bae15ba | 795 | MAKE_PAYLOAD_OK, /*returned when there is a payload to be later send by HTTP*/ \ |
AzureIoTClient | 6:73793bae15ba | 796 | MAKE_PAYLOAD_NO_ITEMS, /*returned when there are no items to be send*/ \ |
AzureIoTClient | 6:73793bae15ba | 797 | MAKE_PAYLOAD_ERROR, /*returned when there were errors*/ \ |
AzureIoTClient | 6:73793bae15ba | 798 | MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT /*returned when the first item doesn't fit*/ |
AzureIoTClient | 6:73793bae15ba | 799 | |
AzureIoTClient | 6:73793bae15ba | 800 | DEFINE_ENUM(MAKE_PAYLOAD_RESULT, MAKE_PAYLOAD_RESULT_VALUES); |
AzureIoTClient | 6:73793bae15ba | 801 | |
AzureIoTClient | 6:73793bae15ba | 802 | /*this function assembles several {"body":"base64 encoding of the message content"," base64Encoded": true} into 1 payload*/ |
AzureIoTClient | 6:73793bae15ba | 803 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_070: [IoTHubTransportHttp_DoWork shall build the following string:[{"body":"base64 encoding of the message1 content"},{"body":"base64 encoding of the message2 content"}...]]*/ |
AzureIoTClient | 6:73793bae15ba | 804 | static MAKE_PAYLOAD_RESULT makePayload(HTTPTRANSPORT_HANDLE_DATA* handleData, STRING_HANDLE* payload) |
AzureIoTClient | 6:73793bae15ba | 805 | { |
AzureIoTClient | 6:73793bae15ba | 806 | MAKE_PAYLOAD_RESULT result; |
AzureIoTClient | 6:73793bae15ba | 807 | size_t allMessagesSize = 0; |
AzureIoTClient | 6:73793bae15ba | 808 | *payload = STRING_construct("["); |
AzureIoTClient | 6:73793bae15ba | 809 | if (*payload == NULL) |
AzureIoTClient | 6:73793bae15ba | 810 | { |
AzureIoTClient | 6:73793bae15ba | 811 | LogError("unable to STRING_construct\r\n"); |
AzureIoTClient | 6:73793bae15ba | 812 | result = MAKE_PAYLOAD_ERROR; |
AzureIoTClient | 6:73793bae15ba | 813 | } |
AzureIoTClient | 6:73793bae15ba | 814 | else |
AzureIoTClient | 6:73793bae15ba | 815 | { |
AzureIoTClient | 6:73793bae15ba | 816 | bool isFirst = true; |
AzureIoTClient | 6:73793bae15ba | 817 | PDLIST_ENTRY actual; |
AzureIoTClient | 6:73793bae15ba | 818 | /*either all the items enter the list or only some*/ |
AzureIoTClient | 6:73793bae15ba | 819 | result = MAKE_PAYLOAD_OK; /*optimistically initializing it*/ |
AzureIoTClient | 6:73793bae15ba | 820 | bool keepGoing = true; /*keepGoing gets sometimes to false from within the loop*/ |
AzureIoTClient | 6:73793bae15ba | 821 | while (keepGoing && ((actual = handleData->waitingToSend->Flink) != handleData->waitingToSend)) |
AzureIoTClient | 6:73793bae15ba | 822 | { |
AzureIoTClient | 6:73793bae15ba | 823 | size_t messageSize; |
AzureIoTClient | 6:73793bae15ba | 824 | STRING_HANDLE temp = make1EventJSONitem(actual, &messageSize); |
AzureIoTClient | 6:73793bae15ba | 825 | if (isFirst) |
AzureIoTClient | 6:73793bae15ba | 826 | { |
AzureIoTClient | 6:73793bae15ba | 827 | isFirst = false; |
AzureIoTClient | 6:73793bae15ba | 828 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_073: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/ |
AzureIoTClient | 6:73793bae15ba | 829 | if (temp == NULL) /*first item failed to create, nothing to send*/ |
AzureIoTClient | 6:73793bae15ba | 830 | { |
AzureIoTClient | 6:73793bae15ba | 831 | result = MAKE_PAYLOAD_ERROR; |
AzureIoTClient | 6:73793bae15ba | 832 | STRING_delete(*payload); |
AzureIoTClient | 6:73793bae15ba | 833 | *payload = NULL; |
AzureIoTClient | 6:73793bae15ba | 834 | keepGoing = false; |
AzureIoTClient | 6:73793bae15ba | 835 | } |
AzureIoTClient | 6:73793bae15ba | 836 | else |
AzureIoTClient | 6:73793bae15ba | 837 | { |
AzureIoTClient | 6:73793bae15ba | 838 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_071: [If the oldest message in waitingToSend causes the message size to exceed the message size limit then it shall be removed from waitingToSend, and IoTHubClient_LL_SendComplete shall be called. Parameter PDLIST_ENTRY completed shall point to a list containing only the oldest item, and parameter IOTHUB_BATCHSTATE result shall be set to IOTHUB_BATCHSTATE_FAILED.]*/ |
AzureIoTClient | 6:73793bae15ba | 839 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_118: [The message size shall be limited to 255KB - 1 byte.]*/ |
AzureIoTClient | 6:73793bae15ba | 840 | if (messageSize > MAXIMUM_MESSAGE_SIZE) |
AzureIoTClient | 6:73793bae15ba | 841 | { |
AzureIoTClient | 6:73793bae15ba | 842 | PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
AzureIoTClient | 6:73793bae15ba | 843 | DList_InsertTailList(&(handleData->eventConfirmations), head); |
AzureIoTClient | 6:73793bae15ba | 844 | result = MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT; |
AzureIoTClient | 6:73793bae15ba | 845 | STRING_delete(*payload); |
AzureIoTClient | 6:73793bae15ba | 846 | *payload = NULL; |
AzureIoTClient | 6:73793bae15ba | 847 | keepGoing = false; |
AzureIoTClient | 6:73793bae15ba | 848 | } |
AzureIoTClient | 6:73793bae15ba | 849 | else |
AzureIoTClient | 6:73793bae15ba | 850 | { |
AzureIoTClient | 6:73793bae15ba | 851 | if (STRING_concat_with_STRING(*payload, temp) != 0) |
AzureIoTClient | 6:73793bae15ba | 852 | { |
AzureIoTClient | 6:73793bae15ba | 853 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_073: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/ |
AzureIoTClient | 6:73793bae15ba | 854 | result = MAKE_PAYLOAD_ERROR; |
AzureIoTClient | 6:73793bae15ba | 855 | STRING_delete(*payload); |
AzureIoTClient | 6:73793bae15ba | 856 | *payload = NULL; |
AzureIoTClient | 6:73793bae15ba | 857 | keepGoing = false; |
AzureIoTClient | 6:73793bae15ba | 858 | } |
AzureIoTClient | 6:73793bae15ba | 859 | else |
AzureIoTClient | 6:73793bae15ba | 860 | { |
AzureIoTClient | 6:73793bae15ba | 861 | /*first item was put nicely in the payload*/ |
AzureIoTClient | 6:73793bae15ba | 862 | PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
AzureIoTClient | 6:73793bae15ba | 863 | DList_InsertTailList(&(handleData->eventConfirmations), head); |
AzureIoTClient | 6:73793bae15ba | 864 | allMessagesSize += messageSize; |
AzureIoTClient | 6:73793bae15ba | 865 | } |
AzureIoTClient | 6:73793bae15ba | 866 | } |
AzureIoTClient | 6:73793bae15ba | 867 | STRING_delete(temp); |
AzureIoTClient | 6:73793bae15ba | 868 | } |
AzureIoTClient | 6:73793bae15ba | 869 | } |
AzureIoTClient | 6:73793bae15ba | 870 | else |
AzureIoTClient | 6:73793bae15ba | 871 | { |
AzureIoTClient | 6:73793bae15ba | 872 | /*there is at least 1 item already in the payload*/ |
AzureIoTClient | 6:73793bae15ba | 873 | if (temp == NULL) |
AzureIoTClient | 6:73793bae15ba | 874 | { |
AzureIoTClient | 6:73793bae15ba | 875 | /*there are multiple payloads encoded, the last one had an internal error, just go with those - closing the payload happens "after the loop"*/ |
AzureIoTClient | 6:73793bae15ba | 876 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_072: [If at any point during construction of the string there are errors, IoTHubTransportHttp_DoWork shall use the so far constructed string as payload.]*/ |
AzureIoTClient | 6:73793bae15ba | 877 | result = MAKE_PAYLOAD_OK; |
AzureIoTClient | 6:73793bae15ba | 878 | keepGoing = false; |
AzureIoTClient | 6:73793bae15ba | 879 | } |
AzureIoTClient | 6:73793bae15ba | 880 | else |
AzureIoTClient | 6:73793bae15ba | 881 | { |
AzureIoTClient | 6:73793bae15ba | 882 | if (allMessagesSize + messageSize > MAXIMUM_MESSAGE_SIZE) |
AzureIoTClient | 6:73793bae15ba | 883 | { |
AzureIoTClient | 6:73793bae15ba | 884 | /*this item doesn't make it to the payload, but the payload is valid so far*/ |
AzureIoTClient | 6:73793bae15ba | 885 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_072: [If at any point during construction of the string there are errors, IoTHubTransportHttp_DoWork shall use the so far constructed string as payload.]*/ |
AzureIoTClient | 6:73793bae15ba | 886 | result = MAKE_PAYLOAD_OK; |
AzureIoTClient | 6:73793bae15ba | 887 | keepGoing = false; |
AzureIoTClient | 6:73793bae15ba | 888 | } |
AzureIoTClient | 6:73793bae15ba | 889 | else if (STRING_concat_with_STRING(*payload, temp) != 0) |
AzureIoTClient | 6:73793bae15ba | 890 | { |
AzureIoTClient | 6:73793bae15ba | 891 | /*should still send what there is so far...*/ |
AzureIoTClient | 6:73793bae15ba | 892 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_072: [If at any point during construction of the string there are errors, IoTHubTransportHttp_DoWork shall use the so far constructed string as payload.]*/ |
AzureIoTClient | 6:73793bae15ba | 893 | result = MAKE_PAYLOAD_OK; |
AzureIoTClient | 6:73793bae15ba | 894 | keepGoing = false; |
AzureIoTClient | 6:73793bae15ba | 895 | } |
AzureIoTClient | 6:73793bae15ba | 896 | else |
AzureIoTClient | 6:73793bae15ba | 897 | { |
AzureIoTClient | 6:73793bae15ba | 898 | /*cool, the payload made it there, let's continue... */ |
AzureIoTClient | 6:73793bae15ba | 899 | PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
AzureIoTClient | 6:73793bae15ba | 900 | DList_InsertTailList(&(handleData->eventConfirmations), head); |
AzureIoTClient | 6:73793bae15ba | 901 | allMessagesSize += messageSize; |
AzureIoTClient | 6:73793bae15ba | 902 | } |
AzureIoTClient | 6:73793bae15ba | 903 | STRING_delete(temp); |
AzureIoTClient | 6:73793bae15ba | 904 | } |
AzureIoTClient | 6:73793bae15ba | 905 | } |
AzureIoTClient | 6:73793bae15ba | 906 | } |
AzureIoTClient | 6:73793bae15ba | 907 | |
AzureIoTClient | 6:73793bae15ba | 908 | /*closing the payload*/ |
AzureIoTClient | 6:73793bae15ba | 909 | if (result == MAKE_PAYLOAD_OK) |
AzureIoTClient | 6:73793bae15ba | 910 | { |
AzureIoTClient | 6:73793bae15ba | 911 | ((char*)STRING_c_str(*payload))[STRING_length(*payload) - 1] = ']'; /*TODO - do this in STRING_HANDLE*/ |
AzureIoTClient | 6:73793bae15ba | 912 | } |
AzureIoTClient | 6:73793bae15ba | 913 | else |
AzureIoTClient | 6:73793bae15ba | 914 | { |
AzureIoTClient | 6:73793bae15ba | 915 | /*no need to close anything*/ |
AzureIoTClient | 6:73793bae15ba | 916 | } |
AzureIoTClient | 6:73793bae15ba | 917 | } |
AzureIoTClient | 6:73793bae15ba | 918 | return result; |
AzureIoTClient | 6:73793bae15ba | 919 | } |
AzureIoTClient | 6:73793bae15ba | 920 | |
AzureIoTClient | 6:73793bae15ba | 921 | static void reversePutListBackIn(PDLIST_ENTRY source, PDLIST_ENTRY destination) |
AzureIoTClient | 6:73793bae15ba | 922 | { |
AzureIoTClient | 6:73793bae15ba | 923 | /*this function takes a list, and inserts it in another list. When done in the context of this file, it reverses the effects of a not-able-to-send situation*/ |
AzureIoTClient | 6:73793bae15ba | 924 | DList_AppendTailList(destination->Flink, source); |
AzureIoTClient | 6:73793bae15ba | 925 | DList_RemoveEntryList(source); |
AzureIoTClient | 6:73793bae15ba | 926 | DList_InitializeListHead(source); |
AzureIoTClient | 6:73793bae15ba | 927 | } |
AzureIoTClient | 6:73793bae15ba | 928 | |
AzureIoTClient | 6:73793bae15ba | 929 | static void DoEvent(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 6:73793bae15ba | 930 | { |
AzureIoTClient | 6:73793bae15ba | 931 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 6:73793bae15ba | 932 | if (DList_IsListEmpty(handleData->waitingToSend)) |
AzureIoTClient | 6:73793bae15ba | 933 | { |
AzureIoTClient | 6:73793bae15ba | 934 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_019: [If the list is empty then IoTHubTransportHttp_DoWork shall proceed to the following action.] */ |
AzureIoTClient | 6:73793bae15ba | 935 | } |
AzureIoTClient | 6:73793bae15ba | 936 | else |
AzureIoTClient | 6:73793bae15ba | 937 | { |
AzureIoTClient | 6:73793bae15ba | 938 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_101: [If option SetBatching is true then _Dowork shall send batched event message as specced below.] */ |
AzureIoTClient | 6:73793bae15ba | 939 | if (handleData->doBatchedTransfers) |
AzureIoTClient | 6:73793bae15ba | 940 | { |
AzureIoTClient | 6:73793bae15ba | 941 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_102: [Request HTTP headers shall have the value of "Content-Type" created or updated to "application/vnd.microsoft.iothub.json" by a call to HTTPHeaders_ReplaceHeaderNameValuePair.] */ |
AzureIoTClient | 6:73793bae15ba | 942 | if (HTTPHeaders_ReplaceHeaderNameValuePair(handleData->eventHTTPrequestHeaders, CONTENT_TYPE, APPLICATION_VND_MICROSOFT_IOTHUB_JSON) != HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 943 | { |
AzureIoTClient | 6:73793bae15ba | 944 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_103: [If updating Content-Type fails for any reason, then _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 945 | LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair\r\n"); |
AzureIoTClient | 6:73793bae15ba | 946 | } |
AzureIoTClient | 6:73793bae15ba | 947 | else |
AzureIoTClient | 6:73793bae15ba | 948 | { |
AzureIoTClient | 6:73793bae15ba | 949 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_018: [It shall inspect the "waitingToSend" DLIST passed in config structure.] */ |
AzureIoTClient | 6:73793bae15ba | 950 | STRING_HANDLE payload; |
AzureIoTClient | 6:73793bae15ba | 951 | switch (makePayload(handleData, &payload)) |
AzureIoTClient | 6:73793bae15ba | 952 | { |
AzureIoTClient | 6:73793bae15ba | 953 | case MAKE_PAYLOAD_OK: |
AzureIoTClient | 6:73793bae15ba | 954 | { |
AzureIoTClient | 6:73793bae15ba | 955 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_031: [Once a final payload has been obtained, IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters:] */ |
AzureIoTClient | 6:73793bae15ba | 956 | BUFFER_HANDLE temp = BUFFER_new(); |
AzureIoTClient | 6:73793bae15ba | 957 | if (temp == NULL) |
AzureIoTClient | 6:73793bae15ba | 958 | { |
AzureIoTClient | 6:73793bae15ba | 959 | LogError("unable to BUFFER_new\r\n"); |
AzureIoTClient | 6:73793bae15ba | 960 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_073: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/ |
AzureIoTClient | 6:73793bae15ba | 961 | reversePutListBackIn(&(handleData->eventConfirmations), handleData->waitingToSend); |
AzureIoTClient | 6:73793bae15ba | 962 | } |
AzureIoTClient | 6:73793bae15ba | 963 | else |
AzureIoTClient | 6:73793bae15ba | 964 | { |
AzureIoTClient | 6:73793bae15ba | 965 | if (BUFFER_build(temp, (const unsigned char*)STRING_c_str(payload), STRING_length(payload)) != 0) |
AzureIoTClient | 6:73793bae15ba | 966 | { |
AzureIoTClient | 6:73793bae15ba | 967 | LogError("unable to BUFFER_build\r\n"); |
AzureIoTClient | 6:73793bae15ba | 968 | //items go back to waitingToSend |
AzureIoTClient | 6:73793bae15ba | 969 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_073: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/ |
AzureIoTClient | 6:73793bae15ba | 970 | reversePutListBackIn(&(handleData->eventConfirmations), handleData->waitingToSend); |
AzureIoTClient | 6:73793bae15ba | 971 | } |
AzureIoTClient | 6:73793bae15ba | 972 | else |
AzureIoTClient | 6:73793bae15ba | 973 | { |
AzureIoTClient | 6:73793bae15ba | 974 | unsigned int statusCode; |
AzureIoTClient | 6:73793bae15ba | 975 | HTTPAPIEX_RESULT r; |
AzureIoTClient | 6:73793bae15ba | 976 | if ((r = HTTPAPIEX_SAS_ExecuteRequest( |
AzureIoTClient | 6:73793bae15ba | 977 | handleData->sasObject, |
AzureIoTClient | 6:73793bae15ba | 978 | handleData->httpApiExHandle, |
AzureIoTClient | 6:73793bae15ba | 979 | HTTPAPI_REQUEST_POST, |
AzureIoTClient | 6:73793bae15ba | 980 | STRING_c_str(handleData->eventHTTPrelativePath), |
AzureIoTClient | 6:73793bae15ba | 981 | handleData->eventHTTPrequestHeaders, |
AzureIoTClient | 6:73793bae15ba | 982 | temp, |
AzureIoTClient | 6:73793bae15ba | 983 | &statusCode, |
AzureIoTClient | 6:73793bae15ba | 984 | NULL, |
AzureIoTClient | 6:73793bae15ba | 985 | NULL |
AzureIoTClient | 6:73793bae15ba | 986 | )) != HTTPAPIEX_OK) |
AzureIoTClient | 6:73793bae15ba | 987 | { |
AzureIoTClient | 6:73793bae15ba | 988 | LogError("unable to HTTPAPIEX_ExecuteRequest\r\n"); |
AzureIoTClient | 6:73793bae15ba | 989 | //items go back to waitingToSend |
AzureIoTClient | 6:73793bae15ba | 990 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_065: [if HTTPAPIEX_SAS_ExecuteRequest fails or the http status code >=300 then IoTHubTransportHttp_DoWork shall not do any other action (it is assumed at the next _DoWork it shall be retried).] */ |
AzureIoTClient | 6:73793bae15ba | 991 | reversePutListBackIn(&(handleData->eventConfirmations), handleData->waitingToSend); |
AzureIoTClient | 6:73793bae15ba | 992 | } |
AzureIoTClient | 6:73793bae15ba | 993 | else |
AzureIoTClient | 6:73793bae15ba | 994 | { |
AzureIoTClient | 6:73793bae15ba | 995 | if (statusCode < 300) |
AzureIoTClient | 6:73793bae15ba | 996 | { |
AzureIoTClient | 6:73793bae15ba | 997 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_067: [If HTTPAPIEX_SAS_ExecuteRequest does not fail and http status code <300 then IoTHubTransportHttp_DoWork shall call IoTHubClient_LL_SendComplete. Parameter PDLIST_ENTRY completed shall point to a list containing all the items batched, and parameter IOTHUB_BATCHSTATE result shall be set to IOTHUB_BATCHSTATE_SUCESS. The batched items shall be removed from waitingToSend.] */ |
AzureIoTClient | 6:73793bae15ba | 998 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_SUCCESS); |
AzureIoTClient | 6:73793bae15ba | 999 | } |
AzureIoTClient | 6:73793bae15ba | 1000 | else |
AzureIoTClient | 6:73793bae15ba | 1001 | { |
AzureIoTClient | 6:73793bae15ba | 1002 | //items go back to waitingToSend |
AzureIoTClient | 6:73793bae15ba | 1003 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_065: [if HTTPAPIEX_SAS_ExecuteRequest fails or the http status code >=300 then IoTHubTransportHttp_DoWork shall not do any other action (it is assumed at the next _DoWork it shall be retried).] */ |
AzureIoTClient | 6:73793bae15ba | 1004 | LogError("unexpected HTTP status code (%u)\r\n", statusCode); |
AzureIoTClient | 6:73793bae15ba | 1005 | reversePutListBackIn(&(handleData->eventConfirmations), handleData->waitingToSend); |
AzureIoTClient | 6:73793bae15ba | 1006 | } |
AzureIoTClient | 6:73793bae15ba | 1007 | } |
AzureIoTClient | 6:73793bae15ba | 1008 | } |
AzureIoTClient | 6:73793bae15ba | 1009 | BUFFER_delete(temp); |
AzureIoTClient | 6:73793bae15ba | 1010 | } |
AzureIoTClient | 6:73793bae15ba | 1011 | STRING_delete(payload); |
AzureIoTClient | 6:73793bae15ba | 1012 | break; |
AzureIoTClient | 6:73793bae15ba | 1013 | } |
AzureIoTClient | 6:73793bae15ba | 1014 | case MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT: |
AzureIoTClient | 6:73793bae15ba | 1015 | { |
AzureIoTClient | 6:73793bae15ba | 1016 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_FAILED); /*takes care of emptying the list too*/ |
AzureIoTClient | 6:73793bae15ba | 1017 | break; |
AzureIoTClient | 6:73793bae15ba | 1018 | } |
AzureIoTClient | 6:73793bae15ba | 1019 | case MAKE_PAYLOAD_ERROR: |
AzureIoTClient | 6:73793bae15ba | 1020 | { |
AzureIoTClient | 6:73793bae15ba | 1021 | LogError("unrecoverable errors while building a batch message\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1022 | break; |
AzureIoTClient | 6:73793bae15ba | 1023 | } |
AzureIoTClient | 6:73793bae15ba | 1024 | case MAKE_PAYLOAD_NO_ITEMS: |
AzureIoTClient | 6:73793bae15ba | 1025 | { |
AzureIoTClient | 6:73793bae15ba | 1026 | /*do nothing*/ |
AzureIoTClient | 6:73793bae15ba | 1027 | break; |
AzureIoTClient | 6:73793bae15ba | 1028 | } |
AzureIoTClient | 6:73793bae15ba | 1029 | default: |
AzureIoTClient | 6:73793bae15ba | 1030 | { |
AzureIoTClient | 6:73793bae15ba | 1031 | LogError("internal error: switch's default branch reached when never intended\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1032 | break; |
AzureIoTClient | 6:73793bae15ba | 1033 | } |
AzureIoTClient | 6:73793bae15ba | 1034 | } |
AzureIoTClient | 6:73793bae15ba | 1035 | } |
AzureIoTClient | 6:73793bae15ba | 1036 | } |
AzureIoTClient | 6:73793bae15ba | 1037 | else |
AzureIoTClient | 6:73793bae15ba | 1038 | { |
AzureIoTClient | 6:73793bae15ba | 1039 | const unsigned char* messageContent=NULL; |
AzureIoTClient | 6:73793bae15ba | 1040 | size_t messageSize=0; |
AzureIoTClient | 6:73793bae15ba | 1041 | size_t originalMessageSize=0; |
AzureIoTClient | 6:73793bae15ba | 1042 | IOTHUB_MESSAGE_LIST* message = containingRecord(handleData->waitingToSend->Flink, IOTHUB_MESSAGE_LIST, entry); |
AzureIoTClient | 6:73793bae15ba | 1043 | IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message->messageHandle); |
AzureIoTClient | 6:73793bae15ba | 1044 | |
AzureIoTClient | 6:73793bae15ba | 1045 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_122: [The message size is computed from the length of the payload + 384.]*/ |
AzureIoTClient | 6:73793bae15ba | 1046 | if (!( |
AzureIoTClient | 6:73793bae15ba | 1047 | (((contentType == IOTHUBMESSAGE_BYTEARRAY) && |
AzureIoTClient | 6:73793bae15ba | 1048 | (IoTHubMessage_GetByteArray(message->messageHandle, &messageContent, &originalMessageSize)==IOTHUB_MESSAGE_OK)) ? (messageSize= originalMessageSize + MAXIMUM_PAYLOAD_OVERHEAD, 1): 0) |
AzureIoTClient | 6:73793bae15ba | 1049 | |
AzureIoTClient | 6:73793bae15ba | 1050 | || |
AzureIoTClient | 6:73793bae15ba | 1051 | |
AzureIoTClient | 6:73793bae15ba | 1052 | ((contentType == IOTHUBMESSAGE_STRING) && ( |
AzureIoTClient | 6:73793bae15ba | 1053 | messageContent = (const unsigned char*)IoTHubMessage_GetString(message->messageHandle), |
AzureIoTClient | 6:73793bae15ba | 1054 | (messageSize = MAXIMUM_PAYLOAD_OVERHEAD + (originalMessageSize = ((messageContent == NULL)?0:strlen((const char*)messageContent)))), |
AzureIoTClient | 6:73793bae15ba | 1055 | messageContent!=NULL) |
AzureIoTClient | 6:73793bae15ba | 1056 | ) |
AzureIoTClient | 6:73793bae15ba | 1057 | )) |
AzureIoTClient | 6:73793bae15ba | 1058 | { |
AzureIoTClient | 6:73793bae15ba | 1059 | LogError("unable to get the message content\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1060 | /*go on...*/ |
AzureIoTClient | 6:73793bae15ba | 1061 | } |
AzureIoTClient | 6:73793bae15ba | 1062 | else |
AzureIoTClient | 6:73793bae15ba | 1063 | { |
AzureIoTClient | 6:73793bae15ba | 1064 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_109: [If the oldest message in waitingToSend causes the message to exceed the message size limit then it shall be removed from waitingToSend, and IoTHubClient_LL_SendComplete shall be called. Parameter PDLIST_ENTRY completed shall point to a list containing only the oldest item, and parameter IOTHUB_BATCHSTATE result shall be set to IOTHUB_BATCHSTATE_FAILED.]*/ |
AzureIoTClient | 6:73793bae15ba | 1065 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_121: [The message size shall be limited to 255KB -1 bytes.] */ |
AzureIoTClient | 6:73793bae15ba | 1066 | if (messageSize > MAXIMUM_MESSAGE_SIZE) |
AzureIoTClient | 6:73793bae15ba | 1067 | { |
AzureIoTClient | 6:73793bae15ba | 1068 | PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
AzureIoTClient | 6:73793bae15ba | 1069 | DList_InsertTailList(&(handleData->eventConfirmations), head); |
AzureIoTClient | 6:73793bae15ba | 1070 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_FAILED); /*takes care of emptying the list too*/ |
AzureIoTClient | 6:73793bae15ba | 1071 | } |
AzureIoTClient | 6:73793bae15ba | 1072 | else |
AzureIoTClient | 6:73793bae15ba | 1073 | { |
AzureIoTClient | 6:73793bae15ba | 1074 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_104: [If option SetBatching is false then _Dowork shall send individual event message as specced below.] */ |
AzureIoTClient | 6:73793bae15ba | 1075 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_105: [A clone of the event HTTP request headers shall be created.]*/ |
AzureIoTClient | 6:73793bae15ba | 1076 | HTTP_HEADERS_HANDLE clonedEventHTTPrequestHeaders = HTTPHeaders_Clone(handleData->eventHTTPrequestHeaders); |
AzureIoTClient | 6:73793bae15ba | 1077 | if (clonedEventHTTPrequestHeaders == NULL) |
AzureIoTClient | 6:73793bae15ba | 1078 | { |
AzureIoTClient | 6:73793bae15ba | 1079 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1080 | LogError("HTTPHeaders_Clone failed\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1081 | } |
AzureIoTClient | 6:73793bae15ba | 1082 | else |
AzureIoTClient | 6:73793bae15ba | 1083 | { |
AzureIoTClient | 6:73793bae15ba | 1084 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_106: [The cloned HTTP headers shall have the HTTP header "Content-Type" set to "application/octet-stream".] */ |
AzureIoTClient | 6:73793bae15ba | 1085 | if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, CONTENT_TYPE, APPLICATION_OCTET_STREAM) != HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 1086 | { |
AzureIoTClient | 6:73793bae15ba | 1087 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1088 | LogError("HTTPHeaders_ReplaceHeaderNameValuePair failed\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1089 | } |
AzureIoTClient | 6:73793bae15ba | 1090 | else |
AzureIoTClient | 6:73793bae15ba | 1091 | { |
AzureIoTClient | 6:73793bae15ba | 1092 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_107: [Every message property "property":"value" shall be added to the HTTP headers as an individual header "iothub-app-property":"value".] */ |
AzureIoTClient | 6:73793bae15ba | 1093 | MAP_HANDLE map = IoTHubMessage_Properties(message->messageHandle); |
AzureIoTClient | 6:73793bae15ba | 1094 | const char*const* keys; |
AzureIoTClient | 6:73793bae15ba | 1095 | const char*const* values; |
AzureIoTClient | 6:73793bae15ba | 1096 | size_t count; |
AzureIoTClient | 6:73793bae15ba | 1097 | if (Map_GetInternals(map, &keys, &values, &count) != MAP_OK) |
AzureIoTClient | 6:73793bae15ba | 1098 | { |
AzureIoTClient | 6:73793bae15ba | 1099 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1100 | LogError("unable to Map_GetInternals\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1101 | } |
AzureIoTClient | 6:73793bae15ba | 1102 | else |
AzureIoTClient | 6:73793bae15ba | 1103 | { |
AzureIoTClient | 6:73793bae15ba | 1104 | size_t i; |
AzureIoTClient | 6:73793bae15ba | 1105 | bool goOn = true; |
AzureIoTClient | 6:73793bae15ba | 1106 | for (i = 0; (i < count) && goOn; i++) |
AzureIoTClient | 6:73793bae15ba | 1107 | { |
AzureIoTClient | 6:73793bae15ba | 1108 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_123: [Every property name shall add to the message size the length of the property name + the length of the property value + 16 bytes.] */ |
AzureIoTClient | 6:73793bae15ba | 1109 | messageSize += (strlen(values[i]) + strlen(keys[i]) + MAXIMUM_PROPERTY_OVERHEAD); |
AzureIoTClient | 6:73793bae15ba | 1110 | if (messageSize > MAXIMUM_MESSAGE_SIZE) |
AzureIoTClient | 6:73793bae15ba | 1111 | { |
AzureIoTClient | 6:73793bae15ba | 1112 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_121: [The message size shall be limited to 255KB -1 bytes.] */ |
AzureIoTClient | 6:73793bae15ba | 1113 | PDLIST_ENTRY head = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
AzureIoTClient | 6:73793bae15ba | 1114 | DList_InsertTailList(&(handleData->eventConfirmations), head); |
AzureIoTClient | 6:73793bae15ba | 1115 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_FAILED); /*takes care of emptying the list too*/ |
AzureIoTClient | 6:73793bae15ba | 1116 | goOn = false; |
AzureIoTClient | 6:73793bae15ba | 1117 | } |
AzureIoTClient | 6:73793bae15ba | 1118 | else |
AzureIoTClient | 6:73793bae15ba | 1119 | { |
AzureIoTClient | 6:73793bae15ba | 1120 | STRING_HANDLE temp = STRING_construct(IOTHUB_APP_PREFIX); |
AzureIoTClient | 6:73793bae15ba | 1121 | if (temp == NULL) |
AzureIoTClient | 6:73793bae15ba | 1122 | { |
AzureIoTClient | 6:73793bae15ba | 1123 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1124 | LogError("unable to STRING_construct\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1125 | goOn = false; |
AzureIoTClient | 6:73793bae15ba | 1126 | } |
AzureIoTClient | 6:73793bae15ba | 1127 | else |
AzureIoTClient | 6:73793bae15ba | 1128 | { |
AzureIoTClient | 6:73793bae15ba | 1129 | if (STRING_concat(temp, keys[i]) != 0) |
AzureIoTClient | 6:73793bae15ba | 1130 | { |
AzureIoTClient | 6:73793bae15ba | 1131 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1132 | LogError("unable to STRING_concat\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1133 | goOn = false; |
AzureIoTClient | 6:73793bae15ba | 1134 | } |
AzureIoTClient | 6:73793bae15ba | 1135 | else |
AzureIoTClient | 6:73793bae15ba | 1136 | { |
AzureIoTClient | 6:73793bae15ba | 1137 | if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, STRING_c_str(temp), values[i]) != HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 1138 | { |
AzureIoTClient | 6:73793bae15ba | 1139 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1140 | LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1141 | goOn = false; |
AzureIoTClient | 6:73793bae15ba | 1142 | } |
AzureIoTClient | 6:73793bae15ba | 1143 | } |
AzureIoTClient | 6:73793bae15ba | 1144 | STRING_delete(temp); |
AzureIoTClient | 6:73793bae15ba | 1145 | } |
AzureIoTClient | 6:73793bae15ba | 1146 | } |
AzureIoTClient | 6:73793bae15ba | 1147 | } |
AzureIoTClient | 6:73793bae15ba | 1148 | |
AzureIoTClient | 6:73793bae15ba | 1149 | if (!goOn) |
AzureIoTClient | 6:73793bae15ba | 1150 | { |
AzureIoTClient | 6:73793bae15ba | 1151 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_108: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1152 | } |
AzureIoTClient | 6:73793bae15ba | 1153 | else |
AzureIoTClient | 6:73793bae15ba | 1154 | { |
AzureIoTClient | 6:73793bae15ba | 1155 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_110: [IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters] */ |
AzureIoTClient | 6:73793bae15ba | 1156 | BUFFER_HANDLE toBeSend = BUFFER_new(); |
AzureIoTClient | 6:73793bae15ba | 1157 | if (toBeSend == NULL) |
AzureIoTClient | 6:73793bae15ba | 1158 | { |
AzureIoTClient | 6:73793bae15ba | 1159 | LogError("unable to BUFFER_new\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1160 | } |
AzureIoTClient | 6:73793bae15ba | 1161 | else |
AzureIoTClient | 6:73793bae15ba | 1162 | { |
AzureIoTClient | 6:73793bae15ba | 1163 | if (BUFFER_build(toBeSend, messageContent, originalMessageSize) != 0) |
AzureIoTClient | 6:73793bae15ba | 1164 | { |
AzureIoTClient | 6:73793bae15ba | 1165 | LogError("unable to BUFFER_build\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1166 | } |
AzureIoTClient | 6:73793bae15ba | 1167 | else |
AzureIoTClient | 6:73793bae15ba | 1168 | { |
AzureIoTClient | 6:73793bae15ba | 1169 | unsigned int statusCode; |
AzureIoTClient | 6:73793bae15ba | 1170 | HTTPAPIEX_RESULT r; |
AzureIoTClient | 6:73793bae15ba | 1171 | if ((r = HTTPAPIEX_SAS_ExecuteRequest( |
AzureIoTClient | 6:73793bae15ba | 1172 | handleData->sasObject, |
AzureIoTClient | 6:73793bae15ba | 1173 | handleData->httpApiExHandle, |
AzureIoTClient | 6:73793bae15ba | 1174 | HTTPAPI_REQUEST_POST, |
AzureIoTClient | 6:73793bae15ba | 1175 | STRING_c_str(handleData->eventHTTPrelativePath), |
AzureIoTClient | 6:73793bae15ba | 1176 | clonedEventHTTPrequestHeaders, |
AzureIoTClient | 6:73793bae15ba | 1177 | toBeSend, |
AzureIoTClient | 6:73793bae15ba | 1178 | &statusCode, |
AzureIoTClient | 6:73793bae15ba | 1179 | NULL, |
AzureIoTClient | 6:73793bae15ba | 1180 | NULL |
AzureIoTClient | 6:73793bae15ba | 1181 | )) != HTTPAPIEX_OK) |
AzureIoTClient | 6:73793bae15ba | 1182 | { |
AzureIoTClient | 6:73793bae15ba | 1183 | LogError("unable to HTTPAPIEX_ExecuteRequest\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1184 | } |
AzureIoTClient | 6:73793bae15ba | 1185 | else |
AzureIoTClient | 6:73793bae15ba | 1186 | { |
AzureIoTClient | 6:73793bae15ba | 1187 | if (statusCode < 300) |
AzureIoTClient | 6:73793bae15ba | 1188 | { |
AzureIoTClient | 6:73793bae15ba | 1189 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_112: [If HTTPAPIEX_SAS_ExecuteRequest does not fail and http status code <300 then IoTHubTransportHttp_DoWork shall call IoTHubClient_LL_SendComplete. Parameter PDLIST_ENTRY completed shall point to a list the item send, and parameter IOTHUB_BATCHSTATE result shall be set to IOTHUB_BATCHSTATE_SUCCESS. The item shall be removed from waitingToSend.] */ |
AzureIoTClient | 6:73793bae15ba | 1190 | PDLIST_ENTRY justSent = DList_RemoveHeadList(handleData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
AzureIoTClient | 6:73793bae15ba | 1191 | DList_InsertTailList(&(handleData->eventConfirmations), justSent); |
AzureIoTClient | 6:73793bae15ba | 1192 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(handleData->eventConfirmations), IOTHUB_BATCHSTATE_SUCCESS); /*takes care of emptying the list too*/ |
AzureIoTClient | 6:73793bae15ba | 1193 | } |
AzureIoTClient | 6:73793bae15ba | 1194 | else |
AzureIoTClient | 6:73793bae15ba | 1195 | { |
AzureIoTClient | 6:73793bae15ba | 1196 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_111: [If HTTPAPIEX_SAS_ExecuteRequest fails or the http status code >=300 then IoTHubTransportHttp_DoWork shall not do any other action (it is assumed at the next _DoWork it shall be retried).] */ |
AzureIoTClient | 6:73793bae15ba | 1197 | LogError("unexpected HTTP status code (%u)\r\n", statusCode); |
AzureIoTClient | 6:73793bae15ba | 1198 | } |
AzureIoTClient | 6:73793bae15ba | 1199 | } |
AzureIoTClient | 6:73793bae15ba | 1200 | } |
AzureIoTClient | 6:73793bae15ba | 1201 | BUFFER_delete(toBeSend); |
AzureIoTClient | 6:73793bae15ba | 1202 | } |
AzureIoTClient | 6:73793bae15ba | 1203 | } |
AzureIoTClient | 6:73793bae15ba | 1204 | } |
AzureIoTClient | 6:73793bae15ba | 1205 | } |
AzureIoTClient | 6:73793bae15ba | 1206 | HTTPHeaders_Free(clonedEventHTTPrequestHeaders); |
AzureIoTClient | 6:73793bae15ba | 1207 | } |
AzureIoTClient | 6:73793bae15ba | 1208 | } |
AzureIoTClient | 6:73793bae15ba | 1209 | } |
AzureIoTClient | 6:73793bae15ba | 1210 | } |
AzureIoTClient | 6:73793bae15ba | 1211 | } |
AzureIoTClient | 6:73793bae15ba | 1212 | } |
AzureIoTClient | 6:73793bae15ba | 1213 | |
AzureIoTClient | 6:73793bae15ba | 1214 | #define ACTION_VALUES \ |
AzureIoTClient | 6:73793bae15ba | 1215 | ABANDON, \ |
AzureIoTClient | 6:73793bae15ba | 1216 | REJECT, \ |
AzureIoTClient | 6:73793bae15ba | 1217 | ACCEPT |
AzureIoTClient | 6:73793bae15ba | 1218 | DEFINE_ENUM(ACTION, ACTION_VALUES); |
AzureIoTClient | 6:73793bae15ba | 1219 | |
AzureIoTClient | 6:73793bae15ba | 1220 | static void abandonOrAcceptMessage(HTTPTRANSPORT_HANDLE_DATA* handleData, const char* ETag, ACTION action) |
AzureIoTClient | 6:73793bae15ba | 1221 | { |
AzureIoTClient | 6:73793bae15ba | 1222 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_050: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters: |
AzureIoTClient | 6:73793bae15ba | 1223 | -requestType: POST |
AzureIoTClient | 6:73793bae15ba | 1224 | -relativePath: abandon relative path begin (as created by _Create) + value of ETag + "/abandon?api-version=2015-08-15-preview" |
AzureIoTClient | 6:73793bae15ba | 1225 | - requestHttpHeadersHandle: an HTTP headers instance containing the following |
AzureIoTClient | 6:73793bae15ba | 1226 | Authorization: " " |
AzureIoTClient | 6:73793bae15ba | 1227 | If-Match: value of ETag |
AzureIoTClient | 6:73793bae15ba | 1228 | - requestContent: NULL |
AzureIoTClient | 6:73793bae15ba | 1229 | - statusCode: a pointer to unsigned int which might be examined for logging |
AzureIoTClient | 6:73793bae15ba | 1230 | - responseHeadearsHandle: NULL |
AzureIoTClient | 6:73793bae15ba | 1231 | - responseContent: NULL]*/ |
AzureIoTClient | 6:73793bae15ba | 1232 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_051: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters: |
AzureIoTClient | 6:73793bae15ba | 1233 | -requestType: DELETE |
AzureIoTClient | 6:73793bae15ba | 1234 | -relativePath: abandon relative path begin + value of ETag + "?api-version=2015-08-15-preview" |
AzureIoTClient | 6:73793bae15ba | 1235 | - requestHttpHeadersHandle: an HTTP headers instance containing the following |
AzureIoTClient | 6:73793bae15ba | 1236 | Authorization: " " |
AzureIoTClient | 6:73793bae15ba | 1237 | If-Match: value of ETag |
AzureIoTClient | 6:73793bae15ba | 1238 | - requestContent: NULL |
AzureIoTClient | 6:73793bae15ba | 1239 | - statusCode: a pointer to unsigned int which might be used by logging |
AzureIoTClient | 6:73793bae15ba | 1240 | - responseHeadearsHandle: NULL |
AzureIoTClient | 6:73793bae15ba | 1241 | - responseContent: NULL]*/ |
AzureIoTClient | 6:73793bae15ba | 1242 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_077: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters: |
AzureIoTClient | 6:73793bae15ba | 1243 | -requestType: DELETE |
AzureIoTClient | 6:73793bae15ba | 1244 | -relativePath: abandon relative path begin + value of ETag +"?api-version=2015-08-15-preview" + "&reject" |
AzureIoTClient | 6:73793bae15ba | 1245 | - requestHttpHeadersHandle: an HTTP headers instance containing the following |
AzureIoTClient | 6:73793bae15ba | 1246 | Authorization: " " |
AzureIoTClient | 6:73793bae15ba | 1247 | If-Match: value of ETag |
AzureIoTClient | 6:73793bae15ba | 1248 | - requestContent: NULL |
AzureIoTClient | 6:73793bae15ba | 1249 | - statusCode: a pointer to unsigned int which might be used by logging |
AzureIoTClient | 6:73793bae15ba | 1250 | - responseHeadearsHandle: NULL |
AzureIoTClient | 6:73793bae15ba | 1251 | - responseContent: NULL]*/ |
AzureIoTClient | 6:73793bae15ba | 1252 | |
AzureIoTClient | 6:73793bae15ba | 1253 | STRING_HANDLE fullAbandonRelativePath = STRING_clone(handleData->abandonHTTPrelativePathBegin); |
AzureIoTClient | 6:73793bae15ba | 1254 | if (fullAbandonRelativePath == NULL) |
AzureIoTClient | 6:73793bae15ba | 1255 | { |
AzureIoTClient | 6:73793bae15ba | 1256 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1257 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1258 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1259 | LogError("unable to STRING_clone\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1260 | } |
AzureIoTClient | 6:73793bae15ba | 1261 | else |
AzureIoTClient | 6:73793bae15ba | 1262 | { |
AzureIoTClient | 6:73793bae15ba | 1263 | STRING_HANDLE ETagUnquoted = STRING_construct_n(ETag + 1, strlen(ETag) - 2); /*skip first character which is '"' and the last one (which is also '"')*/ |
AzureIoTClient | 6:73793bae15ba | 1264 | if (ETagUnquoted == NULL) |
AzureIoTClient | 6:73793bae15ba | 1265 | { |
AzureIoTClient | 6:73793bae15ba | 1266 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1267 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1268 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1269 | LogError("unable to STRING_construct_n\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1270 | } |
AzureIoTClient | 6:73793bae15ba | 1271 | else |
AzureIoTClient | 6:73793bae15ba | 1272 | { |
AzureIoTClient | 6:73793bae15ba | 1273 | if (!( |
AzureIoTClient | 6:73793bae15ba | 1274 | (STRING_concat_with_STRING(fullAbandonRelativePath, ETagUnquoted) == 0) && |
AzureIoTClient | 6:73793bae15ba | 1275 | (STRING_concat(fullAbandonRelativePath, (action == ABANDON) ? "/abandon" API_VERSION : ((action == REJECT) ? API_VERSION "&reject" : API_VERSION)) == 0) |
AzureIoTClient | 6:73793bae15ba | 1276 | )) |
AzureIoTClient | 6:73793bae15ba | 1277 | { |
AzureIoTClient | 6:73793bae15ba | 1278 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1279 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1280 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1281 | LogError("unable to STRING_concat\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1282 | } |
AzureIoTClient | 6:73793bae15ba | 1283 | else |
AzureIoTClient | 6:73793bae15ba | 1284 | { |
AzureIoTClient | 6:73793bae15ba | 1285 | HTTP_HEADERS_HANDLE abandonRequestHttpHeaders = HTTPHeaders_Alloc(); |
AzureIoTClient | 6:73793bae15ba | 1286 | if (abandonRequestHttpHeaders == NULL) |
AzureIoTClient | 6:73793bae15ba | 1287 | { |
AzureIoTClient | 6:73793bae15ba | 1288 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1289 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1290 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1291 | LogError("unable to HTTPHeaders_Alloc\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1292 | } |
AzureIoTClient | 6:73793bae15ba | 1293 | else |
AzureIoTClient | 6:73793bae15ba | 1294 | { |
AzureIoTClient | 6:73793bae15ba | 1295 | if (!( |
AzureIoTClient | 6:73793bae15ba | 1296 | (HTTPHeaders_AddHeaderNameValuePair(abandonRequestHttpHeaders, "Authorization", " ") == HTTP_HEADERS_OK) && |
AzureIoTClient | 6:73793bae15ba | 1297 | (HTTPHeaders_AddHeaderNameValuePair(abandonRequestHttpHeaders, "If-Match", ETag) == HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 1298 | )) |
AzureIoTClient | 6:73793bae15ba | 1299 | { |
AzureIoTClient | 6:73793bae15ba | 1300 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1301 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1302 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1303 | LogError("unable to HTTPHeaders_AddHeaderNameValuePair\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1304 | } |
AzureIoTClient | 6:73793bae15ba | 1305 | else |
AzureIoTClient | 6:73793bae15ba | 1306 | { |
AzureIoTClient | 6:73793bae15ba | 1307 | unsigned int statusCode; |
AzureIoTClient | 6:73793bae15ba | 1308 | if (HTTPAPIEX_SAS_ExecuteRequest( |
AzureIoTClient | 6:73793bae15ba | 1309 | handleData->sasObject, |
AzureIoTClient | 6:73793bae15ba | 1310 | handleData->httpApiExHandle, |
AzureIoTClient | 6:73793bae15ba | 1311 | (action == ABANDON) ? HTTPAPI_REQUEST_POST : HTTPAPI_REQUEST_DELETE, /*-requestType: POST */ |
AzureIoTClient | 6:73793bae15ba | 1312 | STRING_c_str(fullAbandonRelativePath), /*-relativePath: abandon relative path begin (as created by _Create) + value of ETag + "/abandon?api-version=2015-08-15-preview" */ |
AzureIoTClient | 6:73793bae15ba | 1313 | abandonRequestHttpHeaders, /*- requestHttpHeadersHandle: an HTTP headers instance containing the following */ |
AzureIoTClient | 6:73793bae15ba | 1314 | NULL, /*- requestContent: NULL */ |
AzureIoTClient | 6:73793bae15ba | 1315 | &statusCode, /*- statusCode: a pointer to unsigned int which might be examined for logging */ |
AzureIoTClient | 6:73793bae15ba | 1316 | NULL, /*- responseHeadearsHandle: NULL */ |
AzureIoTClient | 6:73793bae15ba | 1317 | NULL /*- responseContent: NULL] */ |
AzureIoTClient | 6:73793bae15ba | 1318 | ) != HTTPAPIEX_OK) |
AzureIoTClient | 6:73793bae15ba | 1319 | { |
AzureIoTClient | 6:73793bae15ba | 1320 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1321 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1322 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1323 | LogError("unable to HTTPAPIEX_ExecuteRequest\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1324 | } |
AzureIoTClient | 6:73793bae15ba | 1325 | else |
AzureIoTClient | 6:73793bae15ba | 1326 | { |
AzureIoTClient | 6:73793bae15ba | 1327 | if (statusCode != 204) |
AzureIoTClient | 6:73793bae15ba | 1328 | { |
AzureIoTClient | 6:73793bae15ba | 1329 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1330 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1331 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1332 | LogError("unexpected status code returned %u (was expecting 204)\r\n", statusCode); |
AzureIoTClient | 6:73793bae15ba | 1333 | } |
AzureIoTClient | 6:73793bae15ba | 1334 | else |
AzureIoTClient | 6:73793bae15ba | 1335 | { |
AzureIoTClient | 6:73793bae15ba | 1336 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_052: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
AzureIoTClient | 6:73793bae15ba | 1337 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_054: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1338 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_078: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
AzureIoTClient | 6:73793bae15ba | 1339 | /*all is fine*/ |
AzureIoTClient | 6:73793bae15ba | 1340 | } |
AzureIoTClient | 6:73793bae15ba | 1341 | } |
AzureIoTClient | 6:73793bae15ba | 1342 | } |
AzureIoTClient | 6:73793bae15ba | 1343 | HTTPHeaders_Free(abandonRequestHttpHeaders); |
AzureIoTClient | 6:73793bae15ba | 1344 | } |
AzureIoTClient | 6:73793bae15ba | 1345 | } |
AzureIoTClient | 6:73793bae15ba | 1346 | STRING_delete(ETagUnquoted); |
AzureIoTClient | 6:73793bae15ba | 1347 | } |
AzureIoTClient | 6:73793bae15ba | 1348 | STRING_delete(fullAbandonRelativePath); |
AzureIoTClient | 6:73793bae15ba | 1349 | } |
AzureIoTClient | 6:73793bae15ba | 1350 | } |
AzureIoTClient | 6:73793bae15ba | 1351 | |
AzureIoTClient | 6:73793bae15ba | 1352 | static void DoMessages(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 6:73793bae15ba | 1353 | { |
AzureIoTClient | 6:73793bae15ba | 1354 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 6:73793bae15ba | 1355 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_057: [If flag DoWork_PullMessage is set to false then _DoWork shall advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1356 | if (handleData->DoWork_PullMessage) |
AzureIoTClient | 6:73793bae15ba | 1357 | { |
AzureIoTClient | 6:73793bae15ba | 1358 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_116: [After client creation, the first GET shall be allowed no matter what the value of GetMinimumPollingTime.] */ |
AzureIoTClient | 6:73793bae15ba | 1359 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_117: [If time is not available then all calls shall be treated as if they are the first one.] */ |
AzureIoTClient | 6:73793bae15ba | 1360 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_115: [A GET request that happens earlier than GetMinimumPollingTime shall be ignored.] */ |
AzureIoTClient | 6:73793bae15ba | 1361 | time_t timeNow = get_time(NULL); |
AzureIoTClient | 6:73793bae15ba | 1362 | bool isPollingAllowed = handleData->isFirstPoll || (timeNow == (time_t)(-1)) || (get_difftime(timeNow, handleData->lastPollTime) > handleData->getMinimumPollingTime); |
AzureIoTClient | 6:73793bae15ba | 1363 | if (isPollingAllowed) |
AzureIoTClient | 6:73793bae15ba | 1364 | { |
AzureIoTClient | 6:73793bae15ba | 1365 | HTTP_HEADERS_HANDLE responseHTTPHeaders = HTTPHeaders_Alloc(); |
AzureIoTClient | 6:73793bae15ba | 1366 | if (responseHTTPHeaders == NULL) |
AzureIoTClient | 6:73793bae15ba | 1367 | { |
AzureIoTClient | 6:73793bae15ba | 1368 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_037: [If the call to HTTPAPIEX_SAS_ExecuteRequest did not executed successfully or building any part of the prerequisites of the call fails, then _DoWork shall advance to the next action in this description.] */ |
AzureIoTClient | 6:73793bae15ba | 1369 | LogError("unable to HTTPHeaders_Alloc\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1370 | } |
AzureIoTClient | 6:73793bae15ba | 1371 | else |
AzureIoTClient | 6:73793bae15ba | 1372 | { |
AzureIoTClient | 6:73793bae15ba | 1373 | BUFFER_HANDLE responseContent = BUFFER_new(); |
AzureIoTClient | 6:73793bae15ba | 1374 | if (responseContent == NULL) |
AzureIoTClient | 6:73793bae15ba | 1375 | { |
AzureIoTClient | 6:73793bae15ba | 1376 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_037: [If the call to HTTPAPIEX_SAS_ExecuteRequest did not executed successfully or building any part of the prerequisites of the call fails, then _DoWork shall advance to the next action in this description.] */ |
AzureIoTClient | 6:73793bae15ba | 1377 | LogError("unable to BUFFER_new\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1378 | } |
AzureIoTClient | 6:73793bae15ba | 1379 | else |
AzureIoTClient | 6:73793bae15ba | 1380 | { |
AzureIoTClient | 6:73793bae15ba | 1381 | unsigned int statusCode; |
AzureIoTClient | 6:73793bae15ba | 1382 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_036: [Otherwise, IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters |
AzureIoTClient | 6:73793bae15ba | 1383 | requestType: GET |
AzureIoTClient | 6:73793bae15ba | 1384 | relativePath: the message HTTP relative path |
AzureIoTClient | 6:73793bae15ba | 1385 | requestHttpHeadersHandle: message HTTP request headers created by _Create |
AzureIoTClient | 6:73793bae15ba | 1386 | requestContent: NULL |
AzureIoTClient | 6:73793bae15ba | 1387 | statusCode: a pointer to unsigned int which shall be later examined |
AzureIoTClient | 6:73793bae15ba | 1388 | responseHeadearsHandle: a new instance of HTTP headers |
AzureIoTClient | 6:73793bae15ba | 1389 | responseContent: a new instance of buffer] |
AzureIoTClient | 6:73793bae15ba | 1390 | */ |
AzureIoTClient | 6:73793bae15ba | 1391 | if (HTTPAPIEX_SAS_ExecuteRequest( |
AzureIoTClient | 6:73793bae15ba | 1392 | handleData->sasObject, |
AzureIoTClient | 6:73793bae15ba | 1393 | handleData->httpApiExHandle, |
AzureIoTClient | 6:73793bae15ba | 1394 | HTTPAPI_REQUEST_GET, /*requestType: GET*/ |
AzureIoTClient | 6:73793bae15ba | 1395 | STRING_c_str(handleData->messageHTTPrelativePath), /*relativePath: the message HTTP relative path*/ |
AzureIoTClient | 6:73793bae15ba | 1396 | handleData->messageHTTPrequestHeaders, /*requestHttpHeadersHandle: message HTTP request headers created by _Create*/ |
AzureIoTClient | 6:73793bae15ba | 1397 | NULL, /*requestContent: NULL*/ |
AzureIoTClient | 6:73793bae15ba | 1398 | &statusCode, /*statusCode: a pointer to unsigned int which shall be later examined*/ |
AzureIoTClient | 6:73793bae15ba | 1399 | responseHTTPHeaders, /*responseHeadearsHandle: a new instance of HTTP headers*/ |
AzureIoTClient | 6:73793bae15ba | 1400 | responseContent /*responseContent: a new instance of buffer*/ |
AzureIoTClient | 6:73793bae15ba | 1401 | ) |
AzureIoTClient | 6:73793bae15ba | 1402 | != HTTPAPIEX_OK) |
AzureIoTClient | 6:73793bae15ba | 1403 | { |
AzureIoTClient | 6:73793bae15ba | 1404 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_037: [If the call to HTTPAPIEX_SAS_ExecuteRequest did not executed successfully or building any part of the prerequisites of the call fails, then _DoWork shall advance to the next action in this description.] */ |
AzureIoTClient | 6:73793bae15ba | 1405 | LogError("unable to HTTPAPIEX_ExecuteRequest\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1406 | } |
AzureIoTClient | 6:73793bae15ba | 1407 | else |
AzureIoTClient | 6:73793bae15ba | 1408 | { |
AzureIoTClient | 6:73793bae15ba | 1409 | /*HTTP dialogue was succesfull*/ |
AzureIoTClient | 6:73793bae15ba | 1410 | if (timeNow == (time_t)(-1)) |
AzureIoTClient | 6:73793bae15ba | 1411 | { |
AzureIoTClient | 6:73793bae15ba | 1412 | handleData->isFirstPoll = true; |
AzureIoTClient | 6:73793bae15ba | 1413 | } |
AzureIoTClient | 6:73793bae15ba | 1414 | else |
AzureIoTClient | 6:73793bae15ba | 1415 | { |
AzureIoTClient | 6:73793bae15ba | 1416 | handleData->isFirstPoll = false; |
AzureIoTClient | 6:73793bae15ba | 1417 | handleData->lastPollTime = timeNow; |
AzureIoTClient | 6:73793bae15ba | 1418 | } |
AzureIoTClient | 6:73793bae15ba | 1419 | if (statusCode == 204) |
AzureIoTClient | 6:73793bae15ba | 1420 | { |
AzureIoTClient | 6:73793bae15ba | 1421 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_038: [If the HTTPAPIEX_SAS_ExecuteRequest executed successfully then status code shall be examined. Any status code different than 200 causes _DoWork to advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1422 | /*this is an expected status code, means "no commands", but logging that creates panic*/ |
AzureIoTClient | 6:73793bae15ba | 1423 | |
AzureIoTClient | 6:73793bae15ba | 1424 | /*do nothing, advance to next action*/ |
AzureIoTClient | 6:73793bae15ba | 1425 | } |
AzureIoTClient | 6:73793bae15ba | 1426 | else if (statusCode != 200) |
AzureIoTClient | 6:73793bae15ba | 1427 | { |
AzureIoTClient | 6:73793bae15ba | 1428 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_038: [If the HTTPAPIEX_SAS_ExecuteRequest executed successfully then status code shall be examined. Any status code different than 200 causes _DoWork to advance to the next action.] */ |
AzureIoTClient | 6:73793bae15ba | 1429 | LogError("expected status code was 200, but actually was received %u... moving on\r\n", statusCode); |
AzureIoTClient | 6:73793bae15ba | 1430 | } |
AzureIoTClient | 6:73793bae15ba | 1431 | else |
AzureIoTClient | 6:73793bae15ba | 1432 | { |
AzureIoTClient | 6:73793bae15ba | 1433 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_039: [If status code is 200, then _DoWork shall make a copy of the value of the "ETag" http header.]*/ |
AzureIoTClient | 6:73793bae15ba | 1434 | const char* etagValue = HTTPHeaders_FindHeaderValue(responseHTTPHeaders, "ETag"); |
AzureIoTClient | 6:73793bae15ba | 1435 | if (etagValue == NULL) |
AzureIoTClient | 6:73793bae15ba | 1436 | { |
AzureIoTClient | 6:73793bae15ba | 1437 | LogError("unable to find a received header called \"E-Tag\"\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1438 | } |
AzureIoTClient | 6:73793bae15ba | 1439 | else |
AzureIoTClient | 6:73793bae15ba | 1440 | { |
AzureIoTClient | 6:73793bae15ba | 1441 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_040: [If no such header is found or is invalid, then _DoWork shall advance to the next action.]*/ |
AzureIoTClient | 6:73793bae15ba | 1442 | size_t etagsize = strlen(etagValue); |
AzureIoTClient | 6:73793bae15ba | 1443 | if ( |
AzureIoTClient | 6:73793bae15ba | 1444 | (etagsize < 2) || |
AzureIoTClient | 6:73793bae15ba | 1445 | (etagValue[0] != '"') || |
AzureIoTClient | 6:73793bae15ba | 1446 | (etagValue[etagsize - 1] != '"') |
AzureIoTClient | 6:73793bae15ba | 1447 | ) |
AzureIoTClient | 6:73793bae15ba | 1448 | { |
AzureIoTClient | 6:73793bae15ba | 1449 | LogError("ETag is not a valid quoted string\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1450 | } |
AzureIoTClient | 6:73793bae15ba | 1451 | else |
AzureIoTClient | 6:73793bae15ba | 1452 | { |
AzureIoTClient | 6:73793bae15ba | 1453 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_041: [_DoWork shall assemble an IOTHUBMESSAGE_HANDLE from the received HTTP content (using the responseContent buffer).] */ |
AzureIoTClient | 6:73793bae15ba | 1454 | IOTHUB_MESSAGE_HANDLE receivedMessage = IoTHubMessage_CreateFromByteArray(BUFFER_u_char(responseContent), BUFFER_length(responseContent)); |
AzureIoTClient | 6:73793bae15ba | 1455 | if (receivedMessage == NULL) |
AzureIoTClient | 6:73793bae15ba | 1456 | { |
AzureIoTClient | 6:73793bae15ba | 1457 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_042: [If assembling the message fails in any way, then _DoWork shall "abandon" the message.]*/ |
AzureIoTClient | 6:73793bae15ba | 1458 | LogError("unable to IoTHubMessage_CreateFromByteArray, trying to abandon the message... \r\n"); |
AzureIoTClient | 6:73793bae15ba | 1459 | abandonOrAcceptMessage(handle, etagValue, ABANDON); |
AzureIoTClient | 6:73793bae15ba | 1460 | } |
AzureIoTClient | 6:73793bae15ba | 1461 | else |
AzureIoTClient | 6:73793bae15ba | 1462 | { |
AzureIoTClient | 6:73793bae15ba | 1463 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_087: [All the HTTP headers of the form iothub-app-name:somecontent shall be transformed in message properties {name, somecontent}.]*/ |
AzureIoTClient | 6:73793bae15ba | 1464 | size_t nHeaders; |
AzureIoTClient | 6:73793bae15ba | 1465 | if (HTTPHeaders_GetHeaderCount(responseHTTPHeaders, &nHeaders) != HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 1466 | { |
AzureIoTClient | 6:73793bae15ba | 1467 | LogError("unable to get the count of HTTP headers\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1468 | abandonOrAcceptMessage(handle, etagValue, ABANDON); |
AzureIoTClient | 6:73793bae15ba | 1469 | } |
AzureIoTClient | 6:73793bae15ba | 1470 | else |
AzureIoTClient | 6:73793bae15ba | 1471 | { |
AzureIoTClient | 6:73793bae15ba | 1472 | size_t i; |
AzureIoTClient | 6:73793bae15ba | 1473 | MAP_HANDLE properties = (nHeaders > 0) ? IoTHubMessage_Properties(receivedMessage) : NULL; |
AzureIoTClient | 6:73793bae15ba | 1474 | for (i = 0; i < nHeaders; i++) |
AzureIoTClient | 6:73793bae15ba | 1475 | { |
AzureIoTClient | 6:73793bae15ba | 1476 | char* completeHeader; |
AzureIoTClient | 6:73793bae15ba | 1477 | if (HTTPHeaders_GetHeader(responseHTTPHeaders, i, &completeHeader) != HTTP_HEADERS_OK) |
AzureIoTClient | 6:73793bae15ba | 1478 | { |
AzureIoTClient | 6:73793bae15ba | 1479 | break; |
AzureIoTClient | 6:73793bae15ba | 1480 | } |
AzureIoTClient | 6:73793bae15ba | 1481 | else |
AzureIoTClient | 6:73793bae15ba | 1482 | { |
AzureIoTClient | 6:73793bae15ba | 1483 | if (strncmp(IOTHUB_APP_PREFIX, completeHeader, strlen(IOTHUB_APP_PREFIX)) == 0) |
AzureIoTClient | 6:73793bae15ba | 1484 | { |
AzureIoTClient | 6:73793bae15ba | 1485 | /*looks like a property headers*/ |
AzureIoTClient | 6:73793bae15ba | 1486 | /*there's a guaranteed ':' in the completeHeader, by HTTP_HEADERS module*/ |
AzureIoTClient | 6:73793bae15ba | 1487 | char* whereIsColon = strchr(completeHeader, ':'); |
AzureIoTClient | 6:73793bae15ba | 1488 | *whereIsColon = '\0'; /*cut it down*/ |
AzureIoTClient | 6:73793bae15ba | 1489 | if (Map_AddOrUpdate(properties, completeHeader + strlen(IOTHUB_APP_PREFIX), whereIsColon + 2) != MAP_OK) /*whereIsColon+1 is a space because HTTPEHADERS outputs a ": " between name and value*/ |
AzureIoTClient | 6:73793bae15ba | 1490 | { |
AzureIoTClient | 6:73793bae15ba | 1491 | free(completeHeader); |
AzureIoTClient | 6:73793bae15ba | 1492 | break; |
AzureIoTClient | 6:73793bae15ba | 1493 | } |
AzureIoTClient | 6:73793bae15ba | 1494 | } |
AzureIoTClient | 6:73793bae15ba | 1495 | free(completeHeader); |
AzureIoTClient | 6:73793bae15ba | 1496 | } |
AzureIoTClient | 6:73793bae15ba | 1497 | } |
AzureIoTClient | 6:73793bae15ba | 1498 | |
AzureIoTClient | 6:73793bae15ba | 1499 | if (i < nHeaders) |
AzureIoTClient | 6:73793bae15ba | 1500 | { |
AzureIoTClient | 6:73793bae15ba | 1501 | abandonOrAcceptMessage(handle, etagValue, ABANDON); |
AzureIoTClient | 6:73793bae15ba | 1502 | } |
AzureIoTClient | 6:73793bae15ba | 1503 | else |
AzureIoTClient | 6:73793bae15ba | 1504 | { |
AzureIoTClient | 6:73793bae15ba | 1505 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_043: [Otherwise, _DoWork shall call IoTHubClient_LL_MessageCallback with parameters handle = iotHubClientHandle and message = newly created message.]*/ |
AzureIoTClient | 6:73793bae15ba | 1506 | IOTHUBMESSAGE_DISPOSITION_RESULT messageResult = IoTHubClient_LL_MessageCallback(iotHubClientHandle, receivedMessage); |
AzureIoTClient | 6:73793bae15ba | 1507 | if (messageResult == IOTHUBMESSAGE_ACCEPTED) |
AzureIoTClient | 6:73793bae15ba | 1508 | { |
AzureIoTClient | 6:73793bae15ba | 1509 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_044: [If IoTHubClient_LL_MessageCallback returns IOTHUBMESSAGE_ACCEPTED then _DoWork shall "accept" the message.]*/ |
AzureIoTClient | 6:73793bae15ba | 1510 | abandonOrAcceptMessage(handle, etagValue, ACCEPT); |
AzureIoTClient | 6:73793bae15ba | 1511 | } |
AzureIoTClient | 6:73793bae15ba | 1512 | else if (messageResult == IOTHUBMESSAGE_REJECTED) |
AzureIoTClient | 6:73793bae15ba | 1513 | { |
AzureIoTClient | 6:73793bae15ba | 1514 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_074: [If IoTHubClient_LL_MessageCallback returns IOTHUBMESSAGE_REJECTED then _DoWork shall "reject" the message.]*/ |
AzureIoTClient | 6:73793bae15ba | 1515 | abandonOrAcceptMessage(handle, etagValue, REJECT); |
AzureIoTClient | 6:73793bae15ba | 1516 | } |
AzureIoTClient | 6:73793bae15ba | 1517 | else |
AzureIoTClient | 6:73793bae15ba | 1518 | { |
AzureIoTClient | 6:73793bae15ba | 1519 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_079: [If IoTHubClient_LL_MessageCallback returns IOTHUBMESSAGE_ABANDONED then _DoWork shall "abandon" the message.] */ |
AzureIoTClient | 6:73793bae15ba | 1520 | abandonOrAcceptMessage(handle, etagValue, ABANDON); |
AzureIoTClient | 6:73793bae15ba | 1521 | } |
AzureIoTClient | 6:73793bae15ba | 1522 | } |
AzureIoTClient | 6:73793bae15ba | 1523 | } |
AzureIoTClient | 6:73793bae15ba | 1524 | IoTHubMessage_Destroy(receivedMessage); |
AzureIoTClient | 6:73793bae15ba | 1525 | } |
AzureIoTClient | 6:73793bae15ba | 1526 | } |
AzureIoTClient | 6:73793bae15ba | 1527 | |
AzureIoTClient | 6:73793bae15ba | 1528 | } |
AzureIoTClient | 6:73793bae15ba | 1529 | } |
AzureIoTClient | 6:73793bae15ba | 1530 | } |
AzureIoTClient | 6:73793bae15ba | 1531 | BUFFER_delete(responseContent); |
AzureIoTClient | 6:73793bae15ba | 1532 | } |
AzureIoTClient | 6:73793bae15ba | 1533 | HTTPHeaders_Free(responseHTTPHeaders); |
AzureIoTClient | 6:73793bae15ba | 1534 | } |
AzureIoTClient | 6:73793bae15ba | 1535 | } |
AzureIoTClient | 6:73793bae15ba | 1536 | else |
AzureIoTClient | 6:73793bae15ba | 1537 | { |
AzureIoTClient | 6:73793bae15ba | 1538 | /*isPollingAllowed is false... */ |
AzureIoTClient | 6:73793bae15ba | 1539 | /*do nothing "shall be ignored*/ |
AzureIoTClient | 6:73793bae15ba | 1540 | } |
AzureIoTClient | 6:73793bae15ba | 1541 | } |
AzureIoTClient | 6:73793bae15ba | 1542 | } |
AzureIoTClient | 6:73793bae15ba | 1543 | |
AzureIoTClient | 6:73793bae15ba | 1544 | void IoTHubTransportHttp_DoWork(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 6:73793bae15ba | 1545 | { |
AzureIoTClient | 6:73793bae15ba | 1546 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_017: [If parameter handle is NULL or parameter iotHubClientHandle then IoTHubTransportHttp_DoWork shall immeditely return.]*/ |
AzureIoTClient | 6:73793bae15ba | 1547 | if ((handle != NULL) && (iotHubClientHandle != NULL)) |
AzureIoTClient | 6:73793bae15ba | 1548 | { |
AzureIoTClient | 6:73793bae15ba | 1549 | DoEvent(handle, iotHubClientHandle); |
AzureIoTClient | 6:73793bae15ba | 1550 | DoMessages(handle, iotHubClientHandle); |
AzureIoTClient | 6:73793bae15ba | 1551 | } |
AzureIoTClient | 6:73793bae15ba | 1552 | } |
AzureIoTClient | 6:73793bae15ba | 1553 | |
AzureIoTClient | 6:73793bae15ba | 1554 | IOTHUB_CLIENT_RESULT IoTHubTransportHttp_GetSendStatus(TRANSPORT_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus) |
AzureIoTClient | 6:73793bae15ba | 1555 | { |
AzureIoTClient | 6:73793bae15ba | 1556 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 6:73793bae15ba | 1557 | |
AzureIoTClient | 6:73793bae15ba | 1558 | /* Codes_SRS_IOTHUBTRANSPORTTHTTP_09_001: [IoTHubTransportHttp_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter] */ |
AzureIoTClient | 6:73793bae15ba | 1559 | if (handle == NULL) |
AzureIoTClient | 6:73793bae15ba | 1560 | { |
AzureIoTClient | 6:73793bae15ba | 1561 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 6:73793bae15ba | 1562 | LogError("Invalid handle to IoTHubClient HTTP transport instance.\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1563 | } |
AzureIoTClient | 6:73793bae15ba | 1564 | else if (iotHubClientStatus == NULL) |
AzureIoTClient | 6:73793bae15ba | 1565 | { |
AzureIoTClient | 6:73793bae15ba | 1566 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 6:73793bae15ba | 1567 | LogError("Invalid pointer to output parameter IOTHUB_CLIENT_STATUS.\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1568 | } |
AzureIoTClient | 6:73793bae15ba | 1569 | else |
AzureIoTClient | 6:73793bae15ba | 1570 | { |
AzureIoTClient | 6:73793bae15ba | 1571 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 6:73793bae15ba | 1572 | |
AzureIoTClient | 6:73793bae15ba | 1573 | /* Codes_SRS_IOTHUBTRANSPORTTHTTP_09_002: [IoTHubTransportHttp_GetSendStatus shall return IOTHUB_CLIENT_OK and status IOTHUB_CLIENT_SEND_STATUS_IDLE if there are currently no event items to be sent or being sent] */ |
AzureIoTClient | 6:73793bae15ba | 1574 | if (!DList_IsListEmpty(handleData->waitingToSend)) |
AzureIoTClient | 6:73793bae15ba | 1575 | { |
AzureIoTClient | 6:73793bae15ba | 1576 | *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY; |
AzureIoTClient | 6:73793bae15ba | 1577 | } |
AzureIoTClient | 6:73793bae15ba | 1578 | /* Codes_SRS_IOTHUBTRANSPORTTHTTP_09_003: [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] */ |
AzureIoTClient | 6:73793bae15ba | 1579 | else |
AzureIoTClient | 6:73793bae15ba | 1580 | { |
AzureIoTClient | 6:73793bae15ba | 1581 | *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE; |
AzureIoTClient | 6:73793bae15ba | 1582 | } |
AzureIoTClient | 6:73793bae15ba | 1583 | |
AzureIoTClient | 6:73793bae15ba | 1584 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 6:73793bae15ba | 1585 | } |
AzureIoTClient | 6:73793bae15ba | 1586 | |
AzureIoTClient | 6:73793bae15ba | 1587 | return result; |
AzureIoTClient | 6:73793bae15ba | 1588 | } |
AzureIoTClient | 6:73793bae15ba | 1589 | |
AzureIoTClient | 6:73793bae15ba | 1590 | IOTHUB_CLIENT_RESULT IoTHubTransportHttp_SetOption(TRANSPORT_HANDLE handle, const char* option, const void* value) |
AzureIoTClient | 6:73793bae15ba | 1591 | { |
AzureIoTClient | 6:73793bae15ba | 1592 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 6:73793bae15ba | 1593 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_080: [If handle parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 6:73793bae15ba | 1594 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_081: [If option parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 6:73793bae15ba | 1595 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_082: [If value parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
AzureIoTClient | 6:73793bae15ba | 1596 | if ( |
AzureIoTClient | 6:73793bae15ba | 1597 | (handle == NULL) || |
AzureIoTClient | 6:73793bae15ba | 1598 | (option == NULL) || |
AzureIoTClient | 6:73793bae15ba | 1599 | (value == NULL) |
AzureIoTClient | 6:73793bae15ba | 1600 | ) |
AzureIoTClient | 6:73793bae15ba | 1601 | { |
AzureIoTClient | 6:73793bae15ba | 1602 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 6:73793bae15ba | 1603 | LogError("invalid parameter (NULL) passed to IoTHubTransportHttp_SetOption\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1604 | } |
AzureIoTClient | 6:73793bae15ba | 1605 | else |
AzureIoTClient | 6:73793bae15ba | 1606 | { |
AzureIoTClient | 6:73793bae15ba | 1607 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 6:73793bae15ba | 1608 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_097: ["Batching"] */ |
AzureIoTClient | 6:73793bae15ba | 1609 | if (strcmp("Batching", option) == 0) |
AzureIoTClient | 6:73793bae15ba | 1610 | { |
AzureIoTClient | 6:73793bae15ba | 1611 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_083: [If optionName is an option handled by IoTHubTransportHttp then it shall be set.] */ |
AzureIoTClient | 6:73793bae15ba | 1612 | handleData->doBatchedTransfers = *(bool*)value; |
AzureIoTClient | 6:73793bae15ba | 1613 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 6:73793bae15ba | 1614 | } |
AzureIoTClient | 6:73793bae15ba | 1615 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_114: ["MinimumPollingTime"] */ |
AzureIoTClient | 6:73793bae15ba | 1616 | else if (strcmp("MinimumPollingTime", option) == 0) |
AzureIoTClient | 6:73793bae15ba | 1617 | { |
AzureIoTClient | 6:73793bae15ba | 1618 | handleData->getMinimumPollingTime = *(unsigned int*)value; |
AzureIoTClient | 6:73793bae15ba | 1619 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 6:73793bae15ba | 1620 | } |
AzureIoTClient | 6:73793bae15ba | 1621 | else |
AzureIoTClient | 6:73793bae15ba | 1622 | { |
AzureIoTClient | 6:73793bae15ba | 1623 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_084: [Otherwise, IoTHubTransport_Http shall call HTTPAPIEX_SetOption with the same parameters and return the translated code.] */ |
AzureIoTClient | 6:73793bae15ba | 1624 | HTTPAPIEX_RESULT HTTPAPIEX_result = HTTPAPIEX_SetOption(handleData->httpApiExHandle, option, value); |
AzureIoTClient | 6:73793bae15ba | 1625 | /*Codes_SRS_IOTHUBTRANSPORTTHTTP_02_086: [The following table translates HTTPAPIEX return codes to IOTHUB_CLIENT_RESULT return codes:] */ |
AzureIoTClient | 6:73793bae15ba | 1626 | if (HTTPAPIEX_result == HTTPAPIEX_OK) |
AzureIoTClient | 6:73793bae15ba | 1627 | { |
AzureIoTClient | 6:73793bae15ba | 1628 | result = IOTHUB_CLIENT_OK; |
AzureIoTClient | 6:73793bae15ba | 1629 | } |
AzureIoTClient | 6:73793bae15ba | 1630 | else if (HTTPAPIEX_result == HTTPAPIEX_INVALID_ARG) |
AzureIoTClient | 6:73793bae15ba | 1631 | { |
AzureIoTClient | 6:73793bae15ba | 1632 | result = IOTHUB_CLIENT_INVALID_ARG; |
AzureIoTClient | 6:73793bae15ba | 1633 | LogError("HTTPAPIEX_SetOption failed\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1634 | } |
AzureIoTClient | 6:73793bae15ba | 1635 | else |
AzureIoTClient | 6:73793bae15ba | 1636 | { |
AzureIoTClient | 6:73793bae15ba | 1637 | result = IOTHUB_CLIENT_ERROR; |
AzureIoTClient | 6:73793bae15ba | 1638 | LogError("HTTPAPIEX_SetOption failed\r\n"); |
AzureIoTClient | 6:73793bae15ba | 1639 | } |
AzureIoTClient | 6:73793bae15ba | 1640 | } |
AzureIoTClient | 6:73793bae15ba | 1641 | } |
AzureIoTClient | 6:73793bae15ba | 1642 | return result; |
AzureIoTClient | 6:73793bae15ba | 1643 | } |