chan_oss.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  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 ? ast_channel_tech_pvt(c) : 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, struct ast_format_cap *cap, const struct ast_channel *requestor,
  281. const char *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, const 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. .requester = oss_request,
  298. .send_digit_begin = oss_digit_begin,
  299. .send_digit_end = oss_digit_end,
  300. .send_text = oss_text,
  301. .hangup = oss_hangup,
  302. .answer = oss_answer,
  303. .read = oss_read,
  304. .call = oss_call,
  305. .write = oss_write,
  306. .write_video = console_write_video,
  307. .indicate = oss_indicate,
  308. .fixup = oss_fixup,
  309. };
  310. /*!
  311. * \brief returns a pointer to the descriptor with the given name
  312. */
  313. static struct chan_oss_pvt *find_desc(const char *dev)
  314. {
  315. struct chan_oss_pvt *o = NULL;
  316. if (!dev)
  317. ast_log(LOG_WARNING, "null dev\n");
  318. for (o = oss_default.next; o && o->name && dev && strcmp(o->name, dev) != 0; o = o->next);
  319. if (!o)
  320. ast_log(LOG_WARNING, "could not find <%s>\n", dev ? dev : "--no-device--");
  321. return o;
  322. }
  323. /* !
  324. * \brief split a string in extension-context, returns pointers to malloc'ed
  325. * strings.
  326. *
  327. * If we do not have 'overridecontext' then the last @ is considered as
  328. * a context separator, and the context is overridden.
  329. * This is usually not very necessary as you can play with the dialplan,
  330. * and it is nice not to need it because you have '@' in SIP addresses.
  331. *
  332. * \return the buffer address.
  333. */
  334. static char *ast_ext_ctx(const char *src, char **ext, char **ctx)
  335. {
  336. struct chan_oss_pvt *o = find_desc(oss_active);
  337. if (ext == NULL || ctx == NULL)
  338. return NULL; /* error */
  339. *ext = *ctx = NULL;
  340. if (src && *src != '\0')
  341. *ext = ast_strdup(src);
  342. if (*ext == NULL)
  343. return NULL;
  344. if (!o->overridecontext) {
  345. /* parse from the right */
  346. *ctx = strrchr(*ext, '@');
  347. if (*ctx)
  348. *(*ctx)++ = '\0';
  349. }
  350. return *ext;
  351. }
  352. /*!
  353. * \brief Returns the number of blocks used in the audio output channel
  354. */
  355. static int used_blocks(struct chan_oss_pvt *o)
  356. {
  357. struct audio_buf_info info;
  358. if (ioctl(o->sounddev, SNDCTL_DSP_GETOSPACE, &info)) {
  359. if (!(o->warned & WARN_used_blocks)) {
  360. ast_log(LOG_WARNING, "Error reading output space\n");
  361. o->warned |= WARN_used_blocks;
  362. }
  363. return 1;
  364. }
  365. if (o->total_blocks == 0) {
  366. if (0) /* debugging */
  367. ast_log(LOG_WARNING, "fragtotal %d size %d avail %d\n", info.fragstotal, info.fragsize, info.fragments);
  368. o->total_blocks = info.fragments;
  369. }
  370. return o->total_blocks - info.fragments;
  371. }
  372. /*! Write an exactly FRAME_SIZE sized frame */
  373. static int soundcard_writeframe(struct chan_oss_pvt *o, short *data)
  374. {
  375. int res;
  376. if (o->sounddev < 0)
  377. setformat(o, O_RDWR);
  378. if (o->sounddev < 0)
  379. return 0; /* not fatal */
  380. /*
  381. * Nothing complex to manage the audio device queue.
  382. * If the buffer is full just drop the extra, otherwise write.
  383. * XXX in some cases it might be useful to write anyways after
  384. * a number of failures, to restart the output chain.
  385. */
  386. res = used_blocks(o);
  387. if (res > o->queuesize) { /* no room to write a block */
  388. if (o->w_errors++ == 0 && (oss_debug & 0x4))
  389. ast_log(LOG_WARNING, "write: used %d blocks (%d)\n", res, o->w_errors);
  390. return 0;
  391. }
  392. o->w_errors = 0;
  393. return write(o->sounddev, (void *)data, FRAME_SIZE * 2);
  394. }
  395. /*!
  396. * reset and close the device if opened,
  397. * then open and initialize it in the desired mode,
  398. * trigger reads and writes so we can start using it.
  399. */
  400. static int setformat(struct chan_oss_pvt *o, int mode)
  401. {
  402. int fmt, desired, res, fd;
  403. if (o->sounddev >= 0) {
  404. ioctl(o->sounddev, SNDCTL_DSP_RESET, 0);
  405. close(o->sounddev);
  406. o->duplex = M_UNSET;
  407. o->sounddev = -1;
  408. }
  409. if (mode == O_CLOSE) /* we are done */
  410. return 0;
  411. if (ast_tvdiff_ms(ast_tvnow(), o->lastopen) < 1000)
  412. return -1; /* don't open too often */
  413. o->lastopen = ast_tvnow();
  414. fd = o->sounddev = open(o->device, mode | O_NONBLOCK);
  415. if (fd < 0) {
  416. ast_log(LOG_WARNING, "Unable to re-open DSP device %s: %s\n", o->device, strerror(errno));
  417. return -1;
  418. }
  419. if (o->owner)
  420. ast_channel_set_fd(o->owner, 0, fd);
  421. #if __BYTE_ORDER == __LITTLE_ENDIAN
  422. fmt = AFMT_S16_LE;
  423. #else
  424. fmt = AFMT_S16_BE;
  425. #endif
  426. res = ioctl(fd, SNDCTL_DSP_SETFMT, &fmt);
  427. if (res < 0) {
  428. ast_log(LOG_WARNING, "Unable to set format to 16-bit signed\n");
  429. return -1;
  430. }
  431. switch (mode) {
  432. case O_RDWR:
  433. res = ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
  434. /* Check to see if duplex set (FreeBSD Bug) */
  435. res = ioctl(fd, SNDCTL_DSP_GETCAPS, &fmt);
  436. if (res == 0 && (fmt & DSP_CAP_DUPLEX)) {
  437. ast_verb(2, "Console is full duplex\n");
  438. o->duplex = M_FULL;
  439. };
  440. break;
  441. case O_WRONLY:
  442. o->duplex = M_WRITE;
  443. break;
  444. case O_RDONLY:
  445. o->duplex = M_READ;
  446. break;
  447. }
  448. fmt = 0;
  449. res = ioctl(fd, SNDCTL_DSP_STEREO, &fmt);
  450. if (res < 0) {
  451. ast_log(LOG_WARNING, "Failed to set audio device to mono\n");
  452. return -1;
  453. }
  454. fmt = desired = DEFAULT_SAMPLE_RATE; /* 8000 Hz desired */
  455. res = ioctl(fd, SNDCTL_DSP_SPEED, &fmt);
  456. if (res < 0) {
  457. ast_log(LOG_WARNING, "Failed to set sample rate to %d\n", desired);
  458. return -1;
  459. }
  460. if (fmt != desired) {
  461. if (!(o->warned & WARN_speed)) {
  462. ast_log(LOG_WARNING,
  463. "Requested %d Hz, got %d Hz -- sound may be choppy\n",
  464. desired, fmt);
  465. o->warned |= WARN_speed;
  466. }
  467. }
  468. /*
  469. * on Freebsd, SETFRAGMENT does not work very well on some cards.
  470. * Default to use 256 bytes, let the user override
  471. */
  472. if (o->frags) {
  473. fmt = o->frags;
  474. res = ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &fmt);
  475. if (res < 0) {
  476. if (!(o->warned & WARN_frag)) {
  477. ast_log(LOG_WARNING,
  478. "Unable to set fragment size -- sound may be choppy\n");
  479. o->warned |= WARN_frag;
  480. }
  481. }
  482. }
  483. /* on some cards, we need SNDCTL_DSP_SETTRIGGER to start outputting */
  484. res = PCM_ENABLE_INPUT | PCM_ENABLE_OUTPUT;
  485. res = ioctl(fd, SNDCTL_DSP_SETTRIGGER, &res);
  486. /* it may fail if we are in half duplex, never mind */
  487. return 0;
  488. }
  489. /*
  490. * some of the standard methods supported by channels.
  491. */
  492. static int oss_digit_begin(struct ast_channel *c, char digit)
  493. {
  494. return 0;
  495. }
  496. static int oss_digit_end(struct ast_channel *c, char digit, unsigned int duration)
  497. {
  498. /* no better use for received digits than print them */
  499. ast_verbose(" << Console Received digit %c of duration %u ms >> \n",
  500. digit, duration);
  501. return 0;
  502. }
  503. static int oss_text(struct ast_channel *c, const char *text)
  504. {
  505. /* print received messages */
  506. ast_verbose(" << Console Received text %s >> \n", text);
  507. return 0;
  508. }
  509. /*!
  510. * \brief handler for incoming calls. Either autoanswer, or start ringing
  511. */
  512. static int oss_call(struct ast_channel *c, const char *dest, int timeout)
  513. {
  514. struct chan_oss_pvt *o = ast_channel_tech_pvt(c);
  515. struct ast_frame f = { AST_FRAME_CONTROL, };
  516. AST_DECLARE_APP_ARGS(args,
  517. AST_APP_ARG(name);
  518. AST_APP_ARG(flags);
  519. );
  520. char *parse = ast_strdupa(dest);
  521. AST_NONSTANDARD_APP_ARGS(args, parse, '/');
  522. ast_verbose(" << Call to device '%s' dnid '%s' rdnis '%s' on console from '%s' <%s> >>\n",
  523. dest,
  524. S_OR(ast_channel_dialed(c)->number.str, ""),
  525. S_COR(ast_channel_redirecting(c)->from.number.valid, ast_channel_redirecting(c)->from.number.str, ""),
  526. S_COR(ast_channel_caller(c)->id.name.valid, ast_channel_caller(c)->id.name.str, ""),
  527. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->id.number.str, ""));
  528. if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "answer") == 0) {
  529. f.subclass.integer = AST_CONTROL_ANSWER;
  530. ast_queue_frame(c, &f);
  531. } else if (!ast_strlen_zero(args.flags) && strcasecmp(args.flags, "noanswer") == 0) {
  532. f.subclass.integer = AST_CONTROL_RINGING;
  533. ast_queue_frame(c, &f);
  534. ast_indicate(c, AST_CONTROL_RINGING);
  535. } else if (o->autoanswer) {
  536. ast_verbose(" << Auto-answered >> \n");
  537. f.subclass.integer = AST_CONTROL_ANSWER;
  538. ast_queue_frame(c, &f);
  539. o->hookstate = 1;
  540. } else {
  541. ast_verbose("<< Type 'answer' to answer, or use 'autoanswer' for future calls >> \n");
  542. f.subclass.integer = AST_CONTROL_RINGING;
  543. ast_queue_frame(c, &f);
  544. ast_indicate(c, AST_CONTROL_RINGING);
  545. }
  546. return 0;
  547. }
  548. /*!
  549. * \brief remote side answered the phone
  550. */
  551. static int oss_answer(struct ast_channel *c)
  552. {
  553. struct chan_oss_pvt *o = ast_channel_tech_pvt(c);
  554. ast_verbose(" << Console call has been answered >> \n");
  555. ast_setstate(c, AST_STATE_UP);
  556. o->hookstate = 1;
  557. return 0;
  558. }
  559. static int oss_hangup(struct ast_channel *c)
  560. {
  561. struct chan_oss_pvt *o = ast_channel_tech_pvt(c);
  562. ast_channel_tech_pvt_set(c, NULL);
  563. o->owner = NULL;
  564. ast_verbose(" << Hangup on console >> \n");
  565. console_video_uninit(o->env);
  566. ast_module_unref(ast_module_info->self);
  567. if (o->hookstate) {
  568. if (o->autoanswer || o->autohangup) {
  569. /* Assume auto-hangup too */
  570. o->hookstate = 0;
  571. setformat(o, O_CLOSE);
  572. }
  573. }
  574. return 0;
  575. }
  576. /*! \brief used for data coming from the network */
  577. static int oss_write(struct ast_channel *c, struct ast_frame *f)
  578. {
  579. int src;
  580. struct chan_oss_pvt *o = ast_channel_tech_pvt(c);
  581. /*
  582. * we could receive a block which is not a multiple of our
  583. * FRAME_SIZE, so buffer it locally and write to the device
  584. * in FRAME_SIZE chunks.
  585. * Keep the residue stored for future use.
  586. */
  587. src = 0; /* read position into f->data */
  588. while (src < f->datalen) {
  589. /* Compute spare room in the buffer */
  590. int l = sizeof(o->oss_write_buf) - o->oss_write_dst;
  591. if (f->datalen - src >= l) { /* enough to fill a frame */
  592. memcpy(o->oss_write_buf + o->oss_write_dst, f->data.ptr + src, l);
  593. soundcard_writeframe(o, (short *) o->oss_write_buf);
  594. src += l;
  595. o->oss_write_dst = 0;
  596. } else { /* copy residue */
  597. l = f->datalen - src;
  598. memcpy(o->oss_write_buf + o->oss_write_dst, f->data.ptr + src, l);
  599. src += l; /* but really, we are done */
  600. o->oss_write_dst += l;
  601. }
  602. }
  603. return 0;
  604. }
  605. static struct ast_frame *oss_read(struct ast_channel *c)
  606. {
  607. int res;
  608. struct chan_oss_pvt *o = ast_channel_tech_pvt(c);
  609. struct ast_frame *f = &o->read_f;
  610. /* XXX can be simplified returning &ast_null_frame */
  611. /* prepare a NULL frame in case we don't have enough data to return */
  612. memset(f, '\0', sizeof(struct ast_frame));
  613. f->frametype = AST_FRAME_NULL;
  614. f->src = oss_tech.type;
  615. res = read(o->sounddev, o->oss_read_buf + o->readpos, sizeof(o->oss_read_buf) - o->readpos);
  616. if (res < 0) /* audio data not ready, return a NULL frame */
  617. return f;
  618. o->readpos += res;
  619. if (o->readpos < sizeof(o->oss_read_buf)) /* not enough samples */
  620. return f;
  621. if (o->mute)
  622. return f;
  623. o->readpos = AST_FRIENDLY_OFFSET; /* reset read pointer for next frame */
  624. if (ast_channel_state(c) != AST_STATE_UP) /* drop data if frame is not up */
  625. return f;
  626. /* ok we can build and deliver the frame to the caller */
  627. f->frametype = AST_FRAME_VOICE;
  628. ast_format_set(&f->subclass.format, AST_FORMAT_SLINEAR, 0);
  629. f->samples = FRAME_SIZE;
  630. f->datalen = FRAME_SIZE * 2;
  631. f->data.ptr = o->oss_read_buf + AST_FRIENDLY_OFFSET;
  632. if (o->boost != BOOST_SCALE) { /* scale and clip values */
  633. int i, x;
  634. int16_t *p = (int16_t *) f->data.ptr;
  635. for (i = 0; i < f->samples; i++) {
  636. x = (p[i] * o->boost) / BOOST_SCALE;
  637. if (x > 32767)
  638. x = 32767;
  639. else if (x < -32768)
  640. x = -32768;
  641. p[i] = x;
  642. }
  643. }
  644. f->offset = AST_FRIENDLY_OFFSET;
  645. return f;
  646. }
  647. static int oss_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  648. {
  649. struct chan_oss_pvt *o = ast_channel_tech_pvt(newchan);
  650. o->owner = newchan;
  651. return 0;
  652. }
  653. static int oss_indicate(struct ast_channel *c, int cond, const void *data, size_t datalen)
  654. {
  655. struct chan_oss_pvt *o = ast_channel_tech_pvt(c);
  656. int res = 0;
  657. switch (cond) {
  658. case AST_CONTROL_INCOMPLETE:
  659. case AST_CONTROL_BUSY:
  660. case AST_CONTROL_CONGESTION:
  661. case AST_CONTROL_RINGING:
  662. case AST_CONTROL_PVT_CAUSE_CODE:
  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, ast_channel_name(c));
  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. ast_channel_tech_set(c, &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. ast_format_set(ast_channel_readformat(c), AST_FORMAT_SLINEAR, 0);
  699. ast_format_set(ast_channel_writeformat(c), AST_FORMAT_SLINEAR, 0);
  700. ast_format_cap_add(ast_channel_nativeformats(c), ast_channel_readformat(c));
  701. /* if the console makes the call, add video to the offer */
  702. /* if (state == AST_STATE_RINGING) TODO XXX CONSOLE VIDEO IS DISABLED UNTIL IT GETS A MAINTAINER
  703. c->nativeformats |= console_video_formats; */
  704. ast_channel_tech_pvt_set(c, o);
  705. if (!ast_strlen_zero(o->language))
  706. ast_channel_language_set(c, 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. ast_channel_caller(c)->ani.number.valid = 1;
  711. ast_channel_caller(c)->ani.number.str = ast_strdup(o->cid_num);
  712. }
  713. if (!ast_strlen_zero(ext)) {
  714. ast_channel_dialed(c)->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", ast_channel_name(c));
  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, struct ast_format_cap *cap, const struct ast_channel *requestor, const char *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. struct ast_format tmpfmt;
  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, 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 (!(ast_format_cap_iscompatible(cap, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0)))) {
  749. ast_log(LOG_NOTICE, "Format %s unsupported\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
  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 ? ast_channel_linkedid(requestor) : 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 *digits;
  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. digits = a->argv[e->args];
  975. /* send the string one char at a time */
  976. for (i = 0; i < strlen(digits); i++) {
  977. f.subclass.integer = digits[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 = ast_strdupa(ast_channel_context(o->owner));
  1056. if (!ast_exists_extension(b, ctx, ext, 1,
  1057. S_COR(ast_channel_caller(b)->id.number.valid, ast_channel_caller(b)->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", ast_channel_name(b), 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 (ast_asprintf(&cmd, "mixer %s", o->mixer_cmd) >= 0) {
  1237. ast_log(LOG_WARNING, "running [%s]\n", cmd);
  1238. if (system(cmd) < 0) {
  1239. ast_log(LOG_WARNING, "system() failed: %s\n", strerror(errno));
  1240. }
  1241. ast_free(cmd);
  1242. }
  1243. }
  1244. /* if the config file requested to start the GUI, do it */
  1245. if (get_gui_startup(o->env))
  1246. console_video_start(o->env, NULL);
  1247. if (o == &oss_default) /* we are done with the default */
  1248. return NULL;
  1249. openit:
  1250. #ifdef TRYOPEN
  1251. if (setformat(o, O_RDWR) < 0) { /* open device */
  1252. ast_verb(1, "Device %s not detected\n", ctg);
  1253. ast_verb(1, "Turn off OSS support by adding " "'noload=chan_oss.so' in /etc/asterisk/modules.conf\n");
  1254. goto error;
  1255. }
  1256. if (o->duplex != M_FULL)
  1257. ast_log(LOG_WARNING, "XXX I don't work right with non " "full-duplex sound cards XXX\n");
  1258. #endif /* TRYOPEN */
  1259. /* link into list of devices */
  1260. if (o != &oss_default) {
  1261. o->next = oss_default.next;
  1262. oss_default.next = o;
  1263. }
  1264. return o;
  1265. #ifdef TRYOPEN
  1266. error:
  1267. if (o != &oss_default)
  1268. ast_free(o);
  1269. return NULL;
  1270. #endif
  1271. }
  1272. static int load_module(void)
  1273. {
  1274. struct ast_config *cfg = NULL;
  1275. char *ctg = NULL;
  1276. struct ast_flags config_flags = { 0 };
  1277. struct ast_format tmpfmt;
  1278. /* Copy the default jb config over global_jbconf */
  1279. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  1280. /* load config file */
  1281. if (!(cfg = ast_config_load(config, config_flags))) {
  1282. ast_log(LOG_NOTICE, "Unable to load config %s\n", config);
  1283. return AST_MODULE_LOAD_DECLINE;
  1284. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  1285. ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config);
  1286. return AST_MODULE_LOAD_DECLINE;
  1287. }
  1288. do {
  1289. store_config(cfg, ctg);
  1290. } while ( (ctg = ast_category_browse(cfg, ctg)) != NULL);
  1291. ast_config_destroy(cfg);
  1292. if (find_desc(oss_active) == NULL) {
  1293. ast_log(LOG_NOTICE, "Device %s not found\n", oss_active);
  1294. /* XXX we could default to 'dsp' perhaps ? */
  1295. /* XXX should cleanup allocated memory etc. */
  1296. return AST_MODULE_LOAD_FAILURE;
  1297. }
  1298. if (!(oss_tech.capabilities = ast_format_cap_alloc())) {
  1299. return AST_MODULE_LOAD_FAILURE;
  1300. }
  1301. ast_format_cap_add(oss_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR, 0));
  1302. /* TODO XXX CONSOLE VIDEO IS DISABLE UNTIL IT HAS A MAINTAINER
  1303. * add console_video_formats to oss_tech.capabilities once this occurs. */
  1304. if (ast_channel_register(&oss_tech)) {
  1305. ast_log(LOG_ERROR, "Unable to register channel type 'OSS'\n");
  1306. return AST_MODULE_LOAD_DECLINE;
  1307. }
  1308. ast_cli_register_multiple(cli_oss, ARRAY_LEN(cli_oss));
  1309. return AST_MODULE_LOAD_SUCCESS;
  1310. }
  1311. static int unload_module(void)
  1312. {
  1313. struct chan_oss_pvt *o, *next;
  1314. ast_channel_unregister(&oss_tech);
  1315. ast_cli_unregister_multiple(cli_oss, ARRAY_LEN(cli_oss));
  1316. o = oss_default.next;
  1317. while (o) {
  1318. close(o->sounddev);
  1319. if (o->owner)
  1320. ast_softhangup(o->owner, AST_SOFTHANGUP_APPUNLOAD);
  1321. if (o->owner)
  1322. return -1;
  1323. next = o->next;
  1324. ast_free(o->name);
  1325. ast_free(o);
  1326. o = next;
  1327. }
  1328. oss_tech.capabilities = ast_format_cap_destroy(oss_tech.capabilities);
  1329. return 0;
  1330. }
  1331. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "OSS Console Channel Driver");