Version 0.1.7 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 (f)
362                         return f;
363                 /* If we get here, we have a complete voice frame */
364                 p->fr.frametype = AST_FRAME_VOICE;
365                 p->fr.subclass = AST_FORMAT_SLINEAR;
366                 p->fr.timelen = 30;
367                 p->fr.data = p->obuf;
368                 p->fr.datalen = p->obuflen;
369                 p->fr.mallocd = 0;
370                 p->fr.offset = AST_FRIENDLY_OFFSET;
371                 p->fr.src = __FUNCTION__;
372                 p->obuflen = 0;
373                 return &p->fr;
374         }
375         return NULL;
376 }
377
378 static int i4l_write(struct ast_modem_pvt *p, struct ast_frame *f)
379 {
380 #define MAX_WRITE_SIZE 512
381         unsigned char result[MAX_WRITE_SIZE << 1];
382         unsigned char b;
383         int bpos=0, x;
384         int res;
385         if (f->datalen > MAX_WRITE_SIZE) {
386                 ast_log(LOG_WARNING, "Discarding too big frame of size %d\n", f->datalen);
387                 return -1;
388         }
389         if (f->frametype != AST_FRAME_VOICE) {
390                 ast_log(LOG_WARNING, "Don't know how to handle %d type frames\n", f->frametype);
391                 return -1;
392         }
393         if (f->subclass != AST_FORMAT_SLINEAR) {
394                 ast_log(LOG_WARNING, "Don't know how to handle anything but signed linear frames\n");
395                 return -1;
396         }
397         for (x=0;x<f->datalen/2;x++) {
398                 b = INT2ALAW(((short *)f->data)[x]);
399                 result[bpos++] = b;
400                 if (b == CHAR_DLE)
401                         result[bpos++]=b;
402         }
403 #if 0
404         res = fwrite(result, bpos, 1, p->f);
405         res *= bpos;
406 #else
407         res = write(p->fd, result, bpos);
408 #endif
409         if (res < 1) {
410                 ast_log(LOG_WARNING, "Failed to write buffer\n");
411                 return -1;
412         }
413 #if 0
414         printf("Result of write is %d\n", res);
415 #endif
416         return 0;
417 }
418
419 static char *i4l_identify(struct ast_modem_pvt *p)
420 {
421         return strdup("Linux ISDN");
422 }
423
424 static void i4l_incusecnt()
425 {
426         pthread_mutex_lock(&usecnt_lock);
427         usecnt++;
428         pthread_mutex_unlock(&usecnt_lock);
429         ast_update_use_count();
430 }
431
432 static void i4l_decusecnt()
433 {
434         pthread_mutex_lock(&usecnt_lock);
435         usecnt++;
436         pthread_mutex_unlock(&usecnt_lock);
437         ast_update_use_count();
438 }
439
440 static int i4l_answer(struct ast_modem_pvt *p)
441 {
442         if (ast_modem_send(p, "ATA", 0) ||
443              ast_modem_expect(p, "VCON", 10)) {
444                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
445                 return -1;
446         }
447 #if 1
448         if (ast_modem_send(p, "AT+VDD=0,8", 0) ||
449              ast_modem_expect(p, "OK", 5)) {
450                 ast_log(LOG_WARNING, "Unable to set to phone line interface\n");
451                 return -1;
452         }
453 #endif
454         if (ast_modem_send(p, "AT+VTX+VRX", 0) ||
455              ast_modem_expect(p, "CONNECT", 10)) {
456                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
457                 return -1;
458         }
459         p->ministate = STATE_VOICE;
460         return 0;
461 }
462
463 static int i4l_dialdigit(struct ast_modem_pvt *p, char digit)
464 {
465         char c[2];
466         if (p->ministate == STATE_VOICE) {
467                 c[0] = CHAR_DLE;
468                 c[1] = digit;
469                 write(p->fd, c, 2);
470         } else
471                 ast_log(LOG_DEBUG, "Asked to send digit but call not up on %s\n", p->dev);
472         return 0;
473 }
474
475 static int i4l_dial(struct ast_modem_pvt *p, char *stuff)
476 {
477         char cmd[80];
478         snprintf(cmd, sizeof(cmd), "ATD%c %s", p->dialtype,stuff);
479         if (ast_modem_send(p, cmd, 0)) {
480                 ast_log(LOG_WARNING, "Unable to dial\n");
481                 return -1;
482         }
483         return 0;
484 }
485
486 static int i4l_hangup(struct ast_modem_pvt *p)
487 {
488         char dummy[50];
489         sprintf(dummy, "%c%c", 0x10, 0x3);
490         if (write(p->fd, dummy, 2) < 0) {
491                 ast_log(LOG_WARNING, "Failed to break\n");
492                 return -1;
493         }
494
495         /* Read anything outstanding */
496         while(read(p->fd, dummy, sizeof(dummy)) > 0);
497
498         sprintf(dummy, "%c%c", 0x10, 0x14);
499         if (write(p->fd, dummy, 2) < 0) {
500                 ast_log(LOG_WARNING, "Failed to break\n");
501                 return -1;
502         }
503         ast_modem_expect(p, "VCON", 1);
504 #if 0
505         if (ast_modem_expect(p, "VCON", 8)) {
506                 ast_log(LOG_WARNING, "Didn't get expected VCON\n");
507                 return -1;
508         }
509 #endif
510         write(p->fd, "\n\n", 2);
511         read(p->fd, dummy, sizeof(dummy));
512         /* Hangup by switching to data, then back to voice */
513         if (ast_modem_send(p, "ATH", 0) ||
514              ast_modem_expect(p, "NO CARRIER", 8)) {
515                 ast_log(LOG_WARNING, "Unable to hang up\n");
516                 return -1;
517         }
518         if (ast_modem_expect(p, "OK", 5)) {
519                 ast_log(LOG_WARNING, "Final 'OK' not received\n");
520                 return -1;
521         }
522         return 0;
523 }
524
525 static struct ast_modem_driver i4l_driver =
526 {
527         "i4l",
528         i4l_idents,
529         AST_FORMAT_SLINEAR,
530         0,              /* Not full duplex */
531         i4l_incusecnt,  /* incusecnt */
532         i4l_decusecnt,  /* decusecnt */
533         i4l_identify,   /* identify */
534         i4l_init,       /* init */
535         i4l_setdev,     /* setdev */
536         i4l_read,
537         i4l_write,
538         i4l_dial,       /* dial */
539         i4l_answer,     /* answer */
540         i4l_hangup,     /* hangup */
541         i4l_startrec,   /* start record */
542         NULL,   /* stop record */
543         NULL,   /* start playback */
544         NULL,   /* stop playback */
545         NULL,   /* set silence supression */
546         i4l_dialdigit,  /* dialdigit */
547 };
548
549
550
551 int usecount(void)
552 {
553         int res;
554         pthread_mutex_lock(&usecnt_lock);
555         res = usecnt;
556         pthread_mutex_unlock(&usecnt_lock);
557         return res;
558 }
559
560 int load_module(void)
561 {
562         return ast_register_modem_driver(&i4l_driver);
563 }
564
565 int unload_module(void)
566 {
567         return ast_unregister_modem_driver(&i4l_driver);
568 }
569
570 char *description()
571 {
572         return desc;
573 }
574
575 char *key()
576 {
577         return ASTERISK_GPL_KEY;
578 }