aboutsummaryrefslogtreecommitdiff
path: root/libmm-glib/mm-call-properties.c
blob: ff486aa5eca57cce211b54f96bf6406a1b4f6c39 (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * libmm-glib -- Access modem status & information from glib applications
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 *
 * Copyright (C) 2015 Riccardo Vangelisti <riccardo.vangelisti@sadel.it>
 */

#include <string.h>
#include <errno.h>
#include <stdlib.h>

#include "mm-errors-types.h"
#include "mm-enums-types.h"
#include "mm-flags-types.h"
#include "mm-common-helpers.h"
#include "mm-call-properties.h"

/**
 * SECTION: mm-call-properties
 * @title: MMCallProperties
 * @short_description: Helper object to handle CALL properties.
 *
 * The #MMCallProperties is an object handling the properties to be set
 * in newly created CALL objects.
 *
 * This object is created by the user and passed to ModemManager with either
 * mm_modem_voice_create_call() or mm_modem_voice_create_call_sync().
 */

G_DEFINE_TYPE (MMCallProperties, mm_call_properties, G_TYPE_OBJECT)

#define PROPERTY_NUMBER             "number"
#define PROPERTY_DTMF_TONE_DURATION "dtmf-tone-duration"

struct _MMCallPropertiesPrivate {
    gchar *number;
    guint  dtmf_tone_duration;
};

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

/**
 * mm_call_properties_set_number:
 * @self: A #MMCallProperties.
 * @text: The number to set, in UTF-8.
 *
 * Sets the call number.
 *
 * Since: 1.6
 */
void
mm_call_properties_set_number (MMCallProperties *self,
                               const gchar *number)
{
    g_return_if_fail (MM_IS_CALL_PROPERTIES (self));

    g_free (self->priv->number);
    self->priv->number = g_strdup (number);
}

/**
 * mm_call_properties_get_number:
 * @self: A #MMCallProperties.
 *
 * Gets the number, in UTF-8.
 *
 * Returns: the call number, or %NULL if it doesn't contain any (anonymous
 * caller). Do not free the returned value, it is owned by @self.
 *
 * Since: 1.6
 */
const gchar *
mm_call_properties_get_number (MMCallProperties *self)
{
    g_return_val_if_fail (MM_IS_CALL_PROPERTIES (self), NULL);

    return self->priv->number;
}

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

/**
 * mm_call_properties_set_dtmf_tone_duration:
 * @self: A #MMCallProperties.
 * @duration_ms: The desired duration of DTMF tones in milliseconds.
 *
 * Sets the DTMF tone duration if supported by the modem.
 *
 * Since: 1.26
 */
void
mm_call_properties_set_dtmf_tone_duration (MMCallProperties *self,
                                           const guint duration_ms)
{
    g_return_if_fail (MM_IS_CALL_PROPERTIES (self));

    self->priv->dtmf_tone_duration = duration_ms;
}

/**
 * mm_call_properties_get_dtmf_tone_duration:
 * @self: A #MMCallProperties.
 *
 * Gets the desired DTMF tone duration in milliseconds.
 *
 * Returns: the DTMF tone duration in milliseconds.
 *
 * Since: 1.26
 */
guint
mm_call_properties_get_dtmf_tone_duration (MMCallProperties *self)
{
    g_return_val_if_fail (MM_IS_CALL_PROPERTIES (self), 0);

    return self->priv->dtmf_tone_duration;
}

/*****************************************************************************/
/*
 * mm_call_properties_get_dictionary: (skip)
 */
GVariant *
mm_call_properties_get_dictionary (MMCallProperties *self)
{
    GVariantBuilder builder;

    /* We do allow NULL */
    if (!self)
        return NULL;

    g_return_val_if_fail (MM_IS_CALL_PROPERTIES (self), NULL);

    g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));

    if (self->priv->number)
        g_variant_builder_add (&builder,
                               "{sv}",
                               PROPERTY_NUMBER,
                               g_variant_new_string (self->priv->number));

    if (self->priv->dtmf_tone_duration)
        g_variant_builder_add (&builder,
                               "{sv}",
                               PROPERTY_DTMF_TONE_DURATION,
                               g_variant_new_uint32 (self->priv->dtmf_tone_duration));

    return g_variant_ref_sink (g_variant_builder_end (&builder));
}

/*****************************************************************************/
static gboolean
consume_string (MMCallProperties *self,
                const gchar *key,
                const gchar *value,
                GError **error)
{
    if (g_str_equal (key, PROPERTY_NUMBER)) {
        mm_call_properties_set_number (self, value);
    } else if (g_str_equal (key, PROPERTY_DTMF_TONE_DURATION)) {
        guint num;

        if (!mm_get_uint_from_str (value, &num)) {
            g_set_error (error,
                         MM_CORE_ERROR,
                         MM_CORE_ERROR_INVALID_ARGS,
                         "Failed to parse DTMF tone duration from value '%s'",
                         value);
            return FALSE;
        }
        mm_call_properties_set_dtmf_tone_duration (self, num);
    } else {
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_INVALID_ARGS,
                     "Invalid properties string, unexpected key '%s'",
                     key);
        return FALSE;
    }

    return TRUE;
}

