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
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 */
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 */
** 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};
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;
}
/*******************************************************************************
**
+** 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
**
** Description BTIF storage API - Fetches the adapter property->type
#include "btif_util.h"
+#include <assert.h>
#include <ctype.h>
#include <netinet/in.h>
#include <stdio.h>
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);
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)
#include <gtest/gtest.h>
extern "C" {
+#include "btif/include/btif_storage.h"
#include "btif/include/btif_util.h"
}
TEST(BtifStorageTest, test_string_to_uuid) {
const char *s1 = "e39c6285-867f-4b1d-9db0-35fbd9aebf22";
- const uint8_t o1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d,
- 0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22};
+ const uint8_t u1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d,
+ 0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22};
bt_uuid_t uuid;
memset(&uuid, 0, sizeof(uuid));
- EXPECT_FALSE(memcmp(&uuid, o1, sizeof(o1)) == 0);
+ EXPECT_FALSE(memcmp(&uuid, u1, sizeof(u1)) == 0);
- string_to_uuid(s1, &uuid);
- EXPECT_TRUE(memcmp(&uuid, o1, sizeof(o1)) == 0);
+ bool rc = string_to_uuid(s1, &uuid);
+ EXPECT_TRUE(rc);
+ EXPECT_TRUE(memcmp(&uuid, u1, sizeof(u1)) == 0);
}
+TEST(BtifStorageTest, test_string_to_uuid_invalid) {
+ bt_uuid_t uuid;
+ bool rc = string_to_uuid("This is not a UUID", &uuid);
+ EXPECT_FALSE(rc);
+}
+
+TEST(BtifStorageTest, test_uuid_split_multiple) {
+ const char *s1 = "e39c6285-867f-4b1d-9db0-35fbd9aebf22 e39c6285-867f-4b1d-9db0-35fbd9aebf23";
+ const uint8_t u1[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d,
+ 0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x22};
+ const uint8_t u2[] = {0xe3, 0x9c, 0x62, 0x85, 0x86, 0x7f, 0x4b, 0x1d,
+ 0x9d, 0xb0, 0x35, 0xfb, 0xd9, 0xae, 0xbf, 0x23};
+
+ bt_uuid_t uuids[2];
+ size_t num_uuids = btif_split_uuids_string(s1, uuids, 2);
+ EXPECT_EQ(num_uuids, 2u);
+ EXPECT_TRUE(memcmp(&uuids[0], u1, sizeof(u1)) == 0);
+ EXPECT_TRUE(memcmp(&uuids[1], u2, sizeof(u2)) == 0);
+}
+
+TEST(BtifStorageTest, test_uuid_split_partial) {
+ const char *s1 = "e39c6285-867f-4b1d-9db0-35fbd9aebf22 e39c6285-867f-4b1d-9db0-35fbd9aebf23";
+
+ bt_uuid_t uuids[2];
+ size_t num_uuids = btif_split_uuids_string(s1, uuids, 1);
+ EXPECT_EQ(num_uuids, 1u);
+}