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 <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>
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 = AST_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)-1);
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=9", 0) ||
165 ast_modem_expect(p, "OK", 5)) {
166 ast_log(LOG_WARNING, "Unable to set to transparent/ringing 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 'I': /* Pseudo ringing */
204 p->fr.frametype = AST_FRAME_CONTROL;
205 p->fr.subclass = AST_CONTROL_RINGING;
207 case 'X': /* Pseudo connect */
208 p->fr.frametype = AST_FRAME_CONTROL;
209 p->fr.subclass = AST_CONTROL_ANSWER;
211 p->owner->state = AST_STATE_UP;
215 case 'b': /* Busy signal */
216 p->fr.frametype = AST_FRAME_CONTROL;
217 p->fr.subclass = AST_CONTROL_BUSY;
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");
224 case CHAR_ETX: /* End Transmission */
226 case 'u': /* Underrun */
227 ast_log(LOG_WARNING, "Data underrun\n");
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 */
239 ast_log(LOG_DEBUG, "Ignoring Escaped character '%c' (%d)\n", esc, esc);
253 ast_log(LOG_DEBUG, "DTMF: '%c' (%d)\n", esc, esc);
254 p->fr.frametype=AST_FRAME_DTMF;
257 case 0: /* Pseudo signal */
260 ast_log(LOG_DEBUG, "Unknown Escaped character '%c' (%d)\n", esc, esc);
265 static struct ast_frame *i4l_read(struct ast_modem_pvt *p)
269 struct ast_frame *f=NULL;
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]);
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');
289 if (!strcasecmp(result, "BUSY")) {
290 /* Same as a busy signal */
291 return i4l_handle_escape(p, 'b');
293 if (!strncasecmp(result, "CALLER NUMBER: ", 15 )) {
294 strncpy(p->cid, result + 15, sizeof(p->cid)-1);
295 return i4l_handle_escape(p, 'R');
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');
302 if (!strncasecmp(result, "RING", 4)) {
304 strncpy(p->dnid, result + 4, sizeof(p->dnid)-1);
305 return i4l_handle_escape(p, 'R');
307 if (!strcasecmp(result, "NO CARRIER")) {
308 if (option_verbose > 2)
309 ast_verbose(VERBOSE_PREFIX_3 "%s hung up on\n", p->dev);
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);
318 ast_log(LOG_DEBUG, "Modem said '%s'\n", result);
319 return i4l_handle_escape(p, 0);
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);
328 /* If there's nothing there, just continue on */
330 return i4l_handle_escape(p, 0);
331 ast_log(LOG_WARNING, "Read failed: %s\n", strerror(errno));
334 for (x=0;x<res;x++) {
335 /* Process all the bytes that we've read */
339 ast_log(LOG_DEBUG, "Ooh, an escape at %d...\n", x);
342 /* Note that next value is
343 an escape, and continue. */
347 /* Send as is -- fallthrough */
352 ast_log(LOG_DEBUG, "Value of escape is %c (%d)...\n", result[x] < 32 ? '^' : result[x], result[x]);
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. */
362 *(b++) = ALAW2INT(result[x] & 0xff);
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;
376 p->fr.data = p->obuf;
377 p->fr.datalen = p->obuflen;
379 p->fr.offset = AST_FRIENDLY_OFFSET;
380 p->fr.src = __FUNCTION__;
387 static int i4l_write(struct ast_modem_pvt *p, struct ast_frame *f)
389 #define MAX_WRITE_SIZE 1024
390 unsigned char result[MAX_WRITE_SIZE << 1];
394 if (f->datalen > MAX_WRITE_SIZE) {
395 ast_log(LOG_WARNING, "Discarding too big frame of size %d\n", f->datalen);
398 if (f->frametype != AST_FRAME_VOICE) {
399 ast_log(LOG_WARNING, "Don't know how to handle %d type frames\n", f->frametype);
402 if (f->subclass != AST_FORMAT_SLINEAR) {
403 ast_log(LOG_WARNING, "Don't know how to handle anything but signed linear frames\n");
406 for (x=0;x<f->datalen/2;x++) {
407 b = INT2ALAW(((short *)f->data)[x]);
413 res = fwrite(result, bpos, 1, p->f);
416 res = write(p->fd, result, bpos);
419 if (errno != EAGAIN) {
420 ast_log(LOG_WARNING, "Failed to write buffer\n");
425 printf("Result of write is %d\n", res);
430 static char *i4l_identify(struct ast_modem_pvt *p)
432 return strdup("Linux ISDN");
435 static void i4l_incusecnt()
437 ast_pthread_mutex_lock(&usecnt_lock);
439 ast_pthread_mutex_unlock(&usecnt_lock);
440 ast_update_use_count();
443 static void i4l_decusecnt()
445 ast_pthread_mutex_lock(&usecnt_lock);
447 ast_pthread_mutex_unlock(&usecnt_lock);
448 ast_update_use_count();
451 static int i4l_answer(struct ast_modem_pvt *p)
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);
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");
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);
470 p->ministate = STATE_VOICE;
474 static int i4l_dialdigit(struct ast_modem_pvt *p, char digit)
477 if (p->ministate == STATE_VOICE) {
482 ast_log(LOG_DEBUG, "Asked to send digit but call not up on %s\n", p->dev);
486 static int i4l_dial(struct ast_modem_pvt *p, char *stuff)
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");
497 static int i4l_hangup(struct ast_modem_pvt *p)
500 sprintf(dummy, "%c%c", 0x10, 0x3);
501 if (write(p->fd, dummy, 2) < 0) {
502 ast_log(LOG_WARNING, "Failed to break\n");
506 /* Read anything outstanding */
507 while(read(p->fd, dummy, sizeof(dummy)) > 0);
509 sprintf(dummy, "%c%c", 0x10, 0x14);
510 if (write(p->fd, dummy, 2) < 0) {
511 ast_log(LOG_WARNING, "Failed to break\n");
514 ast_modem_expect(p, "VCON", 1);
516 if (ast_modem_expect(p, "VCON", 8)) {
517 ast_log(LOG_WARNING, "Didn't get expected VCON\n");
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");
529 if (ast_modem_expect(p, "OK", 5)) {
530 ast_log(LOG_WARNING, "Final 'OK' not received\n");
536 static struct ast_modem_driver i4l_driver =
541 0, /* Not full duplex */
542 i4l_incusecnt, /* incusecnt */
543 i4l_decusecnt, /* decusecnt */
544 i4l_identify, /* identify */
546 i4l_setdev, /* setdev */
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 */
565 ast_pthread_mutex_lock(&usecnt_lock);
567 ast_pthread_mutex_unlock(&usecnt_lock);
571 int load_module(void)
573 return ast_register_modem_driver(&i4l_driver);
576 int unload_module(void)
578 return ast_unregister_modem_driver(&i4l_driver);
588 return ASTERISK_GPL_KEY;