~davewalker/ubuntu/maverick/asterisk/lp_705014

« back to all changes in this revision

Viewing changes to apps/app_disa.c

  • Committer: Bazaar Package Importer
  • Author(s): Kilian Krause
  • Date: 2005-03-09 22:09:05 UTC
  • mto: (1.2.1 upstream) (8.2.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050309220905-9afy6hcpw96xbr6j
Tags: upstream-1.0.6
ImportĀ upstreamĀ versionĀ 1.0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
#include <asterisk/file.h>
18
18
#include <asterisk/logger.h>
19
19
#include <asterisk/channel.h>
 
20
#include <asterisk/indications.h>
20
21
#include <asterisk/pbx.h>
21
22
#include <asterisk/module.h>
22
23
#include <asterisk/translate.h>
25
26
#include <stdlib.h>
26
27
#include <stdio.h>
27
28
#include <math.h>
28
 
#include <pthread.h>
29
29
#include <sys/time.h>
30
30
 
31
 
/*
32
 
#define TONE_BLOCK_SIZE 320
33
 
*/
34
31
 
35
32
static char *tdesc = "DISA (Direct Inward System Access) Application";
36
33
 
83
80
 
84
81
LOCAL_USER_DECL;
85
82
 
86
 
static float loudness=4096.0;
87
 
 
88
83
static int firstdigittimeout = 20000; /* 20 seconds first digit timeout */
89
84
static int digittimeout = 10000; /* 10 seconds subsequent digit timeout */
90
85
 
91
 
static void make_tone_block(unsigned char *data, float f1, float f2, int len, int *x)
92
 
{
93
 
int     i;
94
 
float   val;
95
 
 
96
 
        for(i = 0; i < len; i++)
97
 
        {
98
 
                val = loudness * sin((f1 * 2.0 * M_PI * (*x))/8000.0);
99
 
                val += loudness * sin((f2 * 2.0 * M_PI * (*x)++)/8000.0);
100
 
                data[i] = AST_LIN2MU((int)val);
101
 
         }              
102
 
          /* wrap back around from 8000 */
103
 
        if (*x >= 8000) *x = 0;
104
 
        return;
105
 
}
106
 
 
107
86
static int ms_diff(struct timeval *tv1, struct timeval *tv2)
108
87
{
109
88
int     ms;
113
92
        return(ms);
114
93
}
115
94
 
 
95
static void play_dialtone(struct ast_channel *chan)
 
96
{
 
97
        const struct tone_zone_sound *ts = NULL;
 
98
        ts = ast_get_indication_tone(chan->zone, "dial");
 
99
        if (ts)
 
100
                ast_playtones_start(chan, 0, ts->data, 0);
 
101
        else
 
102
                ast_tonepair_start(chan, 350, 440, 0, 0);
 
103
}
 
104
 
116
105
static int disa_exec(struct ast_channel *chan, void *data)
117
106
{
118
 
        int i,j,k,x;
 
107
        int i,j,k,x,did_ignore;
119
108
        struct localuser *u;
120
 
        char tmp[256],arg2[256],exten[AST_MAX_EXTENSION],acctcode[20];
121
 
        struct {
122
 
                unsigned char offset[AST_FRIENDLY_OFFSET];
123
 
                unsigned char buf[640];
124
 
        } tone_block;
 
109
        char tmp[256],arg2[256]="",exten[AST_MAX_EXTENSION],acctcode[20]="";
125
110
        char *ourcontext,*ourcallerid;
126
 
        struct ast_frame *f,wf;
 
111
        struct ast_frame *f;
127
112
        struct timeval lastout, now, lastdigittime;
128
113
        int res;
 
114
        time_t rstart;
129
115
        FILE *fp;
130
116
        char *stringp=NULL;
131
117
 
150
136
        ourcontext = strsep(&stringp, "|");
151
137
        /* if context specified, save 2nd arg and parse third */
152
138
        if (ourcontext) {
153
 
                strcpy(arg2,ourcontext);
 
139
                strncpy(arg2,ourcontext, sizeof(arg2) - 1);
154
140
                ourcallerid = strsep(&stringp,"|");
155
141
        }
156
142
          /* if context not specified, use "disa" */
166
152
                ast_answer(chan);
167
153
        }
168
154
        i = k = x = 0; /* k is 0 for pswd entry, 1 for ext entry */
 
155
        did_ignore = 0;
169
156
        exten[0] = 0;
170
157
        acctcode[0] = 0;
171
158
        /* can we access DISA without password? */ 
 
159
 
 
160
        ast_log(LOG_DEBUG, "Context: %s\n",ourcontext);
 
161
 
172
162
        if (!strcasecmp(tmp, "no-password"))
173
163
        {;
174
164
                k = 1;
175
165
                ast_log(LOG_DEBUG, "DISA no-password login success\n");
176
166
        }
177
167
        gettimeofday(&lastdigittime,NULL);
 
168
 
 
169
        play_dialtone(chan);
 
170
 
178
171
        for(;;)
179
172
        {
180
173
                gettimeofday(&now,NULL);
205
198
                        return -1;
206
199
                }
207
200
                if (f->frametype == AST_FRAME_VOICE) {
208
 
                        if (!i || (ast_ignore_pattern(ourcontext, exten) && k)) {
209
 
                                wf.frametype = AST_FRAME_VOICE;
210
 
                                wf.subclass = AST_FORMAT_ULAW;
211
 
                                wf.offset = AST_FRIENDLY_OFFSET;
212
 
                                wf.mallocd = 0;
213
 
                                wf.data = tone_block.buf;
214
 
                                wf.datalen = f->datalen;
215
 
                                make_tone_block(tone_block.buf, 350, 440, f->datalen, &x);
216
 
                                wf.samples = wf.datalen;
217
 
                                ast_frfree(f);
218
 
                            if (ast_write(chan, &wf)) 
219
 
                                {
220
 
                                ast_log(LOG_WARNING, "DISA Failed to write frame on %s\n",chan->name);
221
 
                                        LOCAL_USER_REMOVE(u);
222
 
                                        return -1;
223
 
                                }
224
 
                        } else
225
 
                                ast_frfree(f);
 
201
                        ast_frfree(f);
226
202
                        continue;
227
203
                }
228
204
                  /* if not DTMF, just do it again */
234
210
 
235
211
                j = f->subclass;  /* save digit */
236
212
                ast_frfree(f);
237
 
                
 
213
                if (i == 0) 
 
214
                        ast_playtones_stop(chan);
 
215
 
238
216
                gettimeofday(&lastdigittime,NULL);
239
217
                  /* got a DTMF tone */
240
218
                if (i < AST_MAX_EXTENSION) /* if still valid number of digits */
287
265
 
288
266
                                        }
