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