chan_console.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2006 - 2008, Digium, Inc.
  5. *
  6. * Russell Bryant <russell@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*!
  19. * \file
  20. * \brief Cross-platform console channel driver
  21. *
  22. * \author Russell Bryant <russell@digium.com>
  23. *
  24. * \note Some of the code in this file came from chan_oss and chan_alsa.
  25. * chan_oss, Mark Spencer <markster@digium.com>
  26. * chan_oss, Luigi Rizzo
  27. * chan_alsa, Matthew Fredrickson <creslin@digium.com>
  28. *
  29. * \ingroup channel_drivers
  30. *
  31. * \extref Portaudio http://www.portaudio.com/
  32. *
  33. * To install portaudio v19 from svn, check it out using the following command:
  34. * - svn co https://www.portaudio.com/repos/portaudio/branches/v19-devel
  35. *
  36. * \note Since this works with any audio system that libportaudio supports,
  37. * including ALSA and OSS, this may someday deprecate chan_alsa and chan_oss.
  38. * However, before that can be done, it needs to *at least* have all of the
  39. * features that these other channel drivers have. The features implemented
  40. * in at least one of the other console channel drivers that are not yet
  41. * implemented here are:
  42. *
  43. * - Set Auto-answer from the dialplan
  44. * - transfer CLI command
  45. * - boost CLI command and .conf option
  46. * - console_video support
  47. */
  48. /*** MODULEINFO
  49. <depend>portaudio</depend>
  50. <support_level>extended</support_level>
  51. ***/
  52. #include "asterisk.h"
  53. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  54. #include <sys/signal.h> /* SIGURG */
  55. #include <portaudio.h>
  56. #include "asterisk/module.h"
  57. #include "asterisk/channel.h"
  58. #include "asterisk/pbx.h"
  59. #include "asterisk/causes.h"
  60. #include "asterisk/cli.h"
  61. #include "asterisk/musiconhold.h"
  62. #include "asterisk/callerid.h"
  63. #include "asterisk/astobj2.h"
  64. /*!
  65. * \brief The sample rate to request from PortAudio
  66. *
  67. * \todo Make this optional. If this is only going to talk to 8 kHz endpoints,
  68. * then it makes sense to use 8 kHz natively.
  69. */
  70. #define SAMPLE_RATE 16000
  71. /*!
  72. * \brief The number of samples to configure the portaudio stream for
  73. *
  74. * 320 samples (20 ms) is the most common frame size in Asterisk. So, the code
  75. * in this module reads 320 sample frames from the portaudio stream and queues
  76. * them up on the Asterisk channel. Frames of any size can be written to a
  77. * portaudio stream, but the portaudio documentation does say that for high
  78. * performance applications, the data should be written to Pa_WriteStream in
  79. * the same size as what is used to initialize the stream.
  80. */
  81. #define NUM_SAMPLES 320
  82. /*! \brief Mono Input */
  83. #define INPUT_CHANNELS 1
  84. /*! \brief Mono Output */
  85. #define OUTPUT_CHANNELS 1
  86. /*!
  87. * \brief Maximum text message length
  88. * \note This should be changed if there is a common definition somewhere
  89. * that defines the maximum length of a text message.
  90. */
  91. #define TEXT_SIZE 256
  92. /*! \brief Dance, Kirby, Dance! @{ */
  93. #define V_BEGIN " --- <(\"<) --- "
  94. #define V_END " --- (>\")> ---\n"
  95. /*! @} */
  96. static const char config_file[] = "console.conf";
  97. /*!
  98. * \brief Console pvt structure
  99. *
  100. * Currently, this is a singleton object. However, multiple instances will be
  101. * needed when this module is updated for multiple device support.
  102. */
  103. static struct console_pvt {
  104. AST_DECLARE_STRING_FIELDS(
  105. /*! Name of the device */
  106. AST_STRING_FIELD(name);
  107. AST_STRING_FIELD(input_device);
  108. AST_STRING_FIELD(output_device);
  109. /*! Default context for outgoing calls */
  110. AST_STRING_FIELD(context);
  111. /*! Default extension for outgoing calls */
  112. AST_STRING_FIELD(exten);
  113. /*! Default CallerID number */
  114. AST_STRING_FIELD(cid_num);
  115. /*! Default CallerID name */
  116. AST_STRING_FIELD(cid_name);
  117. /*! Default MOH class to listen to, if:
  118. * - No MOH class set on the channel
  119. * - Peer channel putting this device on hold did not suggest a class */
  120. AST_STRING_FIELD(mohinterpret);
  121. /*! Default language */
  122. AST_STRING_FIELD(language);
  123. /*! Default parkinglot */
  124. AST_STRING_FIELD(parkinglot);
  125. );
  126. /*! Current channel for this device */
  127. struct ast_channel *owner;
  128. /*! Current PortAudio stream for this device */
  129. PaStream *stream;
  130. /*! A frame for preparing to queue on to the channel */
  131. struct ast_frame fr;
  132. /*! Running = 1, Not running = 0 */
  133. unsigned int streamstate:1;
  134. /*! On-hook = 0, Off-hook = 1 */
  135. unsigned int hookstate:1;
  136. /*! Unmuted = 0, Muted = 1 */
  137. unsigned int muted:1;
  138. /*! Automatically answer incoming calls */
  139. unsigned int autoanswer:1;
  140. /*! Ignore context in the console dial CLI command */
  141. unsigned int overridecontext:1;
  142. /*! Set during a reload so that we know to destroy this if it is no longer
  143. * in the configuration file. */
  144. unsigned int destroy:1;
  145. /*! ID for the stream monitor thread */
  146. pthread_t thread;
  147. } globals;
  148. AST_MUTEX_DEFINE_STATIC(globals_lock);
  149. static struct ao2_container *pvts;
  150. #define NUM_PVT_BUCKETS 7
  151. static struct console_pvt *active_pvt;
  152. AST_RWLOCK_DEFINE_STATIC(active_lock);
  153. /*!
  154. * \brief Global jitterbuffer configuration
  155. *
  156. * \note Disabled by default.
  157. * \note Values shown here match the defaults shown in console.conf.sample
  158. */
  159. static struct ast_jb_conf default_jbconf = {
  160. .flags = 0,
  161. .max_size = 200,
  162. .resync_threshold = 1000,
  163. .impl = "fixed",
  164. .target_extra = 40,
  165. };
  166. static struct ast_jb_conf global_jbconf;
  167. /*! Channel Technology Callbacks @{ */
  168. static struct ast_channel *console_request(const char *type, struct ast_format_cap *cap,
  169. const struct ast_channel *requestor, void *data, int *cause);
  170. static int console_digit_begin(struct ast_channel *c, char digit);
  171. static int console_digit_end(struct ast_channel *c, char digit, unsigned int duration);
  172. static int console_text(struct ast_channel *c, const char *text);
  173. static int console_hangup(struct ast_channel *c);
  174. static int console_answer(struct ast_channel *c);
  175. static struct ast_frame *console_read(struct ast_channel *chan);
  176. static int console_call(struct ast_channel *c, char *dest, int timeout);
  177. static int console_write(struct ast_channel *chan, struct ast_frame *f);
  178. static int console_indicate(struct ast_channel *chan, int cond,
  179. const void *data, size_t datalen);
  180. static int console_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  181. /*! @} */
  182. static struct ast_channel_tech console_tech = {
  183. .type = "Console",
  184. .description = "Console Channel Driver",
  185. .requester = console_request,
  186. .send_digit_begin = console_digit_begin,
  187. .send_digit_end = console_digit_end,
  188. .send_text = console_text,
  189. .hangup = console_hangup,
  190. .answer = console_answer,
  191. .read = console_read,
  192. .call = console_call,
  193. .write = console_write,
  194. .indicate = console_indicate,
  195. .fixup = console_fixup,
  196. };
  197. /*! \brief lock a console_pvt struct */
  198. #define console_pvt_lock(pvt) ao2_lock(pvt)
  199. /*! \brief unlock a console_pvt struct */
  200. #define console_pvt_unlock(pvt) ao2_unlock(pvt)
  201. static inline struct console_pvt *ref_pvt(struct console_pvt *pvt)
  202. {
  203. if (pvt)
  204. ao2_ref(pvt, +1);
  205. return pvt;
  206. }
  207. static inline struct console_pvt *unref_pvt(struct console_pvt *pvt)
  208. {
  209. ao2_ref(pvt, -1);
  210. return NULL;
  211. }
  212. static struct console_pvt *find_pvt(const char *name)
  213. {
  214. struct console_pvt tmp_pvt = {
  215. .name = name,
  216. };
  217. return ao2_find(pvts, &tmp_pvt, OBJ_POINTER);
  218. }
  219. /*!
  220. * \brief Stream monitor thread
  221. *
  222. * \arg data A pointer to the console_pvt structure that contains the portaudio
  223. * stream that needs to be monitored.
  224. *
  225. * This function runs in its own thread to monitor data coming in from a
  226. * portaudio stream. When enough data is available, it is queued up to
  227. * be read from the Asterisk channel.
  228. */
  229. static void *stream_monitor(void *data)
  230. {
  231. struct console_pvt *pvt = data;
  232. char buf[NUM_SAMPLES * sizeof(int16_t)];
  233. PaError res;
  234. struct ast_frame f = {
  235. .frametype = AST_FRAME_VOICE,
  236. .src = "console_stream_monitor",
  237. .data.ptr = buf,
  238. .datalen = sizeof(buf),
  239. .samples = sizeof(buf) / sizeof(int16_t),
  240. };
  241. ast_format_set(&f.subclass.format, AST_FORMAT_SLINEAR16, 0);
  242. for (;;) {
  243. pthread_testcancel();
  244. res = Pa_ReadStream(pvt->stream, buf, sizeof(buf) / sizeof(int16_t));
  245. pthread_testcancel();
  246. if (!pvt->owner) {
  247. return NULL;
  248. }
  249. if (res == paNoError)
  250. ast_queue_frame(pvt->owner, &f);
  251. }
  252. return NULL;
  253. }
  254. static int open_stream(struct console_pvt *pvt)
  255. {
  256. int res = paInternalError;
  257. if (!strcasecmp(pvt->input_device, "default") &&
  258. !strcasecmp(pvt->output_device, "default")) {
  259. res = Pa_OpenDefaultStream(&pvt->stream, INPUT_CHANNELS, OUTPUT_CHANNELS,
  260. paInt16, SAMPLE_RATE, NUM_SAMPLES, NULL, NULL);
  261. } else {
  262. PaStreamParameters input_params = {
  263. .channelCount = 1,
  264. .sampleFormat = paInt16,
  265. .suggestedLatency = (1.0 / 50.0), /* 20 ms */
  266. .device = paNoDevice,
  267. };
  268. PaStreamParameters output_params = {
  269. .channelCount = 1,
  270. .sampleFormat = paInt16,
  271. .suggestedLatency = (1.0 / 50.0), /* 20 ms */
  272. .device = paNoDevice,
  273. };
  274. PaDeviceIndex idx, num_devices, def_input, def_output;
  275. if (!(num_devices = Pa_GetDeviceCount()))
  276. return res;
  277. def_input = Pa_GetDefaultInputDevice();
  278. def_output = Pa_GetDefaultOutputDevice();
  279. for (idx = 0;
  280. idx < num_devices && (input_params.device == paNoDevice
  281. || output_params.device == paNoDevice);
  282. idx++)
  283. {
  284. const PaDeviceInfo *dev = Pa_GetDeviceInfo(idx);
  285. if (dev->maxInputChannels) {
  286. if ( (idx == def_input && !strcasecmp(pvt->input_device, "default")) ||
  287. !strcasecmp(pvt->input_device, dev->name) )
  288. input_params.device = idx;
  289. }
  290. if (dev->maxOutputChannels) {
  291. if ( (idx == def_output && !strcasecmp(pvt->output_device, "default")) ||
  292. !strcasecmp(pvt->output_device, dev->name) )
  293. output_params.device = idx;
  294. }
  295. }
  296. if (input_params.device == paNoDevice)
  297. ast_log(LOG_ERROR, "No input device found for console device '%s'\n", pvt->name);
  298. if (output_params.device == paNoDevice)
  299. ast_log(LOG_ERROR, "No output device found for console device '%s'\n", pvt->name);
  300. res = Pa_OpenStream(&pvt->stream, &input_params, &output_params,
  301. SAMPLE_RATE, NUM_SAMPLES, paNoFlag, NULL, NULL);
  302. }
  303. return res;
  304. }
  305. static int start_stream(struct console_pvt *pvt)
  306. {
  307. PaError res;
  308. int ret_val = 0;
  309. console_pvt_lock(pvt);
  310. /* It is possible for console_hangup to be called before the
  311. * stream is started, if this is the case pvt->owner will be NULL
  312. * and start_stream should be aborted. */
  313. if (pvt->streamstate || !pvt->owner)
  314. goto return_unlock;
  315. pvt->streamstate = 1;
  316. ast_debug(1, "Starting stream\n");
  317. res = open_stream(pvt);
  318. if (res != paNoError) {
  319. ast_log(LOG_WARNING, "Failed to open stream - (%d) %s\n",
  320. res, Pa_GetErrorText(res));
  321. ret_val = -1;
  322. goto return_unlock;
  323. }
  324. res = Pa_StartStream(pvt->stream);
  325. if (res != paNoError) {
  326. ast_log(LOG_WARNING, "Failed to start stream - (%d) %s\n",
  327. res, Pa_GetErrorText(res));
  328. ret_val = -1;
  329. goto return_unlock;
  330. }
  331. if (ast_pthread_create_background(&pvt->thread, NULL, stream_monitor, pvt)) {
  332. ast_log(LOG_ERROR, "Failed to start stream monitor thread\n");
  333. ret_val = -1;
  334. }
  335. return_unlock:
  336. console_pvt_unlock(pvt);
  337. return ret_val;
  338. }
  339. static int stop_stream(struct console_pvt *pvt)
  340. {
  341. if (!pvt->streamstate || pvt->thread == AST_PTHREADT_NULL)
  342. return 0;
  343. pthread_cancel(pvt->thread);
  344. pthread_kill(pvt->thread, SIGURG);
  345. pthread_join(pvt->thread, NULL);
  346. console_pvt_lock(pvt);
  347. Pa_AbortStream(pvt->stream);
  348. Pa_CloseStream(pvt->stream);
  349. pvt->stream = NULL;
  350. pvt->streamstate = 0;
  351. console_pvt_unlock(pvt);
  352. return 0;
  353. }
  354. /*!
  355. * \note Called with the pvt struct locked
  356. */
  357. static struct ast_channel *console_new(struct console_pvt *pvt, const char *ext, const char *ctx, int state, const char *linkedid)
  358. {
  359. struct ast_channel *chan;
  360. if (!(chan = ast_channel_alloc(1, state, pvt->cid_num, pvt->cid_name, NULL,
  361. ext, ctx, linkedid, 0, "Console/%s", pvt->name))) {
  362. return NULL;
  363. }
  364. chan->tech = &console_tech;
  365. ast_format_set(&chan->readformat, AST_FORMAT_SLINEAR16, 0);
  366. ast_format_set(&chan->writeformat, AST_FORMAT_SLINEAR16, 0);
  367. ast_format_cap_add(chan->nativeformats, &chan->readformat);
  368. chan->tech_pvt = ref_pvt(pvt);
  369. pvt->owner = chan;
  370. if (!ast_strlen_zero(pvt->language))
  371. ast_string_field_set(chan, language, pvt->language);
  372. ast_jb_configure(chan, &global_jbconf);
  373. if (state != AST_STATE_DOWN) {
  374. if (ast_pbx_start(chan)) {
  375. chan->hangupcause = AST_CAUSE_SWITCH_CONGESTION;
  376. ast_hangup(chan);
  377. chan = NULL;
  378. } else
  379. start_stream(pvt);
  380. }
  381. return chan;
  382. }
  383. static struct ast_channel *console_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause)
  384. {
  385. struct ast_channel *chan = NULL;
  386. struct console_pvt *pvt;
  387. char buf[512];
  388. if (!(pvt = find_pvt(data))) {
  389. ast_log(LOG_ERROR, "Console device '%s' not found\n", (char *) data);
  390. return NULL;
  391. }
  392. if (!(ast_format_cap_has_joint(cap, console_tech.capabilities))) {
  393. ast_log(LOG_NOTICE, "Channel requested with unsupported format(s): '%s'\n", ast_getformatname_multiple(buf, sizeof(buf), cap));
  394. goto return_unref;
  395. }
  396. if (pvt->owner) {
  397. ast_log(LOG_NOTICE, "Console channel already active!\n");
  398. *cause = AST_CAUSE_BUSY;
  399. goto return_unref;
  400. }
  401. console_pvt_lock(pvt);
  402. chan = console_new(pvt, NULL, NULL, AST_STATE_DOWN, requestor ? requestor->linkedid : NULL);
  403. console_pvt_unlock(pvt);
  404. if (!chan)
  405. ast_log(LOG_WARNING, "Unable to create new Console channel!\n");
  406. return_unref:
  407. unref_pvt(pvt);
  408. return chan;
  409. }
  410. static int console_digit_begin(struct ast_channel *c, char digit)
  411. {
  412. ast_verb(1, V_BEGIN "Console Received Beginning of Digit %c" V_END, digit);
  413. return -1; /* non-zero to request inband audio */
  414. }
  415. static int console_digit_end(struct ast_channel *c, char digit, unsigned int duration)
  416. {
  417. ast_verb(1, V_BEGIN "Console Received End of Digit %c (duration %u)" V_END,
  418. digit, duration);
  419. return -1; /* non-zero to request inband audio */
  420. }
  421. static int console_text(struct ast_channel *c, const char *text)
  422. {
  423. ast_verb(1, V_BEGIN "Console Received Text '%s'" V_END, text);
  424. return 0;
  425. }
  426. static int console_hangup(struct ast_channel *c)
  427. {
  428. struct console_pvt *pvt = c->tech_pvt;
  429. ast_verb(1, V_BEGIN "Hangup on Console" V_END);
  430. pvt->hookstate = 0;
  431. pvt->owner = NULL;
  432. stop_stream(pvt);
  433. c->tech_pvt = unref_pvt(pvt);
  434. return 0;
  435. }
  436. static int console_answer(struct ast_channel *c)
  437. {
  438. struct console_pvt *pvt = c->tech_pvt;
  439. ast_verb(1, V_BEGIN "Call from Console has been Answered" V_END);
  440. ast_setstate(c, AST_STATE_UP);
  441. return start_stream(pvt);
  442. }
  443. /*
  444. * \brief Implementation of the ast_channel_tech read() callback
  445. *
  446. * Calling this function is harmless. However, if it does get called, it
  447. * is an indication that something weird happened that really shouldn't
  448. * have and is worth looking into.
  449. *
  450. * Why should this function not get called? Well, let me explain. There are
  451. * a couple of ways to pass on audio that has come from this channel. The way
  452. * that this channel driver uses is that once the audio is available, it is
  453. * wrapped in an ast_frame and queued onto the channel using ast_queue_frame().
  454. *
  455. * The other method would be signalling to the core that there is audio waiting,
  456. * and that it needs to call the channel's read() callback to get it. The way
  457. * the channel gets signalled is that one or more file descriptors are placed
  458. * in the fds array on the ast_channel which the core will poll() on. When the
  459. * fd indicates that input is available, the read() callback is called. This
  460. * is especially useful when there is a dedicated file descriptor where the
  461. * audio is read from. An example would be the socket for an RTP stream.
  462. */
  463. static struct ast_frame *console_read(struct ast_channel *chan)
  464. {
  465. ast_debug(1, "I should not be called ...\n");
  466. return &ast_null_frame;
  467. }
  468. static int console_call(struct ast_channel *c, char *dest, int timeout)
  469. {
  470. struct console_pvt *pvt = c->tech_pvt;
  471. enum ast_control_frame_type ctrl;
  472. ast_verb(1, V_BEGIN "Call to device '%s' on console from '%s' <%s>" V_END,
  473. dest,
  474. S_COR(c->caller.id.name.valid, c->caller.id.name.str, ""),
  475. S_COR(c->caller.id.number.valid, c->caller.id.number.str, ""));
  476. console_pvt_lock(pvt);
  477. if (pvt->autoanswer) {
  478. pvt->hookstate = 1;
  479. console_pvt_unlock(pvt);
  480. ast_verb(1, V_BEGIN "Auto-answered" V_END);
  481. ctrl = AST_CONTROL_ANSWER;
  482. } else {
  483. console_pvt_unlock(pvt);
  484. ast_verb(1, V_BEGIN "Type 'console answer' to answer, or use the 'autoanswer' option "
  485. "for future calls" V_END);
  486. ctrl = AST_CONTROL_RINGING;
  487. ast_indicate(c, AST_CONTROL_RINGING);
  488. }
  489. ast_queue_control(c, ctrl);
  490. return start_stream(pvt);
  491. }
  492. static int console_write(struct ast_channel *chan, struct ast_frame *f)
  493. {
  494. struct console_pvt *pvt = chan->tech_pvt;
  495. Pa_WriteStream(pvt->stream, f->data.ptr, f->samples);
  496. return 0;
  497. }
  498. static int console_indicate(struct ast_channel *chan, int cond, const void *data, size_t datalen)
  499. {
  500. struct console_pvt *pvt = chan->tech_pvt;
  501. int res = 0;
  502. switch (cond) {
  503. case AST_CONTROL_BUSY:
  504. case AST_CONTROL_CONGESTION:
  505. case AST_CONTROL_RINGING:
  506. case -1:
  507. res = -1; /* Ask for inband indications */
  508. break;
  509. case AST_CONTROL_PROGRESS:
  510. case AST_CONTROL_PROCEEDING:
  511. case AST_CONTROL_VIDUPDATE:
  512. case AST_CONTROL_SRCUPDATE:
  513. break;
  514. case AST_CONTROL_HOLD:
  515. ast_verb(1, V_BEGIN "Console Has Been Placed on Hold" V_END);
  516. ast_moh_start(chan, data, pvt->mohinterpret);
  517. break;
  518. case AST_CONTROL_UNHOLD:
  519. ast_verb(1, V_BEGIN "Console Has Been Retrieved from Hold" V_END);
  520. ast_moh_stop(chan);
  521. break;
  522. default:
  523. ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n",
  524. cond, chan->name);
  525. /* The core will play inband indications for us if appropriate */
  526. res = -1;
  527. }
  528. return res;
  529. }
  530. static int console_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  531. {
  532. struct console_pvt *pvt = newchan->tech_pvt;
  533. pvt->owner = newchan;
  534. return 0;
  535. }
  536. /*!
  537. * split a string in extension-context, returns pointers to malloc'ed
  538. * strings.
  539. * If we do not have 'overridecontext' then the last @ is considered as
  540. * a context separator, and the context is overridden.
  541. * This is usually not very necessary as you can play with the dialplan,
  542. * and it is nice not to need it because you have '@' in SIP addresses.
  543. * Return value is the buffer address.
  544. *
  545. * \note came from chan_oss
  546. */
  547. static char *ast_ext_ctx(struct console_pvt *pvt, const char *src, char **ext, char **ctx)
  548. {
  549. if (ext == NULL || ctx == NULL)
  550. return NULL; /* error */
  551. *ext = *ctx = NULL;
  552. if (src && *src != '\0')
  553. *ext = ast_strdup(src);
  554. if (*ext == NULL)
  555. return NULL;
  556. if (!pvt->overridecontext) {
  557. /* parse from the right */
  558. *ctx = strrchr(*ext, '@');
  559. if (*ctx)
  560. *(*ctx)++ = '\0';
  561. }
  562. return *ext;
  563. }
  564. static struct console_pvt *get_active_pvt(void)
  565. {
  566. struct console_pvt *pvt;
  567. ast_rwlock_rdlock(&active_lock);
  568. pvt = ref_pvt(active_pvt);
  569. ast_rwlock_unlock(&active_lock);
  570. return pvt;
  571. }
  572. static char *cli_console_autoanswer(struct ast_cli_entry *e, int cmd,
  573. struct ast_cli_args *a)
  574. {
  575. struct console_pvt *pvt = get_active_pvt();
  576. char *res = CLI_SUCCESS;
  577. switch (cmd) {
  578. case CLI_INIT:
  579. e->command = "console {set|show} autoanswer [on|off]";
  580. e->usage =
  581. "Usage: console {set|show} autoanswer [on|off]\n"
  582. " Enables or disables autoanswer feature. If used without\n"
  583. " argument, displays the current on/off status of autoanswer.\n"
  584. " The default value of autoanswer is in 'oss.conf'.\n";
  585. return NULL;
  586. case CLI_GENERATE:
  587. return NULL;
  588. }
  589. if (!pvt) {
  590. ast_cli(a->fd, "No console device is set as active.\n");
  591. return CLI_FAILURE;
  592. }
  593. if (a->argc == e->args - 1) {
  594. ast_cli(a->fd, "Auto answer is %s.\n", pvt->autoanswer ? "on" : "off");
  595. unref_pvt(pvt);
  596. return CLI_SUCCESS;
  597. }
  598. if (a->argc != e->args) {
  599. unref_pvt(pvt);
  600. return CLI_SHOWUSAGE;
  601. }
  602. if (!strcasecmp(a->argv[e->args-1], "on"))
  603. pvt->autoanswer = 1;
  604. else if (!strcasecmp(a->argv[e->args - 1], "off"))
  605. pvt->autoanswer = 0;
  606. else
  607. res = CLI_SHOWUSAGE;
  608. unref_pvt(pvt);
  609. return res;
  610. }
  611. static char *cli_console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  612. {
  613. struct console_pvt *pvt = get_active_pvt();
  614. if (cmd == CLI_INIT) {
  615. e->command = "console flash";
  616. e->usage =
  617. "Usage: console flash\n"
  618. " Flashes the call currently placed on the console.\n";
  619. return NULL;
  620. } else if (cmd == CLI_GENERATE)
  621. return NULL;
  622. if (!pvt) {
  623. ast_cli(a->fd, "No console device is set as active\n");
  624. return CLI_FAILURE;
  625. }
  626. if (a->argc != e->args)
  627. return CLI_SHOWUSAGE;
  628. if (!pvt->owner) {
  629. ast_cli(a->fd, "No call to flash\n");
  630. unref_pvt(pvt);
  631. return CLI_FAILURE;
  632. }
  633. pvt->hookstate = 0;
  634. ast_queue_control(pvt->owner, AST_CONTROL_FLASH);
  635. unref_pvt(pvt);
  636. return CLI_SUCCESS;
  637. }
  638. static char *cli_console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  639. {
  640. char *s = NULL;
  641. const char *mye = NULL, *myc = NULL;
  642. struct console_pvt *pvt = get_active_pvt();
  643. if (cmd == CLI_INIT) {
  644. e->command = "console dial";
  645. e->usage =
  646. "Usage: console dial [extension[@context]]\n"
  647. " Dials a given extension (and context if specified)\n";
  648. return NULL;
  649. } else if (cmd == CLI_GENERATE)
  650. return NULL;
  651. if (!pvt) {
  652. ast_cli(a->fd, "No console device is currently set as active\n");
  653. return CLI_FAILURE;
  654. }
  655. if (a->argc > e->args + 1)
  656. return CLI_SHOWUSAGE;
  657. if (pvt->owner) { /* already in a call */
  658. int i;
  659. struct ast_frame f = { AST_FRAME_DTMF };
  660. const char *s;
  661. if (a->argc == e->args) { /* argument is mandatory here */
  662. ast_cli(a->fd, "Already in a call. You can only dial digits until you hangup.\n");
  663. unref_pvt(pvt);
  664. return CLI_FAILURE;
  665. }
  666. s = a->argv[e->args];
  667. /* send the string one char at a time */
  668. for (i = 0; i < strlen(s); i++) {
  669. f.subclass.integer = s[i];
  670. ast_queue_frame(pvt->owner, &f);
  671. }
  672. unref_pvt(pvt);
  673. return CLI_SUCCESS;
  674. }
  675. /* if we have an argument split it into extension and context */
  676. if (a->argc == e->args + 1) {
  677. char *ext = NULL, *con = NULL;
  678. s = ast_ext_ctx(pvt, a->argv[e->args], &ext, &con);
  679. ast_debug(1, "provided '%s', exten '%s' context '%s'\n",
  680. a->argv[e->args], mye, myc);
  681. mye = ext;
  682. myc = con;
  683. }
  684. /* supply default values if needed */
  685. if (ast_strlen_zero(mye))
  686. mye = pvt->exten;
  687. if (ast_strlen_zero(myc))
  688. myc = pvt->context;
  689. if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
  690. console_pvt_lock(pvt);
  691. pvt->hookstate = 1;
  692. console_new(pvt, mye, myc, AST_STATE_RINGING, NULL);
  693. console_pvt_unlock(pvt);
  694. } else
  695. ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
  696. free(s);
  697. unref_pvt(pvt);
  698. return CLI_SUCCESS;
  699. }
  700. static char *cli_console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  701. {
  702. struct console_pvt *pvt = get_active_pvt();
  703. if (cmd == CLI_INIT) {
  704. e->command = "console hangup";
  705. e->usage =
  706. "Usage: console hangup\n"
  707. " Hangs up any call currently placed on the console.\n";
  708. return NULL;
  709. } else if (cmd == CLI_GENERATE)
  710. return NULL;
  711. if (!pvt) {
  712. ast_cli(a->fd, "No console device is set as active\n");
  713. return CLI_FAILURE;
  714. }
  715. if (a->argc != e->args)
  716. return CLI_SHOWUSAGE;
  717. if (!pvt->owner && !pvt->hookstate) {
  718. ast_cli(a->fd, "No call to hang up\n");
  719. unref_pvt(pvt);
  720. return CLI_FAILURE;
  721. }
  722. pvt->hookstate = 0;
  723. if (pvt->owner)
  724. ast_queue_hangup(pvt->owner);
  725. unref_pvt(pvt);
  726. return CLI_SUCCESS;
  727. }
  728. static char *cli_console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  729. {
  730. const char *s;
  731. struct console_pvt *pvt = get_active_pvt();
  732. char *res = CLI_SUCCESS;
  733. if (cmd == CLI_INIT) {
  734. e->command = "console {mute|unmute}";
  735. e->usage =
  736. "Usage: console {mute|unmute}\n"
  737. " Mute/unmute the microphone.\n";
  738. return NULL;
  739. } else if (cmd == CLI_GENERATE)
  740. return NULL;
  741. if (!pvt) {
  742. ast_cli(a->fd, "No console device is set as active\n");
  743. return CLI_FAILURE;
  744. }
  745. if (a->argc != e->args)
  746. return CLI_SHOWUSAGE;
  747. s = a->argv[e->args-1];
  748. if (!strcasecmp(s, "mute"))
  749. pvt->muted = 1;
  750. else if (!strcasecmp(s, "unmute"))
  751. pvt->muted = 0;
  752. else
  753. res = CLI_SHOWUSAGE;
  754. ast_verb(1, V_BEGIN "The Console is now %s" V_END,
  755. pvt->muted ? "Muted" : "Unmuted");
  756. unref_pvt(pvt);
  757. return res;
  758. }
  759. static char *cli_list_available(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  760. {
  761. PaDeviceIndex idx, num, def_input, def_output;
  762. if (cmd == CLI_INIT) {
  763. e->command = "console list available";
  764. e->usage =
  765. "Usage: console list available\n"
  766. " List all available devices.\n";
  767. return NULL;
  768. } else if (cmd == CLI_GENERATE)
  769. return NULL;
  770. if (a->argc != e->args)
  771. return CLI_SHOWUSAGE;
  772. ast_cli(a->fd, "\n"
  773. "=============================================================\n"
  774. "=== Available Devices =======================================\n"
  775. "=============================================================\n"
  776. "===\n");
  777. num = Pa_GetDeviceCount();
  778. if (!num) {
  779. ast_cli(a->fd, "(None)\n");
  780. return CLI_SUCCESS;
  781. }
  782. def_input = Pa_GetDefaultInputDevice();
  783. def_output = Pa_GetDefaultOutputDevice();
  784. for (idx = 0; idx < num; idx++) {
  785. const PaDeviceInfo *dev = Pa_GetDeviceInfo(idx);
  786. if (!dev)
  787. continue;
  788. ast_cli(a->fd, "=== ---------------------------------------------------------\n"
  789. "=== Device Name: %s\n", dev->name);
  790. if (dev->maxInputChannels)
  791. ast_cli(a->fd, "=== ---> %sInput Device\n", (idx == def_input) ? "Default " : "");
  792. if (dev->maxOutputChannels)
  793. ast_cli(a->fd, "=== ---> %sOutput Device\n", (idx == def_output) ? "Default " : "");
  794. ast_cli(a->fd, "=== ---------------------------------------------------------\n===\n");
  795. }
  796. ast_cli(a->fd, "=============================================================\n\n");
  797. return CLI_SUCCESS;
  798. }
  799. static char *cli_list_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  800. {
  801. struct ao2_iterator i;
  802. struct console_pvt *pvt;
  803. if (cmd == CLI_INIT) {
  804. e->command = "console list devices";
  805. e->usage =
  806. "Usage: console list devices\n"
  807. " List all configured devices.\n";
  808. return NULL;
  809. } else if (cmd == CLI_GENERATE)
  810. return NULL;
  811. if (a->argc != e->args)
  812. return CLI_SHOWUSAGE;
  813. ast_cli(a->fd, "\n"
  814. "=============================================================\n"
  815. "=== Configured Devices ======================================\n"
  816. "=============================================================\n"
  817. "===\n");
  818. i = ao2_iterator_init(pvts, 0);
  819. while ((pvt = ao2_iterator_next(&i))) {
  820. console_pvt_lock(pvt);
  821. ast_cli(a->fd, "=== ---------------------------------------------------------\n"
  822. "=== Device Name: %s\n"
  823. "=== ---> Active: %s\n"
  824. "=== ---> Input Device: %s\n"
  825. "=== ---> Output Device: %s\n"
  826. "=== ---> Context: %s\n"
  827. "=== ---> Extension: %s\n"
  828. "=== ---> CallerID Num: %s\n"
  829. "=== ---> CallerID Name: %s\n"
  830. "=== ---> MOH Interpret: %s\n"
  831. "=== ---> Language: %s\n"
  832. "=== ---> Parkinglot: %s\n"
  833. "=== ---> Muted: %s\n"
  834. "=== ---> Auto-Answer: %s\n"
  835. "=== ---> Override Context: %s\n"
  836. "=== ---------------------------------------------------------\n===\n",
  837. pvt->name, (pvt == active_pvt) ? "Yes" : "No",
  838. pvt->input_device, pvt->output_device, pvt->context,
  839. pvt->exten, pvt->cid_num, pvt->cid_name, pvt->mohinterpret,
  840. pvt->language, pvt->parkinglot, pvt->muted ? "Yes" : "No", pvt->autoanswer ? "Yes" : "No",
  841. pvt->overridecontext ? "Yes" : "No");
  842. console_pvt_unlock(pvt);
  843. unref_pvt(pvt);
  844. }
  845. ao2_iterator_destroy(&i);
  846. ast_cli(a->fd, "=============================================================\n\n");
  847. return CLI_SUCCESS;
  848. }
  849. /*!
  850. * \brief answer command from the console
  851. */
  852. static char *cli_console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  853. {
  854. struct console_pvt *pvt = get_active_pvt();
  855. switch (cmd) {
  856. case CLI_INIT:
  857. e->command = "console answer";
  858. e->usage =
  859. "Usage: console answer\n"
  860. " Answers an incoming call on the console channel.\n";
  861. return NULL;
  862. case CLI_GENERATE:
  863. return NULL; /* no completion */
  864. }
  865. if (!pvt) {
  866. ast_cli(a->fd, "No console device is set as active\n");
  867. return CLI_FAILURE;
  868. }
  869. if (a->argc != e->args) {
  870. unref_pvt(pvt);
  871. return CLI_SHOWUSAGE;
  872. }
  873. if (!pvt->owner) {
  874. ast_cli(a->fd, "No one is calling us\n");
  875. unref_pvt(pvt);
  876. return CLI_FAILURE;
  877. }
  878. pvt->hookstate = 1;
  879. ast_indicate(pvt->owner, -1);
  880. ast_queue_control(pvt->owner, AST_CONTROL_ANSWER);
  881. unref_pvt(pvt);
  882. return CLI_SUCCESS;
  883. }
  884. /*!
  885. * \brief Console send text CLI command
  886. *
  887. * \note concatenate all arguments into a single string. argv is NULL-terminated
  888. * so we can use it right away
  889. */
  890. static char *cli_console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  891. {
  892. char buf[TEXT_SIZE];
  893. struct console_pvt *pvt = get_active_pvt();
  894. struct ast_frame f = {
  895. .frametype = AST_FRAME_TEXT,
  896. .data.ptr = buf,
  897. .src = "console_send_text",
  898. };
  899. int len;
  900. if (cmd == CLI_INIT) {
  901. e->command = "console send text";
  902. e->usage =
  903. "Usage: console send text <message>\n"
  904. " Sends a text message for display on the remote terminal.\n";
  905. return NULL;
  906. } else if (cmd == CLI_GENERATE)
  907. return NULL;
  908. if (!pvt) {
  909. ast_cli(a->fd, "No console device is set as active\n");
  910. return CLI_FAILURE;
  911. }
  912. if (a->argc < e->args + 1) {
  913. unref_pvt(pvt);
  914. return CLI_SHOWUSAGE;
  915. }
  916. if (!pvt->owner) {
  917. ast_cli(a->fd, "Not in a call\n");
  918. unref_pvt(pvt);
  919. return CLI_FAILURE;
  920. }
  921. ast_join(buf, sizeof(buf) - 1, a->argv + e->args);
  922. if (ast_strlen_zero(buf)) {
  923. unref_pvt(pvt);
  924. return CLI_SHOWUSAGE;
  925. }
  926. len = strlen(buf);
  927. buf[len] = '\n';
  928. f.datalen = len + 1;
  929. ast_queue_frame(pvt->owner, &f);
  930. unref_pvt(pvt);
  931. return CLI_SUCCESS;
  932. }
  933. static void set_active(struct console_pvt *pvt, const char *value)
  934. {
  935. if (pvt == &globals) {
  936. ast_log(LOG_ERROR, "active is only valid as a per-device setting\n");
  937. return;
  938. }
  939. if (!ast_true(value))
  940. return;
  941. ast_rwlock_wrlock(&active_lock);
  942. if (active_pvt)
  943. unref_pvt(active_pvt);
  944. active_pvt = ref_pvt(pvt);
  945. ast_rwlock_unlock(&active_lock);
  946. }
  947. static char *cli_console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  948. {
  949. struct console_pvt *pvt;
  950. switch (cmd) {
  951. case CLI_INIT:
  952. e->command = "console {set|show} active";
  953. e->usage =
  954. "Usage: console {set|show} active [<device>]\n"
  955. " Set or show the active console device for the Asterisk CLI.\n";
  956. return NULL;
  957. case CLI_GENERATE:
  958. if (a->pos == e->args) {
  959. struct ao2_iterator i;
  960. int x = 0;
  961. char *res = NULL;
  962. i = ao2_iterator_init(pvts, 0);
  963. while ((pvt = ao2_iterator_next(&i))) {
  964. if (++x > a->n && !strncasecmp(pvt->name, a->word, strlen(a->word)))
  965. res = ast_strdup(pvt->name);
  966. unref_pvt(pvt);
  967. if (res) {
  968. ao2_iterator_destroy(&i);
  969. return res;
  970. }
  971. }
  972. ao2_iterator_destroy(&i);
  973. }
  974. return NULL;
  975. }
  976. if (a->argc < e->args)
  977. return CLI_SHOWUSAGE;
  978. if (a->argc == 3) {
  979. pvt = get_active_pvt();
  980. if (!pvt)
  981. ast_cli(a->fd, "No device is currently set as the active console device.\n");
  982. else {
  983. console_pvt_lock(pvt);
  984. ast_cli(a->fd, "The active console device is '%s'.\n", pvt->name);
  985. console_pvt_unlock(pvt);
  986. pvt = unref_pvt(pvt);
  987. }
  988. return CLI_SUCCESS;
  989. }
  990. if (!(pvt = find_pvt(a->argv[e->args - 1]))) {
  991. ast_cli(a->fd, "Could not find a device called '%s'.\n", a->argv[e->args]);
  992. return CLI_FAILURE;
  993. }
  994. set_active(pvt, "yes");
  995. console_pvt_lock(pvt);
  996. ast_cli(a->fd, "The active console device has been set to '%s'\n", pvt->name);
  997. console_pvt_unlock(pvt);
  998. unref_pvt(pvt);
  999. return CLI_SUCCESS;
  1000. }
  1001. static struct ast_cli_entry cli_console[] = {
  1002. AST_CLI_DEFINE(cli_console_dial, "Dial an extension from the console"),
  1003. AST_CLI_DEFINE(cli_console_hangup, "Hangup a call on the console"),
  1004. AST_CLI_DEFINE(cli_console_mute, "Disable/Enable mic input"),
  1005. AST_CLI_DEFINE(cli_console_answer, "Answer an incoming console call"),
  1006. AST_CLI_DEFINE(cli_console_sendtext, "Send text to a connected party"),
  1007. AST_CLI_DEFINE(cli_console_flash, "Send a flash to the connected party"),
  1008. AST_CLI_DEFINE(cli_console_autoanswer, "Turn autoanswer on or off"),
  1009. AST_CLI_DEFINE(cli_list_available, "List available devices"),
  1010. AST_CLI_DEFINE(cli_list_devices, "List configured devices"),
  1011. AST_CLI_DEFINE(cli_console_active, "View or Set the active console device"),
  1012. };
  1013. /*!
  1014. * \brief Set default values for a pvt struct
  1015. *
  1016. * \note This function expects the pvt lock to be held.
  1017. */
  1018. static void set_pvt_defaults(struct console_pvt *pvt)
  1019. {
  1020. if (pvt == &globals) {
  1021. ast_string_field_set(pvt, mohinterpret, "default");
  1022. ast_string_field_set(pvt, context, "default");
  1023. ast_string_field_set(pvt, exten, "s");
  1024. ast_string_field_set(pvt, language, "");
  1025. ast_string_field_set(pvt, cid_num, "");
  1026. ast_string_field_set(pvt, cid_name, "");
  1027. ast_string_field_set(pvt, parkinglot, "");
  1028. pvt->overridecontext = 0;
  1029. pvt->autoanswer = 0;
  1030. } else {
  1031. ast_mutex_lock(&globals_lock);
  1032. ast_string_field_set(pvt, mohinterpret, globals.mohinterpret);
  1033. ast_string_field_set(pvt, context, globals.context);
  1034. ast_string_field_set(pvt, exten, globals.exten);
  1035. ast_string_field_set(pvt, language, globals.language);
  1036. ast_string_field_set(pvt, cid_num, globals.cid_num);
  1037. ast_string_field_set(pvt, cid_name, globals.cid_name);
  1038. ast_string_field_set(pvt, parkinglot, globals.parkinglot);
  1039. pvt->overridecontext = globals.overridecontext;
  1040. pvt->autoanswer = globals.autoanswer;
  1041. ast_mutex_unlock(&globals_lock);
  1042. }
  1043. }
  1044. static void store_callerid(struct console_pvt *pvt, const char *value)
  1045. {
  1046. char cid_name[256];
  1047. char cid_num[256];
  1048. ast_callerid_split(value, cid_name, sizeof(cid_name),
  1049. cid_num, sizeof(cid_num));
  1050. ast_string_field_set(pvt, cid_name, cid_name);
  1051. ast_string_field_set(pvt, cid_num, cid_num);
  1052. }
  1053. /*!
  1054. * \brief Store a configuration parameter in a pvt struct
  1055. *
  1056. * \note This function expects the pvt lock to be held.
  1057. */
  1058. static void store_config_core(struct console_pvt *pvt, const char *var, const char *value)
  1059. {
  1060. if (pvt == &globals && !ast_jb_read_conf(&global_jbconf, var, value))
  1061. return;
  1062. CV_START(var, value);
  1063. CV_STRFIELD("context", pvt, context);
  1064. CV_STRFIELD("extension", pvt, exten);
  1065. CV_STRFIELD("mohinterpret", pvt, mohinterpret);
  1066. CV_STRFIELD("language", pvt, language);
  1067. CV_F("callerid", store_callerid(pvt, value));
  1068. CV_BOOL("overridecontext", pvt->overridecontext);
  1069. CV_BOOL("autoanswer", pvt->autoanswer);
  1070. CV_STRFIELD("parkinglot", pvt, parkinglot);
  1071. if (pvt != &globals) {
  1072. CV_F("active", set_active(pvt, value))
  1073. CV_STRFIELD("input_device", pvt, input_device);
  1074. CV_STRFIELD("output_device", pvt, output_device);
  1075. }
  1076. ast_log(LOG_WARNING, "Unknown option '%s'\n", var);
  1077. CV_END;
  1078. }
  1079. static void pvt_destructor(void *obj)
  1080. {
  1081. struct console_pvt *pvt = obj;
  1082. ast_string_field_free_memory(pvt);
  1083. }
  1084. static int init_pvt(struct console_pvt *pvt, const char *name)
  1085. {
  1086. pvt->thread = AST_PTHREADT_NULL;
  1087. if (ast_string_field_init(pvt, 32))
  1088. return -1;
  1089. ast_string_field_set(pvt, name, S_OR(name, ""));
  1090. return 0;
  1091. }
  1092. static void build_device(struct ast_config *cfg, const char *name)
  1093. {
  1094. struct ast_variable *v;
  1095. struct console_pvt *pvt;
  1096. int new = 0;
  1097. if ((pvt = find_pvt(name))) {
  1098. console_pvt_lock(pvt);
  1099. set_pvt_defaults(pvt);
  1100. pvt->destroy = 0;
  1101. } else {
  1102. if (!(pvt = ao2_alloc(sizeof(*pvt), pvt_destructor)))
  1103. return;
  1104. init_pvt(pvt, name);
  1105. set_pvt_defaults(pvt);
  1106. new = 1;
  1107. }
  1108. for (v = ast_variable_browse(cfg, name); v; v = v->next)
  1109. store_config_core(pvt, v->name, v->value);
  1110. if (new)
  1111. ao2_link(pvts, pvt);
  1112. else
  1113. console_pvt_unlock(pvt);
  1114. unref_pvt(pvt);
  1115. }
  1116. static int pvt_mark_destroy_cb(void *obj, void *arg, int flags)
  1117. {
  1118. struct console_pvt *pvt = obj;
  1119. pvt->destroy = 1;
  1120. return 0;
  1121. }
  1122. static void destroy_pvts(void)
  1123. {
  1124. struct ao2_iterator i;
  1125. struct console_pvt *pvt;
  1126. i = ao2_iterator_init(pvts, 0);
  1127. while ((pvt = ao2_iterator_next(&i))) {
  1128. if (pvt->destroy) {
  1129. ao2_unlink(pvts, pvt);
  1130. ast_rwlock_wrlock(&active_lock);
  1131. if (active_pvt == pvt)
  1132. active_pvt = unref_pvt(pvt);
  1133. ast_rwlock_unlock(&active_lock);
  1134. }
  1135. unref_pvt(pvt);
  1136. }
  1137. ao2_iterator_destroy(&i);
  1138. }
  1139. /*!
  1140. * \brief Load the configuration
  1141. * \param reload if this was called due to a reload
  1142. * \retval 0 success
  1143. * \retval -1 failure
  1144. */
  1145. static int load_config(int reload)
  1146. {
  1147. struct ast_config *cfg;
  1148. struct ast_variable *v;
  1149. struct ast_flags config_flags = { 0 };
  1150. char *context = NULL;
  1151. /* default values */
  1152. memcpy(&global_jbconf, &default_jbconf, sizeof(global_jbconf));
  1153. ast_mutex_lock(&globals_lock);
  1154. set_pvt_defaults(&globals);
  1155. ast_mutex_unlock(&globals_lock);
  1156. if (!(cfg = ast_config_load(config_file, config_flags))) {
  1157. ast_log(LOG_NOTICE, "Unable to open configuration file %s!\n", config_file);
  1158. return -1;
  1159. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  1160. ast_log(LOG_NOTICE, "Config file %s has an invalid format\n", config_file);
  1161. return -1;
  1162. }
  1163. ao2_callback(pvts, OBJ_NODATA, pvt_mark_destroy_cb, NULL);
  1164. ast_mutex_lock(&globals_lock);
  1165. for (v = ast_variable_browse(cfg, "general"); v; v = v->next)
  1166. store_config_core(&globals, v->name, v->value);
  1167. ast_mutex_unlock(&globals_lock);
  1168. while ((context = ast_category_browse(cfg, context))) {
  1169. if (strcasecmp(context, "general"))
  1170. build_device(cfg, context);
  1171. }
  1172. ast_config_destroy(cfg);
  1173. destroy_pvts();
  1174. return 0;
  1175. }
  1176. static int pvt_hash_cb(const void *obj, const int flags)
  1177. {
  1178. const struct console_pvt *pvt = obj;
  1179. return ast_str_case_hash(pvt->name);
  1180. }
  1181. static int pvt_cmp_cb(void *obj, void *arg, int flags)
  1182. {
  1183. struct console_pvt *pvt = obj, *pvt2 = arg;
  1184. return !strcasecmp(pvt->name, pvt2->name) ? CMP_MATCH | CMP_STOP : 0;
  1185. }
  1186. static void stop_streams(void)
  1187. {
  1188. struct console_pvt *pvt;
  1189. struct ao2_iterator i;
  1190. i = ao2_iterator_init(pvts, 0);
  1191. while ((pvt = ao2_iterator_next(&i))) {
  1192. if (pvt->hookstate)
  1193. stop_stream(pvt);
  1194. unref_pvt(pvt);
  1195. }
  1196. ao2_iterator_destroy(&i);
  1197. }
  1198. static int unload_module(void)
  1199. {
  1200. console_tech.capabilities = ast_format_cap_destroy(console_tech.capabilities);
  1201. ast_channel_unregister(&console_tech);
  1202. ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console));
  1203. stop_streams();
  1204. Pa_Terminate();
  1205. /* Will unref all the pvts so they will get destroyed, too */
  1206. ao2_ref(pvts, -1);
  1207. pvt_destructor(&globals);
  1208. return 0;
  1209. }
  1210. static int load_module(void)
  1211. {
  1212. struct ast_format tmpfmt;
  1213. PaError res;
  1214. if (!(console_tech.capabilities = ast_format_cap_alloc())) {
  1215. return AST_MODULE_LOAD_DECLINE;
  1216. }
  1217. ast_format_cap_add(console_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0));
  1218. init_pvt(&globals, NULL);
  1219. if (!(pvts = ao2_container_alloc(NUM_PVT_BUCKETS, pvt_hash_cb, pvt_cmp_cb)))
  1220. goto return_error;
  1221. if (load_config(0))
  1222. goto return_error;
  1223. res = Pa_Initialize();
  1224. if (res != paNoError) {
  1225. ast_log(LOG_WARNING, "Failed to initialize audio system - (%d) %s\n",
  1226. res, Pa_GetErrorText(res));
  1227. goto return_error_pa_init;
  1228. }
  1229. if (ast_channel_register(&console_tech)) {
  1230. ast_log(LOG_ERROR, "Unable to register channel type 'Console'\n");
  1231. goto return_error_chan_reg;
  1232. }
  1233. if (ast_cli_register_multiple(cli_console, ARRAY_LEN(cli_console)))
  1234. goto return_error_cli_reg;
  1235. return AST_MODULE_LOAD_SUCCESS;
  1236. return_error_cli_reg:
  1237. ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console));
  1238. return_error_chan_reg:
  1239. ast_channel_unregister(&console_tech);
  1240. return_error_pa_init:
  1241. Pa_Terminate();
  1242. return_error:
  1243. if (pvts)
  1244. ao2_ref(pvts, -1);
  1245. pvts = NULL;
  1246. pvt_destructor(&globals);
  1247. return AST_MODULE_LOAD_DECLINE;
  1248. }
  1249. static int reload(void)
  1250. {
  1251. return load_config(1);
  1252. }
  1253. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Console Channel Driver",
  1254. .load = load_module,
  1255. .unload = unload_module,
  1256. .reload = reload,
  1257. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  1258. );