Simple sample that demonstrates reading the FXOS8700CQ accelerometer, convert the data to JSON and send to an Azure IoT Hub.

Dependencies:   azure_umqtt_c iothub_mqtt_transport mbed-rtos mbed wolfSSL Socket lwip-eth lwip-sys lwip

Committer:
markrad
Date:
Tue Apr 25 01:33:13 2017 +0000
Revision:
7:2564d95cbf81
Parent:
3:c0556ff7b8e3
Fix bug in NTP library. Clean up code some.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 3:c0556ff7b8e3 1 // Copyright (c) Microsoft. All rights reserved.
markrad 3:c0556ff7b8e3 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
markrad 3:c0556ff7b8e3 3
markrad 3:c0556ff7b8e3 4 #include <stdlib.h>
markrad 3:c0556ff7b8e3 5 #ifdef _CRTDBG_MAP_ALLOC
markrad 3:c0556ff7b8e3 6 #include <crtdbg.h>
markrad 3:c0556ff7b8e3 7 #endif
markrad 3:c0556ff7b8e3 8 #include "azure_c_shared_utility/gballoc.h"
markrad 3:c0556ff7b8e3 9
markrad 3:c0556ff7b8e3 10 #include "blob.h"
markrad 3:c0556ff7b8e3 11
markrad 3:c0556ff7b8e3 12 #include "azure_c_shared_utility/httpapiex.h"
markrad 3:c0556ff7b8e3 13 #include "azure_c_shared_utility/xlogging.h"
markrad 3:c0556ff7b8e3 14 #include "azure_c_shared_utility/base64.h"
markrad 3:c0556ff7b8e3 15
markrad 3:c0556ff7b8e3 16 /*a block has 4MB*/
markrad 3:c0556ff7b8e3 17 #define BLOCK_SIZE (4*1024*1024)
markrad 3:c0556ff7b8e3 18
markrad 3:c0556ff7b8e3 19 BLOB_RESULT Blob_UploadFromSasUri(const char* SASURI, const unsigned char* source, size_t size, unsigned int* httpStatus, BUFFER_HANDLE httpResponse)
markrad 3:c0556ff7b8e3 20 {
markrad 3:c0556ff7b8e3 21 BLOB_RESULT result;
markrad 3:c0556ff7b8e3 22 /*Codes_SRS_BLOB_02_001: [ If SASURI is NULL then Blob_UploadFromSasUri shall fail and return BLOB_INVALID_ARG. ]*/
markrad 3:c0556ff7b8e3 23 if (SASURI == NULL)
markrad 3:c0556ff7b8e3 24 {
markrad 3:c0556ff7b8e3 25 LogError("parameter SASURI is NULL");
markrad 3:c0556ff7b8e3 26 result = BLOB_INVALID_ARG;
markrad 3:c0556ff7b8e3 27 }
markrad 3:c0556ff7b8e3 28 else
markrad 3:c0556ff7b8e3 29 {
markrad 3:c0556ff7b8e3 30 /*Codes_SRS_BLOB_02_002: [ If source is NULL and size is not zero then Blob_UploadFromSasUri shall fail and return BLOB_INVALID_ARG. ]*/
markrad 3:c0556ff7b8e3 31 if (
markrad 3:c0556ff7b8e3 32 (size > 0) &&
markrad 3:c0556ff7b8e3 33 (source == NULL)
markrad 3:c0556ff7b8e3 34 )
markrad 3:c0556ff7b8e3 35 {
markrad 3:c0556ff7b8e3 36 LogError("combination of source = %p and size = %zu is invalid", source, size);
markrad 3:c0556ff7b8e3 37 result = BLOB_INVALID_ARG;
markrad 3:c0556ff7b8e3 38 }
markrad 3:c0556ff7b8e3 39 /*Codes_SRS_BLOB_02_034: [ If size is bigger than 50000*4*1024*1024 then Blob_UploadFromSasUri shall fail and return BLOB_INVALID_ARG. ]*/
markrad 3:c0556ff7b8e3 40 else if (size > 50000ULL * 4 * 1024 * 1024) /*https://msdn.microsoft.com/en-us/library/azure/dd179467.aspx says "Each block can be a different size, up to a maximum of 4 MB, and a block blob can include a maximum of 50,000 blocks."*/
markrad 3:c0556ff7b8e3 41 {
markrad 3:c0556ff7b8e3 42 LogError("size too big (%zu)", size);
markrad 3:c0556ff7b8e3 43 result = BLOB_INVALID_ARG;
markrad 3:c0556ff7b8e3 44 }
markrad 3:c0556ff7b8e3 45 else
markrad 3:c0556ff7b8e3 46 {
markrad 3:c0556ff7b8e3 47 /*Codes_SRS_BLOB_02_017: [ Blob_UploadFromSasUri shall copy from SASURI the hostname to a new const char* ]*/
markrad 3:c0556ff7b8e3 48 /*Codes_SRS_BLOB_02_004: [ Blob_UploadFromSasUri shall copy from SASURI the hostname to a new const char*. ]*/
markrad 3:c0556ff7b8e3 49 /*to find the hostname, the following logic is applied:*/
markrad 3:c0556ff7b8e3 50 /*the hostname starts at the first character after "://"*/
markrad 3:c0556ff7b8e3 51 /*the hostname ends at the first character before the next "/" after "://"*/
markrad 3:c0556ff7b8e3 52 const char* hostnameBegin = strstr(SASURI, "://");
markrad 3:c0556ff7b8e3 53 if (hostnameBegin == NULL)
markrad 3:c0556ff7b8e3 54 {
markrad 3:c0556ff7b8e3 55 /*Codes_SRS_BLOB_02_005: [ If the hostname cannot be determined, then Blob_UploadFromSasUri shall fail and return BLOB_INVALID_ARG. ]*/
markrad 3:c0556ff7b8e3 56 LogError("hostname cannot be determined");
markrad 3:c0556ff7b8e3 57 result = BLOB_INVALID_ARG;
markrad 3:c0556ff7b8e3 58 }
markrad 3:c0556ff7b8e3 59 else
markrad 3:c0556ff7b8e3 60 {
markrad 3:c0556ff7b8e3 61 hostnameBegin += 3; /*have to skip 3 characters which are "://"*/
markrad 3:c0556ff7b8e3 62 const char* hostnameEnd = strchr(hostnameBegin, '/');
markrad 3:c0556ff7b8e3 63 if (hostnameEnd == NULL)
markrad 3:c0556ff7b8e3 64 {
markrad 3:c0556ff7b8e3 65 /*Codes_SRS_BLOB_02_005: [ If the hostname cannot be determined, then Blob_UploadFromSasUri shall fail and return BLOB_INVALID_ARG. ]*/
markrad 3:c0556ff7b8e3 66 LogError("hostname cannot be determined");
markrad 3:c0556ff7b8e3 67 result = BLOB_INVALID_ARG;
markrad 3:c0556ff7b8e3 68 }
markrad 3:c0556ff7b8e3 69 else
markrad 3:c0556ff7b8e3 70 {
markrad 3:c0556ff7b8e3 71 size_t hostnameSize = hostnameEnd - hostnameBegin;
markrad 3:c0556ff7b8e3 72 char* hostname = (char*)malloc(hostnameSize + 1); /*+1 because of '\0' at the end*/
markrad 3:c0556ff7b8e3 73 if (hostname == NULL)
markrad 3:c0556ff7b8e3 74 {
markrad 3:c0556ff7b8e3 75 /*Codes_SRS_BLOB_02_016: [ If the hostname copy cannot be made then then Blob_UploadFromSasUri shall fail and return BLOB_ERROR ]*/
markrad 3:c0556ff7b8e3 76 LogError("oom - out of memory");
markrad 3:c0556ff7b8e3 77 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 78 }
markrad 3:c0556ff7b8e3 79 else
markrad 3:c0556ff7b8e3 80 {
markrad 3:c0556ff7b8e3 81 HTTPAPIEX_HANDLE httpApiExHandle;
markrad 3:c0556ff7b8e3 82 memcpy(hostname, hostnameBegin, hostnameSize);
markrad 3:c0556ff7b8e3 83 hostname[hostnameSize] = '\0';
markrad 3:c0556ff7b8e3 84
markrad 3:c0556ff7b8e3 85 /*Codes_SRS_BLOB_02_006: [ Blob_UploadFromSasUri shall create a new HTTPAPI_EX_HANDLE by calling HTTPAPIEX_Create passing the hostname. ]*/
markrad 3:c0556ff7b8e3 86 /*Codes_SRS_BLOB_02_018: [ Blob_UploadFromSasUri shall create a new HTTPAPI_EX_HANDLE by calling HTTPAPIEX_Create passing the hostname. ]*/
markrad 3:c0556ff7b8e3 87 httpApiExHandle = HTTPAPIEX_Create(hostname);
markrad 3:c0556ff7b8e3 88 if (httpApiExHandle == NULL)
markrad 3:c0556ff7b8e3 89 {
markrad 3:c0556ff7b8e3 90 /*Codes_SRS_BLOB_02_007: [ If HTTPAPIEX_Create fails then Blob_UploadFromSasUri shall fail and return BLOB_ERROR. ]*/
markrad 3:c0556ff7b8e3 91 LogError("unable to create a HTTPAPIEX_HANDLE");
markrad 3:c0556ff7b8e3 92 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 93 }
markrad 3:c0556ff7b8e3 94 else
markrad 3:c0556ff7b8e3 95 {
markrad 3:c0556ff7b8e3 96 /*Codes_SRS_BLOB_02_008: [ Blob_UploadFromSasUri shall compute the relative path of the request from the SASURI parameter. ]*/
markrad 3:c0556ff7b8e3 97 /*Codes_SRS_BLOB_02_019: [ Blob_UploadFromSasUri shall compute the base relative path of the request from the SASURI parameter. ]*/
markrad 3:c0556ff7b8e3 98 const char* relativePath = hostnameEnd; /*this is where the relative path begins in the SasUri*/
markrad 3:c0556ff7b8e3 99
markrad 3:c0556ff7b8e3 100 if (size < 64 * 1024 * 1024) /*code path for sizes <64MB*/
markrad 3:c0556ff7b8e3 101 {
markrad 3:c0556ff7b8e3 102 /*Codes_SRS_BLOB_02_010: [ Blob_UploadFromSasUri shall create a BUFFER_HANDLE from source and size parameters. ]*/
markrad 3:c0556ff7b8e3 103 BUFFER_HANDLE requestBuffer = BUFFER_create(source, size);
markrad 3:c0556ff7b8e3 104 if (requestBuffer == NULL)
markrad 3:c0556ff7b8e3 105 {
markrad 3:c0556ff7b8e3 106 /*Codes_SRS_BLOB_02_011: [ If any of the previous steps related to building the HTTPAPI_EX_ExecuteRequest parameters fails, then Blob_UploadFromSasUri shall fail and return BLOB_ERROR. ]*/
markrad 3:c0556ff7b8e3 107 LogError("unable to BUFFER_create");
markrad 3:c0556ff7b8e3 108 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 109 }
markrad 3:c0556ff7b8e3 110 else
markrad 3:c0556ff7b8e3 111 {
markrad 3:c0556ff7b8e3 112 /*Codes_SRS_BLOB_02_009: [ Blob_UploadFromSasUri shall create an HTTP_HEADERS_HANDLE for the request HTTP headers carrying the following headers: ]*/
markrad 3:c0556ff7b8e3 113 HTTP_HEADERS_HANDLE requestHttpHeaders = HTTPHeaders_Alloc();
markrad 3:c0556ff7b8e3 114 if (requestHttpHeaders == NULL)
markrad 3:c0556ff7b8e3 115 {
markrad 3:c0556ff7b8e3 116 /*Codes_SRS_BLOB_02_011: [ If any of the previous steps related to building the HTTPAPI_EX_ExecuteRequest parameters fails, then Blob_UploadFromSasUri shall fail and return BLOB_ERROR. ]*/
markrad 3:c0556ff7b8e3 117 LogError("unable to HTTPHeaders_Alloc");
markrad 3:c0556ff7b8e3 118 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 119 }
markrad 3:c0556ff7b8e3 120 else
markrad 3:c0556ff7b8e3 121 {
markrad 3:c0556ff7b8e3 122 if (HTTPHeaders_AddHeaderNameValuePair(requestHttpHeaders, "x-ms-blob-type", "BlockBlob") != HTTP_HEADERS_OK)
markrad 3:c0556ff7b8e3 123 {
markrad 3:c0556ff7b8e3 124 /*Codes_SRS_BLOB_02_011: [ If any of the previous steps related to building the HTTPAPI_EX_ExecuteRequest parameters fails, then Blob_UploadFromSasUri shall fail and return BLOB_ERROR. ]*/
markrad 3:c0556ff7b8e3 125 LogError("unable to HTTPHeaders_AddHeaderNameValuePair");
markrad 3:c0556ff7b8e3 126 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 127 }
markrad 3:c0556ff7b8e3 128 else
markrad 3:c0556ff7b8e3 129 {
markrad 3:c0556ff7b8e3 130 /*Codes_SRS_BLOB_02_012: [ Blob_UploadFromSasUri shall call HTTPAPIEX_ExecuteRequest passing the parameters previously build, httpStatus and httpResponse ]*/
markrad 3:c0556ff7b8e3 131 if (HTTPAPIEX_ExecuteRequest(httpApiExHandle, HTTPAPI_REQUEST_PUT, relativePath, requestHttpHeaders, requestBuffer, httpStatus, NULL, httpResponse) != HTTPAPIEX_OK)
markrad 3:c0556ff7b8e3 132 {
markrad 3:c0556ff7b8e3 133 /*Codes_SRS_BLOB_02_013: [ If HTTPAPIEX_ExecuteRequest fails, then Blob_UploadFromSasUri shall fail and return BLOB_HTTP_ERROR. ]*/
markrad 3:c0556ff7b8e3 134 LogError("failed to HTTPAPIEX_ExecuteRequest");
markrad 3:c0556ff7b8e3 135 result = BLOB_HTTP_ERROR;
markrad 3:c0556ff7b8e3 136 }
markrad 3:c0556ff7b8e3 137 else
markrad 3:c0556ff7b8e3 138 {
markrad 3:c0556ff7b8e3 139 /*Codes_SRS_BLOB_02_015: [ Otherwise, HTTPAPIEX_ExecuteRequest shall succeed and return BLOB_OK. ]*/
markrad 3:c0556ff7b8e3 140 result = BLOB_OK;
markrad 3:c0556ff7b8e3 141 }
markrad 3:c0556ff7b8e3 142 }
markrad 3:c0556ff7b8e3 143 HTTPHeaders_Free(requestHttpHeaders);
markrad 3:c0556ff7b8e3 144 }
markrad 3:c0556ff7b8e3 145 BUFFER_delete(requestBuffer);
markrad 3:c0556ff7b8e3 146 }
markrad 3:c0556ff7b8e3 147 }
markrad 3:c0556ff7b8e3 148 else /*code path for size >= 64MB*/
markrad 3:c0556ff7b8e3 149 {
markrad 3:c0556ff7b8e3 150 size_t toUpload = size;
markrad 3:c0556ff7b8e3 151 /*Codes_SRS_BLOB_02_028: [ Blob_UploadFromSasUri shall construct an XML string with the following content: ]*/
markrad 3:c0556ff7b8e3 152 STRING_HANDLE xml = STRING_construct("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<BlockList>"); /*the XML "build as we go"*/
markrad 3:c0556ff7b8e3 153 if (xml == NULL)
markrad 3:c0556ff7b8e3 154 {
markrad 3:c0556ff7b8e3 155 /*Codes_SRS_BLOB_02_033: [ If any previous operation that doesn't have an explicit failure description fails then Blob_UploadFromSasUri shall fail and return BLOB_ERROR ]*/
markrad 3:c0556ff7b8e3 156 LogError("failed to STRING_construct");
markrad 3:c0556ff7b8e3 157 result = BLOB_HTTP_ERROR;
markrad 3:c0556ff7b8e3 158 }
markrad 3:c0556ff7b8e3 159 else
markrad 3:c0556ff7b8e3 160 {
markrad 3:c0556ff7b8e3 161 /*Codes_SRS_BLOB_02_021: [ For every block of 4MB the following operations shall happen: ]*/
markrad 3:c0556ff7b8e3 162 unsigned int blockID = 0;
markrad 3:c0556ff7b8e3 163 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 164
markrad 3:c0556ff7b8e3 165 int isError = 0; /*used to cleanly exit the loop*/
markrad 3:c0556ff7b8e3 166 do
markrad 3:c0556ff7b8e3 167 {
markrad 3:c0556ff7b8e3 168 /*setting this block size*/
markrad 3:c0556ff7b8e3 169 size_t thisBlockSize = (toUpload > BLOCK_SIZE) ? BLOCK_SIZE : toUpload;
markrad 3:c0556ff7b8e3 170 /*Codes_SRS_BLOB_02_020: [ Blob_UploadFromSasUri shall construct a BASE64 encoded string from the block ID (000000... 0499999) ]*/
markrad 3:c0556ff7b8e3 171 char temp[7]; /*this will contain 000000... 049999*/
markrad 3:c0556ff7b8e3 172 if (sprintf(temp, "%6u", (unsigned int)blockID) != 6) /*produces 000000... 049999*/
markrad 3:c0556ff7b8e3 173 {
markrad 3:c0556ff7b8e3 174 /*Codes_SRS_BLOB_02_033: [ If any previous operation that doesn't have an explicit failure description fails then Blob_UploadFromSasUri shall fail and return BLOB_ERROR ]*/
markrad 3:c0556ff7b8e3 175 LogError("failed to sprintf");
markrad 3:c0556ff7b8e3 176 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 177 isError = 1;
markrad 3:c0556ff7b8e3 178 }
markrad 3:c0556ff7b8e3 179 else
markrad 3:c0556ff7b8e3 180 {
markrad 3:c0556ff7b8e3 181 STRING_HANDLE blockIdString = Base64_Encode_Bytes((const unsigned char*)temp, 6);
markrad 3:c0556ff7b8e3 182 if (blockIdString == NULL)
markrad 3:c0556ff7b8e3 183 {
markrad 3:c0556ff7b8e3 184 /*Codes_SRS_BLOB_02_033: [ If any previous operation that doesn't have an explicit failure description fails then Blob_UploadFromSasUri shall fail and return BLOB_ERROR ]*/
markrad 3:c0556ff7b8e3 185 LogError("unable to Base64_Encode_Bytes");
markrad 3:c0556ff7b8e3 186 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 187 isError = 1;
markrad 3:c0556ff7b8e3 188 }
markrad 3:c0556ff7b8e3 189 else
markrad 3:c0556ff7b8e3 190 {
markrad 3:c0556ff7b8e3 191 /*add the blockId base64 encoded to the XML*/
markrad 3:c0556ff7b8e3 192 if (!(
markrad 3:c0556ff7b8e3 193 (STRING_concat(xml, "<Latest>")==0) &&
markrad 3:c0556ff7b8e3 194 (STRING_concat_with_STRING(xml, blockIdString)==0) &&
markrad 3:c0556ff7b8e3 195 (STRING_concat(xml, "</Latest>") == 0)
markrad 3:c0556ff7b8e3 196 ))
markrad 3:c0556ff7b8e3 197 {
markrad 3:c0556ff7b8e3 198 /*Codes_SRS_BLOB_02_033: [ If any previous operation that doesn't have an explicit failure description fails then Blob_UploadFromSasUri shall fail and return BLOB_ERROR ]*/
markrad 3:c0556ff7b8e3 199 LogError("unable to STRING_concat");
markrad 3:c0556ff7b8e3 200 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 201 isError = 1;
markrad 3:c0556ff7b8e3 202 }
markrad 3:c0556ff7b8e3 203 else
markrad 3:c0556ff7b8e3 204 {
markrad 3:c0556ff7b8e3 205 /*Codes_SRS_BLOB_02_022: [ Blob_UploadFromSasUri shall construct a new relativePath from following string: base relativePath + "&comp=block&blockid=BASE64 encoded string of blockId" ]*/
markrad 3:c0556ff7b8e3 206 STRING_HANDLE newRelativePath = STRING_construct(relativePath);
markrad 3:c0556ff7b8e3 207 if (newRelativePath == NULL)
markrad 3:c0556ff7b8e3 208 {
markrad 3:c0556ff7b8e3 209 /*Codes_SRS_BLOB_02_033: [ If any previous operation that doesn't have an explicit failure description fails then Blob_UploadFromSasUri shall fail and return BLOB_ERROR ]*/
markrad 3:c0556ff7b8e3 210 LogError("unable to STRING_construct");
markrad 3:c0556ff7b8e3 211 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 212 isError = 1;
markrad 3:c0556ff7b8e3 213 }
markrad 3:c0556ff7b8e3 214 else
markrad 3:c0556ff7b8e3 215 {
markrad 3:c0556ff7b8e3 216 if (!(
markrad 3:c0556ff7b8e3 217 (STRING_concat(newRelativePath, "&comp=block&blockid=") == 0) &&
markrad 3:c0556ff7b8e3 218 (STRING_concat_with_STRING(newRelativePath, blockIdString) == 0)
markrad 3:c0556ff7b8e3 219 ))
markrad 3:c0556ff7b8e3 220 {
markrad 3:c0556ff7b8e3 221 /*Codes_SRS_BLOB_02_033: [ If any previous operation that doesn't have an explicit failure description fails then Blob_UploadFromSasUri shall fail and return BLOB_ERROR ]*/
markrad 3:c0556ff7b8e3 222 LogError("unable to STRING concatenate");
markrad 3:c0556ff7b8e3 223 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 224 isError = 1;
markrad 3:c0556ff7b8e3 225 }
markrad 3:c0556ff7b8e3 226 else
markrad 3:c0556ff7b8e3 227 {
markrad 3:c0556ff7b8e3 228 /*Codes_SRS_BLOB_02_023: [ Blob_UploadFromSasUri shall create a BUFFER_HANDLE from source and size parameters. ]*/
markrad 3:c0556ff7b8e3 229 BUFFER_HANDLE requestContent = BUFFER_create(source + (size - toUpload), thisBlockSize);
markrad 3:c0556ff7b8e3 230 if (requestContent == NULL)
markrad 3:c0556ff7b8e3 231 {
markrad 3:c0556ff7b8e3 232 /*Codes_SRS_BLOB_02_033: [ If any previous operation that doesn't have an explicit failure description fails then Blob_UploadFromSasUri shall fail and return BLOB_ERROR ]*/
markrad 3:c0556ff7b8e3 233 LogError("unable to BUFFER_create");
markrad 3:c0556ff7b8e3 234 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 235 isError = 1;
markrad 3:c0556ff7b8e3 236 }
markrad 3:c0556ff7b8e3 237 else
markrad 3:c0556ff7b8e3 238 {
markrad 3:c0556ff7b8e3 239 /*Codes_SRS_BLOB_02_024: [ Blob_UploadFromSasUri shall call HTTPAPIEX_ExecuteRequest with a PUT operation, passing httpStatus and httpResponse. ]*/
markrad 3:c0556ff7b8e3 240 if (HTTPAPIEX_ExecuteRequest(
markrad 3:c0556ff7b8e3 241 httpApiExHandle,
markrad 3:c0556ff7b8e3 242 HTTPAPI_REQUEST_PUT,
markrad 3:c0556ff7b8e3 243 STRING_c_str(newRelativePath),
markrad 3:c0556ff7b8e3 244 NULL,
markrad 3:c0556ff7b8e3 245 requestContent,
markrad 3:c0556ff7b8e3 246 httpStatus,
markrad 3:c0556ff7b8e3 247 NULL,
markrad 3:c0556ff7b8e3 248 httpResponse) != HTTPAPIEX_OK
markrad 3:c0556ff7b8e3 249 )
markrad 3:c0556ff7b8e3 250 {
markrad 3:c0556ff7b8e3 251 /*Codes_SRS_BLOB_02_025: [ If HTTPAPIEX_ExecuteRequest fails then Blob_UploadFromSasUri shall fail and return BLOB_HTTP_ERROR. ]*/
markrad 3:c0556ff7b8e3 252 LogError("unable to HTTPAPIEX_ExecuteRequest");
markrad 3:c0556ff7b8e3 253 result = BLOB_HTTP_ERROR;
markrad 3:c0556ff7b8e3 254 isError = 1;
markrad 3:c0556ff7b8e3 255 }
markrad 3:c0556ff7b8e3 256 else if (*httpStatus >= 300)
markrad 3:c0556ff7b8e3 257 {
markrad 3:c0556ff7b8e3 258 /*Codes_SRS_BLOB_02_026: [ Otherwise, if HTTP response code is >=300 then Blob_UploadFromSasUri shall succeed and return BLOB_OK. ]*/
markrad 3:c0556ff7b8e3 259 LogError("HTTP status from storage does not indicate success (%d)", (int)*httpStatus);
markrad 3:c0556ff7b8e3 260 result = BLOB_OK;
markrad 3:c0556ff7b8e3 261 isError = 1;
markrad 3:c0556ff7b8e3 262 }
markrad 3:c0556ff7b8e3 263 else
markrad 3:c0556ff7b8e3 264 {
markrad 3:c0556ff7b8e3 265 /*Codes_SRS_BLOB_02_027: [ Otherwise Blob_UploadFromSasUri shall continue execution. ]*/
markrad 3:c0556ff7b8e3 266 }
markrad 3:c0556ff7b8e3 267 BUFFER_delete(requestContent);
markrad 3:c0556ff7b8e3 268 }
markrad 3:c0556ff7b8e3 269 }
markrad 3:c0556ff7b8e3 270 STRING_delete(newRelativePath);
markrad 3:c0556ff7b8e3 271 }
markrad 3:c0556ff7b8e3 272 }
markrad 3:c0556ff7b8e3 273 STRING_delete(blockIdString);
markrad 3:c0556ff7b8e3 274 }
markrad 3:c0556ff7b8e3 275 }
markrad 3:c0556ff7b8e3 276
markrad 3:c0556ff7b8e3 277 blockID++;
markrad 3:c0556ff7b8e3 278 toUpload -= thisBlockSize;
markrad 3:c0556ff7b8e3 279 } while ((toUpload > 0) && !isError);
markrad 3:c0556ff7b8e3 280
markrad 3:c0556ff7b8e3 281 if (isError)
markrad 3:c0556ff7b8e3 282 {
markrad 3:c0556ff7b8e3 283 /*do nothing, it will be reported "as is"*/
markrad 3:c0556ff7b8e3 284 }
markrad 3:c0556ff7b8e3 285 else
markrad 3:c0556ff7b8e3 286 {
markrad 3:c0556ff7b8e3 287 /*complete the XML*/
markrad 3:c0556ff7b8e3 288 if (STRING_concat(xml, "</BlockList>") != 0)
markrad 3:c0556ff7b8e3 289 {
markrad 3:c0556ff7b8e3 290 /*Codes_SRS_BLOB_02_033: [ If any previous operation that doesn't have an explicit failure description fails then Blob_UploadFromSasUri shall fail and return BLOB_ERROR ]*/
markrad 3:c0556ff7b8e3 291 LogError("failed to STRING_concat");
markrad 3:c0556ff7b8e3 292 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 293 }
markrad 3:c0556ff7b8e3 294 else
markrad 3:c0556ff7b8e3 295 {
markrad 3:c0556ff7b8e3 296 /*Codes_SRS_BLOB_02_029: [Blob_UploadFromSasUri shall construct a new relativePath from following string : base relativePath + "&comp=blocklist"]*/
markrad 3:c0556ff7b8e3 297 STRING_HANDLE newRelativePath = STRING_construct(relativePath);
markrad 3:c0556ff7b8e3 298 if (newRelativePath == NULL)
markrad 3:c0556ff7b8e3 299 {
markrad 3:c0556ff7b8e3 300 /*Codes_SRS_BLOB_02_033: [ If any previous operation that doesn't have an explicit failure description fails then Blob_UploadFromSasUri shall fail and return BLOB_ERROR ]*/
markrad 3:c0556ff7b8e3 301 LogError("failed to STRING_construct");
markrad 3:c0556ff7b8e3 302 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 303 }
markrad 3:c0556ff7b8e3 304 else
markrad 3:c0556ff7b8e3 305 {
markrad 3:c0556ff7b8e3 306 if (STRING_concat(newRelativePath, "&comp=blocklist") != 0)
markrad 3:c0556ff7b8e3 307 {
markrad 3:c0556ff7b8e3 308 /*Codes_SRS_BLOB_02_033: [ If any previous operation that doesn't have an explicit failure description fails then Blob_UploadFromSasUri shall fail and return BLOB_ERROR ]*/
markrad 3:c0556ff7b8e3 309 LogError("failed to STRING_concat");
markrad 3:c0556ff7b8e3 310 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 311 }
markrad 3:c0556ff7b8e3 312 else
markrad 3:c0556ff7b8e3 313 {
markrad 3:c0556ff7b8e3 314 /*Codes_SRS_BLOB_02_030: [ Blob_UploadFromSasUri shall call HTTPAPIEX_ExecuteRequest with a PUT operation, passing the new relativePath, httpStatus and httpResponse and the XML string as content. ]*/
markrad 3:c0556ff7b8e3 315 const char* s = STRING_c_str(xml);
markrad 3:c0556ff7b8e3 316 BUFFER_HANDLE xmlAsBuffer = BUFFER_create((const unsigned char*)s, strlen(s));
markrad 3:c0556ff7b8e3 317 if (xmlAsBuffer == NULL)
markrad 3:c0556ff7b8e3 318 {
markrad 3:c0556ff7b8e3 319 /*Codes_SRS_BLOB_02_033: [ If any previous operation that doesn't have an explicit failure description fails then Blob_UploadFromSasUri shall fail and return BLOB_ERROR ]*/
markrad 3:c0556ff7b8e3 320 LogError("failed to BUFFER_create");
markrad 3:c0556ff7b8e3 321 result = BLOB_ERROR;
markrad 3:c0556ff7b8e3 322 }
markrad 3:c0556ff7b8e3 323 else
markrad 3:c0556ff7b8e3 324 {
markrad 3:c0556ff7b8e3 325 if (HTTPAPIEX_ExecuteRequest(
markrad 3:c0556ff7b8e3 326 httpApiExHandle,
markrad 3:c0556ff7b8e3 327 HTTPAPI_REQUEST_PUT,
markrad 3:c0556ff7b8e3 328 STRING_c_str(newRelativePath),
markrad 3:c0556ff7b8e3 329 NULL,
markrad 3:c0556ff7b8e3 330 xmlAsBuffer,
markrad 3:c0556ff7b8e3 331 httpStatus,
markrad 3:c0556ff7b8e3 332 NULL,
markrad 3:c0556ff7b8e3 333 httpResponse
markrad 3:c0556ff7b8e3 334 ) != HTTPAPIEX_OK)
markrad 3:c0556ff7b8e3 335 {
markrad 3:c0556ff7b8e3 336 /*Codes_SRS_BLOB_02_031: [ If HTTPAPIEX_ExecuteRequest fails then Blob_UploadFromSasUri shall fail and return BLOB_HTTP_ERROR. ]*/
markrad 3:c0556ff7b8e3 337 LogError("unable to HTTPAPIEX_ExecuteRequest");
markrad 3:c0556ff7b8e3 338 result = BLOB_HTTP_ERROR;
markrad 3:c0556ff7b8e3 339 }
markrad 3:c0556ff7b8e3 340 else
markrad 3:c0556ff7b8e3 341 {
markrad 3:c0556ff7b8e3 342 /*Codes_SRS_BLOB_02_032: [ Otherwise, Blob_UploadFromSasUri shall succeed and return BLOB_OK. ]*/
markrad 3:c0556ff7b8e3 343 result = BLOB_OK;
markrad 3:c0556ff7b8e3 344 }
markrad 3:c0556ff7b8e3 345 BUFFER_delete(xmlAsBuffer);
markrad 3:c0556ff7b8e3 346 }
markrad 3:c0556ff7b8e3 347 }
markrad 3:c0556ff7b8e3 348 STRING_delete(newRelativePath);
markrad 3:c0556ff7b8e3 349 }
markrad 3:c0556ff7b8e3 350 }
markrad 3:c0556ff7b8e3 351 }
markrad 3:c0556ff7b8e3 352 STRING_delete(xml);
markrad 3:c0556ff7b8e3 353 }
markrad 3:c0556ff7b8e3 354 }
markrad 3:c0556ff7b8e3 355 HTTPAPIEX_Destroy(httpApiExHandle);
markrad 3:c0556ff7b8e3 356 }
markrad 3:c0556ff7b8e3 357 free(hostname);
markrad 3:c0556ff7b8e3 358 }
markrad 3:c0556ff7b8e3 359 }
markrad 3:c0556ff7b8e3 360 }
markrad 3:c0556ff7b8e3 361 }
markrad 3:c0556ff7b8e3 362 }
markrad 3:c0556ff7b8e3 363 return result;
markrad 3:c0556ff7b8e3 364 }