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) 2011 - Google, Inc.
*/
#include <gio/gio.h>
#include "mm-enums-types.h"
#include "mm-common-helpers.h"
gchar *
mm_common_get_capabilities_string (MMModemCapability caps)
{
GFlagsClass *flags_class;
GString *str;
str = g_string_new ("");
flags_class = G_FLAGS_CLASS (g_type_class_ref (MM_TYPE_MODEM_CAPABILITY));
if (caps == MM_MODEM_CAPABILITY_NONE) {
GFlagsValue *value;
value = g_flags_get_first_value (flags_class, caps);
g_string_append (str, value->value_nick);
} else {
MMModemCapability it;
gboolean first = TRUE;
for (it = MM_MODEM_CAPABILITY_POTS; /* first */
it <= MM_MODEM_CAPABILITY_LTE_ADVANCED; /* last */
it = it << 1) {
if (caps & it) {
GFlagsValue *value;
value = g_flags_get_first_value (flags_class, it);
g_string_append_printf (str, "%s%s",
first ? "" : ", ",
value->value_nick);
if (first)
first = FALSE;
}
}
}
g_type_class_unref (flags_class);
return g_string_free (str, FALSE);
}
gchar *
mm_common_get_access_technologies_string (MMModemAccessTechnology access_tech)
{
GFlagsClass *flags_class;
GString *str;
str = g_string_new ("");
flags_class = G_FLAGS_CLASS (g_type_class_ref (MM_TYPE_MODEM_ACCESS_TECHNOLOGY));
if (access_tech == MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN) {
GFlagsValue *value;
value = g_flags_get_first_value (flags_class, access_tech);
g_string_append (str, value->value_nick);
} else {
MMModemAccessTechnology it;
gboolean first = TRUE;
for (it = MM_MODEM_ACCESS_TECHNOLOGY_GSM; /* first */
it <= MM_MODEM_ACCESS_TECHNOLOGY_LTE; /* last */
it = it << 1) {
if (access_tech & it) {
GFlagsValue *value;
value = g_flags_get_first_value (flags_class, it);
g_string_append_printf (str, "%s%s",
first ? "" : ", ",
value->value_nick);
if (first)
first = FALSE;
}
}
}
g_type_class_unref (flags_class);
return g_string_free (str, FALSE);
}
gchar *
mm_common_get_bands_string (const MMModemBand *bands,
guint n_bands)
{
GEnumClass *enum_class;
gboolean first = TRUE;
GString *str;
guint i;
str = g_string_new ("");
if (n_bands == 0) {
g_string_append (str, "none");
return g_string_free (str, FALSE);
}
enum_class = G_ENUM_CLASS (g_type_class_ref (MM_TYPE_MODEM_BAND));
for (i = 0; i < n_bands; i++) {
GEnumValue *value;
value = g_enum_get_value (enum_class, bands[i]);
g_string_append_printf (str, "%s%s",
first ? "" : ", ",
value->value_nick);
if (first)
first = FALSE;
}
g_type_class_unref (enum_class);
return g_string_free (str, FALSE);
}
void
mm_common_get_bands_from_string (const gchar *str,
MMModemBand **bands,
guint *n_bands)
{
GArray *array;
gchar **band_strings;
GEnumClass *enum_class;
array = g_array_new (FALSE, FALSE, sizeof (MMModemBand));
enum_class = G_ENUM_CLASS (g_type_class_ref (MM_TYPE_MODEM_BAND));
band_strings = g_strsplit (str, "|", -1);
if (band_strings) {
guint i;
for (i = 0; band_strings[i]; i++) {
guint j;
gboolean found = FALSE;
for (j = 0; enum_class->values[j].value_nick; j++) {
if (g_str_equal (band_strings[i], enum_class->values[j].value_nick)) {
g_array_append_val (array, enum_class->values[j].value);
found = TRUE;
break;
}
}
if (!found)
g_warning ("Couldn't match '%s' with a valid MMModemBand value",
band_strings[i]);
}
}
if (!array->len) {
GEnumValue *value;
value = g_enum_get_value (enum_class, MM_MODEM_BAND_UNKNOWN);
g_array_append_val (array, value->value);
}
g_type_class_unref (enum_class);
g_strfreev (band_strings);
*n_bands = array->len;
*bands = (MMModemBand *)g_array_free (array, FALSE);
}
GArray *
mm_common_bands_variant_to_garray (GVariant *variant)
{
GArray *array;
GVariantIter iter;
guint32 band = MM_MODEM_BAND_UNKNOWN;
g_variant_iter_init (&iter, variant);
array = g_array_sized_new (FALSE,
FALSE,
sizeof (MMModemBand),
g_variant_iter_n_children (&iter));
while (g_variant_iter_loop (&iter, "u", &band))
g_array_append_val (array, band);
return array;
}
GVariant *
mm_common_bands_array_to_variant (const MMModemBand *bands,
guint n_bands)
{
GVariantBuilder builder;
guint i;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("au"));
for (i = 0; i < n_bands; i++) {
g_variant_builder_add_value (&builder,
g_variant_new_uint32 ((guint32)bands[i]));
}
return g_variant_builder_end (&builder);
}
GVariant *
mm_common_bands_garray_to_variant (GArray *array)
{
return mm_common_bands_array_to_variant ((const MMModemBand *)array->data,
array->len);
}
|