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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
|
/* -*- 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) 2008 - 2009 Novell, Inc.
* Copyright (C) 2009 - 2012 Red Hat, Inc.
* Copyright (C) 2012 Lanedo GmbH
*/
#include <stdlib.h>
#include <string.h>
#include "mm-common-sierra.h"
#include "mm-base-modem-at.h"
#include "mm-log.h"
#include "mm-modem-helpers.h"
#include "mm-sim-sierra.h"
static MMIfaceModem *iface_modem_parent;
/*****************************************************************************/
/* Custom init and port type hints */
#define TAG_SIERRA_APP_PORT "sierra-app-port"
#define TAG_SIERRA_APP1_PPP_OK "sierra-app1-ppp-ok"
gboolean
mm_common_sierra_grab_port (MMPlugin *self,
MMBaseModem *modem,
MMPortProbe *probe,
GError **error)
{
MMPortSerialAtFlag pflags = MM_PORT_SERIAL_AT_FLAG_NONE;
MMPortType ptype;
ptype = mm_port_probe_get_port_type (probe);
/* Is it a GSM secondary port? */
if (g_object_get_data (G_OBJECT (probe), TAG_SIERRA_APP_PORT)) {
if (g_object_get_data (G_OBJECT (probe), TAG_SIERRA_APP1_PPP_OK))
pflags = MM_PORT_SERIAL_AT_FLAG_PPP;
else
pflags = MM_PORT_SERIAL_AT_FLAG_SECONDARY;
} else if (ptype == MM_PORT_TYPE_AT)
pflags = MM_PORT_SERIAL_AT_FLAG_PRIMARY;
return mm_base_modem_grab_port (modem,
mm_port_probe_peek_port (probe),
ptype,
pflags,
error);
}
gboolean
mm_common_sierra_port_probe_list_is_icera (GList *probes)
{
GList *l;
for (l = probes; l; l = g_list_next (l)) {
/* Only assume the Icera probing check is valid IF the port is not
* secondary. This will skip the stupid ports which reply OK to every
* AT command, even the one we use to check for Icera support */
if (mm_port_probe_is_icera (MM_PORT_PROBE (l->data)) &&
!g_object_get_data (G_OBJECT (l->data), TAG_SIERRA_APP_PORT))
return TRUE;
}
return FALSE;
}
typedef struct {
MMPortSerialAt *port;
guint retries;
} SierraCustomInitContext;
static void
sierra_custom_init_context_free (SierraCustomInitContext *ctx)
{
g_object_unref (ctx->port);
g_slice_free (SierraCustomInitContext, ctx);
}
gboolean
mm_common_sierra_custom_init_finish (MMPortProbe *probe,
GAsyncResult *result,
GError **error)
{
return g_task_propagate_boolean (G_TASK (result), error);
}
static void sierra_custom_init_step (GTask *task);
static void
gcap_ready (MMPortSerialAt *port,
GAsyncResult *res,
GTask *task)
{
MMPortProbe *probe;
SierraCustomInitContext *ctx;
g_autofree gchar *response = NULL;
GError *error = NULL;
probe = g_task_get_source_object (task);
ctx = g_task_get_task_data (task);
response = mm_port_serial_at_command_finish (port, res, &error);
if (error) {
/* If consumed all tries and the last error was a timeout, assume the
* port is not AT */
if (ctx->retries == 0 &&
g_error_matches (error, MM_SERIAL_ERROR, MM_SERIAL_ERROR_RESPONSE_TIMEOUT)) {
mm_port_probe_set_result_at (probe, FALSE);
}
/* If reported a hard parse error, this port is definitely not an AT
* port, skip trying. */
else if (g_error_matches (error, MM_SERIAL_ERROR, MM_SERIAL_ERROR_PARSE_FAILED)) {
mm_port_probe_set_result_at (probe, FALSE);
ctx->retries = 0;
}
/* Some Icera-based devices (eg, USB305) have an AT-style port that
* replies to everything with ERROR, so tag as unsupported; sometimes
* the real AT ports do this too, so let a retry tag the port as
* supported if it responds correctly later. */
else if (g_error_matches (error, MM_MOBILE_EQUIPMENT_ERROR, MM_MOBILE_EQUIPMENT_ERROR_UNKNOWN)) {
mm_port_probe_set_result_at (probe, FALSE);
}
/* Just retry... */
sierra_custom_init_step (task);
goto out;
}
/* A valid reply to ATI tells us this is an AT port already */
mm_port_probe_set_result_at (probe, TRUE);
/* Sierra APPx ports have limited AT command parsers that just reply with
* "OK" to most commands. These can sometimes be used for PPP while the
* main port is used for status and control, but older modems tend to crash
* or fail PPP. So we allowlist modems that are known to allow PPP on the
* secondary APP ports.
*/
if (strstr (response, "APP1")) {
g_object_set_data (G_OBJECT (probe), TAG_SIERRA_APP_PORT, GUINT_TO_POINTER (TRUE));
/* PPP-on-APP1-port allowlist */
if (strstr (response, "C885") ||
strstr (response, "USB 306") ||
strstr (response, "MC8790"))
g_object_set_data (G_OBJECT (probe), TAG_SIERRA_APP1_PPP_OK, GUINT_TO_POINTER (TRUE));
/* For debugging: let users figure out if their device supports PPP
* on the APP1 port or not.
*/
if (getenv ("MM_SIERRA_APP1_PPP_OK")) {
mm_obj_dbg (probe, "APP1 PPP OK '%s'", response);
g_object_set_data (G_OBJECT (probe), TAG_SIERRA_APP1_PPP_OK, GUINT_TO_POINTER (TRUE));
}
} else if (strstr (response, "APP2") ||
strstr (response, "APP3") ||
strstr (response, "APP4")) {
/* Additional APP ports don't support most AT commands, so they cannot
* be used as the primary port.
*/
g_object_set_data (G_OBJECT (probe), TAG_SIERRA_APP_PORT, GUINT_TO_POINTER (TRUE));
}
g_task_return_boolean (task, TRUE);
g_object_unref (task);
out:
if (error)
g_error_free (error);
}
static void
sierra_custom_init_step (GTask *task)
{
MMPortProbe *probe;
SierraCustomInitContext *ctx;
GCancellable *cancellable;
probe = g_task_get_source_object (task);
ctx = g_task_get_task_data (task);
cancellable = g_task_get_cancellable (task);
/* If cancelled, end */
if (g_cancellable_is_cancelled (cancellable)) {
mm_obj_dbg (probe, "no need to keep on running custom init");
g_task_return_boolean (task, TRUE);
g_object_unref (task);
return;
}
if (ctx->retries == 0) {
mm_obj_dbg (probe, "couldn't get port type hints");
g_task_return_boolean (task, TRUE);
g_object_unref (task);
return;
}
ctx->retries--;
mm_port_serial_at_command (
ctx->port,
"ATI",
3,
FALSE, /* raw */
FALSE, /* allow_cached */
cancellable,
(GAsyncReadyCallback)gcap_ready,
task);
}
void
mm_common_sierra_custom_init (MMPortProbe *probe,
MMPortSerialAt *port,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
SierraCustomInitContext *ctx;
GTask *task;
ctx = g_slice_new (SierraCustomInitContext);
ctx->port = g_object_ref (port);
ctx->retries = 3;
task = g_task_new (probe, cancellable, callback, user_data);
g_task_set_check_cancellable (task, FALSE);
g_task_set_task_data (task, ctx, (GDestroyNotify)sierra_custom_init_context_free);
sierra_custom_init_step (task);
}
/*****************************************************************************/
/* Modem power up (Modem interface) */
gboolean
mm_common_sierra_modem_power_up_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return g_task_propagate_boolean (G_TASK (res), error);
}
static gboolean
sierra_power_up_wait_cb (GTask *task)
{
g_task_return_boolean (task, TRUE);
g_object_unref (task);
return G_SOURCE_REMOVE;
}
static void
cfun_enable_ready (MMBaseModem *self,
GAsyncResult *res,
GTask *task)
{
GError *error = NULL;
guint i;
const gchar **drivers;
gboolean is_new_sierra = FALSE;
if (!mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error)) {
g_task_return_error (task, error);
g_object_unref (task);
return;
}
/* Many Sierra devices return OK immediately in response to CFUN=1 but
* need some time to finish powering up, otherwise subsequent commands
* may return failure or even crash the modem. Give more time for older
* devices like the AC860 and C885, which aren't driven by the 'sierra_net'
* driver. Assume any DirectIP (ie, sierra_net) device is new enough
* to allow a lower timeout.
*/
drivers = mm_base_modem_get_drivers (MM_BASE_MODEM (self));
for (i = 0; drivers[i]; i++) {
if (g_str_equal (drivers[i], "sierra_net")) {
is_new_sierra = TRUE;
break;
}
}
/* The modem object will be valid in the callback as 'task' keeps a
* reference to it. */
g_timeout_add_seconds (is_new_sierra ? 5 : 10, (GSourceFunc)sierra_power_up_wait_cb, task);
}
static void
pcstate_enable_ready (MMBaseModem *self,
GAsyncResult *res,
GTask *task)
{
/* Ignore errors for now; we're not sure if all Sierra CDMA devices support
* at!pcstate.
*/
mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, NULL);
g_task_return_boolean (task, TRUE);
g_object_unref (task);
}
void
mm_common_sierra_modem_power_up (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
task = g_task_new (self, NULL, callback, user_data);
/* For CDMA modems, run !pcstate */
if (mm_iface_modem_is_cdma_only (self)) {
mm_base_modem_at_command (MM_BASE_MODEM (self),
"!pcstate=1",
5,
FALSE,
(GAsyncReadyCallback)pcstate_enable_ready,
task);
return;
}
mm_obj_warn (self, "not in full functionality status, power-up command is needed");
mm_obj_warn (self, "device may be rebooted");
/* Try to go to full functionality mode without rebooting the system.
* Works well if we previously switched off the power with CFUN=4
*/
mm_base_modem_at_command (MM_BASE_MODEM (self),
"+CFUN=1,0", /* ",0" requests no reset */
10,
FALSE,
(GAsyncReadyCallback)cfun_enable_ready,
task);
}
/*****************************************************************************/
/* Power state loading (Modem interface) */
MMModemPowerState
mm_common_sierra_load_power_state_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
GError *inner_error = NULL;
gssize value;
value = g_task_propagate_int (G_TASK (res), &inner_error);
if (inner_error) {
g_propagate_error (error, inner_error);
return MM_MODEM_POWER_STATE_UNKNOWN;
}
return (MMModemPowerState)value;
}
static void
parent_load_power_state_ready (MMIfaceModem *self,
GAsyncResult *res,
GTask *task)
{
GError *error = NULL;
MMModemPowerState state;
state = iface_modem_parent->load_power_state_finish (self, res, &error);
if (error)
g_task_return_error (task, error);
else
g_task_return_int (task, state);
g_object_unref (task);
}
static void
pcstate_query_ready (MMBaseModem *self,
GAsyncResult *res,
GTask *task)
{
const gchar *result;
guint state;
GError *error = NULL;
result = mm_base_modem_at_command_finish (MM_BASE_MODEM (self), res, &error);
if (!result) {
g_task_return_error (task, error);
g_object_unref (task);
return;
}
/* Parse power state reply */
result = mm_strip_tag (result, "!PCSTATE:");
if (!mm_get_uint_from_str (result, &state)) {
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Failed to parse !PCSTATE response '%s'",
result);
} else {
switch (state) {
case 0:
g_task_return_int (task, MM_MODEM_POWER_STATE_LOW);
break;
case 1:
g_task_return_int (task, MM_MODEM_POWER_STATE_ON);
break;
default:
g_task_return_new_error (task,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Unhandled power state: '%u'",
state);
break;
}
}
g_object_unref (task);
}
void
mm_common_sierra_load_power_state (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
GTask *task;
task = g_task_new (self, NULL, callback, user_data);
/* Check power state with AT!PCSTATE? */
if (mm_iface_modem_is_cdma_only (self)) {
mm_base_modem_at_command (MM_BASE_MODEM (self),
"!pcstate?",
3,
FALSE,
(GAsyncReadyCallback)pcstate_query_ready,
task);
return;
}
/* Otherwise run parent's */
iface_modem_parent->load_power_state (self,
(GAsyncReadyCallback)parent_load_power_state_ready,
task);
}
/*****************************************************************************/
/* Create SIM (Modem interface) */
MMBaseSim *
mm_common_sierra_create_sim_finish (MMIfaceModem *self,
GAsyncResult *res,
GError **error)
{
return mm_sim_sierra_new_finish (res, error);
}
void
mm_common_sierra_create_sim (MMIfaceModem *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
/* New Sierra SIM */
mm_sim_sierra_new (MM_BASE_MODEM (self),
NULL, /* cancellable */
callback,
user_data);
}
/*****************************************************************************/
/* Setup ports */
void
mm_common_sierra_setup_ports (MMBroadbandModem *self)
{
MMPortSerialAt *ports[2];
guint i;
g_autoptr(GRegex) pacsp_regex = NULL;
pacsp_regex = g_regex_new ("\\r\\n\\+PACSP.*\\r\\n", G_REGEX_RAW | G_REGEX_OPTIMIZE, 0, NULL);
ports[0] = mm_base_modem_peek_port_primary (MM_BASE_MODEM (self));
ports[1] = mm_base_modem_peek_port_secondary (MM_BASE_MODEM (self));
for (i = 0; i < G_N_ELEMENTS (ports); i++) {
if (!ports[i])
continue;
if (i == 1) {
/* Built-in echo removal conflicts with the APP1 port's limited AT
* parser, which doesn't always prefix responses with <CR><LF>.
*/
g_object_set (ports[i],
MM_PORT_SERIAL_AT_REMOVE_ECHO, FALSE,
NULL);
}
mm_port_serial_at_add_unsolicited_msg_handler (
ports[i],
pacsp_regex,
NULL, NULL, NULL);
}
}
/*****************************************************************************/
void
mm_common_sierra_peek_parent_interfaces (MMIfaceModem *iface)
{
iface_modem_parent = g_type_interface_peek_parent (iface);
}
|