Terry found this problem with running the expr2 parser on OSX. Make the #defines...
[asterisk/asterisk.git] / main / fskmodem.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  * 
8  * Includes code and algorithms from the Zapata library.
9  *
10  * See http://www.asterisk.org for more information about
11  * the Asterisk project. Please do not directly contact
12  * any of the maintainers of this project for assistance;
13  * the project provides a web site, mailing lists and IRC
14  * channels for your use.
15  *
16  * This program is free software, distributed under the terms of
17  * the GNU General Public License Version 2. See the LICENSE file
18  * at the top of the source tree.
19  */
20
21 /*! \file
22  *
23  * \brief FSK Modulator/Demodulator 
24  *
25  * \author Mark Spencer <markster@digium.com>
26  *
27  * \arg Includes code and algorithms from the Zapata library.
28  *
29  */
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/fskmodem.h"
36
37 #define NBW     2
38 #define BWLIST  {75,800}
39 #define NF      6
40 #define FLIST {1400,1800,1200,2200,1300,2100}
41
42 #define STATE_SEARCH_STARTBIT   0
43 #define STATE_SEARCH_STARTBIT2  1
44 #define STATE_SEARCH_STARTBIT3  2
45 #define STATE_GET_BYTE                  3
46
47 static inline int iget_sample(short **buffer, int *len)
48 {
49         int retval;
50         retval = (int) **buffer;
51         (*buffer)++;
52         (*len)--;
53         return retval;
54 }
55
56 #define IGET_SAMPLE iget_sample(&buffer, len)
57 /*! \brief Coefficients for input filters
58  * Coefficients table, generated by program "mkfilter"  
59  * mkfilter is part of the zapatatelephony.org distribution
60  * Format: coef[IDX_FREC][IDX_BW][IDX_COEF]
61  * IDX_COEF = 0 =>      1/GAIN          
62  * IDX_COEF = 1-6       =>      Coefficientes y[n]                      
63 */
64 static double coef_in[NF][NBW][8]={
65         {  { 1.8229206611e-04,-7.8997325866e-01,2.2401819940e+00,-4.6751353581e+00,5.5080745712e+00,-5.0571565772e+00,2.6215820004e+00,0.0000000000e+00,
66         },  { 9.8532175289e-02,-5.6297236492e-02,3.3146713415e-01,-9.2239200436e-01,1.4844365184e+00,-2.0183258642e+00,2.0074154497e+00,0.0000000000e+00,
67         },  },  {  { 1.8229206610e-04,-7.8997325866e-01,7.7191410839e-01,-2.8075643964e+00,1.6948618347e+00,-3.0367273700e+00,9.0333559408e-01,0.0000000000e+00,
68         },  { 9.8531161839e-02,-5.6297236492e-02,1.1421579050e-01,-4.8122536483e-01,4.0121072432e-01,-7.4834487567e-01,6.9170822332e-01,0.0000000000e+00,
69         },  },  {  { 1.8229206611e-04,-7.8997325866e-01,2.9003821430e+00,-6.1082779024e+00,7.7169345751e+00,-6.6075999680e+00,3.3941838836e+00,0.0000000000e+00,
70         },  { 9.8539686961e-02,-5.6297236492e-02,4.2915323820e-01,-1.2609358633e+00,2.2399213250e+00,-2.9928879142e+00,2.5990173742e+00,0.0000000000e+00,
71         },  },  {  { 1.8229206610e-04,-7.8997325866e-01,-7.7191410839e-01,-2.8075643964e+00,-1.6948618347e+00,-3.0367273700e+00,-9.0333559408e-01,0.0000000000e+00,
72         },  { 9.8531161839e-02,-5.6297236492e-02,-1.1421579050e-01,-4.8122536483e-01,-4.0121072432e-01,-7.4834487567e-01,-6.9170822332e-01,0.0000000000e+00,
73         },  },  {  { 1.8229206611e-04,-7.8997325866e-01,2.5782298908e+00,-5.3629717478e+00,6.5890882172e+00,-5.8012914776e+00,3.0171839130e+00,0.0000000000e+00,
74         },  { 9.8534230718e-02,-5.6297236492e-02,3.8148618075e-01,-1.0848760410e+00,1.8441165168e+00,-2.4860666655e+00,2.3103384142e+00,0.0000000000e+00,
75         },  },  {  { 1.8229206610e-04,-7.8997325866e-01,-3.8715051001e-01,-2.6192408538e+00,-8.3977994034e-01,-2.8329897913e+00,-4.5306444352e-01,0.0000000000e+00,
76         },  { 9.8531160936e-02,-5.6297236492e-02,-5.7284484199e-02,-4.3673866734e-01,-1.9564766257e-01,-6.2028156584e-01,-3.4692356122e-01,0.0000000000e+00,
77         },  }, 
78 };
79
80 /*! \brief Coefficients for output filter
81  * Coefficients table, generated by program "mkfilter"
82  * Format: coef[IDX_BW][IDX_COEF]       
83  * IDX_COEF = 0 =>      1/GAIN  
84  * IDX_COEF = 1-6       =>      Coefficientes y[n]
85 */
86 static double coef_out[NBW][8]={
87         { 1.3868644653e-08,-6.3283665042e-01,4.0895057217e+00,-1.1020074592e+01,1.5850766191e+01,-1.2835109292e+01,5.5477477340e+00,0.0000000000e+00,
88         },  { 3.1262119724e-03,-7.8390522307e-03,8.5209627801e-02,-4.0804129163e-01,1.1157139955e+00,-1.8767603680e+00,1.8916395224e+00,0.0000000000e+00 
89         }, 
90 };
91
92 /*! Integer Pass Band demodulator filter  */
93 static inline int ibpdfilter(struct filter_struct * fs, int in)
94 {
95         int i,j;
96         int s;
97         int64_t s_interim;
98         
99         /* integer filter */
100         s =  in * fs->icoefs[0];
101         fs->ixv[(fs->ip+6)&7] = s;
102         
103         s=     (fs->ixv[fs->ip]       + fs->ixv[(fs->ip+6)&7]) +
104                 6  * (fs->ixv[(fs->ip+1)&7] + fs->ixv[(fs->ip+5)&7]) +
105                 15 * (fs->ixv[(fs->ip+2)&7] + fs->ixv[(fs->ip+4)&7]) +
106                 20 *  fs->ixv[(fs->ip+3)&7];
107         
108         for (i=1, j=fs->ip; i < 7; i++,j++) {
109                 /* Promote operation to 64 bit to prevent overflow that occurred in 32 bit) */
110                 s_interim = (int64_t)(fs->iyv[j & 7]) * 
111                                 (int64_t)(fs->icoefs[i]) /
112                                 (int64_t)(1024);
113                 s += (int) s_interim;
114         }
115         fs->iyv[ j & 7] = s;
116         fs->ip++;
117         fs->ip &= 7;
118         return s;
119 }
120
121 /*! Integer Band Pass filter */
122 static inline int ibpfilter(struct filter_struct * fs, int in)
123 {
124         int i, j;
125         int s;
126         int64_t s_interim;
127         
128         /* integer filter */
129         s =  in * fs->icoefs[0] / 256;
130         fs->ixv[(fs->ip+6) & 7] = s;
131         
132         s = (fs->ixv[(fs->ip+6) & 7] - fs->ixv[fs->ip])
133                 + 3 * (fs->ixv[(fs->ip+2) & 7] - fs->ixv[(fs->ip+4) & 7]);
134         
135         for (i=1,j=fs->ip; i < 7; i++,j++) { 
136                 s_interim = (int64_t)(fs->iyv[j&7]) * 
137                                 (int64_t)(fs->icoefs[i]) / 
138                                 (int64_t)(256);
139                 s+= (int) s_interim;
140         }
141         fs->iyv[j&7]=s;
142         fs->ip++; fs->ip &= 7;
143         return s;
144 }
145
146 static inline int idemodulator(fsk_data *fskd, int *retval, int x)
147 {
148         int is, im, id;
149         int ilin2;
150  
151         is = ibpfilter(&fskd->space_filter, x);
152         im = ibpfilter(&fskd->mark_filter, x);
153         
154         ilin2 = ((im * im) - (is * is))/(256 * 256);
155         
156         id = ibpdfilter(&fskd->demod_filter, ilin2);
157         
158         *retval = id;
159         return 0;
160 }
161
162 static int get_bit_raw(fsk_data *fskd, short *buffer, int *len)
163 {
164         /* This function implements a DPLL to synchronize with the bits */
165         int f;
166
167         int ix;
168         /* PLL coeffs are set up in callerid_new */
169         for (f = 0;;){
170                 if (idemodulator(fskd, &ix, IGET_SAMPLE)) return(-1);
171                 if ((ix * fskd->xi0)<0) {       /* Transicion */
172                         if (!f) {
173                                 if (fskd->icont<(fskd->pllispb2)) 
174                                         fskd->icont+=fskd->pllids; 
175                                 else 
176                                         fskd->icont-=fskd->pllids;
177                                 f = 1;
178                         }
179                 }
180                 fskd->xi0=ix;
181                 fskd->icont+=32;
182                 if (fskd->icont>fskd->pllispb) {
183                         fskd->icont-=fskd->pllispb;
184                         break;
185                 }
186         }
187         f=(ix>0)?0x80:0;
188         return f;
189 }
190
191 int fskmodem_init(fsk_data *fskd)
192 {
193         int i;
194
195         fskd->space_filter.ip  = 0;
196         fskd->mark_filter.ip   = 0;
197         fskd->demod_filter.ip  = 0;
198
199         for ( i = 0 ; i < 7 ; i++ ) {
200                 fskd->space_filter.icoefs[i] = 
201                         coef_in[fskd->f_space_idx][fskd->bw][i] * 256;
202                 fskd->space_filter.ixv[i] = 0;;
203                 fskd->space_filter.iyv[i] = 0;;
204
205                 fskd->mark_filter.icoefs[i] = 
206                         coef_in[fskd->f_mark_idx][fskd->bw][i] * 256;
207                 fskd->mark_filter.ixv[i] = 0;;
208                 fskd->mark_filter.iyv[i] = 0;;
209
210                 fskd->demod_filter.icoefs[i] = 
211                         coef_out[fskd->bw][i] * 1024;
212                 fskd->demod_filter.ixv[i] = 0;;
213                 fskd->demod_filter.iyv[i] = 0;;
214         }
215         return 0;
216 }
217
218 int fsk_serial(fsk_data *fskd, short *buffer, int *len, int *outbyte)
219 {
220         int a;
221         int i, j, n1, r;
222         int samples = 0;
223         int olen;
224         int beginlen = *len;
225         int beginlenx;
226         
227         switch (fskd->state) {
228                 /* Pick up where we left off */
229         case STATE_SEARCH_STARTBIT2:
230                 goto search_startbit2;
231         case STATE_SEARCH_STARTBIT3:
232                 goto search_startbit3;
233         case STATE_GET_BYTE:
234                 goto getbyte;
235         }
236         /* We await for start bit       */
237         do {
238                 /* this was jesus's nice, reasonable, working (at least with RTTY) code
239                 to look for the beginning of the start bit. Unfortunately, since TTY/TDD's
240                 just start sending a start bit with nothing preceding it at the beginning
241                 of a transmission (what a LOSING design), we cant do it this elegantly */
242                 /* NOT USED
243                                 if (demodulator(zap,&x1)) 
244                                         return -1;
245                                 for(;;) {
246                                         if (demodulator(zap,&x2)) 
247                                                 return -1;
248                                         if (x1>0 && x2<0) break;
249                                         x1=x2;
250                                 }
251                 */
252                 /* this is now the imprecise, losing, but functional code to detect the
253                 beginning of a start bit in the TDD sceanario. It just looks for sufficient
254                 level to maybe, perhaps, guess, maybe that its maybe the beginning of
255                 a start bit, perhaps. This whole thing stinks! */
256                 beginlenx=beginlen; /* just to avoid unused war warnings */
257                 if (idemodulator(fskd, &fskd->xi1, IGET_SAMPLE))
258                         return -1;
259                 samples++;
260                 for(;;) {
261 search_startbit2:                  
262                         if (*len <= 0) {
263                                 fskd->state = STATE_SEARCH_STARTBIT2;
264                                 return 0;
265                         }
266                         samples++;
267                         if (idemodulator(fskd,&fskd->xi2,IGET_SAMPLE)) 
268                                 return -1;
269 #if 0
270                         printf("xi2 = %d ", fskd->xi2);
271 #endif                  
272                         if (fskd->xi2 < 512) 
273                                 break; 
274                 }
275 search_startbit3:                  
276                 /* We await for 0.5 bits before using DPLL */
277                 i=fskd->ispb/2;
278                 if (*len < i) {
279                         fskd->state = STATE_SEARCH_STARTBIT3;
280                         return 0;
281                 }
282                 for (; i>0; i--) {
283                         if (idemodulator(fskd, &fskd->xi1, IGET_SAMPLE))
284                                 return(-1); 
285 #if 0
286                         printf("xi1 = %d ", fskd->xi1);
287 #endif                  
288                         samples++; 
289                 }
290
291                 /* x1 must be negative (start bit confirmation) */
292
293         } while (fskd->xi1>0);
294         fskd->state = STATE_GET_BYTE;
295
296 getbyte:
297
298         /* Need at least 80 samples (for 1200) or
299                 1320 (for 45.5) to be sure we'll have a byte */
300         if (fskd->nbit < 8) {
301                 if (*len < 1320)
302                         return 0;
303         } else {
304                 if (*len < 80)
305                         return 0;
306         }
307         
308         /* Now we read the data bits */
309         j = fskd->nbit;
310         for (a = n1 = 0; j; j--) {
311                 olen = *len;
312                 i = get_bit_raw(fskd, buffer, len);
313                 buffer += (olen - *len);
314                 if (i == -1) 
315                         return -1;
316                 if (i) 
317                         n1++;
318                 a >>= 1; 
319                 a |= i;
320         }
321         j = 8 - fskd->nbit;
322         a >>= j;
323
324         /* We read parity bit (if exists) and check parity */
325         if (fskd->parity) {
326                 olen = *len;
327                 i = get_bit_raw(fskd, buffer, len); 
328                 buffer += (olen - *len);
329                 if (i == -1) 
330                         return -1;
331                 if (i) 
332                         n1++;
333                 if (fskd->parity == 1) {        /* parity=1 (even) */
334                         if (n1 & 1) 
335                                 a |= 0x100;                     /* error */
336                 } else {                                        /* parity=2 (odd) */
337                         if (!(n1 & 1)) 
338                                 a |= 0x100;                     /* error */
339                 }
340         }
341         
342         /* We read STOP bits. All of them must be 1 */
343         
344         for (j = fskd->instop; j; j--) {
345                 r = get_bit_raw(fskd, buffer, len);
346                 if (r == -1) 
347                         return -1;
348                 if (!r) 
349                         a |= 0x200;
350         }
351
352         /* And finally we return  
353          * Bit 8 : Parity error 
354          * Bit 9 : Framming error
355         */
356                 
357         *outbyte = a;
358         fskd->state = STATE_SEARCH_STARTBIT;
359         return 1;
360 }