Version 0.3.0 from FTP
[asterisk/asterisk.git] / channels / chan_phone.c
index 7300646..09f5b1b 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>
@@ -55,14 +56,14 @@ static int silencesupression = 0;
 
 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) */
-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.  */
@@ -140,8 +141,31 @@ 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;
+
+       // 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;
-       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;
        }
@@ -149,8 +173,10 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
           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->fds[0]);
-       ioctl(p->fd, PHONE_RING_START);
-       ast->state = AST_STATE_RINGING;
+
+       ioctl(p->fd, PHONE_RING_START,&cid);
+       ast_setstate(ast, AST_STATE_RINGING);
+       ast_queue_control(ast, AST_CONTROL_RINGING, 0);
        return 0;
 }
 
@@ -165,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? */
-       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))
@@ -204,7 +230,7 @@ static int phone_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;
 }
@@ -269,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;
-       ast->state = AST_STATE_UP;
+       ast_setstate(ast, AST_STATE_UP);
        return 0;
 }
 
@@ -321,22 +347,22 @@ static struct ast_frame  *phone_exception(struct ast_channel *ast)
                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);
-                               ast->state = AST_STATE_UP;
+                               ast_setstate(ast, AST_STATE_UP);
                                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) {
-               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");
@@ -351,6 +377,7 @@ static struct ast_frame  *phone_read(struct ast_channel *ast)
 {
        int res;
        struct phone_pvt *p = ast->pvt->pvt;
+       
 
        /* Some nice norms */
        p->fr.datalen = 0;
@@ -406,24 +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) {
-#if 0
-               res = frlen;
-               ast_log(LOG_DEBUG, "Wrote %d bytes\n", res);
-#else
                res = write(p->fd, p->obuf, frlen);
-#endif
                if (res != frlen) {
                        if (res < 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.
  */
-#if 0
-                               ast_log(LOG_WARNING, "Write failed: %s\n", strerror(errno));
-                               return -1;
-#else
                                return 0;
-#endif
                        } else {
                                ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frlen);
                        }
@@ -448,21 +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) {
-               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 | AST_FORMAT_ULAW))) {
                ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
-               ast_frfree(frame);
                return -1;
        }
+#if 0
        /* 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);
        }
+#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);
@@ -560,17 +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) {
-                       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 (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);
+                               else
+                                       ast_log(LOG_WARNING, "Only wrote %d of %d bytes\n", res, frame->datalen);
 #endif
-                       return -1;
+                               return -1;
+                       } else /* Pretend it worked */
+                               res = expected;
                }
                sofar += res;
                pos += res;
@@ -581,7 +607,7 @@ 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;
-       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;
@@ -590,7 +616,7 @@ static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *conte
                tmp->nativeformats = prefformat;
                tmp->pvt->rawreadformat = prefformat;
                tmp->pvt->rawwriteformat = prefformat;
-               tmp->state = state;
+               ast_setstate(tmp, state);
                if (state == AST_STATE_RING)
                        tmp->rings = 1;
                tmp->pvt->pvt = i;
@@ -1114,7 +1140,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;