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
|
/* gsmd unix domain socket handling
*
* (C) 2006-2007 by OpenMoko, Inc.
* Written by Harald Welte <laforge@openmoko.org>
* 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 as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "gsmd.h"
#include <gsmd/gsmd.h>
#include <gsmd/usock.h>
#include <gsmd/select.h>
#include <gsmd/atcmd.h>
#include <gsmd/usock.h>
#include <gsmd/talloc.h>
#include <gsmd/extrsp.h>
#include <gsmd/ts0707.h>
#include <gsmd/sms.h>
static void *__ucmd_ctx, *__gu_ctx;
struct gsmd_ucmd *ucmd_alloc(int extra_size)
{
return talloc_size(__ucmd_ctx,
sizeof(struct gsmd_ucmd) + extra_size);
}
void usock_cmd_enqueue(struct gsmd_ucmd *ucmd, struct gsmd_user *gu)
{
DEBUGP("enqueueing usock cmd %p for user %p\n", ucmd, gu);
/* add to per-user list of finished cmds */
llist_add_tail(&ucmd->list, &gu->finished_ucmds);
/* mark socket of user as we-want-to-write */
gu->gfd.when |= GSMD_FD_WRITE;
}
/* callback for completed passthrough gsmd_atcmd's */
static int usock_cmd_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
int rlen = strlen(resp)+1;
struct gsmd_ucmd *ucmd = ucmd_alloc(rlen);
DEBUGP("entering(cmd=%p, gu=%p)\n", cmd, gu);
if (!ucmd)
return -ENOMEM;
/* FIXME: pass error values back somehow */
ucmd->hdr.version = GSMD_PROTO_VERSION;
ucmd->hdr.msg_type = GSMD_MSG_PASSTHROUGH;
ucmd->hdr.msg_subtype = GSMD_PASSTHROUGH_RESP;
ucmd->hdr.len = rlen;
ucmd->hdr.id = cmd->id;
memcpy(ucmd->buf, resp, ucmd->hdr.len);
usock_cmd_enqueue(ucmd, gu);
return 0;
}
typedef int usock_msg_handler(struct gsmd_user *gu, struct gsmd_msg_hdr *gph, int len);
static int usock_rcv_passthrough(struct gsmd_user *gu, struct gsmd_msg_hdr *gph, int len)
{
struct gsmd_atcmd *cmd;
cmd = atcmd_fill((char *)gph+sizeof(*gph), gph->len, &usock_cmd_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
DEBUGP("submitting cmd=%p, gu=%p\n", cmd, gu);
return atcmd_submit(gu->gsmd, cmd);
}
static int usock_rcv_event(struct gsmd_user *gu, struct gsmd_msg_hdr *gph, int len)
{
u_int32_t *evtmask = (u_int32_t *) ((char *)gph + sizeof(*gph));
if (len < sizeof(*gph) + sizeof(u_int32_t))
return -EINVAL;
if (gph->msg_subtype != GSMD_EVT_SUBSCRIPTIONS)
return -EINVAL;
gu->subscriptions = *evtmask;
return 0;
}
static int voicecall_get_stat_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
struct gsmd_call_status gcs;
struct gsm_extrsp *er;
DEBUGP("resp: %s\n", resp);
er = extrsp_parse(cmd, resp);
if ( !er )
return -ENOMEM;
gcs.is_last = (cmd->ret == 0 || cmd->ret == 4)? 1:0;
if ( !strncmp(resp, "OK", 2) ) {
/* No existing call */
gcs.idx = 0;
}
else if ( !strncmp(resp, "+CME", 4) ) {
/* +CME ERROR: <err> */
DEBUGP("+CME error\n");
gcs.idx = 0 - atoi(strpbrk(resp, "0123456789"));
}
else if ( er->num_tokens == 7 &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[2].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[3].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[4].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[5].type == GSMD_ECMD_RTT_STRING &&
er->tokens[6].type == GSMD_ECMD_RTT_NUMERIC ) {
/*
* [+CLCC: <id1>,<dir>,<stat>,<mode>,<mpty>[,
* <number>,<type>[,<alpha>]]
* [<CR><LF>+CLCC: <id2>,<dir>,<stat>,<mode>,<mpty>[,
* <number>,<type>[,<alpha>]]
* [...]]]
*/
gcs.idx = er->tokens[0].u.numeric;
gcs.dir = er->tokens[1].u.numeric;
gcs.stat = er->tokens[2].u.numeric;
gcs.mode = er->tokens[3].u.numeric;
gcs.mpty = er->tokens[4].u.numeric;
strlcpy(gcs.number, er->tokens[5].u.string, GSMD_ADDR_MAXLEN+1);
gcs.type = er->tokens[6].u.numeric;
}
else if ( er->num_tokens == 8 &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[2].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[3].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[4].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[5].type == GSMD_ECMD_RTT_STRING &&
er->tokens[6].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[7].type == GSMD_ECMD_RTT_STRING ) {
/*
* [+CLCC: <id1>,<dir>,<stat>,<mode>,<mpty>[,
* <number>,<type>[,<alpha>]]
* [<CR><LF>+CLCC: <id2>,<dir>,<stat>,<mode>,<mpty>[,
* <number>,<type>[,<alpha>]]
* [...]]]
*/
gcs.idx = er->tokens[0].u.numeric;
gcs.dir = er->tokens[1].u.numeric;
gcs.stat = er->tokens[2].u.numeric;
gcs.mode = er->tokens[3].u.numeric;
gcs.mpty = er->tokens[4].u.numeric;
strlcpy(gcs.number, er->tokens[5].u.string, GSMD_ADDR_MAXLEN+1);
gcs.type = er->tokens[6].u.numeric;
strlcpy(gcs.alpha, er->tokens[7].u.string, GSMD_ALPHA_MAXLEN+1);
}
else {
DEBUGP("Invalid Input : Parse error\n");
return -EINVAL;
}
talloc_free(er);
return gsmd_ucmd_submit(gu, GSMD_MSG_VOICECALL, GSMD_VOICECALL_GET_STAT,
cmd->id, sizeof(gcs), &gcs);
}
static int voicecall_ctrl_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
int ret = 0;
DEBUGP("resp: %s\n", resp);
if ( !strncmp(resp, "+CME", 4) ) {
/* +CME ERROR: <err> */
DEBUGP("+CME error\n");
ret = atoi(strpbrk(resp, "0123456789"));
}
return gsmd_ucmd_submit(gu, GSMD_MSG_VOICECALL, GSMD_VOICECALL_CTRL,
cmd->id, sizeof(ret), &ret);
}
static int voicecall_fwd_stat_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
struct gsm_extrsp *er;
struct gsmd_call_fwd_stat gcfs;
DEBUGP("resp: %s\n", resp);
er = extrsp_parse(cmd, resp);
if ( !er )
return -ENOMEM;
gcfs.is_last = (cmd->ret == 0 || cmd->ret == 4)? 1:0;
if ( er->num_tokens == 2 &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_NUMERIC ) {
/*
* +CCFC: <status>,<class1>[,<number>,<type>
* [,<subaddr>,<satype>[,<time>]]][
* <CR><LF>+CCFC: <status>,<class2>[,<number>,<type>
* [,<subaddr>,<satype>[,<time>]]]
* [...]]
*/
gcfs.status = er->tokens[0].u.numeric;
gcfs.classx = er->tokens[1].u.numeric;
gcfs.addr.number[0] = '\0';
}
else if ( er->num_tokens == 4 &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[2].type == GSMD_ECMD_RTT_STRING &&
er->tokens[3].type == GSMD_ECMD_RTT_NUMERIC ) {
gcfs.status = er->tokens[0].u.numeric;
gcfs.classx = er->tokens[1].u.numeric;
strlcpy(gcfs.addr.number, er->tokens[2].u.string, GSMD_ADDR_MAXLEN+1);
gcfs.addr.type = er->tokens[3].u.numeric;
}
else if ( er->num_tokens == 7 &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[2].type == GSMD_ECMD_RTT_STRING &&
er->tokens[3].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[4].type == GSMD_ECMD_RTT_EMPTY &&
er->tokens[5].type == GSMD_ECMD_RTT_EMPTY &&
er->tokens[6].type == GSMD_ECMD_RTT_NUMERIC ) {
gcfs.status = er->tokens[0].u.numeric;
gcfs.classx = er->tokens[1].u.numeric;
strlcpy(gcfs.addr.number, er->tokens[2].u.string, GSMD_ADDR_MAXLEN+1);
gcfs.addr.type = er->tokens[3].u.numeric;
gcfs.time = er->tokens[6].u.numeric;
}
else {
DEBUGP("Invalid Input : Parse error\n");
return -EINVAL;
}
talloc_free(er);
return gsmd_ucmd_submit(gu, GSMD_MSG_VOICECALL, GSMD_VOICECALL_FWD_STAT,
cmd->id, sizeof(gcfs), &gcfs);
}
static int usock_ringing_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
/* If the incoming call answer/rejection succeeded then we
* know the modem isn't ringing and we update the state info. */
if (cmd->ret == 0)
gu->gsmd->dev_state.ringing = 0;
return usock_cmd_cb(cmd, ctx, resp);
}
static int usock_rcv_voicecall(struct gsmd_user *gu, struct gsmd_msg_hdr *gph,
int len)
{
struct gsmd_atcmd *cmd = NULL;
struct gsmd_addr *ga;
struct gsmd_dtmf *gd;
struct gsmd_call_ctrl *gcc;
struct gsmd_call_fwd_reg *gcfr;
char buf[64];
int atcmd_len;
int *reason;
switch (gph->msg_subtype) {
case GSMD_VOICECALL_DIAL:
if (len < sizeof(*gph) + sizeof(*ga))
return -EINVAL;
ga = (struct gsmd_addr *) ((void *)gph + sizeof(*gph));
ga->number[GSMD_ADDR_MAXLEN] = '\0';
cmd = atcmd_fill("ATD", 5 + strlen(ga->number),
&usock_cmd_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "ATD%s;", ga->number);
/* FIXME: number type! */
break;
case GSMD_VOICECALL_HANGUP:
/* ATH0 is not supported by QC, we hope ATH is supported by everone */
cmd = atcmd_fill("ATH", 4,
gu->gsmd->dev_state.ringing ?
usock_ringing_cb : usock_cmd_cb,
gu, gph->id,NULL);
/* This command is special because it needs to be sent to
* the MS even if a command is currently executing. */
if (cmd) {
return cancel_atcmd(gu->gsmd, cmd);
}
break;
case GSMD_VOICECALL_ANSWER:
cmd = atcmd_fill("ATA", 4, &usock_ringing_cb, gu, gph->id,NULL);
break;
case GSMD_VOICECALL_DTMF:
if (len < sizeof(*gph) + sizeof(*gd))
return -EINVAL;
gd = (struct gsmd_dtmf *) ((void *)gph + sizeof(*gph));
if (len < sizeof(*gph) + sizeof(*gd) + gd->len)
return -EINVAL;
/* FIXME: we don't yet support DTMF of multiple digits */
if (gd->len != 1)
return -EINVAL;
atcmd_len = 1 + strlen("AT+VTS=") + (gd->len * 2);
cmd = atcmd_fill("AT+VTS=", atcmd_len, &usock_cmd_cb,
gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "AT+VTS=%c;", gd->dtmf[0]);
break;
case GSMD_VOICECALL_GET_STAT:
cmd = atcmd_fill("AT+CLCC", 7+1, &voicecall_get_stat_cb,
gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
break;
case GSMD_VOICECALL_CTRL:
if (len < sizeof(*gph) + sizeof(*gcc))
return -EINVAL;
gcc = (struct gsmd_call_ctrl *) ((void *)gph + sizeof(*gph));
atcmd_len = 1 + strlen("AT+CHLD=") + 2;
cmd = atcmd_fill("AT+CHLD=", atcmd_len, &voicecall_ctrl_cb,
gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
switch (gcc->proc) {
case GSMD_CALL_CTRL_R_HLDS:
case GSMD_CALL_CTRL_UDUB:
sprintf(cmd->buf, "AT+CHLD=%d", 0);
break;
case GSMD_CALL_CTRL_R_ACTS_A_HLD_WAIT:
sprintf(cmd->buf, "AT+CHLD=%d", 1);
break;
case GSMD_CALL_CTRL_R_ACT_X:
sprintf(cmd->buf, "AT+CHLD=%d%d", 1, gcc->idx);
break;
case GSMD_CALL_CTRL_H_ACTS_A_HLD_WAIT:
sprintf(cmd->buf, "AT+CHLD=%d", 2);
break;
case GSMD_CALL_CTRL_H_ACTS_EXCEPT_X:
sprintf(cmd->buf, "AT+CHLD=%d%d", 2, gcc->idx);
break;
case GSMD_CALL_CTRL_M_HELD:
sprintf(cmd->buf, "AT+CHLD=%d", 3);
break;
default:
return -EINVAL;
}
break;
case GSMD_VOICECALL_FWD_DIS:
if(len < sizeof(*gph) + sizeof(int))
return -EINVAL;
reason = (int *) ((void *)gph + sizeof(*gph));
sprintf(buf, "%d,0", *reason);
atcmd_len = 1 + strlen("AT+CCFC=") + strlen(buf);
cmd = atcmd_fill("AT+CCFC=", atcmd_len,
&usock_cmd_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "AT+CCFC=%s", buf);
break;
case GSMD_VOICECALL_FWD_EN:
if(len < sizeof(*gph) + sizeof(int))
return -EINVAL;
reason = (int *) ((void *)gph + sizeof(*gph));
sprintf(buf, "%d,1", *reason);
atcmd_len = 1 + strlen("AT+CCFC=") + strlen(buf);
cmd = atcmd_fill("AT+CCFC=", atcmd_len,
&usock_cmd_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "AT+CCFC=%s", buf);
break;
case GSMD_VOICECALL_FWD_STAT:
if(len < sizeof(*gph) + sizeof(int))
return -EINVAL;
reason = (int *) ((void *)gph + sizeof(*gph));
sprintf(buf, "%d,2", *reason);
atcmd_len = 1 + strlen("AT+CCFC=") + strlen(buf);
cmd = atcmd_fill("AT+CCFC=", atcmd_len,
&voicecall_fwd_stat_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "AT+CCFC=%s", buf);
break;
case GSMD_VOICECALL_FWD_REG:
if(len < sizeof(*gph) + sizeof(int))
return -EINVAL;
gcfr = (struct gsmd_call_fwd_reg *) ((void *)gph + sizeof(*gph));
sprintf(buf, "%d,3,\"%s\"", gcfr->reason, gcfr->addr.number);
atcmd_len = 1 + strlen("AT+CCFC=") + strlen(buf);
cmd = atcmd_fill("AT+CCFC=", atcmd_len,
&usock_cmd_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "AT+CCFC=%s", buf);
break;
case GSMD_VOICECALL_FWD_ERAS:
if(len < sizeof(*gph) + sizeof(int))
return -EINVAL;
reason = (int *) ((void *)gph + sizeof(*gph));
sprintf(buf, "%d,4", *reason);
atcmd_len = 1 + strlen("AT+CCFC=") + strlen(buf);
cmd = atcmd_fill("AT+CCFC=", atcmd_len,
&usock_cmd_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "AT+CCFC=%s", buf);
break;
default:
return -EINVAL;
}
if (cmd)
return atcmd_submit(gu->gsmd, cmd);
else
return -ENOMEM;
}
static int null_cmd_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
gsmd_log(GSMD_DEBUG, "null cmd cb\n");
return 0;
}
/* PIN command callback. Gets called for response to AT+CPIN cmcd */
static int pin_cmd_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
int ret = cmd->ret;
/* Pass a GSM07.07 CME code directly, don't issue a new PIN
* request because the client waits for a response to her
* PIN submission rather than an event. */
return gsmd_ucmd_submit(gu, GSMD_MSG_PIN, GSMD_PIN_INPUT,
cmd->id, sizeof(ret), &ret);
}
static const char *pin_type_names[__NUM_GSMD_PIN] = {
[GSMD_PIN_READY] = "READY",
[GSMD_PIN_SIM_PIN] = "SIM PIN",
[GSMD_PIN_SIM_PUK] = "SIM PUK",
[GSMD_PIN_PH_SIM_PIN] = "Phone-to-SIM PIN",
[GSMD_PIN_PH_FSIM_PIN] = "Phone-to-very-first SIM PIN",
[GSMD_PIN_PH_FSIM_PUK] = "Phone-to-very-first SIM PUK",
[GSMD_PIN_SIM_PIN2] = "SIM PIN2",
[GSMD_PIN_SIM_PUK2] = "SIM PUK2",
[GSMD_PIN_PH_NET_PIN] = "Network personalization PIN",
[GSMD_PIN_PH_NET_PUK] = "Network personalizaiton PUK",
[GSMD_PIN_PH_NETSUB_PIN]= "Network subset personalisation PIN",
[GSMD_PIN_PH_NETSUB_PUK]= "Network subset personalisation PUK",
[GSMD_PIN_PH_SP_PIN] = "Service provider personalisation PIN",
[GSMD_PIN_PH_SP_PUK] = "Service provider personalisation PUK",
[GSMD_PIN_PH_CORP_PIN] = "Corporate personalisation PIN",
[GSMD_PIN_PH_CORP_PUK] = "Corporate personalisation PUK",
};
static int get_cpin_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
enum gsmd_pin_type type;
if (!strncmp(resp, "+CPIN: ", 7)) {
unsigned int i;
resp += 7;
for (i = 0; i < __NUM_GSMD_PIN; i++) {
if(!strcmp(resp,pin_type_names[i]))
type = i;
}
}
return gsmd_ucmd_submit(ctx, GSMD_MSG_PIN, GSMD_PIN_GET_STATUS,
cmd->id, sizeof(type), &type);
}
static int usock_rcv_pin(struct gsmd_user *gu, struct gsmd_msg_hdr *gph,
int len)
{
struct gsmd_pin *gp = (struct gsmd_pin *) ((void *)gph + sizeof(*gph));
struct gsmd_atcmd *cmd;
switch (gph->msg_subtype) {
case GSMD_PIN_INPUT:
if (gph->len < sizeof(*gp) || len < sizeof(*gp)+sizeof(*gph))
return -EINVAL;
gsmd_log(GSMD_DEBUG, "pin type=%u, pin='%s', newpin='%s'\n",
gp->type, gp->pin, gp->newpin);
cmd = atcmd_fill("AT+CPIN=\"", 9+GSMD_PIN_MAXLEN+3+GSMD_PIN_MAXLEN+2,
&pin_cmd_cb, gu, 0, NULL);
if (!cmd)
return -ENOMEM;
strlcat(cmd->buf, gp->pin, cmd->buflen);
switch (gp->type) {
case GSMD_PIN_SIM_PUK:
case GSMD_PIN_SIM_PUK2:
strlcat(cmd->buf, "\",\"", cmd->buflen);
strlcat(cmd->buf, gp->newpin, cmd->buflen);
break;
default:
break;
}
strlcat(cmd->buf, "\"", cmd->buflen);
break;
case GSMD_PIN_GET_STATUS:
cmd = atcmd_fill("AT+CPIN?", 8 + 1, &get_cpin_cb, gu, 0, NULL);
break;
default:
gsmd_log(GSMD_ERROR, "unknown pin type %u\n",
gph->msg_subtype);
return -EINVAL;
}
return atcmd_submit(gu->gsmd, cmd);
}
static int phone_powerup_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
int ret = cmd->ret;
/* We need to verify if there is some error */
switch (ret) {
case 0:
gsmd_log(GSMD_DEBUG, "Radio powered-up\n");
gu->gsmd->dev_state.on = 1;
break;
default:
/* something went wrong */
gsmd_log(GSMD_DEBUG, "Radio power-up failed\n");
break;
}
return gsmd_ucmd_submit(gu, GSMD_MSG_PHONE, GSMD_PHONE_POWERUP,
cmd->id, sizeof(ret), &ret);
}
static int phone_powerdown_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
int ret = cmd->ret;
return gsmd_ucmd_submit(ctx, GSMD_MSG_PHONE, GSMD_PHONE_POWERDOWN,
cmd->id, sizeof(ret), &ret);
}
static int phone_power_status_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
DEBUGP("resp: %s\n", resp);
if (!strncmp(resp, "+CFUN: ", 7))
resp += 7;
return gsmd_ucmd_submit(ctx, GSMD_MSG_PHONE, GSMD_PHONE_POWER_STATUS,
cmd->id, strlen(resp) + 1, resp);
}
static int phone_get_manuf_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
DEBUGP("cmd = '%s', resp: '%s'\n", cmd->buf, resp);
if (!strncmp(resp, "+CGMI: ", 7))
resp += 7;
return gsmd_ucmd_submit(gu, GSMD_MSG_PHONE, GSMD_PHONE_GET_MANUF,
cmd->id, strlen(resp) + 1, resp);
}
static int phone_get_model_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
DEBUGP("cmd = '%s', resp: '%s'\n", cmd->buf, resp);
if (!strncmp(resp, "+CGMM: ", 7))
resp += 7;
return gsmd_ucmd_submit(gu, GSMD_MSG_PHONE, GSMD_PHONE_GET_MODEL,
cmd->id, strlen(resp) + 1, resp);
}
static int phone_get_revision_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
DEBUGP("cmd = '%s', resp: '%s'\n", cmd->buf, resp);
if (!strncmp(resp, "+CGMR: ", 7))
resp += 7;
return gsmd_ucmd_submit(gu, GSMD_MSG_PHONE, GSMD_PHONE_GET_REVISION,
cmd->id, strlen(resp) + 1, resp);
}
static int phone_get_serial_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
DEBUGP("cmd = '%s', resp: '%s'\n", cmd->buf, resp);
if (!strncmp(resp, "+CGSN: ", 7))
resp += 7;
return gsmd_ucmd_submit(gu, GSMD_MSG_PHONE, GSMD_PHONE_GET_SERIAL,
cmd->id, strlen(resp) + 1, resp);
}
static int phone_get_battery_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
struct gsmd_battery_charge gbs;
struct gsm_extrsp *er;
DEBUGP("cmd = '%s', resp: '%s'\n", cmd->buf, resp);
er = extrsp_parse(gsmd_tallocs, resp);
if(!er)
return -ENOMEM;
/* +CBC: 0,0 */
if((er->num_tokens == 2) &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_NUMERIC ) {
gbs.bcs = er->tokens[0].u.numeric;
gbs.bcl = er->tokens[1].u.numeric;
}
talloc_free(er);
return gsmd_ucmd_submit(gu, GSMD_MSG_PHONE, GSMD_PHONE_GET_BATTERY,
cmd -> id, sizeof(gbs), &gbs);
}
static int phone_vibrator_enable_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
int ret = cmd->ret;
switch(ret) {
case 0:
gsmd_log(GSMD_DEBUG, "Vibrator enabled\n");
gu->gsmd->dev_state.vibrator = 1;
break;
default:
gsmd_log(GSMD_DEBUG, "AT+CVIB=1 operation failed\n");
break;
}
return gsmd_ucmd_submit(gu, GSMD_MSG_PHONE, GSMD_PHONE_VIB_ENABLE,
cmd->id, sizeof(ret), &ret);
}
static int phone_vibrator_disable_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
int ret = cmd->ret;
return gsmd_ucmd_submit(ctx, GSMD_MSG_PHONE, GSMD_PHONE_VIB_DISABLE,
cmd->id, sizeof(ret), &ret);
}
static int usock_rcv_phone(struct gsmd_user *gu, struct gsmd_msg_hdr *gph,
int len)
{
struct gsmd_atcmd *cmd;
switch (gph->msg_subtype) {
case GSMD_PHONE_POWERUP:
cmd = atcmd_fill("AT+CFUN=1", 9+1,
&phone_powerup_cb, gu, 0, NULL);
break;
case GSMD_PHONE_POWERDOWN:
cmd = atcmd_fill("AT+CFUN=0", 9+1,
&phone_powerdown_cb, gu, 0, NULL);
gu->gsmd->dev_state.on = 0;
break;
case GSMD_PHONE_POWER_STATUS:
cmd = atcmd_fill("AT+CFUN?", 8+1,
&phone_power_status_cb, gu, 0, NULL);
break;
case GSMD_PHONE_GET_IMSI:
return gsmd_ucmd_submit(gu, GSMD_MSG_PHONE, GSMD_PHONE_GET_IMSI,
0, strlen(gu->gsmd->imsi) + 1, gu->gsmd->imsi);
break;
case GSMD_PHONE_GET_MANUF:
cmd = atcmd_fill("AT+CGMI", 7+1,
&phone_get_manuf_cb, gu, 0, NULL);
break;
case GSMD_PHONE_GET_MODEL:
cmd = atcmd_fill("AT+CGMM", 7+1,
&phone_get_model_cb, gu, 0, NULL);
break;
case GSMD_PHONE_GET_REVISION:
cmd = atcmd_fill("AT+CGMR", 7+1,
&phone_get_revision_cb, gu, 0, NULL);
break;
case GSMD_PHONE_GET_SERIAL:
cmd = atcmd_fill("AT+CGSN", 7+1,
&phone_get_serial_cb, gu, 0, NULL);
break;
case GSMD_PHONE_GET_BATTERY:
cmd = atcmd_fill("AT+CBC", 6+1, &phone_get_battery_cb, gu, 0, NULL);
break;
case GSMD_PHONE_VIB_ENABLE:
cmd = atcmd_fill("AT+CVIB=1", 9+1, &phone_vibrator_enable_cb, gu, 0, NULL);
break;
case GSMD_PHONE_VIB_DISABLE:
cmd = atcmd_fill("AT+CVIB=0", 9+1, &phone_vibrator_disable_cb, gu, 0, NULL);
gu->gsmd->dev_state.vibrator = 0;
break;
default:
return -EINVAL;
}
if (!cmd)
return -ENOMEM;
return atcmd_submit(gu->gsmd, cmd);
}
static int usock_rcv_modem(struct gsmd_user *gu, struct gsmd_msg_hdr *gph,
int len)
{
struct gsmd *g = gu->gsmd;
if (g->machinepl->power) {
g->machinepl->power(g, gph->msg_subtype);
}
return 0;
}
static int network_query_reg_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
struct gsm_extrsp *er;
enum gsmd_netreg_state state;
DEBUGP("cmd = '%s', resp: '%s'\n", cmd->buf, resp);
if (strncmp(resp, "+CREG: ", 7))
return -EINVAL;
er = extrsp_parse(gsmd_tallocs, resp);
if(!er)
return -ENOMEM;
//extrsp_dump(er);
/* +CREG: <n>,<stat>[,<lac>,<ci>] */
if((er->num_tokens == 4 || er->num_tokens == 2 ) &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_NUMERIC ) {
state = er->tokens[1].u.numeric;
}
talloc_free(er);
return gsmd_ucmd_submit(gu, GSMD_MSG_NETWORK, GSMD_NETWORK_QUERY_REG,
cmd->id, sizeof(state), &state);
}
static int network_vmail_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
struct gsmd_voicemail vmail;
struct gsm_extrsp *er;
int rc;
int ret = cmd->ret;
DEBUGP("cmd = '%s', resp: '%s'\n", cmd->buf, resp);
if (cmd->buf[7] == '=') {
/* response to set command */
rc = gsmd_ucmd_submit(gu, GSMD_MSG_NETWORK,
GSMD_NETWORK_VMAIL_SET,cmd->id, sizeof(ret), &ret);
} else {
/* response to get command */
if (strncmp(resp, "+CSVM: ", 7))
return -EINVAL;
resp += 7;
er = extrsp_parse(gsmd_tallocs, resp);
if(!er)
return -ENOMEM;
if(er->num_tokens == 3 &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_STRING &&
er->tokens[2].type == GSMD_ECMD_RTT_NUMERIC) {
vmail.enable = er->tokens[0].u.numeric;
strlcpy(vmail.addr.number, er->tokens[1].u.string, GSMD_ADDR_MAXLEN+1);
vmail.addr.type = er->tokens[2].u.numeric;
}
rc = gsmd_ucmd_submit(gu, GSMD_MSG_NETWORK, GSMD_NETWORK_VMAIL_GET,
cmd->id, sizeof(vmail), &vmail);
talloc_free(er);
}
return rc;
}
int gsmd_ucmd_submit(struct gsmd_user *gu, u_int8_t msg_type,
u_int8_t msg_subtype, u_int16_t id, int len, const void *data)
{
struct gsmd_ucmd *ucmd = ucmd_alloc(len);
if (!ucmd)
return -ENOMEM;
ucmd->hdr.version = GSMD_PROTO_VERSION;
ucmd->hdr.msg_type = msg_type;
ucmd->hdr.msg_subtype = msg_subtype;
ucmd->hdr.len = len;
ucmd->hdr.id = id;
memcpy(ucmd->buf, data, len);
usock_cmd_enqueue(ucmd, gu);
return 0;
}
static int network_sigq_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_signal_quality gsq;
char *comma;
gsq.rssi = atoi(resp + 6);
comma = strchr(resp, ',');
if (!comma ++)
return -EIO;
gsq.ber = atoi(comma);
return gsmd_ucmd_submit(ctx, GSMD_MSG_NETWORK, GSMD_NETWORK_SIGQ_GET,
cmd->id, sizeof(gsq), &gsq);
}
static int network_oper_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
const char *end, *opname;
int format, s, ret;
char *buf;
/* Format: <mode>[,<format>,<oper>] */
/* In case we're not registered, return an empty string. */
if (sscanf(resp, "+COPS: %*i,%i,\"%n", &format, &s) <= 0)
end = opname = resp;
else {
/* If the phone returned the opname in a short or numeric
* format, then it probably doesn't know the operator's full
* name or doesn't support it. Return any information we
* have in this case. */
if (format != 0)
gsmd_log(GSMD_NOTICE, "+COPS response in a format "
" different than long alphanumeric - "
" returning as is!\n");
opname = resp + s;
end = strchr(opname, '"');
if (!end)
return -EINVAL;
}
buf = strndup(opname, end - opname);
ret = gsmd_ucmd_submit(gu, GSMD_MSG_NETWORK, GSMD_NETWORK_OPER_GET,
cmd->id, end - opname + 1, buf);
free(buf);
return ret;
}
static int network_oper_n_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
char buf[16+1] = {'\0'};
struct gsm_extrsp *er;
er = extrsp_parse(cmd, resp);
if ( !er )
return -ENOMEM;
//extrsp_dump(er);
/* Format: <mode>[,<format>,<oper>] */
if ( er->num_tokens == 1 &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC ) {
/* In case we're not registered, return an empty string */
buf[0] = '\0';
}
else if ( er->num_tokens >= 3 &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[2].type == GSMD_ECMD_RTT_STRING ) {
strlcpy(buf, er->tokens[2].u.string, sizeof(buf));
}
else {
DEBUGP("Invalid Input : Parse error\n");
return -EINVAL;
}
talloc_free(er);
return gsmd_ucmd_submit(gu, GSMD_MSG_NETWORK, GSMD_NETWORK_OPER_N_GET,
cmd->id, sizeof(buf), buf);
}
static int network_opers_parse(const char *str, struct gsmd_msg_oper **out)
{
int len = 0;
struct gsm_extrsp *er;
char buf[64];
char *head, *tail, *ptr;
struct gsmd_msg_oper *out2;
if (strncmp(str, "+COPS: ", 7))
return -EINVAL;
/*
* string ",," means the begginig of extended parameters and we
* don't want to scan them for operators.
*/
ptr = strstr(str, ",,");
if(ptr)
ptr[0] = '\0';
ptr = (char *) str;
while (*str) {
if ( *str == '(' && isdigit(*(str+1)) ) {
len++;
str+=2;
}
else
str++;
}
*out = talloc_size(__gu_ctx, sizeof(struct gsmd_msg_oper) * (len + 1));
if (!out)
return -ENOMEM;
out2 = *out;
str = ptr;
while (*str) {
if ( *str == '(' )
head = (char *) str;
else if ( *str == ')' ) {
tail = (char *) str;
memset(buf, '\0', sizeof(buf));
strncpy(buf, head+1, (tail-head-1));
DEBUGP("buf: %s\n", buf);
er = extrsp_parse(gsmd_tallocs, buf);
if ( !er )
return -ENOMEM;
//extrsp_dump(er);
if ( er->num_tokens >= 4 &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_STRING &&
er->tokens[2].type == GSMD_ECMD_RTT_STRING &&
er->tokens[3].type == GSMD_ECMD_RTT_STRING ) {
/*
* +COPS=? +COPS: [list of supported (<stat>,long alphanumeric <oper>
* ,short alphanumeric <oper>,numeric <oper>)s]
*/
out2->stat = er->tokens[0].u.numeric;
strlcpy(out2->opname_longalpha, er->tokens[1].u.string,
sizeof(out2->opname_longalpha));
strlcpy(out2->opname_shortalpha, er->tokens[2].u.string,
sizeof(out2->opname_shortalpha));
strlcpy(out2->opname_num, er->tokens[3].u.string,
sizeof(out2->opname_num));
}
else {
DEBUGP("Invalid Input : Parse error\n");
talloc_free(*out);
return -EINVAL;
}
talloc_free(er);
out2->is_last = 0;
out2 ++;
}
str ++;
}
out2->is_last = 1;
return len;
}
static int network_opers_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
struct gsmd_msg_oper *buf = NULL;
int len, ret;
len = network_opers_parse(resp, &buf);
if(len < 0)
return len; /* error we got from network_opers_parse */
ret = gsmd_ucmd_submit(gu, GSMD_MSG_NETWORK, GSMD_NETWORK_OPER_LIST,
cmd->id, sizeof(*buf) * (len + 1), buf);
talloc_free(buf);
return ret;
}
static int network_pref_opers_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = (struct gsmd_user *) ctx;
struct gsmd_msg_prefoper entry;
int index;
char opname[17];
if (cmd->ret && cmd->ret != -255)
return 0; /* TODO: Send a response */
if (sscanf(resp, "+CPOL: %i,0,\"%16[^\"]\"", &index, opname) < 2)
return -EINVAL; /* TODO: Send a response */
entry.index = index;
entry.is_last = (cmd->ret == 0);
memcpy(entry.opname_longalpha, opname, sizeof(entry.opname_longalpha));
return gsmd_ucmd_submit(gu, GSMD_MSG_NETWORK, GSMD_NETWORK_PREF_LIST,
cmd->id, sizeof(entry), &entry);
}
static int network_pref_num_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = (struct gsmd_user *) ctx;
int min_index, max_index, size;
if (cmd->ret)
return 0; /* TODO: Send a response */
/* This is not a full general case, theoretically the range string
* can include commas and more dashes, but we have no full parser for
* ranges yet. */
if (sscanf(resp, "+CPOL: (%i-%i)", &min_index, &max_index) < 2)
return -EINVAL; /* TODO: Send a response */
size = max_index - min_index + 1;
return gsmd_ucmd_submit(gu, GSMD_MSG_NETWORK, GSMD_NETWORK_PREF_SPACE,
cmd->id, sizeof(size), &size);
}
static int network_ownnumbers_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = (struct gsmd_user *) ctx;
struct gsmd_own_number *num;
int len, ret, type;
char dummy;
if (cmd->ret && cmd->ret != -255)
return 0; /* TODO: Send a response */
if (sscanf(resp, "+CNUM: \"%*[^\"]\"%c%n", &dummy, &len) > 0)
len -= strlen("+CNUM: \"\",");
else
len = 0;
num = talloc_size(__gu_ctx, sizeof(*num) + len + 1);
if (len)
ret = sscanf(resp, "+CNUM: \"%[^\"]\",\"%32[^\"]\",%i,%*i,%i,",
num->name, num->addr.number,
&type, &num->service) - 1;
else
ret = sscanf(resp, "+CNUM: ,\"%32[^\"]\",%i,%*i,%i,",
num->addr.number,
&type, &num->service);
if (ret < 2) {
talloc_free(num);
return -EINVAL; /* TODO: Send a response */
}
if (ret < 3)
num->service = GSMD_SERVICE_UNKNOWN;
num->name[len] = 0;
num->addr.type = type;
num->is_last = (cmd->ret == 0);
ret = gsmd_ucmd_submit(gu, GSMD_MSG_NETWORK, GSMD_NETWORK_GET_NUMBER,
cmd->id, sizeof(*num) + len + 1, num);
talloc_free(num);
return ret;
}
static int usock_rcv_network(struct gsmd_user *gu, struct gsmd_msg_hdr *gph,
int len)
{
struct gsmd_atcmd *cmd;
struct gsmd_voicemail *vmail = (struct gsmd_voicemail *) gph->data;
gsmd_oper_numeric *oper = (gsmd_oper_numeric *) gph->data;
char buffer[15 + sizeof(gsmd_oper_numeric)];
int cmdlen;
switch (gph->msg_subtype) {
case GSMD_NETWORK_REGISTER:
if ((*oper)[0])
cmdlen = sprintf(buffer, "AT+COPS=1,2,\"%.*s\"",
sizeof(gsmd_oper_numeric), (char *)oper);
else
cmdlen = sprintf(buffer, "AT+COPS=0");
cmd = atcmd_fill(buffer, cmdlen + 1, &null_cmd_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_DEREGISTER:
cmd = atcmd_fill("AT+COPS=2", 9+1, &null_cmd_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_QUERY_REG:
cmd = atcmd_fill("AT+CREG?", 8+1, &network_query_reg_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_VMAIL_GET:
cmd = atcmd_fill("AT+CSVM?", 8+1, &network_vmail_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_VMAIL_SET:
cmdlen = sprintf(buffer, "AT+CSVM=1,\"%s\",%d",
vmail->addr.number, vmail->addr.type);
cmd = atcmd_fill(buffer, cmdlen + 1, &network_vmail_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_SIGQ_GET:
cmd = atcmd_fill("AT+CSQ", 6+1, &network_sigq_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_OPER_GET:
/* Set long alphanumeric format */
atcmd_submit(gu->gsmd, atcmd_fill("AT+COPS=3,0", 11+1,
&null_cmd_cb, gu, 0, NULL));
cmd = atcmd_fill("AT+COPS?", 8+1, &network_oper_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_OPER_N_GET:
/* Set numeric format */
atcmd_submit(gu->gsmd, atcmd_fill("AT+COPS=3,2", 11+1,
&null_cmd_cb, gu, 0, NULL));
cmd = atcmd_fill("AT+COPS?", 8+1, &network_oper_n_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_OPER_LIST:
cmd = atcmd_fill("AT+COPS=?", 9+1, &network_opers_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_PREF_LIST:
/* Set long alphanumeric format */
atcmd_submit(gu->gsmd, atcmd_fill("AT+CPOL=,0", 10 + 1,
&null_cmd_cb, gu, 0, NULL));
cmd = atcmd_fill("AT+CPOL?", 8 + 1,
&network_pref_opers_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_PREF_DEL:
cmdlen = sprintf(buffer, "AT+CPOL=%i", *(int *) gph->data);
cmd = atcmd_fill(buffer, cmdlen + 1, &null_cmd_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_PREF_ADD:
cmdlen = sprintf(buffer, "AT+CPOL=,2,\"%.*s\"",
sizeof(gsmd_oper_numeric), (char *)oper);
cmd = atcmd_fill(buffer, cmdlen + 1, &null_cmd_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_PREF_SPACE:
cmd = atcmd_fill("AT+CPOL=?", 9 + 1,
&network_pref_num_cb, gu, 0, NULL);
break;
case GSMD_NETWORK_GET_NUMBER:
cmd = atcmd_fill("AT+CNUM", 7 + 1,
&network_ownnumbers_cb, gu, 0, NULL);
break;
default:
return -EINVAL;
}
if (!cmd)
return -ENOMEM;
return atcmd_submit(gu->gsmd, cmd);
}
static int phonebook_find_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
struct gsmd_phonebooks gps;
struct gsm_extrsp *er;
DEBUGP("resp: %s\n", resp);
er = extrsp_parse(cmd, resp);
if ( !er )
return -ENOMEM;
gps.is_last = (cmd->ret == 0 || cmd->ret == 4)? 1:0;
if ( !strncmp(resp, "OK", 2) ) {
/* The record is empty or could not read yet */
gps.pb.index = 0;
}
else if ( !strncmp(resp, "+CME", 4) ) {
DEBUGP("== +CME error\n");
/* +CME ERROR: 21 */
gps.pb.index = 0 - atoi(strpbrk(resp, "0123456789"));
}
else if ( er->num_tokens == 4 &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_STRING &&
er->tokens[2].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[3].type == GSMD_ECMD_RTT_STRING ) {
/*
* [+CPBR: <index1>,<number>,<type>,<text>[[...]
* <CR><LF>+CPBR: <index2>,<unmber>,<type>,<text>]]
*/
gps.pb.index = er->tokens[0].u.numeric;
strlcpy(gps.pb.numb, er->tokens[1].u.string, GSMD_PB_NUMB_MAXLEN+1);
gps.pb.type = er->tokens[2].u.numeric;
strlcpy(gps.pb.text, er->tokens[3].u.string, GSMD_PB_TEXT_MAXLEN+1);
}
else {
DEBUGP("Invalid Input : Parse error\n");
return -EINVAL;
}
talloc_free(er);
return gsmd_ucmd_submit(gu, GSMD_MSG_PHONEBOOK, GSMD_PHONEBOOK_FIND,
cmd->id, sizeof(gps), &gps);
}
static int phonebook_read_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
struct gsmd_phonebook gp;
struct gsm_extrsp *er;
DEBUGP("resp: %s\n", resp);
er = extrsp_parse(cmd, resp);
if ( !er )
return -ENOMEM;
if ( !strncmp(resp, "OK", 2) ) {
/* The record is empty or could not read yet */
gp.index = 0;
}
else if ( !strncmp(resp, "+CME", 4) ) {
DEBUGP("+CME error\n");
/* +CME ERROR: 21 */
gp.index = 0 - atoi(strpbrk(resp, "0123456789"));
}
else if ( er->num_tokens == 4 &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_STRING &&
er->tokens[2].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[3].type == GSMD_ECMD_RTT_STRING ) {
/*
* [+CPBR: <index1>,<number>,<type>,<text>[[...]
* <CR><LF>+CPBR: <index2>,<unmber>,<type>,<text>]]
*/
gp.index = er->tokens[0].u.numeric;
strlcpy(gp.numb, er->tokens[1].u.string, GSMD_PB_NUMB_MAXLEN+1);
gp.type = er->tokens[2].u.numeric;
strlcpy(gp.text, er->tokens[3].u.string, GSMD_PB_TEXT_MAXLEN+1);
}
else {
DEBUGP("Invalid Input : Parse error\n");
return -EINVAL;
}
talloc_free(er);
return gsmd_ucmd_submit(gu, GSMD_MSG_PHONEBOOK, GSMD_PHONEBOOK_READ,
cmd->id, sizeof(gp), &gp);
}
static int phonebook_readrg_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
struct gsmd_user *gu = ctx;
struct gsmd_phonebooks gps;
struct gsm_extrsp *er;
DEBUGP("resp: %s\n", resp);
er = extrsp_parse(cmd, resp);
if ( !er )
return -ENOMEM;
gps.is_last = (cmd->ret == 0 || cmd->ret == 4)? 1:0;
if ( !strncmp(resp, "OK", 2) ) {
/* The record is empty or could not read yet */
gps.pb.index = 0;
}
else if ( !strncmp(resp, "+CME", 4) ) {
DEBUGP("+CME error\n");
/* +CME ERROR: 21 */
gps.pb.index = 0 - atoi(strpbrk(resp, "0123456789"));
}
else if ( er->num_tokens == 4 &&
er->tokens[0].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[1].type == GSMD_ECMD_RTT_STRING &&
er->tokens[2].type == GSMD_ECMD_RTT_NUMERIC &&
er->tokens[3].type == GSMD_ECMD_RTT_STRING ) {
/*
* [+CPBR: <index1>,<number>,<type>,<text>[[...]
* <CR><LF>+CPBR: <index2>,<unmber>,<type>,<text>]]
*/
gps.pb.index = er->tokens[0].u.numeric;
strlcpy(gps.pb.numb, er->tokens[1].u.string, GSMD_PB_NUMB_MAXLEN+1);
gps.pb.type = er->tokens[2].u.numeric;
strlcpy(gps.pb.text, er->tokens[3].u.string, GSMD_PB_TEXT_MAXLEN+1);
}
else {
DEBUGP("Invalid Input : Parse error\n");
return -EINVAL;
}
talloc_free(er);
return gsmd_ucmd_submit(gu, GSMD_MSG_PHONEBOOK, GSMD_PHONEBOOK_READRG,
cmd->id, sizeof(gps), &gps);
}
static int phonebook_write_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
DEBUGP("resp: %s\n", resp);
return gsmd_ucmd_submit(ctx, GSMD_MSG_PHONEBOOK, GSMD_PHONEBOOK_WRITE,
cmd->id, strlen(resp) + 1, resp);
}
static int phonebook_delete_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
DEBUGP("resp: %s\n", resp);
return gsmd_ucmd_submit(ctx, GSMD_MSG_PHONEBOOK, GSMD_PHONEBOOK_DELETE,
cmd->id, strlen(resp) + 1, resp);
}
static int phonebook_get_support_cb(struct gsmd_atcmd *cmd, void *ctx, char *resp)
{
/* TODO: Need to handle command error */
/* +CPBR: (1-100),44,16 */
struct gsmd_user *gu = ctx;
struct gsmd_phonebook_support gps;
char *fcomma, *lcomma;
char *dash;
DEBUGP("resp: %s\n", resp);
dash = strchr(resp, '-');
if (!dash)
return -EIO; /* TODO: Send a response */
gps.index = atoi(dash + 1);
fcomma = strchr(resp, ',');
if (!fcomma)
return -EIO; /* TODO: Send a response */
gps.nlength = atoi(fcomma+1);
lcomma = strrchr(resp, ',');
if (!lcomma)
return -EIO; /* TODO: Send a response */
gps.tlength = atoi(lcomma+1);
return gsmd_ucmd_submit(gu,
GSMD_MSG_PHONEBOOK, GSMD_PHONEBOOK_GET_SUPPORT,
cmd->id, sizeof(gps), &gps);
}
static int phonebook_list_storage_cb(struct gsmd_atcmd *cmd,
void *ctx, char *resp)
{
/* TODO; using link list ; need to handle command error */
struct gsmd_user *gu = ctx;
struct gsmd_phonebook_storage gps;
char *ptr;
DEBUGP("resp: %s\n", resp);
/*
* +CPBS: (<storage>s)
*/
gps.num = 0;
if (!strncmp(resp, "+CPBS", 5)) {
char* delim = "(,";
ptr = strpbrk(resp, delim);
while (ptr) {
strncpy(gps.mem[gps.num].type, ptr + 2, 2);
gps.mem[gps.num].type[2] = '\0';
ptr = strpbrk(ptr + 2, delim);
gps.num++;
}
}
return gsmd_ucmd_submit(gu,
GSMD_MSG_PHONEBOOK, GSMD_PHONEBOOK_LIST_STORAGE,
cmd->id, sizeof(gps), &gps);
}
static int usock_rcv_phonebook(struct gsmd_user *gu,
struct gsmd_msg_hdr *gph,int len)
{
struct gsmd_atcmd *cmd = NULL;
struct gsmd_phonebook_readrg *gpr;
struct gsmd_phonebook *gp;
struct gsmd_phonebook_find *gpf;
int *index;
int atcmd_len;
char *storage;
char buf[1024];
switch (gph->msg_subtype) {
case GSMD_PHONEBOOK_LIST_STORAGE:
cmd = atcmd_fill("AT+CPBS=?", 9 + 1,
&phonebook_list_storage_cb,
gu, gph->id, NULL);
break;
case GSMD_PHONEBOOK_SET_STORAGE:
if (len < sizeof(*gph) + 3)
return -EINVAL;
storage = (char*) ((void *)gph + sizeof(*gph));
/* ex. AT+CPBS="ME" */
atcmd_len = 1 + strlen("AT+CPBS=\"") + 2 + strlen("\"");
cmd = atcmd_fill("AT+CPBS=\"", atcmd_len,
&usock_cmd_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "AT+CPBS=\"%s\"", storage);
break;
case GSMD_PHONEBOOK_FIND:
if(len < sizeof(*gph) + sizeof(*gpf))
return -EINVAL;
gpf = (struct gsmd_phonebook_find *) ((void *)gph + sizeof(*gph));
atcmd_len = 1 + strlen("AT+CPBF=\"") +
strlen(gpf->findtext) + strlen("\"");
cmd = atcmd_fill("AT+CPBF=\"", atcmd_len,
&phonebook_find_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "AT+CPBF=\"%s\"", gpf->findtext);
break;
case GSMD_PHONEBOOK_READ:
if(len < sizeof(*gph) + sizeof(int))
return -EINVAL;
index = (int *) ((void *)gph + sizeof(*gph));
sprintf(buf, "%d", *index);
/* ex, AT+CPBR=23 */
atcmd_len = 1 + strlen("AT+CPBR=") + strlen(buf);
cmd = atcmd_fill("AT+CPBR=", atcmd_len,
&phonebook_read_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "AT+CPBR=%d", *index);
break;
case GSMD_PHONEBOOK_READRG:
if(len < sizeof(*gph) + sizeof(*gpr))
return -EINVAL;
gpr = (struct gsmd_phonebook_readrg *) ((void *)gph + sizeof(*gph));
sprintf(buf, "%d,%d", gpr->index1, gpr->index2);
/* ex, AT+CPBR=1,100 */
atcmd_len = 1 + strlen("AT+CPBR=") + strlen(buf);
cmd = atcmd_fill("AT+CPBR=", atcmd_len,
&phonebook_readrg_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "AT+CPBR=%s", buf);
break;
case GSMD_PHONEBOOK_WRITE:
if(len < sizeof(*gph) + sizeof(*gp))
return -EINVAL;
gp = (struct gsmd_phonebook *) ((void *)gph + sizeof(*gph));
sprintf(buf, "%d,\"%s\",%d,\"%s\"",
gp->index, gp->numb, gp->type, gp->text);
atcmd_len = 1 + strlen("AT+CPBW=") + strlen(buf);
cmd = atcmd_fill("AT+CPBW=", atcmd_len,
&phonebook_write_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "AT+CPBW=%s", buf);
break;
case GSMD_PHONEBOOK_DELETE:
if(len < sizeof(*gph) + sizeof(int))
return -EINVAL;
index = (int *) ((void *)gph + sizeof(*gph));
sprintf(buf, "%d", *index);
/* ex, AT+CPBW=3*/
atcmd_len = 1 + strlen("AT+CPBW=") + strlen(buf);
cmd = atcmd_fill("AT+CPBW=", atcmd_len,
&phonebook_delete_cb, gu, gph->id, NULL);
if (!cmd)
return -ENOMEM;
sprintf(cmd->buf, "AT+CPBW=%s", buf);
break;
case GSMD_PHONEBOOK_GET_SUPPORT:
cmd = atcmd_fill("AT+CPBR=?", 9+1,
&phonebook_get_support_cb, gu, gph->id, NULL);
break;
default:
return -EINVAL;
}
if (cmd)
return atcmd_submit(gu->gsmd, cmd);
else
return 0;
}
static usock_msg_handler *pcmd_type_handlers[__NUM_GSMD_MSGS] = {
[GSMD_MSG_PASSTHROUGH] = &usock_rcv_passthrough,
[GSMD_MSG_EVENT] = &usock_rcv_event,
[GSMD_MSG_VOICECALL] = &usock_rcv_voicecall,
[GSMD_MSG_PIN] = &usock_rcv_pin,
[GSMD_MSG_PHONE] = &usock_rcv_phone,
[GSMD_MSG_NETWORK] = &usock_rcv_network,
[GSMD_MSG_SMS] = &usock_rcv_sms,
[GSMD_MSG_CB] = &usock_rcv_cb,
[GSMD_MSG_PHONEBOOK] = &usock_rcv_phonebook,
[GSMD_MSG_MODEM] = &usock_rcv_modem,
};
static int usock_rcv_pcmd(struct gsmd_user *gu, char *buf, int len)
{
struct gsmd_msg_hdr *gph = (struct gsmd_msg_hdr *)buf;
usock_msg_handler *umh;
if (gph->version != GSMD_PROTO_VERSION)
return -EINVAL;
if (gph->msg_type >= __NUM_GSMD_MSGS)
return -EINVAL;
umh = pcmd_type_handlers[gph->msg_type];
if (!umh)
return -EINVAL;
return umh(gu, gph, len);
}
/* callback for read/write on client (libgsmd) socket */
static int gsmd_usock_user_cb(int fd, unsigned int what, void *data)
{
struct gsmd_user *gu = data;
/* FIXME: check some kind of backlog and limit it */
if (what & GSMD_FD_READ) {
char buf[1024];
int rcvlen;
/* read data from socket, determine what he wants */
rcvlen = read(fd, buf, sizeof(buf));
if (rcvlen == 0) {
DEBUGP("EOF, this client has just vanished\n");
/* EOF, this client has just vanished */
gsmd_unregister_fd(&gu->gfd);
close(fd);
/* finish pending atcmd's from this client thus
* destroying references to the user structure. */
atcmd_terminate_matching(gu->gsmd, gu);
/* destroy whole user structure */
llist_del(&gu->list);
talloc_free(gu);
return 0;
} else if (rcvlen < 0)
return rcvlen;
else
return usock_rcv_pcmd(gu, buf, rcvlen);
}
if (what & GSMD_FD_WRITE) {
/* write data from pending replies to socket */
struct gsmd_ucmd *ucmd, *uctmp;
llist_for_each_entry_safe(ucmd, uctmp, &gu->finished_ucmds,
list) {
int rc;
rc = write(fd, &ucmd->hdr, sizeof(ucmd->hdr) + ucmd->hdr.len);
if (rc < 0) {
DEBUGP("write return %d\n", rc);
return rc;
}
if (rc == 0) {
DEBUGP("write returns zero!!\n");
break;
}
if (rc != sizeof(ucmd->hdr) + ucmd->hdr.len) {
DEBUGP("short write\n");
break;
}
DEBUGP("successfully sent cmd %p to user %p, freeing\n", ucmd, gu);
llist_del(&ucmd->list);
talloc_free(ucmd);
}
if (llist_empty(&gu->finished_ucmds))
gu->gfd.when &= ~GSMD_FD_WRITE;
}
return 0;
}
/* callback for read on master-listen-socket */
static int gsmd_usock_cb(int fd, unsigned int what, void *data)
{
struct gsmd *g = data;
struct gsmd_user *newuser;
/* FIXME: implement this */
if (what & GSMD_FD_READ) {
/* new incoming connection */
newuser = talloc(__gu_ctx, struct gsmd_user);
if (!newuser)
return -ENOMEM;
newuser->gfd.fd = accept(fd, NULL, 0);
if (newuser->gfd.fd < 0) {
DEBUGP("error accepting incoming conn: `%s'\n",
strerror(errno));
talloc_free(newuser);
}
newuser->gfd.when = GSMD_FD_READ;
newuser->gfd.data = newuser;
newuser->gfd.cb = &gsmd_usock_user_cb;
newuser->gsmd = g;
newuser->subscriptions = 0xffffffff;
INIT_LLIST_HEAD(&newuser->finished_ucmds);
llist_add(&newuser->list, &g->users);
gsmd_register_fd(&newuser->gfd);
}
return 0;
}
/* handling of socket with incoming client connections */
int usock_init(struct gsmd *g)
{
struct sockaddr_un sun;
int fd, rc;
__ucmd_ctx = talloc_named_const(gsmd_tallocs, 1, "ucmd");
__gu_ctx = talloc_named_const(gsmd_tallocs, 1, "gsmd_user");
fd = socket(PF_UNIX, GSMD_UNIX_SOCKET_TYPE, 0);
if (fd < 0)
return fd;
memset(&sun, 0, sizeof(sun));
sun.sun_family = AF_UNIX;
memcpy(sun.sun_path, GSMD_UNIX_SOCKET, sizeof(GSMD_UNIX_SOCKET));
rc = bind(fd, (struct sockaddr *)&sun, sizeof(sun));
if (rc < 0) {
close(fd);
return rc;
}
rc = listen(fd, 10);
if (rc < 0) {
close(fd);
return rc;
}
g->gfd_sock.fd = fd;
g->gfd_sock.when = GSMD_FD_READ | GSMD_FD_EXCEPT;
g->gfd_sock.data = g;
g->gfd_sock.cb = &gsmd_usock_cb;
return gsmd_register_fd(&g->gfd_sock);
}
|