aboutsummaryrefslogtreecommitdiff
path: root/src/mm-bearer.c
blob: 01afd3b345c4ed88f05b318126af4e7e254aa9e9 (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
/* -*- 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) 2008 - 2009 Novell, Inc.
 * Copyright (C) 2009 - 2011 Red Hat, Inc.
 * Copyright (C) 2011 Google, Inc.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

#include <ModemManager.h>

#include <mm-enums-types.h>
#include <mm-errors-types.h>
#include <mm-gdbus-bearer.h>

#include "mm-iface-modem.h"
#include "mm-bearer.h"
#include "mm-base-modem-at.h"
#include "mm-base-modem.h"
#include "mm-utils.h"
#include "mm-log.h"
#include "mm-modem-helpers.h"

G_DEFINE_TYPE (MMBearer, mm_bearer, MM_GDBUS_TYPE_BEARER_SKELETON);

enum {
    PROP_0,
    PROP_PATH,
    PROP_CONNECTION,
    PROP_MODEM,
    PROP_CAPABILITY,
    PROP_CONNECTION_APN,
    PROP_CONNECTION_IP_TYPE,
    PROP_CONNECTION_USER,
    PROP_CONNECTION_PASSWORD,
    PROP_CONNECTION_NUMBER,
    PROP_LAST
};

static GParamSpec *properties[PROP_LAST];

struct _MMBearerPrivate {
    /* The connection to the system bus */
    GDBusConnection *connection;
    /* The modem which owns this BEARER */
    MMBaseModem *modem;
    /* The path where the BEARER object is exported */
    gchar *path;
    /* Capability of this bearer */
    MMModemCapability capability;

    /* Input properties configured */
    gchar *connection_apn;
    gchar *connection_ip_type;
    gchar *connection_user;
    gchar *connection_password;
    gchar *connection_number;
};

/*****************************************************************************/
/* CONNECT */

static gboolean
handle_connect (MMBearer *self,
                GDBusMethodInvocation *invocation,
                const gchar *arg_number)
{
    return FALSE;
}

/*****************************************************************************/
/* DISCONNECT */

static gboolean
handle_disconnect (MMBearer *self,
                GDBusMethodInvocation *invocation,
                const gchar *arg_number)
{
    return FALSE;
}

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

static void
mm_bearer_export (MMBearer *self)
{
    GError *error = NULL;

    /* Handle method invocations */
    g_signal_connect (self,
                      "handle-connect",
                      G_CALLBACK (handle_connect),
                      NULL);
    g_signal_connect (self,
                      "handle-disconnect",
                      G_CALLBACK (handle_disconnect),
                      NULL);

    if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self),
                                           self->priv->connection,
                                           self->priv->path,
                                           &error)) {
        mm_warn ("couldn't export BEARER at '%s': '%s'",
                 self->priv->path,
                 error->message);
        g_error_free (error);
    }
}

static void
mm_bearer_unexport (MMBearer *self)
{
    const gchar *path;

    path = g_dbus_interface_skeleton_get_object_path (G_DBUS_INTERFACE_SKELETON (self));
    /* Only unexport if currently exported */
    if (path) {
        mm_dbg ("Removing from DBus bearer at '%s'", path);
        g_dbus_interface_skeleton_unexport (G_DBUS_INTERFACE_SKELETON (self));
    }
}

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

const gchar *
mm_bearer_get_path (MMBearer *self)
{
    return self->priv->path;
}

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

static gboolean
parse_input_properties (MMBearer *bearer,
                        MMModemCapability bearer_capability,
                        GVariant *properties,
                        GError **error)
{
    GVariantIter iter;
    gchar *key;
    gchar *value;

    g_variant_iter_init (&iter, properties);
    while (g_variant_iter_loop (&iter, "{ss}", &key, &value)) {
        gchar *previous = NULL;

        g_object_get (G_OBJECT (bearer),
                      key, &previous,
                      NULL);

        if (previous) {
            if (g_str_equal (previous, value)) {
                /* no big deal */
                g_free (previous);
                continue;
            }

            g_free (previous);
            g_set_error (error,
                         MM_CORE_ERROR,
                         MM_CORE_ERROR_INVALID_ARGS,
                         "Invalid input properties: duplicated key '%s'",
                         key);
            return FALSE;
        }

        g_object_set (G_OBJECT (bearer),
                      key, value,
                      NULL);
    }

    /* Check mandatory properties for each capability */
#define CHECK_MANDATORY_PROPERTY(NAME, PROPERTY) do     \
    {                                                   \
        gchar *value;                                   \
                                                        \
        g_object_get (G_OBJECT (bearer),                \
                      PROPERTY, &value,                 \
                      NULL);                            \
        if (!value) {                                   \
            g_set_error (error,                         \
                         MM_CORE_ERROR,                                 \
                         MM_CORE_ERROR_INVALID_ARGS,                    \
                         "Invalid input properties: %s bearer requires '%s'", \
                         NAME,                                          \
                         PROPERTY);                                     \
            return FALSE;                                               \
        }                                                               \
        g_free (value);                                                 \
    } while (0)

    /* POTS bearer? */
    if (bearer_capability & MM_MODEM_CAPABILITY_POTS) {
        CHECK_MANDATORY_PROPERTY ("POTS", MM_BEARER_CONNECTION_NUMBER);
    }
    /* CDMA bearer? */
    else if (bearer_capability & MM_MODEM_CAPABILITY_CDMA_EVDO) {
        /* No mandatory properties here */
    }
    /* 3GPP bearer? */
    else {
        CHECK_MANDATORY_PROPERTY ("3GPP", MM_BEARER_CONNECTION_APN);
    }

#undef CHECK_MANDATORY_PROPERTY

    return TRUE;
}

