aboutsummaryrefslogtreecommitdiffstats
path: root/apps/app_sms.c
blob: 3a7677601f891bb26f5c9a8e0eca8aca2e35903a (plain)
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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2004 - 2005, Adrian Kennard, rights assigned to Digium
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief SMS application - ETSI ES 201 912 protocol 1 implimentation
 * \ingroup applications
 * 
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "asterisk/lock.h"
#include "asterisk/file.h"
#include "asterisk/logger.h"
#include "asterisk/options.h"
#include "asterisk/channel.h"
#include "asterisk/pbx.h"
#include "asterisk/module.h"
#include "asterisk/alaw.h"
#include "asterisk/callerid.h"

/* output using Alaw rather than linear */
/* #define OUTALAW */

/* ToDo */
/* Add full VP support */
/* Handle status report messages (generation and reception) */
/* Time zones on time stamps */
/* user ref field */

static volatile unsigned char message_ref;      /* arbitary message ref */
static volatile unsigned int seq;       /* arbitrary message sequence number for unqiue files */

static char log_file[255];
static char spool_dir[255];

static char *tdesc = "SMS/PSTN handler";

static char *app = "SMS";

static char *synopsis = "Communicates with SMS service centres and SMS capable analogue phones";

static char *descrip =
	"  SMS(name|[a][s]):  SMS handles exchange of SMS data with a call to/from SMS capabale\n"
	"phone or SMS PSTN service center. Can send and/or receive SMS messages.\n"
	"Works to ETSI ES 201 912 compatible with BT SMS PSTN service in UK\n"
	"Typical usage is to use to handle called from the SMS service centre CLI,\n"
	"or to set up a call using 'outgoing' or manager interface to connect\n"
	"service centre to SMS()\n"
	"name is the name of the queue used in /var/spool/asterisk/sms\n"
	"Arguments:\n"
	" a: answer, i.e. send initial FSK packet.\n"
	" s: act as service centre talking to a phone.\n"
	"Messages are processed as per text file message queues.\n" 
	"smsq (a separate software) is a command to generate message\n"
	"queues and send messages.\n";

static signed short wave[] = {
	0, 392, 782, 1167, 1545, 1913, 2270, 2612, 2939, 3247, 3536, 3802, 4045, 4263, 4455, 4619, 4755, 4862, 4938, 4985,
	5000, 4985, 4938, 4862, 4755, 4619, 4455, 4263, 4045, 3802, 3536, 3247, 2939, 2612, 2270, 1913, 1545, 1167, 782, 392,
	0, -392, -782, -1167,
	 -1545, -1913, -2270, -2612, -2939, -3247, -3536, -3802, -4045, -4263, -4455, -4619, -4755, -4862, -4938, -4985, -5000,
	-4985, -4938, -4862,
	-4755, -4619, -4455, -4263, -4045, -3802, -3536, -3247, -2939, -2612, -2270, -1913, -1545, -1167, -782, -392
};

#ifdef OUTALAW
static unsigned char wavea[80];
#endif

STANDARD_LOCAL_USER;

LOCAL_USER_DECL;

/* SMS 7 bit character mapping to UCS-2 */
static const unsigned short defaultalphabet[] = {
	0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC,
	0x00F2, 0x00E7, 0x000A, 0x00D8, 0x00F8, 0x000D, 0x00C5, 0x00E5,
	0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8,
	0x03A3, 0x0398, 0x039E, 0x00A0, 0x00C6, 0x00E6, 0x00DF, 0x00C9,
	' ', '!', '"', '#', 164, '%', '&', 39, '(', ')', '*', '+', ',', '-', '.', '/',
	'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
	161, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
	'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 196, 214, 209, 220, 167,
	191, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
	'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 228, 246, 241, 252, 224,
};

