2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * FreeBSD changes and multiple device support by Luigi Rizzo, 2005.05.25
9 * note-this code best seen with ts=8 (8-spaces tabs) in the editor
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
24 * \brief Channel driver for OSS sound cards
26 * \author Mark Spencer <markster@digium.com>
30 * \arg \ref Config_oss
32 * \ingroup channel_drivers
36 <depend>ossaudio</depend>
41 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
48 #include <sys/ioctl.h>
55 #include <linux/soundcard.h>
56 #elif defined(__FreeBSD__)
57 #include <sys/soundcard.h>
59 #include <soundcard.h>
62 #include "asterisk/lock.h"
63 #include "asterisk/frame.h"
64 #include "asterisk/logger.h"
65 #include "asterisk/callerid.h"
66 #include "asterisk/channel.h"
67 #include "asterisk/module.h"
68 #include "asterisk/options.h"
69 #include "asterisk/pbx.h"
70 #include "asterisk/config.h"
71 #include "asterisk/cli.h"
72 #include "asterisk/utils.h"
73 #include "asterisk/causes.h"
74 #include "asterisk/endian.h"
75 #include "asterisk/stringfields.h"
76 #include "asterisk/abstract_jb.h"
77 #include "asterisk/musiconhold.h"
79 /* ringtones we use */
85 /*! Global jitterbuffer configuration - by default, jb is disabled */
86 static struct ast_jb_conf default_jbconf =
90 .resync_threshold = -1,
93 static struct ast_jb_conf global_jbconf;
96 * Basic mode of operation:
98 * we have one keyboard (which receives commands from the keyboard)
99 * and multiple headset's connected to audio cards.
100 * Cards/Headsets are named as the sections of oss.conf.
101 * The section called [general] contains the default parameters.
103 * At any time, the keyboard is attached to one card, and you
104 * can switch among them using the command 'console foo'
105 * where 'foo' is the name of the card you want.
107 * oss.conf parameters are
111 ; General config options, with default values shown.
112 ; You should use one section per device, with [general] being used
113 ; for the first device and also as a template for other devices.
115 ; All but 'debug' can go also in the device-specific sections.
117 ; debug = 0x0 ; misc debug flags, default is 0
119 ; Set the device to use for I/O
122 ; Optional mixer command to run upon startup (e.g. to set
123 ; volume levels, mutes, etc.
126 ; Software mic volume booster (or attenuator), useful for sound
127 ; cards or microphones with poor sensitivity. The volume level
128 ; is in dB, ranging from -20.0 to +20.0
129 ; boost = n ; mic volume boost in dB
131 ; Set the callerid for outgoing calls
132 ; callerid = John Doe <555-1234>
134 ; autoanswer = no ; no autoanswer on call
135 ; autohangup = yes ; hangup when other party closes
136 ; extension = s ; default extension to call
137 ; context = default ; default context for outgoing calls
138 ; language = "" ; default language
140 ; Default Music on Hold class to use when this channel is placed on hold in
141 ; the case that the music class is not set on the channel with
142 ; Set(CHANNEL(musicclass)=whatever) in the dialplan and the peer channel
143 ; putting this one on hold did not suggest a class to use.
145 ; mohinterpret=default
147 ; If you set overridecontext to 'yes', then the whole dial string
148 ; will be interpreted as an extension, which is extremely useful
149 ; to dial SIP, IAX and other extensions which use the '@' character.
150 ; The default is 'no' just for backward compatibility, but the
151 ; suggestion is to change it.
152 ; overridecontext = no ; if 'no', the last @ will start the context
153 ; if 'yes' the whole string is an extension.
155 ; low level device parameters in case you have problems with the
156 ; device driver on your operating system. You should not touch these
157 ; unless you know what you are doing.
158 ; queuesize = 10 ; frames in device driver
159 ; frags = 8 ; argument to SETFRAGMENT
161 ;------------------------------ JITTER BUFFER CONFIGURATION --------------------------
162 ; jbenable = yes ; Enables the use of a jitterbuffer on the receiving side of an
163 ; OSS channel. Defaults to "no". An enabled jitterbuffer will
164 ; be used only if the sending side can create and the receiving
165 ; side can not accept jitter. The OSS channel can't accept jitter,
166 ; thus an enabled jitterbuffer on the receive OSS side will always
167 ; be used if the sending side can create jitter.
169 ; jbmaxsize = 200 ; Max length of the jitterbuffer in milliseconds.
171 ; jbresyncthreshold = 1000 ; Jump in the frame timestamps over which the jitterbuffer is
172 ; resynchronized. Useful to improve the quality of the voice, with
173 ; big jumps in/broken timestamps, usualy sent from exotic devices
174 ; and programs. Defaults to 1000.
176 ; jbimpl = fixed ; Jitterbuffer implementation, used on the receiving side of an OSS
177 ; channel. Two implementations are currenlty available - "fixed"
178 ; (with size always equals to jbmax-size) and "adaptive" (with
179 ; variable size, actually the new jb of IAX2). Defaults to fixed.
181 ; jblog = no ; Enables jitterbuffer frame logging. Defaults to "no".
182 ;-----------------------------------------------------------------------------------
185 ; device = /dev/dsp1 ; alternate device
189 .. and so on for the other cards.
194 * Helper macros to parse config arguments. They will go in a common
195 * header file if their usage is globally accepted. In the meantime,
196 * we define them here. Typical usage is as below.
197 * Remember to open a block right before M_START (as it declares
198 * some variables) and use the M_* macros WITHOUT A SEMICOLON:
201 * M_START(v->name, v->value)
203 * M_BOOL("dothis", x->flag1)
204 * M_STR("name", x->somestring)
205 * M_F("bar", some_c_code)
206 * M_END(some_final_statement)
207 * ... other code in the block
210 * XXX NOTE these macros should NOT be replicated in other parts of asterisk.
211 * Likely we will come up with a better way of doing config file parsing.
213 #define M_START(var, val) \
214 char *__s = var; char *__val = val;
216 #define M_F(tag, f) if (!strcasecmp((__s), tag)) { f; } else
217 #define M_BOOL(tag, dst) M_F(tag, (dst) = ast_true(__val) )
218 #define M_UINT(tag, dst) M_F(tag, (dst) = strtoul(__val, NULL, 0) )
219 #define M_STR(tag, dst) M_F(tag, ast_copy_string(dst, __val, sizeof(dst)))
222 * The following parameters are used in the driver:
224 * FRAME_SIZE the size of an audio frame, in samples.
225 * 160 is used almost universally, so you should not change it.
227 * FRAGS the argument for the SETFRAGMENT ioctl.
228 * Overridden by the 'frags' parameter in oss.conf
230 * Bits 0-7 are the base-2 log of the device's block size,
231 * bits 16-31 are the number of blocks in the driver's queue.
232 * There are a lot of differences in the way this parameter
233 * is supported by different drivers, so you may need to
234 * experiment a bit with the value.
235 * A good default for linux is 30 blocks of 64 bytes, which
236 * results in 6 frames of 320 bytes (160 samples).
237 * FreeBSD works decently with blocks of 256 or 512 bytes,
238 * leaving the number unspecified.
239 * Note that this only refers to the device buffer size,
240 * this module will then try to keep the lenght of audio
241 * buffered within small constraints.
243 * QUEUE_SIZE The max number of blocks actually allowed in the device
244 * driver's buffer, irrespective of the available number.
245 * Overridden by the 'queuesize' parameter in oss.conf
247 * Should be >=2, and at most as large as the hw queue above
248 * (otherwise it will never be full).
251 #define FRAME_SIZE 160
252 #define QUEUE_SIZE 10
254 #if defined(__FreeBSD__)
257 #define FRAGS ( ( (6 * 5) << 16 ) | 0x6 )
261 * XXX text message sizes are probably 256 chars, but i am
262 * not sure if there is a suitable definition anywhere.
264 #define TEXT_SIZE 256
267 #define TRYOPEN 1 /* try to open on startup */
269 #define O_CLOSE 0x444 /* special 'close' mode for device */
270 /* Which device to use */
271 #if defined( __OpenBSD__ ) || defined( __NetBSD__ )
272 #define DEV_DSP "/dev/audio"
274 #define DEV_DSP "/dev/dsp"
278 #define MIN(a,b) ((a) < (b) ? (a) : (b))
281 #define MAX(a,b) ((a) > (b) ? (a) : (b))
286 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
288 static char *config = "oss.conf"; /* default config file */
290 static int oss_debug;
293 * Each sound is made of 'datalen' samples of sound, repeated as needed to
294 * generate 'samplen' samples of data, then followed by 'silencelen' samples
295 * of silence. The loop is repeated if 'repeat' is set.
307 static struct sound sounds[] = {
308 { AST_CONTROL_RINGING, "RINGING", ringtone, sizeof(ringtone)/2, 16000, 32000, 1 },
309 { AST_CONTROL_BUSY, "BUSY", busy, sizeof(busy)/2, 4000, 4000, 1 },
310 { AST_CONTROL_CONGESTION, "CONGESTION", busy, sizeof(busy)/2, 2000, 2000, 1 },
311 { AST_CONTROL_RING, "RING10", ring10, sizeof(ring10)/2, 16000, 32000, 1 },
312 { AST_CONTROL_ANSWER, "ANSWER", answer, sizeof(answer)/2, 2200, 0, 0 },
313 { -1, NULL, 0, 0, 0, 0 }, /* end marker */
318 * descriptor for one of our channels.
319 * There is one used for 'default' values (from the [general] entry in
320 * the configuration file), and then one instance for each device
321 * (the default is cloned from [general], others are only created
322 * if the relevant section exists).
324 struct chan_oss_pvt {
325 struct chan_oss_pvt *next;
329 * cursound indicates which in struct sound we play. -1 means nothing,
330 * any other value is a valid sound, in which case sampsent indicates
331 * the next sample to send in [0..samplen + silencelen]
332 * nosound is set to disable the audio data from the channel
333 * (so we can play the tones etc.).
335 int sndcmd[2]; /* Sound command pipe */
336 int cursound; /* index of sound to send */
337 int sampsent; /* # of sound samples sent */
338 int nosound; /* set to block audio from the PBX */
340 int total_blocks; /* total blocks in the output device */
342 enum { M_UNSET, M_FULL, M_READ, M_WRITE } duplex;
346 char *mixer_cmd; /* initial command to issue to the mixer */
347 unsigned int queuesize; /* max fragments in queue */
348 unsigned int frags; /* parameter for SETFRAGMENT */
350 int warned; /* various flags used for warnings */
351 #define WARN_used_blocks 1
354 int w_errors; /* overfull in the write path */
355 struct timeval lastopen;
360 /* boost support. BOOST_SCALE * 10 ^(BOOST_MAX/20) must
361 * be representable in 16 bits to avoid overflows.
363 #define BOOST_SCALE (1<<9)
364 #define BOOST_MAX 40 /* slightly less than 7 bits */
365 int boost; /* input boost, scaled by BOOST_SCALE */
366 char device[64]; /* device to open */
370 struct ast_channel *owner;
371 char ext[AST_MAX_EXTENSION];
372 char ctx[AST_MAX_CONTEXT];
373 char language[MAX_LANGUAGE];
374 char cid_name[256]; /*XXX */
375 char cid_num[256]; /*XXX */
376 char mohinterpret[MAX_MUSICCLASS];
378 /* buffers used in oss_write */
379 char oss_write_buf[FRAME_SIZE * 2];
381 /* buffers used in oss_read - AST_FRIENDLY_OFFSET space for headers
382 * plus enough room for a full frame
384 char oss_read_buf[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
385 int readpos; /* read position above */
386 struct ast_frame read_f; /* returned by oss_read */
389 static struct chan_oss_pvt oss_default = {
392 .duplex = M_UNSET, /* XXX check this */
395 .queuesize = QUEUE_SIZE,
399 .readpos = AST_FRIENDLY_OFFSET, /* start here on reads */
400 .lastopen = { 0, 0 },
401 .boost = BOOST_SCALE,
404 static char *oss_active; /* the active device */
406 static int setformat(struct chan_oss_pvt *o, int mode);
408 static struct ast_channel *oss_request(const char *type, int format, void *data
410 static int oss_digit_begin(struct ast_channel *c, char digit);
411 static int oss_digit_end(struct ast_channel *c, char digit);
412 static int oss_text(struct ast_channel *c, const char *text);
413 static int oss_hangup(struct ast_channel *c);
414 static int oss_answer(struct ast_channel *c);
415 static struct ast_frame *oss_read(struct ast_channel *chan);
416 static int oss_call(struct ast_channel *c, char *dest, int timeout);
417 static int oss_write(struct ast_channel *chan, struct ast_frame *f);
418 static int oss_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
419 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
420 static char tdesc[] = "OSS Console Channel Driver";
422 static const struct ast_channel_tech oss_tech = {
424 .description = tdesc,
425 .capabilities = AST_FORMAT_SLINEAR,
426 .requester = oss_request,
427 .send_digit_begin = oss_digit_begin,
428 .send_digit_end = oss_digit_end,
429 .send_text = oss_text,
430 .hangup = oss_hangup,
431 .answer = oss_answer,
435 .indicate = oss_indicate,
440 * returns a pointer to the descriptor with the given name
442 static struct chan_oss_pvt *find_desc(char *dev)
444 struct chan_oss_pvt *o = NULL;
447 ast_log(LOG_WARNING, "null dev\n");
449 for (o = oss_default.next; o && o->name && dev && strcmp(o->name, dev) != 0; o = o->next);
452 ast_log(LOG_WARNING, "could not find <%s>\n", dev ? dev : "--no-device--");
458 * split a string in extension-context, returns pointers to malloc'ed
460 * If we do not have 'overridecontext' then the last @ is considered as
461 * a context separator, and the context is overridden.
462 * This is usually not very necessary as you can play with the dialplan,
463 * and it is nice not to need it because you have '@' in SIP addresses.
464 * Return value is the buffer address.
466 static char *ast_ext_ctx(const char *src, char **ext, char **ctx)
468 struct chan_oss_pvt *o = find_desc(oss_active);
470 if (ext == NULL || ctx == NULL)
471 return NULL; /* error */
475 if (src && *src != '\0')
476 *ext = ast_strdup(src);
481 if (!o->overridecontext) {
482 /* parse from the right */
483 *ctx = strrchr(*ext, '@');
492 * Returns the number of blocks used in the audio output channel
494 static int used_blocks(struct chan_oss_pvt *o)
496 struct audio_buf_info info;
498 if (ioctl(o->sounddev, SNDCTL_DSP_GETOSPACE, &info)) {
499 if (!(o->warned & WARN_used_blocks)) {
500 ast_log(LOG_WARNING, "Error reading output space\n");
501 o->warned |= WARN_used_blocks;
506 if (o->total_blocks == 0) {
507 if (0) /* debugging */
508 ast_log(LOG_WARNING, "fragtotal %d size %d avail %d\n", info.fragstotal, info.fragsize, info.fragments);
509 o->total_blocks = info.fragments;
512 return o->total_blocks - info.fragments;
515 /* Write an exactly FRAME_SIZE sized frame */
516 static int soundcard_writeframe(struct chan_oss_pvt *o, short *data)
521 setformat(o, O_RDWR);
523 return 0; /* not fatal */
525 * Nothing complex to manage the audio device queue.
526 * If the buffer is full just drop the extra, otherwise write.
527 * XXX in some cases it might be useful to write anyways after
528 * a number of failures, to restart the output chain.
530 res = used_blocks(o);
531 if (res > o->queuesize) { /* no room to write a block */
532 if (o->w_errors++ == 0 && (oss_debug & 0x4))
533 ast_log(LOG_WARNING, "write: used %d blocks (%d)\n", res, o->w_errors);
537 return write(o->sounddev, ((void *) data), FRAME_SIZE * 2);
541 * Handler for 'sound writable' events from the sound thread.
542 * Builds a frame from the high level description of the sounds,
543 * and passes it to the audio device.
544 * The actual sound is made of 1 or more sequences of sound samples
545 * (s->datalen, repeated to make s->samplen samples) followed by
546 * s->silencelen samples of silence. The position in the sequence is stored
547 * in o->sampsent, which goes between 0 .. s->samplen+s->silencelen.
548 * In case we fail to write a frame, don't update o->sampsent.
550 static void send_sound(struct chan_oss_pvt *o)
552 short myframe[FRAME_SIZE];
554 int l_sampsent = o->sampsent;
557 if (o->cursound < 0) /* no sound to send */
560 s = &sounds[o->cursound];
562 for (ofs = 0; ofs < FRAME_SIZE; ofs += l) {
563 l = s->samplen - l_sampsent; /* # of available samples */
565 start = l_sampsent % s->datalen; /* source offset */
566 if (l > FRAME_SIZE - ofs) /* don't overflow the frame */
567 l = FRAME_SIZE - ofs;
568 if (l > s->datalen - start) /* don't overflow the source */
569 l = s->datalen - start;
570 bcopy(s->data + start, myframe + ofs, l * 2);
572 ast_log(LOG_WARNING, "send_sound sound %d/%d of %d into %d\n", l_sampsent, l, s->samplen, ofs);
574 } else { /* end of samples, maybe some silence */
575 static const short silence[FRAME_SIZE] = { 0, };
579 if (l > FRAME_SIZE - ofs)
580 l = FRAME_SIZE - ofs;
581 bcopy(silence, myframe + ofs, l * 2);
583 } else { /* silence is over, restart sound if loop */
584 if (s->repeat == 0) { /* last block */
586 o->nosound = 0; /* allow audio data */
587 if (ofs < FRAME_SIZE) /* pad with silence */
588 bcopy(silence, myframe + ofs, (FRAME_SIZE - ofs) * 2);
594 l = soundcard_writeframe(o, myframe);
596 o->sampsent = l_sampsent; /* update status */
599 static void *sound_thread(void *arg)
602 struct chan_oss_pvt *o = (struct chan_oss_pvt *) arg;
605 * Just in case, kick the driver by trying to read from it.
606 * Ignore errors - this read is almost guaranteed to fail.
608 read(o->sounddev, ign, sizeof(ign));
615 FD_SET(o->sndcmd[0], &rfds);
616 maxfd = o->sndcmd[0]; /* pipe from the main process */
617 if (o->cursound > -1 && o->sounddev < 0)
618 setformat(o, O_RDWR); /* need the channel, try to reopen */
619 else if (o->cursound == -1 && o->owner == NULL)
620 setformat(o, O_CLOSE); /* can close */
621 if (o->sounddev > -1) {
622 if (!o->owner) { /* no one owns the audio, so we must drain it */
623 FD_SET(o->sounddev, &rfds);
624 maxfd = MAX(o->sounddev, maxfd);
626 if (o->cursound > -1) {
627 FD_SET(o->sounddev, &wfds);
628 maxfd = MAX(o->sounddev, maxfd);
631 /* ast_select emulates linux behaviour in terms of timeout handling */
632 res = ast_select(maxfd + 1, &rfds, &wfds, NULL, NULL);
634 ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
638 if (FD_ISSET(o->sndcmd[0], &rfds)) {
639 /* read which sound to play from the pipe */
642 read(o->sndcmd[0], &what, sizeof(what));
643 for (i = 0; sounds[i].ind != -1; i++) {
644 if (sounds[i].ind == what) {
647 o->nosound = 1; /* block audio from pbx */
651 if (sounds[i].ind == -1)
652 ast_log(LOG_WARNING, "invalid sound index: %d\n", what);
654 if (o->sounddev > -1) {
655 if (FD_ISSET(o->sounddev, &rfds)) /* read and ignore errors */
656 read(o->sounddev, ign, sizeof(ign));
657 if (FD_ISSET(o->sounddev, &wfds))
661 return NULL; /* Never reached */
665 * reset and close the device if opened,
666 * then open and initialize it in the desired mode,
667 * trigger reads and writes so we can start using it.
669 static int setformat(struct chan_oss_pvt *o, int mode)
671 int fmt, desired, res, fd;
673 if (o->sounddev >= 0) {
674 ioctl(o->sounddev, SNDCTL_DSP_RESET, 0);
679 if (mode == O_CLOSE) /* we are done */
681 if (ast_tvdiff_ms(ast_tvnow(), o->lastopen) < 1000)
682 return -1; /* don't open too often */
683 o->lastopen = ast_tvnow();
684 fd = o->sounddev = open(o->device, mode | O_NONBLOCK);
686 ast_log(LOG_WARNING, "Unable to re-open DSP device %s: %s\n", o->device, strerror(errno));
690 o->owner->fds[0] = fd;
692 #if __BYTE_ORDER == __LITTLE_ENDIAN
697 res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
699 ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
704 res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
705 /* Check to see if duplex set (FreeBSD Bug) */
706 res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt);
707 if (res == 0 && (fmt & DSP_CAP_DUPLEX)) {
708 if (option_verbose > 1)
709 ast_verbose(VERBOSE_PREFIX_2 "Console is full duplex\n");
722 res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
724 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
727 fmt = desired = DEFAULT_SAMPLE_RATE; /* 8000 Hz desired */
728 res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
731 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
734 if (fmt != desired) {
735 if (!(o->warned & WARN_speed)) {
737 "Requested %d Hz, got %d Hz -- sound may be choppy\n",
739 o->warned |= WARN_speed;
743 * on Freebsd, SETFRAGMENT does not work very well on some cards.
744 * Default to use 256 bytes, let the user override
748 res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
750 if (!(o->warned & WARN_frag)) {
752 "Unable to set fragment size -- sound may be choppy\n");
753 o->warned |= WARN_frag;
757 /* on some cards, we need SNDCTL_DSP_SETTRIGGER to start outputting */
758 res = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
759 res = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &res);
760 /* it may fail if we are in half duplex, never mind */
765 * some of the standard methods supported by channels.
767 static int oss_digit_begin(struct ast_channel *c, char digit)
772 static int oss_digit_end(struct ast_channel *c, char digit)
774 /* no better use for received digits than print them */
775 ast_verbose(" << Console Received digit %c >> \n", digit);
779 static int oss_text(struct ast_channel *c, const char *text)
781 /* print received messages */
782 ast_verbose(" << Console Received text %s >> \n", text);
786 /* Play ringtone 'x' on device 'o' */
787 static void ring(struct chan_oss_pvt *o, int x)
789 write(o->sndcmd[1], &x, sizeof(x));
794 * handler for incoming calls. Either autoanswer, or start ringing
796 static int oss_call(struct ast_channel *c, char *dest, int timeout)
798 struct chan_oss_pvt *o = c->tech_pvt;
799 struct ast_frame f = { 0, };
801 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);
803 ast_verbose(" << Auto-answered >> \n");
804 f.frametype = AST_FRAME_CONTROL;
805 f.subclass = AST_CONTROL_ANSWER;
806 ast_queue_frame(c, &f);
808 ast_verbose("<< Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
809 f.frametype = AST_FRAME_CONTROL;
810 f.subclass = AST_CONTROL_RINGING;
811 ast_queue_frame(c, &f);
812 ring(o, AST_CONTROL_RING);
818 * remote side answered the phone
820 static int oss_answer(struct ast_channel *c)
822 struct chan_oss_pvt *o = c->tech_pvt;
824 ast_verbose(" << Console call has been answered >> \n");
826 /* play an answer tone (XXX do we really need it ?) */
827 ring(o, AST_CONTROL_ANSWER);
829 ast_setstate(c, AST_STATE_UP);
835 static int oss_hangup(struct ast_channel *c)
837 struct chan_oss_pvt *o = c->tech_pvt;
843 ast_verbose(" << Hangup on console >> \n");
844 ast_mutex_lock(&usecnt_lock); /* XXX not sure why */
846 ast_mutex_unlock(&usecnt_lock);
848 if (o->autoanswer || o->autohangup) {
849 /* Assume auto-hangup too */
851 setformat(o, O_CLOSE);
853 /* Make congestion noise */
854 ring(o, AST_CONTROL_CONGESTION);
860 /* used for data coming from the network */
861 static int oss_write(struct ast_channel *c, struct ast_frame *f)
864 struct chan_oss_pvt *o = c->tech_pvt;
866 /* Immediately return if no sound is enabled */
869 /* Stop any currently playing sound */
872 * we could receive a block which is not a multiple of our
873 * FRAME_SIZE, so buffer it locally and write to the device
874 * in FRAME_SIZE chunks.
875 * Keep the residue stored for future use.
877 src = 0; /* read position into f->data */
878 while (src < f->datalen) {
879 /* Compute spare room in the buffer */
880 int l = sizeof(o->oss_write_buf) - o->oss_write_dst;
882 if (f->datalen - src >= l) { /* enough to fill a frame */
883 memcpy(o->oss_write_buf + o->oss_write_dst, f->data + src, l);
884 soundcard_writeframe(o, (short *) o->oss_write_buf);
886 o->oss_write_dst = 0;
887 } else { /* copy residue */
888 l = f->datalen - src;
889 memcpy(o->oss_write_buf + o->oss_write_dst, f->data + src, l);
890 src += l; /* but really, we are done */
891 o->oss_write_dst += l;
897 static struct ast_frame *oss_read(struct ast_channel *c)
900 struct chan_oss_pvt *o = c->tech_pvt;
901 struct ast_frame *f = &o->read_f;
903 /* XXX can be simplified returning &ast_null_frame */
904 /* prepare a NULL frame in case we don't have enough data to return */
905 bzero(f, sizeof(struct ast_frame));
906 f->frametype = AST_FRAME_NULL;
907 f->src = oss_tech.type;
909 res = read(o->sounddev, o->oss_read_buf + o->readpos, sizeof(o->oss_read_buf) - o->readpos);
910 if (res < 0) /* audio data not ready, return a NULL frame */
914 if (o->readpos < sizeof(o->oss_read_buf)) /* not enough samples */
920 o->readpos = AST_FRIENDLY_OFFSET; /* reset read pointer for next frame */
921 if (c->_state != AST_STATE_UP) /* drop data if frame is not up */
923 /* ok we can build and deliver the frame to the caller */
924 f->frametype = AST_FRAME_VOICE;
925 f->subclass = AST_FORMAT_SLINEAR;
926 f->samples = FRAME_SIZE;
927 f->datalen = FRAME_SIZE * 2;
928 f->data = o->oss_read_buf + AST_FRIENDLY_OFFSET;
929 if (o->boost != BOOST_SCALE) { /* scale and clip values */
931 int16_t *p = (int16_t *) f->data;
932 for (i = 0; i < f->samples; i++) {
933 x = (p[i] * o->boost) / BOOST_SCALE;
942 f->offset = AST_FRIENDLY_OFFSET;
946 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
948 struct chan_oss_pvt *o = newchan->tech_pvt;
953 static int oss_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen)
955 struct chan_oss_pvt *o = c->tech_pvt;
959 case AST_CONTROL_BUSY:
960 case AST_CONTROL_CONGESTION:
961 case AST_CONTROL_RINGING:
967 o->nosound = 0; /* when cursound is -1 nosound must be 0 */
970 case AST_CONTROL_VIDUPDATE:
973 case AST_CONTROL_HOLD:
974 ast_verbose(" << Console Has Been Placed on Hold >> \n");
975 ast_moh_start(c, data, o->mohinterpret);
977 case AST_CONTROL_UNHOLD:
978 ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
983 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name);
994 * allocate a new channel.
996 static struct ast_channel *oss_new(struct chan_oss_pvt *o, char *ext, char *ctx, int state)
998 struct ast_channel *c;
1000 c = ast_channel_alloc(1);
1003 c->tech = &oss_tech;
1004 ast_string_field_build(c, name, "OSS/%s", o->device + 5);
1005 if (o->sounddev < 0)
1006 setformat(o, O_RDWR);
1007 c->fds[0] = o->sounddev; /* -1 if device closed, override later */
1008 c->nativeformats = AST_FORMAT_SLINEAR;
1009 c->readformat = AST_FORMAT_SLINEAR;
1010 c->writeformat = AST_FORMAT_SLINEAR;
1013 if (!ast_strlen_zero(ctx))
1014 ast_copy_string(c->context, ctx, sizeof(c->context));
1015 if (!ast_strlen_zero(ext))
1016 ast_copy_string(c->exten, ext, sizeof(c->exten));
1017 if (!ast_strlen_zero(o->language))
1018 ast_string_field_set(c, language, o->language);
1019 ast_set_callerid(c, o->cid_num, o->cid_name, o->cid_num);
1020 if (!ast_strlen_zero(ext))
1021 c->cid.cid_dnid = ast_strdup(ext);
1024 ast_setstate(c, state);
1025 ast_mutex_lock(&usecnt_lock);
1027 ast_mutex_unlock(&usecnt_lock);
1028 ast_update_use_count();
1029 ast_jb_configure(c, &global_jbconf);
1030 if (state != AST_STATE_DOWN) {
1031 if (ast_pbx_start(c)) {
1032 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", c->name);
1034 o->owner = c = NULL;
1035 /* XXX what about the channel itself ? */
1036 /* XXX what about usecnt ? */
1043 static struct ast_channel *oss_request(const char *type, int format, void *data, int *cause)
1045 struct ast_channel *c;
1046 struct chan_oss_pvt *o = find_desc(data);
1048 ast_log(LOG_WARNING, "oss_request ty <%s> data 0x%p <%s>\n", type, data, (char *) data);
1050 ast_log(LOG_NOTICE, "Device %s not found\n", (char *) data);
1051 /* XXX we could default to 'dsp' perhaps ? */
1054 if ((format & AST_FORMAT_SLINEAR) == 0) {
1055 ast_log(LOG_NOTICE, "Format 0x%x unsupported\n", format);
1059 ast_log(LOG_NOTICE, "Already have a call (chan %p) on the OSS channel\n", o->owner);
1060 *cause = AST_CAUSE_BUSY;
1063 c = oss_new(o, NULL, NULL, AST_STATE_DOWN);
1065 ast_log(LOG_WARNING, "Unable to create new OSS channel\n");
1071 static int console_autoanswer_deprecated(int fd, int argc, char *argv[])
1073 struct chan_oss_pvt *o = find_desc(oss_active);
1076 ast_cli(fd, "Auto answer is %s.\n", o->autoanswer ? "on" : "off");
1077 return RESULT_SUCCESS;
1080 return RESULT_SHOWUSAGE;
1082 ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n", oss_active);
1083 return RESULT_FAILURE;
1085 if (!strcasecmp(argv[1], "on"))
1087 else if (!strcasecmp(argv[1], "off"))
1090 return RESULT_SHOWUSAGE;
1091 return RESULT_SUCCESS;
1094 static int console_autoanswer(int fd, int argc, char *argv[])
1096 struct chan_oss_pvt *o = find_desc(oss_active);
1099 ast_cli(fd, "Auto answer is %s.\n", o->autoanswer ? "on" : "off");
1100 return RESULT_SUCCESS;
1103 return RESULT_SHOWUSAGE;
1105 ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n",
1107 return RESULT_FAILURE;
1109 if (!strcasecmp(argv[2], "on"))
1111 else if (!strcasecmp(argv[2], "off"))
1114 return RESULT_SHOWUSAGE;
1115 return RESULT_SUCCESS;
1118 static char *autoanswer_complete_deprecated(const char *line, const char *word, int pos, int state)
1120 static char *choices[] = { "on", "off", NULL };
1122 return (pos != 2) ? NULL : ast_cli_complete(word, choices, state);
1125 static char *autoanswer_complete(const char *line, const char *word, int pos, int state)
1127 static char *choices[] = { "on", "off", NULL };
1129 return (pos != 3) ? NULL : ast_cli_complete(word, choices, state);
1132 static char autoanswer_usage[] =
1133 "Usage: console autoanswer [on|off]\n"
1134 " Enables or disables autoanswer feature. If used without\n"
1135 " argument, displays the current on/off status of autoanswer.\n"
1136 " The default value of autoanswer is in 'oss.conf'.\n";
1139 * answer command from the console
1141 static int console_answer_deprecated(int fd, int argc, char *argv[])
1143 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
1144 struct chan_oss_pvt *o = find_desc(oss_active);
1147 return RESULT_SHOWUSAGE;
1149 ast_cli(fd, "No one is calling us\n");
1150 return RESULT_FAILURE;
1155 ast_queue_frame(o->owner, &f);
1157 /* XXX do we really need it ? considering we shut down immediately... */
1158 ring(o, AST_CONTROL_ANSWER);
1160 return RESULT_SUCCESS;
1163 static int console_answer(int fd, int argc, char *argv[])
1165 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
1166 struct chan_oss_pvt *o = find_desc(oss_active);
1169 return RESULT_SHOWUSAGE;
1171 ast_cli(fd, "No one is calling us\n");
1172 return RESULT_FAILURE;
1177 ast_queue_frame(o->owner, &f);
1179 /* XXX do we really need it ? considering we shut down immediately... */
1180 ring(o, AST_CONTROL_ANSWER);
1182 return RESULT_SUCCESS;
1185 static char answer_usage[] =
1186 "Usage: console answer\n"
1187 " Answers an incoming call on the console (OSS) channel.\n";
1190 * concatenate all arguments into a single string. argv is NULL-terminated
1191 * so we can use it right away
1193 static int console_sendtext_deprecated(int fd, int argc, char *argv[])
1195 struct chan_oss_pvt *o = find_desc(oss_active);
1196 char buf[TEXT_SIZE];
1199 return RESULT_SHOWUSAGE;
1201 ast_cli(fd, "Not in a call\n");
1202 return RESULT_FAILURE;
1204 ast_join(buf, sizeof(buf) - 1, argv + 2);
1205 if (!ast_strlen_zero(buf)) {
1206 struct ast_frame f = { 0, };
1207 int i = strlen(buf);
1209 f.frametype = AST_FRAME_TEXT;
1213 ast_queue_frame(o->owner, &f);
1215 return RESULT_SUCCESS;
1218 static int console_sendtext(int fd, int argc, char *argv[])
1220 struct chan_oss_pvt *o = find_desc(oss_active);
1221 char buf[TEXT_SIZE];
1224 return RESULT_SHOWUSAGE;
1226 ast_cli(fd, "Not in a call\n");
1227 return RESULT_FAILURE;
1229 ast_join(buf, sizeof(buf) - 1, argv + 3);
1230 if (!ast_strlen_zero(buf)) {
1231 struct ast_frame f = { 0, };
1232 int i = strlen(buf);
1234 f.frametype = AST_FRAME_TEXT;
1238 ast_queue_frame(o->owner, &f);
1240 return RESULT_SUCCESS;
1243 static char sendtext_usage[] =
1244 "Usage: console send text <message>\n"
1245 " Sends a text message for display on the remote terminal.\n";
1247 static int console_hangup_deprecated(int fd, int argc, char *argv[])
1249 struct chan_oss_pvt *o = find_desc(oss_active);
1252 return RESULT_SHOWUSAGE;
1255 if (!o->owner && !o->hookstate) { /* XXX maybe only one ? */
1256 ast_cli(fd, "No call to hang up\n");
1257 return RESULT_FAILURE;
1261 ast_queue_hangup(o->owner);
1262 setformat(o, O_CLOSE);
1263 return RESULT_SUCCESS;
1266 static int console_hangup(int fd, int argc, char *argv[])
1268 struct chan_oss_pvt *o = find_desc(oss_active);
1271 return RESULT_SHOWUSAGE;
1274 if (!o->owner && !o->hookstate) { /* XXX maybe only one ? */
1275 ast_cli(fd, "No call to hang up\n");
1276 return RESULT_FAILURE;
1280 ast_queue_hangup(o->owner);
1281 setformat(o, O_CLOSE);
1282 return RESULT_SUCCESS;
1285 static char hangup_usage[] =
1286 "Usage: console hangup\n"
1287 " Hangs up any call currently placed on the console.\n";
1289 static int console_flash_deprecated(int fd, int argc, char *argv[])
1291 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH };
1292 struct chan_oss_pvt *o = find_desc(oss_active);
1295 return RESULT_SHOWUSAGE;
1297 o->nosound = 0; /* when cursound is -1 nosound must be 0 */
1298 if (!o->owner) { /* XXX maybe !o->hookstate too ? */
1299 ast_cli(fd, "No call to flash\n");
1300 return RESULT_FAILURE;
1303 if (o->owner) /* XXX must be true, right ? */
1304 ast_queue_frame(o->owner, &f);
1305 return RESULT_SUCCESS;
1308 static int console_flash(int fd, int argc, char *argv[])
1310 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH };
1311 struct chan_oss_pvt *o = find_desc(oss_active);
1314 return RESULT_SHOWUSAGE;
1316 o->nosound = 0; /* when cursound is -1 nosound must be 0 */
1317 if (!o->owner) { /* XXX maybe !o->hookstate too ? */
1318 ast_cli(fd, "No call to flash\n");
1319 return RESULT_FAILURE;
1322 if (o->owner) /* XXX must be true, right ? */
1323 ast_queue_frame(o->owner, &f);
1324 return RESULT_SUCCESS;
1327 static char flash_usage[] =
1328 "Usage: console flash\n"
1329 " Flashes the call currently placed on the console.\n";
1331 static int console_dial_deprecated(int fd, int argc, char *argv[])
1333 char *s = NULL, *mye = NULL, *myc = NULL;
1334 struct chan_oss_pvt *o = find_desc(oss_active);
1336 if (argc != 1 && argc != 2)
1337 return RESULT_SHOWUSAGE;
1338 if (o->owner) { /* already in a call */
1340 struct ast_frame f = { AST_FRAME_DTMF, 0 };
1342 if (argc == 1) { /* argument is mandatory here */
1343 ast_cli(fd, "Already in a call. You can only dial digits until you hangup.\n");
1344 return RESULT_FAILURE;
1347 /* send the string one char at a time */
1348 for (i = 0; i < strlen(s); i++) {
1350 ast_queue_frame(o->owner, &f);
1352 return RESULT_SUCCESS;
1354 /* if we have an argument split it into extension and context */
1356 s = ast_ext_ctx(argv[1], &mye, &myc);
1357 /* supply default values if needed */
1362 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
1364 oss_new(o, mye, myc, AST_STATE_RINGING);
1366 ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
1369 return RESULT_SUCCESS;
1372 static int console_dial(int fd, int argc, char *argv[])
1374 char *s = NULL, *mye = NULL, *myc = NULL;
1375 struct chan_oss_pvt *o = find_desc(oss_active);
1377 if (argc != 2 && argc != 3)
1378 return RESULT_SHOWUSAGE;
1379 if (o->owner) { /* already in a call */
1381 struct ast_frame f = { AST_FRAME_DTMF, 0 };
1383 if (argc == 1) { /* argument is mandatory here */
1384 ast_cli(fd, "Already in a call. You can only dial digits until you hangup.\n");
1385 return RESULT_FAILURE;
1388 /* send the string one char at a time */
1389 for (i = 0; i < strlen(s); i++) {
1391 ast_queue_frame(o->owner, &f);
1393 return RESULT_SUCCESS;
1395 /* if we have an argument split it into extension and context */
1397 s = ast_ext_ctx(argv[2], &mye, &myc);
1398 /* supply default values if needed */
1403 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
1405 oss_new(o, mye, myc, AST_STATE_RINGING);
1407 ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
1410 return RESULT_SUCCESS;
1413 static char dial_usage[] =
1414 "Usage: console dial [extension[@context]]\n"
1415 " Dials a given extension (and context if specified)\n";
1417 static int __console_mute_unmute(int mute)
1419 struct chan_oss_pvt *o = find_desc(oss_active);
1422 return RESULT_SUCCESS;
1425 static int console_mute_deprecated(int fd, int argc, char *argv[])
1428 return RESULT_SHOWUSAGE;
1430 return __console_mute_unmute(1);
1433 static int console_mute(int fd, int argc, char *argv[])
1436 return RESULT_SHOWUSAGE;
1438 return __console_mute_unmute(1);
1441 static char mute_usage[] =
1442 "Usage: console mute\nMutes the microphone\n";
1444 static int console_unmute_deprecated(int fd, int argc, char *argv[])
1447 return RESULT_SHOWUSAGE;
1449 return __console_mute_unmute(0);
1452 static int console_unmute(int fd, int argc, char *argv[])
1455 return RESULT_SHOWUSAGE;
1457 return __console_mute_unmute(0);
1460 static char unmute_usage[] =
1461 "Usage: console unmute\nUnmutes the microphone\n";
1463 static int console_transfer_deprecated(int fd, int argc, char *argv[])
1465 struct chan_oss_pvt *o = find_desc(oss_active);
1466 struct ast_channel *b = NULL;
1467 char *tmp, *ext, *ctx;
1470 return RESULT_SHOWUSAGE;
1472 return RESULT_FAILURE;
1473 if (o->owner ==NULL || (b = ast_bridged_channel(o->owner)) == NULL) {
1474 ast_cli(fd, "There is no call to transfer\n");
1475 return RESULT_SUCCESS;
1478 tmp = ast_ext_ctx(argv[1], &ext, &ctx);
1479 if (ctx == NULL) /* supply default context if needed */
1480 ctx = o->owner->context;
1481 if (!ast_exists_extension(b, ctx, ext, 1, b->cid.cid_num))
1482 ast_cli(fd, "No such extension exists\n");
1484 ast_cli(fd, "Whee, transferring %s to %s@%s.\n",
1486 if (ast_async_goto(b, ctx, ext, 1))
1487 ast_cli(fd, "Failed to transfer :(\n");
1491 return RESULT_SUCCESS;
1494 static int console_transfer(int fd, int argc, char *argv[])
1496 struct chan_oss_pvt *o = find_desc(oss_active);
1497 struct ast_channel *b = NULL;
1498 char *tmp, *ext, *ctx;
1501 return RESULT_SHOWUSAGE;
1503 return RESULT_FAILURE;
1504 if (o->owner == NULL || (b = ast_bridged_channel(o->owner)) == NULL) {
1505 ast_cli(fd, "There is no call to transfer\n");
1506 return RESULT_SUCCESS;
1509 tmp = ast_ext_ctx(argv[2], &ext, &ctx);
1510 if (ctx == NULL) /* supply default context if needed */
1511 ctx = o->owner->context;
1512 if (!ast_exists_extension(b, ctx, ext, 1, b->cid.cid_num))
1513 ast_cli(fd, "No such extension exists\n");
1515 ast_cli(fd, "Whee, transferring %s to %s@%s.\n", b->name, ext, ctx);
1516 if (ast_async_goto(b, ctx, ext, 1))
1517 ast_cli(fd, "Failed to transfer :(\n");
1521 return RESULT_SUCCESS;
1524 static char transfer_usage[] =
1525 "Usage: console transfer <extension>[@context]\n"
1526 " Transfers the currently connected call to the given extension (and\n"
1527 "context if specified)\n";
1529 static int console_active_deprecated(int fd, int argc, char *argv[])
1532 ast_cli(fd, "active console is [%s]\n", oss_active);
1534 return RESULT_SHOWUSAGE;
1536 struct chan_oss_pvt *o;
1537 if (strcmp(argv[1], "show") == 0) {
1538 for (o = oss_default.next; o; o = o->next)
1539 ast_cli(fd, "device [%s] exists\n", o->name);
1540 return RESULT_SUCCESS;
1542 o = find_desc(argv[1]);
1544 ast_cli(fd, "No device [%s] exists\n", argv[1]);
1546 oss_active = o->name;
1548 return RESULT_SUCCESS;
1551 static int console_active(int fd, int argc, char *argv[])
1554 ast_cli(fd, "active console is [%s]\n", oss_active);
1556 return RESULT_SHOWUSAGE;
1558 struct chan_oss_pvt *o;
1559 if (strcmp(argv[2], "show") == 0) {
1560 for (o = oss_default.next; o; o = o->next)
1561 ast_cli(fd, "device [%s] exists\n", o->name);
1562 return RESULT_SUCCESS;
1564 o = find_desc(argv[2]);
1566 ast_cli(fd, "No device [%s] exists\n", argv[2]);
1568 oss_active = o->name;
1570 return RESULT_SUCCESS;
1573 static char active_usage[] =
1574 "Usage: console active [device]\n"
1575 " If used without a parameter, displays which device is the current\n"
1576 "console. If a device is specified, the console sound device is changed to\n"
1577 "the device specified.\n";
1580 * store the boost factor
1582 static void store_boost(struct chan_oss_pvt *o, char *s)
1585 if (sscanf(s, "%lf", &boost) != 1) {
1586 ast_log(LOG_WARNING, "invalid boost <%s>\n", s);
1589 if (boost < -BOOST_MAX) {
1590 ast_log(LOG_WARNING, "boost %s too small, using %d\n", s, -BOOST_MAX);
1592 } else if (boost > BOOST_MAX) {
1593 ast_log(LOG_WARNING, "boost %s too large, using %d\n", s, BOOST_MAX);
1596 boost = exp(log(10) * boost / 20) * BOOST_SCALE;
1598 ast_log(LOG_WARNING, "setting boost %s to %d\n", s, o->boost);
1601 static int do_boost(int fd, int argc, char *argv[])
1603 struct chan_oss_pvt *o = find_desc(oss_active);
1606 ast_cli(fd, "boost currently %5.1f\n", 20 * log10(((double) o->boost / (double) BOOST_SCALE)));
1608 store_boost(o, argv[2]);
1609 return RESULT_SUCCESS;
1612 static struct ast_cli_entry cli_oss_answer_deprecated = {
1614 console_answer_deprecated, NULL,
1617 static struct ast_cli_entry cli_oss_hangup_deprecated = {
1619 console_hangup_deprecated, NULL,
1622 static struct ast_cli_entry cli_oss_flash_deprecated = {
1624 console_flash_deprecated, NULL,
1627 static struct ast_cli_entry cli_oss_dial_deprecated = {
1629 console_dial_deprecated, NULL,
1632 static struct ast_cli_entry cli_oss_mute_deprecated = {
1634 console_mute_deprecated, NULL,
1637 static struct ast_cli_entry cli_oss_unmute_deprecated = {
1639 console_unmute_deprecated, NULL,
1642 static struct ast_cli_entry cli_oss_transfer_deprecated = {
1643 { "transfer", NULL },
1644 console_transfer_deprecated, NULL,
1647 static struct ast_cli_entry cli_oss_send_text_deprecated = {
1648 { "send", "text", NULL },
1649 console_sendtext_deprecated, NULL,
1652 static struct ast_cli_entry cli_oss_autoanswer_deprecated = {
1653 { "autoanswer", NULL },
1654 console_autoanswer_deprecated, NULL,
1655 NULL, autoanswer_complete_deprecated };
1657 static struct ast_cli_entry cli_oss_boost_deprecated = {
1658 { "oss", "boost", NULL },
1662 static struct ast_cli_entry cli_oss_active_deprecated = {
1663 { "console", NULL },
1664 console_active_deprecated, NULL,
1667 static struct ast_cli_entry cli_oss[] = {
1668 { { "console", "answer", NULL },
1669 console_answer, "Answer an incoming console call",
1670 answer_usage, NULL, &cli_oss_answer_deprecated },
1672 { { "console", "hangup", NULL },
1673 console_hangup, "Hangup a call on the console",
1674 hangup_usage, NULL, &cli_oss_hangup_deprecated },
1676 { { "console", "flash", NULL },
1677 console_flash, "Flash a call on the console",
1678 flash_usage, NULL, &cli_oss_flash_deprecated },
1680 { { "console", "dial", NULL },
1681 console_dial, "Dial an extension on the console",
1682 dial_usage, NULL, &cli_oss_dial_deprecated },
1684 { { "console", "mute", NULL },
1685 console_mute, "Disable mic input",
1686 mute_usage, NULL, &cli_oss_mute_deprecated },
1688 { { "console", "unmute", NULL },
1689 console_unmute, "Enable mic input",
1690 unmute_usage, NULL, &cli_oss_unmute_deprecated },
1692 { { "console", "transfer", NULL },
1693 console_transfer, "Transfer a call to a different extension",
1694 transfer_usage, NULL, &cli_oss_transfer_deprecated },
1696 { { "console", "send", "text", NULL },
1697 console_sendtext, "Send text to the remote device",
1698 sendtext_usage, NULL, &cli_oss_send_text_deprecated },
1700 { { "console", "autoanswer", NULL },
1701 console_autoanswer, "Sets/displays autoanswer",
1702 autoanswer_usage, autoanswer_complete, &cli_oss_autoanswer_deprecated },
1704 { { "console", "boost", NULL },
1705 do_boost, "Sets/displays mic boost in dB",
1706 NULL, NULL, &cli_oss_boost_deprecated },
1708 { { "console", "active", NULL },
1709 console_active, "Sets/displays active console",
1710 active_usage, NULL, &cli_oss_active_deprecated },
1714 * store the mixer argument from the config file, filtering possibly
1715 * invalid or dangerous values (the string is used as argument for
1716 * system("mixer %s")
1718 static void store_mixer(struct chan_oss_pvt *o, char *s)
1722 for (i = 0; i < strlen(s); i++) {
1723 if (!isalnum(s[i]) && index(" \t-/", s[i]) == NULL) {
1724 ast_log(LOG_WARNING, "Suspect char %c in mixer cmd, ignoring:\n\t%s\n", s[i], s);
1730 o->mixer_cmd = ast_strdup(s);
1731 ast_log(LOG_WARNING, "setting mixer %s\n", s);
1735 * store the callerid components
1737 static void store_callerid(struct chan_oss_pvt *o, char *s)
1739 ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
1743 * grab fields from the config file, init the descriptor and open the device.
1745 static struct chan_oss_pvt *store_config(struct ast_config *cfg, char *ctg)
1747 struct ast_variable *v;
1748 struct chan_oss_pvt *o;
1754 if (!(o = ast_calloc(1, sizeof(*o))))
1757 /* "general" is also the default thing */
1758 if (strcmp(ctg, "general") == 0) {
1759 o->name = ast_strdup("dsp");
1760 oss_active = o->name;
1763 o->name = ast_strdup(ctg);
1766 strcpy(o->mohinterpret, "default");
1768 o->lastopen = ast_tvnow(); /* don't leave it 0 or tvdiff may wrap */
1769 /* fill other fields from configuration */
1770 for (v = ast_variable_browse(cfg, ctg); v; v = v->next) {
1771 M_START(v->name, v->value);
1773 /* handle jb conf */
1774 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
1777 M_BOOL("autoanswer", o->autoanswer)
1778 M_BOOL("autohangup", o->autohangup)
1779 M_BOOL("overridecontext", o->overridecontext)
1780 M_STR("device", o->device)
1781 M_UINT("frags", o->frags)
1782 M_UINT("debug", oss_debug)
1783 M_UINT("queuesize", o->queuesize)
1784 M_STR("context", o->ctx)
1785 M_STR("language", o->language)
1786 M_STR("mohinterpret", o->mohinterpret)
1787 M_STR("extension", o->ext)
1788 M_F("mixer", store_mixer(o, v->value))
1789 M_F("callerid", store_callerid(o, v->value))
1790 M_F("boost", store_boost(o, v->value))
1794 if (ast_strlen_zero(o->device))
1795 ast_copy_string(o->device, DEV_DSP, sizeof(o->device));
1799 asprintf(&cmd, "mixer %s", o->mixer_cmd);
1800 ast_log(LOG_WARNING, "running [%s]\n", cmd);
1804 if (o == &oss_default) /* we are done with the default */
1809 if (setformat(o, O_RDWR) < 0) { /* open device */
1810 if (option_verbose > 0) {
1811 ast_verbose(VERBOSE_PREFIX_2 "Device %s not detected\n", ctg);
1812 ast_verbose(VERBOSE_PREFIX_2 "Turn off OSS support by adding " "'noload=chan_oss.so' in /etc/asterisk/modules.conf\n");
1816 if (o->duplex != M_FULL)
1817 ast_log(LOG_WARNING, "XXX I don't work right with non " "full-duplex sound cards XXX\n");
1818 #endif /* TRYOPEN */
1819 if (pipe(o->sndcmd) != 0) {
1820 ast_log(LOG_ERROR, "Unable to create pipe\n");
1823 ast_pthread_create(&o->sthread, NULL, sound_thread, o);
1824 /* link into list of devices */
1825 if (o != &oss_default) {
1826 o->next = oss_default.next;
1827 oss_default.next = o;
1832 if (o != &oss_default)
1837 static int load_module(void)
1840 struct ast_config *cfg;
1842 /* Copy the default jb config over global_jbconf */
1843 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
1845 /* load config file */
1846 cfg = ast_config_load(config);
1848 char *ctg = NULL; /* first pass is 'general' */
1851 store_config(cfg, ctg);
1852 } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL);
1853 ast_config_destroy(cfg);
1855 ast_log(LOG_NOTICE, "Unable to load config oss.conf\n");
1856 return AST_MODULE_LOAD_DECLINE;
1858 if (find_desc(oss_active) == NULL) {
1859 ast_log(LOG_NOTICE, "Device %s not found\n", oss_active);
1860 /* XXX we could default to 'dsp' perhaps ? */
1861 /* XXX should cleanup allocated memory etc. */
1864 i = ast_channel_register(&oss_tech);
1866 ast_log(LOG_ERROR, "Unable to register channel class 'Console'\n");
1867 /* XXX should cleanup allocated memory etc. */
1870 ast_cli_register_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry));
1875 static int unload_module(void)
1877 struct chan_oss_pvt *o;
1879 ast_channel_unregister(&oss_tech);
1880 ast_cli_unregister_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry));
1882 for (o = oss_default.next; o; o = o->next) {
1884 if (o->sndcmd[0] > 0) {
1885 close(o->sndcmd[0]);
1886 close(o->sndcmd[1]);
1889 ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
1890 if (o->owner) /* XXX how ??? */
1892 /* XXX what about the thread ? */
1893 /* XXX what about the memory allocated ? */
1898 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "OSS Console Channel Driver");