MMBearer *
mm_bearer_new (MMBaseModem *modem,
               GVariant *properties,
               MMModemCapability capability,
               GError **error)
{
    static guint32 id = 0;
    gchar *path;
    MMBearer *bearer;

    /* Ensure only one capability is set */
    g_assert_cmpuint (mm_count_bits_set (capability), ==, 1);

    /* Build the unique path for the Bearer, and create the object */
    path = g_strdup_printf (MM_DBUS_BEARER_PREFIX "%d", id++);
    bearer = g_object_new  (MM_TYPE_BEARER,
                            MM_BEARER_PATH,       path,
                            MM_BEARER_MODEM,      modem,
                            MM_BEARER_CAPABILITY, capability,
                            NULL);
    g_free (path);

    /* Parse and set input properties */
    if (!parse_input_properties (bearer, capability, properties, error)) {
        g_object_unref (bearer);
        return NULL;
    }

    /* Set defaults */
    mm_gdbus_bearer_set_interface (MM_GDBUS_BEARER (bearer), NULL);
    mm_gdbus_bearer_set_connected (MM_GDBUS_BEARER (bearer), FALSE);
    mm_gdbus_bearer_set_suspended (MM_GDBUS_BEARER (bearer), FALSE);
    mm_gdbus_bearer_set_ip4_config (MM_GDBUS_BEARER (bearer), NULL);
    mm_gdbus_bearer_set_ip6_config (MM_GDBUS_BEARER (bearer), NULL);

    return bearer;
}

