summaryrefslogtreecommitdiff
path: root/libmm-glib/mm-bearer-stats.c
blob: 61b24de36063f5006b7ad0d29d1ced7be110c6cf (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/* -*- 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-2021 Azimut Electronics
 * Copyright (C) 2015-2021 Aleksander Morgado <aleksander@aleksander.es>
 * Copyright (C) 2021 Intel Corporation
 */

#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"
#define PROPERTY_START_DATE      "start-date"
#define PROPERTY_ATTEMPTS        "attempts"
#define PROPERTY_FAILED_ATTEMPTS "failed-attempts"
#define PROPERTY_TOTAL_DURATION  "total-duration"
#define PROPERTY_TOTAL_RX_BYTES  "total-rx-bytes"
#define PROPERTY_TOTAL_TX_BYTES  "total-tx-bytes"
#define PROPERTY_UPLINK_SPEED    "uplink-speed"
#define PROPERTY_DOWNLINK_SPEED  "downlink-speed"

struct _MMBearerStatsPrivate {
    guint   duration;
    guint64 rx_bytes;
    guint64 tx_bytes;
    guint64 start_date;
    guint   attempts;
    guint   failed_attempts;
    guint   total_duration;
    guint64 total_rx_bytes;
    guint64 total_tx_bytes;
    guint64 uplink_speed;
    guint64 downlink_speed;
};

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

/**
 * 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_start_date:
 * @self: a #MMBearerStats.
 *
 * Gets the start date of the current connection as a timestamp in seconds
 * since the epoch.
 *
 * Returns: a #guint64.
 *
 * Since: 1.20
 */
guint64
mm_bearer_stats_get_start_date (MMBearerStats *self)
{
    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);

    return self->priv->start_date;
}

/**
 * mm_bearer_stats_set_start_date: (skip)
 */
void
mm_bearer_stats_set_start_date (MMBearerStats *self,
                                guint64        start_date)
{
    g_return_if_fail (MM_IS_BEARER_STATS (self));

    self->priv->start_date = start_date;
}

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

/**
 * mm_bearer_stats_get_attempts:
 * @self: a #MMBearerStats.
 *
 * Gets the number of connection attempts done with this bearer.
 *
 * Returns: a #guint.
 *
 * Since: 1.14
 */
guint
mm_bearer_stats_get_attempts (MMBearerStats *self)
{
    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);

    return self->priv->attempts;
}

/**
 * mm_bearer_stats_set_attempts: (skip)
 */
void
mm_bearer_stats_set_attempts (MMBearerStats *self,
                              guint          attempts)
{
    g_return_if_fail (MM_IS_BEARER_STATS (self));

    self->priv->attempts = attempts;
}

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

/**
 * mm_bearer_stats_get_failed_attempts:
 * @self: a #MMBearerStats.
 *
 * Gets the number of failed connection attempts done with this bearer.
 *
 * Returns: a #guint.
 *
 * Since: 1.14
 */
guint
mm_bearer_stats_get_failed_attempts (MMBearerStats *self)
{
    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);

    return self->priv->failed_attempts;
}

/**
 * mm_bearer_stats_set_failed_attempts: (skip)
 */
void
mm_bearer_stats_set_failed_attempts (MMBearerStats *self,
                                     guint          failed_attempts)
{
    g_return_if_fail (MM_IS_BEARER_STATS (self));

    self->priv->failed_attempts = failed_attempts;
}

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

/**
 * mm_bearer_stats_get_total_duration:
 * @self: a #MMBearerStats.
 *
 * Gets the total duration of all the connections of this bearer.
 *
 * Returns: a #guint.
 *
 * Since: 1.14
 */
guint
mm_bearer_stats_get_total_duration (MMBearerStats *self)
{
    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);

    return self->priv->total_duration;
}

/**
 * mm_bearer_stats_set_total_duration: (skip)
 */
void
mm_bearer_stats_set_total_duration (MMBearerStats *self,
                                    guint          total_duration)
{
    g_return_if_fail (MM_IS_BEARER_STATS (self));

    self->priv->total_duration = total_duration;
}

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

/**
 * mm_bearer_stats_get_total_rx_bytes:
 * @self: a #MMBearerStats.
 *
 * Gets the total number of bytes received without error during all the
 * connections of this bearer.
 *
 * Returns: a #guint64.
 *
 * Since: 1.14
 */
guint64
mm_bearer_stats_get_total_rx_bytes (MMBearerStats *self)
{
    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);

    return self->priv->total_rx_bytes;
}

/**
 * mm_bearer_stats_set_total_rx_bytes: (skip)
 */
void
mm_bearer_stats_set_total_rx_bytes (MMBearerStats *self,
                                    guint64        total_bytes)
{
    g_return_if_fail (MM_IS_BEARER_STATS (self));

    self->priv->total_rx_bytes = total_bytes;
}

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

/**
 * mm_bearer_stats_get_total_tx_bytes:
 * @self: a #MMBearerStats.
 *
 * Gets the total number of bytes transmitted without error during all the
 * connections of this bearer.
 *
 * Returns: a #guint64.
 *
 * Since: 1.14
 */
