aboutsummaryrefslogtreecommitdiff
path: root/libmm-glib/mm-location-cdma-bs.c
blob: 993f6bef7fa4725bbe8e8993c7bdb98951767f4a (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
/* -*- 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) 2012 Lanedo GmbH <aleksander@lanedo.com>
 */

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

#include "mm-common-helpers.h"
#include "mm-errors-types.h"
#include "mm-location-cdma-bs.h"

/**
 * SECTION: mm-location-cdma-bs
 * @title: MMLocationCdmaBs
 * @short_description: Helper object to handle CDMA Base Station location information.
 *
 * The #MMLocationCdmaBs is an object handling the location information of the
 * CDMA base station in which the modem is registered.
 *
 * This object is retrieved with either mm_modem_location_get_cdma_bs(),
 * mm_modem_location_get_cdma_bs_sync(), mm_modem_location_get_full() or
 * mm_modem_location_get_full_sync().
 */

G_DEFINE_TYPE (MMLocationCdmaBs, mm_location_cdma_bs, G_TYPE_OBJECT);

#define PROPERTY_LATITUDE  "latitude"
#define PROPERTY_LONGITUDE "longitude"

struct _MMLocationCdmaBsPrivate {
    gdouble  latitude;
    gdouble  longitude;
};

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

/**
 * mm_location_cdma_bs_get_longitude:
 * @self: a #MMLocationCdmaBs.
 *
 * Gets the longitude, in the [-180,180] range.
 *
 * Returns: the longitude, or %MM_LOCATION_LONGITUDE_UNKNOWN if unknown.
 */
gdouble
mm_location_cdma_bs_get_longitude (MMLocationCdmaBs *self)
{
    g_return_val_if_fail (MM_IS_LOCATION_CDMA_BS (self),
                          MM_LOCATION_LONGITUDE_UNKNOWN);

    return self->priv->longitude;
}

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

/**
 * mm_location_cdma_bs_get_latitude:
 * @self: a #MMLocationCdmaBs.
 *
 * Gets the latitude, in the [-90,90] range.
 *
 * Returns: the latitude, or %MM_LOCATION_LATITUDE_UNKNOWN if unknown.
 */
gdouble
mm_location_cdma_bs_get_latitude (MMLocationCdmaBs *self)
{
    g_return_val_if_fail (MM_IS_LOCATION_CDMA_BS (self),
                          MM_LOCATION_LATITUDE_UNKNOWN);

    return self->priv->latitude;
}

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

gboolean
mm_location_cdma_bs_set (MMLocationCdmaBs *self,
                         gdouble longitude,
                         gdouble latitude)
{
    g_return_val_if_fail ((longitude == MM_LOCATION_LONGITUDE_UNKNOWN ||
                           (longitude >= -180.0 && longitude <= 180.0)),
                          FALSE);
    g_return_val_if_fail ((latitude == MM_LOCATION_LATITUDE_UNKNOWN ||
                           (latitude >= -90.0 && latitude <= 90.0)),
                          FALSE);

    if (self->priv->longitude == longitude &&
        self->priv->latitude == latitude)
        return FALSE;

    self->priv->longitude = longitude;
    self->priv->latitude = latitude;
    return TRUE;
}

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

GVariant *
mm_location_cdma_bs_get_dictionary (MMLocationCdmaBs *self)
{
    GVariantBuilder builder;

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

    g_return_val_if_fail (MM_IS_LOCATION_CDMA_BS (self), NULL);

    /* If mandatory parameters are not found, return NULL */
    if (self->priv->longitude == MM_LOCATION_LONGITUDE_UNKNOWN ||
        self->priv->latitude == MM_LOCATION_LATITUDE_UNKNOWN)
        return NULL;

    g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
    g_variant_builder_add (&builder,
                           "{sv}",
                           PROPERTY_LONGITUDE,
                           g_variant_new_double (self->priv->longitude));
    g_variant_builder_add (&builder,
                           "{sv}",
                           PROPERTY_LATITUDE,
                           g_variant_new_double (self->priv->latitude));

    return g_variant_ref_sink (g_variant_builder_end (&builder));
}

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

MMLocationCdmaBs *
mm_location_cdma_bs_new_from_dictionary (GVariant *dictionary,
                                         GError **error)
{
    GError *inner_error = NULL;
    MMLocationCdmaBs *self;
    GVariantIter iter;
    gchar *key;
    GVariant *value;

    self = mm_location_cdma_bs_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 CDMA BS location from dictionary: "
                     "invalid variant type received");
        g_object_unref (self);
        return NULL;
    }

    g_variant_iter_init (&iter, dictionary);
    while (!inner_error &&
           g_variant_iter_next (&iter, "{sv}", &key, &value)) {
        if (g_str_equal (key, PROPERTY_LONGITUDE))
            self->priv->longitude = g_variant_get_double (value);
        else if (g_str_equal (key, PROPERTY_LATITUDE))
            self->priv->latitude = g_variant_get_double (value);
        g_free (key);
        g_variant_unref (value);
    }

    /* If any of the mandatory parameters is missing, cleanup */
    if (self->priv->longitude == MM_LOCATION_LONGITUDE_UNKNOWN ||
        self->priv->latitude == MM_LOCATION_LATITUDE_UNKNOWN) {
        g_set_error (error,
                     MM_CORE_ERROR,
                     MM_CORE_ERROR_INVALID_ARGS,
                     "Cannot create CDMA BS location from dictionary: "
                     "mandatory parameters missing "
                     "(longitude: %s, latitude: %s)",
                     (self->priv->longitude != MM_LOCATION_LONGITUDE_UNKNOWN) ? "yes" : "missing",
                     (self->priv->latitude != MM_LOCATION_LATITUDE_UNKNOWN) ? "yes" : "missing");
        g_clear_object (&self);
    }

    return self;
}

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

MMLocationCdmaBs *
mm_location_cdma_bs_new (void)
{
    return (MM_LOCATION_CDMA_BS (
                g_object_new (MM_TYPE_LOCATION_CDMA_BS, NULL)));
}

static void
mm_location_cdma_bs_init (MMLocationCdmaBs *self)
{
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
                                              MM_TYPE_LOCATION_CDMA_BS,
                                              MMLocationCdmaBsPrivate);

    self->priv->latitude = MM_LOCATION_LATITUDE_UNKNOWN;
    self->priv->longitude = MM_LOCATION_LONGITUDE_UNKNOWN;
}

static void
mm_location_cdma_bs_class_init (MMLocationCdmaBsClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    g_type_class_add_private (object_class, sizeof (MMLocationCdmaBsPrivate));
}