More BSD enhancements
[asterisk/asterisk.git] / channels / chan_phone.c
index ee45e39..80232c3 100755 (executable)
@@ -141,24 +141,23 @@ 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;
-       struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_RINGING };
 
        // CID stuff for the phonejack...
 
        PHONE_CID cid;
        time_t UtcTime;
-       struct tm *t;
+       struct tm tm;
 
 
        if (ast->callerid) {
                time(&UtcTime);
-               t = localtime(&UtcTime);
+               localtime_r(&UtcTime,&tm);
 
-               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);
+               if(&tm != NULL) {
+                       sprintf(cid.month, "%02d",(tm.tm_mon + 1));
+                       sprintf(cid.day, "%02d", tm.tm_mday);
+                       sprintf(cid.hour, "%02d", tm.tm_hour);
+                       sprintf(cid.min, "%02d", tm.tm_min);
                }
                strcpy(cid.name, "Unknown");
                sprintf(cid.number,"%s",ast->callerid);
@@ -166,7 +165,7 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
 
        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;
        }
@@ -176,8 +175,8 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
                ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fds[0]);
 
        ioctl(p->fd, PHONE_RING_START,&cid);
-       ast_queue_frame(ast, &f, 0);
-       ast->state = AST_STATE_RINGING;
+       ast_setstate(ast, AST_STATE_RINGING);
+       ast_queue_control(ast, AST_CONTROL_RINGING, 0);
        return 0;
 }
 
@@ -192,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))
@@ -231,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;
 }
@@ -296,10 +295,11 @@ 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;
 }
 
+#if 0
 static char phone_2digit(char c)
 {
        if (c == 12)
@@ -311,6 +311,7 @@ static char phone_2digit(char c)
        else
                return '?';
 }
+#endif
 
 static struct ast_frame  *phone_exception(struct ast_channel *ast)
 {
@@ -321,7 +322,7 @@ static struct ast_frame  *phone_exception(struct ast_channel *ast)
 
        /* Some nice norms */
        p->fr.datalen = 0;
-       p->fr.timelen = 0;
+       p->fr.samples = 0;
        p->fr.data =  NULL;
        p->fr.src = type;
        p->fr.offset = 0;
@@ -348,15 +349,15 @@ 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
@@ -382,7 +383,7 @@ static struct ast_frame  *phone_read(struct ast_channel *ast)
 
        /* Some nice norms */
        p->fr.datalen = 0;
-       p->fr.timelen = 0;
+       p->fr.samples = 0;
        p->fr.data =  NULL;
        p->fr.src = type;
        p->fr.offset = 0;
@@ -416,6 +417,7 @@ static struct ast_frame  *phone_read(struct ast_channel *ast)
                res = 4;
                break;
        }
+       p->fr.samples = 240;
        p->fr.datalen = res;
        p->fr.frametype = AST_FRAME_VOICE;
        p->fr.subclass = p->lastinput;
@@ -434,24 +436,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);
                        }
@@ -485,11 +477,18 @@ static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
                ast_log(LOG_WARNING, "Cannot handle frames in %d format\n", frame->subclass);
                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);
@@ -587,7 +586,7 @@ static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
                        res = phone_write_buf(p, pos, expected, maxfr);
                }
                if (res != expected) {
-                       if (errno != EAGAIN) {
+                       if ((errno != EAGAIN) && (errno != EINTR)) {
                                if (res < 0) 
                                        ast_log(LOG_WARNING, "Write returned error (%s)\n", strerror(errno));
        /*
@@ -620,7 +619,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;
@@ -634,6 +633,8 @@ static struct ast_channel *phone_new(struct phone_pvt *i, int state, char *conte
                strncpy(tmp->context, context, sizeof(tmp->context)-1);
                if (strlen(i->ext))
                        strncpy(tmp->exten, i->ext, sizeof(tmp->exten)-1);
+               else
+                       strncpy(tmp->exten, "s",  sizeof(tmp->exten) - 1);
                if (strlen(i->language))
                        strncpy(tmp->language, i->language, sizeof(tmp->language)-1);
                if (strlen(i->callerid))
@@ -847,9 +848,9 @@ static void *do_monitor(void *data)
                                tv.tv_usec = 30000;
                                tv.tv_sec = 0;
                        }
-                       res = select(n + 1, &rfds, NULL, &efds, &tv);
+                       res = ast_select(n + 1, &rfds, NULL, &efds, &tv);
                } else {
-                       res = select(n + 1, &rfds, NULL, &efds, NULL);
+                       res = ast_select(n + 1, &rfds, NULL, &efds, NULL);
                        tv.tv_usec = 0;
                        tv.tv_sec = 0;
                        tonepos = 0;
@@ -1144,7 +1145,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;