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