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