Version 0.2.0 from FTP
[asterisk/asterisk.git] / channels / chan_phone.c
index b7f82ea..09f5b1b 100755 (executable)
@@ -14,6 +14,7 @@
 #include <stdio.h>
 #include <pthread.h>
 #include <string.h>
 #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>
 #include <asterisk/channel.h>
 #include <asterisk/channel_pvt.h>
 #include <asterisk/config.h>
@@ -35,6 +36,7 @@
 #include "DialTone.h"
 
 #define PHONE_MAX_BUF 480
 #include "DialTone.h"
 
 #define PHONE_MAX_BUF 480
+#define DEFAULT_GAIN 0x100
 
 static char *desc = "Linux Telephony API Support";
 static char *type = "Phone";
 
 static char *desc = "Linux Telephony API Support";
 static char *type = "Phone";
@@ -52,16 +54,16 @@ static int echocancel = AEC_OFF;
 
 static int silencesupression = 0;
 
 
 static int silencesupression = 0;
 
-static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR;
+static int prefformat = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW;
 
 
-static pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
 
 /* Protect the interface list (of phone_pvt's) */
 
 /* Protect the interface list (of phone_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. */
 
 /* 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.  */
 
 /* This is the thread for the monitor which checks for input on the channels
    which are not currently in use.  */
@@ -90,13 +92,19 @@ static struct phone_pvt {
        char buf[PHONE_MAX_BUF];                                        /* Static buffer for reading frames */
        int obuflen;
        int dialtone;
        char buf[PHONE_MAX_BUF];                                        /* Static buffer for reading frames */
        int obuflen;
        int dialtone;
+       int txgain, rxgain;             /* gain control for playing, recording  */
+                                                                       /* 0x100 - 1.0, 0x200 - 2.0, 0x80 - 0.5 */
+       int cpt;                                                /* Call Progress Tone playing? */
        int silencesupression;
        char context[AST_MAX_EXTENSION];
        char obuf[PHONE_MAX_BUF * 2];
        char ext[AST_MAX_EXTENSION];
        char language[MAX_LANGUAGE];
        int silencesupression;
        char context[AST_MAX_EXTENSION];
        char obuf[PHONE_MAX_BUF * 2];
        char ext[AST_MAX_EXTENSION];
        char language[MAX_LANGUAGE];
+       char callerid[AST_MAX_EXTENSION];
 } *iflist = NULL;
 
 } *iflist = NULL;
 
+static char callerid[AST_MAX_EXTENSION];
+
 static int phone_digit(struct ast_channel *ast, char digit)
 {
        struct phone_pvt *p;
 static int phone_digit(struct ast_channel *ast, char digit)
 {
        struct phone_pvt *p;
@@ -133,17 +141,42 @@ static int phone_digit(struct ast_channel *ast, char digit)
 static int phone_call(struct ast_channel *ast, char *dest, int timeout)
 {
        struct phone_pvt *p;
 static int phone_call(struct ast_channel *ast, char *dest, int timeout)
 {
        struct phone_pvt *p;
+
+       // CID stuff for the phonejack...
+
+       PHONE_CID cid;
+       time_t UtcTime;
+       struct tm *t;
+
+
+       if (ast->callerid) {
+               time(&UtcTime);
+               t = localtime(&UtcTime);
+
+               if(t != NULL) {
+                       sprintf(cid.month, "%02d",(t->tm_mon + 1));
+                       sprintf(cid.day, "%02d", t->tm_mday);
+                       sprintf(cid.hour, "%02d", t->tm_hour);
+                       sprintf(cid.min, "%02d", t->tm_min);
+               }
+               strcpy(cid.name, "Unknown");
+               sprintf(cid.number,"%s",ast->callerid);
+       }
+
        p = ast->pvt->pvt;
        p = ast->pvt->pvt;
-       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, "phone_call called on %s, neither down nor reserved\n", ast->name);
                return -1;
        }
        /* When we call, it just works, really, there's no destination...  Just
           ring the phone and wait for someone to answer */
        if (option_debug)
                ast_log(LOG_WARNING, "phone_call called on %s, neither down nor reserved\n", ast->name);
                return -1;
        }
        /* When we call, it just works, really, there's no destination...  Just
           ring the phone and wait for someone to answer */
        if (option_debug)
-               ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fd);
-       ioctl(p->fd, PHONE_RING_START);
-       ast->state = AST_STATE_RINGING;
+               ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fds[0]);
+
+       ioctl(p->fd, PHONE_RING_START,&cid);
+       ast_setstate(ast, AST_STATE_RINGING);
+       ast_queue_control(ast, AST_CONTROL_RINGING, 0);
        return 0;
 }
 
        return 0;
 }
 
