handle going to 'fax' extension when inside a macro properly (bug #4368)
[asterisk/asterisk.git] / channels / chan_modem_i4l.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * ISDN4Linux TTY Driver
5  * 
6  * Copyright (C) 2001, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15
16 #include <string.h>
17 #include <stdlib.h>
18 #include <errno.h>
19 #include <unistd.h>
20 #include <sys/ioctl.h>
21 #include "asterisk/lock.h"
22 #include "asterisk/vmodem.h"
23 #include "asterisk/module.h"
24 #include "asterisk/frame.h"
25 #include "asterisk/logger.h"
26 #include "asterisk/options.h"
27 #include "asterisk/dsp.h"
28 #include "asterisk/callerid.h"
29 #include "asterisk/ulaw.h"
30 #include "asterisk/pbx.h"
31
32 #define STATE_COMMAND   0
33 #define STATE_VOICE     1
34
35 static char *breakcmd = "\0x10\0x14\0x10\0x3";
36
37 static char *desc = "ISDN4Linux Emulated Modem Driver";
38
39 static int usecnt;
40 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
41
42 static char *i4l_idents[] = {
43         /* Identify ISDN4Linux Driver */
44         "Linux ISDN",
45         NULL
46 };
47
48 static int i4l_setdev(struct ast_modem_pvt *p, int dev)
49 {
50         char cmd[80];
51         if ((dev != MODEM_DEV_TELCO) && (dev != MODEM_DEV_TELCO_SPK)) {
52                 ast_log(LOG_WARNING, "ISDN4Linux only supports telco device, not %d.\n", dev);
53                 return -1;
54         } else  /* Convert DEV to our understanding of it */
55                 dev = 2;
56         if (ast_modem_send(p, "AT+VLS?", 0)) {
57                 ast_log(LOG_WARNING, "Unable to select current mode %d\n", dev);
58                 return -1;
59         }
60         if (ast_modem_read_response(p, 5)) {
61                 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
62                 return -1;
63         }
64         ast_modem_trim(p->response);
65         strncpy(cmd, p->response, sizeof(cmd)-1);
66         if (ast_modem_expect(p, "OK", 5)) {
67                 ast_log(LOG_WARNING, "Modem did not respond properly\n");
68                 return -1;
69         }
70         if (dev == atoi(cmd)) {
71                 /* We're already in the right mode, don't bother changing for fear of
72                    hanging up */
73                 return 0;
74         }
75         snprintf(cmd, sizeof(cmd), "AT+VLS=%d", dev);
76         if (ast_modem_send(p, cmd, 0))  {
77                 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
78                 return -1;
79         }
80         if (ast_modem_read_response(p, 5)) {
81                 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
82                 return -1;
83         }
84         ast_modem_trim(p->response);
85         if (strcasecmp(p->response, "VCON") && strcasecmp(p->response, "OK")) {
86                 ast_log(LOG_WARNING, "Unexpected reply: %s\n", p->response);
87                 return -1;
88         }
89         return 0;
90 }
91
92 static int i4l_startrec(struct ast_modem_pvt *p)
93 {
94         if (ast_modem_send(p, "AT+VRX+VTX", 0) ||
95              ast_modem_expect(p, "CONNECT", 5)) {
96                 ast_log(LOG_WARNING, "Unable to start recording\n");
97                 return -1;
98         }
99         p->ministate = STATE_VOICE;
100         
101         /*  let ast dsp detect dtmf */
102         if (p->dtmfmode & MODEM_DTMF_AST) {
103                 if (p->dsp) {
104                         ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", p->dev);
105                 } else {
106                         p->dsp = ast_dsp_new();
107                         if (p->dsp) {
108                                 ast_log(LOG_DEBUG, "Detecting DTMF inband with sw DSP on %s\n",p->dev);
109                                 ast_dsp_set_features(p->dsp, DSP_FEATURE_DTMF_DETECT|DSP_FEATURE_FAX_DETECT);
110                                 ast_dsp_digitmode(p->dsp, DSP_DIGITMODE_DTMF | 0);
111                         }
112                 }
113         }
114
115         return 0;
116 }
117
118 static int i4l_break(struct ast_modem_pvt *p)
119 {
120         if (ast_modem_send(p, breakcmd, 2)) {
121                 ast_log(LOG_WARNING, "Failed to break\n");
122                 return -1;
123         }
124         if (ast_modem_send(p, "\r\n", 2)) {
125                 ast_log(LOG_WARNING, "Failed to send enter?\n");
126                 return -1;
127         }
128 #if 0
129         /* Read any outstanding junk */
130         while(!ast_modem_read_response(p, 1));
131 #endif
132         if (ast_modem_send(p, "AT", 0)) {
133                 /* Modem might be stuck in some weird mode, try to get it out */
134                 ast_modem_send(p, "+++", 3);
135                 if (ast_modem_expect(p, "OK", 10)) {
136                         ast_log(LOG_WARNING, "Modem is not responding\n");
137                         return -1;
138                 }
139                 if (ast_modem_send(p, "AT", 0)) {
140                         ast_log(LOG_WARNING, "Modem is not responding\n");
141                         return -1;
142                 }
143         }
144         if (ast_modem_expect(p, "OK", 5)) {
145                 ast_log(LOG_WARNING, "Modem did not respond properly\n");
146                 return -1;
147         }
148         return 0;
149 }
150
151 static int i4l_init(struct ast_modem_pvt *p)
152 {
153         char cmd[256];
154         if (option_debug)
155                 ast_log(LOG_DEBUG, "i4l_init()\n");
156         if (i4l_break(p))
157                 return -1;
158         /* Force into command mode */
159         p->ministate = STATE_COMMAND;
160         if (ast_modem_send(p, "AT+FCLASS=8", 0) ||
161              ast_modem_expect(p, "OK", 5)) {
162                 ast_log(LOG_WARNING, "Unable to set to voice mode\n");
163                 return -1;
164         }
165         if (strlen(p->msn)) {
166                 snprintf(cmd, sizeof(cmd), "AT&E%s", p->msn);
167                 if (ast_modem_send(p, cmd, 0) ||
168                     ast_modem_expect(p, "OK", 5)) {
169                         ast_log(LOG_WARNING, "Unable to set MSN to %s\n", p->msn);
170                         return -1;
171                 }
172         }
173         if (strlen(p->incomingmsn)) {
174                 char *q;
175                 snprintf(cmd, sizeof(cmd), "AT&L%s", p->incomingmsn);
176                 /* translate , into ; since that is the seperator I4L uses, but can't be directly */
177                 /* put in the config file because it will interpret the rest of the line as comment. */
178                 q = cmd+4;
179                 while (*q) {
180                         if (*q == ',') *q = ';';
181                         ++q;
182                 }
183                 if (ast_modem_send(p, cmd, 0) ||
184                     ast_modem_expect(p, "OK", 5)) {
185                         ast_log(LOG_WARNING, "Unable to set Listen to %s\n", p->msn);
186                         return -1;
187                 }
188         }
189         if (ast_modem_send(p, "AT&D2", 0) ||
190              ast_modem_expect(p, "OK", 5)) {
191                 ast_log(LOG_WARNING, "Unable to set to DTR disconnect mode\n");
192                 return -1;
193         }
194         if (ast_modem_send(p, "ATS18=1", 0) ||
195              ast_modem_expect(p, "OK", 5)) {
196                 ast_log(LOG_WARNING, "Unable to set to audio only mode\n");
197                 return -1;
198         }
199         if (ast_modem_send(p, "ATS13.6=1", 0) ||
200              ast_modem_expect(p, "OK", 5)) {
201                 ast_log(LOG_WARNING, "Unable to set to RUNG indication\n");
202                 return -1;
203         }
204         if (ast_modem_send(p, "ATS14=4", 0) ||
205              ast_modem_expect(p, "OK", 5)) {
206                 ast_log(LOG_WARNING, "Unable to set to transparent mode\n");
207                 return -1;
208         }
209         if (ast_modem_send(p, "ATS23=9", 0) ||
210              ast_modem_expect(p, "OK", 5)) {
211                 ast_log(LOG_WARNING, "Unable to set to transparent/ringing mode\n");
212                 return -1;
213         }
214
215         if (ast_modem_send(p, "AT+VSM=6", 0) ||
216              ast_modem_expect(p, "OK", 5)) {
217                 ast_log(LOG_WARNING, "Unable to set to muLAW mode\n");
218                 return -1;
219         }
220         if (ast_modem_send(p, "AT+VLS=2", 0) ||
221              ast_modem_expect(p, "OK", 5)) {
222                 ast_log(LOG_WARNING, "Unable to set to phone line interface\n");
223                 return -1;
224         }
225         p->escape = 0;
226         return 0;
227 }
228
229 static struct ast_frame *i4l_handle_escape(struct ast_modem_pvt *p, char esc)
230 {
231         /* Handle escaped characters -- but sometimes we call it directly as 
232            a quick way to cause known responses */
233         p->fr.frametype = AST_FRAME_NULL;
234         p->fr.subclass = 0;
235         p->fr.data = NULL;
236         p->fr.datalen = 0;
237         p->fr.samples = 0;
238         p->fr.offset = 0;
239         p->fr.mallocd = 0;
240         p->fr.delivery.tv_sec = 0;
241         p->fr.delivery.tv_usec = 0;
242         if (esc && option_debug)
243                 ast_log(LOG_DEBUG, "Escaped character '%c'\n", esc);
244         
245         switch(esc) {
246         case 'R': /* Pseudo ring */
247                 p->fr.frametype = AST_FRAME_CONTROL;
248                 p->fr.subclass = AST_CONTROL_RING;
249                 return &p->fr;
250         case 'I': /* Pseudo ringing */
251                 p->fr.frametype = AST_FRAME_CONTROL;
252                 p->fr.subclass =  AST_CONTROL_RINGING;
253                 return &p->fr;
254         case 'X': /* Pseudo connect */
255                 p->fr.frametype = AST_FRAME_CONTROL;
256                 p->fr.subclass = AST_CONTROL_ANSWER;
257                 if (p->owner)
258                         ast_setstate(p->owner, AST_STATE_UP);
259                 if (i4l_startrec(p))
260                         return  NULL;
261                 return &p->fr;
262         case 'b': /* Busy signal */
263                 p->fr.frametype = AST_FRAME_CONTROL;
264                 p->fr.subclass = AST_CONTROL_BUSY;
265                 return &p->fr;
266         case 'o': /* Overrun */
267                 ast_log(LOG_WARNING, "Overflow on modem, flushing buffers\n");
268                 if (ast_modem_send(p, "\0x10E", 2)) 
269                         ast_log(LOG_WARNING, "Unable to flush buffers\n");
270                 return &p->fr;  
271         case CHAR_ETX: /* End Transmission */
272                 return NULL;
273         case 'u': /* Underrun */
274                 ast_log(LOG_WARNING, "Data underrun\n");
275                 /* Fall Through */
276         case 'd': /* Dialtone */
277         case 'c': /* Calling Tone */
278         case 'e': /* European version */
279         case 'a': /* Answer Tone */
280         case 'f': /* Bell Answer Tone */
281         case 'T': /* Timing mark */
282         case 't': /* Handset off hook */
283         case 'h': /* Handset hungup */
284                 /* Ignore */
285                 if (option_debug)
286                         ast_log(LOG_DEBUG, "Ignoring Escaped character '%c' (%d)\n", esc, esc);
287                 return &p->fr;
288         case '0':
289         case '1':
290         case '2':
291         case '3':
292         case '4':
293         case '5':
294         case '6':
295         case '7':
296         case '8':
297         case '9':
298         case '*':
299         case '#':
300                 ast_log(LOG_DEBUG, "Detected outband DTMF digit: '%c' (%d)\n", esc, esc);
301                 p->fr.frametype=AST_FRAME_DTMF;
302                 p->fr.subclass=esc;
303                 return &p->fr;
304         case 0: /* Pseudo signal */
305                 return &p->fr;
306         default:
307                 ast_log(LOG_DEBUG, "Unknown Escaped character '%c' (%d)\n", esc, esc);
308         }
309         return &p->fr;
310 }
311
312 static struct ast_frame *i4l_read(struct ast_modem_pvt *p)
313 {
314         unsigned char result[256];
315         short *b;
316         struct ast_frame *f=NULL;
317         int res;
318         int x;
319         if (p->ministate == STATE_COMMAND) {
320                 /* Read the first two bytes, first, in case it's a control message */
321                 res = read(p->fd, result, 2);
322                 if (res < 2) {
323                         /* short read, means there was a hangup? */
324                         /* (or is this also possible without hangup?) */
325                         /* Anyway, reading from unitialized buffers is a bad idea anytime. */
326                         if (errno == EAGAIN)
327                                 return i4l_handle_escape(p, 0);
328                         return NULL;
329                 }
330                 if (result[0] == CHAR_DLE) {
331                         return i4l_handle_escape(p, result[1]);
332                         
333                 } else {
334                         if ((result[0] == '\n') || (result[0] == '\r'))
335                                 return i4l_handle_escape(p, 0);
336                         /* Read the rest of the line */
337                         fgets(result + 2, sizeof(result) - 2, p->f);
338                         ast_modem_trim(result);
339                         if (!strcasecmp(result, "VCON")) {
340                                 /* If we're in immediate mode, reply now */
341 /*                              if (p->mode == MODEM_MODE_IMMEDIATE) */
342                                         return i4l_handle_escape(p, 'X');
343                         } else
344                         if (!strcasecmp(result, "BUSY")) {
345                                 /* Same as a busy signal */
346                                 return i4l_handle_escape(p, 'b');
347                         } else
348                         if (!strncasecmp(result, "CALLER NUMBER: ", 15 )) {
349                                 strncpy(p->cid_num, result + 15, sizeof(p->cid_num)-1);
350                                 return i4l_handle_escape(p, 0);
351                         } else
352                         if (!strcasecmp(result, "RINGING")) {
353                                 if (option_verbose > 2)
354                                         ast_verbose(VERBOSE_PREFIX_3 "%s is ringing...\n", p->dev);
355                                 return i4l_handle_escape(p, 'I');
356                         } else
357                         if (!strncasecmp(result, "RUNG", 4)) {
358                                 /* PM2002: the line was hung up before we picked it up, bye bye */
359                                 if (option_verbose > 2) 
360                                         ast_verbose(VERBOSE_PREFIX_3 "%s was hung up on before we answered\n", p->dev);
361                                 return NULL;
362                         } else
363                         if (!strncasecmp(result, "RING", 4)) {
364                                 if (result[4]=='/') 
365                                         strncpy(p->dnid, result + 5, sizeof(p->dnid)-1);
366                                 return i4l_handle_escape(p, 'R');
367                         } else
368                         if (!strcasecmp(result, "NO CARRIER")) {
369                                 if (option_verbose > 2) 
370                                         ast_verbose(VERBOSE_PREFIX_3 "%s hung up on\n", p->dev);
371                                 return NULL;
372                         } else
373                         if (!strcasecmp(result, "NO DIALTONE")) {
374                                 /* There's no dialtone, so the line isn't working */
375                                 ast_log(LOG_WARNING, "Device '%s' lacking dialtone\n", p->dev);
376                                 return NULL;
377                         }
378                         if (option_debug)
379                                 ast_log(LOG_DEBUG, "Modem said '%s'\n", result);
380                         return i4l_handle_escape(p, 0);
381                 }
382         } else {
383                 /* We have to be more efficient in voice mode */
384                 b = (short *)(p->obuf + p->obuflen);
385                 while (p->obuflen/2 < 240) {
386                         /* Read ahead the full amount */
387                         res = read(p->fd, result, 240 - p->obuflen/2);
388                         if (res < 1) {
389                                 /* If there's nothing there, just continue on */
390                                 if (errno == EAGAIN)
391                                         return i4l_handle_escape(p, 0);
392                                 ast_log(LOG_WARNING, "Read failed: %s\n", strerror(errno));
393                                 return NULL;
394                         }
395                         
396                         for (x=0;x<res;x++) {
397                                 /* Process all the bytes that we've read */
398                                 switch(result[x]) {
399                                 case CHAR_DLE:
400 #if 0
401                                         ast_log(LOG_DEBUG, "Ooh, an escape at %d...\n", x);
402 #endif
403                                         if (!p->escape) {
404                                                 /* Note that next value is
405                                                    an escape, and continue. */
406                                                 p->escape++;
407                                                 break;
408                                         } else {
409                                                 /* Send as is -- fallthrough */
410                                                 p->escape = 0;
411                                         }
412                                 default:
413                                         if (p->escape) {
414                                                 ast_log(LOG_DEBUG, "Value of escape is %c (%d)...\n", result[x] < 32 ? '^' : result[x], result[x]);
415                                                 p->escape = 0;
416                                                 if (f) 
417                                                         ast_log(LOG_WARNING, "Warning: Dropped a signal frame\n");
418                                                 f = i4l_handle_escape(p, result[x]);
419                                                 /* If i4l_handle_escape says NULL, say it now, doesn't matter
420                                                 what else is there, the connection is dead. */
421                                                 if (!f)
422                                                         return NULL;
423                                         } else {
424                                                 *(b++) = AST_MULAW(result[x]);
425                                                 p->obuflen += 2;
426                                         }
427                                 }
428                         }
429                         if (f)
430                                 break;
431                 }
432                 if (f) {
433                         if( ! (!(p->dtmfmode & MODEM_DTMF_I4L) && f->frametype == AST_FRAME_DTMF))
434                         return f;
435                 }
436
437                 /* If we get here, we have a complete voice frame */
438                 p->fr.frametype = AST_FRAME_VOICE;
439                 p->fr.subclass = AST_FORMAT_SLINEAR;
440                 p->fr.samples = 240;
441                 p->fr.data = p->obuf;
442                 p->fr.datalen = p->obuflen;
443                 p->fr.mallocd = 0;
444                 p->fr.delivery.tv_sec = 0;
445                 p->fr.delivery.tv_usec = 0;
446                 p->fr.offset = AST_FRIENDLY_OFFSET;
447                 p->fr.src = __FUNCTION__;
448                 p->obuflen = 0;
449
450                 /* process with dsp */
451                 if (p->dsp) {
452                         f = ast_dsp_process(p->owner, p->dsp, &p->fr);
453                         if (f && (f->frametype == AST_FRAME_DTMF)) {
454                                 ast_log(LOG_DEBUG, "Detected inband DTMF digit: %c on %s\n", f->subclass, p->dev);
455                                 if (f->subclass == 'f') {
456                                         /* Fax tone -- Handle and return NULL */
457                                         struct ast_channel *ast = p->owner;
458                                         if (!p->faxhandled) {
459                                                 p->faxhandled++;
460                                                 if (strcmp(ast->exten, "fax")) {
461                                                         const char *target_context = ast_strlen_zero(ast->macrocontext) ? ast->context : ast->macrocontext;
462                                                         
463                                                         if (ast_exists_extension(ast, target_context, "fax", 1, ast->cid.cid_num)) {
464                                                                 if (option_verbose > 2)
465                                                                         ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name);
466                                                                 /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
467                                                                 pbx_builtin_setvar_helper(ast, "FAXEXTEN", ast->exten);
468                                                                 if (ast_async_goto(ast, target_context, "fax", 1))
469                                                                         ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, target_context);
470                                                         } else
471                                                                 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
472                                                 } else
473                                                         ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
474                                         } else
475                                                 ast_log(LOG_DEBUG, "Fax already handled\n");
476                                         p->fr.frametype = AST_FRAME_NULL;
477                                         p->fr.subclass = 0;
478                                         f = &p->fr;
479                                 }
480                                 return f;
481                         }
482                 }
483                 
484                 return &p->fr;
485         }
486         return NULL;
487 }
488
489 static int i4l_write(struct ast_modem_pvt *p, struct ast_frame *f)
490 {
491 #define MAX_WRITE_SIZE 2048
492         unsigned char result[MAX_WRITE_SIZE << 1];
493         unsigned char b;
494         int bpos=0, x;
495         int res;
496         if (f->datalen > MAX_WRITE_SIZE) {
497                 ast_log(LOG_WARNING, "Discarding too big frame of size %d\n", f->datalen);
498                 return -1;
499         }
500         if (f->frametype != AST_FRAME_VOICE) {
501                 ast_log(LOG_WARNING, "Don't know how to handle %d type frames\n", f->frametype);
502                 return -1;
503         }
504         if (f->subclass != AST_FORMAT_SLINEAR) {
505                 ast_log(LOG_WARNING, "Don't know how to handle anything but signed linear frames\n");
506                 return -1;
507         }
508         for (x=0;x<f->datalen/2;x++) {
509                 b = AST_LIN2MU(((short *)f->data)[x]);
510                 result[bpos++] = b;
511                 if (b == CHAR_DLE)
512                         result[bpos++]=b;
513         }
514 #if 0
515         res = fwrite(result, bpos, 1, p->f);
516         res *= bpos;
517 #else
518         res = write(p->fd, result, bpos);
519 #endif
520         if (res < 1) {
521                 if (errno != EAGAIN) {
522                         ast_log(LOG_WARNING, "Failed to write buffer\n");
523                         return -1;
524                 }
525         }
526 #if 0
527         printf("Result of write is %d\n", res);
528 #endif
529         return 0;
530 }
531
532 static char *i4l_identify(struct ast_modem_pvt *p)
533 {
534         return strdup("Linux ISDN");
535 }
536
537 static void i4l_incusecnt(void)
538 {
539         ast_mutex_lock(&usecnt_lock);
540         usecnt++;
541         ast_mutex_unlock(&usecnt_lock);
542         ast_update_use_count();
543 }
544
545 static void i4l_decusecnt(void)
546 {
547         ast_mutex_lock(&usecnt_lock);
548         usecnt++;
549         ast_mutex_unlock(&usecnt_lock);
550         ast_update_use_count();
551 }
552
553 static int i4l_answer(struct ast_modem_pvt *p)
554 {
555         if (ast_modem_send(p, "ATA\r", 4) ||
556              ast_modem_expect(p, "VCON", 10)) {
557                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
558                 return -1;
559         }
560 #if 1
561         if (ast_modem_send(p, "AT+VDD=0,8", 0) ||
562              ast_modem_expect(p, "OK", 5)) {
563                 ast_log(LOG_WARNING, "Unable to set to phone line interface\n");
564                 return -1;
565         }
566 #endif
567         if (ast_modem_send(p, "AT+VTX+VRX", 0) ||
568              ast_modem_expect(p, "CONNECT", 10)) {
569                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
570                 return -1;
571         }
572         p->ministate = STATE_VOICE;
573
574         /*  let ast dsp detect dtmf */
575         if (p->dtmfmode & MODEM_DTMF_AST) {
576                 if (p->dsp) {
577                         ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", p->dev);
578                 } else {
579                         p->dsp = ast_dsp_new();
580                         if (p->dsp) {
581                                 ast_log(LOG_DEBUG, "Detecting DTMF inband with sw DSP on %s\n",p->dev);
582                                 ast_dsp_set_features(p->dsp, DSP_FEATURE_DTMF_DETECT|DSP_FEATURE_FAX_DETECT);
583                                 ast_dsp_digitmode(p->dsp, DSP_DIGITMODE_DTMF | 0);
584                         }
585                 }
586         }
587
588         return 0;
589 }
590
591 static int i4l_dialdigit(struct ast_modem_pvt *p, char digit)
592 {
593         char c[2];
594         if (p->ministate == STATE_VOICE) {
595                 if (p->dtmfmodegen & MODEM_DTMF_I4L) {
596                 c[0] = CHAR_DLE;
597                 c[1] = digit;
598                 write(p->fd, c, 2);
599                         ast_log(LOG_DEBUG, "Send ISDN out-of-band DTMF %c\n",digit);
600                 }
601                 if(p->dtmfmodegen & MODEM_DTMF_AST) {
602                         ast_log(LOG_DEBUG, "Generating inband DTMF\n");
603                         return -1;
604                 }
605         } else
606                 ast_log(LOG_DEBUG, "Asked to send digit but call not up on %s\n", p->dev);
607         return 0;
608 }
609
610 static int i4l_dial(struct ast_modem_pvt *p, char *stuff)
611 {
612         char cmd[80];
613         char tmpmsn[255];
614         struct ast_channel *c = p->owner;
615
616         /* Find callerid number first, to set the correct A number */
617         if (c && c->cid.cid_num && !(c->cid.cid_pres & 0x20)) {
618             snprintf(tmpmsn, sizeof(tmpmsn), ",%s,", c->cid.cid_num);
619             if(strlen(p->outgoingmsn) && strstr(p->outgoingmsn,tmpmsn) != NULL) {
620               /* Tell ISDN4Linux to use this as A number */
621               snprintf(cmd, sizeof(cmd), "AT&E%s\n", c->cid.cid_num);
622               if (ast_modem_send(p, cmd, strlen(cmd))) {
623                 ast_log(LOG_WARNING, "Unable to set A number to %s\n", c->cid.cid_num);
624               }
625
626             } else {
627               ast_log(LOG_WARNING, "Outgoing MSN %s not allowed (see outgoingmsn=%s in modem.conf)\n",c->cid.cid_num,p->outgoingmsn);
628             }
629         }
630
631         snprintf(cmd, sizeof(cmd), "ATD%c %s\n", p->dialtype,stuff);
632         if (ast_modem_send(p, cmd, strlen(cmd))) {
633                 ast_log(LOG_WARNING, "Unable to dial\n");
634                 return -1;
635         }
636         return 0;
637 }
638
639 static int i4l_hangup(struct ast_modem_pvt *p)
640 {
641         char dummy[50];
642         int dtr = TIOCM_DTR;
643
644         /* free the memory used by the DSP */
645         if (p->dsp) {
646                 ast_dsp_free(p->dsp);
647                 p->dsp = NULL;
648         }
649
650         /* down DTR to hangup modem */
651         ioctl(p->fd, TIOCMBIC, &dtr);
652         /* Read anything outstanding */
653         while(read(p->fd, dummy, sizeof(dummy)) > 0);
654
655         /* rise DTR to re-enable line */
656         ioctl(p->fd, TIOCMBIS, &dtr);
657         
658         /* Read anything outstanding */
659         while(read(p->fd, dummy, sizeof(dummy)) > 0);
660
661         /* basically we're done, just to be sure */
662         write(p->fd, "\n\n", 2);
663         read(p->fd, dummy, sizeof(dummy));
664         if (ast_modem_send(p, "ATH", 0)) {
665                 ast_log(LOG_WARNING, "Unable to hang up\n");
666                 return -1;
667         }
668         if (ast_modem_expect(p, "OK", 5)) {
669                 ast_log(LOG_WARNING, "Final 'OK' not received\n");
670                 return -1;
671         }
672
673         return 0;
674 }
675
676 static struct ast_modem_driver i4l_driver =
677 {
678         "i4l",
679         i4l_idents,
680         AST_FORMAT_SLINEAR,
681         0,              /* Not full duplex */
682         i4l_incusecnt,  /* incusecnt */
683         i4l_decusecnt,  /* decusecnt */
684         i4l_identify,   /* identify */
685         i4l_init,       /* init */
686         i4l_setdev,     /* setdev */
687         i4l_read,
688         i4l_write,
689         i4l_dial,       /* dial */
690         i4l_answer,     /* answer */
691         i4l_hangup,     /* hangup */
692         i4l_startrec,   /* start record */
693         NULL,   /* stop record */
694         NULL,   /* start playback */
695         NULL,   /* stop playback */
696         NULL,   /* set silence supression */
697         i4l_dialdigit,  /* dialdigit */
698 };
699
700
701
702 int usecount(void)
703 {
704         int res;
705         ast_mutex_lock(&usecnt_lock);
706         res = usecnt;
707         ast_mutex_unlock(&usecnt_lock);
708         return res;
709 }
710
711 int load_module(void)
712 {
713         return ast_register_modem_driver(&i4l_driver);
714 }
715
716 int unload_module(void)
717 {
718         return ast_unregister_modem_driver(&i4l_driver);
719 }
720
721 char *description()
722 {
723         return desc;
724 }
725
726 char *key()
727 {
728         return ASTERISK_GPL_KEY;
729 }