summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArnaud Desmier <adesmier@sequans.com>2014-04-10 11:18:47 +0200
committerAleksander Morgado <aleksander@aleksander.es>2014-04-10 17:08:14 +0200
commitf7d3ec13cc9b2c5952b7db84f269423a4049e8a9 (patch)
tree49d1ea85fc34065af5b593967985da0f28bec021
parentb1ae81a75a1a68ea1c7316047528e79092bf0d37 (diff)
libmbim: add support for custom services
* use mbim_register_custom_service API to register a new service. * use mbim_unregister_custom_service API to unregister it.
-rw-r--r--docs/reference/libmbim-glib/libmbim-glib-common.sections4
-rw-r--r--src/libmbim-glib/mbim-message.c8
-rw-r--r--src/libmbim-glib/mbim-uuid.c121
-rw-r--r--src/libmbim-glib/mbim-uuid.h11
-rw-r--r--src/libmbim-glib/test/test-uuid.c31
5 files changed, 170 insertions, 5 deletions
diff --git a/docs/reference/libmbim-glib/libmbim-glib-common.sections b/docs/reference/libmbim-glib/libmbim-glib-common.sections
index 59f6021..a1d1103 100644
--- a/docs/reference/libmbim-glib/libmbim-glib-common.sections
+++ b/docs/reference/libmbim-glib/libmbim-glib-common.sections
@@ -24,6 +24,10 @@ MBIM_UUID_MS_FIRMWARE_ID
MBIM_UUID_MS_HOST_SHUTDOWN
<SUBSECTION Methods>
mbim_service_get_string
+mbim_service_lookup_name
+mbim_register_custom_service
+mbim_unregister_custom_service
+mbim_service_id_is_custom
mbim_context_type_get_string
mbim_uuid_cmp
mbim_uuid_from_printable
diff --git a/src/libmbim-glib/mbim-message.c b/src/libmbim-glib/mbim-message.c
index 1a37766..7078129 100644
--- a/src/libmbim-glib/mbim-message.c
+++ b/src/libmbim-glib/mbim-message.c
@@ -1198,7 +1198,7 @@ mbim_message_get_printable (const MbimMessage *self,
"%s cid = '%s' (0x%08x)\n"
"%s type = '%s' (0x%08x)\n",
line_prefix,
- line_prefix, mbim_service_get_string (mbim_message_command_get_service (self)), uuid_printable,
+ line_prefix, mbim_service_lookup_name (mbim_message_command_get_service (self)), uuid_printable,
line_prefix, cid_printable, mbim_message_command_get_cid (self),
line_prefix, mbim_message_command_type_get_string (mbim_message_command_get_command_type (self)), mbim_message_command_get_command_type (self));
g_free (uuid_printable);
@@ -1229,7 +1229,7 @@ mbim_message_get_printable (const MbimMessage *self,
"%s cid = '%s' (0x%08x)\n",
line_prefix,
line_prefix, mbim_status_error_get_string (status), status,
- line_prefix, mbim_service_get_string (mbim_message_command_done_get_service (self)), uuid_printable,
+ line_prefix, mbim_service_lookup_name (mbim_message_command_done_get_service (self)), uuid_printable,
line_prefix, cid_printable, mbim_message_command_done_get_cid (self));
g_free (uuid_printable);
}
@@ -1255,7 +1255,7 @@ mbim_message_get_printable (const MbimMessage *self,
"%s service = '%s' (%s)\n"
"%s cid = '%s' (0x%08x)\n",
line_prefix,
- line_prefix, mbim_service_get_string (mbim_message_indicate_status_get_service (self)), uuid_printable,
+ line_prefix, mbim_service_lookup_name (mbim_message_indicate_status_get_service (self)), uuid_printable,
line_prefix, cid_printable, mbim_message_indicate_status_get_cid (self));
g_free (uuid_printable);
}
@@ -1738,7 +1738,7 @@ mbim_message_command_new (guint32 transaction_id,
/* Known service required */
g_return_val_if_fail (service > MBIM_SERVICE_INVALID, FALSE);
- g_return_val_if_fail (service <= MBIM_SERVICE_MS_HOST_SHUTDOWN, FALSE);
+ g_return_val_if_fail (service <= MBIM_SERVICE_MS_HOST_SHUTDOWN || mbim_service_id_is_custom (service), FALSE);
service_id = mbim_uuid_from_service (service);
self = _mbim_message_allocate (MBIM_MESSAGE_TYPE_COMMAND,
diff --git a/src/libmbim-glib/mbim-uuid.c b/src/libmbim-glib/mbim-uuid.c
index 2ee5e5e..3d168c9 100644
--- a/src/libmbim-glib/mbim-uuid.c
+++ b/src/libmbim-glib/mbim-uuid.c
@@ -27,6 +27,7 @@
#include <string.h>
#include "mbim-uuid.h"
+#include "generated/mbim-enum-types.h"
/**
* SECTION: mbim-uuid
@@ -217,6 +218,108 @@ static const MbimUuid uuid_ms_host_shutdown = {
.e = { 0x27, 0xd7, 0xfb, 0x80, 0x95, 0x9c }
};
+static GList *mbim_custom_service_list = NULL;
+
+typedef struct {
+ guint service_id;
+ MbimUuid uuid;
+ gchar *nickname;
+} MbimCustomService;
+
+/**
+ * mbim_register_custom_service:
+ * @uuid: MbimUuid structure corresponding to service
+ * @nickname: a printable name for service
+ *
+ * Register a custom service
+ *
+ * Return: TRUE if service has been registered, FALSE if not
+ */
+guint
+mbim_register_custom_service (const MbimUuid *uuid,
+ const gchar *nickname)
+{
+ MbimCustomService *s;
+ GList *l;
+ guint service_id = 100;
+
+ for (l = mbim_custom_service_list; l != NULL; l = l->next) {
+ s = (MbimCustomService *)l->data;
+ if (mbim_uuid_cmp (&s->uuid, uuid))
+ return s->service_id;
+ else
+ service_id = MAX (service_id, s->service_id);
+ }
+
+ /* create a new custom service */
+ s = g_slice_new (MbimCustomService);
+ s->service_id = service_id + 1;
+ memcpy (&s->uuid, uuid, sizeof (MbimUuid));
+ s->nickname = g_strdup (nickname);
+
+ mbim_custom_service_list = g_list_append (mbim_custom_service_list, s);
+ return s->service_id;
+}
+
+gboolean
+mbim_unregister_custom_service (const guint id)
+{
+ MbimCustomService *s;
+ GList *l;
+
+ for (l = mbim_custom_service_list; l != NULL; l = l->next) {
+ s = (MbimCustomService *)l->data;
+ if (s->service_id == id) {
+ g_free (s->nickname);
+ g_slice_free (MbimCustomService, s);
+ mbim_custom_service_list = \
+ g_list_delete_link (mbim_custom_service_list, l);
+ return TRUE;
+ }
+ }
+
+ return FALSE;
+}
+
+gboolean
+mbim_service_id_is_custom (const guint id)
+{
+ GList *l;
+
+ if (id <= MBIM_SERVICE_MS_HOST_SHUTDOWN)
+ return FALSE;
+
+ for (l = mbim_custom_service_list; l != NULL; l = l->next) {
+ if (((MbimCustomService *)l->data)->service_id == id)
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+/**
+ * mbim_service_lookup_name:
+ * @service: a MbimService or custom service.
+ *
+ * Gets the nickname string for the @service.
+ *
+ * Returns: (transfer none): a string with the nickname, or %NULL if not found. Do not free the returned value.
+ */
+const gchar *
+mbim_service_lookup_name (guint service)
+{
+ GList *l;
+
+ if (service <= MBIM_SERVICE_MS_HOST_SHUTDOWN)
+ return mbim_service_get_string (service);
+
+ for (l = mbim_custom_service_list; l != NULL; l = l->next) {
+ if (service == ((MbimCustomService *)l->data)->service_id)
+ return ((MbimCustomService *)l->data)->nickname;
+ }
+ return NULL;
+}
+
/**
* mbim_uuid_from_service:
* @service: a #MbimService.
@@ -228,7 +331,11 @@ static const MbimUuid uuid_ms_host_shutdown = {
const MbimUuid *
mbim_uuid_from_service (MbimService service)
{
- g_return_val_if_fail (service >= MBIM_SERVICE_INVALID && service <= MBIM_SERVICE_MS_HOST_SHUTDOWN,
+ GList *l;
+
+ g_return_val_if_fail (service >= MBIM_SERVICE_INVALID &&
+ (service <= MBIM_SERVICE_MS_HOST_SHUTDOWN ||
+ mbim_service_id_is_custom (service)),
&uuid_invalid);
switch (service) {
@@ -253,6 +360,10 @@ mbim_uuid_from_service (MbimService service)
case MBIM_SERVICE_MS_HOST_SHUTDOWN:
return &uuid_ms_host_shutdown;
default:
+ for (l = mbim_custom_service_list; l != NULL; l = l->next) {
+ if (service == ((MbimCustomService *)l->data)->service_id)
+ return &((MbimCustomService *)l->data)->uuid;
+ }
g_assert_not_reached ();
}
}
@@ -268,6 +379,8 @@ mbim_uuid_from_service (MbimService service)
MbimService
mbim_uuid_to_service (const MbimUuid *uuid)
{
+ GList *l;
+
if (mbim_uuid_cmp (uuid, &uuid_basic_connect))
return MBIM_SERVICE_BASIC_CONNECT;
@@ -295,6 +408,11 @@ mbim_uuid_to_service (const MbimUuid *uuid)
if (mbim_uuid_cmp (uuid, &uuid_ms_host_shutdown))
return MBIM_SERVICE_MS_HOST_SHUTDOWN;
+ for (l = mbim_custom_service_list; l != NULL; l = l->next) {
+ if (mbim_uuid_cmp (&((MbimCustomService *)l->data)->uuid, uuid))
+ return ((MbimCustomService *)l->data)->service_id;
+ }
+
return MBIM_SERVICE_INVALID;
}
@@ -372,6 +490,7 @@ static const MbimUuid uuid_context_type_local = {
.e = { 0x03, 0x3C, 0x39, 0xF6, 0x0D, 0xB9 }
};
+
/**
* mbim_uuid_from_context_type:
* @context_type: a #MbimContextType.
diff --git a/src/libmbim-glib/mbim-uuid.h b/src/libmbim-glib/mbim-uuid.h
index 50430b0..a9e6960 100644
--- a/src/libmbim-glib/mbim-uuid.h
+++ b/src/libmbim-glib/mbim-uuid.h
@@ -176,6 +176,15 @@ typedef enum {
*/
#define MBIM_UUID_MS_HOST_SHUTDOWN mbim_uuid_from_service (MBIM_SERVICE_MS_HOST_SHUTDOWN)
+const gchar *mbim_service_lookup_name (MbimService val);
+
+guint mbim_register_custom_service (const MbimUuid *uuid,
+ const gchar *nickname);
+
+gboolean mbim_unregister_custom_service (const guint id);
+
+gboolean mbim_service_id_is_custom (const guint id);
+
/* To/From service */
const MbimUuid *mbim_uuid_from_service (MbimService service);
MbimService mbim_uuid_to_service (const MbimUuid *uuid);
@@ -210,6 +219,8 @@ typedef enum {
MBIM_CONTEXT_TYPE_LOCAL = 9,
} MbimContextType;
+const gchar *mbim_context_type_get_string (MbimContextType val);
+
/* To/From context type */
const MbimUuid *mbim_uuid_from_context_type (MbimContextType context_type);
MbimContextType mbim_uuid_to_context_type (const MbimUuid *uuid);
diff --git a/src/libmbim-glib/test/test-uuid.c b/src/libmbim-glib/test/test-uuid.c
index ce6e4bf..d8950ad 100644
--- a/src/libmbim-glib/test/test-uuid.c
+++ b/src/libmbim-glib/test/test-uuid.c
@@ -158,6 +158,35 @@ test_uuid_invalid_no_hex (void)
/*****************************************************************************/
+static void
+test_uuid_custom (void)
+{
+ static const gchar *nick = "register_custom";
+ static const MbimUuid uuid_custom = {
+ .a = { 0x52, 0x65, 0x67, 0x69 },
+ .b = { 0x73, 0x74 },
+ .c = { 0x65, 0x72 },
+ .d = { 0x20, 0x63 },
+ .e = { 0x75, 0x73, 0x74, 0x6f, 0x6d, 0x21 }
+ };
+ guint service;
+
+ /* SERVICE_AUTH is not a custom service */
+ g_assert (!mbim_service_id_is_custom (MBIM_SERVICE_AUTH));
+
+ service = mbim_register_custom_service (&uuid_custom, nick);
+ g_assert (mbim_service_id_is_custom (service));
+ g_assert (g_strcmp0 (mbim_service_lookup_name (service), nick) == 0);
+ g_assert (mbim_uuid_cmp (mbim_uuid_from_service (service), &uuid_custom));
+ g_assert (mbim_uuid_to_service (&uuid_custom) == service);
+ g_assert (mbim_unregister_custom_service (service));
+
+ /* once removed service is not custom */
+ g_assert (!mbim_service_id_is_custom (service));
+}
+
+/*****************************************************************************/
+
int main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
@@ -181,5 +210,7 @@ int main (int argc, char **argv)
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);
+ g_test_add_func ("/libmbim-glib/uuid/custom", test_uuid_custom);
+
return g_test_run ();
}