Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

   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
/*
 *
 *  oFono - Open Source Telephony
 *
 *  Copyright (C) 2008-2010  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdio.h>
#include <errno.h>

#include <glib.h>
#include <gdbus.h>

#include "ofono.h"

#include "common.h"
#include "simutil.h"
#include "util.h"
#include "storage.h"

#define NETWORK_REGISTRATION_FLAG_HOME_SHOW_PLMN 0x1
#define NETWORK_REGISTRATION_FLAG_ROAMING_SHOW_SPN 0x2

enum network_registration_mode {
	NETWORK_REGISTRATION_MODE_AUTO = 0,
	NETWORK_REGISTRATION_MODE_MANUAL = 1,
	NETWORK_REGISTRATION_MODE_OFF = 2,
	NETWORK_REGISTRATION_MODE_MANUAL_AUTO = 4
};

#define SETTINGS_STORE "netreg"
#define SETTINGS_GROUP "Settings"

static GSList *g_drivers = NULL;

/* 27.007 Section 7.3 <stat> */
enum operator_status {
	OPERATOR_STATUS_UNKNOWN = 0,
	OPERATOR_STATUS_AVAILABLE = 1,
	OPERATOR_STATUS_CURRENT = 2,
	OPERATOR_STATUS_FORBIDDEN = 3
};

struct ofono_netreg {
	int status;
	int location;
	int cellid;
	int technology;
	int mode;
	char *base_station;
	struct network_operator_data *current_operator;
	GSList *operator_list;
	struct ofono_network_registration_ops *ops;
	int flags;
	DBusMessage *pending;
	int signal_strength;
	char *spname;
	struct sim_spdi *spdi;
	struct sim_eons *eons;
	struct ofono_sim *sim;
	GKeyFile *settings;
	char *imsi;
	struct ofono_watchlist *status_watches;
	const struct ofono_netreg_driver *driver;
	void *driver_data;
	struct ofono_atom *atom;
};

static void operator_list_callback(const struct ofono_error *error, int total,
				const struct ofono_network_operator *list,
				void *data);

static void current_operator_callback(const struct ofono_error *error,
				const struct ofono_network_operator *current,
				void *data);

static void signal_strength_callback(const struct ofono_error *error,
					int strength, void *data);

static void registration_status_callback(const struct ofono_error *error,
					int status, int lac, int ci, int tech,
					void *data);

struct network_operator_data {
	char name[OFONO_MAX_OPERATOR_NAME_LENGTH + 1];
	char mcc[OFONO_MAX_MCC_LENGTH + 1];
	char mnc[OFONO_MAX_MNC_LENGTH + 1];
	int status;
	unsigned int techs;
	const struct sim_eons_operator_info *eons_info;
	struct ofono_netreg *netreg;
};

static const char *registration_mode_to_string(int mode)
{
	switch (mode) {
	case NETWORK_REGISTRATION_MODE_AUTO:
		return "auto";
	case NETWORK_REGISTRATION_MODE_MANUAL:
		return "manual";
	case NETWORK_REGISTRATION_MODE_OFF:
		return "off";
	default:
		return "unknown";
	}
}

static inline const char *network_operator_status_to_string(int status)
{
	switch (status) {
	case OPERATOR_STATUS_AVAILABLE:
		return "available";
	case OPERATOR_STATUS_CURRENT:
		return "current";
	case OPERATOR_STATUS_FORBIDDEN:
		return "forbidden";
	}

	return "unknown";
}

static char **network_operator_technologies(struct network_operator_data *opd)
{
	unsigned int ntechs = 0;
	char **techs;
	unsigned int i;

	for (i = 0; i < sizeof(opd->techs); i++) {
		if (opd->techs & (1 << i))
			ntechs += 1;
	}

	techs = g_new0(char *, ntechs + 1);
	ntechs = 0;

	for (i = 0; i < sizeof(opd->techs); i++) {
		if (!(opd->techs & (1 << i)))
			continue;

		techs[ntechs++] = g_strdup(registration_tech_to_string(i));
	}

	return techs;
}

static void set_registration_mode(struct ofono_netreg *netreg, int mode)
{
	DBusConnection *conn;
	const char *strmode;
	const char *path;

	if (netreg->mode == mode)
		return;

	netreg->mode = mode;

	if (netreg->settings) {
		g_key_file_set_integer(netreg->settings, SETTINGS_GROUP,
					"Mode", netreg->mode);
		storage_sync(netreg->imsi, SETTINGS_STORE, netreg->settings);
	}

	strmode = registration_mode_to_string(mode);

	conn = ofono_dbus_get_connection();
	path = __ofono_atom_get_path(netreg->atom);

	ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"Mode", DBUS_TYPE_STRING, &strmode);
}

static void register_callback(const struct ofono_error *error, void *data)
{
	struct ofono_netreg *netreg = data;
	DBusConnection *conn = ofono_dbus_get_connection();
	DBusMessage *reply;

	if (!netreg->pending)
		goto out;

	if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
		reply = dbus_message_new_method_return(netreg->pending);
	else
		reply = __ofono_error_failed(netreg->pending);

	g_dbus_send_message(conn, reply);

	dbus_message_unref(netreg->pending);
	netreg->pending = NULL;

out:
	if (netreg->driver->registration_status)
		netreg->driver->registration_status(netreg,
					registration_status_callback, netreg);
}

static void init_register(const struct ofono_error *error, void *data)
{
	struct ofono_netreg *netreg = data;

	if (netreg->driver->registration_status)
		netreg->driver->registration_status(netreg,
					registration_status_callback, netreg);
}