static void
set_property (GObject *object,
              guint prop_id,
              const GValue *value,
              GParamSpec *pspec)
{
    MMBearer *self = MM_BEARER (object);

    switch (prop_id) {
    case PROP_PATH:
        g_free (self->priv->path);
        self->priv->path = g_value_dup_string (value);
        break;
    case PROP_CONNECTION:
        if (self->priv->connection)
            g_object_unref (self->priv->connection);
        self->priv->connection = g_value_dup_object (value);

        /* Export when we get a DBus connection */
        if (self->priv->connection)
            mm_bearer_export (self);
        else
            mm_bearer_unexport (self);
        break;
    case PROP_MODEM:
        if (self->priv->modem)
            g_object_unref (self->priv->modem);
        self->priv->modem = g_value_dup_object (value);
        if (self->priv->modem)
            /* Bind the modem's connection (which is set when it is exported,
             * and unset when unexported) to the BEARER's connection */
            g_object_bind_property (self->priv->modem, MM_BASE_MODEM_CONNECTION,
                                    self, MM_BEARER_CONNECTION,
                                    G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
        break;
    case PROP_CAPABILITY:
        self->priv->capability = g_value_get_flags (value);
        break;
    case PROP_CONNECTION_APN:
        g_free (self->priv->connection_apn);
        self->priv->connection_apn = g_value_dup_string (value);
        break;
    case PROP_CONNECTION_IP_TYPE:
        g_free (self->priv->connection_ip_type);
        self->priv->connection_ip_type = g_value_dup_string (value);
        break;
    case PROP_CONNECTION_USER:
        g_free (self->priv->connection_user);
        self->priv->connection_user = g_value_dup_string (value);
        break;
    case PROP_CONNECTION_PASSWORD:
        g_free (self->priv->connection_password);
        self->priv->connection_password = g_value_dup_string (value);
        break;
    case PROP_CONNECTION_NUMBER:
        g_free (self->priv->connection_number);
        self->priv->connection_number = g_value_dup_string (value);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
get_property (GObject *object,
              guint prop_id,
              GValue *value,
              GParamSpec *pspec)
{
    MMBearer *self = MM_BEARER (object);

    switch (prop_id) {
    case PROP_PATH:
        g_value_set_string (value, self->priv->path);
        break;
    case PROP_CONNECTION:
        g_value_set_object (value, self->priv->connection);
        break;
    case PROP_MODEM:
        g_value_set_object (value, self->priv->modem);
        break;
    case PROP_CAPABILITY:
        g_value_set_flags (value, self->priv->capability);
        break;
    case PROP_CONNECTION_APN:
        g_value_set_string (value, self->priv->connection_apn);
        break;
    case PROP_CONNECTION_IP_TYPE:
        g_value_set_string (value, self->priv->connection_ip_type);
        break;
    case PROP_CONNECTION_USER:
        g_value_set_string (value, self->priv->connection_user);
        break;
    case PROP_CONNECTION_PASSWORD:
        g_value_set_string (value, self->priv->connection_password);
        break;
    case PROP_CONNECTION_NUMBER:
        g_value_set_string (value, self->priv->connection_number);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
mm_bearer_init (MMBearer *self)
{
    /* Initialize private data */
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
                                              MM_TYPE_BEARER,
                                              MMBearerPrivate);
}

static void
finalize (GObject *object)
{
    MMBearer *self = MM_BEARER (object);

    g_free (self->priv->path);

    g_free (self->priv->connection_apn);
    g_free (self->priv->connection_ip_type);
    g_free (self->priv->connection_user);
    g_free (self->priv->connection_password);
    g_free (self->priv->connection_number);

    G_OBJECT_CLASS (mm_bearer_parent_class)->finalize (object);
}

static void
dispose (GObject *object)
{
    MMBearer *self = MM_BEARER (object);

    if (self->priv->connection) {
        mm_bearer_unexport (self);
        g_clear_object (&self->priv->connection);
    }

    if (self->priv->modem)
        g_clear_object (&self->priv->modem);

    G_OBJECT_CLASS (mm_bearer_parent_class)->dispose (object);
}

static void
mm_bearer_class_init (MMBearerClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (object_class, sizeof (MMBearerPrivate));

    /* Virtual methods */
    object_class->get_property = get_property;
    object_class->set_property = set_property;
    object_class->finalize = finalize;
    object_class->dispose = dispose;

    properties[PROP_CONNECTION] =
        g_param_spec_object (MM_BEARER_CONNECTION,
                             "Connection",
                             "GDBus connection to the system bus.",
                             G_TYPE_DBUS_CONNECTION,
                             G_PARAM_READWRITE);
    g_object_class_install_property (object_class, PROP_CONNECTION, properties[PROP_CONNECTION]);

    properties[PROP_PATH] =
        g_param_spec_string (MM_BEARER_PATH,
                             "Path",
                             "DBus path of the Bearer",
                             NULL,
                             G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
    g_object_class_install_property (object_class, PROP_PATH, properties[PROP_PATH]);

    properties[PROP_MODEM] =
        g_param_spec_object (MM_BEARER_MODEM,
                             "Modem",
                             "The Modem which owns this Bearer",
                             MM_TYPE_BASE_MODEM,
                             G_PARAM_READWRITE);
    g_object_class_install_property (object_class, PROP_MODEM, properties[PROP_MODEM]);

    properties[PROP_CAPABILITY] =
        g_param_spec_flags (MM_BEARER_CAPABILITY,
                            "Capability",
                            "The Capability supported by this Bearer",
                            MM_TYPE_MODEM_CAPABILITY,
                            MM_MODEM_CAPABILITY_NONE,
                            G_PARAM_READWRITE);
    g_object_class_install_property (object_class, PROP_CAPABILITY, properties[PROP_CAPABILITY]);

    properties[PROP_CONNECTION_APN] =
        g_param_spec_string (MM_BEARER_CONNECTION_APN,
                             "Connection APN",
                             "Access Point Name to use in the connection",
                             NULL,
                             G_PARAM_READWRITE);
    g_object_class_install_property (object_class, PROP_CONNECTION_APN, properties[PROP_CONNECTION_APN]);

    properties[PROP_CONNECTION_IP_TYPE] =
        g_param_spec_string (MM_BEARER_CONNECTION_IP_TYPE,
                             "Connection IP type",
                             "IP setup to use in the connection",
                             NULL,
                             G_PARAM_READWRITE);
    g_object_class_install_property (object_class, PROP_CONNECTION_IP_TYPE, properties[PROP_CONNECTION_IP_TYPE]);

    properties[PROP_CONNECTION_USER] =
        g_param_spec_string (MM_BEARER_CONNECTION_USER,
                             "Connection User",
                             "User to use in the connection",
                             NULL,
                             G_PARAM_READWRITE);
    g_object_class_install_property (object_class, PROP_CONNECTION_USER, properties[PROP_CONNECTION_USER]);

    properties[PROP_CONNECTION_PASSWORD] =
        g_param_spec_string (MM_BEARER_CONNECTION_PASSWORD,
                             "Connection Password",
                             "Password to use in the connection",
                             NULL,
                             G_PARAM_READWRITE);
    g_object_class_install_property (object_class, PROP_CONNECTION_PASSWORD, properties[PROP_CONNECTION_PASSWORD]);

    properties[PROP_CONNECTION_NUMBER] =
        g_param_spec_string (MM_BEARER_CONNECTION_NUMBER,
                             "Connection Number",
                             "Number to use in the connection",
                             NULL,
                             G_PARAM_READWRITE);
    g_object_class_install_property (object_class, PROP_CONNECTION_NUMBER, properties[PROP_CONNECTION_NUMBER]);
}