typedef struct {
    MMCallProperties *properties;
    GError *error;
} ParseKeyValueContext;

static gboolean
key_value_foreach (const gchar *key,
                   const gchar *value,
                   ParseKeyValueContext *ctx)
{
    return consume_string (ctx->properties,
                           key,
                           value,
                           &ctx->error);
}

/*
 * mm_call_properties_new_from_string: (skip)
 */
MMCallProperties *
mm_call_properties_new_from_string (const gchar *str,
                                    GError **error)
{
    ParseKeyValueContext ctx;

    ctx.properties = mm_call_properties_new ();
    ctx.error = NULL;

    mm_common_parse_key_value_string (str,
                                      &ctx.error,
                                      (MMParseKeyValueForeachFn)key_value_foreach,
                                      &ctx);

    /* If error, destroy the object */
    if (ctx.error) {
        g_propagate_error (error, ctx.error);
        g_object_unref (ctx.properties);
        ctx.properties = NULL;
    }

    return ctx.properties;
}

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

static gboolean
consume_variant (MMCallProperties *properties,
                 const gchar *key,
                 GVariant *value,
                 GError **error)
{
    if (g_str_equal (key, PROPERTY_NUMBER)) {
        if (!g_variant_is_of_type (value, G_VARIANT_TYPE_STRING)) {
            g_set_error_literal (error,
                                 MM_CORE_ERROR,
                                 MM_CORE_ERROR_INVALID_ARGS,
                                 "Invalid properties dictionary; number not a string");
            return FALSE;
        }
        mm_call_properties_set_number (
            properties,
            g_variant_get_string (value, NULL));
    } else if (g_str_equal (key, PROPERTY_DTMF_TONE_DURATION)) {
        if (!g_variant_is_of_type (value, G_VARIANT_TYPE_UINT32)) {
            g_set_error_literal (error,
                                 MM_CORE_ERROR,
                                 MM_CORE_ERROR_INVALID_ARGS,
                                 "Invalid properties dictionary; dtmf-tone-duration not a uint32");
            return FALSE;
        }
        mm_call_properties_set_dtmf_tone_duration (
            properties,
            g_variant_get_uint32 (value));
    } else {
        /* Set error */
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_INVALID_ARGS,
                     "Invalid properties dictionary, unexpected key '%s'",
                     key);
        return FALSE;
    }

    return TRUE;
}

/*
 * mm_call_properties_new_from_dictionary: (skip)
 */
MMCallProperties *
mm_call_properties_new_from_dictionary (GVariant  *dictionary,
                                        GError   **error)
{
    GError *inner_error = NULL;
    GVariantIter iter;
    gchar *key;
    GVariant *value;
    MMCallProperties *properties;

    properties = mm_call_properties_new ();
    if (!dictionary)
        return properties;

    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 call properties from dictionary: "
                     "invalid variant type received");
        g_object_unref (properties);
        return NULL;
    }

    g_variant_iter_init (&iter, dictionary);
    while (!inner_error &&
           g_variant_iter_next (&iter, "{sv}", &key, &value)) {
        consume_variant (properties,
                         key,
                         value,
                         &inner_error);
        g_free (key);
        g_variant_unref (value);
    }

    /* If error, destroy the object */
    if (inner_error) {
        g_propagate_error (error, inner_error);
        g_object_unref (properties);
        properties = NULL;
    }

    return properties;
}

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

/**
 * mm_call_properties_new:
 *
 * Creates a new empty #MMCallProperties.
 *
 * Returns: (transfer full): a #MMCallProperties. The returned value should be
 * freed with g_object_unref().
 *
 * Since: 1.6
 */
MMCallProperties *
mm_call_properties_new (void)
{
    return (MM_CALL_PROPERTIES (g_object_new (MM_TYPE_CALL_PROPERTIES, NULL)));
}

static void
mm_call_properties_init (MMCallProperties *self)
{
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
                                              MM_TYPE_CALL_PROPERTIES,
                                              MMCallPropertiesPrivate);
}

static void
finalize (GObject *object)
{
    MMCallProperties *self = MM_CALL_PROPERTIES (object);

    g_free (self->priv->number);

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

static void
mm_call_properties_class_init (MMCallPropertiesClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (object_class, sizeof (MMCallPropertiesPrivate));

    object_class->finalize = finalize;
}