static const unsigned short escapes[] = {
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x000C, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0x005E, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0x007B, 0x007D, 0, 0, 0, 0, 0, 0x005C,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x005B, 0x007E, 0x005D, 0,
	0x007C, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0x20AC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

#define SMSLEN 160              /* max SMS length */

typedef struct sms_s
{
	unsigned char hangup;        /* we are done... */
	unsigned char err;           /* set for any errors */
	unsigned char smsc:1;        /* we are SMSC */
	unsigned char rx:1;          /* this is a received message */
	char queue[30];              /* queue name */
	char oa[20];                 /* originating address */
	char da[20];                 /* destination address */
	time_t scts;                 /* time stamp, UTC */
	unsigned char pid;           /* protocol ID */
	unsigned char dcs;           /* data coding scheme */
	short mr;                    /* message reference - actually a byte, but usde -1 for not set */
	int udl;                     /* user data length */
	int udhl;                    /* user data header length */
	unsigned char srr:1;         /* Status Report request */
	unsigned char udhi:1;        /* User Data Header required, even if length 0 */
	unsigned char rp:1;          /* Reply Path */
	unsigned int vp;             /* validity period in minutes, 0 for not set */
	unsigned short ud[SMSLEN];   /* user data (message), UCS-2 coded */
	unsigned char udh[SMSLEN];   /* user data header */
	char cli[20];                /* caller ID */
	unsigned char ophase;        /* phase (0-79) for 0 and 1 frequencies (1300Hz and 2100Hz) */
	unsigned char ophasep;       /* phase (0-79) for 1200 bps */
	unsigned char obyte;         /* byte being sent */
	unsigned int opause;         /* silent pause before sending (in sample periods) */
	unsigned char obitp;         /* bit in byte */
	unsigned char osync;         /* sync bits to send */
	unsigned char obytep;        /* byte in data */
	unsigned char obyten;        /* bytes in data */
	unsigned char omsg[256];     /* data buffer (out) */
	unsigned char imsg[200];     /* data buffer (in) */
	signed long long ims0,
		imc0,
		ims1,
		imc1;                      /* magnitude averages sin/cos 0/1 */
	unsigned int idle;
	unsigned short imag;         /* signal level */
	unsigned char ips0,
		ips1,
		ipc0,
		ipc1;                      /* phase sin/cos 0/1 */
	unsigned char ibitl;         /* last bit */
	unsigned char ibitc;         /* bit run length count */
	unsigned char iphasep;       /* bit phase (0-79) for 1200 bps */
	unsigned char ibitn;         /* bit number in byte being received */
	unsigned char ibytev;        /* byte value being received */
	unsigned char ibytep;        /* byte pointer in messafe */
	unsigned char ibytec;        /* byte checksum for message */
	unsigned char ierr;          /* error flag */
	unsigned char ibith;         /* history of last bits */
	unsigned char ibitt;         /* total of 1's in last 3 bites */
	/* more to go here */
} sms_t;

/* different types of encoding */
#define is7bit(dcs) (((dcs)&0xC0)?(!((dcs)&4)):(!((dcs)&12)))
#define is8bit(dcs) (((dcs)&0xC0)?(((dcs)&4)):(((dcs)&12)==4))
#define is16bit(dcs) (((dcs)&0xC0)?0:(((dcs)&12)==8))

static void *sms_alloc (struct ast_channel *chan, void *params)
{
	return params;
}

static void sms_release (struct ast_channel *chan, void *data)
{
	return;
}

static void sms_messagetx (sms_t * h);

/*--- numcpy: copy number, skipping non digits apart from leading + */
static void numcpy (char *d, char *s)
{
	if (*s == '+')
		*d++ = *s++;
	while (*s) {
  		if (isdigit (*s))
     			*d++ = *s;
		s++;
	}
	*d = 0;
}

/*--- isodate: static, return a date/time in ISO format */
static char * isodate (time_t t)
{
	static char date[20];
	strftime (date, sizeof (date), "%Y-%m-%dT%H:%M:%S", localtime (&t));
	return date;
}

/*--- utf8decode: reads next UCS character from null terminated UTF-8 string and advanced pointer */
/* for non valid UTF-8 sequences, returns character as is */
/* Does not advance pointer for null termination */
static long utf8decode (unsigned char **pp)
{
	unsigned char *p = *pp;
	if (!*p)
		return 0;                 /* null termination of string */
	(*pp)++;
	if (*p < 0xC0)
		return *p;                /* ascii or continuation character */
	if (*p < 0xE0) {
		if (*p < 0xC2 || (p[1] & 0xC0) != 0x80)
			return *p;             /* not valid UTF-8 */
		(*pp)++;
		return ((*p & 0x1F) << 6) + (p[1] & 0x3F);
   	}
	if (*p < 0xF0) {
		if ((*p == 0xE0 && p[1] < 0xA0) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80)
			 return *p;             /* not valid UTF-8 */
		(*pp) += 2;
		return ((*p & 0x0F) << 12) + ((p[1] & 0x3F) << 6) + (p[2] & 0x3F);
	}
	if (*p < 0xF8) {
		if ((*p == 0xF0 && p[1] < 0x90) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80)
			return *p;             /* not valid UTF-8 */
		(*pp) += 3;
		return ((*p & 0x07) << 18) + ((p[1] & 0x3F) << 12) + ((p[2] & 0x3F) << 6) + (p[3] & 0x3F);
	}
	if (*p < 0xFC) {
		if ((*p == 0xF8 && p[1] < 0x88) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
			|| (p[4] & 0xC0) != 0x80)
			return *p;             /* not valid UTF-8 */
		(*pp) += 4;
		return ((*p & 0x03) << 24) + ((p[1] & 0x3F) << 18) + ((p[2] & 0x3F) << 12) + ((p[3] & 0x3F) << 6) + (p[4] & 0x3F);
	}
	if (*p < 0xFE) {
		if ((*p == 0xFC && p[1] < 0x84) || (p[1] & 0xC0) != 0x80 || (p[2] & 0xC0) != 0x80 || (p[3] & 0xC0) != 0x80
			|| (p[4] & 0xC0) != 0x80 || (p[5] & 0xC0) != 0x80)
			return *p;             /* not valid UTF-8 */
		(*pp) += 5;
		return ((*p & 0x01) << 30) + ((p[1] & 0x3F) << 24) + ((p[2] & 0x3F) << 18) + ((p[3] & 0x3F) << 12) + ((p[4] & 0x3F) << 6) + (p[5] & 0x3F);
	}
	return *p;                   /* not sensible */
}

/*--- packsms7: takes a binary header (udhl bytes at udh) and UCS-2 message (udl characters at ud) and packs in to o using SMS 7 bit character codes */
/* The return value is the number of septets packed in to o, which is internally limited to SMSLEN */
/* o can be null, in which case this is used to validate or count only */
/* if the input contains invalid characters then the return value is -1 */
static int packsms7 (unsigned char *o, int udhl, unsigned char *udh, int udl, unsigned short *ud)
{
	 unsigned char p = 0, b = 0, n = 0;

	if (udhl) {                            /* header */
		if (o)
			o[p++] = udhl;
		b = 1;
		n = 1;
		while (udhl--) {
			if (o)
				o[p++] = *udh++;
			b += 8;
			while (b >= 7) {
				b -= 7;
				n++;
			}
			if (n >= SMSLEN)
				return n;
		}
		if (b) {
			b = 7 - b;
			if (++n >= SMSLEN)
				return n;
			};	/* filling to septet boundary */
		}
		if (o)
			o[p] = 0;
		/* message */
		while (udl--) {
			long u;
			unsigned char v;
			u = *ud++;
			for (v = 0; v < 128 && defaultalphabet[v] != u; v++);
			if (v == 128 && u && n + 1 < SMSLEN) {
				for (v = 0; v < 128 && escapes[v] != u; v++);
				if (v < 128) {	/* escaped sequence */
				if (o)
					o[p] |= (27 << b);
				b += 7;
				if (b >= 8) {
					b -= 8;
					p++;
					if (o)
						o[p] = (27 >> (7 - b));
				}
				n++;
			}
		}
		if (v == 128)
			return -1;             /* invalid character */
		if (o)
			o[p] |= (v << b);
		b += 7;
		if (b >= 8) {
			b -= 8;
			p++;
			if (o)
				o[p] = (v >> (7 - b));
		}
		if (++n >= SMSLEN)
			return n;
	}
	return n;
}

/*--- packsms8: takes a binary header (udhl bytes at udh) and UCS-2 message (udl characters at ud) and packs in to o using 8 bit character codes */
/* The return value is the number of bytes packed in to o, which is internally limited to 140 */
/* o can be null, in which case this is used to validate or count only */
/* if the input contains invalid characters then the return value is -1 */
static int packsms8 (unsigned char *o, int udhl, unsigned char *udh, int udl, unsigned short *ud)
{
	unsigned char p = 0;

	/* header - no encoding */
	if (udhl) {
		if (o)
			o[p++] = udhl;
		while (udhl--) {
			if (o)
				o[p++] = *udh++;
			if (p >= 140)
				return p;
		}
	}
	while (udl--) {
		long u;
		u = *ud++;
		if (u < 0 || u > 0xFF)
			return -1;             /* not valid */
		if (o)
			o[p++] = u;
		if (p >= 140)
			return p;
	}
	return p;
}

/*--- packsms16: takes a binary header (udhl bytes at udh) and UCS-2 
	message (udl characters at ud) and packs in to o using 16 bit 
	UCS-2 character codes 
	The return value is the number of bytes packed in to o, which is 
	internally limited to 140 
	o can be null, in which case this is used to validate or count 
	only if the input contains invalid characters then 
	the return value is -1 */
static int packsms16 (unsigned char *o, int udhl, unsigned char *udh, int udl, unsigned short *ud)
{
	unsigned char p = 0;
	/* header - no encoding */
	if (udhl) {
		if (o)
			o[p++] = udhl;
		while (udhl--) {
			if (o)
				o[p++] = *udh++;
			if (p >= 140)
				return p;
		}
	}
	while (udl--) {
		long u;
		u = *ud++;
		if (o)
			o[p++] = (u >> 8);
		if (p >= 140)
			return p - 1;          /* could not fit last character */
		if (o)
			o[p++] = u;
		if (p >= 140)
			return p;
	}
	return p;
}

/*--- packsms: general pack, with length and data, 
	returns number of bytes of target used */
static int packsms (unsigned char dcs, unsigned char *base, unsigned int udhl, unsigned char *udh, int udl, unsigned short *ud)
{
	unsigned char *p = base;
	if (udl) {
		int l = 0;
		if (is7bit (dcs)) {		 /* 7 bit */
			l = packsms7 (p + 1, udhl, udh, udl, ud);
			if (l < 0)
				l = 0;
			*p++ = l;
			p += (l * 7 + 7) / 8;
		} else if (is8bit (dcs)) {								 /* 8 bit */
			l = packsms8 (p + 1, udhl, udh, udl, ud);
			if (l < 0)
				l = 0;
			*p++ = l;
			p += l;
		} else {			 /* UCS-2 */
			l = packsms16 (p + 1, udhl, udh, udl, ud);
			if (l < 0)
				l = 0;
			*p++ = l;
			p += l;
		}
	} else
		*p++ = 0;			  /* no user data */
	return p - base;
}


/*--- packdate: pack a date and return */
static void packdate (unsigned char *o, time_t w)
{
	struct tm *t = localtime (&w);
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__)
	int z = -t->tm_gmtoff / 60 / 15;
#else
	int z = timezone / 60 / 15;
#endif
	*o++ = ((t->tm_year % 10) << 4) + (t->tm_year % 100) / 10;
	*o++ = (((t->tm_mon + 1) % 10) << 4) + (t->tm_mon + 1) / 10;
	*o++ = ((t->tm_mday % 10) << 4) + t->tm_mday / 10;
	*o++ = ((t->tm_hour % 10) << 4) + t->tm_hour / 10;
	*o++ = ((t->tm_min % 10) << 4) + t->tm_min / 10;
	*o++ = ((t->tm_sec % 10) << 4) + t->tm_sec / 10;
	if (z < 0)
		*o++ = (((-z) % 10) << 4) + (-z) / 10 + 0x08;
	else
		*o++ = ((z % 10) << 4) + z / 10;
}

