You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by "sjanc (via GitHub)" <gi...@apache.org> on 2023/08/04 09:00:05 UTC

[GitHub] [mynewt-nimble] sjanc commented on a diff in pull request #1545: host: Convert string 128-bit UUID to ble_uuid128_t

sjanc commented on code in PR #1545:
URL: https://github.com/apache/mynewt-nimble/pull/1545#discussion_r1284171533


##########
nimble/host/src/ble_uuid.c:
##########
@@ -136,6 +186,104 @@ ble_uuid_to_str(const ble_uuid_t *uuid, char *dst)
     return dst;
 }
 
+int
+ble_uuid_from_str(ble_uuid_any_t *uuid, const char *str)
+{
+    uint8_t *u8p;
+    const char *str_ptr;
+    uint32_t u16 = 0;
+    uint32_t u32 = 0;
+    uint8_t tmp_rslt = 0;
+
+    int len = (int) strlen(str);
+    str_ptr = &str[len - 2];
+
+    if (len <= BLE_UUID16_STR_MAX_LEN) {

Review Comment:
   should this also validate min len, and maybe that len is even?
   
   if I pass "1" it shouldn't probably start parsing (and str_ptr will point before str buffer)



##########
nimble/host/src/ble_uuid.c:
##########
@@ -136,6 +186,104 @@ ble_uuid_to_str(const ble_uuid_t *uuid, char *dst)
     return dst;
 }
 
+int
+ble_uuid_from_str(ble_uuid_any_t *uuid, const char *str)
+{
+    uint8_t *u8p;
+    const char *str_ptr;
+    uint32_t u16 = 0;
+    uint32_t u32 = 0;
+    uint8_t tmp_rslt = 0;
+
+    int len = (int) strlen(str);
+    str_ptr = &str[len - 2];
+
+    if (len <= BLE_UUID16_STR_MAX_LEN) {
+        uuid->u.type = BLE_UUID_TYPE_16;
+    } else if (len > BLE_UUID16_STR_MAX_LEN &&

Review Comment:
   this can be simply  "else if (len <= BLE_UUID32_STR_MAX_LEN)"  cause previous check already handled <= BLE_UUID16_STR_MAX_LEN
   
   will make code a bit easier to follow



##########
nimble/host/src/ble_uuid.c:
##########
@@ -136,6 +186,104 @@ ble_uuid_to_str(const ble_uuid_t *uuid, char *dst)
     return dst;
 }
 
+int
+ble_uuid_from_str(ble_uuid_any_t *uuid, const char *str)
+{
+    uint8_t *u8p;
+    const char *str_ptr;
+    uint32_t u16 = 0;
+    uint32_t u32 = 0;
+    uint8_t tmp_rslt = 0;
+
+    int len = (int) strlen(str);
+    str_ptr = &str[len - 2];
+
+    if (len <= BLE_UUID16_STR_MAX_LEN) {
+        uuid->u.type = BLE_UUID_TYPE_16;
+    } else if (len > BLE_UUID16_STR_MAX_LEN &&
+               len <= BLE_UUID32_STR_MAX_LEN) {
+        uuid->u.type = BLE_UUID_TYPE_32;
+    } else if (len > BLE_UUID32_STR_MAX_LEN &&
+               len <= BLE_UUID128_STR_MAX_LEN) {
+        uuid->u.type = BLE_UUID_TYPE_128;
+    } else {
+        return BLE_HS_EINVAL;
+    }
+
+    switch (uuid->u.type) {
+    case BLE_UUID_TYPE_128:
+        uuid->u.type = BLE_UUID_TYPE_128;
+        u8p = uuid->u128.value;
+        for (int i = 0; i < 16; i++) {
+            if (hex2bin(str_ptr, u8p, 1) != 1) {
+                return BLE_HS_EINVAL;
+            }
+
+            /* Check if string end */
+            if (str_ptr == str) {
+                break;
+            }
+
+            /* Remove '-' */
+            if (*(str_ptr - 1) == '-') {
+                str_ptr--;
+            }
+
+            str_ptr -= 2;
+            u8p++;
+        }
+
+        if (memcmp(ble_uuid_base, uuid->u128.value, 12) == 0) {
+            uint8_t *tmp_ptr = &uuid->u128.value[12];
+            uint32_t tmp_val32 = 0;
+            for (int i = 0; i < 4; i++) {
+                tmp_val32 |= ((uint32_t)(*tmp_ptr++) << 8 * i);
+            }
+
+            if (tmp_val32 < UINT16_MAX) {

Review Comment:
   shouldn't this be <= ?
   
   (could also add test for edge values :-)



##########
nimble/host/include/host/ble_uuid.h:
##########
@@ -253,6 +253,26 @@ void ble_uuid_copy(ble_uuid_any_t *dst, const ble_uuid_t *src);
  */
 char *ble_uuid_to_str(const ble_uuid_t *uuid, char *dst);
 
+/**
+ * @brief Converts the specified UUID string to ble_uuid_any_t representation.
+ *        If the UUID is recognised as Bluetooth SIG UUID, it will provide its
+ *        32-bit or 16-bit representation.
+ *
+ * Example 128-bit string representations:
+ *     o "12345678-1234-1234-1234-123456789abc\0"

Review Comment:
   string is by definition null terminated



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@mynewt.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org