289
267
                                         /* password good, set to dial state */
290
 
                                                ast_log(LOG_WARNING,"DISA on chan %s password is good\n",chan->name);
 
268
                                        ast_log(LOG_DEBUG,"DISA on chan %s password is good\n",chan->name);
 
269
                                        play_dialtone(chan);
 
270
 
291
271
                                        k = 1;
292
272
                                        i = 0;  /* re-set buffer pointer */
293
273
                                        exten[sizeof(acctcode)] = 0;
294
 
                                        strcpy(acctcode,exten);
 
274
                                        strncpy(acctcode,exten, sizeof(acctcode) - 1);
295
275
                                        exten[0] = 0;
296
276
                                        ast_log(LOG_DEBUG,"Successful DISA log-in on chan %s\n",chan->name);
297
277
                                        continue;
298
278
                                }
299
279
                        }
 
280
 
300
281
                        exten[i++] = j;  /* save digit */
301
282
                        exten[i] = 0;
302
283
                        if (!k) continue; /* if getting password, continue doing it */
303
284
                          /* if this exists */
304
285
 
 
286
                        if (ast_ignore_pattern(ourcontext, exten)) {
 
287
                                play_dialtone(chan);
 
288
                                did_ignore = 1;
 
289
                        } else
 
290
                                if (did_ignore) {
 
291
                                        ast_playtones_stop(chan);
 
292
                                        did_ignore = 0;
 
293
                                }
 
