Skip to content
Snippets Groups Projects
app_sms.c 57 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * 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.
     */
    
    
     * \brief SMS application - ETSI ES 201 912 protocol 1 implementation
     * 
     * \par Development notes
     * \note The ETSI standards are available free of charge from ETSI at
     *	http://pda.etsi.org/pda/queryform.asp
     * 	Among the relevant documents here we have:
     *
     *	ES 201 912	SMS for PSTN/ISDN
     *	TS 123 040	Technical realization of SMS
     *
     * 
    
    Russell Bryant's avatar
    Russell Bryant committed
     * \ingroup applications
    
     * \author Adrian Kennard (for the original protocol 1 code)
    
    Olle Johansson's avatar
    Olle Johansson committed
     * \author Filippo Grassilli (Hyppo) - protocol 2 support
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
     *		   Not fully tested, under development
    
    #include "asterisk.h"
    
    ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
    
    
    #include <dirent.h>
    #include <ctype.h>
    
    #include <sys/stat.h>
    
    #include "asterisk/paths.h"	/* use ast_config_AST_SPOOL_DIR and LOG_DIR */
    
    #include "asterisk/lock.h"
    #include "asterisk/file.h"
    #include "asterisk/channel.h"
    #include "asterisk/pbx.h"
    #include "asterisk/module.h"
    #include "asterisk/alaw.h"
    #include "asterisk/callerid.h"
    
    #include "asterisk/utils.h"
    #include "asterisk/app.h"
    
    /* #define OUTALAW */ /* enable this to output Alaw rather than linear */
    
    /* 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 *app = "SMS";
    
    static char *synopsis = "Communicates with SMS service centres and SMS capable analogue phones";
    
    static char *descrip =
    
    	"  SMS(name,[a][s][t][p(d)][r][o],addr,body):\n"
    
    	"SMS handles exchange of SMS data with a call to/from SMS capable\n"
    
    Kevin P. Fleming's avatar
    Kevin P. Fleming committed
    	"phone or SMS PSTN service center. Can send and/or receive SMS messages.\n"
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	"Works to ETSI ES 201 912; compatible with BT SMS PSTN service in UK\n"
    
    	"and Telecom Italia in Italy.\n"
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	"Typical usage is to use to handle calls 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"
    	" t  - use protocol 2 (default used is protocol 1).\n"
    	" p(N)  - set the initial delay to N ms (default is 300).\n"
    	"         addr and body are a deprecated format to send messages out.\n"
    	" s  - set the Status Report Request (SRR) bit.\n"
    	" o  - the body should be coded as octets not 7-bit symbols.\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"
    	"NOTE: the protocol has tight delay bounds. Please use short frames\n"
    	"and disable/keep short the jitter buffer on the ATA to make sure that\n"
    	"respones (ACK etc.) are received in time.\n";
    
    /*
     * 80 samples of a single period of the wave. At 8000 Hz, it means these
     * are the samples of a 100 Hz signal.
     * To pick the two carriers (1300Hz for '1' and 2100 Hz for '0') used by
     * the modulation, we should take one every 13 and 21 samples respectively.
     */
    
    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];
    
    typedef unsigned char output_t;
    static const output_t *wave_out = wavea;	/* outgoing samples */
    #define __OUT_FMT AST_FORMAT_ALAW;
    #else
    typedef signed short output_t;
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    static const output_t *wave_out = wave;		/* outgoing samples */
    
    #define __OUT_FMT AST_FORMAT_SLINEAR
    
    #define OSYNC_BITS	80	/* initial sync bits */
    
    /*!
     * The SMS spec ETSI ES 201 912 defines two protocols with different message types.
     * Also note that the high bit is used to indicate whether the message
     * is complete or not, but in two opposite ways:
     * for Protocol 1, 0x80 means that the message is complete;
     * for Protocol 2, 0x00 means that the message is complete;
     */
    enum message_types {
    	DLL_SMS_MASK	= 0x7f,	/* mask for the valid bits */
    
    	/* Protocol 1 values */
    	DLL1_SMS_DATA		= 0x11,	/* data packet */
    	DLL1_SMS_ERROR		= 0x12,
    	DLL1_SMS_EST		= 0x13,	/* start the connection */
    	DLL1_SMS_REL		= 0x14,	/* end the connection */
    	DLL1_SMS_ACK		= 0x15,
    	DLL1_SMS_NACK		= 0x16,
    
    	DLL1_SMS_COMPLETE 	= 0x80,	/* packet is complete */
    	DLL1_SMS_MORE 		= 0x00,	/* more data to follow */
    
    	/* Protocol 2 values */
    	DLL2_SMS_EST		= 0x7f,	/* magic number. No message body */
    	DLL2_SMS_INFO_MO 	= 0x10,
    	DLL2_SMS_INFO_MT 	= 0x11,
    	DLL2_SMS_INFO_STA 	= 0x12,
    	DLL2_SMS_NACK 		= 0x13,
    	DLL2_SMS_ACK0 		= 0x14,	/* ack even-numbered frame */
    	DLL2_SMS_ACK1 		= 0x15,	/* ack odd-numbered frame */
    	DLL2_SMS_ENQ 		= 0x16,
    	DLL2_SMS_REL 		= 0x17,	/* end the connection */
    
    	DLL2_SMS_COMPLETE 	= 0x00,	/* packet is complete */
    	DLL2_SMS_MORE 		= 0x80,	/* more data to follow */
    };
    
    /* 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,
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    #define SMSLEN		160	      /*!< max SMS length */
    #define SMSLEN_8	140	      /*!< max SMS length for 8-bit char */
    
    typedef struct sms_s {
    
    Olle Johansson's avatar
    Olle Johansson committed
    	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 */
    
    	struct timeval scts;         /*!< time stamp, UTC */
    
    Olle Johansson's avatar
    Olle Johansson committed
    	unsigned char pid;           /*!< protocol ID */
    	unsigned char dcs;           /*!< data coding scheme */
    
    	short mr;                    /*!< message reference - actually a byte, but use -1 for not set */
    
    Olle Johansson's avatar
    Olle Johansson committed
    	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[250];     /*!< data buffer (in) */
    
    	signed long long ims0,
    		imc0,
    		ims1,
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		imc1;                    /*!< magnitude averages sin/cos 0/1 */
    
    	unsigned int idle;
    
    Olle Johansson's avatar
    Olle Johansson committed
    	unsigned short imag;         /*!< signal level */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	unsigned char ips0;          /*!< phase sin for bit 0, start at  0 inc by 21 mod 80 */
    	unsigned char ips1;          /*!< phase cos for bit 0, start at 20 inc by 21 mod 80 */
    	unsigned char ipc0;          /*!< phase sin for bit 1, start at  0 inc by 13 mod 80 */
    	unsigned char ipc1;          /*!< phase cos for bit 1, start at 20 inc by 13 mod 80 */
    
    Olle Johansson's avatar
    Olle Johansson committed
    	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 message */
    
    Olle Johansson's avatar
    Olle Johansson committed
    	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 bytes */
    
    	/* more to go here */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	int opause_0;                /*!< initial delay in ms, p() option */
    
    	int protocol;                /*!< ETSI SMS protocol to use (passed at app call) */
    	int oseizure;                /*!< protocol 2: channel seizure bits to send */
    	int framenumber;             /*!< protocol 2: frame number (for sending ACK0 or ACK1) */
    
    	char udtxt[SMSLEN]; /*!< user data (message), PLAIN text */
    
    /* different types of encoding */
    
    #define is7bit(dcs)	( ((dcs) & 0xC0) ? (!((dcs)&4) ) : (((dcs) & 0xc) == 0) )
    #define is8bit(dcs)	( ((dcs) & 0xC0) ? ( ((dcs)&4) ) : (((dcs) & 0xc) == 4) )
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    #define is16bit(dcs)	( ((dcs) & 0xC0) ? 0	     : (((dcs) & 0xc) == 8) )
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    static void sms_messagetx(sms_t *h);
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief copy number, skipping non digits apart from leading + */
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    static void numcpy(char *d, char *s)
    
    	if (*s == '+')
    		*d++ = *s++;
    	while (*s) {
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
      		if (isdigit(*s))
    
         			*d++ = *s;
    		s++;
    	}
    	*d = 0;
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief static, return a date/time in ISO format */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    static char *isodate(time_t t, char *buf, int len)
    
    	struct ast_tm tm;
    	struct timeval tv = { t, 0 };
    	ast_localtime(&tv, &tm, NULL);
    	ast_strftime(buf, len, "%Y-%m-%dT%H:%M:%S", &tm);
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    	return buf;
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    /*! \brief Reads next UCS character from NUL terminated UTF-8 string and advance 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)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		return 0;		 /* null termination of string */
    
    	(*pp)++;
    	if (*p < 0xC0)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		return *p;		/* ascii or continuation character */
    
    	if (*p < 0xE0) {
    		if (*p < 0xC2 || (p[1] & 0xC0) != 0x80)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    			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)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    			 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)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    			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)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    			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)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    			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);
    	}
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	return *p;		   /* not sensible */
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \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 */
    
    /* 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;	/* output pointer (bytes) */
    	unsigned char b = 0;	/* bit position */
    	unsigned char n = 0;	/* output character count */
    	unsigned char dummy[SMSLEN];
    
    	if (o == NULL)		/* output to a dummy buffer if o not set */
    		o = dummy;
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	if (udhl) {			    /* header */
    
    		o[p++] = udhl;
    
    		b = 1;
    		n = 1;
    		while (udhl--) {
    
    			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 */
    	}
    	o[p] = 0;
    	/* message */
    	while (udl--) {
    		long u;
    		unsigned char v;
    		u = *ud++;
    		/* XXX 0 is invalid ? */
    		/* look up in defaultalphabet[]. If found, v is the 7-bit code */
    		for (v = 0; v < 128 && defaultalphabet[v] != u; v++);
    		if (v == 128 /* not found */ && u && n + 1 < SMSLEN) {
    			/* if not found, look in the escapes table (we need 2 bytes) */
    			for (v = 0; v < 128 && escapes[v] != u; v++);
    			if (v < 128) {	/* escaped sequence, esc + v */
    				/* store the low (8-b) bits in o[p], the remaining bits in o[p+1] */
    				o[p] |= (27 << b);	/* the low bits go into o[p] */ 
    
    				b += 7;
    				if (b >= 8) {
    					b -= 8;
    					p++;
    
    					o[p] = (27 >> (7 - b));
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    			return -1;	     /* invalid character */
    
    		/* store, same as above */
    		o[p] |= (v << b);
    
    		b += 7;
    		if (b >= 8) {
    			b -= 8;
    			p++;
    
    			o[p] = (v >> (7 - b));
    
    		}
    		if (++n >= SMSLEN)
    			return n;
    	}
    	return n;
    
    /*! \brief 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;
    
    	unsigned char dummy[SMSLEN_8];
    
    	if (o == NULL)
    		o = dummy;
    
    	/* header - no encoding */
    	if (udhl) {
    
    		o[p++] = udhl;
    
    		while (udhl--) {
    
    			o[p++] = *udh++;
    			if (p >= SMSLEN_8)
    
    				return p;
    		}
    	}
    	while (udl--) {
    		long u;
    		u = *ud++;
    		if (u < 0 || u > 0xFF)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    			return -1;	     /* not valid */
    
    		o[p++] = u;
    		if (p >= SMSLEN_8)
    
    			return p;
    	}
    	return p;
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief 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;
    
    	unsigned char dummy[SMSLEN_8];
    
    	if (o == NULL)
    		o = dummy;
    
    	/* header - no encoding */
    	if (udhl) {
    
    		o[p++] = udhl;
    
    		while (udhl--) {
    
    			o[p++] = *udh++;
    			if (p >= SMSLEN_8)
    
    				return p;
    		}
    	}
    	while (udl--) {
    		long u;
    		u = *ud++;
    
    		o[p++] = (u >> 8);
    		if (p >= SMSLEN_8)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    			return p - 1;	  /* could not fit last character */
    
    		o[p++] = u;
    		if (p >= SMSLEN_8)
    
    			return p;
    	}
    	return p;
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief 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 == 0)
    		*p++ = 0;			/* no user data */
    	else {
    		
    
    		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;
    		}
    
    	return p - base;
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief pack a date and return */
    
    static void packdate(unsigned char *o, time_t w)
    
    	struct ast_tm t;
    	struct timeval tv = { w, 0 };
    
    #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined( __NetBSD__ ) || defined(__APPLE__) || defined(__CYGWIN__)
    
    	z = -t.tm_gmtoff / 60 / 15;
    
    	z = timezone / 60 / 15;
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	*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;
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief unpack a date and return */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    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));
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	return mktime(&t);
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief 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);	/* everything in one byte */
    
    		else
    			v = ((((i[p] >> b) + (i[p + 1] << (8 - b)))) & 0x7F);
    		b += 7;
    		if (b >= 8) {
    			b -= 8;
    			p++;
    		}
    
    		/* 0x00A0 is the encoding of ESC (27) in defaultalphabet */
    
    		if (o > ud && o[-1] == 0x00A0 && escapes[v])
    			o[-1] = escapes[v];
    		else
    			*o++ = defaultalphabet[v];
    	}
    	*udl = (o - ud);
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief 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 explicitly 8 bit coding in DCS */
    
    	*udl = (o - ud);
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief 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);
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief 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++;
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	if (is7bit(dcs)) {
    		unpacksms7(i, l, udh, udhl, ud, udl, udhi);
    
    		l = (l * 7 + 7) / 8;		/* adjust length to return */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	} else if (is8bit(dcs))
    		unpacksms8(i, l, udh, udhl, ud, udl, udhi);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		unpacksms16(i, l, udh, udhl, ud, udl, udhi);
    
    	return l + 1;
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief 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;
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief 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;		/* number of bytes */
    	if (*i == '+') {	/* record as bit 0 in byte 1 */
    
    		i++;
    		o[1] = 0x91;
    	} else
    		o[1] = 0x81;
    
    	for ( ; *i ; i++) {
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		if (!isdigit(*i))	/* ignore non-digits */
    
    			continue;
    		if (o[0] & 1)
    			o[p++] |= ((*i & 0xF) << 4);
    		else
    			o[p] = (*i & 0xF);
    		o[0]++;
    	}
    
    	if (o[0] & 1)
    		o[p++] |= 0xF0;			  /* pad */
    	return p;
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief Log the output, and remove file */
    
    static void sms_log(sms_t * h, char status)
    
    	int o;
    
    	if (*h->oa == '\0' && *h->da == '\0')
    		return;
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	o = open(log_file, O_CREAT | O_APPEND | O_WRONLY, AST_FILE_MODE);
    
    	if (o >= 0) {
    		char line[1000], mrs[3] = "", *p;
    		char buf[30];
    		unsigned char n;
    
    		if (h->mr >= 0)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    			snprintf(mrs, sizeof(mrs), "%02X", h->mr);
    		snprintf(line, sizeof(line), "%s %c%c%c%s %s %s %s ",
    			isodate(time(NULL), buf, sizeof(buf)),
    
    			status, h->rx ? 'I' : 'O', h->smsc ? 'S' : 'M', mrs, h->queue,
    			S_OR(h->oa, "-"), S_OR(h->da, "-") );
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		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;
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		write(o, line, strlen(line));
    		close(o);
    
    	*h->oa = *h->da = h->udl = 0;
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief 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 */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	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 = ast_tvnow();
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	s = fopen(fn, "r");
    
    	if (s) {
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		if (unlink(fn)) {	/* concurrent access, we lost */
    			fclose(s);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		while (fgets (line, sizeof(line), s)) {	/* process line in file */
    
    Jason Parker's avatar
    Jason Parker committed
    			void *pp = &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 */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    			while (isalnum(*p)) {
    
    				*p = tolower (*p);
    				p++;
    			}
    			while (isspace (*p))
    				*p++ = 0;
    
    			if (*p == '=') {
    
    				*p++ = 0;
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    				if (!strcmp(line, "ud")) {	 /* parse message (UTF-8) */
    
    					unsigned char o = 0;
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    					memcpy(h->udtxt, p, SMSLEN);	/* for protocol 2 */
    
    					while (*p && o < SMSLEN)
    
    Jason Parker's avatar
    Jason Parker committed
    						h->ud[o++] = utf8decode(pp);
    
    					h->udl = o;
    					if (*p)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    						ast_log(LOG_WARNING, "UD too long in %s\n", fn);
    
    				} else {
    
    					while (isspace (*p))
    						p++;
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    					if (!strcmp(line, "oa") && strlen(p) < sizeof(h->oa))
    
    						numcpy (h->oa, p);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    					else if (!strcmp(line, "da") && strlen(p) < sizeof(h->oa))
    
    						numcpy (h->da, p);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    					else if (!strcmp(line, "pid"))
    						h->pid = atoi(p);
    					else if (!strcmp(line, "dcs")) {
    						h->dcs = atoi(p);
    
    						dcsset = 1;
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    					} 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 */
    
    						if (sscanf (p, "%d-%d-%dT%d:%d:%d", &Y, &m, &d, &H, &M, &S) == 6) {
    
    							struct ast_tm t = { 0, };
    
    							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 = ast_mktime(&t, NULL);
    							if (h->scts.tv_sec == 0)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    								ast_log(LOG_WARNING, "Bad date/timein %s: %s", fn, p);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    						ast_log(LOG_WARNING, "Cannot parse in %s: %s=%si\n", fn, line, p);
    
    			} else if (*p == '#') {		/* raw hex format */
    
    				*p++ = 0;
    
    				if (*p == '#') {
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    					if (!strcmp(line, "ud")) {	/* user data */
    
    						int o = 0;
    
    						while (*p && o < SMSLEN) {
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    							if (isxdigit(*p) && isxdigit(p[1]) && isxdigit(p[2]) && isxdigit(p[3])) {
    
    								h->ud[o++] =
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    									(((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)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    							ast_log(LOG_WARNING, "UD too long / invalid UCS-2 hex in %s\n", fn);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    						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) {
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    						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)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    						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) {
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    						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)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    						ast_log(LOG_WARNING, "UDH too long / invalid hex in %s\n", fn);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    					ast_log(LOG_WARNING, "Only ud and udh can use # format, %s\n", fn);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    				ast_log(LOG_WARNING, "Cannot parse in %s: %s\n", fn, line);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		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 */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    					ast_log(LOG_WARNING, "Sending in 16 bit format(%s)\n", fn);
    
    			} else {
    
    				h->dcs = 0xF5;		/* default to 8 bit */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    				ast_log(LOG_WARNING, "Sending in 8 bit format(%s)\n", fn);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		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);
    
    Olle Johansson's avatar
    Olle Johansson committed
    /*! \brief white a received text message to a file */
    
    static void sms_writefile(sms_t * h)
    
    	char fn[200] = "", fn2[200] = "";
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    	char buf[30];
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	snprintf(fn, sizeof(fn), "%s/sms/%s", ast_config_AST_SPOOL_DIR, h->smsc ? h->rx ? "morx" : "mttx" : h->rx ? "mtrx" : "motx");
    
    	ast_mkdir(fn, 0777);			/* ensure it exists */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	ast_copy_string(fn2, fn, sizeof(fn2));
    
    	snprintf(fn2 + strlen(fn2), sizeof(fn2) - strlen(fn2), "/%s.%s-%d", h->queue, isodate(h->scts.tv_sec, buf, sizeof(buf)), seq++);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    	snprintf(fn + strlen(fn), sizeof(fn) - strlen(fn), "/.%s", fn2 + strlen(fn) + 1);
    	o = fopen(fn, "w");
    
    	if (o == NULL)
    		return;
    
    	if (*h->oa)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		fprintf(o, "oa=%s\n", h->oa);
    
    	if (*h->da)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		fprintf(o, "da=%s\n", h->da);
    
    	if (h->udhi) {
    		unsigned int p;
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		fprintf(o, "udh#");
    
    		for (p = 0; p < h->udhl; p++)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    			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)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    			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)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    				fputc(191, o);
    
    			else if (v < 0x80)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    				fputc(v, o);
    
    			else if (v < 0x800)
    			{
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    				fputc(0xC0 + (v >> 6), o);
    				fputc(0x80 + (v & 0x3F), o);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    				fputc(0xE0 + (v >> 12), o);
    				fputc(0x80 + ((v >> 6) & 0x3F), o);
    				fputc(0x80 + (v & 0x3F), o);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		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 */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    				fprintf(o, "ud#");
    
    				for (p = 0; p < h->udl; p++)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    					fprintf(o, "%02X", h->ud[p]);
    				fprintf(o, "\n");
    
    			} else {						 /* write in UCS-2 */
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    				fprintf(o, "ud##");
    
    				for (p = 0; p < h->udl; p++)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    					fprintf(o, "%04X", h->ud[p]);
    				fprintf(o, "\n");
    
    Luigi Rizzo's avatar
    Luigi Rizzo committed
    		}
    
    	if (h->scts.tv_sec) {
    
    		char buf[30];
    
    		fprintf(o, "scts=%s\n", isodate(h->scts.tv_sec, buf, sizeof(buf)));
    
    	}
    	if (h->pid)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		fprintf(o, "pid=%d\n", h->pid);
    
    	if (h->dcs != 0xF1)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		fprintf(o, "dcs=%d\n", h->dcs);
    
    	if (h->vp)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		fprintf(o, "vp=%d\n", h->vp);
    
    	if (h->srr)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		fprintf(o, "srr=1\n");
    
    	if (h->mr >= 0)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		fprintf(o, "mr=%d\n", h->mr);
    
    	if (h->rp)
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		fprintf(o, "rp=1\n");
    	fclose(o);
    	if (rename(fn, fn2))
    		unlink(fn);
    
    Tilghman Lesher's avatar
    Tilghman Lesher committed
    		ast_log(LOG_EVENT, "Received to %s\n", fn2);