Version 0.2.0 from FTP
[asterisk/asterisk.git] / channels / chan_modem.c
index ea5757e..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>
@@ -64,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.  */
@@ -90,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;
 }
 
@@ -165,7 +166,7 @@ static int modem_call(struct ast_channel *ast, char *idest, int timeout)
        struct ast_modem_pvt *p;
        int ms = timeout;
        char rdest[80], *where, dstr[100];
-       strncpy(rdest, idest, sizeof(rdest));
+       strncpy(rdest, idest, sizeof(rdest)-1);
        strtok(rdest, ":");
        where = strtok(NULL, ":");
        if (!where) {
@@ -176,7 +177,7 @@ static int modem_call(struct ast_channel *ast, char *idest, int timeout)
        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)) {
+               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;
                }
@@ -190,15 +191,15 @@ static int modem_call(struct ast_channel *ast, char *idest, int timeout)
                        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, dstr);
-               ast->state = AST_STATE_DIALING;
-               while((ast->state != AST_STATE_UP) && (ms > 0)) {
+               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) {
@@ -245,7 +246,7 @@ int ast_modem_read_response(struct ast_modem_pvt *p, int timeout)
        do {
                res = ast_waitfor_n_fd(&p->fd, 1, &timeout, NULL);
                if (res < 0) {
-                       strncpy(p->response, "(No Response)", sizeof(p->response));
+                       strncpy(p->response, "(No Response)", sizeof(p->response)-1);
                        return -1;
                }
                  /* get no more then buffer length */
@@ -257,7 +258,7 @@ int ast_modem_read_response(struct ast_modem_pvt *p, int timeout)
                                  /* if nothing in buffer, go back into timeout stuff */
                                if (errno == EWOULDBLOCK) break;
                                /* return as error */
-                               strncpy(p->response, "(No Response)", sizeof(p->response));
+                               strncpy(p->response, "(No Response)", sizeof(p->response)-1);
                                return -1;
                        }
                          /* save char */
@@ -281,7 +282,7 @@ int ast_modem_read_response(struct ast_modem_pvt *p, int timeout)
                        }
                }
        } while(timeout > 0);
-       strncpy(p->response, "(No Response)", sizeof(p->response));
+       strncpy(p->response, "(No Response)", sizeof(p->response)-1);
        return -1;
 }
 
@@ -289,7 +290,7 @@ 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, NULL);
                if (res < 0) {
@@ -363,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");
@@ -409,8 +410,9 @@ 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;
        ast_pthread_mutex_lock(&usecnt_lock);
        usecnt--;
@@ -421,7 +423,7 @@ static int modem_hangup(struct ast_channel *ast)
        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;
 }
@@ -438,7 +440,7 @@ static int modem_answer(struct ast_channel *ast)
        }
        if (!res) {
                ast->rings = 0;
-               ast->state = AST_STATE_UP;
+               ast_setstate(ast, AST_STATE_UP);
        }
        return res;
 }
@@ -468,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->fds[0] = i->fd;
                tmp->nativeformats = i->mc->formats;
-               tmp->state = state;
+               ast_setstate(tmp, state);
                if (state == AST_STATE_RING)
                        tmp->rings = 1;
                tmp->pvt->pvt = i;
@@ -493,11 +504,13 @@ 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));
+                       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;
                ast_pthread_mutex_lock(&usecnt_lock);
                usecnt++;
@@ -677,9 +690,9 @@ static struct ast_modem_pvt *mkif(char *iface)
                        free(tmp);
                        return NULL;
                }
-               strncpy(tmp->language, language, sizeof(tmp->language));
-               strncpy(tmp->msn, msn, sizeof(tmp->msn));
-               strncpy(tmp->dev, iface, sizeof(tmp->dev));
+               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);
@@ -691,18 +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->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;
                
@@ -721,7 +730,7 @@ 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 */
@@ -746,7 +755,7 @@ 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);
        
        ast_pthread_mutex_unlock(&iflock);
        return tmp;
@@ -811,17 +820,17 @@ 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));
+                       strncpy(msn, v->value, sizeof(msn)-1);
                } else if (!strcasecmp(v->name, "language")) {
-                       strncpy(language, v->value, sizeof(language));
+                       strncpy(language, v->value, sizeof(language)-1);
                }
                v = v->next;
        }
@@ -849,7 +858,7 @@ int unload_module()
                p = iflist;
                while(p) {
                        if (p->owner)
-                               ast_softhangup(p->owner);
+                               ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
                        p = p->next;
                }
                iflist = NULL;