2 * Asterisk -- A telephony toolkit for Linux.
4 * Use /dev/dsp as a channel, and the console to command it :).
6 * The full-duplex "simulation" is pretty weak. This is generally a
7 * VERY BADLY WRITTEN DRIVER so please don't use it as a model for
10 * Copyright (C) 1999, Mark Spencer
12 * Mark Spencer <markster@linux-support.net>
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License
18 #include <asterisk/frame.h>
19 #include <asterisk/logger.h>
20 #include <asterisk/channel.h>
21 #include <asterisk/module.h>
22 #include <asterisk/channel_pvt.h>
23 #include <asterisk/options.h>
24 #include <asterisk/pbx.h>
25 #include <asterisk/config.h>
26 #include <asterisk/cli.h>
30 #include <sys/ioctl.h>
35 #include <linux/soundcard.h>
37 /* Which device to use */
38 #define DEV_DSP "/dev/dsp"
40 /* Lets use 160 sample frames, just like GSM. */
41 #define FRAME_SIZE 160
43 /* When you set the frame size, you have to come up with
44 the right buffer format as well. */
45 /* 5 64-byte frames = one frame */
46 #define BUFFER_FMT ((buffersize * 5) << 16) | (0x0006);
48 /* Don't switch between read/write modes faster than every 300 ms */
49 #define MIN_SWITCH_TIME 600
51 static struct timeval lasttime;
54 static int needanswer = 0;
55 static int needhangup = 0;
56 static int silencesuppression = 0;
57 static int silencethreshold = 1000;
59 static char digits[80] = "";
61 static pthread_mutex_t usecnt_lock = PTHREAD_MUTEX_INITIALIZER;
63 static char *type = "Console";
64 static char *desc = "OSS Console Channel Driver";
65 static char *tdesc = "OSS Console Channel Driver";
66 static char *config = "oss.conf";
68 static char context[AST_MAX_EXTENSION] = "default";
69 static char exten[AST_MAX_EXTENSION] = "s";
71 /* Some pipes to prevent overflow */
73 static pthread_mutex_t sound_lock = PTHREAD_MUTEX_INITIALIZER;
74 static pthread_t silly;
76 static struct chan_oss_pvt {
77 /* We only have one OSS structure -- near sighted perhaps, but it
78 keeps this driver as simple as possible -- as it should be. */
79 struct ast_channel *owner;
80 char exten[AST_MAX_EXTENSION];
81 char context[AST_MAX_EXTENSION];
84 static int time_has_passed()
88 gettimeofday(&tv, NULL);
89 ms = (tv.tv_sec - lasttime.tv_sec) * 1000 +
90 (tv.tv_usec - lasttime.tv_usec) / 1000;
91 if (ms > MIN_SWITCH_TIME)
96 /* Number of buffers... Each is FRAMESIZE/8 ms long. For example
97 with 160 sample frames, and a buffer size of 3, we have a 60ms buffer,
101 #define MAX_BUFFER_SIZE 100
102 static int buffersize = 3;
104 static int full_duplex = 0;
106 /* Are we reading or writing (simulated full duplex) */
107 static int readmode = 1;
109 /* File descriptor for sound device */
110 static int sounddev = -1;
112 static int autoanswer = 1;
114 static int calc_loudness(short *frame)
118 for (x=0;x<FRAME_SIZE;x++) {
124 sum = sum/FRAME_SIZE;
128 static int silence_suppress(short *buf)
132 static int silentframes = 0;
133 static char silbuf[FRAME_SIZE * 2 * SILBUF];
134 static int silbufcnt=0;
135 if (!silencesuppression)
137 loudness = calc_loudness((short *)(buf));
139 ast_log(LOG_DEBUG, "loudness is %d\n", loudness);
140 if (loudness < silencethreshold) {
143 /* Keep track of the last few bits of silence so we can play
144 them as lead-in when the time is right */
145 if (silbufcnt >= SILBUF) {
146 /* Make way for more buffer */
147 memmove(silbuf, silbuf + FRAME_SIZE * 2, FRAME_SIZE * 2 * (SILBUF - 1));
150 memcpy(silbuf + FRAME_SIZE * 2 * silbufcnt, buf, FRAME_SIZE * 2);
151 if (silentframes > 10) {
152 /* We've had plenty of silence, so compress it now */
157 /* Write any buffered silence we have, it may have something
160 write(funnel[1], silbuf, silbufcnt * FRAME_SIZE);
167 static void *silly_thread(void *ignore)
169 char buf[FRAME_SIZE * 2];
172 /* Read from the sound device, and write to the pipe. */
174 /* Give the writer a better shot at the lock */
178 pthread_testcancel();
179 pthread_mutex_lock(&sound_lock);
180 res = read(sounddev, buf + pos, FRAME_SIZE * 2 - pos);
181 pthread_mutex_unlock(&sound_lock);
184 if (pos == FRAME_SIZE * 2) {
185 if (needhangup || needanswer || strlen(digits) ||
186 !silence_suppress((short *)buf)) {
187 res = write(funnel[1], buf, sizeof(buf));
195 pthread_testcancel();
200 static int setformat(void)
202 int fmt, desired, res, fd = sounddev;
203 static int warnedalready = 0;
204 static int warnedalready2 = 0;
205 pthread_mutex_lock(&sound_lock);
207 res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
209 ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
210 pthread_mutex_unlock(&sound_lock);
213 res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
215 if (option_verbose > 1)
216 ast_verbose(VERBOSE_PREFIX_2 "Console is full duplex\n");
220 res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
222 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
223 pthread_mutex_unlock(&sound_lock);
226 /* 8000 Hz desired */
229 res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
231 ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
232 pthread_mutex_unlock(&sound_lock);
235 if (fmt != desired) {
236 if (!warnedalready++)
237 ast_log(LOG_WARNING, "Requested %d Hz, got %d Hz -- sound may be choppy\n", desired, fmt);
241 res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
243 if (!warnedalready2++)
244 ast_log(LOG_WARNING, "Unable to set fragment size -- sound may be choppy\n");
247 pthread_mutex_unlock(&sound_lock);
251 static int soundcard_setoutput(int force)
253 /* Make sure the soundcard is in output mode. */
255 if (full_duplex || (!readmode && !force))
257 pthread_mutex_lock(&sound_lock);
259 if (force || time_has_passed()) {
260 ioctl(sounddev, SNDCTL_DSP_RESET);
261 /* Keep the same fd reserved by closing the sound device and copying stdin at the same
263 /* dup2(0, sound); */
265 fd = open(DEV_DSP, O_WRONLY);
267 ast_log(LOG_WARNING, "Unable to re-open DSP device: %s\n", strerror(errno));
268 pthread_mutex_unlock(&sound_lock);
271 /* dup2 will close the original and make fd be sound */
272 if (dup2(fd, sounddev) < 0) {
273 ast_log(LOG_WARNING, "dup2() failed: %s\n", strerror(errno));
274 pthread_mutex_unlock(&sound_lock);
278 pthread_mutex_unlock(&sound_lock);
281 pthread_mutex_unlock(&sound_lock);
284 pthread_mutex_unlock(&sound_lock);
288 static int soundcard_setinput(int force)
291 if (full_duplex || (readmode && !force))
293 pthread_mutex_lock(&sound_lock);
295 if (force || time_has_passed()) {
296 ioctl(sounddev, SNDCTL_DSP_RESET);
298 /* dup2(0, sound); */
299 fd = open(DEV_DSP, O_RDONLY);
301 ast_log(LOG_WARNING, "Unable to re-open DSP device: %s\n", strerror(errno));
302 pthread_mutex_unlock(&sound_lock);
305 /* dup2 will close the original and make fd be sound */
306 if (dup2(fd, sounddev) < 0) {
307 ast_log(LOG_WARNING, "dup2() failed: %s\n", strerror(errno));
308 pthread_mutex_unlock(&sound_lock);
312 pthread_mutex_unlock(&sound_lock);
315 pthread_mutex_unlock(&sound_lock);
318 pthread_mutex_unlock(&sound_lock);
322 static int soundcard_init()
324 /* Assume it's full duplex for starters */
325 int fd = open(DEV_DSP, O_RDWR);
327 ast_log(LOG_ERROR, "Unable to open %s: %s\n", DEV_DSP, strerror(errno));
330 gettimeofday(&lasttime, NULL);
334 soundcard_setinput(1);
338 static int oss_digit(struct ast_channel *c, char digit)
340 ast_verbose( " << Console Received digit %c >> \n", digit);
344 static int oss_call(struct ast_channel *c, char *dest, int timeout)
346 ast_verbose( " << Call placed to '%s' on console >> \n", dest);
348 ast_verbose( " << Auto-answered >> \n" );
351 ast_verbose( " << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
356 static int oss_answer(struct ast_channel *c)
358 ast_verbose( " << Console call has been answered >> \n");
359 c->state = AST_STATE_UP;
363 static int oss_hangup(struct ast_channel *c)
367 ast_verbose( " << Hangup on console >> \n");
368 pthread_mutex_lock(&usecnt_lock);
370 pthread_mutex_unlock(&usecnt_lock);
376 static int soundcard_writeframe(short *data)
378 /* Write an exactly FRAME_SIZE sized of frame */
379 static int bufcnt = 0;
380 static char buffer[FRAME_SIZE * 2 * MAX_BUFFER_SIZE * 5];
381 struct audio_buf_info info;
385 pthread_mutex_lock(&sound_lock);
386 if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info)) {
388 ast_log(LOG_WARNING, "Error reading output space\n");
392 if ((info.fragments >= buffersize * 5) && (bufcnt == buffersize)) {
393 /* We've run out of stuff, buffer again */
396 if (bufcnt == buffersize) {
397 /* Write sample immediately */
398 res = write(fd, ((void *)data), FRAME_SIZE * 2);
400 /* Copy the data into our buffer */
401 res = FRAME_SIZE * 2;
402 memcpy(buffer + (bufcnt * FRAME_SIZE * 2), data, FRAME_SIZE * 2);
404 if (bufcnt == buffersize) {
405 res = write(fd, ((void *)buffer), FRAME_SIZE * 2 * buffersize);
408 pthread_mutex_unlock(&sound_lock);
413 static int oss_write(struct ast_channel *chan, struct ast_frame *f)
416 static char sizbuf[8000];
417 static int sizpos = 0;
420 if (!full_duplex && (strlen(digits) || needhangup || needanswer)) {
421 /* If we're half duplex, we have to switch to read mode
422 to honor immediate needs if necessary */
423 res = soundcard_setinput(1);
425 ast_log(LOG_WARNING, "Unable to set device to input mode\n");
430 res = soundcard_setoutput(0);
432 ast_log(LOG_WARNING, "Unable to set output device\n");
434 } else if (res > 0) {
435 /* The device is still in read mode, and it's too soon to change it,
436 so just pretend we wrote it */
439 /* We have to digest the frame in 160-byte portions */
440 if (f->datalen > sizeof(sizbuf) - sizpos) {
441 ast_log(LOG_WARNING, "Frame too large\n");
444 memcpy(sizbuf + sizpos, f->data, f->datalen);
447 while(len - pos > FRAME_SIZE * 2) {
448 soundcard_writeframe((short *)(sizbuf + pos));
449 pos += FRAME_SIZE * 2;
452 memmove(sizbuf, sizbuf + pos, len - pos);
457 static struct ast_frame *oss_read(struct ast_channel *chan)
459 static struct ast_frame f;
460 static char buf[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
461 static int readpos = 0;
465 ast_log(LOG_DEBUG, "oss_read()\n");
468 f.frametype = AST_FRAME_NULL;
480 if (strlen(digits)) {
481 f.frametype = AST_FRAME_DTMF;
482 f.subclass = digits[0];
483 for (res=0;res<strlen(digits);res++)
484 digits[res] = digits[res + 1];
490 f.frametype = AST_FRAME_CONTROL;
491 f.subclass = AST_CONTROL_ANSWER;
492 chan->state = AST_STATE_UP;
496 res = soundcard_setinput(0);
498 ast_log(LOG_WARNING, "Unable to set input mode\n");
502 /* Theoretically shouldn't happen, but anyway, return a NULL frame */
505 res = read(funnel[0], buf + AST_FRIENDLY_OFFSET + readpos, FRAME_SIZE * 2 - readpos);
507 ast_log(LOG_WARNING, "Error reading from sound device: %s\n", strerror(errno));
512 if (readpos == FRAME_SIZE * 2) {
515 f.frametype = AST_FRAME_VOICE;
516 f.subclass = AST_FORMAT_SLINEAR;
517 f.timelen = FRAME_SIZE / 8;
518 f.datalen = FRAME_SIZE * 2;
519 f.data = buf + AST_FRIENDLY_OFFSET;
520 f.offset = AST_FRIENDLY_OFFSET;
527 static struct ast_channel *oss_new(struct chan_oss_pvt *p, int state)
529 struct ast_channel *tmp;
530 tmp = ast_channel_alloc();
532 snprintf(tmp->name, sizeof(tmp->name), "OSS/%s", DEV_DSP + 5);
535 tmp->format = AST_FORMAT_SLINEAR;
537 tmp->pvt->send_digit = oss_digit;
538 tmp->pvt->hangup = oss_hangup;
539 tmp->pvt->answer = oss_answer;
540 tmp->pvt->read = oss_read;
541 tmp->pvt->write = oss_write;
542 if (strlen(p->context))
543 strncpy(tmp->context, p->context, sizeof(tmp->context));
544 if (strlen(p->exten))
545 strncpy(tmp->exten, p->exten, sizeof(tmp->exten));
548 pthread_mutex_lock(&usecnt_lock);
550 pthread_mutex_unlock(&usecnt_lock);
551 ast_update_use_count();
552 if (state != AST_STATE_DOWN) {
553 if (ast_pbx_start(tmp)) {
554 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
563 static struct ast_channel *oss_request(char *type, int format, void *data)
565 int oldformat = format;
566 format &= AST_FORMAT_SLINEAR;
568 ast_log(LOG_NOTICE, "Asked to get a channel of format '%d'\n", oldformat);
572 ast_log(LOG_NOTICE, "Already have a call on the OSS channel\n");
575 return oss_new(&oss, AST_STATE_DOWN);
578 static int console_autoanswer(int fd, int argc, char *argv[])
580 if ((argc != 1) && (argc != 2))
581 return RESULT_SHOWUSAGE;
583 ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
584 return RESULT_SUCCESS;
586 if (!strcasecmp(argv[1], "on"))
588 else if (!strcasecmp(argv[1], "off"))
591 return RESULT_SHOWUSAGE;
593 return RESULT_SUCCESS;
596 static char *autoanswer_complete(char *line, char *word, int pos, int state)
599 #define MIN(a,b) ((a) < (b) ? (a) : (b))
603 if (strlen(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
606 if (strlen(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
607 return strdup("off");
614 static char autoanswer_usage[] =
615 "Usage: autoanswer [on|off]\n"
616 " Enables or disables autoanswer feature. If used without\n"
617 " argument, displays the current on/off status of autoanswer.\n"
618 " The default value of autoanswer is in 'oss.conf'.\n";
620 static int console_answer(int fd, int argc, char *argv[])
623 return RESULT_SHOWUSAGE;
625 ast_cli(fd, "No one is calling us\n");
626 return RESULT_FAILURE;
629 return RESULT_SUCCESS;
632 static char answer_usage[] =
634 " Answers an incoming call on the console (OSS) channel.\n";
636 static int console_hangup(int fd, int argc, char *argv[])
639 return RESULT_SHOWUSAGE;
641 ast_cli(fd, "No call to hangup up\n");
642 return RESULT_FAILURE;
645 return RESULT_SUCCESS;
648 static char hangup_usage[] =
650 " Hangs up any call currently placed on the console.\n";
653 static int console_dial(int fd, int argc, char *argv[])
655 char tmp[256], *tmp2;
657 if ((argc != 1) && (argc != 2))
658 return RESULT_SHOWUSAGE;
661 strncat(digits, argv[1], sizeof(digits) - strlen(digits));
663 ast_cli(fd, "You're already in a call. You can use this only to dial digits until you hangup\n");
664 return RESULT_FAILURE;
666 return RESULT_SUCCESS;
671 strncpy(tmp, argv[1], sizeof(tmp));
673 tmp2 = strtok(NULL, "@");
676 if (tmp2 && strlen(tmp2))
679 if (ast_exists_extension(NULL, myc, mye, 1)) {
680 strncpy(oss.exten, mye, sizeof(oss.exten));
681 strncpy(oss.context, myc, sizeof(oss.context));
682 oss_new(&oss, AST_STATE_UP);
684 ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
685 return RESULT_SUCCESS;
688 static char dial_usage[] =
689 "Usage: dial [extension[@context]]\n"
690 " Dials a given extensison (";
693 static struct ast_cli_entry myclis[] = {
694 { { "answer", NULL }, console_answer, "Answer an incoming console call", answer_usage },
695 { { "hangup", NULL }, console_hangup, "Hangup a call on the console", hangup_usage },
696 { { "dial", NULL }, console_dial, "Dial an extension on the console", dial_usage },
697 { { "autoanswer", NULL }, console_autoanswer, "Sets/displays autoanswer", autoanswer_usage, autoanswer_complete }
705 struct ast_config *cfg = ast_load(config);
706 struct ast_variable *v;
709 ast_log(LOG_ERROR, "Unable to create pipe\n");
712 /* We make the funnel so that writes to the funnel don't block...
713 Our "silly" thread can read to its heart content, preventing
714 recording overruns */
715 flags = fcntl(funnel[1], F_GETFL);
717 fcntl(funnel[0], F_SETFL, flags | O_NONBLOCK);
719 fcntl(funnel[1], F_SETFL, flags | O_NONBLOCK);
720 res = soundcard_init();
727 ast_log(LOG_WARNING, "XXX I don't work right with non-full duplex sound cards XXX\n");
728 pthread_create(&silly, NULL, silly_thread, NULL);
729 res = ast_channel_register(type, tdesc, AST_FORMAT_SLINEAR, oss_request);
731 ast_log(LOG_ERROR, "Unable to register channel class '%s'\n", type);
734 for (x=0;x<sizeof(myclis)/sizeof(struct ast_cli_entry); x++)
735 ast_cli_register(myclis + x);
737 v = ast_variable_browse(cfg, "general");
739 if (!strcasecmp(v->name, "autoanswer"))
740 autoanswer = ast_true(v->value);
741 else if (!strcasecmp(v->name, "silencesuppression"))
742 silencesuppression = ast_true(v->value);
743 else if (!strcasecmp(v->name, "silencethreshold"))
744 silencethreshold = atoi(v->value);
745 else if (!strcasecmp(v->name, "context"))
746 strncpy(context, v->value, sizeof(context));
747 else if (!strcasecmp(v->name, "extension"))
748 strncpy(exten, v->value, sizeof(exten));
761 for (x=0;x<sizeof(myclis)/sizeof(struct ast_cli_entry); x++)
762 ast_cli_unregister(myclis + x);
769 pthread_cancel(silly);
770 pthread_join(silly, NULL);
773 ast_softhangup(oss.owner);
787 pthread_mutex_lock(&usecnt_lock);
789 pthread_mutex_unlock(&usecnt_lock);