Version 0.2.0 from FTP
[asterisk/asterisk.git] / channels / chan_modem.c
index bf4a89c..758235b 100755 (executable)
@@ -14,6 +14,7 @@
 #include <stdio.h>
 #include <pthread.h>
 #include <string.h>
+#include <asterisk/lock.h>
 #include <asterisk/channel.h>
 #include <asterisk/channel_pvt.h>
 #include <asterisk/config.h>
@@ -49,8 +50,14 @@ static char mtype[80] = "autodetect";
 /* Default context for incoming calls */
 static char context[AST_MAX_EXTENSION]= "default";
 
+/* Default language */
+static char language[MAX_LANGUAGE] = "";
+
 /* Initialization String */
-static char initstr[AST_MAX_INIT_STR] = "ATE1Q0";
+static char initstr[AST_MAX_INIT_STR] = "ATE0Q0";
+
+/* Default MSN */
+static char msn[AST_MAX_EXTENSION]="";
 
 static int usecnt =0;
 
@@ -58,14 +65,14 @@ static int baudrate = 115200;
 
 static int stripmsd = 0;
 
-static pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
 
 /* Protect the interface list (of ast_modem_pvt's) */
-static pthread_mutex_t iflock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t iflock = AST_MUTEX_INITIALIZER;
 
 /* Protect the monitoring thread, so only one process can kill or start it, and not
    when it's doing something critical. */
-static pthread_mutex_t monlock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t monlock = AST_MUTEX_INITIALIZER;
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
@@ -84,7 +91,7 @@ static int modem_digit(struct ast_channel *ast, char digit)
        p = ast->pvt->pvt;
        if (p->mc->dialdigit)
                return p->mc->dialdigit(p, digit);
-       else ast_log(LOG_DEBUG, "Channel %s lacks digit dialing\n");
+       else ast_log(LOG_DEBUG, "Channel %s lacks digit dialing\n", ast->name);
        return 0;
 }
 
