| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Asterisk -- A telephony toolkit for Linux. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * CallerID Generation support  | 
					
						
							|  |  |  |  *  | 
					
						
							| 
									
										
										
										
											2005-01-21 07:06:25 +00:00
										 |  |  |  * Copyright (C) 2001 - 2005, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2004-09-09 02:29:10 +00:00
										 |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							|  |  |  |  * the GNU General Public License. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Includes code and algorithms from the Zapata library. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <time.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <unistd.h>
 | 
					
						
							|  |  |  | #include <math.h>
 | 
					
						
							| 
									
										
										
										
											2004-09-19 16:17:18 +00:00
										 |  |  | #include <ctype.h>
 | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | #include <asterisk/ulaw.h>
 | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | #include <asterisk/alaw.h>
 | 
					
						
							|  |  |  | #include <asterisk/frame.h>
 | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | #include <asterisk/channel.h>
 | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | #include <asterisk/callerid.h>
 | 
					
						
							|  |  |  | #include <asterisk/logger.h>
 | 
					
						
							|  |  |  | #include <asterisk/fskmodem.h>
 | 
					
						
							| 
									
										
										
										
											2004-05-04 06:42:06 +00:00
										 |  |  | #include <asterisk/utils.h>
 | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | struct callerid_state { | 
					
						
							|  |  |  | 	fsk_data fskd; | 
					
						
							|  |  |  | 	char rawdata[256]; | 
					
						
							|  |  |  | 	short oldstuff[160]; | 
					
						
							|  |  |  | 	int oldlen; | 
					
						
							|  |  |  | 	int pos; | 
					
						
							|  |  |  | 	int type; | 
					
						
							|  |  |  | 	int cksum; | 
					
						
							|  |  |  | 	char name[64]; | 
					
						
							|  |  |  | 	char number[64]; | 
					
						
							|  |  |  | 	int flags; | 
					
						
							|  |  |  | 	int sawflag; | 
					
						
							|  |  |  | 	int len; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-01-17 14:37:17 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | float cid_dr[4], cid_di[4]; | 
					
						
							| 
									
										
										
										
											2001-12-20 15:21:47 +00:00
										 |  |  | float clidsb = 8000.0 / 1200.0; | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | float sasdr, sasdi; | 
					
						
							|  |  |  | float casdr1, casdi1, casdr2, casdi2; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define CALLERID_SPACE	2200.0		/* 2200 hz for "0" */
 | 
					
						
							|  |  |  | #define CALLERID_MARK	1200.0		/* 1200 hz for "1" */
 | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | #define SAS_FREQ		 440.0
 | 
					
						
							|  |  |  | #define CAS_FREQ1		2130.0
 | 
					
						
							|  |  |  | #define CAS_FREQ2		2750.0
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline void gen_tones(unsigned char *buf, int len, int codec, float ddr1, float ddi1, float ddr2, float ddi2, float *cr1, float *ci1, float *cr2, float *ci2) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int x; | 
					
						
							|  |  |  | 	float t; | 
					
						
							|  |  |  | 	for (x=0;x<len;x++) { | 
					
						
							|  |  |  | 		t = *cr1 * ddr1 - *ci1 * ddi1; | 
					
						
							|  |  |  | 		*ci1 = *cr1 * ddi1 + *ci1 * ddr1; | 
					
						
							|  |  |  | 		*cr1 = t; | 
					
						
							|  |  |  | 		t = 2.0 - (*cr1 * *cr1 + *ci1 * *ci1); | 
					
						
							|  |  |  | 		*cr1 *= t; | 
					
						
							|  |  |  | 		*ci1 *= t; 	 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		t = *cr2 * ddr2 - *ci2 * ddi2; | 
					
						
							|  |  |  | 		*ci2 = *cr2 * ddi2 + *ci2 * ddr2; | 
					
						
							|  |  |  | 		*cr2 = t; | 
					
						
							|  |  |  | 		t = 2.0 - (*cr2 * *cr2 + *ci2 * *ci2); | 
					
						
							|  |  |  | 		*cr2 *= t; | 
					
						
							|  |  |  | 		*ci2 *= t; 	 | 
					
						
							| 
									
										
										
										
											2004-06-21 22:36:25 +00:00
										 |  |  | 		buf[x] = AST_LIN2X((*cr1 + *cr2) * 2048.0); | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static inline void gen_tone(unsigned char *buf, int len, int codec, float ddr1, float ddi1, float *cr1, float *ci1) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int x; | 
					
						
							|  |  |  | 	float t; | 
					
						
							|  |  |  | 	for (x=0;x<len;x++) { | 
					
						
							|  |  |  | 		t = *cr1 * ddr1 - *ci1 * ddi1; | 
					
						
							|  |  |  | 		*ci1 = *cr1 * ddi1 + *ci1 * ddr1; | 
					
						
							|  |  |  | 		*cr1 = t; | 
					
						
							|  |  |  | 		t = 2.0 - (*cr1 * *cr1 + *ci1 * *ci1); | 
					
						
							|  |  |  | 		*cr1 *= t; | 
					
						
							|  |  |  | 		*ci1 *= t; 	 | 
					
						
							|  |  |  | 		buf[x] = AST_LIN2X(*cr1 * 8192.0); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | void callerid_init(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* Initialize stuff for inverse FFT */ | 
					
						
							| 
									
										
										
										
											2002-01-17 14:37:17 +00:00
										 |  |  | 	cid_dr[0] = cos(CALLERID_SPACE * 2.0 * M_PI / 8000.0); | 
					
						
							|  |  |  | 	cid_di[0] = sin(CALLERID_SPACE * 2.0 * M_PI / 8000.0); | 
					
						
							|  |  |  | 	cid_dr[1] = cos(CALLERID_MARK * 2.0 * M_PI / 8000.0); | 
					
						
							|  |  |  | 	cid_di[1] = sin(CALLERID_MARK * 2.0 * M_PI / 8000.0); | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 	sasdr = cos(SAS_FREQ * 2.0 * M_PI / 8000.0); | 
					
						
							|  |  |  | 	sasdi = sin(SAS_FREQ * 2.0 * M_PI / 8000.0); | 
					
						
							|  |  |  | 	casdr1 = cos(CAS_FREQ1 * 2.0 * M_PI / 8000.0); | 
					
						
							|  |  |  | 	casdi1 = sin(CAS_FREQ1 * 2.0 * M_PI / 8000.0); | 
					
						
							|  |  |  | 	casdr2 = cos(CAS_FREQ2 * 2.0 * M_PI / 8000.0); | 
					
						
							|  |  |  | 	casdi2 = sin(CAS_FREQ2 * 2.0 * M_PI / 8000.0); | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-19 16:17:18 +00:00
										 |  |  | struct callerid_state *callerid_new(int cid_signalling) | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	struct callerid_state *cid; | 
					
						
							|  |  |  | 	cid = malloc(sizeof(struct callerid_state)); | 
					
						
							|  |  |  | 	if (cid) { | 
					
						
							| 
									
										
										
										
											2003-10-08 21:57:43 +00:00
										 |  |  | 		memset(cid, 0, sizeof(struct callerid_state)); | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 		cid->fskd.spb = 7;		/* 1200 baud */ | 
					
						
							|  |  |  | 		cid->fskd.hdlc = 0;		/* Async */ | 
					
						
							|  |  |  | 		cid->fskd.nbit = 8;		/* 8 bits */ | 
					
						
							|  |  |  | 		cid->fskd.nstop = 1;	/* 1 stop bit */ | 
					
						
							|  |  |  | 		cid->fskd.paridad = 0;	/* No parity */ | 
					
						
							|  |  |  | 		cid->fskd.bw=1;			/* Filter 800 Hz */ | 
					
						
							| 
									
										
										
										
											2004-09-19 16:17:18 +00:00
										 |  |  | 		if (cid_signalling == 2) { /* v23 signalling */ | 
					
						
							|  |  |  | 			cid->fskd.f_mark_idx =  4;	/* 1300 Hz */ | 
					
						
							|  |  |  | 			cid->fskd.f_space_idx = 5;	/* 2100 Hz */ | 
					
						
							|  |  |  | 		} else { /* Bell 202 signalling as default */  | 
					
						
							|  |  |  | 			cid->fskd.f_mark_idx =  2;	/* 1200 Hz */ | 
					
						
							|  |  |  | 			cid->fskd.f_space_idx = 3;	/* 2200 Hz */ | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 		cid->fskd.pcola = 0;		/* No clue */ | 
					
						
							|  |  |  | 		cid->fskd.cont = 0;			/* Digital PLL reset */ | 
					
						
							|  |  |  | 		cid->fskd.x0 = 0.0; | 
					
						
							|  |  |  | 		cid->fskd.state = 0; | 
					
						
							|  |  |  | 		memset(cid->name, 0, sizeof(cid->name)); | 
					
						
							|  |  |  | 		memset(cid->number, 0, sizeof(cid->number)); | 
					
						
							|  |  |  | 		cid->flags = CID_UNKNOWN_NAME | CID_UNKNOWN_NUMBER; | 
					
						
							|  |  |  | 		cid->pos = 0; | 
					
						
							|  |  |  | 	} else | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Out of memory\n"); | 
					
						
							|  |  |  | 	return cid; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void callerid_get(struct callerid_state *cid, char **name, char **number, int *flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	*flags = cid->flags; | 
					
						
							|  |  |  | 	if (cid->flags & (CID_UNKNOWN_NAME | CID_PRIVATE_NUMBER)) | 
					
						
							|  |  |  | 		*name = NULL; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		*name = cid->name; | 
					
						
							|  |  |  | 	if (cid->flags & (CID_UNKNOWN_NUMBER | CID_PRIVATE_NUMBER)) | 
					
						
							|  |  |  | 		*number = NULL; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		*number = cid->number; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-09-19 16:17:18 +00:00
										 |  |  | void callerid_get_dtmf(char *cidstring, char *number, int *flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 	int code; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* "Clear" the number-buffer. */ | 
					
						
							|  |  |  | 	number[0] = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (strlen(cidstring) < 2) { | 
					
						
							|  |  |  | 		ast_log(LOG_DEBUG, "No cid detected\n"); | 
					
						
							|  |  |  | 		*flags = CID_UNKNOWN_NUMBER; | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* Detect protocol and special types */ | 
					
						
							|  |  |  | 	if (cidstring[0] == 'B') { | 
					
						
							|  |  |  | 		/* Handle special codes */ | 
					
						
							|  |  |  | 		code = atoi(&cidstring[1]); | 
					
						
							|  |  |  | 		if (code == 0) | 
					
						
							|  |  |  | 			*flags = CID_UNKNOWN_NUMBER; | 
					
						
							|  |  |  | 		else if (code == 10)  | 
					
						
							|  |  |  | 			*flags = CID_PRIVATE_NUMBER; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			ast_log(LOG_DEBUG, "Unknown DTMF code %d\n", code); | 
					
						
							|  |  |  | 	} else if (cidstring[0] == 'D' && cidstring[2] == '#') { | 
					
						
							|  |  |  | 		/* .DK special code */ | 
					
						
							|  |  |  | 		if (cidstring[1] == '1') | 
					
						
							|  |  |  | 			*flags = CID_PRIVATE_NUMBER; | 
					
						
							|  |  |  | 		if (cidstring[1] == '2' || cidstring[1] == '3') | 
					
						
							|  |  |  | 			*flags = CID_UNKNOWN_NUMBER; | 
					
						
							|  |  |  | 	} else if (cidstring[0] == 'D' || cidstring[0] == 'A') { | 
					
						
							|  |  |  | 		/* "Standard" callerid */ | 
					
						
							|  |  |  | 		for (i = 1; i < strlen(cidstring); i++ ) { | 
					
						
							|  |  |  | 			if (cidstring[i] == 'C' || cidstring[i] == '#') | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			if (isdigit(cidstring[i])) | 
					
						
							|  |  |  | 				number[i-1] = cidstring[i]; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				ast_log(LOG_DEBUG, "Unknown CID digit '%c'\n", | 
					
						
							|  |  |  | 					cidstring[i]); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		number[i-1] = 0; | 
					
						
							|  |  |  | 	} else if (isdigit(cidstring[0])) { | 
					
						
							|  |  |  | 		/* It begins with a digit, so we parse it as a number and hope
 | 
					
						
							|  |  |  | 		 * for the best */ | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Couldn't detect start-character. CID " | 
					
						
							|  |  |  | 			"parsing might be unreliable\n"); | 
					
						
							|  |  |  | 		for (i = 0; i < strlen(cidstring); i++) { | 
					
						
							|  |  |  | 			if (isdigit(cidstring[i])) | 
					
						
							|  |  |  |                                 number[i] = cidstring[i]; | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		number[i] = 0; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		ast_log(LOG_DEBUG, "Unknown CID protocol, start digit '%c'\n",  | 
					
						
							|  |  |  | 			cidstring[0]); | 
					
						
							|  |  |  | 		*flags = CID_UNKNOWN_NUMBER; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | int ast_gen_cas(unsigned char *outbuf, int sendsas, int len, int codec) | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int pos = 0; | 
					
						
							|  |  |  | 	int saslen=2400; | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 	float cr1 = 1.0; | 
					
						
							|  |  |  | 	float ci1 = 0.0; | 
					
						
							|  |  |  | 	float cr2 = 1.0; | 
					
						
							|  |  |  | 	float ci2 = 0.0; | 
					
						
							| 
									
										
										
										
											2001-12-20 15:21:47 +00:00
										 |  |  | 	if (sendsas) { | 
					
						
							|  |  |  | 		if (len < saslen) | 
					
						
							|  |  |  | 			return -1; | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 		gen_tone(outbuf, saslen, codec, sasdr, sasdi, &cr1, &ci1); | 
					
						
							|  |  |  | 		len -= saslen; | 
					
						
							|  |  |  | 		pos += saslen; | 
					
						
							|  |  |  | 		cr2 = cr1; | 
					
						
							|  |  |  | 		ci2 = ci1; | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 	gen_tones(outbuf + pos, len, codec, casdr1, casdi1, casdr2, casdi2, &cr1, &ci1, &cr2, &ci2); | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | int callerid_feed(struct callerid_state *cid, unsigned char *ubuf, int len, int codec) | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int mylen = len; | 
					
						
							|  |  |  | 	int olen; | 
					
						
							|  |  |  | 	int b = 'X'; | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	int x; | 
					
						
							|  |  |  | 	short *buf = malloc(2 * len + cid->oldlen); | 
					
						
							|  |  |  | 	short *obuf = buf; | 
					
						
							|  |  |  | 	if (!buf) { | 
					
						
							|  |  |  | 		ast_log(LOG_WARNING, "Out of memory\n"); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	memset(buf, 0, 2 * len + cid->oldlen); | 
					
						
							|  |  |  | 	memcpy(buf, cid->oldstuff, cid->oldlen); | 
					
						
							|  |  |  | 	mylen += cid->oldlen/2; | 
					
						
							|  |  |  | 	for (x=0;x<len;x++)  | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 		buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]); | 
					
						
							| 
									
										
										
										
											2004-07-23 00:27:26 +00:00
										 |  |  | 	while(mylen >= 160) { | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 		olen = mylen; | 
					
						
							|  |  |  | 		res = fsk_serie(&cid->fskd, buf, &mylen, &b); | 
					
						
							| 
									
										
										
										
											2001-08-23 17:57:10 +00:00
										 |  |  | 		if (mylen < 0) { | 
					
						
							|  |  |  | 			ast_log(LOG_ERROR, "fsk_serie made mylen < 0 (%d)\n", mylen); | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 		buf += (olen - mylen); | 
					
						
							|  |  |  | 		if (res < 0) { | 
					
						
							|  |  |  | 			ast_log(LOG_NOTICE, "fsk_serie failed\n"); | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if (res == 1) { | 
					
						
							|  |  |  | 			/* Ignore invalid bytes */ | 
					
						
							|  |  |  | 			if (b > 0xff) | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			switch(cid->sawflag) { | 
					
						
							|  |  |  | 			case 0: /* Look for flag */ | 
					
						
							|  |  |  | 				if (b == 'U') | 
					
						
							|  |  |  | 					cid->sawflag = 2; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case 2: /* Get lead-in */ | 
					
						
							|  |  |  | 				if ((b == 0x04) || (b == 0x80)) { | 
					
						
							|  |  |  | 					cid->type = b; | 
					
						
							|  |  |  | 					cid->sawflag = 3; | 
					
						
							|  |  |  | 					cid->cksum = b; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case 3:	/* Get length */ | 
					
						
							|  |  |  | 				/* Not a lead in.  We're ready  */ | 
					
						
							|  |  |  | 				cid->sawflag = 4; | 
					
						
							|  |  |  | 				cid->len = b; | 
					
						
							|  |  |  | 				cid->pos = 0; | 
					
						
							|  |  |  | 				cid->cksum += b; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case 4: /* Retrieve message */ | 
					
						
							|  |  |  | 				if (cid->pos >= 128) { | 
					
						
							|  |  |  | 					ast_log(LOG_WARNING, "Caller ID too long???\n"); | 
					
						
							|  |  |  | 					return -1; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				cid->rawdata[cid->pos++] = b; | 
					
						
							|  |  |  | 				cid->len--; | 
					
						
							|  |  |  | 				cid->cksum += b; | 
					
						
							|  |  |  | 				if (!cid->len) { | 
					
						
							|  |  |  | 					cid->rawdata[cid->pos] = '\0'; | 
					
						
							|  |  |  | 					cid->sawflag = 5; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			case 5: /* Check checksum */ | 
					
						
							|  |  |  | 				if (b != (256 - (cid->cksum & 0xff))) { | 
					
						
							|  |  |  | 					ast_log(LOG_NOTICE, "Caller*ID failed checksum\n"); | 
					
						
							|  |  |  | 					/* Try again */ | 
					
						
							|  |  |  | 					cid->sawflag = 0; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2004-07-09 10:08:09 +00:00
										 |  |  | 				cid->number[0] = '\0'; | 
					
						
							|  |  |  | 				cid->name[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 				/* If we get this far we're fine.  */ | 
					
						
							|  |  |  | 				if (cid->type == 0x80) { | 
					
						
							|  |  |  | 					/* MDMF */ | 
					
						
							|  |  |  | 					/* Go through each element and process */ | 
					
						
							|  |  |  | 					for (x=0;x< cid->pos;) { | 
					
						
							|  |  |  | 						switch(cid->rawdata[x++]) { | 
					
						
							|  |  |  | 						case 1: | 
					
						
							|  |  |  | 							/* Date */ | 
					
						
							|  |  |  | 							break; | 
					
						
							|  |  |  | 						case 2: /* Number */ | 
					
						
							| 
									
										
										
										
											2002-09-04 05:09:41 +00:00
										 |  |  | 						case 3: /* Number (for Zebble) */ | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 						case 4: /* Number */ | 
					
						
							|  |  |  | 							res = cid->rawdata[x]; | 
					
						
							|  |  |  | 							if (res > 32) { | 
					
						
							|  |  |  | 								ast_log(LOG_NOTICE, "Truncating long caller ID number from %d bytes to 32\n", cid->rawdata[x]); | 
					
						
							|  |  |  | 								res = 32;  | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2004-09-09 02:29:10 +00:00
										 |  |  | 							if (ast_strlen_zero(cid->number)) { | 
					
						
							|  |  |  | 								memcpy(cid->number, cid->rawdata + x + 1, res); | 
					
						
							|  |  |  | 								/* Null terminate */ | 
					
						
							|  |  |  | 								cid->number[res] = '\0'; | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 							break; | 
					
						
							| 
									
										
										
										
											2004-09-30 17:54:41 +00:00
										 |  |  | 						case 6: /* Stentor Call Qualifier (ie. Long Distance call) */ | 
					
						
							|  |  |  | 							break; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 						case 7: /* Name */ | 
					
						
							|  |  |  | 						case 8: /* Name */ | 
					
						
							|  |  |  | 							res = cid->rawdata[x]; | 
					
						
							|  |  |  | 							if (res > 32) { | 
					
						
							|  |  |  | 								ast_log(LOG_NOTICE, "Truncating long caller ID name from %d bytes to 32\n", cid->rawdata[x]); | 
					
						
							|  |  |  | 								res = 32;  | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							memcpy(cid->name, cid->rawdata + x + 1, res); | 
					
						
							|  |  |  | 							cid->name[res] = '\0'; | 
					
						
							|  |  |  | 							break; | 
					
						
							| 
									
										
										
										
											2004-09-19 16:17:18 +00:00
										 |  |  | 						case 17: /* UK: Call type, 1=Voice Call, 2=Ringback when free, 129=Message waiting  */ | 
					
						
							|  |  |  | 						case 19: /* UK: Network message system status (Number of messages waiting) */ | 
					
						
							| 
									
										
										
										
											2003-08-09 23:57:54 +00:00
										 |  |  | 						case 22: /* Something French */ | 
					
						
							|  |  |  | 							break; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 						default: | 
					
						
							|  |  |  | 							ast_log(LOG_NOTICE, "Unknown IE %d\n", cid->rawdata[x-1]); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						x += cid->rawdata[x]; | 
					
						
							|  |  |  | 						x++; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					/* SDMF */ | 
					
						
							| 
									
										
										
										
											2001-12-20 15:21:47 +00:00
										 |  |  | 					strncpy(cid->number, cid->rawdata + 8, sizeof(cid->number)-1); | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				/* Update flags */ | 
					
						
							|  |  |  | 				cid->flags = 0; | 
					
						
							|  |  |  | 				if (!strcmp(cid->number, "P")) { | 
					
						
							|  |  |  | 					strcpy(cid->number, ""); | 
					
						
							|  |  |  | 					cid->flags |= CID_PRIVATE_NUMBER; | 
					
						
							| 
									
										
										
										
											2004-05-04 06:42:06 +00:00
										 |  |  | 				} else if (!strcmp(cid->number, "O") || ast_strlen_zero(cid->number)) { | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 					strcpy(cid->number, ""); | 
					
						
							|  |  |  | 					cid->flags |= CID_UNKNOWN_NUMBER; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (!strcmp(cid->name, "P")) { | 
					
						
							|  |  |  | 					strcpy(cid->name, ""); | 
					
						
							|  |  |  | 					cid->flags |= CID_PRIVATE_NAME; | 
					
						
							| 
									
										
										
										
											2004-05-04 06:42:06 +00:00
										 |  |  | 				} else if (!strcmp(cid->name, "O") || ast_strlen_zero(cid->name)) { | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 					strcpy(cid->name, ""); | 
					
						
							|  |  |  | 					cid->flags |= CID_UNKNOWN_NAME; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return 1; | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			default: | 
					
						
							|  |  |  | 				ast_log(LOG_ERROR, "Dunno what to do with a digit in sawflag %d\n", cid->sawflag); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (mylen) { | 
					
						
							|  |  |  | 		memcpy(cid->oldstuff, buf, mylen * 2); | 
					
						
							|  |  |  | 		cid->oldlen = mylen * 2; | 
					
						
							| 
									
										
										
										
											2001-08-23 17:57:10 +00:00
										 |  |  | 	} else | 
					
						
							|  |  |  | 		cid->oldlen = 0; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 	free(obuf); | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void callerid_free(struct callerid_state *cid) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	free(cid); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | static int callerid_genmsg(char *msg, int size, char *number, char *name, int flags) | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	time_t t; | 
					
						
							| 
									
										
										
										
											2003-03-31 03:19:34 +00:00
										 |  |  | 	struct tm tm; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 	char *ptr; | 
					
						
							|  |  |  | 	int res; | 
					
						
							|  |  |  | 	int i,x; | 
					
						
							|  |  |  | 	/* Get the time */ | 
					
						
							|  |  |  | 	time(&t); | 
					
						
							| 
									
										
										
										
											2003-03-31 03:19:34 +00:00
										 |  |  | 	localtime_r(&t,&tm); | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	ptr = msg; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* Format time and message header */ | 
					
						
							| 
									
										
										
										
											2003-03-31 03:19:34 +00:00
										 |  |  | 	res = snprintf(ptr, size, "\001\010%02d%02d%02d%02d", tm.tm_mon + 1, | 
					
						
							|  |  |  | 				tm.tm_mday, tm.tm_hour, tm.tm_min); | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 	size -= res; | 
					
						
							|  |  |  | 	ptr += res; | 
					
						
							| 
									
										
										
										
											2004-05-04 06:42:06 +00:00
										 |  |  | 	if (!number || ast_strlen_zero(number) || (flags & CID_UNKNOWN_NUMBER)) { | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 		/* Indicate number not known */ | 
					
						
							|  |  |  | 		res = snprintf(ptr, size, "\004\001O"); | 
					
						
							|  |  |  | 		size -= res; | 
					
						
							|  |  |  | 		ptr += res; | 
					
						
							|  |  |  | 	} else if (flags & CID_PRIVATE_NUMBER) { | 
					
						
							|  |  |  | 		/* Indicate number is private */ | 
					
						
							|  |  |  | 		res = snprintf(ptr, size, "\004\001P"); | 
					
						
							|  |  |  | 		size -= res; | 
					
						
							|  |  |  | 		ptr += res; | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 		/* Send up to 16 digits of number MAX */ | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 		i = strlen(number); | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 		if (i > 16) i = 16; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 		res = snprintf(ptr, size, "\002%c", i); | 
					
						
							|  |  |  | 		size -= res; | 
					
						
							|  |  |  | 		ptr += res; | 
					
						
							|  |  |  | 		for (x=0;x<i;x++) | 
					
						
							|  |  |  | 			ptr[x] = number[x]; | 
					
						
							|  |  |  | 		ptr[i] = '\0'; | 
					
						
							|  |  |  | 		ptr += i; | 
					
						
							|  |  |  | 		size -= i; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-05-04 06:42:06 +00:00
										 |  |  | 	if (!name || ast_strlen_zero(name) || (flags & CID_UNKNOWN_NAME)) { | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 		/* Indicate name not known */ | 
					
						
							|  |  |  | 		res = snprintf(ptr, size, "\010\001O"); | 
					
						
							|  |  |  | 		size -= res; | 
					
						
							|  |  |  | 		ptr += res; | 
					
						
							|  |  |  | 	} else if (flags & CID_PRIVATE_NAME) { | 
					
						
							|  |  |  | 		/* Indicate name is private */ | 
					
						
							|  |  |  | 		res = snprintf(ptr, size, "\010\001P"); | 
					
						
							|  |  |  | 		size -= res; | 
					
						
							|  |  |  | 		ptr += res; | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 		/* Send up to 16 digits of name MAX */ | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 		i = strlen(name); | 
					
						
							|  |  |  | 		if (i > 16) i = 16; | 
					
						
							|  |  |  | 		res = snprintf(ptr, size, "\007%c", i); | 
					
						
							|  |  |  | 		size -= res; | 
					
						
							|  |  |  | 		ptr += res; | 
					
						
							|  |  |  | 		for (x=0;x<i;x++) | 
					
						
							|  |  |  | 			ptr[x] = name[x]; | 
					
						
							|  |  |  | 		ptr[i] = '\0'; | 
					
						
							|  |  |  | 		ptr += i; | 
					
						
							|  |  |  | 		size -= i; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 	return (ptr - msg); | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | int vmwi_generate(unsigned char *buf, int active, int mdmf, int codec) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	unsigned char msg[256]; | 
					
						
							|  |  |  | 	int len=0; | 
					
						
							|  |  |  | 	int sum; | 
					
						
							|  |  |  | 	int x; | 
					
						
							|  |  |  | 	int bytes = 0; | 
					
						
							|  |  |  | 	float cr = 1.0; | 
					
						
							|  |  |  | 	float ci = 0.0; | 
					
						
							|  |  |  | 	float scont = 0.0; | 
					
						
							|  |  |  | 	if (mdmf) { | 
					
						
							|  |  |  | 		/* MDMF Message waiting */ | 
					
						
							|  |  |  | 		msg[len++] = 0x82; | 
					
						
							|  |  |  | 		/* Length is 3 */ | 
					
						
							|  |  |  | 		msg[len++] = 3; | 
					
						
							|  |  |  | 		/* IE is "Message Waiting Parameter" */ | 
					
						
							|  |  |  | 		msg[len++] = 0xb; | 
					
						
							|  |  |  | 		/* Length of IE is one */ | 
					
						
							|  |  |  | 		msg[len++] = 1; | 
					
						
							|  |  |  | 		/* Active or not */ | 
					
						
							|  |  |  | 		if (active) | 
					
						
							|  |  |  | 			msg[len++] = 0xff; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			msg[len++] = 0x00; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		/* SDMF Message waiting */ | 
					
						
							|  |  |  | 		msg[len++] = 0x6; | 
					
						
							|  |  |  | 		/* Length is 3 */ | 
					
						
							|  |  |  | 		msg[len++] = 3; | 
					
						
							|  |  |  | 		if (active) { | 
					
						
							|  |  |  | 			msg[len++] = 0x42; | 
					
						
							|  |  |  | 			msg[len++] = 0x42; | 
					
						
							|  |  |  | 			msg[len++] = 0x42; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			msg[len++] = 0x6f; | 
					
						
							|  |  |  | 			msg[len++] = 0x6f; | 
					
						
							|  |  |  | 			msg[len++] = 0x6f; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	sum = 0; | 
					
						
							|  |  |  | 	for (x=0;x<len;x++) | 
					
						
							|  |  |  | 		sum += msg[x]; | 
					
						
							|  |  |  | 	sum = (256 - (sum & 255)); | 
					
						
							|  |  |  | 	msg[len++] = sum; | 
					
						
							| 
									
										
										
										
											2004-04-04 02:00:19 +00:00
										 |  |  | 	/* Wait a half a second */ | 
					
						
							|  |  |  | 	for (x=0;x<4000;x++) | 
					
						
							|  |  |  | 		PUT_BYTE(0x7f); | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 	/* Transmit 30 0x55's (looks like a square wave) for channel seizure */ | 
					
						
							|  |  |  | 	for (x=0;x<30;x++) | 
					
						
							|  |  |  | 		PUT_CLID(0x55); | 
					
						
							|  |  |  | 	/* Send 170ms of callerid marks */ | 
					
						
							|  |  |  | 	for (x=0;x<170;x++) | 
					
						
							|  |  |  | 		PUT_CLID_MARKMS; | 
					
						
							|  |  |  | 	for (x=0;x<len;x++) { | 
					
						
							|  |  |  | 		PUT_CLID(msg[x]); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/* Send 50 more ms of marks */ | 
					
						
							|  |  |  | 	for (x=0;x<50;x++) | 
					
						
							|  |  |  | 		PUT_CLID_MARKMS; | 
					
						
							|  |  |  | 	return bytes; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int callerid_generate(unsigned char *buf, char *number, char *name, int flags, int callwaiting, int codec) | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int bytes=0; | 
					
						
							|  |  |  | 	int x, sum; | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 	int len; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 	/* Initial carriers (real/imaginary) */ | 
					
						
							|  |  |  | 	float cr = 1.0; | 
					
						
							|  |  |  | 	float ci = 0.0; | 
					
						
							|  |  |  | 	float scont = 0.0; | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 	unsigned char msg[256]; | 
					
						
							|  |  |  | 	len = callerid_genmsg(msg, sizeof(msg), number, name, flags); | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | 	if (!callwaiting) { | 
					
						
							|  |  |  | 		/* Wait a half a second */ | 
					
						
							|  |  |  | 		for (x=0;x<4000;x++) | 
					
						
							|  |  |  | 			PUT_BYTE(0x7f); | 
					
						
							|  |  |  | 		/* Transmit 30 0x55's (looks like a square wave) for channel seizure */ | 
					
						
							|  |  |  | 		for (x=0;x<30;x++) | 
					
						
							|  |  |  | 			PUT_CLID(0x55); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 	/* Send 150ms of callerid marks */ | 
					
						
							|  |  |  | 	for (x=0;x<150;x++) | 
					
						
							|  |  |  | 		PUT_CLID_MARKMS; | 
					
						
							|  |  |  | 	/* Send 0x80 indicating MDMF format */ | 
					
						
							|  |  |  | 	PUT_CLID(0x80); | 
					
						
							|  |  |  | 	/* Put length of whole message */ | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 	PUT_CLID(len); | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 	sum = 0x80 + strlen(msg); | 
					
						
							|  |  |  | 	/* Put each character of message and update checksum */ | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 	for (x=0;x<len; x++) { | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 		PUT_CLID(msg[x]); | 
					
						
							|  |  |  | 		sum += msg[x]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	/* Send 2's compliment of sum */ | 
					
						
							|  |  |  | 	PUT_CLID(256 - (sum & 255)); | 
					
						
							| 
									
										
										
										
											2002-11-29 16:43:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 	/* Send 50 more ms of marks */ | 
					
						
							|  |  |  | 	for (x=0;x<50;x++) | 
					
						
							|  |  |  | 		PUT_CLID_MARKMS; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return bytes; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ast_shrink_phone_number(char *n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int x,y=0; | 
					
						
							| 
									
										
										
										
											2004-10-08 15:49:37 +00:00
										 |  |  | 	int bracketed=0; | 
					
						
							|  |  |  | 	for (x=0;n[x];x++) { | 
					
						
							|  |  |  | 		switch(n[x]) { | 
					
						
							|  |  |  | 		case '[': | 
					
						
							|  |  |  | 			bracketed++; | 
					
						
							|  |  |  | 			n[y++] = n[x]; | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 		case ']': | 
					
						
							|  |  |  | 			bracketed--; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 			n[y++] = n[x]; | 
					
						
							| 
									
										
										
										
											2004-10-08 15:49:37 +00:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		case '-': | 
					
						
							|  |  |  | 			if (bracketed) | 
					
						
							|  |  |  | 				n[y++] = n[x]; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2004-10-22 13:08:35 +00:00
										 |  |  | 		case '.': | 
					
						
							|  |  |  | 			if (!n[x+1]) | 
					
						
							|  |  |  | 				n[y++] = n[x]; | 
					
						
							|  |  |  | 			break; | 
					
						
							| 
									
										
										
										
											2004-10-08 15:49:37 +00:00
										 |  |  | 		default: | 
					
						
							| 
									
										
										
										
											2004-10-22 13:08:35 +00:00
										 |  |  | 			if (!strchr("( )", n[x])) | 
					
						
							| 
									
										
										
										
											2004-10-08 15:49:37 +00:00
										 |  |  | 				n[y++] = n[x]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 	n[y] = '\0'; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ast_isphonenumber(char *n) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int x; | 
					
						
							| 
									
										
										
										
											2004-05-04 06:42:06 +00:00
										 |  |  | 	if (!n || ast_strlen_zero(n)) | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 	for (x=0;n[x];x++) | 
					
						
							| 
									
										
										
										
											2004-04-04 22:14:10 +00:00
										 |  |  | 		if (!strchr("0123456789*#+", n[x])) | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ast_callerid_parse(char *instr, char **name, char **location) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	char *ns, *ne; | 
					
						
							|  |  |  | 	char *ls, *le; | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | 	char tmp[256]; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 	/* Try for "name" <location> format or 
 | 
					
						
							|  |  |  | 	   name <location> format */ | 
					
						
							|  |  |  | 	if ((ls = strchr(instr, '<')) && (le = strchr(ls, '>'))) { | 
					
						
							|  |  |  | 		/* Found the location */ | 
					
						
							|  |  |  | 		*le = '\0'; | 
					
						
							|  |  |  | 		*ls = '\0'; | 
					
						
							|  |  |  | 		*location = ls + 1; | 
					
						
							|  |  |  | 		if ((ns = strchr(instr, '\"')) && (ne = strchr(ns + 1, '\"'))) { | 
					
						
							|  |  |  | 			/* Get name out of quotes */ | 
					
						
							|  |  |  | 			*ns = '\0'; | 
					
						
							|  |  |  | 			*ne = '\0'; | 
					
						
							|  |  |  | 			*name = ns + 1; | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			/* Just trim off any trailing spaces */ | 
					
						
							|  |  |  | 			*name = instr; | 
					
						
							| 
									
										
										
										
											2004-05-04 06:42:06 +00:00
										 |  |  | 			while(!ast_strlen_zero(instr) && (instr[strlen(instr) - 1] < 33)) | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 				instr[strlen(instr) - 1] = '\0'; | 
					
						
							|  |  |  | 			/* And leading spaces */ | 
					
						
							|  |  |  | 			while(**name && (**name < 33)) | 
					
						
							| 
									
										
										
										
											2003-11-09 18:46:52 +00:00
										 |  |  | 				(*name)++; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2001-12-20 15:21:47 +00:00
										 |  |  | 		strncpy(tmp, instr, sizeof(tmp)-1); | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | 		ast_shrink_phone_number(tmp); | 
					
						
							| 
									
										
										
										
											2001-08-23 17:57:10 +00:00
										 |  |  | 		if (ast_isphonenumber(tmp)) { | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | 			/* Assume it's just a location */ | 
					
						
							|  |  |  | 			*name = NULL; | 
					
						
							|  |  |  | 			*location = instr; | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2003-08-16 20:40:42 +00:00
										 |  |  | 			/* Assume it's just a name.  Make sure it's not quoted though */ | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | 			*name = instr; | 
					
						
							| 
									
										
										
										
											2003-08-16 20:40:42 +00:00
										 |  |  | 			while(*(*name) && ((*(*name) < 33) || (*(*name) == '\"'))) (*name)++; | 
					
						
							|  |  |  | 			ne = *name + strlen(*name) - 1; | 
					
						
							|  |  |  | 			while((ne > *name) && ((*ne < 33) || (*ne == '\"'))) { *ne = '\0'; ne--; } | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | 			*location = NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | static int __ast_callerid_generate(unsigned char *buf, char *name, char *number, int callwaiting, int codec) | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | 	if (name && ast_strlen_zero(name)) | 
					
						
							|  |  |  | 		name = NULL; | 
					
						
							|  |  |  | 	if (number && ast_strlen_zero(number)) | 
					
						
							|  |  |  | 		number = NULL; | 
					
						
							|  |  |  | 	return callerid_generate(buf, number, name, 0, callwaiting, codec); | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | int ast_callerid_generate(unsigned char *buf, char *name, char *number, int codec) | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | 	return __ast_callerid_generate(buf, name, number, 0, codec); | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | int ast_callerid_callwaiting_generate(unsigned char *buf, char *name, char *number, int codec) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return __ast_callerid_generate(buf, name, number, 1, codec); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-10-22 19:45:39 +00:00
										 |  |  | char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *num, const char *unknown) | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-10-22 19:45:39 +00:00
										 |  |  | 	if (!unknown) | 
					
						
							|  |  |  | 		unknown = "<unknown>"; | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | 	if (name && num) | 
					
						
							|  |  |  | 		snprintf(buf, bufsiz, "\"%s\" <%s>", name, num); | 
					
						
							|  |  |  | 	else if (name)  | 
					
						
							|  |  |  | 		strncpy(buf, name, bufsiz - 1); | 
					
						
							|  |  |  | 	else if (num) | 
					
						
							|  |  |  | 		strncpy(buf, num, bufsiz - 1); | 
					
						
							|  |  |  | 	else | 
					
						
							| 
									
										
										
										
											2004-10-22 19:45:39 +00:00
										 |  |  | 		strncpy(buf, unknown, bufsiz - 1); | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | 	return buf; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | int ast_callerid_split(const char *buf, char *name, int namelen, char *num, int numlen) | 
					
						
							| 
									
										
										
										
											2001-05-07 00:43:32 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | 	char *tmp; | 
					
						
							|  |  |  | 	char *l = NULL, *n = NULL; | 
					
						
							|  |  |  | 	tmp = ast_strdupa(buf); | 
					
						
							|  |  |  | 	if (!tmp) { | 
					
						
							|  |  |  | 		name[0] = '\0'; | 
					
						
							|  |  |  | 		num[0] = '\0'; | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ast_callerid_parse(tmp, &n, &l); | 
					
						
							|  |  |  | 	if (n) | 
					
						
							|  |  |  | 		strncpy(name, n, namelen - 1); | 
					
						
							| 
									
										
										
										
											2005-02-04 17:45:37 +00:00
										 |  |  | 	else | 
					
						
							|  |  |  | 		name[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | 	if (l) { | 
					
						
							|  |  |  | 		ast_shrink_phone_number(l); | 
					
						
							|  |  |  | 		strncpy(num, l, numlen - 1); | 
					
						
							| 
									
										
										
										
											2005-02-04 17:45:37 +00:00
										 |  |  | 	} else | 
					
						
							|  |  |  | 		num[0] = '\0'; | 
					
						
							| 
									
										
										
										
											2004-10-02 00:58:31 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2001-03-24 16:46:30 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2005-02-26 07:34:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static struct { | 
					
						
							|  |  |  | 	int val; | 
					
						
							|  |  |  | 	char *name; | 
					
						
							|  |  |  | 	char *description; | 
					
						
							|  |  |  | } pres_types[] = { | 
					
						
							|  |  |  | 	{  AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED, "allowed_not_screened", "Presentation Allowed, Not Screened"}, | 
					
						
							|  |  |  | 	{  AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, "allowed_passed_screen", "Presentation Allowed, Passed Screen"}, | 
					
						
							|  |  |  | 	{  AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN, "allowed_failed_screen", "Presentation Allowed, Failed Screen"}, | 
					
						
							|  |  |  | 	{  AST_PRES_ALLOWED_NETWORK_NUMBER, "allowed", "Presentation Allowed, Network Number"}, | 
					
						
							|  |  |  | 	{  AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED, "prohib_not_screened", "Presentation Prohibited, Not Screened"}, | 
					
						
							|  |  |  | 	{  AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN, "prohib_passed_screen", "Presentation Prohibited, Passed Screen"}, | 
					
						
							|  |  |  | 	{  AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN, "prohib_failed_screen", "Presentation Prohibited, Failed Screen"}, | 
					
						
							|  |  |  | 	{  AST_PRES_PROHIB_NETWORK_NUMBER, "prohib", "Presentation Prohibited, Network Number"}, | 
					
						
							|  |  |  | 	{  AST_PRES_NUMBER_NOT_AVAILABLE, "unavailable", "Number Unavailable"}, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int ast_parse_caller_presentation(const char *data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < ((sizeof(pres_types) / sizeof(pres_types[0]))); i++) { | 
					
						
							|  |  |  | 		if (!strcasecmp(pres_types[i].name, data)) | 
					
						
							|  |  |  | 			return pres_types[i].val; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const char *ast_describe_caller_presentation(int data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < ((sizeof(pres_types) / sizeof(pres_types[0]))); i++) { | 
					
						
							|  |  |  | 		if (pres_types[i].val == data) | 
					
						
							|  |  |  | 			return pres_types[i].description; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return "unknown"; | 
					
						
							|  |  |  | } |