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
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>
28 #define STATE_COMMAND 0
31 static char *breakcmd = "\0x10\0x14\0x10\0x3";
33 static char *desc = "ISDN4Linux Emulated Modem Driver";
36 pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
38 static char *i4l_idents[] = {
39 /* Identify ISDN4Linux Driver */
44 static int i4l_setdev(struct ast_modem_pvt *p, int dev)
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);
50 } else /* Convert DEV to our understanding of it */
52 if (ast_modem_send(p, "AT+VLS?", 0)) {
53 ast_log(LOG_WARNING, "Unable to select current mode %d\n", dev);
56 if (ast_modem_read_response(p, 5)) {
57 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
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");
66 if (dev == atoi(cmd)) {
67 /* We're already in the right mode, don't bother changing for fear of
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);
76 if (ast_modem_read_response(p, 5)) {
77 ast_log(LOG_WARNING, "Unable to select device %d\n", dev);
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);
88 static int i4l_startrec(struct ast_modem_pvt *p)
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");
95 p->ministate = STATE_VOICE;
99 static int i4l_break(struct ast_modem_pvt *p)
101 if (ast_modem_send(p, breakcmd, 2)) {
102 ast_log(LOG_WARNING, "Failed to break\n");
105 if (ast_modem_send(p, "\r\n", 2)) {
106 ast_log(LOG_WARNING, "Failed to send enter?\n");
110 /* Read any outstanding junk */
111 while(!ast_modem_read_response(p, 1));
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");
120 if (ast_modem_send(p, "AT", 0)) {
121 ast_log(LOG_WARNING, "Modem is not responding\n");
125 if (ast_modem_expect(p, "OK", 5)) {
126 ast_log(LOG_WARNING, "Modem did not respond properly\n");
132 static int i4l_init(struct ast_modem_pvt *p)
136 ast_log(LOG_DEBUG, "i4l_init()\n");
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");
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);
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");
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");
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");
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");
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");
184 static struct ast_frame *i4l_handle_escape(struct ast_modem_pvt *p, char esc)
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;
195 if (esc && option_debug)
196 ast_log(LOG_DEBUG, "Escaped character '%c'\n", esc);
199 case 'R': /* Pseudo ring */
200 p->fr.frametype = AST_FRAME_CONTROL;
201 p->fr.subclass = AST_CONTROL_RING;
203 case 'X': /* Pseudo connect */
204 p->fr.frametype = AST_FRAME_CONTROL;
205 p->fr.subclass = AST_CONTROL_ANSWER;
207 p->owner->state = AST_STATE_UP;
211 case 'b': /* Busy signal */
212 p->fr.frametype = AST_FRAME_CONTROL;
213 p->fr.subclass = AST_CONTROL_BUSY;
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");
220 case CHAR_ETX: /* End Transmission */
222 case 'u': /* Underrun */
223 ast_log(LOG_WARNING, "Data underrun\n");
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 */
235 ast_log(LOG_DEBUG, "Ignoring Escaped character '%c' (%d)\n", esc, esc);
249 ast_log(LOG_DEBUG, "DTMF: '%c' (%d)\n", esc, esc);
250 p->fr.frametype=AST_FRAME_DTMF;
253 case 0: /* Pseudo signal */
256 ast_log(LOG_DEBUG, "Unknown Escaped character '%c' (%d)\n", esc, esc);
261 static struct ast_frame *i4l_read(struct ast_modem_pvt *p)
265 struct ast_frame *f=NULL;
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]);
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');
285 if (!strcasecmp(result, "BUSY")) {
286 /* Same as a busy signal */
287 return i4l_handle_escape(p, 'b');
289 if (!strncasecmp(result, "CALLER NUMBER: ", 15 )) {
290 strncpy(p->cid, result + 15, sizeof(p->cid));
291 return i4l_handle_escape(p, 'R');
293 if (!strncasecmp(result, "RING", 4)) {
295 strncpy(p->dnid, result + 4, sizeof(p->dnid));
296 return i4l_handle_escape(p, 'R');
298 if (!strcasecmp(result, "NO CARRIER")) {
299 if (option_verbose > 2)
300 ast_verbose(VERBOSE_PREFIX_3 "%s hung up on\n", p->dev);
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);
309 ast_log(LOG_DEBUG, "Modem said '%s'\n", result);
310 return i4l_handle_escape(p, 0);
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);
319 /* If there's nothing there, just continue on */
321 return i4l_handle_escape(p, 0);
322 ast_log(LOG_WARNING, "Read failed: %s\n", strerror(errno));
325 for (x=0;x<res;x++) {
326 /* Process all the bytes that we've read */
330 ast_log(LOG_DEBUG, "Ooh, an escape at %d...\n", x);
333 /* Note that next value is
334 an escape, and continue. */
338 /* Send as is -- fallthrough */
343 ast_log(LOG_DEBUG, "Value of escape is %c (%d)...\n", result[x] < 32 ? '^' : result[x], result[x]);
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. */
353 *(b++) = ALAW2INT(result[x] & 0xff);
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;
367 p->fr.data = p->obuf;
368 p->fr.datalen = p->obuflen;
370 p->fr.offset = AST_FRIENDLY_OFFSET;
371 p->fr.src = __FUNCTION__;
378 static int i4l_write(struct ast_modem_pvt *p, struct ast_frame *f)
380 #define MAX_WRITE_SIZE 512
381 unsigned char result[MAX_WRITE_SIZE << 1];
385 if (f->datalen > MAX_WRITE_SIZE) {
386 ast_log(LOG_WARNING, "Discarding too big frame of size %d\n", f->datalen);
389 if (f->frametype != AST_FRAME_VOICE) {
390 ast_log(LOG_WARNING, "Don't know how to handle %d type frames\n", f->frametype);
393 if (f->subclass != AST_FORMAT_SLINEAR) {
394 ast_log(LOG_WARNING, "Don't know how to handle anything but signed linear frames\n");
397 for (x=0;x<f->datalen/2;x++) {
398 b = INT2ALAW(((short *)f->data)[x]);
404 res = fwrite(result, bpos, 1, p->f);
407 res = write(p->fd, result, bpos);
410 ast_log(LOG_WARNING, "Failed to write buffer\n");
414 printf("Result of write is %d\n", res);
419 static char *i4l_identify(struct ast_modem_pvt *p)
421 return strdup("Linux ISDN");
424 static void i4l_incusecnt()
426 ast_pthread_mutex_lock(&usecnt_lock);
428 ast_pthread_mutex_unlock(&usecnt_lock);
429 ast_update_use_count();
432 static void i4l_decusecnt()
434 ast_pthread_mutex_lock(&usecnt_lock);
436 ast_pthread_mutex_unlock(&usecnt_lock);
437 ast_update_use_count();
440 static int i4l_answer(struct ast_modem_pvt *p)
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);
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");
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);
459 p->ministate = STATE_VOICE;
463 static int i4l_dialdigit(struct ast_modem_pvt *p, char digit)
466 if (p->ministate == STATE_VOICE) {
471 ast_log(LOG_DEBUG, "Asked to send digit but call not up on %s\n", p->dev);
475 static int i4l_dial(struct ast_modem_pvt *p, char *stuff)
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");
486 static int i4l_hangup(struct ast_modem_pvt *p)
489 sprintf(dummy, "%c%c", 0x10, 0x3);
490 if (write(p->fd, dummy, 2) < 0) {
491 ast_log(LOG_WARNING, "Failed to break\n");
495 /* Read anything outstanding */
496 while(read(p->fd, dummy, sizeof(dummy)) > 0);
498 sprintf(dummy, "%c%c", 0x10, 0x14);
499 if (write(p->fd, dummy, 2) < 0) {
500 ast_log(LOG_WARNING, "Failed to break\n");
503 ast_modem_expect(p, "VCON", 1);
505 if (ast_modem_expect(p, "VCON", 8)) {
506 ast_log(LOG_WARNING, "Didn't get expected VCON\n");
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");
518 if (ast_modem_expect(p, "OK", 5)) {
519 ast_log(LOG_WARNING, "Final 'OK' not received\n");
525 static struct ast_modem_driver i4l_driver =
530 0, /* Not full duplex */
531 i4l_incusecnt, /* incusecnt */
532 i4l_decusecnt, /* decusecnt */
533 i4l_identify, /* identify */
535 i4l_setdev, /* setdev */
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 */
554 ast_pthread_mutex_lock(&usecnt_lock);
556 ast_pthread_mutex_unlock(&usecnt_lock);
560 int load_module(void)
562 return ast_register_modem_driver(&i4l_driver);
565 int unload_module(void)
567 return ast_unregister_modem_driver(&i4l_driver);
577 return ASTERISK_GPL_KEY;