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