/* Must use g_strfreev on network_operators */
static void network_operator_populate_registered(struct ofono_netreg *netreg,
						char ***network_operators)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	char **children;
	int i;
	int prefix_len;
	int num_children;
	GSList *l;
	char path[256];
	char mnc[OFONO_MAX_MNC_LENGTH + 1];
	char mcc[OFONO_MAX_MCC_LENGTH + 1];
	int op_path_len;

	prefix_len = snprintf(path, sizeof(path), "%s/operator",
				__ofono_atom_get_path(netreg->atom));

	if (!dbus_connection_list_registered(conn, path, &children)) {
		DBG("Unable to obtain registered NetworkOperator(s)");
		*network_operators = g_try_new0(char *, 1);
		return;
	}

	for (i = 0; children[i]; i++)
		;

	num_children = i;

	*network_operators = g_try_new0(char *, num_children + 1);

	/* Enough to store '/' + MCC + MNC + null */
	op_path_len = prefix_len;
	op_path_len += OFONO_MAX_MCC_LENGTH + OFONO_MAX_MNC_LENGTH + 2;

	/* Quoting 27.007: "The list of operators shall be in order: home
	 * network, networks referenced in SIM or active application in the
	 * UICC (GSM or USIM) in the following order: HPLMN selector, User
	 * controlled PLMN selector, Operator controlled PLMN selector and
	 * PLMN selector (in the SIM or GSM application), and other networks."
	 * Thus we must make sure we return the list in the same order,
	 * if possible.  Luckily the operator_list is stored in order already
	 */
	i = 0;
	for (l = netreg->operator_list; l; l = l->next) {
		struct network_operator_data *opd = l->data;
		int j;

		for (j = 0; children[j]; j++) {
			sscanf(children[j], "%3[0-9]%[0-9]", mcc, mnc);
			if (!strcmp(opd->mcc, mcc) && !strcmp(opd->mnc, mnc)) {
				(*network_operators)[i] =
					g_try_new(char, op_path_len);
				snprintf((*network_operators)[i], op_path_len,
						"%s/%s", path, children[j]);
				++i;
			}
		}
	}

	dbus_free_string_array(children);
}

static struct network_operator_data *
	network_operator_create(const struct ofono_network_operator *op)
{
	struct network_operator_data *opd;

	opd = g_new0(struct network_operator_data, 1);

	memcpy(&opd->name, op->name, sizeof(opd->name));
	memcpy(&opd->mcc, op->mcc, sizeof(opd->mcc));
	memcpy(&opd->mnc, op->mnc, sizeof(opd->mnc));

	opd->status = op->status;

	if (op->tech != -1)
		opd->techs |= 1 << op->tech;

	return opd;
}

static void network_operator_destroy(gpointer userdata)
{
	struct network_operator_data *op = userdata;

	g_free(op);
}

static gint network_operator_compare(gconstpointer a, gconstpointer b)
{
	const struct network_operator_data *opda = a;
	const struct ofono_network_operator *opb = b;

	int comp1;
	int comp2;

	comp1 = strcmp(opda->mcc, opb->mcc);
	comp2 = strcmp(opda->mnc, opb->mnc);

	return comp1 != 0 ? comp1 : comp2;
}

static gint network_operator_data_compare(gconstpointer a, gconstpointer b)
{
	const struct network_operator_data *opa = a;
	const struct network_operator_data *opb = b;

	int comp1;
	int comp2;

	comp1 = strcmp(opa->mcc, opb->mcc);
	comp2 = strcmp(opa->mnc, opb->mnc);

	return comp1 != 0 ? comp1 : comp2;
}

static inline const char *network_operator_build_path(struct ofono_netreg *netreg,
							const char *mcc,
							const char *mnc)
{
	static char path[256];

	snprintf(path, sizeof(path), "%s/operator/%s%s",
			__ofono_atom_get_path(netreg->atom),
			mcc, mnc);

	return path;
}

static void network_operator_emit_available_operators(struct ofono_netreg *netreg)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	const char *path = __ofono_atom_get_path(netreg->atom);
	char **network_operators;

	network_operator_populate_registered(netreg, &network_operators);

	ofono_dbus_signal_array_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"Operators",
					DBUS_TYPE_OBJECT_PATH,
					&network_operators);

	g_strfreev(network_operators);
}

static void set_network_operator_status(struct network_operator_data *opd,
					int status)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	struct ofono_netreg *netreg = opd->netreg;
	const char *status_str;
	const char *path;

	if (opd->status == status)
		return;

	opd->status = status;

	/* Don't emit for the case where only operator name is reported */
	if (opd->mcc[0] == '\0' && opd->mnc[0] == '\0')
		return;

	status_str = network_operator_status_to_string(status);
	path = network_operator_build_path(netreg, opd->mcc, opd->mnc);

	ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_OPERATOR_INTERFACE,
					"Status", DBUS_TYPE_STRING,
					&status_str);
}

static void set_network_operator_techs(struct network_operator_data *opd,
					unsigned int techs)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	struct ofono_netreg *netreg = opd->netreg;
	char **technologies;
	const char *path;

	if (opd->techs == techs)
		return;

	opd->techs = techs;
	technologies = network_operator_technologies(opd);
	path = network_operator_build_path(netreg, opd->mcc, opd->mnc);

	ofono_dbus_signal_array_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"Technologies", DBUS_TYPE_STRING,
					&technologies);
	g_strfreev(technologies);
}

static char *get_operator_display_name(struct ofono_netreg *netreg)
{
	struct network_operator_data *opd = netreg->current_operator;
	const char *plmn;
	static char name[1024];
	static char mccmnc[OFONO_MAX_MCC_LENGTH + OFONO_MAX_MNC_LENGTH + 1];
	int len = sizeof(name);
	int home_or_spdi;

	/* The name displayed to user depends on whether we're in a home
	 * PLMN or roaming and on configuration bits from the SIM, all
	 * together there are four cases to consider.  */

	if (!opd) {
		g_strlcpy(name, "", len);
		return name;
	}

	plmn = opd->name;

	/*
	 * This is a fallback on some really broken hardware which do not
	 * report the COPS name
	 */
	if (plmn[0] == '\0') {
		snprintf(mccmnc, sizeof(mccmnc), "%s%s", opd->mcc, opd->mnc);
		plmn = mccmnc;
	}

	if (opd->eons_info && opd->eons_info->longname)
		plmn = opd->eons_info->longname;

	if (!netreg->spname || strlen(netreg->spname) == 0) {
		g_strlcpy(name, plmn, len);
		return name;
	}

	if (netreg->status == NETWORK_REGISTRATION_STATUS_REGISTERED)
		home_or_spdi = TRUE;
	else
		home_or_spdi = sim_spdi_lookup(netreg->spdi,
							opd->mcc, opd->mnc);

	if (home_or_spdi)
		if (netreg->flags & NETWORK_REGISTRATION_FLAG_HOME_SHOW_PLMN)
			/* Case 1 */
			snprintf(name, len, "%s (%s)", netreg->spname, plmn);
		else
			/* Case 2 */
			snprintf(name, len, "%s", netreg->spname);
	else
		if (netreg->flags & NETWORK_REGISTRATION_FLAG_ROAMING_SHOW_SPN)
			/* Case 3 */
			snprintf(name, len, "%s (%s)", netreg->spname, plmn);
		else
			/* Case 4 */
			snprintf(name, len, "%s", plmn);

	return name;
}

