Microsoft Azure IoTHub client libraries

Dependents:   sht15_remote_monitoring RobotArmDemo iothub_client_sample_amqp f767zi_mqtt ... more

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

Committer:
AzureIoTClient
Date:
Fri Feb 24 14:00:43 2017 -0800
Revision:
60:41648c4e7036
Parent:
58:15b0d29b2667
Child:
62:5a4cdacf5090
1.1.8

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 42:448eecc3676e 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 42:448eecc3676e 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 42:448eecc3676e 3
Azure.IoT Build 45:54c11b1b1407 4 #ifdef DONT_USE_UPLOADTOBLOB
Azure.IoT Build 45:54c11b1b1407 5 #error "trying to compile iothub_client_ll_uploadtoblob.c while the symbol DONT_USE_UPLOADTOBLOB is #define'd"
Azure.IoT Build 45:54c11b1b1407 6 #else
Azure.IoT Build 45:54c11b1b1407 7
AzureIoTClient 42:448eecc3676e 8 #include <stdlib.h>
AzureIoTClient 42:448eecc3676e 9 #include <string.h>
AzureIoTClient 60:41648c4e7036 10 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 42:448eecc3676e 11 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 42:448eecc3676e 12 #include "azure_c_shared_utility/string_tokenizer.h"
AzureIoTClient 42:448eecc3676e 13 #include "azure_c_shared_utility/doublylinkedlist.h"
Azure.IoT Build 45:54c11b1b1407 14 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 42:448eecc3676e 15 #include "azure_c_shared_utility/tickcounter.h"
AzureIoTClient 42:448eecc3676e 16 #include "azure_c_shared_utility/httpapiexsas.h"
AzureIoTClient 42:448eecc3676e 17
AzureIoTClient 42:448eecc3676e 18 #include "iothub_client_ll.h"
AzureIoTClient 48:cc5d91f2b06d 19 #include "iothub_client_options.h"
AzureIoTClient 42:448eecc3676e 20 #include "iothub_client_private.h"
AzureIoTClient 42:448eecc3676e 21 #include "iothub_client_version.h"
AzureIoTClient 42:448eecc3676e 22 #include "iothub_transport_ll.h"
AzureIoTClient 42:448eecc3676e 23 #include "parson.h"
AzureIoTClient 42:448eecc3676e 24 #include "iothub_client_ll_uploadtoblob.h"
AzureIoTClient 42:448eecc3676e 25 #include "blob.h"
AzureIoTClient 42:448eecc3676e 26
AzureIoTClient 47:aaa262b5f898 27
AzureIoTClient 47:aaa262b5f898 28 #ifdef WINCE
AzureIoTClient 47:aaa262b5f898 29 #include <stdarg.h>
AzureIoTClient 47:aaa262b5f898 30 // Returns number of characters copied.
AzureIoTClient 47:aaa262b5f898 31 int snprintf(char * s, size_t n, const char * format, ...)
AzureIoTClient 47:aaa262b5f898 32 {
AzureIoTClient 48:cc5d91f2b06d 33 int result;
AzureIoTClient 48:cc5d91f2b06d 34 va_list args;
AzureIoTClient 48:cc5d91f2b06d 35 va_start(args, format);
AzureIoTClient 48:cc5d91f2b06d 36 result = vsnprintf(s, n, format, args);
AzureIoTClient 48:cc5d91f2b06d 37 va_end(args);
AzureIoTClient 48:cc5d91f2b06d 38 return result;
AzureIoTClient 47:aaa262b5f898 39 }
AzureIoTClient 47:aaa262b5f898 40 #endif
AzureIoTClient 47:aaa262b5f898 41
AzureIoTClient 47:aaa262b5f898 42
AzureIoTClient 42:448eecc3676e 43 /*Codes_SRS_IOTHUBCLIENT_LL_02_085: [ IoTHubClient_LL_UploadToBlob shall use the same authorization as step 1. to prepare and perform a HTTP request with the following parameters: ]*/
AzureIoTClient 42:448eecc3676e 44 #define FILE_UPLOAD_FAILED_BODY "{ \"isSuccess\":false, \"statusCode\":-1,\"statusDescription\" : \"client not able to connect with the server\" }"
AzureIoTClient 42:448eecc3676e 45
AzureIoTClient 42:448eecc3676e 46 #define AUTHORIZATION_SCHEME_VALUES \
AzureIoTClient 42:448eecc3676e 47 DEVICE_KEY, \
AzureIoTClient 46:6a69294b6119 48 X509, \
AzureIoTClient 42:448eecc3676e 49 SAS_TOKEN
AzureIoTClient 42:448eecc3676e 50 DEFINE_ENUM(AUTHORIZATION_SCHEME, AUTHORIZATION_SCHEME_VALUES);
AzureIoTClient 42:448eecc3676e 51
AzureIoTClient 46:6a69294b6119 52 typedef struct UPLOADTOBLOB_X509_CREDENTIALS_TAG
AzureIoTClient 46:6a69294b6119 53 {
AzureIoTClient 46:6a69294b6119 54 const char* x509certificate;
AzureIoTClient 46:6a69294b6119 55 const char* x509privatekey;
AzureIoTClient 46:6a69294b6119 56 }UPLOADTOBLOB_X509_CREDENTIALS;
AzureIoTClient 46:6a69294b6119 57
AzureIoTClient 42:448eecc3676e 58 typedef struct IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA_TAG
AzureIoTClient 42:448eecc3676e 59 {
AzureIoTClient 42:448eecc3676e 60 STRING_HANDLE deviceId; /*needed for file upload*/
AzureIoTClient 42:448eecc3676e 61 const char* hostname; /*needed for file upload*/
AzureIoTClient 42:448eecc3676e 62 AUTHORIZATION_SCHEME authorizationScheme; /*needed for file upload*/
AzureIoTClient 42:448eecc3676e 63 union {
AzureIoTClient 42:448eecc3676e 64 STRING_HANDLE deviceKey; /*used when authorizationScheme is DEVICE_KEY*/
AzureIoTClient 42:448eecc3676e 65 STRING_HANDLE sas; /*used when authorizationScheme is SAS_TOKEN*/
AzureIoTClient 46:6a69294b6119 66 UPLOADTOBLOB_X509_CREDENTIALS x509credentials; /*assumed to be used when both deviceKey and deviceSasToken are NULL*/
AzureIoTClient 42:448eecc3676e 67 } credentials; /*needed for file upload*/
AzureIoTClient 42:448eecc3676e 68 }IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA;
AzureIoTClient 42:448eecc3676e 69
AzureIoTClient 42:448eecc3676e 70 IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE IoTHubClient_LL_UploadToBlob_Create(const IOTHUB_CLIENT_CONFIG* config)
AzureIoTClient 42:448eecc3676e 71 {
AzureIoTClient 42:448eecc3676e 72 IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA* handleData = malloc(sizeof(IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA));
AzureIoTClient 42:448eecc3676e 73 if (handleData == NULL)
AzureIoTClient 42:448eecc3676e 74 {
AzureIoTClient 42:448eecc3676e 75 LogError("oom - malloc");
AzureIoTClient 42:448eecc3676e 76 /*return as is*/
AzureIoTClient 42:448eecc3676e 77 }
AzureIoTClient 42:448eecc3676e 78 else
AzureIoTClient 42:448eecc3676e 79 {
AzureIoTClient 42:448eecc3676e 80 size_t iotHubNameLength = strlen(config->iotHubName);
AzureIoTClient 42:448eecc3676e 81 size_t iotHubSuffixLength = strlen(config->iotHubSuffix);
AzureIoTClient 42:448eecc3676e 82 handleData->deviceId = STRING_construct(config->deviceId);
AzureIoTClient 42:448eecc3676e 83 if (handleData->deviceId == NULL)
AzureIoTClient 42:448eecc3676e 84 {
AzureIoTClient 42:448eecc3676e 85 LogError("unable to STRING_construct");
AzureIoTClient 42:448eecc3676e 86 free(handleData);
AzureIoTClient 42:448eecc3676e 87 handleData = NULL;
AzureIoTClient 42:448eecc3676e 88 }
AzureIoTClient 42:448eecc3676e 89 else
AzureIoTClient 42:448eecc3676e 90 {
AzureIoTClient 42:448eecc3676e 91 handleData->hostname = malloc(iotHubNameLength + 1 + iotHubSuffixLength + 1); /*first +1 is because "." the second +1 is because \0*/
AzureIoTClient 42:448eecc3676e 92 if (handleData->hostname == NULL)
AzureIoTClient 42:448eecc3676e 93 {
AzureIoTClient 42:448eecc3676e 94 LogError("malloc failed");
AzureIoTClient 42:448eecc3676e 95 STRING_delete(handleData->deviceId);
AzureIoTClient 42:448eecc3676e 96 free(handleData);
AzureIoTClient 42:448eecc3676e 97 handleData = NULL;
AzureIoTClient 42:448eecc3676e 98 }
AzureIoTClient 42:448eecc3676e 99 else
AzureIoTClient 42:448eecc3676e 100 {
AzureIoTClient 58:15b0d29b2667 101 (void)memcpy((char*)handleData->hostname, config->iotHubName, iotHubNameLength);
AzureIoTClient 42:448eecc3676e 102 ((char*)handleData->hostname)[iotHubNameLength] = '.';
AzureIoTClient 58:15b0d29b2667 103 (void)memcpy((char*)handleData->hostname + iotHubNameLength + 1, config->iotHubSuffix, iotHubSuffixLength + 1); /*+1 will copy the \0 too*/
AzureIoTClient 46:6a69294b6119 104 if ((config->deviceSasToken != NULL) && (config->deviceKey == NULL))
AzureIoTClient 42:448eecc3676e 105 {
AzureIoTClient 42:448eecc3676e 106 handleData->authorizationScheme = SAS_TOKEN;
AzureIoTClient 42:448eecc3676e 107 handleData->credentials.sas = STRING_construct(config->deviceSasToken);
AzureIoTClient 42:448eecc3676e 108 if (handleData->credentials.sas == NULL)
AzureIoTClient 42:448eecc3676e 109 {
AzureIoTClient 42:448eecc3676e 110 LogError("unable to STRING_construct");
AzureIoTClient 42:448eecc3676e 111 free((void*)handleData->hostname);
AzureIoTClient 42:448eecc3676e 112 STRING_delete(handleData->deviceId);
AzureIoTClient 42:448eecc3676e 113 free(handleData);
AzureIoTClient 42:448eecc3676e 114 handleData = NULL;
AzureIoTClient 42:448eecc3676e 115 }
AzureIoTClient 42:448eecc3676e 116 else
AzureIoTClient 42:448eecc3676e 117 {
AzureIoTClient 42:448eecc3676e 118 /*return as is*/
AzureIoTClient 42:448eecc3676e 119 }
AzureIoTClient 42:448eecc3676e 120 }
AzureIoTClient 46:6a69294b6119 121 else if ((config->deviceSasToken == NULL) && (config->deviceKey != NULL))
AzureIoTClient 42:448eecc3676e 122 {
AzureIoTClient 42:448eecc3676e 123 handleData->authorizationScheme = DEVICE_KEY;
AzureIoTClient 42:448eecc3676e 124 handleData->credentials.deviceKey = STRING_construct(config->deviceKey);
AzureIoTClient 42:448eecc3676e 125 if (handleData->credentials.deviceKey == NULL)
AzureIoTClient 42:448eecc3676e 126 {
AzureIoTClient 42:448eecc3676e 127 LogError("unable to STRING_construct");
AzureIoTClient 42:448eecc3676e 128 free((void*)handleData->hostname);
AzureIoTClient 42:448eecc3676e 129 STRING_delete(handleData->deviceId);
AzureIoTClient 42:448eecc3676e 130 free(handleData);
AzureIoTClient 42:448eecc3676e 131 handleData = NULL;
AzureIoTClient 42:448eecc3676e 132 }
AzureIoTClient 42:448eecc3676e 133 else
AzureIoTClient 42:448eecc3676e 134 {
AzureIoTClient 42:448eecc3676e 135 /*return as is*/
AzureIoTClient 42:448eecc3676e 136 }
AzureIoTClient 42:448eecc3676e 137 }
AzureIoTClient 46:6a69294b6119 138 else if ((config->deviceSasToken == NULL) && (config->deviceKey == NULL))
AzureIoTClient 46:6a69294b6119 139 {
AzureIoTClient 46:6a69294b6119 140 handleData->authorizationScheme = X509;
AzureIoTClient 46:6a69294b6119 141 handleData->credentials.x509credentials.x509certificate = NULL;
AzureIoTClient 46:6a69294b6119 142 handleData->credentials.x509credentials.x509privatekey = NULL;
AzureIoTClient 46:6a69294b6119 143 /*return as is*/
AzureIoTClient 46:6a69294b6119 144 }
AzureIoTClient 42:448eecc3676e 145 }
AzureIoTClient 42:448eecc3676e 146 }
AzureIoTClient 42:448eecc3676e 147 }
AzureIoTClient 42:448eecc3676e 148 return (IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE)handleData;
AzureIoTClient 42:448eecc3676e 149
AzureIoTClient 42:448eecc3676e 150 }
AzureIoTClient 42:448eecc3676e 151
AzureIoTClient 42:448eecc3676e 152 /*returns 0 when correlationId, sasUri contain data*/
AzureIoTClient 42:448eecc3676e 153 static int IoTHubClient_LL_UploadToBlob_step1and2(IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA* handleData, HTTPAPIEX_HANDLE iotHubHttpApiExHandle, HTTP_HEADERS_HANDLE requestHttpHeaders, const char* destinationFileName,
AzureIoTClient 42:448eecc3676e 154 STRING_HANDLE correlationId, STRING_HANDLE sasUri)
AzureIoTClient 42:448eecc3676e 155 {
AzureIoTClient 42:448eecc3676e 156 int result;
AzureIoTClient 42:448eecc3676e 157
AzureIoTClient 42:448eecc3676e 158 /*Codes_SRS_IOTHUBCLIENT_LL_02_066: [ IoTHubClient_LL_UploadToBlob shall create an HTTP relative path formed from "/devices/" + deviceId + "/files/" + destinationFileName + "?api-version=API_VERSION". ]*/
AzureIoTClient 42:448eecc3676e 159 STRING_HANDLE relativePath = STRING_construct("/devices/");
AzureIoTClient 42:448eecc3676e 160 if (relativePath == NULL)
AzureIoTClient 42:448eecc3676e 161 {
AzureIoTClient 42:448eecc3676e 162 /*Codes_SRS_IOTHUBCLIENT_LL_02_067: [ If creating the relativePath fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 163 LogError("unable to STRING_construct");
AzureIoTClient 60:41648c4e7036 164 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 165 }
AzureIoTClient 42:448eecc3676e 166 else
AzureIoTClient 42:448eecc3676e 167 {
AzureIoTClient 42:448eecc3676e 168 if (!(
AzureIoTClient 42:448eecc3676e 169 (STRING_concat_with_STRING(relativePath, handleData->deviceId) == 0) &&
AzureIoTClient 42:448eecc3676e 170 (STRING_concat(relativePath, "/files/") == 0) &&
AzureIoTClient 42:448eecc3676e 171 (STRING_concat(relativePath, destinationFileName) == 0) &&
AzureIoTClient 42:448eecc3676e 172 (STRING_concat(relativePath, API_VERSION) == 0)
AzureIoTClient 42:448eecc3676e 173 ))
AzureIoTClient 42:448eecc3676e 174 {
AzureIoTClient 42:448eecc3676e 175 /*Codes_SRS_IOTHUBCLIENT_LL_02_067: [ If creating the relativePath fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 176 LogError("unable to concatenate STRING");
AzureIoTClient 60:41648c4e7036 177 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 178 }
AzureIoTClient 42:448eecc3676e 179 else
AzureIoTClient 42:448eecc3676e 180 {
AzureIoTClient 42:448eecc3676e 181 /*Codes_SRS_IOTHUBCLIENT_LL_02_068: [ IoTHubClient_LL_UploadToBlob shall create an HTTP responseContent BUFFER_HANDLE. ]*/
AzureIoTClient 42:448eecc3676e 182 BUFFER_HANDLE responseContent = BUFFER_new();
AzureIoTClient 42:448eecc3676e 183 if (responseContent == NULL)
AzureIoTClient 42:448eecc3676e 184 {
AzureIoTClient 42:448eecc3676e 185 /*Codes_SRS_IOTHUBCLIENT_LL_02_069: [ If creating the HTTP response buffer handle fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 60:41648c4e7036 186 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 187 LogError("unable to BUFFER_new");
AzureIoTClient 42:448eecc3676e 188 }
AzureIoTClient 42:448eecc3676e 189 else
AzureIoTClient 42:448eecc3676e 190 {
AzureIoTClient 42:448eecc3676e 191 /*Codes_SRS_IOTHUBCLIENT_LL_02_072: [ IoTHubClient_LL_UploadToBlob shall add the following name:value to request HTTP headers: ] "Content-Type": "application/json" "Accept": "application/json" "User-Agent": "iothubclient/" IOTHUB_SDK_VERSION*/
AzureIoTClient 46:6a69294b6119 192 /*Codes_SRS_IOTHUBCLIENT_LL_02_107: [ - "Authorization" header shall not be build. ]*/
AzureIoTClient 42:448eecc3676e 193 if (!(
AzureIoTClient 42:448eecc3676e 194 (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "Content-Type", "application/json") == HTTP_HEADERS_OK) &&
AzureIoTClient 42:448eecc3676e 195 (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "Accept", "application/json") == HTTP_HEADERS_OK) &&
AzureIoTClient 42:448eecc3676e 196 (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "User-Agent", "iothubclient/" IOTHUB_SDK_VERSION) == HTTP_HEADERS_OK) &&
AzureIoTClient 46:6a69294b6119 197 (handleData->authorizationScheme==X509 || (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "Authorization", "") == HTTP_HEADERS_OK))
AzureIoTClient 42:448eecc3676e 198 ))
AzureIoTClient 42:448eecc3676e 199 {
AzureIoTClient 42:448eecc3676e 200 /*Codes_SRS_IOTHUBCLIENT_LL_02_071: [ If creating the HTTP headers fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 201 LogError("unable to HTTPHeaders_AddHeaderNameValuePair");
AzureIoTClient 60:41648c4e7036 202 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 203 }
AzureIoTClient 42:448eecc3676e 204 else
AzureIoTClient 42:448eecc3676e 205 {
AzureIoTClient 42:448eecc3676e 206 int wasIoTHubRequestSuccess = 0; /*!=0 means responseContent has a buffer that should be parsed by parson after executing the below switch*/
AzureIoTClient 46:6a69294b6119 207 /* set the result to error by default */
AzureIoTClient 60:41648c4e7036 208 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 209 switch (handleData->authorizationScheme)
AzureIoTClient 42:448eecc3676e 210 {
AzureIoTClient 42:448eecc3676e 211 default:
AzureIoTClient 42:448eecc3676e 212 {
AzureIoTClient 42:448eecc3676e 213 /*wasIoTHubRequestSuccess takes care of the return value*/
AzureIoTClient 42:448eecc3676e 214 LogError("Internal Error: unexpected value in handleData->authorizationScheme = %d", handleData->authorizationScheme);
AzureIoTClient 60:41648c4e7036 215 result = __FAILURE__;
AzureIoTClient 46:6a69294b6119 216 break;
AzureIoTClient 46:6a69294b6119 217 }
AzureIoTClient 46:6a69294b6119 218 case(X509):
AzureIoTClient 46:6a69294b6119 219 {
AzureIoTClient 46:6a69294b6119 220 unsigned int statusCode;
AzureIoTClient 46:6a69294b6119 221 /*Codes_SRS_IOTHUBCLIENT_LL_02_108: [ IoTHubClient_LL_UploadToBlob shall execute HTTPAPIEX_ExecuteRequest passing the following information for arguments: ]*/
AzureIoTClient 46:6a69294b6119 222 if (HTTPAPIEX_ExecuteRequest(
AzureIoTClient 46:6a69294b6119 223 iotHubHttpApiExHandle, /*HTTPAPIEX_HANDLE handle - the handle created at the beginning of `IoTHubClient_LL_UploadToBlob`*/
AzureIoTClient 46:6a69294b6119 224 HTTPAPI_REQUEST_GET, /*HTTPAPI_REQUEST_TYPE requestType - HTTPAPI_REQUEST_GET*/
AzureIoTClient 46:6a69294b6119 225 STRING_c_str(relativePath), /*const char* relativePath - the HTTP relative path*/
AzureIoTClient 46:6a69294b6119 226 requestHttpHeaders, /*HTTP_HEADERS_HANDLE requestHttpHeadersHandle - request HTTP headers*/
AzureIoTClient 46:6a69294b6119 227 NULL, /*BUFFER_HANDLE requestContent - NULL*/
AzureIoTClient 46:6a69294b6119 228 &statusCode, /*unsigned int* statusCode - the address of an unsigned int that will contain the HTTP status code*/
AzureIoTClient 46:6a69294b6119 229 NULL, /*HTTP_HEADERS_HANDLE responseHttpHeadersHandl - NULL*/
AzureIoTClient 46:6a69294b6119 230 responseContent /*BUFFER_HANDLE responseContent - the HTTP response BUFFER_HANDLE - responseContent*/
AzureIoTClient 46:6a69294b6119 231 ) != HTTPAPIEX_OK)
AzureIoTClient 46:6a69294b6119 232 {
AzureIoTClient 46:6a69294b6119 233 /*Codes_SRS_IOTHUBCLIENT_LL_02_076: [ If HTTPAPIEX_ExecuteRequest call fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 60:41648c4e7036 234 result = __FAILURE__;
AzureIoTClient 46:6a69294b6119 235 LogError("unable to HTTPAPIEX_ExecuteRequest");
AzureIoTClient 46:6a69294b6119 236 }
AzureIoTClient 46:6a69294b6119 237 else
AzureIoTClient 46:6a69294b6119 238 {
AzureIoTClient 46:6a69294b6119 239 /*Codes_SRS_IOTHUBCLIENT_LL_02_077: [ If HTTP statusCode is greater than or equal to 300 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 46:6a69294b6119 240 if (statusCode >= 300)
AzureIoTClient 46:6a69294b6119 241 {
AzureIoTClient 60:41648c4e7036 242 result = __FAILURE__;
AzureIoTClient 46:6a69294b6119 243 LogError("HTTP code was %u", statusCode);
AzureIoTClient 46:6a69294b6119 244 }
AzureIoTClient 46:6a69294b6119 245 else
AzureIoTClient 46:6a69294b6119 246 {
AzureIoTClient 46:6a69294b6119 247 wasIoTHubRequestSuccess = 1;
AzureIoTClient 46:6a69294b6119 248 }
AzureIoTClient 46:6a69294b6119 249 }
AzureIoTClient 42:448eecc3676e 250 break;
AzureIoTClient 42:448eecc3676e 251 }
AzureIoTClient 42:448eecc3676e 252 case (SAS_TOKEN):
AzureIoTClient 42:448eecc3676e 253 {
AzureIoTClient 42:448eecc3676e 254 const char* sasToken = STRING_c_str(handleData->credentials.sas);
AzureIoTClient 42:448eecc3676e 255 /*Codes_SRS_IOTHUBCLIENT_LL_02_073: [ If the credentials used to create handle have "sasToken" then IoTHubClient_LL_UploadToBlob shall add the following HTTP request headers: ]*/
AzureIoTClient 42:448eecc3676e 256 if (HTTPHeaders_ReplaceHeaderNameValuePair(requestHttpHeaders, "Authorization", sasToken) != HTTP_HEADERS_OK)
AzureIoTClient 42:448eecc3676e 257 {
AzureIoTClient 42:448eecc3676e 258 /*Codes_SRS_IOTHUBCLIENT_LL_02_074: [ If adding "Authorization" fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR ]*/
AzureIoTClient 60:41648c4e7036 259 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 260 LogError("unable to HTTPHeaders_AddHeaderNameValuePair");
AzureIoTClient 42:448eecc3676e 261 }
AzureIoTClient 42:448eecc3676e 262 else
AzureIoTClient 42:448eecc3676e 263 {
AzureIoTClient 42:448eecc3676e 264 unsigned int statusCode;
AzureIoTClient 42:448eecc3676e 265 /*Codes_SRS_IOTHUBCLIENT_LL_02_075: [ IoTHubClient_LL_UploadToBlob shall execute HTTPAPIEX_ExecuteRequest passing the following information for arguments: ]*/
AzureIoTClient 42:448eecc3676e 266 if (HTTPAPIEX_ExecuteRequest(
AzureIoTClient 42:448eecc3676e 267 iotHubHttpApiExHandle, /*HTTPAPIEX_HANDLE handle - the handle created at the beginning of `IoTHubClient_LL_UploadToBlob`*/
AzureIoTClient 42:448eecc3676e 268 HTTPAPI_REQUEST_GET, /*HTTPAPI_REQUEST_TYPE requestType - HTTPAPI_REQUEST_GET*/
AzureIoTClient 42:448eecc3676e 269 STRING_c_str(relativePath), /*const char* relativePath - the HTTP relative path*/
AzureIoTClient 42:448eecc3676e 270 requestHttpHeaders, /*HTTP_HEADERS_HANDLE requestHttpHeadersHandle - request HTTP headers*/
AzureIoTClient 42:448eecc3676e 271 NULL, /*BUFFER_HANDLE requestContent - NULL*/
AzureIoTClient 42:448eecc3676e 272 &statusCode, /*unsigned int* statusCode - the address of an unsigned int that will contain the HTTP status code*/
AzureIoTClient 42:448eecc3676e 273 NULL, /*HTTP_HEADERS_HANDLE responseHttpHeadersHandl - NULL*/
AzureIoTClient 42:448eecc3676e 274 responseContent /*BUFFER_HANDLE responseContent - the HTTP response BUFFER_HANDLE - responseContent*/
AzureIoTClient 42:448eecc3676e 275 ) != HTTPAPIEX_OK)
AzureIoTClient 42:448eecc3676e 276 {
AzureIoTClient 42:448eecc3676e 277 /*Codes_SRS_IOTHUBCLIENT_LL_02_076: [ If HTTPAPIEX_ExecuteRequest call fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 60:41648c4e7036 278 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 279 LogError("unable to HTTPAPIEX_ExecuteRequest");
AzureIoTClient 42:448eecc3676e 280 }
AzureIoTClient 42:448eecc3676e 281 else
AzureIoTClient 42:448eecc3676e 282 {
AzureIoTClient 42:448eecc3676e 283 /*Codes_SRS_IOTHUBCLIENT_LL_02_077: [ If HTTP statusCode is greater than or equal to 300 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 284 if (statusCode >= 300)
AzureIoTClient 42:448eecc3676e 285 {
AzureIoTClient 60:41648c4e7036 286 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 287 LogError("HTTP code was %u", statusCode);
AzureIoTClient 42:448eecc3676e 288 }
AzureIoTClient 42:448eecc3676e 289 else
AzureIoTClient 42:448eecc3676e 290 {
AzureIoTClient 42:448eecc3676e 291 wasIoTHubRequestSuccess = 1;
AzureIoTClient 42:448eecc3676e 292 }
AzureIoTClient 42:448eecc3676e 293 }
AzureIoTClient 42:448eecc3676e 294 }
AzureIoTClient 42:448eecc3676e 295 break;
AzureIoTClient 42:448eecc3676e 296 }
AzureIoTClient 42:448eecc3676e 297 case(DEVICE_KEY):
AzureIoTClient 42:448eecc3676e 298 {
AzureIoTClient 42:448eecc3676e 299 /*Codes_SRS_IOTHUBCLIENT_LL_02_078: [ If the credentials used to create handle have "deviceKey" then IoTHubClient_LL_UploadToBlob shall create an HTTPAPIEX_SAS_HANDLE passing as arguments: ]*/
AzureIoTClient 42:448eecc3676e 300 STRING_HANDLE uriResource = STRING_construct(handleData->hostname);
AzureIoTClient 42:448eecc3676e 301 if (uriResource == NULL)
AzureIoTClient 42:448eecc3676e 302 {
AzureIoTClient 42:448eecc3676e 303 /*Codes_SRS_IOTHUBCLIENT_LL_02_089: [ If creating the HTTPAPIEX_SAS_HANDLE fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 60:41648c4e7036 304 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 305 LogError("unable to STRING_construct");
AzureIoTClient 42:448eecc3676e 306 }
AzureIoTClient 42:448eecc3676e 307 else
AzureIoTClient 42:448eecc3676e 308 {
AzureIoTClient 42:448eecc3676e 309 if (!(
AzureIoTClient 42:448eecc3676e 310 (STRING_concat(uriResource, "/devices/") == 0) &&
AzureIoTClient 42:448eecc3676e 311 (STRING_concat_with_STRING(uriResource, handleData->deviceId) == 0)
AzureIoTClient 42:448eecc3676e 312 ))
AzureIoTClient 42:448eecc3676e 313 {
AzureIoTClient 42:448eecc3676e 314 /*Codes_SRS_IOTHUBCLIENT_LL_02_089: [ If creating the HTTPAPIEX_SAS_HANDLE fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 315 LogError("unable to STRING_concat_with_STRING");
AzureIoTClient 60:41648c4e7036 316 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 317 }
AzureIoTClient 42:448eecc3676e 318 else
AzureIoTClient 42:448eecc3676e 319 {
AzureIoTClient 42:448eecc3676e 320 STRING_HANDLE empty = STRING_new();
AzureIoTClient 42:448eecc3676e 321 if (empty == NULL)
AzureIoTClient 42:448eecc3676e 322 {
AzureIoTClient 42:448eecc3676e 323 LogError("unable to STRING_new");
AzureIoTClient 60:41648c4e7036 324 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 325 }
AzureIoTClient 42:448eecc3676e 326 else
AzureIoTClient 42:448eecc3676e 327 {
AzureIoTClient 42:448eecc3676e 328 /*Codes_SRS_IOTHUBCLIENT_LL_02_089: [ If creating the HTTPAPIEX_SAS_HANDLE fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 329 HTTPAPIEX_SAS_HANDLE sasHandle = HTTPAPIEX_SAS_Create(handleData->credentials.deviceKey, uriResource, empty);
AzureIoTClient 42:448eecc3676e 330 if (sasHandle == NULL)
AzureIoTClient 42:448eecc3676e 331 {
AzureIoTClient 42:448eecc3676e 332 LogError("unable to HTTPAPIEX_SAS_Create");
AzureIoTClient 60:41648c4e7036 333 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 334 }
AzureIoTClient 42:448eecc3676e 335 else
AzureIoTClient 42:448eecc3676e 336 {
AzureIoTClient 42:448eecc3676e 337 unsigned int statusCode;
AzureIoTClient 42:448eecc3676e 338 /*Codes_SRS_IOTHUBCLIENT_LL_02_090: [ IoTHubClient_LL_UploadToBlob shall call HTTPAPIEX_SAS_ExecuteRequest passing as arguments: ]*/
AzureIoTClient 42:448eecc3676e 339 if (HTTPAPIEX_SAS_ExecuteRequest(
AzureIoTClient 42:448eecc3676e 340 sasHandle, /*HTTPAPIEX_SAS_HANDLE sasHandle - the created HTTPAPIEX_SAS_HANDLE*/
AzureIoTClient 42:448eecc3676e 341 iotHubHttpApiExHandle, /*HTTPAPIEX_HANDLE handle - the created HTTPAPIEX_HANDLE*/
AzureIoTClient 42:448eecc3676e 342 HTTPAPI_REQUEST_GET, /*HTTPAPI_REQUEST_TYPE requestType - HTTPAPI_REQUEST_GET*/
AzureIoTClient 42:448eecc3676e 343 STRING_c_str(relativePath), /*const char* relativePath - the HTTP relative path*/
AzureIoTClient 42:448eecc3676e 344 requestHttpHeaders, /*HTTP_HEADERS_HANDLE requestHttpHeadersHandle - request HTTP headers*/
AzureIoTClient 42:448eecc3676e 345 NULL, /*BUFFER_HANDLE requestContent - NULL*/
AzureIoTClient 42:448eecc3676e 346 &statusCode, /*unsigned int* statusCode - the address of an unsigned int that will contain the HTTP status code*/
AzureIoTClient 42:448eecc3676e 347 NULL, /*HTTP_HEADERS_HANDLE responseHeadersHandle - NULL*/
AzureIoTClient 42:448eecc3676e 348 responseContent
AzureIoTClient 42:448eecc3676e 349 ) != HTTPAPIEX_OK)
AzureIoTClient 42:448eecc3676e 350 {
AzureIoTClient 42:448eecc3676e 351 /*Codes_SRS_IOTHUBCLIENT_LL_02_079: [ If HTTPAPIEX_SAS_ExecuteRequest fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 352 LogError("unable to HTTPAPIEX_SAS_ExecuteRequest");
AzureIoTClient 60:41648c4e7036 353 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 354 }
AzureIoTClient 42:448eecc3676e 355 else
AzureIoTClient 42:448eecc3676e 356 {
AzureIoTClient 42:448eecc3676e 357 if (statusCode >= 300)
AzureIoTClient 42:448eecc3676e 358 {
AzureIoTClient 42:448eecc3676e 359 /*Codes_SRS_IOTHUBCLIENT_LL_02_080: [ If status code is greater than or equal to 300 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 60:41648c4e7036 360 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 361 LogError("HTTP code was %u", statusCode);
AzureIoTClient 42:448eecc3676e 362 }
AzureIoTClient 42:448eecc3676e 363 else
AzureIoTClient 42:448eecc3676e 364 {
AzureIoTClient 42:448eecc3676e 365 wasIoTHubRequestSuccess = 1;
AzureIoTClient 42:448eecc3676e 366 }
AzureIoTClient 42:448eecc3676e 367 }
AzureIoTClient 42:448eecc3676e 368 HTTPAPIEX_SAS_Destroy(sasHandle);
AzureIoTClient 42:448eecc3676e 369 }
AzureIoTClient 42:448eecc3676e 370 STRING_delete(empty);
AzureIoTClient 42:448eecc3676e 371 }
AzureIoTClient 42:448eecc3676e 372 }
AzureIoTClient 42:448eecc3676e 373 STRING_delete(uriResource);
AzureIoTClient 42:448eecc3676e 374 }
AzureIoTClient 42:448eecc3676e 375 }
AzureIoTClient 42:448eecc3676e 376 } /*switch*/
AzureIoTClient 42:448eecc3676e 377
AzureIoTClient 42:448eecc3676e 378 if (wasIoTHubRequestSuccess == 0)
AzureIoTClient 42:448eecc3676e 379 {
AzureIoTClient 42:448eecc3676e 380 /*do nothing, shall be reported as an error*/
AzureIoTClient 42:448eecc3676e 381 }
AzureIoTClient 42:448eecc3676e 382 else
AzureIoTClient 42:448eecc3676e 383 {
AzureIoTClient 42:448eecc3676e 384 const unsigned char*responseContent_u_char = BUFFER_u_char(responseContent);
AzureIoTClient 42:448eecc3676e 385 size_t responseContent_length = BUFFER_length(responseContent);
AzureIoTClient 42:448eecc3676e 386 STRING_HANDLE responseAsString = STRING_from_byte_array(responseContent_u_char, responseContent_length);
AzureIoTClient 42:448eecc3676e 387 if (responseAsString == NULL)
AzureIoTClient 42:448eecc3676e 388 {
AzureIoTClient 60:41648c4e7036 389 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 390 LogError("unable to get the response as string");
AzureIoTClient 42:448eecc3676e 391 }
AzureIoTClient 42:448eecc3676e 392 else
AzureIoTClient 42:448eecc3676e 393 {
AzureIoTClient 42:448eecc3676e 394 /*Codes_SRS_IOTHUBCLIENT_LL_02_081: [ Otherwise, IoTHubClient_LL_UploadToBlob shall use parson to extract and save the following information from the response buffer: correlationID and SasUri. ]*/
AzureIoTClient 42:448eecc3676e 395 JSON_Value* allJson = json_parse_string(STRING_c_str(responseAsString));
AzureIoTClient 42:448eecc3676e 396 if (allJson == NULL)
AzureIoTClient 42:448eecc3676e 397 {
AzureIoTClient 42:448eecc3676e 398 /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 399 LogError("unable to json_parse_string");
AzureIoTClient 60:41648c4e7036 400 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 401 }
AzureIoTClient 42:448eecc3676e 402 else
AzureIoTClient 42:448eecc3676e 403 {
AzureIoTClient 42:448eecc3676e 404 JSON_Object* jsonObject = json_value_get_object(allJson);
AzureIoTClient 42:448eecc3676e 405 if (jsonObject == NULL)
AzureIoTClient 42:448eecc3676e 406 {
AzureIoTClient 42:448eecc3676e 407 /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 408 LogError("unable to json_value_get_object");
AzureIoTClient 60:41648c4e7036 409 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 410 }
AzureIoTClient 42:448eecc3676e 411 else
AzureIoTClient 42:448eecc3676e 412 {
AzureIoTClient 42:448eecc3676e 413 const char* json_correlationId;
AzureIoTClient 42:448eecc3676e 414 json_correlationId = json_object_get_string(jsonObject, "correlationId");
AzureIoTClient 42:448eecc3676e 415 if (json_correlationId == NULL)
AzureIoTClient 42:448eecc3676e 416 {
AzureIoTClient 42:448eecc3676e 417 /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 418 LogError("unable to json_object_get_string(jsonObject, \"correlationId\")");
AzureIoTClient 60:41648c4e7036 419 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 420 }
AzureIoTClient 42:448eecc3676e 421 else
AzureIoTClient 42:448eecc3676e 422 {
AzureIoTClient 42:448eecc3676e 423 if (STRING_copy(correlationId, json_correlationId) != 0)
AzureIoTClient 42:448eecc3676e 424 {
AzureIoTClient 42:448eecc3676e 425 /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 426 LogError("unable to copy json_correlationId");
AzureIoTClient 60:41648c4e7036 427 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 428 }
AzureIoTClient 42:448eecc3676e 429 else
AzureIoTClient 42:448eecc3676e 430 {
AzureIoTClient 42:448eecc3676e 431 const char* json_hostName = json_object_get_string(jsonObject, "hostName");
AzureIoTClient 42:448eecc3676e 432 if (json_hostName == NULL)
AzureIoTClient 42:448eecc3676e 433 {
AzureIoTClient 42:448eecc3676e 434 /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 435 LogError("unable to json_object_get_string(jsonObject, \"hostName\")");
AzureIoTClient 60:41648c4e7036 436 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 437 }
AzureIoTClient 42:448eecc3676e 438 else
AzureIoTClient 42:448eecc3676e 439 {
AzureIoTClient 42:448eecc3676e 440 const char* json_containerName = json_object_get_string(jsonObject, "containerName");
AzureIoTClient 42:448eecc3676e 441 if (json_containerName == NULL)
AzureIoTClient 42:448eecc3676e 442 {
AzureIoTClient 42:448eecc3676e 443 /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 444 LogError("unable to json_object_get_string(jsonObject, \"containerName\")");
AzureIoTClient 60:41648c4e7036 445 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 446 }
AzureIoTClient 42:448eecc3676e 447 else
AzureIoTClient 42:448eecc3676e 448 {
AzureIoTClient 42:448eecc3676e 449 const char* json_blobName = json_object_get_string(jsonObject, "blobName");
AzureIoTClient 42:448eecc3676e 450 if (json_blobName == NULL)
AzureIoTClient 42:448eecc3676e 451 {
AzureIoTClient 42:448eecc3676e 452 /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 453 LogError("unable to json_object_get_string(jsonObject, \"blobName\")");
AzureIoTClient 60:41648c4e7036 454 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 455 }
AzureIoTClient 42:448eecc3676e 456 else
AzureIoTClient 42:448eecc3676e 457 {
AzureIoTClient 42:448eecc3676e 458 const char* json_sasToken = json_object_get_string(jsonObject, "sasToken");
AzureIoTClient 42:448eecc3676e 459 if (json_sasToken == NULL)
AzureIoTClient 42:448eecc3676e 460 {
AzureIoTClient 42:448eecc3676e 461 /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 462 LogError("unable to json_object_get_string(jsonObject, \"sasToken\")");
AzureIoTClient 60:41648c4e7036 463 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 464 }
AzureIoTClient 42:448eecc3676e 465 else
AzureIoTClient 42:448eecc3676e 466 {
AzureIoTClient 42:448eecc3676e 467 /*good JSON received from the service*/
AzureIoTClient 42:448eecc3676e 468
AzureIoTClient 42:448eecc3676e 469 if (STRING_copy(sasUri, "https://") != 0)
AzureIoTClient 42:448eecc3676e 470 {
AzureIoTClient 42:448eecc3676e 471 /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 472 LogError("unable to STRING_copy");
AzureIoTClient 60:41648c4e7036 473 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 474 }
AzureIoTClient 42:448eecc3676e 475 else
AzureIoTClient 42:448eecc3676e 476 {
AzureIoTClient 42:448eecc3676e 477 if (!(
AzureIoTClient 42:448eecc3676e 478 (STRING_concat(sasUri, json_hostName) == 0) &&
AzureIoTClient 42:448eecc3676e 479 (STRING_concat(sasUri, "/") == 0) &&
AzureIoTClient 42:448eecc3676e 480 (STRING_concat(sasUri, json_containerName) == 0) &&
AzureIoTClient 42:448eecc3676e 481 (STRING_concat(sasUri, "/") == 0) &&
AzureIoTClient 42:448eecc3676e 482 (STRING_concat(sasUri, json_blobName) == 0) &&
AzureIoTClient 42:448eecc3676e 483 (STRING_concat(sasUri, json_sasToken) == 0)
AzureIoTClient 42:448eecc3676e 484 ))
AzureIoTClient 42:448eecc3676e 485 {
AzureIoTClient 42:448eecc3676e 486 /*Codes_SRS_IOTHUBCLIENT_LL_02_082: [ If extracting and saving the correlationId or SasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 487 LogError("unable to STRING_concat");
AzureIoTClient 60:41648c4e7036 488 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 489 }
AzureIoTClient 42:448eecc3676e 490 else
AzureIoTClient 42:448eecc3676e 491 {
AzureIoTClient 42:448eecc3676e 492 result = 0; /*success in step 1*/
AzureIoTClient 42:448eecc3676e 493 }
AzureIoTClient 42:448eecc3676e 494 }
AzureIoTClient 42:448eecc3676e 495 }
AzureIoTClient 42:448eecc3676e 496 }
AzureIoTClient 42:448eecc3676e 497 }
AzureIoTClient 42:448eecc3676e 498 }
AzureIoTClient 42:448eecc3676e 499 }
AzureIoTClient 42:448eecc3676e 500 }
AzureIoTClient 42:448eecc3676e 501 }
AzureIoTClient 42:448eecc3676e 502 json_value_free(allJson);
AzureIoTClient 42:448eecc3676e 503 }
AzureIoTClient 42:448eecc3676e 504 STRING_delete(responseAsString);
AzureIoTClient 42:448eecc3676e 505 }
AzureIoTClient 42:448eecc3676e 506 }
AzureIoTClient 42:448eecc3676e 507 }
AzureIoTClient 42:448eecc3676e 508 BUFFER_delete(responseContent);
AzureIoTClient 42:448eecc3676e 509 }
AzureIoTClient 42:448eecc3676e 510 }
AzureIoTClient 42:448eecc3676e 511 STRING_delete(relativePath);
AzureIoTClient 42:448eecc3676e 512 }
AzureIoTClient 42:448eecc3676e 513
AzureIoTClient 42:448eecc3676e 514 return result;
AzureIoTClient 42:448eecc3676e 515 }
AzureIoTClient 42:448eecc3676e 516
AzureIoTClient 42:448eecc3676e 517 /*returns 0 when the IoTHub has been informed about the file upload status*/
AzureIoTClient 42:448eecc3676e 518 static int IoTHubClient_LL_UploadToBlob_step3(IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA* handleData, STRING_HANDLE correlationId, HTTPAPIEX_HANDLE iotHubHttpApiExHandle, HTTP_HEADERS_HANDLE requestHttpHeaders, BUFFER_HANDLE messageBody)
AzureIoTClient 42:448eecc3676e 519 {
AzureIoTClient 42:448eecc3676e 520 int result;
AzureIoTClient 42:448eecc3676e 521 /*here is step 3. depending on the outcome of step 2 it needs to inform IoTHub about the file upload status*/
AzureIoTClient 42:448eecc3676e 522 /*if step 1 failed, there's nothing that step 3 needs to report.*/
AzureIoTClient 42:448eecc3676e 523 /*this POST "tries" to happen*/
AzureIoTClient 42:448eecc3676e 524
AzureIoTClient 42:448eecc3676e 525 /*Codes_SRS_IOTHUBCLIENT_LL_02_085: [ IoTHubClient_LL_UploadToBlob shall use the same authorization as step 1. to prepare and perform a HTTP request with the following parameters: ]*/
AzureIoTClient 42:448eecc3676e 526 STRING_HANDLE uriResource = STRING_construct(handleData->hostname);
AzureIoTClient 42:448eecc3676e 527 if (uriResource == NULL)
AzureIoTClient 42:448eecc3676e 528 {
AzureIoTClient 42:448eecc3676e 529 LogError("unable to construct URI");
AzureIoTClient 60:41648c4e7036 530 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 531 }
AzureIoTClient 42:448eecc3676e 532 else
AzureIoTClient 42:448eecc3676e 533 {
AzureIoTClient 42:448eecc3676e 534 if (!(
AzureIoTClient 42:448eecc3676e 535 (STRING_concat(uriResource, "/devices/") == 0) &&
AzureIoTClient 42:448eecc3676e 536 (STRING_concat_with_STRING(uriResource, handleData->deviceId) == 0) &&
AzureIoTClient 42:448eecc3676e 537 (STRING_concat(uriResource, "/files/notifications") == 0)
AzureIoTClient 42:448eecc3676e 538 ))
AzureIoTClient 42:448eecc3676e 539 {
AzureIoTClient 42:448eecc3676e 540 LogError("unable to STRING_concat");
AzureIoTClient 60:41648c4e7036 541 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 542 }
AzureIoTClient 42:448eecc3676e 543 else
AzureIoTClient 42:448eecc3676e 544 {
AzureIoTClient 42:448eecc3676e 545 STRING_HANDLE relativePathNotification = STRING_construct("/devices/");
AzureIoTClient 42:448eecc3676e 546 if (relativePathNotification == NULL)
AzureIoTClient 42:448eecc3676e 547 {
AzureIoTClient 60:41648c4e7036 548 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 549 LogError("unable to STRING_construct");
AzureIoTClient 42:448eecc3676e 550 }
AzureIoTClient 42:448eecc3676e 551 else
AzureIoTClient 42:448eecc3676e 552 {
AzureIoTClient 42:448eecc3676e 553 if (!(
AzureIoTClient 42:448eecc3676e 554 (STRING_concat_with_STRING(relativePathNotification, handleData->deviceId) == 0) &&
AzureIoTClient 42:448eecc3676e 555 (STRING_concat(relativePathNotification, "/files/notifications/") == 0) &&
AzureIoTClient 42:448eecc3676e 556 (STRING_concat(relativePathNotification, STRING_c_str(correlationId)) == 0) &&
AzureIoTClient 42:448eecc3676e 557 (STRING_concat(relativePathNotification, API_VERSION) == 0)
AzureIoTClient 42:448eecc3676e 558 ))
AzureIoTClient 42:448eecc3676e 559 {
AzureIoTClient 42:448eecc3676e 560 LogError("unable to STRING_concat_with_STRING");
AzureIoTClient 60:41648c4e7036 561 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 562 }
AzureIoTClient 42:448eecc3676e 563 else
AzureIoTClient 42:448eecc3676e 564 {
AzureIoTClient 42:448eecc3676e 565 /*Codes_SRS_IOTHUBCLIENT_LL_02_086: [ If performing the HTTP request fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 566 switch (handleData->authorizationScheme)
AzureIoTClient 42:448eecc3676e 567 {
AzureIoTClient 42:448eecc3676e 568 default:
AzureIoTClient 42:448eecc3676e 569 {
AzureIoTClient 42:448eecc3676e 570 LogError("internal error: unknown authorization Scheme");
AzureIoTClient 60:41648c4e7036 571 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 572 break;
AzureIoTClient 42:448eecc3676e 573 }
AzureIoTClient 46:6a69294b6119 574 case (X509):
AzureIoTClient 46:6a69294b6119 575 {
AzureIoTClient 46:6a69294b6119 576 unsigned int notificationStatusCode;
AzureIoTClient 46:6a69294b6119 577 if (HTTPAPIEX_ExecuteRequest(
AzureIoTClient 46:6a69294b6119 578 iotHubHttpApiExHandle,
AzureIoTClient 46:6a69294b6119 579 HTTPAPI_REQUEST_POST,
AzureIoTClient 46:6a69294b6119 580 STRING_c_str(relativePathNotification),
AzureIoTClient 46:6a69294b6119 581 requestHttpHeaders,
AzureIoTClient 46:6a69294b6119 582 messageBody,
AzureIoTClient 46:6a69294b6119 583 &notificationStatusCode,
AzureIoTClient 46:6a69294b6119 584 NULL,
AzureIoTClient 46:6a69294b6119 585 NULL) != HTTPAPIEX_OK)
AzureIoTClient 46:6a69294b6119 586 {
AzureIoTClient 46:6a69294b6119 587 LogError("unable to do HTTPAPIEX_ExecuteRequest");
AzureIoTClient 60:41648c4e7036 588 result = __FAILURE__;
AzureIoTClient 46:6a69294b6119 589 }
AzureIoTClient 46:6a69294b6119 590 else
AzureIoTClient 46:6a69294b6119 591 {
AzureIoTClient 46:6a69294b6119 592 if (notificationStatusCode >= 300)
AzureIoTClient 46:6a69294b6119 593 {
AzureIoTClient 46:6a69294b6119 594 /*Codes_SRS_IOTHUBCLIENT_LL_02_087: [If the statusCode of the HTTP request is greater than or equal to 300 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR]*/
AzureIoTClient 46:6a69294b6119 595 LogError("server didn't like the notification request");
AzureIoTClient 60:41648c4e7036 596 result = __FAILURE__;
AzureIoTClient 46:6a69294b6119 597 }
AzureIoTClient 46:6a69294b6119 598 else
AzureIoTClient 46:6a69294b6119 599 {
AzureIoTClient 46:6a69294b6119 600 result = 0;
AzureIoTClient 46:6a69294b6119 601 }
AzureIoTClient 46:6a69294b6119 602 }
AzureIoTClient 46:6a69294b6119 603 break;
AzureIoTClient 46:6a69294b6119 604 }
AzureIoTClient 42:448eecc3676e 605 case (DEVICE_KEY):
AzureIoTClient 42:448eecc3676e 606 {
AzureIoTClient 42:448eecc3676e 607 STRING_HANDLE empty = STRING_new();
AzureIoTClient 42:448eecc3676e 608 if (empty == NULL)
AzureIoTClient 42:448eecc3676e 609 {
AzureIoTClient 42:448eecc3676e 610 LogError("unable to STRING_new");
AzureIoTClient 60:41648c4e7036 611 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 612 }
AzureIoTClient 42:448eecc3676e 613 else
AzureIoTClient 42:448eecc3676e 614 {
AzureIoTClient 42:448eecc3676e 615 HTTPAPIEX_SAS_HANDLE sasHandle = HTTPAPIEX_SAS_Create(handleData->credentials.deviceKey, uriResource, empty);
AzureIoTClient 42:448eecc3676e 616 if (sasHandle == NULL)
AzureIoTClient 42:448eecc3676e 617 {
AzureIoTClient 42:448eecc3676e 618 LogError("unable to HTTPAPIEX_SAS_Create");
AzureIoTClient 60:41648c4e7036 619 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 620 }
AzureIoTClient 42:448eecc3676e 621 else
AzureIoTClient 42:448eecc3676e 622 {
AzureIoTClient 42:448eecc3676e 623 unsigned int statusCode;
AzureIoTClient 42:448eecc3676e 624 if (HTTPAPIEX_SAS_ExecuteRequest(
AzureIoTClient 42:448eecc3676e 625 sasHandle, /*HTTPAPIEX_SAS_HANDLE sasHandle - the created HTTPAPIEX_SAS_HANDLE*/
AzureIoTClient 42:448eecc3676e 626 iotHubHttpApiExHandle, /*HTTPAPIEX_HANDLE handle - the created HTTPAPIEX_HANDLE*/
AzureIoTClient 42:448eecc3676e 627 HTTPAPI_REQUEST_POST, /*HTTPAPI_REQUEST_TYPE requestType - HTTPAPI_REQUEST_GET*/
AzureIoTClient 42:448eecc3676e 628 STRING_c_str(relativePathNotification), /*const char* relativePath - the HTTP relative path*/
AzureIoTClient 42:448eecc3676e 629 requestHttpHeaders, /*HTTP_HEADERS_HANDLE requestHttpHeadersHandle - request HTTP headers*/
AzureIoTClient 42:448eecc3676e 630 messageBody, /*BUFFER_HANDLE requestContent*/
AzureIoTClient 42:448eecc3676e 631 &statusCode, /*unsigned int* statusCode - the address of an unsigned int that will contain the HTTP status code*/
AzureIoTClient 42:448eecc3676e 632 NULL, /*HTTP_HEADERS_HANDLE responseHeadersHandle - NULL*/
AzureIoTClient 42:448eecc3676e 633 NULL
AzureIoTClient 42:448eecc3676e 634 ) != HTTPAPIEX_OK)
AzureIoTClient 42:448eecc3676e 635 {
AzureIoTClient 42:448eecc3676e 636 /*Codes_SRS_IOTHUBCLIENT_LL_02_079: [ If HTTPAPIEX_SAS_ExecuteRequest fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 637 LogError("unable to HTTPAPIEX_SAS_ExecuteRequest");
AzureIoTClient 60:41648c4e7036 638 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 639 ;
AzureIoTClient 42:448eecc3676e 640 }
AzureIoTClient 42:448eecc3676e 641 else
AzureIoTClient 42:448eecc3676e 642 {
AzureIoTClient 42:448eecc3676e 643 if (statusCode >= 300)
AzureIoTClient 42:448eecc3676e 644 {
AzureIoTClient 42:448eecc3676e 645 /*Codes_SRS_IOTHUBCLIENT_LL_02_087: [If the statusCode of the HTTP request is greater than or equal to 300 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR]*/
AzureIoTClient 60:41648c4e7036 646 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 647 LogError("HTTP code was %u", statusCode);
AzureIoTClient 42:448eecc3676e 648 }
AzureIoTClient 42:448eecc3676e 649 else
AzureIoTClient 42:448eecc3676e 650 {
AzureIoTClient 42:448eecc3676e 651 result = 0;
AzureIoTClient 42:448eecc3676e 652 }
AzureIoTClient 42:448eecc3676e 653 }
AzureIoTClient 42:448eecc3676e 654 HTTPAPIEX_SAS_Destroy(sasHandle);
AzureIoTClient 42:448eecc3676e 655 }
AzureIoTClient 42:448eecc3676e 656 STRING_delete(empty);
AzureIoTClient 42:448eecc3676e 657 }
AzureIoTClient 42:448eecc3676e 658 break;
AzureIoTClient 42:448eecc3676e 659 }
AzureIoTClient 42:448eecc3676e 660 case(SAS_TOKEN):
AzureIoTClient 42:448eecc3676e 661 {
AzureIoTClient 42:448eecc3676e 662 unsigned int notificationStatusCode;
AzureIoTClient 42:448eecc3676e 663 if (HTTPAPIEX_ExecuteRequest(
AzureIoTClient 42:448eecc3676e 664 iotHubHttpApiExHandle,
AzureIoTClient 42:448eecc3676e 665 HTTPAPI_REQUEST_POST,
AzureIoTClient 42:448eecc3676e 666 STRING_c_str(relativePathNotification),
AzureIoTClient 42:448eecc3676e 667 requestHttpHeaders,
AzureIoTClient 42:448eecc3676e 668 messageBody,
AzureIoTClient 42:448eecc3676e 669 &notificationStatusCode,
AzureIoTClient 42:448eecc3676e 670 NULL,
AzureIoTClient 42:448eecc3676e 671 NULL) != HTTPAPIEX_OK)
AzureIoTClient 42:448eecc3676e 672 {
AzureIoTClient 42:448eecc3676e 673 LogError("unable to do HTTPAPIEX_ExecuteRequest");
AzureIoTClient 60:41648c4e7036 674 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 675 }
AzureIoTClient 42:448eecc3676e 676 else
AzureIoTClient 42:448eecc3676e 677 {
AzureIoTClient 42:448eecc3676e 678 if (notificationStatusCode >= 300)
AzureIoTClient 42:448eecc3676e 679 {
AzureIoTClient 42:448eecc3676e 680 /*Codes_SRS_IOTHUBCLIENT_LL_02_087: [If the statusCode of the HTTP request is greater than or equal to 300 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR]*/
AzureIoTClient 42:448eecc3676e 681 LogError("server didn't like the notification request");
AzureIoTClient 60:41648c4e7036 682 result = __FAILURE__;
AzureIoTClient 42:448eecc3676e 683 }
AzureIoTClient 42:448eecc3676e 684 else
AzureIoTClient 42:448eecc3676e 685 {
AzureIoTClient 42:448eecc3676e 686 result = 0;
AzureIoTClient 42:448eecc3676e 687 }
AzureIoTClient 42:448eecc3676e 688 }
AzureIoTClient 42:448eecc3676e 689 break;
AzureIoTClient 42:448eecc3676e 690 }
AzureIoTClient 42:448eecc3676e 691 } /*switch authorizationScheme*/
AzureIoTClient 42:448eecc3676e 692 }
AzureIoTClient 42:448eecc3676e 693 STRING_delete(relativePathNotification);
AzureIoTClient 42:448eecc3676e 694 }
AzureIoTClient 42:448eecc3676e 695 }
AzureIoTClient 42:448eecc3676e 696 STRING_delete(uriResource);
AzureIoTClient 42:448eecc3676e 697 }
AzureIoTClient 42:448eecc3676e 698 return result;
AzureIoTClient 42:448eecc3676e 699 }
AzureIoTClient 42:448eecc3676e 700
AzureIoTClient 42:448eecc3676e 701 IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadToBlob_Impl(IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE handle, const char* destinationFileName, const unsigned char* source, size_t size)
AzureIoTClient 42:448eecc3676e 702 {
AzureIoTClient 42:448eecc3676e 703 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 48:cc5d91f2b06d 704 BUFFER_HANDLE toBeTransmitted;
AzureIoTClient 48:cc5d91f2b06d 705 int requiredStringLength;
AzureIoTClient 48:cc5d91f2b06d 706 char* requiredString;
AzureIoTClient 42:448eecc3676e 707
AzureIoTClient 42:448eecc3676e 708 /*Codes_SRS_IOTHUBCLIENT_LL_02_061: [ If handle is NULL then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 709 /*Codes_SRS_IOTHUBCLIENT_LL_02_062: [ If destinationFileName is NULL then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 710 /*Codes_SRS_IOTHUBCLIENT_LL_02_063: [ If source is NULL and size is greater than 0 then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 42:448eecc3676e 711 if (
AzureIoTClient 42:448eecc3676e 712 (handle == NULL) ||
AzureIoTClient 42:448eecc3676e 713 (destinationFileName == NULL) ||
Azure.IoT Build 45:54c11b1b1407 714 ((source == NULL) && (size > 0))
AzureIoTClient 42:448eecc3676e 715 )
AzureIoTClient 42:448eecc3676e 716 {
AzureIoTClient 42:448eecc3676e 717 LogError("invalid argument detected handle=%p destinationFileName=%p source=%p size=%zu", handle, destinationFileName, source, size);
AzureIoTClient 42:448eecc3676e 718 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 42:448eecc3676e 719 }
AzureIoTClient 42:448eecc3676e 720 else
AzureIoTClient 42:448eecc3676e 721 {
AzureIoTClient 42:448eecc3676e 722 IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA*)handle;
AzureIoTClient 42:448eecc3676e 723
AzureIoTClient 42:448eecc3676e 724 /*Codes_SRS_IOTHUBCLIENT_LL_02_064: [ IoTHubClient_LL_UploadToBlob shall create an HTTPAPIEX_HANDLE to the IoTHub hostname. ]*/
AzureIoTClient 42:448eecc3676e 725 HTTPAPIEX_HANDLE iotHubHttpApiExHandle = HTTPAPIEX_Create(handleData->hostname);
AzureIoTClient 42:448eecc3676e 726
AzureIoTClient 42:448eecc3676e 727 /*Codes_SRS_IOTHUBCLIENT_LL_02_065: [ If creating the HTTPAPIEX_HANDLE fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 42:448eecc3676e 728 if (iotHubHttpApiExHandle == NULL)
AzureIoTClient 42:448eecc3676e 729 {
AzureIoTClient 42:448eecc3676e 730 LogError("unable to HTTPAPIEX_Create");
AzureIoTClient 42:448eecc3676e 731 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 732 }
AzureIoTClient 42:448eecc3676e 733 else
AzureIoTClient 42:448eecc3676e 734 {
AzureIoTClient 46:6a69294b6119 735 if (
AzureIoTClient 46:6a69294b6119 736 (handleData->authorizationScheme == X509) &&
AzureIoTClient 46:6a69294b6119 737
AzureIoTClient 46:6a69294b6119 738 /*transmit the x509certificate and x509privatekey*/
AzureIoTClient 46:6a69294b6119 739 /*Codes_SRS_IOTHUBCLIENT_LL_02_106: [ - x509certificate and x509privatekey saved options shall be passed on the HTTPAPIEX_SetOption ]*/
AzureIoTClient 46:6a69294b6119 740 (!(
AzureIoTClient 48:cc5d91f2b06d 741 (HTTPAPIEX_SetOption(iotHubHttpApiExHandle, OPTION_X509_CERT, handleData->credentials.x509credentials.x509certificate) == HTTPAPIEX_OK) &&
AzureIoTClient 48:cc5d91f2b06d 742 (HTTPAPIEX_SetOption(iotHubHttpApiExHandle, OPTION_X509_PRIVATE_KEY, handleData->credentials.x509credentials.x509privatekey) == HTTPAPIEX_OK)
AzureIoTClient 46:6a69294b6119 743 ))
AzureIoTClient 46:6a69294b6119 744 )
AzureIoTClient 42:448eecc3676e 745 {
AzureIoTClient 46:6a69294b6119 746 LogError("unable to HTTPAPIEX_SetOption for x509");
AzureIoTClient 42:448eecc3676e 747 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 748 }
AzureIoTClient 42:448eecc3676e 749 else
AzureIoTClient 42:448eecc3676e 750 {
AzureIoTClient 46:6a69294b6119 751
AzureIoTClient 46:6a69294b6119 752 STRING_HANDLE correlationId = STRING_new();
AzureIoTClient 46:6a69294b6119 753 if (correlationId == NULL)
AzureIoTClient 42:448eecc3676e 754 {
AzureIoTClient 42:448eecc3676e 755 LogError("unable to STRING_new");
AzureIoTClient 42:448eecc3676e 756 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 757 }
AzureIoTClient 42:448eecc3676e 758 else
AzureIoTClient 42:448eecc3676e 759 {
AzureIoTClient 46:6a69294b6119 760 STRING_HANDLE sasUri = STRING_new();
AzureIoTClient 46:6a69294b6119 761 if (sasUri == NULL)
AzureIoTClient 42:448eecc3676e 762 {
AzureIoTClient 46:6a69294b6119 763 LogError("unable to STRING_new");
AzureIoTClient 42:448eecc3676e 764 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 765 }
AzureIoTClient 42:448eecc3676e 766 else
AzureIoTClient 42:448eecc3676e 767 {
AzureIoTClient 46:6a69294b6119 768 /*Codes_SRS_IOTHUBCLIENT_LL_02_070: [ IoTHubClient_LL_UploadToBlob shall create request HTTP headers. ]*/
AzureIoTClient 46:6a69294b6119 769 HTTP_HEADERS_HANDLE requestHttpHeaders = HTTPHeaders_Alloc(); /*these are build by step 1 and used by step 3 too*/
AzureIoTClient 46:6a69294b6119 770 if (requestHttpHeaders == NULL)
AzureIoTClient 42:448eecc3676e 771 {
AzureIoTClient 46:6a69294b6119 772 LogError("unable to HTTPHeaders_Alloc");
AzureIoTClient 42:448eecc3676e 773 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 774 }
AzureIoTClient 42:448eecc3676e 775 else
AzureIoTClient 42:448eecc3676e 776 {
AzureIoTClient 46:6a69294b6119 777 /*do step 1*/
AzureIoTClient 46:6a69294b6119 778 if (IoTHubClient_LL_UploadToBlob_step1and2(handleData, iotHubHttpApiExHandle, requestHttpHeaders, destinationFileName, correlationId, sasUri) != 0)
AzureIoTClient 42:448eecc3676e 779 {
AzureIoTClient 46:6a69294b6119 780 LogError("error in IoTHubClient_LL_UploadToBlob_step1");
AzureIoTClient 42:448eecc3676e 781 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 782 }
AzureIoTClient 42:448eecc3676e 783 else
AzureIoTClient 42:448eecc3676e 784 {
AzureIoTClient 46:6a69294b6119 785 /*do step 2.*/
AzureIoTClient 42:448eecc3676e 786
AzureIoTClient 46:6a69294b6119 787 unsigned int httpResponse;
AzureIoTClient 46:6a69294b6119 788 BUFFER_HANDLE responseToIoTHub = BUFFER_new();
AzureIoTClient 46:6a69294b6119 789 if (responseToIoTHub == NULL)
AzureIoTClient 46:6a69294b6119 790 {
AzureIoTClient 42:448eecc3676e 791 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 46:6a69294b6119 792 LogError("unable to BUFFER_new");
AzureIoTClient 42:448eecc3676e 793 }
AzureIoTClient 42:448eecc3676e 794 else
AzureIoTClient 42:448eecc3676e 795 {
AzureIoTClient 46:6a69294b6119 796 int step2success;
AzureIoTClient 46:6a69294b6119 797 /*Codes_SRS_IOTHUBCLIENT_LL_02_083: [ IoTHubClient_LL_UploadToBlob shall call Blob_UploadFromSasUri and capture the HTTP return code and HTTP body. ]*/
AzureIoTClient 46:6a69294b6119 798 step2success = (Blob_UploadFromSasUri(STRING_c_str(sasUri), source, size, &httpResponse, responseToIoTHub) == BLOB_OK);
AzureIoTClient 46:6a69294b6119 799 if (!step2success)
AzureIoTClient 46:6a69294b6119 800 {
AzureIoTClient 46:6a69294b6119 801 /*Codes_SRS_IOTHUBCLIENT_LL_02_084: [ If Blob_UploadFromSasUri fails then IoTHubClient_LL_UploadToBlob shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 46:6a69294b6119 802 LogError("unable to Blob_UploadFromSasUri");
AzureIoTClient 42:448eecc3676e 803
AzureIoTClient 46:6a69294b6119 804 /*do step 3*/ /*try*/
AzureIoTClient 46:6a69294b6119 805 /*Codes_SRS_IOTHUBCLIENT_LL_02_091: [ If step 2 fails without establishing an HTTP dialogue, then the HTTP message body shall look like: ]*/
AzureIoTClient 46:6a69294b6119 806 if (BUFFER_build(responseToIoTHub, (const unsigned char*)FILE_UPLOAD_FAILED_BODY, sizeof(FILE_UPLOAD_FAILED_BODY) / sizeof(FILE_UPLOAD_FAILED_BODY[0])) == 0)
AzureIoTClient 46:6a69294b6119 807 {
AzureIoTClient 46:6a69294b6119 808 if (IoTHubClient_LL_UploadToBlob_step3(handleData, correlationId, iotHubHttpApiExHandle, requestHttpHeaders, responseToIoTHub) != 0)
AzureIoTClient 46:6a69294b6119 809 {
AzureIoTClient 46:6a69294b6119 810 LogError("IoTHubClient_LL_UploadToBlob_step3 failed");
AzureIoTClient 46:6a69294b6119 811 }
AzureIoTClient 46:6a69294b6119 812 }
AzureIoTClient 42:448eecc3676e 813 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 814 }
AzureIoTClient 42:448eecc3676e 815 else
AzureIoTClient 42:448eecc3676e 816 {
AzureIoTClient 46:6a69294b6119 817 /*must make a json*/
AzureIoTClient 46:6a69294b6119 818
AzureIoTClient 47:aaa262b5f898 819 requiredStringLength = snprintf(NULL, 0, "{\"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":\"%s\"}", ((httpResponse < 300) ? "true" : "false"), httpResponse, BUFFER_u_char(responseToIoTHub));
AzureIoTClient 46:6a69294b6119 820
AzureIoTClient 47:aaa262b5f898 821 requiredString = malloc(requiredStringLength + 1);
AzureIoTClient 46:6a69294b6119 822 if (requiredString == 0)
AzureIoTClient 42:448eecc3676e 823 {
AzureIoTClient 46:6a69294b6119 824 LogError("unable to malloc");
AzureIoTClient 42:448eecc3676e 825 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 826 }
AzureIoTClient 42:448eecc3676e 827 else
AzureIoTClient 42:448eecc3676e 828 {
AzureIoTClient 46:6a69294b6119 829 /*do again snprintf*/
AzureIoTClient 46:6a69294b6119 830 (void)snprintf(requiredString, requiredStringLength + 1, "{\"isSuccess\":%s, \"statusCode\":%d, \"statusDescription\":\"%s\"}", ((httpResponse < 300) ? "true" : "false"), httpResponse, BUFFER_u_char(responseToIoTHub));
AzureIoTClient 47:aaa262b5f898 831 toBeTransmitted = BUFFER_create((const unsigned char*)requiredString, requiredStringLength);
AzureIoTClient 46:6a69294b6119 832 if (toBeTransmitted == NULL)
AzureIoTClient 42:448eecc3676e 833 {
AzureIoTClient 46:6a69294b6119 834 LogError("unable to BUFFER_create");
AzureIoTClient 42:448eecc3676e 835 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 42:448eecc3676e 836 }
AzureIoTClient 42:448eecc3676e 837 else
AzureIoTClient 42:448eecc3676e 838 {
AzureIoTClient 46:6a69294b6119 839 if (IoTHubClient_LL_UploadToBlob_step3(handleData, correlationId, iotHubHttpApiExHandle, requestHttpHeaders, toBeTransmitted) != 0)
AzureIoTClient 46:6a69294b6119 840 {
AzureIoTClient 46:6a69294b6119 841 LogError("IoTHubClient_LL_UploadToBlob_step3 failed");
AzureIoTClient 46:6a69294b6119 842 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 46:6a69294b6119 843 }
AzureIoTClient 46:6a69294b6119 844 else
AzureIoTClient 46:6a69294b6119 845 {
AzureIoTClient 46:6a69294b6119 846 result = (httpResponse < 300) ? IOTHUB_CLIENT_OK : IOTHUB_CLIENT_ERROR;
AzureIoTClient 46:6a69294b6119 847 }
AzureIoTClient 46:6a69294b6119 848 BUFFER_delete(toBeTransmitted);
AzureIoTClient 42:448eecc3676e 849 }
AzureIoTClient 46:6a69294b6119 850 free(requiredString);
AzureIoTClient 42:448eecc3676e 851 }
AzureIoTClient 42:448eecc3676e 852 }
AzureIoTClient 46:6a69294b6119 853 BUFFER_delete(responseToIoTHub);
AzureIoTClient 42:448eecc3676e 854 }
AzureIoTClient 42:448eecc3676e 855 }
AzureIoTClient 46:6a69294b6119 856 HTTPHeaders_Free(requestHttpHeaders);
AzureIoTClient 42:448eecc3676e 857 }
AzureIoTClient 46:6a69294b6119 858 STRING_delete(sasUri);
AzureIoTClient 42:448eecc3676e 859 }
AzureIoTClient 46:6a69294b6119 860 STRING_delete(correlationId);
AzureIoTClient 42:448eecc3676e 861 }
AzureIoTClient 42:448eecc3676e 862 }
AzureIoTClient 42:448eecc3676e 863 HTTPAPIEX_Destroy(iotHubHttpApiExHandle);
AzureIoTClient 42:448eecc3676e 864 }
AzureIoTClient 42:448eecc3676e 865 }
AzureIoTClient 42:448eecc3676e 866 return result;
AzureIoTClient 42:448eecc3676e 867 }
AzureIoTClient 42:448eecc3676e 868
AzureIoTClient 42:448eecc3676e 869 void IoTHubClient_LL_UploadToBlob_Destroy(IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE handle)
AzureIoTClient 42:448eecc3676e 870 {
AzureIoTClient 42:448eecc3676e 871 if (handle == NULL)
AzureIoTClient 42:448eecc3676e 872 {
AzureIoTClient 42:448eecc3676e 873 LogError("unexepected NULL argument");
AzureIoTClient 42:448eecc3676e 874 }
AzureIoTClient 42:448eecc3676e 875 else
AzureIoTClient 42:448eecc3676e 876 {
AzureIoTClient 42:448eecc3676e 877 IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA*)handle;
AzureIoTClient 42:448eecc3676e 878 switch (handleData->authorizationScheme)
AzureIoTClient 42:448eecc3676e 879 {
AzureIoTClient 42:448eecc3676e 880 case(SAS_TOKEN):
AzureIoTClient 42:448eecc3676e 881 {
AzureIoTClient 42:448eecc3676e 882 STRING_delete(handleData->credentials.sas);
AzureIoTClient 42:448eecc3676e 883 break;
AzureIoTClient 42:448eecc3676e 884 }
AzureIoTClient 42:448eecc3676e 885 case(DEVICE_KEY):
AzureIoTClient 42:448eecc3676e 886 {
AzureIoTClient 42:448eecc3676e 887 STRING_delete(handleData->credentials.deviceKey);
AzureIoTClient 42:448eecc3676e 888 break;
AzureIoTClient 42:448eecc3676e 889 }
AzureIoTClient 46:6a69294b6119 890 case(X509):
AzureIoTClient 46:6a69294b6119 891 {
AzureIoTClient 46:6a69294b6119 892 if (handleData->credentials.x509credentials.x509certificate != NULL)
AzureIoTClient 46:6a69294b6119 893 {
AzureIoTClient 46:6a69294b6119 894 free((void*)handleData->credentials.x509credentials.x509certificate);
AzureIoTClient 46:6a69294b6119 895 }
AzureIoTClient 46:6a69294b6119 896 if (handleData->credentials.x509credentials.x509privatekey != NULL)
AzureIoTClient 46:6a69294b6119 897 {
AzureIoTClient 46:6a69294b6119 898 free((void*)handleData->credentials.x509credentials.x509privatekey);
AzureIoTClient 46:6a69294b6119 899 }
AzureIoTClient 46:6a69294b6119 900 break;
AzureIoTClient 46:6a69294b6119 901 }
AzureIoTClient 42:448eecc3676e 902 default:
AzureIoTClient 42:448eecc3676e 903 {
AzureIoTClient 42:448eecc3676e 904 LogError("INTERNAL ERROR");
AzureIoTClient 42:448eecc3676e 905 break;
AzureIoTClient 42:448eecc3676e 906 }
AzureIoTClient 42:448eecc3676e 907 }
AzureIoTClient 42:448eecc3676e 908 free((void*)handleData->hostname);
AzureIoTClient 42:448eecc3676e 909 STRING_delete(handleData->deviceId);
AzureIoTClient 42:448eecc3676e 910 free(handleData);
AzureIoTClient 42:448eecc3676e 911 }
AzureIoTClient 42:448eecc3676e 912 }
AzureIoTClient 46:6a69294b6119 913
AzureIoTClient 46:6a69294b6119 914 IOTHUB_CLIENT_RESULT IoTHubClient_LL_UploadToBlob_SetOption(IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE handle, const char* optionName, const void* value)
AzureIoTClient 46:6a69294b6119 915 {
AzureIoTClient 46:6a69294b6119 916 IOTHUB_CLIENT_RESULT result;
AzureIoTClient 46:6a69294b6119 917 /*Codes_SRS_IOTHUBCLIENT_LL_02_110: [ If parameter handle is NULL then IoTHubClient_LL_UploadToBlob_SetOption shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 46:6a69294b6119 918 if (handle == NULL)
AzureIoTClient 46:6a69294b6119 919 {
AzureIoTClient 46:6a69294b6119 920 LogError("invalid argument detected: IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE handle=%p, const char* optionName=%s, const void* value=%p", handle, optionName, value);
AzureIoTClient 46:6a69294b6119 921 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 46:6a69294b6119 922 }
AzureIoTClient 46:6a69294b6119 923 else
AzureIoTClient 46:6a69294b6119 924 {
AzureIoTClient 46:6a69294b6119 925 IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA* handleData = (IOTHUB_CLIENT_LL_UPLOADTOBLOB_HANDLE_DATA*)handle;
AzureIoTClient 46:6a69294b6119 926
AzureIoTClient 46:6a69294b6119 927 /*Codes_SRS_IOTHUBCLIENT_LL_02_100: [ x509certificate - then value then is a null terminated string that contains the x509 certificate. ]*/
AzureIoTClient 48:cc5d91f2b06d 928 if (strcmp(optionName, OPTION_X509_CERT) == 0)
AzureIoTClient 46:6a69294b6119 929 {
AzureIoTClient 46:6a69294b6119 930 /*Codes_SRS_IOTHUBCLIENT_LL_02_109: [ If the authentication scheme is NOT x509 then IoTHubClient_LL_UploadToBlob_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 46:6a69294b6119 931 if (handleData->authorizationScheme != X509)
AzureIoTClient 46:6a69294b6119 932 {
AzureIoTClient 46:6a69294b6119 933 LogError("trying to set a x509 certificate while the authentication scheme is not x509");
AzureIoTClient 46:6a69294b6119 934 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 46:6a69294b6119 935 }
AzureIoTClient 46:6a69294b6119 936 else
AzureIoTClient 46:6a69294b6119 937 {
AzureIoTClient 46:6a69294b6119 938 /*Codes_SRS_IOTHUBCLIENT_LL_02_103: [ The options shall be saved. ]*/
AzureIoTClient 46:6a69294b6119 939 /*try to make a copy of the certificate*/
AzureIoTClient 46:6a69294b6119 940 char* temp;
AzureIoTClient 46:6a69294b6119 941 if (mallocAndStrcpy_s(&temp, value) != 0)
AzureIoTClient 46:6a69294b6119 942 {
AzureIoTClient 46:6a69294b6119 943 /*Codes_SRS_IOTHUBCLIENT_LL_02_104: [ If saving fails, then IoTHubClient_LL_UploadToBlob_SetOption shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 46:6a69294b6119 944 LogError("unable to mallocAndStrcpy_s");
AzureIoTClient 46:6a69294b6119 945 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 46:6a69294b6119 946 }
AzureIoTClient 46:6a69294b6119 947 else
AzureIoTClient 46:6a69294b6119 948 {
AzureIoTClient 46:6a69294b6119 949 /*Codes_SRS_IOTHUBCLIENT_LL_02_105: [ Otherwise IoTHubClient_LL_UploadToBlob_SetOption shall succeed and return IOTHUB_CLIENT_OK. ]*/
AzureIoTClient 46:6a69294b6119 950 if (handleData->credentials.x509credentials.x509certificate != NULL) /*free any previous values, if any*/
AzureIoTClient 46:6a69294b6119 951 {
AzureIoTClient 46:6a69294b6119 952 free((void*)handleData->credentials.x509credentials.x509certificate);
AzureIoTClient 46:6a69294b6119 953 }
AzureIoTClient 46:6a69294b6119 954 handleData->credentials.x509credentials.x509certificate = temp;
AzureIoTClient 46:6a69294b6119 955 result = IOTHUB_CLIENT_OK;
AzureIoTClient 46:6a69294b6119 956 }
AzureIoTClient 46:6a69294b6119 957 }
AzureIoTClient 46:6a69294b6119 958 }
AzureIoTClient 46:6a69294b6119 959 /*Codes_SRS_IOTHUBCLIENT_LL_02_101: [ x509privatekey - then value is a null terminated string that contains the x509 privatekey. ]*/
AzureIoTClient 48:cc5d91f2b06d 960 else if (strcmp(optionName, OPTION_X509_PRIVATE_KEY) == 0)
AzureIoTClient 46:6a69294b6119 961 {
AzureIoTClient 46:6a69294b6119 962 /*Codes_SRS_IOTHUBCLIENT_LL_02_109: [ If the authentication scheme is NOT x509 then IoTHubClient_LL_UploadToBlob_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 46:6a69294b6119 963 if (handleData->authorizationScheme != X509)
AzureIoTClient 46:6a69294b6119 964 {
AzureIoTClient 46:6a69294b6119 965 LogError("trying to set a x509 privatekey while the authentication scheme is not x509");
AzureIoTClient 46:6a69294b6119 966 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 46:6a69294b6119 967 }
AzureIoTClient 46:6a69294b6119 968 else
AzureIoTClient 46:6a69294b6119 969 {
AzureIoTClient 46:6a69294b6119 970 /*Codes_SRS_IOTHUBCLIENT_LL_02_103: [ The options shall be saved. ]*/
AzureIoTClient 46:6a69294b6119 971 /*try to make a copy of the privatekey*/
AzureIoTClient 46:6a69294b6119 972 char* temp;
AzureIoTClient 46:6a69294b6119 973 if (mallocAndStrcpy_s(&temp, value) != 0)
AzureIoTClient 46:6a69294b6119 974 {
AzureIoTClient 46:6a69294b6119 975 /*Codes_SRS_IOTHUBCLIENT_LL_02_104: [ If saving fails, then IoTHubClient_LL_UploadToBlob_SetOption shall fail and return IOTHUB_CLIENT_ERROR. ]*/
AzureIoTClient 46:6a69294b6119 976 LogError("unable to mallocAndStrcpy_s");
AzureIoTClient 46:6a69294b6119 977 result = IOTHUB_CLIENT_ERROR;
AzureIoTClient 46:6a69294b6119 978 }
AzureIoTClient 46:6a69294b6119 979 else
AzureIoTClient 46:6a69294b6119 980 {
AzureIoTClient 46:6a69294b6119 981 /*Codes_SRS_IOTHUBCLIENT_LL_02_105: [ Otherwise IoTHubClient_LL_UploadToBlob_SetOption shall succeed and return IOTHUB_CLIENT_OK. ]*/
AzureIoTClient 46:6a69294b6119 982 if (handleData->credentials.x509credentials.x509privatekey != NULL) /*free any previous values, if any*/
AzureIoTClient 46:6a69294b6119 983 {
AzureIoTClient 46:6a69294b6119 984 free((void*)handleData->credentials.x509credentials.x509privatekey);
AzureIoTClient 46:6a69294b6119 985 }
AzureIoTClient 46:6a69294b6119 986 handleData->credentials.x509credentials.x509privatekey = temp;
AzureIoTClient 46:6a69294b6119 987 result = IOTHUB_CLIENT_OK;
AzureIoTClient 46:6a69294b6119 988 }
AzureIoTClient 46:6a69294b6119 989 }
AzureIoTClient 46:6a69294b6119 990 }
AzureIoTClient 46:6a69294b6119 991 else
AzureIoTClient 46:6a69294b6119 992 {
AzureIoTClient 46:6a69294b6119 993 /*Codes_SRS_IOTHUBCLIENT_LL_02_102: [ If an unknown option is presented then IoTHubClient_LL_UploadToBlob_SetOption shall return IOTHUB_CLIENT_INVALID_ARG. ]*/
AzureIoTClient 46:6a69294b6119 994 result = IOTHUB_CLIENT_INVALID_ARG;
AzureIoTClient 46:6a69294b6119 995 }
AzureIoTClient 46:6a69294b6119 996 }
AzureIoTClient 46:6a69294b6119 997 return result;
AzureIoTClient 46:6a69294b6119 998 }
AzureIoTClient 46:6a69294b6119 999
AzureIoTClient 46:6a69294b6119 1000
Azure.IoT Build 45:54c11b1b1407 1001 #endif /*DONT_USE_UPLOADTOBLOB*/
AzureIoTClient 46:6a69294b6119 1002
AzureIoTClient 46:6a69294b6119 1003