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
|
/*
*
* oFono - Open Source Telephony
*
* Copyright (C) 2008-2009 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 <time.h>
#include <dbus/dbus.h>
#include <glib.h>
#include <gdbus.h>
#include "ofono.h"
#include "driver.h"
#include "common.h"
#include "dbus-gsm.h"
#include "modem.h"
#include "cssn.h"
#define VOICECALL_MANAGER_INTERFACE "org.ofono.VoiceCallManager"
#define VOICECALL_INTERFACE "org.ofono.VoiceCall"
#define VOICECALLS_FLAG_PENDING 0x1
#define VOICECALLS_FLAG_MULTI_RELEASE 0x2
#define VOICECALLS_FLAG_UPDATING_CALL_LIST 0x4
#define VOICECALLS_FLAG_UPDATING_MPTY_CALL_LIST 0x8
#define MAX_VOICE_CALLS 16
struct voicecalls_data {
GSList *call_list;
GSList *release_list;
GSList *multiparty_list;
struct ofono_voicecall_ops *ops;
int flags;
DBusMessage *pending;
};
struct voicecall {
struct ofono_call *call;
struct ofono_modem *modem;
time_t start_time;
time_t detect_time;
};
static void generic_callback(const struct ofono_error *error, void *data);
static void dial_callback(const struct ofono_error *error, void *data);
static void multirelease_callback(const struct ofono_error *err, void *data);
static void multiparty_create_callback(const struct ofono_error *error,
void *data);
static void private_chat_callback(const struct ofono_error *error, void *data);
static gint call_compare_by_id(gconstpointer a, gconstpointer b)
{
const struct ofono_call *call = ((struct voicecall *)a)->call;
unsigned int id = GPOINTER_TO_UINT(b);
if (id < call->id)
return -1;
if (id > call->id)
return 1;
return 0;
}
static gint call_compare(gconstpointer a, gconstpointer b)
{
const struct voicecall *ca = a;
const struct voicecall *cb = b;
if (ca->call->id < cb->call->id)
return -1;
if (ca->call->id > cb->call->id)
return 1;
return 0;
}
static const char *call_status_to_string(int status)
{
switch (status) {
case CALL_STATUS_ACTIVE:
return "active";
case CALL_STATUS_HELD:
return "held";
case CALL_STATUS_DIALING:
return "dialing";
case CALL_STATUS_ALERTING:
return "alerting";
case CALL_STATUS_INCOMING:
return "incoming";
case CALL_STATUS_WAITING:
return "waiting";
default:
return "disconnected";
}
}
static const char *phone_and_clip_to_string(const struct ofono_phone_number *n,
int clip_validity)
{
if (clip_validity == CLIP_VALIDITY_WITHHELD && !strlen(n->number))
return "withheld";
if (clip_validity == CLIP_VALIDITY_NOT_AVAILABLE)
return "";
return phone_number_to_string(n);
}
static const char *time_to_str(const time_t *t)
{
static char buf[128];
strftime(buf, 127, "%a, %d %b %Y %H:%M:%S %z", localtime(t));
buf[127] = '\0';
return buf;
}
static DBusMessage *voicecall_get_properties(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct voicecall *v = data;
struct ofono_call *call = v->call;
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter dict;
const char *status;
const char *callerid;
const char *timestr = "";
reply = dbus_message_new_method_return(msg);
if (!reply)
return NULL;
status = call_status_to_string(call->status);
callerid = phone_number_to_string(&call->phone_number);
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
PROPERTIES_ARRAY_SIGNATURE,
&dict);
dbus_gsm_dict_append(&dict, "State", DBUS_TYPE_STRING, &status);
dbus_gsm_dict_append(&dict, "LineIdentification",
DBUS_TYPE_STRING, &callerid);
if (call->status == CALL_STATUS_ACTIVE ||
(call->status == CALL_STATUS_DISCONNECTED && v->start_time != 0) ||
call->status == CALL_STATUS_HELD) {
timestr = time_to_str(&v->start_time);
dbus_gsm_dict_append(&dict, "StartTime", DBUS_TYPE_STRING,
×tr);
}
dbus_message_iter_close_container(&iter, &dict);
return reply;
}
static DBusMessage *voicecall_busy(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct voicecall *v = data;
struct ofono_modem *modem = v->modem;
struct voicecalls_data *voicecalls = modem->voicecalls;
struct ofono_call *call = v->call;
if (call->status != CALL_STATUS_INCOMING &&
call->status != CALL_STATUS_WAITING)
return dbus_gsm_failed(msg);
if (!voicecalls->ops->set_udub)
return dbus_gsm_not_implemented(msg);
if (voicecalls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
voicecalls->flags |= VOICECALLS_FLAG_PENDING;
voicecalls->pending = dbus_message_ref(msg);
voicecalls->ops->set_udub(modem, generic_callback, voicecalls);
return NULL;
}
static DBusMessage *voicecall_deflect(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct voicecall *v = data;
struct ofono_modem *modem = v->modem;
struct voicecalls_data *voicecalls = modem->voicecalls;
struct ofono_call *call = v->call;
struct ofono_phone_number ph;
const char *number;
if (call->status != CALL_STATUS_INCOMING &&
call->status != CALL_STATUS_WAITING)
return dbus_gsm_failed(msg);
if (!voicecalls->ops->deflect)
return dbus_gsm_not_implemented(msg);
if (voicecalls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &number,
DBUS_TYPE_INVALID) == FALSE)
return dbus_gsm_invalid_args(msg);
if (!valid_phone_number_format(number))
return dbus_gsm_invalid_format(msg);
voicecalls->flags |= VOICECALLS_FLAG_PENDING;
voicecalls->pending = dbus_message_ref(msg);
string_to_phone_number(number, &ph);
voicecalls->ops->deflect(modem, &ph, generic_callback, voicecalls);
return NULL;
}
static DBusMessage *voicecall_hangup(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct voicecall *v = data;
struct ofono_modem *modem = v->modem;
struct voicecalls_data *voicecalls = modem->voicecalls;
struct ofono_call *call = v->call;
if (call->status == CALL_STATUS_DISCONNECTED)
return dbus_gsm_failed(msg);
if (!voicecalls->ops->release_specific)
return dbus_gsm_not_implemented(msg);
if (voicecalls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
voicecalls->flags |= VOICECALLS_FLAG_PENDING;
voicecalls->pending = dbus_message_ref(msg);
voicecalls->ops->release_specific(modem, call->id,
generic_callback, voicecalls);
return NULL;
}
static DBusMessage *voicecall_answer(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct voicecall *v = data;
struct ofono_modem *modem = v->modem;
struct voicecalls_data *voicecalls = modem->voicecalls;
struct ofono_call *call = v->call;
if (call->status != CALL_STATUS_INCOMING)
return dbus_gsm_failed(msg);
if (!voicecalls->ops->answer)
return dbus_gsm_not_implemented(msg);
if (voicecalls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
voicecalls->flags |= VOICECALLS_FLAG_PENDING;
voicecalls->pending = dbus_message_ref(msg);
voicecalls->ops->answer(modem, generic_callback, voicecalls);
return NULL;
}
static GDBusMethodTable voicecall_methods[] = {
{ "GetProperties", "", "a{sv}", voicecall_get_properties },
{ "Busy", "", "", voicecall_busy,
G_DBUS_METHOD_FLAG_ASYNC },
{ "Deflect", "s", "", voicecall_deflect,
G_DBUS_METHOD_FLAG_ASYNC },
{ "Hangup", "", "", voicecall_hangup,
G_DBUS_METHOD_FLAG_ASYNC },
{ "Answer", "", "", voicecall_answer,
G_DBUS_METHOD_FLAG_ASYNC },
{ }
};
static GDBusSignalTable voicecall_signals[] = {
{ "PropertyChanged", "sv" },
{ "DisconnectReason", "s" },
{ }
};
static struct voicecall *voicecall_create(struct ofono_modem *modem,
struct ofono_call *call)
{
struct voicecall *v;
v = g_try_new0(struct voicecall, 1);
if (!v)
return NULL;
v->call = call;
v->modem = modem;
return v;
}
static void voicecall_destroy(gpointer userdata)
{
struct voicecall *voicecall = (struct voicecall *)userdata;
g_free(voicecall->call);
g_free(voicecall);
}
static const char *voicecall_build_path(struct ofono_modem *modem,
const struct ofono_call *call)
{
static char path[MAX_DBUS_PATH_LEN];
snprintf(path, MAX_DBUS_PATH_LEN, "%s/voicecall%02d",
modem->path, call->id);
return path;
}
static void voicecall_set_call_status(struct ofono_modem *modem,
struct voicecall *call,
int status)
{
DBusConnection *conn = dbus_gsm_connection();
const char *path;
const char *status_str;
int old_status;
if (call->call->status == status)
return;
old_status = call->call->status;
call->call->status = status;
status_str = call_status_to_string(status);
path = voicecall_build_path(modem, call->call);
dbus_gsm_signal_property_changed(conn, path, VOICECALL_INTERFACE,
"State", DBUS_TYPE_STRING,
&status_str);
if (status == CALL_STATUS_ACTIVE &&
(old_status == CALL_STATUS_INCOMING ||
old_status == CALL_STATUS_DIALING ||
old_status == CALL_STATUS_ALERTING ||
old_status == CALL_STATUS_WAITING)) {
const char *timestr;
call->start_time = time(NULL);
timestr = time_to_str(&call->start_time);
dbus_gsm_signal_property_changed(conn, path,
VOICECALL_INTERFACE,
"StartTime",
DBUS_TYPE_STRING,
×tr);
}
}
static void voicecall_set_call_lineid(struct ofono_modem *modem,
struct voicecall *v,
const struct ofono_phone_number *ph,
int clip_validity)
{
struct ofono_call *call = v->call;
DBusConnection *conn = dbus_gsm_connection();
const char *path;
const char *lineid_str;
if (!strcmp(call->phone_number.number, ph->number) &&
call->phone_number.type == ph->type &&
call->clip_validity == clip_validity)
return;
/* Two cases: We get an incoming call with CLIP factored in, or
* CLIP comes in later as a separate event
* For COLP only the phone number should be checked, it can come
* in with the initial call event or later as a separate event */
/* For plugins that don't keep state, ignore */
if (call->clip_validity == CLIP_VALIDITY_VALID &&
clip_validity == CLIP_VALIDITY_NOT_AVAILABLE)
return;
strcpy(call->phone_number.number, ph->number);
call->clip_validity = clip_validity;
call->phone_number.type = ph->type;
path = voicecall_build_path(modem, call);
if (call->direction == CALL_DIRECTION_MOBILE_TERMINATED)
lineid_str = phone_and_clip_to_string(ph, clip_validity);
else
lineid_str = phone_number_to_string(ph);
dbus_gsm_signal_property_changed(conn, path, VOICECALL_INTERFACE,
"LineIdentification",
DBUS_TYPE_STRING, &lineid_str);
}
static gboolean voicecall_dbus_register(struct voicecall *voicecall)
{
DBusConnection *conn = dbus_gsm_connection();
const char *path;
if (!voicecall)
return FALSE;
path = voicecall_build_path(voicecall->modem, voicecall->call);
if (!g_dbus_register_interface(conn, path, VOICECALL_INTERFACE,
voicecall_methods,
voicecall_signals,
NULL, voicecall,
voicecall_destroy)) {
ofono_error("Could not register VoiceCall %s", path);
voicecall_destroy(voicecall);
return FALSE;
}
return TRUE;
}
static gboolean voicecall_dbus_unregister(struct ofono_modem *modem,
struct voicecall *call)
{
DBusConnection *conn = dbus_gsm_connection();
const char *path = voicecall_build_path(modem, call->call);
return g_dbus_unregister_interface(conn, path,
VOICECALL_INTERFACE);
}
static struct voicecalls_data *voicecalls_create()
{
struct voicecalls_data *calls;
calls = g_try_new0(struct voicecalls_data, 1);
return calls;
}
static void voicecalls_destroy(gpointer userdata)
{
struct ofono_modem *modem = userdata;
struct voicecalls_data *calls = modem->voicecalls;
GSList *l;
for (l = calls->call_list; l; l = l->next)
voicecall_dbus_unregister(modem, l->data);
g_slist_free(calls->call_list);
g_free(calls);
modem->voicecalls = 0;
}
static int voicecalls_path_list(struct ofono_modem *modem, GSList *call_list,
char ***objlist)
{
GSList *l;
int i;
struct voicecall *v;
*objlist = g_new0(char *, g_slist_length(call_list) + 1);
if (*objlist == NULL)
return -1;
for (i = 0, l = call_list; l; l = l->next, i++) {
v = l->data;
(*objlist)[i] = g_strdup(voicecall_build_path(modem, v->call));
}
return 0;
}
static gboolean voicecalls_have_active(struct voicecalls_data *calls)
{
GSList *l;
struct voicecall *v;
for (l = calls->call_list; l; l = l->next) {
v = l->data;
if (v->call->status == CALL_STATUS_ACTIVE ||
v->call->status == CALL_STATUS_INCOMING ||
v->call->status == CALL_STATUS_DIALING ||
v->call->status == CALL_STATUS_ALERTING)
return TRUE;
}
return FALSE;
}
static gboolean voicecalls_have_connected(struct voicecalls_data *calls)
{
GSList *l;
struct voicecall *v;
for (l = calls->call_list; l; l = l->next) {
v = l->data;
if (v->call->status == CALL_STATUS_ACTIVE)
return TRUE;
}
return FALSE;
}
static gboolean voicecalls_have_held(struct voicecalls_data *calls)
{
GSList *l;
struct voicecall *v;
for (l = calls->call_list; l; l = l->next) {
v = l->data;
if (v->call->status == CALL_STATUS_HELD)
return TRUE;
}
return FALSE;
}
static int voicecalls_num_with_status(struct voicecalls_data *calls,
int status)
{
GSList *l;
struct voicecall *v;
int num = 0;
for (l = calls->call_list; l; l = l->next) {
v = l->data;
if (v->call->status == status)
num += 1;
}
return num;
}
static int voicecalls_num_active(struct voicecalls_data *calls)
{
return voicecalls_num_with_status(calls, CALL_STATUS_ACTIVE);
}
static int voicecalls_num_held(struct voicecalls_data *calls)
{
return voicecalls_num_with_status(calls, CALL_STATUS_HELD);
}
static int voicecalls_num_connecting(struct voicecalls_data *calls)
{
int r = 0;
r += voicecalls_num_with_status(calls, CALL_STATUS_DIALING);
r += voicecalls_num_with_status(calls, CALL_STATUS_ALERTING);
return r;
}
static GSList *voicecalls_held_list(struct voicecalls_data *calls)
{
GSList *l;
GSList *r = NULL;
struct voicecall *v;
for (l = calls->call_list; l; l = l->next) {
v = l->data;
if (v->call->status == CALL_STATUS_HELD)
r = g_slist_prepend(r, v);
}
if (r)
r = g_slist_reverse(r);
return r;
}
/* Intended to be used for multiparty, which cannot be incoming,
* alerting or dialing */
static GSList *voicecalls_active_list(struct voicecalls_data *calls)
{
GSList *l;
GSList *r = NULL;
struct voicecall *v;
for (l = calls->call_list; l; l = l->next) {
v = l->data;
if (v->call->status == CALL_STATUS_ACTIVE)
r = g_slist_prepend(r, v);
}
if (r)
r = g_slist_reverse(r);
return r;
}
static gboolean voicecalls_have_waiting(struct voicecalls_data *calls)
{
GSList *l;
struct voicecall *v;
for (l = calls->call_list; l; l = l->next) {
v = l->data;
if (v->call->status == CALL_STATUS_WAITING)
return TRUE;
}
return FALSE;
}
static void voicecalls_release_queue(struct ofono_modem *modem, GSList *calls)
{
struct voicecalls_data *voicecalls = modem->voicecalls;
GSList *l;
g_slist_free(voicecalls->release_list);
voicecalls->release_list = NULL;
for (l = calls; l; l = l->next) {
voicecalls->release_list =
g_slist_prepend(voicecalls->release_list, l->data);
}
}
static void voicecalls_release_next(struct ofono_modem *modem)
{
struct voicecalls_data *voicecalls = modem->voicecalls;
struct voicecall *call;
if (!voicecalls->release_list)
return;
call = voicecalls->release_list->data;
voicecalls->release_list = g_slist_remove(voicecalls->release_list,
call);
voicecalls->ops->release_specific(modem, call->call->id,
multirelease_callback, modem);
}
static DBusMessage *manager_get_properties(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter dict;
char **callobj_list;
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,
PROPERTIES_ARRAY_SIGNATURE, &dict);
voicecalls_path_list(modem, calls->call_list, &callobj_list);
dbus_gsm_dict_append_array(&dict, "Calls", DBUS_TYPE_OBJECT_PATH,
&callobj_list);
dbus_gsm_free_string_array(callobj_list);
voicecalls_path_list(modem, calls->multiparty_list, &callobj_list);
dbus_gsm_dict_append_array(&dict, "MultipartyCalls",
DBUS_TYPE_OBJECT_PATH, &callobj_list);
dbus_gsm_free_string_array(callobj_list);
dbus_message_iter_close_container(&iter, &dict);
return reply;
}
static DBusMessage *manager_dial(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
const char *number;
struct ofono_phone_number ph;
const char *clirstr;
enum ofono_clir_option clir;
if (calls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
if (g_slist_length(calls->call_list) >= MAX_VOICE_CALLS)
return dbus_gsm_failed(msg);
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &number,
DBUS_TYPE_STRING, &clirstr,
DBUS_TYPE_INVALID) == FALSE)
return dbus_gsm_invalid_args(msg);
if (!valid_phone_number_format(number))
return dbus_gsm_invalid_format(msg);
if (strlen(clirstr) == 0 || !strcmp(clirstr, "default"))
clir = OFONO_CLIR_OPTION_DEFAULT;
else if (!strcmp(clirstr, "disabled"))
clir = OFONO_CLIR_OPTION_SUPPRESSION;
else if (!strcmp(clirstr, "enabled"))
clir = OFONO_CLIR_OPTION_INVOCATION;
else
return dbus_gsm_invalid_format(msg);
if (!calls->ops->dial)
return dbus_gsm_not_implemented(msg);
if (voicecalls_have_active(calls) &&
voicecalls_have_held(calls))
return dbus_gsm_failed(msg);
calls->flags |= VOICECALLS_FLAG_PENDING;
calls->pending = dbus_message_ref(msg);
string_to_phone_number(number, &ph);
calls->ops->dial(modem, &ph, clir, OFONO_CUG_OPTION_DEFAULT,
dial_callback, modem);
return NULL;
}
static DBusMessage *manager_transfer(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
int numactive;
int numheld;
if (calls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
numactive = voicecalls_num_active(calls);
/* According to 22.091 section 5.8, the network has the option of
* implementing the call transfer operation for a call that is
* still dialing/alerting.
*/
numactive += voicecalls_num_connecting(calls);
numheld = voicecalls_num_held(calls);
if ((numactive != 1) && (numheld != 1))
return dbus_gsm_failed(msg);
if (!calls->ops->transfer)
return dbus_gsm_not_implemented(msg);
calls->flags |= VOICECALLS_FLAG_PENDING;
calls->pending = dbus_message_ref(msg);
calls->ops->transfer(modem, generic_callback, calls);
return NULL;
}
static DBusMessage *manager_swap_calls(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
if (calls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
if (voicecalls_have_waiting(calls))
return dbus_gsm_failed(msg);
if (!calls->ops->hold_all_active)
return dbus_gsm_not_implemented(msg);
calls->flags |= VOICECALLS_FLAG_PENDING;
calls->pending = dbus_message_ref(msg);
calls->ops->hold_all_active(modem, generic_callback, calls);
return NULL;
}
static DBusMessage *manager_release_and_answer(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
if (calls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
if (!voicecalls_have_active(calls) || !voicecalls_have_waiting(calls))
return dbus_gsm_failed(msg);
if (!calls->ops->release_all_active)
return dbus_gsm_not_implemented(msg);
calls->flags |= VOICECALLS_FLAG_PENDING;
calls->pending = dbus_message_ref(msg);
calls->ops->release_all_active(modem, generic_callback, calls);
return NULL;
}
static DBusMessage *manager_hold_and_answer(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
if (calls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
if (voicecalls_have_active(calls) && voicecalls_have_held(calls) &&
voicecalls_have_waiting(calls))
return dbus_gsm_failed(msg);
if (!calls->ops->hold_all_active)
return dbus_gsm_not_implemented(msg);
calls->flags |= VOICECALLS_FLAG_PENDING;
calls->pending = dbus_message_ref(msg);
calls->ops->hold_all_active(modem, generic_callback, calls);
return NULL;
}
static DBusMessage *manager_hangup_all(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
if (calls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
if (!calls->ops->release_specific)
return dbus_gsm_not_implemented(msg);
if (g_slist_length(calls->call_list) == 0) {
DBusMessage *reply = dbus_message_new_method_return(msg);
return reply;
}
calls->flags |= VOICECALLS_FLAG_PENDING;
calls->flags |= VOICECALLS_FLAG_MULTI_RELEASE;
calls->pending = dbus_message_ref(msg);
voicecalls_release_queue(modem, calls->call_list);
voicecalls_release_next(modem);
return NULL;
}
static DBusMessage *multiparty_private_chat(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
const char *callpath;
const char *c;
unsigned int id;
GSList *l;
if (calls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &callpath,
DBUS_TYPE_INVALID) == FALSE)
return dbus_gsm_invalid_args(msg);
if (strlen(callpath) == 0 || strlen(callpath) > MAX_DBUS_PATH_LEN)
return dbus_gsm_invalid_format(msg);
c = strrchr(callpath, '/');
if (!c || strncmp(modem->path, callpath, c-callpath))
return dbus_gsm_not_found(msg);
if (!sscanf(c, "/voicecall%2u", &id))
return dbus_gsm_not_found(msg);
for (l = calls->multiparty_list; l; l = l->next) {
struct voicecall *v = l->data;
if (v->call->id == id)
break;
}
if (!l)
return dbus_gsm_not_found(msg);
/* If we found id on the list of multiparty calls, then by definition
* the multiparty call exists. Only thing to check is whether we have
* held calls
*/
if (voicecalls_have_held(calls))
return dbus_gsm_failed(msg);
if (!calls->ops->private_chat)
return dbus_gsm_not_implemented(msg);
calls->flags |= VOICECALLS_FLAG_PENDING;
calls->pending = dbus_message_ref(msg);
calls->ops->private_chat(modem, id, private_chat_callback, modem);
return NULL;
}
static DBusMessage *multiparty_create(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
if (calls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
if (!voicecalls_have_held(calls) || !voicecalls_have_active(calls))
return dbus_gsm_failed(msg);
if (!calls->ops->create_multiparty)
return dbus_gsm_not_implemented(msg);
calls->flags |= VOICECALLS_FLAG_PENDING;
calls->pending = dbus_message_ref(msg);
calls->ops->create_multiparty(modem, multiparty_create_callback, modem);
return NULL;
}
static DBusMessage *multiparty_hangup(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
if (calls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
if (!calls->ops->release_specific)
return dbus_gsm_not_implemented(msg);
if (!calls->ops->release_all_held)
return dbus_gsm_not_implemented(msg);
if (!calls->ops->release_all_active)
return dbus_gsm_not_implemented(msg);
if (g_slist_length(calls->multiparty_list) == 0) {
DBusMessage *reply = dbus_message_new_method_return(msg);
return reply;
}
calls->flags |= VOICECALLS_FLAG_PENDING;
calls->pending = dbus_message_ref(msg);
/* We don't have waiting calls, as we can't use +CHLD to release */
if (!voicecalls_have_waiting(calls)) {
struct voicecall *v = calls->multiparty_list->data;
if (v->call->status == CALL_STATUS_HELD) {
calls->ops->release_all_held(modem, generic_callback,
calls);
goto out;
}
/* Multiparty is currently active, if we have held calls
* we shouldn't use release_all_active here since this also
* has the side-effect of activating held calls
*/
if (!voicecalls_have_held(calls)) {
calls->ops->release_all_active(modem, generic_callback,
calls);
goto out;
}
}
/* Fall back to the old-fashioned way */
calls->flags |= VOICECALLS_FLAG_MULTI_RELEASE;
voicecalls_release_queue(modem, calls->multiparty_list);
voicecalls_release_next(modem);
out:
return NULL;
}
static DBusMessage *manager_tone(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
const char *in_tones;
char *tones;
int i, len;
if (calls->flags & VOICECALLS_FLAG_PENDING)
return dbus_gsm_busy(msg);
if (!calls->ops->send_tones)
return dbus_gsm_not_implemented(msg);
/* Send DTMFs only if we have at least one connected call */
if (!voicecalls_have_connected(calls))
return dbus_gsm_failed(msg);
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &in_tones,
DBUS_TYPE_INVALID) == FALSE)
return dbus_gsm_invalid_args(msg);
len = strlen(in_tones);
if (len == 0)
return dbus_gsm_invalid_format(msg);
tones = g_ascii_strup(in_tones, len);
/* Tones can be 0-9, *, #, A-D according to 27.007 C.2.11 */
for (i = 0; i < len; i++) {
if (g_ascii_isdigit(tones[i]) ||
tones[i] == '*' || tones[i] == '#' ||
(tones[i] >= 'A' && tones[i] <= 'D'))
continue;
g_free(tones);
return dbus_gsm_invalid_format(msg);
}
calls->flags |= VOICECALLS_FLAG_PENDING;
calls->pending = dbus_message_ref(msg);
calls->ops->send_tones(modem, tones, generic_callback, calls);
g_free(tones);
return NULL;
}
static GDBusMethodTable manager_methods[] = {
{ "GetProperties", "", "a{sv}", manager_get_properties },
{ "Dial", "ss", "o", manager_dial,
G_DBUS_METHOD_FLAG_ASYNC },
{ "Transfer", "", "", manager_transfer,
G_DBUS_METHOD_FLAG_ASYNC },
{ "SwapCalls", "", "", manager_swap_calls,
G_DBUS_METHOD_FLAG_ASYNC },
{ "ReleaseAndAnswer", "", "", manager_release_and_answer,
G_DBUS_METHOD_FLAG_ASYNC },
{ "HoldAndAnswer", "", "", manager_hold_and_answer,
G_DBUS_METHOD_FLAG_ASYNC },
{ "HangupAll", "", "", manager_hangup_all,
G_DBUS_METHOD_FLAG_ASYNC },
{ "PrivateChat", "o", "ao", multiparty_private_chat,
G_DBUS_METHOD_FLAG_ASYNC },
{ "CreateMultiparty", "", "ao", multiparty_create,
G_DBUS_METHOD_FLAG_ASYNC },
{ "HangupMultiparty", "", "", multiparty_hangup,
G_DBUS_METHOD_FLAG_ASYNC },
{ "SendTones", "s", "", manager_tone,
G_DBUS_METHOD_FLAG_ASYNC },
{ }
};
static GDBusSignalTable manager_signals[] = {
{ "PropertyChanged", "sv" },
{ }
};
static gboolean real_emit_call_list_changed(void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *voicecalls = modem->voicecalls;
DBusConnection *conn = dbus_gsm_connection();
char **objpath_list;
voicecalls_path_list(modem, voicecalls->call_list, &objpath_list);
dbus_gsm_signal_array_property_changed(conn, modem->path,
VOICECALL_MANAGER_INTERFACE,
"Calls",
DBUS_TYPE_OBJECT_PATH,
&objpath_list);
dbus_gsm_free_string_array(objpath_list);
ofono_debug("Resetting updating flag");
voicecalls->flags &= ~VOICECALLS_FLAG_UPDATING_CALL_LIST;
return FALSE;
}
static void emit_call_list_changed(struct ofono_modem *modem)
{
#ifdef DELAY_EMIT
struct voicecalls_data *calls = modem->voicecalls;
if (!(calls->flags & VOICECALLS_FLAG_UPDATING_CALL_LIST)) {
calls->flags |= VOICECALLS_FLAG_UPDATING_CALL_LIST;
g_timeout_add(0, real_emit_call_list_changed, modem);
}
#else
real_emit_call_list_changed(modem);
#endif
}
static gboolean real_emit_multiparty_call_list_changed(void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *voicecalls = modem->voicecalls;
DBusConnection *conn = dbus_gsm_connection();
char **objpath_list;
voicecalls_path_list(modem, voicecalls->multiparty_list, &objpath_list);
dbus_gsm_signal_array_property_changed(conn, modem->path,
VOICECALL_MANAGER_INTERFACE, "MultipartyCalls",
DBUS_TYPE_OBJECT_PATH,
&objpath_list);
dbus_gsm_free_string_array(objpath_list);
voicecalls->flags &= ~VOICECALLS_FLAG_UPDATING_MPTY_CALL_LIST;
return FALSE;
}
static void emit_multiparty_call_list_changed(struct ofono_modem *modem)
{
#ifdef DELAY_EMIT
struct voicecalls_data *calls = modem->voicecalls;
if (!(calls->flags & VOICECALLS_FLAG_UPDATING_MPTY_CALL_LIST)) {
calls->flags |= VOICECALLS_FLAG_UPDATING_MPTY_CALL_LIST;
g_timeout_add(0, real_emit_multiparty_call_list_changed, modem);
}
#else
real_emit_multiparty_call_list_changed(modem);
#endif
}
void ofono_voicecall_disconnected(struct ofono_modem *modem, int id,
enum ofono_disconnect_reason reason,
const struct ofono_error *error)
{
GSList *l;
struct voicecalls_data *calls = modem->voicecalls;
struct voicecall *call;
time_t ts;
enum call_status prev_status;
ofono_debug("Got disconnection event for id: %d, reason: %d", id, reason);
l = g_slist_find_custom(calls->call_list, GINT_TO_POINTER(id),
call_compare_by_id);
if (!l) {
ofono_error("Plugin notified us of call disconnect for"
" unknown call");
return;
}
call = l->data;
ts = time(NULL);
prev_status = call->call->status;
l = g_slist_find_custom(calls->multiparty_list, GINT_TO_POINTER(id),
call_compare_by_id);
if (l) {
calls->multiparty_list =
g_slist_remove(calls->multiparty_list, call);
if (calls->multiparty_list->next == NULL) { /* Size == 1 */
g_slist_free(calls->multiparty_list);
calls->multiparty_list = 0;
}
emit_multiparty_call_list_changed(modem);
}
calls->release_list = g_slist_remove(calls->release_list, call);
modem_release_callid(modem, id);
/* TODO: Emit disconnect reason */
voicecall_set_call_status(modem, call, CALL_STATUS_DISCONNECTED);
if (prev_status == CALL_STATUS_INCOMING)
ofono_history_call_missed(modem, call->call, ts);
else
ofono_history_call_ended(modem, call->call,
call->detect_time, ts);
voicecall_dbus_unregister(modem, call);
calls->call_list = g_slist_remove(calls->call_list, call);
emit_call_list_changed(modem);
}
void ofono_voicecall_notify(struct ofono_modem *modem, const struct ofono_call *call)
{
GSList *l;
struct voicecalls_data *calls = modem->voicecalls;
struct voicecall *v = NULL;
struct ofono_call *newcall = NULL;
ofono_debug("Got a voicecall event, status: %d, id: %u, number: %s",
call->status, call->id, call->phone_number.number);
l = g_slist_find_custom(calls->call_list, GINT_TO_POINTER(call->id),
call_compare_by_id);
if (l) {
ofono_debug("Found call with id: %d\n", call->id);
voicecall_set_call_status(modem, l->data, call->status);
voicecall_set_call_lineid(modem, l->data, &call->phone_number,
call->clip_validity);
return;
}
ofono_debug("Did not find a call with id: %d\n", call->id);
newcall = g_try_new0(struct ofono_call, 1);
if (!call) {
ofono_error("Unable to allocate call");
goto err;
}
memcpy(newcall, call, sizeof(struct ofono_call));
if (modem_alloc_callid(modem) != call->id) {
ofono_error("Warning: Call id and internally tracked id"
" do not correspond");
goto err;
}
v = voicecall_create(modem, newcall);
if (!v) {
ofono_error("Unable to allocate voicecall_data");
goto err;
}
v->detect_time = time(NULL);
if (!voicecall_dbus_register(v)) {
ofono_error("Unable to register voice call");
goto err;
}
calls->call_list = g_slist_insert_sorted(calls->call_list, v,
call_compare);
emit_call_list_changed(modem);
return;
err:
if (newcall)
g_free(newcall);
if (v)
g_free(v);
}
static void generic_callback(const struct ofono_error *error, void *data)
{
struct voicecalls_data *calls = data;
DBusConnection *conn = dbus_gsm_connection();
DBusMessage *reply;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
ofono_debug("command failed with error: %s",
telephony_error_to_str(error));
calls->flags &= ~VOICECALLS_FLAG_PENDING;
if (!calls->pending)
return;
if (error->type == OFONO_ERROR_TYPE_NO_ERROR)
reply = dbus_message_new_method_return(calls->pending);
else
reply = dbus_gsm_failed(calls->pending);
g_dbus_send_message(conn, reply);
dbus_message_unref(calls->pending);
calls->pending = NULL;
}
static void multirelease_callback(const struct ofono_error *error, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
DBusConnection *conn = dbus_gsm_connection();
DBusMessage *reply;
if (g_slist_length(calls->release_list)) {
voicecalls_release_next(modem);
return;
}
calls->flags &= ~VOICECALLS_FLAG_MULTI_RELEASE;
calls->flags &= ~VOICECALLS_FLAG_PENDING;
if (!calls->pending)
return;
reply = dbus_message_new_method_return(calls->pending);
g_dbus_send_message(conn, reply);
dbus_message_unref(calls->pending);
calls->pending = NULL;
}
static struct ofono_call *synthesize_outgoing_call(struct ofono_modem *modem,
DBusMessage *msg)
{
const char *number;
struct ofono_call *call;
call = g_try_new0(struct ofono_call, 1);
if (!call)
return call;
call->id = modem_alloc_callid(modem);
if (call->id == 0) {
ofono_error("Failed to alloc callid, too many calls");
g_free(call);
return NULL;
}
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &number,
DBUS_TYPE_INVALID) == FALSE)
number = "";
else
string_to_phone_number(number, &call->phone_number);
call->direction = CALL_DIRECTION_MOBILE_ORIGINATED;
call->status = CALL_STATUS_DIALING;
call->clip_validity = CLIP_VALIDITY_VALID;
return call;
}
static void dial_callback(const struct ofono_error *error, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
DBusConnection *conn = dbus_gsm_connection();
DBusMessage *reply;
GSList *l;
struct ofono_call *call;
const char *path;
gboolean need_to_emit = FALSE;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
ofono_debug("Dial callback returned error: %s",
telephony_error_to_str(error));
calls->flags &= ~VOICECALLS_FLAG_PENDING;
if (!calls->pending)
return;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
reply = dbus_gsm_failed(calls->pending);
g_dbus_send_message(conn, reply);
goto out;
}
reply = dbus_message_new_method_return(calls->pending);
if (!reply)
goto out;
/* Two things can happen, the call notification arrived before dial
* callback or dial callback was first. Handle here */
for (l = calls->call_list; l; l = l->next) {
struct voicecall *v = l->data;
if (v->call->status == CALL_STATUS_DIALING ||
v->call->status == CALL_STATUS_ALERTING)
break;
}
if (!l) {
struct voicecall *v;
call = synthesize_outgoing_call(modem, calls->pending);
if (!call) {
reply = dbus_gsm_failed(calls->pending);
g_dbus_send_message(conn, reply);
goto out;
}
v = voicecall_create(modem, call);
if (!v) {
reply = dbus_gsm_failed(calls->pending);
g_dbus_send_message(conn, reply);
goto out;
}
v->detect_time = time(NULL);
ofono_debug("Registering new call: %d", call->id);
voicecall_dbus_register(v);
calls->call_list = g_slist_insert_sorted(calls->call_list, v,
call_compare);
need_to_emit = TRUE;
} else {
struct voicecall *v = l->data;
call = v->call;
}
path = voicecall_build_path(modem, call);
dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
DBUS_TYPE_INVALID);
g_dbus_send_message(conn, reply);
if (need_to_emit)
emit_call_list_changed(modem);
out:
dbus_message_unref(calls->pending);
calls->pending = NULL;
}
static void multiparty_callback_common(struct ofono_modem *modem,
DBusMessage *reply)
{
struct voicecalls_data *voicecalls = modem->voicecalls;
DBusMessageIter iter;
DBusMessageIter array_iter;
char **objpath_list;
int i;
voicecalls_path_list(modem, voicecalls->multiparty_list, &objpath_list);
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; objpath_list[i]; i++)
dbus_message_iter_append_basic(&array_iter,
DBUS_TYPE_OBJECT_PATH, &objpath_list[i]);
dbus_message_iter_close_container(&iter, &array_iter);
}
static void multiparty_create_callback(const struct ofono_error *error, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
DBusConnection *conn = dbus_gsm_connection();
DBusMessage *reply;
gboolean need_to_emit = FALSE;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
ofono_debug("command failed with error: %s",
telephony_error_to_str(error));
calls->flags &= ~VOICECALLS_FLAG_PENDING;
if (!calls->pending)
return;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
reply = dbus_gsm_failed(calls->pending);
goto out;
}
/* We just created a multiparty call, gather all held
* active calls and add them to the multiparty list
*/
if (calls->multiparty_list) {
g_slist_free(calls->multiparty_list);
calls->multiparty_list = 0;
}
calls->multiparty_list = g_slist_concat(calls->multiparty_list,
voicecalls_held_list(calls));
calls->multiparty_list = g_slist_concat(calls->multiparty_list,
voicecalls_active_list(calls));
calls->multiparty_list = g_slist_sort(calls->multiparty_list,
call_compare);
if (g_slist_length(calls->multiparty_list) < 2) {
ofono_error("Created multiparty call, but size is less than 2"
" panic!");
reply = dbus_gsm_failed(calls->pending);
} else {
reply = dbus_message_new_method_return(calls->pending);
multiparty_callback_common(modem, reply);
need_to_emit = TRUE;
}
out:
g_dbus_send_message(conn, reply);
if (need_to_emit)
emit_multiparty_call_list_changed(modem);
dbus_message_unref(calls->pending);
calls->pending = NULL;
}
static void private_chat_callback(const struct ofono_error *error, void *data)
{
struct ofono_modem *modem = data;
struct voicecalls_data *calls = modem->voicecalls;
DBusConnection *conn = dbus_gsm_connection();
DBusMessage *reply;
gboolean need_to_emit = FALSE;
const char *callpath;
const char *c;
int id;
GSList *l;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR)
ofono_debug("command failed with error: %s",
telephony_error_to_str(error));
calls->flags &= ~VOICECALLS_FLAG_PENDING;
if (!calls->pending)
return;
if (error->type != OFONO_ERROR_TYPE_NO_ERROR) {
reply = dbus_gsm_failed(calls->pending);
goto out;
}
dbus_message_get_args(calls->pending, NULL,
DBUS_TYPE_OBJECT_PATH, &callpath,
DBUS_TYPE_INVALID);
c = strrchr(callpath, '/');
sscanf(c, "/voicecall%2u", &id);
l = g_slist_find_custom(calls->multiparty_list, GINT_TO_POINTER(id),
call_compare_by_id);
if (l) {
calls->multiparty_list =
g_slist_remove(calls->multiparty_list, l->data);
if (g_slist_length(calls->multiparty_list) < 2) {
g_slist_free(calls->multiparty_list);
calls->multiparty_list = 0;
}
}
reply = dbus_message_new_method_return(calls->pending);
multiparty_callback_common(modem, reply);
need_to_emit = TRUE;
out:
g_dbus_send_message(conn, reply);
if (need_to_emit)
emit_multiparty_call_list_changed(modem);
dbus_message_unref(calls->pending);
calls->pending = NULL;
}
int ofono_voicecall_register(struct ofono_modem *modem, struct ofono_voicecall_ops *ops)
{
DBusConnection *conn = dbus_gsm_connection();
if (modem == NULL)
return -1;
if (ops == NULL)
return -1;
modem->voicecalls = voicecalls_create();
if (modem->voicecalls == NULL)
return -1;
modem->voicecalls->ops = ops;
if (!g_dbus_register_interface(conn, modem->path,
VOICECALL_MANAGER_INTERFACE,
manager_methods, manager_signals, NULL,
modem, voicecalls_destroy)) {
ofono_error("Could not create %s interface",
VOICECALL_MANAGER_INTERFACE);
voicecalls_destroy(modem->voicecalls);
return -1;
}
modem_add_interface(modem, VOICECALL_MANAGER_INTERFACE);
return 0;
}
void ofono_voicecall_unregister(struct ofono_modem *modem)
{
DBusConnection *conn = dbus_gsm_connection();
if (!modem->voicecalls)
return;
modem_remove_interface(modem, VOICECALL_MANAGER_INTERFACE);
g_dbus_unregister_interface(conn, modem->path,
VOICECALL_MANAGER_INTERFACE);
modem->voicecalls = NULL;
}
|