@@ -158,7 +191,7 @@ static int phone_hangup(struct ast_channel *ast)
                return 0;
        }
        /* XXX Is there anything we can do to really hang up except stop recording? */
                return 0;
        }
        /* XXX Is there anything we can do to really hang up except stop recording? */
-       ast->state = AST_STATE_DOWN;
+       ast_setstate(ast, AST_STATE_DOWN);
        if (ioctl(p->fd, PHONE_REC_STOP))
                ast_log(LOG_WARNING, "Failed to stop recording\n");
        if (ioctl(p->fd, PHONE_PLAY_STOP))
        if (ioctl(p->fd, PHONE_REC_STOP))
                ast_log(LOG_WARNING, "Failed to stop recording\n");
        if (ioctl(p->fd, PHONE_PLAY_STOP))
@@ -179,6 +212,7 @@ static int phone_hangup(struct ast_channel *ast)
                if (option_debug)
                        ast_log(LOG_DEBUG, "Got hunghup, giving busy signal\n");
                ioctl(p->fd, PHONE_BUSY);
                if (option_debug)
                        ast_log(LOG_DEBUG, "Got hunghup, giving busy signal\n");
                ioctl(p->fd, PHONE_BUSY);
+               p->cpt = 1;
        }
        p->lastformat = -1;
        p->lastinput = -1;
        }
        p->lastformat = -1;
        p->lastinput = -1;
@@ -187,16 +221,16 @@ static int phone_hangup(struct ast_channel *ast)
        p->dialtone = 0;
        memset(p->ext, 0, sizeof(p->ext));
        ((struct phone_pvt *)(ast->pvt->pvt))->owner = NULL;
        p->dialtone = 0;
        memset(p->ext, 0, sizeof(p->ext));
        ((struct phone_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");
        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_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;
 }
        restart_monitor();
        return 0;
 }
@@ -226,6 +260,15 @@ static int phone_setup(struct ast_channel *ast)
                                return -1;
                        }
                }
                                return -1;
                        }
                }
+       } else if (ast->pvt->rawreadformat == AST_FORMAT_ULAW) {
+               ioctl(p->fd, PHONE_REC_STOP);
+               if (p->lastinput != AST_FORMAT_ULAW) {
+                       p->lastinput = AST_FORMAT_ULAW;
+                       if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
+                               ast_log(LOG_WARNING, "Failed to set codec to signed linear 16\n");
+                               return -1;
+                       }
+               }
        } else {
                ast_log(LOG_WARNING, "Can't do format %d\n", ast->pvt->rawreadformat);
                return -1;
        } else {
                ast_log(LOG_WARNING, "Can't do format %d\n", ast->pvt->rawreadformat);
                return -1;
@@ -252,7 +295,7 @@ static int phone_answer(struct ast_channel *ast)
        if (option_debug)
                ast_log(LOG_DEBUG, "phone_answer(%s)\n", ast->name);
        ast->rings = 0;
        if (option_debug)
                ast_log(LOG_DEBUG, "phone_answer(%s)\n", ast->name);
        ast->rings = 0;
-       ast->state = AST_STATE_UP;
+       ast_setstate(ast, AST_STATE_UP);
        return 0;
 }
 
        return 0;
 }
 
@@ -304,22 +347,22 @@ static struct ast_frame  *phone_exception(struct ast_channel *ast)
                if (!res && (p->mode != MODE_FXO))
                        return NULL;
                else {
                if (!res && (p->mode != MODE_FXO))
                        return NULL;
                else {
-                       if (ast->state == AST_STATE_RINGING) {
+                       if (ast->_state == AST_STATE_RINGING) {
                                /* They've picked up the phone */
                                p->fr.frametype = AST_FRAME_CONTROL;
                                p->fr.subclass = AST_CONTROL_ANSWER;
                                phone_setup(ast);
                                /* They've picked up the phone */
                                p->fr.frametype = AST_FRAME_CONTROL;
                                p->fr.subclass = AST_CONTROL_ANSWER;
                                phone_setup(ast);
-                               ast->state = AST_STATE_UP;
+                               ast_setstate(ast, AST_STATE_UP);
                                return &p->fr;
                        }  else 
                                return &p->fr;
                        }  else 
-                               ast_log(LOG_WARNING, "Got off hook in weird state %d\n", ast->state);
+                               ast_log(LOG_WARNING, "Got off hook in weird state %d\n", ast->_state);
                }
        }
 #if 1
        if (phonee.bits.pstn_ring)
                ast_verbose("Unit is ringing\n");
        if (phonee.bits.caller_id) {
                }
        }
 #if 1
        if (phonee.bits.pstn_ring)
                ast_verbose("Unit is ringing\n");
        if (phonee.bits.caller_id) {
-               ast_verbose("We have caller ID: %s\n");
+               ast_verbose("We have caller ID\n");
        }
        if (phonee.bits.pstn_wink)
                ast_verbose("Detected Wink\n");
        }
        if (phonee.bits.pstn_wink)
                ast_verbose("Detected Wink\n");