/*--- unpackdate: unpack a date and return */
static time_t unpackdate (unsigned char *i)
{
	struct tm t;
	t.tm_year = 100 + (i[0] & 0xF) * 10 + (i[0] >> 4);
	t.tm_mon = (i[1] & 0xF) * 10 + (i[1] >> 4) - 1;
	t.tm_mday = (i[2] & 0xF) * 10 + (i[2] >> 4);
	t.tm_hour = (i[3] & 0xF) * 10 + (i[3] >> 4);
	t.tm_min = (i[4] & 0xF) * 10 + (i[4] >> 4);
	t.tm_sec = (i[5] & 0xF) * 10 + (i[5] >> 4);
	t.tm_isdst = 0;
	if (i[6] & 0x08)
		t.tm_min += 15 * ((i[6] & 0x7) * 10 + (i[6] >> 4));
	else
		t.tm_min -= 15 * ((i[6] & 0x7) * 10 + (i[6] >> 4));
	return mktime (&t);
}

/*--- unpacksms7: unpacks bytes (7 bit encoding) at i, len l septets, 
	and places in udh and ud setting udhl and udl. udh not used 
	if udhi not set */
static void unpacksms7 (unsigned char *i, unsigned char l, unsigned char *udh, int *udhl, unsigned short *ud, int *udl, char udhi)
{
	unsigned char b = 0, p = 0;
	unsigned short *o = ud;
	*udhl = 0;
	if (udhi && l) {		 /* header */
		int h = i[p];
		*udhl = h;
		if (h) {
			b = 1;
			p++;
			l--;
			while (h-- && l) {
				*udh++ = i[p++];
				b += 8;
				while (b >= 7) {
					b -= 7;
					l--;
					if (!l)
						break;
				}
			}
			/* adjust for fill, septets */
			if (b) {
				b = 7 - b;
				l--;
			}
		}
	}
	while (l--) {
		unsigned char v;
		if (b < 2)
			v = ((i[p] >> b) & 0x7F);
		else
			v = ((((i[p] >> b) + (i[p + 1] << (8 - b)))) & 0x7F);
		b += 7;
		if (b >= 8) {
			b -= 8;
			p++;
		}
		if (o > ud && o[-1] == 0x00A0 && escapes[v])
			o[-1] = escapes[v];
		else
			*o++ = defaultalphabet[v];
	}
	*udl = (o - ud);
}

/*--- unpacksms8: unpacks bytes (8 bit encoding) at i, len l septets, 
      and places in udh and ud setting udhl and udl. udh not used 
      if udhi not set */
static void unpacksms8 (unsigned char *i, unsigned char l, unsigned char *udh, int *udhl, unsigned short *ud, int *udl, char udhi)
{
	unsigned short *o = ud;
	*udhl = 0;
	if (udhi) {
		int n = *i;
		*udhl = n;
		if (n) {
			i++;
			l--;
			while (l && n) {
				l--;
				n--;
				*udh++ = *i++;
			}
		}
	}
	while (l--)
		*o++ = *i++;	  /* not to UTF-8 as explicitely 8 bit coding in DCS */
	*udl = (o - ud);
}

/*--- unpacksms16: unpacks bytes (16 bit encoding) at i, len l septets,
	 and places in udh and ud setting udhl and udl. 
	udh not used if udhi not set */
static void unpacksms16 (unsigned char *i, unsigned char l, unsigned char *udh, int *udhl, unsigned short *ud, int *udl, char udhi)
{
	unsigned short *o = ud;
	*udhl = 0;
	if (udhi) {
		int n = *i;
		*udhl = n;
		if (n) {
			i++;
			l--;
			while (l && n) {
				l--;
				n--;
				*udh++ = *i++;
			}
		}
	}
	while (l--) {
		int v = *i++;
		if (l--)
			v = (v << 8) + *i++;
		*o++ = v;
	}
	*udl = (o - ud);
}

/*--- unpacksms: general unpack - starts with length byte (octet or septet) and returns number of bytes used, inc length */
static int unpacksms (unsigned char dcs, unsigned char *i, unsigned char *udh, int *udhl, unsigned short *ud, int *udl, char udhi)
{
	int l = *i++;
	if (is7bit (dcs)) {
		unpacksms7 (i, l, udh, udhl, ud, udl, udhi);
		l = (l * 7 + 7) / 8;		/* adjust length to return */
	} else if (is8bit (dcs))
		unpacksms8 (i, l, udh, udhl, ud, udl, udhi);
	else
		unpacksms16 (i, l, udh, udhl, ud, udl, udhi);
	return l + 1;
}

