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
|
/*
*
* 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 <sys/time.h>
#include "ofono.h"
#include "common.h"
#include "util.h"
#include "smsutil.h"
#include "storage.h"
#include "simutil.h"
#define uninitialized_var(x) x = x
#define MESSAGE_MANAGER_FLAG_CACHED 0x1
#define SETTINGS_STORE "sms"
#define SETTINGS_GROUP "Settings"
#define TXQ_MAX_RETRIES 4
static gboolean tx_next(gpointer user_data);
static GSList *g_drivers = NULL;
enum message_state {
MESSAGE_STATE_PENDING = 0,
MESSAGE_STATE_SENT,
MESSAGE_STATE_FAILED
};
struct message {
struct ofono_uuid uuid;
enum message_state state;
struct tx_queue_entry *entry;
};
struct sms_handler {
struct ofono_watchlist_item item;
int dst;
int src;
};
struct ofono_sms {
int flags;
DBusMessage *pending;
struct ofono_phone_number sca;
struct sms_assembly *assembly;
guint ref;
GQueue *txq;
guint tx_source;
struct ofono_message_waiting *mw;
unsigned int mw_watch;
struct ofono_sim *sim;
GKeyFile *settings;
char *imsi;
int bearer;
const struct ofono_sms_driver *driver;
void *driver_data;
struct ofono_atom *atom;
ofono_bool_t use_delivery_reports;
struct status_report_assembly *sr_assembly;
GHashTable *messages;
struct ofono_watchlist *text_handlers;
struct ofono_watchlist *datagram_handlers;
};
struct pending_pdu {
unsigned char pdu[176];
int tpdu_len;
int pdu_len;
};
struct tx_queue_entry {
struct pending_pdu *pdus;
unsigned char num_pdus;
unsigned char cur_pdu;
struct sms_address receiver;
struct ofono_uuid uuid;
unsigned int retry;
unsigned int flags;
ofono_sms_txq_submit_cb_t cb;
void *data;
ofono_destroy_func destroy;
};
static gboolean uuid_equal(gconstpointer v1, gconstpointer v2)
{
return memcmp(v1, v2, OFONO_SHA1_UUID_LEN) == 0;
}
static gboolean port_equal(int received, int expected)
{
return expected == -1 || received == expected;
}
static guint uuid_hash(gconstpointer v)
{
const struct ofono_uuid *uuid = v;
guint h;
memcpy(&h, uuid->uuid, sizeof(h));
return h;
}
static const char *sms_bearer_to_string(int bearer)
{
switch (bearer) {
case 0:
return "ps-only";
case 1:
return "cs-only";
case 2:
return "ps-preferred";
case 3:
return "cs-preferred";
};
return "unknown";
}
static int sms_bearer_from_string(const char *str)
{
if (g_str_equal(str, "ps-only"))
return 0;
else if (g_str_equal(str, "cs-only"))
return 1;
else if (g_str_equal(str, "ps-preferred"))
return 2;
else if (g_str_equal(str, "cs-preferred"))
return 3;
return -1;
}
static const char *message_state_to_string(enum message_state s)
{
switch (s) {
case MESSAGE_STATE_PENDING:
return "pending";
case MESSAGE_STATE_SENT:
return "sent";
case MESSAGE_STATE_FAILED:
return "failed";
}
return "invalid";
}
static void append_message_properties(struct message *m, DBusMessageIter *dict)
{
const char *state;
state = message_state_to_string(m->state);
ofono_dbus_dict_append(dict, "State", DBUS_TYPE_STRING, &state);
}
static unsigned int add_sms_handler(struct ofono_watchlist *watchlist,
int dst, int src, void *notify,
void *data, ofono_destroy_func destroy)
{
struct sms_handler *handler;
if (notify == NULL)
return 0;
handler = g_try_new0(struct sms_handler, 1);
if (handler == NULL)
return 0;
handler->dst = dst;
handler->src = src;
handler->item.notify = notify;
handler->item.notify_data = data;
handler->item.destroy = destroy;
return __ofono_watchlist_add_item(watchlist,
(struct ofono_watchlist_item *) handler);
}
unsigned int __ofono_sms_text_watch_add(struct ofono_sms *sms,
ofono_sms_text_notify_cb_t cb,
void *data, ofono_destroy_func destroy)
{
if (sms == NULL)
return 0;
DBG("%p", sms);
return add_sms_handler(sms->text_handlers, -1, -1, cb, data, destroy);
}
gboolean __ofono_sms_text_watch_remove(struct ofono_sms *sms,
unsigned int id)
{
if (sms == NULL)
return FALSE;
DBG("%p", sms);
return __ofono_watchlist_remove_item(sms->text_handlers, id);
}
unsigned int __ofono_sms_datagram_watch_add(struct ofono_sms *sms,
ofono_sms_datagram_notify_cb_t cb,
int dst, int src, void *data,
ofono_destroy_func destroy)
{
if (sms == NULL)
return 0;
DBG("%p: dst %d, src %d", sms, dst, src);
return add_sms_handler(sms->datagram_handlers, dst, src, cb, data,
destroy);
}
gboolean __ofono_sms_datagram_watch_remove(struct ofono_sms *sms,
unsigned int id)
{
if (sms == NULL)
return FALSE;
DBG("%p", sms);
return __ofono_watchlist_remove_item(sms->datagram_handlers, id);
}
static DBusMessage *message_get_properties(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct message *m = data;
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter dict;
reply = dbus_message_new_method_return(msg);
if (reply == NULL)
return NULL;
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
OFONO_PROPERTIES_ARRAY_SIGNATURE,
&dict);
append_message_properties(m, &dict);
dbus_message_iter_close_container(&iter, &dict);
return reply;
}
static GDBusMethodTable message_methods[] = {
{ "GetProperties", "", "a{sv}", message_get_properties },
{ }
};
static GDBusSignalTable message_signals[] = {
{ "PropertyChanged", "sv" },
{ }
};
static struct message *message_create(const struct ofono_uuid *uuid)
{
struct message *v;
if (uuid == NULL)
return NULL;
v = g_try_new0(struct message, 1);
if (v == NULL)
return NULL;
memcpy(&v->uuid, uuid, sizeof(*uuid));
return v;
}
static void message_destroy(gpointer userdata)
{
struct message *m = userdata;
g_free(m);
}
const char *__ofono_sms_message_path_from_uuid(struct ofono_sms *sms,
const struct ofono_uuid *uuid)
{
static char path[256];
snprintf(path, sizeof(path), "%s/message_%s",
__ofono_atom_get_path(sms->atom),
ofono_uuid_to_str(uuid));
return path;
}
static gboolean message_dbus_register(struct ofono_sms *sms, struct message *m)
{
DBusConnection *conn = ofono_dbus_get_connection();
const char *path;
if (m == NULL)
return FALSE;
path = __ofono_sms_message_path_from_uuid(sms, &m->uuid);
if (!g_dbus_register_interface(conn, path, OFONO_MESSAGE_INTERFACE,
message_methods, message_signals,
NULL, m, message_destroy)) {
ofono_error("Could not register Message %s", path);
message_destroy(m);
return FALSE;
}
return TRUE;
}
static gboolean message_dbus_unregister(struct ofono_sms *sms,
struct message *m)
{
DBusConnection *conn = ofono_dbus_get_connection();
const char *path = __ofono_sms_message_path_from_uuid(sms, &m->uuid);
return g_dbus_unregister_interface(conn, path,
OFONO_MESSAGE_INTERFACE);
}
static void emit_message_added(struct ofono_sms *sms, struct message *m)
{
DBusMessage *signal;
DBusMessageIter iter;
DBusMessageIter dict;
const char *path;
path = __ofono_atom_get_path(sms->atom);
signal = dbus_message_new_signal(path,
OFONO_MESSAGE_MANAGER_INTERFACE,
"MessageAdded");
if (signal == NULL)
return;
dbus_message_iter_init_append(signal, &iter);
path = __ofono_sms_message_path_from_uuid(sms, &m->uuid);
dbus_message_iter_append_basic(&iter, DBUS_TYPE_OBJECT_PATH, &path);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
OFONO_PROPERTIES_ARRAY_SIGNATURE,
&dict);
append_message_properties(m, &dict);
dbus_message_iter_close_container(&iter, &dict);
g_dbus_send_message(ofono_dbus_get_connection(), signal);
}
static void emit_message_removed(struct ofono_sms *sms, struct message *m)
{
DBusConnection *conn = ofono_dbus_get_connection();
const char *atompath = __ofono_atom_get_path(sms->atom);
const char *path = __ofono_sms_message_path_from_uuid(sms, &m->uuid);
g_dbus_emit_signal(conn, atompath, OFONO_MESSAGE_MANAGER_INTERFACE,
"MessageRemoved", DBUS_TYPE_OBJECT_PATH, &path,
DBUS_TYPE_INVALID);
}
static void message_set_state(struct ofono_sms *sms,
const struct ofono_uuid *uuid,
enum message_state new_state)
{
DBusConnection *conn = ofono_dbus_get_connection();
const char *path;
const char *state;
struct message *m;
m = g_hash_table_lookup(sms->messages, uuid);
if (m == NULL)
return;
if (m->state == new_state)
return;
m->state = new_state;
path = __ofono_sms_message_path_from_uuid(sms, uuid);
state = message_state_to_string(m->state);
ofono_dbus_signal_property_changed(conn, path,
OFONO_MESSAGE_INTERFACE,
"State", DBUS_TYPE_STRING,
&state);
if (m->state == MESSAGE_STATE_SENT ||
m->state == MESSAGE_STATE_FAILED) {
m->entry = NULL;
g_hash_table_remove(sms->messages, uuid);
emit_message_removed(sms, m);
message_dbus_unregister(sms, m);
}
}
static void set_bearer(struct ofono_sms *sms, int bearer)
{
DBusConnection *conn = ofono_dbus_get_connection();
const char *path = __ofono_atom_get_path(sms->atom);
const char *value;
if (sms->bearer == bearer)
return;
sms->bearer = bearer;
value = sms_bearer_to_string(sms->bearer);
ofono_dbus_signal_property_changed(conn, path,
OFONO_MESSAGE_MANAGER_INTERFACE,
"Bearer",
DBUS_TYPE_STRING, &value);
}
static void set_sca(struct ofono_sms *sms,
const struct ofono_phone_number *sca)
{
DBusConnection *conn = ofono_dbus_get_connection();
const char *path = __ofono_atom_get_path(sms->atom);
const char *value;
if (sms->sca.type == sca->type &&
!strcmp(sms->sca.number, sca->number))
return;
sms->sca.type = sca->type;
strncpy(sms->sca.number, sca->number, OFONO_MAX_PHONE_NUMBER_LENGTH);
sms->sca.number[OFONO_MAX_PHONE_NUMBER_LENGTH] = '\0';
value = phone_number_to_string(&sms->sca);
ofono_dbus_signal_property_changed(conn, path,
OFONO_MESSAGE_MANAGER_INTERFACE,
"ServiceCenterAddress",
DBUS_TYPE_STRING, &value);
}
static DBusMessage *generate_get_properties_reply(struct ofono_sms *sms,
DBusMessage *msg)
{
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter dict;
const char *sca;
const char *bearer;
reply = dbus_message_new_method_return(msg);
if (reply == NULL)
return NULL;
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
OFONO_PROPERTIES_ARRAY_SIGNATURE,
&dict);
sca = phone_number_to_string(&sms->sca);
ofono_dbus_dict_append(&dict, "ServiceCenterAddress", DBUS_TYPE_STRING,
&sca);
ofono_dbus_dict_append(&dict, "UseDeliveryReports", DBUS_TYPE_BOOLEAN,
&sms->use_delivery_reports);
bearer = sms_bearer_to_string(sms->bearer);
ofono_dbus_dict_append(&dict, "Bearer", DBUS_TYPE_STRING, &bearer);
dbus_message_iter_close_container(&iter, &dict);
return reply;
}
static void sms_sca_query_cb(const struct ofono_error *error,
const struct ofono_phone_number *sca,
void *data)
{
struct ofono_sms *sms = data;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
goto out;
set_sca(sms, sca);
sms->flags |= MESSAGE_MANAGER_FLAG_CACHED;
out:
if (sms->pending) {
DBusMessage *reply = generate_get_properties_reply(sms,
sms->pending);
__ofono_dbus_pending_reply(&sms->pending, reply);
}
}
static DBusMessage *sms_get_properties(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_sms *sms = data;
if (sms->pending)
return __ofono_error_busy(msg);
if (sms->driver->sca_query == NULL)
return __ofono_error_not_implemented(msg);
if (sms->flags & MESSAGE_MANAGER_FLAG_CACHED)
return generate_get_properties_reply(sms, msg);
sms->pending = dbus_message_ref(msg);
sms->driver->sca_query(sms, sms_sca_query_cb, sms);
return NULL;
}
static void bearer_set_query_callback(const struct ofono_error *error,
int bearer, void *data)
{
struct ofono_sms *sms = data;
DBusMessage *reply;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
ofono_error("Set Bearer succeeded, but query failed");
reply = __ofono_error_failed(sms->pending);
__ofono_dbus_pending_reply(&sms->pending, reply);
return;
}
reply = dbus_message_new_method_return(sms->pending);
__ofono_dbus_pending_reply(&sms->pending, reply);
set_bearer(sms, bearer);
}
static void bearer_set_callback(const struct ofono_error *error, void *data)
{
struct ofono_sms *sms = data;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
DBG("Setting Bearer failed");
__ofono_dbus_pending_reply(&sms->pending,
__ofono_error_failed(sms->pending));
return;
}
sms->driver->bearer_query(sms, bearer_set_query_callback, sms);
}
static void sca_set_query_callback(const struct ofono_error *error,
const struct ofono_phone_number *sca,
void *data)
{
struct ofono_sms *sms = data;
DBusMessage *reply;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
ofono_error("Set SCA succeeded, but query failed");
sms->flags &= ~MESSAGE_MANAGER_FLAG_CACHED;
reply = __ofono_error_failed(sms->pending);
__ofono_dbus_pending_reply(&sms->pending, reply);
return;
}
set_sca(sms, sca);
reply = dbus_message_new_method_return(sms->pending);
__ofono_dbus_pending_reply(&sms->pending, reply);
}
static void sca_set_callback(const struct ofono_error *error, void *data)
{
struct ofono_sms *sms = data;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
DBG("Setting SCA failed");
__ofono_dbus_pending_reply(&sms->pending,
__ofono_error_failed(sms->pending));
return;
}
sms->driver->sca_query(sms, sca_set_query_callback, sms);
}
static DBusMessage *sms_set_property(DBusConnection *conn, DBusMessage *msg,
void *data)
{
struct ofono_sms *sms = data;
DBusMessageIter iter;
DBusMessageIter var;
const char *property;
if (sms->pending)
return __ofono_error_busy(msg);
if (!dbus_message_iter_init(msg, &iter))
return __ofono_error_invalid_args(msg);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&iter, &property);
dbus_message_iter_next(&iter);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
return __ofono_error_invalid_args(msg);
dbus_message_iter_recurse(&iter, &var);
if (!strcmp(property, "ServiceCenterAddress")) {
const char *value;
struct ofono_phone_number sca;
if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&var, &value);
if (strlen(value) == 0 || !valid_phone_number_format(value))
return __ofono_error_invalid_format(msg);
if (sms->driver->sca_set == NULL ||
sms->driver->sca_query == NULL)
return __ofono_error_not_implemented(msg);
string_to_phone_number(value, &sca);
sms->pending = dbus_message_ref(msg);
sms->driver->sca_set(sms, &sca, sca_set_callback, sms);
return NULL;
}
if (!strcmp(property, "Bearer")) {
const char *value;
int bearer;
if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_STRING)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&var, &value);
bearer = sms_bearer_from_string(value);
if (bearer < 0)
return __ofono_error_invalid_format(msg);
if (sms->driver->bearer_set == NULL ||
sms->driver->bearer_query == NULL)
return __ofono_error_not_implemented(msg);
sms->pending = dbus_message_ref(msg);
sms->driver->bearer_set(sms, bearer, bearer_set_callback, sms);
return NULL;
}
if (!strcmp(property, "UseDeliveryReports")) {
const char *path = __ofono_atom_get_path(sms->atom);
dbus_bool_t value;
if (dbus_message_iter_get_arg_type(&var) != DBUS_TYPE_BOOLEAN)
return __ofono_error_invalid_args(msg);
dbus_message_iter_get_basic(&var, &value);
g_dbus_send_reply(conn, msg, DBUS_TYPE_INVALID);
if (sms->use_delivery_reports != (ofono_bool_t) value) {
sms->use_delivery_reports = value;
ofono_dbus_signal_property_changed(conn, path,
OFONO_MESSAGE_MANAGER_INTERFACE,
"UseDeliveryReports",
DBUS_TYPE_BOOLEAN, &value);
}
return NULL;
}
return __ofono_error_invalid_args(msg);
}
/*
* Destroy/release the contents of a 'struct tx_queue_entry'
*
* This releases resources allocated *inside* @entry and @entry
* itself.
*/
static void tx_queue_entry_destroy(struct tx_queue_entry *entry)
{
if (entry->destroy)
entry->destroy(entry->data);
g_free(entry->pdus);
g_free(entry);
}
static void tx_queue_entry_destroy_foreach(gpointer _entry, gpointer unused)
{
tx_queue_entry_destroy(_entry);
}
static void tx_finished(const struct ofono_error *error, int mr, void *data)
{
struct ofono_sms *sms = data;
struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
struct tx_queue_entry *entry = g_queue_peek_head(sms->txq);
gboolean ok = error->type == OFONO_ERROR_TYPE_NO_ERROR;
DBG("tx_finished");
if (ok == FALSE) {
if (!(entry->flags & OFONO_SMS_SUBMIT_FLAG_RETRY))
goto next_q;
entry->retry += 1;
if (entry->retry < TXQ_MAX_RETRIES) {
DBG("Sending failed, retry in %d secs",
entry->retry * 5);
sms->tx_source = g_timeout_add_seconds(entry->retry * 5,
tx_next, sms);
return;
}
DBG("Max retries reached, giving up");
goto next_q;
}
entry->cur_pdu += 1;
entry->retry = 0;
if (entry->flags & OFONO_SMS_SUBMIT_FLAG_REQUEST_SR)
status_report_assembly_add_fragment(sms->sr_assembly,
entry->uuid.uuid,
&entry->receiver,
mr, time(NULL),
entry->num_pdus);
if (entry->cur_pdu < entry->num_pdus) {
sms->tx_source = g_timeout_add(0, tx_next, sms);
return;
}
next_q:
entry = g_queue_pop_head(sms->txq);
if (entry->cb)
entry->cb(ok, entry->data);
if (entry->flags & OFONO_SMS_SUBMIT_FLAG_RECORD_HISTORY) {
enum ofono_history_sms_status hs;
if (ok)
hs = OFONO_HISTORY_SMS_STATUS_SUBMITTED;
else
hs = OFONO_HISTORY_SMS_STATUS_SUBMIT_FAILED;
__ofono_history_sms_send_status(modem, &entry->uuid,
time(NULL), hs);
}
if (entry->flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS) {
enum message_state ms;
if (ok)
ms = MESSAGE_STATE_SENT;
else
ms = MESSAGE_STATE_FAILED;
message_set_state(sms, &entry->uuid, ms);
}
tx_queue_entry_destroy(entry);
if (g_queue_peek_head(sms->txq)) {
DBG("Scheduling next");
sms->tx_source = g_timeout_add(0, tx_next, sms);
}
}
static gboolean tx_next(gpointer user_data)
{
struct ofono_sms *sms = user_data;
int send_mms = 0;
struct tx_queue_entry *entry = g_queue_peek_head(sms->txq);
struct pending_pdu *pdu = &entry->pdus[entry->cur_pdu];
struct ofono_error error;
error.type = OFONO_ERROR_TYPE_NO_ERROR;
DBG("tx_next: %p", entry);
sms->tx_source = 0;
if (entry == NULL)
return FALSE;
if (g_queue_get_length(sms->txq) > 1
|| (entry->num_pdus - entry->cur_pdu) > 1)
send_mms = 1;
sms->driver->submit(sms, pdu->pdu, pdu->pdu_len, pdu->tpdu_len,
send_mms, tx_finished, sms);
return FALSE;
}
/**
* Generate a UUID from an SMS PDU List
*
* @param pdu Pointer to array of PDUs data to generate the ID from
* @param pdus Number of entries in the \e pdu array
* @return 0 in error (no memory or serious code inconsistency in the
* input data structures), otherwise the SMS UUID.
*
* @internal
*
* The current time is added to avoid the UUID being the same when the
* same message is sent to the same destination repeatedly. Note we
* need a high resolution time (not just seconds), otherwise resending
* in the same second (not that rare) could yield the same UUID.
*/
static gboolean sms_uuid_from_pdus(const struct pending_pdu *pdu,
unsigned char pdus,
struct ofono_uuid *uuid)
{
GChecksum *checksum;
gsize uuid_size = sizeof(uuid->uuid);
unsigned int cnt;
struct timeval now;
checksum = g_checksum_new(G_CHECKSUM_SHA1);
if (checksum == NULL)
return FALSE;
for (cnt = 0; cnt < pdus; cnt++)
g_checksum_update(checksum, pdu[cnt].pdu, pdu[cnt].pdu_len);
gettimeofday(&now, NULL);
g_checksum_update(checksum, (void *) &now, sizeof(now));
g_checksum_get_digest(checksum, uuid->uuid, &uuid_size);
g_checksum_free(checksum);
return TRUE;
}
static struct tx_queue_entry *tx_queue_entry_new(GSList *msg_list,
unsigned int flags)
{
struct tx_queue_entry *entry;
int i = 0;
GSList *l;
entry = g_try_new0(struct tx_queue_entry, 1);
if (entry == NULL)
return NULL;
entry->num_pdus = g_slist_length(msg_list);
entry->pdus = g_try_new0(struct pending_pdu, entry->num_pdus);
if (entry->pdus == NULL)
goto error;
if (flags & OFONO_SMS_SUBMIT_FLAG_REQUEST_SR) {
struct sms *head = msg_list->data;
memcpy(&entry->receiver, &head->submit.daddr,
sizeof(entry->receiver));
}
entry->flags = flags;
for (l = msg_list; l; l = l->next) {
struct pending_pdu *pdu = &entry->pdus[i++];
struct sms *s = l->data;
sms_encode(s, &pdu->pdu_len, &pdu->tpdu_len, pdu->pdu);
DBG("pdu_len: %d, tpdu_len: %d",
pdu->pdu_len, pdu->tpdu_len);
}
if (sms_uuid_from_pdus(entry->pdus, entry->num_pdus, &entry->uuid))
return entry;
error:
g_free(entry->pdus);
g_free(entry);
return NULL;
}
static void tx_queue_entry_set_submit_notify(struct tx_queue_entry *entry,
ofono_sms_txq_submit_cb_t cb,
void *data,
ofono_destroy_func destroy)
{
entry->cb = cb;
entry->data = data;
entry->destroy = destroy;
}
static void message_queued(struct ofono_sms *sms,
const struct ofono_uuid *uuid, void *data)
{
DBusConnection *conn = ofono_dbus_get_connection();
DBusMessage *msg = data;
const char *path;
path = __ofono_sms_message_path_from_uuid(sms, uuid);
g_dbus_send_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &path,
DBUS_TYPE_INVALID);
}
/*
* Pre-process a SMS text message and deliver it [D-Bus SendMessage()]
*
* @conn: D-Bus connection
* @msg: message data (telephone number and text)
* @data: SMS object to use for transmision
*
* An alphabet is chosen for the text and it (might be) segmented in
* fragments by sms_text_prepare() into @msg_list. A queue list @entry
* is created by tx_queue_entry_new() and g_queue_push_tail()
* appends that entry to the SMS transmit queue. Then the tx_next()
* function is scheduled to run to process the queue.
*/
static DBusMessage *sms_send_message(DBusConnection *conn, DBusMessage *msg,
void *data)
{
struct ofono_sms *sms = data;
const char *to;
const char *text;
GSList *msg_list;
struct ofono_modem *modem;
unsigned int flags;
gboolean use_16bit_ref = FALSE;
int err;
struct ofono_uuid uuid;
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &to,
DBUS_TYPE_STRING, &text,
DBUS_TYPE_INVALID))
return __ofono_error_invalid_args(msg);
if (valid_phone_number_format(to) == FALSE)
return __ofono_error_invalid_format(msg);
msg_list = sms_text_prepare(to, text, sms->ref, use_16bit_ref,
sms->use_delivery_reports);
if (msg_list == NULL)
return __ofono_error_invalid_format(msg);
flags = OFONO_SMS_SUBMIT_FLAG_RECORD_HISTORY;
flags |= OFONO_SMS_SUBMIT_FLAG_RETRY;
flags |= OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS;
if (sms->use_delivery_reports)
flags |= OFONO_SMS_SUBMIT_FLAG_REQUEST_SR;
err = __ofono_sms_txq_submit(sms, msg_list, flags, &uuid,
message_queued, msg);
g_slist_foreach(msg_list, (GFunc) g_free, NULL);
g_slist_free(msg_list);
if (err < 0)
return __ofono_error_failed(msg);
modem = __ofono_atom_get_modem(sms->atom);
__ofono_history_sms_send_pending(modem, &uuid, to, time(NULL), text);
return NULL;
}
static DBusMessage *sms_get_messages(DBusConnection *conn, DBusMessage *msg,
void *data)
{
struct ofono_sms *sms = data;
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter array;
DBusMessageIter entry, dict;
const char *path;
GHashTableIter hashiter;
gpointer key, value;
struct message *m;
reply = dbus_message_new_method_return(msg);
if (reply == NULL)
return NULL;
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
DBUS_STRUCT_BEGIN_CHAR_AS_STRING
DBUS_TYPE_OBJECT_PATH_AS_STRING
DBUS_TYPE_ARRAY_AS_STRING
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
DBUS_TYPE_STRING_AS_STRING
DBUS_TYPE_VARIANT_AS_STRING
DBUS_DICT_ENTRY_END_CHAR_AS_STRING
DBUS_STRUCT_END_CHAR_AS_STRING,
&array);
g_hash_table_iter_init(&hashiter, sms->messages);
while (g_hash_table_iter_next(&hashiter, &key, &value)) {
m = value;
path = __ofono_sms_message_path_from_uuid(sms, &m->uuid);
dbus_message_iter_open_container(&array, DBUS_TYPE_STRUCT,
NULL, &entry);
dbus_message_iter_append_basic(&entry, DBUS_TYPE_OBJECT_PATH,
&path);
dbus_message_iter_open_container(&entry, DBUS_TYPE_ARRAY,
OFONO_PROPERTIES_ARRAY_SIGNATURE,
&dict);
append_message_properties(m, &dict);
dbus_message_iter_close_container(&entry, &dict);
dbus_message_iter_close_container(&array, &entry);
}
dbus_message_iter_close_container(&iter, &array);
return reply;
}
static GDBusMethodTable sms_manager_methods[] = {
{ "GetProperties", "", "a{sv}", sms_get_properties,
G_DBUS_METHOD_FLAG_ASYNC },
{ "SetProperty", "sv", "", sms_set_property,
G_DBUS_METHOD_FLAG_ASYNC },
{ "SendMessage", "ss", "o", sms_send_message,
G_DBUS_METHOD_FLAG_ASYNC },
{ "GetMessages", "", "a(oa{sv})", sms_get_messages },
{ }
};
static GDBusSignalTable sms_manager_signals[] = {
{ "PropertyChanged", "sv" },
{ "IncomingMessage", "sa{sv}" },
{ "ImmediateMessage", "sa{sv}" },
{ "MessageAdded", "oa{sv}" },
{ "MessageRemoved", "o" },
{ }
};
static gboolean compute_incoming_msgid(GSList *sms_list,
struct ofono_uuid *uuid)
{
GChecksum *checksum;
GSList *l;
const struct sms *s;
unsigned char buf[176];
gsize uuid_size = sizeof(uuid->uuid);
int len;
checksum = g_checksum_new(G_CHECKSUM_SHA1);
if (checksum == NULL)
return FALSE;
for (l = sms_list; l; l = l->next) {
s = l->data;
if (sms_encode(s, &len, NULL, buf) == FALSE) {
g_checksum_free(checksum);
return FALSE;
}
g_checksum_update(checksum, buf, len);
}
g_checksum_get_digest(checksum, uuid->uuid, &uuid_size);
g_checksum_free(checksum);
return TRUE;
}
static void dispatch_app_datagram(struct ofono_sms *sms,
const struct ofono_uuid *uuid,
int dst, int src,
unsigned char *buf, unsigned len,
const struct sms_address *addr,
const struct sms_scts *scts)
{
const char *sender = sms_address_to_string(addr);
time_t ts;
struct tm remote;
struct tm local;
ofono_sms_datagram_notify_cb_t notify;
struct sms_handler *h;
GSList *l;
ts = sms_scts_to_time(scts, &remote);
localtime_r(&ts, &local);
for (l = sms->datagram_handlers->items; l; l = l->next) {
h = l->data;
notify = h->item.notify;
if (!port_equal(dst, h->dst) || !port_equal(src, h->src))
continue;
notify(sender, &remote, &local, dst, src, buf, len,
h->item.notify_data);
}
}
static void dispatch_text_message(struct ofono_sms *sms,
const struct ofono_uuid *uuid,
const char *message,
enum sms_class cls,
const struct sms_address *addr,
const struct sms_scts *scts)
{
struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
DBusConnection *conn = ofono_dbus_get_connection();
const char *path = __ofono_atom_get_path(sms->atom);
DBusMessage *signal;
DBusMessageIter iter;
DBusMessageIter dict;
char buf[128];
const char *signal_name;
time_t ts;
struct tm remote;
struct tm local;
const char *str = buf;
ofono_sms_text_notify_cb_t notify;
struct sms_handler *h;
GSList *l;
if (message == NULL)
return;
if (cls == SMS_CLASS_0)
signal_name = "ImmediateMessage";
else
signal_name = "IncomingMessage";
signal = dbus_message_new_signal(path, OFONO_MESSAGE_MANAGER_INTERFACE,
signal_name);
if (signal == NULL)
return;
dbus_message_iter_init_append(signal, &iter);
dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &message);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
OFONO_PROPERTIES_ARRAY_SIGNATURE,
&dict);
ts = sms_scts_to_time(scts, &remote);
localtime_r(&ts, &local);
strftime(buf, 127, "%Y-%m-%dT%H:%M:%S%z", &local);
buf[127] = '\0';
ofono_dbus_dict_append(&dict, "LocalSentTime", DBUS_TYPE_STRING, &str);
strftime(buf, 127, "%Y-%m-%dT%H:%M:%S%z", &remote);
buf[127] = '\0';
ofono_dbus_dict_append(&dict, "SentTime", DBUS_TYPE_STRING, &str);
str = sms_address_to_string(addr);
ofono_dbus_dict_append(&dict, "Sender", DBUS_TYPE_STRING, &str);
dbus_message_iter_close_container(&iter, &dict);
g_dbus_send_message(conn, signal);
if (cls == SMS_CLASS_0)
return;
for (l = sms->text_handlers->items; l; l = l->next) {
h = l->data;
notify = h->item.notify;
notify(str, &remote, &local, message, h->item.notify_data);
}
__ofono_history_sms_received(modem, uuid, str, &remote, &local,
message);
}
static void sms_dispatch(struct ofono_sms *sms, GSList *sms_list)
{
GSList *l;
const struct sms *s;
struct ofono_uuid uuid;
enum sms_charset uninitialized_var(old_charset);
enum sms_class cls;
int srcport = -1;
int dstport = -1;
DBG("");
if (sms_list == NULL)
return;
/*
* Qutoting 23.040: The TP elements in the SMS‑SUBMIT PDU, apart from
* TP‑MR, TP-SRR, TP‑UDL and TP‑UD, should remain unchanged for each
* SM which forms part of a concatenated SM, otherwise this may lead
* to irrational behaviour
*
* This means that we assume that at least the charset is the same
* across all parts of the SMS in the case of 8-bit data. Other
* cases can be handled by converting to UTF8.
*
* We also check that if 8-bit or 16-bit application addressing is
* used, the addresses are the same across all segments.
*/
for (l = sms_list; l; l = l->next) {
guint8 dcs;
gboolean comp = FALSE;
enum sms_charset charset;
int cdst = -1;
int csrc = -1;
gboolean is_8bit;
s = l->data;
dcs = s->deliver.dcs;
if (sms_mwi_dcs_decode(dcs, NULL, &charset, NULL, NULL))
cls = SMS_CLASS_UNSPECIFIED;
else if (!sms_dcs_decode(dcs, &cls, &charset, &comp, NULL)) {
ofono_error("The deliver DCS is not recognized");
return;
}
if (comp) {
ofono_error("Compressed data not supported");
return;
}
if (l == sms_list)
old_charset = charset;
if (charset == SMS_CHARSET_8BIT && charset != old_charset) {
ofono_error("Can't concatenate disparate charsets");
return;
}
if (sms_extract_app_port(s, &cdst, &csrc, &is_8bit)) {
csrc = is_8bit ? (csrc << 16) : csrc;
cdst = is_8bit ? (cdst << 16) : cdst;
if (l == sms_list) {
srcport = csrc;
dstport = cdst;
}
}
if (srcport != csrc || dstport != cdst) {
ofono_error("Source / Destination ports across "
"concatenated message are not the "
"same, ignoring");
return;
}
}
if (!compute_incoming_msgid(sms_list, &uuid))
return;
s = sms_list->data;
/* Handle datagram */
if (old_charset == SMS_CHARSET_8BIT) {
unsigned char *buf;
long len;
if (srcport == -1 || dstport == -1) {
ofono_error("Got an 8-bit encoded message, however "
"no valid src/address port, ignore");
return;
}
buf = sms_decode_datagram(sms_list, &len);
if (buf == NULL)
return;
dispatch_app_datagram(sms, &uuid, dstport, srcport, buf, len,
&s->deliver.oaddr, &s->deliver.scts);
g_free(buf);
} else {
char *message = sms_decode_text(sms_list);
if (message == NULL)
return;
dispatch_text_message(sms, &uuid, message, cls,
&s->deliver.oaddr, &s->deliver.scts);
g_free(message);
}
}
static void handle_deliver(struct ofono_sms *sms, const struct sms *incoming)
{
GSList *l;
guint16 ref;
guint8 max;
guint8 seq;
DBG("");
if (sms_extract_concatenation(incoming, &ref, &max, &seq)) {
GSList *sms_list;
if (sms->assembly == NULL)
return;
sms_list = sms_assembly_add_fragment(sms->assembly,
incoming, time(NULL),
&incoming->deliver.oaddr,
ref, max, seq);
if (sms_list == NULL)
return;
sms_dispatch(sms, sms_list);
g_slist_foreach(sms_list, (GFunc) g_free, NULL);
g_slist_free(sms_list);
return;
}
l = g_slist_append(NULL, (void *) incoming);
sms_dispatch(sms, l);
g_slist_free(l);
}
static void handle_sms_status_report(struct ofono_sms *sms,
const struct sms *incoming)
{
struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
gboolean delivered;
struct ofono_uuid uuid;
DBG("");
if (status_report_assembly_report(sms->sr_assembly, incoming, uuid.uuid,
&delivered) == FALSE)
return;
__ofono_history_sms_send_status(modem, &uuid, time(NULL),
delivered ? OFONO_HISTORY_SMS_STATUS_DELIVERED :
OFONO_HISTORY_SMS_STATUS_DELIVER_FAILED);
}
static inline gboolean handle_mwi(struct ofono_sms *sms, struct sms *s)
{
gboolean discard;
DBG("");
if (sms->mw == NULL)
return FALSE;
__ofono_message_waiting_mwi(sms->mw, s, &discard);
return discard;
}
void ofono_sms_deliver_notify(struct ofono_sms *sms, unsigned char *pdu,
int len, int tpdu_len)
{
struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
struct ofono_atom *stk_atom;
struct ofono_atom *sim_atom;
struct sms s;
enum sms_class cls;
DBG("len %d tpdu len %d", len, tpdu_len);
if (!sms_decode(pdu, len, FALSE, tpdu_len, &s)) {
ofono_error("Unable to decode PDU");
return;
}
if (s.type != SMS_TYPE_DELIVER) {
ofono_error("Expecting a DELIVER pdu");
return;
}
if (s.deliver.pid == SMS_PID_TYPE_SM_TYPE_0) {
DBG("Explicitly ignoring type 0 SMS");
return;
}
/*
* This is an older style MWI notification, process MWI
* headers and handle it like any other message
*/
if (s.deliver.pid == SMS_PID_TYPE_RETURN_CALL) {
if (handle_mwi(sms, &s))
return;
goto out;
}
/*
* The DCS indicates this is an MWI notification, process it
* and then handle the User-Data as any other message
*/
if (sms_mwi_dcs_decode(s.deliver.dcs, NULL, NULL, NULL, NULL)) {
if (handle_mwi(sms, &s))
return;
goto out;
}
if (!sms_dcs_decode(s.deliver.dcs, &cls, NULL, NULL, NULL)) {
ofono_error("Unknown / Reserved DCS. Ignoring");
return;
}
switch (s.deliver.pid) {
case SMS_PID_TYPE_ME_DOWNLOAD:
if (cls == SMS_CLASS_1) {
ofono_error("ME Download message ignored");
return;
}
break;
case SMS_PID_TYPE_ME_DEPERSONALIZATION:
if (s.deliver.dcs == 0x11) {
ofono_error("ME Depersonalization message ignored");
return;
}
break;
case SMS_PID_TYPE_USIM_DOWNLOAD:
case SMS_PID_TYPE_ANSI136:
/* If not Class 2, handle in a "normal" way */
if (cls != SMS_CLASS_2)
break;
sim_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_SIM);
if (sim_atom == NULL)
return;
if (!__ofono_sim_service_available(
__ofono_atom_get_data(sim_atom),
SIM_UST_SERVICE_DATA_DOWNLOAD_SMS_PP,
SIM_SST_SERVICE_DATA_DOWNLOAD_SMS_PP))
return;
stk_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_STK);
if (stk_atom == NULL)
return;
__ofono_sms_sim_download(__ofono_atom_get_data(stk_atom),
&s, NULL, sms);
/*
* Passing the USIM response back to network is not
* currently supported
*
* TODO: store in EFsms if not handled
*/
return;
default:
break;
}
/*
* Check to see if the SMS has any other MWI related headers,
* as sometimes they are "tacked on" by the SMSC.
* While we're doing this we also check for messages containing
* WCMP headers or headers that can't possibly be in a normal
* message. If we find messages like that, we ignore them.
*/
if (s.deliver.udhi) {
struct sms_udh_iter iter;
enum sms_iei iei;
if (!sms_udh_iter_init(&s, &iter))
goto out;
while ((iei = sms_udh_iter_get_ie_type(&iter)) !=
SMS_IEI_INVALID) {
if (iei > 0x25) {
ofono_error("Reserved / Unknown / USAT"
"header in use, ignore");
return;
}
switch (iei) {
case SMS_IEI_SPECIAL_MESSAGE_INDICATION:
case SMS_IEI_ENHANCED_VOICE_MAIL_INFORMATION:
/*
* TODO: ignore if not in the very first
* segment of a concatenated SM so as not
* to repeat the indication.
*/
if (handle_mwi(sms, &s))
return;
goto out;
case SMS_IEI_WCMP:
ofono_error("No support for WCMP, ignoring");
return;
default:
sms_udh_iter_next(&iter);
}
}
}
out:
handle_deliver(sms, &s);
}
void ofono_sms_status_notify(struct ofono_sms *sms, unsigned char *pdu,
int len, int tpdu_len)
{
struct sms s;
enum sms_class cls;
DBG("len %d tpdu len %d", len, tpdu_len);
if (!sms_decode(pdu, len, FALSE, tpdu_len, &s)) {
ofono_error("Unable to decode PDU");
return;
}
if (s.type != SMS_TYPE_STATUS_REPORT) {
ofono_error("Expecting a STATUS REPORT pdu");
return;
}
if (s.status_report.srq) {
ofono_error("Waiting an answer to SMS-SUBMIT, not SMS-COMMAND");
return;
}
if (!sms_dcs_decode(s.status_report.dcs, &cls, NULL, NULL, NULL)) {
ofono_error("Unknown / Reserved DCS. Ignoring");
return;
}
handle_sms_status_report(sms, &s);
}
int ofono_sms_driver_register(const struct ofono_sms_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_sms_driver_unregister(const struct ofono_sms_driver *d)
{
DBG("driver: %p, name: %s", d, d->name);
g_drivers = g_slist_remove(g_drivers, (void *) d);
}
static void sms_unregister(struct ofono_atom *atom)
{
struct ofono_sms *sms = __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);
g_dbus_unregister_interface(conn, path,
OFONO_MESSAGE_MANAGER_INTERFACE);
ofono_modem_remove_interface(modem, OFONO_MESSAGE_MANAGER_INTERFACE);
if (sms->mw_watch) {
__ofono_modem_remove_atom_watch(modem, sms->mw_watch);
sms->mw_watch = 0;
sms->mw = NULL;
}
if (sms->messages) {
GHashTableIter iter;
struct message *m;
gpointer key, value;
g_hash_table_iter_init(&iter, sms->messages);
while (g_hash_table_iter_next(&iter, &key, &value)) {
m = value;
message_dbus_unregister(sms, m);
}
g_hash_table_destroy(sms->messages);
sms->messages = NULL;
}
__ofono_watchlist_free(sms->text_handlers);
sms->text_handlers = NULL;
__ofono_watchlist_free(sms->datagram_handlers);
sms->datagram_handlers = NULL;
}
static void sms_remove(struct ofono_atom *atom)
{
struct ofono_sms *sms = __ofono_atom_get_data(atom);
DBG("atom: %p", atom);
if (sms == NULL)
return;
if (sms->driver && sms->driver->remove)
sms->driver->remove(sms);
if (sms->tx_source) {
g_source_remove(sms->tx_source);
sms->tx_source = 0;
}
if (sms->assembly) {
sms_assembly_free(sms->assembly);
sms->assembly = NULL;
}
if (sms->txq) {
g_queue_foreach(sms->txq, tx_queue_entry_destroy_foreach, NULL);
g_queue_free(sms->txq);
sms->txq = NULL;
}
if (sms->settings) {
g_key_file_set_integer(sms->settings, SETTINGS_GROUP,
"NextReference", sms->ref);
g_key_file_set_boolean(sms->settings, SETTINGS_GROUP,
"UseDeliveryReports",
sms->use_delivery_reports);
g_key_file_set_integer(sms->settings, SETTINGS_GROUP,
"Bearer", sms->bearer);
storage_close(sms->imsi, SETTINGS_STORE, sms->settings, TRUE);
g_free(sms->imsi);
sms->imsi = NULL;
sms->settings = NULL;
}
if (sms->sr_assembly) {
status_report_assembly_free(sms->sr_assembly);
sms->sr_assembly = NULL;
}
g_free(sms);
}
/*
* Create a SMS driver
*
* This creates a SMS driver that is hung off a @modem
* object. However, for the driver to be used by the system, it has to
* be registered with the oFono core using ofono_sms_register().
*
* This is done once the modem driver determines that SMS is properly
* supported by the hardware.
*/
struct ofono_sms *ofono_sms_create(struct ofono_modem *modem,
unsigned int vendor,
const char *driver,
void *data)
{
struct ofono_sms *sms;
GSList *l;
if (driver == NULL)
return NULL;
sms = g_try_new0(struct ofono_sms, 1);
if (sms == NULL)
return NULL;
sms->sca.type = 129;
sms->ref = 1;
sms->txq = g_queue_new();
sms->messages = g_hash_table_new(uuid_hash, uuid_equal);
sms->atom = __ofono_modem_add_atom(modem, OFONO_ATOM_TYPE_SMS,
sms_remove, sms);
for (l = g_drivers; l; l = l->next) {
const struct ofono_sms_driver *drv = l->data;
if (g_strcmp0(drv->name, driver))
continue;
if (drv->probe(sms, vendor, data) < 0)
continue;
sms->driver = drv;
break;
}
return sms;
}
static void mw_watch(struct ofono_atom *atom,
enum ofono_atom_watch_condition cond, void *data)
{
struct ofono_sms *sms = data;
if (cond == OFONO_ATOM_WATCH_CONDITION_UNREGISTERED) {
sms->mw = NULL;
return;
}
sms->mw = __ofono_atom_get_data(atom);
}
static void sms_load_settings(struct ofono_sms *sms, const char *imsi)
{
GError *error = NULL;
sms->settings = storage_open(imsi, SETTINGS_STORE);
if (sms->settings == NULL)
return;
sms->imsi = g_strdup(imsi);
sms->ref = g_key_file_get_integer(sms->settings, SETTINGS_GROUP,
"NextReference", NULL);
if (sms->ref >= 65536)
sms->ref = 1;
sms->use_delivery_reports =
g_key_file_get_boolean(sms->settings, SETTINGS_GROUP,
"UseDeliveryReports", NULL);
sms->bearer = g_key_file_get_integer(sms->settings, SETTINGS_GROUP,
"Bearer", &error);
if (error)
sms->bearer = 3; /* Default to CS then PS */
}
static void bearer_init_callback(const struct ofono_error *error, void *data)
{
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
ofono_error("Error bootstrapping SMS Bearer Preference");
}
/*
* Indicate oFono that a SMS driver is ready for operation
*
* This is called after ofono_sms_create() was done and the modem
* driver determined that a modem supports SMS correctly. Once this
* call succeeds, the D-BUS interface for SMS goes live.
*/
void ofono_sms_register(struct ofono_sms *sms)
{
DBusConnection *conn = ofono_dbus_get_connection();
struct ofono_modem *modem = __ofono_atom_get_modem(sms->atom);
const char *path = __ofono_atom_get_path(sms->atom);
struct ofono_atom *mw_atom;
struct ofono_atom *sim_atom;
if (!g_dbus_register_interface(conn, path,
OFONO_MESSAGE_MANAGER_INTERFACE,
sms_manager_methods,
sms_manager_signals,
NULL, sms, NULL)) {
ofono_error("Could not create %s interface",
OFONO_MESSAGE_MANAGER_INTERFACE);
return;
}
ofono_modem_add_interface(modem, OFONO_MESSAGE_MANAGER_INTERFACE);
sms->mw_watch = __ofono_modem_add_atom_watch(modem,
OFONO_ATOM_TYPE_MESSAGE_WAITING,
mw_watch, sms, NULL);
mw_atom = __ofono_modem_find_atom(modem,
OFONO_ATOM_TYPE_MESSAGE_WAITING);
if (mw_atom && __ofono_atom_get_registered(mw_atom))
mw_watch(mw_atom, OFONO_ATOM_WATCH_CONDITION_REGISTERED, sms);
sim_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_SIM);
/*
* If we have a sim atom, we can uniquely identify the SIM,
* otherwise create an sms assembly which doesn't backup the fragment
* store.
*/
if (sim_atom) {
const char *imsi;
sms->sim = __ofono_atom_get_data(sim_atom);
imsi = ofono_sim_get_imsi(sms->sim);
sms->assembly = sms_assembly_new(imsi);
sms->sr_assembly = status_report_assembly_new(imsi);
sms_load_settings(sms, imsi);
} else {
sms->assembly = sms_assembly_new(NULL);
sms->sr_assembly = status_report_assembly_new(NULL);
sms->bearer = 3; /* Default to CS then PS */
}
if (sms->driver->bearer_set)
sms->driver->bearer_set(sms, sms->bearer,
bearer_init_callback, sms);
sms->text_handlers = __ofono_watchlist_new(g_free);
sms->datagram_handlers = __ofono_watchlist_new(g_free);
__ofono_atom_register(sms->atom, sms_unregister);
}
void ofono_sms_remove(struct ofono_sms *sms)
{
__ofono_atom_free(sms->atom);
}
void ofono_sms_set_data(struct ofono_sms *sms, void *data)
{
sms->driver_data = data;
}
void *ofono_sms_get_data(struct ofono_sms *sms)
{
return sms->driver_data;
}
unsigned short __ofono_sms_get_next_ref(struct ofono_sms *sms)
{
return sms->ref;
}
int __ofono_sms_txq_submit(struct ofono_sms *sms, GSList *list,
unsigned int flags,
struct ofono_uuid *uuid,
ofono_sms_txq_queued_cb_t cb, void *data)
{
struct message *m = NULL;
struct tx_queue_entry *entry;
entry = tx_queue_entry_new(list, flags);
if (entry == NULL)
return -ENOMEM;
if (flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS) {
m = message_create(&entry->uuid);
if (m == NULL)
goto err;
if (message_dbus_register(sms, m) == FALSE)
goto err;
g_hash_table_insert(sms->messages, &m->uuid, m);
m->entry = entry;
}
if (list->next != NULL) {
if (sms->ref == 65536)
sms->ref = 1;
else
sms->ref = sms->ref + 1;
}
g_queue_push_tail(sms->txq, entry);
if (g_queue_get_length(sms->txq) == 1)
sms->tx_source = g_timeout_add(0, tx_next, sms);
if (uuid)
memcpy(uuid, &entry->uuid, sizeof(*uuid));
if (cb)
cb(sms, &entry->uuid, data);
if (m && (flags & OFONO_SMS_SUBMIT_FLAG_EXPOSE_DBUS))
emit_message_added(sms, m);
return 0;
err:
tx_queue_entry_destroy(entry);
return -EINVAL;
}
int __ofono_sms_txq_set_submit_notify(struct ofono_sms *sms,
struct ofono_uuid *uuid,
ofono_sms_txq_submit_cb_t cb,
void *data,
ofono_destroy_func destroy)
{
struct message *m;
m = g_hash_table_lookup(sms->messages, uuid);
if (m == NULL)
return -ENOENT;
if (m->entry == NULL)
return -ENOTSUP;
tx_queue_entry_set_submit_notify(m->entry, cb, data, destroy);
return 0;
}
|