guint64
mm_bearer_stats_get_total_tx_bytes (MMBearerStats *self)
{
    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);

    return self->priv->total_tx_bytes;
}

/**
 * mm_bearer_stats_set_total_tx_bytes: (skip)
 */
void
mm_bearer_stats_set_total_tx_bytes (MMBearerStats *self,
                                    guint64        total_bytes)
{
    g_return_if_fail (MM_IS_BEARER_STATS (self));

    self->priv->total_tx_bytes = total_bytes;
}

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

/**
 * mm_bearer_stats_get_uplink_speed:
 * @self: a #MMBearerStats.
 *
 * Gets the speed of the uplink, in bits per second.
 *
 * Returns: a #guint64.
 *
 * Since: 1.20
 */
guint64
mm_bearer_stats_get_uplink_speed (MMBearerStats *self)
{
    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);

    return self->priv->uplink_speed;
}

/**
 * mm_bearer_stats_set_uplink_speed: (skip)
 */
void
mm_bearer_stats_set_uplink_speed (MMBearerStats *self,
                                  guint64        speed)
{
    g_return_if_fail (MM_IS_BEARER_STATS (self));

    self->priv->uplink_speed = speed;
}

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

/**
 * mm_bearer_stats_get_downlink_speed:
 * @self: a #MMBearerStats.
 *
 * Gets the speed of the downlink, in bits per second.
 *
 * Returns: a #guint64.
 *
 * Since: 1.20
 */
guint64
mm_bearer_stats_get_downlink_speed (MMBearerStats *self)
{
    g_return_val_if_fail (MM_IS_BEARER_STATS (self), 0);

    return self->priv->downlink_speed;
}

/**
 * mm_bearer_stats_set_downlink_speed: (skip)
 */
void
mm_bearer_stats_set_downlink_speed (MMBearerStats *self,
                                    guint64        speed)
{
    g_return_if_fail (MM_IS_BEARER_STATS (self));

    self->priv->downlink_speed = speed;
}

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

/**
 * 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));
    g_variant_builder_add  (&builder,
                            "{sv}",
                            PROPERTY_START_DATE,
                            g_variant_new_uint64 (self->priv->start_date));
    g_variant_builder_add  (&builder,
                            "{sv}",
                            PROPERTY_ATTEMPTS,
                            g_variant_new_uint32 (self->priv->attempts));
    g_variant_builder_add  (&builder,
                            "{sv}",
                            PROPERTY_FAILED_ATTEMPTS,
                            g_variant_new_uint32 (self->priv->failed_attempts));
    g_variant_builder_add  (&builder,
                            "{sv}",
                            PROPERTY_TOTAL_DURATION,
                            g_variant_new_uint32 (self->priv->total_duration));
    g_variant_builder_add  (&builder,
                            "{sv}",
                            PROPERTY_TOTAL_RX_BYTES,
                            g_variant_new_uint64 (self->priv->total_rx_bytes));
    g_variant_builder_add  (&builder,
                            "{sv}",
                            PROPERTY_TOTAL_TX_BYTES,
                            g_variant_new_uint64 (self->priv->total_tx_bytes));
    g_variant_builder_add  (&builder,
                            "{sv}",
                            PROPERTY_UPLINK_SPEED,
                            g_variant_new_uint64 (self->priv->uplink_speed));
    g_variant_builder_add  (&builder,
                            "{sv}",
                            PROPERTY_DOWNLINK_SPEED,
                            g_variant_new_uint64 (self->priv->downlink_speed));
    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));
        } else if (g_str_equal (key, PROPERTY_START_DATE)) {
            mm_bearer_stats_set_start_date (
                self,
                g_variant_get_uint64 (value));
        } else if (g_str_equal (key, PROPERTY_ATTEMPTS)) {
            mm_bearer_stats_set_attempts (
                self,
                g_variant_get_uint32 (value));
        } else if (g_str_equal (key, PROPERTY_FAILED_ATTEMPTS)) {
            mm_bearer_stats_set_failed_attempts (
                self,
                g_variant_get_uint32 (value));
        } else if (g_str_equal (key, PROPERTY_TOTAL_DURATION)) {
            mm_bearer_stats_set_total_duration (
                self,
                g_variant_get_uint32 (value));
        } else if (g_str_equal (key, PROPERTY_TOTAL_RX_BYTES)) {
            mm_bearer_stats_set_total_rx_bytes (
                self,
                g_variant_get_uint64 (value));
        } else if (g_str_equal (key, PROPERTY_TOTAL_TX_BYTES)) {
            mm_bearer_stats_set_total_tx_bytes (
                self,
                g_variant_get_uint64 (value));
        } else if (g_str_equal (key, PROPERTY_UPLINK_SPEED)) {
            mm_bearer_stats_set_uplink_speed (
                self,
                g_variant_get_uint64 (value));
        } else if (g_str_equal (key, PROPERTY_DOWNLINK_SPEED)) {
            mm_bearer_stats_set_downlink_speed (
                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));
}