static void set_network_operator_name(struct network_operator_data *opd,
					const char *name)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	struct ofono_netreg *netreg = opd->netreg;
	const char *path;
	const char *operator;

	if (name[0] == '\0')
		return;

	if (!strncmp(opd->name, name, OFONO_MAX_OPERATOR_NAME_LENGTH))
		return;

	strncpy(opd->name, name, OFONO_MAX_OPERATOR_NAME_LENGTH);
	opd->name[OFONO_MAX_OPERATOR_NAME_LENGTH] = '\0';

	/* If we have Enhanced Operator Name info on the SIM, we always use
	 * that, so do not need to emit the signal here
	 */
	if (opd->eons_info && opd->eons_info->longname)
		return;

	if (opd == netreg->current_operator) {
		const char *path = __ofono_atom_get_path(netreg->atom);

		operator = get_operator_display_name(netreg);

		ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"Name", DBUS_TYPE_STRING,
					&operator);
	}

	/* Don't emit when only operator name is reported */
	if (opd->mcc[0] == '\0' && opd->mnc[0] == '\0')
		return;

	path = network_operator_build_path(netreg, opd->mcc, opd->mnc);

	ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_OPERATOR_INTERFACE,
					"Name", DBUS_TYPE_STRING, &name);
}

static void set_network_operator_eons_info(struct network_operator_data *opd,
				const struct sim_eons_operator_info *eons_info)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	struct ofono_netreg *netreg = opd->netreg;
	const struct sim_eons_operator_info *old_eons_info = opd->eons_info;
	const char *path;
	const char *oldname;
	const char *newname;
	const char *oldinfo;
	const char *newinfo;

	if (!old_eons_info && !eons_info)
		return;

	path = network_operator_build_path(netreg, opd->mcc, opd->mnc);
	opd->eons_info = eons_info;

	if (old_eons_info && old_eons_info->longname)
		oldname = old_eons_info->longname;
	else
		oldname = opd->name;

	if (eons_info && eons_info->longname)
		newname = eons_info->longname;
	else
		newname = opd->name;

	if (oldname != newname && strcmp(oldname, newname)) {
		ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_OPERATOR_INTERFACE,
					"Name", DBUS_TYPE_STRING, &newname);

		if (opd == netreg->current_operator) {
			const char *npath = __ofono_atom_get_path(netreg->atom);
			const char *operator = get_operator_display_name(netreg);

			ofono_dbus_signal_property_changed(conn, npath,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"Name", DBUS_TYPE_STRING,
					&operator);
		}
	}

	if (old_eons_info && old_eons_info->info)
		oldinfo = old_eons_info->info;
	else
		oldinfo = "";

	if (eons_info && eons_info->info)
		newinfo = eons_info->info;
	else
		newinfo = "";

	if (oldinfo != newinfo && strcmp(oldinfo, newinfo))
		ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_OPERATOR_INTERFACE,
					"AdditionalInformation",
					DBUS_TYPE_STRING, &newinfo);
}

static DBusMessage *network_operator_get_properties(DBusConnection *conn,
							DBusMessage *msg,
							void *data)
{
	struct network_operator_data *opd = data;
	DBusMessage *reply;
	DBusMessageIter iter;
	DBusMessageIter dict;
	char mccmnc[OFONO_MAX_MCC_LENGTH + OFONO_MAX_MNC_LENGTH + 1];

	const char *name = opd->name;
	const char *status =
		network_operator_status_to_string(opd->status);

	if (opd->eons_info && opd->eons_info->longname)
		name = opd->eons_info->longname;

	reply = dbus_message_new_method_return(msg);
	if (!reply)
		return NULL;

	dbus_message_iter_init_append(reply, &iter);

	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
					OFONO_PROPERTIES_ARRAY_SIGNATURE,
					&dict);

	if (name[0] == '\0') {
		snprintf(mccmnc, sizeof(mccmnc), "%s%s", opd->mcc, opd->mnc);
		name = mccmnc;
	}

	ofono_dbus_dict_append(&dict, "Name", DBUS_TYPE_STRING, &name);

	ofono_dbus_dict_append(&dict, "Status", DBUS_TYPE_STRING, &status);

	if (*opd->mcc != '\0') {
		const char *mcc = opd->mcc;
		ofono_dbus_dict_append(&dict, "MobileCountryCode",
					DBUS_TYPE_STRING, &mcc);
	}

	if (*opd->mnc != '\0') {
		const char *mnc = opd->mnc;
		ofono_dbus_dict_append(&dict, "MobileNetworkCode",
					DBUS_TYPE_STRING, &mnc);
	}

	if (opd->techs != 0) {
		char **technologies = network_operator_technologies(opd);

		ofono_dbus_dict_append_array(&dict, "Technologies",
						DBUS_TYPE_STRING,
						&technologies);

		g_strfreev(technologies);
	}

	if (opd->eons_info && opd->eons_info->info) {
		const char *additional = opd->eons_info->info;

		ofono_dbus_dict_append(&dict, "AdditionalInformation",
					DBUS_TYPE_STRING, &additional);
	}

	dbus_message_iter_close_container(&iter, &dict);

	return reply;
}

static DBusMessage *network_operator_register(DBusConnection *conn,
						DBusMessage *msg, void *data)
{
	struct network_operator_data *opd = data;
	struct ofono_netreg *netreg = opd->netreg;

	if (netreg->pending)
		return __ofono_error_busy(msg);

	if (netreg->driver->register_manual == NULL)
		return __ofono_error_not_implemented(msg);

	netreg->pending = dbus_message_ref(msg);

	netreg->driver->register_manual(netreg, opd->mcc, opd->mnc,
					register_callback, netreg);

	set_registration_mode(netreg, NETWORK_REGISTRATION_MODE_MANUAL);

	return NULL;
}

