![]() |
Home page |
Mailing list |
Docs
Asterisk developer's documentation :: Codename Pineapple
chan_oss.c
Go to the documentation of this file.
00001 /* 00002 * Asterisk -- An open source telephony toolkit. 00003 * 00004 * Copyright (C) 1999 - 2005, Digium, Inc. 00005 * 00006 * Mark Spencer <markster@digium.com> 00007 * 00008 * FreeBSD changes and multiple device support by Luigi Rizzo, 2005.05.25 00009 * note-this code best seen with ts=8 (8-spaces tabs) in the editor 00010 * 00011 * See http://www.asterisk.org for more information about 00012 * the Asterisk project. Please do not directly contact 00013 * any of the maintainers of this project for assistance; 00014 * the project provides a web site, mailing lists and IRC 00015 * channels for your use. 00016 * 00017 * This program is free software, distributed under the terms of 00018 * the GNU General Public License Version 2. See the LICENSE file 00019 * at the top of the source tree. 00020 */ 00021 00022 /*! \file 00023 * 00024 * \brief Channel driver for OSS sound cards 00025 * 00026 * \author Mark Spencer <markster@digium.com> 00027 * \author Luigi Rizzo 00028 * 00029 * \par See also 00030 * \arg \ref Config_oss 00031 * 00032 * \ingroup channel_drivers 00033 */ 00034 00035 /*** MODULEINFO 00036 <depend>ossaudio</depend> 00037 ***/ 00038 00039 #include "asterisk.h" 00040 00041 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 51806 $") 00042 00043 #include <stdio.h> 00044 #include <ctype.h> 00045 #include <math.h> 00046 #include <string.h> 00047 #include <unistd.h> 00048 #include <sys/ioctl.h> 00049 #include <fcntl.h> 00050 #include <sys/time.h> 00051 #include <stdlib.h> 00052 #include <errno.h> 00053 00054 #ifdef __linux 00055 #include <linux/soundcard.h> 00056 #elif defined(__FreeBSD__) 00057 #include <sys/soundcard.h> 00058 #else 00059 #include <soundcard.h> 00060 #endif 00061 00062 #include "asterisk/lock.h" 00063 #include "asterisk/frame.h" 00064 #include "asterisk/logger.h" 00065 #include "asterisk/callerid.h" 00066 #include "asterisk/channel.h" 00067 #include "asterisk/module.h" 00068 #include "asterisk/options.h" 00069 #include "asterisk/pbx.h" 00070 #include "asterisk/config.h" 00071 #include "asterisk/cli.h" 00072 #include "asterisk/utils.h" 00073 #include "asterisk/causes.h" 00074 #include "asterisk/endian.h" 00075 #include "asterisk/stringfields.h" 00076 #include "asterisk/abstract_jb.h" 00077 #include "asterisk/musiconhold.h" 00078 #include "asterisk/app.h" 00079 00080 /* ringtones we use */ 00081 #include "busy.h" 00082 #include "ringtone.h" 00083 #include "ring10.h" 00084 #include "answer.h" 00085 00086 /*! Global jitterbuffer configuration - by default, jb is disabled */ 00087 static struct ast_jb_conf default_jbconf = 00088 { 00089 .flags = 0, 00090 .max_size = -1, 00091 .resync_threshold = -1, 00092 .impl = "", 00093 }; 00094 static struct ast_jb_conf global_jbconf; 00095 00096 /* 00097 * Basic mode of operation: 00098 * 00099 * we have one keyboard (which receives commands from the keyboard) 00100 * and multiple headset's connected to audio cards. 00101 * Cards/Headsets are named as the sections of oss.conf. 00102 * The section called [general] contains the default parameters. 00103 * 00104 * At any time, the keyboard is attached to one card, and you 00105 * can switch among them using the command 'console foo' 00106 * where 'foo' is the name of the card you want. 00107 * 00108 * oss.conf parameters are 00109 START_CONFIG 00110 00111 [general] 00112 ; General config options, with default values shown. 00113 ; You should use one section per device, with [general] being used 00114 ; for the first device and also as a template for other devices. 00115 ; 00116 ; All but 'debug' can go also in the device-specific sections. 00117 ; 00118 ; debug = 0x0 ; misc debug flags, default is 0 00119 00120 ; Set the device to use for I/O 00121 ; device = /dev/dsp 00122 00123 ; Optional mixer command to run upon startup (e.g. to set 00124 ; volume levels, mutes, etc. 00125 ; mixer = 00126 00127 ; Software mic volume booster (or attenuator), useful for sound 00128 ; cards or microphones with poor sensitivity. The volume level 00129 ; is in dB, ranging from -20.0 to +20.0 00130 ; boost = n ; mic volume boost in dB 00131 00132 ; Set the callerid for outgoing calls 00133 ; callerid = John Doe <555-1234> 00134 00135 ; autoanswer = no ; no autoanswer on call 00136 ; autohangup = yes ; hangup when other party closes 00137 ; extension = s ; default extension to call 00138 ; context = default ; default context for outgoing calls 00139 ; language = "" ; default language 00140 00141 ; Default Music on Hold class to use when this channel is placed on hold in 00142 ; the case that the music class is not set on the channel with 00143 ; Set(CHANNEL(musicclass)=whatever) in the dialplan and the peer channel 00144 ; putting this one on hold did not suggest a class to use. 00145 ; 00146 ; mohinterpret=default 00147 00148 ; If you set overridecontext to 'yes', then the whole dial string 00149 ; will be interpreted as an extension, which is extremely useful 00150 ; to dial SIP, IAX and other extensions which use the '@' character. 00151 ; The default is 'no' just for backward compatibility, but the 00152 ; suggestion is to change it. 00153 ; overridecontext = no ; if 'no', the last @ will start the context 00154 ; if 'yes' the whole string is an extension. 00155 00156 ; low level device parameters in case you have problems with the 00157 ; device driver on your operating system. You should not touch these 00158 ; unless you know what you are doing. 00159 ; queuesize = 10 ; frames in device driver 00160 ; frags = 8 ; argument to SETFRAGMENT 00161 00162 ;------------------------------ JITTER BUFFER CONFIGURATION -------------------------- 00163 ; jbenable = yes ; Enables the use of a jitterbuffer on the receiving side of an 00164 ; OSS channel. Defaults to "no". An enabled jitterbuffer will 00165 ; be used only if the sending side can create and the receiving 00166 ; side can not accept jitter. The OSS channel can't accept jitter, 00167 ; thus an enabled jitterbuffer on the receive OSS side will always 00168 ; be used if the sending side can create jitter. 00169 00170 ; jbmaxsize = 200 ; Max length of the jitterbuffer in milliseconds. 00171 00172 ; jbresyncthreshold = 1000 ; Jump in the frame timestamps over which the jitterbuffer is 00173 ; resynchronized. Useful to improve the quality of the voice, with 00174 ; big jumps in/broken timestamps, usualy sent from exotic devices 00175 ; and programs. Defaults to 1000. 00176 00177 ; jbimpl = fixed ; Jitterbuffer implementation, used on the receiving side of an OSS 00178 ; channel. Two implementations are currenlty available - "fixed" 00179 ; (with size always equals to jbmax-size) and "adaptive" (with 00180 ; variable size, actually the new jb of IAX2). Defaults to fixed. 00181 00182 ; jblog = no ; Enables jitterbuffer frame logging. Defaults to "no". 00183 ;----------------------------------------------------------------------------------- 00184 00185 [card1] 00186 ; device = /dev/dsp1 ; alternate device 00187 00188 END_CONFIG 00189 00190 .. and so on for the other cards. 00191 00192 */ 00193 00194 /* 00195 * Helper macros to parse config arguments. They will go in a common 00196 * header file if their usage is globally accepted. In the meantime, 00197 * we define them here. Typical usage is as below. 00198 * Remember to open a block right before M_START (as it declares 00199 * some variables) and use the M_* macros WITHOUT A SEMICOLON: 00200 * 00201 * { 00202 * M_START(v->name, v->value) 00203 * 00204 * M_BOOL("dothis", x->flag1) 00205 * M_STR("name", x->somestring) 00206 * M_F("bar", some_c_code) 00207 * M_END(some_final_statement) 00208 * ... other code in the block 00209 * } 00210 * 00211 * XXX NOTE these macros should NOT be replicated in other parts of asterisk. 00212 * Likely we will come up with a better way of doing config file parsing. 00213 */ 00214 #define M_START(var, val) \ 00215 char *__s = var; char *__val = val; 00216 #define M_END(x) x; 00217 #define M_F(tag, f) if (!strcasecmp((__s), tag)) { f; } else 00218 #define M_BOOL(tag, dst) M_F(tag, (dst) = ast_true(__val) ) 00219 #define M_UINT(tag, dst) M_F(tag, (dst) = strtoul(__val, NULL, 0) ) 00220 #define M_STR(tag, dst) M_F(tag, ast_copy_string(dst, __val, sizeof(dst))) 00221 00222 /* 00223 * The following parameters are used in the driver: 00224 * 00225 * FRAME_SIZE the size of an audio frame, in samples. 00226 * 160 is used almost universally, so you should not change it. 00227 * 00228 * FRAGS the argument for the SETFRAGMENT ioctl. 00229 * Overridden by the 'frags' parameter in oss.conf 00230 * 00231 * Bits 0-7 are the base-2 log of the device's block size, 00232 * bits 16-31 are the number of blocks in the driver's queue. 00233 * There are a lot of differences in the way this parameter 00234 * is supported by different drivers, so you may need to 00235 * experiment a bit with the value. 00236 * A good default for linux is 30 blocks of 64 bytes, which 00237 * results in 6 frames of 320 bytes (160 samples). 00238 * FreeBSD works decently with blocks of 256 or 512 bytes, 00239 * leaving the number unspecified. 00240 * Note that this only refers to the device buffer size, 00241 * this module will then try to keep the lenght of audio 00242 * buffered within small constraints. 00243 * 00244 * QUEUE_SIZE The max number of blocks actually allowed in the device 00245 * driver's buffer, irrespective of the available number. 00246 * Overridden by the 'queuesize' parameter in oss.conf 00247 * 00248 * Should be >=2, and at most as large as the hw queue above 00249 * (otherwise it will never be full). 00250 */ 00251 00252 #define FRAME_SIZE 160 00253 #define QUEUE_SIZE 10 00254 00255 #if defined(__FreeBSD__) 00256 #define FRAGS 0x8 00257 #else 00258 #define FRAGS ( ( (6 * 5) << 16 ) | 0x6 ) 00259 #endif 00260 00261 /* 00262 * XXX text message sizes are probably 256 chars, but i am 00263 * not sure if there is a suitable definition anywhere. 00264 */ 00265 #define TEXT_SIZE 256 00266 00267 #if 0 00268 #define TRYOPEN 1 /* try to open on startup */ 00269 #endif 00270 #define O_CLOSE 0x444 /* special 'close' mode for device */ 00271 /* Which device to use */ 00272 #if defined( __OpenBSD__ ) || defined( __NetBSD__ ) 00273 #define DEV_DSP "/dev/audio" 00274 #else 00275 #define DEV_DSP "/dev/dsp" 00276 #endif 00277 00278 #ifndef MIN 00279 #define MIN(a,b) ((a) < (b) ? (a) : (b)) 00280 #endif 00281 #ifndef MAX 00282 #define MAX(a,b) ((a) > (b) ? (a) : (b)) 00283 #endif 00284 00285 static char *config = "oss.conf"; /* default config file */ 00286 00287 static int oss_debug; 00288 00289 /*! 00290 * Each sound is made of 'datalen' samples of sound, repeated as needed to 00291 * generate 'samplen' samples of data, then followed by 'silencelen' samples 00292 * of silence. The loop is repeated if 'repeat' is set. 00293 */ 00294 struct sound { 00295 int ind; 00296 char *desc; 00297 short *data; 00298 int datalen; 00299 int samplen; 00300 int silencelen; 00301 int repeat; 00302 }; 00303 00304 static struct sound sounds[] = { 00305 { AST_CONTROL_RINGING, "RINGING", ringtone, sizeof(ringtone)/2, 16000, 32000, 1 }, 00306 { AST_CONTROL_BUSY, "BUSY", busy, sizeof(busy)/2, 4000, 4000, 1 }, 00307 { AST_CONTROL_CONGESTION, "CONGESTION", busy, sizeof(busy)/2, 2000, 2000, 1 }, 00308 { AST_CONTROL_RING, "RING10", ring10, sizeof(ring10)/2, 16000, 32000, 1 }, 00309 { AST_CONTROL_ANSWER, "ANSWER", answer, sizeof(answer)/2, 2200, 0, 0 }, 00310 { -1, NULL, 0, 0, 0, 0 }, /* end marker */ 00311 }; 00312 00313 00314 /*! 00315 * \brief descriptor for one of our channels. 00316 * 00317 * There is one used for 'default' values (from the [general] entry in 00318 * the configuration file), and then one instance for each device 00319 * (the default is cloned from [general], others are only created 00320 * if the relevant section exists). 00321 */ 00322 struct chan_oss_pvt { 00323 struct chan_oss_pvt *next; 00324 00325 char *name; 00326 /*! 00327 * cursound indicates which in struct sound we play. -1 means nothing, 00328 * any other value is a valid sound, in which case sampsent indicates 00329 * the next sample to send in [0..samplen + silencelen] 00330 * nosound is set to disable the audio data from the channel 00331 * (so we can play the tones etc.). 00332 */ 00333 int sndcmd[2]; /*!< Sound command pipe */ 00334 int cursound; /*!< index of sound to send */ 00335 int sampsent; /*!< # of sound samples sent */ 00336 int nosound; /*!< set to block audio from the PBX */ 00337 00338 int total_blocks; /*!< total blocks in the output device */ 00339 int sounddev; 00340 enum { M_UNSET, M_FULL, M_READ, M_WRITE } duplex; 00341 int autoanswer; 00342 int autohangup; 00343 int hookstate; 00344 char *mixer_cmd; /*!< initial command to issue to the mixer */ 00345 unsigned int queuesize; /*!< max fragments in queue */ 00346 unsigned int frags; /*!< parameter for SETFRAGMENT */ 00347 00348 int warned; /*!< various flags used for warnings */ 00349 #define WARN_used_blocks 1 00350 #define WARN_speed 2 00351 #define WARN_frag 4 00352 int w_errors; /*!< overfull in the write path */ 00353 struct timeval lastopen; 00354 00355 int overridecontext; 00356 int mute; 00357 00358 /*! boost support. BOOST_SCALE * 10 ^(BOOST_MAX/20) must 00359 * be representable in 16 bits to avoid overflows. 00360 */ 00361 #define BOOST_SCALE (1<<9) 00362 #define BOOST_MAX 40 /*!< slightly less than 7 bits */ 00363 int boost; /*!< input boost, scaled by BOOST_SCALE */ 00364 char device[64]; /*!< device to open */ 00365 00366 pthread_t sthread; 00367 00368 struct ast_channel *owner; 00369 char ext[AST_MAX_EXTENSION]; 00370 char ctx[AST_MAX_CONTEXT]; 00371 char language[MAX_LANGUAGE]; 00372 char cid_name[256]; /*XXX */ 00373 char cid_num[256]; /*XXX */ 00374 char mohinterpret[MAX_MUSICCLASS]; 00375 00376 /*! buffers used in oss_write */ 00377 char oss_write_buf[FRAME_SIZE * 2]; 00378 int oss_write_dst; 00379 /*! buffers used in oss_read - AST_FRIENDLY_OFFSET space for headers 00380 * plus enough room for a full frame 00381 */ 00382 char oss_read_buf[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET]; 00383 int readpos; /*!< read position above */ 00384 struct ast_frame read_f; /*!< returned by oss_read */ 00385 }; 00386 00387 static struct chan_oss_pvt oss_default = { 00388 .cursound = -1, 00389 .sounddev = -1, 00390 .duplex = M_UNSET, /* XXX check this */ 00391 .autoanswer = 1, 00392 .autohangup = 1, 00393 .queuesize = QUEUE_SIZE, 00394 .frags = FRAGS, 00395 .ext = "s", 00396 .ctx = "default", 00397 .readpos = AST_FRIENDLY_OFFSET, /* start here on reads */ 00398 .lastopen = { 0, 0 }, 00399 .boost = BOOST_SCALE, 00400 }; 00401 00402 static char *oss_active; /*!< the active device */ 00403 00404 static int setformat(struct chan_oss_pvt *o, int mode); 00405 00406 static struct ast_channel *oss_request(const char *type, int format, void *data 00407 , int *cause); 00408 static int oss_digit_begin(struct ast_channel *c, char digit); 00409 static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration); 00410 static int oss_text(struct ast_channel *c, const char *text); 00411 static int oss_hangup(struct ast_channel *c); 00412 static int oss_answer(struct ast_channel *c); 00413 static struct ast_frame *oss_read(struct ast_channel *chan); 00414 static int oss_call(struct ast_channel *c, char *dest, int timeout); 00415 static int oss_write(struct ast_channel *chan, struct ast_frame *f); 00416 static int oss_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen); 00417 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan); 00418 static char tdesc[] = "OSS Console Channel Driver"; 00419 00420 static const struct ast_channel_tech oss_tech = { 00421 .type = "Console", 00422 .description = tdesc, 00423 .capabilities = AST_FORMAT_SLINEAR, 00424 .requester = oss_request, 00425 .send_digit_begin = oss_digit_begin, 00426 .send_digit_end = oss_digit_end, 00427 .send_text = oss_text, 00428 .hangup = oss_hangup, 00429 .answer = oss_answer, 00430 .read = oss_read, 00431 .call = oss_call, 00432 .write = oss_write, 00433 .indicate = oss_indicate, 00434 .fixup = oss_fixup, 00435 }; 00436 00437 /*! 00438 * \brief returns a pointer to the descriptor with the given name 00439 */ 00440 static struct chan_oss_pvt *find_desc(char *dev) 00441 { 00442 struct chan_oss_pvt *o = NULL; 00443 00444 if (!dev) 00445 ast_log(LOG_WARNING, "null dev\n"); 00446 00447 for (o = oss_default.next; o && o->name && dev && strcmp(o->name, dev) != 0; o = o->next); 00448 00449 if (!o) 00450 ast_log(LOG_WARNING, "could not find <%s>\n", dev ? dev : "--no-device--"); 00451 00452 return o; 00453 } 00454 00455 /* ! 00456 * \brief split a string in extension-context, returns pointers to malloc'ed 00457 * strings. 00458 * 00459 * If we do not have 'overridecontext' then the last @ is considered as 00460 * a context separator, and the context is overridden. 00461 * This is usually not very necessary as you can play with the dialplan, 00462 * and it is nice not to need it because you have '@' in SIP addresses. 00463 * 00464 * \return the buffer address. 00465 */ 00466 static char *ast_ext_ctx(const char *src, char **ext, char **ctx) 00467 { 00468 struct chan_oss_pvt *o = find_desc(oss_active); 00469 00470 if (ext == NULL || ctx == NULL) 00471 return NULL; /* error */ 00472 00473 *ext = *ctx = NULL; 00474 00475 if (src && *src != '\0') 00476 *ext = ast_strdup(src); 00477 00478 if (*ext == NULL) 00479 return NULL; 00480 00481 if (!o->overridecontext) { 00482 /* parse from the right */ 00483 *ctx = strrchr(*ext, '@'); 00484 if (*ctx) 00485 *(*ctx)++ = '\0'; 00486 } 00487 00488 return *ext; 00489 } 00490 00491 /*! 00492 * \brief Returns the number of blocks used in the audio output channel 00493 */ 00494 static int used_blocks(struct chan_oss_pvt *o) 00495 { 00496 struct audio_buf_info info; 00497 00498 if (ioctl(o->sounddev, SNDCTL_DSP_GETOSPACE, &info)) { 00499 if (!(o->warned & WARN_used_blocks)) { 00500 ast_log(LOG_WARNING, "Error reading output space\n"); 00501 o->warned |= WARN_used_blocks; 00502 } 00503 return 1; 00504 } 00505 00506 if (o->total_blocks == 0) { 00507 if (0) /* debugging */ 00508 ast_log(LOG_WARNING, "fragtotal %d size %d avail %d\n", info.fragstotal, info.fragsize, info.fragments); 00509 o->total_blocks = info.fragments; 00510 } 00511 00512 return o->total_blocks - info.fragments; 00513 } 00514 00515 /*! Write an exactly FRAME_SIZE sized frame */ 00516 static int soundcard_writeframe(struct chan_oss_pvt *o, short *data) 00517 { 00518 int res; 00519 00520 if (o->sounddev < 0) 00521 setformat(o, O_RDWR); 00522 if (o->sounddev < 0) 00523 return 0; /* not fatal */ 00524 /* 00525 * Nothing complex to manage the audio device queue. 00526 * If the buffer is full just drop the extra, otherwise write. 00527 * XXX in some cases it might be useful to write anyways after 00528 * a number of failures, to restart the output chain. 00529 */ 00530 res = used_blocks(o); 00531 if (res > o->queuesize) { /* no room to write a block */ 00532 if (o->w_errors++ == 0 && (oss_debug & 0x4)) 00533 ast_log(LOG_WARNING, "write: used %d blocks (%d)\n", res, o->w_errors); 00534 return 0; 00535 } 00536 o->w_errors = 0; 00537 return write(o->sounddev, (void *)data, FRAME_SIZE * 2); 00538 } 00539 00540 /*! 00541 * \brief Handler for 'sound writable' events from the sound thread. 00542 * 00543 * Builds a frame from the high level description of the sounds, 00544 * and passes it to the audio device. 00545 * The actual sound is made of 1 or more sequences of sound samples 00546 * (s->datalen, repeated to make s->samplen samples) followed by 00547 * s->silencelen samples of silence. The position in the sequence is stored 00548 * in o->sampsent, which goes between 0 .. s->samplen+s->silencelen. 00549 * In case we fail to write a frame, don't update o->sampsent. 00550 */ 00551 static void send_sound(struct chan_oss_pvt *o) 00552 { 00553 short myframe[FRAME_SIZE]; 00554 int ofs, l, start; 00555 int l_sampsent = o->sampsent; 00556 struct sound *s; 00557 00558 if (o->cursound < 0) /* no sound to send */ 00559 return; 00560 00561 s = &sounds[o->cursound]; 00562 00563 for (ofs = 0; ofs < FRAME_SIZE; ofs += l) { 00564 l = s->samplen - l_sampsent; /* # of available samples */ 00565 if (l > 0) { 00566 start = l_sampsent % s->datalen; /* source offset */ 00567 l = MIN(l, FRAME_SIZE - ofs); /* don't overflow the frame */ 00568 l = MIN(l, s->datalen - start); /* don't overflow the source */ 00569 bcopy(s->data + start, myframe + ofs, l * 2); 00570 if (0) 00571 ast_log(LOG_WARNING, "send_sound sound %d/%d of %d into %d\n", l_sampsent, l, s->samplen, ofs); 00572 l_sampsent += l; 00573 } else { /* end of samples, maybe some silence */ 00574 static const short silence[FRAME_SIZE] = { 0, }; 00575 00576 l += s->silencelen; 00577 if (l > 0) { 00578 l = MIN(l, FRAME_SIZE - ofs); 00579 bcopy(silence, myframe + ofs, l * 2); 00580 l_sampsent += l; 00581 } else { /* silence is over, restart sound if loop */ 00582 if (s->repeat == 0) { /* last block */ 00583 o->cursound = -1; 00584 o->nosound = 0; /* allow audio data */ 00585 if (ofs < FRAME_SIZE) /* pad with silence */ 00586 bcopy(silence, myframe + ofs, (FRAME_SIZE - ofs) * 2); 00587 } 00588 l_sampsent = 0; 00589 } 00590 } 00591 } 00592 l = soundcard_writeframe(o, myframe); 00593 if (l > 0) 00594 o->sampsent = l_sampsent; /* update status */ 00595 } 00596 00597 static void *sound_thread(void *arg) 00598 { 00599 char ign[4096]; 00600 struct chan_oss_pvt *o = (struct chan_oss_pvt *) arg; 00601 00602 /* 00603 * Just in case, kick the driver by trying to read from it. 00604 * Ignore errors - this read is almost guaranteed to fail. 00605 */ 00606 read(o->sounddev, ign, sizeof(ign)); 00607 for (;;) { 00608 fd_set rfds, wfds; 00609 int maxfd, res; 00610 struct timeval *to = NULL, t; 00611 00612 FD_ZERO(&rfds); 00613 FD_ZERO(&wfds); 00614 FD_SET(o->sndcmd[0], &rfds); 00615 maxfd = o->sndcmd[0]; /* pipe from the main process */ 00616 if (o->cursound > -1 && o->sounddev < 0) 00617 setformat(o, O_RDWR); /* need the channel, try to reopen */ 00618 else if (o->cursound == -1 && o->owner == NULL) 00619 setformat(o, O_CLOSE); /* can close */ 00620 if (o->sounddev > -1) { 00621 if (!o->owner) { /* no one owns the audio, so we must drain it */ 00622 FD_SET(o->sounddev, &rfds); 00623 maxfd = MAX(o->sounddev, maxfd); 00624 } 00625 if (o->cursound > -1) { 00626 /* 00627 * We would like to use select here, but the device 00628 * is always writable, so this would become busy wait. 00629 * So we rather set a timeout to 1/2 of the frame size. 00630 */ 00631 t.tv_sec = 0; 00632 t.tv_usec = (1000000 * FRAME_SIZE) / (5 * DEFAULT_SAMPLE_RATE); 00633 to = &t; 00634 } 00635 } 00636 /* ast_select emulates linux behaviour in terms of timeout handling */ 00637 res = ast_select(maxfd + 1, &rfds, &wfds, NULL, to); 00638 if (res < 0) { 00639 ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno)); 00640 sleep(1); 00641 continue; 00642 } 00643 if (FD_ISSET(o->sndcmd[0], &rfds)) { 00644 /* read which sound to play from the pipe */ 00645 int i, what = -1; 00646 00647 read(o->sndcmd[0], &what, sizeof(what)); 00648 for (i = 0; sounds[i].ind != -1; i++) { 00649 if (sounds[i].ind == what) { 00650 o->cursound = i; 00651 o->sampsent = 0; 00652 o->nosound = 1; /* block audio from pbx */ 00653 break; 00654 } 00655 } 00656 if (sounds[i].ind == -1) 00657 ast_log(LOG_WARNING, "invalid sound index: %d\n", what); 00658 } 00659 if (o->sounddev > -1) { 00660 if (FD_ISSET(o->sounddev, &rfds)) /* read and ignore errors */ 00661 read(o->sounddev, ign, sizeof(ign)); 00662 if (to != NULL) /* maybe it is possible to write */ 00663 send_sound(o); 00664 } 00665 } 00666 return NULL; /* Never reached */ 00667 } 00668 00669 /*! 00670 * reset and close the device if opened, 00671 * then open and initialize it in the desired mode, 00672 * trigger reads and writes so we can start using it. 00673 */ 00674 static int setformat(struct chan_oss_pvt *o, int mode) 00675 { 00676 int fmt, desired, res, fd; 00677 00678 if (o->sounddev >= 0) { 00679 ioctl(o->sounddev, SNDCTL_DSP_RESET, 0); 00680 close(o->sounddev); 00681 o->duplex = M_UNSET; 00682 o->sounddev = -1; 00683 } 00684 if (mode == O_CLOSE) /* we are done */ 00685 return 0; 00686 if (ast_tvdiff_ms(ast_tvnow(), o->lastopen) < 1000) 00687 return -1; /* don't open too often */ 00688 o->lastopen = ast_tvnow(); 00689 fd = o->sounddev = open(o->device, mode | O_NONBLOCK); 00690 if (fd < 0) { 00691 ast_log(LOG_WARNING, "Unable to re-open DSP device %s: %s\n", o->device, strerror(errno)); 00692 return -1; 00693 } 00694 if (o->owner) 00695 o->owner->fds[0] = fd; 00696 00697 #if __BYTE_ORDER == __LITTLE_ENDIAN 00698 fmt = AFMT_S16_LE; 00699 #else 00700 fmt = AFMT_S16_BE; 00701 #endif 00702 res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt); 00703 if (res < 0) { 00704 ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n"); 00705 return -1; 00706 } 00707 switch (mode) { 00708 case O_RDWR: 00709 res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0); 00710 /* Check to see if duplex set (FreeBSD Bug) */ 00711 res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt); 00712 if (res == 0 && (fmt & DSP_CAP_DUPLEX)) { 00713 if (option_verbose > 1) 00714 ast_verbose(VERBOSE_PREFIX_2 "Console is full duplex\n"); 00715 o->duplex = M_FULL; 00716 }; 00717 break; 00718 00719 case O_WRONLY: 00720 o->duplex = M_WRITE; 00721 break; 00722 00723 case O_RDONLY: 00724 o->duplex = M_READ; 00725 break; 00726 } 00727 00728 fmt = 0; 00729 res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt); 00730 if (res < 0) { 00731 ast_log(LOG_WARNING, "Failed to set audio device to mono\n"); 00732 return -1; 00733 } 00734 fmt = desired = DEFAULT_SAMPLE_RATE; /* 8000 Hz desired */ 00735 res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt); 00736 00737 if (res < 0) { 00738 ast_log(LOG_WARNING, "Failed to set audio device to mono\n"); 00739 return -1; 00740 } 00741 if (fmt != desired) { 00742 if (!(o->warned & WARN_speed)) { 00743 ast_log(LOG_WARNING, 00744 "Requested %d Hz, got %d Hz -- sound may be choppy\n", 00745 desired, fmt); 00746 o->warned |= WARN_speed; 00747 } 00748 } 00749 /* 00750 * on Freebsd, SETFRAGMENT does not work very well on some cards. 00751 * Default to use 256 bytes, let the user override 00752 */ 00753 if (o->frags) { 00754 fmt = o->frags; 00755 res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt); 00756 if (res < 0) { 00757 if (!(o->warned & WARN_frag)) { 00758 ast_log(LOG_WARNING, 00759 "Unable to set fragment size -- sound may be choppy\n"); 00760 o->warned |= WARN_frag; 00761 } 00762 } 00763 } 00764 /* on some cards, we need SNDCTL_DSP_SETTRIGGER to start outputting */ 00765 res = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT; 00766 res = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &res); 00767 /* it may fail if we are in half duplex, never mind */ 00768 return 0; 00769 } 00770 00771 /* 00772 * some of the standard methods supported by channels. 00773 */ 00774 static int oss_digit_begin(struct ast_channel *c, char digit) 00775 { 00776 return 0; 00777 } 00778 00779 static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration) 00780 { 00781 /* no better use for received digits than print them */ 00782 ast_verbose(" << Console Received digit %c of duration %u ms >> \n", 00783 digit, duration); 00784 return 0; 00785 } 00786 00787 static int oss_text(struct ast_channel *c, const char *text) 00788 { 00789 /* print received messages */ 00790 ast_verbose(" << Console Received text %s >> \n", text); 00791 return 0; 00792 } 00793 00794 /*! \brief Play ringtone 'x' on device 'o' */ 00795 static void ring(struct chan_oss_pvt *o, int x) 00796 { 00797 write(o->sndcmd[1], &x, sizeof(x)); 00798 } 00799 00800 00801 /*! 00802 * \brief handler for incoming calls. Either autoanswer, or start ringing 00803 */ 00804 static int oss_call(struct ast_channel *c, char *dest, int timeout) 00805 { 00806 struct chan_oss_pvt *o = c->tech_pvt; 00807 struct ast_frame f = { 0, }; 00808 AST_DECLARE_APP_ARGS(args, 00809 AST_APP_ARG(name); 00810 AST_APP_ARG(flags); 00811 ); 00812 char *parse = ast_strdupa(dest); 00813 00814 AST_NONSTANDARD_APP_ARGS(args, parse, '/'); 00815 00816 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); 00817 if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "answer") == 0) { 00818 f.frametype = AST_FRAME_CONTROL; 00819 f.subclass = AST_CONTROL_ANSWER; 00820 ast_queue_frame(c, &f); 00821 } else if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "noanswer") == 0) { 00822 f.frametype = AST_FRAME_CONTROL; 00823 f.subclass = AST_CONTROL_RINGING; 00824 ast_queue_frame(c, &f); 00825 ring(o, AST_CONTROL_RING); 00826 } else if (o->autoanswer) { 00827 ast_verbose(" << Auto-answered >> \n"); 00828 f.frametype = AST_FRAME_CONTROL; 00829 f.subclass = AST_CONTROL_ANSWER; 00830 ast_queue_frame(c, &f); 00831 } else { 00832 ast_verbose("<< Type 'answer' to answer, or use 'autoanswer' for future calls >> \n"); 00833 f.frametype = AST_FRAME_CONTROL; 00834 f.subclass = AST_CONTROL_RINGING; 00835 ast_queue_frame(c, &f); 00836 ring(o, AST_CONTROL_RING); 00837 } 00838 return 0; 00839 } 00840 00841 /*! 00842 * \brief remote side answered the phone 00843 */ 00844 static int oss_answer(struct ast_channel *c) 00845 { 00846 struct chan_oss_pvt *o = c->tech_pvt; 00847 00848 ast_verbose(" << Console call has been answered >> \n"); 00849 #if 0 00850 /* play an answer tone (XXX do we really need it ?) */ 00851 ring(o, AST_CONTROL_ANSWER); 00852 #endif 00853 ast_setstate(c, AST_STATE_UP); 00854 o->cursound = -1; 00855 o->nosound = 0; 00856 return 0; 00857 } 00858 00859 static int oss_hangup(struct ast_channel *c) 00860 { 00861 struct chan_oss_pvt *o = c->tech_pvt; 00862 00863 o->cursound = -1; 00864 o->nosound = 0; 00865 c->tech_pvt = NULL; 00866 o->owner = NULL; 00867 ast_verbose(" << Hangup on console >> \n"); 00868 ast_module_unref(ast_module_info->self); 00869 if (o->hookstate) { 00870 if (o->autoanswer || o->autohangup) { 00871 /* Assume auto-hangup too */ 00872 o->hookstate = 0; 00873 setformat(o, O_CLOSE); 00874 } else { 00875 /* Make congestion noise */ 00876 ring(o, AST_CONTROL_CONGESTION); 00877 } 00878 } 00879 return 0; 00880 } 00881 00882 /*! \brief used for data coming from the network */ 00883 static int oss_write(struct ast_channel *c, struct ast_frame *f) 00884 { 00885 int src; 00886 struct chan_oss_pvt *o = c->tech_pvt; 00887 00888 /* Immediately return if no sound is enabled */ 00889 if (o->nosound) 00890 return 0; 00891 /* Stop any currently playing sound */ 00892 o->cursound = -1; 00893 /* 00894 * we could receive a block which is not a multiple of our 00895 * FRAME_SIZE, so buffer it locally and write to the device 00896 * in FRAME_SIZE chunks. 00897 * Keep the residue stored for future use. 00898 */ 00899 src = 0; /* read position into f->data */ 00900 while (src < f->datalen) { 00901 /* Compute spare room in the buffer */ 00902 int l = sizeof(o->oss_write_buf) - o->oss_write_dst; 00903 00904 if (f->datalen - src >= l) { /* enough to fill a frame */ 00905 memcpy(o->oss_write_buf + o->oss_write_dst, f->data + src, l); 00906 soundcard_writeframe(o, (short *) o->oss_write_buf); 00907 src += l; 00908 o->oss_write_dst = 0; 00909 } else { /* copy residue */ 00910 l = f->datalen - src; 00911 memcpy(o->oss_write_buf + o->oss_write_dst, f->data + src, l); 00912 src += l; /* but really, we are done */ 00913 o->oss_write_dst += l; 00914 } 00915 } 00916 return 0; 00917 } 00918 00919 static struct ast_frame *oss_read(struct ast_channel *c) 00920 { 00921 int res; 00922 struct chan_oss_pvt *o = c->tech_pvt; 00923 struct ast_frame *f = &o->read_f; 00924 00925 /* XXX can be simplified returning &ast_null_frame */ 00926 /* prepare a NULL frame in case we don't have enough data to return */ 00927 bzero(f, sizeof(struct ast_frame)); 00928 f->frametype = AST_FRAME_NULL; 00929 f->src = oss_tech.type; 00930 00931 res = read(o->sounddev, o->oss_read_buf + o->readpos, sizeof(o->oss_read_buf) - o->readpos); 00932 if (res < 0) /* audio data not ready, return a NULL frame */ 00933 return f; 00934 00935 o->readpos += res; 00936 if (o->readpos < sizeof(o->oss_read_buf)) /* not enough samples */ 00937 return f; 00938 00939 if (o->mute) 00940 return f; 00941 00942 o->readpos = AST_FRIENDLY_OFFSET; /* reset read pointer for next frame */ 00943 if (c->_state != AST_STATE_UP) /* drop data if frame is not up */ 00944 return f; 00945 /* ok we can build and deliver the frame to the caller */ 00946 f->frametype = AST_FRAME_VOICE; 00947 f->subclass = AST_FORMAT_SLINEAR; 00948 f->samples = FRAME_SIZE; 00949 f->datalen = FRAME_SIZE * 2; 00950 f->data = o->oss_read_buf + AST_FRIENDLY_OFFSET; 00951 if (o->boost != BOOST_SCALE) { /* scale and clip values */ 00952 int i, x; 00953 int16_t *p = (int16_t *) f->data; 00954 for (i = 0; i < f->samples; i++) { 00955 x = (p[i] * o->boost) / BOOST_SCALE; 00956 if (x > 32767) 00957 x = 32767; 00958 else if (x < -32768) 00959 x = -32768; 00960 p[i] = x; 00961 } 00962 } 00963 00964 f->offset = AST_FRIENDLY_OFFSET; 00965 return f; 00966 } 00967 00968 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan) 00969 { 00970 struct chan_oss_pvt *o = newchan->tech_pvt; 00971 o->owner = newchan; 00972 return 0; 00973 } 00974 00975 static int oss_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen) 00976 { 00977 struct chan_oss_pvt *o = c->tech_pvt; 00978 int res = -1; 00979 00980 switch (cond) { 00981 case AST_CONTROL_BUSY: 00982 case AST_CONTROL_CONGESTION: 00983 case AST_CONTROL_RINGING: 00984 res = cond; 00985 break; 00986 00987 case -1: 00988 o->cursound = -1; 00989 o->nosound = 0; /* when cursound is -1 nosound must be 0 */ 00990 return 0; 00991 00992 case AST_CONTROL_VIDUPDATE: 00993 res = -1; 00994 break; 00995 00996 case AST_CONTROL_HOLD: 00997 ast_verbose(" << Console Has Been Placed on Hold >> \n"); 00998 ast_moh_start(c, data, o->mohinterpret); 00999 break; 01000 01001 case AST_CONTROL_UNHOLD: 01002 ast_verbose(" << Console Has Been Retrieved from Hold >> \n"); 01003 ast_moh_stop(c); 01004 break; 01005 01006 default: 01007 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name); 01008 return -1; 01009 } 01010 01011 if (res > -1) 01012 ring(o, res); 01013 01014 return 0; 01015 } 01016 01017 /*! 01018 * \brief allocate a new channel. 01019 */ 01020 static struct ast_channel *oss_new(struct chan_oss_pvt *o, char *ext, char *ctx, int state) 01021 { 01022 struct ast_channel *c; 01023 01024 c = ast_channel_alloc(1, state, o->cid_num, o->cid_name, "OSS/%s", o->device + 5); 01025 if (c == NULL) 01026 return NULL; 01027 c->tech = &oss_tech; 01028 if (o->sounddev < 0) 01029 setformat(o, O_RDWR); 01030 c->fds[0] = o->sounddev; /* -1 if device closed, override later */ 01031 c->nativeformats = AST_FORMAT_SLINEAR; 01032 c->readformat = AST_FORMAT_SLINEAR; 01033 c->writeformat = AST_FORMAT_SLINEAR; 01034 c->tech_pvt = o; 01035 01036 if (!ast_strlen_zero(ctx)) 01037 ast_copy_string(c->context, ctx, sizeof(c->context)); 01038 if (!ast_strlen_zero(ext)) 01039 ast_copy_string(c->exten, ext, sizeof(c->exten)); 01040 if (!ast_strlen_zero(o->language)) 01041 ast_string_field_set(c, language, o->language); 01042 /* Don't use ast_set_callerid() here because it will 01043 * generate a needless NewCallerID event */ 01044 c->cid.cid_num = ast_strdup(o->cid_num); 01045 c->cid.cid_ani = ast_strdup(o->cid_num); 01046 c->cid.cid_name = ast_strdup(o->cid_name); 01047 if (!ast_strlen_zero(ext)) 01048 c->cid.cid_dnid = ast_strdup(ext); 01049 01050 o->owner = c; 01051 ast_module_ref(ast_module_info->self); 01052 ast_jb_configure(c, &global_jbconf); 01053 if (state != AST_STATE_DOWN) { 01054 if (ast_pbx_start(c)) { 01055 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", c->name); 01056 ast_hangup(c); 01057 o->owner = c = NULL; 01058 /* XXX what about the channel itself ? */ 01059 } 01060 } 01061 01062 return c; 01063 } 01064 01065 static struct ast_channel *oss_request(const char *type, int format, void *data, int *cause) 01066 { 01067 struct ast_channel *c; 01068 struct chan_oss_pvt *o; 01069 AST_DECLARE_APP_ARGS(args, 01070 AST_APP_ARG(name); 01071 AST_APP_ARG(flags); 01072 ); 01073 char *parse = ast_strdupa(data); 01074 01075 AST_NONSTANDARD_APP_ARGS(args, parse, '/'); 01076 o = find_desc(args.name); 01077 01078 ast_log(LOG_WARNING, "oss_request ty <%s> data 0x%p <%s>\n", type, data, (char *) data); 01079 if (o == NULL) { 01080 ast_log(LOG_NOTICE, "Device %s not found\n", args.name); 01081 /* XXX we could default to 'dsp' perhaps ? */ 01082 return NULL; 01083 } 01084 if ((format & AST_FORMAT_SLINEAR) == 0) { 01085 ast_log(LOG_NOTICE, "Format 0x%x unsupported\n", format); 01086 return NULL; 01087 } 01088 if (o->owner) { 01089 ast_log(LOG_NOTICE, "Already have a call (chan %p) on the OSS channel\n", o->owner); 01090 *cause = AST_CAUSE_BUSY; 01091 return NULL; 01092 } 01093 c = oss_new(o, NULL, NULL, AST_STATE_DOWN); 01094 if (c == NULL) { 01095 ast_log(LOG_WARNING, "Unable to create new OSS channel\n"); 01096 return NULL; 01097 } 01098 return c; 01099 } 01100 01101 static char *console_autoanswer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) 01102 { 01103 struct chan_oss_pvt *o = find_desc(oss_active); 01104 01105 switch (cmd) { 01106 case CLI_INIT: 01107 e->command = "console autoanswer [on|off]"; 01108 e->usage = 01109 "Usage: console autoanswer [on|off]\n" 01110 " Enables or disables autoanswer feature. If used without\n" 01111 " argument, displays the current on/off status of autoanswer.\n" 01112 " The default value of autoanswer is in 'oss.conf'.\n"; 01113 return NULL; 01114 01115 case CLI_GENERATE: 01116 return NULL; 01117 } 01118 01119 if (a->argc == e->args - 1) { 01120 ast_cli(a->fd, "Auto answer is %s.\n", o->autoanswer ? "on" : "off"); 01121 return CLI_SUCCESS; 01122 } 01123 if (a->argc != e->args) 01124 return CLI_SHOWUSAGE; 01125 if (o == NULL) { 01126 ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n", 01127 oss_active); 01128 return CLI_FAILURE; 01129 } 01130 if (!strcasecmp(a->argv[e->args-1], "on")) 01131 o->autoanswer = 1; 01132 else if (!strcasecmp(a->argv[e->args - 1], "off")) 01133 o->autoanswer = 0; 01134 else 01135 return CLI_SHOWUSAGE; 01136 return CLI_SUCCESS; 01137 } 01138 01139 /*! 01140 * \brief answer command from the console 01141 */ 01142 static char *console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) 01143 { 01144 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER }; 01145 struct chan_oss_pvt *o = find_desc(oss_active); 01146 01147 switch (cmd) { 01148 case CLI_INIT: 01149 e->command = "console answer"; 01150 e->usage = 01151 "Usage: console answer\n" 01152 " Answers an incoming call on the console (OSS) channel.\n"; 01153 return NULL; 01154 01155 case CLI_GENERATE: 01156 return NULL; /* no completion */ 01157 } 01158 if (a->argc != e->args) 01159 return CLI_SHOWUSAGE; 01160 if (!o->owner) { 01161 ast_cli(a->fd, "No one is calling us\n"); 01162 return CLI_FAILURE; 01163 } 01164 o->hookstate = 1; 01165 o->cursound = -1; 01166 o->nosound = 0; 01167 ast_queue_frame(o->owner, &f); 01168 return CLI_SUCCESS; 01169 } 01170 01171 /*! 01172 * \brief Console send text CLI command 01173 * 01174 * \note concatenate all arguments into a single string. argv is NULL-terminated 01175 * so we can use it right away 01176 */ 01177 static char *console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) 01178 { 01179 struct chan_oss_pvt *o = find_desc(oss_active); 01180 char buf[TEXT_SIZE]; 01181 01182 if (cmd == CLI_INIT) { 01183 e->command = "console send text"; 01184 e->usage = 01185 "Usage: console send text <message>\n" 01186 " Sends a text message for display on the remote terminal.\n"; 01187 return NULL; 01188 } else if (cmd == CLI_GENERATE) 01189 return NULL; 01190 01191 if (a->argc < e->args + 1) 01192 return CLI_SHOWUSAGE; 01193 if (!o->owner) { 01194 ast_cli(a->fd, "Not in a call\n"); 01195 return CLI_FAILURE; 01196 } 01197 ast_join(buf, sizeof(buf) - 1, a->argv + e->args); 01198 if (!ast_strlen_zero(buf)) { 01199 struct ast_frame f = { 0, }; 01200 int i = strlen(buf); 01201 buf[i] = '\n'; 01202 f.frametype = AST_FRAME_TEXT; 01203 f.subclass = 0; 01204 f.data = buf; 01205 f.datalen = i + 1; 01206 ast_queue_frame(o->owner, &f); 01207 } 01208 return CLI_SUCCESS; 01209 } 01210 01211 static char *console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) 01212 { 01213 struct chan_oss_pvt *o = find_desc(oss_active); 01214 01215 if (cmd == CLI_INIT) { 01216 e->command = "console hangup"; 01217 e->usage = 01218 "Usage: console hangup\n" 01219 " Hangs up any call currently placed on the console.\n"; 01220 return NULL; 01221 } else if (cmd == CLI_GENERATE) 01222 return NULL; 01223 01224 if (a->argc != e->args) 01225 return CLI_SHOWUSAGE; 01226 o->cursound = -1; 01227 o->nosound = 0; 01228 if (!o->owner && !o->hookstate) { /* XXX maybe only one ? */ 01229 ast_cli(a->fd, "No call to hang up\n"); 01230 return CLI_FAILURE; 01231 } 01232 o->hookstate = 0; 01233 if (o->owner) 01234 ast_queue_hangup(o->owner); 01235 setformat(o, O_CLOSE); 01236 return CLI_SUCCESS; 01237 } 01238 01239 static char *console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) 01240 { 01241 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH }; 01242 struct chan_oss_pvt *o = find_desc(oss_active); 01243 01244 if (cmd == CLI_INIT) { 01245 e->command = "console flash"; 01246 e->usage = 01247 "Usage: console flash\n" 01248 " Flashes the call currently placed on the console.\n"; 01249 return NULL; 01250 } else if (cmd == CLI_GENERATE) 01251 return NULL; 01252 01253 if (a->argc != e->args) 01254 return CLI_SHOWUSAGE; 01255 o->cursound = -1; 01256 o->nosound = 0; /* when cursound is -1 nosound must be 0 */ 01257 if (!o->owner) { /* XXX maybe !o->hookstate too ? */ 01258 ast_cli(a->fd, "No call to flash\n"); 01259 return CLI_FAILURE; 01260 } 01261 o->hookstate = 0; 01262 if (o->owner) /* XXX must be true, right ? */ 01263 ast_queue_frame(o->owner, &f); 01264 return CLI_SUCCESS; 01265 } 01266 01267 static char *console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) 01268 { 01269 char *s = NULL, *mye = NULL, *myc = NULL; 01270 struct chan_oss_pvt *o = find_desc(oss_active); 01271 01272 if (cmd == CLI_INIT) { 01273 e->command = "console dial"; 01274 e->usage = 01275 "Usage: console dial [extension[@context]]\n" 01276 " Dials a given extension (and context if specified)\n"; 01277 return NULL; 01278 } else if (cmd == CLI_GENERATE) 01279 return NULL; 01280 01281 if (a->argc > e->args + 1) 01282 return CLI_SHOWUSAGE; 01283 if (o->owner) { /* already in a call */ 01284 int i; 01285 struct ast_frame f = { AST_FRAME_DTMF, 0 }; 01286 01287 if (a->argc == e->args) { /* argument is mandatory here */ 01288 ast_cli(a->fd, "Already in a call. You can only dial digits until you hangup.\n"); 01289 return CLI_FAILURE; 01290 } 01291 s = a->argv[e->args]; 01292 /* send the string one char at a time */ 01293 for (i = 0; i < strlen(s); i++) { 01294 f.subclass = s[i]; 01295 ast_queue_frame(o->owner, &f); 01296 } 01297 return CLI_SUCCESS; 01298 } 01299 /* if we have an argument split it into extension and context */ 01300 if (a->argc == e->args + 1) 01301 s = ast_ext_ctx(a->argv[e->args], &mye, &myc); 01302 /* supply default values if needed */ 01303 if (mye == NULL) 01304 mye = o->ext; 01305 if (myc == NULL) 01306 myc = o->ctx; 01307 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) { 01308 o->hookstate = 1; 01309 oss_new(o, mye, myc, AST_STATE_RINGING); 01310 } else 01311 ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc); 01312 if (s) 01313 free(s); 01314 return CLI_SUCCESS; 01315 } 01316 01317 static char *console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) 01318 { 01319 struct chan_oss_pvt *o = find_desc(oss_active); 01320 char *s; 01321 01322 if (cmd == CLI_INIT) { 01323 e->command = "console {mute|unmute}"; 01324 e->usage = 01325 "Usage: console {mute|unmute}\n" 01326 " Mute/unmute the microphone.\n"; 01327 return NULL; 01328 } else if (cmd == CLI_GENERATE) 01329 return NULL; 01330 01331 if (a->argc != e->args) 01332 return CLI_SHOWUSAGE; 01333 s = a->argv[e->args-1]; 01334 if (!strcasecmp(s, "mute")) 01335 o->mute = 1; 01336 else if (!strcasecmp(s, "unmute")) 01337 o->mute = 0; 01338 else 01339 return CLI_SHOWUSAGE; 01340 return CLI_SUCCESS; 01341 } 01342 01343 static int console_transfer(int fd, int argc, char *argv[]) 01344 { 01345 struct chan_oss_pvt *o = find_desc(oss_active); 01346 struct ast_channel *b = NULL; 01347 char *tmp, *ext, *ctx; 01348 01349 if (argc != 3) 01350 return RESULT_SHOWUSAGE; 01351 if (o == NULL) 01352 return RESULT_FAILURE; 01353 if (o->owner == NULL || (b = ast_bridged_channel(o->owner)) == NULL) { 01354 ast_cli(fd, "There is no call to transfer\n"); 01355 return RESULT_SUCCESS; 01356 } 01357 01358 tmp = ast_ext_ctx(argv[2], &ext, &ctx); 01359 if (ctx == NULL) /* supply default context if needed */ 01360 ctx = o->owner->context; 01361 if (!ast_exists_extension(b, ctx, ext, 1, b->cid.cid_num)) 01362 ast_cli(fd, "No such extension exists\n"); 01363 else { 01364 ast_cli(fd, "Whee, transferring %s to %s@%s.\n", b->name, ext, ctx); 01365 if (ast_async_goto(b, ctx, ext, 1)) 01366 ast_cli(fd, "Failed to transfer :(\n"); 01367 } 01368 if (tmp) 01369 free(tmp); 01370 return RESULT_SUCCESS; 01371 } 01372 01373 static const char transfer_usage[] = 01374 "Usage: console transfer <extension>[@context]\n" 01375 " Transfers the currently connected call to the given extension (and\n" 01376 "context if specified)\n"; 01377 01378 static int console_active(int fd, int argc, char *argv[]) 01379 { 01380 if (argc == 2) 01381 ast_cli(fd, "active console is [%s]\n", oss_active); 01382 else if (argc != 3) 01383 return RESULT_SHOWUSAGE; 01384 else { 01385 struct chan_oss_pvt *o; 01386 if (strcmp(argv[2], "show") == 0) { 01387 for (o = oss_default.next; o; o = o->next) 01388 ast_cli(fd, "device [%s] exists\n", o->name); 01389 return RESULT_SUCCESS; 01390 } 01391 o = find_desc(argv[2]); 01392 if (o == NULL) 01393 ast_cli(fd, "No device [%s] exists\n", argv[2]); 01394 else 01395 oss_active = o->name; 01396 } 01397 return RESULT_SUCCESS; 01398 } 01399 01400 static const char active_usage[] = 01401 "Usage: console active [device]\n" 01402 " If used without a parameter, displays which device is the current\n" 01403 "console. If a device is specified, the console sound device is changed to\n" 01404 "the device specified.\n"; 01405 01406 /*! 01407 * \brief store the boost factor 01408 */ 01409 static void store_boost(struct chan_oss_pvt *o, char *s) 01410 { 01411 double boost = 0; 01412 if (sscanf(s, "%lf", &boost) != 1) { 01413 ast_log(LOG_WARNING, "invalid boost <%s>\n", s); 01414 return; 01415 } 01416 if (boost < -BOOST_MAX) { 01417 ast_log(LOG_WARNING, "boost %s too small, using %d\n", s, -BOOST_MAX); 01418 boost = -BOOST_MAX; 01419 } else if (boost > BOOST_MAX) { 01420 ast_log(LOG_WARNING, "boost %s too large, using %d\n", s, BOOST_MAX); 01421 boost = BOOST_MAX; 01422 } 01423 boost = exp(log(10) * boost / 20) * BOOST_SCALE; 01424 o->boost = boost; 01425 ast_log(LOG_WARNING, "setting boost %s to %d\n", s, o->boost); 01426 } 01427 01428 static int do_boost(int fd, int argc, char *argv[]) 01429 { 01430 struct chan_oss_pvt *o = find_desc(oss_active); 01431 01432 if (argc == 2) 01433 ast_cli(fd, "boost currently %5.1f\n", 20 * log10(((double) o->boost / (double) BOOST_SCALE))); 01434 else if (argc == 3) 01435 store_boost(o, argv[2]); 01436 return RESULT_SUCCESS; 01437 } 01438 01439 static struct ast_cli_entry cli_oss[] = { 01440 NEW_CLI(console_answer, "Answer an incoming console call"), 01441 NEW_CLI(console_hangup, "Hangup a call on the console"), 01442 NEW_CLI(console_flash, "Flash a call on the console"), 01443 NEW_CLI(console_dial, "Dial an extension on the console"), 01444 NEW_CLI(console_mute, "Disable/Enable mic input"), 01445 { { "console", "transfer", NULL }, 01446 console_transfer, "Transfer a call to a different extension", 01447 transfer_usage }, 01448 01449 NEW_CLI(console_sendtext, "Send text to the remote device"), 01450 NEW_CLI(console_autoanswer, "Sets/displays autoanswer"), 01451 01452 { { "console", "boost", NULL }, 01453 do_boost, "Sets/displays mic boost in dB", 01454 NULL }, 01455 01456 { { "console", "active", NULL }, 01457 console_active, "Sets/displays active console", 01458 active_usage }, 01459 }; 01460 01461 /*! 01462 * store the mixer argument from the config file, filtering possibly 01463 * invalid or dangerous values (the string is used as argument for 01464 * system("mixer %s") 01465 */ 01466 static void store_mixer(struct chan_oss_pvt *o, char *s) 01467 { 01468 int i; 01469 01470 for (i = 0; i < strlen(s); i++) { 01471 if (!isalnum(s[i]) && index(" \t-/", s[i]) == NULL) { 01472 ast_log(LOG_WARNING, "Suspect char %c in mixer cmd, ignoring:\n\t%s\n", s[i], s); 01473 return; 01474 } 01475 } 01476 if (o->mixer_cmd) 01477 free(o->mixer_cmd); 01478 o->mixer_cmd = ast_strdup(s); 01479 ast_log(LOG_WARNING, "setting mixer %s\n", s); 01480 } 01481 01482 /*! 01483 * store the callerid components 01484 */ 01485 static void store_callerid(struct chan_oss_pvt *o, char *s) 01486 { 01487 ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num)); 01488 } 01489 01490 /*! 01491 * grab fields from the config file, init the descriptor and open the device. 01492 */ 01493 static struct chan_oss_pvt *store_config(struct ast_config *cfg, char *ctg) 01494 { 01495 struct ast_variable *v; 01496 struct chan_oss_pvt *o; 01497 01498 if (ctg == NULL) { 01499 o = &oss_default; 01500 ctg = "general"; 01501 } else { 01502 if (!(o = ast_calloc(1, sizeof(*o)))) 01503 return NULL; 01504 *o = oss_default; 01505 /* "general" is also the default thing */ 01506 if (strcmp(ctg, "general") == 0) { 01507 o->name = ast_strdup("dsp"); 01508 oss_active = o->name; 01509 goto openit; 01510 } 01511 o->name = ast_strdup(ctg); 01512 } 01513 01514 strcpy(o->mohinterpret, "default"); 01515 01516 o->lastopen = ast_tvnow(); /* don't leave it 0 or tvdiff may wrap */ 01517 /* fill other fields from configuration */ 01518 for (v = ast_variable_browse(cfg, ctg); v; v = v->next) { 01519 M_START(v->name, v->value); 01520 01521 /* handle jb conf */ 01522 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) 01523 continue; 01524 01525 M_BOOL("autoanswer", o->autoanswer) 01526 M_BOOL("autohangup", o->autohangup) 01527 M_BOOL("overridecontext", o->overridecontext) 01528 M_STR("device", o->device) 01529 M_UINT("frags", o->frags) 01530 M_UINT("debug", oss_debug) 01531 M_UINT("queuesize", o->queuesize) 01532 M_STR("context", o->ctx) 01533 M_STR("language", o->language) 01534 M_STR("mohinterpret", o->mohinterpret) 01535 M_STR("extension", o->ext) 01536 M_F("mixer", store_mixer(o, v->value)) 01537 M_F("callerid", store_callerid(o, v->value)) 01538 M_F("boost", store_boost(o, v->value)) 01539 01540 M_END(/* */); 01541 } 01542 if (ast_strlen_zero(o->device)) 01543 ast_copy_string(o->device, DEV_DSP, sizeof(o->device)); 01544 if (o->mixer_cmd) { 01545 char *cmd; 01546 01547 asprintf(&cmd, "mixer %s", o->mixer_cmd); 01548 ast_log(LOG_WARNING, "running [%s]\n", cmd); 01549 system(cmd); 01550 free(cmd); 01551 } 01552 if (o == &oss_default) /* we are done with the default */ 01553 return NULL; 01554 01555 openit: 01556 #if TRYOPEN 01557 if (setformat(o, O_RDWR) < 0) { /* open device */ 01558 if (option_verbose > 0) { 01559 ast_verbose(VERBOSE_PREFIX_2 "Device %s not detected\n", ctg); 01560 ast_verbose(VERBOSE_PREFIX_2 "Turn off OSS support by adding " "'noload=chan_oss.so' in /etc/asterisk/modules.conf\n"); 01561 } 01562 goto error; 01563 } 01564 if (o->duplex != M_FULL) 01565 ast_log(LOG_WARNING, "XXX I don't work right with non " "full-duplex sound cards XXX\n"); 01566 #endif /* TRYOPEN */ 01567 if (pipe(o->sndcmd) != 0) { 01568 ast_log(LOG_ERROR, "Unable to create pipe\n"); 01569 goto error; 01570 } 01571 ast_pthread_create_background(&o->sthread, NULL, sound_thread, o); 01572 /* link into list of devices */ 01573 if (o != &oss_default) { 01574 o->next = oss_default.next; 01575 oss_default.next = o; 01576 } 01577 return o; 01578 01579 error: 01580 if (o != &oss_default) 01581 free(o); 01582 return NULL; 01583 } 01584 01585 static int load_module(void) 01586 { 01587 struct ast_config *cfg = NULL; 01588 char *ctg = NULL; 01589 01590 /* Copy the default jb config over global_jbconf */ 01591 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf)); 01592 01593 /* load config file */ 01594 if (!(cfg = ast_config_load(config))) { 01595 ast_log(LOG_NOTICE, "Unable to load config %s\n", config); 01596 return AST_MODULE_LOAD_DECLINE; 01597 } 01598 01599 do { 01600 store_config(cfg, ctg); 01601 } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL); 01602 01603 ast_config_destroy(cfg); 01604 01605 if (find_desc(oss_active) == NULL) { 01606 ast_log(LOG_NOTICE, "Device %s not found\n", oss_active); 01607 /* XXX we could default to 'dsp' perhaps ? */ 01608 /* XXX should cleanup allocated memory etc. */ 01609 return AST_MODULE_LOAD_FAILURE; 01610 } 01611 01612 if (ast_channel_register(&oss_tech)) { 01613 ast_log(LOG_ERROR, "Unable to register channel type 'OSS'\n"); 01614 return AST_MODULE_LOAD_FAILURE; 01615 } 01616 01617 ast_cli_register_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry)); 01618 01619 return AST_MODULE_LOAD_SUCCESS; 01620 } 01621 01622 01623 static int unload_module(void) 01624 { 01625 struct chan_oss_pvt *o; 01626 01627 ast_channel_unregister(&oss_tech); 01628 ast_cli_unregister_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry)); 01629 01630 for (o = oss_default.next; o; o = o->next) { 01631 close(o->sounddev); 01632 if (o->sndcmd[0] > 0) { 01633 close(o->sndcmd[0]); 01634 close(o->sndcmd[1]); 01635 } 01636 if (o->owner) 01637 ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD); 01638 if (o->owner) /* XXX how ??? */ 01639 return -1; 01640 /* XXX what about the thread ? */ 01641 /* XXX what about the memory allocated ? */ 01642 } 01643 return 0; 01644 } 01645 01646 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "OSS Console Channel Driver");