294
 
305
295
                          /* if can do some more, do it */
306
 
                        if (!ast_matchmore_extension(chan,ourcontext,exten,1, chan->callerid)) 
 
296
                        if (!ast_matchmore_extension(chan,ourcontext,exten,1, chan->callerid)) {
307
297
                                break;
 
298
                        }
308
299
                }
309
300
        }
310
301
 
311
302
        if (k && ast_exists_extension(chan,ourcontext,exten,1, chan->callerid))
312
303
        {
 
304
                ast_playtones_stop(chan);
313
305
                /* We're authenticated and have a valid extension */
314
306
                if (ourcallerid && *ourcallerid)
315
307
                {
316
308
                        if (chan->callerid) free(chan->callerid);
317
309
                        chan->callerid = strdup(ourcallerid);
318
310
                }
319
 
                strcpy(chan->exten,exten);
320
 
                strcpy(chan->context,ourcontext);
321
 
                strcpy(chan->accountcode,acctcode);
 
311
                strncpy(chan->exten, exten, sizeof(chan->exten) - 1);
 
312
                strncpy(chan->context, ourcontext, sizeof(chan->context) - 1);
 
313
                strncpy(chan->accountcode, acctcode, sizeof(chan->accountcode) - 1);
322
314
                chan->priority = 0;
323
315
                ast_cdr_init(chan->cdr,chan);
324
316
                LOCAL_USER_REMOVE(u);
326
318
        }
327
319
 
328
320
reorder:
329
 
                
330
 
        /* something is invalid, give em reorder forever */
331
 
        x = 0;
332
 
        k = 0;  /* k = 0 means busy tone, k = 1 means silence) */
333
 
        i = 0;  /* Number of samples we've done */
334
 
        for(;;)
 
321
 
 
322
        ast_indicate(chan,AST_CONTROL_CONGESTION);
 
323
        /* something is invalid, give em reorder for several seconds */
 
324
        time(&rstart);
 
325
        while(time(NULL) < rstart + 10)
335
326
        {
336
327
                if (ast_waitfor(chan, -1) < 0)
337
328
                        break;
338
329
                f = ast_read(chan);
339
330
                if (!f)
340
331
                        break;
341
 
                if (f->frametype == AST_FRAME_VOICE) {
342
 
                        wf.frametype = AST_FRAME_VOICE;
343
 
                        wf.subclass = AST_FORMAT_ULAW;
344
 
                        wf.offset = AST_FRIENDLY_OFFSET;
345
 
                        wf.mallocd = 0;
346
 
                        wf.data = tone_block.buf;
347
 
                        wf.datalen = f->datalen;
348
 
                        wf.samples = wf.datalen;
349
 
                        if (k) 
350
 
                                memset(tone_block.buf, 0x7f, wf.datalen);
351
 
                        else
352
 
                                make_tone_block(tone_block.buf,480.0, 620.0,wf.datalen, &x);
353
 
                        i += wf.datalen / 8;
354
 
                        if (i > 250) {
355
 
                                i = 0;
356
 
                                k = !k;
357
 
                        }
358
 
                    if (ast_write(chan, &wf)) 
359
 
                        {
360
 
                        ast_log(LOG_WARNING, "DISA Failed to write frame on %s\n",chan->name);
361
 
                                LOCAL_USER_REMOVE(u);
362
 
                                return -1;
363
 
                        }
364
 
                }
365
332
                ast_frfree(f);
366
333
        }
 
334
        ast_playtones_stop(chan);
367
335
        LOCAL_USER_REMOVE(u);
368
336
        return -1;
369
337
}