static GDBusMethodTable network_operator_methods[] = {
	{ "GetProperties",	"",	"a{sv}",	network_operator_get_properties },
	{ "Register",		"",	"",		network_operator_register,
							G_DBUS_METHOD_FLAG_ASYNC },
	{ }
};

static GDBusSignalTable network_operator_signals[] = {
	{ "PropertyChanged",	"sv" },
	{ }
};

static gboolean network_operator_dbus_register(struct ofono_netreg *netreg,
					struct network_operator_data *opd)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	const char *path;

	path = network_operator_build_path(netreg, opd->mcc, opd->mnc);

	if (!g_dbus_register_interface(conn, path,
					OFONO_NETWORK_OPERATOR_INTERFACE,
					network_operator_methods,
					network_operator_signals,
					NULL, opd,
					network_operator_destroy)) {
		ofono_error("Could not register NetworkOperator %s", path);
		return FALSE;
	}

	opd->netreg = netreg;
	opd->eons_info = NULL;

	if (netreg->eons)
		opd->eons_info = sim_eons_lookup(netreg->eons,
							opd->mcc, opd->mnc);

	return TRUE;
}

static gboolean network_operator_dbus_unregister(struct ofono_netreg *netreg,
						struct network_operator_data *opd)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	const char *path;

	path = network_operator_build_path(netreg, opd->mcc, opd->mnc);

	return g_dbus_unregister_interface(conn, path,
					OFONO_NETWORK_OPERATOR_INTERFACE);
}

static DBusMessage *network_get_properties(DBusConnection *conn,
						DBusMessage *msg, void *data)
{
	struct ofono_netreg *netreg = data;
	DBusMessage *reply;
	DBusMessageIter iter;
	DBusMessageIter dict;

	const char *status = registration_status_to_string(netreg->status);
	const char *operator;
	const char *mode = registration_mode_to_string(netreg->mode);

	char **network_operators;

	reply = dbus_message_new_method_return(msg);
	if (!reply)
		return NULL;

	dbus_message_iter_init_append(reply, &iter);

	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
					OFONO_PROPERTIES_ARRAY_SIGNATURE,
					&dict);

	ofono_dbus_dict_append(&dict, "Status", DBUS_TYPE_STRING, &status);
	ofono_dbus_dict_append(&dict, "Mode", DBUS_TYPE_STRING, &mode);

	if (netreg->location != -1) {
		dbus_uint16_t location = netreg->location;
		ofono_dbus_dict_append(&dict, "LocationAreaCode",
					DBUS_TYPE_UINT16, &location);
	}

	if (netreg->cellid != -1) {
		dbus_uint32_t cellid = netreg->cellid;
		ofono_dbus_dict_append(&dict, "CellId",
					DBUS_TYPE_UINT32, &cellid);
	}

	if (netreg->technology != -1) {
		const char *technology =
			registration_tech_to_string(netreg->technology);

		ofono_dbus_dict_append(&dict, "Technology", DBUS_TYPE_STRING,
					&technology);
	}

	operator = get_operator_display_name(netreg);
	ofono_dbus_dict_append(&dict, "Name", DBUS_TYPE_STRING, &operator);

	network_operator_populate_registered(netreg, &network_operators);

	ofono_dbus_dict_append_array(&dict, "Operators",
					DBUS_TYPE_OBJECT_PATH,
					&network_operators);

	g_strfreev(network_operators);

	if (netreg->signal_strength != -1) {
		dbus_uint16_t strength = netreg->signal_strength;
		ofono_dbus_dict_append(&dict, "Strength", DBUS_TYPE_UINT16,
					&strength);
	}

	if (netreg->base_station)
		ofono_dbus_dict_append(&dict, "BaseStation", DBUS_TYPE_STRING,
					&netreg->base_station);

	dbus_message_iter_close_container(&iter, &dict);

	return reply;
}

static DBusMessage *network_register(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	struct ofono_netreg *netreg = data;

	if (netreg->pending)
		return __ofono_error_busy(msg);

	if (netreg->driver->register_auto == NULL)
		return __ofono_error_not_implemented(msg);

	netreg->pending = dbus_message_ref(msg);

	netreg->driver->register_auto(netreg, register_callback, netreg);

	set_registration_mode(netreg, NETWORK_REGISTRATION_MODE_AUTO);

	return NULL;
}

static DBusMessage *network_deregister(DBusConnection *conn,
					DBusMessage *msg, void *data)
{
	struct ofono_netreg *netreg = data;

	if (netreg->pending)
		return __ofono_error_busy(msg);

	if (netreg->driver->deregister == NULL)
		return __ofono_error_not_implemented(msg);

	netreg->pending = dbus_message_ref(msg);

	netreg->driver->deregister(netreg, register_callback, netreg);

	set_registration_mode(netreg, NETWORK_REGISTRATION_MODE_OFF);

	return NULL;
}

static DBusMessage *network_propose_scan(DBusConnection *conn,
						DBusMessage *msg, void *data)
{
	struct ofono_netreg *netreg = data;

	if (netreg->pending)
		return __ofono_error_busy(msg);

	if (netreg->driver->list_operators == NULL)
		return __ofono_error_not_implemented(msg);

	netreg->pending = dbus_message_ref(msg);

	netreg->driver->list_operators(netreg, operator_list_callback, netreg);

	return NULL;
}

static GDBusMethodTable network_registration_methods[] = {
	{ "GetProperties",	"",	"a{sv}",	network_get_properties	},
	{ "Register",		"",	"",		network_register,
							G_DBUS_METHOD_FLAG_ASYNC },
	{ "Deregister",		"",	"",		network_deregister,
							G_DBUS_METHOD_FLAG_ASYNC },
	{ "ProposeScan",	"",	"ao",		network_propose_scan,
							G_DBUS_METHOD_FLAG_ASYNC },
	{ }
};

static GDBusSignalTable network_registration_signals[] = {
	{ "PropertyChanged",	"sv" },
	{ }
};

