don't use '%i' at all, since we have no current use cases that need non base-10 parsi...
[asterisk/asterisk.git] / channels / chan_phone.c
index dfc9a84..dd8aa65 100755 (executable)
@@ -3,7 +3,7 @@
  *
  * Generic Linux Telephony Interface driver
  * 
- * Copyright (C) 1999-2004, Digium, Inc.
+ * Copyright (C) 1999 - 2005, Digium, Inc.
  *
  * Mark Spencer <markster@digium.com>
  *
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
-#include <asterisk/lock.h>
-#include <asterisk/channel.h>
-#include <asterisk/channel_pvt.h>
-#include <asterisk/config.h>
-#include <asterisk/logger.h>
-#include <asterisk/module.h>
-#include <asterisk/pbx.h>
-#include <asterisk/options.h>
-#include <asterisk/utils.h>
-#include <asterisk/callerid.h>
-#include <asterisk/causes.h>
+#include "asterisk/lock.h"
+#include "asterisk/channel.h"
+#include "asterisk/config.h"
+#include "asterisk/logger.h"
+#include "asterisk/module.h"
+#include "asterisk/pbx.h"
+#include "asterisk/options.h"
+#include "asterisk/utils.h"
+#include "asterisk/callerid.h"
+#include "asterisk/causes.h"
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <errno.h>
 #define PHONE_MAX_BUF 480
 #define DEFAULT_GAIN 0x100
 
-static char *desc = "Linux Telephony API Support";
-static char *type = "Phone";
-static char *tdesc = "Standard Linux Telephony API Driver";
-static char *config = "phone.conf";
+static const char desc[] = "Linux Telephony API Support";
+static const char type[] = "Phone";
+static const char tdesc[] = "Standard Linux Telephony API Driver";
+static const char config[] = "phone.conf";
 
 /* Default context for dialtone mode */
 static char context[AST_MAX_EXTENSION] = "default";