@@ -155,10 +162,11 @@ int ast_unregister_modem_driver(struct ast_modem_driver *mc)
 
 static int modem_call(struct ast_channel *ast, char *idest, int timeout)
 {
+       static int modem_hangup(struct ast_channel *ast);
        struct ast_modem_pvt *p;
        int ms = timeout;
-       char rdest[80], *where;
-       strncpy(rdest, idest, sizeof(rdest));
+       char rdest[80], *where, dstr[100];
+       strncpy(rdest, idest, sizeof(rdest)-1);
        strtok(rdest, ":");
        where = strtok(NULL, ":");
        if (!where) {
@@ -166,24 +174,32 @@ static int modem_call(struct ast_channel *ast, char *idest, int timeout)
                return -1;
        }
        p = ast->pvt->pvt;
-       if ((ast->state != AST_STATE_DOWN) && (ast->state != AST_STATE_RESERVED)) {
-               ast_log(LOG_WARNING, "modem_call called on %s, neither down nor reserved\n", ast->name);
-               return -1;
+       strcpy(dstr,where + p->stripmsd);
+       /* if not a transfer or just sending tones, must be in correct state */
+       if (strcasecmp(rdest, "transfer") && strcasecmp(rdest,"sendtones")) {
+               if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) {
+                       ast_log(LOG_WARNING, "modem_call called on %s, neither down nor reserved\n", ast->name);
+                       return -1;
+               }
+       } 
+       if (!strcasecmp(rdest,"transfer")) /* if a transfer, put in transfer stuff */
+       {
+               sprintf(dstr,"!,%s",where + p->stripmsd);
        }
        if (!strcasecmp(where, "handset")) {
                if (p->mc->setdev)
                        if (p->mc->setdev(p, MODEM_DEV_HANDSET))
                                return -1;
                /* Should be immediately up */
-               ast->state = AST_STATE_UP;
+               ast_setstate(ast, AST_STATE_UP);
        } else {
                if (p->mc->setdev)
                        if (p->mc->setdev(p, MODEM_DEV_TELCO_SPK))
                                return -1;
                if (p->mc->dial)
-                       p->mc->dial(p, where + p->stripmsd);
-               ast->state = AST_STATE_DIALING;
-               while((ast->state != AST_STATE_UP) && (ms > 0)) {
+                       p->mc->dial(p, dstr);
+               ast_setstate(ast, AST_STATE_DIALING);
+               while((ast->_state != AST_STATE_UP) && (ms > 0)) {
                        ms = ast_waitfor(ast, ms);
                        /* Just read packets and watch what happens */
                        if (ms > 0) {
@@ -199,14 +215,20 @@ static int modem_call(struct ast_channel *ast, char *idest, int timeout)
 
 int ast_modem_send(struct ast_modem_pvt *p, char *cmd, int len)
 {
-       int res;
+       int i;
+       usleep(5000);
        if (!len) {
-               fprintf(p->f, "%s\r\n", cmd);
-               res = ast_modem_expect(p, cmd, ECHO_TIMEOUT);
-               if (res) {
-                       ast_log(LOG_WARNING, "Unexpected reply %s\n", p->response);
-                       return -1;
-               }
+               for(i = 0; cmd[i];)
+                  {
+                       if (fwrite(cmd + i,1,1,p->f) != 1)
+                          {
+                               if (errno == EWOULDBLOCK) continue;
+                               return -1;
+                          }
+                       i++;
+                  }
+               tcdrain(fileno(p->f)); 
+               fprintf(p->f,"\r\n");
                return 0;
        } else {
                if (fwrite(cmd, 1, len, p->f) < len)
@@ -217,18 +239,50 @@ int ast_modem_send(struct ast_modem_pvt *p, char *cmd, int len)
 
 int ast_modem_read_response(struct ast_modem_pvt *p, int timeout)
 {
-       int res = -1;
+       int res = -1,c,i;
        timeout *= 1000;
-       strncpy(p->response, "(No Response)", sizeof(p->response));
+       p->response[0] = 0;
+       c = i = 0;
        do {
-               res = ast_waitfor_n_fd(&p->fd, 1, &timeout);
+               res = ast_waitfor_n_fd(&p->fd, 1, &timeout, NULL);
                if (res < 0) {
+                       strncpy(p->response, "(No Response)", sizeof(p->response)-1);
                        return -1;
                }
-               /* Read a response */
-               fgets(p->response, sizeof(p->response), p->f);
-               return 0;
+                 /* get no more then buffer length */
+               while(i < sizeof(p->response) - 1)
+               {
+                       c = fgetc(p->f);  /* get a char */
+                       if (c < 1) /* if error */
+                       {
+                                 /* if nothing in buffer, go back into timeout stuff */
+                               if (errno == EWOULDBLOCK) break;
+                               /* return as error */
+                               strncpy(p->response, "(No Response)", sizeof(p->response)-1);
+                               return -1;
+                       }
+                         /* save char */
+                       p->response[i++] = c;
+                       p->response[i] = 0;                     
+                         /* if end of input */
+                       if (c == '\n') break;
+               }
+               if (c >= 0)  /* if input terminated normally */
+               {
+                         /* ignore just CR/LF */
+                       if (!strcmp(p->response,"\r\n"))
+                       {
+                                 /* reset input buffer stuff */
+                               i = 0; 
+                               p->response[0] = 0;
+                       }
+                       else /* otherwise return with info in buffer */
+                       {
+                               return 0;
+                       }
+               }
        } while(timeout > 0);
+       strncpy(p->response, "(No Response)", sizeof(p->response)-1);
        return -1;
 }
 
@@ -236,15 +290,15 @@ int ast_modem_expect(struct ast_modem_pvt *p, char *result, int timeout)
 {
        int res = -1;
        timeout *= 1000;
-       strncpy(p->response, "(No Response)", sizeof(p->response));
+       strncpy(p->response, "(No Response)", sizeof(p->response)-1);
        do {
-               res = ast_waitfor_n_fd(&p->fd, 1, &timeout);
+               res = ast_waitfor_n_fd(&p->fd, 1, &timeout, NULL);
                if (res < 0) {
                        return -1;
                }
                /* Read a response */
                fgets(p->response, sizeof(p->response), p->f);
-#if 0
+#if    0
                fprintf(stderr, "Modem said: %s", p->response);
 #endif
                if (!strncasecmp(p->response, result, strlen(result))) 
@@ -267,16 +321,23 @@ void ast_modem_trim(char *s)
 
 static int modem_setup(struct ast_modem_pvt *p, int baudrate)
 {
+
        /* Make sure there's a modem there and that it's in a reasonable 
           mode.  Set the baud rate, etc.  */
        char identity[256];
        char *ident = NULL;
+       char etx[2] = { 0x10, '!' }; 
        if (option_debug)
                ast_log(LOG_DEBUG, "Setting up modem %s\n", p->dev);
+       if (ast_modem_send(p, etx, 2)) {
+               ast_log(LOG_WARNING, "Failed to send ETX?\n");
+               return -1;
+       }
        if (ast_modem_send(p, "\r\n", 2)) {
                ast_log(LOG_WARNING, "Failed to send enter?\n");
                return -1;
        }
+       usleep(10000);
        /* Read any outstanding stuff */
        while(!ast_modem_read_response(p, 0));
        if (ast_modem_send(p, "ATZ", 0)) {
@@ -303,7 +364,7 @@ static int modem_setup(struct ast_modem_pvt *p, int baudrate)
                ast_log(LOG_WARNING, "Modem did not provide identification\n");
                return -1;
        }
-       strncpy(identity, p->response, sizeof(identity));
+       strncpy(identity, p->response, sizeof(identity)-1);
        ast_modem_trim(identity);
        if (ast_modem_expect(p, "OK", ECHO_TIMEOUT)) {
                ast_log(LOG_WARNING, "Modem did not provide identification\n");
@@ -340,7 +401,8 @@ static int modem_setup(struct ast_modem_pvt *p, int baudrate)
 static int modem_hangup(struct ast_channel *ast)
 {
        struct ast_modem_pvt *p;
-       ast_log(LOG_DEBUG, "modem_hangup(%s)\n", ast->name);
+       if (option_debug)
+               ast_log(LOG_DEBUG, "modem_hangup(%s)\n", ast->name);
        p = ast->pvt->pvt;
        /* Hang up */
        if (p->mc->hangup)
@@ -348,19 +410,20 @@ static int modem_hangup(struct ast_channel *ast)
        /* Re-initialize */
        if (p->mc->init)
                p->mc->init(p);
-       ast->state = AST_STATE_DOWN;
+       ast_setstate(ast, AST_STATE_DOWN);
        memset(p->cid, 0, sizeof(p->cid));
+       memset(p->dnid, 0, sizeof(p->dnid));
        ((struct ast_modem_pvt *)(ast->pvt->pvt))->owner = NULL;
-       pthread_mutex_lock(&usecnt_lock);
+       ast_pthread_mutex_lock(&usecnt_lock);
        usecnt--;
        if (usecnt < 0) 
                ast_log(LOG_WARNING, "Usecnt < 0???\n");
-       pthread_mutex_unlock(&usecnt_lock);
+       ast_pthread_mutex_unlock(&usecnt_lock);
        ast_update_use_count();
        if (option_verbose > 2) 
                ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
        ast->pvt->pvt = NULL;
-       ast->state = AST_STATE_DOWN;
+       ast_setstate(ast, AST_STATE_DOWN);
        restart_monitor();
        return 0;
 }
@@ -369,18 +432,20 @@ static int modem_answer(struct ast_channel *ast)
 {
        struct ast_modem_pvt *p;
        int res=0;
-       ast_log(LOG_DEBUG, "modem_hangup(%s)\n", ast->name);
+       if (option_debug)
+               ast_log(LOG_DEBUG, "modem_answer(%s)\n", ast->name);
        p = ast->pvt->pvt;
        if (p->mc->answer) {
                res = p->mc->answer(p);
        }
        if (!res) {
                ast->rings = 0;
-               ast->state = AST_STATE_UP;
+               ast_setstate(ast, AST_STATE_UP);
        }
        return res;
 }
 
+#if    0
 static char modem_2digit(char c)
 {
        if (c == 12)
@@ -392,7 +457,7 @@ static char modem_2digit(char c)
        else
                return '?';
 }
-
+#endif
 static struct ast_frame *modem_read(struct ast_channel *ast)
 {
        struct ast_modem_pvt *p = ast->pvt->pvt;
@@ -405,22 +470,31 @@ static struct ast_frame *modem_read(struct ast_channel *ast)
 static int modem_write(struct ast_channel *ast, struct ast_frame *frame)
 {
        int res=0;
+       long flags;
        struct ast_modem_pvt *p = ast->pvt->pvt;
+
+       /* Temporarily make non-blocking */
+       flags = fcntl(ast->fds[0], F_GETFL);
+       fcntl(ast->fds[0], F_SETFL, flags | O_NONBLOCK);
+
        if (p->mc->write)
                res = p->mc->write(p, frame);
+
+       /* Block again */
+       fcntl(ast->fds[0], F_SETFL, flags);
        return 0;
 }
 
 struct ast_channel *ast_modem_new(struct ast_modem_pvt *i, int state)
 {
        struct ast_channel *tmp;
-       tmp = ast_channel_alloc();
+       tmp = ast_channel_alloc(1);
        if (tmp) {
                snprintf(tmp->name, sizeof(tmp->name), "Modem[%s]/%s", i->mc->name, i->dev + 5);
                tmp->type = type;
-               tmp->fd = i->fd;
-               tmp->format = i->mc->formats;
-               tmp->state = state;
+               tmp->fds[0] = i->fd;
+               tmp->nativeformats = i->mc->formats;
+               ast_setstate(tmp, state);
                if (state == AST_STATE_RING)
                        tmp->rings = 1;
                tmp->pvt->pvt = i;
@@ -430,13 +504,17 @@ struct ast_channel *ast_modem_new(struct ast_modem_pvt *i, int state)
                tmp->pvt->answer = modem_answer;
                tmp->pvt->read = modem_read;
                tmp->pvt->write = modem_write;
-               strncpy(tmp->context, i->context, sizeof(tmp->context));
+               strncpy(tmp->context, i->context, sizeof(tmp->context)-1);
                if (strlen(i->cid))
                        tmp->callerid = strdup(i->cid);
+               if (strlen(i->language))
+                       strncpy(tmp->language,i->language, sizeof(tmp->language)-1);
+               if (strlen(i->dnid))
+                       strncpy(tmp->exten, i->dnid, sizeof(tmp->exten) - 1);
                i->owner = tmp;
-               pthread_mutex_lock(&usecnt_lock);
+               ast_pthread_mutex_lock(&usecnt_lock);
                usecnt++;
-               pthread_mutex_unlock(&usecnt_lock);
+               ast_pthread_mutex_unlock(&usecnt_lock);
                ast_update_use_count();
                if (state != AST_STATE_DOWN) {
                        if (ast_pbx_start(tmp)) {
@@ -454,10 +532,10 @@ static void modem_mini_packet(struct ast_modem_pvt *i)
 {
        struct ast_frame *fr;
        fr = i->mc->read(i);
+       if (!fr) return;
        if (fr->frametype == AST_FRAME_CONTROL) {
                if (fr->subclass == AST_CONTROL_RING) {
                        ast_modem_new(i, AST_STATE_RING);
-                       restart_monitor();
                }
        }
 }
@@ -479,14 +557,14 @@ static void *do_monitor(void *data)
        for(;;) {
                /* Don't let anybody kill us right away.  Nobody should lock the interface list
                   and wait for the monitor list, but the other way around is okay. */
-               if (pthread_mutex_lock(&monlock)) {
+               if (ast_pthread_mutex_lock(&monlock)) {
                        ast_log(LOG_ERROR, "Unable to grab monitor lock\n");
                        return NULL;
                }
                /* Lock the interface list */
-               if (pthread_mutex_lock(&iflock)) {
+               if (ast_pthread_mutex_lock(&iflock)) {
                        ast_log(LOG_ERROR, "Unable to grab interface lock\n");
-                       pthread_mutex_unlock(&monlock);
+                       ast_pthread_mutex_unlock(&monlock);
                        return NULL;
                }
                /* Build the stuff we're going to select on, that is the socket of every
@@ -509,10 +587,10 @@ static void *do_monitor(void *data)
                        i = i->next;
                }
                /* Okay, now that we know what to do, release the interface lock */
-               pthread_mutex_unlock(&iflock);
+               ast_pthread_mutex_unlock(&iflock);
                
                /* And from now on, we're okay to be killed, so release the monitor lock as well */
-               pthread_mutex_unlock(&monlock);
+               ast_pthread_mutex_unlock(&monlock);
 #if 0
                ast_log(LOG_DEBUG, "In monitor, n=%d, pid=%d\n", n, getpid());
 #endif
@@ -527,7 +605,7 @@ static void *do_monitor(void *data)
                }
                /* Alright, lock the interface list again, and let's look and see what has
                   happened */
-               if (pthread_mutex_lock(&iflock)) {
+               if (ast_pthread_mutex_lock(&iflock)) {
                        ast_log(LOG_WARNING, "Unable to lock the interface list\n");
                        continue;
                }
@@ -543,7 +621,7 @@ static void *do_monitor(void *data)
                        }
                        i=i->next;
                }
-               pthread_mutex_unlock(&iflock);
+               ast_pthread_mutex_unlock(&iflock);
        }
        /* Never reached */
        return NULL;
@@ -555,12 +633,12 @@ static int restart_monitor()
        /* If we're supposed to be stopped -- stay stopped */
        if (monitor_thread == -2)
                return 0;
-       if (pthread_mutex_lock(&monlock)) {
+       if (ast_pthread_mutex_lock(&monlock)) {
                ast_log(LOG_WARNING, "Unable to lock monitor\n");
                return -1;
        }
        if (monitor_thread == pthread_self()) {
-               pthread_mutex_unlock(&monlock);
+               ast_pthread_mutex_unlock(&monlock);
                ast_log(LOG_WARNING, "Cannot kill myself\n");
                return -1;
        }
@@ -572,14 +650,29 @@ static int restart_monitor()
        }
        /* Start a new monitor */
        if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
-               pthread_mutex_unlock(&monlock);
+               ast_pthread_mutex_unlock(&monlock);
                ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
                return -1;
        }
-       pthread_mutex_unlock(&monlock);
+       ast_pthread_mutex_unlock(&monlock);
        return 0;
 }
 
+static void stty(struct ast_modem_pvt *p)
+{
+       struct termios mode;
+       memset(&mode, 0, sizeof(mode));
+       if (tcgetattr(p->fd, &mode)) {
+               ast_log(LOG_WARNING, "Unable to get serial parameters on %s: %s\n", p->dev, strerror(errno));
+               return;
+       }
+       cfmakeraw(&mode);
+       cfsetspeed(&mode, B115200);
+       if (tcsetattr(p->fd, TCSANOW, &mode)) 
+               ast_log(LOG_WARNING, "Unable to set serial parameters on %s: %s\n", p->dev, strerror(errno));
+       
+}
+
 static struct ast_modem_pvt *mkif(char *iface)
 {
        /* Make a ast_modem_pvt structure for this interface */
@@ -589,6 +682,7 @@ static struct ast_modem_pvt *mkif(char *iface)
 #endif
        
        tmp = malloc(sizeof(struct ast_modem_pvt));
+       memset(tmp, 0, sizeof(struct ast_modem_pvt));
        if (tmp) {
                tmp->fd = open(iface, O_RDWR | O_NONBLOCK);
                if (tmp->fd < 0) {
@@ -596,6 +690,12 @@ static struct ast_modem_pvt *mkif(char *iface)
                        free(tmp);
                        return NULL;
                }
+               strncpy(tmp->language, language, sizeof(tmp->language)-1);
+               strncpy(tmp->msn, msn, sizeof(tmp->msn)-1);
+               strncpy(tmp->dev, iface, sizeof(tmp->dev)-1);
+               /* Maybe in the future we want to allow variable
+                  serial settings */
+               stty(tmp);
                tmp->f = fdopen(tmp->fd, "w+");
                /* Disable buffering */
                setvbuf(tmp->f, NULL, _IONBF,0);
@@ -604,19 +704,14 @@ static struct ast_modem_pvt *mkif(char *iface)
                        free(tmp);
                        return NULL;
                }
-#if 0
-               flags = fcntl(tmp->fd, F_GETFL);
-               fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
-#endif
                tmp->owner = NULL;
                tmp->ministate = 0;
                tmp->stripmsd = stripmsd;
                tmp->dialtype = dialtype;
                tmp->mode = gmode;
                memset(tmp->cid, 0, sizeof(tmp->cid));
-               strncpy(tmp->dev, iface, sizeof(tmp->dev));
-               strncpy(tmp->context, context, sizeof(tmp->context));
-               strncpy(tmp->initstr, initstr, sizeof(tmp->initstr));
+               strncpy(tmp->context, context, sizeof(tmp->context)-1);
+               strncpy(tmp->initstr, initstr, sizeof(tmp->initstr)-1);
                tmp->next = NULL;
                tmp->obuflen = 0;
                
@@ -635,11 +730,11 @@ static struct ast_channel *modem_request(char *type, int format, void *data)
        struct ast_modem_pvt *p;
        struct ast_channel *tmp = NULL;
        char dev[80];
-       strncpy(dev, (char *)data, sizeof(dev));
+       strncpy(dev, (char *)data, sizeof(dev)-1);
        strtok(dev, ":");
        oldformat = format;
        /* Search for an unowned channel */
-       if (pthread_mutex_lock(&iflock)) {
+       if (ast_pthread_mutex_lock(&iflock)) {
                ast_log(LOG_ERROR, "Unable to lock interface list???\n");
                return NULL;
        }
@@ -660,9 +755,9 @@ static struct ast_channel *modem_request(char *type, int format, void *data)
                p = p->next;
        }
        if (!p) 
-               ast_log(LOG_WARNING, "Requested device '%s' does not exist\n", p->dev);
+               ast_log(LOG_WARNING, "Requested device '%s' does not exist\n", dev);
        
-       pthread_mutex_unlock(&iflock);
+       ast_pthread_mutex_unlock(&iflock);
        return tmp;
 }
 
@@ -679,7 +774,7 @@ int load_module()
                ast_log(LOG_ERROR, "Unable to load config %s\n", config);
                return -1;
        }
-       if (pthread_mutex_lock(&iflock)) {
+       if (ast_pthread_mutex_lock(&iflock)) {
                /* It's a little silly to lock it, but we mind as well just to be sure */
                ast_log(LOG_ERROR, "Unable to lock interface list???\n");
                return -1;
@@ -696,9 +791,9 @@ int load_module()
                                } else {
                                        ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
                                        ast_destroy(cfg);
-                                       pthread_mutex_unlock(&iflock);
+                                       ast_pthread_mutex_unlock(&iflock);
                                        unload_module();
-                                       pthread_mutex_unlock(&iflock);
+                                       ast_pthread_mutex_unlock(&iflock);
                                        return -1;
                                }
                } else if (!strcasecmp(v->name, "driver")) {
@@ -709,7 +804,7 @@ int load_module()
                        if (ast_load_resource(driver)) {
                                ast_log(LOG_ERROR, "Failed to load driver %s\n", driver);
                                ast_destroy(cfg);
-                               pthread_mutex_unlock(&iflock);
+                               ast_pthread_mutex_unlock(&iflock);
                                unload_module();
                                return -1;
                        }
@@ -725,18 +820,23 @@ int load_module()
                } else if (!strcasecmp(v->name, "stripmsd")) {
                        stripmsd = atoi(v->value);
                } else if (!strcasecmp(v->name, "type")) {
-                       strncpy(mtype, v->value, sizeof(mtype));
+                       strncpy(mtype, v->value, sizeof(mtype)-1);
                } else if (!strcasecmp(v->name, "initstr")) {
-                       strncpy(initstr, v->value, sizeof(initstr));
+                       strncpy(initstr, v->value, sizeof(initstr)-1);
                } else if (!strcasecmp(v->name, "dialtype")) {
                        dialtype = toupper(v->value[0]);
                } else if (!strcasecmp(v->name, "context")) {
-                       strncpy(context, v->value, sizeof(context));
+                       strncpy(context, v->value, sizeof(context)-1);
+               } else if (!strcasecmp(v->name, "msn")) {
+                       strncpy(msn, v->value, sizeof(msn)-1);
+               } else if (!strcasecmp(v->name, "language")) {
+                       strncpy(language, v->value, sizeof(language)-1);
                }
                v = v->next;
        }
-       pthread_mutex_unlock(&iflock);
-       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)) {
+       ast_pthread_mutex_unlock(&iflock);
+       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)) {
                ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
                ast_destroy(cfg);
                unload_module();
@@ -753,33 +853,33 @@ int unload_module()
        struct ast_modem_pvt *p, *pl;
        /* First, take us out of the channel loop */
        ast_channel_unregister(type);
-       if (!pthread_mutex_lock(&iflock)) {
+       if (!ast_pthread_mutex_lock(&iflock)) {
                /* Hangup all interfaces if they have an owner */
                p = iflist;
                while(p) {
                        if (p->owner)
-                               ast_softhangup(p->owner);
+                               ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
                        p = p->next;
                }
                iflist = NULL;
-               pthread_mutex_unlock(&iflock);
+               ast_pthread_mutex_unlock(&iflock);
        } else {
                ast_log(LOG_WARNING, "Unable to lock the monitor\n");
                return -1;
        }
-       if (!pthread_mutex_lock(&monlock)) {
+       if (!ast_pthread_mutex_lock(&monlock)) {
                if (monitor_thread > -1) {
                        pthread_cancel(monitor_thread);
                        pthread_join(monitor_thread, NULL);
                }
                monitor_thread = -2;
-               pthread_mutex_unlock(&monlock);
+               ast_pthread_mutex_unlock(&monlock);
        } else {
                ast_log(LOG_WARNING, "Unable to lock the monitor\n");
                return -1;
        }
 
-       if (!pthread_mutex_lock(&iflock)) {
+       if (!ast_pthread_mutex_lock(&iflock)) {
                /* Destroy all the interfaces and free their memory */
                p = iflist;
                while(p) {
@@ -792,7 +892,7 @@ int unload_module()
                        free(pl);
                }
                iflist = NULL;
-               pthread_mutex_unlock(&iflock);
+               ast_pthread_mutex_unlock(&iflock);
        } else {
                ast_log(LOG_WARNING, "Unable to lock the monitor\n");
                return -1;
@@ -804,9 +904,9 @@ int unload_module()
 int usecount(void)
 {
        int res;
-       pthread_mutex_lock(&usecnt_lock);
+       ast_pthread_mutex_lock(&usecnt_lock);
        res = usecnt;
-       pthread_mutex_unlock(&usecnt_lock);
+       ast_pthread_mutex_unlock(&usecnt_lock);
        return res;
 }
 
@@ -815,3 +915,8 @@ char *description()
        return desc;
 }
 
+char *key()
+{
+       return ASTERISK_GPL_KEY;
+}
+