static void set_registration_status(struct ofono_netreg *netreg, int status)
{
	const char *str_status = registration_status_to_string(status);
	const char *path = __ofono_atom_get_path(netreg->atom);
	DBusConnection *conn = ofono_dbus_get_connection();

	netreg->status = status;

	ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"Status", DBUS_TYPE_STRING,
					&str_status);
}

static void set_registration_location(struct ofono_netreg *netreg, int lac)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	const char *path = __ofono_atom_get_path(netreg->atom);
	dbus_uint16_t dbus_lac = lac;

	if (lac > 0xffff)
		return;

	netreg->location = lac;

	if (netreg->location == -1)
		return;

	ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"LocationAreaCode",
					DBUS_TYPE_UINT16, &dbus_lac);
}

static void set_registration_cellid(struct ofono_netreg *netreg, int ci)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	const char *path = __ofono_atom_get_path(netreg->atom);
	dbus_uint32_t dbus_ci = ci;

	netreg->cellid = ci;

	if (netreg->cellid == -1)
		return;

	ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"CellId", DBUS_TYPE_UINT32, &dbus_ci);
}

static void set_registration_technology(struct ofono_netreg *netreg, int tech)
{
	const char *tech_str = registration_tech_to_string(tech);
	DBusConnection *conn = ofono_dbus_get_connection();
	const char *path = __ofono_atom_get_path(netreg->atom);

	netreg->technology = tech;

	if (netreg->technology == -1)
		return;

	ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"Technology", DBUS_TYPE_STRING,
					&tech_str);
}

void __ofono_netreg_set_base_station_name(struct ofono_netreg *netreg,
						const char *name)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	const char *path = __ofono_atom_get_path(netreg->atom);
	const char *base_station = name ? name : "";

	/* Cell ID changed, but we don't have a cell name, nothing to do */
	if (netreg->base_station == NULL && name == NULL)
		return;

	if (netreg->base_station)
		g_free(netreg->base_station);

	if (name == NULL) {
		netreg->base_station = NULL;

		/* We just got unregistered, set name to NULL
		 * but don't emit signal */
		if (netreg->current_operator == NULL)
			return;
	} else {
		netreg->base_station = g_strdup(name);
	}

	ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"BaseStation", DBUS_TYPE_STRING,
					&base_station);
}

unsigned int __ofono_netreg_add_status_watch(struct ofono_netreg *netreg,
				ofono_netreg_status_notify_cb_t notify,
				void *data, ofono_destroy_func destroy)
{
	struct ofono_watchlist_item *item;

	DBG("%p", netreg);

	if (netreg == NULL)
		return 0;

	if (notify == NULL)
		return 0;

	item = g_new0(struct ofono_watchlist_item, 1);

	item->notify = notify;
	item->destroy = destroy;
	item->notify_data = data;

	return __ofono_watchlist_add_item(netreg->status_watches, item);
}

gboolean __ofono_netreg_remove_status_watch(struct ofono_netreg *netreg,
						unsigned int id)
{
	DBG("%p", netreg);

	return __ofono_watchlist_remove_item(netreg->status_watches, id);
}

static void notify_status_watches(struct ofono_netreg *netreg)
{
	struct ofono_watchlist_item *item;
	GSList *l;
	ofono_netreg_status_notify_cb_t notify;
	const char *mcc = NULL;
	const char *mnc = NULL;

	if (netreg->current_operator) {
		mcc = netreg->current_operator->mcc;
		mnc = netreg->current_operator->mnc;
	}

	for (l = netreg->status_watches->items; l; l = l->next) {
		item = l->data;
		notify = item->notify;

		notify(netreg->status, netreg->location, netreg->cellid,
			netreg->technology, mcc, mnc, item->notify_data);
	}
}

void ofono_netreg_status_notify(struct ofono_netreg *netreg, int status,
			int lac, int ci, int tech)
{
	if (!netreg)
		return;

	if (netreg->status != status)
		set_registration_status(netreg, status);

	if (netreg->location != lac)
		set_registration_location(netreg, lac);

	if (netreg->cellid != ci)
		set_registration_cellid(netreg, ci);

	if (netreg->technology != tech)
		set_registration_technology(netreg, tech);

	if (netreg->status == NETWORK_REGISTRATION_STATUS_REGISTERED ||
		netreg->status == NETWORK_REGISTRATION_STATUS_ROAMING) {
		if (netreg->driver->current_operator)
			netreg->driver->current_operator(netreg,
					current_operator_callback, netreg);
	} else {
		struct ofono_error error;

		error.type = OFONO_ERROR_TYPE_NO_ERROR;
		error.error = 0;

		current_operator_callback(&error, NULL, netreg);
		__ofono_netreg_set_base_station_name(netreg, NULL);

		netreg->signal_strength = -1;
	}

	notify_status_watches(netreg);
}

void ofono_netreg_time_notify(struct ofono_netreg *netreg,
				struct ofono_network_time *info)
{
	struct ofono_modem *modem = __ofono_atom_get_modem(netreg->atom);

	if (!info)
		return;

	__ofono_nettime_info_received(modem, info);
}

static GSList *compress_operator_list(const struct ofono_network_operator *list,
					int total)
{
	GSList *oplist = 0;
	GSList *o;
	int i;
	struct network_operator_data *opd;

	for (i = 0; i < total; i++) {
		o = NULL;

		if (oplist)
			o = g_slist_find_custom(oplist, &list[i],
						network_operator_compare);

		if (!o) {
			opd = network_operator_create(&list[i]);
			oplist = g_slist_prepend(oplist, opd);
		} else if (o && list[i].tech != -1) {
			opd = o->data;
			opd->techs |= 1 << list[i].tech;
		}
	}

	if (oplist)
		oplist = g_slist_reverse(oplist);

	return oplist;
}

