2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2004 - 2005, Adrian Kennard, rights assigned to Digium
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
19 * \brief SMS application - ETSI ES 201 912 protocol 1 implementation
21 * \par Development notes
22 * \note The ETSI standards are available free of charge from ETSI at
23 * http://pda.etsi.org/pda/queryform.asp
24 * Among the relevant documents here we have:
26 * ES 201 912 SMS for PSTN/ISDN
27 * TS 123 040 Technical realization of SMS
30 * \ingroup applications
32 * \author Adrian Kennard (for the original protocol 1 code)
33 * \author Filippo Grassilli (Hyppo) - protocol 2 support
34 * Not fully tested, under development
39 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
45 #include "asterisk/paths.h" /* use ast_config_AST_SPOOL_DIR and LOG_DIR */
46 #include "asterisk/lock.h"
47 #include "asterisk/file.h"
48 #include "asterisk/channel.h"
49 #include "asterisk/pbx.h"
50 #include "asterisk/module.h"
51 #include "asterisk/alaw.h"
52 #include "asterisk/callerid.h"
53 #include "asterisk/utils.h"
54 #include "asterisk/app.h"
56 /* #define OUTALAW */ /* enable this to output Alaw rather than linear */
59 /* Add full VP support */
60 /* Handle status report messages (generation and reception) */
61 /* Time zones on time stamps */
64 static volatile unsigned char message_ref
; /* arbitary message ref */
65 static volatile unsigned int seq
; /* arbitrary message sequence number for unqiue files */
67 static char log_file
[255];
69 static char *app
= "SMS";
71 static char *synopsis
= "Communicates with SMS service centres and SMS capable analogue phones";
73 static char *descrip
=
74 " SMS(name,[a][s][t][p(d)][r][o],addr,body):\n"
75 "SMS handles exchange of SMS data with a call to/from SMS capable\n"
76 "phone or SMS PSTN service center. Can send and/or receive SMS messages.\n"
77 "Works to ETSI ES 201 912; compatible with BT SMS PSTN service in UK\n"
78 "and Telecom Italia in Italy.\n"
79 "Typical usage is to use to handle calls from the SMS service centre CLI,\n"
80 "or to set up a call using 'outgoing' or manager interface to connect\n"
81 "service centre to SMS()\n"
82 "name is the name of the queue used in /var/spool/asterisk/sms\n"
84 " a - answer, i.e. send initial FSK packet.\n"
85 " s - act as service centre talking to a phone.\n"
86 " t - use protocol 2 (default used is protocol 1).\n"
87 " p(N) - set the initial delay to N ms (default is 300).\n"
88 " addr and body are a deprecated format to send messages out.\n"
89 " s - set the Status Report Request (SRR) bit.\n"
90 " o - the body should be coded as octets not 7-bit symbols.\n"
91 "Messages are processed as per text file message queues.\n"
92 "smsq (a separate software) is a command to generate message\n"
93 "queues and send messages.\n"
94 "NOTE: the protocol has tight delay bounds. Please use short frames\n"
95 "and disable/keep short the jitter buffer on the ATA to make sure that\n"
96 "respones (ACK etc.) are received in time.\n";
99 * 80 samples of a single period of the wave. At 8000 Hz, it means these
100 * are the samples of a 100 Hz signal.
101 * To pick the two carriers (1300Hz for '1' and 2100 Hz for '0') used by
102 * the modulation, we should take one every 13 and 21 samples respectively.
104 static signed short wave
[] = {
105 0, 392, 782, 1167, 1545, 1913, 2270, 2612, 2939, 3247, 3536, 3802, 4045, 4263, 4455, 4619, 4755, 4862, 4938, 4985,
106 5000, 4985, 4938, 4862, 4755, 4619, 4455, 4263, 4045, 3802, 3536, 3247, 2939, 2612, 2270, 1913, 1545, 1167, 782, 392,
107 0, -392, -782, -1167,
108 -1545, -1913, -2270, -2612, -2939, -3247, -3536, -3802, -4045, -4263, -4455, -4619, -4755, -4862, -4938, -4985, -5000,
110 -4755, -4619, -4455, -4263, -4045, -3802, -3536, -3247, -2939, -2612, -2270, -1913, -1545, -1167, -782, -392
114 static unsigned char wavea
[80];
115 typedef unsigned char output_t
;
116 static const output_t
*wave_out
= wavea
; /* outgoing samples */
117 #define __OUT_FMT AST_FORMAT_ALAW;
119 typedef signed short output_t
;
120 static const output_t
*wave_out
= wave
; /* outgoing samples */
121 #define __OUT_FMT AST_FORMAT_SLINEAR
124 #define OSYNC_BITS 80 /* initial sync bits */
127 * The SMS spec ETSI ES 201 912 defines two protocols with different message types.
128 * Also note that the high bit is used to indicate whether the message
129 * is complete or not, but in two opposite ways:
130 * for Protocol 1, 0x80 means that the message is complete;
131 * for Protocol 2, 0x00 means that the message is complete;
134 DLL_SMS_MASK
= 0x7f, /* mask for the valid bits */
136 /* Protocol 1 values */
137 DLL1_SMS_DATA
= 0x11, /* data packet */
138 DLL1_SMS_ERROR
= 0x12,
139 DLL1_SMS_EST
= 0x13, /* start the connection */
140 DLL1_SMS_REL
= 0x14, /* end the connection */
142 DLL1_SMS_NACK
= 0x16,
144 DLL1_SMS_COMPLETE
= 0x80, /* packet is complete */
145 DLL1_SMS_MORE
= 0x00, /* more data to follow */
147 /* Protocol 2 values */
148 DLL2_SMS_EST
= 0x7f, /* magic number. No message body */
149 DLL2_SMS_INFO_MO
= 0x10,
150 DLL2_SMS_INFO_MT
= 0x11,
151 DLL2_SMS_INFO_STA
= 0x12,
152 DLL2_SMS_NACK
= 0x13,
153 DLL2_SMS_ACK0
= 0x14, /* ack even-numbered frame */
154 DLL2_SMS_ACK1
= 0x15, /* ack odd-numbered frame */
156 DLL2_SMS_REL
= 0x17, /* end the connection */
158 DLL2_SMS_COMPLETE
= 0x00, /* packet is complete */
159 DLL2_SMS_MORE
= 0x80, /* more data to follow */
162 /* SMS 7 bit character mapping to UCS-2 */
163 static const unsigned short defaultalphabet
[] = {
164 0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC,
165 0x00F2, 0x00E7, 0x000A, 0x00D8, 0x00F8, 0x000D, 0x00C5, 0x00E5,
166 0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8,
167 0x03A3, 0x0398, 0x039E, 0x00A0, 0x00C6, 0x00E6, 0x00DF, 0x00C9,
168 ' ', '!', '"', '#', 164, '%', '&', 39, '(', ')', '*', '+', ',', '-', '.', '/',
169 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
170 161, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
171 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 196, 214, 209, 220, 167,
172 191, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
173 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 228, 246, 241, 252, 224,
176 static const unsigned short escapes
[] = {
177 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x000C, 0, 0, 0, 0, 0,
178 0, 0, 0, 0, 0x005E, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
179 0, 0, 0, 0, 0, 0, 0, 0, 0x007B, 0x007D, 0, 0, 0, 0, 0, 0x005C,
180 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x005B, 0x007E, 0x005D, 0,
181 0x007C, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
182 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183 0, 0, 0, 0, 0, 0x20AC, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
184 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
187 #define SMSLEN 160 /*!< max SMS length */
188 #define SMSLEN_8 140 /*!< max SMS length for 8-bit char */
190 typedef struct sms_s
{
191 unsigned char hangup
; /*!< we are done... */
192 unsigned char err
; /*!< set for any errors */
193 unsigned char smsc
:1; /*!< we are SMSC */
194 unsigned char rx
:1; /*!< this is a received message */
195 char queue
[30]; /*!< queue name */
196 char oa
[20]; /*!< originating address */
197 char da
[20]; /*!< destination address */
198 struct timeval scts
; /*!< time stamp, UTC */
199 unsigned char pid
; /*!< protocol ID */
200 unsigned char dcs
; /*!< data coding scheme */
201 short mr
; /*!< message reference - actually a byte, but use -1 for not set */
202 int udl
; /*!< user data length */
203 int udhl
; /*!< user data header length */
204 unsigned char srr
:1; /*!< Status Report request */
205 unsigned char udhi
:1; /*!< User Data Header required, even if length 0 */
206 unsigned char rp
:1; /*!< Reply Path */
207 unsigned int vp
; /*!< validity period in minutes, 0 for not set */
208 unsigned short ud
[SMSLEN
]; /*!< user data (message), UCS-2 coded */
209 unsigned char udh
[SMSLEN
]; /*!< user data header */
210 char cli
[20]; /*!< caller ID */
211 unsigned char ophase
; /*!< phase (0-79) for 0 and 1 frequencies (1300Hz and 2100Hz) */
212 unsigned char ophasep
; /*!< phase (0-79) for 1200 bps */
213 unsigned char obyte
; /*!< byte being sent */
214 unsigned int opause
; /*!< silent pause before sending (in sample periods) */
215 unsigned char obitp
; /*!< bit in byte */
216 unsigned char osync
; /*!< sync bits to send */
217 unsigned char obytep
; /*!< byte in data */
218 unsigned char obyten
; /*!< bytes in data */
219 unsigned char omsg
[256]; /*!< data buffer (out) */
220 unsigned char imsg
[250]; /*!< data buffer (in) */
221 signed long long ims0
,
224 imc1
; /*!< magnitude averages sin/cos 0/1 */
226 unsigned short imag
; /*!< signal level */
227 unsigned char ips0
; /*!< phase sin for bit 0, start at 0 inc by 21 mod 80 */
228 unsigned char ips1
; /*!< phase cos for bit 0, start at 20 inc by 21 mod 80 */
229 unsigned char ipc0
; /*!< phase sin for bit 1, start at 0 inc by 13 mod 80 */
230 unsigned char ipc1
; /*!< phase cos for bit 1, start at 20 inc by 13 mod 80 */
231 unsigned char ibitl
; /*!< last bit */
232 unsigned char ibitc
; /*!< bit run length count */
233 unsigned char iphasep
; /*!< bit phase (0-79) for 1200 bps */
234 unsigned char ibitn
; /*!< bit number in byte being received */
235 unsigned char ibytev
; /*!< byte value being received */
236 unsigned char ibytep
; /*!< byte pointer in message */
237 unsigned char ibytec
; /*!< byte checksum for message */
238 unsigned char ierr
; /*!< error flag */
239 unsigned char ibith
; /*!< history of last bits */
240 unsigned char ibitt
; /*!< total of 1's in last 3 bytes */
241 /* more to go here */
243 int opause_0
; /*!< initial delay in ms, p() option */
244 int protocol
; /*!< ETSI SMS protocol to use (passed at app call) */
245 int oseizure
; /*!< protocol 2: channel seizure bits to send */
246 int framenumber
; /*!< protocol 2: frame number (for sending ACK0 or ACK1) */
247 char udtxt
[SMSLEN
]; /*!< user data (message), PLAIN text */
250 /* different types of encoding */
251 #define is7bit(dcs) ( ((dcs) & 0xC0) ? (!((dcs)&4) ) : (((dcs) & 0xc) == 0) )
252 #define is8bit(dcs) ( ((dcs) & 0xC0) ? ( ((dcs)&4) ) : (((dcs) & 0xc) == 4) )
253 #define is16bit(dcs) ( ((dcs) & 0xC0) ? 0 : (((dcs) & 0xc) == 8) )
255 static void sms_messagetx(sms_t
*h
);
257 /*! \brief copy number, skipping non digits apart from leading + */
258 static void numcpy(char *d
, char *s
)
271 /*! \brief static, return a date/time in ISO format */
272 static char *isodate(time_t t
, char *buf
, int len
)
275 struct timeval tv
= { t
, 0 };
276 ast_localtime(&tv
, &tm
, NULL
);
277 ast_strftime(buf
, len
, "%Y-%m-%dT%H:%M:%S", &tm
);
281 /*! \brief Reads next UCS character from NUL terminated UTF-8 string and advance pointer */
282 /* for non valid UTF-8 sequences, returns character as is */
283 /* Does not advance pointer for null termination */
284 static long utf8decode(unsigned char **pp
)
286 unsigned char *p
= *pp
;
288 return 0; /* null termination of string */
291 return *p
; /* ascii or continuation character */
293 if (*p
< 0xC2 || (p
[1] & 0xC0) != 0x80)
294 return *p
; /* not valid UTF-8 */
296 return ((*p
& 0x1F) << 6) + (p
[1] & 0x3F);
299 if ((*p
== 0xE0 && p
[1] < 0xA0) || (p
[1] & 0xC0) != 0x80 || (p
[2] & 0xC0) != 0x80)
300 return *p
; /* not valid UTF-8 */
302 return ((*p
& 0x0F) << 12) + ((p
[1] & 0x3F) << 6) + (p
[2] & 0x3F);
305 if ((*p
== 0xF0 && p
[1] < 0x90) || (p
[1] & 0xC0) != 0x80 || (p
[2] & 0xC0) != 0x80 || (p
[3] & 0xC0) != 0x80)
306 return *p
; /* not valid UTF-8 */
308 return ((*p
& 0x07) << 18) + ((p
[1] & 0x3F) << 12) + ((p
[2] & 0x3F) << 6) + (p
[3] & 0x3F);
311 if ((*p
== 0xF8 && p
[1] < 0x88) || (p
[1] & 0xC0) != 0x80 || (p
[2] & 0xC0) != 0x80 || (p
[3] & 0xC0) != 0x80
312 || (p
[4] & 0xC0) != 0x80)
313 return *p
; /* not valid UTF-8 */
315 return ((*p
& 0x03) << 24) + ((p
[1] & 0x3F) << 18) + ((p
[2] & 0x3F) << 12) + ((p
[3] & 0x3F) << 6) + (p
[4] & 0x3F);
318 if ((*p
== 0xFC && p
[1] < 0x84) || (p
[1] & 0xC0) != 0x80 || (p
[2] & 0xC0) != 0x80 || (p
[3] & 0xC0) != 0x80
319 || (p
[4] & 0xC0) != 0x80 || (p
[5] & 0xC0) != 0x80)
320 return *p
; /* not valid UTF-8 */
322 return ((*p
& 0x01) << 30) + ((p
[1] & 0x3F) << 24) + ((p
[2] & 0x3F) << 18) + ((p
[3] & 0x3F) << 12) + ((p
[4] & 0x3F) << 6) + (p
[5] & 0x3F);
324 return *p
; /* not sensible */
327 /*! \brief 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 */
328 /* The return value is the number of septets packed in to o, which is internally limited to SMSLEN */
329 /* o can be null, in which case this is used to validate or count only */
330 /* if the input contains invalid characters then the return value is -1 */
331 static int packsms7(unsigned char *o
, int udhl
, unsigned char *udh
, int udl
, unsigned short *ud
)
333 unsigned char p
= 0; /* output pointer (bytes) */
334 unsigned char b
= 0; /* bit position */
335 unsigned char n
= 0; /* output character count */
336 unsigned char dummy
[SMSLEN
];
338 if (o
== NULL
) /* output to a dummy buffer if o not set */
341 if (udhl
) { /* header */
359 }; /* filling to septet boundary */
367 /* XXX 0 is invalid ? */
368 /* look up in defaultalphabet[]. If found, v is the 7-bit code */
369 for (v
= 0; v
< 128 && defaultalphabet
[v
] != u
; v
++);
370 if (v
== 128 /* not found */ && u
&& n
+ 1 < SMSLEN
) {
371 /* if not found, look in the escapes table (we need 2 bytes) */
372 for (v
= 0; v
< 128 && escapes
[v
] != u
; v
++);
373 if (v
< 128) { /* escaped sequence, esc + v */
374 /* store the low (8-b) bits in o[p], the remaining bits in o[p+1] */
375 o
[p
] |= (27 << b
); /* the low bits go into o[p] */
380 o
[p
] = (27 >> (7 - b
));
386 return -1; /* invalid character */
387 /* store, same as above */
393 o
[p
] = (v
>> (7 - b
));
401 /*! \brief takes a binary header (udhl bytes at udh) and UCS-2 message (udl characters at ud)
402 * and packs in to o using 8 bit character codes.
403 * The return value is the number of bytes packed in to o, which is internally limited to 140.
404 * o can be null, in which case this is used to validate or count only.
405 * if the input contains invalid characters then the return value is -1
407 static int packsms8(unsigned char *o
, int udhl
, unsigned char *udh
, int udl
, unsigned short *ud
)
410 unsigned char dummy
[SMSLEN_8
];
414 /* header - no encoding */
426 if (u
< 0 || u
> 0xFF)
427 return -1; /* not valid */
435 /*! \brief takes a binary header (udhl bytes at udh) and UCS-2
436 message (udl characters at ud) and packs in to o using 16 bit
437 UCS-2 character codes
438 The return value is the number of bytes packed in to o, which is
439 internally limited to 140
440 o can be null, in which case this is used to validate or count
441 only if the input contains invalid characters then
442 the return value is -1 */
443 static int packsms16(unsigned char *o
, int udhl
, unsigned char *udh
, int udl
, unsigned short *ud
)
446 unsigned char dummy
[SMSLEN_8
];
450 /* header - no encoding */
464 return p
- 1; /* could not fit last character */
472 /*! \brief general pack, with length and data,
473 returns number of bytes of target used */
474 static int packsms(unsigned char dcs
, unsigned char *base
, unsigned int udhl
, unsigned char *udh
, int udl
, unsigned short *ud
)
476 unsigned char *p
= base
;
478 *p
++ = 0; /* no user data */
482 if (is7bit(dcs
)) { /* 7 bit */
483 l
= packsms7(p
+ 1, udhl
, udh
, udl
, ud
);
487 p
+= (l
* 7 + 7) / 8;
488 } else if (is8bit(dcs
)) { /* 8 bit */
489 l
= packsms8(p
+ 1, udhl
, udh
, udl
, ud
);
495 l
= packsms16(p
+ 1, udhl
, udh
, udl
, ud
);
506 /*! \brief pack a date and return */
507 static void packdate(unsigned char *o
, time_t w
)
510 struct timeval tv
= { w
, 0 };
513 ast_localtime(&tv
, &t
, NULL
);
514 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__) || defined(__CYGWIN__)
515 z
= -t
.tm_gmtoff
/ 60 / 15;
517 z
= timezone
/ 60 / 15;
519 *o
++ = ((t
.tm_year
% 10) << 4) + (t
.tm_year
% 100) / 10;
520 *o
++ = (((t
.tm_mon
+ 1) % 10) << 4) + (t
.tm_mon
+ 1) / 10;
521 *o
++ = ((t
.tm_mday
% 10) << 4) + t
.tm_mday
/ 10;
522 *o
++ = ((t
.tm_hour
% 10) << 4) + t
.tm_hour
/ 10;
523 *o
++ = ((t
.tm_min
% 10) << 4) + t
.tm_min
/ 10;
524 *o
++ = ((t
.tm_sec
% 10) << 4) + t
.tm_sec
/ 10;
526 *o
++ = (((-z
) % 10) << 4) + (-z
) / 10 + 0x08;
528 *o
++ = ((z
% 10) << 4) + z
/ 10;
531 /*! \brief unpack a date and return */
532 static struct timeval
unpackdate(unsigned char *i
)
536 t
.tm_year
= 100 + (i
[0] & 0xF) * 10 + (i
[0] >> 4);
537 t
.tm_mon
= (i
[1] & 0xF) * 10 + (i
[1] >> 4) - 1;
538 t
.tm_mday
= (i
[2] & 0xF) * 10 + (i
[2] >> 4);
539 t
.tm_hour
= (i
[3] & 0xF) * 10 + (i
[3] >> 4);
540 t
.tm_min
= (i
[4] & 0xF) * 10 + (i
[4] >> 4);
541 t
.tm_sec
= (i
[5] & 0xF) * 10 + (i
[5] >> 4);
544 t
.tm_min
+= 15 * ((i
[6] & 0x7) * 10 + (i
[6] >> 4));
546 t
.tm_min
-= 15 * ((i
[6] & 0x7) * 10 + (i
[6] >> 4));
548 return ast_mktime(&t
, NULL
);
551 /*! \brief unpacks bytes (7 bit encoding) at i, len l septets,
552 and places in udh and ud setting udhl and udl. udh not used
554 static void unpacksms7(unsigned char *i
, unsigned char l
, unsigned char *udh
, int *udhl
, unsigned short *ud
, int *udl
, char udhi
)
556 unsigned char b
= 0, p
= 0;
557 unsigned short *o
= ud
;
559 if (udhi
&& l
) { /* header */
576 /* adjust for fill, septets */
586 v
= ((i
[p
] >> b
) & 0x7F); /* everything in one byte */
588 v
= ((((i
[p
] >> b
) + (i
[p
+ 1] << (8 - b
)))) & 0x7F);
594 /* 0x00A0 is the encoding of ESC (27) in defaultalphabet */
595 if (o
> ud
&& o
[-1] == 0x00A0 && escapes
[v
])
598 *o
++ = defaultalphabet
[v
];
603 /*! \brief unpacks bytes (8 bit encoding) at i, len l septets,
604 * and places in udh and ud setting udhl and udl. udh not used
607 static void unpacksms8(unsigned char *i
, unsigned char l
, unsigned char *udh
, int *udhl
, unsigned short *ud
, int *udl
, char udhi
)
609 unsigned short *o
= ud
;
625 *o
++ = *i
++; /* not to UTF-8 as explicitly 8 bit coding in DCS */
629 /*! \brief unpacks bytes (16 bit encoding) at i, len l septets,
630 and places in udh and ud setting udhl and udl.
631 udh not used if udhi not set */
632 static void unpacksms16(unsigned char *i
, unsigned char l
, unsigned char *udh
, int *udhl
, unsigned short *ud
, int *udl
, char udhi
)
634 unsigned short *o
= ud
;
658 /*! \brief general unpack - starts with length byte (octet or septet) and returns number of bytes used, inc length */
659 static int unpacksms(unsigned char dcs
, unsigned char *i
, unsigned char *udh
, int *udhl
, unsigned short *ud
, int *udl
, char udhi
)
663 unpacksms7(i
, l
, udh
, udhl
, ud
, udl
, udhi
);
664 l
= (l
* 7 + 7) / 8; /* adjust length to return */
665 } else if (is8bit(dcs
))
666 unpacksms8(i
, l
, udh
, udhl
, ud
, udl
, udhi
);
668 unpacksms16(i
, l
, udh
, udhl
, ud
, udl
, udhi
);
672 /*! \brief unpack an address from i, return byte length, unpack to o */
673 static unsigned char unpackaddress(char *o
, unsigned char *i
)
675 unsigned char l
= i
[0],
679 for (p
= 0; p
< l
; p
++) {
681 *o
++ = (i
[2 + p
/ 2] >> 4) + '0';
683 *o
++ = (i
[2 + p
/ 2] & 0xF) + '0';
689 /*! \brief store an address at o, and return number of bytes used */
690 static unsigned char packaddress(unsigned char *o
, char *i
)
693 o
[0] = 0; /* number of bytes */
694 if (*i
== '+') { /* record as bit 0 in byte 1 */
700 if (!isdigit(*i
)) /* ignore non-digits */
703 o
[p
++] |= ((*i
& 0xF) << 4);
709 o
[p
++] |= 0xF0; /* pad */
713 /*! \brief Log the output, and remove file */
714 static void sms_log(sms_t
* h
, char status
)
718 if (*h
->oa
== '\0' && *h
->da
== '\0')
720 o
= open(log_file
, O_CREAT
| O_APPEND
| O_WRONLY
, AST_FILE_MODE
);
722 char line
[1000], mrs
[3] = "", *p
;
727 snprintf(mrs
, sizeof(mrs
), "%02X", h
->mr
);
728 snprintf(line
, sizeof(line
), "%s %c%c%c%s %s %s %s ",
729 isodate(time(NULL
), buf
, sizeof(buf
)),
730 status
, h
->rx
? 'I' : 'O', h
->smsc
? 'S' : 'M', mrs
, h
->queue
,
731 S_OR(h
->oa
, "-"), S_OR(h
->da
, "-") );
732 p
= line
+ strlen(line
);
733 for (n
= 0; n
< h
->udl
; n
++) {
734 if (h
->ud
[n
] == '\\') {
737 } else if (h
->ud
[n
] == '\n') {
740 } else if (h
->ud
[n
] == '\r') {
743 } else if (h
->ud
[n
] < 32 || h
->ud
[n
] == 127)
750 write(o
, line
, strlen(line
));
753 *h
->oa
= *h
->da
= h
->udl
= 0;
756 /*! \brief parse and delete a file */
757 static void sms_readfile(sms_t
* h
, char *fn
)
761 char dcsset
= 0; /* if DSC set */
762 ast_log(LOG_EVENT
, "Sending %s\n", fn
);
763 h
->rx
= h
->udl
= *h
->oa
= *h
->da
= h
->pid
= h
->srr
= h
->udhi
= h
->rp
= h
->vp
= h
->udhl
= 0;
765 h
->dcs
= 0xF1; /* normal messages class 1 */
766 h
->scts
= ast_tvnow();
769 if (unlink(fn
)) { /* concurrent access, we lost */
773 while (fgets (line
, sizeof(line
), s
)) { /* process line in file */
776 for (p
= line
; *p
&& *p
!= '\n' && *p
!= '\r'; p
++);
777 *p
= 0; /* strip eoln */
779 if (!*p
|| *p
== ';')
780 continue; /* blank line or comment, ignore */
781 while (isalnum(*p
)) {
789 if (!strcmp(line
, "ud")) { /* parse message (UTF-8) */
791 memcpy(h
->udtxt
, p
, SMSLEN
); /* for protocol 2 */
792 while (*p
&& o
< SMSLEN
)
793 h
->ud
[o
++] = utf8decode(pp
);
796 ast_log(LOG_WARNING
, "UD too long in %s\n", fn
);
800 if (!strcmp(line
, "oa") && strlen(p
) < sizeof(h
->oa
))
802 else if (!strcmp(line
, "da") && strlen(p
) < sizeof(h
->oa
))
804 else if (!strcmp(line
, "pid"))
806 else if (!strcmp(line
, "dcs")) {
809 } else if (!strcmp(line
, "mr"))
811 else if (!strcmp(line
, "srr"))
812 h
->srr
= (atoi(p
) ? 1 : 0);
813 else if (!strcmp(line
, "vp"))
815 else if (!strcmp(line
, "rp"))
816 h
->rp
= (atoi(p
) ? 1 : 0);
817 else if (!strcmp(line
, "scts")) { /* get date/time */
824 if (sscanf (p
, "%d-%d-%dT%d:%d:%d", &Y
, &m
, &d
, &H
, &M
, &S
) == 6) {
825 struct ast_tm t
= { 0, };
826 t
.tm_year
= Y
- 1900;
833 h
->scts
= ast_mktime(&t
, NULL
);
834 if (h
->scts
.tv_sec
== 0)
835 ast_log(LOG_WARNING
, "Bad date/timein %s: %s", fn
, p
);
838 ast_log(LOG_WARNING
, "Cannot parse in %s: %s=%si\n", fn
, line
, p
);
840 } else if (*p
== '#') { /* raw hex format */
844 if (!strcmp(line
, "ud")) { /* user data */
846 while (*p
&& o
< SMSLEN
) {
847 if (isxdigit(*p
) && isxdigit(p
[1]) && isxdigit(p
[2]) && isxdigit(p
[3])) {
849 (((isalpha(*p
) ? 9 : 0) + (*p
& 0xF)) << 12) +
850 (((isalpha(p
[1]) ? 9 : 0) + (p
[1] & 0xF)) << 8) +
851 (((isalpha(p
[2]) ? 9 : 0) + (p
[2] & 0xF)) << 4) + ((isalpha(p
[3]) ? 9 : 0) + (p
[3] & 0xF));
858 ast_log(LOG_WARNING
, "UD too long / invalid UCS-2 hex in %s\n", fn
);
860 ast_log(LOG_WARNING
, "Only ud can use ## format, %s\n", fn
);
861 } else if (!strcmp(line
, "ud")) { /* user data */
863 while (*p
&& o
< SMSLEN
) {
864 if (isxdigit(*p
) && isxdigit(p
[1])) {
865 h
->ud
[o
++] = (((isalpha(*p
) ? 9 : 0) + (*p
& 0xF)) << 4) + ((isalpha(p
[1]) ? 9 : 0) + (p
[1] & 0xF));
872 ast_log(LOG_WARNING
, "UD too long / invalid UCS-1 hex in %s\n", fn
);
873 } else if (!strcmp(line
, "udh")) { /* user data header */
876 while (*p
&& o
< SMSLEN
) {
877 if (isxdigit(*p
) && isxdigit(p
[1])) {
878 h
->udh
[o
] = (((isalpha(*p
) ? 9 : 0) + (*p
& 0xF)) << 4) + ((isalpha(p
[1]) ? 9 : 0) + (p
[1] & 0xF));
886 ast_log(LOG_WARNING
, "UDH too long / invalid hex in %s\n", fn
);
888 ast_log(LOG_WARNING
, "Only ud and udh can use # format, %s\n", fn
);
890 ast_log(LOG_WARNING
, "Cannot parse in %s: %s\n", fn
, line
);
893 if (!dcsset
&& packsms7(0, h
->udhl
, h
->udh
, h
->udl
, h
->ud
) < 0) {
894 if (packsms8(0, h
->udhl
, h
->udh
, h
->udl
, h
->ud
) < 0) {
895 if (packsms16(0, h
->udhl
, h
->udh
, h
->udl
, h
->ud
) < 0)
896 ast_log(LOG_WARNING
, "Invalid UTF-8 message even for UCS-2 (%s)\n", fn
);
898 h
->dcs
= 0x08; /* default to 16 bit */
899 ast_log(LOG_WARNING
, "Sending in 16 bit format(%s)\n", fn
);
902 h
->dcs
= 0xF5; /* default to 8 bit */
903 ast_log(LOG_WARNING
, "Sending in 8 bit format(%s)\n", fn
);
906 if (is7bit(h
->dcs
) && packsms7(0, h
->udhl
, h
->udh
, h
->udl
, h
->ud
) < 0)
907 ast_log(LOG_WARNING
, "Invalid 7 bit GSM data %s\n", fn
);
908 if (is8bit(h
->dcs
) && packsms8(0, h
->udhl
, h
->udh
, h
->udl
, h
->ud
) < 0)
909 ast_log(LOG_WARNING
, "Invalid 8 bit data %s\n", fn
);
910 if (is16bit(h
->dcs
) && packsms16(0, h
->udhl
, h
->udh
, h
->udl
, h
->ud
) < 0)
911 ast_log(LOG_WARNING
, "Invalid 16 bit data %s\n", fn
);
915 /*! \brief white a received text message to a file */
916 static void sms_writefile(sms_t
* h
)
918 char fn
[200] = "", fn2
[200] = "";
922 snprintf(fn
, sizeof(fn
), "%s/sms/%s", ast_config_AST_SPOOL_DIR
, h
->smsc
? h
->rx
? "morx" : "mttx" : h
->rx
? "mtrx" : "motx");
923 ast_mkdir(fn
, 0777); /* ensure it exists */
924 ast_copy_string(fn2
, fn
, sizeof(fn2
));
925 snprintf(fn2
+ strlen(fn2
), sizeof(fn2
) - strlen(fn2
), "/%s.%s-%d", h
->queue
, isodate(h
->scts
.tv_sec
, buf
, sizeof(buf
)), seq
++);
926 snprintf(fn
+ strlen(fn
), sizeof(fn
) - strlen(fn
), "/.%s", fn2
+ strlen(fn
) + 1);
932 fprintf(o
, "oa=%s\n", h
->oa
);
934 fprintf(o
, "da=%s\n", h
->da
);
938 for (p
= 0; p
< h
->udhl
; p
++)
939 fprintf(o
, "%02X", h
->udh
[p
]);
944 for (p
= 0; p
< h
->udl
&& h
->ud
[p
] >= ' '; p
++);
946 fputc(';', o
); /* cannot use ud=, but include as a comment for human readable */
948 for (p
= 0; p
< h
->udl
; p
++) {
949 unsigned short v
= h
->ud
[p
];
956 fputc(0xC0 + (v
>> 6), o
);
957 fputc(0x80 + (v
& 0x3F), o
);
960 fputc(0xE0 + (v
>> 12), o
);
961 fputc(0x80 + ((v
>> 6) & 0x3F), o
);
962 fputc(0x80 + (v
& 0x3F), o
);
966 for (p
= 0; p
< h
->udl
&& h
->ud
[p
] >= ' '; p
++);
968 for (p
= 0; p
< h
->udl
&& h
->ud
[p
] < 0x100; p
++);
969 if (p
== h
->udl
) { /* can write in ucs-1 hex */
971 for (p
= 0; p
< h
->udl
; p
++)
972 fprintf(o
, "%02X", h
->ud
[p
]);
974 } else { /* write in UCS-2 */
976 for (p
= 0; p
< h
->udl
; p
++)
977 fprintf(o
, "%04X", h
->ud
[p
]);
982 if (h
->scts
.tv_sec
) {
984 fprintf(o
, "scts=%s\n", isodate(h
->scts
.tv_sec
, buf
, sizeof(buf
)));
987 fprintf(o
, "pid=%d\n", h
->pid
);
989 fprintf(o
, "dcs=%d\n", h
->dcs
);
991 fprintf(o
, "vp=%d\n", h
->vp
);
993 fprintf(o
, "srr=1\n");
995 fprintf(o
, "mr=%d\n", h
->mr
);
997 fprintf(o
, "rp=1\n");
1002 ast_log(LOG_EVENT
, "Received to %s\n", fn2
);
1005 /*! \brief read dir skipping dot files... */
1006 static struct dirent
*readdirqueue(DIR *d
, char *queue
)
1011 } while (f
&& (*f
->d_name
== '.' || strncmp(f
->d_name
, queue
, strlen(queue
)) || f
->d_name
[strlen(queue
)] != '.'));
1015 /*! \brief handle the incoming message */
1016 static unsigned char sms_handleincoming (sms_t
* h
)
1018 unsigned char p
= 3;
1019 if (h
->smsc
) { /* SMSC */
1020 if ((h
->imsg
[2] & 3) == 1) { /* SMS-SUBMIT */
1021 h
->udhl
= h
->udl
= 0;
1023 h
->srr
= ((h
->imsg
[2] & 0x20) ? 1 : 0);
1024 h
->udhi
= ((h
->imsg
[2] & 0x40) ? 1 : 0);
1025 h
->rp
= ((h
->imsg
[2] & 0x80) ? 1 : 0);
1026 ast_copy_string(h
->oa
, h
->cli
, sizeof(h
->oa
));
1027 h
->scts
= ast_tvnow();
1028 h
->mr
= h
->imsg
[p
++];
1029 p
+= unpackaddress(h
->da
, h
->imsg
+ p
);
1030 h
->pid
= h
->imsg
[p
++];
1031 h
->dcs
= h
->imsg
[p
++];
1032 if ((h
->imsg
[2] & 0x18) == 0x10) { /* relative VP */
1033 if (h
->imsg
[p
] < 144)
1034 h
->vp
= (h
->imsg
[p
] + 1) * 5;
1035 else if (h
->imsg
[p
] < 168)
1036 h
->vp
= 720 + (h
->imsg
[p
] - 143) * 30;
1037 else if (h
->imsg
[p
] < 197)
1038 h
->vp
= (h
->imsg
[p
] - 166) * 1440;
1040 h
->vp
= (h
->imsg
[p
] - 192) * 10080;
1042 } else if (h
->imsg
[2] & 0x18)
1043 p
+= 7; /* ignore enhanced / absolute VP */
1044 p
+= unpacksms(h
->dcs
, h
->imsg
+ p
, h
->udh
, &h
->udhl
, h
->ud
, &h
->udl
, h
->udhi
);
1045 h
->rx
= 1; /* received message */
1046 sms_writefile(h
); /* write the file */
1047 if (p
!= h
->imsg
[1] + 2) {
1048 ast_log(LOG_WARNING
, "Mismatch receive unpacking %d/%d\n", p
, h
->imsg
[1] + 2);
1049 return 0xFF; /* duh! */
1052 ast_log(LOG_WARNING
, "Unknown message type %02X\n", h
->imsg
[2]);
1055 } else { /* client */
1056 if (!(h
->imsg
[2] & 3)) { /* SMS-DELIVER */
1057 *h
->da
= h
->srr
= h
->rp
= h
->vp
= h
->udhi
= h
->udhl
= h
->udl
= 0;
1058 h
->srr
= ((h
->imsg
[2] & 0x20) ? 1 : 0);
1059 h
->udhi
= ((h
->imsg
[2] & 0x40) ? 1 : 0);
1060 h
->rp
= ((h
->imsg
[2] & 0x80) ? 1 : 0);
1062 p
+= unpackaddress(h
->oa
, h
->imsg
+ p
);
1063 h
->pid
= h
->imsg
[p
++];
1064 h
->dcs
= h
->imsg
[p
++];
1065 h
->scts
= unpackdate(h
->imsg
+ p
);
1067 p
+= unpacksms(h
->dcs
, h
->imsg
+ p
, h
->udh
, &h
->udhl
, h
->ud
, &h
->udl
, h
->udhi
);
1068 h
->rx
= 1; /* received message */
1069 sms_writefile(h
); /* write the file */
1070 if (p
!= h
->imsg
[1] + 2) {
1071 ast_log(LOG_WARNING
, "Mismatch receive unpacking %d/%d\n", p
, h
->imsg
[1] + 2);
1072 return 0xFF; /* duh! */
1075 ast_log(LOG_WARNING
, "Unknown message type %02X\n", h
->imsg
[2]);
1079 return 0; /* no error */
1083 #define NAME_MAX 1024
1087 * Add data to a protocol 2 message.
1088 * Use the length field (h->omsg[1]) as a pointer to the next free position.
1090 static void adddata_proto2(sms_t
*h
, unsigned char msg
, char *data
, int size
)
1092 int x
= h
->omsg
[1]+2; /* Get current position */
1094 x
+= 2; /* First: skip Payload length (set later) */
1095 h
->omsg
[x
++] = msg
; /* Message code */
1096 h
->omsg
[x
++] = (unsigned char)size
; /* Data size Low */
1097 h
->omsg
[x
++] = 0; /* Data size Hi */
1098 for (; size
> 0 ; size
--)
1099 h
->omsg
[x
++] = *data
++;
1100 h
->omsg
[1] = x
- 2; /* Frame size */
1101 h
->omsg
[2] = x
- 4; /* Payload length (Lo) */
1102 h
->omsg
[3] = 0; /* Payload length (Hi) */
1105 static void putdummydata_proto2(sms_t
*h
)
1107 adddata_proto2(h
, 0x10, "\0", 1); /* Media Identifier > SMS */
1108 adddata_proto2(h
, 0x11, "\0\0\0\0\0\0", 6); /* Firmware version */
1109 adddata_proto2(h
, 0x12, "\2\0\4", 3); /* SMS provider ID */
1110 adddata_proto2(h
, 0x13, h
->udtxt
, h
->udl
); /* Body */
1113 static void sms_compose2(sms_t
*h
, int more
)
1116 struct timeval tv
= h
->scts
;
1119 h
->omsg
[0] = 0x00; /* set later... */
1121 putdummydata_proto2(h
);
1122 if (h
->smsc
) { /* deliver */
1123 h
->omsg
[0] = 0x11; /* SMS_DELIVERY */
1124 /* Required: 10 11 12 13 14 15 17 (seems they must be ordered!) */
1125 ast_localtime(&tv
, &tm
, NULL
);
1126 sprintf(stm
, "%02d%02d%02d%02d", tm
.tm_mon
+ 1, tm
.tm_mday
, tm
.tm_hour
, tm
.tm_min
); /* Date mmddHHMM */
1127 adddata_proto2(h
, 0x14, stm
, 8); /* Date */
1129 strcpy(h
->oa
, "00000000");
1130 adddata_proto2(h
, 0x15, h
->oa
, strlen(h
->oa
)); /* Originator */
1131 adddata_proto2(h
, 0x17, "\1", 1); /* Calling Terminal ID */
1132 } else { /* submit */
1133 h
->omsg
[0] = 0x10; /* SMS_SUBMIT */
1134 /* Required: 10 11 12 13 17 18 1B 1C (seems they must be ordered!) */
1135 adddata_proto2(h
, 0x17, "\1", 1); /* Calling Terminal ID */
1137 strcpy(h
->da
, "00000000");
1138 adddata_proto2(h
, 0x18, h
->da
, strlen(h
->da
)); /* Originator */
1139 adddata_proto2(h
, 0x1B, "\1", 1); /* Called Terminal ID */
1140 adddata_proto2(h
, 0x1C, "\0\0\0", 3); /* Notification */
1144 static void putdummydata_proto2(sms_t
*h
);
1146 #define MAX_DEBUG_LEN 300
1147 static char *sms_hexdump(unsigned char buf
[], int size
, char *s
/* destination */)
1152 for (p
= s
, f
= 0; f
< size
&& f
< MAX_DEBUG_LEN
; f
++, p
+= 3)
1153 sprintf(p
, "%02X ", (unsigned char)buf
[f
]);
1158 /*! \brief sms_handleincoming_proto2: handle the incoming message */
1159 static int sms_handleincoming_proto2(sms_t
*h
)
1164 struct timeval tv
= { 0, 0 };
1165 char debug_buf
[MAX_DEBUG_LEN
* 3 + 1];
1167 sz
= h
->imsg
[1] + 2;
1168 /* ast_verb(3, "SMS-P2 Frame: %s\n", sms_hexdump(h->imsg, sz, debug_buf)); */
1170 /* Parse message body (called payload) */
1171 tv
= h
->scts
= ast_tvnow();
1172 for (f
= 4; f
< sz
; ) {
1174 msgsz
= h
->imsg
[f
++];
1175 msgsz
+= (h
->imsg
[f
++] * 256);
1177 case 0x13: /* Body */
1178 ast_verb(3, "SMS-P2 Body#%02X=[%.*s]\n", msg
, msgsz
, &h
->imsg
[f
]);
1179 if (msgsz
>= sizeof(h
->imsg
))
1180 msgsz
= sizeof(h
->imsg
) - 1;
1181 for (i
= 0; i
< msgsz
; i
++)
1182 h
->ud
[i
] = h
->imsg
[f
+ i
];
1185 case 0x14: /* Date SCTS */
1186 tv
= h
->scts
= ast_tvnow();
1187 ast_localtime(&tv
, &tm
, NULL
);
1188 tm
.tm_mon
= ( (h
->imsg
[f
] * 10) + h
->imsg
[f
+ 1] ) - 1;
1189 tm
.tm_mday
= ( (h
->imsg
[f
+ 2] * 10) + h
->imsg
[f
+ 3] );
1190 tm
.tm_hour
= ( (h
->imsg
[f
+ 4] * 10) + h
->imsg
[f
+ 5] );
1191 tm
.tm_min
= ( (h
->imsg
[f
+ 6] * 10) + h
->imsg
[f
+ 7] );
1193 h
->scts
= ast_mktime(&tm
, NULL
);
1194 ast_verb(3, "SMS-P2 Date#%02X=%02d/%02d %02d:%02d\n", msg
, tm
.tm_mday
, tm
.tm_mon
+ 1, tm
.tm_hour
, tm
.tm_min
);
1196 case 0x15: /* Calling line (from SMSC) */
1199 ast_verb(3, "SMS-P2 Origin#%02X=[%.*s]\n", msg
, msgsz
, &h
->imsg
[f
]);
1200 ast_copy_string(h
->oa
, (char *)(&h
->imsg
[f
]), msgsz
+ 1);
1202 case 0x18: /* Destination(from TE/phone) */
1205 ast_verb(3, "SMS-P2 Destination#%02X=[%.*s]\n", msg
, msgsz
, &h
->imsg
[f
]);
1206 ast_copy_string(h
->da
, (char *)(&h
->imsg
[f
]), msgsz
+ 1);
1208 case 0x1C: /* Notify */
1209 ast_verb(3, "SMS-P2 Notify#%02X=%s\n", msg
, sms_hexdump(&h
->imsg
[f
], 3, debug_buf
));
1212 ast_verb(3, "SMS-P2 Par#%02X [%d]: %s\n", msg
, msgsz
, sms_hexdump(&h
->imsg
[f
], msgsz
, debug_buf
));
1215 f
+=msgsz
; /* Skip to next */
1217 h
->rx
= 1; /* received message */
1218 sms_writefile(h
); /* write the file */
1219 return 0; /* no error */
1223 static void smssend(sms_t
*h
, char *c
)
1226 for (f
= 0; f
< strlen(c
); f
++) {
1227 sscanf(&c
[f
*3], "%x", &x
);
1234 static void sms_nextoutgoing (sms_t
*h
);
1236 static void sms_messagerx2(sms_t
* h
)
1238 int p
= h
->imsg
[0] & DLL_SMS_MASK
; /* mask the high bit */
1241 #define DLL2_ACK(h) ((h->framenumber & 1) ? DLL2_SMS_ACK1: DLL2_SMS_ACK1)
1243 case DLL2_SMS_EST
: /* Protocol 2: Connection ready (fake): send message */
1244 sms_nextoutgoing (h
);
1245 /* smssend(h,"11 29 27 00 10 01 00 00 11 06 00 00 00 00 00 00 00 12 03 00 02 00 04 13 01 00 41 14 08 00 30 39 31 35 30 02 30 02 15 02 00 39 30 "); */
1248 case DLL2_SMS_INFO_MO
: /* transport SMS_SUBMIT */
1249 case DLL2_SMS_INFO_MT
: /* transport SMS_DELIVERY */
1250 cause
= sms_handleincoming_proto2(h
);
1251 if (!cause
) /* ACK */
1253 h
->omsg
[0] = DLL2_ACK(h
);
1254 h
->omsg
[1] = 0x06; /* msg len */
1255 h
->omsg
[2] = 0x04; /* payload len */
1256 h
->omsg
[3] = 0x00; /* payload len */
1257 h
->omsg
[4] = 0x1f; /* Response type */
1258 h
->omsg
[5] = 0x01; /* parameter len */
1259 h
->omsg
[6] = 0x00; /* parameter len */
1260 h
->omsg
[7] = cause
; /* CONFIRM or error */
1264 case DLL2_SMS_NACK
: /* Protocol 2: SMS_NAK */
1265 h
->omsg
[0] = DLL2_SMS_REL
; /* SMS_REL */
1266 h
->omsg
[1] = 0x00; /* msg len */
1272 /* SMS_ACK also transport SMS_SUBMIT or SMS_DELIVERY */
1273 if ( (h
->omsg
[0] & DLL_SMS_MASK
) == DLL2_SMS_REL
) {
1274 /* a response to our Release, just hangup */
1275 h
->hangup
= 1; /* hangup */
1277 /* XXX depending on what we are.. */
1278 ast_log(LOG_NOTICE
, "SMS_SUBMIT or SMS_DELIVERY");
1279 sms_nextoutgoing (h
);
1283 case DLL2_SMS_REL
: /* Protocol 2: SMS_REL (hangup req) */
1284 h
->omsg
[0] = DLL2_ACK(h
);
1291 /*! \brief compose a message for protocol 1 */
1292 static void sms_compose1(sms_t
*h
, int more
)
1294 unsigned int p
= 2; /* next byte to write. Skip type and len */
1296 h
->omsg
[0] = 0x91; /* SMS_DATA */
1297 if (h
->smsc
) { /* deliver */
1298 h
->omsg
[p
++] = (more
? 4 : 0) + ((h
->udhl
> 0) ? 0x40 : 0);
1299 p
+= packaddress(h
->omsg
+ p
, h
->oa
);
1300 h
->omsg
[p
++] = h
->pid
;
1301 h
->omsg
[p
++] = h
->dcs
;
1302 packdate(h
->omsg
+ p
, h
->scts
.tv_sec
);
1304 p
+= packsms(h
->dcs
, h
->omsg
+ p
, h
->udhl
, h
->udh
, h
->udl
, h
->ud
);
1305 } else { /* submit */
1307 0x01 + (more
? 4 : 0) + (h
->srr
? 0x20 : 0) + (h
->rp
? 0x80 : 0) + (h
->vp
? 0x10 : 0) + (h
->udhi
? 0x40 : 0);
1309 h
->mr
= message_ref
++;
1310 h
->omsg
[p
++] = h
->mr
;
1311 p
+= packaddress(h
->omsg
+ p
, h
->da
);
1312 h
->omsg
[p
++] = h
->pid
;
1313 h
->omsg
[p
++] = h
->dcs
;
1314 if (h
->vp
) { /* relative VP */
1316 h
->omsg
[p
++] = (h
->vp
+ 4) / 5 - 1;
1317 else if (h
->vp
< 1440)
1318 h
->omsg
[p
++] = (h
->vp
- 720 + 29) / 30 + 143;
1319 else if (h
->vp
< 43200)
1320 h
->omsg
[p
++] = (h
->vp
+ 1439) / 1440 + 166;
1321 else if (h
->vp
< 635040)
1322 h
->omsg
[p
++] = (h
->vp
+ 10079) / 10080 + 192;
1324 h
->omsg
[p
++] = 255; /* max */
1326 p
+= packsms(h
->dcs
, h
->omsg
+ p
, h
->udhl
, h
->udh
, h
->udl
, h
->ud
);
1331 /*! \brief find and fill in next message, or send a REL if none waiting */
1332 static void sms_nextoutgoing (sms_t
* h
)
1334 char fn
[100 + NAME_MAX
] = "";
1338 *h
->da
= *h
->oa
= '\0'; /* clear destinations */
1339 h
->rx
= 0; /* outgoing message */
1340 snprintf(fn
, sizeof(fn
), "%s/sms/%s", ast_config_AST_SPOOL_DIR
, h
->smsc
? "mttx" : "motx");
1341 ast_mkdir(fn
, 0777); /* ensure it exists */
1344 struct dirent
*f
= readdirqueue(d
, h
->queue
);
1346 snprintf(fn
+ strlen(fn
), sizeof(fn
) - strlen(fn
), "/%s", f
->d_name
);
1347 sms_readfile(h
, fn
);
1348 if (readdirqueue(d
, h
->queue
))
1349 more
= 1; /* more to send */
1353 if (*h
->da
|| *h
->oa
) { /* message to send */
1354 if (h
->protocol
== 2)
1355 sms_compose2(h
, more
);
1357 sms_compose1(h
, more
);
1358 } else { /* no message */
1359 if (h
->protocol
== 2) {
1360 h
->omsg
[0] = 0x17; /* SMS_REL */
1363 h
->omsg
[0] = 0x94; /* SMS_REL */
1372 static void sms_debug (int dir
, sms_t
*h
)
1374 char txt
[259 * 3 + 1];
1375 char *p
= txt
; /* always long enough */
1376 unsigned char *msg
= (dir
== DIR_RX
) ? h
->imsg
: h
->omsg
;
1377 int n
= (dir
== DIR_RX
) ? h
->ibytep
: msg
[1] + 2;
1379 while (q
< n
&& q
< 30) {
1380 sprintf(p
, " %02X", msg
[q
++]);
1385 ast_verb(3, "SMS %s%s\n", dir
== DIR_RX
? "RX" : "TX", txt
);
1389 static void sms_messagerx(sms_t
* h
)
1393 sms_debug (DIR_RX
, h
);
1394 if (h
->protocol
== 2) {
1398 /* parse incoming message for Protocol 1 */
1399 switch (h
->imsg
[0]) {
1400 case 0x91: /* SMS_DATA */
1401 cause
= sms_handleincoming (h
);
1404 h
->omsg
[0] = 0x95; /* SMS_ACK */
1406 h
->omsg
[2] = 0x00; /* deliver report */
1407 h
->omsg
[3] = 0x00; /* no parameters */
1410 h
->omsg
[0] = 0x96; /* SMS_NACK */
1412 h
->omsg
[2] = 0; /* delivery report */
1413 h
->omsg
[3] = cause
; /* cause */
1414 h
->omsg
[4] = 0; /* no parameters */
1419 case 0x92: /* SMS_ERROR */
1421 sms_messagetx(h
); /* send whatever we sent again */
1423 case 0x93: /* SMS_EST */
1424 sms_nextoutgoing (h
);
1426 case 0x94: /* SMS_REL */
1427 h
->hangup
= 1; /* hangup */
1429 case 0x95: /* SMS_ACK */
1431 sms_nextoutgoing (h
);
1433 case 0x96: /* SMS_NACK */
1436 sms_nextoutgoing (h
);
1438 default: /* Unknown */
1439 h
->omsg
[0] = 0x92; /* SMS_ERROR */
1441 h
->omsg
[2] = 3; /* unknown message type; */
1447 static void sms_messagetx(sms_t
* h
)
1449 unsigned char c
= 0, p
;
1450 int len
= h
->omsg
[1] + 2; /* total message length excluding checksum */
1452 for (p
= 0; p
< len
; p
++) /* compute checksum */
1454 h
->omsg
[len
] = 0 - c
; /* actually, (256 - (c & 0fxx)) & 0xff) */
1455 sms_debug(DIR_TX
, h
);
1456 h
->framenumber
++; /* Proto 2 */
1457 h
->obyte
= 1; /* send mark ('1') at the beginning */
1459 /* Change the initial message delay. BT requires 300ms,
1460 * but for others this might be way too much and the phone
1461 * could time out. XXX make it configurable.
1463 if (h
->omsg
[0] == 0x93)
1464 h
->opause
= 8 * h
->opause_0
; /* initial message delay */
1467 if (h
->protocol
== 2) {
1468 h
->oseizure
= 300; /* Proto 2: 300bits (or more ?) */
1469 h
->obyte
= 0; /* Seizure starts with space (0) */
1472 h
->oseizure
= 0; /* Proto 1: No seizure */
1474 /* Note - setting osync triggers the generator */
1475 h
->osync
= OSYNC_BITS
; /* 80 sync bits */
1476 h
->obyten
= len
+ 1; /* bytes to send (including checksum) */
1480 * outgoing data are produced by this generator function, that reads from
1481 * the descriptor whether it has data to send and which ones.
1483 static int sms_generate(struct ast_channel
*chan
, void *data
, int len
, int samples
)
1485 struct ast_frame f
= { 0 };
1486 #define MAXSAMPLES (800)
1491 if (samples
> MAXSAMPLES
) {
1492 ast_log(LOG_WARNING
, "Only doing %d samples (%d requested)\n",
1493 MAXSAMPLES
, samples
);
1494 samples
= MAXSAMPLES
;
1496 len
= samples
* sizeof(*buf
) + AST_FRIENDLY_OFFSET
;
1499 f
.frametype
= AST_FRAME_VOICE
;
1500 f
.subclass
= __OUT_FMT
;
1501 f
.datalen
= samples
* sizeof(*buf
);
1502 f
.offset
= AST_FRIENDLY_OFFSET
;
1505 f
.samples
= samples
;
1507 /* create a buffer containing the digital sms pattern */
1508 for (i
= 0; i
< samples
; i
++) {
1509 buf
[i
] = wave_out
[0]; /* default is silence */
1513 else if (h
->obyten
|| h
->osync
) { /* sending data */
1514 buf
[i
] = wave_out
[h
->ophase
];
1515 h
->ophase
+= (h
->obyte
& 1) ? 13 : 21; /* compute next phase */
1516 if (h
->ophase
>= 80)
1518 if ((h
->ophasep
+= 12) >= 80) { /* time to send the next bit */
1520 if (h
->oseizure
> 0) { /* sending channel seizure (proto 2) */
1522 h
->obyte
^= 1; /* toggle low bit */
1523 } else if (h
->osync
) {
1524 h
->obyte
= 1; /* send mark as sync bit */
1525 h
->osync
--; /* sending sync bits */
1526 if (h
->osync
== 0 && h
->protocol
== 2 && h
->omsg
[0] == DLL2_SMS_EST
) {
1527 h
->obytep
= h
->obyten
= 0; /* we are done */
1532 h
->obyte
= 0; /* start bit; */
1533 else if (h
->obitp
== 2)
1534 h
->obyte
= h
->omsg
[h
->obytep
];
1535 else if (h
->obitp
== 10) {
1536 h
->obyte
= 1; /* stop bit */
1539 if (h
->obytep
== h
->obyten
) {
1540 h
->obytep
= h
->obyten
= 0; /* sent */
1541 h
->osync
= 10; /* trailing marks */
1549 if (ast_write(chan
, &f
) < 0) {
1550 ast_log(LOG_WARNING
, "Failed to write frame to '%s': %s\n", chan
->name
, strerror(errno
));
1558 * Just return the pointer to the descriptor that we received.
1560 static void *sms_alloc(struct ast_channel
*chan
, void *sms_t_ptr
)
1565 static void sms_release(struct ast_channel
*chan
, void *data
)
1567 return; /* nothing to do here. */
1570 static struct ast_generator smsgen
= {
1572 .release
= sms_release
,
1573 .generate
= sms_generate
,
1577 * Process an incoming frame, trying to detect the carrier and
1578 * decode the message. The two frequencies are 1300 and 2100 Hz.
1579 * The decoder detects the amplitude of the signal over the last
1580 * few samples, filtering the absolute values with a lowpass filter.
1581 * If the magnitude (h->imag) is large enough, multiply the signal
1582 * by the two carriers, and compute the amplitudes m0 and m1.
1583 * Record the current sample as '0' or '1' depending on which one is greater.
1584 * The last 3 bits are stored in h->ibith, with the count of '1'
1586 * XXX the rest is to be determined.
1588 static void sms_process(sms_t
* h
, int samples
, signed short *data
)
1593 * Ignore incoming audio while a packet is being transmitted,
1594 * the protocol is half-duplex.
1595 * Unfortunately this means that if the outbound and incoming
1596 * transmission overlap (which is an error condition anyways),
1597 * we may miss some data and this makes debugging harder.
1599 if (h
->obyten
|| h
->osync
)
1601 for ( ; samples
-- ; data
++) {
1602 unsigned long long m0
, m1
;
1603 if (abs(*data
) > h
->imag
)
1604 h
->imag
= abs(*data
);
1606 h
->imag
= h
->imag
* 7 / 8;
1607 if (h
->imag
<= 500) { /* below [arbitrary] threahold: lost carrier */
1608 if (h
->idle
++ == 80000) { /* nothing happening */
1609 ast_log(LOG_NOTICE
, "No data, hanging up\n");
1613 if (h
->ierr
) { /* error */
1614 ast_log(LOG_NOTICE
, "Error %d, hanging up\n", h
->ierr
);
1617 h
->omsg
[0] = 0x92; /* error */
1619 h
->omsg
[2] = h
->ierr
;
1620 sms_messagetx(h
); /* send error */
1622 h
->ierr
= h
->ibitn
= h
->ibytep
= h
->ibytec
= 0;
1627 /* multiply signal by the two carriers. */
1628 h
->ims0
= (h
->ims0
* 6 + *data
* wave
[h
->ips0
]) / 7;
1629 h
->imc0
= (h
->imc0
* 6 + *data
* wave
[h
->ipc0
]) / 7;
1630 h
->ims1
= (h
->ims1
* 6 + *data
* wave
[h
->ips1
]) / 7;
1631 h
->imc1
= (h
->imc1
* 6 + *data
* wave
[h
->ipc1
]) / 7;
1632 /* compute the amplitudes */
1633 m0
= h
->ims0
* h
->ims0
+ h
->imc0
* h
->imc0
;
1634 m1
= h
->ims1
* h
->ims1
+ h
->imc1
* h
->imc1
;
1636 /* advance the sin/cos pointers */
1637 if ((h
->ips0
+= 21) >= 80)
1639 if ((h
->ipc0
+= 21) >= 80)
1641 if ((h
->ips1
+= 13) >= 80)
1643 if ((h
->ipc1
+= 13) >= 80)
1646 /* set new bit to 1 or 0 depending on which value is stronger */
1654 bit
= ((h
->ibitt
> 1) ? 1 : 0);
1655 if (bit
!= h
->ibitl
)
1660 if (!h
->ibitn
&& h
->ibitc
== 4 && !bit
) {
1664 if (bit
&& h
->ibitc
== 200) { /* sync, restart message */
1665 /* Protocol 2: empty connnection ready (I am master) */
1666 if (h
->framenumber
< 0 && h
->ibytec
>= 160 && !memcmp(h
->imsg
, "UUUUUUUUUUUUUUUUUUUU", 20)) {
1668 ast_verb(3, "SMS protocol 2 detected\n");
1670 h
->imsg
[0] = 0xff; /* special message (fake) */
1671 h
->imsg
[1] = h
->imsg
[2] = 0x00;
1672 h
->ierr
= h
->ibitn
= h
->ibytep
= h
->ibytec
= 0;
1675 h
->ierr
= h
->ibitn
= h
->ibytep
= h
->ibytec
= 0;
1679 if (h
->iphasep
>= 80) { /* next bit */
1681 if (h
->ibitn
++ == 9) { /* end of byte */
1682 if (!bit
) { /* bad stop bit */
1683 ast_log(LOG_NOTICE
, "bad stop bit");
1684 h
->ierr
= 0xFF; /* unknown error */
1686 if (h
->ibytep
< sizeof(h
->imsg
)) {
1687 h
->imsg
[h
->ibytep
] = h
->ibytev
;
1688 h
->ibytec
+= h
->ibytev
;
1690 } else if (h
->ibytep
== sizeof(h
->imsg
)) {
1691 ast_log(LOG_NOTICE
, "msg too large");
1692 h
->ierr
= 2; /* bad message length */
1694 if (h
->ibytep
> 1 && h
->ibytep
== 3 + h
->imsg
[1] && !h
->ierr
) {
1698 ast_log(LOG_NOTICE
, "bad checksum");
1699 h
->ierr
= 1; /* bad checksum */
1705 h
->ibytev
= (h
->ibytev
>> 1) + (bit
? 0x80 : 0);
1712 * Standard argument parsing:
1713 * - one enum for the flags we recognise,
1714 * - one enum for argument indexes
1715 * - AST_APP_OPTIONS() to drive the parsing routine
1716 * - in the function, AST_DECLARE_APP_ARGS(...) for the arguments.
1719 OPTION_BE_SMSC
= (1 << 0), /* act as sms center */
1720 OPTION_ANSWER
= (1 << 1), /* answer on incoming calls */
1721 OPTION_TWO
= (1 << 2), /* Use Protocol Two */
1722 OPTION_PAUSE
= (1 << 3), /* pause before sending data, in ms */
1723 OPTION_SRR
= (1 << 4), /* set srr */
1724 OPTION_DCS
= (1 << 5), /* set dcs */
1728 OPTION_ARG_PAUSE
= 0,
1729 OPTION_ARG_ARRAY_SIZE
1732 AST_APP_OPTIONS(sms_options
, {
1733 AST_APP_OPTION('s', OPTION_BE_SMSC
),
1734 AST_APP_OPTION('a', OPTION_ANSWER
),
1735 AST_APP_OPTION('t', OPTION_TWO
),
1736 AST_APP_OPTION('r', OPTION_SRR
),
1737 AST_APP_OPTION('o', OPTION_DCS
),
1738 AST_APP_OPTION_ARG('p', OPTION_PAUSE
, OPTION_ARG_PAUSE
),
1741 static int sms_exec(struct ast_channel
*chan
, void *data
)
1745 /* argument parsing support */
1746 struct ast_flags sms_flags
;
1747 char *parse
, *sms_opts
[OPTION_ARG_ARRAY_SIZE
];
1749 AST_DECLARE_APP_ARGS(sms_args
,
1751 AST_APP_ARG(options
);
1757 ast_log(LOG_ERROR
, "Requires queue name at least\n");
1761 parse
= ast_strdupa(data
); /* create a local copy */
1762 AST_STANDARD_APP_ARGS(sms_args
, parse
);
1763 if (sms_args
.argc
> 1)
1764 ast_app_parse_options(sms_options
, &sms_flags
, sms_opts
, sms_args
.options
);
1766 ast_verb(1, "sms argc %d queue <%s> opts <%s> addr <%s> body <%s>\n",
1767 sms_args
.argc
, S_OR(sms_args
.queue
, ""),
1768 S_OR(sms_args
.options
, ""),
1769 S_OR(sms_args
.addr
, ""),
1770 S_OR(sms_args
.body
, "") );
1772 h
.ipc0
= h
.ipc1
= 20; /* phase for cosine */
1773 h
.dcs
= 0xF1; /* default */
1775 if (chan
->cid
.cid_num
)
1776 ast_copy_string(h
.cli
, chan
->cid
.cid_num
, sizeof(h
.cli
));
1778 if (ast_strlen_zero(sms_args
.queue
)) {
1779 ast_log(LOG_ERROR
, "Requires queue name\n");
1782 if (strlen(sms_args
.queue
) >= sizeof(h
.queue
)) {
1783 ast_log(LOG_ERROR
, "Queue name too long\n");
1786 ast_copy_string(h
.queue
, sms_args
.queue
, sizeof(h
.queue
));
1788 for (p
= h
.queue
; *p
; p
++)
1790 *p
= '-'; /* make very safe for filenames */
1792 h
.smsc
= ast_test_flag(&sms_flags
, OPTION_BE_SMSC
);
1793 h
.protocol
= ast_test_flag(&sms_flags
, OPTION_TWO
) ? 2 : 1;
1794 if (!ast_strlen_zero(sms_opts
[OPTION_ARG_PAUSE
]))
1795 h
.opause_0
= atoi(sms_opts
[OPTION_ARG_PAUSE
]);
1796 if (h
.opause_0
< 25 || h
.opause_0
> 2000)
1797 h
.opause_0
= 300; /* default 300ms */
1798 ast_verb(1, "initial delay %dms\n", h
.opause_0
);
1801 /* the following apply if there is an arg3/4 and apply to the created message file */
1802 if (ast_test_flag(&sms_flags
, OPTION_SRR
))
1804 if (ast_test_flag(&sms_flags
, OPTION_DCS
))
1813 case '7': /* set the pid for saved local message */
1814 h
.pid
= 0x40 + (*d
& 0xF);
1818 if (sms_args
.argc
> 2) {
1821 /* submitting a message, not taking call. */
1822 /* deprecated, use smsq instead */
1823 h
.scts
= ast_tvnow();
1824 if (ast_strlen_zero(sms_args
.addr
) || strlen(sms_args
.addr
) >= sizeof(h
.oa
)) {
1825 ast_log(LOG_ERROR
, "Address too long %s\n", sms_args
.addr
);
1829 ast_copy_string(h
.oa
, sms_args
.addr
, sizeof(h
.oa
));
1831 ast_copy_string(h
.da
, sms_args
.addr
, sizeof(h
.da
));
1832 ast_copy_string(h
.oa
, h
.cli
, sizeof(h
.oa
));
1835 if (ast_strlen_zero(sms_args
.body
)) {
1836 ast_log(LOG_ERROR
, "Missing body for %s\n", sms_args
.addr
);
1839 up
= (unsigned char *)sms_args
.body
;
1840 while (*up
&& h
.udl
< SMSLEN
)
1841 h
.ud
[h
.udl
++] = utf8decode(&up
);
1842 if (is7bit(h
.dcs
) && packsms7(0, h
.udhl
, h
.udh
, h
.udl
, h
.ud
) < 0) {
1843 ast_log(LOG_WARNING
, "Invalid 7 bit GSM data\n");
1846 if (is8bit(h
.dcs
) && packsms8(0, h
.udhl
, h
.udh
, h
.udl
, h
.ud
) < 0) {
1847 ast_log(LOG_WARNING
, "Invalid 8 bit data\n");
1850 if (is16bit(h
.dcs
) && packsms16(0, h
.udhl
, h
.udh
, h
.udl
, h
.ud
) < 0) {
1851 ast_log(LOG_WARNING
, "Invalid 16 bit data\n");
1854 h
.rx
= 0; /* sent message */
1861 if (ast_test_flag(&sms_flags
, OPTION_ANSWER
)) {
1862 h
.framenumber
= 1; /* Proto 2 */
1863 /* set up SMS_EST initial message */
1864 if (h
.protocol
== 2) {
1865 h
.omsg
[0] = DLL2_SMS_EST
;
1868 h
.omsg
[0] = DLL1_SMS_EST
| DLL1_SMS_COMPLETE
;
1874 if (chan
->_state
!= AST_STATE_UP
)
1877 res
= ast_set_write_format(chan
, __OUT_FMT
);
1879 res
= ast_set_read_format(chan
, AST_FORMAT_SLINEAR
);
1881 ast_log(LOG_ERROR
, "Unable to set to linear mode, giving up\n");
1885 if ( (res
= ast_activate_generator(chan
, &smsgen
, &h
)) < 0) {
1886 ast_log(LOG_ERROR
, "Failed to activate generator on '%s'\n", chan
->name
);
1890 /* Do our thing here */
1892 struct ast_frame
*f
;
1893 int i
= ast_waitfor(chan
, -1);
1895 ast_log(LOG_NOTICE
, "waitfor failed\n");
1899 ast_log(LOG_NOTICE
, "channel hangup\n");
1904 ast_log(LOG_NOTICE
, "ast_read failed\n");
1907 if (f
->frametype
== AST_FRAME_VOICE
) {
1908 sms_process(&h
, f
->samples
, f
->data
);
1913 res
= h
.err
; /* XXX */
1915 sms_log(&h
, '?'); /* log incomplete message */
1920 static int unload_module(void)
1922 return ast_unregister_application(app
);
1925 static int load_module(void)
1929 for (p
= 0; p
< 80; p
++)
1930 wavea
[p
] = AST_LIN2A (wave
[p
]);
1932 snprintf(log_file
, sizeof(log_file
), "%s/sms", ast_config_AST_LOG_DIR
);
1933 return ast_register_application(app
, sms_exec
, synopsis
, descrip
);
1936 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "SMS/PSTN handler");