/*--- unpackaddress: unpack an address from i, return byte length, unpack to o */
static unsigned char unpackaddress (char *o, unsigned char *i)
{
	unsigned char l = i[0],
		p;
	if (i[1] == 0x91)
		*o++ = '+';
	for (p = 0; p < l; p++) {
		if (p & 1)
			*o++ = (i[2 + p / 2] >> 4) + '0';
		else
			*o++ = (i[2 + p / 2] & 0xF) + '0';
	}
	*o = 0;
	return (l + 5) / 2;
}

/*--- packaddress: store an address at o, and return number of bytes used */
static unsigned char packaddress (unsigned char *o, char *i)
{
	unsigned char p = 2;
	o[0] = 0;
	if (*i == '+') {
		i++;
		o[1] = 0x91;
	} else
		o[1] = 0x81;
	while (*i)
		if (isdigit (*i)) {
			if (o[0] & 1)
				o[p++] |= ((*i & 0xF) << 4);
			else
				o[p] = (*i & 0xF);
			o[0]++;
			i++;
		} else
			i++;
	if (o[0] & 1)
		o[p++] |= 0xF0;			  /* pad */
	return p;
}

/*--- sms_log: Log the output, and remove file */
static void sms_log (sms_t * h, char status)
{
	if (*h->oa || *h->da) {
		int o = open (log_file, O_CREAT | O_APPEND | O_WRONLY, 0666);
		if (o >= 0) {
			char line[1000], mrs[3] = "", *p;
			unsigned char n;

			if (h->mr >= 0)
				snprintf (mrs, sizeof (mrs), "%02X", h->mr);
			snprintf (line, sizeof (line), "%s %c%c%c%s %s %s %s ",
				 isodate (time (0)), status, h->rx ? 'I' : 'O', h->smsc ? 'S' : 'M', mrs, h->queue, *h->oa ? h->oa : "-",
				 *h->da ? h->da : "-");
			p = line + strlen (line);
			for (n = 0; n < h->udl; n++)
				if (h->ud[n] == '\\') {
					*p++ = '\\';
					*p++ = '\\';
				} else if (h->ud[n] == '\n') {
					*p++ = '\\';
					*p++ = 'n';
				} else if (h->ud[n] == '\r') {
					*p++ = '\\';
					*p++ = 'r';
				} else if (h->ud[n] < 32 || h->ud[n] == 127)
					*p++ = 191;
				else
					*p++ = h->ud[n];
			*p++ = '\n';
			*p = 0;
			write (o, line, strlen (line));
			close (o);
		}
		*h->oa = *h->da = h->udl = 0;
	}
}