static gboolean update_operator_list(struct ofono_netreg *netreg, int total,
					const struct ofono_network_operator *list)
{
	GSList *n = NULL;
	GSList *o;
	GSList *compressed;
	GSList *c;
	gboolean changed = FALSE;

	compressed = compress_operator_list(list, total);

	for (c = compressed; c; c = c->next) {
		struct network_operator_data *copd = c->data;

		o = g_slist_find_custom(netreg->operator_list, copd,
					network_operator_data_compare);

		if (o) { /* Update and move to a new list */
			set_network_operator_status(o->data, copd->status);
			set_network_operator_techs(o->data, copd->techs);
			set_network_operator_name(o->data, copd->name);

			n = g_slist_prepend(n, o->data);
			netreg->operator_list =
				g_slist_remove(netreg->operator_list, o->data);
		} else {
			/* New operator */
			struct network_operator_data *opd;

			opd = g_memdup(copd,
					sizeof(struct network_operator_data));

			if (!network_operator_dbus_register(netreg, opd)) {
				g_free(opd);
				continue;
			}

			n = g_slist_prepend(n, opd);
			changed = TRUE;
		}
	}

	g_slist_foreach(compressed, (GFunc)g_free, NULL);
	g_slist_free(compressed);

	if (n)
		n = g_slist_reverse(n);

	if (netreg->operator_list)
		changed = TRUE;

	for (o = netreg->operator_list; o; o = o->next)
		network_operator_dbus_unregister(netreg, o->data);

	g_slist_free(netreg->operator_list);

	netreg->operator_list = n;

	return changed;
}

static void operator_list_callback(const struct ofono_error *error, int total,
					const struct ofono_network_operator *list,
					void *data)
{
	struct ofono_netreg *netreg = data;
	DBusMessage *reply;
	char **network_operators;
	DBusMessageIter iter;
	DBusMessageIter array_iter;
	int i;
	gboolean need_to_emit;

	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
		DBG("Error occurred during operator list");
		__ofono_dbus_pending_reply(&netreg->pending,
					__ofono_error_failed(netreg->pending));
		return;
	}

	need_to_emit = update_operator_list(netreg, total, list);

	reply = dbus_message_new_method_return(netreg->pending);
	network_operator_populate_registered(netreg, &network_operators);

	dbus_message_iter_init_append(reply, &iter);
	dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
		DBUS_TYPE_OBJECT_PATH_AS_STRING, &array_iter);

	for (i = 0; network_operators[i]; i++)
		dbus_message_iter_append_basic(&array_iter,
			DBUS_TYPE_OBJECT_PATH, &network_operators[i]);

	dbus_message_iter_close_container(&iter, &array_iter);
	__ofono_dbus_pending_reply(&netreg->pending, reply);

	g_strfreev(network_operators);

	if (need_to_emit)
		network_operator_emit_available_operators(netreg);
}

static void current_operator_callback(const struct ofono_error *error,
				const struct ofono_network_operator *current,
				void *data)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	struct ofono_netreg *netreg = data;
	const char *path = __ofono_atom_get_path(netreg->atom);
	GSList *op = NULL;
	const char *operator;

	DBG("%p, %p", netreg, netreg->current_operator);

	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
		DBG("Error during current operator");
		return;
	}

	if (!netreg->current_operator && !current)
		return;

	/* We got a new network operator, reset the previous one's status */
	/* It will be updated properly later */
	if (netreg->current_operator &&
		(!current ||
			network_operator_compare(netreg->current_operator, current)))
		set_network_operator_status(netreg->current_operator,
						OPERATOR_STATUS_AVAILABLE);

	if (current)
		op = g_slist_find_custom(netreg->operator_list, current,
					network_operator_compare);

	if (op) {
		struct network_operator_data *opd = op->data;
		unsigned int techs = opd->techs;

		if (current->tech != -1) {
			techs |= 1 << current->tech;
			set_network_operator_techs(opd, techs);
		}

		set_network_operator_status(opd, OPERATOR_STATUS_CURRENT);
		set_network_operator_name(opd, current->name);

		if (netreg->current_operator == op->data)
			return;

		netreg->current_operator = op->data;
		goto emit;
	}

	if (current) {
		struct network_operator_data *opd;

		opd = network_operator_create(current);

		if (opd->mcc[0] != '\0' && opd->mnc[0] != '\0' &&
				!network_operator_dbus_register(netreg, opd)) {
			g_free(opd);
			return;
		}

		netreg->current_operator = opd;
		netreg->operator_list = g_slist_append(netreg->operator_list,
							opd);

		if (opd->mcc[0] != '\0' && opd->mnc[0] != '\0')
			network_operator_emit_available_operators(netreg);
	} else {
		/* We don't free this here because operator is registered */
		/* Taken care of elsewhere */
		netreg->current_operator = NULL;
	}

emit:
	operator = get_operator_display_name(netreg);

	ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"Name", DBUS_TYPE_STRING,
					&operator);

	notify_status_watches(netreg);
}

static void registration_status_callback(const struct ofono_error *error,
					int status, int lac, int ci, int tech,
					void *data)
{
	struct ofono_netreg *netreg = data;

	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
		DBG("Error during registration status query");
		return;
	}

	ofono_netreg_status_notify(netreg, status, lac, ci, tech);
}

static void init_registration_status(const struct ofono_error *error,
					int status, int lac, int ci, int tech,
					void *data)
{
	struct ofono_netreg *netreg = data;

	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
		DBG("Error during registration status query");
		return;
	}

	ofono_netreg_status_notify(netreg, status, lac, ci, tech);

	/* Bootstrap our signal strength value without waiting for the
	 * stack to report it
	 */
	if (netreg->status == NETWORK_REGISTRATION_STATUS_REGISTERED ||
		netreg->status == NETWORK_REGISTRATION_STATUS_ROAMING) {
		if (netreg->driver->strength)
			netreg->driver->strength(netreg,
					signal_strength_callback, netreg);
	}

	if (netreg->mode == NETWORK_REGISTRATION_MODE_AUTO &&
		(status == NETWORK_REGISTRATION_STATUS_NOT_REGISTERED ||
			status == NETWORK_REGISTRATION_STATUS_DENIED ||
			status == NETWORK_REGISTRATION_STATUS_UNKNOWN)) {
		if (netreg->driver->register_auto)
			netreg->driver->register_auto(netreg, init_register,
							netreg);
	}
}

void ofono_netreg_strength_notify(struct ofono_netreg *netreg, int strength)
{
	DBusConnection *conn = ofono_dbus_get_connection();

	if (netreg->signal_strength == strength)
		return;

	/* Theoretically we can get signal strength even when not registered
	 * to any network.  However, what do we do with it in that case?
	 */
	if (netreg->status != NETWORK_REGISTRATION_STATUS_REGISTERED &&
		netreg->status != NETWORK_REGISTRATION_STATUS_ROAMING)
		return;

	netreg->signal_strength = strength;

	if (strength != -1) {
		const char *path = __ofono_atom_get_path(netreg->atom);
		dbus_uint16_t strength = netreg->signal_strength;

		ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"Strength", DBUS_TYPE_UINT16,
					&strength);
	}
}

