2 * Asterisk -- A telephony toolkit for Linux.
4 * A/Open ITU-56/2 Voice Modem Driver (Rockwell, IS-101, and others)
6 * Copyright (C) 1999, 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
17 #include <asterisk/channel.h>
18 #include <asterisk/channel_pvt.h>
19 #include <asterisk/config.h>
20 #include <asterisk/logger.h>
21 #include <asterisk/module.h>
22 #include <asterisk/pbx.h>
23 #include <asterisk/options.h>
24 #include <asterisk/vmodem.h>
25 #include <sys/socket.h>
30 #include <arpa/inet.h>
32 #include <sys/ioctl.h>
33 #include <sys/termios.h>
34 #include <sys/signal.h>
37 /* Up to 10 seconds for an echo to arrive */
38 #define ECHO_TIMEOUT 10
40 static char *desc = "Generic Voice Modem Driver";
41 static char *tdesc = "Generic Voice Modem Channel Driver";
42 static char *type = "Modem";
43 static char *config = "modem.conf";
44 static char dialtype = 'T';
45 static int gmode = MODEM_MODE_IMMEDIATE;
47 /* Default modem type */
48 static char mtype[80] = "autodetect";
49 /* Default context for incoming calls */
50 static char context[AST_MAX_EXTENSION]= "default";
52 /* Default language */
53 static char language[MAX_LANGUAGE] = "";
55 /* Initialization String */
56 static char initstr[AST_MAX_INIT_STR] = "ATE1Q0";
60 static int baudrate = 115200;
62 static int stripmsd = 0;
64 static pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
66 /* Protect the interface list (of ast_modem_pvt's) */
67 static pthread_mutex_t iflock = PTHREAD_MUTEX_INITIALIZER;
69 /* Protect the monitoring thread, so only one process can kill or start it, and not
70 when it's doing something critical. */
71 static pthread_mutex_t monlock = PTHREAD_MUTEX_INITIALIZER;
73 /* This is the thread for the monitor which checks for input on the channels
74 which are not currently in use. */
75 static pthread_t monitor_thread = -1;
77 static int restart_monitor(void);
79 /* The private structures of the Phone Jack channels are linked for
80 selecting outgoing channels */
82 static struct ast_modem_pvt *iflist = NULL;
84 static int modem_digit(struct ast_channel *ast, char digit)
86 struct ast_modem_pvt *p;
89 return p->mc->dialdigit(p, digit);
90 else ast_log(LOG_DEBUG, "Channel %s lacks digit dialing\n");
94 static struct ast_modem_driver *drivers = NULL;
96 static struct ast_frame *modem_read(struct ast_channel *);
98 static struct ast_modem_driver *find_capability(char *ident)
100 struct ast_modem_driver *mc;
104 for (x=0;mc->idents[x];x++) {
105 if (!strcmp(ident, mc->idents[x]))
119 static struct ast_modem_driver *find_driver(char *drv)
121 struct ast_modem_driver *mc;
124 if (!strcasecmp(mc->name, drv))
135 int ast_register_modem_driver(struct ast_modem_driver *mc)
142 int ast_unregister_modem_driver(struct ast_modem_driver *mc)
144 struct ast_modem_driver *last = NULL, *cur;
149 last->next = mc->next;
159 static int modem_call(struct ast_channel *ast, char *idest, int timeout)
161 struct ast_modem_pvt *p;
163 char rdest[80], *where;
164 strncpy(rdest, idest, sizeof(rdest));
166 where = strtok(NULL, ":");
168 ast_log(LOG_WARNING, "Destination %s requres a real destination (device:destination)\n", idest);
172 if ((ast->state != AST_STATE_DOWN) && (ast->state != AST_STATE_RESERVED)) {
173 ast_log(LOG_WARNING, "modem_call called on %s, neither down nor reserved\n", ast->name);
176 if (!strcasecmp(where, "handset")) {
178 if (p->mc->setdev(p, MODEM_DEV_HANDSET))
180 /* Should be immediately up */
181 ast->state = AST_STATE_UP;
184 if (p->mc->setdev(p, MODEM_DEV_TELCO_SPK))
187 p->mc->dial(p, where + p->stripmsd);
188 ast->state = AST_STATE_DIALING;
189 while((ast->state != AST_STATE_UP) && (ms > 0)) {
190 ms = ast_waitfor(ast, ms);
191 /* Just read packets and watch what happens */
193 if (!modem_read(ast))
203 int ast_modem_send(struct ast_modem_pvt *p, char *cmd, int len)
207 fprintf(p->f, "%s\r\n", cmd);
208 res = ast_modem_expect(p, cmd, ECHO_TIMEOUT);
210 ast_log(LOG_WARNING, "Unexpected reply %s\n", p->response);
215 if (fwrite(cmd, 1, len, p->f) < len)
221 int ast_modem_read_response(struct ast_modem_pvt *p, int timeout)
225 strncpy(p->response, "(No Response)", sizeof(p->response));
227 res = ast_waitfor_n_fd(&p->fd, 1, &timeout);
231 /* Read a response */
232 fgets(p->response, sizeof(p->response), p->f);
234 } while(timeout > 0);
238 int ast_modem_expect(struct ast_modem_pvt *p, char *result, int timeout)
242 strncpy(p->response, "(No Response)", sizeof(p->response));
244 res = ast_waitfor_n_fd(&p->fd, 1, &timeout);
248 /* Read a response */
249 fgets(p->response, sizeof(p->response), p->f);
251 fprintf(stderr, "Modem said: %s", p->response);
253 if (!strncasecmp(p->response, result, strlen(result)))
255 } while(timeout > 0);
259 void ast_modem_trim(char *s)
264 if ((s[x] != '\r') && (s[x] != '\n') && (s[x] != ' '))
271 static int modem_setup(struct ast_modem_pvt *p, int baudrate)
273 /* Make sure there's a modem there and that it's in a reasonable
274 mode. Set the baud rate, etc. */
277 char etx[2] = { 0x10, 0x03 };
279 ast_log(LOG_DEBUG, "Setting up modem %s\n", p->dev);
280 if (ast_modem_send(p, etx, 2)) {
281 ast_log(LOG_WARNING, "Failed to send ETX?\n");
284 if (ast_modem_send(p, "\r\n", 2)) {
285 ast_log(LOG_WARNING, "Failed to send enter?\n");
288 /* Read any outstanding stuff */
289 while(!ast_modem_read_response(p, 0));
290 if (ast_modem_send(p, "ATZ", 0)) {
291 ast_log(LOG_WARNING, "Modem not responding on %s\n", p->dev);
294 if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
295 ast_log(LOG_WARNING, "Modem reset failed: %s\n", p->response);
298 if (ast_modem_send(p, p->initstr, 0)) {
299 ast_log(LOG_WARNING, "Modem not responding on %s\n", p->dev);
302 if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
303 ast_log(LOG_WARNING, "Modem initialization failed: %s\n", p->response);
306 if (ast_modem_send(p, "ATI3", 0)) {
307 ast_log(LOG_WARNING, "Modem not responding on %s\n", p->dev);
310 if (ast_modem_read_response(p, ECHO_TIMEOUT)) {
311 ast_log(LOG_WARNING, "Modem did not provide identification\n");
314 strncpy(identity, p->response, sizeof(identity));
315 ast_modem_trim(identity);
316 if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
317 ast_log(LOG_WARNING, "Modem did not provide identification\n");
320 if (!strcasecmp(mtype, "autodetect")) {
321 p->mc = find_capability(identity);
323 ast_log(LOG_WARNING, "Unable to autodetect modem. You'll need to specify a driver in modem.conf. Please report modem identification (%s) and which driver works to markster@linux-support.net.\n", identity);
327 p->mc = find_driver(mtype);
329 ast_log(LOG_WARNING, "No driver for modem type '%s'\n", mtype);
334 if (p->mc->init(p)) {
335 ast_log(LOG_WARNING, "Modem Initialization Failed on '%s', driver %s.\n", p->dev, p->mc->name);
340 if (option_verbose > 2) {
341 ast_verbose(VERBOSE_PREFIX_3 "Configured modem %s with driver %s (%s)\n", p->dev, p->mc->name, p->mc->identify ? (ident = p->mc->identify(p)) : "No identification");
348 static int modem_hangup(struct ast_channel *ast)
350 struct ast_modem_pvt *p;
351 ast_log(LOG_DEBUG, "modem_hangup(%s)\n", ast->name);
359 ast->state = AST_STATE_DOWN;
360 memset(p->cid, 0, sizeof(p->cid));
361 ((struct ast_modem_pvt *)(ast->pvt->pvt))->owner = NULL;
362 pthread_mutex_lock(&usecnt_lock);
365 ast_log(LOG_WARNING, "Usecnt < 0???\n");
366 pthread_mutex_unlock(&usecnt_lock);
367 ast_update_use_count();
368 if (option_verbose > 2)
369 ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
370 ast->pvt->pvt = NULL;
371 ast->state = AST_STATE_DOWN;
376 static int modem_answer(struct ast_channel *ast)
378 struct ast_modem_pvt *p;
380 ast_log(LOG_DEBUG, "modem_hangup(%s)\n", ast->name);
383 res = p->mc->answer(p);
387 ast->state = AST_STATE_UP;
392 static char modem_2digit(char c)
398 else if ((c < 10) && (c >= 0))
404 static struct ast_frame *modem_read(struct ast_channel *ast)
406 struct ast_modem_pvt *p = ast->pvt->pvt;
407 struct ast_frame *fr=NULL;
413 static int modem_write(struct ast_channel *ast, struct ast_frame *frame)
416 struct ast_modem_pvt *p = ast->pvt->pvt;
418 res = p->mc->write(p, frame);
422 struct ast_channel *ast_modem_new(struct ast_modem_pvt *i, int state)
424 struct ast_channel *tmp;
425 tmp = ast_channel_alloc();
427 snprintf(tmp->name, sizeof(tmp->name), "Modem[%s]/%s", i->mc->name, i->dev + 5);
430 tmp->format = i->mc->formats;
432 if (state == AST_STATE_RING)
435 tmp->pvt->send_digit = modem_digit;
436 tmp->pvt->call = modem_call;
437 tmp->pvt->hangup = modem_hangup;
438 tmp->pvt->answer = modem_answer;
439 tmp->pvt->read = modem_read;
440 tmp->pvt->write = modem_write;
441 strncpy(tmp->context, i->context, sizeof(tmp->context));
443 tmp->callerid = strdup(i->cid);
444 if (strlen(i->language))
445 strncpy(tmp->language,i->language, sizeof(tmp->language));
447 pthread_mutex_lock(&usecnt_lock);
449 pthread_mutex_unlock(&usecnt_lock);
450 ast_update_use_count();
451 if (state != AST_STATE_DOWN) {
452 if (ast_pbx_start(tmp)) {
453 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
459 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
463 static void modem_mini_packet(struct ast_modem_pvt *i)
465 struct ast_frame *fr;
467 if (fr->frametype == AST_FRAME_CONTROL) {
468 if (fr->subclass == AST_CONTROL_RING) {
469 ast_modem_new(i, AST_STATE_RING);
475 static void *do_monitor(void *data)
479 struct ast_modem_pvt *i;
480 /* This thread monitors all the frame relay interfaces which are not yet in use
481 (and thus do not have a separate thread) indefinitely */
482 /* From here on out, we die whenever asked */
484 if (pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL)) {
485 ast_log(LOG_WARNING, "Unable to set cancel type to asynchronous\n");
490 /* Don't let anybody kill us right away. Nobody should lock the interface list
491 and wait for the monitor list, but the other way around is okay. */
492 if (pthread_mutex_lock(&monlock)) {
493 ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
496 /* Lock the interface list */
497 if (pthread_mutex_lock(&iflock)) {
498 ast_log(LOG_ERROR, "Unable to grab interface lock\n");
499 pthread_mutex_unlock(&monlock);
502 /* Build the stuff we're going to select on, that is the socket of every
503 ast_modem_pvt that does not have an associated owner channel */
509 if (FD_ISSET(i->fd, &rfds))
510 ast_log(LOG_WARNING, "Descriptor %d appears twice (%s)?\n", i->fd, i->dev);
512 /* This needs to be watched, as it lacks an owner */
513 FD_SET(i->fd, &rfds);
514 FD_SET(i->fd, &efds);
521 /* Okay, now that we know what to do, release the interface lock */
522 pthread_mutex_unlock(&iflock);
524 /* And from now on, we're okay to be killed, so release the monitor lock as well */
525 pthread_mutex_unlock(&monlock);
527 ast_log(LOG_DEBUG, "In monitor, n=%d, pid=%d\n", n, getpid());
529 /* Wait indefinitely for something to happen */
530 pthread_testcancel();
531 res = select(n + 1, &rfds, NULL, &efds, NULL);
532 pthread_testcancel();
533 /* Okay, select has finished. Let's see what happened. */
535 ast_log(LOG_WARNING, "select return %d: %s\n", res, strerror(errno));
538 /* Alright, lock the interface list again, and let's look and see what has
540 if (pthread_mutex_lock(&iflock)) {
541 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
546 if (FD_ISSET(i->fd, &rfds) || FD_ISSET(i->fd, &efds)) {
548 ast_log(LOG_WARNING, "Whoa.... I'm owned but found (%d, %s)...\n", i->fd, i->dev);
552 modem_mini_packet(i);
556 pthread_mutex_unlock(&iflock);
563 static int restart_monitor()
565 /* If we're supposed to be stopped -- stay stopped */
566 if (monitor_thread == -2)
568 if (pthread_mutex_lock(&monlock)) {
569 ast_log(LOG_WARNING, "Unable to lock monitor\n");
572 if (monitor_thread == pthread_self()) {
573 pthread_mutex_unlock(&monlock);
574 ast_log(LOG_WARNING, "Cannot kill myself\n");
577 if (monitor_thread != -1) {
578 pthread_cancel(monitor_thread);
579 /* Nudge it a little, as it's probably stuck in select */
580 pthread_kill(monitor_thread, SIGURG);
581 pthread_join(monitor_thread, NULL);
583 /* Start a new monitor */
584 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
585 pthread_mutex_unlock(&monlock);
586 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
589 pthread_mutex_unlock(&monlock);
593 static struct ast_modem_pvt *mkif(char *iface)
595 /* Make a ast_modem_pvt structure for this interface */
596 struct ast_modem_pvt *tmp;
601 tmp = malloc(sizeof(struct ast_modem_pvt));
603 tmp->fd = open(iface, O_RDWR | O_NONBLOCK);
605 ast_log(LOG_WARNING, "Unable to open '%s'\n", iface);
609 strncpy(tmp->language, language, sizeof(tmp->language));
610 tmp->f = fdopen(tmp->fd, "w+");
611 /* Disable buffering */
612 setvbuf(tmp->f, NULL, _IONBF,0);
614 ast_log(LOG_WARNING, "Unable to fdopen '%s'\n", iface);
619 flags = fcntl(tmp->fd, F_GETFL);
620 fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
624 tmp->stripmsd = stripmsd;
625 tmp->dialtype = dialtype;
627 memset(tmp->cid, 0, sizeof(tmp->cid));
628 strncpy(tmp->dev, iface, sizeof(tmp->dev));
629 strncpy(tmp->context, context, sizeof(tmp->context));
630 strncpy(tmp->initstr, initstr, sizeof(tmp->initstr));
634 if (modem_setup(tmp, baudrate) < 0) {
635 ast_log(LOG_WARNING, "Unable to configure modem '%s'\n", iface);
643 static struct ast_channel *modem_request(char *type, int format, void *data)
646 struct ast_modem_pvt *p;
647 struct ast_channel *tmp = NULL;
649 strncpy(dev, (char *)data, sizeof(dev));
652 /* Search for an unowned channel */
653 if (pthread_mutex_lock(&iflock)) {
654 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
659 if (!strcmp(dev, p->dev + 5)) {
660 if (p->mc->formats & format) {
662 tmp = ast_modem_new(p, AST_STATE_DOWN);
666 ast_log(LOG_WARNING, "Device '%s' is busy\n", p->dev);
668 ast_log(LOG_WARNING, "Asked for a format %d line on %s\n", format, p->dev);
674 ast_log(LOG_WARNING, "Requested device '%s' does not exist\n", p->dev);
676 pthread_mutex_unlock(&iflock);
682 struct ast_config *cfg;
683 struct ast_variable *v;
684 struct ast_modem_pvt *tmp;
686 cfg = ast_load(config);
688 /* We *must* have a config file otherwise stop immediately */
690 ast_log(LOG_ERROR, "Unable to load config %s\n", config);
693 if (pthread_mutex_lock(&iflock)) {
694 /* It's a little silly to lock it, but we mind as well just to be sure */
695 ast_log(LOG_ERROR, "Unable to lock interface list???\n");
698 v = ast_variable_browse(cfg, "interfaces");
700 /* Create the interface list */
701 if (!strcasecmp(v->name, "device")) {
702 tmp = mkif(v->value);
708 ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
710 pthread_mutex_unlock(&iflock);
712 pthread_mutex_unlock(&iflock);
715 } else if (!strcasecmp(v->name, "driver")) {
716 snprintf(driver, sizeof(driver), "chan_modem_%s.so", v->value);
717 if (option_verbose > 1)
718 ast_verbose(VERBOSE_PREFIX_2 "Loading modem driver %s", driver);
720 if (ast_load_resource(driver)) {
721 ast_log(LOG_ERROR, "Failed to load driver %s\n", driver);
723 pthread_mutex_unlock(&iflock);
727 } else if (!strcasecmp(v->name, "mode")) {
728 if (!strncasecmp(v->value, "ri", 2))
729 gmode = MODEM_MODE_WAIT_RING;
730 else if (!strncasecmp(v->value, "im", 2))
731 gmode = MODEM_MODE_IMMEDIATE;
732 else if (!strncasecmp(v->value, "an", 2))
733 gmode = MODEM_MODE_WAIT_ANSWER;
735 ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
736 } else if (!strcasecmp(v->name, "stripmsd")) {
737 stripmsd = atoi(v->value);
738 } else if (!strcasecmp(v->name, "type")) {
739 strncpy(mtype, v->value, sizeof(mtype));
740 } else if (!strcasecmp(v->name, "initstr")) {
741 strncpy(initstr, v->value, sizeof(initstr));
742 } else if (!strcasecmp(v->name, "dialtype")) {
743 dialtype = toupper(v->value[0]);
744 } else if (!strcasecmp(v->name, "context")) {
745 strncpy(context, v->value, sizeof(context));
746 } else if (!strcasecmp(v->name, "language")) {
747 strncpy(language, v->value, sizeof(language));
751 pthread_mutex_unlock(&iflock);
752 if (ast_channel_register(type, tdesc, /* XXX Don't know our types -- maybe we should register more than one XXX */ AST_FORMAT_SLINEAR, modem_request)) {
753 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
759 /* And start the monitor for the first time */
766 struct ast_modem_pvt *p, *pl;
767 /* First, take us out of the channel loop */
768 ast_channel_unregister(type);
769 if (!pthread_mutex_lock(&iflock)) {
770 /* Hangup all interfaces if they have an owner */
774 ast_softhangup(p->owner);
778 pthread_mutex_unlock(&iflock);
780 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
783 if (!pthread_mutex_lock(&monlock)) {
784 if (monitor_thread > -1) {
785 pthread_cancel(monitor_thread);
786 pthread_join(monitor_thread, NULL);
789 pthread_mutex_unlock(&monlock);
791 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
795 if (!pthread_mutex_lock(&iflock)) {
796 /* Destroy all the interfaces and free their memory */
799 /* Close the socket, assuming it's real */
804 /* Free associated memory */
808 pthread_mutex_unlock(&iflock);
810 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
820 pthread_mutex_lock(&usecnt_lock);
822 pthread_mutex_unlock(&usecnt_lock);