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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
|
/* -*- 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 - 2012 Google, Inc.
*/
#include <stdlib.h>
#include <string.h>
#include <ModemManager.h>
#define _LIBMM_INSIDE_MM
#include <libmm-glib.h>
#include "mm-iface-modem.h"
#include "mm-iface-modem-location.h"
#include "mm-iface-modem-3gpp.h"
#include "mm-base-modem.h"
#include "mm-modem-helpers.h"
#include "mm-error-helpers.h"
#include "mm-log.h"
#define REGISTRATION_CHECK_TIMEOUT_SEC 30
#define DEFERRED_REGISTRATION_STATE_UPDATE_TIMEOUT_SEC 15
#define SUBSYSTEM_3GPP "3gpp"
#define REGISTRATION_STATE_CONTEXT_TAG "3gpp-registration-state-context-tag"
#define REGISTRATION_CHECK_CONTEXT_TAG "3gpp-registration-check-context-tag"
static GQuark registration_state_context_quark;
static GQuark registration_check_context_quark;
/*****************************************************************************/
void
mm_iface_modem_3gpp_bind_simple_status (MMIfaceModem3gpp *self,
MMSimpleStatus *status)
{
MmGdbusModem3gpp *skeleton;
g_object_get (self,
MM_IFACE_MODEM_3GPP_DBUS_SKELETON, &skeleton,
NULL);
if (!skeleton)
return;
g_object_bind_property (skeleton, "registration-state",
status, MM_SIMPLE_PROPERTY_3GPP_REGISTRATION_STATE,
G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
g_object_bind_property (skeleton, "operator-code",
status, MM_SIMPLE_PROPERTY_3GPP_OPERATOR_CODE,
G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
g_object_bind_property (skeleton, "operator-name",
status, MM_SIMPLE_PROPERTY_3GPP_OPERATOR_NAME,
G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
g_object_unref (skeleton);
}
/*****************************************************************************/
typedef struct {
MMModem3gppRegistrationState cs;
MMModem3gppRegistrationState ps;
MMModem3gppRegistrationState eps;
MMModem3gppRegistrationState deferred_new_state;
guint deferred_update_id;
gboolean manual_registration;
GCancellable *pending_registration_cancellable;
gboolean reloading_operator;
} RegistrationStateContext;
static void
registration_state_context_free (RegistrationStateContext *ctx)
{
if (ctx->pending_registration_cancellable) {
g_cancellable_cancel (ctx->pending_registration_cancellable);
g_object_unref (ctx->pending_registration_cancellable);
}
if (ctx->deferred_update_id) {
g_source_remove (ctx->deferred_update_id);
}
g_slice_free (RegistrationStateContext, ctx);
}
static RegistrationStateContext *
get_registration_state_context (MMIfaceModem3gpp *self)
{
RegistrationStateContext *ctx;
if (G_UNLIKELY (!registration_state_context_quark))
registration_state_context_quark = (g_quark_from_static_string (
REGISTRATION_STATE_CONTEXT_TAG));
ctx = g_object_get_qdata (G_OBJECT (self), registration_state_context_quark);
if (!ctx) {
/* Create context and keep it as object data */
ctx = g_slice_new0 (RegistrationStateContext);
ctx->cs = MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN;
ctx->ps = MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN;
ctx->eps = MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN;
ctx->deferred_new_state = MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN;
g_object_set_qdata_full (
G_OBJECT (self),
registration_state_context_quark,
ctx,
(GDestroyNotify)registration_state_context_free);
}
return ctx;
}
static MMModem3gppRegistrationState
get_consolidated_reg_state (RegistrationStateContext *ctx)
{
/* Some devices (Blackberries for example) will respond to +CGREG, but
* return ERROR for +CREG, probably because their firmware is just stupid.
* So here we prefer the +CREG response, but if we never got a successful
* +CREG response, we'll take +CGREG instead.
*/
if (ctx->cs == MM_MODEM_3GPP_REGISTRATION_STATE_HOME ||
ctx->cs == MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING)
return ctx->cs;
if (ctx->ps == MM_MODEM_3GPP_REGISTRATION_STATE_HOME ||
ctx->ps == MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING)
return ctx->ps;
if (ctx->eps == MM_MODEM_3GPP_REGISTRATION_STATE_HOME ||
ctx->eps == MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING)
return ctx->eps;
if (ctx->cs == MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING)
return ctx->cs;
if (ctx->ps == MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING)
return ctx->ps;
if (ctx->eps == MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING)
return ctx->eps;
return ctx->cs;
}
/*****************************************************************************/
typedef struct {
MMIfaceModem3gpp *self;
MmGdbusModem3gpp *skeleton;
GSimpleAsyncResult *result;
GCancellable *cancellable;
gchar *operator_id;
GTimer *timer;
guint max_registration_time;
} RegisterInNetworkContext;
static void
register_in_network_context_complete_and_free (RegisterInNetworkContext *ctx)
{
g_simple_async_result_complete_in_idle (ctx->result);
g_object_unref (ctx->result);
if (ctx->timer)
g_timer_destroy (ctx->timer);
if (ctx->cancellable) {
RegistrationStateContext *registration_state_context;
/* Clear our cancellable if still around */
registration_state_context = get_registration_state_context (ctx->self);
if (registration_state_context->pending_registration_cancellable == ctx->cancellable)
g_clear_object (®istration_state_context->pending_registration_cancellable);
g_object_unref (ctx->cancellable);
}
g_free (ctx->operator_id);
if (ctx->skeleton)
g_object_unref (ctx->skeleton);
g_object_unref (ctx->self);
g_slice_free (RegisterInNetworkContext, ctx);
}
static void
register_in_network_context_failed (RegisterInNetworkContext *ctx,
GError *error)
{
mm_iface_modem_3gpp_update_cs_registration_state (ctx->self, MM_MODEM_3GPP_REGISTRATION_STATE_IDLE);
mm_iface_modem_3gpp_update_ps_registration_state (ctx->self, MM_MODEM_3GPP_REGISTRATION_STATE_IDLE);
mm_iface_modem_3gpp_update_eps_registration_state (ctx->self, MM_MODEM_3GPP_REGISTRATION_STATE_IDLE);
mm_iface_modem_3gpp_update_access_technologies (ctx->self, MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN);
mm_iface_modem_3gpp_update_location (ctx->self, 0, 0);
g_simple_async_result_take_error (ctx->result, error);
}
gboolean
mm_iface_modem_3gpp_register_in_network_finish (MMIfaceModem3gpp *self,
GAsyncResult *res,
GError **error)
{
return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
}
static void run_registration_checks_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
RegisterInNetworkContext *ctx);
static gboolean
run_registration_checks_again (RegisterInNetworkContext *ctx)
{
/* Get fresh registration state */
mm_iface_modem_3gpp_run_registration_checks (
ctx->self,
(GAsyncReadyCallback)run_registration_checks_ready,
ctx);
return FALSE;
}
static void
run_registration_checks_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
RegisterInNetworkContext *ctx)
{
GError *error = NULL;
RegistrationStateContext *registration_state_context;
MMModem3gppRegistrationState current_registration_state;
mm_iface_modem_3gpp_run_registration_checks_finish (MM_IFACE_MODEM_3GPP (self), res, &error);
if (error) {
mm_dbg ("3GPP registration check failed: '%s'", error->message);
register_in_network_context_failed (ctx, error);
register_in_network_context_complete_and_free (ctx);
return;
}
registration_state_context = get_registration_state_context (ctx->self);
current_registration_state = get_consolidated_reg_state (registration_state_context);
/* If we got a final state and it's denied, we can assume the registration is
* finished */
if (current_registration_state == MM_MODEM_3GPP_REGISTRATION_STATE_DENIED) {
mm_dbg ("Registration denied");
register_in_network_context_failed (
ctx,
mm_mobile_equipment_error_for_code (MM_MOBILE_EQUIPMENT_ERROR_NETWORK_NOT_ALLOWED));
register_in_network_context_complete_and_free (ctx);
return;
}
/* If we got registered, end registration checks */
if (current_registration_state == MM_MODEM_3GPP_REGISTRATION_STATE_HOME ||
current_registration_state == MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING) {
mm_dbg ("Modem is currently registered in a 3GPP network");
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
register_in_network_context_complete_and_free (ctx);
return;
}
/* Don't spend too much time waiting to get registered */
if (g_timer_elapsed (ctx->timer, NULL) > ctx->max_registration_time) {
mm_dbg ("3GPP registration check timed out");
register_in_network_context_failed (
ctx,
mm_mobile_equipment_error_for_code (MM_MOBILE_EQUIPMENT_ERROR_NETWORK_TIMEOUT));
register_in_network_context_complete_and_free (ctx);
return;
}
/* If we're still waiting for automatic registration to complete or
* fail, check again in a few seconds.
*
* This 3s timeout will catch results from automatic registrations as
* well.
*/
mm_dbg ("Modem not yet registered in a 3GPP network... will recheck soon");
g_timeout_add_seconds (3, (GSourceFunc)run_registration_checks_again, ctx);
}
static void
register_in_network_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
RegisterInNetworkContext *ctx)
{
GError *error = NULL;
if (!MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->register_in_network_finish (self, res, &error)) {
/* Propagate error when trying to lock to network */
register_in_network_context_failed (ctx, error);
register_in_network_context_complete_and_free (ctx);
return;
}
/* Now try to gather current registration status until we're registered or
* the time goes off */
mm_iface_modem_3gpp_run_registration_checks (
self,
(GAsyncReadyCallback)run_registration_checks_ready,
ctx);
}
void
mm_iface_modem_3gpp_register_in_network (MMIfaceModem3gpp *self,
const gchar *operator_id,
guint max_registration_time,
GAsyncReadyCallback callback,
gpointer user_data)
{
RegisterInNetworkContext *ctx;
const gchar *current_operator_code;
RegistrationStateContext *registration_state_context;
GError *error = NULL;
ctx = g_slice_new0 (RegisterInNetworkContext);
ctx->self = g_object_ref (self);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
mm_iface_modem_3gpp_register_in_network);
ctx->operator_id = (operator_id && operator_id[0]) ? g_strdup (operator_id) : NULL;
ctx->max_registration_time = max_registration_time;
g_object_get (self,
MM_IFACE_MODEM_3GPP_DBUS_SKELETON, &ctx->skeleton,
NULL);
if (!ctx->skeleton) {
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't get interface skeleton");
register_in_network_context_complete_and_free (ctx);
return;
}
/* Validate input MCC/MNC */
if (ctx->operator_id && !mm_3gpp_parse_operator_id (ctx->operator_id, NULL, NULL, &error)) {
g_assert (error != NULL);
g_simple_async_result_take_error (ctx->result, error);
register_in_network_context_complete_and_free (ctx);
return;
}
/* Get registration state context */
registration_state_context = get_registration_state_context (self);
/* (Try to) cancel previous registration request */
if (registration_state_context->pending_registration_cancellable) {
g_cancellable_cancel (registration_state_context->pending_registration_cancellable);
g_clear_object (®istration_state_context->pending_registration_cancellable);
}
current_operator_code = mm_gdbus_modem3gpp_get_operator_code (ctx->skeleton);
/* Manual registration requested? */
if (ctx->operator_id) {
/* If already registered with the requested operator, we're done */
if (current_operator_code &&
g_str_equal (current_operator_code, ctx->operator_id)) {
registration_state_context->manual_registration = TRUE;
mm_dbg ("Already registered in selected network '%s'...",
current_operator_code);
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
register_in_network_context_complete_and_free (ctx);
return;
}
/* Manual registration to a new operator required */
mm_dbg ("Launching manual network registration (%s)...",
ctx->operator_id);
registration_state_context->manual_registration = TRUE;
}
/* Automatic registration requested? */
else {
/* If the modem is already registered and the last time it was asked
* automatic registration, we're done */
if (current_operator_code &&
!registration_state_context->manual_registration) {
mm_dbg ("Already registered in network '%s',"
" automatic registration not launched...",
current_operator_code);
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
register_in_network_context_complete_and_free (ctx);
return;
}
/* Automatic registration to a new operator requested */
mm_dbg ("Launching automatic network registration...");
registration_state_context->manual_registration = FALSE;
}
ctx->cancellable = g_cancellable_new ();
/* Keep an accessible reference to the cancellable, so that we can cancel
* previous request when needed */
registration_state_context->pending_registration_cancellable =
g_object_ref (ctx->cancellable);
ctx->timer = g_timer_new ();
MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->register_in_network (
self,
ctx->operator_id,
ctx->cancellable,
(GAsyncReadyCallback)register_in_network_ready,
ctx);
}
typedef struct {
MmGdbusModem3gpp *skeleton;
GDBusMethodInvocation *invocation;
MMIfaceModem3gpp *self;
gchar *operator_id;
} HandleRegisterContext;
static void
handle_register_context_free (HandleRegisterContext *ctx)
{
g_object_unref (ctx->skeleton);
g_object_unref (ctx->invocation);
g_object_unref (ctx->self);
g_free (ctx->operator_id);
g_free (ctx);
}
static void
handle_register_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
HandleRegisterContext *ctx)
{
GError *error = NULL;
if (!MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->register_in_network_finish (self, res,&error))
g_dbus_method_invocation_take_error (ctx->invocation, error);
else
mm_gdbus_modem3gpp_complete_register (ctx->skeleton, ctx->invocation);
handle_register_context_free (ctx);
}
static void
handle_register_auth_ready (MMBaseModem *self,
GAsyncResult *res,
HandleRegisterContext *ctx)
{
MMModemState modem_state;
GError *error = NULL;
if (!mm_base_modem_authorize_finish (self, res, &error)) {
g_dbus_method_invocation_take_error (ctx->invocation, error);
handle_register_context_free (ctx);
return;
}
g_assert (MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->register_in_network != NULL);
g_assert (MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->register_in_network_finish != NULL);
modem_state = MM_MODEM_STATE_UNKNOWN;
g_object_get (self,
MM_IFACE_MODEM_STATE, &modem_state,
NULL);
switch (modem_state) {
case MM_MODEM_STATE_FAILED:
case MM_MODEM_STATE_UNKNOWN:
case MM_MODEM_STATE_LOCKED:
/* We should never have such request (interface wasn't exported yet) */
g_assert_not_reached ();
break;
case MM_MODEM_STATE_INITIALIZING:
g_dbus_method_invocation_return_error (ctx->invocation,
MM_CORE_ERROR,
MM_CORE_ERROR_WRONG_STATE,
"Cannot register modem: "
"device not fully initialized yet");
break;
case MM_MODEM_STATE_ENABLED:
case MM_MODEM_STATE_SEARCHING:
case MM_MODEM_STATE_REGISTERED:
mm_iface_modem_3gpp_register_in_network (MM_IFACE_MODEM_3GPP (self),
ctx->operator_id,
60,
(GAsyncReadyCallback)handle_register_ready,
ctx);
return;
case MM_MODEM_STATE_DISABLING:
g_dbus_method_invocation_return_error (ctx->invocation,
MM_CORE_ERROR,
MM_CORE_ERROR_WRONG_STATE,
"Cannot register modem: "
"currently being disabled");
break;
case MM_MODEM_STATE_ENABLING:
case MM_MODEM_STATE_DISABLED:
g_dbus_method_invocation_return_error (ctx->invocation,
MM_CORE_ERROR,
MM_CORE_ERROR_WRONG_STATE,
"Cannot register modem: "
"not yet enabled");
break;
case MM_MODEM_STATE_DISCONNECTING:
case MM_MODEM_STATE_CONNECTING:
case MM_MODEM_STATE_CONNECTED:
g_dbus_method_invocation_return_error (ctx->invocation,
MM_CORE_ERROR,
MM_CORE_ERROR_WRONG_STATE,
"Cannot register modem: "
"modem is connected");
break;
}
handle_register_context_free (ctx);
}
static gboolean
handle_register (MmGdbusModem3gpp *skeleton,
GDBusMethodInvocation *invocation,
const gchar *operator_id,
MMIfaceModem3gpp *self)
{
HandleRegisterContext *ctx;
ctx = g_new (HandleRegisterContext, 1);
ctx->skeleton = g_object_ref (skeleton);
ctx->invocation = g_object_ref (invocation);
ctx->self = g_object_ref (self);
ctx->operator_id = g_strdup (operator_id);
mm_base_modem_authorize (MM_BASE_MODEM (self),
invocation,
MM_AUTHORIZATION_DEVICE_CONTROL,
(GAsyncReadyCallback)handle_register_auth_ready,
ctx);
return TRUE;
}
/*****************************************************************************/
typedef struct {
MmGdbusModem3gpp *skeleton;
GDBusMethodInvocation *invocation;
MMIfaceModem3gpp *self;
} HandleScanContext;
static void
handle_scan_context_free (HandleScanContext *ctx)
{
g_object_unref (ctx->skeleton);
g_object_unref (ctx->invocation);
g_object_unref (ctx->self);
g_free (ctx);
}
static GVariant *
scan_networks_build_result (GList *info_list)
{
GList *l;
GVariantBuilder builder;
g_variant_builder_init (&builder, G_VARIANT_TYPE ("aa{sv}"));
for (l = info_list; l; l = g_list_next (l)) {
MM3gppNetworkInfo *info = l->data;
if (!info->operator_code) {
g_warn_if_reached ();
continue;
}
g_variant_builder_open (&builder, G_VARIANT_TYPE ("a{sv}"));
g_variant_builder_add (&builder, "{sv}",
"operator-code", g_variant_new_string (info->operator_code));
g_variant_builder_add (&builder, "{sv}",
"status", g_variant_new_uint32 (info->status));
g_variant_builder_add (&builder, "{sv}",
"access-technology", g_variant_new_uint32 (info->access_tech));
if (info->operator_long)
g_variant_builder_add (&builder, "{sv}",
"operator-long", g_variant_new_string (info->operator_long));
if (info->operator_short)
g_variant_builder_add (&builder, "{sv}",
"operator-short", g_variant_new_string (info->operator_short));
g_variant_builder_close (&builder);
}
return g_variant_ref (g_variant_builder_end (&builder));
}
static void
handle_scan_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
HandleScanContext *ctx)
{
GError *error = NULL;
GList *info_list;
info_list = MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->scan_networks_finish (self, res, &error);
if (error)
g_dbus_method_invocation_take_error (ctx->invocation, error);
else {
GVariant *dict_array;
dict_array = scan_networks_build_result (info_list);
mm_gdbus_modem3gpp_complete_scan (ctx->skeleton,
ctx->invocation,
dict_array);
g_variant_unref (dict_array);
}
mm_3gpp_network_info_list_free (info_list);
handle_scan_context_free (ctx);
}
static void
handle_scan_auth_ready (MMBaseModem *self,
GAsyncResult *res,
HandleScanContext *ctx)
{
MMModemState modem_state;
GError *error = NULL;
if (!mm_base_modem_authorize_finish (self, res, &error)) {
g_dbus_method_invocation_take_error (ctx->invocation, error);
handle_scan_context_free (ctx);
return;
}
/* If scanning is not implemented, report an error */
if (!MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->scan_networks ||
!MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->scan_networks_finish) {
g_dbus_method_invocation_return_error (ctx->invocation,
MM_CORE_ERROR,
MM_CORE_ERROR_UNSUPPORTED,
"Cannot scan networks: operation not supported");
handle_scan_context_free (ctx);
return;
}
modem_state = MM_MODEM_STATE_UNKNOWN;
g_object_get (self,
MM_IFACE_MODEM_STATE, &modem_state,
NULL);
switch (modem_state) {
case MM_MODEM_STATE_FAILED:
case MM_MODEM_STATE_UNKNOWN:
case MM_MODEM_STATE_LOCKED:
/* We should never have such request (interface wasn't exported yet) */
g_assert_not_reached ();
break;
case MM_MODEM_STATE_INITIALIZING:
g_dbus_method_invocation_return_error (ctx->invocation,
MM_CORE_ERROR,
MM_CORE_ERROR_WRONG_STATE,
"Cannot scan networks: "
"device not fully initialized yet");
break;
case MM_MODEM_STATE_DISABLED:
case MM_MODEM_STATE_DISABLING:
case MM_MODEM_STATE_ENABLING:
g_dbus_method_invocation_return_error (ctx->invocation,
MM_CORE_ERROR,
MM_CORE_ERROR_WRONG_STATE,
"Cannot scan networks: not enabled yet");
break;
case MM_MODEM_STATE_ENABLED:
case MM_MODEM_STATE_SEARCHING:
case MM_MODEM_STATE_REGISTERED:
case MM_MODEM_STATE_DISCONNECTING:
case MM_MODEM_STATE_CONNECTING:
case MM_MODEM_STATE_CONNECTED:
MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->scan_networks (
MM_IFACE_MODEM_3GPP (self),
(GAsyncReadyCallback)handle_scan_ready,
ctx);
return;
}
handle_scan_context_free (ctx);
}
static gboolean
handle_scan (MmGdbusModem3gpp *skeleton,
GDBusMethodInvocation *invocation,
MMIfaceModem3gpp *self)
{
HandleScanContext *ctx;
ctx = g_new (HandleScanContext, 1);
ctx->skeleton = g_object_ref (skeleton);
ctx->invocation = g_object_ref (invocation);
ctx->self = g_object_ref (self);
mm_base_modem_authorize (MM_BASE_MODEM (self),
invocation,
MM_AUTHORIZATION_DEVICE_CONTROL,
(GAsyncReadyCallback)handle_scan_auth_ready,
ctx);
return TRUE;
}
/*****************************************************************************/
gboolean
mm_iface_modem_3gpp_run_registration_checks_finish (MMIfaceModem3gpp *self,
GAsyncResult *res,
GError **error)
{
g_assert (MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->run_registration_checks_finish != NULL);
return MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->run_registration_checks_finish (self, res, error);
}
void
mm_iface_modem_3gpp_run_registration_checks (MMIfaceModem3gpp *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
gboolean cs_supported = FALSE;
gboolean ps_supported = FALSE;
gboolean eps_supported = FALSE;
g_assert (MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->run_registration_checks != NULL);
g_object_get (self,
MM_IFACE_MODEM_3GPP_CS_NETWORK_SUPPORTED, &cs_supported,
MM_IFACE_MODEM_3GPP_PS_NETWORK_SUPPORTED, &ps_supported,
MM_IFACE_MODEM_3GPP_EPS_NETWORK_SUPPORTED, &eps_supported,
NULL);
mm_dbg ("Running registration checks (CS: '%s', PS: '%s', EPS: '%s')",
cs_supported ? "yes" : "no",
ps_supported ? "yes" : "no",
eps_supported ? "yes" : "no");
MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->run_registration_checks (self,
cs_supported,
ps_supported,
eps_supported,
callback,
user_data);
}
/*****************************************************************************/
typedef struct {
MMIfaceModem3gpp *self;
MmGdbusModem3gpp *skeleton;
GSimpleAsyncResult *result;
gboolean operator_code_loaded;
gboolean operator_name_loaded;
} ReloadCurrentOperatorContext;
static void
reload_current_operator_context_complete_and_free (ReloadCurrentOperatorContext *ctx)
{
g_simple_async_result_complete_in_idle (ctx->result);
g_object_unref (ctx->result);
if (ctx->skeleton)
g_object_unref (ctx->skeleton);
g_object_unref (ctx->self);
g_slice_free (ReloadCurrentOperatorContext, ctx);
}
gboolean
mm_iface_modem_3gpp_reload_current_operator_finish (MMIfaceModem3gpp *self,
GAsyncResult *res,
GError **error)
{
return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
}
static void reload_current_operator_context_step (ReloadCurrentOperatorContext *ctx);
static void
load_operator_name_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
ReloadCurrentOperatorContext *ctx)
{
GError *error = NULL;
gchar *str;
str = MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->load_operator_name_finish (self, res, &error);
if (error) {
mm_warn ("Couldn't load Operator Name: '%s'", error->message);
g_error_free (error);
}
if (ctx->skeleton)
mm_gdbus_modem3gpp_set_operator_name (ctx->skeleton, str);
g_free (str);
ctx->operator_name_loaded = TRUE;
reload_current_operator_context_step (ctx);
}
static gboolean
parse_mcc_mnc (const gchar *mccmnc,
guint *mcc_out,
guint *mnc_out)
{
guint mccmnc_len;
gchar mcc[4] = { 0, 0, 0, 0 };
gchar mnc[4] = { 0, 0, 0, 0 };
mccmnc_len = (mccmnc ? strlen (mccmnc) : 0);
if (mccmnc_len != 5 &&
mccmnc_len != 6) {
mm_dbg ("Unexpected MCC/MNC string '%s'", mccmnc);
return FALSE;
}
memcpy (mcc, mccmnc, 3);
/* Not all modems report 6-digit MNCs */
memcpy (mnc, mccmnc + 3, 2);
if (mccmnc_len == 6)
mnc[2] = mccmnc[5];
*mcc_out = atoi (mcc);
*mnc_out = atoi (mnc);
return TRUE;
}
static void
load_operator_code_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
ReloadCurrentOperatorContext *ctx)
{
GError *error = NULL;
gchar *str;
str = MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->load_operator_code_finish (self, res, &error);
if (error) {
mm_warn ("Couldn't load Operator Code: '%s'", error->message);
g_error_free (error);
}
if (ctx->skeleton)
mm_gdbus_modem3gpp_set_operator_code (ctx->skeleton, str);
/* If we also implement the location interface, update the 3GPP location */
if (str && MM_IS_IFACE_MODEM_LOCATION (self)) {
guint mcc = 0;
guint mnc = 0;
if (parse_mcc_mnc (str, &mcc, &mnc))
mm_iface_modem_location_3gpp_update_mcc_mnc (MM_IFACE_MODEM_LOCATION (self), mcc, mnc);
}
g_free (str);
ctx->operator_code_loaded = TRUE;
reload_current_operator_context_step (ctx);
}
static void
reload_current_operator_context_step (ReloadCurrentOperatorContext *ctx)
{
if (!ctx->operator_code_loaded) {
/* Launch operator code update */
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->load_operator_code (
ctx->self,
(GAsyncReadyCallback)load_operator_code_ready,
ctx);
return;
}
if (!ctx->operator_name_loaded) {
/* Launch operator name update */
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->load_operator_name (
ctx->self,
(GAsyncReadyCallback)load_operator_name_ready,
ctx);
return;
}
/* If both loaded, all done */
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
reload_current_operator_context_complete_and_free (ctx);
}
void
mm_iface_modem_3gpp_reload_current_operator (MMIfaceModem3gpp *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
ReloadCurrentOperatorContext *ctx;
ctx = g_slice_new0 (ReloadCurrentOperatorContext);
ctx->self = g_object_ref (self);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
mm_iface_modem_3gpp_reload_current_operator);
g_object_get (self,
MM_IFACE_MODEM_3GPP_DBUS_SKELETON, &ctx->skeleton,
NULL);
if (!ctx->skeleton) {
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't get interface skeleton");
reload_current_operator_context_complete_and_free (ctx);
return;
}
ctx->operator_code_loaded = !(MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->load_operator_code &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->load_operator_code_finish);
if (ctx->operator_code_loaded) {
mm_gdbus_modem3gpp_set_operator_code (ctx->skeleton, NULL);
if (MM_IS_IFACE_MODEM_LOCATION (self))
mm_iface_modem_location_3gpp_update_mcc_mnc (MM_IFACE_MODEM_LOCATION (self), 0, 0);
}
ctx->operator_name_loaded = !(MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->load_operator_name &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->load_operator_name_finish);
if (ctx->operator_name_loaded)
mm_gdbus_modem3gpp_set_operator_name (ctx->skeleton, NULL);
reload_current_operator_context_step (ctx);
}
void
mm_iface_modem_3gpp_clear_current_operator (MMIfaceModem3gpp *self)
{
MmGdbusModem3gpp *skeleton = NULL;
g_object_get (self,
MM_IFACE_MODEM_3GPP_DBUS_SKELETON, &skeleton,
NULL);
if (!skeleton)
return;
mm_gdbus_modem3gpp_set_operator_code (skeleton, NULL);
mm_gdbus_modem3gpp_set_operator_name (skeleton, NULL);
if (MM_IS_IFACE_MODEM_LOCATION (self))
mm_iface_modem_location_3gpp_update_mcc_mnc (MM_IFACE_MODEM_LOCATION (self), 0, 0);
}
/*****************************************************************************/
void
mm_iface_modem_3gpp_update_access_technologies (MMIfaceModem3gpp *self,
MMModemAccessTechnology access_tech)
{
MMModem3gppRegistrationState state;
g_object_get (self,
MM_IFACE_MODEM_3GPP_REGISTRATION_STATE, &state,
NULL);
/* Even if registration state didn't change, report access technology,
* but only if something valid to report */
if (state == MM_MODEM_3GPP_REGISTRATION_STATE_HOME ||
state == MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING) {
if (access_tech != MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN)
mm_iface_modem_update_access_technologies (MM_IFACE_MODEM (self),
access_tech,
MM_IFACE_MODEM_3GPP_ALL_ACCESS_TECHNOLOGIES_MASK);
} else
mm_iface_modem_update_access_technologies (MM_IFACE_MODEM (self),
MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN,
MM_IFACE_MODEM_3GPP_ALL_ACCESS_TECHNOLOGIES_MASK);
}
void
mm_iface_modem_3gpp_update_location (MMIfaceModem3gpp *self,
gulong location_area_code,
gulong cell_id)
{
MMModem3gppRegistrationState state;
if (!MM_IS_IFACE_MODEM_LOCATION (self))
return;
g_object_get (self,
MM_IFACE_MODEM_3GPP_REGISTRATION_STATE, &state,
NULL);
/* Even if registration state didn't change, report access technology or
* location updates, but only if something valid to report */
if (state == MM_MODEM_3GPP_REGISTRATION_STATE_HOME ||
state == MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING) {
if (location_area_code > 0 && cell_id > 0)
mm_iface_modem_location_3gpp_update_lac_ci (MM_IFACE_MODEM_LOCATION (self),
location_area_code,
cell_id);
} else
mm_iface_modem_location_3gpp_clear (MM_IFACE_MODEM_LOCATION (self));
}
/*****************************************************************************/
static void
update_registration_reload_current_operator_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
gpointer user_data)
{
MMModem3gppRegistrationState new_state;
RegistrationStateContext *ctx;
new_state = GPOINTER_TO_UINT (user_data);
mm_info ("Modem %s: 3GPP Registration state changed (registering -> %s)",
g_dbus_object_get_object_path (G_DBUS_OBJECT (self)),
mm_modem_3gpp_registration_state_get_string (new_state));
/* The property in the interface is bound to the property
* in the skeleton, so just updating here is enough */
g_object_set (self,
MM_IFACE_MODEM_3GPP_REGISTRATION_STATE, new_state,
NULL);
mm_iface_modem_update_subsystem_state (MM_IFACE_MODEM (self),
SUBSYSTEM_3GPP,
MM_MODEM_STATE_REGISTERED,
MM_MODEM_STATE_CHANGE_REASON_UNKNOWN);
ctx = get_registration_state_context (self);
ctx->reloading_operator = FALSE;
}
static void
update_non_registered_state (MMIfaceModem3gpp *self,
MMModem3gppRegistrationState old_state,
MMModem3gppRegistrationState new_state)
{
/* Not registered neither in home nor roaming network */
mm_iface_modem_3gpp_clear_current_operator (self);
/* The property in the interface is bound to the property
* in the skeleton, so just updating here is enough */
g_object_set (self,
MM_IFACE_MODEM_3GPP_REGISTRATION_STATE, new_state,
NULL);
mm_iface_modem_update_subsystem_state (
MM_IFACE_MODEM (self),
SUBSYSTEM_3GPP,
(new_state == MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING ?
MM_MODEM_STATE_SEARCHING :
MM_MODEM_STATE_ENABLED),
MM_MODEM_STATE_CHANGE_REASON_UNKNOWN);
}
static gboolean
run_deferred_registration_state_update (MMIfaceModem3gpp *self)
{
MMModem3gppRegistrationState old_state = MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN;
MMModem3gppRegistrationState new_state;
RegistrationStateContext *ctx;
ctx = get_registration_state_context (self);
ctx->deferred_update_id = 0;
g_object_get (self,
MM_IFACE_MODEM_3GPP_REGISTRATION_STATE, &old_state,
NULL);
new_state = ctx->deferred_new_state;
/* Only set new state if different */
if (new_state == old_state)
return FALSE;
mm_info ("Modem %s: (deferred) 3GPP Registration state changed (%s -> %s)",
g_dbus_object_get_object_path (G_DBUS_OBJECT (self)),
mm_modem_3gpp_registration_state_get_string (old_state),
mm_modem_3gpp_registration_state_get_string (new_state));
update_non_registered_state (self, old_state, new_state);
return FALSE;
}
static void
update_registration_state (MMIfaceModem3gpp *self,
MMModem3gppRegistrationState new_state)
{
MMModem3gppRegistrationState old_state = MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN;
RegistrationStateContext *ctx;
g_object_get (self,
MM_IFACE_MODEM_3GPP_REGISTRATION_STATE, &old_state,
NULL);
ctx = get_registration_state_context (self);
g_assert (ctx);
if (ctx->deferred_update_id != 0) {
/* If there is already a deferred 'registration loss' state update and the new update
* is not a registered state, update the deferred state update without extending the
* timeout. */
if (new_state != MM_MODEM_3GPP_REGISTRATION_STATE_HOME &&
new_state != MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING) {
mm_info ("Modem %s: 3GPP Registration state changed (%s -> %s), update deferred",
g_dbus_object_get_object_path (G_DBUS_OBJECT (self)),
mm_modem_3gpp_registration_state_get_string (old_state),
mm_modem_3gpp_registration_state_get_string (new_state));
ctx->deferred_new_state = new_state;
return;
}
/* Otherwise, cancel any deferred registration state update */
g_source_remove (ctx->deferred_update_id);
ctx->deferred_update_id = 0;
ctx->deferred_new_state = MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN;
}
/* Only set new state if different */
if (new_state == old_state)
return;
if (new_state == MM_MODEM_3GPP_REGISTRATION_STATE_HOME ||
new_state == MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING) {
/* If already reloading operator, skip it */
if (ctx->reloading_operator)
return;
mm_info ("Modem %s: 3GPP Registration state changed (%s -> registering)",
g_dbus_object_get_object_path (G_DBUS_OBJECT (self)),
mm_modem_3gpp_registration_state_get_string (old_state));
/* Reload current operator. ONLY update the state to REGISTERED after
* having loaded operator code/name */
ctx->reloading_operator = TRUE;
mm_iface_modem_3gpp_reload_current_operator (
self,
(GAsyncReadyCallback)update_registration_reload_current_operator_ready,
GUINT_TO_POINTER (new_state));
return;
}
if ((old_state == MM_MODEM_3GPP_REGISTRATION_STATE_HOME ||
old_state == MM_MODEM_3GPP_REGISTRATION_STATE_ROAMING) &&
(new_state == MM_MODEM_3GPP_REGISTRATION_STATE_SEARCHING ||
new_state == MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN)) {
mm_info ("Modem %s: 3GPP Registration state changed (%s -> %s), update deferred",
g_dbus_object_get_object_path (G_DBUS_OBJECT (self)),
mm_modem_3gpp_registration_state_get_string (old_state),
mm_modem_3gpp_registration_state_get_string (new_state));
ctx->deferred_new_state = new_state;
ctx->deferred_update_id = g_timeout_add_seconds (
DEFERRED_REGISTRATION_STATE_UPDATE_TIMEOUT_SEC,
(GSourceFunc)run_deferred_registration_state_update,
self);
return;
}
mm_info ("Modem %s: 3GPP Registration state changed (%s -> %s)",
g_dbus_object_get_object_path (G_DBUS_OBJECT (self)),
mm_modem_3gpp_registration_state_get_string (old_state),
mm_modem_3gpp_registration_state_get_string (new_state));
update_non_registered_state (self, old_state, new_state);
}
void
mm_iface_modem_3gpp_update_cs_registration_state (MMIfaceModem3gpp *self,
MMModem3gppRegistrationState state)
{
RegistrationStateContext *ctx;
gboolean supported = FALSE;
g_object_get (self,
MM_IFACE_MODEM_3GPP_CS_NETWORK_SUPPORTED, &supported,
NULL);
if (!supported)
return;
ctx = get_registration_state_context (self);
ctx->cs = state;
update_registration_state (self, get_consolidated_reg_state (ctx));
}
void
mm_iface_modem_3gpp_update_ps_registration_state (MMIfaceModem3gpp *self,
MMModem3gppRegistrationState state)
{
RegistrationStateContext *ctx;
gboolean supported = FALSE;
g_object_get (self,
MM_IFACE_MODEM_3GPP_PS_NETWORK_SUPPORTED, &supported,
NULL);
if (!supported)
return;
ctx = get_registration_state_context (self);
ctx->ps = state;
update_registration_state (self, get_consolidated_reg_state (ctx));
}
void
mm_iface_modem_3gpp_update_eps_registration_state (MMIfaceModem3gpp *self,
MMModem3gppRegistrationState state)
{
RegistrationStateContext *ctx;
gboolean supported = FALSE;
g_object_get (self,
MM_IFACE_MODEM_3GPP_EPS_NETWORK_SUPPORTED, &supported,
NULL);
if (!supported)
return;
ctx = get_registration_state_context (self);
ctx->eps = state;
update_registration_state (self, get_consolidated_reg_state (ctx));
}
/*****************************************************************************/
typedef struct {
guint timeout_source;
gboolean running;
} RegistrationCheckContext;
static void
registration_check_context_free (RegistrationCheckContext *ctx)
{
if (ctx->timeout_source)
g_source_remove (ctx->timeout_source);
g_free (ctx);
}
static void
periodic_registration_checks_ready (MMIfaceModem3gpp *self,
GAsyncResult *res)
{
RegistrationCheckContext *ctx;
GError *error = NULL;
mm_iface_modem_3gpp_run_registration_checks_finish (self, res, &error);
if (error) {
mm_dbg ("Couldn't refresh 3GPP registration status: '%s'", error->message);
g_error_free (error);
}
/* Remove the running tag */
ctx = g_object_get_qdata (G_OBJECT (self), registration_check_context_quark);
if (ctx)
ctx->running = FALSE;
}
static gboolean
periodic_registration_check (MMIfaceModem3gpp *self)
{
RegistrationCheckContext *ctx;
/* Only launch a new one if not one running already */
ctx = g_object_get_qdata (G_OBJECT (self), registration_check_context_quark);
if (!ctx->running) {
ctx->running = TRUE;
mm_iface_modem_3gpp_run_registration_checks (
self,
(GAsyncReadyCallback)periodic_registration_checks_ready,
NULL);
}
return TRUE;
}
static void
periodic_registration_check_disable (MMIfaceModem3gpp *self)
{
if (G_UNLIKELY (!registration_check_context_quark))
registration_check_context_quark = (g_quark_from_static_string (
REGISTRATION_CHECK_CONTEXT_TAG));
/* Overwriting the data will free the previous context */
g_object_set_qdata (G_OBJECT (self),
registration_check_context_quark,
NULL);
mm_dbg ("Periodic 3GPP registration checks disabled");
}
static void
periodic_registration_check_enable (MMIfaceModem3gpp *self)
{
RegistrationCheckContext *ctx;
if (G_UNLIKELY (!registration_check_context_quark))
registration_check_context_quark = (g_quark_from_static_string (
REGISTRATION_CHECK_CONTEXT_TAG));
ctx = g_object_get_qdata (G_OBJECT (self), registration_check_context_quark);
/* If context is already there, we're already enabled */
if (ctx)
return;
/* Create context and keep it as object data */
mm_dbg ("Periodic 3GPP registration checks enabled");
ctx = g_new0 (RegistrationCheckContext, 1);
ctx->timeout_source = g_timeout_add_seconds (REGISTRATION_CHECK_TIMEOUT_SEC,
(GSourceFunc)periodic_registration_check,
self);
g_object_set_qdata_full (G_OBJECT (self),
registration_check_context_quark,
ctx,
(GDestroyNotify)registration_check_context_free);
}
static void
clear_deferred_registration_state_update (MMIfaceModem3gpp *self)
{
RegistrationStateContext *ctx = get_registration_state_context (self);
ctx->deferred_new_state = MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN;
if (ctx->deferred_update_id) {
g_source_remove (ctx->deferred_update_id);
ctx->deferred_update_id = 0;
}
}
/*****************************************************************************/
typedef struct _DisablingContext DisablingContext;
static void interface_disabling_step (DisablingContext *ctx);
typedef enum {
DISABLING_STEP_FIRST,
DISABLING_STEP_PERIODIC_REGISTRATION_CHECKS,
DISABLING_STEP_DISABLE_UNSOLICITED_REGISTRATION_EVENTS,
DISABLING_STEP_CLEANUP_UNSOLICITED_REGISTRATION_EVENTS,
DISABLING_STEP_CLEANUP_UNSOLICITED_EVENTS,
DISABLING_STEP_DISABLE_UNSOLICITED_EVENTS,
DISABLING_STEP_REGISTRATION_STATE,
DISABLING_STEP_LAST
} DisablingStep;
struct _DisablingContext {
MMIfaceModem3gpp *self;
DisablingStep step;
GSimpleAsyncResult *result;
MmGdbusModem *skeleton;
};
static void
disabling_context_complete_and_free (DisablingContext *ctx)
{
g_simple_async_result_complete_in_idle (ctx->result);
g_object_unref (ctx->self);
g_object_unref (ctx->result);
if (ctx->skeleton)
g_object_unref (ctx->skeleton);
g_free (ctx);
}
gboolean
mm_iface_modem_3gpp_disable_finish (MMIfaceModem3gpp *self,
GAsyncResult *res,
GError **error)
{
return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
}
#undef VOID_REPLY_READY_FN
#define VOID_REPLY_READY_FN(NAME,DISPLAY) \
static void \
NAME##_ready (MMIfaceModem3gpp *self, \
GAsyncResult *res, \
DisablingContext *ctx) \
{ \
GError *error = NULL; \
\
MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->NAME##_finish (self, res, &error); \
if (error) { \
mm_dbg ("Couldn't %s: '%s'", DISPLAY, error->message); \
g_error_free (error); \
} \
\
/* Go on to next step */ \
ctx->step++; \
interface_disabling_step (ctx); \
}
VOID_REPLY_READY_FN (cleanup_unsolicited_events,
"cleanup unsolicited events")
VOID_REPLY_READY_FN (disable_unsolicited_events,
"disable unsolicited events")
VOID_REPLY_READY_FN (cleanup_unsolicited_registration_events,
"cleanup unsolicited registration events")
VOID_REPLY_READY_FN (disable_unsolicited_registration_events,
"disable unsolicited registration events")
static void
interface_disabling_step (DisablingContext *ctx)
{
switch (ctx->step) {
case DISABLING_STEP_FIRST:
/* Fall down to next step */
ctx->step++;
case DISABLING_STEP_PERIODIC_REGISTRATION_CHECKS:
/* Disable periodic registration checks, if they were set */
periodic_registration_check_disable (ctx->self);
/* Prevent any deferred registration state update from happening after the modem is disabled */
clear_deferred_registration_state_update (ctx->self);
/* Fall down to next step */
ctx->step++;
case DISABLING_STEP_DISABLE_UNSOLICITED_REGISTRATION_EVENTS: {
gboolean cs_supported = FALSE;
gboolean ps_supported = FALSE;
gboolean eps_supported = FALSE;
g_object_get (ctx->self,
MM_IFACE_MODEM_3GPP_CS_NETWORK_SUPPORTED, &cs_supported,
MM_IFACE_MODEM_3GPP_PS_NETWORK_SUPPORTED, &ps_supported,
MM_IFACE_MODEM_3GPP_EPS_NETWORK_SUPPORTED, &eps_supported,
NULL);
if (MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->disable_unsolicited_registration_events &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->disable_unsolicited_registration_events_finish) {
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->disable_unsolicited_registration_events (
ctx->self,
cs_supported,
ps_supported,
eps_supported,
(GAsyncReadyCallback)disable_unsolicited_registration_events_ready,
ctx);
return;
}
/* Fall down to next step */
ctx->step++;
}
case DISABLING_STEP_CLEANUP_UNSOLICITED_REGISTRATION_EVENTS:
if (MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->cleanup_unsolicited_registration_events &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->cleanup_unsolicited_registration_events_finish) {
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->cleanup_unsolicited_registration_events (
ctx->self,
(GAsyncReadyCallback)cleanup_unsolicited_registration_events_ready,
ctx);
return;
}
/* Fall down to next step */
ctx->step++;
case DISABLING_STEP_CLEANUP_UNSOLICITED_EVENTS:
if (MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->cleanup_unsolicited_events &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->cleanup_unsolicited_events_finish) {
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->cleanup_unsolicited_events (
ctx->self,
(GAsyncReadyCallback)cleanup_unsolicited_events_ready,
ctx);
return;
}
/* Fall down to next step */
ctx->step++;
case DISABLING_STEP_DISABLE_UNSOLICITED_EVENTS:
if (MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->disable_unsolicited_events &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->disable_unsolicited_events_finish) {
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->disable_unsolicited_events (
ctx->self,
(GAsyncReadyCallback)disable_unsolicited_events_ready,
ctx);
return;
}
/* Fall down to next step */
ctx->step++;
case DISABLING_STEP_REGISTRATION_STATE:
update_registration_state (ctx->self, MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN);
mm_iface_modem_3gpp_update_access_technologies (ctx->self, MM_MODEM_ACCESS_TECHNOLOGY_UNKNOWN);
mm_iface_modem_3gpp_update_location (ctx->self, 0, 0);
/* Fall down to next step */
ctx->step++;
case DISABLING_STEP_LAST:
/* We are done without errors! */
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
disabling_context_complete_and_free (ctx);
return;
}
g_assert_not_reached ();
}
void
mm_iface_modem_3gpp_disable (MMIfaceModem3gpp *self,
GAsyncReadyCallback callback,
gpointer user_data)
{
DisablingContext *ctx;
ctx = g_new0 (DisablingContext, 1);
ctx->self = g_object_ref (self);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
mm_iface_modem_3gpp_disable);
ctx->step = DISABLING_STEP_FIRST;
g_object_get (ctx->self,
MM_IFACE_MODEM_3GPP_DBUS_SKELETON, &ctx->skeleton,
NULL);
if (!ctx->skeleton) {
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't get interface skeleton");
disabling_context_complete_and_free (ctx);
return;
}
interface_disabling_step (ctx);
}
/*****************************************************************************/
typedef struct _EnablingContext EnablingContext;
static void interface_enabling_step (EnablingContext *ctx);
typedef enum {
ENABLING_STEP_FIRST,
ENABLING_STEP_SETUP_UNSOLICITED_EVENTS,
ENABLING_STEP_ENABLE_UNSOLICITED_EVENTS,
ENABLING_STEP_SETUP_UNSOLICITED_REGISTRATION_EVENTS,
ENABLING_STEP_ENABLE_UNSOLICITED_REGISTRATION_EVENTS,
ENABLING_STEP_RUN_REGISTRATION_CHECKS,
ENABLING_STEP_LAST
} EnablingStep;
struct _EnablingContext {
MMIfaceModem3gpp *self;
EnablingStep step;
GSimpleAsyncResult *result;
GCancellable *cancellable;
MmGdbusModem3gpp *skeleton;
};
static void
enabling_context_complete_and_free (EnablingContext *ctx)
{
g_simple_async_result_complete_in_idle (ctx->result);
g_object_unref (ctx->self);
g_object_unref (ctx->result);
g_object_unref (ctx->cancellable);
if (ctx->skeleton)
g_object_unref (ctx->skeleton);
g_free (ctx);
}
static gboolean
enabling_context_complete_and_free_if_cancelled (EnablingContext *ctx)
{
if (!g_cancellable_is_cancelled (ctx->cancellable))
return FALSE;
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_CANCELLED,
"Interface enabling cancelled");
enabling_context_complete_and_free (ctx);
return TRUE;
}
gboolean
mm_iface_modem_3gpp_enable_finish (MMIfaceModem3gpp *self,
GAsyncResult *res,
GError **error)
{
return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
}
static void
setup_unsolicited_events_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
EnablingContext *ctx)
{
GError *error = NULL;
MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->setup_unsolicited_events_finish (self, res, &error);
if (error) {
/* This error shouldn't be treated as critical */
mm_dbg ("Setting up unsolicited events failed: '%s'", error->message);
g_error_free (error);
/* If we get an error setting up unsolicited events, don't even bother trying to
* enable them. */
ctx->step = ENABLING_STEP_ENABLE_UNSOLICITED_EVENTS + 1;
interface_enabling_step (ctx);
return;
}
/* Go on to next step */
ctx->step++;
interface_enabling_step (ctx);
}
static void
enable_unsolicited_events_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
EnablingContext *ctx)
{
GError *error = NULL;
MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->enable_unsolicited_events_finish (self, res, &error);
if (error) {
/* This error shouldn't be treated as critical */
mm_dbg ("Enabling unsolicited events failed: '%s'", error->message);
g_error_free (error);
}
/* Go on to next step */
ctx->step++;
interface_enabling_step (ctx);
}
static void
setup_unsolicited_registration_events_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
EnablingContext *ctx)
{
GError *error = NULL;
MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->setup_unsolicited_registration_events_finish (self, res, &error);
if (error) {
/* This error shouldn't be treated as critical */
mm_dbg ("Setting up unsolicited registration events failed: '%s'", error->message);
g_error_free (error);
/* If we get an error setting up unsolicited events, don't even bother trying to
* enable them. */
ctx->step = ENABLING_STEP_ENABLE_UNSOLICITED_REGISTRATION_EVENTS + 1;
interface_enabling_step (ctx);
/* If error, setup periodic registration checks */
periodic_registration_check_enable (ctx->self);
return;
}
/* Go on to next step */
ctx->step++;
interface_enabling_step (ctx);
}
static void
enable_unsolicited_registration_events_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
EnablingContext *ctx)
{
GError *error = NULL;
MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->enable_unsolicited_registration_events_finish (self, res, &error);
if (error) {
/* This error shouldn't be treated as critical */
mm_dbg ("Enabling unsolicited registration events failed: '%s'", error->message);
g_error_free (error);
/* If error, setup periodic registration checks */
periodic_registration_check_enable (ctx->self);
}
/* Go on to next step */
ctx->step++;
interface_enabling_step (ctx);
}
static void
run_all_registration_checks_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
EnablingContext *ctx)
{
GError *error = NULL;
mm_iface_modem_3gpp_run_registration_checks_finish (self, res, &error);
if (error) {
g_simple_async_result_take_error (ctx->result, error);
enabling_context_complete_and_free (ctx);
return;
}
/* Go on to next step */
ctx->step++;
interface_enabling_step (ctx);
}
static void
interface_enabling_step (EnablingContext *ctx)
{
/* Don't run new steps if we're cancelled */
if (enabling_context_complete_and_free_if_cancelled (ctx))
return;
switch (ctx->step) {
case ENABLING_STEP_FIRST:
/* Fall down to next step */
ctx->step++;
case ENABLING_STEP_SETUP_UNSOLICITED_EVENTS:
if (MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->setup_unsolicited_events &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->setup_unsolicited_events_finish) {
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->setup_unsolicited_events (
ctx->self,
(GAsyncReadyCallback)setup_unsolicited_events_ready,
ctx);
return;
}
/* Fall down to next step */
ctx->step++;
case ENABLING_STEP_ENABLE_UNSOLICITED_EVENTS:
if (MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->enable_unsolicited_events &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->enable_unsolicited_events_finish) {
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->enable_unsolicited_events (
ctx->self,
(GAsyncReadyCallback)enable_unsolicited_events_ready,
ctx);
return;
}
/* Fall down to next step */
ctx->step++;
case ENABLING_STEP_SETUP_UNSOLICITED_REGISTRATION_EVENTS:
if (MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->setup_unsolicited_registration_events &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->setup_unsolicited_registration_events_finish) {
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->setup_unsolicited_registration_events (
ctx->self,
(GAsyncReadyCallback)setup_unsolicited_registration_events_ready,
ctx);
return;
}
/* Fall down to next step */
ctx->step++;
case ENABLING_STEP_ENABLE_UNSOLICITED_REGISTRATION_EVENTS: {
gboolean cs_supported = FALSE;
gboolean ps_supported = FALSE;
gboolean eps_supported = FALSE;
g_object_get (ctx->self,
MM_IFACE_MODEM_3GPP_CS_NETWORK_SUPPORTED, &cs_supported,
MM_IFACE_MODEM_3GPP_PS_NETWORK_SUPPORTED, &ps_supported,
MM_IFACE_MODEM_3GPP_EPS_NETWORK_SUPPORTED, &eps_supported,
NULL);
if (MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->enable_unsolicited_registration_events &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->enable_unsolicited_registration_events_finish) {
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->enable_unsolicited_registration_events (
ctx->self,
cs_supported,
ps_supported,
eps_supported,
(GAsyncReadyCallback)enable_unsolicited_registration_events_ready,
ctx);
return;
}
/* Fall down to next step */
ctx->step++;
}
case ENABLING_STEP_RUN_REGISTRATION_CHECKS:
mm_iface_modem_3gpp_run_registration_checks (
ctx->self,
(GAsyncReadyCallback)run_all_registration_checks_ready,
ctx);
return;
case ENABLING_STEP_LAST:
/* We are done without errors! */
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
enabling_context_complete_and_free (ctx);
return;
}
g_assert_not_reached ();
}
void
mm_iface_modem_3gpp_enable (MMIfaceModem3gpp *self,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
EnablingContext *ctx;
ctx = g_new0 (EnablingContext, 1);
ctx->self = g_object_ref (self);
ctx->cancellable = g_object_ref (cancellable);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
mm_iface_modem_3gpp_enable);
ctx->step = ENABLING_STEP_FIRST;
g_object_get (ctx->self,
MM_IFACE_MODEM_3GPP_DBUS_SKELETON, &ctx->skeleton,
NULL);
if (!ctx->skeleton) {
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_FAILED,
"Couldn't get interface skeleton");
enabling_context_complete_and_free (ctx);
return;
}
interface_enabling_step (ctx);
}
/*****************************************************************************/
typedef struct _InitializationContext InitializationContext;
static void interface_initialization_step (InitializationContext *ctx);
typedef enum {
INITIALIZATION_STEP_FIRST,
INITIALIZATION_STEP_IMEI,
INITIALIZATION_STEP_ENABLED_FACILITY_LOCKS,
INITIALIZATION_STEP_LAST
} InitializationStep;
struct _InitializationContext {
MMIfaceModem3gpp *self;
MmGdbusModem3gpp *skeleton;
GSimpleAsyncResult *result;
GCancellable *cancellable;
InitializationStep step;
};
static void
initialization_context_complete_and_free (InitializationContext *ctx)
{
g_simple_async_result_complete_in_idle (ctx->result);
g_object_unref (ctx->self);
g_object_unref (ctx->result);
g_object_unref (ctx->cancellable);
g_object_unref (ctx->skeleton);
g_free (ctx);
}
static gboolean
initialization_context_complete_and_free_if_cancelled (InitializationContext *ctx)
{
if (!g_cancellable_is_cancelled (ctx->cancellable))
return FALSE;
g_simple_async_result_set_error (ctx->result,
MM_CORE_ERROR,
MM_CORE_ERROR_CANCELLED,
"Interface initialization cancelled");
initialization_context_complete_and_free (ctx);
return TRUE;
}
static void
sim_pin_lock_enabled_cb (MMSim *self,
gboolean enabled,
MmGdbusModem3gpp *skeleton)
{
MMModem3gppFacility facilities;
facilities = mm_gdbus_modem3gpp_get_enabled_facility_locks (skeleton);
if (enabled)
facilities |= MM_MODEM_3GPP_FACILITY_SIM;
else
facilities &= ~MM_MODEM_3GPP_FACILITY_SIM;
mm_gdbus_modem3gpp_set_enabled_facility_locks (skeleton, facilities);
}
static void
load_enabled_facility_locks_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
InitializationContext *ctx)
{
GError *error = NULL;
MMModem3gppFacility facilities;
facilities = MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->load_enabled_facility_locks_finish (self, res, &error);
mm_gdbus_modem3gpp_set_enabled_facility_locks (ctx->skeleton, facilities);
if (error) {
mm_warn ("couldn't load facility locks: '%s'", error->message);
g_error_free (error);
} else {
MMSim *sim = NULL;
/* We loaded the initial list of facility locks; but we do need to update
* the SIM PIN lock status when that changes. We'll connect to the signal
* which notifies about such update. There is no need to ref self as the
* SIM itself is an object which exists as long as self exists. */
g_object_get (self,
MM_IFACE_MODEM_SIM, &sim,
NULL);
g_signal_connect (sim,
MM_SIM_PIN_LOCK_ENABLED,
G_CALLBACK (sim_pin_lock_enabled_cb),
ctx->skeleton);
g_object_unref (sim);
}
/* Go on to next step */
ctx->step++;
interface_initialization_step (ctx);
}
static void
load_imei_ready (MMIfaceModem3gpp *self,
GAsyncResult *res,
InitializationContext *ctx)
{
GError *error = NULL;
gchar *imei;
imei = MM_IFACE_MODEM_3GPP_GET_INTERFACE (self)->load_imei_finish (self, res, &error);
mm_gdbus_modem3gpp_set_imei (ctx->skeleton, imei);
g_free (imei);
if (error) {
mm_warn ("couldn't load IMEI: '%s'", error->message);
g_error_free (error);
}
/* Go on to next step */
ctx->step++;
interface_initialization_step (ctx);
}
static void
interface_initialization_step (InitializationContext *ctx)
{
/* Don't run new steps if we're cancelled */
if (initialization_context_complete_and_free_if_cancelled (ctx))
return;
switch (ctx->step) {
case INITIALIZATION_STEP_FIRST:
/* Fall down to next step */
ctx->step++;
case INITIALIZATION_STEP_IMEI:
/* IMEI value is meant to be loaded only once during the whole
* lifetime of the modem. Therefore, if we already have it loaded,
* don't try to load it again. */
if (!mm_gdbus_modem3gpp_get_imei (ctx->skeleton) &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->load_imei &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->load_imei_finish) {
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->load_imei (
ctx->self,
(GAsyncReadyCallback)load_imei_ready,
ctx);
return;
}
/* Fall down to next step */
ctx->step++;
case INITIALIZATION_STEP_ENABLED_FACILITY_LOCKS:
if (MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->load_enabled_facility_locks &&
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->load_enabled_facility_locks_finish) {
MM_IFACE_MODEM_3GPP_GET_INTERFACE (ctx->self)->load_enabled_facility_locks (
ctx->self,
(GAsyncReadyCallback)load_enabled_facility_locks_ready,
ctx);
return;
}
/* Fall down to next step */
ctx->step++;
case INITIALIZATION_STEP_LAST:
/* We are done without errors! */
/* Handle method invocations */
g_signal_connect (ctx->skeleton,
"handle-register",
G_CALLBACK (handle_register),
ctx->self);
g_signal_connect (ctx->skeleton,
"handle-scan",
G_CALLBACK (handle_scan),
ctx->self);
/* Finally, export the new interface */
mm_gdbus_object_skeleton_set_modem3gpp (MM_GDBUS_OBJECT_SKELETON (ctx->self),
MM_GDBUS_MODEM3GPP (ctx->skeleton));
g_simple_async_result_set_op_res_gboolean (ctx->result, TRUE);
initialization_context_complete_and_free (ctx);
return;
}
g_assert_not_reached ();
}
gboolean
mm_iface_modem_3gpp_initialize_finish (MMIfaceModem3gpp *self,
GAsyncResult *res,
GError **error)
{
return !g_simple_async_result_propagate_error (G_SIMPLE_ASYNC_RESULT (res), error);
}
void
mm_iface_modem_3gpp_initialize (MMIfaceModem3gpp *self,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
MmGdbusModem3gpp *skeleton = NULL;
InitializationContext *ctx;
/* Did we already create it? */
g_object_get (self,
MM_IFACE_MODEM_3GPP_DBUS_SKELETON, &skeleton,
NULL);
if (!skeleton) {
skeleton = mm_gdbus_modem3gpp_skeleton_new ();
/* Set all initial property defaults */
mm_gdbus_modem3gpp_set_imei (skeleton, NULL);
mm_gdbus_modem3gpp_set_operator_code (skeleton, NULL);
mm_gdbus_modem3gpp_set_operator_name (skeleton, NULL);
mm_gdbus_modem3gpp_set_enabled_facility_locks (skeleton, MM_MODEM_3GPP_FACILITY_NONE);
/* Bind our RegistrationState property */
g_object_bind_property (self, MM_IFACE_MODEM_3GPP_REGISTRATION_STATE,
skeleton, "registration-state",
G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
g_object_set (self,
MM_IFACE_MODEM_3GPP_DBUS_SKELETON, skeleton,
NULL);
/* If the modem is *only* LTE, we assume that CS network is not
* supported */
if (mm_iface_modem_is_3gpp_lte_only (MM_IFACE_MODEM (self))) {
mm_dbg ("Modem is LTE-only, assuming CS network is not supported");
g_object_set (self,
MM_IFACE_MODEM_3GPP_CS_NETWORK_SUPPORTED, FALSE,
NULL);
}
}
ctx = g_new0 (InitializationContext, 1);
ctx->self = g_object_ref (self);
ctx->cancellable = g_object_ref (cancellable);
ctx->result = g_simple_async_result_new (G_OBJECT (self),
callback,
user_data,
mm_iface_modem_3gpp_initialize);
ctx->step = INITIALIZATION_STEP_FIRST;
ctx->skeleton = skeleton;
/* Perform async initialization here */
interface_initialization_step (ctx);
}
void
mm_iface_modem_3gpp_shutdown (MMIfaceModem3gpp *self)
{
/* Remove RegistrationCheckContext object to make sure any pending
* invocation of periodic_registration_check is cancelled before the
* DBus skeleton is removed. */
if (G_LIKELY (registration_check_context_quark))
g_object_set_qdata (G_OBJECT (self),
registration_check_context_quark,
NULL);
/* Unexport DBus interface and remove the skeleton */
mm_gdbus_object_skeleton_set_modem3gpp (MM_GDBUS_OBJECT_SKELETON (self), NULL);
g_object_set (self,
MM_IFACE_MODEM_3GPP_DBUS_SKELETON, NULL,
NULL);
}
/*****************************************************************************/
static void
iface_modem_3gpp_init (gpointer g_iface)
{
static gboolean initialized = FALSE;
if (initialized)
return;
/* Properties */
g_object_interface_install_property
(g_iface,
g_param_spec_object (MM_IFACE_MODEM_3GPP_DBUS_SKELETON,
"3GPP DBus skeleton",
"DBus skeleton for the 3GPP interface",
MM_GDBUS_TYPE_MODEM3GPP_SKELETON,
G_PARAM_READWRITE));
g_object_interface_install_property
(g_iface,
g_param_spec_enum (MM_IFACE_MODEM_3GPP_REGISTRATION_STATE,
"RegistrationState",
"Registration state of the modem",
MM_TYPE_MODEM_3GPP_REGISTRATION_STATE,
MM_MODEM_3GPP_REGISTRATION_STATE_UNKNOWN,
G_PARAM_READWRITE));
g_object_interface_install_property
(g_iface,
g_param_spec_boolean (MM_IFACE_MODEM_3GPP_CS_NETWORK_SUPPORTED,
"CS network supported",
"Whether the modem works in the CS network",
TRUE,
G_PARAM_READWRITE));
g_object_interface_install_property
(g_iface,
g_param_spec_boolean (MM_IFACE_MODEM_3GPP_PS_NETWORK_SUPPORTED,
"PS network supported",
"Whether the modem works in the PS network",
TRUE,
G_PARAM_READWRITE));
g_object_interface_install_property
(g_iface,
g_param_spec_boolean (MM_IFACE_MODEM_3GPP_EPS_NETWORK_SUPPORTED,
"EPS network supported",
"Whether the modem works in the EPS network",
FALSE,
G_PARAM_READWRITE));
initialized = TRUE;
}
GType
mm_iface_modem_3gpp_get_type (void)
{
static GType iface_modem_3gpp_type = 0;
if (!G_UNLIKELY (iface_modem_3gpp_type)) {
static const GTypeInfo info = {
sizeof (MMIfaceModem3gpp), /* class_size */
iface_modem_3gpp_init, /* base_init */
NULL, /* base_finalize */
};
iface_modem_3gpp_type = g_type_register_static (G_TYPE_INTERFACE,
"MMIfaceModem3gpp",
&info,
0);
g_type_interface_add_prerequisite (iface_modem_3gpp_type, MM_TYPE_IFACE_MODEM);
}
return iface_modem_3gpp_type;
}
|