static void signal_strength_callback(const struct ofono_error *error,
					int strength, void *data)
{
	struct ofono_netreg *netreg = data;

	if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
		DBG("Error during signal strength query");
		return;
	}

	ofono_netreg_strength_notify(netreg, strength);
}

static void sim_opl_read_cb(int ok, int length, int record,
				const unsigned char *data,
				int record_length, void *userdata)
{
	struct ofono_netreg *netreg = userdata;
	int total;
	GSList *l;

	if (!ok) {
		if (record > 0)
			goto optimize;

		return;
	}

	if (record_length < 8 || length < record_length)
		return;

	total = length / record_length;

	sim_eons_add_opl_record(netreg->eons, data, record_length);

	if (record != total)
		return;

optimize:
	sim_eons_optimize(netreg->eons);

	for (l = netreg->operator_list; l; l = l->next) {
		struct network_operator_data *opd = l->data;
		const struct sim_eons_operator_info *eons_info;

		eons_info = sim_eons_lookup(netreg->eons, opd->mcc, opd->mnc);

		set_network_operator_eons_info(opd, eons_info);
	}
}

static void sim_pnn_read_cb(int ok, int length, int record,
				const unsigned char *data,
				int record_length, void *userdata)
{
	struct ofono_netreg *netreg = userdata;
	int total;

	if (!ok)
		goto check;

	if (length < 3 || record_length < 3 || length < record_length)
		return;

	total = length / record_length;

	if (!netreg->eons)
		netreg->eons = sim_eons_new(total);

	sim_eons_add_pnn_record(netreg->eons, record, data, record_length);

	if (record != total)
		return;

check:
	/* If PNN is not present then OPL is not useful, don't
	 * retrieve it.  If OPL is not there then PNN[1] will
	 * still be used for the HPLMN and/or EHPLMN, if PNN
	 * is present.  */
	if (netreg->eons && !sim_eons_pnn_is_empty(netreg->eons))
		ofono_sim_read(netreg->sim, SIM_EFOPL_FILEID,
				OFONO_SIM_FILE_STRUCTURE_FIXED,
				sim_opl_read_cb, netreg);
}

static void sim_spdi_read_cb(int ok, int length, int record,
				const unsigned char *data,
				int record_length, void *userdata)
{
	struct ofono_netreg *netreg = userdata;
	struct network_operator_data *current = netreg->current_operator;

	if (!ok)
		return;

	netreg->spdi = sim_spdi_new(data, length);

	if (!current)
		return;

	if (netreg->status == NETWORK_REGISTRATION_STATUS_ROAMING) {
		DBusConnection *conn = ofono_dbus_get_connection();
		const char *path = __ofono_atom_get_path(netreg->atom);
		const char *operator;

		if (!sim_spdi_lookup(netreg->spdi,
					current->mcc, current->mnc))
			return;

		operator = get_operator_display_name(netreg);

		ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"Name", DBUS_TYPE_STRING,
					&operator);
	}
}

static void sim_spn_read_cb(int ok, int length, int record,
				const unsigned char *data,
				int record_length, void *userdata)
{
	struct ofono_netreg *netreg = userdata;
	unsigned char dcbyte;
	char *spn;

	if (!ok)
		return;

	dcbyte = data[0];

	/* TS 31.102 says:
	 *
	 * the string shall use:
	 *
	 * - either the SMS default 7-bit coded alphabet as defined in
	 *   TS 23.038 [5] with bit 8 set to 0. The string shall be left
	 *   justified. Unused bytes shall be set to 'FF'.
	 *
	 * - or one of the UCS2 code options defined in the annex of TS
	 *   31.101 [11].
	 *
	 * 31.101 has no such annex though.  51.101 refers to Annex B of
	 * itself which is not there either.  11.11 contains the same
	 * paragraph as 51.101 and has an Annex B which we implement.
	 */
	spn = sim_string_to_utf8(data + 1, length - 1);

	if (!spn) {
		ofono_error("EFspn read successfully, but couldn't parse");
		return;
	}

	if (strlen(spn) == 0) {
		g_free(spn);
		return;
	}

	netreg->spname = spn;
	ofono_sim_read(netreg->sim, SIM_EFSPDI_FILEID,
			OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
			sim_spdi_read_cb, netreg);

	if (dcbyte & SIM_EFSPN_DC_HOME_PLMN_BIT)
		netreg->flags |= NETWORK_REGISTRATION_FLAG_HOME_SHOW_PLMN;

	if (!(dcbyte & SIM_EFSPN_DC_ROAMING_SPN_BIT))
		netreg->flags |= NETWORK_REGISTRATION_FLAG_ROAMING_SHOW_SPN;

	if (netreg->current_operator) {
		DBusConnection *conn = ofono_dbus_get_connection();
		const char *path = __ofono_atom_get_path(netreg->atom);
		const char *operator;

		operator = get_operator_display_name(netreg);

		ofono_dbus_signal_property_changed(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					"Name", DBUS_TYPE_STRING,
					&operator);
	}
}

int ofono_netreg_get_location(struct ofono_netreg *netreg)
{
	if (netreg == NULL)
		return -1;

	return netreg->location;
}

int ofono_netreg_get_cellid(struct ofono_netreg *netreg)
{
	if (netreg == NULL)
		return -1;

	return netreg->cellid;
}

int ofono_netreg_get_status(struct ofono_netreg *netreg)
{
	if (netreg == NULL)
		return -1;

	return netreg->status;
}

int ofono_netreg_get_technology(struct ofono_netreg *netreg)
{
	if (netreg == NULL)
		return -1;

	return netreg->technology;
}

const char *ofono_netreg_get_mcc(struct ofono_netreg *netreg)
{
	if (netreg == NULL)
		return NULL;

	if (netreg->current_operator == NULL)
		return NULL;

	return netreg->current_operator->mcc;
}