/*--- sms_readfile: parse and delete a file */
static void sms_readfile (sms_t * h, char *fn)
{
	char line[1000];
	FILE *s;
	char dcsset = 0;				 /* if DSC set */
	ast_log (LOG_EVENT, "Sending %s\n", fn);
	h->rx = h->udl = *h->oa = *h->da = h->pid = h->srr = h->udhi = h->rp = h->vp = h->udhl = 0;
	h->mr = -1;
	h->dcs = 0xF1;					/* normal messages class 1 */
	h->scts = time (0);
	s = fopen (fn, "r");
	if (s)
	{
		if (unlink (fn))
		{								 /* concurrent access, we lost */
			fclose (s);
			return;
		}
		while (fgets (line, sizeof (line), s))
		{								 /* process line in file */
			unsigned char *p;
			for (p = line; *p && *p != '\n' && *p != '\r'; p++);
			*p = 0;					 /* strip eoln */
			p = line;
			if (!*p || *p == ';')
				continue;			  /* blank line or comment, ignore */
			while (isalnum (*p))
			{
				*p = tolower (*p);
				p++;
			}
			while (isspace (*p))
				*p++ = 0;
			if (*p == '=')
			{
				*p++ = 0;
				if (!strcmp (line, "ud"))
				{						 /* parse message (UTF-8) */
					unsigned char o = 0;
					while (*p && o < SMSLEN)
						h->ud[o++] = utf8decode((unsigned char **)&p);
					h->udl = o;
					if (*p)
						ast_log (LOG_WARNING, "UD too long in %s\n", fn);
				} else
				{
					while (isspace (*p))
						p++;
					if (!strcmp (line, "oa") && strlen (p) < sizeof (h->oa))
						numcpy (h->oa, p);
					else if (!strcmp (line, "da") && strlen (p) < sizeof (h->oa))
						numcpy (h->da, p);
					else if (!strcmp (line, "pid"))
						h->pid = atoi (p);
					else if (!strcmp (line, "dcs"))
					{
						h->dcs = atoi (p);
						dcsset = 1;
					} else if (!strcmp (line, "mr"))
						h->mr = atoi (p);
					else if (!strcmp (line, "srr"))
						h->srr = (atoi (p) ? 1 : 0);
					else if (!strcmp (line, "vp"))
						h->vp = atoi (p);
					else if (!strcmp (line, "rp"))
						h->rp = (atoi (p) ? 1 : 0);
					else if (!strcmp (line, "scts"))
					{					 /* get date/time */
						int Y,
						  m,
						  d,
						  H,
						  M,
						  S;
						if (sscanf (p, "%d-%d-%dT%d:%d:%d", &Y, &m, &d, &H, &M, &S) == 6)
						{
							struct tm t;
							t.tm_year = Y - 1900;
							t.tm_mon = m - 1;
							t.tm_mday = d;
							t.tm_hour = H;
							t.tm_min = M;
							t.tm_sec = S;
							t.tm_isdst = -1;
							h->scts = mktime (&t);
							if (h->scts == (time_t) - 1)
								ast_log (LOG_WARNING, "Bad date/timein %s: %s", fn, p);
						}
					} else
						ast_log (LOG_WARNING, "Cannot parse in %s: %s=%si\n", fn, line, p);
				}
			} else if (*p == '#')
			{							 /* raw hex format */
				*p++ = 0;
				if (*p == '#')
				{
					p++;
					if (!strcmp (line, "ud"))
					{					 /* user data */
						int o = 0;
						while (*p && o < SMSLEN)
						{
							if (isxdigit (*p) && isxdigit (p[1]) && isxdigit (p[2]) && isxdigit (p[3]))
							{
								h->ud[o++] =
									(((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 12) +
									(((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF)) << 8) +
									(((isalpha (p[2]) ? 9 : 0) + (p[2] & 0xF)) << 4) + ((isalpha (p[3]) ? 9 : 0) + (p[3] & 0xF));
								p += 4;
							} else
								break;
						}
						h->udl = o;
						if (*p)
							ast_log (LOG_WARNING, "UD too long / invalid UCS-2 hex in %s\n", fn);
					} else
						ast_log (LOG_WARNING, "Only ud can use ## format, %s\n", fn);
				} else if (!strcmp (line, "ud"))
				{						 /* user data */
					int o = 0;
					while (*p && o < SMSLEN)
					{
						if (isxdigit (*p) && isxdigit (p[1]))
						{
							h->ud[o++] = (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF));
							p += 2;
						} else
							break;
					}
					h->udl = o;
					if (*p)
						ast_log (LOG_WARNING, "UD too long / invalid UCS-1 hex in %s\n", fn);
				} else if (!strcmp (line, "udh"))
				{						 /* user data header */
					unsigned char o = 0;
					h->udhi = 1;
					while (*p && o < SMSLEN)
					{
						if (isxdigit (*p) && isxdigit (p[1]))
						{
							h->udh[o] = (((isalpha (*p) ? 9 : 0) + (*p & 0xF)) << 4) + ((isalpha (p[1]) ? 9 : 0) + (p[1] & 0xF));
							o++;
							p += 2;
						} else
							break;
					}
					h->udhl = o;
					if (*p)
						ast_log (LOG_WARNING, "UDH too long / invalid hex in %s\n", fn);
				} else
					ast_log (LOG_WARNING, "Only ud and udh can use # format, %s\n", fn);
			} else
				ast_log (LOG_WARNING, "Cannot parse in %s: %s\n", fn, line);
		}
		fclose (s);
		if (!dcsset && packsms7 (0, h->udhl, h->udh, h->udl, h->ud) < 0)
		{
			if (packsms8 (0, h->udhl, h->udh, h->udl, h->ud) < 0)
			{
				if (packsms16 (0, h->udhl, h->udh, h->udl, h->ud) < 0)
					ast_log (LOG_WARNING, "Invalid UTF-8 message even for UCS-2 (%s)\n", fn);
				else
				{
					h->dcs = 0x08;	/* default to 16 bit */
					ast_log (LOG_WARNING, "Sending in 16 bit format (%s)\n", fn);
				}
			} else
			{
				h->dcs = 0xF5;		/* default to 8 bit */
				ast_log (LOG_WARNING, "Sending in 8 bit format (%s)\n", fn);
			}
		}
		if (is7bit (h->dcs) && packsms7 (0, h->udhl, h->udh, h->udl, h->ud) < 0)
			ast_log (LOG_WARNING, "Invalid 7 bit GSM data %s\n", fn);
		if (is8bit (h->dcs) && packsms8 (0, h->udhl, h->udh, h->udl, h->ud) < 0)
			ast_log (LOG_WARNING, "Invalid 8 bit data %s\n", fn);
		if (is16bit (h->dcs) && packsms16 (0, h->udhl, h->udh, h->udl, h->ud) < 0)
			ast_log (LOG_WARNING, "Invalid 16 bit data %s\n", fn);
	}
}

/*--- sms_writefile: white a received text message to a file */
static void sms_writefile (sms_t * h)
{
	char fn[200] = "", fn2[200] = "";
	FILE *o;
	ast_copy_string (fn, spool_dir, sizeof (fn));
	mkdir (fn, 0777);			/* ensure it exists */
	snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s", h->smsc ? h->rx ? "morx" : "mttx" : h->rx ? "mtrx" : "motx");
	mkdir (fn, 0777);			/* ensure it exists */
	ast_copy_string (fn2, fn, sizeof (fn2));
	snprintf (fn2 + strlen (fn2), sizeof (fn2) - strlen (fn2), "/%s.%s-%d", h->queue, isodate (h->scts), seq++);
	snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/.%s", fn2 + strlen (fn) + 1);
	o = fopen (fn, "w");
	if (o) {
		if (*h->oa)
			fprintf (o, "oa=%s\n", h->oa);
		if (*h->da)
			fprintf (o, "da=%s\n", h->da);
		if (h->udhi) {
			unsigned int p;
			fprintf (o, "udh#");
			for (p = 0; p < h->udhl; p++)
				fprintf (o, "%02X", h->udh[p]);
			fprintf (o, "\n");
		}
		if (h->udl) {
			unsigned int p;
			for (p = 0; p < h->udl && h->ud[p] >= ' '; p++);
			if (p < h->udl)
				fputc (';', o);	  /* cannot use ud=, but include as a comment for human readable */
			fprintf (o, "ud=");
			for (p = 0; p < h->udl; p++) {
				unsigned short v = h->ud[p];
				if (v < 32)
					fputc (191, o);
				else if (v < 0x80)
					fputc (v, o);
				else if (v < 0x800)
				{
					fputc (0xC0 + (v >> 6), o);
					fputc (0x80 + (v & 0x3F), o);
				} else
				{
					fputc (0xE0 + (v >> 12), o);
					fputc (0x80 + ((v >> 6) & 0x3F), o);
					fputc (0x80 + (v & 0x3F), o);
				}
			}
			fprintf (o, "\n");
			for (p = 0; p < h->udl && h->ud[p] >= ' '; p++);
			if (p < h->udl) {
				for (p = 0; p < h->udl && h->ud[p] < 0x100; p++);
				if (p == h->udl) {						 /* can write in ucs-1 hex */
					fprintf (o, "ud#");
					for (p = 0; p < h->udl; p++)
						fprintf (o, "%02X", h->ud[p]);
					fprintf (o, "\n");
				} else {						 /* write in UCS-2 */
					fprintf (o, "ud##");
					for (p = 0; p < h->udl; p++)
						fprintf (o, "%04X", h->ud[p]);
					fprintf (o, "\n");
				}
			}
		}
		if (h->scts)
			fprintf (o, "scts=%s\n", isodate (h->scts));
		if (h->pid)
			fprintf (o, "pid=%d\n", h->pid);
		if (h->dcs != 0xF1)
			fprintf (o, "dcs=%d\n", h->dcs);
		if (h->vp)
			fprintf (o, "vp=%d\n", h->vp);
		if (h->srr)
			fprintf (o, "srr=1\n");
		if (h->mr >= 0)
			fprintf (o, "mr=%d\n", h->mr);
		if (h->rp)
			fprintf (o, "rp=1\n");
		fclose (o);
		if (rename (fn, fn2))
			unlink (fn);
		else
			ast_log (LOG_EVENT, "Received to %s\n", fn2);
	}
}

/*--- readdirqueue: read dir skipping dot files... */
static struct dirent *readdirqueue (DIR * d, char *queue)
{
   struct dirent *f;
   do {
      f = readdir (d);
   } while (f && (*f->d_name == '.' || strncmp (f->d_name, queue, strlen (queue)) || f->d_name[strlen (queue)] != '.'));
   return f;
}

/*--- sms_handleincoming: handle the incoming message */
static unsigned char sms_handleincoming (sms_t * h)
{
	unsigned char p = 3;
	if (h->smsc) {									 /* SMSC */
		if ((h->imsg[2] & 3) == 1) {				/* SMS-SUBMIT */
			h->udhl = h->udl = 0;
			h->vp = 0;
			h->srr = ((h->imsg[2] & 0x20) ? 1 : 0);
			h->udhi = ((h->imsg[2] & 0x40) ? 1 : 0);
			h->rp = ((h->imsg[2] & 0x80) ? 1 : 0);
			ast_copy_string (h->oa, h->cli, sizeof (h->oa));
			h->scts = time (0);
			h->mr = h->imsg[p++];
			p += unpackaddress (h->da, h->imsg + p);
			h->pid = h->imsg[p++];
			h->dcs = h->imsg[p++];
			if ((h->imsg[2] & 0x18) == 0x10) {							 /* relative VP */
				if (h->imsg[p] < 144)
					h->vp = (h->imsg[p] + 1) * 5;
				else if (h->imsg[p] < 168)
					h->vp = 720 + (h->imsg[p] - 143) * 30;
				else if (h->imsg[p] < 197)
					h->vp = (h->imsg[p] - 166) * 1440;
				else
					h->vp = (h->imsg[p] - 192) * 10080;
				p++;
			} else if (h->imsg[2] & 0x18)
				p += 7;				 /* ignore enhanced / absolute VP */
			p += unpacksms (h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl, h->udhi);
			h->rx = 1;				 /* received message */
			sms_writefile (h);	  /* write the file */
			if (p != h->imsg[1] + 2) {
				ast_log (LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p, h->imsg[1] + 2);
				return 0xFF;		  /* duh! */
			}
		} else {
			ast_log (LOG_WARNING, "Unknown message type %02X\n", h->imsg[2]);
			return 0xFF;
		}
	} else {									 /* client */
		if (!(h->imsg[2] & 3)) {								 /* SMS-DELIVER */
			*h->da = h->srr = h->rp = h->vp = h->udhi = h->udhl = h->udl = 0;
			h->srr = ((h->imsg[2] & 0x20) ? 1 : 0);
			h->udhi = ((h->imsg[2] & 0x40) ? 1 : 0);
			h->rp = ((h->imsg[2] & 0x80) ? 1 : 0);
			h->mr = -1;
			p += unpackaddress (h->oa, h->imsg + p);
			h->pid = h->imsg[p++];
			h->dcs = h->imsg[p++];
			h->scts = unpackdate (h->imsg + p);
			p += 7;
			p += unpacksms (h->dcs, h->imsg + p, h->udh, &h->udhl, h->ud, &h->udl, h->udhi);
			h->rx = 1;				 /* received message */
			sms_writefile (h);	  /* write the file */
			if (p != h->imsg[1] + 2) {
				ast_log (LOG_WARNING, "Mismatch receive unpacking %d/%d\n", p, h->imsg[1] + 2);
				return 0xFF;		  /* duh! */
			}
		} else {
			ast_log (LOG_WARNING, "Unknown message type %02X\n", h->imsg[2]);
			return 0xFF;
		}
	}
	return 0;						  /* no error */
}

#ifdef SOLARIS
#define NAME_MAX 1024
#endif

/*--- sms_nextoutgoing: find and fill in next message, 
	or send a REL if none waiting */
static void sms_nextoutgoing (sms_t * h)
{          
	char fn[100 + NAME_MAX] = "";
	DIR *d;
	char more = 0;
	ast_copy_string (fn, spool_dir, sizeof (fn));
	mkdir (fn, 0777);				/* ensure it exists */
	h->rx = 0;						 /* outgoing message */
	snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s", h->smsc ? "mttx" : "motx");
	mkdir (fn, 0777);				/* ensure it exists */
	d = opendir (fn);
	if (d) {
		struct dirent *f = readdirqueue (d, h->queue);
		if (f) {
			snprintf (fn + strlen (fn), sizeof (fn) - strlen (fn), "/%s", f->d_name);
			sms_readfile (h, fn);
			if (readdirqueue (d, h->queue))
				more = 1;			  /* more to send */
		}
		closedir (d);
	}
	if (*h->da || *h->oa) {									 /* message to send */
		unsigned char p = 2;
		h->omsg[0] = 0x91;		  /* SMS_DATA */
		if (h->smsc) {			 /* deliver */
			h->omsg[p++] = (more ? 4 : 0) + ((h->udhl > 0) ? 0x40 : 0);
			p += packaddress (h->omsg + p, h->oa);
			h->omsg[p++] = h->pid;
			h->omsg[p++] = h->dcs;
			packdate (h->omsg + p, h->scts);
			p += 7;
			p += packsms (h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud);
		} else {			 /* submit */
			h->omsg[p++] =
				0x01 + (more ? 4 : 0) + (h->srr ? 0x20 : 0) + (h->rp ? 0x80 : 0) + (h->vp ? 0x10 : 0) + (h->udhi ? 0x40 : 0);
			if (h->mr < 0)
				h->mr = message_ref++;
			h->omsg[p++] = h->mr;
			p += packaddress (h->omsg + p, h->da);
			h->omsg[p++] = h->pid;
			h->omsg[p++] = h->dcs;
			if (h->vp) {		 /* relative VP */
				if (h->vp < 720)
					h->omsg[p++] = (h->vp + 4) / 5 - 1;
				else if (h->vp < 1440)
					h->omsg[p++] = (h->vp - 720 + 29) / 30 + 143;
				else if (h->vp < 43200)
					h->omsg[p++] = (h->vp + 1439) / 1440 + 166;
				else if (h->vp < 635040)
					h->omsg[p++] = (h->vp + 10079) / 10080 + 192;
				else
					h->omsg[p++] = 255;		/* max */
			}
			p += packsms (h->dcs, h->omsg + p, h->udhl, h->udh, h->udl, h->ud);
		}
		h->omsg[1] = p - 2;
		sms_messagetx (h);
	} else {				 /* no message */
		h->omsg[0] = 0x94;		  /* SMS_REL */
		h->omsg[1] = 0;
		sms_messagetx (h);
	}
}

static void sms_debug (char *dir, unsigned char *msg)
{
	char txt[259 * 3 + 1],
	 *p = txt;						 /* always long enough */
	int n = msg[1] + 3,
		q = 0;
	while (q < n && q < 30) {
		sprintf (p, " %02X", msg[q++]);
		p += 3;
	}
	if (q < n)
		sprintf (p, "...");
	if (option_verbose > 2)
		ast_verbose (VERBOSE_PREFIX_3 "SMS %s%s\n", dir, txt);
}

static void sms_messagerx(sms_t * h)
{
	sms_debug ("RX", h->imsg);
	/* testing */
	switch (h->imsg[0]) {
	case 0x91:						/* SMS_DATA */
		{
			unsigned char cause = sms_handleincoming (h);
			if (!cause) {
				sms_log (h, 'Y');
				h->omsg[0] = 0x95;  /* SMS_ACK */
				h->omsg[1] = 0x02;
				h->omsg[2] = 0x00;  /* deliver report */
				h->omsg[3] = 0x00;  /* no parameters */
			} else {							 /* NACK */
				sms_log (h, 'N');
				h->omsg[0] = 0x96;  /* SMS_NACK */
				h->omsg[1] = 3;
				h->omsg[2] = 0;	  /* delivery report */
				h->omsg[3] = cause; /* cause */
				h->omsg[4] = 0;	  /* no parameters */
			}
			sms_messagetx (h);
		}
		break;
	case 0x92:						/* SMS_ERROR */
		h->err = 1;
		sms_messagetx (h);		  /* send whatever we sent again */
		break;
	case 0x93:						/* SMS_EST */
		sms_nextoutgoing (h);
		break;
	case 0x94:						/* SMS_REL */
		h->hangup = 1;				/* hangup */
		break;
	case 0x95:						/* SMS_ACK */
		sms_log (h, 'Y');
		sms_nextoutgoing (h);
		break;
	case 0x96:						/* SMS_NACK */
		h->err = 1;
		sms_log (h, 'N');
		sms_nextoutgoing (h);
		break;
	default:						  /* Unknown */
		h->omsg[0] = 0x92;		  /* SMS_ERROR */
		h->omsg[1] = 1;
		h->omsg[2] = 3;			  /* unknown message type; */
		sms_messagetx (h);
		break;
	}
}

static void sms_messagetx(sms_t * h)
{
	unsigned char c = 0, p;
	for (p = 0; p < h->omsg[1] + 2; p++)
		c += h->omsg[p];
	h->omsg[h->omsg[1] + 2] = 0 - c;
	sms_debug ("TX", h->omsg);
	h->obyte = 1;
	h->opause = 200;
	if (h->omsg[0] == 0x93)
		h->opause = 2400;			/* initial message delay 300ms (for BT) */
	h->obytep = 0;
	h->obitp = 0;
	h->osync = 80;
	h->obyten = h->omsg[1] + 3;
}

static int sms_generate (struct ast_channel *chan, void *data, int len, int samples)
{
	struct ast_frame f = { 0 };
#define MAXSAMPLES 800
#ifdef OUTALAW
	unsigned char *buf;
#else
	short *buf;
#endif
#define SAMPLE2LEN sizeof(*buf)
	sms_t *h = data;
	int i;

	if (samples > MAXSAMPLES) {
		ast_log (LOG_WARNING, "Only doing %d samples (%d requested)\n",
			 MAXSAMPLES, samples);
		samples = MAXSAMPLES;
	}
	len = samples * SAMPLE2LEN + AST_FRIENDLY_OFFSET;
	buf = alloca(len);

	f.frametype = AST_FRAME_VOICE;
#ifdef OUTALAW
	f.subclass = AST_FORMAT_ALAW;
	f.datalen = samples;
#else
	f.subclass = AST_FORMAT_SLINEAR;
	f.datalen = samples * 2;
#endif
	f.offset = AST_FRIENDLY_OFFSET;
	f.mallocd = 0;
	f.data = buf;
	f.samples = samples;
	f.src = "app_sms";
	/* create a buffer containing the digital sms pattern */
	for (i = 0; i < samples; i++) {
#ifdef OUTALAW
		buf[i] = wavea[0];
#else
		buf[i] = wave[0];
#endif
		if (h->opause)
			h->opause--;
		else if (h->obyten || h->osync) {								 /* sending data */
#ifdef OUTALAW
			buf[i] = wavea[h->ophase];
#else
			buf[i] = wave[h->ophase];
#endif
			if ((h->ophase += ((h->obyte & 1) ? 13 : 21)) >= 80)
				h->ophase -= 80;
			if ((h->ophasep += 12) >= 80) {							 /* next bit */
				h->ophasep -= 80;
				if (h->osync)
					h->osync--;		/* sending sync bits */
				else {
					h->obyte >>= 1;
					h->obitp++;
					if (h->obitp == 1)
						h->obyte = 0; /* start bit; */
					else if (h->obitp == 2)
						h->obyte = h->omsg[h->obytep];
					else if (h->obitp == 10) {
						h->obyte = 1; /* stop bit */
						h->obitp = 0;
						h->obytep++;
						if (h->obytep == h->obyten) {
							h->obytep = h->obyten = 0; /* sent */
							h->osync = 10;	  /* trailing marks */
						}
					}
				}
			}
		}
	}
	if (ast_write (chan, &f) < 0) {
		ast_log (LOG_WARNING, "Failed to write frame to '%s': %s\n", chan->name, strerror (errno));
		return -1;
	}
	return 0;
}

static void sms_process (sms_t * h, int samples, signed short *data)
{
	if (h->obyten || h->osync)
		return;						 /* sending */
	while (samples--) {
		unsigned long long m0, m1;
		if (abs (*data) > h->imag)
			h->imag = abs (*data);
		else
			h->imag = h->imag * 7 / 8;
		if (h->imag > 500) {
			h->idle = 0;
			h->ims0 = (h->ims0 * 6 + *data * wave[h->ips0]) / 7;
			h->imc0 = (h->imc0 * 6 + *data * wave[h->ipc0]) / 7;
			h->ims1 = (h->ims1 * 6 + *data * wave[h->ips1]) / 7;
			h->imc1 = (h->imc1 * 6 + *data * wave[h->ipc1]) / 7;
			m0 = h->ims0 * h->ims0 + h->imc0 * h->imc0;
			m1 = h->ims1 * h->ims1 + h->imc1 * h->imc1;
			if ((h->ips0 += 21) >= 80)
				h->ips0 -= 80;
			if ((h->ipc0 += 21) >= 80)
				h->ipc0 -= 80;
			if ((h->ips1 += 13) >= 80)
				h->ips1 -= 80;
			if ((h->ipc1 += 13) >= 80)
				h->ipc1 -= 80;
			{
				char bit;
				h->ibith <<= 1;
				if (m1 > m0)
					h->ibith |= 1;
				if (h->ibith & 8)
					h->ibitt--;
				if (h->ibith & 1)
					h->ibitt++;
				bit = ((h->ibitt > 1) ? 1 : 0);
				if (bit != h->ibitl)
					h->ibitc = 1;
				else
					h->ibitc++;
				h->ibitl = bit;
				if (!h->ibitn && h->ibitc == 4 && !bit) {
					h->ibitn = 1;
					h->iphasep = 0;
				}
				if (bit && h->ibitc == 200) {						 /* sync, restart message */
					h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
				}
				if (h->ibitn) {
					h->iphasep += 12;
					if (h->iphasep >= 80) {					 /* next bit */
						h->iphasep -= 80;
						if (h->ibitn++ == 9) {				 /* end of byte */
							if (!bit)  /* bad stop bit */
								h->ierr = 0xFF; /* unknown error */
							else {
								if (h->ibytep < sizeof (h->imsg)) {
									h->imsg[h->ibytep] = h->ibytev;
									h->ibytec += h->ibytev;
									h->ibytep++;
								} else if (h->ibytep == sizeof (h->imsg))
									h->ierr = 2; /* bad message length */
								if (h->ibytep > 1 && h->ibytep == 3 + h->imsg[1] && !h->ierr) {
									if (!h->ibytec)
										sms_messagerx (h);
									else
										h->ierr = 1;		/* bad checksum */
								}
							}
							h->ibitn = 0;
						}
						h->ibytev = (h->ibytev >> 1) + (bit ? 0x80 : 0);
					}
				}
			}
		} else {			 /* lost carrier */
			if (h->idle++ == 80000) {		 /* nothing happening */
				ast_log (LOG_EVENT, "No data, hanging up\n");
				h->hangup = 1;
				h->err = 1;
			}
			if (h->ierr) {							 /* error */
				h->err = 1;
				h->omsg[0] = 0x92;  /* error */
				h->omsg[1] = 1;
				h->omsg[2] = h->ierr;
				sms_messagetx (h);  /* send error */
			}
			h->ierr = h->ibitn = h->ibytep = h->ibytec = 0;
		}
		data++;
	}
}

static struct ast_generator smsgen = {
	alloc:sms_alloc,
	release:sms_release,
	generate:sms_generate,
};

static int sms_exec (struct ast_channel *chan, void *data)
{
	int res = -1;
	struct localuser *u;
	struct ast_frame *f;
	sms_t h = { 0 };
	
	LOCAL_USER_ADD(u);

	h.ipc0 = h.ipc1 = 20;		  /* phase for cosine */
	h.dcs = 0xF1;					 /* default */
	if (!data) {
		ast_log (LOG_ERROR, "Requires queue name at least\n");
		LOCAL_USER_REMOVE(u);
		return -1;
	}

	if (chan->cid.cid_num)
		ast_copy_string (h.cli, chan->cid.cid_num, sizeof (h.cli));

	{
		unsigned char *p;
		unsigned char *d = data,
			answer = 0;
		if (!*d || *d == '|') {
			ast_log (LOG_ERROR, "Requires queue name\n");
			LOCAL_USER_REMOVE(u);
			return -1;
		}
		for (p = d; *p && *p != '|'; p++);
		if (p - d >= sizeof (h.queue)) {
			ast_log (LOG_ERROR, "Queue name too long\n");
			LOCAL_USER_REMOVE(u);
			return -1;
		}
		strncpy (h.queue, d, p - d);
		if (*p == '|')
			p++;
		d = p;
		for (p = h.queue; *p; p++)
			if (!isalnum (*p))
				*p = '-';			  /* make very safe for filenames */
		while (*d && *d != '|') {
			switch (*d) {
			case 'a':				 /* we have to send the initial FSK sequence */
				answer = 1;
				break;
			case 's':				 /* we are acting as a service centre talking to a phone */
				h.smsc = 1;
				break;
				/* the following apply if there is an arg3/4 and apply to the created message file */
			case 'r':
				h.srr = 1;
				break;
			case 'o':
				h.dcs |= 4;			/* octets */
				break;
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':				 /* set the pid for saved local message */
				h.pid = 0x40 + (*d & 0xF);
				break;
			}
			d++;
		}
		if (*d == '|') {
			/* submitting a message, not taking call. */
			/* depricated, use smsq instead */
			d++;
			h.scts = time (0);
			for (p = d; *p && *p != '|'; p++);
			if (*p)
				*p++ = 0;
			if (strlen (d) >= sizeof (h.oa)) {
				ast_log (LOG_ERROR, "Address too long %s\n", d);
				return 0;
			}
			if (h.smsc) {
				ast_copy_string (h.oa, d, sizeof (h.oa));
			} else {
				ast_copy_string (h.da, d, sizeof (h.da));
			}
			if (!h.smsc)
				ast_copy_string (h.oa, h.cli, sizeof (h.oa));
			d = p;
			h.udl = 0;
			while (*p && h.udl < SMSLEN)
				h.ud[h.udl++] = utf8decode(&p);
			if (is7bit (h.dcs) && packsms7 (0, h.udhl, h.udh, h.udl, h.ud) < 0)
				ast_log (LOG_WARNING, "Invalid 7 bit GSM data\n");
			if (is8bit (h.dcs) && packsms8 (0, h.udhl, h.udh, h.udl, h.ud) < 0)
				ast_log (LOG_WARNING, "Invalid 8 bit data\n");
			if (is16bit (h.dcs) && packsms16 (0, h.udhl, h.udh, h.udl, h.ud) < 0)
				ast_log (LOG_WARNING, "Invalid 16 bit data\n");
			h.rx = 0;				  /* sent message */
			h.mr = -1;
			sms_writefile (&h);
			LOCAL_USER_REMOVE(u);
			return 0;
		}

		if (answer) {
			/* set up SMS_EST initial message */
			h.omsg[0] = 0x93;
			h.omsg[1] = 0;
			sms_messagetx (&h);
		}
	}

	if (chan->_state != AST_STATE_UP)
		ast_answer (chan);

#ifdef OUTALAW
	res = ast_set_write_format (chan, AST_FORMAT_ALAW);
#else
	res = ast_set_write_format (chan, AST_FORMAT_SLINEAR);
#endif
	if (res >= 0)
		res = ast_set_read_format (chan, AST_FORMAT_SLINEAR);
	if (res < 0) {
		ast_log (LOG_ERROR, "Unable to set to linear mode, giving up\n");
		LOCAL_USER_REMOVE (u);
		return -1;
	}

	if (ast_activate_generator (chan, &smsgen, &h) < 0) {
		ast_log (LOG_ERROR, "Failed to activate generator on '%s'\n", chan->name);
		LOCAL_USER_REMOVE (u);
		return -1;
	}

	/* Do our thing here */
	while (ast_waitfor (chan, -1) > -1 && !h.hangup)
	{
		f = ast_read (chan);
		if (!f)
			break;
		if (f->frametype == AST_FRAME_VOICE) {
			sms_process (&h, f->samples, f->data);
		}

		ast_frfree (f);
	}

	sms_log (&h, '?');			  /* log incomplete message */

	LOCAL_USER_REMOVE (u);
	return (h.err);
}

int unload_module (void)
{
	int res;

	res = ast_unregister_application (app);
	
	STANDARD_HANGUP_LOCALUSERS;

	return res;	
}

int load_module (void)
{
#ifdef OUTALAW
	{
		int p;
		for (p = 0; p < 80; p++)
			wavea[p] = AST_LIN2A (wave[p]);
	}
#endif
	snprintf (log_file, sizeof (log_file), "%s/sms", ast_config_AST_LOG_DIR);
	snprintf (spool_dir, sizeof (spool_dir), "%s/sms", ast_config_AST_SPOOL_DIR);
	return ast_register_application (app, sms_exec, synopsis, descrip);
}

char *description (void)
{
	return tdesc;
}

int usecount (void)
{
	int res;
	STANDARD_USECOUNT (res);
	return res;
}

char *key ()
{
	return ASTERISK_GPL_KEY;
}