Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 365b7911 authored by Andre Eisenbach's avatar Andre Eisenbach
Browse files

Fixed paired device config UUID parsing logic

Also added unit tests to cover this bug.

Bug: 26883553
Change-Id: Ice8641fad5c38ee43f1b080665dde70979f9d60f
parent d2372ba4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -121,7 +121,7 @@ LOCAL_SRC_FILES := $(btifCommonSrc)
LOCAL_CFLAGS := $(btifCommonCFlags)
# Many .h files have redefined typedefs
LOCAL_CLANG_CFLAGS += -Wno-error=typedef-redefinition
LOCAL_SHARED_LIBRARIES := libc liblog
LOCAL_SHARED_LIBRARIES := libcutils liblog
LOCAL_MODULE_CLASS := STATIC_LIBRARIES
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := libbtif
+5 −0
Original line number Diff line number Diff line
@@ -352,4 +352,9 @@ bt_status_t btif_storage_set_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
bt_status_t btif_storage_get_remote_version(const bt_bdaddr_t *remote_bd_addr,
                                  bt_remote_version_t *p_ver);

/******************************************************************************
 * Exported for unit tests
 *****************************************************************************/
size_t btif_split_uuids_string(const char *str, bt_uuid_t *p_uuid, size_t max_uuids);

#endif /* BTIF_STORAGE_H */
+7 −1
Original line number Diff line number Diff line
@@ -68,8 +68,14 @@ UINT32 devclass2uint(DEV_CLASS dev_class);
void uint2devclass(UINT32 dev, DEV_CLASS dev_class);
void uuid16_to_uuid128(uint16_t uuid16, bt_uuid_t* uuid128);

// Takes a |str| containing a 128-bit GUID formatted UUID and stores the
// result in |p_uuid|. |str| must be formatted in this format:
//   "12345678-1234-1234-1234-123456789012"
// |p_uuid| cannot be null. Returns true if parsing was successful, false
// otherwise. Returns false if |str| is null.
bool string_to_uuid(const char *str, bt_uuid_t *p_uuid);

void uuid_to_string_legacy(bt_uuid_t *p_uuid, char *str);
void string_to_uuid(const char *str, bt_uuid_t *p_uuid);
int ascii_2_hex (const char *p_ascii, int len, UINT8 *p_hex);

#endif /* BTIF_UTIL_H */
+29 −34
Original line number Diff line number Diff line
@@ -187,38 +187,6 @@ bt_status_t btif_storage_get_remote_addr_type(bt_bdaddr_t *remote_bd_addr,
**  Static functions
************************************************************************************/

/*******************************************************************************
**
** Function         btif_in_split_uuids_string_to_list
**
** Description      Internal helper function to split the string of UUIDs
**                  read from the NVRAM to an array
**
** Returns          None
**
*******************************************************************************/
static void btif_in_split_uuids_string_to_list(char *str, bt_uuid_t *p_uuid,
                                               uint32_t *p_num_uuid)
{
    char buf[64];
    char *p_start = str;
    char *p_needle;
    uint32_t num = 0;
    do
    {
        //p_needle = strchr(p_start, ';');
        p_needle = strchr(p_start, ' ');
        if (p_needle < p_start) break;
        memset(buf, 0, sizeof(buf));
        strncpy(buf, p_start, (p_needle-p_start));
        string_to_uuid(buf, p_uuid + num);
        num++;
        p_start = ++p_needle;

    } while (*p_start != 0);
    *p_num_uuid = num;
}

static int prop2cfg(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
{
    bdstr_t bdstr = {0};
@@ -389,8 +357,7 @@ static int cfg2prop(bt_bdaddr_t *remote_bd_addr, bt_property_t *prop)
                                    BTIF_STORAGE_PATH_REMOTE_SERVICE, value, &size))
            {
                bt_uuid_t *p_uuid = (bt_uuid_t*)prop->val;
                uint32_t num_uuids = 0;
                btif_in_split_uuids_string_to_list(value, p_uuid, &num_uuids);
                size_t num_uuids = btif_split_uuids_string(value, p_uuid, BT_MAX_NUM_UUIDS);
                prop->len = num_uuids * sizeof(bt_uuid_t);
                ret = TRUE;
            }
@@ -592,6 +559,34 @@ static void btif_read_le_key(const uint8_t key_type, const size_t key_len, bt_bd
 * the property->type.
 *******************************************************************************/

/*******************************************************************************
**
** Function         btif_split_uuids_string
**
** Description      Internal helper function to split the string of UUIDs
**                  read from the NVRAM to an array
**
** Returns          Number of UUIDs parsed from the supplied string
**
*******************************************************************************/
size_t btif_split_uuids_string(const char *str, bt_uuid_t *p_uuid, size_t max_uuids)
{
    assert(str);
    assert(p_uuid);

    size_t num_uuids = 0;
    while (str && num_uuids < max_uuids)
    {
        bool rc = string_to_uuid(str, p_uuid++);
        if (!rc) break;
        num_uuids++;
        str = strchr(str, ' ');
        if (str) str++;
    }

    return num_uuids;
}

/*******************************************************************************
**
** Function         btif_storage_get_adapter_property
+10 −4
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@

#include "btif_util.h"

#include <assert.h>
#include <ctype.h>
#include <netinet/in.h>
#include <stdio.h>
@@ -111,13 +112,19 @@ void uuid16_to_uuid128(uint16_t uuid16, bt_uuid_t* uuid128)
    memcpy(uuid128->uu + 2, &uuid16_bo, sizeof(uint16_t));
}

void string_to_uuid(const char *str, bt_uuid_t *p_uuid)
bool string_to_uuid(const char *str, bt_uuid_t *p_uuid)
{
    assert(p_uuid);
    if (str == NULL)
        return false;

    uint32_t uuid0, uuid4;
    uint16_t uuid1, uuid2, uuid3, uuid5;

    sscanf(str, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
    int rc = sscanf(str, "%08x-%04hx-%04hx-%04hx-%08x%04hx",
                &uuid0, &uuid1, &uuid2, &uuid3, &uuid4, &uuid5);
    if (rc != 6)
        return false;

    uuid0 = htonl(uuid0);
    uuid1 = htons(uuid1);
@@ -133,8 +140,7 @@ void string_to_uuid(const char *str, bt_uuid_t *p_uuid)
    memcpy(&(p_uuid->uu[10]), &uuid4, 4);
    memcpy(&(p_uuid->uu[14]), &uuid5, 2);

    return;

    return true;
}

void uuid_to_string_legacy(bt_uuid_t *p_uuid, char *str)
Loading