const char *ofono_netreg_get_mnc(struct ofono_netreg *netreg)
{
	if (netreg == NULL)
		return NULL;

	if (netreg->current_operator == NULL)
		return NULL;

	return netreg->current_operator->mnc;
}

int ofono_netreg_driver_register(const struct ofono_netreg_driver *d)
{
	DBG("driver: %p, name: %s", d, d->name);

	if (d->probe == NULL)
		return -EINVAL;

	g_drivers = g_slist_prepend(g_drivers, (void *)d);

	return 0;
}

void ofono_netreg_driver_unregister(const struct ofono_netreg_driver *d)
{
	DBG("driver: %p, name: %s", d, d->name);

	g_drivers = g_slist_remove(g_drivers, (void *)d);
}

static void netreg_unregister(struct ofono_atom *atom)
{
	struct ofono_netreg *netreg = __ofono_atom_get_data(atom);
	DBusConnection *conn = ofono_dbus_get_connection();
	struct ofono_modem *modem = __ofono_atom_get_modem(atom);
	const char *path = __ofono_atom_get_path(atom);
	GSList *l;

	__ofono_watchlist_free(netreg->status_watches);
	netreg->status_watches = NULL;

	for (l = netreg->operator_list; l; l = l->next) {
		struct network_operator_data *opd = l->data;

		if (opd->mcc[0] == '\0' && opd->mnc[0] == '\0')
			continue;

		network_operator_dbus_unregister(netreg, l->data);
	}

	g_slist_free(netreg->operator_list);
	netreg->operator_list = NULL;

	if (netreg->base_station) {
		g_free(netreg->base_station);
		netreg->base_station = NULL;
	}

	if (netreg->settings) {
		storage_close(netreg->imsi, SETTINGS_STORE,
				netreg->settings, TRUE);

		g_free(netreg->imsi);
		netreg->imsi = NULL;
		netreg->settings = NULL;
	}

	g_dbus_unregister_interface(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE);
	ofono_modem_remove_interface(modem,
					OFONO_NETWORK_REGISTRATION_INTERFACE);
}

static void netreg_remove(struct ofono_atom *atom)
{
	struct ofono_netreg *netreg = __ofono_atom_get_data(atom);

	DBG("atom: %p", atom);

	if (netreg == NULL)
		return;

	if (netreg->driver && netreg->driver->remove)
		netreg->driver->remove(netreg);

	if (netreg->eons)
		sim_eons_free(netreg->eons);

	if (netreg->spdi)
		sim_spdi_free(netreg->spdi);

	if (netreg->spname)
		g_free(netreg->spname);

	g_free(netreg);
}

struct ofono_netreg *ofono_netreg_create(struct ofono_modem *modem,
					unsigned int vendor,
					const char *driver,
					void *data)
{
	struct ofono_netreg *netreg;
	GSList *l;

	if (driver == NULL)
		return NULL;

	netreg = g_try_new0(struct ofono_netreg, 1);

	if (netreg == NULL)
		return NULL;

	netreg->status = NETWORK_REGISTRATION_STATUS_UNKNOWN;
	netreg->location = -1;
	netreg->cellid = -1;
	netreg->technology = -1;
	netreg->signal_strength = -1;

	netreg->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_NETREG,
						netreg_remove, netreg);

	for (l = g_drivers; l; l = l->next) {
		const struct ofono_netreg_driver *drv = l->data;

		if (g_strcmp0(drv->name, driver))
			continue;

		if (drv->probe(netreg, vendor, data) < 0)
			continue;

		netreg->driver = drv;
		break;
	}

	return netreg;
}

static void netreg_load_settings(struct ofono_netreg *netreg)
{
	const char *imsi;
	int mode;

	imsi = ofono_sim_get_imsi(netreg->sim);

	if (!imsi)
		return;

	netreg->settings = storage_open(imsi, SETTINGS_STORE);

	if (netreg->settings == NULL)
		return;

	netreg->imsi = g_strdup(imsi);

	mode = g_key_file_get_integer(netreg->settings, SETTINGS_GROUP,
					"Mode", NULL);

	if (mode >= 0 && mode <= 2)
		netreg->mode = mode;

	g_key_file_set_integer(netreg->settings, SETTINGS_GROUP,
				"Mode", netreg->mode);
}

void ofono_netreg_register(struct ofono_netreg *netreg)
{
	DBusConnection *conn = ofono_dbus_get_connection();
	struct ofono_modem *modem = __ofono_atom_get_modem(netreg->atom);
	const char *path = __ofono_atom_get_path(netreg->atom);
	struct ofono_atom *sim_atom;

	if (!g_dbus_register_interface(conn, path,
					OFONO_NETWORK_REGISTRATION_INTERFACE,
					network_registration_methods,
					network_registration_signals,
					NULL, netreg, NULL)) {
		ofono_error("Could not create %s interface",
				OFONO_NETWORK_REGISTRATION_INTERFACE);

		return;
	}

	netreg->status_watches = __ofono_watchlist_new(g_free);

	ofono_modem_add_interface(modem, OFONO_NETWORK_REGISTRATION_INTERFACE);

	if (netreg->driver->registration_status)
		netreg->driver->registration_status(netreg,
					init_registration_status, netreg);

	sim_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_SIM);

	if (sim_atom) {
		/* Assume that if sim atom exists, it is ready */
		netreg->sim = __ofono_atom_get_data(sim_atom);

		netreg_load_settings(netreg);

		ofono_sim_read(netreg->sim, SIM_EFPNN_FILEID,
				OFONO_SIM_FILE_STRUCTURE_FIXED,
				sim_pnn_read_cb, netreg);
		ofono_sim_read(netreg->sim, SIM_EFSPN_FILEID,
				OFONO_SIM_FILE_STRUCTURE_TRANSPARENT,
				sim_spn_read_cb, netreg);
	}

	__ofono_atom_register(netreg->atom, netreg_unregister);
}

void ofono_netreg_remove(struct ofono_netreg *netreg)
{
	__ofono_atom_free(netreg->atom);
}

void ofono_netreg_set_data(struct ofono_netreg *netreg, void *data)
{
	netreg->driver_data = data;
}

void *ofono_netreg_get_data(struct ofono_netreg *netreg)
{
	return netreg->driver_data;
}