Azure IoT common library
Dependents: STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more
uniqueid_stub.c@48:81866008bba4, 2018-09-11 (annotated)
- Committer:
- AzureIoTClient
- Date:
- Tue Sep 11 11:15:08 2018 -0700
- Revision:
- 48:81866008bba4
- Parent:
- 33:810f9cff537a
1.2.9
Who changed what in which revision?
User | Revision | Line number | New 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 | 48:81866008bba4 | 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 | } |