00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "asterisk.h"
00040
00041 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 79174 $")
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
00079
00080 #include "busy.h"
00081 #include "ringtone.h"
00082 #include "ring10.h"
00083 #include "answer.h"
00084
00085
00086 static struct ast_jb_conf default_jbconf =
00087 {
00088 .flags = 0,
00089 .max_size = -1,
00090 .resync_threshold = -1,
00091 .impl = "",
00092 };
00093 static struct ast_jb_conf global_jbconf;
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213 #define M_START(var, val) \
00214 char *__s = var; char *__val = val;
00215 #define M_END(x) x;
00216 #define M_F(tag, f) if (!strcasecmp((__s), tag)) { f; } else
00217 #define M_BOOL(tag, dst) M_F(tag, (dst) = ast_true(__val) )
00218 #define M_UINT(tag, dst) M_F(tag, (dst) = strtoul(__val, NULL, 0) )
00219 #define M_STR(tag, dst) M_F(tag, ast_copy_string(dst, __val, sizeof(dst)))
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251 #define FRAME_SIZE 160
00252 #define QUEUE_SIZE 10
00253
00254 #if defined(__FreeBSD__)
00255 #define FRAGS 0x8
00256 #else
00257 #define FRAGS ( ( (6 * 5) << 16 ) | 0x6 )
00258 #endif
00259
00260
00261
00262
00263
00264 #define TEXT_SIZE 256
00265
00266 #if 0
00267 #define TRYOPEN 1
00268 #endif
00269 #define O_CLOSE 0x444
00270
00271 #if defined( __OpenBSD__ ) || defined( __NetBSD__ )
00272 #define DEV_DSP "/dev/audio"
00273 #else
00274 #define DEV_DSP "/dev/dsp"
00275 #endif
00276
00277 #ifndef MIN
00278 #define MIN(a,b) ((a) < (b) ? (a) : (b))
00279 #endif
00280 #ifndef MAX
00281 #define MAX(a,b) ((a) > (b) ? (a) : (b))
00282 #endif
00283
00284 static char *config = "oss.conf";
00285
00286 static int oss_debug;
00287
00288
00289
00290
00291
00292
00293 struct sound {
00294 int ind;
00295 char *desc;
00296 short *data;
00297 int datalen;
00298 int samplen;
00299 int silencelen;
00300 int repeat;
00301 };
00302
00303 static struct sound sounds[] = {
00304 { AST_CONTROL_RINGING, "RINGING", ringtone, sizeof(ringtone)/2, 16000, 32000, 1 },
00305 { AST_CONTROL_BUSY, "BUSY", busy, sizeof(busy)/2, 4000, 4000, 1 },
00306 { AST_CONTROL_CONGESTION, "CONGESTION", busy, sizeof(busy)/2, 2000, 2000, 1 },
00307 { AST_CONTROL_RING, "RING10", ring10, sizeof(ring10)/2, 16000, 32000, 1 },
00308 { AST_CONTROL_ANSWER, "ANSWER", answer, sizeof(answer)/2, 2200, 0, 0 },
00309 { -1, NULL, 0, 0, 0, 0 },
00310 };
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320 struct chan_oss_pvt {
00321 struct chan_oss_pvt *next;
00322
00323 char *name;
00324
00325
00326
00327
00328
00329
00330
00331 int sndcmd[2];
00332 int cursound;
00333 int sampsent;
00334 int nosound;
00335
00336 int total_blocks;
00337 int sounddev;
00338 enum { M_UNSET, M_FULL, M_READ, M_WRITE } duplex;
00339 int autoanswer;
00340 int autohangup;
00341 int hookstate;
00342 char *mixer_cmd;
00343 unsigned int queuesize;
00344 unsigned int frags;
00345
00346 int warned;
00347 #define WARN_used_blocks 1
00348 #define WARN_speed 2
00349 #define WARN_frag 4
00350 int w_errors;
00351 struct timeval lastopen;
00352
00353 int overridecontext;
00354 int mute;
00355
00356
00357
00358
00359 #define BOOST_SCALE (1<<9)
00360 #define BOOST_MAX 40
00361 int boost;
00362 char device[64];
00363
00364 pthread_t sthread;
00365
00366 struct ast_channel *owner;
00367 char ext[AST_MAX_EXTENSION];
00368 char ctx[AST_MAX_CONTEXT];
00369 char language[MAX_LANGUAGE];
00370 char cid_name[256];
00371 char cid_num[256];
00372 char mohinterpret[MAX_MUSICCLASS];
00373
00374
00375 char oss_write_buf[FRAME_SIZE * 2];
00376 int oss_write_dst;
00377
00378
00379
00380 char oss_read_buf[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
00381 int readpos;
00382 struct ast_frame read_f;
00383 };
00384
00385 static struct chan_oss_pvt oss_default = {
00386 .cursound = -1,
00387 .sounddev = -1,
00388 .duplex = M_UNSET,
00389 .autoanswer = 1,
00390 .autohangup = 1,
00391 .queuesize = QUEUE_SIZE,
00392 .frags = FRAGS,
00393 .ext = "s",
00394 .ctx = "default",
00395 .readpos = AST_FRIENDLY_OFFSET,
00396 .lastopen = { 0, 0 },
00397 .boost = BOOST_SCALE,
00398 };
00399
00400 static char *oss_active;
00401
00402 static int setformat(struct chan_oss_pvt *o, int mode);
00403
00404 static struct ast_channel *oss_request(const char *type, int format, void *data
00405 , int *cause);
00406 static int oss_digit_begin(struct ast_channel *c, char digit);
00407 static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration);
00408 static int oss_text(struct ast_channel *c, const char *dest, const char *text, int ispdu);
00409 static int oss_hangup(struct ast_channel *c);
00410 static int oss_answer(struct ast_channel *c);
00411 static struct ast_frame *oss_read(struct ast_channel *chan);
00412 static int oss_call(struct ast_channel *c, char *dest, int timeout);
00413 static int oss_write(struct ast_channel *chan, struct ast_frame *f);
00414 static int oss_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
00415 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
00416 static char tdesc[] = "OSS Console Channel Driver";
00417
00418 static const struct ast_channel_tech oss_tech = {
00419 .type = "Console",
00420 .description = tdesc,
00421 .capabilities = AST_FORMAT_SLINEAR,
00422 .requester = oss_request,
00423 .send_digit_begin = oss_digit_begin,
00424 .send_digit_end = oss_digit_end,
00425 .send_text = oss_text,
00426 .hangup = oss_hangup,
00427 .answer = oss_answer,
00428 .read = oss_read,
00429 .call = oss_call,
00430 .write = oss_write,
00431 .indicate = oss_indicate,
00432 .fixup = oss_fixup,
00433 };
00434
00435
00436
00437
00438 static struct chan_oss_pvt *find_desc(char *dev)
00439 {
00440 struct chan_oss_pvt *o = NULL;
00441
00442 if (!dev)
00443 ast_log(LOG_WARNING, "null dev\n");
00444
00445 for (o = oss_default.next; o && o->name && dev && strcmp(o->name, dev) != 0; o = o->next);
00446
00447 if (!o)
00448 ast_log(LOG_WARNING, "could not find <%s>\n", dev ? dev : "--no-device--");
00449
00450 return o;
00451 }
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 static char *ast_ext_ctx(const char *src, char **ext, char **ctx)
00463 {
00464 struct chan_oss_pvt *o = find_desc(oss_active);
00465
00466 if (ext == NULL || ctx == NULL)
00467 return NULL;
00468
00469 *ext = *ctx = NULL;
00470
00471 if (src && *src != '\0')
00472 *ext = ast_strdup(src);
00473
00474 if (*ext == NULL)
00475 return NULL;
00476
00477 if (!o->overridecontext) {
00478
00479 *ctx = strrchr(*ext, '@');
00480 if (*ctx)
00481 *(*ctx)++ = '\0';
00482 }
00483
00484 return *ext;
00485 }
00486
00487
00488
00489
00490 static int used_blocks(struct chan_oss_pvt *o)
00491 {
00492 struct audio_buf_info info;
00493
00494 if (ioctl(o->sounddev, SNDCTL_DSP_GETOSPACE, &info)) {
00495 if (!(o->warned & WARN_used_blocks)) {
00496 ast_log(LOG_WARNING, "Error reading output space\n");
00497 o->warned |= WARN_used_blocks;
00498 }
00499 return 1;
00500 }
00501
00502 if (o->total_blocks == 0) {
00503 if (0)
00504 ast_log(LOG_WARNING, "fragtotal %d size %d avail %d\n", info.fragstotal, info.fragsize, info.fragments);
00505 o->total_blocks = info.fragments;
00506 }
00507
00508 return o->total_blocks - info.fragments;
00509 }
00510
00511
00512 static int soundcard_writeframe(struct chan_oss_pvt *o, short *data)
00513 {
00514 int res;
00515
00516 if (o->sounddev < 0)
00517 setformat(o, O_RDWR);
00518 if (o->sounddev < 0)
00519 return 0;
00520
00521
00522
00523
00524
00525
00526 res = used_blocks(o);
00527 if (res > o->queuesize) {
00528 if (o->w_errors++ == 0 && (oss_debug & 0x4))
00529 ast_log(LOG_WARNING, "write: used %d blocks (%d)\n", res, o->w_errors);
00530 return 0;
00531 }
00532 o->w_errors = 0;
00533 return write(o->sounddev, ((void *) data), FRAME_SIZE * 2);
00534 }
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546 static void send_sound(struct chan_oss_pvt *o)
00547 {
00548 short myframe[FRAME_SIZE];
00549 int ofs, l, start;
00550 int l_sampsent = o->sampsent;
00551 struct sound *s;
00552
00553 if (o->cursound < 0)
00554 return;
00555
00556 s = &sounds[o->cursound];
00557
00558 for (ofs = 0; ofs < FRAME_SIZE; ofs += l) {
00559 l = s->samplen - l_sampsent;
00560 if (l > 0) {
00561 start = l_sampsent % s->datalen;
00562 if (l > FRAME_SIZE - ofs)
00563 l = FRAME_SIZE - ofs;
00564 if (l > s->datalen - start)
00565 l = s->datalen - start;
00566 bcopy(s->data + start, myframe + ofs, l * 2);
00567 if (0)
00568 ast_log(LOG_WARNING, "send_sound sound %d/%d of %d into %d\n", l_sampsent, l, s->samplen, ofs);
00569 l_sampsent += l;
00570 } else {
00571 static const short silence[FRAME_SIZE] = { 0, };
00572
00573 l += s->silencelen;
00574 if (l > 0) {
00575 if (l > FRAME_SIZE - ofs)
00576 l = FRAME_SIZE - ofs;
00577 bcopy(silence, myframe + ofs, l * 2);
00578 l_sampsent += l;
00579 } else {
00580 if (s->repeat == 0) {
00581 o->cursound = -1;
00582 o->nosound = 0;
00583 if (ofs < FRAME_SIZE)
00584 bcopy(silence, myframe + ofs, (FRAME_SIZE - ofs) * 2);
00585 }
00586 l_sampsent = 0;
00587 }
00588 }
00589 }
00590 l = soundcard_writeframe(o, myframe);
00591 if (l > 0)
00592 o->sampsent = l_sampsent;
00593 }
00594
00595 static void *sound_thread(void *arg)
00596 {
00597 char ign[4096];
00598 struct chan_oss_pvt *o = (struct chan_oss_pvt *) arg;
00599
00600
00601
00602
00603
00604 read(o->sounddev, ign, sizeof(ign));
00605 for (;;) {
00606 fd_set rfds, wfds;
00607 int maxfd, res;
00608
00609 FD_ZERO(&rfds);
00610 FD_ZERO(&wfds);
00611 FD_SET(o->sndcmd[0], &rfds);
00612 maxfd = o->sndcmd[0];
00613 if (o->cursound > -1 && o->sounddev < 0)
00614 setformat(o, O_RDWR);
00615 else if (o->cursound == -1 && o->owner == NULL)
00616 setformat(o, O_CLOSE);
00617 if (o->sounddev > -1) {
00618 if (!o->owner) {
00619 FD_SET(o->sounddev, &rfds);
00620 maxfd = MAX(o->sounddev, maxfd);
00621 }
00622 if (o->cursound > -1) {
00623 FD_SET(o->sounddev, &wfds);
00624 maxfd = MAX(o->sounddev, maxfd);
00625 }
00626 }
00627
00628 res = ast_select(maxfd + 1, &rfds, &wfds, NULL, NULL);
00629 if (res < 1) {
00630 ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
00631 sleep(1);
00632 continue;
00633 }
00634 if (FD_ISSET(o->sndcmd[0], &rfds)) {
00635
00636 int i, what = -1;
00637
00638 read(o->sndcmd[0], &what, sizeof(what));
00639 for (i = 0; sounds[i].ind != -1; i++) {
00640 if (sounds[i].ind == what) {
00641 o->cursound = i;
00642 o->sampsent = 0;
00643 o->nosound = 1;
00644 break;
00645 }
00646 }
00647 if (sounds[i].ind == -1)
00648 ast_log(LOG_WARNING, "invalid sound index: %d\n", what);
00649 }
00650 if (o->sounddev > -1) {
00651 if (FD_ISSET(o->sounddev, &rfds))
00652 read(o->sounddev, ign, sizeof(ign));
00653 if (FD_ISSET(o->sounddev, &wfds))
00654 send_sound(o);
00655 }
00656 }
00657 return NULL;
00658 }
00659
00660
00661
00662
00663
00664
00665 static int setformat(struct chan_oss_pvt *o, int mode)
00666 {
00667 int fmt, desired, res, fd;
00668
00669 if (o->sounddev >= 0) {
00670 ioctl(o->sounddev, SNDCTL_DSP_RESET, 0);
00671 close(o->sounddev);
00672 o->duplex = M_UNSET;
00673 o->sounddev = -1;
00674 }
00675 if (mode == O_CLOSE)
00676 return 0;
00677 if (ast_tvdiff_ms(ast_tvnow(), o->lastopen) < 1000)
00678 return -1;
00679 o->lastopen = ast_tvnow();
00680 fd = o->sounddev = open(o->device, mode | O_NONBLOCK);
00681 if (fd < 0) {
00682 ast_log(LOG_WARNING, "Unable to re-open DSP device %s: %s\n", o->device, strerror(errno));
00683 return -1;
00684 }
00685 if (o->owner)
00686 o->owner->fds[0] = fd;
00687
00688 #if __BYTE_ORDER == __LITTLE_ENDIAN
00689 fmt = AFMT_S16_LE;
00690 #else
00691 fmt = AFMT_S16_BE;
00692 #endif
00693 res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
00694 if (res < 0) {
00695 ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
00696 return -1;
00697 }
00698 switch (mode) {
00699 case O_RDWR:
00700 res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
00701
00702 res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt);
00703 if (res == 0 && (fmt & DSP_CAP_DUPLEX)) {
00704 if (option_verbose > 1)
00705 ast_verbose(VERBOSE_PREFIX_2 "Console is full duplex\n");
00706 o->duplex = M_FULL;
00707 };
00708 break;
00709 case O_WRONLY:
00710 o->duplex = M_WRITE;
00711 break;
00712 case O_RDONLY:
00713 o->duplex = M_READ;
00714 break;
00715 }
00716
00717 fmt = 0;
00718 res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
00719 if (res < 0) {
00720 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
00721 return -1;
00722 }
00723 fmt = desired = DEFAULT_SAMPLE_RATE;
00724 res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
00725
00726 if (res < 0) {
00727 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
00728 return -1;
00729 }
00730 if (fmt != desired) {
00731 if (!(o->warned & WARN_speed)) {
00732 ast_log(LOG_WARNING,
00733 "Requested %d Hz, got %d Hz -- sound may be choppy\n",
00734 desired, fmt);
00735 o->warned |= WARN_speed;
00736 }
00737 }
00738
00739
00740
00741
00742 if (o->frags) {
00743 fmt = o->frags;
00744 res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
00745 if (res < 0) {
00746 if (!(o->warned & WARN_frag)) {
00747 ast_log(LOG_WARNING,
00748 "Unable to set fragment size -- sound may be choppy\n");
00749 o->warned |= WARN_frag;
00750 }
00751 }
00752 }
00753
00754 res = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
00755 res = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &res);
00756
00757 return 0;
00758 }
00759
00760
00761
00762
00763 static int oss_digit_begin(struct ast_channel *c, char digit)
00764 {
00765 return 0;
00766 }
00767
00768 static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration)
00769 {
00770
00771 ast_verbose(" << Console Received digit %c of duration %u ms >> \n",
00772 digit, duration);
00773 return 0;
00774 }
00775
00776 static int oss_text(struct ast_channel *c, const char *dest, const char *text, int ispdu)
00777 {
00778
00779 ast_verbose(" << Console Received text %s >> \n", text);
00780 return 0;
00781 }
00782
00783
00784 static void ring(struct chan_oss_pvt *o, int x)
00785 {
00786 write(o->sndcmd[1], &x, sizeof(x));
00787 }
00788
00789
00790
00791
00792
00793 static int oss_call(struct ast_channel *c, char *dest, int timeout)
00794 {
00795 struct chan_oss_pvt *o = c->tech_pvt;
00796 struct ast_frame f = { 0, };
00797
00798 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);
00799 if (o->autoanswer) {
00800 ast_verbose(" << Auto-answered >> \n");
00801 f.frametype = AST_FRAME_CONTROL;
00802 f.subclass = AST_CONTROL_ANSWER;
00803 ast_queue_frame(c, &f);
00804 } else {
00805 ast_verbose("<< Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
00806 f.frametype = AST_FRAME_CONTROL;
00807 f.subclass = AST_CONTROL_RINGING;
00808 ast_queue_frame(c, &f);
00809 ring(o, AST_CONTROL_RING);
00810 }
00811 return 0;
00812 }
00813
00814
00815
00816
00817 static int oss_answer(struct ast_channel *c)
00818 {
00819 struct chan_oss_pvt *o = c->tech_pvt;
00820
00821 ast_verbose(" << Console call has been answered >> \n");
00822 #if 0
00823
00824 ring(o, AST_CONTROL_ANSWER);
00825 #endif
00826 ast_setstate(c, AST_STATE_UP);
00827 o->cursound = -1;
00828 o->nosound = 0;
00829 return 0;
00830 }
00831
00832 static int oss_hangup(struct ast_channel *c)
00833 {
00834 struct chan_oss_pvt *o = c->tech_pvt;
00835
00836 o->cursound = -1;
00837 o->nosound = 0;
00838 c->tech_pvt = NULL;
00839 o->owner = NULL;
00840 ast_verbose(" << Hangup on console >> \n");
00841 ast_module_unref(ast_module_info->self);
00842 if (o->hookstate) {
00843 if (o->autoanswer || o->autohangup) {
00844
00845 o->hookstate = 0;
00846 setformat(o, O_CLOSE);
00847 } else {
00848
00849 ring(o, AST_CONTROL_CONGESTION);
00850 }
00851 }
00852 return 0;
00853 }
00854
00855
00856 static int oss_write(struct ast_channel *c, struct ast_frame *f)
00857 {
00858 int src;
00859 struct chan_oss_pvt *o = c->tech_pvt;
00860
00861
00862 if (o->nosound)
00863 return 0;
00864
00865 o->cursound = -1;
00866
00867
00868
00869
00870
00871
00872 src = 0;
00873 while (src < f->datalen) {
00874
00875 int l = sizeof(o->oss_write_buf) - o->oss_write_dst;
00876
00877 if (f->datalen - src >= l) {
00878 memcpy(o->oss_write_buf + o->oss_write_dst, f->data + src, l);
00879 soundcard_writeframe(o, (short *) o->oss_write_buf);
00880 src += l;
00881 o->oss_write_dst = 0;
00882 } else {
00883 l = f->datalen - src;
00884 memcpy(o->oss_write_buf + o->oss_write_dst, f->data + src, l);
00885 src += l;
00886 o->oss_write_dst += l;
00887 }
00888 }
00889 return 0;
00890 }
00891
00892 static struct ast_frame *oss_read(struct ast_channel *c)
00893 {
00894 int res;
00895 struct chan_oss_pvt *o = c->tech_pvt;
00896 struct ast_frame *f = &o->read_f;
00897
00898
00899
00900 bzero(f, sizeof(struct ast_frame));
00901 f->frametype = AST_FRAME_NULL;
00902 f->src = oss_tech.type;
00903
00904 res = read(o->sounddev, o->oss_read_buf + o->readpos, sizeof(o->oss_read_buf) - o->readpos);
00905 if (res < 0)
00906 return f;
00907
00908 o->readpos += res;
00909 if (o->readpos < sizeof(o->oss_read_buf))
00910 return f;
00911
00912 if (o->mute)
00913 return f;
00914
00915 o->readpos = AST_FRIENDLY_OFFSET;
00916 if (c->_state != AST_STATE_UP)
00917 return f;
00918
00919 f->frametype = AST_FRAME_VOICE;
00920 f->subclass = AST_FORMAT_SLINEAR;
00921 f->samples = FRAME_SIZE;
00922 f->datalen = FRAME_SIZE * 2;
00923 f->data = o->oss_read_buf + AST_FRIENDLY_OFFSET;
00924 if (o->boost != BOOST_SCALE) {
00925 int i, x;
00926 int16_t *p = (int16_t *) f->data;
00927 for (i = 0; i < f->samples; i++) {
00928 x = (p[i] * o->boost) / BOOST_SCALE;
00929 if (x > 32767)
00930 x = 32767;
00931 else if (x < -32768)
00932 x = -32768;
00933 p[i] = x;
00934 }
00935 }
00936
00937 f->offset = AST_FRIENDLY_OFFSET;
00938 return f;
00939 }
00940
00941 static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
00942 {
00943 struct chan_oss_pvt *o = newchan->tech_pvt;
00944 o->owner = newchan;
00945 return 0;
00946 }
00947
00948 static int oss_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen)
00949 {
00950 struct chan_oss_pvt *o = c->tech_pvt;
00951 int res = -1;
00952
00953 switch (cond) {
00954 case AST_CONTROL_BUSY:
00955 case AST_CONTROL_CONGESTION:
00956 case AST_CONTROL_RINGING:
00957 res = cond;
00958 break;
00959
00960 case -1:
00961 o->cursound = -1;
00962 o->nosound = 0;
00963 return 0;
00964
00965 case AST_CONTROL_VIDUPDATE:
00966 res = -1;
00967 break;
00968 case AST_CONTROL_HOLD:
00969 ast_verbose(" << Console Has Been Placed on Hold >> \n");
00970 ast_moh_start(c, data, o->mohinterpret);
00971 break;
00972 case AST_CONTROL_UNHOLD:
00973 ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
00974 ast_moh_stop(c);
00975 break;
00976
00977 default:
00978 ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name);
00979 return -1;
00980 }
00981
00982 if (res > -1)
00983 ring(o, res);
00984
00985 return 0;
00986 }
00987
00988
00989
00990
00991 static struct ast_channel *oss_new(struct chan_oss_pvt *o, char *ext, char *ctx, int state)
00992 {
00993 struct ast_channel *c;
00994
00995 c = ast_channel_alloc(1, state, o->cid_num, o->cid_name, "", ext, ctx, 0, "OSS/%s", o->device + 5);
00996 if (c == NULL)
00997 return NULL;
00998 c->tech = &oss_tech;
00999 if (o->sounddev < 0)
01000 setformat(o, O_RDWR);
01001 c->fds[0] = o->sounddev;
01002 c->nativeformats = AST_FORMAT_SLINEAR;
01003 c->readformat = AST_FORMAT_SLINEAR;
01004 c->writeformat = AST_FORMAT_SLINEAR;
01005 c->tech_pvt = o;
01006
01007 if (!ast_strlen_zero(o->language))
01008 ast_string_field_set(c, language, o->language);
01009
01010
01011 c->cid.cid_ani = ast_strdup(o->cid_num);
01012 if (!ast_strlen_zero(ext))
01013 c->cid.cid_dnid = ast_strdup(ext);
01014
01015 o->owner = c;
01016 ast_module_ref(ast_module_info->self);
01017 ast_jb_configure(c, &global_jbconf);
01018 if (state != AST_STATE_DOWN) {
01019 if (ast_pbx_start(c)) {
01020 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", c->name);
01021 ast_hangup(c);
01022 o->owner = c = NULL;
01023
01024
01025 }
01026 }
01027
01028 return c;
01029 }
01030
01031 static struct ast_channel *oss_request(const char *type, int format, void *data, int *cause)
01032 {
01033 struct ast_channel *c;
01034 struct chan_oss_pvt *o = find_desc(data);
01035
01036 ast_log(LOG_WARNING, "oss_request ty <%s> data 0x%p <%s>\n", type, data, (char *) data);
01037 if (o == NULL) {
01038 ast_log(LOG_NOTICE, "Device %s not found\n", (char *) data);
01039
01040 return NULL;
01041 }
01042 if ((format & AST_FORMAT_SLINEAR) == 0) {
01043 ast_log(LOG_NOTICE, "Format 0x%x unsupported\n", format);
01044 return NULL;
01045 }
01046 if (o->owner) {
01047 ast_log(LOG_NOTICE, "Already have a call (chan %p) on the OSS channel\n", o->owner);
01048 *cause = AST_CAUSE_BUSY;
01049 return NULL;
01050 }
01051 c = oss_new(o, NULL, NULL, AST_STATE_DOWN);
01052 if (c == NULL) {
01053 ast_log(LOG_WARNING, "Unable to create new OSS channel\n");
01054 return NULL;
01055 }
01056 return c;
01057 }
01058
01059 static int console_autoanswer_deprecated(int fd, int argc, char *argv[])
01060 {
01061 struct chan_oss_pvt *o = find_desc(oss_active);
01062
01063 if (argc == 1) {
01064 ast_cli(fd, "Auto answer is %s.\n", o->autoanswer ? "on" : "off");
01065 return RESULT_SUCCESS;
01066 }
01067 if (argc != 2)
01068 return RESULT_SHOWUSAGE;
01069 if (o == NULL) {
01070 ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n", oss_active);
01071 return RESULT_FAILURE;
01072 }
01073 if (!strcasecmp(argv[1], "on"))
01074 o->autoanswer = -1;
01075 else if (!strcasecmp(argv[1], "off"))
01076 o->autoanswer = 0;
01077 else
01078 return RESULT_SHOWUSAGE;
01079 return RESULT_SUCCESS;
01080 }
01081
01082 static int console_autoanswer(int fd, int argc, char *argv[])
01083 {
01084 struct chan_oss_pvt *o = find_desc(oss_active);
01085
01086 if (argc == 2) {
01087 ast_cli(fd, "Auto answer is %s.\n", o->autoanswer ? "on" : "off");
01088 return RESULT_SUCCESS;
01089 }
01090 if (argc != 3)
01091 return RESULT_SHOWUSAGE;
01092 if (o == NULL) {
01093 ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n",
01094 oss_active);
01095 return RESULT_FAILURE;
01096 }
01097 if (!strcasecmp(argv[2], "on"))
01098 o->autoanswer = -1;
01099 else if (!strcasecmp(argv[2], "off"))
01100 o->autoanswer = 0;
01101 else
01102 return RESULT_SHOWUSAGE;
01103 return RESULT_SUCCESS;
01104 }
01105
01106 static char *autoanswer_complete_deprecated(const char *line, const char *word, int pos, int state)
01107 {
01108 static char *choices[] = { "on", "off", NULL };
01109
01110 return (pos != 2) ? NULL : ast_cli_complete(word, choices, state);
01111 }
01112
01113 static char *autoanswer_complete(const char *line, const char *word, int pos, int state)
01114 {
01115 static char *choices[] = { "on", "off", NULL };
01116
01117 return (pos != 3) ? NULL : ast_cli_complete(word, choices, state);
01118 }
01119
01120 static char autoanswer_usage[] =
01121 "Usage: console autoanswer [on|off]\n"
01122 " Enables or disables autoanswer feature. If used without\n"
01123 " argument, displays the current on/off status of autoanswer.\n"
01124 " The default value of autoanswer is in 'oss.conf'.\n";
01125
01126
01127
01128
01129 static int console_answer_deprecated(int fd, int argc, char *argv[])
01130 {
01131 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
01132 struct chan_oss_pvt *o = find_desc(oss_active);
01133
01134 if (argc != 1)
01135 return RESULT_SHOWUSAGE;
01136 if (!o->owner) {
01137 ast_cli(fd, "No one is calling us\n");
01138 return RESULT_FAILURE;
01139 }
01140 o->hookstate = 1;
01141 o->cursound = -1;
01142 o->nosound = 0;
01143 ast_queue_frame(o->owner, &f);
01144 #if 0
01145
01146 ring(o, AST_CONTROL_ANSWER);
01147 #endif
01148 return RESULT_SUCCESS;
01149 }
01150
01151 static int console_answer(int fd, int argc, char *argv[])
01152 {
01153 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
01154 struct chan_oss_pvt *o = find_desc(oss_active);
01155
01156 if (argc != 2)
01157 return RESULT_SHOWUSAGE;
01158 if (!o->owner) {
01159 ast_cli(fd, "No one is calling us\n");
01160 return RESULT_FAILURE;
01161 }
01162 o->hookstate = 1;
01163 o->cursound = -1;
01164 o->nosound = 0;
01165 ast_queue_frame(o->owner, &f);
01166 #if 0
01167
01168 ring(o, AST_CONTROL_ANSWER);
01169 #endif
01170 return RESULT_SUCCESS;
01171 }
01172
01173 static char answer_usage[] =
01174 "Usage: console answer\n"
01175 " Answers an incoming call on the console (OSS) channel.\n";
01176
01177
01178
01179
01180
01181 static int console_sendtext_deprecated(int fd, int argc, char *argv[])
01182 {
01183 struct chan_oss_pvt *o = find_desc(oss_active);
01184 char buf[TEXT_SIZE];
01185
01186 if (argc < 2)
01187 return RESULT_SHOWUSAGE;
01188 if (!o->owner) {
01189 ast_cli(fd, "Not in a call\n");
01190 return RESULT_FAILURE;
01191 }
01192 ast_join(buf, sizeof(buf) - 1, argv + 2);
01193 if (!ast_strlen_zero(buf)) {
01194 struct ast_frame f = { 0, };
01195 int i = strlen(buf);
01196 buf[i] = '\n';
01197 f.frametype = AST_FRAME_TEXT;
01198 f.subclass = 0;
01199 f.data = buf;
01200 f.datalen = i + 1;
01201 ast_queue_frame(o->owner, &f);
01202 }
01203 return RESULT_SUCCESS;
01204 }
01205
01206 static int console_sendtext(int fd, int argc, char *argv[])
01207 {
01208 struct chan_oss_pvt *o = find_desc(oss_active);
01209 char buf[TEXT_SIZE];
01210
01211 if (argc < 3)
01212 return RESULT_SHOWUSAGE;
01213 if (!o->owner) {
01214 ast_cli(fd, "Not in a call\n");
01215 return RESULT_FAILURE;
01216 }
01217 ast_join(buf, sizeof(buf) - 1, argv + 3);
01218 if (!ast_strlen_zero(buf)) {
01219 struct ast_frame f = { 0, };
01220 int i = strlen(buf);
01221 buf[i] = '\n';
01222 f.frametype = AST_FRAME_TEXT;
01223 f.subclass = 0;
01224 f.data = buf;
01225 f.datalen = i + 1;
01226 ast_queue_frame(o->owner, &f);
01227 }
01228 return RESULT_SUCCESS;
01229 }
01230
01231 static char sendtext_usage[] =
01232 "Usage: console send text <message>\n"
01233 " Sends a text message for display on the remote terminal.\n";
01234
01235 static int console_hangup_deprecated(int fd, int argc, char *argv[])
01236 {
01237 struct chan_oss_pvt *o = find_desc(oss_active);
01238
01239 if (argc != 1)
01240 return RESULT_SHOWUSAGE;
01241 o->cursound = -1;
01242 o->nosound = 0;
01243 if (!o->owner && !o->hookstate) {
01244 ast_cli(fd, "No call to hang up\n");
01245 return RESULT_FAILURE;
01246 }
01247 o->hookstate = 0;
01248 if (o->owner)
01249 ast_queue_hangup(o->owner);
01250 setformat(o, O_CLOSE);
01251 return RESULT_SUCCESS;
01252 }
01253
01254 static int console_hangup(int fd, int argc, char *argv[])
01255 {
01256 struct chan_oss_pvt *o = find_desc(oss_active);
01257
01258 if (argc != 2)
01259 return RESULT_SHOWUSAGE;
01260 o->cursound = -1;
01261 o->nosound = 0;
01262 if (!o->owner && !o->hookstate) {
01263 ast_cli(fd, "No call to hang up\n");
01264 return RESULT_FAILURE;
01265 }
01266 o->hookstate = 0;
01267 if (o->owner)
01268 ast_queue_hangup(o->owner);
01269 setformat(o, O_CLOSE);
01270 return RESULT_SUCCESS;
01271 }
01272
01273 static char hangup_usage[] =
01274 "Usage: console hangup\n"
01275 " Hangs up any call currently placed on the console.\n";
01276
01277 static int console_flash_deprecated(int fd, int argc, char *argv[])
01278 {
01279 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH };
01280 struct chan_oss_pvt *o = find_desc(oss_active);
01281
01282 if (argc != 1)
01283 return RESULT_SHOWUSAGE;
01284 o->cursound = -1;
01285 o->nosound = 0;
01286 if (!o->owner) {
01287 ast_cli(fd, "No call to flash\n");
01288 return RESULT_FAILURE;
01289 }
01290 o->hookstate = 0;
01291 if (o->owner)
01292 ast_queue_frame(o->owner, &f);
01293 return RESULT_SUCCESS;
01294 }
01295
01296 static int console_flash(int fd, int argc, char *argv[])
01297 {
01298 struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH };
01299 struct chan_oss_pvt *o = find_desc(oss_active);
01300
01301 if (argc != 2)
01302 return RESULT_SHOWUSAGE;
01303 o->cursound = -1;
01304 o->nosound = 0;
01305 if (!o->owner) {
01306 ast_cli(fd, "No call to flash\n");
01307 return RESULT_FAILURE;
01308 }
01309 o->hookstate = 0;
01310 if (o->owner)
01311 ast_queue_frame(o->owner, &f);
01312 return RESULT_SUCCESS;
01313 }
01314
01315 static char flash_usage[] =
01316 "Usage: console flash\n"
01317 " Flashes the call currently placed on the console.\n";
01318
01319 static int console_dial_deprecated(int fd, int argc, char *argv[])
01320 {
01321 char *s = NULL, *mye = NULL, *myc = NULL;
01322 struct chan_oss_pvt *o = find_desc(oss_active);
01323
01324 if (argc != 1 && argc != 2)
01325 return RESULT_SHOWUSAGE;
01326 if (o->owner) {
01327 int i;
01328 struct ast_frame f = { AST_FRAME_DTMF, 0 };
01329
01330 if (argc == 1) {
01331 ast_cli(fd, "Already in a call. You can only dial digits until you hangup.\n");
01332 return RESULT_FAILURE;
01333 }
01334 s = argv[1];
01335
01336 for (i = 0; i < strlen(s); i++) {
01337 f.subclass = s[i];
01338 ast_queue_frame(o->owner, &f);
01339 }
01340 return RESULT_SUCCESS;
01341 }
01342
01343 if (argc == 2)
01344 s = ast_ext_ctx(argv[1], &mye, &myc);
01345
01346 if (mye == NULL)
01347 mye = o->ext;
01348 if (myc == NULL)
01349 myc = o->ctx;
01350 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
01351 o->hookstate = 1;
01352 oss_new(o, mye, myc, AST_STATE_RINGING);
01353 } else
01354 ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
01355 if (s)
01356 free(s);
01357 return RESULT_SUCCESS;
01358 }
01359
01360 static int console_dial(int fd, int argc, char *argv[])
01361 {
01362 char *s = NULL, *mye = NULL, *myc = NULL;
01363 struct chan_oss_pvt *o = find_desc(oss_active);
01364
01365 if (argc != 2 && argc != 3)
01366 return RESULT_SHOWUSAGE;
01367 if (o->owner) {
01368 int i;
01369 struct ast_frame f = { AST_FRAME_DTMF, 0 };
01370
01371 if (argc == 2) {
01372 ast_cli(fd, "Already in a call. You can only dial digits until you hangup.\n");
01373 return RESULT_FAILURE;
01374 }
01375 s = argv[2];
01376
01377 for (i = 0; i < strlen(s); i++) {
01378 f.subclass = s[i];
01379 ast_queue_frame(o->owner, &f);
01380 }
01381 return RESULT_SUCCESS;
01382 }
01383
01384 if (argc == 3)
01385 s = ast_ext_ctx(argv[2], &mye, &myc);
01386
01387 if (mye == NULL)
01388 mye = o->ext;
01389 if (myc == NULL)
01390 myc = o->ctx;
01391 if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
01392 o->hookstate = 1;
01393 oss_new(o, mye, myc, AST_STATE_RINGING);
01394 } else
01395 ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
01396 if (s)
01397 free(s);
01398 return RESULT_SUCCESS;
01399 }
01400
01401 static char dial_usage[] =
01402 "Usage: console dial [extension[@context]]\n"
01403 " Dials a given extension (and context if specified)\n";
01404
01405 static int __console_mute_unmute(int mute)
01406 {
01407 struct chan_oss_pvt *o = find_desc(oss_active);
01408
01409 o->mute = mute;
01410 return RESULT_SUCCESS;
01411 }
01412
01413 static int console_mute_deprecated(int fd, int argc, char *argv[])
01414 {
01415 if (argc != 1)
01416 return RESULT_SHOWUSAGE;
01417
01418 return __console_mute_unmute(1);
01419 }
01420
01421 static int console_mute(int fd, int argc, char *argv[])
01422 {
01423 if (argc != 2)
01424 return RESULT_SHOWUSAGE;
01425
01426 return __console_mute_unmute(1);
01427 }
01428
01429 static char mute_usage[] =
01430 "Usage: console mute\nMutes the microphone\n";
01431
01432 static int console_unmute_deprecated(int fd, int argc, char *argv[])
01433 {
01434 if (argc != 1)
01435 return RESULT_SHOWUSAGE;
01436
01437 return __console_mute_unmute(0);
01438 }
01439
01440 static int console_unmute(int fd, int argc, char *argv[])
01441 {
01442 if (argc != 2)
01443 return RESULT_SHOWUSAGE;
01444
01445 return __console_mute_unmute(0);
01446 }
01447
01448 static char unmute_usage[] =
01449 "Usage: console unmute\nUnmutes the microphone\n";
01450
01451 static int console_transfer_deprecated(int fd, int argc, char *argv[])
01452 {
01453 struct chan_oss_pvt *o = find_desc(oss_active);
01454 struct ast_channel *b = NULL;
01455 char *tmp, *ext, *ctx;
01456
01457 if (argc != 2)
01458 return RESULT_SHOWUSAGE;
01459 if (o == NULL)
01460 return RESULT_FAILURE;
01461 if (o->owner ==NULL || (b = ast_bridged_channel(o->owner)) == NULL) {
01462 ast_cli(fd, "There is no call to transfer\n");
01463 return RESULT_SUCCESS;
01464 }
01465
01466 tmp = ast_ext_ctx(argv[1], &ext, &ctx);
01467 if (ctx == NULL)
01468 ctx = o->owner->context;
01469 if (!ast_exists_extension(b, ctx, ext, 1, b->cid.cid_num))
01470 ast_cli(fd, "No such extension exists\n");
01471 else {
01472 ast_cli(fd, "Whee, transferring %s to %s@%s.\n",
01473 b->name, ext, ctx);
01474 if (ast_async_goto(b, ctx, ext, 1))
01475 ast_cli(fd, "Failed to transfer :(\n");
01476 }
01477 if (tmp)
01478 free(tmp);
01479 return RESULT_SUCCESS;
01480 }
01481
01482 static int console_transfer(int fd, int argc, char *argv[])
01483 {
01484 struct chan_oss_pvt *o = find_desc(oss_active);
01485 struct ast_channel *b = NULL;
01486 char *tmp, *ext, *ctx;
01487
01488 if (argc != 3)
01489 return RESULT_SHOWUSAGE;
01490 if (o == NULL)
01491 return RESULT_FAILURE;
01492 if (o->owner == NULL || (b = ast_bridged_channel(o->owner)) == NULL) {
01493 ast_cli(fd, "There is no call to transfer\n");
01494 return RESULT_SUCCESS;
01495 }
01496
01497 tmp = ast_ext_ctx(argv[2], &ext, &ctx);
01498 if (ctx == NULL)
01499 ctx = o->owner->context;
01500 if (!ast_exists_extension(b, ctx, ext, 1, b->cid.cid_num))
01501 ast_cli(fd, "No such extension exists\n");
01502 else {
01503 ast_cli(fd, "Whee, transferring %s to %s@%s.\n", b->name, ext, ctx);
01504 if (ast_async_goto(b, ctx, ext, 1))
01505 ast_cli(fd, "Failed to transfer :(\n");
01506 }
01507 if (tmp)
01508 free(tmp);
01509 return RESULT_SUCCESS;
01510 }
01511
01512 static char transfer_usage[] =
01513 "Usage: console transfer <extension>[@context]\n"
01514 " Transfers the currently connected call to the given extension (and\n"
01515 "context if specified)\n";
01516
01517 static int console_active_deprecated(int fd, int argc, char *argv[])
01518 {
01519 if (argc == 1)
01520 ast_cli(fd, "active console is [%s]\n", oss_active);
01521 else if (argc != 2)
01522 return RESULT_SHOWUSAGE;
01523 else {
01524 struct chan_oss_pvt *o;
01525 if (strcmp(argv[1], "show") == 0) {
01526 for (o = oss_default.next; o; o = o->next)
01527 ast_cli(fd, "device [%s] exists\n", o->name);
01528 return RESULT_SUCCESS;
01529 }
01530 o = find_desc(argv[1]);
01531 if (o == NULL)
01532 ast_cli(fd, "No device [%s] exists\n", argv[1]);
01533 else
01534 oss_active = o->name;
01535 }
01536 return RESULT_SUCCESS;
01537 }
01538
01539 static int console_active(int fd, int argc, char *argv[])
01540 {
01541 if (argc == 2)
01542 ast_cli(fd, "active console is [%s]\n", oss_active);
01543 else if (argc != 3)
01544 return RESULT_SHOWUSAGE;
01545 else {
01546 struct chan_oss_pvt *o;
01547 if (strcmp(argv[2], "show") == 0) {
01548 for (o = oss_default.next; o; o = o->next)
01549 ast_cli(fd, "device [%s] exists\n", o->name);
01550 return RESULT_SUCCESS;
01551 }
01552 o = find_desc(argv[2]);
01553 if (o == NULL)
01554 ast_cli(fd, "No device [%s] exists\n", argv[2]);
01555 else
01556 oss_active = o->name;
01557 }
01558 return RESULT_SUCCESS;
01559 }
01560
01561 static char active_usage[] =
01562 "Usage: console active [device]\n"
01563 " If used without a parameter, displays which device is the current\n"
01564 "console. If a device is specified, the console sound device is changed to\n"
01565 "the device specified.\n";
01566
01567
01568
01569
01570 static void store_boost(struct chan_oss_pvt *o, char *s)
01571 {
01572 double boost = 0;
01573 if (sscanf(s, "%lf", &boost) != 1) {
01574 ast_log(LOG_WARNING, "invalid boost <%s>\n", s);
01575 return;
01576 }
01577 if (boost < -BOOST_MAX) {
01578 ast_log(LOG_WARNING, "boost %s too small, using %d\n", s, -BOOST_MAX);
01579 boost = -BOOST_MAX;
01580 } else if (boost > BOOST_MAX) {
01581 ast_log(LOG_WARNING, "boost %s too large, using %d\n", s, BOOST_MAX);
01582 boost = BOOST_MAX;
01583 }
01584 boost = exp(log(10) * boost / 20) * BOOST_SCALE;
01585 o->boost = boost;
01586 ast_log(LOG_WARNING, "setting boost %s to %d\n", s, o->boost);
01587 }
01588
01589 static int do_boost(int fd, int argc, char *argv[])
01590 {
01591 struct chan_oss_pvt *o = find_desc(oss_active);
01592
01593 if (argc == 2)
01594 ast_cli(fd, "boost currently %5.1f\n", 20 * log10(((double) o->boost / (double) BOOST_SCALE)));
01595 else if (argc == 3)
01596 store_boost(o, argv[2]);
01597 return RESULT_SUCCESS;
01598 }
01599
01600 static struct ast_cli_entry cli_oss_answer_deprecated = {
01601 { "answer", NULL },
01602 console_answer_deprecated, NULL,
01603 NULL };
01604
01605 static struct ast_cli_entry cli_oss_hangup_deprecated = {
01606 { "hangup", NULL },
01607 console_hangup_deprecated, NULL,
01608 NULL };
01609
01610 static struct ast_cli_entry cli_oss_flash_deprecated = {
01611 { "flash", NULL },
01612 console_flash_deprecated, NULL,
01613 NULL };
01614
01615 static struct ast_cli_entry cli_oss_dial_deprecated = {
01616 { "dial", NULL },
01617 console_dial_deprecated, NULL,
01618 NULL };
01619
01620 static struct ast_cli_entry cli_oss_mute_deprecated = {
01621 { "mute", NULL },
01622 console_mute_deprecated, NULL,
01623 NULL };
01624
01625 static struct ast_cli_entry cli_oss_unmute_deprecated = {
01626 { "unmute", NULL },
01627 console_unmute_deprecated, NULL,
01628 NULL };
01629
01630 static struct ast_cli_entry cli_oss_transfer_deprecated = {
01631 { "transfer", NULL },
01632 console_transfer_deprecated, NULL,
01633 NULL };
01634
01635 static struct ast_cli_entry cli_oss_send_text_deprecated = {
01636 { "send", "text", NULL },
01637 console_sendtext_deprecated, NULL,
01638 NULL };
01639
01640 static struct ast_cli_entry cli_oss_autoanswer_deprecated = {
01641 { "autoanswer", NULL },
01642 console_autoanswer_deprecated, NULL,
01643 NULL, autoanswer_complete_deprecated };
01644
01645 static struct ast_cli_entry cli_oss_boost_deprecated = {
01646 { "oss", "boost", NULL },
01647 do_boost, NULL,
01648 NULL };
01649
01650 static struct ast_cli_entry cli_oss_active_deprecated = {
01651 { "console", NULL },
01652 console_active_deprecated, NULL,
01653 NULL };
01654
01655 static struct ast_cli_entry cli_oss[] = {
01656 { { "console", "answer", NULL },
01657 console_answer, "Answer an incoming console call",
01658 answer_usage, NULL, &cli_oss_answer_deprecated },
01659
01660 { { "console", "hangup", NULL },
01661 console_hangup, "Hangup a call on the console",
01662 hangup_usage, NULL, &cli_oss_hangup_deprecated },
01663
01664 { { "console", "flash", NULL },
01665 console_flash, "Flash a call on the console",
01666 flash_usage, NULL, &cli_oss_flash_deprecated },
01667
01668 { { "console", "dial", NULL },
01669 console_dial, "Dial an extension on the console",
01670 dial_usage, NULL, &cli_oss_dial_deprecated },
01671
01672 { { "console", "mute", NULL },
01673 console_mute, "Disable mic input",
01674 mute_usage, NULL, &cli_oss_mute_deprecated },
01675
01676 { { "console", "unmute", NULL },
01677 console_unmute, "Enable mic input",
01678 unmute_usage, NULL, &cli_oss_unmute_deprecated },
01679
01680 { { "console", "transfer", NULL },
01681 console_transfer, "Transfer a call to a different extension",
01682 transfer_usage, NULL, &cli_oss_transfer_deprecated },
01683
01684 { { "console", "send", "text", NULL },
01685 console_sendtext, "Send text to the remote device",
01686 sendtext_usage, NULL, &cli_oss_send_text_deprecated },
01687
01688 { { "console", "autoanswer", NULL },
01689 console_autoanswer, "Sets/displays autoanswer",
01690 autoanswer_usage, autoanswer_complete, &cli_oss_autoanswer_deprecated },
01691
01692 { { "console", "boost", NULL },
01693 do_boost, "Sets/displays mic boost in dB",
01694 NULL, NULL, &cli_oss_boost_deprecated },
01695
01696 { { "console", "active", NULL },
01697 console_active, "Sets/displays active console",
01698 active_usage, NULL, &cli_oss_active_deprecated },
01699 };
01700
01701
01702
01703
01704
01705
01706 static void store_mixer(struct chan_oss_pvt *o, char *s)
01707 {
01708 int i;
01709
01710 for (i = 0; i < strlen(s); i++) {
01711 if (!isalnum(s[i]) && index(" \t-/", s[i]) == NULL) {
01712 ast_log(LOG_WARNING, "Suspect char %c in mixer cmd, ignoring:\n\t%s\n", s[i], s);
01713 return;
01714 }
01715 }
01716 if (o->mixer_cmd)
01717 free(o->mixer_cmd);
01718 o->mixer_cmd = ast_strdup(s);
01719 ast_log(LOG_WARNING, "setting mixer %s\n", s);
01720 }
01721
01722
01723
01724
01725 static void store_callerid(struct chan_oss_pvt *o, char *s)
01726 {
01727 ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
01728 }
01729
01730
01731
01732
01733 static struct chan_oss_pvt *store_config(struct ast_config *cfg, char *ctg)
01734 {
01735 struct ast_variable *v;
01736 struct chan_oss_pvt *o;
01737
01738 if (ctg == NULL) {
01739 o = &oss_default;
01740 ctg = "general";
01741 } else {
01742 if (!(o = ast_calloc(1, sizeof(*o))))
01743 return NULL;
01744 *o = oss_default;
01745
01746 if (strcmp(ctg, "general") == 0) {
01747 o->name = ast_strdup("dsp");
01748 oss_active = o->name;
01749 goto openit;
01750 }
01751 o->name = ast_strdup(ctg);
01752 }
01753
01754 strcpy(o->mohinterpret, "default");
01755
01756 o->lastopen = ast_tvnow();
01757
01758 for (v = ast_variable_browse(cfg, ctg); v; v = v->next) {
01759 M_START(v->name, v->value);
01760
01761
01762 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
01763 continue;
01764
01765 M_BOOL("autoanswer", o->autoanswer)
01766 M_BOOL("autohangup", o->autohangup)
01767 M_BOOL("overridecontext", o->overridecontext)
01768 M_STR("device", o->device)
01769 M_UINT("frags", o->frags)
01770 M_UINT("debug", oss_debug)
01771 M_UINT("queuesize", o->queuesize)
01772 M_STR("context", o->ctx)
01773 M_STR("language", o->language)
01774 M_STR("mohinterpret", o->mohinterpret)
01775 M_STR("extension", o->ext)
01776 M_F("mixer", store_mixer(o, v->value))
01777 M_F("callerid", store_callerid(o, v->value))
01778 M_F("boost", store_boost(o, v->value))
01779 M_END(;
01780 );
01781 }
01782 if (ast_strlen_zero(o->device))
01783 ast_copy_string(o->device, DEV_DSP, sizeof(o->device));
01784 if (o->mixer_cmd) {
01785 char *cmd;
01786
01787 asprintf(&cmd, "mixer %s", o->mixer_cmd);
01788 ast_log(LOG_WARNING, "running [%s]\n", cmd);
01789 system(cmd);
01790 free(cmd);
01791 }
01792 if (o == &oss_default)
01793 return NULL;
01794
01795 openit:
01796 #if TRYOPEN
01797 if (setformat(o, O_RDWR) < 0) {
01798 if (option_verbose > 0) {
01799 ast_verbose(VERBOSE_PREFIX_2 "Device %s not detected\n", ctg);
01800 ast_verbose(VERBOSE_PREFIX_2 "Turn off OSS support by adding " "'noload=chan_oss.so' in /etc/asterisk/modules.conf\n");
01801 }
01802 goto error;
01803 }
01804 if (o->duplex != M_FULL)
01805 ast_log(LOG_WARNING, "XXX I don't work right with non " "full-duplex sound cards XXX\n");
01806 #endif
01807 if (pipe(o->sndcmd) != 0) {
01808 ast_log(LOG_ERROR, "Unable to create pipe\n");
01809 goto error;
01810 }
01811 ast_pthread_create_background(&o->sthread, NULL, sound_thread, o);
01812
01813 if (o != &oss_default) {
01814 o->next = oss_default.next;
01815 oss_default.next = o;
01816 }
01817 return o;
01818
01819 error:
01820 if (o != &oss_default)
01821 free(o);
01822 return NULL;
01823 }
01824
01825 static int load_module(void)
01826 {
01827 struct ast_config *cfg = NULL;
01828 char *ctg = NULL;
01829
01830
01831 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
01832
01833
01834 if (!(cfg = ast_config_load(config))) {
01835 ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
01836 return AST_MODULE_LOAD_DECLINE;
01837 }
01838
01839 do {
01840 store_config(cfg, ctg);
01841 } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL);
01842
01843 ast_config_destroy(cfg);
01844
01845 if (find_desc(oss_active) == NULL) {
01846 ast_log(LOG_NOTICE, "Device %s not found\n", oss_active);
01847
01848
01849 return AST_MODULE_LOAD_FAILURE;
01850 }
01851
01852 if (ast_channel_register(&oss_tech)) {
01853 ast_log(LOG_ERROR, "Unable to register channel type 'OSS'\n");
01854 return AST_MODULE_LOAD_FAILURE;
01855 }
01856
01857 ast_cli_register_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry));
01858
01859 return AST_MODULE_LOAD_SUCCESS;
01860 }
01861
01862
01863 static int unload_module(void)
01864 {
01865 struct chan_oss_pvt *o;
01866
01867 ast_channel_unregister(&oss_tech);
01868 ast_cli_unregister_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry));
01869
01870 for (o = oss_default.next; o; o = o->next) {
01871 close(o->sounddev);
01872 if (o->sndcmd[0] > 0) {
01873 close(o->sndcmd[0]);
01874 close(o->sndcmd[1]);
01875 }
01876 if (o->owner)
01877 ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
01878 if (o->owner)
01879 return -1;
01880
01881
01882 }
01883 return 0;
01884 }
01885
01886 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "OSS Console Channel Driver");