Version 0.1.12 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 <asterisk/lock.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 = AST_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)-1);
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=9", 0) ||
165              ast_modem_expect(p, "OK", 5)) {
166                 ast_log(LOG_WARNING, "Unable to set to transparent/ringing 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 'I': /* Pseudo ringing */
204                 p->fr.frametype = AST_FRAME_CONTROL;
205                 p->fr.subclass =  AST_CONTROL_RINGING;
206                 return &p->fr;
207         case 'X': /* Pseudo connect */
208                 p->fr.frametype = AST_FRAME_CONTROL;
209                 p->fr.subclass = AST_CONTROL_ANSWER;
210                 if (p->owner)
211                         p->owner->state = AST_STATE_UP;
212                 if (i4l_startrec(p))
213                         return  NULL;
214                 return &p->fr;
215         case 'b': /* Busy signal */
216                 p->fr.frametype = AST_FRAME_CONTROL;
217                 p->fr.subclass = AST_CONTROL_BUSY;
218                 return &p->fr;
219         case 'o': /* Overrun */
220                 ast_log(LOG_WARNING, "Overflow on modem, flushing buffers\n");
221                 if (ast_modem_send(p, "\0x10E", 2)) 
222                         ast_log(LOG_WARNING, "Unable to flush buffers\n");
223                 return &p->fr;  
224         case CHAR_ETX: /* End Transmission */
225                 return NULL;
226         case 'u': /* Underrun */
227                 ast_log(LOG_WARNING, "Data underrun\n");
228                 /* Fall Through */
229         case 'd': /* Dialtone */
230         case 'c': /* Calling Tone */
231         case 'e': /* European version */
232         case 'a': /* Answer Tone */
233         case 'f': /* Bell Answer Tone */
234         case 'T': /* Timing mark */
235         case 't': /* Handset off hook */
236         case 'h': /* Handset hungup */
237                 /* Ignore */
238                 if (option_debug)
239                         ast_log(LOG_DEBUG, "Ignoring Escaped character '%c' (%d)\n", esc, esc);
240                 return &p->fr;
241         case '0':
242         case '1':
243         case '2':
244         case '3':
245         case '4':
246         case '5':
247         case '6':
248         case '7':
249         case '8':
250         case '9':
251         case '*':
252         case '#':
253                 ast_log(LOG_DEBUG, "DTMF: '%c' (%d)\n", esc, esc);
254                 p->fr.frametype=AST_FRAME_DTMF;
255                 p->fr.subclass=esc;
256                 return &p->fr;
257         case 0: /* Pseudo signal */
258                 return &p->fr;
259         default:
260                 ast_log(LOG_DEBUG, "Unknown Escaped character '%c' (%d)\n", esc, esc);
261         }
262         return &p->fr;
263 }
264
265 static struct ast_frame *i4l_read(struct ast_modem_pvt *p)
266 {
267         char result[256];
268         short *b;
269         struct ast_frame *f=NULL;
270         int res;
271         int x;
272         if (p->ministate == STATE_COMMAND) {
273                 /* Read the first two bytes, first, in case it's a control message */
274                 read(p->fd, result, 2);
275                 if (result[0] == CHAR_DLE) {
276                         return i4l_handle_escape(p, result[1]);
277                         
278                 } else {
279                         if ((result[0] == '\n') || (result[0] == '\r'))
280                                 return i4l_handle_escape(p, 0);
281                         /* Read the rest of the line */
282                         fgets(result + 2, sizeof(result) - 2, p->f);
283                         ast_modem_trim(result);
284                         if (!strcasecmp(result, "VCON")) {
285                                 /* If we're in immediate mode, reply now */
286                                 if (p->mode == MODEM_MODE_IMMEDIATE)
287                                         return i4l_handle_escape(p, 'X');
288                         } else
289                         if (!strcasecmp(result, "BUSY")) {
290                                 /* Same as a busy signal */
291                                 return i4l_handle_escape(p, 'b');
292                         } else
293                         if (!strncasecmp(result, "CALLER NUMBER: ", 15 )) {
294                                 strncpy(p->cid, result + 15, sizeof(p->cid)-1);
295                                 return i4l_handle_escape(p, 'R');
296                         } else
297                         if (!strcasecmp(result, "RINGING")) {
298                                 if (option_verbose > 2)
299                                         ast_verbose(VERBOSE_PREFIX_3 "%s is ringing...\n", p->dev);
300                                 return i4l_handle_escape(p, 'I');
301                         } else
302                         if (!strncasecmp(result, "RING", 4)) {
303                                 if (result[4]=='/') 
304                                         strncpy(p->dnid, result + 4, sizeof(p->dnid)-1);
305                                 return i4l_handle_escape(p, 'R');
306                         } else
307                         if (!strcasecmp(result, "NO CARRIER")) {
308                                 if (option_verbose > 2) 
309                                         ast_verbose(VERBOSE_PREFIX_3 "%s hung up on\n", p->dev);
310                                 return NULL;
311                         } else
312                         if (!strcasecmp(result, "NO DIALTONE")) {
313                                 /* There's no dialtone, so the line isn't working */
314                                 ast_log(LOG_WARNING, "Device '%s' lacking dialtone\n", p->dev);
315                                 return NULL;
316                         }
317                         if (option_debug)
318                                 ast_log(LOG_DEBUG, "Modem said '%s'\n", result);
319                         return i4l_handle_escape(p, 0);
320                 }
321         } else {
322                 /* We have to be more efficient in voice mode */
323                 b = (short *)(p->obuf + p->obuflen);
324                 while (p->obuflen/2 < 240) {
325                         /* Read ahead the full amount */
326                         res = read(p->fd, result, 240 - p->obuflen/2);
327                         if (res < 1) {
328                                 /* If there's nothing there, just continue on */
329                                 if (errno == EAGAIN)
330                                         return i4l_handle_escape(p, 0);
331                                 ast_log(LOG_WARNING, "Read failed: %s\n", strerror(errno));
332                         }
333                         
334                         for (x=0;x<res;x++) {
335                                 /* Process all the bytes that we've read */
336                                 switch(result[x]) {
337                                 case CHAR_DLE:
338 #if 0
339                                         ast_log(LOG_DEBUG, "Ooh, an escape at %d...\n", x);
340 #endif
341                                         if (!p->escape) {
342                                                 /* Note that next value is
343                                                    an escape, and continue. */
344                                                 p->escape++;
345                                                 break;
346                                         } else {
347                                                 /* Send as is -- fallthrough */
348                                                 p->escape = 0;
349                                         }
350                                 default:
351                                         if (p->escape) {
352                                                 ast_log(LOG_DEBUG, "Value of escape is %c (%d)...\n", result[x] < 32 ? '^' : result[x], result[x]);
353                                                 p->escape = 0;
354                                                 if (f) 
355                                                         ast_log(LOG_WARNING, "Warning: Dropped a signal frame\n");
356                                                 f = i4l_handle_escape(p, result[x]);
357                                                 /* If i4l_handle_escape says NULL, say it now, doesn't matter
358                                                 what else is there, the connection is dead. */
359                                                 if (!f)
360                                                         return NULL;
361                                         } else {
362                                                 *(b++) = ALAW2INT(result[x] & 0xff);
363                                                 p->obuflen += 2;
364                                         }
365                                 }
366                         }
367                         if (f)
368                                 break;
369                 }
370                 if (f)
371                         return f;
372                 /* If we get here, we have a complete voice frame */
373                 p->fr.frametype = AST_FRAME_VOICE;
374                 p->fr.subclass = AST_FORMAT_SLINEAR;
375                 p->fr.timelen = 30;
376                 p->fr.data = p->obuf;
377                 p->fr.datalen = p->obuflen;
378                 p->fr.mallocd = 0;
379                 p->fr.offset = AST_FRIENDLY_OFFSET;
380                 p->fr.src = __FUNCTION__;
381                 p->obuflen = 0;
382                 return &p->fr;
383         }
384         return NULL;
385 }
386
387 static int i4l_write(struct ast_modem_pvt *p, struct ast_frame *f)
388 {
389 #define MAX_WRITE_SIZE 1024
390         unsigned char result[MAX_WRITE_SIZE << 1];
391         unsigned char b;
392         int bpos=0, x;
393         int res;
394         if (f->datalen > MAX_WRITE_SIZE) {
395                 ast_log(LOG_WARNING, "Discarding too big frame of size %d\n", f->datalen);
396                 return -1;
397         }
398         if (f->frametype != AST_FRAME_VOICE) {
399                 ast_log(LOG_WARNING, "Don't know how to handle %d type frames\n", f->frametype);
400                 return -1;
401         }
402         if (f->subclass != AST_FORMAT_SLINEAR) {
403                 ast_log(LOG_WARNING, "Don't know how to handle anything but signed linear frames\n");
404                 return -1;
405         }
406         for (x=0;x<f->datalen/2;x++) {
407                 b = INT2ALAW(((short *)f->data)[x]);
408                 result[bpos++] = b;
409                 if (b == CHAR_DLE)
410                         result[bpos++]=b;
411         }
412 #if 0
413         res = fwrite(result, bpos, 1, p->f);
414         res *= bpos;
415 #else
416         res = write(p->fd, result, bpos);
417 #endif
418         if (res < 1) {
419                 if (errno != EAGAIN) {
420                         ast_log(LOG_WARNING, "Failed to write buffer\n");
421                         return -1;
422                 }
423         }
424 #if 0
425         printf("Result of write is %d\n", res);
426 #endif
427         return 0;
428 }
429
430 static char *i4l_identify(struct ast_modem_pvt *p)
431 {
432         return strdup("Linux ISDN");
433 }
434
435 static void i4l_incusecnt()
436 {
437         ast_pthread_mutex_lock(&usecnt_lock);
438         usecnt++;
439         ast_pthread_mutex_unlock(&usecnt_lock);
440         ast_update_use_count();
441 }
442
443 static void i4l_decusecnt()
444 {
445         ast_pthread_mutex_lock(&usecnt_lock);
446         usecnt++;
447         ast_pthread_mutex_unlock(&usecnt_lock);
448         ast_update_use_count();
449 }
450
451 static int i4l_answer(struct ast_modem_pvt *p)
452 {
453         if (ast_modem_send(p, "ATA\r", 4) ||
454              ast_modem_expect(p, "VCON", 10)) {
455                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
456                 return -1;
457         }
458 #if 1
459         if (ast_modem_send(p, "AT+VDD=0,8", 0) ||
460              ast_modem_expect(p, "OK", 5)) {
461                 ast_log(LOG_WARNING, "Unable to set to phone line interface\n");
462                 return -1;
463         }
464 #endif
465         if (ast_modem_send(p, "AT+VTX+VRX", 0) ||
466              ast_modem_expect(p, "CONNECT", 10)) {
467                 ast_log(LOG_WARNING, "Unable to answer: %s", p->response);
468                 return -1;
469         }
470         p->ministate = STATE_VOICE;
471         return 0;
472 }
473
474 static int i4l_dialdigit(struct ast_modem_pvt *p, char digit)
475 {
476         char c[2];
477         if (p->ministate == STATE_VOICE) {
478                 c[0] = CHAR_DLE;
479                 c[1] = digit;
480                 write(p->fd, c, 2);
481         } else
482                 ast_log(LOG_DEBUG, "Asked to send digit but call not up on %s\n", p->dev);
483         return 0;
484 }
485
486 static int i4l_dial(struct ast_modem_pvt *p, char *stuff)
487 {
488         char cmd[80];
489         snprintf(cmd, sizeof(cmd), "ATD%c %s\n", p->dialtype,stuff);
490         if (ast_modem_send(p, cmd, strlen(cmd))) {
491                 ast_log(LOG_WARNING, "Unable to dial\n");
492                 return -1;
493         }
494         return 0;
495 }
496
497 static int i4l_hangup(struct ast_modem_pvt *p)
498 {
499         char dummy[50];
500         sprintf(dummy, "%c%c", 0x10, 0x3);
501         if (write(p->fd, dummy, 2) < 0) {
502                 ast_log(LOG_WARNING, "Failed to break\n");
503                 return -1;
504         }
505
506         /* Read anything outstanding */
507         while(read(p->fd, dummy, sizeof(dummy)) > 0);
508
509         sprintf(dummy, "%c%c", 0x10, 0x14);
510         if (write(p->fd, dummy, 2) < 0) {
511                 ast_log(LOG_WARNING, "Failed to break\n");
512                 return -1;
513         }
514         ast_modem_expect(p, "VCON", 1);
515 #if 0
516         if (ast_modem_expect(p, "VCON", 8)) {
517                 ast_log(LOG_WARNING, "Didn't get expected VCON\n");
518                 return -1;
519         }
520 #endif
521         write(p->fd, "\n\n", 2);
522         read(p->fd, dummy, sizeof(dummy));
523         /* Hangup by switching to data, then back to voice */
524         if (ast_modem_send(p, "ATH", 0) ||
525              ast_modem_expect(p, "NO CARRIER", 8)) {
526                 ast_log(LOG_WARNING, "Unable to hang up\n");
527                 return -1;
528         }
529         if (ast_modem_expect(p, "OK", 5)) {
530                 ast_log(LOG_WARNING, "Final 'OK' not received\n");
531                 return -1;
532         }
533         return 0;
534 }
535
536 static struct ast_modem_driver i4l_driver =
537 {
538         "i4l",
539         i4l_idents,
540         AST_FORMAT_SLINEAR,
541         0,              /* Not full duplex */
542         i4l_incusecnt,  /* incusecnt */
543         i4l_decusecnt,  /* decusecnt */
544         i4l_identify,   /* identify */
545         i4l_init,       /* init */
546         i4l_setdev,     /* setdev */
547         i4l_read,
548         i4l_write,
549         i4l_dial,       /* dial */
550         i4l_answer,     /* answer */
551         i4l_hangup,     /* hangup */
552         i4l_startrec,   /* start record */
553         NULL,   /* stop record */
554         NULL,   /* start playback */
555         NULL,   /* stop playback */
556         NULL,   /* set silence supression */
557         i4l_dialdigit,  /* dialdigit */
558 };
559
560
561
562 int usecount(void)
563 {
564         int res;
565         ast_pthread_mutex_lock(&usecnt_lock);
566         res = usecnt;
567         ast_pthread_mutex_unlock(&usecnt_lock);
568         return res;
569 }
570
571 int load_module(void)
572 {
573         return ast_register_modem_driver(&i4l_driver);
574 }
575
576 int unload_module(void)
577 {
578         return ast_unregister_modem_driver(&i4l_driver);
579 }
580
581 char *description()
582 {
583         return desc;
584 }
585
586 char *key()
587 {
588         return ASTERISK_GPL_KEY;
589 }