aboutsummaryrefslogtreecommitdiff
path: root/src/mm-modem-location.c
blob: 0018295017a63327b81047d204f8e1a9368f5db2 (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
/* -*- 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) 2010 Red Hat, Inc.
 */

#include <string.h>
#include <dbus/dbus-glib.h>

#include "mm-modem-location.h"
#include "mm-errors.h"
#include "mm-callback-info.h"
#include "mm-marshal.h"

static void impl_modem_location_enable (MMModemLocation *modem,
                                        gboolean enable,
                                        gboolean signal_location,
                                        DBusGMethodInvocation *context);

static void impl_modem_location_get_location (MMModemLocation *modem,
                                              DBusGMethodInvocation *context);

#include "mm-modem-location-glue.h"

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

static void
async_call_done (MMModem *modem, GError *error, gpointer user_data)
{
    DBusGMethodInvocation *context = (DBusGMethodInvocation *) user_data;

    if (error)
        dbus_g_method_return_error (context, error);
    else
        dbus_g_method_return (context);
}

static void
async_call_not_supported (MMModemLocation *self,
                          MMModemFn callback,
                          gpointer user_data)
{
    MMCallbackInfo *info;

    info = mm_callback_info_new (MM_MODEM (self), callback, user_data);
    info->error = g_error_new_literal (MM_MODEM_ERROR, MM_MODEM_ERROR_OPERATION_NOT_SUPPORTED,
                                       "Operation not supported");
    mm_callback_info_schedule (info);
}

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

typedef struct {
    gboolean enable;
    gboolean signals_location;
} LocAuthInfo;

static void
loc_auth_info_destroy (gpointer data)
{
    LocAuthInfo *info = data;

    memset (info, 0, sizeof (LocAuthInfo));
    g_free (info);
}

static LocAuthInfo *
loc_auth_info_new (gboolean enable, gboolean signals_location)
{
    LocAuthInfo *info;

    info = g_malloc0 (sizeof (LocAuthInfo));
    info->enable = enable;
    info->signals_location = signals_location;
    return info;
}

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

void
mm_modem_location_enable (MMModemLocation *self,
                          gboolean enable,
                          gboolean signals_location,
                          MMModemFn callback,
                          gpointer user_data)
{
    g_return_if_fail (MM_IS_MODEM_LOCATION (self));
    g_return_if_fail (callback != NULL);

    if (MM_MODEM_LOCATION_GET_INTERFACE (self)->enable)
        MM_MODEM_LOCATION_GET_INTERFACE (self)->enable (self, enable, signals_location, callback, user_data);
    else
        async_call_not_supported (self, callback, user_data);
}

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

static void
loc_enable_auth_cb (MMAuthRequest *req,
                    GObject *owner,
                    DBusGMethodInvocation *context,
                    gpointer user_data)
{
    MMModemLocation *self = MM_MODEM_LOCATION (owner);
    LocAuthInfo *info = (LocAuthInfo *) user_data;
    GError *error = NULL;

    /* Return any authorization error, otherwise enable location gathering */
    if (!mm_modem_auth_finish (MM_MODEM (self), req, &error)) {
        dbus_g_method_return_error (context, error);
        g_error_free (error);
    } else
        mm_modem_location_enable (self, info->enable, info->signals_location, async_call_done, context);
}

static void
impl_modem_location_enable (MMModemLocation *modem,
                            gboolean enable,
                            gboolean signals_location,
                            DBusGMethodInvocation *context)
{
    GError *error = NULL;
    LocAuthInfo *info;

    info = loc_auth_info_new (enable, signals_location);

    /* Make sure the caller is authorized to enable location gathering */
    if (!mm_modem_auth_request (MM_MODEM (modem),
                                MM_AUTHORIZATION_LOCATION,
                                context,
                                loc_enable_auth_cb,
                                info,
                                loc_auth_info_destroy,
                                &error)) {
        dbus_g_method_return_error (context, error);
        g_error_free (error);
    }
}

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

static void
loc_get_invoke (MMCallbackInfo *info)
{
    MMModemLocationGetFn callback = (MMModemLocationGetFn) info->callback;

    callback ((MMModemLocation *) info->modem, NULL, info->error, info->user_data);
}

static void
async_get_call_not_supported (MMModemLocation *self,
                              MMModemLocationGetFn callback,
                              gpointer user_data)
{
    MMCallbackInfo *info;

    info = mm_callback_info_new_full (MM_MODEM (self),
                                      loc_get_invoke,
                                      G_CALLBACK (callback),
                                      user_data);
    info->error = g_error_new_literal (MM_MODEM_ERROR, MM_MODEM_ERROR_OPERATION_NOT_SUPPORTED,
                                       "Operation not supported");
    mm_callback_info_schedule (info);
}

void
mm_modem_location_get_location (MMModemLocation *self,
                                MMModemLocationGetFn callback,
                                gpointer user_data)
{
    g_return_if_fail (MM_IS_MODEM_LOCATION (self));
    g_return_if_fail (callback != NULL);

    if (MM_MODEM_LOCATION_GET_INTERFACE (self)->get_location)
        MM_MODEM_LOCATION_GET_INTERFACE (self)->get_location (self, callback, user_data);
    else
        async_get_call_not_supported (self, callback, user_data);
}

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

static void
async_get_call_done (MMModemLocation *self,
                     GHashTable *locations,
                     GError *error,
                     gpointer user_data)
{
    DBusGMethodInvocation *context = (DBusGMethodInvocation *) user_data;

    if (error)
        dbus_g_method_return_error (context, error);
    else
        dbus_g_method_return (context, locations);
}

static void
loc_get_auth_cb (MMAuthRequest *req,
                 GObject *owner,
                 DBusGMethodInvocation *context,
                 gpointer user_data)
{
    MMModemLocation *self = MM_MODEM_LOCATION (owner);
    GError *error = NULL;

    /* Return any authorization error, otherwise get the location */
    if (!mm_modem_auth_finish (MM_MODEM (self), req, &error)) {
        dbus_g_method_return_error (context, error);
        g_error_free (error);
    } else
        mm_modem_location_get_location (self, async_get_call_done, context);
}

static void
impl_modem_location_get_location (MMModemLocation *modem,
                                  DBusGMethodInvocation *context)
{
    GError *error = NULL;
    LocAuthInfo *info;

    info = loc_auth_info_new (FALSE, FALSE);

    /* Make sure the caller is authorized to enable location gathering */
    if (!mm_modem_auth_request (MM_MODEM (modem),
                                MM_AUTHORIZATION_LOCATION,
                                context,
                                loc_get_auth_cb,
                                info,
                                loc_auth_info_destroy,
                                &error)) {
        dbus_g_method_return_error (context, error);
        g_error_free (error);
    }
}

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

static void
mm_modem_location_init (gpointer g_iface)
{
    static gboolean initialized = FALSE;

    if (initialized)
        return;

    /* Properties */
    g_object_interface_install_property
        (g_iface,
         g_param_spec_boxed (MM_MODEM_LOCATION_LOCATION,
                             "Location",
                             "Available location information",
                             G_TYPE_HASH_TABLE,
                             G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    g_object_interface_install_property
        (g_iface,
         g_param_spec_uint (MM_MODEM_LOCATION_CAPABILITIES,
                            "Capabilities",
                            "Supported location information methods",
                            MM_MODEM_LOCATION_CAPABILITY_UNKNOWN,
                              MM_MODEM_LOCATION_CAPABILITY_GPS_NMEA
                            | MM_MODEM_LOCATION_CAPABILITY_GSM_LAC_CI,
                            MM_MODEM_LOCATION_CAPABILITY_UNKNOWN,
                            G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    g_object_interface_install_property
        (g_iface,
         g_param_spec_boolean (MM_MODEM_LOCATION_ENABLED,
                               "Enabled",
                               "Whether or not location gathering is enabled",
                               FALSE,
                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    g_object_interface_install_property
        (g_iface,
         g_param_spec_boolean (MM_MODEM_LOCATION_SIGNALS_LOCATION,
                               "SignalsLocation",
                               "Whether or not location updates are emitted as signals",
                               FALSE,
                               G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));

    initialized = TRUE;
}

GType
mm_modem_location_get_type (void)
{
    static GType loc_type = 0;

    if (!G_UNLIKELY (loc_type)) {
        const GTypeInfo loc_info = {
            sizeof (MMModemLocation), /* class_size */
            mm_modem_location_init,   /* base_init */
            NULL,       /* base_finalize */
            NULL,
            NULL,       /* class_finalize */
            NULL,       /* class_data */
            0,
            0,              /* n_preallocs */
            NULL
        };

        loc_type = g_type_register_static (G_TYPE_INTERFACE,
                                           "MMModemLocation",
                                           &loc_info, 0);

        g_type_interface_add_prerequisite (loc_type, G_TYPE_OBJECT);
        dbus_g_object_type_install_info (loc_type, &dbus_glib_mm_modem_location_object_info);

        /* Register some shadow properties to handle Enabled and Capabilities
         * since these could be used by other interfaces.
         */
        dbus_g_object_type_register_shadow_property (loc_type,
                                                     "Enabled",
                                                     MM_MODEM_LOCATION_ENABLED);
        dbus_g_object_type_register_shadow_property (loc_type,
                                                     "Capabilities",
                                                     MM_MODEM_LOCATION_CAPABILITIES);
    }

    return loc_type;
}