@@ -132,11 +131,63 @@ static struct phone_pvt {
 static char cid_num[AST_MAX_EXTENSION];
 static char cid_name[AST_MAX_EXTENSION];
 
+static struct ast_channel *phone_request(const char *type, int format, void *data, int *cause);
+static int phone_digit(struct ast_channel *ast, char digit);
+static int phone_call(struct ast_channel *ast, char *dest, int timeout);
+static int phone_hangup(struct ast_channel *ast);
+static int phone_answer(struct ast_channel *ast);
+static struct ast_frame *phone_read(struct ast_channel *ast);
+static int phone_write(struct ast_channel *ast, struct ast_frame *frame);
+static struct ast_frame *phone_exception(struct ast_channel *ast);
+static int phone_send_text(struct ast_channel *ast, const char *text);
+static int phone_fixup(struct ast_channel *old, struct ast_channel *new);
+
+static const struct ast_channel_tech phone_tech = {
+       .type = type,
+       .description = tdesc,
+       .capabilities = AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | AST_FORMAT_ULAW,
+       .requester = phone_request,
+       .send_digit = phone_digit,
+       .call = phone_call,
+       .hangup = phone_hangup,
+       .answer = phone_answer,
+       .read = phone_read,
+       .write = phone_write,
+       .exception = phone_exception,
+       .fixup = phone_fixup
+};
+
+static struct ast_channel_tech phone_tech_fxs = {
+       .type = type,
+       .description = tdesc,
+       .requester = phone_request,
+       .send_digit = phone_digit,
+       .call = phone_call,
+       .hangup = phone_hangup,
+       .answer = phone_answer,
+       .read = phone_read,
+       .write = phone_write,
+       .exception = phone_exception,
+       .write_video = phone_write,
+       .send_text = phone_send_text,
+       .fixup = phone_fixup
+};
+
+static struct ast_channel_tech *cur_tech;
+
+static int phone_fixup(struct ast_channel *old, struct ast_channel *new)
+{
+       struct phone_pvt *pvt = old->tech_pvt;
+       if (pvt && pvt->owner == old)
+               pvt->owner = new;
+       return 0;
+}
+
 static int phone_digit(struct ast_channel *ast, char digit)
 {
        struct phone_pvt *p;
        int outdigit;
-       p = ast->pvt->pvt;
+       p = ast->tech_pvt;
        ast_log(LOG_NOTICE, "Dialed %c\n", digit);
        switch(digit) {
        case '0':
@@ -157,7 +208,7 @@ static int phone_digit(struct ast_channel *ast, char digit)
        case '#':
                outdigit = 12;
                break;
-       case 'f':       //flash
+       case 'f':       /*flash*/
        case 'F':
                ioctl(p->fd, IXJCTL_PSTN_SET_STATE, PSTN_ON_HOOK);
                usleep(320000);
@@ -168,7 +219,7 @@ static int phone_digit(struct ast_channel *ast, char digit)
                ast_log(LOG_WARNING, "Unknown digit '%c'\n", digit);
                return -1;
        }
-       ast_log(LOG_NOTICE, "Dialed %i\n", outdigit);
+       ast_log(LOG_NOTICE, "Dialed %d\n", outdigit);
        ioctl(p->fd, PHONE_PLAY_TONE, outdigit);
        p->lastformat = -1;
        return 0;
@@ -181,6 +232,7 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
        PHONE_CID cid;
        time_t UtcTime;
        struct tm tm;
+       int start;
 
        time(&UtcTime);
        localtime_r(&UtcTime,&tm);
@@ -201,7 +253,7 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
        if (ast->cid.cid_num) 
                strncpy(cid.number, ast->cid.cid_num, sizeof(cid.number) - 1);
 
-       p = ast->pvt->pvt;
+       p = ast->tech_pvt;
 
        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);
@@ -210,7 +262,7 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
        if (option_debug)
                ast_log(LOG_DEBUG, "Ringing %s on %s (%d)\n", dest, ast->name, ast->fds[0]);
 
-       int start = IXJ_PHONE_RING_START(cid);
+       start = IXJ_PHONE_RING_START(cid);
        if (start == -1)
                return -1;
        
@@ -232,10 +284,10 @@ static int phone_call(struct ast_channel *ast, char *dest, int timeout)
 static int phone_hangup(struct ast_channel *ast)
 {
        struct phone_pvt *p;
-       p = ast->pvt->pvt;
+       p = ast->tech_pvt;
        if (option_debug)
                ast_log(LOG_DEBUG, "phone_hangup(%s)\n", ast->name);
-       if (!ast->pvt->pvt) {
+       if (!ast->tech_pvt) {
                ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
                return 0;
        }
@@ -269,7 +321,7 @@ static int phone_hangup(struct ast_channel *ast)
        p->obuflen = 0;
        p->dialtone = 0;
        memset(p->ext, 0, sizeof(p->ext));
-       ((struct phone_pvt *)(ast->pvt->pvt))->owner = NULL;
+       ((struct phone_pvt *)(ast->tech_pvt))->owner = NULL;
        ast_mutex_lock(&usecnt_lock);
        usecnt--;
        if (usecnt < 0) 
@@ -278,7 +330,7 @@ static int phone_hangup(struct ast_channel *ast)
        ast_update_use_count();
        if (option_verbose > 2) 
                ast_verbose( VERBOSE_PREFIX_3 "Hungup '%s'\n", ast->name);
-       ast->pvt->pvt = NULL;
+       ast->tech_pvt = NULL;
        ast_setstate(ast, AST_STATE_DOWN);
        restart_monitor();
        return 0;
@@ -287,10 +339,10 @@ static int phone_hangup(struct ast_channel *ast)
 static int phone_setup(struct ast_channel *ast)
 {
        struct phone_pvt *p;
-       p = ast->pvt->pvt;
+       p = ast->tech_pvt;
        ioctl(p->fd, PHONE_CPT_STOP);
        /* Nothing to answering really, just start recording */
-       if (ast->pvt->rawreadformat == AST_FORMAT_G723_1) {
+       if (ast->rawreadformat == AST_FORMAT_G723_1) {
                /* Prefer g723 */
                ioctl(p->fd, PHONE_REC_STOP);
                if (p->lastinput != AST_FORMAT_G723_1) {
@@ -300,7 +352,7 @@ static int phone_setup(struct ast_channel *ast)
                                return -1;
                        }
                }
-       } else if (ast->pvt->rawreadformat == AST_FORMAT_SLINEAR) {
+       } else if (ast->rawreadformat == AST_FORMAT_SLINEAR) {
                ioctl(p->fd, PHONE_REC_STOP);
                if (p->lastinput != AST_FORMAT_SLINEAR) {
                        p->lastinput = AST_FORMAT_SLINEAR;
@@ -309,7 +361,7 @@ static int phone_setup(struct ast_channel *ast)
                                return -1;
                        }
                }
-       } else if (ast->pvt->rawreadformat == AST_FORMAT_ULAW) {
+       } else if (ast->rawreadformat == AST_FORMAT_ULAW) {
                ioctl(p->fd, PHONE_REC_STOP);
                if (p->lastinput != AST_FORMAT_ULAW) {
                        p->lastinput = AST_FORMAT_ULAW;
@@ -320,23 +372,23 @@ static int phone_setup(struct ast_channel *ast)
                }
        } else if (p->mode == MODE_FXS) {
                ioctl(p->fd, PHONE_REC_STOP);
-               if (p->lastinput != ast->pvt->rawreadformat) {
-                       p->lastinput = ast->pvt->rawreadformat;
-                       if (ioctl(p->fd, PHONE_REC_CODEC, ast->pvt->rawreadformat)) {
+               if (p->lastinput != ast->rawreadformat) {
+                       p->lastinput = ast->rawreadformat;
+                       if (ioctl(p->fd, PHONE_REC_CODEC, ast->rawreadformat)) {
                                ast_log(LOG_WARNING, "Failed to set codec to %d\n", 
-                                       ast->pvt->rawreadformat);
+                                       ast->rawreadformat);
                                return -1;
                        }
                }
        } else {
-               ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast->pvt->rawreadformat));
+               ast_log(LOG_WARNING, "Can't do format %s\n", ast_getformatname(ast->rawreadformat));
                return -1;
        }
        if (ioctl(p->fd, PHONE_REC_START)) {
                ast_log(LOG_WARNING, "Failed to start recording\n");
                return -1;
        }
-       //set the DTMF times (the default is too short)
+       /* set the DTMF times (the default is too short) */
        ioctl(p->fd, PHONE_SET_TONE_ON_TIME, 300);
        ioctl(p->fd, PHONE_SET_TONE_OFF_TIME, 200);
        return 0;
@@ -345,7 +397,7 @@ static int phone_setup(struct ast_channel *ast)
 static int phone_answer(struct ast_channel *ast)
 {
        struct phone_pvt *p;
-       p = ast->pvt->pvt;
+       p = ast->tech_pvt;
        /* In case it's a LineJack, take it off hook */
        if (p->mode == MODE_FXO) {
                if (ioctl(p->fd, PHONE_PSTN_SET_STATE, PSTN_OFF_HOOK)) 
@@ -379,7 +431,7 @@ static struct ast_frame  *phone_exception(struct ast_channel *ast)
 {
        int res;
        union telephony_exception phonee;
-       struct phone_pvt *p = ast->pvt->pvt;
+       struct phone_pvt *p = ast->tech_pvt;
        char digit;
 
        /* Some nice norms */
@@ -442,7 +494,7 @@ static struct ast_frame  *phone_exception(struct ast_channel *ast)
 static struct ast_frame  *phone_read(struct ast_channel *ast)
 {
        int res;
-       struct phone_pvt *p = ast->pvt->pvt;
+       struct phone_pvt *p = ast->tech_pvt;
        
 
        /* Some nice norms */
@@ -492,10 +544,13 @@ static struct ast_frame  *phone_read(struct ast_channel *ast)
                          : AST_FRAME_VIDEO;
        p->fr.subclass = p->lastinput;
        p->fr.offset = AST_FRIENDLY_OFFSET;
+       /* Byteswap from little-endian to native-endian */
+       if (p->fr.subclass == AST_FORMAT_SLINEAR)
+               ast_frame_byteswap_le(&p->fr);
        return &p->fr;
 }
 
-static int phone_write_buf(struct phone_pvt *p, char *buf, int len, int frlen)
+static int phone_write_buf(struct phone_pvt *p, const char *buf, int len, int frlen, int swap)
 {
        int res;
        /* Store as much of the buffer as we can, then write fixed frames */
@@ -503,7 +558,10 @@ static int phone_write_buf(struct phone_pvt *p, char *buf, int len, int frlen)
        /* Make sure we have enough buffer space to store the frame */
        if (space < len)
                len = space;
-       memcpy(p->obuf + p->obuflen, buf, len);
+       if (swap)
+               ast_swapcopy_samples(p->obuf+p->obuflen, buf, len/2);
+       else
+               memcpy(p->obuf + p->obuflen, buf, len);
        p->obuflen += len;
        while(p->obuflen > frlen) {
                res = write(p->fd, p->obuf, frlen);
@@ -526,15 +584,16 @@ static int phone_write_buf(struct phone_pvt *p, char *buf, int len, int frlen)
        return len;
 }
 
-static int phone_send_text(struct ast_channel *ast, char *text)
+static int phone_send_text(struct ast_channel *ast, const char *text)
 {
     int length = strlen(text);
-    return phone_write_buf(ast->pvt->pvt, text, length, length);
+    return phone_write_buf(ast->tech_pvt, text, length, length, 0) == 
+           length ? 0 : -1;
 }
 
 static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
 {
-       struct phone_pvt *p = ast->pvt->pvt;
+       struct phone_pvt *p = ast->tech_pvt;
        int res;
        int maxfr=0;
        char *pos;
@@ -676,12 +735,17 @@ static int phone_write(struct ast_channel *ast, struct ast_frame *frame)
                                memset(tmpbuf + 4, 0, sizeof(tmpbuf) - 4);
                                memcpy(tmpbuf, frame->data, 4);
                                expected = 24;
-                               res = phone_write_buf(p, tmpbuf, expected, maxfr);
+                               res = phone_write_buf(p, tmpbuf, expected, maxfr, 0);
                        }
                        res = 4;
                        expected=4;
                } else {
-                       res = phone_write_buf(p, pos, expected, maxfr);
+                       int swap = 0;
+#if __BYTE_ORDER == __BIG_ENDIAN
+                       if (frame->subclass == AST_FORMAT_SLINEAR)
+                               swap = 1; /* Swap big-endian samples to little-endian as we copy */
+#endif
+                       res = phone_write_buf(p, pos, expected, maxfr, swap);
                }
                if (res != expected) {
                        if ((errno != EAGAIN) && (errno != EINTR)) {
@@ -708,47 +772,37 @@ 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;
+       struct phone_codec_data codec;
        tmp = ast_channel_alloc(1);
        if (tmp) {
+               tmp->tech = cur_tech;
                snprintf(tmp->name, sizeof(tmp->name), "Phone/%s", i->dev + 5);
                tmp->type = type;
                tmp->fds[0] = i->fd;
                /* XXX Switching formats silently causes kernel panics XXX */
-               struct phone_codec_data codec;
                if (i->mode == MODE_FXS &&
                    ioctl(i->fd, PHONE_QUERY_CODEC, &codec) == 0) {
                        if (codec.type == LINEAR16)
                                tmp->nativeformats =
-                               tmp->pvt->rawreadformat =
-                               tmp->pvt->rawwriteformat =
+                               tmp->rawreadformat =
+                               tmp->rawwriteformat =
                                AST_FORMAT_SLINEAR;
                        else {
                                tmp->nativeformats =
-                               tmp->pvt->rawreadformat =
-                               tmp->pvt->rawwriteformat =
+                               tmp->rawreadformat =
+                               tmp->rawwriteformat =
                                prefformat & ~AST_FORMAT_SLINEAR;
                        }
                }
                else {
                        tmp->nativeformats = prefformat;
-                       tmp->pvt->rawreadformat = prefformat;
-                       tmp->pvt->rawwriteformat = prefformat;
+                       tmp->rawreadformat = prefformat;
+                       tmp->rawwriteformat = prefformat;
                }
                ast_setstate(tmp, state);
                if (state == AST_STATE_RING)
                        tmp->rings = 1;
-               tmp->pvt->pvt = i;
-               tmp->pvt->send_digit = phone_digit;
-               tmp->pvt->call = phone_call;
-               tmp->pvt->hangup = phone_hangup;
-               tmp->pvt->answer = phone_answer;
-               tmp->pvt->read = phone_read;
-               tmp->pvt->write = phone_write;
-               if (i->mode == MODE_FXS) {
-                       tmp->pvt->write_video = phone_write;
-                       tmp->pvt->send_text = phone_send_text;
-               }
-               tmp->pvt->exception = phone_exception;
+               tmp->tech_pvt = i;
                strncpy(tmp->context, context, sizeof(tmp->context)-1);
                if (strlen(i->ext))
                        strncpy(tmp->exten, i->ext, sizeof(tmp->exten)-1);
@@ -1170,7 +1224,7 @@ static int __unload_module(void)
 {
        struct phone_pvt *p, *pl;
        /* First, take us out of the channel loop */
-       ast_channel_unregister(type);
+       ast_channel_unregister(cur_tech);
        if (!ast_mutex_lock(&iflock)) {
                /* Hangup all interfaces if they have an owner */
                p = iflist;
@@ -1231,7 +1285,7 @@ int load_module()
        struct phone_pvt *tmp;
        int mode = MODE_IMMEDIATE;
        int txgain = DEFAULT_GAIN, rxgain = DEFAULT_GAIN; /* default gain 1.0 */
-       cfg = ast_load(config);
+       cfg = ast_config_load(config);
 
        /* We *must* have a config file otherwise stop immediately */
        if (!cfg) {
@@ -1254,7 +1308,7 @@ int load_module()
                                        
                                } else {
                                        ast_log(LOG_ERROR, "Unable to register channel '%s'\n", v->value);
-                                       ast_destroy(cfg);
+                                       ast_config_destroy(cfg);
                                        ast_mutex_unlock(&iflock);
                                        __unload_module();
                                        return -1;
@@ -1310,16 +1364,22 @@ int load_module()
                v = v->next;
        }
        ast_mutex_unlock(&iflock);
+
+       if (mode == MODE_FXS) {
+               phone_tech_fxs.capabilities = prefformat;
+               cur_tech = &phone_tech_fxs;
+       } else
+               cur_tech = (struct ast_channel_tech *) &phone_tech;
+
        /* Make sure we can register our Adtranphone channel type */
-       if (ast_channel_register(type, tdesc, mode != MODE_FXS ?
-                        AST_FORMAT_G723_1 | AST_FORMAT_SLINEAR | 
-                        AST_FORMAT_ULAW : prefformat, phone_request)) {
+
+       if (ast_channel_register(cur_tech)) {
                ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
-               ast_destroy(cfg);
+               ast_config_destroy(cfg);
                __unload_module();
                return -1;
        }
-       ast_destroy(cfg);
+       ast_config_destroy(cfg);
        /* And start the monitor for the first time */
        restart_monitor();
        return 0;
@@ -1336,7 +1396,7 @@ int usecount()
 
 char *description()
 {
-       return desc;
+       return (char *) desc;
 }
 
 char *key()