2021-04-01 10:32:38

by Yun-hao Chung

[permalink] [raw]
Subject: [Bluez PATCH 2/8] lib: add hash functions for bt_uuid_t

This adds function GHashFunc and GEqualFunc for bt_uuid_t.
With these functions, we can add uuids into a GHashTable with bt_uuid_t
format.

Reviewed-by: Miao-chen Chou <[email protected]>
---

lib/uuid.c | 21 +++++++++++++++++++++
lib/uuid.h | 3 +++
2 files changed, 24 insertions(+)

diff --git a/lib/uuid.c b/lib/uuid.c
index a09321dc6ed1..0b0ddb3fc9d2 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -16,6 +16,7 @@
#include <string.h>
#include <stdlib.h>
#include <errno.h>
+#include <glib.h>

#include "lib/bluetooth.h"
#include "uuid.h"
@@ -120,6 +121,26 @@ int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2)
return bt_uuid128_cmp(&u1, &u2);
}

+guint bt_uuid_hash(gconstpointer key)
+{
+ const bt_uuid_t *uuid = key;
+ bt_uuid_t uuid_128;
+ uint64_t *val;
+
+ bt_uuid_to_uuid128(uuid, &uuid_128);
+ val = (uint64_t *)&uuid_128.value.u128;
+
+ return g_int64_hash(val) ^ g_int64_hash(val+1);
+}
+
+gboolean bt_uuid_equal(gconstpointer v1, gconstpointer v2)
+{
+ const bt_uuid_t *uuid1 = v1;
+ const bt_uuid_t *uuid2 = v2;
+
+ return bt_uuid_cmp(uuid1, uuid2) == 0;
+}
+
/*
* convert the UUID to string, copying a maximum of n characters.
*/
diff --git a/lib/uuid.h b/lib/uuid.h
index 1a4029b68730..e47ccccb9fd2 100644
--- a/lib/uuid.h
+++ b/lib/uuid.h
@@ -17,6 +17,7 @@ extern "C" {
#endif

#include <stdint.h>
+#include <glib.h>

#define GENERIC_AUDIO_UUID "00001203-0000-1000-8000-00805f9b34fb"

@@ -167,6 +168,8 @@ int bt_uuid128_create(bt_uuid_t *btuuid, uint128_t value);

int bt_uuid_cmp(const bt_uuid_t *uuid1, const bt_uuid_t *uuid2);
void bt_uuid_to_uuid128(const bt_uuid_t *src, bt_uuid_t *dst);
+guint bt_uuid_hash(gconstpointer key);
+gboolean bt_uuid_equal(gconstpointer v1, gconstpointer v2);

#define MAX_LEN_UUID_STR 37

--
2.31.0.291.g576ba9dcdaf-goog