[PATCH 2/6] libmm-glib: new helper object to handle bearer stats

Aleksander Morgado aleksander at aleksander.es
Fri Nov 27 05:51:15 PST 2015


The new MMBearerStats object provides a simple API to interact with the new
dictionary containing the bearer connection stats.
---
 docs/reference/libmm-glib/libmm-glib-docs.xml     |   1 +
 docs/reference/libmm-glib/libmm-glib-sections.txt |  30 +++
 libmm-glib/Makefile.am                            |   3 +
 libmm-glib/mm-bearer-stats.c                      | 222 ++++++++++++++++++++++
 libmm-glib/mm-bearer-stats.h                      |  85 +++++++++
 5 files changed, 341 insertions(+)
 create mode 100644 libmm-glib/mm-bearer-stats.c
 create mode 100644 libmm-glib/mm-bearer-stats.h

diff --git a/docs/reference/libmm-glib/libmm-glib-docs.xml b/docs/reference/libmm-glib/libmm-glib-docs.xml
index 38a0b9f..810eb96 100644
--- a/docs/reference/libmm-glib/libmm-glib-docs.xml
+++ b/docs/reference/libmm-glib/libmm-glib-docs.xml
@@ -130,6 +130,7 @@
       <xi:include href="xml/mm-bearer.xml"/>
       <xi:include href="xml/mm-bearer-properties.xml"/>
       <xi:include href="xml/mm-bearer-ip-config.xml"/>
+      <xi:include href="xml/mm-bearer-stats.xml"/>
     </chapter>
 
     <chapter>
diff --git a/docs/reference/libmm-glib/libmm-glib-sections.txt b/docs/reference/libmm-glib/libmm-glib-sections.txt
index 773b48d..b17f281 100644
--- a/docs/reference/libmm-glib/libmm-glib-sections.txt
+++ b/docs/reference/libmm-glib/libmm-glib-sections.txt
@@ -987,6 +987,33 @@ mm_bearer_ip_config_get_type
 </SECTION>
 
 <SECTION>
+<FILE>mm-bearer-stats</FILE>
+<TITLE>MMBearerStats</TITLE>
+MMBearerStats
+<SUBSECTION Getters>
+mm_bearer_stats_get_duration
+mm_bearer_stats_get_bytes_rx
+mm_bearer_stats_get_bytes_tx
+<SUBSECTION Private>
+mm_bearer_stats_get_dictionary
+mm_bearer_stats_new
+mm_bearer_stats_new_from_dictionary
+mm_bearer_stats_set_duration
+mm_bearer_stats_set_bytes_rx
+mm_bearer_stats_set_bytes_tx
+<SUBSECTION Standard>
+MMBearerStatsClass
+MMBearerStatsPrivate
+MM_BEARER_STATS
+MM_BEARER_STATS_CLASS
+MM_BEARER_STATS_GET_CLASS
+MM_IS_BEARER_STATS
+MM_IS_BEARER_STATS_CLASS
+MM_TYPE_BEARER_STATS
+mm_bearer_stats_get_type
+</SECTION>
+
+<SECTION>
 <FILE>mm-bearer-properties</FILE>
 <TITLE>MMBearerProperties</TITLE>
 MMBearerProperties
@@ -1439,6 +1466,8 @@ mm_gdbus_bearer_get_properties
 mm_gdbus_bearer_dup_properties
 mm_gdbus_bearer_get_connected
 mm_gdbus_bearer_get_suspended
+mm_gdbus_bearer_get_stats
+mm_gdbus_bearer_dup_stats
 <SUBSECTION Methods>
 mm_gdbus_bearer_call_connect
 mm_gdbus_bearer_call_connect_finish
@@ -1455,6 +1484,7 @@ mm_gdbus_bearer_set_ip6_config
 mm_gdbus_bearer_set_ip_timeout
 mm_gdbus_bearer_set_properties
 mm_gdbus_bearer_set_suspended
+mm_gdbus_bearer_set_stats
 mm_gdbus_bearer_override_properties
 mm_gdbus_bearer_complete_connect
 mm_gdbus_bearer_complete_disconnect
diff --git a/libmm-glib/Makefile.am b/libmm-glib/Makefile.am
index cb8f63a..4b7ab4f 100644
--- a/libmm-glib/Makefile.am
+++ b/libmm-glib/Makefile.am
@@ -60,6 +60,8 @@ libmm_glib_la_SOURCES = \
 	mm-call-properties.c \
 	mm-bearer-ip-config.h \
 	mm-bearer-ip-config.c \
+	mm-bearer-stats.h \
+	mm-bearer-stats.c \
 	mm-location-common.h \
 	mm-location-3gpp.h \
 	mm-location-3gpp.c \
@@ -131,6 +133,7 @@ include_HEADERS = \
 	mm-sms-properties.h \
 	mm-call-properties.h \
 	mm-bearer-ip-config.h \
+	mm-bearer-stats.h \
 	mm-location-common.h \
 	mm-location-3gpp.h \
 	mm-location-gps-nmea.h \
