diff options
author | Aleksander Morgado <aleksander@aleksander.es> | 2014-03-04 20:10:29 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2014-03-04 20:27:41 +0100 |
commit | 82ca529fb5efeeb23cb636fe1b5fc0d849f45964 (patch) | |
tree | 22db4b861a381ab5c7e7c5d24dec0cfca1b63815 | |
parent | 5764ec47873540e3e356b9af2bcfdd1fab066a0c (diff) | |
download | libmbim-82ca529fb5efeeb23cb636fe1b5fc0d849f45964.tar.gz |
libmbim-glib,uuid: new 'mbim_uuid_from_printable()'
-rw-r--r-- | docs/reference/libmbim-glib/libmbim-glib-common.sections | 1 | ||||
-rw-r--r-- | src/libmbim-glib/mbim-uuid.c | 52 | ||||
-rw-r--r-- | src/libmbim-glib/mbim-uuid.h | 8 | ||||
-rw-r--r-- | src/libmbim-glib/test/test-uuid.c | 77 |
4 files changed, 134 insertions, 4 deletions
diff --git a/docs/reference/libmbim-glib/libmbim-glib-common.sections b/docs/reference/libmbim-glib/libmbim-glib-common.sections index b56c098..2c0eb31 100644 --- a/docs/reference/libmbim-glib/libmbim-glib-common.sections +++ b/docs/reference/libmbim-glib/libmbim-glib-common.sections @@ -25,6 +25,7 @@ MBIM_UUID_MS_FIRMWARE_ID mbim_service_get_string mbim_context_type_get_string mbim_uuid_cmp +mbim_uuid_from_printable mbim_uuid_get_printable mbim_uuid_from_service mbim_uuid_to_service diff --git a/src/libmbim-glib/mbim-uuid.c b/src/libmbim-glib/mbim-uuid.c index 82ad609..f388604 100644 --- a/src/libmbim-glib/mbim-uuid.c +++ b/src/libmbim-glib/mbim-uuid.c @@ -18,7 +18,8 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * Copyright (C) 2013 Aleksander Morgado <aleksander@gnu.org> + * Copyright (C) 2013 - 2014 Aleksander Morgado <aleksander@gnu.org> + * Copyright (C) 2014 NVDIA Corporation */ #include <config.h> @@ -77,6 +78,55 @@ mbim_uuid_get_printable (const MbimUuid *uuid) uuid->e[0], uuid->e[1], uuid->e[2], uuid->e[3], uuid->e[4], uuid->e[5])); } +/** + * mbim_uuid_from_printable: + * @str: a MBIM UUID. + * @uuid: pointer to the target #MbimUuid. + * + * Fills in @uuid from the printable representation give in @str. + * + * Accepts @str written with or without dashes separating items, e.g.: + * a289cc33-bcbb-8b4f-b6b0-133ec2aae6df + * a289cc33bcbb8b4fb6b0133ec2aae6df + * + * Returns: %TRUE if @uuid was correctly set, %FALSE otherwise. + */ +gboolean +mbim_uuid_from_printable (const gchar *str, + MbimUuid *uuid) +{ + guint a0, a1, a2, a3; + guint b0, b1; + guint c0, c1; + guint d0, d1; + guint e0, e1, e2, e3, e4, e5; + + g_return_val_if_fail (str != NULL, FALSE); + g_return_val_if_fail (uuid != NULL, FALSE); + + if ((strlen (str) != 36) || + (sscanf (str, + "%02x%02x%02x%02x-" + "%02x%02x-" + "%02x%02x-" + "%02x%02x-" + "%02x%02x%02x%02x%02x%02x", + &a0, &a1, &a2, &a3, + &b0, &b1, + &c0, &c1, + &d0, &d1, + &e0, &e1, &e2, &e3, &e4, &e5) == 0)) + return FALSE; + + uuid->a[0] = a0; uuid->a[1] = a1; uuid->a[2] = a2; uuid->a[3] = a3; + uuid->b[0] = b0; uuid->b[1] = b1; + uuid->c[0] = c0; uuid->c[1] = c1; + uuid->d[0] = d0; uuid->d[1] = d1; + uuid->e[0] = e0; uuid->e[1] = e1; uuid->e[2] = e2; uuid->e[3] = e3; uuid->e[4] = e4; uuid->e[5] = e5; + + return TRUE; +} + /*****************************************************************************/ static const MbimUuid uuid_invalid = { diff --git a/src/libmbim-glib/mbim-uuid.h b/src/libmbim-glib/mbim-uuid.h index c05ce41..c79b5e1 100644 --- a/src/libmbim-glib/mbim-uuid.h +++ b/src/libmbim-glib/mbim-uuid.h @@ -50,9 +50,11 @@ struct MBIM_PACKED _MbimUuid { }; #undef MBIM_PACKED -gboolean mbim_uuid_cmp (const MbimUuid *a, - const MbimUuid *b); -gchar *mbim_uuid_get_printable (const MbimUuid *uuid); +gboolean mbim_uuid_cmp (const MbimUuid *a, + const MbimUuid *b); +gchar *mbim_uuid_get_printable (const MbimUuid *uuid); +gboolean mbim_uuid_from_printable (const gchar *str, + MbimUuid *uuid); /*****************************************************************************/ diff --git a/src/libmbim-glib/test/test-uuid.c b/src/libmbim-glib/test/test-uuid.c index 8b0667c..8866fb7 100644 --- a/src/libmbim-glib/test/test-uuid.c +++ b/src/libmbim-glib/test/test-uuid.c @@ -83,6 +83,74 @@ test_uuid_ms_firmware_id (void) compare_uuid_strings (MBIM_UUID_MS_FIRMWARE_ID, "e9f7dea2-feaf-4009-93ce-90a3694103b6"); } + +/*****************************************************************************/ + +static void +compare_uuid_types (const gchar *uuid_str, + const MbimUuid *other_uuid) +{ + MbimUuid uuid; + + g_assert (mbim_uuid_from_printable (uuid_str, &uuid)); + g_assert (mbim_uuid_cmp (&uuid, other_uuid)); +} + +static void +invalid_uuid_str (const gchar *uuid_str) +{ + MbimUuid uuid; + + g_assert (mbim_uuid_from_printable (uuid_str, &uuid) == FALSE); +} + +static void +test_uuid_valid (void) +{ + compare_uuid_types ("a289cc33-bcbb-8b4f-b6b0-133ec2aae6df", + MBIM_UUID_BASIC_CONNECT); +} + +static void +test_uuid_valid_camelcase (void) +{ + compare_uuid_types ("A289cC33-BcBb-8B4f-B6b0-133Ec2Aae6Df", + MBIM_UUID_BASIC_CONNECT); +} + +static void +test_uuid_invalid_empty (void) +{ + invalid_uuid_str (""); +} + +static void +test_uuid_invalid_short (void) +{ + invalid_uuid_str ("a289cc33-bcbb-8b4f-b6b0-"); +} + +static void +test_uuid_invalid_long (void) +{ + invalid_uuid_str ("a289cc33-bcbb-8b4f-b6b0-133ec2aae6dfaaa"); +} + +static void +test_uuid_invalid_dashes (void) +{ + /* Dashes one too early */ + invalid_uuid_str ("289cc33-bcbb-8b4f-b6b0-133ec2aae6dfa"); +} + +static void +test_uuid_invalid_no_hex (void) +{ + invalid_uuid_str ("hello wo-rld -8b4f-b6b0-133ec2aae6df"); +} + +/*****************************************************************************/ + int main (int argc, char **argv) { g_test_init (&argc, &argv, NULL); @@ -96,5 +164,14 @@ int main (int argc, char **argv) g_test_add_func ("/libmbim-glib/uuid/dss", test_uuid_dss); g_test_add_func ("/libmbim-glib/uuid/ms-firmware-id", test_uuid_ms_firmware_id); + g_test_add_func ("/libmbim-glib/uuid/valid", test_uuid_valid); + g_test_add_func ("/libmbim-glib/uuid/valid/camelcase", test_uuid_valid_camelcase); + + g_test_add_func ("/libmbim-glib/uuid/invalid/empty", test_uuid_invalid_empty); + g_test_add_func ("/libmbim-glib/uuid/invalid/short", test_uuid_invalid_short); + g_test_add_func ("/libmbim-glib/uuid/invalid/long", test_uuid_invalid_long); + g_test_add_func ("/libmbim-glib/uuid/invalid/dashes", test_uuid_invalid_dashes); + g_test_add_func ("/libmbim-glib/uuid/invalid/no-hex", test_uuid_invalid_no_hex); + return g_test_run (); } |