Azure IoT common library

Dependents:   STM32F746_iothub_client_sample_mqtt f767zi_mqtt iothub_client_sample_amqp iothub_client_sample_http ... more

Revision:
32:3b68703b9316
Parent:
6:c55b013dfc2a
Child:
33:810f9cff537a
diff -r 6a55d47aea41 -r 3b68703b9316 uniqueid_stub.c
--- a/uniqueid_stub.c	Fri Aug 11 14:03:20 2017 -0700
+++ b/uniqueid_stub.c	Mon Sep 11 09:23:50 2017 -0700
@@ -74,3 +74,42 @@
     }
     return result;
 }
+
+UNIQUEID_RESULT UniqueId_GetStringFromBytes(unsigned char* uid_bytes, size_t uuid_size, char* output_string)
+{
+    UNIQUEID_RESULT result;
+
+    // Codes_SRS_UNIQUEID_09_001: [ If `uid` or `output_string` are NULL, UniqueId_GetStringFromBytes shall return UNIQUEID_INVALID_ARG ]
+    // Codes_SRS_UNIQUEID_09_002: [ If `uid_size` is different than 16, UniqueId_GetStringFromBytes shall return UNIQUEID_INVALID_ARG ]
+    if (uid_bytes == NULL || uuid_size == 0 || uuid_size != 16 || output_string == NULL)
+    {
+        LogError("Invalid argument (uid=%p, uuid_size=%d, output_string=%p)", uid_bytes, uuid_size, output_string);
+        result = UNIQUEID_INVALID_ARG;
+    }
+    else
+    {
+        // Codes_SRS_UNIQUEID_09_003: [ `output_string` shall be filled according to RFC4122 using the byte sequence in `uid` ]
+        size_t i, j;
+
+        // Codes_SRS_UNIQUEID_09_004: [ If no failures occur, UniqueId_Generate shall return UNIQUEID_OK ]  
+        result = UNIQUEID_OK;
+
+        for (i = 0, j = 0; i < uuid_size; i++, j += 2)
+        {
+            if (sprintf(output_string + j, "%02x", uid_bytes[i]) != 2)
+            {
+                LogError("Failed encoding UUID octect");
+                result = UNIQUEID_ERROR;
+                break;
+            }
+
+            if (i == 3 || i == 5 || i == 7 || i == 9)
+            {
+                output_string[j + 2] = '-';
+                j++;
+            }
+        }
+    }
+
+    return result;
+}
\ No newline at end of file