diff --git a/libmm-glib/mm-bearer-stats.c b/libmm-glib/mm-bearer-stats.c
new file mode 100644
index 0000000..f00caa5
--- /dev/null
+++ b/libmm-glib/mm-bearer-stats.c
@@ -0,0 +1,222 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details:
+ *
+ * Copyright (C) 2015 Azimut Electronics
+ *
+ * Author: Aleksander Morgado <aleksander at aleksander.es>
+ */
+
+#include <string.h>
+
+#include "mm-errors-types.h"
+#include "mm-bearer-stats.h"
+
+/**
+ * SECTION: mm-bearer-stats
+ * @title: MMBearerStats
+ * @short_description: Helper object to handle bearer stats.
+ *
+ * The #MMBearerStats is an object handling the statistics reported by the
+ * bearer object during a connection.
+ *
+ * This object is retrieved with either mm_bearer_get_stats() or
+ * mm_bearer_peek_stats().
+ */
+
+G_DEFINE_TYPE (MMBearerStats, mm_bearer_stats, G_TYPE_OBJECT)
+
+#define PROPERTY_DURATION "duration"
+#define PROPERTY_BYTES_RX "bytes-rx"
+#define PROPERTY_BYTES_TX "bytes-tx"
+
+struct _MMBearerStatsPrivate {
+    guint   duration;
+    guint64 bytes_rx;
+    guint64 bytes_tx;
+};
+
+/*****************************************************************************/
+
+/**
+ * mm_bearer_stats_get_duration:
+ * @self: a #MMBearerStats.
+ *
+ * Gets the duration of the current connection, in seconds.
+ *
+ * Returns: a #guint.
+ */
+guint
+mm_bearer_stats_get_duration (MMBearerStats *self)
+{
+    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);
+
+    return self->priv->duration;
+}
+
+void
+mm_bearer_stats_set_duration (MMBearerStats *self,
+                              guint duration)
+{
+    g_return_if_fail (MM_IS_BEARER_STATS (self));
+
+    self->priv->duration = duration;
+}
+
+/*****************************************************************************/
+
+/**
+ * mm_bearer_stats_get_bytes_rx:
+ * @self: a #MMBearerStats.
+ *
+ * Gets the number of bytes received without error in the connection.
+ *
+ * Returns: a #guint64.
+ */
+guint64
+mm_bearer_stats_get_bytes_rx (MMBearerStats *self)
+{
+    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);
+
+    return self->priv->bytes_rx;
+}
+
+void
+mm_bearer_stats_set_bytes_rx (MMBearerStats *self,
+                              guint64 bytes)
+{
+    g_return_if_fail (MM_IS_BEARER_STATS (self));
+
+    self->priv->bytes_rx = bytes;
+}
+
+/*****************************************************************************/
+
+/**
+ * mm_bearer_stats_get_bytes_tx:
+ * @self: a #MMBearerStats.
+ *
+ * Gets the number of bytes transmitted without error in the connection.
+ *
+ * Returns: a #guint64.
+ */
+guint64
+mm_bearer_stats_get_bytes_tx (MMBearerStats *self)
+{
+    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);
+
+    return self->priv->bytes_tx;
+}
+
+void
+mm_bearer_stats_set_bytes_tx (MMBearerStats *self,
+                              guint64 bytes)
+{
+    g_return_if_fail (MM_IS_BEARER_STATS (self));
+
+    self->priv->bytes_tx = bytes;
+}
+
+/*****************************************************************************/
+
+GVariant *
+mm_bearer_stats_get_dictionary (MMBearerStats *self)
+{
+    GVariantBuilder builder;
+
+    /* We do allow self==NULL. We'll just report NULL. */
+    if (!self)
+        return NULL;
+
+    g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
+    g_variant_builder_add  (&builder,
+                            "{sv}",
+                            PROPERTY_DURATION,
+                            g_variant_new_uint32 (self->priv->duration));
+    g_variant_builder_add  (&builder,
+                            "{sv}",
+                            PROPERTY_BYTES_RX,
+                            g_variant_new_uint64 (self->priv->bytes_rx));
+    g_variant_builder_add  (&builder,
+                            "{sv}",
+                            PROPERTY_BYTES_TX,
+                            g_variant_new_uint64 (self->priv->bytes_tx));
+    return g_variant_builder_end (&builder);
+}
+
+/*****************************************************************************/
+
+MMBearerStats *
+mm_bearer_stats_new_from_dictionary (GVariant *dictionary,
+                                     GError **error)
+{
+    GVariantIter iter;
+    gchar *key;
+    GVariant *value;
+    MMBearerStats *self;
+
+    self = mm_bearer_stats_new ();
+    if (!dictionary)
+        return self;
+
+    if (!g_variant_is_of_type (dictionary, G_VARIANT_TYPE ("a{sv}"))) {
+        g_set_error (error,
+                     MM_CORE_ERROR,
+                     MM_CORE_ERROR_INVALID_ARGS,
+                     "Cannot create Stats from dictionary: "
+                     "invalid variant type received");
+        g_object_unref (self);
+        return NULL;
+    }
+
+    g_variant_iter_init (&iter, dictionary);
+    while (g_variant_iter_next (&iter, "{sv}", &key, &value)) {
+        if (g_str_equal (key, PROPERTY_DURATION)) {
+            mm_bearer_stats_set_duration (
+                self,
+                g_variant_get_uint32 (value));
+        } else if (g_str_equal (key, PROPERTY_BYTES_RX)) {
+            mm_bearer_stats_set_bytes_rx (
+                self,
+                g_variant_get_uint64 (value));
+        } else if (g_str_equal (key, PROPERTY_BYTES_TX)) {
+            mm_bearer_stats_set_bytes_tx (
+                self,
+                g_variant_get_uint64 (value));
+        }
+        g_free (key);
+        g_variant_unref (value);
+    }
+
+    return self;
+}
+
+/*****************************************************************************/
+
+MMBearerStats *
+mm_bearer_stats_new (void)
+{
+    return (MM_BEARER_STATS (g_object_new (MM_TYPE_BEARER_STATS, NULL)));
+}
+
+static void
+mm_bearer_stats_init (MMBearerStats *self)
+{
+    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, MM_TYPE_BEARER_STATS, MMBearerStatsPrivate);
+}
+
+static void
+mm_bearer_stats_class_init (MMBearerStatsClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+    g_type_class_add_private (object_class, sizeof (MMBearerStatsPrivate));
+}
diff --git a/libmm-glib/mm-bearer-stats.h b/libmm-glib/mm-bearer-stats.h
new file mode 100644
index 0000000..ea5d37f
--- /dev/null
+++ b/libmm-glib/mm-bearer-stats.h
@@ -0,0 +1,85 @@
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details:
+ *
+ * Copyright (C) 2015 Azimut Electronics
+ *
+ * Author: Aleksander Morgado <aleksander at aleksander.es>
+ */
+
+#ifndef MM_BEARER_STATS_H
+#define MM_BEARER_STATS_H
+
+#if !defined (__LIBMM_GLIB_H_INSIDE__) && !defined (LIBMM_GLIB_COMPILATION)
+#error "Only <libmm-glib.h> can be included directly."
+#endif
+
+#include <ModemManager.h>
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define MM_TYPE_BEARER_STATS            (mm_bearer_stats_get_type ())
+#define MM_BEARER_STATS(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), MM_TYPE_BEARER_STATS, MMBearerStats))
+#define MM_BEARER_STATS_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  MM_TYPE_BEARER_STATS, MMBearerStatsClass))
+#define MM_IS_BEARER_STATS(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), MM_TYPE_BEARER_STATS))
+#define MM_IS_BEARER_STATS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  MM_TYPE_BEARER_STATS))
+#define MM_BEARER_STATS_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  MM_TYPE_BEARER_STATS, MMBearerStatsClass))
+
+typedef struct _MMBearerStats MMBearerStats;
+typedef struct _MMBearerStatsClass MMBearerStatsClass;
+typedef struct _MMBearerStatsPrivate MMBearerStatsPrivate;
+
+/**
+ * MMBearerStats:
+ *
+ * The #MMBearerStats structure contains private data and should
+ * only be accessed using the provided API.
+ */
+struct _MMBearerStats {
+    /*< private >*/
+    GObject parent;
+    MMBearerStatsPrivate *priv;
+};
+
+struct _MMBearerStatsClass {
+    /*< private >*/
+    GObjectClass parent;
+};
+
+GType mm_bearer_stats_get_type (void);
+
+guint   mm_bearer_stats_get_duration (MMBearerStats *self);
+guint64 mm_bearer_stats_get_bytes_rx (MMBearerStats *self);
+guint64 mm_bearer_stats_get_bytes_tx (MMBearerStats *self);
+
+/*****************************************************************************/
+/* ModemManager/libmm-glib/mmcli specific methods */
+
+#if defined (_LIBMM_INSIDE_MM) ||    \
+    defined (_LIBMM_INSIDE_MMCLI) || \
+    defined (LIBMM_GLIB_COMPILATION)
+
+MMBearerStats *mm_bearer_stats_new (void);
+MMBearerStats *mm_bearer_stats_new_from_dictionary (GVariant *dictionary,
+                                                    GError **error);
+
+void mm_bearer_stats_set_duration (MMBearerStats *self, guint duration);
+void mm_bearer_stats_set_bytes_rx (MMBearerStats *self, guint64 bytes_rx);
+void mm_bearer_stats_set_bytes_tx (MMBearerStats *self, guint64 bytes_tx);
+
+GVariant *mm_bearer_stats_get_dictionary (MMBearerStats *self);
+
+#endif
+
+G_END_DECLS
+
+#endif /* MM_BEARER_STATS_H */
-- 
2.6.2



More information about the ModemManager-devel mailing list