aboutsummaryrefslogtreecommitdiff
path: root/libmm-glib/mm-bearer-stats.c
blob: df499ca59b2f7f9330ea5d3f5bff1950ca042df4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* -*- 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@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_RX_BYTES "rx-bytes"
#define PROPERTY_TX_BYTES "tx-bytes"

struct _MMBearerStatsPrivate {
    guint   duration;
    guint64 rx_bytes;
    guint64 tx_bytes;
};

/*****************************************************************************/

/**
 * mm_bearer_stats_get_duration:
 * @self: a #MMBearerStats.
 *
 * Gets the duration of the current connection, in seconds.
 *
 * Returns: a #guint.
 *
 * Since: 1.6
 */
guint
mm_bearer_stats_get_duration (MMBearerStats *self)
{
    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);

    return self->priv->duration;
}

/**
 * mm_bearer_stats_set_duration: (skip)
 */
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_rx_bytes:
 * @self: a #MMBearerStats.
 *
 * Gets the number of bytes received without error in the connection.
 *
 * Returns: a #guint64.
 *
 * Since: 1.6
 */
guint64
mm_bearer_stats_get_rx_bytes (MMBearerStats *self)
{
    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);

    return self->priv->rx_bytes;
}

/**
 * mm_bearer_stats_set_rx_bytes: (skip)
 */
void
mm_bearer_stats_set_rx_bytes (MMBearerStats *self,
                              guint64 bytes)
{
    g_return_if_fail (MM_IS_BEARER_STATS (self));

    self->priv->rx_bytes = bytes;
}

/*****************************************************************************/

/**
 * mm_bearer_stats_get_tx_bytes:
 * @self: a #MMBearerStats.
 *
 * Gets the number of bytes transmitted without error in the connection.
 *
 * Returns: a #guint64.
 *
 * Since: 1.6
 */
guint64
mm_bearer_stats_get_tx_bytes (MMBearerStats *self)
{
    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);

    return self->priv->tx_bytes;
}

/**
 * mm_bearer_stats_set_tx_bytes: (skip)
 */
void
mm_bearer_stats_set_tx_bytes (MMBearerStats *self,
                              guint64 bytes)
{
    g_return_if_fail (MM_IS_BEARER_STATS (self));

    self->priv->tx_bytes = bytes;
}

/*****************************************************************************/

/**
 * mm_bearer_stats_get_dictionary: (skip)
 */
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_RX_BYTES,
                            g_variant_new_uint64 (self->priv->rx_bytes));
    g_variant_builder_add  (&builder,
                            "{sv}",
                            PROPERTY_TX_BYTES,
                            g_variant_new_uint64 (self->priv->tx_bytes));
    return g_variant_builder_end (&builder);
}

/*****************************************************************************/

/**
 * mm_bearer_stats_new_from_dictionary: (skip)
 */
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_RX_BYTES)) {
            mm_bearer_stats_set_rx_bytes (
                self,
                g_variant_get_uint64 (value));
        } else if (g_str_equal (key, PROPERTY_TX_BYTES)) {
            mm_bearer_stats_set_tx_bytes (
                self,
                g_variant_get_uint64 (value));
        }
        g_free (key);
        g_variant_unref (value);
    }

    return self;
}

/*****************************************************************************/

/**
 * mm_bearer_stats_new: (skip)
 */
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));
}