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@21:b601705c82a8, 2016-07-29 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Fri Jul 29 15:52:52 2016 -0700
- Revision:
- 21:b601705c82a8
- Parent:
- 20:b652f2e4cccf
- Child:
- 22:e0add922c564
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 | |
AzureIoTClient | 20:b652f2e4cccf | 163 | static bool create_eventHTTPrequestHeaders(HTTPTRANSPORT_PERDEVICE_DATA* handleData, const char * deviceId, bool is_x509_used) |
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) && |
AzureIoTClient | 20:b652f2e4cccf | 201 | (is_x509_used || (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 | |
AzureIoTClient | 20:b652f2e4cccf | 230 | static bool create_messageHTTPrequestHeaders(HTTPTRANSPORT_PERDEVICE_DATA* handleData, bool is_x509_used) |
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) && |
AzureIoTClient | 20:b652f2e4cccf | 245 | (is_x509_used || (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 | } |
AzureIoTClient | 20:b652f2e4cccf | 488 | else if (device->deviceId == NULL || ((device->deviceKey != NULL) && (device->deviceSasToken != NULL)) || waitingToSend == NULL || iotHubClientHandle == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 489 | { |
AzureIoTClient | 20:b652f2e4cccf | 490 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_015: [ If IOTHUB_DEVICE_CONFIG fields deviceKey and deviceSasToken are NULL, then IoTHubTransportHttp_Register shall assume a x509 authentication. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 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. ]*/ |
AzureIoTClient | 20:b652f2e4cccf | 494 | LogError("invalid parameters detected TRANSPORT_LL_HANDLE handle=%p, const IOTHUB_DEVICE_CONFIG* device=%p, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle=%p, PDLIST_ENTRY waitingToSend=%p", |
AzureIoTClient | 20:b652f2e4cccf | 495 | handle, device, iotHubClientHandle, waitingToSend); |
Azure.IoT Build | 19:d4f9e872bf3e | 496 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 497 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 498 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 499 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 500 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
Azure.IoT Build | 19:d4f9e872bf3e | 501 | /*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 | 502 | void* listItem = VECTOR_find_if(handleData->perDeviceList, findDeviceById, device->deviceId); |
Azure.IoT Build | 19:d4f9e872bf3e | 503 | if (listItem != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 504 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 505 | /*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 | 506 | LogError("Transport already has device registered by id: [%s]", device->deviceId); |
Azure.IoT Build | 19:d4f9e872bf3e | 507 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 508 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 509 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 510 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 511 | bool was_create_deviceKey_ok = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 512 | bool was_create_deviceSasToken_ok = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 513 | bool was_sasObject_ok = false; |
AzureIoTClient | 20:b652f2e4cccf | 514 | bool was_x509_ok = false; /*there's nothing "created" in the case of x509, it is a flag indicating that x509 is used*/ |
AzureIoTClient | 17:7ef88cde801f | 515 | |
Azure.IoT Build | 19:d4f9e872bf3e | 516 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_038: [ Otherwise, IoTHubTransportHttp_Register shall allocate the IOTHUB_DEVICE_HANDLE structure. ]*/ |
AzureIoTClient | 21:b601705c82a8 | 517 | bool was_resultCreated_ok = ((result = (HTTPTRANSPORT_PERDEVICE_DATA *) malloc(sizeof(HTTPTRANSPORT_PERDEVICE_DATA))) != NULL); |
Azure.IoT Build | 19:d4f9e872bf3e | 518 | bool was_create_deviceId_ok = was_resultCreated_ok && create_deviceId(result, device->deviceId); |
AzureIoTClient | 17:7ef88cde801f | 519 | |
AzureIoTClient | 20:b652f2e4cccf | 520 | if (was_create_deviceId_ok) |
Azure.IoT Build | 19:d4f9e872bf3e | 521 | { |
AzureIoTClient | 20:b652f2e4cccf | 522 | if (device->deviceSasToken != NULL) |
AzureIoTClient | 20:b652f2e4cccf | 523 | { |
AzureIoTClient | 20:b652f2e4cccf | 524 | /*device->deviceKey is certainly NULL (because of the validation condition (else if (device->deviceId == NULL || ((device->deviceKey != NULL) && (device->deviceSasToken != NULL)) || waitingToSend == NULL || iotHubClientHandle == NULL))that does not accept that both satoken AND devicekey are non-NULL*/ |
AzureIoTClient | 20:b652f2e4cccf | 525 | was_create_deviceSasToken_ok = create_deviceSasToken(result, device->deviceSasToken); |
AzureIoTClient | 20:b652f2e4cccf | 526 | result->deviceKey = NULL; |
AzureIoTClient | 20:b652f2e4cccf | 527 | result->sasObject = NULL; |
AzureIoTClient | 20:b652f2e4cccf | 528 | } |
AzureIoTClient | 20:b652f2e4cccf | 529 | else /*when deviceSasToken == NULL*/ |
AzureIoTClient | 20:b652f2e4cccf | 530 | { |
AzureIoTClient | 20:b652f2e4cccf | 531 | if (device->deviceKey != NULL) |
AzureIoTClient | 20:b652f2e4cccf | 532 | { |
AzureIoTClient | 20:b652f2e4cccf | 533 | was_create_deviceKey_ok = create_deviceKey(result, device->deviceKey); |
AzureIoTClient | 20:b652f2e4cccf | 534 | result->deviceSasToken = NULL; |
AzureIoTClient | 20:b652f2e4cccf | 535 | } |
AzureIoTClient | 20:b652f2e4cccf | 536 | else |
AzureIoTClient | 20:b652f2e4cccf | 537 | { |
AzureIoTClient | 20:b652f2e4cccf | 538 | /*when both of them are NULL*/ |
AzureIoTClient | 20:b652f2e4cccf | 539 | was_x509_ok = true; |
AzureIoTClient | 20:b652f2e4cccf | 540 | result->deviceKey = NULL; |
AzureIoTClient | 20:b652f2e4cccf | 541 | result->deviceSasToken = NULL; |
AzureIoTClient | 20:b652f2e4cccf | 542 | } |
AzureIoTClient | 20:b652f2e4cccf | 543 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 544 | } |
AzureIoTClient | 17:7ef88cde801f | 545 | |
AzureIoTClient | 20:b652f2e4cccf | 546 | bool was_eventHTTPrelativePath_ok = (was_create_deviceKey_ok || was_create_deviceSasToken_ok || was_x509_ok) && create_eventHTTPrelativePath(result, device->deviceId); |
Azure.IoT Build | 19:d4f9e872bf3e | 547 | bool was_messageHTTPrelativePath_ok = was_eventHTTPrelativePath_ok && create_messageHTTPrelativePath(result, device->deviceId); |
AzureIoTClient | 20:b652f2e4cccf | 548 | bool was_eventHTTPrequestHeaders_ok = was_messageHTTPrelativePath_ok && create_eventHTTPrequestHeaders(result, device->deviceId, was_x509_ok); |
AzureIoTClient | 20:b652f2e4cccf | 549 | bool was_messageHTTPrequestHeaders_ok = was_eventHTTPrequestHeaders_ok && create_messageHTTPrequestHeaders(result, was_x509_ok); |
Azure.IoT Build | 19:d4f9e872bf3e | 550 | bool was_abandonHTTPrelativePathBegin_ok = was_messageHTTPrequestHeaders_ok && create_abandonHTTPrelativePathBegin(result, device->deviceId); |
AzureIoTClient | 17:7ef88cde801f | 551 | |
AzureIoTClient | 20:b652f2e4cccf | 552 | if (was_x509_ok) |
Azure.IoT Build | 19:d4f9e872bf3e | 553 | { |
AzureIoTClient | 20:b652f2e4cccf | 554 | result->sasObject = NULL; /**/ |
AzureIoTClient | 20:b652f2e4cccf | 555 | was_sasObject_ok = true; |
AzureIoTClient | 20:b652f2e4cccf | 556 | } |
AzureIoTClient | 20:b652f2e4cccf | 557 | else |
AzureIoTClient | 20:b652f2e4cccf | 558 | { |
AzureIoTClient | 20:b652f2e4cccf | 559 | if (!was_create_deviceSasToken_ok) |
AzureIoTClient | 20:b652f2e4cccf | 560 | { |
AzureIoTClient | 20:b652f2e4cccf | 561 | was_sasObject_ok = was_abandonHTTPrelativePathBegin_ok && create_deviceSASObject(result, handleData->hostName, device->deviceId, device->deviceKey); |
AzureIoTClient | 20:b652f2e4cccf | 562 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 563 | } |
AzureIoTClient | 17:7ef88cde801f | 564 | |
Azure.IoT Build | 19:d4f9e872bf3e | 565 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_041: [ IoTHubTransportHttp_Register shall call VECTOR_push_back to store the new device information. ]*/ |
AzureIoTClient | 20:b652f2e4cccf | 566 | bool was_list_add_ok = (was_sasObject_ok || was_create_deviceSasToken_ok || was_x509_ok) && (VECTOR_push_back(handleData->perDeviceList, &result, 1) == 0); |
Azure.IoT Build | 13:848d52f93daf | 567 | |
Azure.IoT Build | 19:d4f9e872bf3e | 568 | if (was_list_add_ok) |
Azure.IoT Build | 19:d4f9e872bf3e | 569 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 570 | /*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 | 571 | /*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 | 572 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_128: [ IoTHubTransportHttp_Register shall mark this device as unsubscribed. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 573 | result->DoWork_PullMessage = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 574 | result->isFirstPoll = true; |
Azure.IoT Build | 19:d4f9e872bf3e | 575 | result->iotHubClientHandle = iotHubClientHandle; |
Azure.IoT Build | 19:d4f9e872bf3e | 576 | result->waitingToSend = waitingToSend; |
Azure.IoT Build | 19:d4f9e872bf3e | 577 | DList_InitializeListHead(&(result->eventConfirmations)); |
AzureIoTClient | 21:b601705c82a8 | 578 | result->transportHandle = (HTTPTRANSPORT_HANDLE_DATA *) handle; |
Azure.IoT Build | 19:d4f9e872bf3e | 579 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 580 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 581 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 582 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_042: [ If the list_add fails then IoTHubTransportHttp_Register shall fail and return NULL. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 583 | if (was_sasObject_ok) destroy_SASObject(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 584 | if (was_abandonHTTPrelativePathBegin_ok) destroy_abandonHTTPrelativePathBegin(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 585 | if (was_messageHTTPrelativePath_ok) destroy_messageHTTPrelativePath(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 586 | if (was_eventHTTPrequestHeaders_ok) destroy_eventHTTPrequestHeaders(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 587 | if (was_messageHTTPrequestHeaders_ok) destroy_messageHTTPrequestHeaders(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 588 | if (was_eventHTTPrelativePath_ok) destroy_eventHTTPrelativePath(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 589 | if (was_create_deviceId_ok) destroy_deviceId(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 590 | if (was_create_deviceKey_ok) destroy_deviceKey(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 591 | if (was_create_deviceSasToken_ok) destroy_deviceSasToken(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 592 | /*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 | 593 | if (was_resultCreated_ok) free(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 594 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 595 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 596 | } |
AzureIoTClient | 17:7ef88cde801f | 597 | |
Azure.IoT Build | 19:d4f9e872bf3e | 598 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 599 | return (IOTHUB_DEVICE_HANDLE)result; |
Azure.IoT Build | 13:848d52f93daf | 600 | } |
Azure.IoT Build | 13:848d52f93daf | 601 | |
Azure.IoT Build | 13:848d52f93daf | 602 | |
Azure.IoT Build | 13:848d52f93daf | 603 | static void destroy_perDeviceData(HTTPTRANSPORT_PERDEVICE_DATA * perDeviceItem) |
Azure.IoT Build | 13:848d52f93daf | 604 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 605 | destroy_deviceId(perDeviceItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 606 | destroy_deviceKey(perDeviceItem); |
AzureIoTClient | 17:7ef88cde801f | 607 | destroy_deviceSas(perDeviceItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 608 | destroy_eventHTTPrelativePath(perDeviceItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 609 | destroy_messageHTTPrelativePath(perDeviceItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 610 | destroy_eventHTTPrequestHeaders(perDeviceItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 611 | destroy_messageHTTPrequestHeaders(perDeviceItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 612 | destroy_abandonHTTPrelativePathBegin(perDeviceItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 613 | destroy_SASObject(perDeviceItem); |
Azure.IoT Build | 13:848d52f93daf | 614 | } |
Azure.IoT Build | 13:848d52f93daf | 615 | |
Azure.IoT Build | 13:848d52f93daf | 616 | static IOTHUB_DEVICE_HANDLE* get_perDeviceDataItem(IOTHUB_DEVICE_HANDLE deviceHandle) |
Azure.IoT Build | 13:848d52f93daf | 617 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 618 | HTTPTRANSPORT_PERDEVICE_DATA* deviceHandleData = (HTTPTRANSPORT_PERDEVICE_DATA*)deviceHandle; |
Azure.IoT Build | 19:d4f9e872bf3e | 619 | IOTHUB_DEVICE_HANDLE* listItem; |
Azure.IoT Build | 13:848d52f93daf | 620 | |
Azure.IoT Build | 19:d4f9e872bf3e | 621 | HTTPTRANSPORT_HANDLE_DATA* handleData = deviceHandleData->transportHandle; |
Azure.IoT Build | 13:848d52f93daf | 622 | |
AzureIoTClient | 21:b601705c82a8 | 623 | listItem = (IOTHUB_DEVICE_HANDLE *) VECTOR_find_if(handleData->perDeviceList, findDeviceHandle, deviceHandle); |
Azure.IoT Build | 19:d4f9e872bf3e | 624 | if (listItem == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 625 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 626 | LogError("device handle not found in transport device list"); |
Azure.IoT Build | 19:d4f9e872bf3e | 627 | listItem = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 628 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 629 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 630 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 631 | /* sucessfully found device in list. */ |
Azure.IoT Build | 19:d4f9e872bf3e | 632 | } |
Azure.IoT Build | 13:848d52f93daf | 633 | |
Azure.IoT Build | 19:d4f9e872bf3e | 634 | return listItem; |
Azure.IoT Build | 13:848d52f93daf | 635 | } |
Azure.IoT Build | 13:848d52f93daf | 636 | |
AzureIoTClient | 18:ab990f6aa61f | 637 | static void IoTHubTransportHttp_Unregister(IOTHUB_DEVICE_HANDLE deviceHandle) |
Azure.IoT Build | 13:848d52f93daf | 638 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 639 | if (deviceHandle == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 640 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 641 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_044: [ If deviceHandle is NULL, then IoTHubTransportHttp_Unregister shall do nothing. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 642 | LogError("Unregister a NULL device handle"); |
Azure.IoT Build | 19:d4f9e872bf3e | 643 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 644 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 645 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 646 | HTTPTRANSPORT_PERDEVICE_DATA* deviceHandleData = (HTTPTRANSPORT_PERDEVICE_DATA*)deviceHandle; |
Azure.IoT Build | 19:d4f9e872bf3e | 647 | HTTPTRANSPORT_HANDLE_DATA* handleData = deviceHandleData->transportHandle; |
Azure.IoT Build | 19:d4f9e872bf3e | 648 | /*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 | 649 | IOTHUB_DEVICE_HANDLE* listItem = get_perDeviceDataItem(deviceHandle); |
Azure.IoT Build | 19:d4f9e872bf3e | 650 | if (listItem == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 651 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 652 | /*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 | 653 | LogError("Device Handle [%p] not found in transport", deviceHandle); |
Azure.IoT Build | 19:d4f9e872bf3e | 654 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 655 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 656 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 657 | HTTPTRANSPORT_PERDEVICE_DATA * perDeviceItem = (HTTPTRANSPORT_PERDEVICE_DATA *)(*listItem); |
Azure.IoT Build | 13:848d52f93daf | 658 | |
Azure.IoT Build | 19:d4f9e872bf3e | 659 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_047: [ IoTHubTransportHttp_Unregister shall free all the resources used in the device structure. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 660 | destroy_perDeviceData(perDeviceItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 661 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_048: [ IoTHubTransportHttp_Unregister shall call list_remove to remove device from devices list. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 662 | VECTOR_erase(handleData->perDeviceList, listItem, 1); |
Azure.IoT Build | 19:d4f9e872bf3e | 663 | free(deviceHandleData); |
Azure.IoT Build | 19:d4f9e872bf3e | 664 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 665 | } |
Azure.IoT Build | 13:848d52f93daf | 666 | |
Azure.IoT Build | 19:d4f9e872bf3e | 667 | return; |
Azure.IoT Build | 13:848d52f93daf | 668 | } |
Azure.IoT Build | 13:848d52f93daf | 669 | |
Azure.IoT Build | 13:848d52f93daf | 670 | |
Azure.IoT Build | 13:848d52f93daf | 671 | /*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 | 672 | static void destroy_hostName(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 673 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 674 | STRING_delete(handleData->hostName); |
Azure.IoT Build | 19:d4f9e872bf3e | 675 | handleData->hostName = NULL; |
AzureIoTClient | 6:73793bae15ba | 676 | } |
AzureIoTClient | 6:73793bae15ba | 677 | |
Azure.IoT Build | 13:848d52f93daf | 678 | /*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 | 679 | static bool create_hostName(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 680 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 681 | bool result; |
Azure.IoT Build | 19:d4f9e872bf3e | 682 | handleData->hostName = STRING_construct(config->upperConfig->iotHubName); |
Azure.IoT Build | 19:d4f9e872bf3e | 683 | if (handleData->hostName == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 684 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 685 | result = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 686 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 687 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 688 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 689 | if ((STRING_concat(handleData->hostName, ".") != 0) || |
Azure.IoT Build | 19:d4f9e872bf3e | 690 | (STRING_concat(handleData->hostName, config->upperConfig->iotHubSuffix) != 0)) |
Azure.IoT Build | 19:d4f9e872bf3e | 691 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 692 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_006: [ If creating the hostname fails then IoTHubTransportHttp_Create shall fail and return NULL. ] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 693 | destroy_hostName(handleData); |
Azure.IoT Build | 19:d4f9e872bf3e | 694 | result = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 695 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 696 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 697 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 698 | result = true; |
Azure.IoT Build | 19:d4f9e872bf3e | 699 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 700 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 701 | return result; |
AzureIoTClient | 6:73793bae15ba | 702 | } |
AzureIoTClient | 6:73793bae15ba | 703 | |
Azure.IoT Build | 13:848d52f93daf | 704 | /*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 | 705 | static void destroy_httpApiExHandle(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 706 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 707 | HTTPAPIEX_Destroy(handleData->httpApiExHandle); |
Azure.IoT Build | 19:d4f9e872bf3e | 708 | handleData->httpApiExHandle = NULL; |
AzureIoTClient | 6:73793bae15ba | 709 | } |
AzureIoTClient | 6:73793bae15ba | 710 | |
Azure.IoT Build | 13:848d52f93daf | 711 | /*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 | 712 | static bool create_httpApiExHandle(HTTPTRANSPORT_HANDLE_DATA* handleData, const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 713 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 714 | bool result; |
Azure.IoT Build | 19:d4f9e872bf3e | 715 | (void)config; |
Azure.IoT Build | 19:d4f9e872bf3e | 716 | handleData->httpApiExHandle = HTTPAPIEX_Create(STRING_c_str(handleData->hostName)); |
Azure.IoT Build | 19:d4f9e872bf3e | 717 | if (handleData->httpApiExHandle == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 718 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 719 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_008: [ If creating the HTTPAPIEX_HANDLE fails then IoTHubTransportHttp_Create shall fail and return NULL. ] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 720 | result = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 721 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 722 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 723 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 724 | result = true; |
Azure.IoT Build | 19:d4f9e872bf3e | 725 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 726 | return result; |
AzureIoTClient | 6:73793bae15ba | 727 | } |
AzureIoTClient | 6:73793bae15ba | 728 | |
Azure.IoT Build | 13:848d52f93daf | 729 | static void destroy_perDeviceList(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 730 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 731 | VECTOR_destroy(handleData->perDeviceList); |
Azure.IoT Build | 19:d4f9e872bf3e | 732 | handleData->perDeviceList = NULL; |
AzureIoTClient | 6:73793bae15ba | 733 | } |
AzureIoTClient | 6:73793bae15ba | 734 | |
Azure.IoT Build | 13:848d52f93daf | 735 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_009: [ IoTHubTransportHttp_Create shall call list_create to create a list of registered devices. ]*/ |
Azure.IoT Build | 13:848d52f93daf | 736 | static bool create_perDeviceList(HTTPTRANSPORT_HANDLE_DATA* handleData) |
AzureIoTClient | 6:73793bae15ba | 737 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 738 | bool result; |
Azure.IoT Build | 19:d4f9e872bf3e | 739 | handleData->perDeviceList = VECTOR_create(sizeof(IOTHUB_DEVICE_HANDLE)); |
Azure.IoT Build | 19:d4f9e872bf3e | 740 | if (handleData == NULL || handleData->perDeviceList == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 741 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 742 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_010: [ If creating the list fails, then IoTHubTransportHttp_Create shall fail and return NULL. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 743 | result = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 744 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 745 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 746 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 747 | result = true; |
Azure.IoT Build | 19:d4f9e872bf3e | 748 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 749 | return result; |
AzureIoTClient | 6:73793bae15ba | 750 | } |
AzureIoTClient | 6:73793bae15ba | 751 | |
Azure.IoT Build | 13:848d52f93daf | 752 | |
AzureIoTClient | 18:ab990f6aa61f | 753 | static TRANSPORT_LL_HANDLE IoTHubTransportHttp_Create(const IOTHUBTRANSPORT_CONFIG* config) |
AzureIoTClient | 6:73793bae15ba | 754 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 755 | HTTPTRANSPORT_HANDLE_DATA* result; |
Azure.IoT Build | 19:d4f9e872bf3e | 756 | if (config == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 757 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 758 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_001: [If parameter config is NULL, then IoTHubTransportHttp_Create shall return NULL.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 759 | LogError("invalid arg (configuration is missing)"); |
Azure.IoT Build | 19:d4f9e872bf3e | 760 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 761 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 762 | else if (config->upperConfig == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 763 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 764 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_002: [ If field transportConfig is NULL, then IoTHubTransportHttp_Create shall return NULL. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 765 | LogError("invalid arg (upperConfig is NULL)"); |
Azure.IoT Build | 19:d4f9e872bf3e | 766 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 767 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 768 | else if (config->upperConfig->protocol == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 769 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 770 | /*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 | 771 | LogError("invalid arg (protocol is NULL)"); |
Azure.IoT Build | 19:d4f9e872bf3e | 772 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 773 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 774 | else if (config->upperConfig->iotHubName == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 775 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 776 | /*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 | 777 | LogError("invalid arg (iotHubName is NULL)"); |
Azure.IoT Build | 19:d4f9e872bf3e | 778 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 779 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 780 | else if (config->upperConfig->iotHubSuffix == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 781 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 782 | /*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 | 783 | LogError("invalid arg (iotHubSuffix is NULL)"); |
Azure.IoT Build | 19:d4f9e872bf3e | 784 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 785 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 786 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 787 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 788 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_130: [ IoTHubTransportHttp_Create shall allocate memory for the handle. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 789 | result = (HTTPTRANSPORT_HANDLE_DATA*)malloc(sizeof(HTTPTRANSPORT_HANDLE_DATA)); |
Azure.IoT Build | 19:d4f9e872bf3e | 790 | if (result == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 791 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 792 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_131: [ If allocation fails, IoTHubTransportHttp_Create shall fail and return NULL. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 793 | LogError("unable to malloc"); |
Azure.IoT Build | 19:d4f9e872bf3e | 794 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 795 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 796 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 797 | bool was_hostName_ok = create_hostName(result, config); |
Azure.IoT Build | 19:d4f9e872bf3e | 798 | bool was_httpApiExHandle_ok = was_hostName_ok && create_httpApiExHandle(result, config); |
Azure.IoT Build | 19:d4f9e872bf3e | 799 | bool was_perDeviceList_ok = was_httpApiExHandle_ok && create_perDeviceList(result); |
Azure.IoT Build | 13:848d52f93daf | 800 | |
AzureIoTClient | 6:73793bae15ba | 801 | |
Azure.IoT Build | 19:d4f9e872bf3e | 802 | if (was_perDeviceList_ok) |
Azure.IoT Build | 19:d4f9e872bf3e | 803 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 804 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_011: [ Otherwise, IoTHubTransportHttp_Create shall succeed and return a non-NULL value. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 805 | result->doBatchedTransfers = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 806 | result->getMinimumPollingTime = DEFAULT_GETMINIMUMPOLLINGTIME; |
Azure.IoT Build | 19:d4f9e872bf3e | 807 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 808 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 809 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 810 | if (was_httpApiExHandle_ok) destroy_httpApiExHandle(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 811 | if (was_hostName_ok) destroy_hostName(result); |
AzureIoTClient | 6:73793bae15ba | 812 | |
Azure.IoT Build | 19:d4f9e872bf3e | 813 | free(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 814 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 815 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 816 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 817 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 818 | return result; |
AzureIoTClient | 6:73793bae15ba | 819 | } |
AzureIoTClient | 6:73793bae15ba | 820 | |
AzureIoTClient | 18:ab990f6aa61f | 821 | static void IoTHubTransportHttp_Destroy(TRANSPORT_LL_HANDLE handle) |
AzureIoTClient | 6:73793bae15ba | 822 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 823 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_012: [ IoTHubTransportHttp_Destroy shall do nothing is handle is NULL. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 824 | if (handle != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 825 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 826 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
Azure.IoT Build | 19:d4f9e872bf3e | 827 | IOTHUB_DEVICE_HANDLE* listItem; |
Azure.IoT Build | 13:848d52f93daf | 828 | |
Azure.IoT Build | 19:d4f9e872bf3e | 829 | size_t deviceListSize = VECTOR_size(handleData->perDeviceList); |
Azure.IoT Build | 13:848d52f93daf | 830 | |
Azure.IoT Build | 19:d4f9e872bf3e | 831 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_013: [ Otherwise, IoTHubTransportHttp_Destroy shall free all the resources currently in use. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 832 | for (size_t i = 0; i < deviceListSize; i++) |
Azure.IoT Build | 19:d4f9e872bf3e | 833 | { |
AzureIoTClient | 21:b601705c82a8 | 834 | listItem = (IOTHUB_DEVICE_HANDLE *) VECTOR_element(handleData->perDeviceList, i); |
Azure.IoT Build | 19:d4f9e872bf3e | 835 | HTTPTRANSPORT_PERDEVICE_DATA* perDeviceItem = (HTTPTRANSPORT_PERDEVICE_DATA*)(*listItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 836 | destroy_perDeviceData(perDeviceItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 837 | free(perDeviceItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 838 | } |
Azure.IoT Build | 13:848d52f93daf | 839 | |
AzureIoTClient | 21:b601705c82a8 | 840 | destroy_hostName((HTTPTRANSPORT_HANDLE_DATA *) handle); |
AzureIoTClient | 21:b601705c82a8 | 841 | destroy_httpApiExHandle((HTTPTRANSPORT_HANDLE_DATA *) handle); |
AzureIoTClient | 21:b601705c82a8 | 842 | destroy_perDeviceList((HTTPTRANSPORT_HANDLE_DATA *)handle); |
Azure.IoT Build | 19:d4f9e872bf3e | 843 | free(handle); |
Azure.IoT Build | 19:d4f9e872bf3e | 844 | } |
AzureIoTClient | 6:73793bae15ba | 845 | } |
AzureIoTClient | 6:73793bae15ba | 846 | |
AzureIoTClient | 18:ab990f6aa61f | 847 | static int IoTHubTransportHttp_Subscribe(IOTHUB_DEVICE_HANDLE handle) |
AzureIoTClient | 6:73793bae15ba | 848 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 849 | int result; |
Azure.IoT Build | 19:d4f9e872bf3e | 850 | if (handle == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 851 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 852 | /*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 | 853 | LogError("invalid arg passed to IoTHubTransportHttp_Subscribe"); |
Azure.IoT Build | 19:d4f9e872bf3e | 854 | result = __LINE__; |
Azure.IoT Build | 19:d4f9e872bf3e | 855 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 856 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 857 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 858 | /*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 | 859 | IOTHUB_DEVICE_HANDLE* listItem = get_perDeviceDataItem(handle); |
Azure.IoT Build | 13:848d52f93daf | 860 | |
Azure.IoT Build | 19:d4f9e872bf3e | 861 | if (listItem == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 862 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 863 | /*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 | 864 | LogError("did not find device in transport handle"); |
Azure.IoT Build | 19:d4f9e872bf3e | 865 | result = __LINE__; |
Azure.IoT Build | 19:d4f9e872bf3e | 866 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 867 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 868 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 869 | HTTPTRANSPORT_PERDEVICE_DATA * perDeviceItem; |
Azure.IoT Build | 13:848d52f93daf | 870 | |
Azure.IoT Build | 19:d4f9e872bf3e | 871 | perDeviceItem = (HTTPTRANSPORT_PERDEVICE_DATA *)(*listItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 872 | /*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 | 873 | perDeviceItem->DoWork_PullMessage = true; |
Azure.IoT Build | 19:d4f9e872bf3e | 874 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 875 | result = 0; |
Azure.IoT Build | 19:d4f9e872bf3e | 876 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 877 | return result; |
AzureIoTClient | 6:73793bae15ba | 878 | } |
AzureIoTClient | 6:73793bae15ba | 879 | |
AzureIoTClient | 18:ab990f6aa61f | 880 | static void IoTHubTransportHttp_Unsubscribe(IOTHUB_DEVICE_HANDLE handle) |
AzureIoTClient | 6:73793bae15ba | 881 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 882 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_107: [ If parameter deviceHandle is NULL then IoTHubTransportHttp_Unsubscribe shall fail do nothing. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 883 | if (handle != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 884 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 885 | /*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 | 886 | IOTHUB_DEVICE_HANDLE* listItem = get_perDeviceDataItem(handle); |
Azure.IoT Build | 19:d4f9e872bf3e | 887 | /*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 | 888 | if (listItem != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 889 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 890 | HTTPTRANSPORT_PERDEVICE_DATA * perDeviceItem = (HTTPTRANSPORT_PERDEVICE_DATA *)(*listItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 891 | /*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 | 892 | perDeviceItem->DoWork_PullMessage = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 893 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 894 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 895 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 896 | LogError("Device not found to unsuscribe."); |
Azure.IoT Build | 19:d4f9e872bf3e | 897 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 898 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 899 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 900 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 901 | LogError("Null handle passed to Unsuscribe."); |
Azure.IoT Build | 19:d4f9e872bf3e | 902 | } |
AzureIoTClient | 6:73793bae15ba | 903 | } |
AzureIoTClient | 6:73793bae15ba | 904 | |
AzureIoTClient | 6:73793bae15ba | 905 | /*produces a representation of the properties, if they exist*/ |
AzureIoTClient | 6:73793bae15ba | 906 | /*if they do not exist, produces ""*/ |
AzureIoTClient | 6:73793bae15ba | 907 | static int concat_Properties(STRING_HANDLE existing, MAP_HANDLE map, size_t* propertiesMessageSizeContribution) |
AzureIoTClient | 6:73793bae15ba | 908 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 909 | int result; |
Azure.IoT Build | 19:d4f9e872bf3e | 910 | const char*const* keys; |
Azure.IoT Build | 19:d4f9e872bf3e | 911 | const char*const* values; |
Azure.IoT Build | 19:d4f9e872bf3e | 912 | size_t count; |
Azure.IoT Build | 19:d4f9e872bf3e | 913 | if (Map_GetInternals(map, &keys, &values, &count) != MAP_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 914 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 915 | result = __LINE__; |
Azure.IoT Build | 19:d4f9e872bf3e | 916 | LogError("error while Map_GetInternals"); |
Azure.IoT Build | 19:d4f9e872bf3e | 917 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 918 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 919 | { |
AzureIoTClient | 6:73793bae15ba | 920 | |
Azure.IoT Build | 19:d4f9e872bf3e | 921 | if (count == 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 922 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 923 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_064: [If IoTHubMessage does not have properties, then "properties":{...} shall be missing from the payload*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 924 | /*no properties - do nothing with existing*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 925 | result = 0; |
Azure.IoT Build | 19:d4f9e872bf3e | 926 | *propertiesMessageSizeContribution = 0; |
Azure.IoT Build | 19:d4f9e872bf3e | 927 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 928 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 929 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 930 | /*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 | 931 | if (STRING_concat(existing, ",\"properties\":") != 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 932 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 933 | /*go ahead and return it*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 934 | result = __LINE__; |
Azure.IoT Build | 19:d4f9e872bf3e | 935 | LogError("failed STRING_concat"); |
Azure.IoT Build | 19:d4f9e872bf3e | 936 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 937 | else if (appendMapToJSON(existing, keys, values, count) != 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 938 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 939 | result = __LINE__; |
Azure.IoT Build | 19:d4f9e872bf3e | 940 | LogError("unable to append the properties"); |
Azure.IoT Build | 19:d4f9e872bf3e | 941 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 942 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 943 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 944 | /*all is fine*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 945 | size_t i; |
Azure.IoT Build | 19:d4f9e872bf3e | 946 | *propertiesMessageSizeContribution = 0; |
Azure.IoT Build | 19:d4f9e872bf3e | 947 | for (i = 0;i < count;i++) |
Azure.IoT Build | 19:d4f9e872bf3e | 948 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 949 | /*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 | 950 | *propertiesMessageSizeContribution += (strlen(keys[i]) + strlen(values[i]) + MAXIMUM_PROPERTY_OVERHEAD); |
Azure.IoT Build | 19:d4f9e872bf3e | 951 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 952 | result = 0; |
Azure.IoT Build | 19:d4f9e872bf3e | 953 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 954 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 955 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 956 | return result; |
AzureIoTClient | 6:73793bae15ba | 957 | } |
AzureIoTClient | 6:73793bae15ba | 958 | |
AzureIoTClient | 6:73793bae15ba | 959 | /*produces a JSON representation of the map : {"a": "value_of_a","b":"value_of_b"}*/ |
AzureIoTClient | 6:73793bae15ba | 960 | 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 | 961 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 962 | int result; |
Azure.IoT Build | 19:d4f9e872bf3e | 963 | if (STRING_concat(existing, "{") != 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 964 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 965 | /*go on and return it*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 966 | result = __LINE__; |
Azure.IoT Build | 19:d4f9e872bf3e | 967 | LogError("STRING_construct failed"); |
Azure.IoT Build | 19:d4f9e872bf3e | 968 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 969 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 970 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 971 | size_t i; |
Azure.IoT Build | 19:d4f9e872bf3e | 972 | for (i = 0; i < count; i++) |
Azure.IoT Build | 19:d4f9e872bf3e | 973 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 974 | if (!( |
Azure.IoT Build | 19:d4f9e872bf3e | 975 | (STRING_concat(existing, (i == 0) ? "\"" IOTHUB_APP_PREFIX : ",\"" IOTHUB_APP_PREFIX) == 0) && |
Azure.IoT Build | 19:d4f9e872bf3e | 976 | (STRING_concat(existing, keys[i]) == 0) && |
Azure.IoT Build | 19:d4f9e872bf3e | 977 | (STRING_concat(existing, "\":\"") == 0) && |
Azure.IoT Build | 19:d4f9e872bf3e | 978 | (STRING_concat(existing, values[i]) == 0) && |
Azure.IoT Build | 19:d4f9e872bf3e | 979 | (STRING_concat(existing, "\"") == 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 980 | )) |
Azure.IoT Build | 19:d4f9e872bf3e | 981 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 982 | LogError("unable to STRING_concat"); |
Azure.IoT Build | 19:d4f9e872bf3e | 983 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 984 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 985 | } |
AzureIoTClient | 6:73793bae15ba | 986 | |
Azure.IoT Build | 19:d4f9e872bf3e | 987 | if (i < count) |
Azure.IoT Build | 19:d4f9e872bf3e | 988 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 989 | result = __LINE__; |
Azure.IoT Build | 19:d4f9e872bf3e | 990 | /*error, let it go through*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 991 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 992 | else if (STRING_concat(existing, "}") != 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 993 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 994 | result = __LINE__; |
Azure.IoT Build | 19:d4f9e872bf3e | 995 | LogError("unable to STRING_concat"); |
Azure.IoT Build | 19:d4f9e872bf3e | 996 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 997 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 998 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 999 | /*all is fine*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1000 | result = 0; |
Azure.IoT Build | 19:d4f9e872bf3e | 1001 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1002 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1003 | return result; |
AzureIoTClient | 6:73793bae15ba | 1004 | } |
AzureIoTClient | 6:73793bae15ba | 1005 | |
AzureIoTClient | 6:73793bae15ba | 1006 | /*makes the following string:{"body":"base64 encoding of the message content"[,"properties":{"a":"valueOfA"}]}*/ |
AzureIoTClient | 6:73793bae15ba | 1007 | /*return NULL if there was a failure, or a non-NULL STRING_HANDLE that contains the intended data*/ |
AzureIoTClient | 6:73793bae15ba | 1008 | static STRING_HANDLE make1EventJSONitem(PDLIST_ENTRY item, size_t *messageSizeContribution) |
AzureIoTClient | 6:73793bae15ba | 1009 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1010 | STRING_HANDLE result; /*temp wants to contain :{"body":"base64 encoding of the message content"[,"properties":{"a":"valueOfA"}]}*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1011 | IOTHUB_MESSAGE_LIST* message = containingRecord(item, IOTHUB_MESSAGE_LIST, entry); |
Azure.IoT Build | 19:d4f9e872bf3e | 1012 | IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message->messageHandle); |
AzureIoTClient | 17:7ef88cde801f | 1013 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1014 | switch (contentType) |
Azure.IoT Build | 19:d4f9e872bf3e | 1015 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1016 | case IOTHUBMESSAGE_BYTEARRAY: |
Azure.IoT Build | 19:d4f9e872bf3e | 1017 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1018 | result = STRING_construct("{\"body\":\""); |
Azure.IoT Build | 19:d4f9e872bf3e | 1019 | if (result == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1020 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1021 | LogError("unable to STRING_construct"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1022 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1023 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1024 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1025 | const unsigned char* source; |
Azure.IoT Build | 19:d4f9e872bf3e | 1026 | size_t size; |
AzureIoTClient | 6:73793bae15ba | 1027 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1028 | if (IoTHubMessage_GetByteArray(message->messageHandle, &source, &size) != IOTHUB_MESSAGE_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1029 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1030 | LogError("unable to get the data for the message."); |
Azure.IoT Build | 19:d4f9e872bf3e | 1031 | STRING_delete(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 1032 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 1033 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1034 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1035 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1036 | STRING_HANDLE encoded = Base64_Encode_Bytes(source, size); |
Azure.IoT Build | 19:d4f9e872bf3e | 1037 | if (encoded == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1038 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1039 | LogError("unable to Base64_Encode_Bytes."); |
Azure.IoT Build | 19:d4f9e872bf3e | 1040 | STRING_delete(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 1041 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 1042 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1043 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1044 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1045 | size_t propertiesSize; |
Azure.IoT Build | 19:d4f9e872bf3e | 1046 | if (!( |
Azure.IoT Build | 19:d4f9e872bf3e | 1047 | (STRING_concat_with_STRING(result, encoded) == 0) && |
Azure.IoT Build | 19:d4f9e872bf3e | 1048 | (STRING_concat(result, "\"") == 0) && /*\" because closing value*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1049 | (concat_Properties(result, IoTHubMessage_Properties(message->messageHandle), &propertiesSize) == 0) && |
Azure.IoT Build | 19:d4f9e872bf3e | 1050 | (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 | 1051 | )) |
Azure.IoT Build | 19:d4f9e872bf3e | 1052 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1053 | STRING_delete(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 1054 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 1055 | LogError("unable to STRING_concat_with_STRING."); |
Azure.IoT Build | 19:d4f9e872bf3e | 1056 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1057 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1058 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1059 | /*all is fine... */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1060 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_062: [The message size is computed from the length of the payload + 384.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1061 | *messageSizeContribution = size + MAXIMUM_PAYLOAD_OVERHEAD + propertiesSize; |
Azure.IoT Build | 19:d4f9e872bf3e | 1062 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1063 | STRING_delete(encoded); |
Azure.IoT Build | 19:d4f9e872bf3e | 1064 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1065 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1066 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1067 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 1068 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1069 | /*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 | 1070 | case IOTHUBMESSAGE_STRING: |
Azure.IoT Build | 19:d4f9e872bf3e | 1071 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1072 | result = STRING_construct("{\"body\":"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1073 | if (result == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1074 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1075 | LogError("unable to STRING_construct"); |
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 | const char* source = IoTHubMessage_GetString(message->messageHandle); |
Azure.IoT Build | 19:d4f9e872bf3e | 1080 | if (source == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1081 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1082 | LogError("unable to IoTHubMessage_GetString"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1083 | STRING_delete(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 1084 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 1085 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1086 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1087 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1088 | STRING_HANDLE asJson = STRING_new_JSON(source); |
Azure.IoT Build | 19:d4f9e872bf3e | 1089 | if (asJson == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1090 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1091 | LogError("unable to STRING_new_JSON"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1092 | STRING_delete(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 1093 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 1094 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1095 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1096 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1097 | size_t propertiesSize; |
Azure.IoT Build | 19:d4f9e872bf3e | 1098 | if (!( |
Azure.IoT Build | 19:d4f9e872bf3e | 1099 | (STRING_concat_with_STRING(result, asJson) == 0) && |
Azure.IoT Build | 19:d4f9e872bf3e | 1100 | (STRING_concat(result, ",\"base64Encoded\":false") == 0) && |
Azure.IoT Build | 19:d4f9e872bf3e | 1101 | (concat_Properties(result, IoTHubMessage_Properties(message->messageHandle), &propertiesSize) == 0) && |
Azure.IoT Build | 19:d4f9e872bf3e | 1102 | (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 | 1103 | )) |
Azure.IoT Build | 19:d4f9e872bf3e | 1104 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1105 | LogError("unable to STRING_concat_with_STRING"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1106 | STRING_delete(result); |
Azure.IoT Build | 19:d4f9e872bf3e | 1107 | result = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 1108 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1109 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1110 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1111 | /*result has the intended content*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1112 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_062: [The message size is computed from the length of the payload + 384.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1113 | *messageSizeContribution = strlen(source) + MAXIMUM_PAYLOAD_OVERHEAD + propertiesSize; |
Azure.IoT Build | 19:d4f9e872bf3e | 1114 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1115 | STRING_delete(asJson); |
Azure.IoT Build | 19:d4f9e872bf3e | 1116 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1117 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1118 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1119 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 1120 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1121 | default: |
Azure.IoT Build | 19:d4f9e872bf3e | 1122 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1123 | LogError("an unknown message type was encountered (%d)", contentType); |
Azure.IoT Build | 19:d4f9e872bf3e | 1124 | result = NULL; /*unknown message type*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1125 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 1126 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1127 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1128 | return result; |
AzureIoTClient | 6:73793bae15ba | 1129 | } |
AzureIoTClient | 6:73793bae15ba | 1130 | |
AzureIoTClient | 6:73793bae15ba | 1131 | #define MAKE_PAYLOAD_RESULT_VALUES \ |
AzureIoTClient | 6:73793bae15ba | 1132 | MAKE_PAYLOAD_OK, /*returned when there is a payload to be later send by HTTP*/ \ |
AzureIoTClient | 6:73793bae15ba | 1133 | MAKE_PAYLOAD_NO_ITEMS, /*returned when there are no items to be send*/ \ |
AzureIoTClient | 6:73793bae15ba | 1134 | MAKE_PAYLOAD_ERROR, /*returned when there were errors*/ \ |
AzureIoTClient | 6:73793bae15ba | 1135 | MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT /*returned when the first item doesn't fit*/ |
AzureIoTClient | 6:73793bae15ba | 1136 | |
AzureIoTClient | 6:73793bae15ba | 1137 | DEFINE_ENUM(MAKE_PAYLOAD_RESULT, MAKE_PAYLOAD_RESULT_VALUES); |
AzureIoTClient | 6:73793bae15ba | 1138 | |
AzureIoTClient | 6:73793bae15ba | 1139 | /*this function assembles several {"body":"base64 encoding of the message content"," base64Encoded": true} into 1 payload*/ |
Azure.IoT Build | 13:848d52f93daf | 1140 | /*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 | 1141 | static MAKE_PAYLOAD_RESULT makePayload(HTTPTRANSPORT_PERDEVICE_DATA* deviceData, STRING_HANDLE* payload) |
AzureIoTClient | 6:73793bae15ba | 1142 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1143 | MAKE_PAYLOAD_RESULT result; |
Azure.IoT Build | 19:d4f9e872bf3e | 1144 | size_t allMessagesSize = 0; |
Azure.IoT Build | 19:d4f9e872bf3e | 1145 | *payload = STRING_construct("["); |
Azure.IoT Build | 19:d4f9e872bf3e | 1146 | if (*payload == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1147 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1148 | LogError("unable to STRING_construct"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1149 | result = MAKE_PAYLOAD_ERROR; |
Azure.IoT Build | 19:d4f9e872bf3e | 1150 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1151 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1152 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1153 | bool isFirst = true; |
Azure.IoT Build | 19:d4f9e872bf3e | 1154 | PDLIST_ENTRY actual; |
Azure.IoT Build | 19:d4f9e872bf3e | 1155 | bool keepGoing = true; /*keepGoing gets sometimes to false from within the loop*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1156 | /*either all the items enter the list or only some*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1157 | result = MAKE_PAYLOAD_OK; /*optimistically initializing it*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1158 | while (keepGoing && ((actual = deviceData->waitingToSend->Flink) != deviceData->waitingToSend)) |
Azure.IoT Build | 19:d4f9e872bf3e | 1159 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1160 | size_t messageSize; |
Azure.IoT Build | 19:d4f9e872bf3e | 1161 | STRING_HANDLE temp = make1EventJSONitem(actual, &messageSize); |
Azure.IoT Build | 19:d4f9e872bf3e | 1162 | if (isFirst) |
Azure.IoT Build | 19:d4f9e872bf3e | 1163 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1164 | isFirst = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1165 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_067: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1166 | if (temp == NULL) /*first item failed to create, nothing to send*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1167 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1168 | result = MAKE_PAYLOAD_ERROR; |
Azure.IoT Build | 19:d4f9e872bf3e | 1169 | STRING_delete(*payload); |
Azure.IoT Build | 19:d4f9e872bf3e | 1170 | *payload = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 1171 | keepGoing = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1172 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1173 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1174 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1175 | /*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 | 1176 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_061: [The message size shall be limited to 255KB - 1 byte.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1177 | if (messageSize > MAXIMUM_MESSAGE_SIZE) |
Azure.IoT Build | 19:d4f9e872bf3e | 1178 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1179 | PDLIST_ENTRY head = DList_RemoveHeadList(deviceData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1180 | DList_InsertTailList(&(deviceData->eventConfirmations), head); |
Azure.IoT Build | 19:d4f9e872bf3e | 1181 | result = MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT; |
Azure.IoT Build | 19:d4f9e872bf3e | 1182 | STRING_delete(*payload); |
Azure.IoT Build | 19:d4f9e872bf3e | 1183 | *payload = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 1184 | keepGoing = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1185 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1186 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1187 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1188 | if (STRING_concat_with_STRING(*payload, temp) != 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 1189 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1190 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_067: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1191 | result = MAKE_PAYLOAD_ERROR; |
Azure.IoT Build | 19:d4f9e872bf3e | 1192 | STRING_delete(*payload); |
Azure.IoT Build | 19:d4f9e872bf3e | 1193 | *payload = NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 1194 | keepGoing = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1195 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1196 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1197 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1198 | /*first item was put nicely in the payload*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1199 | PDLIST_ENTRY head = DList_RemoveHeadList(deviceData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1200 | DList_InsertTailList(&(deviceData->eventConfirmations), head); |
Azure.IoT Build | 19:d4f9e872bf3e | 1201 | allMessagesSize += messageSize; |
Azure.IoT Build | 19:d4f9e872bf3e | 1202 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1203 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1204 | STRING_delete(temp); |
Azure.IoT Build | 19:d4f9e872bf3e | 1205 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1206 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1207 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1208 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1209 | /*there is at least 1 item already in the payload*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1210 | if (temp == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1211 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1212 | /*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 | 1213 | /*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 | 1214 | result = MAKE_PAYLOAD_OK; |
Azure.IoT Build | 19:d4f9e872bf3e | 1215 | keepGoing = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1216 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1217 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1218 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1219 | if (allMessagesSize + messageSize > MAXIMUM_MESSAGE_SIZE) |
Azure.IoT Build | 19:d4f9e872bf3e | 1220 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1221 | /*this item doesn't make it to the payload, but the payload is valid so far*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1222 | /*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 | 1223 | result = MAKE_PAYLOAD_OK; |
Azure.IoT Build | 19:d4f9e872bf3e | 1224 | keepGoing = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1225 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1226 | else if (STRING_concat_with_STRING(*payload, temp) != 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 1227 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1228 | /*should still send what there is so far...*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1229 | /*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 | 1230 | result = MAKE_PAYLOAD_OK; |
Azure.IoT Build | 19:d4f9e872bf3e | 1231 | keepGoing = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1232 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1233 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1234 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1235 | /*cool, the payload made it there, let's continue... */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1236 | PDLIST_ENTRY head = DList_RemoveHeadList(deviceData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1237 | DList_InsertTailList(&(deviceData->eventConfirmations), head); |
Azure.IoT Build | 19:d4f9e872bf3e | 1238 | allMessagesSize += messageSize; |
Azure.IoT Build | 19:d4f9e872bf3e | 1239 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1240 | STRING_delete(temp); |
Azure.IoT Build | 19:d4f9e872bf3e | 1241 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1242 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1243 | } |
AzureIoTClient | 6:73793bae15ba | 1244 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1245 | /*closing the payload*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1246 | if (result == MAKE_PAYLOAD_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1247 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1248 | ((char*)STRING_c_str(*payload))[STRING_length(*payload) - 1] = ']'; /*TODO - do this in STRING_HANDLE*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1249 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1250 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1251 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1252 | /*no need to close anything*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1253 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1254 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1255 | return result; |
AzureIoTClient | 6:73793bae15ba | 1256 | } |
AzureIoTClient | 6:73793bae15ba | 1257 | |
AzureIoTClient | 6:73793bae15ba | 1258 | static void reversePutListBackIn(PDLIST_ENTRY source, PDLIST_ENTRY destination) |
AzureIoTClient | 6:73793bae15ba | 1259 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1260 | /*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 | 1261 | DList_AppendTailList(destination->Flink, source); |
Azure.IoT Build | 19:d4f9e872bf3e | 1262 | DList_RemoveEntryList(source); |
Azure.IoT Build | 19:d4f9e872bf3e | 1263 | DList_InitializeListHead(source); |
AzureIoTClient | 6:73793bae15ba | 1264 | } |
AzureIoTClient | 6:73793bae15ba | 1265 | |
Azure.IoT Build | 13:848d52f93daf | 1266 | static void DoEvent(HTTPTRANSPORT_HANDLE_DATA* handleData, HTTPTRANSPORT_PERDEVICE_DATA* deviceData, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 6:73793bae15ba | 1267 | { |
Azure.IoT Build | 13:848d52f93daf | 1268 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1269 | if (DList_IsListEmpty(deviceData->waitingToSend)) |
Azure.IoT Build | 19:d4f9e872bf3e | 1270 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1271 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_060: [If the list is empty then IoTHubTransportHttp_DoWork shall proceed to the following action.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1272 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1273 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1274 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1275 | /*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 | 1276 | if (handleData->doBatchedTransfers) |
Azure.IoT Build | 19:d4f9e872bf3e | 1277 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1278 | /*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 | 1279 | if (HTTPHeaders_ReplaceHeaderNameValuePair(deviceData->eventHTTPrequestHeaders, CONTENT_TYPE, APPLICATION_VND_MICROSOFT_IOTHUB_JSON) != HTTP_HEADERS_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1280 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1281 | /*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 | 1282 | LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1283 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1284 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1285 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1286 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_059: [It shall inspect the "waitingToSend" DLIST passed in config structure.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1287 | STRING_HANDLE payload; |
Azure.IoT Build | 19:d4f9e872bf3e | 1288 | switch (makePayload(deviceData, &payload)) |
Azure.IoT Build | 19:d4f9e872bf3e | 1289 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1290 | case MAKE_PAYLOAD_OK: |
Azure.IoT Build | 19:d4f9e872bf3e | 1291 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1292 | /*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 | 1293 | BUFFER_HANDLE temp = BUFFER_new(); |
Azure.IoT Build | 19:d4f9e872bf3e | 1294 | if (temp == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1295 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1296 | LogError("unable to BUFFER_new"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1297 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_067: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1298 | reversePutListBackIn(&(deviceData->eventConfirmations), deviceData->waitingToSend); |
Azure.IoT Build | 19:d4f9e872bf3e | 1299 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1300 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1301 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1302 | if (BUFFER_build(temp, (const unsigned char*)STRING_c_str(payload), STRING_length(payload)) != 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 1303 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1304 | LogError("unable to BUFFER_build"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1305 | //items go back to waitingToSend |
Azure.IoT Build | 19:d4f9e872bf3e | 1306 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_067: [If there is no valid payload, IoTHubTransportHttp_DoWork shall advance to the next activity.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1307 | reversePutListBackIn(&(deviceData->eventConfirmations), deviceData->waitingToSend); |
Azure.IoT Build | 19:d4f9e872bf3e | 1308 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1309 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1310 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1311 | unsigned int statusCode; |
Azure.IoT Build | 19:d4f9e872bf3e | 1312 | HTTPAPIEX_RESULT r; |
Azure.IoT Build | 19:d4f9e872bf3e | 1313 | if ((r = HTTPAPIEX_SAS_ExecuteRequest( |
Azure.IoT Build | 19:d4f9e872bf3e | 1314 | deviceData->sasObject, |
Azure.IoT Build | 19:d4f9e872bf3e | 1315 | handleData->httpApiExHandle, |
Azure.IoT Build | 19:d4f9e872bf3e | 1316 | HTTPAPI_REQUEST_POST, |
Azure.IoT Build | 19:d4f9e872bf3e | 1317 | STRING_c_str(deviceData->eventHTTPrelativePath), |
Azure.IoT Build | 19:d4f9e872bf3e | 1318 | deviceData->eventHTTPrequestHeaders, |
Azure.IoT Build | 19:d4f9e872bf3e | 1319 | temp, |
Azure.IoT Build | 19:d4f9e872bf3e | 1320 | &statusCode, |
Azure.IoT Build | 19:d4f9e872bf3e | 1321 | NULL, |
Azure.IoT Build | 19:d4f9e872bf3e | 1322 | NULL |
Azure.IoT Build | 19:d4f9e872bf3e | 1323 | )) != HTTPAPIEX_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1324 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1325 | LogError("unable to HTTPAPIEX_ExecuteRequest"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1326 | //items go back to waitingToSend |
Azure.IoT Build | 19:d4f9e872bf3e | 1327 | /*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 | 1328 | reversePutListBackIn(&(deviceData->eventConfirmations), deviceData->waitingToSend); |
Azure.IoT Build | 19:d4f9e872bf3e | 1329 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1330 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1331 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1332 | if (statusCode < 300) |
Azure.IoT Build | 19:d4f9e872bf3e | 1333 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1334 | /*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 | 1335 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(deviceData->eventConfirmations), IOTHUB_CLIENT_CONFIRMATION_OK); |
Azure.IoT Build | 19:d4f9e872bf3e | 1336 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1337 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1338 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1339 | //items go back to waitingToSend |
Azure.IoT Build | 19:d4f9e872bf3e | 1340 | /*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 | 1341 | LogError("unexpected HTTP status code (%u)", statusCode); |
Azure.IoT Build | 19:d4f9e872bf3e | 1342 | reversePutListBackIn(&(deviceData->eventConfirmations), deviceData->waitingToSend); |
Azure.IoT Build | 19:d4f9e872bf3e | 1343 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1344 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1345 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1346 | BUFFER_delete(temp); |
Azure.IoT Build | 19:d4f9e872bf3e | 1347 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1348 | STRING_delete(payload); |
Azure.IoT Build | 19:d4f9e872bf3e | 1349 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 1350 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1351 | case MAKE_PAYLOAD_FIRST_ITEM_DOES_NOT_FIT: |
Azure.IoT Build | 19:d4f9e872bf3e | 1352 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1353 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(deviceData->eventConfirmations), IOTHUB_CLIENT_CONFIRMATION_ERROR); /*takes care of emptying the list too*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1354 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 1355 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1356 | case MAKE_PAYLOAD_ERROR: |
Azure.IoT Build | 19:d4f9e872bf3e | 1357 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1358 | LogError("unrecoverable errors while building a batch message"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1359 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 1360 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1361 | case MAKE_PAYLOAD_NO_ITEMS: |
Azure.IoT Build | 19:d4f9e872bf3e | 1362 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1363 | /*do nothing*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1364 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 1365 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1366 | default: |
Azure.IoT Build | 19:d4f9e872bf3e | 1367 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1368 | LogError("internal error: switch's default branch reached when never intended"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1369 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 1370 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1371 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1372 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1373 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1374 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1375 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1376 | const unsigned char* messageContent=NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 1377 | size_t messageSize=0; |
Azure.IoT Build | 19:d4f9e872bf3e | 1378 | size_t originalMessageSize=0; |
Azure.IoT Build | 19:d4f9e872bf3e | 1379 | IOTHUB_MESSAGE_LIST* message = containingRecord(deviceData->waitingToSend->Flink, IOTHUB_MESSAGE_LIST, entry); |
Azure.IoT Build | 19:d4f9e872bf3e | 1380 | IOTHUBMESSAGE_CONTENT_TYPE contentType = IoTHubMessage_GetContentType(message->messageHandle); |
AzureIoTClient | 6:73793bae15ba | 1381 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1382 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_073: [The message size is computed from the length of the payload + 384.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1383 | if (!( |
Azure.IoT Build | 19:d4f9e872bf3e | 1384 | (((contentType == IOTHUBMESSAGE_BYTEARRAY) && |
Azure.IoT Build | 19:d4f9e872bf3e | 1385 | (IoTHubMessage_GetByteArray(message->messageHandle, &messageContent, &originalMessageSize)==IOTHUB_MESSAGE_OK)) ? (messageSize= originalMessageSize + MAXIMUM_PAYLOAD_OVERHEAD, 1): 0) |
AzureIoTClient | 17:7ef88cde801f | 1386 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1387 | || |
AzureIoTClient | 6:73793bae15ba | 1388 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1389 | ((contentType == IOTHUBMESSAGE_STRING) && ( |
Azure.IoT Build | 19:d4f9e872bf3e | 1390 | messageContent = (const unsigned char*)IoTHubMessage_GetString(message->messageHandle), |
Azure.IoT Build | 19:d4f9e872bf3e | 1391 | (messageSize = MAXIMUM_PAYLOAD_OVERHEAD + (originalMessageSize = ((messageContent == NULL)?0:strlen((const char*)messageContent)))), |
Azure.IoT Build | 19:d4f9e872bf3e | 1392 | messageContent!=NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1393 | ) |
Azure.IoT Build | 19:d4f9e872bf3e | 1394 | )) |
Azure.IoT Build | 19:d4f9e872bf3e | 1395 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1396 | LogError("unable to get the message content"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1397 | /*go on...*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1398 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1399 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1400 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1401 | /*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 | 1402 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_072: [The message size shall be limited to 255KB -1 bytes.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1403 | if (messageSize > MAXIMUM_MESSAGE_SIZE) |
Azure.IoT Build | 19:d4f9e872bf3e | 1404 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1405 | PDLIST_ENTRY head = DList_RemoveHeadList(deviceData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1406 | DList_InsertTailList(&(deviceData->eventConfirmations), head); |
Azure.IoT Build | 19:d4f9e872bf3e | 1407 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(deviceData->eventConfirmations), IOTHUB_CLIENT_CONFIRMATION_ERROR); /*takes care of emptying the list too*/ |
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_071: [If option SetBatching is false then _Dowork shall send individual event message as specced below.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1412 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_076: [A clone of the event HTTP request headers shall be created.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1413 | HTTP_HEADERS_HANDLE clonedEventHTTPrequestHeaders = HTTPHeaders_Clone(deviceData->eventHTTPrequestHeaders); |
Azure.IoT Build | 19:d4f9e872bf3e | 1414 | if (clonedEventHTTPrequestHeaders == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1415 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1416 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1417 | LogError("HTTPHeaders_Clone failed"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1418 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1419 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1420 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1421 | /*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 | 1422 | if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, CONTENT_TYPE, APPLICATION_OCTET_STREAM) != HTTP_HEADERS_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1423 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1424 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1425 | LogError("HTTPHeaders_ReplaceHeaderNameValuePair failed"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1426 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1427 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1428 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1429 | /*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 | 1430 | MAP_HANDLE map = IoTHubMessage_Properties(message->messageHandle); |
Azure.IoT Build | 19:d4f9e872bf3e | 1431 | const char*const* keys; |
Azure.IoT Build | 19:d4f9e872bf3e | 1432 | const char*const* values; |
Azure.IoT Build | 19:d4f9e872bf3e | 1433 | size_t count; |
Azure.IoT Build | 19:d4f9e872bf3e | 1434 | if (Map_GetInternals(map, &keys, &values, &count) != MAP_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1435 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1436 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_078: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1437 | LogError("unable to Map_GetInternals"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1438 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1439 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1440 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1441 | size_t i; |
Azure.IoT Build | 19:d4f9e872bf3e | 1442 | bool goOn = true; |
Azure.IoT Build | 19:d4f9e872bf3e | 1443 | const char* msgId; |
Azure.IoT Build | 19:d4f9e872bf3e | 1444 | const char* corrId; |
AzureIoTClient | 17:7ef88cde801f | 1445 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1446 | for (i = 0; (i < count) && goOn; i++) |
Azure.IoT Build | 19:d4f9e872bf3e | 1447 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1448 | /*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 | 1449 | messageSize += (strlen(values[i]) + strlen(keys[i]) + MAXIMUM_PROPERTY_OVERHEAD); |
Azure.IoT Build | 19:d4f9e872bf3e | 1450 | if (messageSize > MAXIMUM_MESSAGE_SIZE) |
Azure.IoT Build | 19:d4f9e872bf3e | 1451 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1452 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_072: [The message size shall be limited to 255KB -1 bytes.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1453 | PDLIST_ENTRY head = DList_RemoveHeadList(deviceData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1454 | DList_InsertTailList(&(deviceData->eventConfirmations), head); |
Azure.IoT Build | 19:d4f9e872bf3e | 1455 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(deviceData->eventConfirmations), IOTHUB_CLIENT_CONFIRMATION_ERROR); /*takes care of emptying the list too*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1456 | goOn = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1457 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1458 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1459 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1460 | STRING_HANDLE temp = STRING_construct(IOTHUB_APP_PREFIX); |
Azure.IoT Build | 19:d4f9e872bf3e | 1461 | if (temp == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1462 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1463 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1464 | LogError("unable to STRING_construct"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1465 | goOn = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1466 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1467 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1468 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1469 | if (STRING_concat(temp, keys[i]) != 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 1470 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1471 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1472 | LogError("unable to STRING_concat"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1473 | goOn = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1474 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1475 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1476 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1477 | if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, STRING_c_str(temp), values[i]) != HTTP_HEADERS_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1478 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1479 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1480 | LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1481 | goOn = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1482 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1483 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1484 | STRING_delete(temp); |
Azure.IoT Build | 19:d4f9e872bf3e | 1485 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1486 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1487 | } |
AzureIoTClient | 6:73793bae15ba | 1488 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1489 | // Add the Message Id and the Correlation Id |
Azure.IoT Build | 19:d4f9e872bf3e | 1490 | msgId = IoTHubMessage_GetMessageId(message->messageHandle); |
Azure.IoT Build | 19:d4f9e872bf3e | 1491 | if (goOn && msgId != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1492 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1493 | if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, IOTHUB_MESSAGE_ID, msgId) != HTTP_HEADERS_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1494 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1495 | LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1496 | goOn = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1497 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1498 | } |
AzureIoTClient | 17:7ef88cde801f | 1499 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1500 | corrId = IoTHubMessage_GetCorrelationId(message->messageHandle); |
Azure.IoT Build | 19:d4f9e872bf3e | 1501 | if (goOn && corrId != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1502 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1503 | if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, IOTHUB_CORRELATION_ID, corrId) != HTTP_HEADERS_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1504 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1505 | LogError("unable to HTTPHeaders_ReplaceHeaderNameValuePair"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1506 | goOn = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1507 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1508 | } |
AzureIoTClient | 7:48f0f78cd3ef | 1509 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1510 | if (!goOn) |
Azure.IoT Build | 19:d4f9e872bf3e | 1511 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1512 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_079: [If any HTTP header operation fails, _DoWork shall advance to the next action.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1513 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1514 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1515 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1516 | BUFFER_HANDLE toBeSend = BUFFER_new(); |
Azure.IoT Build | 19:d4f9e872bf3e | 1517 | if (toBeSend == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1518 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1519 | LogError("unable to BUFFER_new"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1520 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1521 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1522 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1523 | if (BUFFER_build(toBeSend, messageContent, originalMessageSize) != 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 1524 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1525 | LogError("unable to BUFFER_build"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1526 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1527 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1528 | { |
AzureIoTClient | 20:b652f2e4cccf | 1529 | unsigned int statusCode = 0; |
Azure.IoT Build | 19:d4f9e872bf3e | 1530 | HTTPAPIEX_RESULT r; |
Azure.IoT Build | 19:d4f9e872bf3e | 1531 | if (deviceData->deviceSasToken != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1532 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1533 | /*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 | 1534 | if (HTTPHeaders_ReplaceHeaderNameValuePair(clonedEventHTTPrequestHeaders, "Authorization", STRING_c_str(deviceData->deviceSasToken)) != HTTP_HEADERS_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1535 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1536 | r = HTTPAPIEX_ERROR; |
Azure.IoT Build | 19:d4f9e872bf3e | 1537 | /*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 | 1538 | LogError("Unable to replace the old SAS Token."); |
Azure.IoT Build | 19:d4f9e872bf3e | 1539 | } |
AzureIoTClient | 7:48f0f78cd3ef | 1540 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1541 | /*Codes_SRS_TRANSPORTMULTITHTTP_03_003: [If a deviceSasToken exists, IoTHubTransportHttp_DoWork shall call HTTPAPIEX_ExecuteRequest passing the following parameters] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1542 | else if ((r = HTTPAPIEX_ExecuteRequest( |
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_ExecuteRequest."); |
Azure.IoT Build | 19:d4f9e872bf3e | 1554 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1555 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1556 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1557 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1558 | /*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 | 1559 | if ((r = HTTPAPIEX_SAS_ExecuteRequest( |
Azure.IoT Build | 19:d4f9e872bf3e | 1560 | deviceData->sasObject, |
Azure.IoT Build | 19:d4f9e872bf3e | 1561 | handleData->httpApiExHandle, |
Azure.IoT Build | 19:d4f9e872bf3e | 1562 | HTTPAPI_REQUEST_POST, |
Azure.IoT Build | 19:d4f9e872bf3e | 1563 | STRING_c_str(deviceData->eventHTTPrelativePath), |
Azure.IoT Build | 19:d4f9e872bf3e | 1564 | clonedEventHTTPrequestHeaders, |
Azure.IoT Build | 19:d4f9e872bf3e | 1565 | toBeSend, |
Azure.IoT Build | 19:d4f9e872bf3e | 1566 | &statusCode, |
Azure.IoT Build | 19:d4f9e872bf3e | 1567 | NULL, |
Azure.IoT Build | 19:d4f9e872bf3e | 1568 | NULL |
Azure.IoT Build | 19:d4f9e872bf3e | 1569 | )) != HTTPAPIEX_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1570 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1571 | LogError("unable to HTTPAPIEX_SAS_ExecuteRequest"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1572 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1573 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1574 | if (r == HTTPAPIEX_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1575 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1576 | if (statusCode < 300) |
Azure.IoT Build | 19:d4f9e872bf3e | 1577 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1578 | /*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 | 1579 | PDLIST_ENTRY justSent = DList_RemoveHeadList(deviceData->waitingToSend); /*actually this is the same as "actual", but now it is removed*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1580 | DList_InsertTailList(&(deviceData->eventConfirmations), justSent); |
Azure.IoT Build | 19:d4f9e872bf3e | 1581 | IoTHubClient_LL_SendComplete(iotHubClientHandle, &(deviceData->eventConfirmations), IOTHUB_CLIENT_CONFIRMATION_OK); /*takes care of emptying the list too*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1582 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1583 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1584 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1585 | /*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 | 1586 | LogError("unexpected HTTP status code (%u)", statusCode); |
Azure.IoT Build | 19:d4f9e872bf3e | 1587 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1588 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1589 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1590 | BUFFER_delete(toBeSend); |
Azure.IoT Build | 19:d4f9e872bf3e | 1591 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1592 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1593 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1594 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1595 | HTTPHeaders_Free(clonedEventHTTPrequestHeaders); |
Azure.IoT Build | 19:d4f9e872bf3e | 1596 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1597 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1598 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1599 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1600 | } |
AzureIoTClient | 6:73793bae15ba | 1601 | } |
AzureIoTClient | 6:73793bae15ba | 1602 | |
AzureIoTClient | 6:73793bae15ba | 1603 | #define ACTION_VALUES \ |
AzureIoTClient | 6:73793bae15ba | 1604 | ABANDON, \ |
AzureIoTClient | 6:73793bae15ba | 1605 | REJECT, \ |
AzureIoTClient | 6:73793bae15ba | 1606 | ACCEPT |
AzureIoTClient | 6:73793bae15ba | 1607 | DEFINE_ENUM(ACTION, ACTION_VALUES); |
AzureIoTClient | 6:73793bae15ba | 1608 | |
Azure.IoT Build | 13:848d52f93daf | 1609 | static void abandonOrAcceptMessage(HTTPTRANSPORT_HANDLE_DATA* handleData, HTTPTRANSPORT_PERDEVICE_DATA* deviceData, const char* ETag, ACTION action) |
AzureIoTClient | 6:73793bae15ba | 1610 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1611 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_097: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters: |
Azure.IoT Build | 19:d4f9e872bf3e | 1612 | -requestType: POST |
Azure.IoT Build | 19:d4f9e872bf3e | 1613 | -relativePath: abandon relative path begin (as created by _Create) + value of ETag + "/abandon?api-version=2016-02-03" |
Azure.IoT Build | 19:d4f9e872bf3e | 1614 | - requestHttpHeadersHandle: an HTTP headers instance containing the following |
Azure.IoT Build | 19:d4f9e872bf3e | 1615 | Authorization: " " |
Azure.IoT Build | 19:d4f9e872bf3e | 1616 | If-Match: value of ETag |
Azure.IoT Build | 19:d4f9e872bf3e | 1617 | - requestContent: NULL |
Azure.IoT Build | 19:d4f9e872bf3e | 1618 | - statusCode: a pointer to unsigned int which might be examined for logging |
Azure.IoT Build | 19:d4f9e872bf3e | 1619 | - responseHeadearsHandle: NULL |
Azure.IoT Build | 19:d4f9e872bf3e | 1620 | - responseContent: NULL]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1621 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_099: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters: |
Azure.IoT Build | 19:d4f9e872bf3e | 1622 | -requestType: DELETE |
Azure.IoT Build | 19:d4f9e872bf3e | 1623 | -relativePath: abandon relative path begin + value of ETag + "?api-version=2016-02-03" |
Azure.IoT Build | 19:d4f9e872bf3e | 1624 | - requestHttpHeadersHandle: an HTTP headers instance containing the following |
Azure.IoT Build | 19:d4f9e872bf3e | 1625 | Authorization: " " |
Azure.IoT Build | 19:d4f9e872bf3e | 1626 | If-Match: value of ETag |
Azure.IoT Build | 19:d4f9e872bf3e | 1627 | - requestContent: NULL |
Azure.IoT Build | 19:d4f9e872bf3e | 1628 | - statusCode: a pointer to unsigned int which might be used by logging |
Azure.IoT Build | 19:d4f9e872bf3e | 1629 | - responseHeadearsHandle: NULL |
Azure.IoT Build | 19:d4f9e872bf3e | 1630 | - responseContent: NULL]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1631 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_101: [_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest with the following parameters: |
Azure.IoT Build | 19:d4f9e872bf3e | 1632 | -requestType: DELETE |
Azure.IoT Build | 19:d4f9e872bf3e | 1633 | -relativePath: abandon relative path begin + value of ETag +"?api-version=2016-02-03" + "&reject" |
Azure.IoT Build | 19:d4f9e872bf3e | 1634 | - requestHttpHeadersHandle: an HTTP headers instance containing the following |
Azure.IoT Build | 19:d4f9e872bf3e | 1635 | Authorization: " " |
Azure.IoT Build | 19:d4f9e872bf3e | 1636 | If-Match: value of ETag |
Azure.IoT Build | 19:d4f9e872bf3e | 1637 | - requestContent: NULL |
Azure.IoT Build | 19:d4f9e872bf3e | 1638 | - statusCode: a pointer to unsigned int which might be used by logging |
Azure.IoT Build | 19:d4f9e872bf3e | 1639 | - responseHeadearsHandle: NULL |
Azure.IoT Build | 19:d4f9e872bf3e | 1640 | - responseContent: NULL]*/ |
AzureIoTClient | 17:7ef88cde801f | 1641 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1642 | STRING_HANDLE fullAbandonRelativePath = STRING_clone(deviceData->abandonHTTPrelativePathBegin); |
Azure.IoT Build | 19:d4f9e872bf3e | 1643 | if (fullAbandonRelativePath == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1644 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1645 | /*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 | 1646 | /*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 | 1647 | /*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 | 1648 | LogError("unable to STRING_clone"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1649 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1650 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1651 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1652 | 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 | 1653 | if (ETagUnquoted == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1654 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1655 | /*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 | 1656 | /*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 | 1657 | /*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 | 1658 | LogError("unable to STRING_construct_n"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1659 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1660 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1661 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1662 | if (!( |
Azure.IoT Build | 19:d4f9e872bf3e | 1663 | (STRING_concat_with_STRING(fullAbandonRelativePath, ETagUnquoted) == 0) && |
Azure.IoT Build | 19:d4f9e872bf3e | 1664 | (STRING_concat(fullAbandonRelativePath, (action == ABANDON) ? "/abandon" API_VERSION : ((action == REJECT) ? API_VERSION "&reject" : API_VERSION)) == 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 1665 | )) |
Azure.IoT Build | 19:d4f9e872bf3e | 1666 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1667 | /*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 | 1668 | /*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 | 1669 | /*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 | 1670 | LogError("unable to STRING_concat"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1671 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1672 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1673 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1674 | HTTP_HEADERS_HANDLE abandonRequestHttpHeaders = HTTPHeaders_Alloc(); |
Azure.IoT Build | 19:d4f9e872bf3e | 1675 | if (abandonRequestHttpHeaders == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1676 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1677 | /*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 | 1678 | /*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 | 1679 | /*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 | 1680 | LogError("unable to HTTPHeaders_Alloc"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1681 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1682 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1683 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1684 | if (!( |
Azure.IoT Build | 19:d4f9e872bf3e | 1685 | (HTTPHeaders_AddHeaderNameValuePair(abandonRequestHttpHeaders, "User-Agent", CLIENT_DEVICE_TYPE_PREFIX CLIENT_DEVICE_BACKSLASH IOTHUB_SDK_VERSION) == HTTP_HEADERS_OK) && |
Azure.IoT Build | 19:d4f9e872bf3e | 1686 | (HTTPHeaders_AddHeaderNameValuePair(abandonRequestHttpHeaders, "Authorization", " ") == HTTP_HEADERS_OK) && |
Azure.IoT Build | 19:d4f9e872bf3e | 1687 | (HTTPHeaders_AddHeaderNameValuePair(abandonRequestHttpHeaders, "If-Match", ETag) == HTTP_HEADERS_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1688 | )) |
Azure.IoT Build | 19:d4f9e872bf3e | 1689 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1690 | /*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 | 1691 | /*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 | 1692 | /*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 | 1693 | LogError("unable to HTTPHeaders_AddHeaderNameValuePair"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1694 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1695 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1696 | { |
AzureIoTClient | 20:b652f2e4cccf | 1697 | unsigned int statusCode = 0; |
Azure.IoT Build | 19:d4f9e872bf3e | 1698 | HTTPAPIEX_RESULT r; |
Azure.IoT Build | 19:d4f9e872bf3e | 1699 | if (deviceData->deviceSasToken != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1700 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1701 | /*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 | 1702 | if (HTTPHeaders_ReplaceHeaderNameValuePair(abandonRequestHttpHeaders, "Authorization", STRING_c_str(deviceData->deviceSasToken)) != HTTP_HEADERS_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1703 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1704 | r = HTTPAPIEX_ERROR; |
Azure.IoT Build | 19:d4f9e872bf3e | 1705 | /*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 | 1706 | LogError("Unable to replace the old SAS Token."); |
Azure.IoT Build | 19:d4f9e872bf3e | 1707 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1708 | else if ((r = HTTPAPIEX_ExecuteRequest( |
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_ExecuteRequest."); |
Azure.IoT Build | 19:d4f9e872bf3e | 1723 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1724 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1725 | else if ((r = HTTPAPIEX_SAS_ExecuteRequest( |
Azure.IoT Build | 19:d4f9e872bf3e | 1726 | deviceData->sasObject, |
Azure.IoT Build | 19:d4f9e872bf3e | 1727 | handleData->httpApiExHandle, |
Azure.IoT Build | 19:d4f9e872bf3e | 1728 | (action == ABANDON) ? HTTPAPI_REQUEST_POST : HTTPAPI_REQUEST_DELETE, /*-requestType: POST */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1729 | 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 | 1730 | abandonRequestHttpHeaders, /*- requestHttpHeadersHandle: an HTTP headers instance containing the following */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1731 | NULL, /*- requestContent: NULL */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1732 | &statusCode, /*- statusCode: a pointer to unsigned int which might be examined for logging */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1733 | NULL, /*- responseHeadearsHandle: NULL */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1734 | NULL /*- responseContent: NULL] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1735 | )) != HTTPAPIEX_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1736 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1737 | /*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 | 1738 | /*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 | 1739 | /*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 | 1740 | LogError("unable to HTTPAPIEX_SAS_ExecuteRequest"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1741 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1742 | if (r == HTTPAPIEX_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1743 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1744 | if (statusCode != 204) |
Azure.IoT Build | 19:d4f9e872bf3e | 1745 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1746 | /*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 | 1747 | /*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 | 1748 | /*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 | 1749 | LogError("unexpected status code returned %u (was expecting 204)", statusCode); |
Azure.IoT Build | 19:d4f9e872bf3e | 1750 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1751 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1752 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1753 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_098: [Abandoning the message is considered successful if the HTTPAPIEX_SAS_ExecuteRequest doesn't fail and the statusCode is 204.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1754 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_100: [Accepting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1755 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_102: [Rejecting a message is successful when HTTPAPIEX_SAS_ExecuteRequest completes successfully and the status code is 204.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1756 | /*all is fine*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1757 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1758 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1759 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1760 | HTTPHeaders_Free(abandonRequestHttpHeaders); |
Azure.IoT Build | 19:d4f9e872bf3e | 1761 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1762 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1763 | STRING_delete(ETagUnquoted); |
Azure.IoT Build | 19:d4f9e872bf3e | 1764 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1765 | STRING_delete(fullAbandonRelativePath); |
Azure.IoT Build | 19:d4f9e872bf3e | 1766 | } |
AzureIoTClient | 6:73793bae15ba | 1767 | } |
AzureIoTClient | 6:73793bae15ba | 1768 | |
Azure.IoT Build | 13:848d52f93daf | 1769 | static void DoMessages(HTTPTRANSPORT_HANDLE_DATA* handleData, HTTPTRANSPORT_PERDEVICE_DATA* deviceData, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 6:73793bae15ba | 1770 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1771 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_083: [ If device is not subscribed then _DoWork shall advance to the next action. ] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1772 | if (deviceData->DoWork_PullMessage) |
Azure.IoT Build | 19:d4f9e872bf3e | 1773 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1774 | /*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 | 1775 | /*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 | 1776 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_122: [A GET request that happens earlier than GetMinimumPollingTime shall be ignored.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1777 | time_t timeNow = get_time(NULL); |
Azure.IoT Build | 19:d4f9e872bf3e | 1778 | bool isPollingAllowed = deviceData->isFirstPoll || (timeNow == (time_t)(-1)) || (get_difftime(timeNow, deviceData->lastPollTime) > handleData->getMinimumPollingTime); |
Azure.IoT Build | 19:d4f9e872bf3e | 1779 | if (isPollingAllowed) |
Azure.IoT Build | 19:d4f9e872bf3e | 1780 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1781 | HTTP_HEADERS_HANDLE responseHTTPHeaders = HTTPHeaders_Alloc(); |
Azure.IoT Build | 19:d4f9e872bf3e | 1782 | if (responseHTTPHeaders == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1783 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1784 | /*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 | 1785 | LogError("unable to HTTPHeaders_Alloc"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1786 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1787 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1788 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1789 | BUFFER_HANDLE responseContent = BUFFER_new(); |
Azure.IoT Build | 19:d4f9e872bf3e | 1790 | if (responseContent == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1791 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1792 | /*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 | 1793 | LogError("unable to BUFFER_new"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1794 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1795 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1796 | { |
AzureIoTClient | 20:b652f2e4cccf | 1797 | unsigned int statusCode = 0; |
Azure.IoT Build | 19:d4f9e872bf3e | 1798 | HTTPAPIEX_RESULT r; |
Azure.IoT Build | 19:d4f9e872bf3e | 1799 | if (deviceData->deviceSasToken != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1800 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1801 | /*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 | 1802 | if (HTTPHeaders_ReplaceHeaderNameValuePair(deviceData->messageHTTPrequestHeaders, "Authorization", STRING_c_str(deviceData->deviceSasToken)) != HTTP_HEADERS_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1803 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1804 | r = HTTPAPIEX_ERROR; |
Azure.IoT Build | 19:d4f9e872bf3e | 1805 | /*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 | 1806 | LogError("Unable to replace the old SAS Token."); |
Azure.IoT Build | 19:d4f9e872bf3e | 1807 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1808 | else if ((r = HTTPAPIEX_ExecuteRequest( |
Azure.IoT Build | 19:d4f9e872bf3e | 1809 | handleData->httpApiExHandle, |
Azure.IoT Build | 19:d4f9e872bf3e | 1810 | HTTPAPI_REQUEST_GET, /*requestType: GET*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1811 | STRING_c_str(deviceData->messageHTTPrelativePath), /*relativePath: the message HTTP relative path*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1812 | deviceData->messageHTTPrequestHeaders, /*requestHttpHeadersHandle: message HTTP request headers created by _Create*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1813 | NULL, /*requestContent: NULL*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1814 | &statusCode, /*statusCode: a pointer to unsigned int which shall be later examined*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1815 | responseHTTPHeaders, /*responseHeadearsHandle: a new instance of HTTP headers*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1816 | responseContent /*responseContent: a new instance of buffer*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1817 | )) != HTTPAPIEX_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1818 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1819 | /*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 | 1820 | LogError("Unable to HTTPAPIEX_ExecuteRequest."); |
Azure.IoT Build | 19:d4f9e872bf3e | 1821 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1822 | } |
AzureIoTClient | 17:7ef88cde801f | 1823 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1824 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_084: [Otherwise, IoTHubTransportHttp_DoWork shall call HTTPAPIEX_SAS_ExecuteRequest passing the following parameters |
Azure.IoT Build | 19:d4f9e872bf3e | 1825 | requestType: GET |
Azure.IoT Build | 19:d4f9e872bf3e | 1826 | relativePath: the message HTTP relative path |
Azure.IoT Build | 19:d4f9e872bf3e | 1827 | requestHttpHeadersHandle: message HTTP request headers created by _Create |
Azure.IoT Build | 19:d4f9e872bf3e | 1828 | requestContent: NULL |
Azure.IoT Build | 19:d4f9e872bf3e | 1829 | statusCode: a pointer to unsigned int which shall be later examined |
Azure.IoT Build | 19:d4f9e872bf3e | 1830 | responseHeadearsHandle: a new instance of HTTP headers |
Azure.IoT Build | 19:d4f9e872bf3e | 1831 | responseContent: a new instance of buffer] |
Azure.IoT Build | 19:d4f9e872bf3e | 1832 | */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1833 | else if ((r = HTTPAPIEX_SAS_ExecuteRequest( |
Azure.IoT Build | 19:d4f9e872bf3e | 1834 | deviceData->sasObject, |
Azure.IoT Build | 19:d4f9e872bf3e | 1835 | handleData->httpApiExHandle, |
Azure.IoT Build | 19:d4f9e872bf3e | 1836 | HTTPAPI_REQUEST_GET, /*requestType: GET*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1837 | STRING_c_str(deviceData->messageHTTPrelativePath), /*relativePath: the message HTTP relative path*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1838 | deviceData->messageHTTPrequestHeaders, /*requestHttpHeadersHandle: message HTTP request headers created by _Create*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1839 | NULL, /*requestContent: NULL*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1840 | &statusCode, /*statusCode: a pointer to unsigned int which shall be later examined*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1841 | responseHTTPHeaders, /*responseHeadearsHandle: a new instance of HTTP headers*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1842 | responseContent /*responseContent: a new instance of buffer*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1843 | )) != HTTPAPIEX_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1844 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1845 | /*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 | 1846 | LogError("unable to HTTPAPIEX_SAS_ExecuteRequest"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1847 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1848 | if (r == HTTPAPIEX_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1849 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1850 | /*HTTP dialogue was succesfull*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1851 | if (timeNow == (time_t)(-1)) |
Azure.IoT Build | 19:d4f9e872bf3e | 1852 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1853 | deviceData->isFirstPoll = true; |
Azure.IoT Build | 19:d4f9e872bf3e | 1854 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1855 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1856 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1857 | deviceData->isFirstPoll = false; |
Azure.IoT Build | 19:d4f9e872bf3e | 1858 | deviceData->lastPollTime = timeNow; |
Azure.IoT Build | 19:d4f9e872bf3e | 1859 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1860 | if (statusCode == 204) |
Azure.IoT Build | 19:d4f9e872bf3e | 1861 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1862 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_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 | 1863 | /*this is an expected status code, means "no commands", but logging that creates panic*/ |
AzureIoTClient | 6:73793bae15ba | 1864 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1865 | /*do nothing, advance to next action*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1866 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1867 | else if (statusCode != 200) |
Azure.IoT Build | 19:d4f9e872bf3e | 1868 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1869 | /*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 | 1870 | LogError("expected status code was 200, but actually was received %u... moving on", statusCode); |
Azure.IoT Build | 19:d4f9e872bf3e | 1871 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1872 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1873 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1874 | /*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 | 1875 | const char* etagValue = HTTPHeaders_FindHeaderValue(responseHTTPHeaders, "ETag"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1876 | if (etagValue == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1877 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1878 | LogError("unable to find a received header called \"E-Tag\""); |
Azure.IoT Build | 19:d4f9e872bf3e | 1879 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1880 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1881 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1882 | /*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 | 1883 | size_t etagsize = strlen(etagValue); |
Azure.IoT Build | 19:d4f9e872bf3e | 1884 | if ( |
Azure.IoT Build | 19:d4f9e872bf3e | 1885 | (etagsize < 2) || |
Azure.IoT Build | 19:d4f9e872bf3e | 1886 | (etagValue[0] != '"') || |
Azure.IoT Build | 19:d4f9e872bf3e | 1887 | (etagValue[etagsize - 1] != '"') |
Azure.IoT Build | 19:d4f9e872bf3e | 1888 | ) |
Azure.IoT Build | 19:d4f9e872bf3e | 1889 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1890 | LogError("ETag is not a valid quoted string"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1891 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1892 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1893 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1894 | /*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 | 1895 | IOTHUB_MESSAGE_HANDLE receivedMessage = IoTHubMessage_CreateFromByteArray(BUFFER_u_char(responseContent), BUFFER_length(responseContent)); |
Azure.IoT Build | 19:d4f9e872bf3e | 1896 | if (receivedMessage == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1897 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1898 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_092: [If assembling the message fails in any way, then _DoWork shall "abandon" the message.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1899 | LogError("unable to IoTHubMessage_CreateFromByteArray, trying to abandon the message... "); |
Azure.IoT Build | 19:d4f9e872bf3e | 1900 | abandonOrAcceptMessage(handleData, deviceData, etagValue, ABANDON); |
Azure.IoT Build | 19:d4f9e872bf3e | 1901 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1902 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1903 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1904 | /*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 | 1905 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_091: [The HTTP header of iothub-messageid shall be set in the MessageId.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1906 | size_t nHeaders; |
Azure.IoT Build | 19:d4f9e872bf3e | 1907 | if (HTTPHeaders_GetHeaderCount(responseHTTPHeaders, &nHeaders) != HTTP_HEADERS_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1908 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1909 | LogError("unable to get the count of HTTP headers"); |
Azure.IoT Build | 19:d4f9e872bf3e | 1910 | abandonOrAcceptMessage(handleData, deviceData, etagValue, ABANDON); |
Azure.IoT Build | 19:d4f9e872bf3e | 1911 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1912 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1913 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1914 | size_t i; |
Azure.IoT Build | 19:d4f9e872bf3e | 1915 | MAP_HANDLE properties = (nHeaders > 0) ? IoTHubMessage_Properties(receivedMessage) : NULL; |
Azure.IoT Build | 19:d4f9e872bf3e | 1916 | for (i = 0; i < nHeaders; i++) |
Azure.IoT Build | 19:d4f9e872bf3e | 1917 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1918 | char* completeHeader; |
Azure.IoT Build | 19:d4f9e872bf3e | 1919 | if (HTTPHeaders_GetHeader(responseHTTPHeaders, i, &completeHeader) != HTTP_HEADERS_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1920 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1921 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 1922 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1923 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1924 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1925 | if (strncmp(IOTHUB_APP_PREFIX, completeHeader, strlen(IOTHUB_APP_PREFIX)) == 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 1926 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1927 | /*looks like a property headers*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1928 | /*there's a guaranteed ':' in the completeHeader, by HTTP_HEADERS module*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1929 | char* whereIsColon = strchr(completeHeader, ':'); |
Azure.IoT Build | 19:d4f9e872bf3e | 1930 | if (whereIsColon != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1931 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1932 | *whereIsColon = '\0'; /*cut it down*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1933 | 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 | 1934 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1935 | free(completeHeader); |
Azure.IoT Build | 19:d4f9e872bf3e | 1936 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 1937 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1938 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1939 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1940 | else if (strncmp(IOTHUB_MESSAGE_ID, completeHeader, strlen(IOTHUB_MESSAGE_ID)) == 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 1941 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1942 | char* whereIsColon = strchr(completeHeader, ':'); |
Azure.IoT Build | 19:d4f9e872bf3e | 1943 | if (whereIsColon != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1944 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1945 | *whereIsColon = '\0'; /*cut it down*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1946 | if (IoTHubMessage_SetMessageId(receivedMessage, whereIsColon + 2) != IOTHUB_MESSAGE_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1947 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1948 | free(completeHeader); |
Azure.IoT Build | 19:d4f9e872bf3e | 1949 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 1950 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1951 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1952 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1953 | else if (strncmp(IOTHUB_CORRELATION_ID, completeHeader, strlen(IOTHUB_CORRELATION_ID)) == 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 1954 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1955 | char* whereIsColon = strchr(completeHeader, ':'); |
Azure.IoT Build | 19:d4f9e872bf3e | 1956 | if (whereIsColon != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 1957 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1958 | *whereIsColon = '\0'; /*cut it down*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1959 | if (IoTHubMessage_SetCorrelationId(receivedMessage, whereIsColon + 2) != IOTHUB_MESSAGE_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 1960 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1961 | free(completeHeader); |
Azure.IoT Build | 19:d4f9e872bf3e | 1962 | break; |
Azure.IoT Build | 19:d4f9e872bf3e | 1963 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1964 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1965 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1966 | free(completeHeader); |
Azure.IoT Build | 19:d4f9e872bf3e | 1967 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1968 | } |
AzureIoTClient | 6:73793bae15ba | 1969 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1970 | if (i < nHeaders) |
Azure.IoT Build | 19:d4f9e872bf3e | 1971 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1972 | abandonOrAcceptMessage(handleData, deviceData, etagValue, ABANDON); |
Azure.IoT Build | 19:d4f9e872bf3e | 1973 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1974 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1975 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1976 | /*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 | 1977 | IOTHUBMESSAGE_DISPOSITION_RESULT messageResult = IoTHubClient_LL_MessageCallback(iotHubClientHandle, receivedMessage); |
Azure.IoT Build | 19:d4f9e872bf3e | 1978 | if (messageResult == IOTHUBMESSAGE_ACCEPTED) |
Azure.IoT Build | 19:d4f9e872bf3e | 1979 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1980 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_094: [If IoTHubClient_LL_MessageCallback returns IOTHUBMESSAGE_ACCEPTED then _DoWork shall "accept" the message.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1981 | abandonOrAcceptMessage(handleData, deviceData, etagValue, ACCEPT); |
Azure.IoT Build | 19:d4f9e872bf3e | 1982 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1983 | else if (messageResult == IOTHUBMESSAGE_REJECTED) |
Azure.IoT Build | 19:d4f9e872bf3e | 1984 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1985 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_095: [If IoTHubClient_LL_MessageCallback returns IOTHUBMESSAGE_REJECTED then _DoWork shall "reject" the message.]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 1986 | abandonOrAcceptMessage(handleData, deviceData, etagValue, REJECT); |
Azure.IoT Build | 19:d4f9e872bf3e | 1987 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1988 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 1989 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 1990 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_096: [If IoTHubClient_LL_MessageCallback returns IOTHUBMESSAGE_ABANDONED then _DoWork shall "abandon" the message.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 1991 | abandonOrAcceptMessage(handleData, deviceData, etagValue, ABANDON); |
Azure.IoT Build | 19:d4f9e872bf3e | 1992 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1993 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1994 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1995 | IoTHubMessage_Destroy(receivedMessage); |
Azure.IoT Build | 19:d4f9e872bf3e | 1996 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 1997 | } |
AzureIoTClient | 17:7ef88cde801f | 1998 | |
Azure.IoT Build | 19:d4f9e872bf3e | 1999 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2000 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2001 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2002 | BUFFER_delete(responseContent); |
Azure.IoT Build | 19:d4f9e872bf3e | 2003 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2004 | HTTPHeaders_Free(responseHTTPHeaders); |
Azure.IoT Build | 19:d4f9e872bf3e | 2005 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2006 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2007 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 2008 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2009 | /*isPollingAllowed is false... */ |
Azure.IoT Build | 19:d4f9e872bf3e | 2010 | /*do nothing "shall be ignored*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 2011 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2012 | } |
AzureIoTClient | 6:73793bae15ba | 2013 | } |
AzureIoTClient | 6:73793bae15ba | 2014 | |
AzureIoTClient | 18:ab990f6aa61f | 2015 | static void IoTHubTransportHttp_DoWork(TRANSPORT_LL_HANDLE handle, IOTHUB_CLIENT_LL_HANDLE iotHubClientHandle) |
AzureIoTClient | 6:73793bae15ba | 2016 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2017 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_049: [ If handle is NULL, then IoTHubTransportHttp_DoWork shall do nothing. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 2018 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_140: [ If iotHubClientHandle is NULL, then IoTHubTransportHttp_DoWork shall do nothing. ]*/ |
Azure.IoT Build | 13:848d52f93daf | 2019 | |
Azure.IoT Build | 19:d4f9e872bf3e | 2020 | (void)iotHubClientHandle; // use the perDevice handle. |
Azure.IoT Build | 19:d4f9e872bf3e | 2021 | if (handle != NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 2022 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2023 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
Azure.IoT Build | 19:d4f9e872bf3e | 2024 | IOTHUB_DEVICE_HANDLE* listItem; |
Azure.IoT Build | 19:d4f9e872bf3e | 2025 | size_t deviceListSize = VECTOR_size(handleData->perDeviceList); |
Azure.IoT Build | 19:d4f9e872bf3e | 2026 | /*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 | 2027 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_050: [ IoTHubTransportHttp_DoWork shall call loop through the device list. ] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 2028 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_051: [ IF the list is empty, then IoTHubTransportHttp_DoWork shall do nothing. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 2029 | for (size_t i = 0; i < deviceListSize; i++) |
Azure.IoT Build | 19:d4f9e872bf3e | 2030 | { |
AzureIoTClient | 21:b601705c82a8 | 2031 | listItem = (IOTHUB_DEVICE_HANDLE *) VECTOR_element(handleData->perDeviceList, i); |
AzureIoTClient | 21:b601705c82a8 | 2032 | HTTPTRANSPORT_PERDEVICE_DATA* perDeviceItem = *(HTTPTRANSPORT_PERDEVICE_DATA**)(listItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 2033 | DoEvent(handleData, perDeviceItem, perDeviceItem->iotHubClientHandle); |
Azure.IoT Build | 19:d4f9e872bf3e | 2034 | DoMessages(handleData, perDeviceItem, perDeviceItem->iotHubClientHandle); |
Azure.IoT Build | 13:848d52f93daf | 2035 | |
Azure.IoT Build | 19:d4f9e872bf3e | 2036 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2037 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2038 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 2039 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2040 | LogError("Invalid Argument NULL call on DoWork."); |
Azure.IoT Build | 19:d4f9e872bf3e | 2041 | } |
AzureIoTClient | 6:73793bae15ba | 2042 | } |
AzureIoTClient | 6:73793bae15ba | 2043 | |
AzureIoTClient | 18:ab990f6aa61f | 2044 | static IOTHUB_CLIENT_RESULT IoTHubTransportHttp_GetSendStatus(IOTHUB_DEVICE_HANDLE handle, IOTHUB_CLIENT_STATUS *iotHubClientStatus) |
AzureIoTClient | 6:73793bae15ba | 2045 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2046 | IOTHUB_CLIENT_RESULT result; |
AzureIoTClient | 6:73793bae15ba | 2047 | |
Azure.IoT Build | 19:d4f9e872bf3e | 2048 | if (handle == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 2049 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2050 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_111: [ IoTHubTransportHttp_GetSendStatus shall return IOTHUB_CLIENT_INVALID_ARG if called with NULL parameter. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 2051 | result = IOTHUB_CLIENT_INVALID_ARG; |
Azure.IoT Build | 19:d4f9e872bf3e | 2052 | LogError("Invalid handle to IoTHubClient HTTP transport instance."); |
Azure.IoT Build | 19:d4f9e872bf3e | 2053 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2054 | else if (iotHubClientStatus == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 2055 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2056 | result = IOTHUB_CLIENT_INVALID_ARG; |
Azure.IoT Build | 19:d4f9e872bf3e | 2057 | LogError("Invalid pointer to output parameter IOTHUB_CLIENT_STATUS."); |
Azure.IoT Build | 19:d4f9e872bf3e | 2058 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2059 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 2060 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2061 | /*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 | 2062 | IOTHUB_DEVICE_HANDLE* listItem = get_perDeviceDataItem(handle); |
Azure.IoT Build | 19:d4f9e872bf3e | 2063 | if (listItem == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 2064 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2065 | /*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 | 2066 | result = IOTHUB_CLIENT_INVALID_ARG; |
Azure.IoT Build | 19:d4f9e872bf3e | 2067 | LogError("Device not found in transport list."); |
Azure.IoT Build | 19:d4f9e872bf3e | 2068 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2069 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 2070 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2071 | HTTPTRANSPORT_PERDEVICE_DATA* deviceData = (HTTPTRANSPORT_PERDEVICE_DATA*)(*listItem); |
Azure.IoT Build | 19:d4f9e872bf3e | 2072 | /* 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 | 2073 | if (!DList_IsListEmpty(deviceData->waitingToSend)) |
Azure.IoT Build | 19:d4f9e872bf3e | 2074 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2075 | *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_BUSY; |
Azure.IoT Build | 19:d4f9e872bf3e | 2076 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2077 | /* 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 | 2078 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 2079 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2080 | *iotHubClientStatus = IOTHUB_CLIENT_SEND_STATUS_IDLE; |
Azure.IoT Build | 19:d4f9e872bf3e | 2081 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2082 | result = IOTHUB_CLIENT_OK; |
Azure.IoT Build | 19:d4f9e872bf3e | 2083 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2084 | } |
AzureIoTClient | 6:73793bae15ba | 2085 | |
Azure.IoT Build | 19:d4f9e872bf3e | 2086 | return result; |
AzureIoTClient | 6:73793bae15ba | 2087 | } |
AzureIoTClient | 6:73793bae15ba | 2088 | |
AzureIoTClient | 18:ab990f6aa61f | 2089 | static IOTHUB_CLIENT_RESULT IoTHubTransportHttp_SetOption(TRANSPORT_LL_HANDLE handle, const char* option, const void* value) |
AzureIoTClient | 6:73793bae15ba | 2090 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2091 | IOTHUB_CLIENT_RESULT result; |
Azure.IoT Build | 19:d4f9e872bf3e | 2092 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_114: [If handle parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 2093 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_115: [If option parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 2094 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_116: [If value parameter is NULL then IoTHubTransportHttp_SetOption shall return IOTHUB_CLIENT_INVALID_ARG.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 2095 | if ( |
Azure.IoT Build | 19:d4f9e872bf3e | 2096 | (handle == NULL) || |
Azure.IoT Build | 19:d4f9e872bf3e | 2097 | (option == NULL) || |
Azure.IoT Build | 19:d4f9e872bf3e | 2098 | (value == NULL) |
Azure.IoT Build | 19:d4f9e872bf3e | 2099 | ) |
Azure.IoT Build | 19:d4f9e872bf3e | 2100 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2101 | result = IOTHUB_CLIENT_INVALID_ARG; |
Azure.IoT Build | 19:d4f9e872bf3e | 2102 | LogError("invalid parameter (NULL) passed to IoTHubTransportHttp_SetOption"); |
Azure.IoT Build | 19:d4f9e872bf3e | 2103 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2104 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 2105 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2106 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
Azure.IoT Build | 19:d4f9e872bf3e | 2107 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_120: ["Batching"] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 2108 | if (strcmp("Batching", option) == 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 2109 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2110 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_117: [If optionName is an option handled by IoTHubTransportHttp then it shall be set.] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 2111 | handleData->doBatchedTransfers = *(bool*)value; |
Azure.IoT Build | 19:d4f9e872bf3e | 2112 | result = IOTHUB_CLIENT_OK; |
Azure.IoT Build | 19:d4f9e872bf3e | 2113 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2114 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_121: ["MinimumPollingTime"] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 2115 | else if (strcmp("MinimumPollingTime", option) == 0) |
Azure.IoT Build | 19:d4f9e872bf3e | 2116 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2117 | handleData->getMinimumPollingTime = *(unsigned int*)value; |
Azure.IoT Build | 19:d4f9e872bf3e | 2118 | result = IOTHUB_CLIENT_OK; |
Azure.IoT Build | 19:d4f9e872bf3e | 2119 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2120 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 2121 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2122 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_126: [ "TrustedCerts"] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 2123 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_127: [ NULL shall be allowed. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 2124 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_129: [ This option shall passed down to the lower layer by calling HTTPAPIEX_SetOption. ]*/ |
Azure.IoT Build | 19:d4f9e872bf3e | 2125 | /*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 | 2126 | HTTPAPIEX_RESULT HTTPAPIEX_result = HTTPAPIEX_SetOption(handleData->httpApiExHandle, option, value); |
Azure.IoT Build | 19:d4f9e872bf3e | 2127 | /*Codes_SRS_TRANSPORTMULTITHTTP_17_119: [The following table translates HTTPAPIEX return codes to IOTHUB_CLIENT_RESULT return codes:] */ |
Azure.IoT Build | 19:d4f9e872bf3e | 2128 | if (HTTPAPIEX_result == HTTPAPIEX_OK) |
Azure.IoT Build | 19:d4f9e872bf3e | 2129 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2130 | result = IOTHUB_CLIENT_OK; |
Azure.IoT Build | 19:d4f9e872bf3e | 2131 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2132 | else if (HTTPAPIEX_result == HTTPAPIEX_INVALID_ARG) |
Azure.IoT Build | 19:d4f9e872bf3e | 2133 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2134 | result = IOTHUB_CLIENT_INVALID_ARG; |
Azure.IoT Build | 19:d4f9e872bf3e | 2135 | LogError("HTTPAPIEX_SetOption failed"); |
Azure.IoT Build | 19:d4f9e872bf3e | 2136 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2137 | else |
Azure.IoT Build | 19:d4f9e872bf3e | 2138 | { |
Azure.IoT Build | 19:d4f9e872bf3e | 2139 | result = IOTHUB_CLIENT_ERROR; |
Azure.IoT Build | 19:d4f9e872bf3e | 2140 | LogError("HTTPAPIEX_SetOption failed"); |
Azure.IoT Build | 19:d4f9e872bf3e | 2141 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2142 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2143 | } |
Azure.IoT Build | 19:d4f9e872bf3e | 2144 | return result; |
AzureIoTClient | 6:73793bae15ba | 2145 | } |
AzureIoTClient | 18:ab990f6aa61f | 2146 | |
AzureIoTClient | 18:ab990f6aa61f | 2147 | static STRING_HANDLE IoTHubTransportHttp_GetHostname(TRANSPORT_LL_HANDLE handle) |
AzureIoTClient | 18:ab990f6aa61f | 2148 | { |
AzureIoTClient | 18:ab990f6aa61f | 2149 | STRING_HANDLE result; |
AzureIoTClient | 18:ab990f6aa61f | 2150 | /*Codes_SRS_TRANSPORTMULTITHTTP_02_001: [ If handle is NULL then IoTHubTransportHttp_GetHostname shall fail and return NULL. ]*/ |
AzureIoTClient | 18:ab990f6aa61f | 2151 | if (handle == NULL) |
AzureIoTClient | 18:ab990f6aa61f | 2152 | { |
AzureIoTClient | 18:ab990f6aa61f | 2153 | LogError("invalid parameter handle=%p", handle); |
AzureIoTClient | 18:ab990f6aa61f | 2154 | result = NULL; |
AzureIoTClient | 18:ab990f6aa61f | 2155 | } |
AzureIoTClient | 18:ab990f6aa61f | 2156 | else |
AzureIoTClient | 18:ab990f6aa61f | 2157 | { |
AzureIoTClient | 18:ab990f6aa61f | 2158 | /*Codes_SRS_TRANSPORTMULTITHTTP_02_002: [ Otherwise IoTHubTransportHttp_GetHostname shall return a non-NULL STRING_HANDLE containing the hostname. ]*/ |
AzureIoTClient | 18:ab990f6aa61f | 2159 | HTTPTRANSPORT_HANDLE_DATA* handleData = (HTTPTRANSPORT_HANDLE_DATA*)handle; |
AzureIoTClient | 18:ab990f6aa61f | 2160 | result = handleData->hostName; |
AzureIoTClient | 18:ab990f6aa61f | 2161 | } |
AzureIoTClient | 18:ab990f6aa61f | 2162 | return result; |
AzureIoTClient | 18:ab990f6aa61f | 2163 | } |
AzureIoTClient | 18:ab990f6aa61f | 2164 | |
AzureIoTClient | 18:ab990f6aa61f | 2165 | /*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 | 2166 | static TRANSPORT_PROVIDER thisTransportProvider = |
AzureIoTClient | 18:ab990f6aa61f | 2167 | { |
AzureIoTClient | 18:ab990f6aa61f | 2168 | IoTHubTransportHttp_GetHostname, /*pfIoTHubTransport_GetHostname IoTHubTransport_GetHostname; */ |
AzureIoTClient | 18:ab990f6aa61f | 2169 | IoTHubTransportHttp_SetOption, /*pfIoTHubTransport_SetOption IoTHubTransport_SetOption; */ |
AzureIoTClient | 18:ab990f6aa61f | 2170 | IoTHubTransportHttp_Create, /*pfIoTHubTransport_Create IoTHubTransport_Create; */ |
AzureIoTClient | 18:ab990f6aa61f | 2171 | IoTHubTransportHttp_Destroy, /*pfIoTHubTransport_Destroy IoTHubTransport_Destroy; */ |
AzureIoTClient | 18:ab990f6aa61f | 2172 | IoTHubTransportHttp_Register, /* pfIotHubTransport_Register IoTHubTransport_Register; */ |
AzureIoTClient | 18:ab990f6aa61f | 2173 | IoTHubTransportHttp_Unregister, /* pfIotHubTransport_Unregister IoTHubTransport_Unegister; */ |
AzureIoTClient | 18:ab990f6aa61f | 2174 | IoTHubTransportHttp_Subscribe, /*pfIoTHubTransport_Subscribe IoTHubTransport_Subscribe; */ |
AzureIoTClient | 18:ab990f6aa61f | 2175 | IoTHubTransportHttp_Unsubscribe, /*pfIoTHubTransport_Unsubscribe IoTHubTransport_Unsubscribe; */ |
AzureIoTClient | 18:ab990f6aa61f | 2176 | IoTHubTransportHttp_DoWork, /*pfIoTHubTransport_DoWork IoTHubTransport_DoWork; */ |
AzureIoTClient | 18:ab990f6aa61f | 2177 | IoTHubTransportHttp_GetSendStatus /* pfIoTHubTransport_GetSendStatus IoTHubTransport_GetSendStatus */ |
AzureIoTClient | 18:ab990f6aa61f | 2178 | }; |
AzureIoTClient | 18:ab990f6aa61f | 2179 | |
AzureIoTClient | 18:ab990f6aa61f | 2180 | const TRANSPORT_PROVIDER* HTTP_Protocol(void) |
AzureIoTClient | 18:ab990f6aa61f | 2181 | { |
AzureIoTClient | 18:ab990f6aa61f | 2182 | return &thisTransportProvider; |
AzureIoTClient | 18:ab990f6aa61f | 2183 | } |