Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Thu Oct 04 09:17:16 2018 -0700
Revision:
49:6bb8b9a66642
Parent:
48:81866008bba4
1.2.10

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 33:810f9cff537a 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 33:810f9cff537a 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 33:810f9cff537a 3
AzureIoTClient 33:810f9cff537a 4 #include <stdlib.h>
AzureIoTClient 33:810f9cff537a 5 #include <stdio.h>
AzureIoTClient 33:810f9cff537a 6 #include "azure_c_shared_utility/gballoc.h"
AzureIoTClient 33:810f9cff537a 7 #include "azure_c_shared_utility/uuid.h"
AzureIoTClient 33:810f9cff537a 8 #include "azure_c_shared_utility/uniqueid.h"
AzureIoTClient 33:810f9cff537a 9 #include "azure_c_shared_utility/optimize_size.h"
AzureIoTClient 33:810f9cff537a 10 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 33:810f9cff537a 11
AzureIoTClient 33:810f9cff537a 12 #define UUID_STRING_LENGTH 36
AzureIoTClient 33:810f9cff537a 13 #define UUID_STRING_SIZE (UUID_STRING_LENGTH + 1)
AzureIoTClient 33:810f9cff537a 14 #define __SUCCESS__ 0
AzureIoTClient 33:810f9cff537a 15 #define UUID_FORMAT_STRING "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x"
AzureIoTClient 33:810f9cff537a 16
AzureIoTClient 48:81866008bba4 17 int UUID_from_string(const char* uuid_string, UUID_T* uuid)
AzureIoTClient 33:810f9cff537a 18 {
AzureIoTClient 33:810f9cff537a 19 int result;
AzureIoTClient 33:810f9cff537a 20
AzureIoTClient 33:810f9cff537a 21 // Codes_SRS_UUID_09_007: [ If `uuid_string` or `uuid` are NULL, UUID_from_string shall return a non-zero value ]
AzureIoTClient 33:810f9cff537a 22 if (uuid_string == NULL || uuid == NULL)
AzureIoTClient 33:810f9cff537a 23 {
AzureIoTClient 33:810f9cff537a 24 LogError("Invalid argument (uuid_string=%p, uuid=%p)", uuid_string, uuid);
AzureIoTClient 33:810f9cff537a 25 result = __FAILURE__;
AzureIoTClient 33:810f9cff537a 26 }
AzureIoTClient 33:810f9cff537a 27 else
AzureIoTClient 33:810f9cff537a 28 {
AzureIoTClient 33:810f9cff537a 29 size_t uuid_string_length = strlen(uuid_string);
AzureIoTClient 33:810f9cff537a 30
AzureIoTClient 33:810f9cff537a 31 if (uuid_string_length != UUID_STRING_LENGTH)
AzureIoTClient 33:810f9cff537a 32 {
AzureIoTClient 33:810f9cff537a 33 LogError("Unexpected size for an UUID string (%d)", uuid_string_length);
AzureIoTClient 33:810f9cff537a 34 result = __FAILURE__;
AzureIoTClient 33:810f9cff537a 35 }
AzureIoTClient 33:810f9cff537a 36 else
AzureIoTClient 33:810f9cff537a 37 {
AzureIoTClient 33:810f9cff537a 38 // Codes_SRS_UUID_09_008: [ Each pair of digits in `uuid_string`, excluding dashes, shall be read as a single HEX value and saved on the respective position in `uuid` ]
AzureIoTClient 33:810f9cff537a 39 size_t i, j;
AzureIoTClient 33:810f9cff537a 40 unsigned char* uuid_bytes;
AzureIoTClient 33:810f9cff537a 41
AzureIoTClient 33:810f9cff537a 42 uuid_bytes = (unsigned char*)uuid;
AzureIoTClient 33:810f9cff537a 43 // Codes_SRS_UUID_09_010: [ If no failures occur, UUID_from_string shall return zero ]
AzureIoTClient 33:810f9cff537a 44 result = __SUCCESS__;
AzureIoTClient 33:810f9cff537a 45
AzureIoTClient 33:810f9cff537a 46 for (i = 0, j = 0; i < uuid_string_length; )
AzureIoTClient 33:810f9cff537a 47 {
AzureIoTClient 33:810f9cff537a 48 if (uuid_string[i] == '-')
AzureIoTClient 33:810f9cff537a 49 {
AzureIoTClient 33:810f9cff537a 50 i++;
AzureIoTClient 33:810f9cff537a 51 }
AzureIoTClient 33:810f9cff537a 52 else
AzureIoTClient 33:810f9cff537a 53 {
AzureIoTClient 33:810f9cff537a 54 char double_hex_digit[3] = { 0, 0, 0 };
AzureIoTClient 33:810f9cff537a 55
AzureIoTClient 33:810f9cff537a 56 (void)memcpy(double_hex_digit, uuid_string + i, 2);
AzureIoTClient 33:810f9cff537a 57
AzureIoTClient 33:810f9cff537a 58 if (sscanf(double_hex_digit, "%02hhx", uuid_bytes + j) != 1)
AzureIoTClient 33:810f9cff537a 59 {
AzureIoTClient 33:810f9cff537a 60 // Codes_SRS_UUID_09_009: [ If `uuid` fails to be generated, UUID_from_string shall return a non-zero value ]
AzureIoTClient 33:810f9cff537a 61 LogError("Failed decoding UUID string (%d)", i);
AzureIoTClient 33:810f9cff537a 62 result = __FAILURE__;
AzureIoTClient 33:810f9cff537a 63 break;
AzureIoTClient 33:810f9cff537a 64 }
AzureIoTClient 33:810f9cff537a 65 else
AzureIoTClient 33:810f9cff537a 66 {
AzureIoTClient 33:810f9cff537a 67 i += 2;
AzureIoTClient 33:810f9cff537a 68 j++;
AzureIoTClient 33:810f9cff537a 69 }
AzureIoTClient 33:810f9cff537a 70 }
AzureIoTClient 33:810f9cff537a 71 }
AzureIoTClient 33:810f9cff537a 72 }
AzureIoTClient 33:810f9cff537a 73 }
AzureIoTClient 33:810f9cff537a 74
AzureIoTClient 33:810f9cff537a 75 return result;
AzureIoTClient 33:810f9cff537a 76 }
AzureIoTClient 33:810f9cff537a 77
AzureIoTClient 49:6bb8b9a66642 78 char* UUID_to_string(const UUID_T* uuid)
AzureIoTClient 33:810f9cff537a 79 {
AzureIoTClient 33:810f9cff537a 80 char* result;
AzureIoTClient 33:810f9cff537a 81
AzureIoTClient 48:81866008bba4 82 // Codes_SRS_UUID_09_011: [ If `uuid` is NULL, UUID_to_string shall return a non-zero value ]
AzureIoTClient 33:810f9cff537a 83 if (uuid == NULL)
AzureIoTClient 33:810f9cff537a 84 {
AzureIoTClient 33:810f9cff537a 85 LogError("Invalid argument (uuid is NULL)");
AzureIoTClient 33:810f9cff537a 86 result = NULL;
AzureIoTClient 33:810f9cff537a 87 }
AzureIoTClient 48:81866008bba4 88 // Codes_SRS_UUID_09_012: [ UUID_to_string shall allocate a valid UUID string (`uuid_string`) as per RFC 4122 ]
AzureIoTClient 33:810f9cff537a 89 else if ((result = (char*)malloc(sizeof(char) * UUID_STRING_SIZE)) == NULL)
AzureIoTClient 33:810f9cff537a 90 {
AzureIoTClient 48:81866008bba4 91 // Codes_SRS_UUID_09_013: [ If `uuid_string` fails to be allocated, UUID_to_string shall return NULL ]
AzureIoTClient 33:810f9cff537a 92 LogError("Failed allocating UUID string");
AzureIoTClient 33:810f9cff537a 93 }
AzureIoTClient 33:810f9cff537a 94 else
AzureIoTClient 33:810f9cff537a 95 {
AzureIoTClient 33:810f9cff537a 96 unsigned char* uuid_bytes;
AzureIoTClient 33:810f9cff537a 97 int number_of_chars_written;
AzureIoTClient 33:810f9cff537a 98
AzureIoTClient 33:810f9cff537a 99 uuid_bytes = (unsigned char*)uuid;
AzureIoTClient 33:810f9cff537a 100
AzureIoTClient 33:810f9cff537a 101 // Codes_SRS_UUID_09_014: [ Each character in `uuid` shall be written in the respective positions of `uuid_string` as a 2-digit HEX value ]
AzureIoTClient 48:81866008bba4 102 number_of_chars_written = sprintf(result, "%" PRI_UUID,
AzureIoTClient 48:81866008bba4 103 UUID_FORMAT_VALUES(uuid_bytes));
AzureIoTClient 33:810f9cff537a 104
AzureIoTClient 33:810f9cff537a 105 if (number_of_chars_written != UUID_STRING_LENGTH)
AzureIoTClient 33:810f9cff537a 106 {
AzureIoTClient 48:81866008bba4 107 // Tests_SRS_UUID_09_015: [ If `uuid_string` fails to be set, UUID_to_string shall return NULL ]
AzureIoTClient 33:810f9cff537a 108 LogError("Failed encoding UUID string");
AzureIoTClient 33:810f9cff537a 109 free(result);
AzureIoTClient 33:810f9cff537a 110 result = NULL;
AzureIoTClient 33:810f9cff537a 111 }
AzureIoTClient 33:810f9cff537a 112 }
AzureIoTClient 33:810f9cff537a 113
AzureIoTClient 33:810f9cff537a 114 // Codes_SRS_UUID_09_016: [ If no failures occur, UUID_to_string shall return `uuid_string` ]
AzureIoTClient 33:810f9cff537a 115 return result;
AzureIoTClient 33:810f9cff537a 116 }
AzureIoTClient 33:810f9cff537a 117
AzureIoTClient 48:81866008bba4 118 int UUID_generate(UUID_T* uuid)
AzureIoTClient 33:810f9cff537a 119 {
AzureIoTClient 33:810f9cff537a 120 int result;
AzureIoTClient 33:810f9cff537a 121
AzureIoTClient 33:810f9cff537a 122 // Codes_SRS_UUID_09_001: [ If `uuid` is NULL, UUID_generate shall return a non-zero value ]
AzureIoTClient 33:810f9cff537a 123 if (uuid == NULL)
AzureIoTClient 33:810f9cff537a 124 {
AzureIoTClient 33:810f9cff537a 125 LogError("Invalid argument (uuid is NULL)");
AzureIoTClient 33:810f9cff537a 126 result = __FAILURE__;
AzureIoTClient 33:810f9cff537a 127 }
AzureIoTClient 33:810f9cff537a 128 else
AzureIoTClient 33:810f9cff537a 129 {
AzureIoTClient 33:810f9cff537a 130 char* uuid_string;
AzureIoTClient 33:810f9cff537a 131
AzureIoTClient 33:810f9cff537a 132 if ((uuid_string = (char*)malloc(sizeof(char) * UUID_STRING_SIZE)) == NULL)
AzureIoTClient 33:810f9cff537a 133 {
AzureIoTClient 33:810f9cff537a 134 // Codes_SRS_UUID_09_003: [ If the UUID string fails to be obtained, UUID_generate shall fail and return a non-zero value ]
AzureIoTClient 33:810f9cff537a 135 LogError("Failed allocating UUID string");
AzureIoTClient 33:810f9cff537a 136 result = __FAILURE__;
AzureIoTClient 33:810f9cff537a 137 }
AzureIoTClient 33:810f9cff537a 138 else
AzureIoTClient 33:810f9cff537a 139 {
AzureIoTClient 48:81866008bba4 140 (void)memset(uuid_string, 0, sizeof(char) * UUID_STRING_SIZE);
AzureIoTClient 33:810f9cff537a 141
AzureIoTClient 33:810f9cff537a 142 // Codes_SRS_UUID_09_002: [ UUID_generate shall obtain an UUID string from UniqueId_Generate ]
AzureIoTClient 33:810f9cff537a 143 if (UniqueId_Generate(uuid_string, UUID_STRING_SIZE) != UNIQUEID_OK)
AzureIoTClient 33:810f9cff537a 144 {
AzureIoTClient 33:810f9cff537a 145 // Codes_SRS_UUID_09_003: [ If the UUID string fails to be obtained, UUID_generate shall fail and return a non-zero value ]
AzureIoTClient 33:810f9cff537a 146 LogError("Failed generating UUID");
AzureIoTClient 33:810f9cff537a 147 result = __FAILURE__;
AzureIoTClient 33:810f9cff537a 148 }
AzureIoTClient 48:81866008bba4 149 // Codes_SRS_UUID_09_004: [ The UUID string shall be parsed into an UUID_T type (16 unsigned char array) and filled in `uuid` ]
AzureIoTClient 33:810f9cff537a 150 else if (UUID_from_string(uuid_string, uuid) != 0)
AzureIoTClient 33:810f9cff537a 151 {
AzureIoTClient 33:810f9cff537a 152 // Codes_SRS_UUID_09_005: [ If `uuid` fails to be set, UUID_generate shall fail and return a non-zero value ]
AzureIoTClient 33:810f9cff537a 153 LogError("Failed parsing UUID string");
AzureIoTClient 33:810f9cff537a 154 result = __FAILURE__;
AzureIoTClient 33:810f9cff537a 155 }
AzureIoTClient 33:810f9cff537a 156 else
AzureIoTClient 33:810f9cff537a 157 {
AzureIoTClient 33:810f9cff537a 158 // Codes_SRS_UUID_09_006: [ If no failures occur, UUID_generate shall return zero ]
AzureIoTClient 33:810f9cff537a 159 result = __SUCCESS__;
AzureIoTClient 33:810f9cff537a 160 }
AzureIoTClient 33:810f9cff537a 161
AzureIoTClient 33:810f9cff537a 162 free(uuid_string);
AzureIoTClient 33:810f9cff537a 163 }
AzureIoTClient 33:810f9cff537a 164 }
AzureIoTClient 33:810f9cff537a 165
AzureIoTClient 33:810f9cff537a 166 return result;
AzureIoTClient 33:810f9cff537a 167 }