| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Asterisk -- An open source telephony toolkit. | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Copyright (C) 1999 - 2005, Digium, Inc. | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-05-15 17:22:41 +00:00
										 |  |  |  * Mark Spencer <markster@digium.com> | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * Includes code and algorithms from the Zapata library. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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. | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software, distributed under the terms of | 
					
						
							| 
									
										
										
										
											2005-09-14 20:46:50 +00:00
										 |  |  |  * the GNU General Public License Version 2. See the LICENSE file | 
					
						
							|  |  |  |  * at the top of the source tree. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-10-24 20:12:06 +00:00
										 |  |  | /*! \file
 | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  |  * \brief FSK Modulator/Demodulator | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2005-12-30 21:18:06 +00:00
										 |  |  |  * \author Mark Spencer <markster@digium.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \arg Includes code and algorithms from the Zapata library. | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-15 16:20:16 +00:00
										 |  |  | /*** MODULEINFO
 | 
					
						
							|  |  |  | 	<support_level>core</support_level> | 
					
						
							|  |  |  |  ***/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-06-06 20:27:51 +00:00
										 |  |  | #include "asterisk.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-04-22 13:11:34 +00:00
										 |  |  | #include "asterisk/fskmodem.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | #define NBW	2
 | 
					
						
							|  |  |  | #define BWLIST	{75,800}
 | 
					
						
							| 
									
										
										
										
											2004-09-19 16:17:18 +00:00
										 |  |  | #define	NF	6
 | 
					
						
							|  |  |  | #define	FLIST {1400,1800,1200,2200,1300,2100}
 | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define STATE_SEARCH_STARTBIT	0
 | 
					
						
							|  |  |  | #define STATE_SEARCH_STARTBIT2	1
 | 
					
						
							|  |  |  | #define STATE_SEARCH_STARTBIT3	2
 | 
					
						
							|  |  |  | #define STATE_GET_BYTE			3
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | static inline int iget_sample(short **buffer, int *len) | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	int retval; | 
					
						
							|  |  |  | 	retval = (int) **buffer; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 	(*buffer)++; | 
					
						
							|  |  |  | 	(*len)--; | 
					
						
							|  |  |  | 	return retval; | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | #define IGET_SAMPLE iget_sample(&buffer, len)
 | 
					
						
							| 
									
										
										
										
											2006-12-05 19:41:26 +00:00
										 |  |  | /*! \brief Coefficients for input filters
 | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  |  * Coefficients table, generated by program "mkfilter" | 
					
						
							| 
									
										
										
										
											2006-12-05 19:41:26 +00:00
										 |  |  |  * mkfilter is part of the zapatatelephony.org distribution | 
					
						
							|  |  |  |  * Format: coef[IDX_FREC][IDX_BW][IDX_COEF] | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  |  * IDX_COEF = 0	=>	1/GAIN | 
					
						
							|  |  |  |  * IDX_COEF = 1-6	=>	Coefficientes y[n] | 
					
						
							| 
									
										
										
										
											2006-12-05 19:41:26 +00:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | static double coef_in[NF][NBW][8]={ | 
					
						
							|  |  |  | 	{  { 1.8229206611e-04,-7.8997325866e-01,2.2401819940e+00,-4.6751353581e+00,5.5080745712e+00,-5.0571565772e+00,2.6215820004e+00,0.0000000000e+00, | 
					
						
							|  |  |  | 	},  { 9.8532175289e-02,-5.6297236492e-02,3.3146713415e-01,-9.2239200436e-01,1.4844365184e+00,-2.0183258642e+00,2.0074154497e+00,0.0000000000e+00, | 
					
						
							|  |  |  | 	},  },  {  { 1.8229206610e-04,-7.8997325866e-01,7.7191410839e-01,-2.8075643964e+00,1.6948618347e+00,-3.0367273700e+00,9.0333559408e-01,0.0000000000e+00, | 
					
						
							|  |  |  | 	},  { 9.8531161839e-02,-5.6297236492e-02,1.1421579050e-01,-4.8122536483e-01,4.0121072432e-01,-7.4834487567e-01,6.9170822332e-01,0.0000000000e+00, | 
					
						
							|  |  |  | 	},  },  {  { 1.8229206611e-04,-7.8997325866e-01,2.9003821430e+00,-6.1082779024e+00,7.7169345751e+00,-6.6075999680e+00,3.3941838836e+00,0.0000000000e+00, | 
					
						
							|  |  |  | 	},  { 9.8539686961e-02,-5.6297236492e-02,4.2915323820e-01,-1.2609358633e+00,2.2399213250e+00,-2.9928879142e+00,2.5990173742e+00,0.0000000000e+00, | 
					
						
							|  |  |  | 	},  },  {  { 1.8229206610e-04,-7.8997325866e-01,-7.7191410839e-01,-2.8075643964e+00,-1.6948618347e+00,-3.0367273700e+00,-9.0333559408e-01,0.0000000000e+00, | 
					
						
							|  |  |  | 	},  { 9.8531161839e-02,-5.6297236492e-02,-1.1421579050e-01,-4.8122536483e-01,-4.0121072432e-01,-7.4834487567e-01,-6.9170822332e-01,0.0000000000e+00, | 
					
						
							|  |  |  | 	},  },  {  { 1.8229206611e-04,-7.8997325866e-01,2.5782298908e+00,-5.3629717478e+00,6.5890882172e+00,-5.8012914776e+00,3.0171839130e+00,0.0000000000e+00, | 
					
						
							|  |  |  | 	},  { 9.8534230718e-02,-5.6297236492e-02,3.8148618075e-01,-1.0848760410e+00,1.8441165168e+00,-2.4860666655e+00,2.3103384142e+00,0.0000000000e+00, | 
					
						
							|  |  |  | 	},  },  {  { 1.8229206610e-04,-7.8997325866e-01,-3.8715051001e-01,-2.6192408538e+00,-8.3977994034e-01,-2.8329897913e+00,-4.5306444352e-01,0.0000000000e+00, | 
					
						
							|  |  |  | 	},  { 9.8531160936e-02,-5.6297236492e-02,-5.7284484199e-02,-4.3673866734e-01,-1.9564766257e-01,-6.2028156584e-01,-3.4692356122e-01,0.0000000000e+00, | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 	},  }, | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-05 19:41:26 +00:00
										 |  |  | /*! \brief Coefficients for output filter
 | 
					
						
							|  |  |  |  * Coefficients table, generated by program "mkfilter" | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  |  * Format: coef[IDX_BW][IDX_COEF] | 
					
						
							|  |  |  |  * IDX_COEF = 0	=>	1/GAIN | 
					
						
							| 
									
										
										
										
											2006-12-05 19:41:26 +00:00
										 |  |  |  * IDX_COEF = 1-6	=>	Coefficientes y[n] | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | */ | 
					
						
							|  |  |  | static double coef_out[NBW][8]={ | 
					
						
							|  |  |  | 	{ 1.3868644653e-08,-6.3283665042e-01,4.0895057217e+00,-1.1020074592e+01,1.5850766191e+01,-1.2835109292e+01,5.5477477340e+00,0.0000000000e+00, | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 	},  { 3.1262119724e-03,-7.8390522307e-03,8.5209627801e-02,-4.0804129163e-01,1.1157139955e+00,-1.8767603680e+00,1.8916395224e+00,0.0000000000e+00 | 
					
						
							|  |  |  | 	}, | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | /*! Integer Pass Band demodulator filter  */ | 
					
						
							|  |  |  | static inline int ibpdfilter(struct filter_struct * fs, int in) | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	int i,j; | 
					
						
							|  |  |  | 	int s; | 
					
						
							|  |  |  | 	int64_t s_interim; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	/* integer filter */ | 
					
						
							|  |  |  | 	s =  in * fs->icoefs[0]; | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	fs->ixv[(fs->ip + 6) & 7] = s; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	s =      (fs->ixv[fs->ip]           + fs->ixv[(fs->ip + 6) & 7]) + | 
					
						
							|  |  |  | 		6  * (fs->ixv[(fs->ip + 1) & 7] + fs->ixv[(fs->ip + 5) & 7]) + | 
					
						
							|  |  |  | 		15 * (fs->ixv[(fs->ip + 2) & 7] + fs->ixv[(fs->ip + 4) & 7]) + | 
					
						
							|  |  |  | 		20 *  fs->ixv[(fs->ip + 3) & 7]; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	for (i = 1, j = fs->ip; i < 7; i++, j++) { | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 		/* Promote operation to 64 bit to prevent overflow that occurred in 32 bit) */ | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		s_interim = (int64_t)(fs->iyv[j & 7]) * | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 				(int64_t)(fs->icoefs[i]) / | 
					
						
							|  |  |  | 				(int64_t)(1024); | 
					
						
							| 
									
										
										
										
											2007-11-06 19:23:10 +00:00
										 |  |  | 		s += (int) s_interim; | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	fs->iyv[j & 7] = s; | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	fs->ip++; | 
					
						
							|  |  |  | 	fs->ip &= 7; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 	return s; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | /*! Integer Band Pass filter */ | 
					
						
							|  |  |  | static inline int ibpfilter(struct filter_struct * fs, int in) | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-11-06 19:23:10 +00:00
										 |  |  | 	int i, j; | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	int s; | 
					
						
							|  |  |  | 	int64_t s_interim; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	/* integer filter */ | 
					
						
							|  |  |  | 	s =  in * fs->icoefs[0] / 256; | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	fs->ixv[(fs->ip + 6) & 7] = s; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	s = (fs->ixv[(fs->ip + 6) & 7] - fs->ixv[fs->ip]) | 
					
						
							|  |  |  | 		+ 3 * (fs->ixv[(fs->ip + 2) & 7] - fs->ixv[(fs->ip + 4) & 7]); | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 1, j = fs->ip; i < 7; i++, j++) { | 
					
						
							|  |  |  | 		s_interim = (int64_t)(fs->iyv[j & 7]) * | 
					
						
							|  |  |  | 				(int64_t)(fs->icoefs[i]) / | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 				(int64_t)(256); | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 		s += (int) s_interim; | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	fs->iyv[j & 7] = s; | 
					
						
							|  |  |  | 	fs->ip++; | 
					
						
							|  |  |  | 	fs->ip &= 7; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 	return s; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | static inline int idemodulator(fsk_data *fskd, int *retval, int x) | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	int is, im, id; | 
					
						
							|  |  |  | 	int ilin2; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	is = ibpfilter(&fskd->space_filter, x); | 
					
						
							|  |  |  | 	im = ibpfilter(&fskd->mark_filter, x); | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	ilin2 = ((im * im) - (is * is)) / (256 * 256); | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	id = ibpdfilter(&fskd->demod_filter, ilin2); | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	*retval = id; | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int get_bit_raw(fsk_data *fskd, short *buffer, int *len) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-12-05 19:41:26 +00:00
										 |  |  | 	/* This function implements a DPLL to synchronize with the bits */ | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 	int f; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	int ix; | 
					
						
							|  |  |  | 	/* PLL coeffs are set up in callerid_new */ | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	for (f = 0;;) { | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 		if (idemodulator(fskd, &ix, IGET_SAMPLE)) return(-1); | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 		if ((ix * fskd->xi0) < 0) { /* Transicion */ | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 			if (!f) { | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 				if (fskd->icont < (fskd->pllispb2)) { | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 					fskd->icont += fskd->pllids; | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					fskd->icont -= fskd->pllids; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2007-11-06 19:23:10 +00:00
										 |  |  | 				f = 1; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 		fskd->xi0 = ix; | 
					
						
							|  |  |  | 		fskd->icont += 32; | 
					
						
							|  |  |  | 		if (fskd->icont > fskd->pllispb) { | 
					
						
							|  |  |  | 			fskd->icont -= fskd->pllispb; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 			break; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	f = (ix > 0) ? 0x80 : 0; | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 	return f; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | int fskmodem_init(fsk_data *fskd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	fskd->space_filter.ip  = 0; | 
					
						
							|  |  |  | 	fskd->mark_filter.ip   = 0; | 
					
						
							|  |  |  | 	fskd->demod_filter.ip  = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for ( i = 0 ; i < 7 ; i++ ) { | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		fskd->space_filter.icoefs[i] = | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 			coef_in[fskd->f_space_idx][fskd->bw][i] * 256; | 
					
						
							|  |  |  | 		fskd->space_filter.ixv[i] = 0;; | 
					
						
							|  |  |  | 		fskd->space_filter.iyv[i] = 0;; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		fskd->mark_filter.icoefs[i] = | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 			coef_in[fskd->f_mark_idx][fskd->bw][i] * 256; | 
					
						
							|  |  |  | 		fskd->mark_filter.ixv[i] = 0;; | 
					
						
							|  |  |  | 		fskd->mark_filter.iyv[i] = 0;; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		fskd->demod_filter.icoefs[i] = | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 			coef_out[fskd->bw][i] * 1024; | 
					
						
							|  |  |  | 		fskd->demod_filter.ixv[i] = 0;; | 
					
						
							|  |  |  | 		fskd->demod_filter.iyv[i] = 0;; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-05 20:15:37 +00:00
										 |  |  | int fsk_serial(fsk_data *fskd, short *buffer, int *len, int *outbyte) | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	int a; | 
					
						
							| 
									
										
										
										
											2007-11-06 19:23:10 +00:00
										 |  |  | 	int i, j, n1, r; | 
					
						
							|  |  |  | 	int samples = 0; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 	int olen; | 
					
						
							| 
									
										
										
										
											2007-11-06 19:23:10 +00:00
										 |  |  | 	int beginlen = *len; | 
					
						
							| 
									
										
										
										
											2007-08-02 18:32:34 +00:00
										 |  |  | 	int beginlenx; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-23 00:11:32 +00:00
										 |  |  | 	switch (fskd->state) { | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 		/* Pick up where we left off */ | 
					
						
							|  |  |  | 	case STATE_SEARCH_STARTBIT2: | 
					
						
							|  |  |  | 		goto search_startbit2; | 
					
						
							|  |  |  | 	case STATE_SEARCH_STARTBIT3: | 
					
						
							|  |  |  | 		goto search_startbit3; | 
					
						
							|  |  |  | 	case STATE_GET_BYTE: | 
					
						
							|  |  |  | 		goto getbyte; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-12-05 19:41:26 +00:00
										 |  |  | 	/* We await for start bit	*/ | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 	do { | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 		/* this was jesus's nice, reasonable, working (at least with RTTY) code
 | 
					
						
							|  |  |  | 		to look for the beginning of the start bit. Unfortunately, since TTY/TDD's | 
					
						
							|  |  |  | 		just start sending a start bit with nothing preceding it at the beginning | 
					
						
							|  |  |  | 		of a transmission (what a LOSING design), we cant do it this elegantly */ | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 		/* NOT USED
 | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 				if (demodulator(zap,&x1)) | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 					return -1; | 
					
						
							|  |  |  | 				for(;;) { | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 					if (demodulator(zap,&x2)) | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 						return -1; | 
					
						
							|  |  |  | 					if (x1>0 && x2<0) break; | 
					
						
							|  |  |  | 					x1=x2; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 		*/ | 
					
						
							|  |  |  | 		/* this is now the imprecise, losing, but functional code to detect the
 | 
					
						
							|  |  |  | 		beginning of a start bit in the TDD sceanario. It just looks for sufficient | 
					
						
							|  |  |  | 		level to maybe, perhaps, guess, maybe that its maybe the beginning of | 
					
						
							|  |  |  | 		a start bit, perhaps. This whole thing stinks! */ | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 		beginlenx = beginlen; /* just to avoid unused war warnings */ | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 		if (idemodulator(fskd, &fskd->xi1, IGET_SAMPLE)) | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 			return -1; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 		samples++; | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 		for(;;) { | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | search_startbit2: | 
					
						
							| 
									
										
										
										
											2007-08-02 18:32:34 +00:00
										 |  |  | 			if (*len <= 0) { | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 				fskd->state = STATE_SEARCH_STARTBIT2; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 				return 0; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			samples++; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 			if (idemodulator(fskd, &fskd->xi2, IGET_SAMPLE)) | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 				return -1; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 			printf("xi2 = %d ", fskd->xi2); | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 			if (fskd->xi2 < 512) { | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | search_startbit3: | 
					
						
							| 
									
										
										
										
											2006-12-05 19:41:26 +00:00
										 |  |  | 		/* We await for 0.5 bits before using DPLL */ | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 		i = fskd->ispb / 2; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 		if (*len < i) { | 
					
						
							|  |  |  | 			fskd->state = STATE_SEARCH_STARTBIT3; | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 		for (; i > 0; i--) { | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 			if (idemodulator(fskd, &fskd->xi1, IGET_SAMPLE)) | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 				return(-1); | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | #if 0
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 			printf("xi1 = %d ", fskd->xi1); | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 			samples++; | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 		/* x1 must be negative (start bit confirmation) */ | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 	} while (fskd->xi1 > 0); | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 	fskd->state = STATE_GET_BYTE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | getbyte: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-10 20:31:39 +00:00
										 |  |  | 	/* Need at least 80 samples (for 1200) or
 | 
					
						
							|  |  |  | 		1320 (for 45.5) to be sure we'll have a byte */ | 
					
						
							|  |  |  | 	if (fskd->nbit < 8) { | 
					
						
							|  |  |  | 		if (*len < 1320) | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		if (*len < 80) | 
					
						
							|  |  |  | 			return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-03-04 23:04:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-05 19:41:26 +00:00
										 |  |  | 	/* Now we read the data bits */ | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 	j = fskd->nbit; | 
					
						
							|  |  |  | 	for (a = n1 = 0; j; j--) { | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 		olen = *len; | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 		i = get_bit_raw(fskd, buffer, len); | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 		buffer += (olen - *len); | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		if (i == -1) | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 			return -1; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		if (i) | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 			n1++; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		a >>= 1; | 
					
						
							| 
									
										
										
										
											2007-11-06 19:23:10 +00:00
										 |  |  | 		a |= i; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2007-11-06 19:23:10 +00:00
										 |  |  | 	j = 8 - fskd->nbit; | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 	a >>= j; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-05 19:41:26 +00:00
										 |  |  | 	/* We read parity bit (if exists) and check parity */ | 
					
						
							| 
									
										
										
										
											2006-12-05 20:15:37 +00:00
										 |  |  | 	if (fskd->parity) { | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 		olen = *len; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		i = get_bit_raw(fskd, buffer, len); | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 		buffer += (olen - *len); | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		if (i == -1) | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 			return -1; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		if (i) | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 			n1++; | 
					
						
							| 
									
										
										
										
											2006-12-05 20:15:37 +00:00
										 |  |  | 		if (fskd->parity == 1) {	/* parity=1 (even) */ | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 			if (n1 & 1) | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 				a |= 0x100;			/* error */ | 
					
						
							|  |  |  | 		} else {					/* parity=2 (odd) */ | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 			if (!(n1 & 1)) | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 				a |= 0x100;			/* error */ | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-05 19:41:26 +00:00
										 |  |  | 	/* We read STOP bits. All of them must be 1 */ | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-11-06 19:23:10 +00:00
										 |  |  | 	for (j = fskd->instop; j; j--) { | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 		r = get_bit_raw(fskd, buffer, len); | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		if (r == -1) | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 			return -1; | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 		if (!r) | 
					
						
							| 
									
										
										
										
											2006-12-02 13:40:13 +00:00
										 |  |  | 			a |= 0x200; | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 	/* And finally we return
 | 
					
						
							|  |  |  | 	 * Bit 8 : Parity error | 
					
						
							| 
									
										
										
										
											2007-08-06 19:52:40 +00:00
										 |  |  | 	 * Bit 9 : Framming error | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2012-03-22 19:51:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-24 02:45:02 +00:00
										 |  |  | 	*outbyte = a; | 
					
						
							|  |  |  | 	fskd->state = STATE_SEARCH_STARTBIT; | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } |