install_prereq: Download latest Jansson.
[asterisk/asterisk.git] / channels / chan_oss.c
index 9d1c456..69863d2 100644 (file)
  * \author Mark Spencer <markster@digium.com>
  * \author Luigi Rizzo
  *
- * \par See also
- * \arg \ref Config_oss
- *
  * \ingroup channel_drivers
  */
 
+/*! \li \ref chan_oss.c uses the configuration file \ref oss.conf
+ * \addtogroup configuration_file
+ */
+
+/*! \page oss.conf oss.conf
+ * \verbinclude oss.conf.sample
+ */
+
 /*** MODULEINFO
-       <depend>ossaudio</depend>
+       <depend>oss</depend>
+       <support_level>extended</support_level>
  ***/
 
 #include "asterisk.h"
 
-ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
-
 #include <ctype.h>             /* isalnum() used here */
 #include <math.h>
-#include <sys/ioctl.h>         
+#include <sys/ioctl.h>
 
 #ifdef __linux
 #include <linux/soundcard.h>
-#elif defined(__FreeBSD__) || defined(__CYGWIN__)
+#elif defined(__FreeBSD__) || defined(__CYGWIN__) || defined(__GLIBC__) || defined(__sun)
 #include <sys/soundcard.h>
 #else
 #include <soundcard.h>
@@ -62,16 +66,20 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/causes.h"
 #include "asterisk/musiconhold.h"
 #include "asterisk/app.h"
+#include "asterisk/bridge.h"
+#include "asterisk/format_cache.h"
 
 #include "console_video.h"
 
-/*! Global jitterbuffer configuration - by default, jb is disabled */
+/*! Global jitterbuffer configuration - by default, jb is disabled
+ *  \note Values shown here match the defaults shown in oss.conf.sample */
 static struct ast_jb_conf default_jbconf =
 {
        .flags = 0,
-       .max_size = -1,
-       .resync_threshold = -1,
-       .impl = "",
+       .max_size = 200,
+       .resync_threshold = 1000,
+       .impl = "fixed",
+       .target_extra = 40,
 };
 static struct ast_jb_conf global_jbconf;
 
@@ -229,13 +237,6 @@ END_CONFIG
 #define DEV_DSP "/dev/dsp"
 #endif
 
-#ifndef MIN
-#define MIN(a,b) ((a) < (b) ? (a) : (b))
-#endif
-#ifndef MAX
-#define MAX(a,b) ((a) > (b) ? (a) : (b))
-#endif
-
 static char *config = "oss.conf";      /* default config file */
 
 static int oss_debug;
@@ -255,9 +256,9 @@ struct chan_oss_pvt {
        int total_blocks;                       /*!< total blocks in the output device */
        int sounddev;
        enum { M_UNSET, M_FULL, M_READ, M_WRITE } duplex;
-       int autoanswer;
-       int autohangup;
-       int hookstate;
+       int autoanswer;             /*!< Boolean: whether to answer the immediately upon calling */
+       int autohangup;             /*!< Boolean: whether to hangup the call when the remote end hangs up */
+       int hookstate;              /*!< Boolean: 1 if offhook; 0 if onhook */
        char *mixer_cmd;                        /*!< initial command to issue to the mixer */
        unsigned int queuesize;         /*!< max fragments in queue */
        unsigned int frags;                     /*!< parameter for SETFRAGMENT */
@@ -289,8 +290,8 @@ struct chan_oss_pvt {
        char ext[AST_MAX_EXTENSION];
        char ctx[AST_MAX_CONTEXT];
        char language[MAX_LANGUAGE];
-       char cid_name[256];                     /*XXX */
-       char cid_num[256];                      /*XXX */
+       char cid_name[256];         /*!< Initial CallerID name */
+       char cid_num[256];          /*!< Initial CallerID number  */
        char mohinterpret[MAX_MUSICCLASS];
 
        /*! buffers used in oss_write */
@@ -305,14 +306,14 @@ struct chan_oss_pvt {
 };
 
 /*! forward declaration */
-static struct chan_oss_pvt *find_desc(char *dev);
+static struct chan_oss_pvt *find_desc(const char *dev);
 
 static char *oss_active;        /*!< the active device */
 
 /*! \brief return the pointer to the video descriptor */
 struct video_desc *get_video_desc(struct ast_channel *c)
 {
-       struct chan_oss_pvt *o = c ? c->tech_pvt : find_desc(oss_active);
+       struct chan_oss_pvt *o = c ? ast_channel_tech_pvt(c) : find_desc(oss_active);
        return o ? o->env : NULL;
 }
 static struct chan_oss_pvt oss_default = {
@@ -332,15 +333,15 @@ static struct chan_oss_pvt oss_default = {
 
 static int setformat(struct chan_oss_pvt *o, int mode);
 
-static struct ast_channel *oss_request(const char *type, int format, void *data
-, int *cause);
+static struct ast_channel *oss_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor,
+                                                                          const char *data, int *cause);
 static int oss_digit_begin(struct ast_channel *c, char digit);
 static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration);
 static int oss_text(struct ast_channel *c, const char *text);
 static int oss_hangup(struct ast_channel *c);
 static int oss_answer(struct ast_channel *c);
 static struct ast_frame *oss_read(struct ast_channel *chan);
-static int oss_call(struct ast_channel *c, char *dest, int timeout);
+static int oss_call(struct ast_channel *c, const char *dest, int timeout);
 static int oss_write(struct ast_channel *chan, struct ast_frame *f);
 static int oss_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
@@ -350,7 +351,6 @@ static char tdesc[] = "OSS Console Channel Driver";
 static struct ast_channel_tech oss_tech = {
        .type = "Console",
        .description = tdesc,
-       .capabilities = AST_FORMAT_SLINEAR, /* overwritten later */
        .requester = oss_request,
        .send_digit_begin = oss_digit_begin,
        .send_digit_end = oss_digit_end,
@@ -368,7 +368,7 @@ static struct ast_channel_tech oss_tech = {
 /*!
  * \brief returns a pointer to the descriptor with the given name
  */
-static struct chan_oss_pvt *find_desc(char *dev)
+static struct chan_oss_pvt *find_desc(const char *dev)
 {
        struct chan_oss_pvt *o = NULL;
 
@@ -536,7 +536,7 @@ static int setformat(struct chan_oss_pvt *o, int mode)
        res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
 
        if (res < 0) {
-               ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
+               ast_log(LOG_WARNING, "Failed to set sample rate to %d\n", desired);
                return -1;
        }
        if (fmt != desired) {
@@ -580,7 +580,7 @@ static int oss_digit_begin(struct ast_channel *c, char digit)
 static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration)
 {
        /* no better use for received digits than print them */
-       ast_verbose(" << Console Received digit %c of duration %u ms >> \n", 
+       ast_verbose(" << Console Received digit %c of duration %u ms >> \n",
                digit, duration);
        return 0;
 }
@@ -595,10 +595,10 @@ static int oss_text(struct ast_channel *c, const char *text)
 /*!
  * \brief handler for incoming calls. Either autoanswer, or start ringing
  */
-static int oss_call(struct ast_channel *c, char *dest, int timeout)
+static int oss_call(struct ast_channel *c, const char *dest, int timeout)
 {
-       struct chan_oss_pvt *o = c->tech_pvt;
-       struct ast_frame f = { 0, };
+       struct chan_oss_pvt *o = ast_channel_tech_pvt(c);
+       struct ast_frame f = { AST_FRAME_CONTROL, };
        AST_DECLARE_APP_ARGS(args,
                AST_APP_ARG(name);
                AST_APP_ARG(flags);
@@ -607,25 +607,27 @@ static int oss_call(struct ast_channel *c, char *dest, int timeout)
 
        AST_NONSTANDARD_APP_ARGS(args, parse, '/');
 
-       ast_verbose(" << Call to device '%s' dnid '%s' rdnis '%s' on console from '%s' <%s> >>\n", dest, c->cid.cid_dnid, c->cid.cid_rdnis, c->cid.cid_name, c->cid.cid_num);
+       ast_verbose(" << Call to device '%s' dnid '%s' rdnis '%s' on console from '%s' <%s> >>\n",
+               dest,
+               S_OR(ast_channel_dialed(c)->number.str, ""),
+               S_COR(ast_channel_redirecting(c)->from.number.valid, ast_channel_redirecting(c)->from.number.str, ""),
+               S_COR(ast_channel_caller(c)->id.name.valid, ast_channel_caller(c)->id.name.str, ""),
+               S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, ""));
        if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "answer") == 0) {
-               f.frametype = AST_FRAME_CONTROL;
-               f.subclass = AST_CONTROL_ANSWER;
+               f.subclass.integer = AST_CONTROL_ANSWER;
                ast_queue_frame(c, &f);
        } else if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "noanswer") == 0) {
-               f.frametype = AST_FRAME_CONTROL;
-               f.subclass = AST_CONTROL_RINGING;
+               f.subclass.integer = AST_CONTROL_RINGING;
                ast_queue_frame(c, &f);
                ast_indicate(c, AST_CONTROL_RINGING);
        } else if (o->autoanswer) {
                ast_verbose(" << Auto-answered >> \n");
-               f.frametype = AST_FRAME_CONTROL;
-               f.subclass = AST_CONTROL_ANSWER;
+               f.subclass.integer = AST_CONTROL_ANSWER;
                ast_queue_frame(c, &f);
+               o->hookstate = 1;
        } else {
                ast_verbose("<< Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
-               f.frametype = AST_FRAME_CONTROL;
-               f.subclass = AST_CONTROL_RINGING;
+               f.subclass.integer = AST_CONTROL_RINGING;
                ast_queue_frame(c, &f);
                ast_indicate(c, AST_CONTROL_RINGING);
        }
@@ -637,16 +639,18 @@ static int oss_call(struct ast_channel *c, char *dest, int timeout)
  */
 static int oss_answer(struct ast_channel *c)
 {
+       struct chan_oss_pvt *o = ast_channel_tech_pvt(c);
        ast_verbose(" << Console call has been answered >> \n");
        ast_setstate(c, AST_STATE_UP);
+       o->hookstate = 1;
        return 0;
 }
 
 static int oss_hangup(struct ast_channel *c)
 {
-       struct chan_oss_pvt *o = c->tech_pvt;
+       struct chan_oss_pvt *o = ast_channel_tech_pvt(c);
 
-       c->tech_pvt = NULL;
+       ast_channel_tech_pvt_set(c, NULL);
        o->owner = NULL;
        ast_verbose(" << Hangup on console >> \n");
        console_video_uninit(o->env);
@@ -665,7 +669,7 @@ static int oss_hangup(struct ast_channel *c)
 static int oss_write(struct ast_channel *c, struct ast_frame *f)
 {
        int src;
-       struct chan_oss_pvt *o = c->tech_pvt;
+       struct chan_oss_pvt *o = ast_channel_tech_pvt(c);
 
        /*
         * we could receive a block which is not a multiple of our
@@ -679,13 +683,13 @@ static int oss_write(struct ast_channel *c, struct ast_frame *f)
                int l = sizeof(o->oss_write_buf) - o->oss_write_dst;
 
                if (f->datalen - src >= l) {    /* enough to fill a frame */
-                       memcpy(o->oss_write_buf + o->oss_write_dst, f->data + src, l);
+                       memcpy(o->oss_write_buf + o->oss_write_dst, f->data.ptr + src, l);
                        soundcard_writeframe(o, (short *) o->oss_write_buf);
                        src += l;
                        o->oss_write_dst = 0;
                } else {                                /* copy residue */
                        l = f->datalen - src;
-                       memcpy(o->oss_write_buf + o->oss_write_dst, f->data + src, l);
+                       memcpy(o->oss_write_buf + o->oss_write_dst, f->data.ptr + src, l);
                        src += l;                       /* but really, we are done */
                        o->oss_write_dst += l;
                }
@@ -696,12 +700,12 @@ static int oss_write(struct ast_channel *c, struct ast_frame *f)
 static struct ast_frame *oss_read(struct ast_channel *c)
 {
        int res;
-       struct chan_oss_pvt *o = c->tech_pvt;
+       struct chan_oss_pvt *o = ast_channel_tech_pvt(c);
        struct ast_frame *f = &o->read_f;
 
        /* XXX can be simplified returning &ast_null_frame */
        /* prepare a NULL frame in case we don't have enough data to return */
-       bzero(f, sizeof(struct ast_frame));
+       memset(f, '\0', sizeof(struct ast_frame));
        f->frametype = AST_FRAME_NULL;
        f->src = oss_tech.type;
 
@@ -717,17 +721,17 @@ static struct ast_frame *oss_read(struct ast_channel *c)
                return f;
 
        o->readpos = AST_FRIENDLY_OFFSET;       /* reset read pointer for next frame */
-       if (c->_state != AST_STATE_UP)  /* drop data if frame is not up */
+       if (ast_channel_state(c) != AST_STATE_UP)       /* drop data if frame is not up */
                return f;
        /* ok we can build and deliver the frame to the caller */
        f->frametype = AST_FRAME_VOICE;
-       f->subclass = AST_FORMAT_SLINEAR;
+       f->subclass.format = ast_format_slin;
        f->samples = FRAME_SIZE;
        f->datalen = FRAME_SIZE * 2;
-       f->data = o->oss_read_buf + AST_FRIENDLY_OFFSET;
+       f->data.ptr = o->oss_read_buf + AST_FRIENDLY_OFFSET;
        if (o->boost != BOOST_SCALE) {  /* scale and clip values */
                int i, x;
-               int16_t *p = (int16_t *) f->data;
+               int16_t *p = (int16_t *) f->data.ptr;
                for (i = 0; i < f->samples; i++) {
                        x = (p[i] * o->boost) / BOOST_SCALE;
                        if (x > 32767)
@@ -744,20 +748,22 @@ static struct ast_frame *oss_read(struct ast_channel *c)
 
 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
 {
-       struct chan_oss_pvt *o = newchan->tech_pvt;
+       struct chan_oss_pvt *o = ast_channel_tech_pvt(newchan);
        o->owner = newchan;
        return 0;
 }
 
 static int oss_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen)
 {
-       struct chan_oss_pvt *o = c->tech_pvt;
+       struct chan_oss_pvt *o = ast_channel_tech_pvt(c);
        int res = 0;
 
        switch (cond) {
+       case AST_CONTROL_INCOMPLETE:
        case AST_CONTROL_BUSY:
        case AST_CONTROL_CONGESTION:
        case AST_CONTROL_RINGING:
+       case AST_CONTROL_PVT_CAUSE_CODE:
        case -1:
                res = -1;
                break;
@@ -775,7 +781,7 @@ static int oss_indicate(struct ast_channel *c, int cond, const void *data, size_
                ast_moh_stop(c);
                break;
        default:
-               ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name);
+               ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, ast_channel_name(c));
                return -1;
        }
 
@@ -785,43 +791,49 @@ static int oss_indicate(struct ast_channel *c, int cond, const void *data, size_
 /*!
  * \brief allocate a new channel.
  */
-static struct ast_channel *oss_new(struct chan_oss_pvt *o, char *ext, char *ctx, int state)
+static struct ast_channel *oss_new(struct chan_oss_pvt *o, char *ext, char *ctx, int state, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor)
 {
        struct ast_channel *c;
 
-       c = ast_channel_alloc(1, state, o->cid_num, o->cid_name, "", ext, ctx, 0, "Console/%s", o->device + 5);
+       c = ast_channel_alloc(1, state, o->cid_num, o->cid_name, "", ext, ctx, assignedids, requestor, 0, "Console/%s", o->device + 5);
        if (c == NULL)
                return NULL;
-       c->tech = &oss_tech;
+       ast_channel_tech_set(c, &oss_tech);
        if (o->sounddev < 0)
                setformat(o, O_RDWR);
        ast_channel_set_fd(c, 0, o->sounddev); /* -1 if device closed, override later */
-       c->nativeformats = AST_FORMAT_SLINEAR;
+
+       ast_channel_set_readformat(c, ast_format_slin);
+       ast_channel_set_writeformat(c, ast_format_slin);
+       ast_channel_nativeformats_set(c, oss_tech.capabilities);
+
        /* if the console makes the call, add video to the offer */
-       if (state == AST_STATE_RINGING)
-               c->nativeformats |= console_video_formats;
+       /* if (state == AST_STATE_RINGING) TODO XXX CONSOLE VIDEO IS DISABLED UNTIL IT GETS A MAINTAINER
+               c->nativeformats |= console_video_formats; */
 
-       c->readformat = AST_FORMAT_SLINEAR;
-       c->writeformat = AST_FORMAT_SLINEAR;
-       c->tech_pvt = o;
+       ast_channel_tech_pvt_set(c, o);
 
        if (!ast_strlen_zero(o->language))
-               ast_string_field_set(c, language, o->language);
+               ast_channel_language_set(c, o->language);
        /* Don't use ast_set_callerid() here because it will
         * generate a needless NewCallerID event */
-       c->cid.cid_ani = ast_strdup(o->cid_num);
-       if (!ast_strlen_zero(ext))
-               c->cid.cid_dnid = ast_strdup(ext);
+       if (!ast_strlen_zero(o->cid_num)) {
+               ast_channel_caller(c)->ani.number.valid = 1;
+               ast_channel_caller(c)->ani.number.str = ast_strdup(o->cid_num);
+       }
+       if (!ast_strlen_zero(ext)) {
+               ast_channel_dialed(c)->number.str = ast_strdup(ext);
+       }
 
        o->owner = c;
        ast_module_ref(ast_module_info->self);
        ast_jb_configure(c, &global_jbconf);
+       ast_channel_unlock(c);
        if (state != AST_STATE_DOWN) {
                if (ast_pbx_start(c)) {
-                       ast_log(LOG_WARNING, "Unable to start PBX on %s\n", c->name);
+                       ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ast_channel_name(c));
                        ast_hangup(c);
                        o->owner = c = NULL;
-                       /* XXX what about the channel itself ? */
                }
        }
        console_video_start(get_video_desc(c), c); /* XXX cleanup */
@@ -829,7 +841,7 @@ static struct ast_channel *oss_new(struct chan_oss_pvt *o, char *ext, char *ctx,
        return c;
 }
 
-static struct ast_channel *oss_request(const char *type, int format, void *data, int *cause)
+static struct ast_channel *oss_request(const char *type, struct ast_format_cap *cap, const struct ast_assigned_ids *assignedids, const struct ast_channel *requestor, const char *data, int *cause)
 {
        struct ast_channel *c;
        struct chan_oss_pvt *o;
@@ -842,14 +854,15 @@ static struct ast_channel *oss_request(const char *type, int format, void *data,
        AST_NONSTANDARD_APP_ARGS(args, parse, '/');
        o = find_desc(args.name);
 
-       ast_log(LOG_WARNING, "oss_request ty <%s> data 0x%p <%s>\n", type, data, (char *) data);
+       ast_log(LOG_WARNING, "oss_request ty <%s> data 0x%p <%s>\n", type, data, data);
        if (o == NULL) {
                ast_log(LOG_NOTICE, "Device %s not found\n", args.name);
                /* XXX we could default to 'dsp' perhaps ? */
                return NULL;
        }
-       if ((format & AST_FORMAT_SLINEAR) == 0) {
-               ast_log(LOG_NOTICE, "Format 0x%x unsupported\n", format);
+       if (ast_format_cap_iscompatible_format(cap, ast_format_slin) == AST_FORMAT_CMP_NOT_EQUAL) {
+               struct ast_str *codec_buf = ast_str_alloca(AST_FORMAT_CAP_NAMES_LEN);
+               ast_log(LOG_NOTICE, "Format %s unsupported\n", ast_format_cap_get_names(cap, &codec_buf));
                return NULL;
        }
        if (o->owner) {
@@ -857,7 +870,7 @@ static struct ast_channel *oss_request(const char *type, int format, void *data,
                *cause = AST_CAUSE_BUSY;
                return NULL;
        }
-       c = oss_new(o, NULL, NULL, AST_STATE_DOWN);
+       c = oss_new(o, NULL, NULL, AST_STATE_DOWN, assignedids, requestor);
        if (c == NULL) {
                ast_log(LOG_WARNING, "Unable to create new OSS channel\n");
                return NULL;
@@ -877,8 +890,9 @@ static char *console_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *
        switch (cmd) {
        case CLI_INIT:
                e->command = CONSOLE_VIDEO_CMDS;
-               e->usage = "Usage: " CONSOLE_VIDEO_CMDS "...\n"
-               "       Generic handler for console commands.\n";
+               e->usage =
+                       "Usage: " CONSOLE_VIDEO_CMDS "...\n"
+                       "       Generic handler for console commands.\n";
                return NULL;
 
        case CLI_GENERATE:
@@ -911,9 +925,9 @@ static char *console_autoanswer(struct ast_cli_entry *e, int cmd, struct ast_cli
 
        switch (cmd) {
        case CLI_INIT:
-               e->command = "console autoanswer [on|off]";
+               e->command = "console {set|show} autoanswer [on|off]";
                e->usage =
-                       "Usage: console autoanswer [on|off]\n"
+                       "Usage: console {set|show} autoanswer [on|off]\n"
                        "       Enables or disables autoanswer feature.  If used without\n"
                        "       argument, displays the current on/off status of autoanswer.\n"
                        "       The default value of autoanswer is in 'oss.conf'.\n";
@@ -946,7 +960,7 @@ static char *console_autoanswer(struct ast_cli_entry *e, int cmd, struct ast_cli
 /*! \brief helper function for the answer key/cli command */
 static char *console_do_answer(int fd)
 {
-       struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
+       struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_ANSWER } };
        struct chan_oss_pvt *o = find_desc(oss_active);
        if (!o->owner) {
                if (fd > -1)
@@ -1011,8 +1025,8 @@ static char *console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_a
                int i = strlen(buf);
                buf[i] = '\n';
                f.frametype = AST_FRAME_TEXT;
-               f.subclass = 0;
-               f.data = buf;
+               f.subclass.integer = 0;
+               f.data.ptr = buf;
                f.datalen = i + 1;
                ast_queue_frame(o->owner, &f);
        }
@@ -1040,14 +1054,14 @@ static char *console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
        }
        o->hookstate = 0;
        if (o->owner)
-               ast_queue_hangup(o->owner);
+               ast_queue_hangup_with_cause(o->owner, AST_CAUSE_NORMAL_CLEARING);
        setformat(o, O_CLOSE);
        return CLI_SUCCESS;
 }
 
 static char *console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-       struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH };
+       struct ast_frame f = { AST_FRAME_CONTROL, { AST_CONTROL_FLASH } };
        struct chan_oss_pvt *o = find_desc(oss_active);
 
        if (cmd == CLI_INIT) {
@@ -1066,14 +1080,15 @@ static char *console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args
                return CLI_FAILURE;
        }
        o->hookstate = 0;
-       if (o->owner)                           /* XXX must be true, right ? */
+       if (o->owner)
                ast_queue_frame(o->owner, &f);
        return CLI_SUCCESS;
 }
 
 static char *console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-       char *s = NULL, *mye = NULL, *myc = NULL;
+       char *s = NULL;
+       char *mye = NULL, *myc = NULL;
        struct chan_oss_pvt *o = find_desc(oss_active);
 
        if (cmd == CLI_INIT) {
@@ -1089,16 +1104,17 @@ static char *console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args
                return CLI_SHOWUSAGE;
        if (o->owner) { /* already in a call */
                int i;
-               struct ast_frame f = { AST_FRAME_DTMF, 0 };
+               struct ast_frame f = { AST_FRAME_DTMF, { 0 } };
+               const char *digits;
 
                if (a->argc == e->args) {       /* argument is mandatory here */
                        ast_cli(a->fd, "Already in a call. You can only dial digits until you hangup.\n");
                        return CLI_FAILURE;
                }
-               s = a->argv[e->args];
+               digits = a->argv[e->args];
                /* send the string one char at a time */
-               for (i = 0; i < strlen(s); i++) {
-                       f.subclass = s[i];
+               for (i = 0; i < strlen(digits); i++) {
+                       f.subclass.integer = digits[i];
                        ast_queue_frame(o->owner, &f);
                }
                return CLI_SUCCESS;
@@ -1113,7 +1129,7 @@ static char *console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args
                myc = o->ctx;
        if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
                o->hookstate = 1;
-               oss_new(o, mye, myc, AST_STATE_RINGING);
+               oss_new(o, mye, myc, AST_STATE_RINGING, NULL, NULL);
        } else
                ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
        if (s)
@@ -1124,33 +1140,39 @@ static char *console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args
 static char *console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        struct chan_oss_pvt *o = find_desc(oss_active);
-       char *s;
-       
+       const char *s;
+       int toggle = 0;
+
        if (cmd == CLI_INIT) {
-               e->command = "console {mute|unmute}";
+               e->command = "console {mute|unmute} [toggle]";
                e->usage =
-                       "Usage: console {mute|unmute}\n"
+                       "Usage: console {mute|unmute} [toggle]\n"
                        "       Mute/unmute the microphone.\n";
                return NULL;
        } else if (cmd == CLI_GENERATE)
                return NULL;
 
-       if (a->argc != e->args)
+       if (a->argc > e->args)
                return CLI_SHOWUSAGE;
-       s = a->argv[e->args-1];
+       if (a->argc == e->args) {
+               if (strcasecmp(a->argv[e->args-1], "toggle"))
+                       return CLI_SHOWUSAGE;
+               toggle = 1;
+       }
+       s = a->argv[e->args-2];
        if (!strcasecmp(s, "mute"))
-               o->mute = 1;
+               o->mute = toggle ? !o->mute : 1;
        else if (!strcasecmp(s, "unmute"))
-               o->mute = 0;
+               o->mute = toggle ? !o->mute : 0;
        else
                return CLI_SHOWUSAGE;
+       ast_cli(a->fd, "Console mic is %s\n", o->mute ? "off" : "on");
        return CLI_SUCCESS;
 }
 
 static char *console_transfer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
        struct chan_oss_pvt *o = find_desc(oss_active);
-       struct ast_channel *b = NULL;
        char *tmp, *ext, *ctx;
 
        switch (cmd) {
@@ -1169,23 +1191,19 @@ static char *console_transfer(struct ast_cli_entry *e, int cmd, struct ast_cli_a
                return CLI_SHOWUSAGE;
        if (o == NULL)
                return CLI_FAILURE;
-       if (o->owner == NULL || (b = ast_bridged_channel(o->owner)) == NULL) {
+       if (o->owner == NULL || !ast_channel_is_bridged(o->owner)) {
                ast_cli(a->fd, "There is no call to transfer\n");
                return CLI_SUCCESS;
        }
 
        tmp = ast_ext_ctx(a->argv[2], &ext, &ctx);
-       if (ctx == NULL)                        /* supply default context if needed */
-               ctx = o->owner->context;
-       if (!ast_exists_extension(b, ctx, ext, 1, b->cid.cid_num))
-               ast_cli(a->fd, "No such extension exists\n");
-       else {
-               ast_cli(a->fd, "Whee, transferring %s to %s@%s.\n", b->name, ext, ctx);
-               if (ast_async_goto(b, ctx, ext, 1))
-                       ast_cli(a->fd, "Failed to transfer :(\n");
+       if (ctx == NULL) {                      /* supply default context if needed */
+               ctx = ast_strdupa(ast_channel_context(o->owner));
        }
-       if (tmp)
-               ast_free(tmp);
+       if (ast_bridge_transfer_blind(1, o->owner, ext, ctx, NULL, NULL) != AST_BRIDGE_TRANSFER_SUCCESS) {
+               ast_log(LOG_WARNING, "Unable to transfer call from channel %s\n", ast_channel_name(o->owner));
+       }
+       ast_free(tmp);
        return CLI_SUCCESS;
 }
 
@@ -1193,7 +1211,7 @@ static char *console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
 {
        switch (cmd) {
        case CLI_INIT:
-               e->command = "console active";
+               e->command = "console {set|show} active [<device>]";
                e->usage =
                        "Usage: console active [device]\n"
                        "       If used without a parameter, displays which device is the current\n"
@@ -1204,20 +1222,20 @@ static char *console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
                return NULL;
        }
 
-       if (a->argc == 2)
+       if (a->argc == 3)
                ast_cli(a->fd, "active console is [%s]\n", oss_active);
-       else if (a->argc != 3)
+       else if (a->argc != 4)
                return CLI_SHOWUSAGE;
        else {
                struct chan_oss_pvt *o;
-               if (strcmp(a->argv[2], "show") == 0) {
+               if (strcmp(a->argv[3], "show") == 0) {
                        for (o = oss_default.next; o; o = o->next)
                                ast_cli(a->fd, "device [%s] exists\n", o->name);
                        return CLI_SUCCESS;
                }
-               o = find_desc(a->argv[2]);
+               o = find_desc(a->argv[3]);
                if (o == NULL)
-                       ast_cli(a->fd, "No device [%s] exists\n", a->argv[2]);
+                       ast_cli(a->fd, "No device [%s] exists\n", a->argv[3]);
                else
                        oss_active = o->name;
        }
@@ -1230,7 +1248,7 @@ static char *console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
 static void store_boost(struct chan_oss_pvt *o, const char *s)
 {
        double boost = 0;
-       if (sscanf(s, "%lf", &boost) != 1) {
+       if (sscanf(s, "%30lf", &boost) != 1) {
                ast_log(LOG_WARNING, "invalid boost <%s>\n", s);
                return;
        }
@@ -1274,7 +1292,7 @@ static struct ast_cli_entry cli_oss[] = {
        AST_CLI_DEFINE(console_flash, "Flash a call on the console"),
        AST_CLI_DEFINE(console_dial, "Dial an extension on the console"),
        AST_CLI_DEFINE(console_mute, "Disable/Enable mic input"),
-       AST_CLI_DEFINE(console_transfer, "Transfer a call to a different extension"),   
+       AST_CLI_DEFINE(console_transfer, "Transfer a call to a different extension"),
        AST_CLI_DEFINE(console_cmd, "Generic console command"),
        AST_CLI_DEFINE(console_sendtext, "Send text to the remote device"),
        AST_CLI_DEFINE(console_autoanswer, "Sets/displays autoanswer"),
@@ -1292,7 +1310,7 @@ static void store_mixer(struct chan_oss_pvt *o, const char *s)
        int i;
 
        for (i = 0; i < strlen(s); i++) {
-               if (!isalnum(s[i]) && index(" \t-/", s[i]) == NULL) {
+               if (!isalnum(s[i]) && strchr(" \t-/", s[i]) == NULL) {
                        ast_log(LOG_WARNING, "Suspect char %c in mixer cmd, ignoring:\n\t%s\n", s[i], s);
                        return;
                }
@@ -1375,11 +1393,19 @@ static struct chan_oss_pvt *store_config(struct ast_config *cfg, char *ctg)
        if (o->mixer_cmd) {
                char *cmd;
 
-               asprintf(&cmd, "mixer %s", o->mixer_cmd);
-               ast_log(LOG_WARNING, "running [%s]\n", cmd);
-               system(cmd);
-               ast_free(cmd);
+               if (ast_asprintf(&cmd, "mixer %s", o->mixer_cmd) >= 0) {
+                       ast_log(LOG_WARNING, "running [%s]\n", cmd);
+                       if (system(cmd) < 0) {
+                               ast_log(LOG_WARNING, "system() failed: %s\n", strerror(errno));
+                       }
+                       ast_free(cmd);
+               }
        }
+
+       /* if the config file requested to start the GUI, do it */
+       if (get_gui_startup(o->env))
+               console_video_start(o->env, NULL);
+
        if (o == &oss_default)          /* we are done with the default */
                return NULL;
 
@@ -1409,6 +1435,41 @@ error:
 #endif
 }
 
+static int unload_module(void)
+{
+       struct chan_oss_pvt *o, *next;
+
+       ast_channel_unregister(&oss_tech);
+       ast_cli_unregister_multiple(cli_oss, ARRAY_LEN(cli_oss));
+
+       o = oss_default.next;
+       while (o) {
+               close(o->sounddev);
+               if (o->owner)
+                       ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
+               if (o->owner)
+                       return -1;
+               next = o->next;
+               ast_free(o->name);
+               ast_free(o);
+               o = next;
+       }
+       ao2_cleanup(oss_tech.capabilities);
+       oss_tech.capabilities = NULL;
+
+       return 0;
+}
+
+/*!
+ * \brief Load the module
+ *
+ * Module loading including tests for configuration or dependencies.
+ * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
+ * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
+ * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
+ * configuration file or other non-critical problem return
+ * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
+ */
 static int load_module(void)
 {
        struct ast_config *cfg = NULL;
@@ -1422,6 +1483,9 @@ static int load_module(void)
        if (!(cfg = ast_config_load(config, config_flags))) {
                ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
                return AST_MODULE_LOAD_DECLINE;
+       } else if (cfg == CONFIG_STATUS_FILEINVALID) {
+               ast_log(LOG_ERROR, "Config file %s is in an invalid format.  Aborting.\n", config);
+               return AST_MODULE_LOAD_DECLINE;
        }
 
        do {
@@ -1433,39 +1497,26 @@ static int load_module(void)
        if (find_desc(oss_active) == NULL) {
                ast_log(LOG_NOTICE, "Device %s not found\n", oss_active);
                /* XXX we could default to 'dsp' perhaps ? */
-               /* XXX should cleanup allocated memory etc. */
-               return AST_MODULE_LOAD_FAILURE;
+               unload_module();
+               return AST_MODULE_LOAD_DECLINE;
+       }
+
+       if (!(oss_tech.capabilities = ast_format_cap_alloc(0))) {
+               return AST_MODULE_LOAD_DECLINE;
        }
+       ast_format_cap_append(oss_tech.capabilities, ast_format_slin, 0);
 
-       oss_tech.capabilities |= console_video_formats;
+       /* TODO XXX CONSOLE VIDEO IS DISABLE UNTIL IT HAS A MAINTAINER
+        * add console_video_formats to oss_tech.capabilities once this occurs. */
 
        if (ast_channel_register(&oss_tech)) {
                ast_log(LOG_ERROR, "Unable to register channel type 'OSS'\n");
-               return AST_MODULE_LOAD_FAILURE;
+               return AST_MODULE_LOAD_DECLINE;
        }
 
-       ast_cli_register_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry));
+       ast_cli_register_multiple(cli_oss, ARRAY_LEN(cli_oss));
 
        return AST_MODULE_LOAD_SUCCESS;
 }
 
-
-static int unload_module(void)
-{
-       struct chan_oss_pvt *o;
-
-       ast_channel_unregister(&oss_tech);
-       ast_cli_unregister_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry));
-
-       for (o = oss_default.next; o; o = o->next) {
-               close(o->sounddev);
-               if (o->owner)
-                       ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
-               if (o->owner)                   /* XXX how ??? */
-                       return -1;
-               /* XXX what about the memory allocated ? */
-       }
-       return 0;
-}
-
-AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "OSS Console Channel Driver");
+AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "OSS Console Channel Driver");