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
|
/* -*- 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 Ammonit Measurement GmbH
* Copyright (C) 2011 Google Inc.
* Author: Aleksander Morgado <aleksander@lanedo.com>
*/
#include <config.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <ctype.h>
#include "ModemManager.h"
#include "mm-modem-helpers.h"
#include "mm-serial-parsers.h"
#include "mm-log.h"
#include "mm-errors-types.h"
#include "mm-iface-modem.h"
#include "mm-base-modem-at.h"
#include "mm-broadband-modem-cinterion.h"
static void iface_modem_init (MMIfaceModem *iface);
G_DEFINE_TYPE_EXTENDED (MMBroadbandModemCinterion, mm_broadband_modem_cinterion, MM_TYPE_BROADBAND_MODEM, 0,
G_IMPLEMENT_INTERFACE (MM_TYPE_IFACE_MODEM, iface_modem_init));
struct _MMBroadbandModemCinterionPrivate {
/* Command to go into sleep mode */
gchar *sleep_mode_cmd;
/* Supported networks */
gboolean only_geran;
gboolean only_utran;
gboolean both_geran_utran;
};
/*****************************************************************************/
/* MODEM POWER UP */
static gboolean
modem_power_down_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
/* Ignore errors */
return TRUE;
}
static void
modem_power_down (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
MMBroadbandModemCinterion *cinterion = MM_BROADBAND_MODEM_CINTERION (self);
GSimpleAsyncResult *result;
if (cinterion->priv->sleep_mode_cmd)
mm_base_modem_at_command_ignore_reply (MM_BASE_MODEM (self),
cinterion->priv->sleep_mode_cmd,
5);
result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
modem_power_down);
g_simple_async_result_set_op_res_gboolean (result, TRUE);
g_simple_async_result_complete_in_idle (result);
g_object_unref (result);
}
/*****************************************************************************/
/* AFTER POWER UP */
static gboolean
modem_after_power_up_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return !!mm_base_modem_at_sequence_finish (MM_BASE_MODEM (self), res, NULL, error);
}
static gboolean
parse_supported_functionality_status (MMBroadbandModemCinterion *self,
gpointer none,
const gchar *command,
const gchar *response,
const GError *error,
GVariant **result,
GError **result_error)
{
/* We need to get which power-off command to use to put the modem in low
* power mode (with serial port open for AT commands, but with RF switched
* off). According to the documentation of various Cinterion modems, some
* support AT+CFUN=4 (HC25) and those which don't support it can use
* AT+CFUN=7 (CYCLIC SLEEP mode with 2s timeout after last character
* received in the serial port).
*
* So, just look for '4' in the reply; if not found, look for '7', and if
* not found, report warning and don't use any.
*/
g_free (self->priv->sleep_mode_cmd);
if (strstr (response, "4") != NULL) {
mm_dbg ("Device supports CFUN=4 sleep mode");
self->priv->sleep_mode_cmd = g_strdup ("+CFUN=4");
} else if (strstr (response, "7") != NULL) {
mm_dbg ("Device supports CFUN=7 sleep mode");
self->priv->sleep_mode_cmd = g_strdup ("+CFUN=7");
} else {
mm_warn ("Unknown functionality mode to go into sleep mode");
self->priv->sleep_mode_cmd = NULL;
}
/* Keep on with next command */
return FALSE;
}
static gboolean
parse_supported_networks (MMBroadbandModemCinterion *self,
gpointer none,
const gchar *command,
const gchar *response,
const GError *error,
GVariant **result,
GError **result_error)
{
/* Note: Documentation says that AT+WS46=? is replied with '+WS46:' followed
* by a list of supported network modes between parenthesis, but the EGS5
* used to test this didn't use the 'WS46:' prefix. Also, more than one
* numeric ID may appear in the list, that's why they are checked
* separately. * */
if (strstr (response, "12") != NULL) {
mm_dbg ("Device allows 2G-only network mode");
self->priv->only_geran = TRUE;
}
if (strstr (response, "22") != NULL) {
mm_dbg ("Device allows 3G-only network mode");
self->priv->only_utran = TRUE;
}
if (strstr (response, "25") != NULL) {
mm_dbg ("Device allows 2G/3G network mode");
self->priv->both_geran_utran = TRUE;
}
/* If no expected ID found, error */
if (!self->priv->only_geran &&
!self->priv->only_utran &&
!self->priv->both_geran_utran) {
mm_warn ("Invalid list of supported networks: '%s'", response);
*result_error = g_error_new (MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Invalid list of supported networks: '%s'",
response);
return FALSE;
}
/* Keep on with next command */
return FALSE;
}
static const MMBaseModemAtCommand after_power_up_commands[] = {
{ "+CFUN=?", 3, FALSE, (MMBaseModemAtResponseProcessor)parse_supported_functionality_status },
{ "+WS46=?", 3, FALSE, (MMBaseModemAtResponseProcessor)parse_supported_networks },
{ NULL }
};
static void
modem_after_power_up (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
mm_base_modem_at_sequence (
MM_BASE_MODEM (self),
after_power_up_commands,
NULL, /* response_processor_context */
NULL, /* response_processor_context_free */
NULL, /* cancellable */
callback,
user_data);
}
/*****************************************************************************/
/* FLOW CONTROL */
static gboolean
setup_flow_control_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
}
static void
setup_flow_control_ready (MMBroadbandModemCinterion *self,
GAsyncResult *res,
GSimpleAsyncResult *operation_result)
{
GError *error = NULL;
if (!mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error))
/* Let the error be critical. We DO need RTS/CTS in order to have
* proper modem disabling. */
g_simple_async_result_take_error (operation_result, error);
else
g_simple_async_result_set_op_res_gboolean (operation_result, TRUE);
g_simple_async_result_complete (operation_result);
g_object_unref (operation_result);
}
static void
setup_flow_control (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *result;
result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
setup_flow_control);
/* We need to enable RTS/CTS so that CYCLIC SLEEP mode works */
mm_base_modem_at_command (MM_BASE_MODEM (self),
"\\Q3",
3,
FALSE,
NULL, /* cancellable */
(GAsyncReadyCallback)setup_flow_control_ready,
result);
}
/*****************************************************************************/
MMBroadbandModemCinterion *
mm_broadband_modem_cinterion_new (const gchar *device,
const gchar *driver,
const gchar *plugin,
guint16 vendor_id,
guint16 product_id)
{
return g_object_new (MM_TYPE_BROADBAND_MODEM_CINTERION,
MM_BASE_MODEM_DEVICE, device,
MM_BASE_MODEM_DRIVER, driver,
MM_BASE_MODEM_PLUGIN, plugin,
MM_BASE_MODEM_VENDOR_ID, vendor_id,
MM_BASE_MODEM_PRODUCT_ID, product_id,
NULL);
}
static void
mm_broadband_modem_cinterion_init (MMBroadbandModemCinterion *self)
{
/* Initialize private data */
self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
MM_TYPE_BROADBAND_MODEM_CINTERION,
MMBroadbandModemCinterionPrivate);
}
static void
finalize (GObject *object)
{
MMBroadbandModemCinterion *self = MM_BROADBAND_MODEM_CINTERION (object);
g_free (self->priv->sleep_mode_cmd);
G_OBJECT_CLASS (mm_broadband_modem_cinterion_parent_class)->finalize (object);
}
static void
iface_modem_init (MMIfaceModem *iface)
{
iface->setup_flow_control = setup_flow_control;
iface->setup_flow_control_finish = setup_flow_control_finish;
iface->modem_after_power_up = modem_after_power_up;
iface->modem_after_power_up_finish = modem_after_power_up_finish;
iface->modem_power_down = modem_power_down;
iface->modem_power_down_finish = modem_power_down_finish;
}
static void
mm_broadband_modem_cinterion_class_init (MMBroadbandModemCinterionClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
g_type_class_add_private (object_class, sizeof (MMBroadbandModemCinterionPrivate));
/* Virtual methods */
object_class->finalize = finalize;
}
|