Version 0.1.6 from FTP
[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 <pthread.h>
21 #include <asterisk/vmodem.h>
22 #include <asterisk/module.h>
23 #include <asterisk/frame.h>
24 #include <asterisk/logger.h>
25 #include <asterisk/options.h>
26 #include "alaw.h"
27
28 #define STATE_COMMAND   0
29 #define STATE_VOICE     1
30
31 static char *breakcmd = "\0x10\0x14\0x10\0x3";
32
33 static char *desc = "ISDN4Linux Emulated Modem Driver";
34
35 int usecnt;
36 pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
37
38 static char *i4l_idents[] = {
39         /* Identify ISDN4Linux Driver */
40         "Linux ISDN",
41         NULL
42 };
43
44 static int i4l_setdev(struct ast_modem_pvt *p, int dev)
45 {
46         char cmd[80];
47         if ((dev != MODEM_DEV_TELCO) && (dev != MODEM_DEV_TELCO_SPK)) {
48                 ast_log(LOG_WARNING, "ISDN4Linux only supports telco device, not %d.\n", dev);
49                 return -1;
50         } else  /* Convert DEV to our understanding of it */
51                 dev = 2;
52         if (ast_modem_send(p, "AT+VLS?", 0)) {
53                 ast_log(LOG_WARNING, "Unable to select current mode %d\n", dev);
54                 return -1;
55         }
56         if (ast_modem_read_response(p, 5)) {
57                 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
58                 return -1;
59         }
60         ast_modem_trim(p->response);
61         strncpy(cmd, p->response, sizeof(cmd));
62         if (ast_modem_expect(p, "OK", 5)) {
63                 ast_log(LOG_WARNING, "Modem did not respond properly\n");
64                 return -1;
65         }
66         if (dev == atoi(cmd)) {
67                 /* We're already in the right mode, don't bother changing for fear of
68                    hanging up */
69                 return 0;
70         }
71         snprintf(cmd, sizeof(cmd), "AT+VLS=%d", dev);
72         if (ast_modem_send(p, cmd, 0))  {
73                 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
74                 return -1;
75         }
76         if (ast_modem_read_response(p, 5)) {
77                 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
78                 return -1;
79         }
80         ast_modem_trim(p->response);
81         if (strcasecmp(p->response, "VCON") && strcasecmp(p->response, "OK")) {
82                 ast_log(LOG_WARNING, "Unexpected reply: %s\n", p->response);
83                 return -1;
84         }
85         return 0;
86 }
87
88 static int i4l_startrec(struct ast_modem_pvt *p)
89 {
90         if (ast_modem_send(p, "AT+VRX+VTX", 0) ||
91              ast_modem_expect(p, "CONNECT", 5)) {
92                 ast_log(LOG_WARNING, "Unable to start recording\n");
93                 return -1;
94         }
95         p->ministate = STATE_VOICE;
96         return 0;
97 }
98
99 static int i4l_break(struct ast_modem_pvt *p)
100 {
101         if (ast_modem_send(p, breakcmd, 2)) {
102                 ast_log(LOG_WARNING, "Failed to break\n");
103                 return -1;
104         }
105         if (ast_modem_send(p, "\r\n", 2)) {
106                 ast_log(LOG_WARNING, "Failed to send enter?\n");
107                 return -1;
108         }
109 #if 0
110         /* Read any outstanding junk */
111         while(!ast_modem_read_response(p, 1));
112 #endif
113         if (ast_modem_send(p, "AT", 0)) {
114                 /* Modem might be stuck in some weird mode, try to get it out */
115                 ast_modem_send(p, "+++", 3);
116                 if (ast_modem_expect(p, "OK", 10)) {
117                         ast_log(LOG_WARNING, "Modem is not responding\n");
118                         return -1;
119                 }
120                 if (ast_modem_send(p, "AT", 0)) {
121                         ast_log(LOG_WARNING, "Modem is not responding\n");
122                         return -1;
123                 }
124         }
125         if (ast_modem_expect(p, "OK", 5)) {
126                 ast_log(LOG_WARNING, "Modem did not respond properly\n");
127                 return -1;
128         }
129         return 0;
130 }
131
132 static int i4l_init(struct ast_modem_pvt *p)
133 {
134         char cmd[256];
135         if (option_debug)
136                 ast_log(LOG_DEBUG, "i4l_init()\n");
137         if (i4l_break(p))
138                 return -1;
139         /* Force into command mode */
140         p->ministate = STATE_COMMAND;
141         if (ast_modem_send(p, "AT+FCLASS=8", 0) ||
142              ast_modem_expect(p, "OK", 5)) {
143                 ast_log(LOG_WARNING, "Unable to set to voice mode\n");
144                 return -1;
145         }
146         if (strlen(p->msn)) {
147                 snprintf(cmd, sizeof(cmd), "AT&E%s", p->msn);
148                 if (ast_modem_send(p, cmd, 0) ||
149                     ast_modem_expect(p, "OK", 5)) {
150                         ast_log(LOG_WARNING, "Unable to set MSN to %s\n", p->msn);
151                         return -1;
152                 }
153         }
154         if (ast_modem_send(p, "ATS18=1", 0) ||
155              ast_modem_expect(p, "OK", 5)) {
156                 ast_log(LOG_WARNING, "Unable to set to audio only mode\n");
157                 return -1;
158         }
159         if (ast_modem_send(p, "ATS14=4", 0) ||
160              ast_modem_expect(p, "OK", 5)) {
161                 ast_log(LOG_WARNING, "Unable to set to transparent mode\n");
162                 return -1;
163         }
164         if (ast_modem_send(p, "ATS23=1", 0) ||
165              ast_modem_expect(p, "OK", 5)) {
166                 ast_log(LOG_WARNING, "Unable to set to transparent mode\n");
167                 return -1;
168         }
169
170         if (ast_modem_send(p, "AT+VSM=5", 0) ||
171              ast_modem_expect(p, "OK", 5)) {
172                 ast_log(LOG_WARNING, "Unable to set to aLAW mode\n");
173                 return -1;
174         }
175         if (ast_modem_send(p, "AT+VLS=2", 0) ||
176              ast_modem_expect(p, "OK", 5)) {
177                 ast_log(LOG_WARNING, "Unable to set to phone line interface\n");
178                 return -1;
179         }
180         p->escape = 0;
181         return 0;
182 }
183
184 static struct ast_frame *i4l_handle_escape(struct ast_modem_pvt *p, char esc)
185 {
186         /* Handle escaped characters -- but sometimes we call it directly as 
187            a quick way to cause known responses */
188         p->fr.frametype = AST_FRAME_NULL;
189         p->fr.subclass = 0;
190         p->fr.data = NULL;
191         p->fr.datalen = 0;
192         p->fr.timelen = 0;
193         p->fr.offset = 0;
194         p->fr.mallocd = 0;
195         if (esc && option_debug)
196                 ast_log(LOG_DEBUG, "Escaped character '%c'\n", esc);
197         
198         switch(esc) {
199         case 'R': /* Pseudo ring */
200                 p->fr.frametype = AST_FRAME_CONTROL;
201                 p->fr.subclass = AST_CONTROL_RING;
202                 return &p->fr;
203         case 'X': /* Pseudo connect */
204                 p->fr.frametype = AST_FRAME_CONTROL;
205                 p->fr.subclass = AST_CONTROL_ANSWER;
206                 if (p->owner)
207                         p->owner->state = AST_STATE_UP;
208                 if (i4l_startrec(p))
209                         return  NULL;
210                 return &p->fr;
211         case 'b': /* Busy signal */
212                 p->fr.frametype = AST_FRAME_CONTROL;
213                 p->fr.subclass = AST_CONTROL_BUSY;
214                 return &p->fr;
215         case 'o': /* Overrun */
216                 ast_log(LOG_WARNING, "Overflow on modem, flushing buffers\n");
217                 if (ast_modem_send(p, "\0x10E", 2)) 
218                         ast_log(LOG_WARNING, "Unable to flush buffers\n");
219                 return &p->fr;  
220         case CHAR_ETX: /* End Transmission */
221                 return NULL;
222         case 'u': /* Underrun */
223                 ast_log(LOG_WARNING, "Data underrun\n");
224                 /* Fall Through */
225         case 'd': /* Dialtone */
226         case 'c': /* Calling Tone */
227         case 'e': /* European version */
228         case 'a': /* Answer Tone */
229         case 'f': /* Bell Answer Tone */
230         case 'T': /* Timing mark */
231         case 't': /* Handset off hook */
232         case 'h': /* Handset hungup */
233                 /* Ignore */
234                 if (option_debug)
235                         ast_log(LOG_DEBUG, "Ignoring Escaped character '%c' (%d)\n", esc, esc);
236                 return &p->fr;
237         case '0':
238         case '1':
239         case '2':
240         case '3':
241         case '4':
242         case '5':
243         case '6':
244         case '7':
245         case '8':
246         case '9':
247         case '*':
248         case '#':
249                 ast_log(LOG_DEBUG, "DTMF: '%c' (%d)\n", esc, esc);
250                 p->fr.frametype=AST_FRAME_DTMF;
251                 p->fr.subclass=esc;
252                 return &p->fr;
253         case 0: /* Pseudo signal */
254                 return &p->fr;
255         default:
256                 ast_log(LOG_DEBUG, "Unknown Escaped character '%c' (%d)\n", esc, esc);
257         }
258         return &p->fr;
259 }
260
261 static struct ast_frame *i4l_read(struct ast_modem_pvt *p)
262 {
263         char result[256];
264         short *b;
265         struct ast_frame *f=NULL;
266         int res;
267         int x;
268         if (p->ministate == STATE_COMMAND) {
269                 /* Read the first two bytes, first, in case it's a control message */
270                 read(p->fd, result, 2);
271                 if (result[0] == CHAR_DLE) {
272                         return i4l_handle_escape(p, result[1]);
273                         
274                 } else {
275                         if ((result[0] == '\n') || (result[0] == '\r'))
276                                 return i4l_handle_escape(p, 0);
277                         /* Read the rest of the line */
278                         fgets(result + 2, sizeof(result) - 2, p->f);
279                         ast_modem_trim(result);
280                         if (!strcasecmp(result, "VCON")) {
281                                 /* If we're in immediate mode, reply now */
282                                 if (p->mode == MODEM_MODE_IMMEDIATE)
283                                         return i4l_handle_escape(p, 'X');
284                         } else
285                         if (!strcasecmp(result, "BUSY")) {
286                                 /* Same as a busy signal */
287                                 return i4l_handle_escape(p, 'b');
288                         } else
289                         if (!strncasecmp(result, "CALLER NUMBER: ", 15 )) {
290                                 strncpy(p->cid, result + 15, sizeof(p->cid));
291                                 return i4l_handle_escape(p, 'R');
292                         } else
293                         if (!strncasecmp(result, "RING", 4)) {
294                                 if (result[4]=='/') 
295                                         strncpy(p->dnid, result + 4, sizeof(p->dnid));
296                                 return i4l_handle_escape(p, 'R');
297                         } else
298                         if (!strcasecmp(result, "NO CARRIER")) {
299                                 if (option_verbose > 2) 
300                                         ast_verbose(VERBOSE_PREFIX_3 "%s hung up on\n", p->dev);
301                                 return NULL;
302                         } else
303                         if (!strcasecmp(result, "NO DIALTONE")) {
304                                 /* There's no dialtone, so the line isn't working */
305                                 ast_log(LOG_WARNING, "Device '%s' lacking dialtone\n", p->dev);
306                                 return NULL;
307                         }
308                         if (option_debug)
309                                 ast_log(LOG_DEBUG, "Modem said '%s'\n", result);
310                         return i4l_handle_escape(p, 0);
311                 }
312         } else {
313                 /* We have to be more efficient in voice mode */
314                 b = (short *)(p->obuf + p->obuflen);
315                 while (p->obuflen/2 < 240) {
316                         /* Read ahead the full amount */
317                         res = read(p->fd, result, 240 - p->obuflen/2);
318                         if (res < 1) {
319                                 /* If there's nothing there, just continue on */
320                                 if (errno == EAGAIN)
321                                         return i4l_handle_escape(p, 0);
322                                 ast_log(LOG_WARNING, "Read failed: %s\n", strerror(errno));
323                         }
324                         
325                         for (x=0;x<res;x++) {
326                                 /* Process all the bytes that we've read */
327                                 switch(result[x]) {
328                                 case CHAR_DLE:
329 #if 0
330                                         ast_log(LOG_DEBUG, "Ooh, an escape at %d...\n", x);
331 #endif
332                                         if (!p->escape) {
333                                                 /* Note that next value is
334                                                    an escape, and continue. */
335                                                 p->escape++;
336                                                 break;
337                                         } else {
338                                                 /* Send as is -- fallthrough */
339                                                 p->escape = 0;
340                                         }
341                                 default:
342                                         if (p->escape) {
343                                                 ast_log(LOG_DEBUG, "Value of escape is %c (%d)...\n", result[x] < 32 ? '^' : result[x], result[x]);
344                                                 p->escape = 0;
345                                                 if (f) 
346                                                         ast_log(LOG_WARNING, "Warning: Dropped a signal frame\n");
347                                                 f = i4l_handle_escape(p, result[x]);
348                                                 /* If i4l_handle_escape says NULL, say it now, doesn't matter
349                                                 what else is there, the connection is dead. */
350                                                 if (!f)
351                                                         return NULL;
352                                         } else {
353                                                 *(b++) = ALAW2INT(result[x] & 0xff);
354                                                 p->obuflen += 2;
355                                         }
356                                 }
357                         }
358                         if (f)
359                                 break;
360                 }
361                 /* If we get here, we have a complete voice frame */
362                 p->fr.frametype = AST_FRAME_VOICE;
363                 p->fr.subclass = AST_FORMAT_SLINEAR;
364                 p->fr.timelen = 30;
365                 p->fr.data = p->obuf;
366                 p->fr.datalen = p->obuflen;
367                 p->fr.mallocd = 0;
368                 p->fr.offset = AST_FRIENDLY_OFFSET;
369                 p->fr.src = __FUNCTION__;
370                 p->obuflen = 0;
371                 return &p->fr;
372         }
373         return NULL;
374 }
375
376 static int i4l_write(struct ast_modem_pvt *p, struct ast_frame *f)
377 {
378 #define MAX_WRITE_SIZE 512
379         unsigned char result[MAX_WRITE_SIZE << 1];
380         unsigned char b;
381         int bpos=0, x;
382         int res;
383         if (f->datalen > MAX_WRITE_SIZE) {
384                 ast_log(LOG_WARNING, "Discarding too big frame of size %d\n", f->datalen);
385                 return -1;
386         }
387         if (f->frametype != AST_FRAME_VOICE) {
388                 ast_log(LOG_WARNING, "Don't know how to handle %d type frames\n", f->frametype);
389                 return -1;
390         }
391         if (f->subclass != AST_FORMAT_SLINEAR) {
392                 ast_log(LOG_WARNING, "Don't know how to handle anything but signed linear frames\n");
393                 return -1;
394         }
395         for (x=0;x<f->datalen/2;x++) {
396                 b = INT2ALAW(((short *)f->data)[x]);
397                 result[bpos++] = b;
398                 if (b == CHAR_DLE)
399                         result[bpos++]=b;
400         }
401 #if 0
402         res = fwrite(result, bpos, 1, p->f);
403         res *= bpos;
404 #else
405         res = write(p->fd, result, bpos);
406 #endif
407         if (res < 1) {
408                 ast_log(LOG_WARNING, "Failed to write buffer\n");
409                 return -1;
410         }
411 #if 0
412         printf("Result of write is %d\n", res);
413 #endif
414         return 0;
415 }
416
417 static char *i4l_identify(struct ast_modem_pvt *p)
418 {
419         return strdup("Linux ISDN");
420 }
421
422 static void i4l_incusecnt()
423 {
424         pthread_mutex_lock(&usecnt_lock);
425         usecnt++;
426         pthread_mutex_unlock(&usecnt_lock);
427         ast_update_use_count();
428 }
429
430 static void i4l_decusecnt()
431 {
432         pthread_mutex_lock(&usecnt_lock);
433         usecnt++;
434         pthread_mutex_unlock(&usecnt_lock);
435         ast_update_use_count();
436 }
437
438 static int i4l_answer(struct ast_modem_pvt *p)
439 {
440         if (ast_modem_send(p, "ATA", 0) ||
441              ast_modem_expect(p, "VCON", 10)) {
442                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
443                 return -1;
444         }
445 #if 1
446         if (ast_modem_send(p, "AT+VDD=0,8", 0) ||
447              ast_modem_expect(p, "OK", 5)) {
448                 ast_log(LOG_WARNING, "Unable to set to phone line interface\n");
449                 return -1;
450         }
451 #endif
452         if (ast_modem_send(p, "AT+VTX+VRX", 0) ||
453              ast_modem_expect(p, "CONNECT", 10)) {
454                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
455                 return -1;
456         }
457         p->ministate = STATE_VOICE;
458         return 0;
459 }
460
461 static int i4l_dialdigit(struct ast_modem_pvt *p, char digit)
462 {
463         char c[2];
464         if (p->ministate == STATE_VOICE) {
465                 c[0] = CHAR_DLE;
466                 c[1] = digit;
467                 write(p->fd, c, 2);
468         } else
469                 ast_log(LOG_DEBUG, "Asked to send digit but call not up on %s\n", p->dev);
470         return 0;
471 }
472
473 static int i4l_dial(struct ast_modem_pvt *p, char *stuff)
474 {
475         char cmd[80];
476         snprintf(cmd, sizeof(cmd), "ATD%c %s", p->dialtype,stuff);
477         if (ast_modem_send(p, cmd, 0)) {
478                 ast_log(LOG_WARNING, "Unable to dial\n");
479                 return -1;
480         }
481         return 0;
482 }
483
484 static int i4l_hangup(struct ast_modem_pvt *p)
485 {
486         char dummy[50];
487         sprintf(dummy, "%c%c", 0x10, 0x3);
488         if (write(p->fd, dummy, 2) < 0) {
489                 ast_log(LOG_WARNING, "Failed to break\n");
490                 return -1;
491         }
492
493         /* Read anything outstanding */
494         while(read(p->fd, dummy, sizeof(dummy)) > 0);
495
496         sprintf(dummy, "%c%c", 0x10, 0x14);
497         if (write(p->fd, dummy, 2) < 0) {
498                 ast_log(LOG_WARNING, "Failed to break\n");
499                 return -1;
500         }
501         ast_modem_expect(p, "VCON", 1);
502 #if 0
503         if (ast_modem_expect(p, "VCON", 8)) {
504                 ast_log(LOG_WARNING, "Didn't get expected VCON\n");
505                 return -1;
506         }
507 #endif
508         write(p->fd, "\n\n", 2);
509         read(p->fd, dummy, sizeof(dummy));
510         /* Hangup by switching to data, then back to voice */
511         if (ast_modem_send(p, "ATH", 0) ||
512              ast_modem_expect(p, "NO CARRIER", 8)) {
513                 ast_log(LOG_WARNING, "Unable to hang up\n");
514                 return -1;
515         }
516         if (ast_modem_expect(p, "OK", 5)) {
517                 ast_log(LOG_WARNING, "Final 'OK' not received\n");
518                 return -1;
519         }
520         return 0;
521 }
522
523 static struct ast_modem_driver i4l_driver =
524 {
525         "i4l",
526         i4l_idents,
527         AST_FORMAT_SLINEAR,
528         0,              /* Not full duplex */
529         i4l_incusecnt,  /* incusecnt */
530         i4l_decusecnt,  /* decusecnt */
531         i4l_identify,   /* identify */
532         i4l_init,       /* init */
533         i4l_setdev,     /* setdev */
534         i4l_read,
535         i4l_write,
536         i4l_dial,       /* dial */
537         i4l_answer,     /* answer */
538         i4l_hangup,     /* hangup */
539         i4l_startrec,   /* start record */
540         NULL,   /* stop record */
541         NULL,   /* start playback */
542         NULL,   /* stop playback */
543         NULL,   /* set silence supression */
544         i4l_dialdigit,  /* dialdigit */
545 };
546
547
548
549 int usecount(void)
550 {
551         int res;
552         pthread_mutex_lock(&usecnt_lock);
553         res = usecnt;
554         pthread_mutex_unlock(&usecnt_lock);
555         return res;
556 }
557
558 int load_module(void)
559 {
560         return ast_register_modem_driver(&i4l_driver);
561 }
562
563 int unload_module(void)
564 {
565         return ast_unregister_modem_driver(&i4l_driver);
566 }
567
568 char *description()
569 {
570         return desc;
571 }
572