chan_oss.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Use /dev/dsp as a channel, and the console to command it :).
  5. *
  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
  8. * writing a driver.
  9. *
  10. * Copyright (C) 1999, Mark Spencer
  11. *
  12. * Mark Spencer <markster@linux-support.net>
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License
  16. */
  17. #include <asterisk/lock.h>
  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>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <errno.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/time.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #ifdef __linux
  36. #include <linux/soundcard.h>
  37. #elif defined(__FreeBSD__)
  38. #include <machine/soundcard.h>
  39. #else
  40. #include <soundcard.h>
  41. #endif
  42. #include "busy.h"
  43. #include "ringtone.h"
  44. #include "ring10.h"
  45. #include "answer.h"
  46. /* Which device to use */
  47. #ifdef __OpenBSD__
  48. #define DEV_DSP "/dev/audio"
  49. #else
  50. #define DEV_DSP "/dev/dsp"
  51. #endif
  52. /* Lets use 160 sample frames, just like GSM. */
  53. #define FRAME_SIZE 160
  54. /* When you set the frame size, you have to come up with
  55. the right buffer format as well. */
  56. /* 5 64-byte frames = one frame */
  57. #define BUFFER_FMT ((buffersize * 10) << 16) | (0x0006);
  58. /* Don't switch between read/write modes faster than every 300 ms */
  59. #define MIN_SWITCH_TIME 600
  60. static struct timeval lasttime;
  61. static int usecnt;
  62. static int silencesuppression = 0;
  63. static int silencethreshold = 1000;
  64. static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
  65. static char *type = "Console";
  66. static char *desc = "OSS Console Channel Driver";
  67. static char *tdesc = "OSS Console Channel Driver";
  68. static char *config = "oss.conf";
  69. static char context[AST_MAX_EXTENSION] = "default";
  70. static char language[MAX_LANGUAGE] = "";
  71. static char exten[AST_MAX_EXTENSION] = "s";
  72. static int hookstate=0;
  73. static short silence[FRAME_SIZE] = {0, };
  74. struct sound {
  75. int ind;
  76. short *data;
  77. int datalen;
  78. int samplen;
  79. int silencelen;
  80. int repeat;
  81. };
  82. static struct sound sounds[] = {
  83. { AST_CONTROL_RINGING, ringtone, sizeof(ringtone)/2, 16000, 32000, 1 },
  84. { AST_CONTROL_BUSY, busy, sizeof(busy)/2, 4000, 4000, 1 },
  85. { AST_CONTROL_CONGESTION, busy, sizeof(busy)/2, 2000, 2000, 1 },
  86. { AST_CONTROL_RING, ring10, sizeof(ring10)/2, 16000, 32000, 1 },
  87. { AST_CONTROL_ANSWER, answer, sizeof(answer)/2, 2200, 0, 0 },
  88. };
  89. /* Sound command pipe */
  90. static int sndcmd[2];
  91. static struct chan_oss_pvt {
  92. /* We only have one OSS structure -- near sighted perhaps, but it
  93. keeps this driver as simple as possible -- as it should be. */
  94. struct ast_channel *owner;
  95. char exten[AST_MAX_EXTENSION];
  96. char context[AST_MAX_EXTENSION];
  97. } oss;
  98. static int time_has_passed(void)
  99. {
  100. struct timeval tv;
  101. int ms;
  102. gettimeofday(&tv, NULL);
  103. ms = (tv.tv_sec - lasttime.tv_sec) * 1000 +
  104. (tv.tv_usec - lasttime.tv_usec) / 1000;
  105. if (ms > MIN_SWITCH_TIME)
  106. return -1;
  107. return 0;
  108. }
  109. /* Number of buffers... Each is FRAMESIZE/8 ms long. For example
  110. with 160 sample frames, and a buffer size of 3, we have a 60ms buffer,
  111. usually plenty. */
  112. static pthread_t sthread;
  113. #define MAX_BUFFER_SIZE 100
  114. static int buffersize = 3;
  115. static int full_duplex = 0;
  116. /* Are we reading or writing (simulated full duplex) */
  117. static int readmode = 1;
  118. /* File descriptor for sound device */
  119. static int sounddev = -1;
  120. static int autoanswer = 1;
  121. #if 0
  122. static int calc_loudness(short *frame)
  123. {
  124. int sum = 0;
  125. int x;
  126. for (x=0;x<FRAME_SIZE;x++) {
  127. if (frame[x] < 0)
  128. sum -= frame[x];
  129. else
  130. sum += frame[x];
  131. }
  132. sum = sum/FRAME_SIZE;
  133. return sum;
  134. }
  135. #endif
  136. static int cursound = -1;
  137. static int sampsent = 0;
  138. static int silencelen=0;
  139. static int offset=0;
  140. static int nosound=0;
  141. static int send_sound(void)
  142. {
  143. short myframe[FRAME_SIZE];
  144. int total = FRAME_SIZE;
  145. short *frame = NULL;
  146. int amt=0;
  147. int res;
  148. int myoff;
  149. audio_buf_info abi;
  150. if (cursound > -1) {
  151. res = ioctl(sounddev, SNDCTL_DSP_GETOSPACE ,&abi);
  152. if (res) {
  153. ast_log(LOG_WARNING, "Unable to read output space\n");
  154. return -1;
  155. }
  156. /* Calculate how many samples we can send, max */
  157. if (total > (abi.fragments * abi.fragsize / 2))
  158. total = abi.fragments * abi.fragsize / 2;
  159. res = total;
  160. if (sampsent < sounds[cursound].samplen) {
  161. myoff=0;
  162. while(total) {
  163. amt = total;
  164. if (amt > (sounds[cursound].datalen - offset))
  165. amt = sounds[cursound].datalen - offset;
  166. memcpy(myframe + myoff, sounds[cursound].data + offset, amt * 2);
  167. total -= amt;
  168. offset += amt;
  169. sampsent += amt;
  170. myoff += amt;
  171. if (offset >= sounds[cursound].datalen)
  172. offset = 0;
  173. }
  174. /* Set it up for silence */
  175. if (sampsent >= sounds[cursound].samplen)
  176. silencelen = sounds[cursound].silencelen;
  177. frame = myframe;
  178. } else {
  179. if (silencelen > 0) {
  180. frame = silence;
  181. silencelen -= res;
  182. } else {
  183. if (sounds[cursound].repeat) {
  184. /* Start over */
  185. sampsent = 0;
  186. offset = 0;
  187. } else {
  188. cursound = -1;
  189. nosound = 0;
  190. }
  191. }
  192. }
  193. if (frame)
  194. res = write(sounddev, frame, res * 2);
  195. if (res > 0)
  196. return 0;
  197. return res;
  198. }
  199. return 0;
  200. }
  201. static void *sound_thread(void *unused)
  202. {
  203. fd_set rfds;
  204. fd_set wfds;
  205. int max;
  206. int res;
  207. char ign[4096];
  208. if (read(sounddev, ign, sizeof(sounddev)) < 0)
  209. ast_log(LOG_WARNING, "Read error on sound device: %s\n", strerror(errno));
  210. for(;;) {
  211. FD_ZERO(&rfds);
  212. FD_ZERO(&wfds);
  213. max = sndcmd[0];
  214. FD_SET(sndcmd[0], &rfds);
  215. if (!oss.owner) {
  216. FD_SET(sounddev, &rfds);
  217. if (sounddev > max)
  218. max = sounddev;
  219. }
  220. if (cursound > -1) {
  221. FD_SET(sounddev, &wfds);
  222. if (sounddev > max)
  223. max = sounddev;
  224. }
  225. res = ast_select(max + 1, &rfds, &wfds, NULL, NULL);
  226. if (res < 1) {
  227. ast_log(LOG_WARNING, "select failed: %s\n", strerror(errno));
  228. continue;
  229. }
  230. if (FD_ISSET(sndcmd[0], &rfds)) {
  231. read(sndcmd[0], &cursound, sizeof(cursound));
  232. silencelen = 0;
  233. offset = 0;
  234. sampsent = 0;
  235. }
  236. if (FD_ISSET(sounddev, &rfds)) {
  237. /* Ignore read */
  238. if (read(sounddev, ign, sizeof(ign)) < 0)
  239. ast_log(LOG_WARNING, "Read error on sound device: %s\n", strerror(errno));
  240. }
  241. if (FD_ISSET(sounddev, &wfds))
  242. if (send_sound())
  243. ast_log(LOG_WARNING, "Failed to write sound\n");
  244. }
  245. /* Never reached */
  246. return NULL;
  247. }
  248. #if 0
  249. static int silence_suppress(short *buf)
  250. {
  251. #define SILBUF 3
  252. int loudness;
  253. static int silentframes = 0;
  254. static char silbuf[FRAME_SIZE * 2 * SILBUF];
  255. static int silbufcnt=0;
  256. if (!silencesuppression)
  257. return 0;
  258. loudness = calc_loudness((short *)(buf));
  259. if (option_debug)
  260. ast_log(LOG_DEBUG, "loudness is %d\n", loudness);
  261. if (loudness < silencethreshold) {
  262. silentframes++;
  263. silbufcnt++;
  264. /* Keep track of the last few bits of silence so we can play
  265. them as lead-in when the time is right */
  266. if (silbufcnt >= SILBUF) {
  267. /* Make way for more buffer */
  268. memmove(silbuf, silbuf + FRAME_SIZE * 2, FRAME_SIZE * 2 * (SILBUF - 1));
  269. silbufcnt--;
  270. }
  271. memcpy(silbuf + FRAME_SIZE * 2 * silbufcnt, buf, FRAME_SIZE * 2);
  272. if (silentframes > 10) {
  273. /* We've had plenty of silence, so compress it now */
  274. return 1;
  275. }
  276. } else {
  277. silentframes=0;
  278. /* Write any buffered silence we have, it may have something
  279. important */
  280. if (silbufcnt) {
  281. write(sounddev, silbuf, silbufcnt * FRAME_SIZE);
  282. silbufcnt = 0;
  283. }
  284. }
  285. return 0;
  286. }
  287. #endif
  288. static int setformat(void)
  289. {
  290. int fmt, desired, res, fd = sounddev;
  291. static int warnedalready = 0;
  292. static int warnedalready2 = 0;
  293. fmt = AFMT_S16_LE;
  294. res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
  295. if (res < 0) {
  296. ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
  297. return -1;
  298. }
  299. res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
  300. if (res >= 0) {
  301. if (option_verbose > 1)
  302. ast_verbose(VERBOSE_PREFIX_2 "Console is full duplex\n");
  303. full_duplex = -1;
  304. }
  305. fmt = 0;
  306. res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
  307. if (res < 0) {
  308. ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
  309. return -1;
  310. }
  311. /* 8000 Hz desired */
  312. desired = 8000;
  313. fmt = desired;
  314. res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
  315. if (res < 0) {
  316. ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
  317. return -1;
  318. }
  319. if (fmt != desired) {
  320. if (!warnedalready++)
  321. ast_log(LOG_WARNING, "Requested %d Hz, got %d Hz -- sound may be choppy\n", desired, fmt);
  322. }
  323. #if 1
  324. fmt = BUFFER_FMT;
  325. res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
  326. if (res < 0) {
  327. if (!warnedalready2++)
  328. ast_log(LOG_WARNING, "Unable to set fragment size -- sound may be choppy\n");
  329. }
  330. #endif
  331. return 0;
  332. }
  333. static int soundcard_setoutput(int force)
  334. {
  335. /* Make sure the soundcard is in output mode. */
  336. int fd = sounddev;
  337. if (full_duplex || (!readmode && !force))
  338. return 0;
  339. readmode = 0;
  340. if (force || time_has_passed()) {
  341. ioctl(sounddev, SNDCTL_DSP_RESET);
  342. /* Keep the same fd reserved by closing the sound device and copying stdin at the same
  343. time. */
  344. /* dup2(0, sound); */
  345. close(sounddev);
  346. fd = open(DEV_DSP, O_WRONLY |O_NONBLOCK);
  347. if (fd < 0) {
  348. ast_log(LOG_WARNING, "Unable to re-open DSP device: %s\n", strerror(errno));
  349. return -1;
  350. }
  351. /* dup2 will close the original and make fd be sound */
  352. if (dup2(fd, sounddev) < 0) {
  353. ast_log(LOG_WARNING, "dup2() failed: %s\n", strerror(errno));
  354. return -1;
  355. }
  356. if (setformat()) {
  357. return -1;
  358. }
  359. return 0;
  360. }
  361. return 1;
  362. }
  363. static int soundcard_setinput(int force)
  364. {
  365. int fd = sounddev;
  366. if (full_duplex || (readmode && !force))
  367. return 0;
  368. readmode = -1;
  369. if (force || time_has_passed()) {
  370. ioctl(sounddev, SNDCTL_DSP_RESET);
  371. close(sounddev);
  372. /* dup2(0, sound); */
  373. fd = open(DEV_DSP, O_RDONLY | O_NONBLOCK);
  374. if (fd < 0) {
  375. ast_log(LOG_WARNING, "Unable to re-open DSP device: %s\n", strerror(errno));
  376. return -1;
  377. }
  378. /* dup2 will close the original and make fd be sound */
  379. if (dup2(fd, sounddev) < 0) {
  380. ast_log(LOG_WARNING, "dup2() failed: %s\n", strerror(errno));
  381. return -1;
  382. }
  383. if (setformat()) {
  384. return -1;
  385. }
  386. return 0;
  387. }
  388. return 1;
  389. }
  390. static int soundcard_init(void)
  391. {
  392. /* Assume it's full duplex for starters */
  393. int fd = open(DEV_DSP, O_RDWR | O_NONBLOCK);
  394. if (fd < 0) {
  395. ast_log(LOG_WARNING, "Unable to open %s: %s\n", DEV_DSP, strerror(errno));
  396. return fd;
  397. }
  398. gettimeofday(&lasttime, NULL);
  399. sounddev = fd;
  400. setformat();
  401. if (!full_duplex)
  402. soundcard_setinput(1);
  403. return sounddev;
  404. }
  405. static int oss_digit(struct ast_channel *c, char digit)
  406. {
  407. ast_verbose( " << Console Received digit %c >> \n", digit);
  408. return 0;
  409. }
  410. static int oss_text(struct ast_channel *c, char *text)
  411. {
  412. ast_verbose( " << Console Received text %s >> \n", text);
  413. return 0;
  414. }
  415. static int oss_call(struct ast_channel *c, char *dest, int timeout)
  416. {
  417. int res = 3;
  418. struct ast_frame f = { 0, };
  419. ast_verbose( " << Call placed to '%s' on console >> \n", dest);
  420. if (autoanswer) {
  421. ast_verbose( " << Auto-answered >> \n" );
  422. f.frametype = AST_FRAME_CONTROL;
  423. f.subclass = AST_CONTROL_ANSWER;
  424. ast_queue_frame(c, &f, 0);
  425. } else {
  426. nosound = 1;
  427. ast_verbose( " << Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
  428. f.frametype = AST_FRAME_CONTROL;
  429. f.subclass = AST_CONTROL_RINGING;
  430. ast_queue_frame(c, &f, 0);
  431. write(sndcmd[1], &res, sizeof(res));
  432. }
  433. return 0;
  434. }
  435. static void answer_sound(void)
  436. {
  437. int res;
  438. nosound = 1;
  439. res = 4;
  440. write(sndcmd[1], &res, sizeof(res));
  441. }
  442. static int oss_answer(struct ast_channel *c)
  443. {
  444. ast_verbose( " << Console call has been answered >> \n");
  445. answer_sound();
  446. ast_setstate(c, AST_STATE_UP);
  447. cursound = -1;
  448. nosound=0;
  449. return 0;
  450. }
  451. static int oss_hangup(struct ast_channel *c)
  452. {
  453. int res = 0;
  454. cursound = -1;
  455. c->pvt->pvt = NULL;
  456. oss.owner = NULL;
  457. ast_verbose( " << Hangup on console >> \n");
  458. ast_mutex_lock(&usecnt_lock);
  459. usecnt--;
  460. ast_mutex_unlock(&usecnt_lock);
  461. if (hookstate) {
  462. if (autoanswer) {
  463. /* Assume auto-hangup too */
  464. hookstate = 0;
  465. } else {
  466. /* Make congestion noise */
  467. res = 2;
  468. write(sndcmd[1], &res, sizeof(res));
  469. }
  470. }
  471. return 0;
  472. }
  473. static int soundcard_writeframe(short *data)
  474. {
  475. /* Write an exactly FRAME_SIZE sized of frame */
  476. static int bufcnt = 0;
  477. static short buffer[FRAME_SIZE * MAX_BUFFER_SIZE * 5];
  478. struct audio_buf_info info;
  479. int res;
  480. int fd = sounddev;
  481. static int warned=0;
  482. if (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info)) {
  483. if (!warned)
  484. ast_log(LOG_WARNING, "Error reading output space\n");
  485. bufcnt = buffersize;
  486. warned++;
  487. }
  488. if ((info.fragments >= buffersize * 5) && (bufcnt == buffersize)) {
  489. /* We've run out of stuff, buffer again */
  490. bufcnt = 0;
  491. }
  492. if (bufcnt == buffersize) {
  493. /* Write sample immediately */
  494. res = write(fd, ((void *)data), FRAME_SIZE * 2);
  495. } else {
  496. /* Copy the data into our buffer */
  497. res = FRAME_SIZE * 2;
  498. memcpy(buffer + (bufcnt * FRAME_SIZE), data, FRAME_SIZE * 2);
  499. bufcnt++;
  500. if (bufcnt == buffersize) {
  501. res = write(fd, ((void *)buffer), FRAME_SIZE * 2 * buffersize);
  502. }
  503. }
  504. return res;
  505. }
  506. static int oss_write(struct ast_channel *chan, struct ast_frame *f)
  507. {
  508. int res;
  509. static char sizbuf[8000];
  510. static int sizpos = 0;
  511. int len = sizpos;
  512. int pos;
  513. /* Immediately return if no sound is enabled */
  514. if (nosound)
  515. return 0;
  516. /* Stop any currently playing sound */
  517. cursound = -1;
  518. if (!full_duplex) {
  519. /* If we're half duplex, we have to switch to read mode
  520. to honor immediate needs if necessary */
  521. res = soundcard_setinput(1);
  522. if (res < 0) {
  523. ast_log(LOG_WARNING, "Unable to set device to input mode\n");
  524. return -1;
  525. }
  526. return 0;
  527. }
  528. res = soundcard_setoutput(0);
  529. if (res < 0) {
  530. ast_log(LOG_WARNING, "Unable to set output device\n");
  531. return -1;
  532. } else if (res > 0) {
  533. /* The device is still in read mode, and it's too soon to change it,
  534. so just pretend we wrote it */
  535. return 0;
  536. }
  537. /* We have to digest the frame in 160-byte portions */
  538. if (f->datalen > sizeof(sizbuf) - sizpos) {
  539. ast_log(LOG_WARNING, "Frame too large\n");
  540. return -1;
  541. }
  542. memcpy(sizbuf + sizpos, f->data, f->datalen);
  543. len += f->datalen;
  544. pos = 0;
  545. while(len - pos > FRAME_SIZE * 2) {
  546. soundcard_writeframe((short *)(sizbuf + pos));
  547. pos += FRAME_SIZE * 2;
  548. }
  549. if (len - pos)
  550. memmove(sizbuf, sizbuf + pos, len - pos);
  551. sizpos = len - pos;
  552. return 0;
  553. }
  554. static struct ast_frame *oss_read(struct ast_channel *chan)
  555. {
  556. static struct ast_frame f;
  557. static char buf[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
  558. static int readpos = 0;
  559. int res;
  560. #if 0
  561. ast_log(LOG_DEBUG, "oss_read()\n");
  562. #endif
  563. f.frametype = AST_FRAME_NULL;
  564. f.subclass = 0;
  565. f.samples = 0;
  566. f.datalen = 0;
  567. f.data = NULL;
  568. f.offset = 0;
  569. f.src = type;
  570. f.mallocd = 0;
  571. res = soundcard_setinput(0);
  572. if (res < 0) {
  573. ast_log(LOG_WARNING, "Unable to set input mode\n");
  574. return NULL;
  575. }
  576. if (res > 0) {
  577. /* Theoretically shouldn't happen, but anyway, return a NULL frame */
  578. return &f;
  579. }
  580. res = read(sounddev, buf + AST_FRIENDLY_OFFSET + readpos, FRAME_SIZE * 2 - readpos);
  581. if (res < 0) {
  582. ast_log(LOG_WARNING, "Error reading from sound device (If you're running 'artsd' then kill it): %s\n", strerror(errno));
  583. #if 0
  584. CRASH;
  585. #endif
  586. return NULL;
  587. }
  588. readpos += res;
  589. if (readpos >= FRAME_SIZE * 2) {
  590. /* A real frame */
  591. readpos = 0;
  592. if (chan->_state != AST_STATE_UP) {
  593. /* Don't transmit unless it's up */
  594. return &f;
  595. }
  596. f.frametype = AST_FRAME_VOICE;
  597. f.subclass = AST_FORMAT_SLINEAR;
  598. f.samples = FRAME_SIZE;
  599. f.datalen = FRAME_SIZE * 2;
  600. f.data = buf + AST_FRIENDLY_OFFSET;
  601. f.offset = AST_FRIENDLY_OFFSET;
  602. f.src = type;
  603. f.mallocd = 0;
  604. #if 0
  605. { static int fd = -1;
  606. if (fd < 0)
  607. fd = open("output.raw", O_RDWR | O_TRUNC | O_CREAT);
  608. write(fd, f.data, f.datalen);
  609. }
  610. #endif
  611. }
  612. return &f;
  613. }
  614. static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  615. {
  616. struct chan_oss_pvt *p = newchan->pvt->pvt;
  617. p->owner = newchan;
  618. return 0;
  619. }
  620. static int oss_indicate(struct ast_channel *chan, int cond)
  621. {
  622. int res;
  623. switch(cond) {
  624. case AST_CONTROL_BUSY:
  625. res = 1;
  626. break;
  627. case AST_CONTROL_CONGESTION:
  628. res = 2;
  629. break;
  630. case AST_CONTROL_RINGING:
  631. res = 0;
  632. break;
  633. default:
  634. ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, chan->name);
  635. return -1;
  636. }
  637. if (res > -1) {
  638. write(sndcmd[1], &res, sizeof(res));
  639. }
  640. return 0;
  641. }
  642. static struct ast_channel *oss_new(struct chan_oss_pvt *p, int state)
  643. {
  644. struct ast_channel *tmp;
  645. tmp = ast_channel_alloc(1);
  646. if (tmp) {
  647. snprintf(tmp->name, sizeof(tmp->name), "OSS/%s", DEV_DSP + 5);
  648. tmp->type = type;
  649. tmp->fds[0] = sounddev;
  650. tmp->nativeformats = AST_FORMAT_SLINEAR;
  651. tmp->pvt->pvt = p;
  652. tmp->pvt->send_digit = oss_digit;
  653. tmp->pvt->send_text = oss_text;
  654. tmp->pvt->hangup = oss_hangup;
  655. tmp->pvt->answer = oss_answer;
  656. tmp->pvt->read = oss_read;
  657. tmp->pvt->call = oss_call;
  658. tmp->pvt->write = oss_write;
  659. tmp->pvt->indicate = oss_indicate;
  660. tmp->pvt->fixup = oss_fixup;
  661. if (strlen(p->context))
  662. strncpy(tmp->context, p->context, sizeof(tmp->context)-1);
  663. if (strlen(p->exten))
  664. strncpy(tmp->exten, p->exten, sizeof(tmp->exten)-1);
  665. if (strlen(language))
  666. strncpy(tmp->language, language, sizeof(tmp->language)-1);
  667. p->owner = tmp;
  668. ast_setstate(tmp, state);
  669. ast_mutex_lock(&usecnt_lock);
  670. usecnt++;
  671. ast_mutex_unlock(&usecnt_lock);
  672. ast_update_use_count();
  673. if (state != AST_STATE_DOWN) {
  674. if (ast_pbx_start(tmp)) {
  675. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmp->name);
  676. ast_hangup(tmp);
  677. tmp = NULL;
  678. }
  679. }
  680. }
  681. return tmp;
  682. }
  683. static struct ast_channel *oss_request(char *type, int format, void *data)
  684. {
  685. int oldformat = format;
  686. struct ast_channel *tmp;
  687. format &= AST_FORMAT_SLINEAR;
  688. if (!format) {
  689. ast_log(LOG_NOTICE, "Asked to get a channel of format '%d'\n", oldformat);
  690. return NULL;
  691. }
  692. if (oss.owner) {
  693. ast_log(LOG_NOTICE, "Already have a call on the OSS channel\n");
  694. return NULL;
  695. }
  696. tmp= oss_new(&oss, AST_STATE_DOWN);
  697. if (!tmp) {
  698. ast_log(LOG_WARNING, "Unable to create new OSS channel\n");
  699. }
  700. return tmp;
  701. }
  702. static int console_autoanswer(int fd, int argc, char *argv[])
  703. {
  704. if ((argc != 1) && (argc != 2))
  705. return RESULT_SHOWUSAGE;
  706. if (argc == 1) {
  707. ast_cli(fd, "Auto answer is %s.\n", autoanswer ? "on" : "off");
  708. return RESULT_SUCCESS;
  709. } else {
  710. if (!strcasecmp(argv[1], "on"))
  711. autoanswer = -1;
  712. else if (!strcasecmp(argv[1], "off"))
  713. autoanswer = 0;
  714. else
  715. return RESULT_SHOWUSAGE;
  716. }
  717. return RESULT_SUCCESS;
  718. }
  719. static char *autoanswer_complete(char *line, char *word, int pos, int state)
  720. {
  721. #ifndef MIN
  722. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  723. #endif
  724. switch(state) {
  725. case 0:
  726. if (strlen(word) && !strncasecmp(word, "on", MIN(strlen(word), 2)))
  727. return strdup("on");
  728. case 1:
  729. if (strlen(word) && !strncasecmp(word, "off", MIN(strlen(word), 3)))
  730. return strdup("off");
  731. default:
  732. return NULL;
  733. }
  734. return NULL;
  735. }
  736. static char autoanswer_usage[] =
  737. "Usage: autoanswer [on|off]\n"
  738. " Enables or disables autoanswer feature. If used without\n"
  739. " argument, displays the current on/off status of autoanswer.\n"
  740. " The default value of autoanswer is in 'oss.conf'.\n";
  741. static int console_answer(int fd, int argc, char *argv[])
  742. {
  743. struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
  744. if (argc != 1)
  745. return RESULT_SHOWUSAGE;
  746. if (!oss.owner) {
  747. ast_cli(fd, "No one is calling us\n");
  748. return RESULT_FAILURE;
  749. }
  750. hookstate = 1;
  751. cursound = -1;
  752. ast_queue_frame(oss.owner, &f, 1);
  753. answer_sound();
  754. return RESULT_SUCCESS;
  755. }
  756. static char sendtext_usage[] =
  757. "Usage: send text <message>\n"
  758. " Sends a text message for display on the remote terminal.\n";
  759. static int console_sendtext(int fd, int argc, char *argv[])
  760. {
  761. int tmparg = 2;
  762. char text2send[256];
  763. struct ast_frame f = { 0, };
  764. if (argc < 2)
  765. return RESULT_SHOWUSAGE;
  766. if (!oss.owner) {
  767. ast_cli(fd, "No one is calling us\n");
  768. return RESULT_FAILURE;
  769. }
  770. if (strlen(text2send))
  771. ast_cli(fd, "Warning: message already waiting to be sent, overwriting\n");
  772. strcpy(text2send, "");
  773. while(tmparg < argc) {
  774. strncat(text2send, argv[tmparg++], sizeof(text2send) - strlen(text2send));
  775. strncat(text2send, " ", sizeof(text2send) - strlen(text2send));
  776. }
  777. if (strlen(text2send)) {
  778. f.frametype = AST_FRAME_TEXT;
  779. f.subclass = 0;
  780. f.data = text2send;
  781. f.datalen = strlen(text2send);
  782. ast_queue_frame(oss.owner, &f, 1);
  783. }
  784. return RESULT_SUCCESS;
  785. }
  786. static char answer_usage[] =
  787. "Usage: answer\n"
  788. " Answers an incoming call on the console (OSS) channel.\n";
  789. static int console_hangup(int fd, int argc, char *argv[])
  790. {
  791. if (argc != 1)
  792. return RESULT_SHOWUSAGE;
  793. cursound = -1;
  794. if (!oss.owner && !hookstate) {
  795. ast_cli(fd, "No call to hangup up\n");
  796. return RESULT_FAILURE;
  797. }
  798. hookstate = 0;
  799. if (oss.owner) {
  800. ast_queue_hangup(oss.owner, 1);
  801. }
  802. return RESULT_SUCCESS;
  803. }
  804. static char hangup_usage[] =
  805. "Usage: hangup\n"
  806. " Hangs up any call currently placed on the console.\n";
  807. static int console_dial(int fd, int argc, char *argv[])
  808. {
  809. char tmp[256], *tmp2;
  810. char *mye, *myc;
  811. int x;
  812. struct ast_frame f = { AST_FRAME_DTMF, 0 };
  813. if ((argc != 1) && (argc != 2))
  814. return RESULT_SHOWUSAGE;
  815. if (oss.owner) {
  816. if (argc == 2) {
  817. for (x=0;x<strlen(argv[1]);x++) {
  818. f.subclass = argv[1][x];
  819. ast_queue_frame(oss.owner, &f, 1);
  820. }
  821. } else {
  822. ast_cli(fd, "You're already in a call. You can use this only to dial digits until you hangup\n");
  823. return RESULT_FAILURE;
  824. }
  825. return RESULT_SUCCESS;
  826. }
  827. mye = exten;
  828. myc = context;
  829. if (argc == 2) {
  830. char *stringp=NULL;
  831. strncpy(tmp, argv[1], sizeof(tmp)-1);
  832. stringp=tmp;
  833. strsep(&stringp, "@");
  834. tmp2 = strsep(&stringp, "@");
  835. if (strlen(tmp))
  836. mye = tmp;
  837. if (tmp2 && strlen(tmp2))
  838. myc = tmp2;
  839. }
  840. if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
  841. strncpy(oss.exten, mye, sizeof(oss.exten)-1);
  842. strncpy(oss.context, myc, sizeof(oss.context)-1);
  843. hookstate = 1;
  844. oss_new(&oss, AST_STATE_RINGING);
  845. } else
  846. ast_cli(fd, "No such extension '%s' in context '%s'\n", mye, myc);
  847. return RESULT_SUCCESS;
  848. }
  849. static char dial_usage[] =
  850. "Usage: dial [extension[@context]]\n"
  851. " Dials a given extensison (";
  852. static int console_transfer(int fd, int argc, char *argv[])
  853. {
  854. char tmp[256];
  855. char *context;
  856. if (argc != 2)
  857. return RESULT_SHOWUSAGE;
  858. if (oss.owner && oss.owner->bridge) {
  859. strncpy(tmp, argv[1], sizeof(tmp) - 1);
  860. context = strchr(tmp, '@');
  861. if (context) {
  862. *context = '\0';
  863. context++;
  864. } else
  865. context = oss.owner->context;
  866. if (ast_exists_extension(oss.owner->bridge, context, tmp, 1, oss.owner->bridge->callerid)) {
  867. ast_cli(fd, "Whee, transferring %s to %s@%s.\n",
  868. oss.owner->bridge->name, tmp, context);
  869. if (ast_async_goto(oss.owner->bridge, context, tmp, 1, 1))
  870. ast_cli(fd, "Failed to transfer :(\n");
  871. } else {
  872. ast_cli(fd, "No such extension exists\n");
  873. }
  874. } else {
  875. ast_cli(fd, "There is no call to transfer\n");
  876. }
  877. return RESULT_SUCCESS;
  878. }
  879. static char transfer_usage[] =
  880. "Usage: transfer <extension>[@context]\n"
  881. " Transfers the currently connected call to the given extension (and\n"
  882. "context if specified)\n";
  883. static struct ast_cli_entry myclis[] = {
  884. { { "answer", NULL }, console_answer, "Answer an incoming console call", answer_usage },
  885. { { "hangup", NULL }, console_hangup, "Hangup a call on the console", hangup_usage },
  886. { { "dial", NULL }, console_dial, "Dial an extension on the console", dial_usage },
  887. { { "transfer", NULL }, console_transfer, "Transfer a call to a different extension", transfer_usage },
  888. { { "send", "text", NULL }, console_sendtext, "Send text to the remote device", sendtext_usage },
  889. { { "autoanswer", NULL }, console_autoanswer, "Sets/displays autoanswer", autoanswer_usage, autoanswer_complete }
  890. };
  891. int load_module()
  892. {
  893. int res;
  894. int x;
  895. struct ast_config *cfg;
  896. struct ast_variable *v;
  897. res = pipe(sndcmd);
  898. if (res) {
  899. ast_log(LOG_ERROR, "Unable to create pipe\n");
  900. return -1;
  901. }
  902. res = soundcard_init();
  903. if (res < 0) {
  904. if (option_verbose > 1) {
  905. ast_verbose(VERBOSE_PREFIX_2 "No sound card detected -- console channel will be unavailable\n");
  906. ast_verbose(VERBOSE_PREFIX_2 "Turn off OSS support by adding 'noload=chan_oss.so' in /etc/asterisk/modules.conf\n");
  907. }
  908. return 0;
  909. }
  910. if (!full_duplex)
  911. ast_log(LOG_WARNING, "XXX I don't work right with non-full duplex sound cards XXX\n");
  912. res = ast_channel_register(type, tdesc, AST_FORMAT_SLINEAR, oss_request);
  913. if (res < 0) {
  914. ast_log(LOG_ERROR, "Unable to register channel class '%s'\n", type);
  915. return -1;
  916. }
  917. for (x=0;x<sizeof(myclis)/sizeof(struct ast_cli_entry); x++)
  918. ast_cli_register(myclis + x);
  919. if ((cfg = ast_load(config))) {
  920. v = ast_variable_browse(cfg, "general");
  921. while(v) {
  922. if (!strcasecmp(v->name, "autoanswer"))
  923. autoanswer = ast_true(v->value);
  924. else if (!strcasecmp(v->name, "silencesuppression"))
  925. silencesuppression = ast_true(v->value);
  926. else if (!strcasecmp(v->name, "silencethreshold"))
  927. silencethreshold = atoi(v->value);
  928. else if (!strcasecmp(v->name, "context"))
  929. strncpy(context, v->value, sizeof(context)-1);
  930. else if (!strcasecmp(v->name, "language"))
  931. strncpy(language, v->value, sizeof(language)-1);
  932. else if (!strcasecmp(v->name, "extension"))
  933. strncpy(exten, v->value, sizeof(exten)-1);
  934. v=v->next;
  935. }
  936. ast_destroy(cfg);
  937. }
  938. pthread_create(&sthread, NULL, sound_thread, NULL);
  939. return 0;
  940. }
  941. int unload_module()
  942. {
  943. int x;
  944. for (x=0;x<sizeof(myclis)/sizeof(struct ast_cli_entry); x++)
  945. ast_cli_unregister(myclis + x);
  946. close(sounddev);
  947. if (sndcmd[0] > 0) {
  948. close(sndcmd[0]);
  949. close(sndcmd[1]);
  950. }
  951. if (oss.owner)
  952. ast_softhangup(oss.owner, AST_SOFTHANGUP_APPUNLOAD);
  953. if (oss.owner)
  954. return -1;
  955. return 0;
  956. }
  957. char *description()
  958. {
  959. return desc;
  960. }
  961. int usecount()
  962. {
  963. int res;
  964. ast_mutex_lock(&usecnt_lock);
  965. res = usecnt;
  966. ast_mutex_unlock(&usecnt_lock);
  967. return res;
  968. }
  969. char *key()
  970. {
  971. return ASTERISK_GPL_KEY;
  972. }