aboutsummaryrefslogtreecommitdiff
path: root/src/mm-port.c
blob: b2018fe2793bc431d69adf22c2ced7c8adee55e7 (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
/* -*- 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) 2009 Red Hat, Inc.
 */

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

#include "mm-port.h"
#include "mm-options.h"

G_DEFINE_TYPE (MMPort, mm_port, G_TYPE_OBJECT)

enum {
    PROP_0,
    PROP_DEVICE,
    PROP_SUBSYS,
    PROP_TYPE,
    PROP_CARRIER_DETECT,
    PROP_CONNECTED,

    LAST_PROP
};

#define MM_PORT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), MM_TYPE_PORT, MMPortPrivate))

typedef struct {
    char *device;
    MMPortSubsys subsys;
    MMPortType ptype;
    gboolean carrier_detect;
    gboolean connected;
} MMPortPrivate;

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

const char *
mm_port_type_to_name (MMPortType ptype)
{
    switch (ptype) {
    case MM_PORT_TYPE_PRIMARY:
        return "primary";
    case MM_PORT_TYPE_SECONDARY:
        return "secondary";
    case MM_PORT_TYPE_IGNORED:
        return "ignored";
    case MM_PORT_TYPE_QCDM:
        return "QCDM";
    default:
        break;
    }
    return "(unknown)";
}

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

static GObject*
constructor (GType type,
             guint n_construct_params,
             GObjectConstructParam *construct_params)
{
    GObject *object;
    MMPortPrivate *priv;

    object = G_OBJECT_CLASS (mm_port_parent_class)->constructor (type,
                                                                 n_construct_params,
                                                                 construct_params);
    if (!object)
        return NULL;

    priv = MM_PORT_GET_PRIVATE (object);

    if (!priv->device) {
        g_warning ("MMPort: no device provided");
        g_object_unref (object);
        return NULL;
    }

    if (priv->subsys == MM_PORT_SUBSYS_UNKNOWN) {
        g_warning ("MMPort: invalid port subsystem");
        g_object_unref (object);
        return NULL;
    }

    if (priv->ptype == MM_PORT_TYPE_UNKNOWN) {
        g_warning ("MMPort: invalid port type");
        g_object_unref (object);
        return NULL;
    }

    return object;
}


const char *
mm_port_get_device (MMPort *self)
{
    g_return_val_if_fail (self != NULL, NULL);
    g_return_val_if_fail (MM_IS_PORT (self), NULL);

    return MM_PORT_GET_PRIVATE (self)->device;
}

MMPortSubsys
mm_port_get_subsys (MMPort *self)
{
    g_return_val_if_fail (self != NULL, MM_PORT_SUBSYS_UNKNOWN);
    g_return_val_if_fail (MM_IS_PORT (self), MM_PORT_SUBSYS_UNKNOWN);

    return MM_PORT_GET_PRIVATE (self)->subsys;
}

MMPortType
mm_port_get_port_type (MMPort *self)
{
    g_return_val_if_fail (self != NULL, MM_PORT_TYPE_UNKNOWN);
    g_return_val_if_fail (MM_IS_PORT (self), MM_PORT_TYPE_UNKNOWN);

    return MM_PORT_GET_PRIVATE (self)->ptype;
}

gboolean
mm_port_get_carrier_detect (MMPort *self)
{
    g_return_val_if_fail (self != NULL, MM_PORT_TYPE_UNKNOWN);
    g_return_val_if_fail (MM_IS_PORT (self), MM_PORT_TYPE_UNKNOWN);

    return MM_PORT_GET_PRIVATE (self)->carrier_detect;
}

gboolean
mm_port_get_connected (MMPort *self)
{
    g_return_val_if_fail (self != NULL, FALSE);
    g_return_val_if_fail (MM_IS_PORT (self), FALSE);

    return MM_PORT_GET_PRIVATE (self)->connected;
}

void
mm_port_set_connected (MMPort *self, gboolean connected)
{
    MMPortPrivate *priv;

    g_return_if_fail (self != NULL);
    g_return_if_fail (MM_IS_PORT (self));

    priv = MM_PORT_GET_PRIVATE (self);
    if (priv->connected != connected) {
        priv->connected = connected;
        g_object_notify (G_OBJECT (self), MM_PORT_CONNECTED);
        if (mm_options_debug()) {
            GTimeVal tv;

            g_get_current_time (&tv);
            g_debug ("<%ld.%ld> (%s): port now %s",
                     tv.tv_sec,
                     tv.tv_usec,
                     priv->device,
                     connected ? "connected" : "disconnected");
        }
    }
}

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

static void
mm_port_init (MMPort *self)
{
}

static void
set_property (GObject *object, guint prop_id,
              const GValue *value, GParamSpec *pspec)
{
    MMPortPrivate *priv = MM_PORT_GET_PRIVATE (object);

    switch (prop_id) {
    case PROP_DEVICE:
        /* Construct only */
        priv->device = g_value_dup_string (value);
        break;
    case PROP_SUBSYS:
        /* Construct only */
        priv->subsys = g_value_get_uint (value);
        break;
    case PROP_TYPE:
        /* Construct only */
        priv->ptype = g_value_get_uint (value);
        break;
    case PROP_CARRIER_DETECT:
        priv->carrier_detect = g_value_get_boolean (value);
        break;
    case PROP_CONNECTED:
        priv->connected = g_value_get_boolean (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)
{
    MMPortPrivate *priv = MM_PORT_GET_PRIVATE (object);

    switch (prop_id) {
    case PROP_DEVICE:
        g_value_set_string (value, priv->device);
        break;
    case PROP_SUBSYS:
        g_value_set_uint (value, priv->subsys);
        break;
    case PROP_TYPE:
        g_value_set_uint (value, priv->ptype);
        break;
    case PROP_CARRIER_DETECT:
        g_value_set_boolean (value, priv->carrier_detect);
        break;
    case PROP_CONNECTED:
        g_value_set_boolean (value, priv->connected);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
finalize (GObject *object)
{
    MMPortPrivate *priv = MM_PORT_GET_PRIVATE (object);

    g_free (priv->device);

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

static void
mm_port_class_init (MMPortClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (object_class, sizeof (MMPortPrivate));

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

    g_object_class_install_property
        (object_class, PROP_DEVICE,
         g_param_spec_string (MM_PORT_DEVICE,
                              "Device",
                              "Device",
                              NULL,
                              G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    g_object_class_install_property
        (object_class, PROP_SUBSYS,
         g_param_spec_uint (MM_PORT_SUBSYS,
                            "Subsystem",
                            "Subsystem",
                            MM_PORT_SUBSYS_UNKNOWN,
                            MM_PORT_SUBSYS_LAST,
                            MM_PORT_SUBSYS_UNKNOWN,
                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    g_object_class_install_property
        (object_class, PROP_TYPE,
         g_param_spec_uint (MM_PORT_TYPE,
                            "Type",
                            "Type",
                            MM_PORT_TYPE_UNKNOWN,
                            MM_PORT_TYPE_LAST,
                            MM_PORT_TYPE_UNKNOWN,
                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    g_object_class_install_property
        (object_class, PROP_CARRIER_DETECT,
         g_param_spec_boolean (MM_PORT_CARRIER_DETECT,
                               "Carrier Detect",
                               "Has Carrier Detect",
                               TRUE,
                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

    g_object_class_install_property
        (object_class, PROP_CONNECTED,
         g_param_spec_boolean (MM_PORT_CONNECTED,
                               "Connected",
                               "Is connected for data and not usable for control",
                               FALSE,
                               G_PARAM_READWRITE));
}