From 7bb8200be3c3e4a57ae68e3e439793684132fa79 Mon Sep 17 00:00:00 2001 From: Deokhyun Kim Date: Thu, 28 Jun 2018 16:33:35 +0900 Subject: [PATCH] Improve inefficient logic converting UUID to string Change-Id: Id0ab908edd526cd4c9af977612ab159f24c02ff2 Signed-off-by: Deokhyun Kim --- bt-api/bt-common.c | 58 ++++++++++++++++++++++++++-------------- bt-service/bt-service-common.c | 60 ++++++++++++++++++++++++++++-------------- 2 files changed, 78 insertions(+), 40 deletions(-) diff --git a/bt-api/bt-common.c b/bt-api/bt-common.c index b049640..792ca88 100644 --- a/bt-api/bt-common.c +++ b/bt-api/bt-common.c @@ -2013,13 +2013,17 @@ void _bt_set_adapter_internal_status(gboolean enabled) BT_EXPORT_API int bluetooth_get_uuid_name(const char *uuid, char **name) { - int i; +#define SHORT_UUID_COUNT 162 +#define LONG_UUID_COUNT 17 + int offset = 0; int uuid_len = 4; - static struct { + typedef struct { const char *uuid; const char *specification_name; - } bt_uuid_name[] = { + } uuid_name_s; + static uuid_name_s short_uuid_name[SHORT_UUID_COUNT] = { + // List should be sorted by UUID /* BT Classic Services */ {"1101", "Serial Port Service"}, {"1102", "LAN Access Using PPP Service"}, @@ -2191,26 +2195,33 @@ BT_EXPORT_API int bluetooth_get_uuid_name(const char *uuid, char **name) {"2A68", "Navigation"}, {"2A6D", "Pressure"}, {"2A6E", "Temperature"}, - + }; + static uuid_name_s long_uuid_name[LONG_UUID_COUNT] = { + // List should be sorted by UUID /* Custom uuids */ - {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"}, - {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"}, - {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"}, + {"1AB7C24D-185A-45B9-90D4-F7AB1A71949A", "Samsung Health Service"}, {"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB", "Data Source"}, + {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"}, + {"32D1955A-E5AA-4A96-9A49-08538DA8B8F6", "Samsung Gear Fit Manager Service"}, + {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"}, + {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"}, {"89D3502B-0F36-433A-8EF4-C502AD55F8DC", "Apple Media Service"}, + {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"}, {"9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2", "Remote Command"}, - {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"}, + {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"}, + {"A49EB41E-CB06-495C-9F4F-BB80A90CDF00", "Samsung Gear Manager Service"}, + {"ADE3D529-C784-4F63-A987-EB69F70EE816", "IoT OIC Service"}, + {"C2051EE0-804D-4D50-A12C-15E243852100", "Notifications Source"}, + {"C2F2CC0F-C085-4DD4-BE5A-ACA3074BBC72", "Control Point"}, {"C6B2F38C-23AB-46D8-A6AB-A3A870BBD5D7", "Entity Attribute"}, - {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"}, - {"c2f2cc0f-c085-4dd4-be5a-aca3074bbc72", "Control Point"}, - {"cece518b-28d7-4171-92d5-76a1e249a3b9", "Notifications Source"}, - {"32D1955A-E5AA-4A96-9A49-08538DA8B8F6", "Samsung Gear Fit Manager Service"}, + {"CECE518B-28D7-4171-92D5-76A1E249A3B9", "Notifications Source"}, {"FE53FF98-B259-4337-B56A-0EC9F82C6BAD", "Control Point"}, - {"C2051EE0-804D-4D50-A12C-15E243852100", "Notifications Source"}, - {"1ab7c24d-185a-45b9-90d4-f7ab1a71949a", "Samsung Health Service"}, - {"ADE3D529-C784-4F63-A987-EB69F70EE816", "IoT OIC Service"}, - {NULL, NULL} }; + const uuid_name_s *uuid_name = short_uuid_name; + int start = 0; + int end = SHORT_UUID_COUNT - 1; + int p; + int ret; if (!uuid || !name) return BLUETOOTH_ERROR_INVALID_PARAM; @@ -2220,15 +2231,22 @@ BT_EXPORT_API int bluetooth_get_uuid_name(const char *uuid, char **name) else { offset = 0; uuid_len = 36; + end = LONG_UUID_COUNT - 1; + uuid_name = long_uuid_name; } } else if (strlen(uuid) >= 8) offset = 4; - for (i = 0; bt_uuid_name[i].uuid; i++) { - if (!g_ascii_strncasecmp(uuid + offset, bt_uuid_name[i].uuid, uuid_len)) { - *name = g_strdup(bt_uuid_name[i].specification_name); + while (start <= end) { + p = start + (end - start) / 2; + ret = g_ascii_strncasecmp(uuid + offset, uuid_name[p].uuid, uuid_len); + if (ret == 0) { + *name = g_strdup(uuid_name[p].specification_name); return BLUETOOTH_ERROR_NONE; - } + } else if (ret < 0) + end = p - 1; + else + start = p + 1; } BT_INFO("Unknown uuid : %s", uuid); diff --git a/bt-service/bt-service-common.c b/bt-service/bt-service-common.c index bca4135..7ac9e9d 100644 --- a/bt-service/bt-service-common.c +++ b/bt-service/bt-service-common.c @@ -697,16 +697,20 @@ char *_bt_get_profile_uuid128(bt_profile_type_t profile_type) const char *_bt_convert_uuid_to_string(const char *uuid) { +#define SHORT_UUID_COUNT 162 +#define LONG_UUID_COUNT 17 + if (!uuid) return NULL; - int i; int offset = 0; int uuid_len = 4; - static struct { + typedef struct { const char *uuid; const char *specification_name; - } bt_uuid_name[] = { + } uuid_name_s; + static uuid_name_s short_uuid_name[SHORT_UUID_COUNT] = { + // List should be sorted by UUID /* BT Classic Services */ {"1101", "Serial Port Service"}, {"1102", "LAN Access Using PPP Service"}, @@ -878,26 +882,34 @@ const char *_bt_convert_uuid_to_string(const char *uuid) {"2A68", "Navigation"}, {"2A6D", "Pressure"}, {"2A6E", "Temperature"}, - + }; + static uuid_name_s long_uuid_name[LONG_UUID_COUNT] = { + // List should be sorted by UUID /* Custom uuids */ - {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"}, - {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"}, - {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"}, + {"1AB7C24D-185A-45B9-90D4-F7AB1A71949A", "Samsung Health Service"}, {"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB", "Data Source"}, + {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"}, + {"32D1955A-E5AA-4A96-9A49-08538DA8B8F6", "Samsung Gear Fit Manager Service"}, + {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"}, + {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"}, {"89D3502B-0F36-433A-8EF4-C502AD55F8DC", "Apple Media Service"}, + {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"}, {"9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2", "Remote Command"}, - {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"}, + {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"}, + {"A49EB41E-CB06-495C-9F4F-BB80A90CDF00", "Samsung Gear Manager Service"}, + {"ADE3D529-C784-4F63-A987-EB69F70EE816", "IoT OIC Service"}, + {"C2051EE0-804D-4D50-A12C-15E243852100", "Notifications Source"}, + {"C2F2CC0F-C085-4DD4-BE5A-ACA3074BBC72", "Control Point"}, {"C6B2F38C-23AB-46D8-A6AB-A3A870BBD5D7", "Entity Attribute"}, - {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"}, - {"c2f2cc0f-c085-4dd4-be5a-aca3074bbc72", "Control Point"}, - {"cece518b-28d7-4171-92d5-76a1e249a3b9", "Notifications Source"}, - {"32D1955A-E5AA-4A96-9A49-08538DA8B8F6", "Samsung Gear Fit Manager Service"}, + {"CECE518B-28D7-4171-92D5-76A1E249A3B9", "Notifications Source"}, {"FE53FF98-B259-4337-B56A-0EC9F82C6BAD", "Control Point"}, - {"C2051EE0-804D-4D50-A12C-15E243852100", "Notifications Source"}, - {"1ab7c24d-185a-45b9-90d4-f7ab1a71949a", "Samsung Health Service"}, - {"ADE3D529-C784-4F63-A987-EB69F70EE816", "IoT OIC Service"}, - {NULL, NULL} }; + const uuid_name_s *uuid_name = short_uuid_name; + static const char *unknown_name = "Unknown"; + int start = 0; + int end = SHORT_UUID_COUNT - 1; + int p; + int ret; if (strlen(uuid) == 36) { if (!g_ascii_strncasecmp(uuid + 9, "0000-1000-8000-00805F9B34FB", 27)) @@ -905,17 +917,25 @@ const char *_bt_convert_uuid_to_string(const char *uuid) else { offset = 0; uuid_len = 36; + end = LONG_UUID_COUNT - 1; + uuid_name = long_uuid_name; } } else if (strlen(uuid) >= 8) offset = 4; - for (i = 0; bt_uuid_name[i].uuid; i++) { - if (!g_ascii_strncasecmp(uuid + offset, bt_uuid_name[i].uuid, uuid_len)) - return (char *)bt_uuid_name[i].specification_name; + while (start <= end) { + p = start + (end - start) / 2; + ret = g_ascii_strncasecmp(uuid + offset, uuid_name[p].uuid, uuid_len); + if (ret == 0) + return uuid_name[p].specification_name; + else if (ret < 0) + end = p - 1; + else + start = p + 1; } BT_INFO("Unknown uuid : %s", uuid); - return "Unknown"; + return unknown_name; } const char *_bt_convert_error_to_string(int error) -- 2.7.4