Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Committer:
AzureIoTClient
Date:
Mon Sep 11 09:23:50 2017 -0700
Revision:
32:3b68703b9316
Parent:
6:c55b013dfc2a
Child:
33:810f9cff537a
1.1.23

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AzureIoTClient 1:9190c0f4d23a 1 // Copyright (c) Microsoft. All rights reserved.
AzureIoTClient 1:9190c0f4d23a 2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
AzureIoTClient 1:9190c0f4d23a 3
AzureIoTClient 1:9190c0f4d23a 4 #include <stdlib.h>
AzureIoTClient 1:9190c0f4d23a 5 #include <stdint.h>
AzureIoTClient 1:9190c0f4d23a 6 #include "azure_c_shared_utility/uniqueid.h"
Azure.IoT Build 6:c55b013dfc2a 7 #include "azure_c_shared_utility/xlogging.h"
AzureIoTClient 1:9190c0f4d23a 8 #include <time.h>
AzureIoTClient 1:9190c0f4d23a 9
AzureIoTClient 1:9190c0f4d23a 10 DEFINE_ENUM_STRINGS(UNIQUEID_RESULT, UNIQUEID_RESULT_VALUES);
AzureIoTClient 1:9190c0f4d23a 11
AzureIoTClient 1:9190c0f4d23a 12 static const char tochar[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
AzureIoTClient 1:9190c0f4d23a 13 static void generate128BitUUID(unsigned char* arrayOfByte)
AzureIoTClient 1:9190c0f4d23a 14 {
AzureIoTClient 1:9190c0f4d23a 15 size_t arrayIndex;
AzureIoTClient 1:9190c0f4d23a 16
AzureIoTClient 1:9190c0f4d23a 17 for (arrayIndex = 0; arrayIndex < 16; arrayIndex++)
AzureIoTClient 1:9190c0f4d23a 18 {
AzureIoTClient 1:9190c0f4d23a 19 arrayOfByte[arrayIndex] = (unsigned char)rand();
AzureIoTClient 1:9190c0f4d23a 20 }
AzureIoTClient 1:9190c0f4d23a 21
AzureIoTClient 1:9190c0f4d23a 22 //
AzureIoTClient 1:9190c0f4d23a 23 // Stick in the version field for random uuid.
AzureIoTClient 1:9190c0f4d23a 24 //
AzureIoTClient 1:9190c0f4d23a 25 arrayOfByte[7] &= 0x0f; //clear the bit field
AzureIoTClient 1:9190c0f4d23a 26 arrayOfByte[7] |= 0x40; //set the ones we care about
AzureIoTClient 1:9190c0f4d23a 27
AzureIoTClient 1:9190c0f4d23a 28 //
AzureIoTClient 1:9190c0f4d23a 29 // Stick in the variant field for the random uuid.
AzureIoTClient 1:9190c0f4d23a 30 //
AzureIoTClient 1:9190c0f4d23a 31 arrayOfByte[8] &= 0xf3; // Clear
AzureIoTClient 1:9190c0f4d23a 32 arrayOfByte[8] |= 0x08; // Set
AzureIoTClient 1:9190c0f4d23a 33
AzureIoTClient 1:9190c0f4d23a 34 }
AzureIoTClient 1:9190c0f4d23a 35
AzureIoTClient 1:9190c0f4d23a 36 // TODO: The User will need to call srand before calling this function
AzureIoTClient 1:9190c0f4d23a 37 UNIQUEID_RESULT UniqueId_Generate(char* uid, size_t len)
AzureIoTClient 1:9190c0f4d23a 38 {
AzureIoTClient 1:9190c0f4d23a 39 UNIQUEID_RESULT result;
AzureIoTClient 1:9190c0f4d23a 40 unsigned char arrayOfChar[16];
AzureIoTClient 1:9190c0f4d23a 41
AzureIoTClient 1:9190c0f4d23a 42 /* Codes_SRS_UNIQUEID_07_002: [If uid is NULL then UniqueId_Generate shall return UNIQUEID_INVALID_ARG] */
AzureIoTClient 1:9190c0f4d23a 43 /* Codes_SRS_UNIQUEID_07_003: [If len is less then 37 then UniqueId_Generate shall return UNIQUEID_INVALID_ARG] */
AzureIoTClient 1:9190c0f4d23a 44 if (uid == NULL || len < 37)
AzureIoTClient 1:9190c0f4d23a 45 {
AzureIoTClient 1:9190c0f4d23a 46 result = UNIQUEID_INVALID_ARG;
AzureIoTClient 1:9190c0f4d23a 47 LogError("Buffer Size is Null or length is less then 37 bytes");
AzureIoTClient 1:9190c0f4d23a 48 }
AzureIoTClient 1:9190c0f4d23a 49 else
AzureIoTClient 1:9190c0f4d23a 50 {
AzureIoTClient 1:9190c0f4d23a 51 size_t arrayIndex;
AzureIoTClient 1:9190c0f4d23a 52 size_t shiftCount;
AzureIoTClient 1:9190c0f4d23a 53 size_t characterPosition = 0;
AzureIoTClient 1:9190c0f4d23a 54
AzureIoTClient 1:9190c0f4d23a 55 /* Codes_SRS_UNIQUEID_07_001: [UniqueId_Generate shall create a unique Id 36 character long string.] */
AzureIoTClient 1:9190c0f4d23a 56 generate128BitUUID(arrayOfChar);
AzureIoTClient 1:9190c0f4d23a 57 for (arrayIndex = 0; arrayIndex < 16; arrayIndex++)
AzureIoTClient 1:9190c0f4d23a 58 {
AzureIoTClient 1:9190c0f4d23a 59 for (shiftCount = 0; shiftCount <= 1; shiftCount++)
AzureIoTClient 1:9190c0f4d23a 60 {
AzureIoTClient 1:9190c0f4d23a 61 char hexChar = tochar[arrayOfChar[arrayIndex] & 0xf];
AzureIoTClient 1:9190c0f4d23a 62 if ((characterPosition == 8) || (characterPosition == 13) || (characterPosition == 18) || (characterPosition == 23))
AzureIoTClient 1:9190c0f4d23a 63 {
AzureIoTClient 1:9190c0f4d23a 64 uid[characterPosition] = '-';
AzureIoTClient 1:9190c0f4d23a 65 characterPosition++;
AzureIoTClient 1:9190c0f4d23a 66 }
AzureIoTClient 1:9190c0f4d23a 67 uid[characterPosition] = hexChar;
AzureIoTClient 1:9190c0f4d23a 68 characterPosition++;
AzureIoTClient 1:9190c0f4d23a 69 arrayOfChar[arrayIndex] = arrayOfChar[arrayIndex] >> 4;
AzureIoTClient 1:9190c0f4d23a 70 }
AzureIoTClient 1:9190c0f4d23a 71 }
AzureIoTClient 1:9190c0f4d23a 72 uid[characterPosition] = 0;
AzureIoTClient 1:9190c0f4d23a 73 result = UNIQUEID_OK;
AzureIoTClient 1:9190c0f4d23a 74 }
AzureIoTClient 1:9190c0f4d23a 75 return result;
AzureIoTClient 1:9190c0f4d23a 76 }
AzureIoTClient 32:3b68703b9316 77
AzureIoTClient 32:3b68703b9316 78 UNIQUEID_RESULT UniqueId_GetStringFromBytes(unsigned char* uid_bytes, size_t uuid_size, char* output_string)
AzureIoTClient 32:3b68703b9316 79 {
AzureIoTClient 32:3b68703b9316 80 UNIQUEID_RESULT result;
AzureIoTClient 32:3b68703b9316 81
AzureIoTClient 32:3b68703b9316 82 // Codes_SRS_UNIQUEID_09_001: [ If `uid` or `output_string` are NULL, UniqueId_GetStringFromBytes shall return UNIQUEID_INVALID_ARG ]
AzureIoTClient 32:3b68703b9316 83 // Codes_SRS_UNIQUEID_09_002: [ If `uid_size` is different than 16, UniqueId_GetStringFromBytes shall return UNIQUEID_INVALID_ARG ]
AzureIoTClient 32:3b68703b9316 84 if (uid_bytes == NULL || uuid_size == 0 || uuid_size != 16 || output_string == NULL)
AzureIoTClient 32:3b68703b9316 85 {
AzureIoTClient 32:3b68703b9316 86 LogError("Invalid argument (uid=%p, uuid_size=%d, output_string=%p)", uid_bytes, uuid_size, output_string);
AzureIoTClient 32:3b68703b9316 87 result = UNIQUEID_INVALID_ARG;
AzureIoTClient 32:3b68703b9316 88 }
AzureIoTClient 32:3b68703b9316 89 else
AzureIoTClient 32:3b68703b9316 90 {
AzureIoTClient 32:3b68703b9316 91 // Codes_SRS_UNIQUEID_09_003: [ `output_string` shall be filled according to RFC4122 using the byte sequence in `uid` ]
AzureIoTClient 32:3b68703b9316 92 size_t i, j;
AzureIoTClient 32:3b68703b9316 93
AzureIoTClient 32:3b68703b9316 94 // Codes_SRS_UNIQUEID_09_004: [ If no failures occur, UniqueId_Generate shall return UNIQUEID_OK ]
AzureIoTClient 32:3b68703b9316 95 result = UNIQUEID_OK;
AzureIoTClient 32:3b68703b9316 96
AzureIoTClient 32:3b68703b9316 97 for (i = 0, j = 0; i < uuid_size; i++, j += 2)
AzureIoTClient 32:3b68703b9316 98 {
AzureIoTClient 32:3b68703b9316 99 if (sprintf(output_string + j, "%02x", uid_bytes[i]) != 2)
AzureIoTClient 32:3b68703b9316 100 {
AzureIoTClient 32:3b68703b9316 101 LogError("Failed encoding UUID octect");
AzureIoTClient 32:3b68703b9316 102 result = UNIQUEID_ERROR;
AzureIoTClient 32:3b68703b9316 103 break;
AzureIoTClient 32:3b68703b9316 104 }
AzureIoTClient 32:3b68703b9316 105
AzureIoTClient 32:3b68703b9316 106 if (i == 3 || i == 5 || i == 7 || i == 9)
AzureIoTClient 32:3b68703b9316 107 {
AzureIoTClient 32:3b68703b9316 108 output_string[j + 2] = '-';
AzureIoTClient 32:3b68703b9316 109 j++;
AzureIoTClient 32:3b68703b9316 110 }
AzureIoTClient 32:3b68703b9316 111 }
AzureIoTClient 32:3b68703b9316 112 }
AzureIoTClient 32:3b68703b9316 113
AzureIoTClient 32:3b68703b9316 114 return result;
AzureIoTClient 32:3b68703b9316 115 }