chan_oss.c 43 KB

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