2 * Asterisk -- A telephony toolkit for Linux.
4 * ISDN4Linux TTY Driver
6 * Copyright (C) 2001, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
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>
29 #define STATE_COMMAND 0
32 static char *breakcmd = "\0x10\0x14\0x10\0x3";
34 static char *desc = "ISDN4Linux Emulated Modem Driver";
37 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
39 static char *i4l_idents[] = {
40 /* Identify ISDN4Linux Driver */
45 static int i4l_setdev(struct ast_modem_pvt *p, int dev)
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);
51 } else /* Convert DEV to our understanding of it */
53 if (ast_modem_send(p, "AT+VLS?", 0)) {
54 ast_log(LOG_WARNING, "Unable to select current mode %d\n", dev);
57 if (ast_modem_read_response(p, 5)) {
58 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
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");
67 if (dev == atoi(cmd)) {
68 /* We're already in the right mode, don't bother changing for fear of
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);
77 if (ast_modem_read_response(p, 5)) {
78 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
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);
89 static int i4l_startrec(struct ast_modem_pvt *p)
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");
96 p->ministate = STATE_VOICE;
100 static int i4l_break(struct ast_modem_pvt *p)
102 if (ast_modem_send(p, breakcmd, 2)) {
103 ast_log(LOG_WARNING, "Failed to break\n");
106 if (ast_modem_send(p, "\r\n", 2)) {
107 ast_log(LOG_WARNING, "Failed to send enter?\n");
111 /* Read any outstanding junk */
112 while(!ast_modem_read_response(p, 1));
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");
121 if (ast_modem_send(p, "AT", 0)) {
122 ast_log(LOG_WARNING, "Modem is not responding\n");
126 if (ast_modem_expect(p, "OK", 5)) {
127 ast_log(LOG_WARNING, "Modem did not respond properly\n");
133 static int i4l_init(struct ast_modem_pvt *p)
137 ast_log(LOG_DEBUG, "i4l_init()\n");
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");
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);
155 if (strlen(p->incomingmsn)) {
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.
162 if (*q == ',') *q = ';';
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);
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");
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");
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");
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");
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");
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");
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");
211 static struct ast_frame *i4l_handle_escape(struct ast_modem_pvt *p, char esc)
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;
222 if (esc && option_debug)
223 ast_log(LOG_DEBUG, "Escaped character '%c'\n", esc);
226 case 'R': /* Pseudo ring */
227 p->fr.frametype = AST_FRAME_CONTROL;
228 p->fr.subclass = AST_CONTROL_RING;
230 case 'I': /* Pseudo ringing */
231 p->fr.frametype = AST_FRAME_CONTROL;
232 p->fr.subclass = AST_CONTROL_RINGING;
234 case 'X': /* Pseudo connect */
235 p->fr.frametype = AST_FRAME_CONTROL;
236 p->fr.subclass = AST_CONTROL_ANSWER;
238 ast_setstate(p->owner, AST_STATE_UP);
242 case 'b': /* Busy signal */
243 p->fr.frametype = AST_FRAME_CONTROL;
244 p->fr.subclass = AST_CONTROL_BUSY;
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");
251 case CHAR_ETX: /* End Transmission */
253 case 'u': /* Underrun */
254 ast_log(LOG_WARNING, "Data underrun\n");
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 */
266 ast_log(LOG_DEBUG, "Ignoring Escaped character '%c' (%d)\n", esc, esc);
280 ast_log(LOG_DEBUG, "DTMF: '%c' (%d)\n", esc, esc);
281 p->fr.frametype=AST_FRAME_DTMF;
284 case 0: /* Pseudo signal */
287 ast_log(LOG_DEBUG, "Unknown Escaped character '%c' (%d)\n", esc, esc);
292 static struct ast_frame *i4l_read(struct ast_modem_pvt *p)
296 struct ast_frame *f=NULL;
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);
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.
307 return i4l_handle_escape(p, 0);
310 if (result[0] == CHAR_DLE) {
311 return i4l_handle_escape(p, result[1]);
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');
324 if (!strcasecmp(result, "BUSY")) {
325 /* Same as a busy signal */
326 return i4l_handle_escape(p, 'b');
328 if (!strncasecmp(result, "CALLER NUMBER: ", 15 )) {
329 strncpy(p->cid, result + 15, sizeof(p->cid)-1);
330 return i4l_handle_escape(p, 0);
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');
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);
343 if (!strncasecmp(result, "RING", 4)) {
345 strncpy(p->dnid, result + 5, sizeof(p->dnid)-1);
346 return i4l_handle_escape(p, 'R');
348 if (!strcasecmp(result, "NO CARRIER")) {
349 if (option_verbose > 2)
350 ast_verbose(VERBOSE_PREFIX_3 "%s hung up on\n", p->dev);
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);
359 ast_log(LOG_DEBUG, "Modem said '%s'\n", result);
360 return i4l_handle_escape(p, 0);
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);
369 /* If there's nothing there, just continue on */
371 return i4l_handle_escape(p, 0);
372 ast_log(LOG_WARNING, "Read failed: %s\n", strerror(errno));
376 for (x=0;x<res;x++) {
377 /* Process all the bytes that we've read */
381 ast_log(LOG_DEBUG, "Ooh, an escape at %d...\n", x);
384 /* Note that next value is
385 an escape, and continue. */
389 /* Send as is -- fallthrough */
394 ast_log(LOG_DEBUG, "Value of escape is %c (%d)...\n", result[x] < 32 ? '^' : result[x], result[x]);
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. */
404 *(b++) = ALAW2INT(result[x] & 0xff);
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;
418 p->fr.data = p->obuf;
419 p->fr.datalen = p->obuflen;
421 p->fr.offset = AST_FRIENDLY_OFFSET;
422 p->fr.src = __FUNCTION__;
429 static int i4l_write(struct ast_modem_pvt *p, struct ast_frame *f)
431 #define MAX_WRITE_SIZE 2048
432 unsigned char result[MAX_WRITE_SIZE << 1];
436 if (f->datalen > MAX_WRITE_SIZE) {
437 ast_log(LOG_WARNING, "Discarding too big frame of size %d\n", f->datalen);
440 if (f->frametype != AST_FRAME_VOICE) {
441 ast_log(LOG_WARNING, "Don't know how to handle %d type frames\n", f->frametype);
444 if (f->subclass != AST_FORMAT_SLINEAR) {
445 ast_log(LOG_WARNING, "Don't know how to handle anything but signed linear frames\n");
448 for (x=0;x<f->datalen/2;x++) {
449 b = INT2ALAW(((short *)f->data)[x]);
455 res = fwrite(result, bpos, 1, p->f);
458 res = write(p->fd, result, bpos);
461 if (errno != EAGAIN) {
462 ast_log(LOG_WARNING, "Failed to write buffer\n");
467 printf("Result of write is %d\n", res);
472 static char *i4l_identify(struct ast_modem_pvt *p)
474 return strdup("Linux ISDN");
477 static void i4l_incusecnt(void)
479 ast_mutex_lock(&usecnt_lock);
481 ast_mutex_unlock(&usecnt_lock);
482 ast_update_use_count();
485 static void i4l_decusecnt(void)
487 ast_mutex_lock(&usecnt_lock);
489 ast_mutex_unlock(&usecnt_lock);
490 ast_update_use_count();
493 static int i4l_answer(struct ast_modem_pvt *p)
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);
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");
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);
512 p->ministate = STATE_VOICE;
516 static int i4l_dialdigit(struct ast_modem_pvt *p, char digit)
519 if (p->ministate == STATE_VOICE) {
524 ast_log(LOG_DEBUG, "Asked to send digit but call not up on %s\n", p->dev);
528 static int i4l_dial(struct ast_modem_pvt *p, char *stuff)
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");
539 static int i4l_hangup(struct ast_modem_pvt *p)
544 /* down DTR to hangup modem */
545 ioctl(p->fd, TIOCMBIC, &dtr);
547 /* Read anything outstanding */
548 while(read(p->fd, dummy, sizeof(dummy)) > 0);
550 /* rise DTR to re-enable line */
551 ioctl(p->fd, TIOCMBIS, &dtr);
553 /* Read anything outstanding */
554 while(read(p->fd, dummy, sizeof(dummy)) > 0);
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");
563 if (ast_modem_expect(p, "OK", 5)) {
564 ast_log(LOG_WARNING, "Final 'OK' not received\n");
571 static struct ast_modem_driver i4l_driver =
576 0, /* Not full duplex */
577 i4l_incusecnt, /* incusecnt */
578 i4l_decusecnt, /* decusecnt */
579 i4l_identify, /* identify */
581 i4l_setdev, /* setdev */
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 */
600 ast_mutex_lock(&usecnt_lock);
602 ast_mutex_unlock(&usecnt_lock);
606 int load_module(void)
608 return ast_register_modem_driver(&i4l_driver);
611 int unload_module(void)
613 return ast_unregister_modem_driver(&i4l_driver);
623 return ASTERISK_GPL_KEY;