@@ -334,6 +377,7 @@ static struct ast_frame  *phone_read(struct ast_channel *ast)
 {
        int res;
        struct phone_pvt *p = ast->pvt->pvt;
 {
        int res;
        struct phone_pvt *p = ast->pvt->pvt;
+       
 
        /* Some nice norms */
        p->fr.datalen = 0;
 
        /* Some nice norms */
        p->fr.datalen = 0;
@@ -389,16 +433,14 @@ static int phone_write_buf(struct phone_pvt *p, char *buf, int len, int frlen)
        memcpy(p->obuf + p->obuflen, buf, len);
        p->obuflen += len;
        while(p->obuflen > frlen) {
        memcpy(p->obuf + p->obuflen, buf, len);
        p->obuflen += len;
        while(p->obuflen > frlen) {
-#if 0
-               res = frlen;
-               ast_log(LOG_DEBUG, "Wrote %d bytes\n", res);
-#else
                res = write(p->fd, p->obuf, frlen);
                res = write(p->fd, p->obuf, frlen);
-#endif
                if (res != frlen) {
                        if (res < 1) {
                if (res != frlen) {
                        if (res < 1) {
-                               ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno));
-                               return -1;
+/*
+ * Card is in non-blocking mode now and it works well now, but there are
+ * lot of messages like this. So, this message is temporarily disabled.
+ */
+                               return 0;
                        } else {
                                ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
                        }
                        } else {
                                ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
                        }
@@ -423,20 +465,27 @@ static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
        char tmpbuf[4];
        /* Write a frame of (presumably voice) data */
        if (frame->frametype != AST_FRAME_VOICE) {
        char tmpbuf[4];
        /* Write a frame of (presumably voice) data */
        if (frame->frametype != AST_FRAME_VOICE) {
-               ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
-               ast_frfree(frame);
-               return -1;
+               if (frame->frametype != AST_FRAME_IMAGE)
+                       ast_log(LOG_WARNING, "Don't know what to do with  frame type '%d'\n", frame->frametype);
+               return 0;
        }
        }
-       if (!(frame->subclass & (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR))) {
+       if (!(frame->subclass &
+               (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW))) {
                ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
                ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
-               ast_frfree(frame);
                return -1;
        }
                return -1;
        }
+#if 0
        /* If we're not in up mode, go into up mode now */
        /* If we're not in up mode, go into up mode now */
-       if (ast->state != AST_STATE_UP) {
-               ast->state = AST_STATE_UP;
+       if (ast->_state != AST_STATE_UP) {
+               ast_setstate(ast, AST_STATE_UP);
                phone_setup(ast);
        }
                phone_setup(ast);
        }
+#else
+       if (ast->_state != AST_STATE_UP) {
+               /* Don't try tos end audio on-hook */
+               return 0;
+       }
+#endif 
        if (frame->subclass == AST_FORMAT_G723_1) {
                if (p->lastformat != AST_FORMAT_G723_1) {
                        ioctl(p->fd, PHONE_PLAY_STOP);
        if (frame->subclass == AST_FORMAT_G723_1) {
                if (p->lastformat != AST_FORMAT_G723_1) {
                        ioctl(p->fd, PHONE_PLAY_STOP);
@@ -479,6 +528,25 @@ static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
                        p->obuflen = 0;
                }
                maxfr = 480;
                        p->obuflen = 0;
                }
                maxfr = 480;
+       } else if (frame->subclass == AST_FORMAT_ULAW) {
+               if (p->lastformat != AST_FORMAT_ULAW) {
+                       ioctl(p->fd, PHONE_PLAY_STOP);
+                       ioctl(p->fd, PHONE_REC_STOP);
+                       if (ioctl(p->fd, PHONE_PLAY_CODEC, ULAW)) {
+                               ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
+                               return -1;
+                       }
+                       if (ioctl(p->fd, PHONE_REC_CODEC, ULAW)) {
+                               ast_log(LOG_WARNING, "Unable to set uLaw mode\n");
+                               return -1;
+                       }
+                       p->lastformat = AST_FORMAT_ULAW;
+                       p->lastinput = AST_FORMAT_ULAW;
+                       codecset = 1;
+                       /* Reset output buffer */
+                       p->obuflen = 0;
+               }
+               maxfr = 240;
        }
        if (codecset) {
                ioctl(p->fd, PHONE_REC_DEPTH, 3);
        }
        if (codecset) {
                ioctl(p->fd, PHONE_REC_DEPTH, 3);
@@ -515,11 +583,20 @@ static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
                        res = phone_write_buf(p, pos, expected, maxfr);
                }
                if (res != expected) {
                        res = phone_write_buf(p, pos, expected, maxfr);
                }
                if (res != expected) {
-                       if (res < 0) 
-                               ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
-                       else
-                               ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
-                       return -1;
+                       if (errno != EAGAIN) {
+                               if (res < 0) 
+                                       ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
+       /*
+        * Card is in non-blocking mode now and it works well now, but there are
+        * lot of messages like this. So, this message is temporarily disabled.
+        */
+#if 0
+                               else
+                                       ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
+#endif
+                               return -1;
+                       } else /* Pretend it worked */
+                               res = expected;
                }
                sofar += res;
                pos += res;
                }
                sofar += res;
                pos += res;
@@ -530,14 +607,16 @@ static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
 static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *context)
 {
        struct ast_channel *tmp;
 static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *context)
 {
        struct ast_channel *tmp;
-       tmp = ast_channel_alloc();
+       tmp = ast_channel_alloc(1);
        if (tmp) {
                snprintf(tmp->name, sizeof(tmp->name), "Phone/%s", i->dev + 5);
                tmp->type = type;
        if (tmp) {
                snprintf(tmp->name, sizeof(tmp->name), "Phone/%s", i->dev + 5);
                tmp->type = type;
-               tmp->fd = i->fd;
+               tmp->fds[0] = i->fd;
                /* XXX Switching formats silently causes kernel panics XXX */
                tmp->nativeformats = prefformat;
                /* XXX Switching formats silently causes kernel panics XXX */
                tmp->nativeformats = prefformat;
-               tmp->state = state;
+               tmp->pvt->rawreadformat = prefformat;
+               tmp->pvt->rawwriteformat = prefformat;
+               ast_setstate(tmp, state);
                if (state == AST_STATE_RING)
                        tmp->rings = 1;
                tmp->pvt->pvt = i;
                if (state == AST_STATE_RING)
                        tmp->rings = 1;
                tmp->pvt->pvt = i;
@@ -548,19 +627,22 @@ static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *conte
                tmp->pvt->read = phone_read;
                tmp->pvt->write = phone_write;
                tmp->pvt->exception = phone_exception;
                tmp->pvt->read = phone_read;
                tmp->pvt->write = phone_write;
                tmp->pvt->exception = phone_exception;
-               strncpy(tmp->context, context, sizeof(tmp->context));
+               strncpy(tmp->context, context, sizeof(tmp->context)-1);
                if (strlen(i->ext))
                if (strlen(i->ext))
-                       strncpy(tmp->exten, i->ext, sizeof(tmp->exten));
+                       strncpy(tmp->exten, i->ext, sizeof(tmp->exten)-1);
                if (strlen(i->language))
                if (strlen(i->language))
-                       strncpy(tmp->language, i->language, sizeof(tmp->language));
+                       strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
+               if (strlen(i->callerid))
+                       tmp->callerid = strdup(i->callerid);
                i->owner = tmp;
                i->owner = tmp;
-               pthread_mutex_lock(&usecnt_lock);
+               ast_pthread_mutex_lock(&usecnt_lock);
                usecnt++;
                usecnt++;
-               pthread_mutex_unlock(&usecnt_lock);
+               ast_pthread_mutex_unlock(&usecnt_lock);
                ast_update_use_count();
                if (state != AST_STATE_DOWN) {
                        if (state == AST_STATE_RING) {
                ast_update_use_count();
                if (state != AST_STATE_DOWN) {
                        if (state == AST_STATE_RING) {
-                               ioctl(tmp->fd, PHONE_RINGBACK);
+                               ioctl(tmp->fds[0], PHONE_RINGBACK);
+                               i->cpt = 1;
                        }
                        if (ast_pbx_start(tmp)) {
                                ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
                        }
                        if (ast_pbx_start(tmp)) {
                                ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
@@ -603,34 +685,35 @@ static void phone_check_exception(struct phone_pvt *i)
                        i->dialtone = 0;
                        if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
                                strcat(i->ext, digit);
                        i->dialtone = 0;
                        if (strlen(i->ext) < AST_MAX_EXTENSION - 1)
                                strcat(i->ext, digit);
-                       if (ast_exists_extension(NULL, i->context, i->ext, 1)) {
+                       if (ast_exists_extension(NULL, i->context, i->ext, 1, i->callerid)) {
                                /* It's a valid extension in its context, get moving! */
                                phone_new(i, AST_STATE_RING, i->context);
                                /* No need to restart monitor, we are the monitor */
                                if (i->owner) {
                                /* It's a valid extension in its context, get moving! */
                                phone_new(i, AST_STATE_RING, i->context);
                                /* No need to restart monitor, we are the monitor */
                                if (i->owner) {
-                                       pthread_mutex_lock(&usecnt_lock);
+                                       ast_pthread_mutex_lock(&usecnt_lock);
                                        usecnt--;
                                        usecnt--;
-                                       pthread_mutex_unlock(&usecnt_lock);
+                                       ast_pthread_mutex_unlock(&usecnt_lock);
                                        ast_update_use_count();
                                }
                                        ast_update_use_count();
                                }
-                       } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1)) {
+                       } else if (!ast_canmatch_extension(NULL, i->context, i->ext, 1, i->callerid)) {
                                /* There is nothing in the specified extension that can match anymore.
                                   Try the default */
                                /* There is nothing in the specified extension that can match anymore.
                                   Try the default */
-                               if (ast_exists_extension(NULL, "default", i->ext, 1)) {
+                               if (ast_exists_extension(NULL, "default", i->ext, 1, i->callerid)) {
                                        /* Check the default, too... */
                                        phone_new(i, AST_STATE_RING, "default");
                                        if (i->owner) {
                                        /* Check the default, too... */
                                        phone_new(i, AST_STATE_RING, "default");
                                        if (i->owner) {
-                                               pthread_mutex_lock(&usecnt_lock);
+                                               ast_pthread_mutex_lock(&usecnt_lock);
                                                usecnt--;
                                                usecnt--;
-                                               pthread_mutex_unlock(&usecnt_lock);
+                                               ast_pthread_mutex_unlock(&usecnt_lock);
                                                ast_update_use_count();
                                        }
                                        /* XXX This should probably be justified better XXX */
                                                ast_update_use_count();
                                        }
                                        /* XXX This should probably be justified better XXX */
-                               }  else if (!ast_canmatch_extension(NULL, "default", i->ext, 1)) {
+                               }  else if (!ast_canmatch_extension(NULL, "default", i->ext, 1, i->callerid)) {
                                        /* It's not a valid extension, give a busy signal */
                                        if (option_debug)
                                                ast_log(LOG_DEBUG, "%s can't match anything in %s or default\n", i->ext, i->context);
                                        ioctl(i->fd, PHONE_BUSY);
                                        /* It's not a valid extension, give a busy signal */
                                        if (option_debug)
                                                ast_log(LOG_DEBUG, "%s can't match anything in %s or default\n", i->ext, i->context);
                                        ioctl(i->fd, PHONE_BUSY);
+                                       i->cpt = 1;
                                }
                        }
 #if 0
                                }
                        }
 #if 0
@@ -644,9 +727,9 @@ static void phone_check_exception(struct phone_pvt *i)
                        if (i->mode == MODE_IMMEDIATE) {
                                phone_new(i, AST_STATE_RING, i->context);
                        } else if (i->mode == MODE_DIALTONE) {
                        if (i->mode == MODE_IMMEDIATE) {
                                phone_new(i, AST_STATE_RING, i->context);
                        } else if (i->mode == MODE_DIALTONE) {
-                               pthread_mutex_lock(&usecnt_lock);
+                               ast_pthread_mutex_lock(&usecnt_lock);
                                usecnt++;
                                usecnt++;
-                               pthread_mutex_unlock(&usecnt_lock);
+                               ast_pthread_mutex_unlock(&usecnt_lock);
                                ast_update_use_count();
                                /* Reset the extension */
                                i->ext[0] = '\0';
                                ast_update_use_count();
                                /* Reset the extension */
                                i->ext[0] = '\0';
@@ -659,13 +742,17 @@ static void phone_check_exception(struct phone_pvt *i)
                        }
                } else {
                        if (i->dialtone) {
                        }
                } else {
                        if (i->dialtone) {
-                               pthread_mutex_lock(&usecnt_lock);
+                               ast_pthread_mutex_lock(&usecnt_lock);
                                usecnt--;
                                usecnt--;
-                               pthread_mutex_unlock(&usecnt_lock);
+                               ast_pthread_mutex_unlock(&usecnt_lock);
                                ast_update_use_count();
                        }
                        memset(i->ext, 0, sizeof(i->ext));
                                ast_update_use_count();
                        }
                        memset(i->ext, 0, sizeof(i->ext));
-                       ioctl(i->fd, PHONE_CPT_STOP);
+                       if (i->cpt)
+                       {
+                               ioctl(i->fd, PHONE_CPT_STOP);
+                               i->cpt = 0;
+                       }
                        ioctl(i->fd, PHONE_PLAY_STOP);
                        ioctl(i->fd, PHONE_REC_STOP);
                        i->dialtone = 0;
                        ioctl(i->fd, PHONE_PLAY_STOP);
                        ioctl(i->fd, PHONE_REC_STOP);
                        i->dialtone = 0;
@@ -701,14 +788,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. */
        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 */
                        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");
                        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
                        return NULL;
                }
                /* Build the stuff we're going to select on, that is the socket of every
@@ -742,10 +829,10 @@ static void *do_monitor(void *data)
                        i = i->next;
                }
                /* Okay, now that we know what to do, release the interface lock */
                        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 */
                
                /* 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);
                /* Wait indefinitely for something to happen */
                if (dotone) {
                        /* If we're ready to recycle the time, set it to 30 ms */
                /* Wait indefinitely for something to happen */
                if (dotone) {
                        /* If we're ready to recycle the time, set it to 30 ms */
@@ -774,7 +861,7 @@ static void *do_monitor(void *data)
                        continue;
                /* Alright, lock the interface list again, and let's look and see what has
                   happened */
                        continue;
                /* 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;
                }
                        ast_log(LOG_WARNING, "Unable to lock the interface list\n");
                        continue;
                }
@@ -796,7 +883,7 @@ static void *do_monitor(void *data)
                        }
                        i=i->next;
                }
                        }
                        i=i->next;
                }
-               pthread_mutex_unlock(&iflock);
+               ast_pthread_mutex_unlock(&iflock);
        }
        /* Never reached */
        return NULL;
        }
        /* Never reached */
        return NULL;
@@ -808,12 +895,12 @@ static int restart_monitor()
        /* If we're supposed to be stopped -- stay stopped */
        if (monitor_thread == -2)
                return 0;
        /* 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()) {
                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;
        }
                ast_log(LOG_WARNING, "Cannot kill myself\n");
                return -1;
        }
@@ -825,21 +912,19 @@ static int restart_monitor()
        }
        /* Start a new monitor */
        if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
        }
        /* 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;
        }
                ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
                return -1;
        }
-       pthread_mutex_unlock(&monlock);
+       ast_pthread_mutex_unlock(&monlock);
        return 0;
 }
 
        return 0;
 }
 
-static struct phone_pvt *mkif(char *iface, int mode)
+static struct phone_pvt *mkif(char *iface, int mode, int txgain, int rxgain)
 {
        /* Make a phone_pvt structure for this interface */
        struct phone_pvt *tmp;
 {
        /* Make a phone_pvt structure for this interface */
        struct phone_pvt *tmp;
-#if 0
        int flags;      
        int flags;      
-#endif
        
        tmp = malloc(sizeof(struct phone_pvt));
        if (tmp) {
        
        tmp = malloc(sizeof(struct phone_pvt));
        if (tmp) {
@@ -852,6 +937,9 @@ static struct phone_pvt *mkif(char *iface, int mode)
                if (mode == MODE_FXO) {
                        if (ioctl(tmp->fd, IXJCTL_PORT, PORT_PSTN)) 
                                ast_log(LOG_DEBUG, "Unable to set port to PSTN\n");
                if (mode == MODE_FXO) {
                        if (ioctl(tmp->fd, IXJCTL_PORT, PORT_PSTN)) 
                                ast_log(LOG_DEBUG, "Unable to set port to PSTN\n");
+               } else {
+                       if (ioctl(tmp->fd, IXJCTL_PORT, PORT_POTS)) 
+                               ast_log(LOG_DEBUG, "Unable to set port to POTS\n");
                }
                ioctl(tmp->fd, PHONE_PLAY_STOP);
                ioctl(tmp->fd, PHONE_REC_STOP);
                }
                ioctl(tmp->fd, PHONE_PLAY_STOP);
                ioctl(tmp->fd, PHONE_REC_STOP);
@@ -867,21 +955,25 @@ static struct phone_pvt *mkif(char *iface, int mode)
                ioctl(tmp->fd, PHONE_VAD, tmp->silencesupression);
 #endif
                tmp->mode = mode;
                ioctl(tmp->fd, PHONE_VAD, tmp->silencesupression);
 #endif
                tmp->mode = mode;
-#if 0
                flags = fcntl(tmp->fd, F_GETFL);
                fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
                flags = fcntl(tmp->fd, F_GETFL);
                fcntl(tmp->fd, F_SETFL, flags | O_NONBLOCK);
-#endif
                tmp->owner = NULL;
                tmp->lastformat = -1;
                tmp->lastinput = -1;
                tmp->ministate = 0;
                memset(tmp->ext, 0, sizeof(tmp->ext));
                tmp->owner = NULL;
                tmp->lastformat = -1;
                tmp->lastinput = -1;
                tmp->ministate = 0;
                memset(tmp->ext, 0, sizeof(tmp->ext));
-               strncpy(tmp->language, language, sizeof(tmp->language));
-               strncpy(tmp->dev, iface, sizeof(tmp->dev));
-               strncpy(tmp->context, context, sizeof(tmp->context));
+               strncpy(tmp->language, language, sizeof(tmp->language)-1);
+               strncpy(tmp->dev, iface, sizeof(tmp->dev)-1);
+               strncpy(tmp->context, context, sizeof(tmp->context)-1);
                tmp->next = NULL;
                tmp->obuflen = 0;
                tmp->dialtone = 0;
                tmp->next = NULL;
                tmp->obuflen = 0;
                tmp->dialtone = 0;
+               tmp->cpt = 0;
+               strncpy(tmp->callerid, callerid, sizeof(tmp->callerid)-1);
+               tmp->txgain = txgain;
+               ioctl(tmp->fd, PHONE_PLAY_VOLUME, tmp->txgain);
+               tmp->rxgain = rxgain;
+               ioctl(tmp->fd, PHONE_REC_VOLUME, tmp->rxgain);
        }
        return tmp;
 }
        }
        return tmp;
 }
@@ -894,13 +986,13 @@ static struct ast_channel *phone_request(char *type, int format, void *data)
        char *name = data;
        
        oldformat = format;
        char *name = data;
        
        oldformat = format;
-       format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR);
+       format &= (AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW);
        if (!format) {
                ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
                return NULL;
        }
        /* Search for an unowned channel */
        if (!format) {
                ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", oldformat);
                return NULL;
        }
        /* 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;
        }
                ast_log(LOG_ERROR, "Unable to lock interface list???\n");
                return NULL;
        }
@@ -914,17 +1006,41 @@ static struct ast_channel *phone_request(char *type, int format, void *data)
                }
                p = p->next;
        }
                }
                p = p->next;
        }
-       pthread_mutex_unlock(&iflock);
+       ast_pthread_mutex_unlock(&iflock);
        restart_monitor();
        return tmp;
 }
 
        restart_monitor();
        return tmp;
 }
 
+/* parse gain value from config file */
+static int parse_gain_value(char *gain_type, char *value)
+{
+       float gain;
+
+       /* try to scan number */
+       if (sscanf(value, "%f", &gain) != 1)
+       {
+               ast_log(LOG_ERROR, "Invalid %s value '%s' in '%s' config\n",
+                       value, gain_type, config);
+               return DEFAULT_GAIN;
+       }
+
+       /* multiplicate gain by 1.0 gain value */ 
+       gain = gain * (float)DEFAULT_GAIN;
+
+       /* percentage? */
+       if (value[strlen(value) - 1] == '%')
+               return (int)(gain / (float)100);
+
+       return (int)gain;
+}
+
 int load_module()
 {
        struct ast_config *cfg;
        struct ast_variable *v;
        struct phone_pvt *tmp;
        int mode = MODE_IMMEDIATE;
 int load_module()
 {
        struct ast_config *cfg;
        struct ast_variable *v;
        struct phone_pvt *tmp;
        int mode = MODE_IMMEDIATE;
+       int txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; /* default gain 1.0 */
        cfg = ast_load(config);
 
        /* We *must* have a config file otherwise stop immediately */
        cfg = ast_load(config);
 
        /* We *must* have a config file otherwise stop immediately */
@@ -932,7 +1048,7 @@ int load_module()
                ast_log(LOG_ERROR, "Unable to load config %s\n", config);
                return -1;
        }
                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;
                /* 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;
@@ -941,7 +1057,7 @@ int load_module()
        while(v) {
                /* Create the interface list */
                if (!strcasecmp(v->name, "device")) {
        while(v) {
                /* Create the interface list */
                if (!strcasecmp(v->name, "device")) {
-                               tmp = mkif(v->value, mode);
+                               tmp = mkif(v->value, mode, txgain, rxgain);
                                if (tmp) {
                                        tmp->next = iflist;
                                        iflist = tmp;
                                if (tmp) {
                                        tmp->next = iflist;
                                        iflist = tmp;
@@ -949,14 +1065,16 @@ int load_module()
                                } else {
                                        ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
                                        ast_destroy(cfg);
                                } 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();
                                        return -1;
                                }
                } else if (!strcasecmp(v->name, "silencesupression")) {
                        silencesupression = ast_true(v->value);
                } else if (!strcasecmp(v->name, "language")) {
                                        unload_module();
                                        return -1;
                                }
                } else if (!strcasecmp(v->name, "silencesupression")) {
                        silencesupression = ast_true(v->value);
                } else if (!strcasecmp(v->name, "language")) {
-                       strncpy(language, v->value, sizeof(language));
+                       strncpy(language, v->value, sizeof(language)-1);
+               } else if (!strcasecmp(v->name, "callerid")) {
+                       strncpy(callerid, v->value, sizeof(callerid)-1);
                } else if (!strcasecmp(v->name, "mode")) {
                        if (!strncasecmp(v->value, "di", 2)) 
                                mode = MODE_DIALTONE;
                } else if (!strcasecmp(v->name, "mode")) {
                        if (!strncasecmp(v->value, "di", 2)) 
                                mode = MODE_DIALTONE;
@@ -967,13 +1085,15 @@ int load_module()
                        else
                                ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
                } else if (!strcasecmp(v->name, "context")) {
                        else
                                ast_log(LOG_WARNING, "Unknown mode: %s\n", v->value);
                } else if (!strcasecmp(v->name, "context")) {
-                       strncpy(context, v->value, sizeof(context));
+                       strncpy(context, v->value, sizeof(context)-1);
                } else if (!strcasecmp(v->name, "format")) {
                        if (!strcasecmp(v->value, "g723.1")) {
                                prefformat = AST_FORMAT_G723_1;
                        } else if (!strcasecmp(v->value, "slinear")) {
                                prefformat = AST_FORMAT_SLINEAR;
                } else if (!strcasecmp(v->name, "format")) {
                        if (!strcasecmp(v->value, "g723.1")) {
                                prefformat = AST_FORMAT_G723_1;
                        } else if (!strcasecmp(v->value, "slinear")) {
                                prefformat = AST_FORMAT_SLINEAR;
-                       } else 
+                       } else if (!strcasecmp(v->value, "ulaw")) {
+                               prefformat = AST_FORMAT_ULAW;
+                       } else
                                ast_log(LOG_WARNING, "Unknown format '%s'\n", v->value);
                } else if (!strcasecmp(v->name, "echocancel")) {
                        if (!strcasecmp(v->value, "off")) {
                                ast_log(LOG_WARNING, "Unknown format '%s'\n", v->value);
                } else if (!strcasecmp(v->name, "echocancel")) {
                        if (!strcasecmp(v->value, "off")) {
@@ -986,12 +1106,17 @@ int load_module()
                                echocancel = AEC_HIGH;
                        } else 
                                ast_log(LOG_WARNING, "Unknown echo cancellation '%s'\n", v->value);
                                echocancel = AEC_HIGH;
                        } else 
                                ast_log(LOG_WARNING, "Unknown echo cancellation '%s'\n", v->value);
-               }
+               } else if (!strcasecmp(v->name, "txgain")) {
+                       txgain = parse_gain_value(v->name, v->value);
+               } else if (!strcasecmp(v->name, "rxgain")) {
+                       rxgain = parse_gain_value(v->name, v->value);
+               }       
                v = v->next;
        }
                v = v->next;
        }
-       pthread_mutex_unlock(&iflock);
+       ast_pthread_mutex_unlock(&iflock);
        /* Make sure we can register our Adtranphone channel type */
        /* Make sure we can register our Adtranphone channel type */
-       if (ast_channel_register(type, tdesc, AST_FORMAT_G723_1, phone_request)) {
+       if (ast_channel_register(type, tdesc, 
+                        AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW, phone_request)) {
                ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
                ast_destroy(cfg);
                unload_module();
                ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
                ast_destroy(cfg);
                unload_module();
@@ -1010,33 +1135,33 @@ int unload_module()
        struct phone_pvt *p, *pl;
        /* First, take us out of the channel loop */
        ast_channel_unregister(type);
        struct phone_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)
                /* 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;
                        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;
        }
        } 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;
                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;
        }
 
        } 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) {
                /* Destroy all the interfaces and free their memory */
                p = iflist;
                while(p) {
@@ -1049,7 +1174,7 @@ int unload_module()
                        free(pl);
                }
                iflist = NULL;
                        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;
        } else {
                ast_log(LOG_WARNING, "Unable to lock the monitor\n");
                return -1;
@@ -1061,9 +1186,9 @@ int unload_module()
 int usecount()
 {
        int res;
 int usecount()
 {
        int res;
-       pthread_mutex_lock(&usecnt_lock);
+       ast_pthread_mutex_lock(&usecnt_lock);
        res = usecnt;
        res = usecnt;
-       pthread_mutex_unlock(&usecnt_lock);
+       ast_pthread_mutex_unlock(&usecnt_lock);
        return res;
 }
 
        return res;
 }