chan_oss.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2007, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * FreeBSD changes and multiple device support by Luigi Rizzo, 2005.05.25
  9. * note-this code best seen with ts=8 (8-spaces tabs) in the editor
  10. *
  11. * See http://www.asterisk.org for more information about
  12. * the Asterisk project. Please do not directly contact
  13. * any of the maintainers of this project for assistance;
  14. * the project provides a web site, mailing lists and IRC
  15. * channels for your use.
  16. *
  17. * This program is free software, distributed under the terms of
  18. * the GNU General Public License Version 2. See the LICENSE file
  19. * at the top of the source tree.
  20. */
  21. // #define HAVE_VIDEO_CONSOLE // uncomment to enable video
  22. /*! \file
  23. *
  24. * \brief Channel driver for OSS sound cards
  25. *
  26. * \author Mark Spencer <markster@digium.com>
  27. * \author Luigi Rizzo
  28. *
  29. * \par See also
  30. * \arg \ref Config_oss
  31. *
  32. * \ingroup channel_drivers
  33. */
  34. /*** MODULEINFO
  35. <depend>ossaudio</depend>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include <ctype.h> /* isalnum() used here */
  40. #include <math.h>
  41. #include <sys/ioctl.h>
  42. #ifdef __linux
  43. #include <linux/soundcard.h>
  44. #elif defined(__FreeBSD__) || defined(__CYGWIN__)
  45. #include <sys/soundcard.h>
  46. #else
  47. #include <soundcard.h>
  48. #endif
  49. #include "asterisk/channel.h"
  50. #include "asterisk/file.h"
  51. #include "asterisk/callerid.h"
  52. #include "asterisk/module.h"
  53. #include "asterisk/pbx.h"
  54. #include "asterisk/cli.h"
  55. #include "asterisk/causes.h"
  56. #include "asterisk/musiconhold.h"
  57. #include "asterisk/app.h"
  58. #include "console_video.h"
  59. /*! Global jitterbuffer configuration - by default, jb is disabled */
  60. static struct ast_jb_conf default_jbconf =
  61. {
  62. .flags = 0,
  63. .max_size = -1,
  64. .resync_threshold = -1,
  65. .impl = "",
  66. .target_extra = -1,
  67. };
  68. static struct ast_jb_conf global_jbconf;
  69. /*
  70. * Basic mode of operation:
  71. *
  72. * we have one keyboard (which receives commands from the keyboard)
  73. * and multiple headset's connected to audio cards.
  74. * Cards/Headsets are named as the sections of oss.conf.
  75. * The section called [general] contains the default parameters.
  76. *
  77. * At any time, the keyboard is attached to one card, and you
  78. * can switch among them using the command 'console foo'
  79. * where 'foo' is the name of the card you want.
  80. *
  81. * oss.conf parameters are
  82. START_CONFIG
  83. [general]
  84. ; General config options, with default values shown.
  85. ; You should use one section per device, with [general] being used
  86. ; for the first device and also as a template for other devices.
  87. ;
  88. ; All but 'debug' can go also in the device-specific sections.
  89. ;
  90. ; debug = 0x0 ; misc debug flags, default is 0
  91. ; Set the device to use for I/O
  92. ; device = /dev/dsp
  93. ; Optional mixer command to run upon startup (e.g. to set
  94. ; volume levels, mutes, etc.
  95. ; mixer =
  96. ; Software mic volume booster (or attenuator), useful for sound
  97. ; cards or microphones with poor sensitivity. The volume level
  98. ; is in dB, ranging from -20.0 to +20.0
  99. ; boost = n ; mic volume boost in dB
  100. ; Set the callerid for outgoing calls
  101. ; callerid = John Doe <555-1234>
  102. ; autoanswer = no ; no autoanswer on call
  103. ; autohangup = yes ; hangup when other party closes
  104. ; extension = s ; default extension to call
  105. ; context = default ; default context for outgoing calls
  106. ; language = "" ; default language
  107. ; Default Music on Hold class to use when this channel is placed on hold in
  108. ; the case that the music class is not set on the channel with
  109. ; Set(CHANNEL(musicclass)=whatever) in the dialplan and the peer channel
  110. ; putting this one on hold did not suggest a class to use.
  111. ;
  112. ; mohinterpret=default
  113. ; If you set overridecontext to 'yes', then the whole dial string
  114. ; will be interpreted as an extension, which is extremely useful
  115. ; to dial SIP, IAX and other extensions which use the '@' character.
  116. ; The default is 'no' just for backward compatibility, but the
  117. ; suggestion is to change it.
  118. ; overridecontext = no ; if 'no', the last @ will start the context
  119. ; if 'yes' the whole string is an extension.
  120. ; low level device parameters in case you have problems with the
  121. ; device driver on your operating system. You should not touch these
  122. ; unless you know what you are doing.
  123. ; queuesize = 10 ; frames in device driver
  124. ; frags = 8 ; argument to SETFRAGMENT
  125. ;------------------------------ JITTER BUFFER CONFIGURATION --------------------------
  126. ; jbenable = yes ; Enables the use of a jitterbuffer on the receiving side of an
  127. ; OSS channel. Defaults to "no". An enabled jitterbuffer will
  128. ; be used only if the sending side can create and the receiving
  129. ; side can not accept jitter. The OSS channel can't accept jitter,
  130. ; thus an enabled jitterbuffer on the receive OSS side will always
  131. ; be used if the sending side can create jitter.
  132. ; jbmaxsize = 200 ; Max length of the jitterbuffer in milliseconds.
  133. ; jbresyncthreshold = 1000 ; Jump in the frame timestamps over which the jitterbuffer is
  134. ; resynchronized. Useful to improve the quality of the voice, with
  135. ; big jumps in/broken timestamps, usualy sent from exotic devices
  136. ; and programs. Defaults to 1000.
  137. ; jbimpl = fixed ; Jitterbuffer implementation, used on the receiving side of an OSS
  138. ; channel. Two implementations are currenlty available - "fixed"
  139. ; (with size always equals to jbmax-size) and "adaptive" (with
  140. ; variable size, actually the new jb of IAX2). Defaults to fixed.
  141. ; jblog = no ; Enables jitterbuffer frame logging. Defaults to "no".
  142. ;-----------------------------------------------------------------------------------
  143. [card1]
  144. ; device = /dev/dsp1 ; alternate device
  145. END_CONFIG
  146. .. and so on for the other cards.
  147. */
  148. /*
  149. * The following parameters are used in the driver:
  150. *
  151. * FRAME_SIZE the size of an audio frame, in samples.
  152. * 160 is used almost universally, so you should not change it.
  153. *
  154. * FRAGS the argument for the SETFRAGMENT ioctl.
  155. * Overridden by the 'frags' parameter in oss.conf
  156. *
  157. * Bits 0-7 are the base-2 log of the device's block size,
  158. * bits 16-31 are the number of blocks in the driver's queue.
  159. * There are a lot of differences in the way this parameter
  160. * is supported by different drivers, so you may need to
  161. * experiment a bit with the value.
  162. * A good default for linux is 30 blocks of 64 bytes, which
  163. * results in 6 frames of 320 bytes (160 samples).
  164. * FreeBSD works decently with blocks of 256 or 512 bytes,
  165. * leaving the number unspecified.
  166. * Note that this only refers to the device buffer size,
  167. * this module will then try to keep the lenght of audio
  168. * buffered within small constraints.
  169. *
  170. * QUEUE_SIZE The max number of blocks actually allowed in the device
  171. * driver's buffer, irrespective of the available number.
  172. * Overridden by the 'queuesize' parameter in oss.conf
  173. *
  174. * Should be >=2, and at most as large as the hw queue above
  175. * (otherwise it will never be full).
  176. */
  177. #define FRAME_SIZE 160
  178. #define QUEUE_SIZE 10
  179. #if defined(__FreeBSD__)
  180. #define FRAGS 0x8
  181. #else
  182. #define FRAGS ( ( (6 * 5) << 16 ) | 0x6 )
  183. #endif
  184. /*
  185. * XXX text message sizes are probably 256 chars, but i am
  186. * not sure if there is a suitable definition anywhere.
  187. */
  188. #define TEXT_SIZE 256
  189. #if 0
  190. #define TRYOPEN 1 /* try to open on startup */
  191. #endif
  192. #define O_CLOSE 0x444 /* special 'close' mode for device */
  193. /* Which device to use */
  194. #if defined( __OpenBSD__ ) || defined( __NetBSD__ )
  195. #define DEV_DSP "/dev/audio"
  196. #else
  197. #define DEV_DSP "/dev/dsp"
  198. #endif
  199. static char *config = "oss.conf"; /* default config file */
  200. static int oss_debug;
  201. /*!
  202. * \brief descriptor for one of our channels.
  203. *
  204. * There is one used for 'default' values (from the [general] entry in
  205. * the configuration file), and then one instance for each device
  206. * (the default is cloned from [general], others are only created
  207. * if the relevant section exists).
  208. */
  209. struct chan_oss_pvt {
  210. struct chan_oss_pvt *next;
  211. char *name;
  212. int total_blocks; /*!< total blocks in the output device */
  213. int sounddev;
  214. enum { M_UNSET, M_FULL, M_READ, M_WRITE } duplex;
  215. int autoanswer; /*!< Boolean: whether to answer the immediately upon calling */
  216. int autohangup; /*!< Boolean: whether to hangup the call when the remote end hangs up */
  217. int hookstate; /*!< Boolean: 1 if offhook; 0 if onhook */
  218. char *mixer_cmd; /*!< initial command to issue to the mixer */
  219. unsigned int queuesize; /*!< max fragments in queue */
  220. unsigned int frags; /*!< parameter for SETFRAGMENT */
  221. int warned; /*!< various flags used for warnings */
  222. #define WARN_used_blocks 1
  223. #define WARN_speed 2
  224. #define WARN_frag 4
  225. int w_errors; /*!< overfull in the write path */
  226. struct timeval lastopen;
  227. int overridecontext;
  228. int mute;
  229. /*! boost support. BOOST_SCALE * 10 ^(BOOST_MAX/20) must
  230. * be representable in 16 bits to avoid overflows.
  231. */
  232. #define BOOST_SCALE (1<<9)
  233. #define BOOST_MAX 40 /*!< slightly less than 7 bits */
  234. int boost; /*!< input boost, scaled by BOOST_SCALE */
  235. char device[64]; /*!< device to open */
  236. pthread_t sthread;
  237. struct ast_channel *owner;
  238. struct video_desc *env; /*!< parameters for video support */
  239. char ext[AST_MAX_EXTENSION];
  240. char ctx[AST_MAX_CONTEXT];
  241. char language[MAX_LANGUAGE];
  242. char cid_name[256]; /*!< Initial CallerID name */
  243. char cid_num[256]; /*!< Initial CallerID number */
  244. char mohinterpret[MAX_MUSICCLASS];
  245. /*! buffers used in oss_write */
  246. char oss_write_buf[FRAME_SIZE * 2];
  247. int oss_write_dst;
  248. /*! buffers used in oss_read - AST_FRIENDLY_OFFSET space for headers
  249. * plus enough room for a full frame
  250. */
  251. char oss_read_buf[FRAME_SIZE * 2 + AST_FRIENDLY_OFFSET];
  252. int readpos; /*!< read position above */
  253. struct ast_frame read_f; /*!< returned by oss_read */
  254. };
  255. /*! forward declaration */
  256. static struct chan_oss_pvt *find_desc(char *dev);
  257. static char *oss_active; /*!< the active device */
  258. /*! \brief return the pointer to the video descriptor */
  259. struct video_desc *get_video_desc(struct ast_channel *c)
  260. {
  261. struct chan_oss_pvt *o = c ? c->tech_pvt : find_desc(oss_active);
  262. return o ? o->env : NULL;
  263. }
  264. static struct chan_oss_pvt oss_default = {
  265. .sounddev = -1,
  266. .duplex = M_UNSET, /* XXX check this */
  267. .autoanswer = 1,
  268. .autohangup = 1,
  269. .queuesize = QUEUE_SIZE,
  270. .frags = FRAGS,
  271. .ext = "s",
  272. .ctx = "default",
  273. .readpos = AST_FRIENDLY_OFFSET, /* start here on reads */
  274. .lastopen = { 0, 0 },
  275. .boost = BOOST_SCALE,
  276. };
  277. static int setformat(struct chan_oss_pvt *o, int mode);
  278. static struct ast_channel *oss_request(const char *type, int format, void *data, int *cause);
  279. static int oss_digit_begin(struct ast_channel *c, char digit);
  280. static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration);
  281. static int oss_text(struct ast_channel *c, const char *text);
  282. static int oss_hangup(struct ast_channel *c);
  283. static int oss_answer(struct ast_channel *c);
  284. static struct ast_frame *oss_read(struct ast_channel *chan);
  285. static int oss_call(struct ast_channel *c, char *dest, int timeout);
  286. static int oss_write(struct ast_channel *chan, struct ast_frame *f);
  287. static int oss_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen);
  288. static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  289. static char tdesc[] = "OSS Console Channel Driver";
  290. /* cannot do const because need to update some fields at runtime */
  291. static struct ast_channel_tech oss_tech = {
  292. .type = "Console",
  293. .description = tdesc,
  294. .capabilities = AST_FORMAT_SLINEAR, /* overwritten later */
  295. .requester = oss_request,
  296. .send_digit_begin = oss_digit_begin,
  297. .send_digit_end = oss_digit_end,
  298. .send_text = oss_text,
  299. .hangup = oss_hangup,
  300. .answer = oss_answer,
  301. .read = oss_read,
  302. .call = oss_call,
  303. .write = oss_write,
  304. .write_video = console_write_video,
  305. .indicate = oss_indicate,
  306. .fixup = oss_fixup,
  307. };
  308. /*!
  309. * \brief returns a pointer to the descriptor with the given name
  310. */
  311. static struct chan_oss_pvt *find_desc(char *dev)
  312. {
  313. struct chan_oss_pvt *o = NULL;
  314. if (!dev)
  315. ast_log(LOG_WARNING, "null dev\n");
  316. for (o = oss_default.next; o && o->name && dev && strcmp(o->name, dev) != 0; o = o->next);
  317. if (!o)
  318. ast_log(LOG_WARNING, "could not find <%s>\n", dev ? dev : "--no-device--");
  319. return o;
  320. }
  321. /* !
  322. * \brief split a string in extension-context, returns pointers to malloc'ed
  323. * strings.
  324. *
  325. * If we do not have 'overridecontext' then the last @ is considered as
  326. * a context separator, and the context is overridden.
  327. * This is usually not very necessary as you can play with the dialplan,
  328. * and it is nice not to need it because you have '@' in SIP addresses.
  329. *
  330. * \return the buffer address.
  331. */
  332. static char *ast_ext_ctx(const char *src, char **ext, char **ctx)
  333. {
  334. struct chan_oss_pvt *o = find_desc(oss_active);
  335. if (ext == NULL || ctx == NULL)
  336. return NULL; /* error */
  337. *ext = *ctx = NULL;
  338. if (src && *src != '\0')
  339. *ext = ast_strdup(src);
  340. if (*ext == NULL)
  341. return NULL;
  342. if (!o->overridecontext) {
  343. /* parse from the right */
  344. *ctx = strrchr(*ext, '@');
  345. if (*ctx)
  346. *(*ctx)++ = '\0';
  347. }
  348. return *ext;
  349. }
  350. /*!
  351. * \brief Returns the number of blocks used in the audio output channel
  352. */
  353. static int used_blocks(struct chan_oss_pvt *o)
  354. {
  355. struct audio_buf_info info;
  356. if (ioctl(o->sounddev, SNDCTL_DSP_GETOSPACE, &info)) {
  357. if (!(o->warned & WARN_used_blocks)) {
  358. ast_log(LOG_WARNING, "Error reading output space\n");
  359. o->warned |= WARN_used_blocks;
  360. }
  361. return 1;
  362. }
  363. if (o->total_blocks == 0) {
  364. if (0) /* debugging */
  365. ast_log(LOG_WARNING, "fragtotal %d size %d avail %d\n", info.fragstotal, info.fragsize, info.fragments);
  366. o->total_blocks = info.fragments;
  367. }
  368. return o->total_blocks - info.fragments;
  369. }
  370. /*! Write an exactly FRAME_SIZE sized frame */
  371. static int soundcard_writeframe(struct chan_oss_pvt *o, short *data)
  372. {
  373. int res;
  374. if (o->sounddev < 0)
  375. setformat(o, O_RDWR);
  376. if (o->sounddev < 0)
  377. return 0; /* not fatal */
  378. /*
  379. * Nothing complex to manage the audio device queue.
  380. * If the buffer is full just drop the extra, otherwise write.
  381. * XXX in some cases it might be useful to write anyways after
  382. * a number of failures, to restart the output chain.
  383. */
  384. res = used_blocks(o);
  385. if (res > o->queuesize) { /* no room to write a block */
  386. if (o->w_errors++ == 0 && (oss_debug & 0x4))
  387. ast_log(LOG_WARNING, "write: used %d blocks (%d)\n", res, o->w_errors);
  388. return 0;
  389. }
  390. o->w_errors = 0;
  391. return write(o->sounddev, (void *)data, FRAME_SIZE * 2);
  392. }
  393. /*!
  394. * reset and close the device if opened,
  395. * then open and initialize it in the desired mode,
  396. * trigger reads and writes so we can start using it.
  397. */
  398. static int setformat(struct chan_oss_pvt *o, int mode)
  399. {
  400. int fmt, desired, res, fd;
  401. if (o->sounddev >= 0) {
  402. ioctl(o->sounddev, SNDCTL_DSP_RESET, 0);
  403. close(o->sounddev);
  404. o->duplex = M_UNSET;
  405. o->sounddev = -1;
  406. }
  407. if (mode == O_CLOSE) /* we are done */
  408. return 0;
  409. if (ast_tvdiff_ms(ast_tvnow(), o->lastopen) < 1000)
  410. return -1; /* don't open too often */
  411. o->lastopen = ast_tvnow();
  412. fd = o->sounddev = open(o->device, mode | O_NONBLOCK);
  413. if (fd < 0) {
  414. ast_log(LOG_WARNING, "Unable to re-open DSP device %s: %s\n", o->device, strerror(errno));
  415. return -1;
  416. }
  417. if (o->owner)
  418. ast_channel_set_fd(o->owner, 0, fd);
  419. #if __BYTE_ORDER == __LITTLE_ENDIAN
  420. fmt = AFMT_S16_LE;
  421. #else
  422. fmt = AFMT_S16_BE;
  423. #endif
  424. res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
  425. if (res < 0) {
  426. ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
  427. return -1;
  428. }
  429. switch (mode) {
  430. case O_RDWR:
  431. res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
  432. /* Check to see if duplex set (FreeBSD Bug) */
  433. res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt);
  434. if (res == 0 && (fmt & DSP_CAP_DUPLEX)) {
  435. ast_verb(2, "Console is full duplex\n");
  436. o->duplex = M_FULL;
  437. };
  438. break;
  439. case O_WRONLY:
  440. o->duplex = M_WRITE;
  441. break;
  442. case O_RDONLY:
  443. o->duplex = M_READ;
  444. break;
  445. }
  446. fmt = 0;
  447. res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
  448. if (res < 0) {
  449. ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
  450. return -1;
  451. }
  452. fmt = desired = DEFAULT_SAMPLE_RATE; /* 8000 Hz desired */
  453. res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
  454. if (res < 0) {
  455. ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
  456. return -1;
  457. }
  458. if (fmt != desired) {
  459. if (!(o->warned & WARN_speed)) {
  460. ast_log(LOG_WARNING,
  461. "Requested %d Hz, got %d Hz -- sound may be choppy\n",
  462. desired, fmt);
  463. o->warned |= WARN_speed;
  464. }
  465. }
  466. /*
  467. * on Freebsd, SETFRAGMENT does not work very well on some cards.
  468. * Default to use 256 bytes, let the user override
  469. */
  470. if (o->frags) {
  471. fmt = o->frags;
  472. res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
  473. if (res < 0) {
  474. if (!(o->warned & WARN_frag)) {
  475. ast_log(LOG_WARNING,
  476. "Unable to set fragment size -- sound may be choppy\n");
  477. o->warned |= WARN_frag;
  478. }
  479. }
  480. }
  481. /* on some cards, we need SNDCTL_DSP_SETTRIGGER to start outputting */
  482. res = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
  483. res = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &res);
  484. /* it may fail if we are in half duplex, never mind */
  485. return 0;
  486. }
  487. /*
  488. * some of the standard methods supported by channels.
  489. */
  490. static int oss_digit_begin(struct ast_channel *c, char digit)
  491. {
  492. return 0;
  493. }
  494. static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration)
  495. {
  496. /* no better use for received digits than print them */
  497. ast_verbose(" << Console Received digit %c of duration %u ms >> \n",
  498. digit, duration);
  499. return 0;
  500. }
  501. static int oss_text(struct ast_channel *c, const char *text)
  502. {
  503. /* print received messages */
  504. ast_verbose(" << Console Received text %s >> \n", text);
  505. return 0;
  506. }
  507. /*!
  508. * \brief handler for incoming calls. Either autoanswer, or start ringing
  509. */
  510. static int oss_call(struct ast_channel *c, char *dest, int timeout)
  511. {
  512. struct chan_oss_pvt *o = c->tech_pvt;
  513. struct ast_frame f = { 0, };
  514. AST_DECLARE_APP_ARGS(args,
  515. AST_APP_ARG(name);
  516. AST_APP_ARG(flags);
  517. );
  518. char *parse = ast_strdupa(dest);
  519. AST_NONSTANDARD_APP_ARGS(args, parse, '/');
  520. 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);
  521. if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "answer") == 0) {
  522. f.frametype = AST_FRAME_CONTROL;
  523. f.subclass = AST_CONTROL_ANSWER;
  524. ast_queue_frame(c, &f);
  525. } else if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "noanswer") == 0) {
  526. f.frametype = AST_FRAME_CONTROL;
  527. f.subclass = AST_CONTROL_RINGING;
  528. ast_queue_frame(c, &f);
  529. ast_indicate(c, AST_CONTROL_RINGING);
  530. } else if (o->autoanswer) {
  531. ast_verbose(" << Auto-answered >> \n");
  532. f.frametype = AST_FRAME_CONTROL;
  533. f.subclass = AST_CONTROL_ANSWER;
  534. ast_queue_frame(c, &f);
  535. o->hookstate = 1;
  536. } else {
  537. ast_verbose("<< Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
  538. f.frametype = AST_FRAME_CONTROL;
  539. f.subclass = AST_CONTROL_RINGING;
  540. ast_queue_frame(c, &f);
  541. ast_indicate(c, AST_CONTROL_RINGING);
  542. }
  543. return 0;
  544. }
  545. /*!
  546. * \brief remote side answered the phone
  547. */
  548. static int oss_answer(struct ast_channel *c)
  549. {
  550. struct chan_oss_pvt *o = c->tech_pvt;
  551. ast_verbose(" << Console call has been answered >> \n");
  552. ast_setstate(c, AST_STATE_UP);
  553. o->hookstate = 1;
  554. return 0;
  555. }
  556. static int oss_hangup(struct ast_channel *c)
  557. {
  558. struct chan_oss_pvt *o = c->tech_pvt;
  559. c->tech_pvt = NULL;
  560. o->owner = NULL;
  561. ast_verbose(" << Hangup on console >> \n");
  562. console_video_uninit(o->env);
  563. ast_module_unref(ast_module_info->self);
  564. if (o->hookstate) {
  565. if (o->autoanswer || o->autohangup) {
  566. /* Assume auto-hangup too */
  567. o->hookstate = 0;
  568. setformat(o, O_CLOSE);
  569. }
  570. }
  571. return 0;
  572. }
  573. /*! \brief used for data coming from the network */
  574. static int oss_write(struct ast_channel *c, struct ast_frame *f)
  575. {
  576. int src;
  577. struct chan_oss_pvt *o = c->tech_pvt;
  578. /*
  579. * we could receive a block which is not a multiple of our
  580. * FRAME_SIZE, so buffer it locally and write to the device
  581. * in FRAME_SIZE chunks.
  582. * Keep the residue stored for future use.
  583. */
  584. src = 0; /* read position into f->data */
  585. while (src < f->datalen) {
  586. /* Compute spare room in the buffer */
  587. int l = sizeof(o->oss_write_buf) - o->oss_write_dst;
  588. if (f->datalen - src >= l) { /* enough to fill a frame */
  589. memcpy(o->oss_write_buf + o->oss_write_dst, f->data.ptr + src, l);
  590. soundcard_writeframe(o, (short *) o->oss_write_buf);
  591. src += l;
  592. o->oss_write_dst = 0;
  593. } else { /* copy residue */
  594. l = f->datalen - src;
  595. memcpy(o->oss_write_buf + o->oss_write_dst, f->data.ptr + src, l);
  596. src += l; /* but really, we are done */
  597. o->oss_write_dst += l;
  598. }
  599. }
  600. return 0;
  601. }
  602. static struct ast_frame *oss_read(struct ast_channel *c)
  603. {
  604. int res;
  605. struct chan_oss_pvt *o = c->tech_pvt;
  606. struct ast_frame *f = &o->read_f;
  607. /* XXX can be simplified returning &ast_null_frame */
  608. /* prepare a NULL frame in case we don't have enough data to return */
  609. memset(f, '\0', sizeof(struct ast_frame));
  610. f->frametype = AST_FRAME_NULL;
  611. f->src = oss_tech.type;
  612. res = read(o->sounddev, o->oss_read_buf + o->readpos, sizeof(o->oss_read_buf) - o->readpos);
  613. if (res < 0) /* audio data not ready, return a NULL frame */
  614. return f;
  615. o->readpos += res;
  616. if (o->readpos < sizeof(o->oss_read_buf)) /* not enough samples */
  617. return f;
  618. if (o->mute)
  619. return f;
  620. o->readpos = AST_FRIENDLY_OFFSET; /* reset read pointer for next frame */
  621. if (c->_state != AST_STATE_UP) /* drop data if frame is not up */
  622. return f;
  623. /* ok we can build and deliver the frame to the caller */
  624. f->frametype = AST_FRAME_VOICE;
  625. f->subclass = AST_FORMAT_SLINEAR;
  626. f->samples = FRAME_SIZE;
  627. f->datalen = FRAME_SIZE * 2;
  628. f->data.ptr = o->oss_read_buf + AST_FRIENDLY_OFFSET;
  629. if (o->boost != BOOST_SCALE) { /* scale and clip values */
  630. int i, x;
  631. int16_t *p = (int16_t *) f->data.ptr;
  632. for (i = 0; i < f->samples; i++) {
  633. x = (p[i] * o->boost) / BOOST_SCALE;
  634. if (x > 32767)
  635. x = 32767;
  636. else if (x < -32768)
  637. x = -32768;
  638. p[i] = x;
  639. }
  640. }
  641. f->offset = AST_FRIENDLY_OFFSET;
  642. return f;
  643. }
  644. static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  645. {
  646. struct chan_oss_pvt *o = newchan->tech_pvt;
  647. o->owner = newchan;
  648. return 0;
  649. }
  650. static int oss_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen)
  651. {
  652. struct chan_oss_pvt *o = c->tech_pvt;
  653. int res = 0;
  654. switch (cond) {
  655. case AST_CONTROL_BUSY:
  656. case AST_CONTROL_CONGESTION:
  657. case AST_CONTROL_RINGING:
  658. case -1:
  659. res = -1;
  660. break;
  661. case AST_CONTROL_PROGRESS:
  662. case AST_CONTROL_PROCEEDING:
  663. case AST_CONTROL_VIDUPDATE:
  664. case AST_CONTROL_SRCUPDATE:
  665. break;
  666. case AST_CONTROL_HOLD:
  667. ast_verbose(" << Console Has Been Placed on Hold >> \n");
  668. ast_moh_start(c, data, o->mohinterpret);
  669. break;
  670. case AST_CONTROL_UNHOLD:
  671. ast_verbose(" << Console Has Been Retrieved from Hold >> \n");
  672. ast_moh_stop(c);
  673. break;
  674. default:
  675. ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n", cond, c->name);
  676. return -1;
  677. }
  678. return res;
  679. }
  680. /*!
  681. * \brief allocate a new channel.
  682. */
  683. static struct ast_channel *oss_new(struct chan_oss_pvt *o, char *ext, char *ctx, int state)
  684. {
  685. struct ast_channel *c;
  686. c = ast_channel_alloc(1, state, o->cid_num, o->cid_name, "", ext, ctx, 0, "Console/%s", o->device + 5);
  687. if (c == NULL)
  688. return NULL;
  689. c->tech = &oss_tech;
  690. if (o->sounddev < 0)
  691. setformat(o, O_RDWR);
  692. ast_channel_set_fd(c, 0, o->sounddev); /* -1 if device closed, override later */
  693. c->nativeformats = AST_FORMAT_SLINEAR;
  694. /* if the console makes the call, add video to the offer */
  695. if (state == AST_STATE_RINGING)
  696. c->nativeformats |= console_video_formats;
  697. c->readformat = AST_FORMAT_SLINEAR;
  698. c->writeformat = AST_FORMAT_SLINEAR;
  699. c->tech_pvt = o;
  700. if (!ast_strlen_zero(o->language))
  701. ast_string_field_set(c, language, o->language);
  702. /* Don't use ast_set_callerid() here because it will
  703. * generate a needless NewCallerID event */
  704. c->cid.cid_ani = ast_strdup(o->cid_num);
  705. if (!ast_strlen_zero(ext))
  706. c->cid.cid_dnid = ast_strdup(ext);
  707. o->owner = c;
  708. ast_module_ref(ast_module_info->self);
  709. ast_jb_configure(c, &global_jbconf);
  710. if (state != AST_STATE_DOWN) {
  711. if (ast_pbx_start(c)) {
  712. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", c->name);
  713. ast_hangup(c);
  714. o->owner = c = NULL;
  715. }
  716. }
  717. console_video_start(get_video_desc(c), c); /* XXX cleanup */
  718. return c;
  719. }
  720. static struct ast_channel *oss_request(const char *type, int format, void *data, int *cause)
  721. {
  722. struct ast_channel *c;
  723. struct chan_oss_pvt *o;
  724. AST_DECLARE_APP_ARGS(args,
  725. AST_APP_ARG(name);
  726. AST_APP_ARG(flags);
  727. );
  728. char *parse = ast_strdupa(data);
  729. AST_NONSTANDARD_APP_ARGS(args, parse, '/');
  730. o = find_desc(args.name);
  731. ast_log(LOG_WARNING, "oss_request ty <%s> data 0x%p <%s>\n", type, data, (char *) data);
  732. if (o == NULL) {
  733. ast_log(LOG_NOTICE, "Device %s not found\n", args.name);
  734. /* XXX we could default to 'dsp' perhaps ? */
  735. return NULL;
  736. }
  737. if ((format & AST_FORMAT_SLINEAR) == 0) {
  738. ast_log(LOG_NOTICE, "Format 0x%x unsupported\n", format);
  739. return NULL;
  740. }
  741. if (o->owner) {
  742. ast_log(LOG_NOTICE, "Already have a call (chan %p) on the OSS channel\n", o->owner);
  743. *cause = AST_CAUSE_BUSY;
  744. return NULL;
  745. }
  746. c = oss_new(o, NULL, NULL, AST_STATE_DOWN);
  747. if (c == NULL) {
  748. ast_log(LOG_WARNING, "Unable to create new OSS channel\n");
  749. return NULL;
  750. }
  751. return c;
  752. }
  753. static void store_config_core(struct chan_oss_pvt *o, const char *var, const char *value);
  754. /*! Generic console command handler. Basically a wrapper for a subset
  755. * of config file options which are also available from the CLI
  756. */
  757. static char *console_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  758. {
  759. struct chan_oss_pvt *o = find_desc(oss_active);
  760. const char *var, *value;
  761. switch (cmd) {
  762. case CLI_INIT:
  763. e->command = CONSOLE_VIDEO_CMDS;
  764. e->usage =
  765. "Usage: " CONSOLE_VIDEO_CMDS "...\n"
  766. " Generic handler for console commands.\n";
  767. return NULL;
  768. case CLI_GENERATE:
  769. return NULL;
  770. }
  771. if (a->argc < e->args)
  772. return CLI_SHOWUSAGE;
  773. if (o == NULL) {
  774. ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n",
  775. oss_active);
  776. return CLI_FAILURE;
  777. }
  778. var = a->argv[e->args-1];
  779. value = a->argc > e->args ? a->argv[e->args] : NULL;
  780. if (value) /* handle setting */
  781. store_config_core(o, var, value);
  782. if (!console_video_cli(o->env, var, a->fd)) /* print video-related values */
  783. return CLI_SUCCESS;
  784. /* handle other values */
  785. if (!strcasecmp(var, "device")) {
  786. ast_cli(a->fd, "device is [%s]\n", o->device);
  787. }
  788. return CLI_SUCCESS;
  789. }
  790. static char *console_autoanswer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  791. {
  792. struct chan_oss_pvt *o = find_desc(oss_active);
  793. switch (cmd) {
  794. case CLI_INIT:
  795. e->command = "console autoanswer [on|off]";
  796. e->usage =
  797. "Usage: console autoanswer [on|off]\n"
  798. " Enables or disables autoanswer feature. If used without\n"
  799. " argument, displays the current on/off status of autoanswer.\n"
  800. " The default value of autoanswer is in 'oss.conf'.\n";
  801. return NULL;
  802. case CLI_GENERATE:
  803. return NULL;
  804. }
  805. if (a->argc == e->args - 1) {
  806. ast_cli(a->fd, "Auto answer is %s.\n", o->autoanswer ? "on" : "off");
  807. return CLI_SUCCESS;
  808. }
  809. if (a->argc != e->args)
  810. return CLI_SHOWUSAGE;
  811. if (o == NULL) {
  812. ast_log(LOG_WARNING, "Cannot find device %s (should not happen!)\n",
  813. oss_active);
  814. return CLI_FAILURE;
  815. }
  816. if (!strcasecmp(a->argv[e->args-1], "on"))
  817. o->autoanswer = 1;
  818. else if (!strcasecmp(a->argv[e->args - 1], "off"))
  819. o->autoanswer = 0;
  820. else
  821. return CLI_SHOWUSAGE;
  822. return CLI_SUCCESS;
  823. }
  824. /*! \brief helper function for the answer key/cli command */
  825. static char *console_do_answer(int fd)
  826. {
  827. struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_ANSWER };
  828. struct chan_oss_pvt *o = find_desc(oss_active);
  829. if (!o->owner) {
  830. if (fd > -1)
  831. ast_cli(fd, "No one is calling us\n");
  832. return CLI_FAILURE;
  833. }
  834. o->hookstate = 1;
  835. ast_queue_frame(o->owner, &f);
  836. return CLI_SUCCESS;
  837. }
  838. /*!
  839. * \brief answer command from the console
  840. */
  841. static char *console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  842. {
  843. switch (cmd) {
  844. case CLI_INIT:
  845. e->command = "console answer";
  846. e->usage =
  847. "Usage: console answer\n"
  848. " Answers an incoming call on the console (OSS) channel.\n";
  849. return NULL;
  850. case CLI_GENERATE:
  851. return NULL; /* no completion */
  852. }
  853. if (a->argc != e->args)
  854. return CLI_SHOWUSAGE;
  855. return console_do_answer(a->fd);
  856. }
  857. /*!
  858. * \brief Console send text CLI command
  859. *
  860. * \note concatenate all arguments into a single string. argv is NULL-terminated
  861. * so we can use it right away
  862. */
  863. static char *console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  864. {
  865. struct chan_oss_pvt *o = find_desc(oss_active);
  866. char buf[TEXT_SIZE];
  867. if (cmd == CLI_INIT) {
  868. e->command = "console send text";
  869. e->usage =
  870. "Usage: console send text <message>\n"
  871. " Sends a text message for display on the remote terminal.\n";
  872. return NULL;
  873. } else if (cmd == CLI_GENERATE)
  874. return NULL;
  875. if (a->argc < e->args + 1)
  876. return CLI_SHOWUSAGE;
  877. if (!o->owner) {
  878. ast_cli(a->fd, "Not in a call\n");
  879. return CLI_FAILURE;
  880. }
  881. ast_join(buf, sizeof(buf) - 1, a->argv + e->args);
  882. if (!ast_strlen_zero(buf)) {
  883. struct ast_frame f = { 0, };
  884. int i = strlen(buf);
  885. buf[i] = '\n';
  886. f.frametype = AST_FRAME_TEXT;
  887. f.subclass = 0;
  888. f.data.ptr = buf;
  889. f.datalen = i + 1;
  890. ast_queue_frame(o->owner, &f);
  891. }
  892. return CLI_SUCCESS;
  893. }
  894. static char *console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  895. {
  896. struct chan_oss_pvt *o = find_desc(oss_active);
  897. if (cmd == CLI_INIT) {
  898. e->command = "console hangup";
  899. e->usage =
  900. "Usage: console hangup\n"
  901. " Hangs up any call currently placed on the console.\n";
  902. return NULL;
  903. } else if (cmd == CLI_GENERATE)
  904. return NULL;
  905. if (a->argc != e->args)
  906. return CLI_SHOWUSAGE;
  907. if (!o->owner && !o->hookstate) { /* XXX maybe only one ? */
  908. ast_cli(a->fd, "No call to hang up\n");
  909. return CLI_FAILURE;
  910. }
  911. o->hookstate = 0;
  912. if (o->owner)
  913. ast_queue_hangup_with_cause(o->owner, AST_CAUSE_NORMAL_CLEARING);
  914. setformat(o, O_CLOSE);
  915. return CLI_SUCCESS;
  916. }
  917. static char *console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  918. {
  919. struct ast_frame f = { AST_FRAME_CONTROL, AST_CONTROL_FLASH };
  920. struct chan_oss_pvt *o = find_desc(oss_active);
  921. if (cmd == CLI_INIT) {
  922. e->command = "console flash";
  923. e->usage =
  924. "Usage: console flash\n"
  925. " Flashes the call currently placed on the console.\n";
  926. return NULL;
  927. } else if (cmd == CLI_GENERATE)
  928. return NULL;
  929. if (a->argc != e->args)
  930. return CLI_SHOWUSAGE;
  931. if (!o->owner) { /* XXX maybe !o->hookstate too ? */
  932. ast_cli(a->fd, "No call to flash\n");
  933. return CLI_FAILURE;
  934. }
  935. o->hookstate = 0;
  936. if (o->owner)
  937. ast_queue_frame(o->owner, &f);
  938. return CLI_SUCCESS;
  939. }
  940. static char *console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  941. {
  942. char *s = NULL, *mye = NULL, *myc = NULL;
  943. struct chan_oss_pvt *o = find_desc(oss_active);
  944. if (cmd == CLI_INIT) {
  945. e->command = "console dial";
  946. e->usage =
  947. "Usage: console dial [extension[@context]]\n"
  948. " Dials a given extension (and context if specified)\n";
  949. return NULL;
  950. } else if (cmd == CLI_GENERATE)
  951. return NULL;
  952. if (a->argc > e->args + 1)
  953. return CLI_SHOWUSAGE;
  954. if (o->owner) { /* already in a call */
  955. int i;
  956. struct ast_frame f = { AST_FRAME_DTMF, 0 };
  957. if (a->argc == e->args) { /* argument is mandatory here */
  958. ast_cli(a->fd, "Already in a call. You can only dial digits until you hangup.\n");
  959. return CLI_FAILURE;
  960. }
  961. s = a->argv[e->args];
  962. /* send the string one char at a time */
  963. for (i = 0; i < strlen(s); i++) {
  964. f.subclass = s[i];
  965. ast_queue_frame(o->owner, &f);
  966. }
  967. return CLI_SUCCESS;
  968. }
  969. /* if we have an argument split it into extension and context */
  970. if (a->argc == e->args + 1)
  971. s = ast_ext_ctx(a->argv[e->args], &mye, &myc);
  972. /* supply default values if needed */
  973. if (mye == NULL)
  974. mye = o->ext;
  975. if (myc == NULL)
  976. myc = o->ctx;
  977. if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
  978. o->hookstate = 1;
  979. oss_new(o, mye, myc, AST_STATE_RINGING);
  980. } else
  981. ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
  982. if (s)
  983. ast_free(s);
  984. return CLI_SUCCESS;
  985. }
  986. static char *console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  987. {
  988. struct chan_oss_pvt *o = find_desc(oss_active);
  989. char *s;
  990. int toggle = 0;
  991. if (cmd == CLI_INIT) {
  992. e->command = "console {mute|unmute} [toggle]";
  993. e->usage =
  994. "Usage: console {mute|unmute} [toggle]\n"
  995. " Mute/unmute the microphone.\n";
  996. return NULL;
  997. } else if (cmd == CLI_GENERATE)
  998. return NULL;
  999. if (a->argc > e->args)
  1000. return CLI_SHOWUSAGE;
  1001. if (a->argc == e->args) {
  1002. if (strcasecmp(a->argv[e->args-1], "toggle"))
  1003. return CLI_SHOWUSAGE;
  1004. toggle = 1;
  1005. }
  1006. s = a->argv[e->args-2];
  1007. if (!strcasecmp(s, "mute"))
  1008. o->mute = toggle ? !o->mute : 1;
  1009. else if (!strcasecmp(s, "unmute"))
  1010. o->mute = toggle ? !o->mute : 0;
  1011. else
  1012. return CLI_SHOWUSAGE;
  1013. ast_cli(a->fd, "Console mic is %s\n", o->mute ? "off" : "on");
  1014. return CLI_SUCCESS;
  1015. }
  1016. static char *console_transfer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1017. {
  1018. struct chan_oss_pvt *o = find_desc(oss_active);
  1019. struct ast_channel *b = NULL;
  1020. char *tmp, *ext, *ctx;
  1021. switch (cmd) {
  1022. case CLI_INIT:
  1023. e->command = "console transfer";
  1024. e->usage =
  1025. "Usage: console transfer <extension>[@context]\n"
  1026. " Transfers the currently connected call to the given extension (and\n"
  1027. " context if specified)\n";
  1028. return NULL;
  1029. case CLI_GENERATE:
  1030. return NULL;
  1031. }
  1032. if (a->argc != 3)
  1033. return CLI_SHOWUSAGE;
  1034. if (o == NULL)
  1035. return CLI_FAILURE;
  1036. if (o->owner == NULL || (b = ast_bridged_channel(o->owner)) == NULL) {
  1037. ast_cli(a->fd, "There is no call to transfer\n");
  1038. return CLI_SUCCESS;
  1039. }
  1040. tmp = ast_ext_ctx(a->argv[2], &ext, &ctx);
  1041. if (ctx == NULL) /* supply default context if needed */
  1042. ctx = o->owner->context;
  1043. if (!ast_exists_extension(b, ctx, ext, 1, b->cid.cid_num))
  1044. ast_cli(a->fd, "No such extension exists\n");
  1045. else {
  1046. ast_cli(a->fd, "Whee, transferring %s to %s@%s.\n", b->name, ext, ctx);
  1047. if (ast_async_goto(b, ctx, ext, 1))
  1048. ast_cli(a->fd, "Failed to transfer :(\n");
  1049. }
  1050. if (tmp)
  1051. ast_free(tmp);
  1052. return CLI_SUCCESS;
  1053. }
  1054. static char *console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1055. {
  1056. switch (cmd) {
  1057. case CLI_INIT:
  1058. e->command = "console active";
  1059. e->usage =
  1060. "Usage: console active [device]\n"
  1061. " If used without a parameter, displays which device is the current\n"
  1062. " console. If a device is specified, the console sound device is changed to\n"
  1063. " the device specified.\n";
  1064. return NULL;
  1065. case CLI_GENERATE:
  1066. return NULL;
  1067. }
  1068. if (a->argc == 2)
  1069. ast_cli(a->fd, "active console is [%s]\n", oss_active);
  1070. else if (a->argc != 3)
  1071. return CLI_SHOWUSAGE;
  1072. else {
  1073. struct chan_oss_pvt *o;
  1074. if (strcmp(a->argv[2], "show") == 0) {
  1075. for (o = oss_default.next; o; o = o->next)
  1076. ast_cli(a->fd, "device [%s] exists\n", o->name);
  1077. return CLI_SUCCESS;
  1078. }
  1079. o = find_desc(a->argv[2]);
  1080. if (o == NULL)
  1081. ast_cli(a->fd, "No device [%s] exists\n", a->argv[2]);
  1082. else
  1083. oss_active = o->name;
  1084. }
  1085. return CLI_SUCCESS;
  1086. }
  1087. /*!
  1088. * \brief store the boost factor
  1089. */
  1090. static void store_boost(struct chan_oss_pvt *o, const char *s)
  1091. {
  1092. double boost = 0;
  1093. if (sscanf(s, "%30lf", &boost) != 1) {
  1094. ast_log(LOG_WARNING, "invalid boost <%s>\n", s);
  1095. return;
  1096. }
  1097. if (boost < -BOOST_MAX) {
  1098. ast_log(LOG_WARNING, "boost %s too small, using %d\n", s, -BOOST_MAX);
  1099. boost = -BOOST_MAX;
  1100. } else if (boost > BOOST_MAX) {
  1101. ast_log(LOG_WARNING, "boost %s too large, using %d\n", s, BOOST_MAX);
  1102. boost = BOOST_MAX;
  1103. }
  1104. boost = exp(log(10) * boost / 20) * BOOST_SCALE;
  1105. o->boost = boost;
  1106. ast_log(LOG_WARNING, "setting boost %s to %d\n", s, o->boost);
  1107. }
  1108. static char *console_boost(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  1109. {
  1110. struct chan_oss_pvt *o = find_desc(oss_active);
  1111. switch (cmd) {
  1112. case CLI_INIT:
  1113. e->command = "console boost";
  1114. e->usage =
  1115. "Usage: console boost [boost in dB]\n"
  1116. " Sets or display mic boost in dB\n";
  1117. return NULL;
  1118. case CLI_GENERATE:
  1119. return NULL;
  1120. }
  1121. if (a->argc == 2)
  1122. ast_cli(a->fd, "boost currently %5.1f\n", 20 * log10(((double) o->boost / (double) BOOST_SCALE)));
  1123. else if (a->argc == 3)
  1124. store_boost(o, a->argv[2]);
  1125. return CLI_SUCCESS;
  1126. }
  1127. static struct ast_cli_entry cli_oss[] = {
  1128. AST_CLI_DEFINE(console_answer, "Answer an incoming console call"),
  1129. AST_CLI_DEFINE(console_hangup, "Hangup a call on the console"),
  1130. AST_CLI_DEFINE(console_flash, "Flash a call on the console"),
  1131. AST_CLI_DEFINE(console_dial, "Dial an extension on the console"),
  1132. AST_CLI_DEFINE(console_mute, "Disable/Enable mic input"),
  1133. AST_CLI_DEFINE(console_transfer, "Transfer a call to a different extension"),
  1134. AST_CLI_DEFINE(console_cmd, "Generic console command"),
  1135. AST_CLI_DEFINE(console_sendtext, "Send text to the remote device"),
  1136. AST_CLI_DEFINE(console_autoanswer, "Sets/displays autoanswer"),
  1137. AST_CLI_DEFINE(console_boost, "Sets/displays mic boost in dB"),
  1138. AST_CLI_DEFINE(console_active, "Sets/displays active console"),
  1139. };
  1140. /*!
  1141. * store the mixer argument from the config file, filtering possibly
  1142. * invalid or dangerous values (the string is used as argument for
  1143. * system("mixer %s")
  1144. */
  1145. static void store_mixer(struct chan_oss_pvt *o, const char *s)
  1146. {
  1147. int i;
  1148. for (i = 0; i < strlen(s); i++) {
  1149. if (!isalnum(s[i]) && strchr(" \t-/", s[i]) == NULL) {
  1150. ast_log(LOG_WARNING, "Suspect char %c in mixer cmd, ignoring:\n\t%s\n", s[i], s);
  1151. return;
  1152. }
  1153. }
  1154. if (o->mixer_cmd)
  1155. ast_free(o->mixer_cmd);
  1156. o->mixer_cmd = ast_strdup(s);
  1157. ast_log(LOG_WARNING, "setting mixer %s\n", s);
  1158. }
  1159. /*!
  1160. * store the callerid components
  1161. */
  1162. static void store_callerid(struct chan_oss_pvt *o, const char *s)
  1163. {
  1164. ast_callerid_split(s, o->cid_name, sizeof(o->cid_name), o->cid_num, sizeof(o->cid_num));
  1165. }
  1166. static void store_config_core(struct chan_oss_pvt *o, const char *var, const char *value)
  1167. {
  1168. CV_START(var, value);
  1169. /* handle jb conf */
  1170. if (!ast_jb_read_conf(&global_jbconf, var, value))
  1171. return;
  1172. if (!console_video_config(&o->env, var, value))
  1173. return; /* matched there */
  1174. CV_BOOL("autoanswer", o->autoanswer);
  1175. CV_BOOL("autohangup", o->autohangup);
  1176. CV_BOOL("overridecontext", o->overridecontext);
  1177. CV_STR("device", o->device);
  1178. CV_UINT("frags", o->frags);
  1179. CV_UINT("debug", oss_debug);
  1180. CV_UINT("queuesize", o->queuesize);
  1181. CV_STR("context", o->ctx);
  1182. CV_STR("language", o->language);
  1183. CV_STR("mohinterpret", o->mohinterpret);
  1184. CV_STR("extension", o->ext);
  1185. CV_F("mixer", store_mixer(o, value));
  1186. CV_F("callerid", store_callerid(o, value)) ;
  1187. CV_F("boost", store_boost(o, value));
  1188. CV_END;
  1189. }
  1190. /*!
  1191. * grab fields from the config file, init the descriptor and open the device.
  1192. */
  1193. static struct chan_oss_pvt *store_config(struct ast_config *cfg, char *ctg)
  1194. {
  1195. struct ast_variable *v;
  1196. struct chan_oss_pvt *o;
  1197. if (ctg == NULL) {
  1198. o = &oss_default;
  1199. ctg = "general";
  1200. } else {
  1201. if (!(o = ast_calloc(1, sizeof(*o))))
  1202. return NULL;
  1203. *o = oss_default;
  1204. /* "general" is also the default thing */
  1205. if (strcmp(ctg, "general") == 0) {
  1206. o->name = ast_strdup("dsp");
  1207. oss_active = o->name;
  1208. goto openit;
  1209. }
  1210. o->name = ast_strdup(ctg);
  1211. }
  1212. strcpy(o->mohinterpret, "default");
  1213. o->lastopen = ast_tvnow(); /* don't leave it 0 or tvdiff may wrap */
  1214. /* fill other fields from configuration */
  1215. for (v = ast_variable_browse(cfg, ctg); v; v = v->next) {
  1216. store_config_core(o, v->name, v->value);
  1217. }
  1218. if (ast_strlen_zero(o->device))
  1219. ast_copy_string(o->device, DEV_DSP, sizeof(o->device));
  1220. if (o->mixer_cmd) {
  1221. char *cmd;
  1222. if (asprintf(&cmd, "mixer %s", o->mixer_cmd) < 0) {
  1223. ast_log(LOG_WARNING, "asprintf() failed: %s\n", strerror(errno));
  1224. } else {
  1225. ast_log(LOG_WARNING, "running [%s]\n", cmd);
  1226. if (system(cmd) < 0) {
  1227. ast_log(LOG_WARNING, "system() failed: %s\n", strerror(errno));
  1228. }
  1229. ast_free(cmd);
  1230. }
  1231. }
  1232. /* if the config file requested to start the GUI, do it */
  1233. if (get_gui_startup(o->env))
  1234. console_video_start(o->env, NULL);
  1235. if (o == &oss_default) /* we are done with the default */
  1236. return NULL;
  1237. openit:
  1238. #ifdef TRYOPEN
  1239. if (setformat(o, O_RDWR) < 0) { /* open device */
  1240. ast_verb(1, "Device %s not detected\n", ctg);
  1241. ast_verb(1, "Turn off OSS support by adding " "'noload=chan_oss.so' in /etc/asterisk/modules.conf\n");
  1242. goto error;
  1243. }
  1244. if (o->duplex != M_FULL)
  1245. ast_log(LOG_WARNING, "XXX I don't work right with non " "full-duplex sound cards XXX\n");
  1246. #endif /* TRYOPEN */
  1247. /* link into list of devices */
  1248. if (o != &oss_default) {
  1249. o->next = oss_default.next;
  1250. oss_default.next = o;
  1251. }
  1252. return o;
  1253. #ifdef TRYOPEN
  1254. error:
  1255. if (o != &oss_default)
  1256. ast_free(o);
  1257. return NULL;
  1258. #endif
  1259. }
  1260. static int load_module(void)
  1261. {
  1262. struct ast_config *cfg = NULL;
  1263. char *ctg = NULL;
  1264. struct ast_flags config_flags = { 0 };
  1265. /* Copy the default jb config over global_jbconf */
  1266. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  1267. /* load config file */
  1268. if (!(cfg = ast_config_load(config, config_flags))) {
  1269. ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
  1270. return AST_MODULE_LOAD_DECLINE;
  1271. }
  1272. do {
  1273. store_config(cfg, ctg);
  1274. } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL);
  1275. ast_config_destroy(cfg);
  1276. if (find_desc(oss_active) == NULL) {
  1277. ast_log(LOG_NOTICE, "Device %s not found\n", oss_active);
  1278. /* XXX we could default to 'dsp' perhaps ? */
  1279. /* XXX should cleanup allocated memory etc. */
  1280. return AST_MODULE_LOAD_FAILURE;
  1281. }
  1282. oss_tech.capabilities |= console_video_formats;
  1283. if (ast_channel_register(&oss_tech)) {
  1284. ast_log(LOG_ERROR, "Unable to register channel type 'OSS'\n");
  1285. return AST_MODULE_LOAD_FAILURE;
  1286. }
  1287. ast_cli_register_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry));
  1288. return AST_MODULE_LOAD_SUCCESS;
  1289. }
  1290. static int unload_module(void)
  1291. {
  1292. struct chan_oss_pvt *o, *next;
  1293. ast_channel_unregister(&oss_tech);
  1294. ast_cli_unregister_multiple(cli_oss, sizeof(cli_oss) / sizeof(struct ast_cli_entry));
  1295. o = oss_default.next;
  1296. while (o) {
  1297. close(o->sounddev);
  1298. if (o->owner)
  1299. ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
  1300. if (o->owner)
  1301. return -1;
  1302. next = o->next;
  1303. ast_free(o->name);
  1304. ast_free(o);
  1305. o = next;
  1306. }
  1307. return 0;
  1308. }
  1309. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "OSS Console Channel Driver");