major Makefile and build process improvements, including removal of all hardcoded...
[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                                                         if (ast_exists_extension(ast, ast_strlen_zero(ast->macrocontext) ? ast->context : ast->macrocontext, "fax", 1, ast->cid.cid_num)) {
462                                                                 if (option_verbose > 2)
463                                                                         ast_verbose(VERBOSE_PREFIX_3 "Redirecting %s to fax extension\n", ast->name);
464                                                                 /* Save the DID/DNIS when we transfer the fax call to a "fax" extension */
465                                                                 pbx_builtin_setvar_helper(ast,"FAXEXTEN",ast->exten);
466                                                                 if (ast_async_goto(ast, ast->context, "fax", 1))
467                                                                         ast_log(LOG_WARNING, "Failed to async goto '%s' into fax of '%s'\n", ast->name, ast->context);
468                                                         } else
469                                                                 ast_log(LOG_NOTICE, "Fax detected, but no fax extension\n");
470                                                 } else
471                                                         ast_log(LOG_DEBUG, "Already in a fax extension, not redirecting\n");
472                                         } else
473                                                 ast_log(LOG_DEBUG, "Fax already handled\n");
474                                         p->fr.frametype = AST_FRAME_NULL;
475                                         p->fr.subclass = 0;
476                                         f = &p->fr;
477                                 }
478                                 return f;
479                         }
480                 }
481                 
482                 return &p->fr;
483         }
484         return NULL;
485 }
486
487 static int i4l_write(struct ast_modem_pvt *p, struct ast_frame *f)
488 {
489 #define MAX_WRITE_SIZE 2048
490         unsigned char result[MAX_WRITE_SIZE << 1];
491         unsigned char b;
492         int bpos=0, x;
493         int res;
494         if (f->datalen > MAX_WRITE_SIZE) {
495                 ast_log(LOG_WARNING, "Discarding too big frame of size %d\n", f->datalen);
496                 return -1;
497         }
498         if (f->frametype != AST_FRAME_VOICE) {
499                 ast_log(LOG_WARNING, "Don't know how to handle %d type frames\n", f->frametype);
500                 return -1;
501         }
502         if (f->subclass != AST_FORMAT_SLINEAR) {
503                 ast_log(LOG_WARNING, "Don't know how to handle anything but signed linear frames\n");
504                 return -1;
505         }
506         for (x=0;x<f->datalen/2;x++) {
507                 b = AST_LIN2MU(((short *)f->data)[x]);
508                 result[bpos++] = b;
509                 if (b == CHAR_DLE)
510                         result[bpos++]=b;
511         }
512 #if 0
513         res = fwrite(result, bpos, 1, p->f);
514         res *= bpos;
515 #else
516         res = write(p->fd, result, bpos);
517 #endif
518         if (res < 1) {
519                 if (errno != EAGAIN) {
520                         ast_log(LOG_WARNING, "Failed to write buffer\n");
521                         return -1;
522                 }
523         }
524 #if 0
525         printf("Result of write is %d\n", res);
526 #endif
527         return 0;
528 }
529
530 static char *i4l_identify(struct ast_modem_pvt *p)
531 {
532         return strdup("Linux ISDN");
533 }
534
535 static void i4l_incusecnt(void)
536 {
537         ast_mutex_lock(&usecnt_lock);
538         usecnt++;
539         ast_mutex_unlock(&usecnt_lock);
540         ast_update_use_count();
541 }
542
543 static void i4l_decusecnt(void)
544 {
545         ast_mutex_lock(&usecnt_lock);
546         usecnt++;
547         ast_mutex_unlock(&usecnt_lock);
548         ast_update_use_count();
549 }
550
551 static int i4l_answer(struct ast_modem_pvt *p)
552 {
553         if (ast_modem_send(p, "ATA\r", 4) ||
554              ast_modem_expect(p, "VCON", 10)) {
555                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
556                 return -1;
557         }
558 #if 1
559         if (ast_modem_send(p, "AT+VDD=0,8", 0) ||
560              ast_modem_expect(p, "OK", 5)) {
561                 ast_log(LOG_WARNING, "Unable to set to phone line interface\n");
562                 return -1;
563         }
564 #endif
565         if (ast_modem_send(p, "AT+VTX+VRX", 0) ||
566              ast_modem_expect(p, "CONNECT", 10)) {
567                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
568                 return -1;
569         }
570         p->ministate = STATE_VOICE;
571
572         /*  let ast dsp detect dtmf */
573         if (p->dtmfmode & MODEM_DTMF_AST) {
574                 if (p->dsp) {
575                         ast_log(LOG_DEBUG, "Already have a dsp on %s?\n", p->dev);
576                 } else {
577                         p->dsp = ast_dsp_new();
578                         if (p->dsp) {
579                                 ast_log(LOG_DEBUG, "Detecting DTMF inband with sw DSP on %s\n",p->dev);
580                                 ast_dsp_set_features(p->dsp, DSP_FEATURE_DTMF_DETECT|DSP_FEATURE_FAX_DETECT);
581                                 ast_dsp_digitmode(p->dsp, DSP_DIGITMODE_DTMF | 0);
582                         }
583                 }
584         }
585
586         return 0;
587 }
588
589 static int i4l_dialdigit(struct ast_modem_pvt *p, char digit)
590 {
591         char c[2];
592         if (p->ministate == STATE_VOICE) {
593                 if (p->dtmfmodegen & MODEM_DTMF_I4L) {
594                 c[0] = CHAR_DLE;
595                 c[1] = digit;
596                 write(p->fd, c, 2);
597                         ast_log(LOG_DEBUG, "Send ISDN out-of-band DTMF %c\n",digit);
598                 }
599                 if(p->dtmfmodegen & MODEM_DTMF_AST) {
600                         ast_log(LOG_DEBUG, "Generating inband DTMF\n");
601                         return -1;
602                 }
603         } else
604                 ast_log(LOG_DEBUG, "Asked to send digit but call not up on %s\n", p->dev);
605         return 0;
606 }
607
608 static int i4l_dial(struct ast_modem_pvt *p, char *stuff)
609 {
610         char cmd[80];
611         char tmpmsn[255];
612         struct ast_channel *c = p->owner;
613
614         /* Find callerid number first, to set the correct A number */
615         if (c && c->cid.cid_num && !(c->cid.cid_pres & 0x20)) {
616             snprintf(tmpmsn, sizeof(tmpmsn), ",%s,", c->cid.cid_num);
617             if(strlen(p->outgoingmsn) && strstr(p->outgoingmsn,tmpmsn) != NULL) {
618               /* Tell ISDN4Linux to use this as A number */
619               snprintf(cmd, sizeof(cmd), "AT&E%s\n", c->cid.cid_num);
620               if (ast_modem_send(p, cmd, strlen(cmd))) {
621                 ast_log(LOG_WARNING, "Unable to set A number to %s\n", c->cid.cid_num);
622               }
623
624             } else {
625               ast_log(LOG_WARNING, "Outgoing MSN %s not allowed (see outgoingmsn=%s in modem.conf)\n",c->cid.cid_num,p->outgoingmsn);
626             }
627         }
628
629         snprintf(cmd, sizeof(cmd), "ATD%c %s\n", p->dialtype,stuff);
630         if (ast_modem_send(p, cmd, strlen(cmd))) {
631                 ast_log(LOG_WARNING, "Unable to dial\n");
632                 return -1;
633         }
634         return 0;
635 }
636
637 static int i4l_hangup(struct ast_modem_pvt *p)
638 {
639         char dummy[50];
640         int dtr = TIOCM_DTR;
641
642         /* free the memory used by the DSP */
643         if (p->dsp) {
644                 ast_dsp_free(p->dsp);
645                 p->dsp = NULL;
646         }
647
648         /* down DTR to hangup modem */
649         ioctl(p->fd, TIOCMBIC, &dtr);
650         /* Read anything outstanding */
651         while(read(p->fd, dummy, sizeof(dummy)) > 0);
652
653         /* rise DTR to re-enable line */
654         ioctl(p->fd, TIOCMBIS, &dtr);
655         
656         /* Read anything outstanding */
657         while(read(p->fd, dummy, sizeof(dummy)) > 0);
658
659         /* basically we're done, just to be sure */
660         write(p->fd, "\n\n", 2);
661         read(p->fd, dummy, sizeof(dummy));
662         if (ast_modem_send(p, "ATH", 0)) {
663                 ast_log(LOG_WARNING, "Unable to hang up\n");
664                 return -1;
665         }
666         if (ast_modem_expect(p, "OK", 5)) {
667                 ast_log(LOG_WARNING, "Final 'OK' not received\n");
668                 return -1;
669         }
670
671         return 0;
672 }
673
674 static struct ast_modem_driver i4l_driver =
675 {
676         "i4l",
677         i4l_idents,
678         AST_FORMAT_SLINEAR,
679         0,              /* Not full duplex */
680         i4l_incusecnt,  /* incusecnt */
681         i4l_decusecnt,  /* decusecnt */
682         i4l_identify,   /* identify */
683         i4l_init,       /* init */
684         i4l_setdev,     /* setdev */
685         i4l_read,
686         i4l_write,
687         i4l_dial,       /* dial */
688         i4l_answer,     /* answer */
689         i4l_hangup,     /* hangup */
690         i4l_startrec,   /* start record */
691         NULL,   /* stop record */
692         NULL,   /* start playback */
693         NULL,   /* stop playback */
694         NULL,   /* set silence supression */
695         i4l_dialdigit,  /* dialdigit */
696 };
697
698
699
700 int usecount(void)
701 {
702         int res;
703         ast_mutex_lock(&usecnt_lock);
704         res = usecnt;
705         ast_mutex_unlock(&usecnt_lock);
706         return res;
707 }
708
709 int load_module(void)
710 {
711         return ast_register_modem_driver(&i4l_driver);
712 }
713
714 int unload_module(void)
715 {
716         return ast_unregister_modem_driver(&i4l_driver);
717 }
718
719 char *description()
720 {
721         return desc;
722 }
723
724 char *key()
725 {
726         return ASTERISK_GPL_KEY;
727 }