chan_oss.c 43 KB

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