chan_console.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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, const char *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, const 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. ast_channel_tech_set(chan, &console_tech);
  365. ast_format_set(ast_channel_readformat(chan), AST_FORMAT_SLINEAR16, 0);
  366. ast_format_set(ast_channel_writeformat(chan), AST_FORMAT_SLINEAR16, 0);
  367. ast_format_cap_add(ast_channel_nativeformats(chan), ast_channel_readformat(chan));
  368. ast_channel_tech_pvt_set(chan, ref_pvt(pvt));
  369. pvt->owner = chan;
  370. if (!ast_strlen_zero(pvt->language))
  371. ast_channel_language_set(chan, pvt->language);
  372. ast_jb_configure(chan, &global_jbconf);
  373. if (state != AST_STATE_DOWN) {
  374. if (ast_pbx_start(chan)) {
  375. ast_channel_hangupcause_set(chan, 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, const char *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", 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 ? ast_channel_linkedid(requestor) : 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 = ast_channel_tech_pvt(c);
  429. ast_verb(1, V_BEGIN "Hangup on Console" V_END);
  430. pvt->hookstate = 0;
  431. pvt->owner = NULL;
  432. stop_stream(pvt);
  433. ast_channel_tech_pvt_set(c, unref_pvt(pvt));
  434. return 0;
  435. }
  436. static int console_answer(struct ast_channel *c)
  437. {
  438. struct console_pvt *pvt = ast_channel_tech_pvt(c);
  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, const char *dest, int timeout)
  469. {
  470. struct console_pvt *pvt = ast_channel_tech_pvt(c);
  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(ast_channel_caller(c)->id.name.valid, ast_channel_caller(c)->id.name.str, ""),
  475. S_COR(ast_channel_caller(c)->id.number.valid, ast_channel_caller(c)->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 = ast_channel_tech_pvt(chan);
  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 = ast_channel_tech_pvt(chan);
  501. int res = 0;
  502. switch (cond) {
  503. case AST_CONTROL_BUSY:
  504. case AST_CONTROL_CONGESTION:
  505. case AST_CONTROL_RINGING:
  506. case AST_CONTROL_INCOMPLETE:
  507. case AST_CONTROL_PVT_CAUSE_CODE:
  508. case -1:
  509. res = -1; /* Ask for inband indications */
  510. break;
  511. case AST_CONTROL_PROGRESS:
  512. case AST_CONTROL_PROCEEDING:
  513. case AST_CONTROL_VIDUPDATE:
  514. case AST_CONTROL_SRCUPDATE:
  515. break;
  516. case AST_CONTROL_HOLD:
  517. ast_verb(1, V_BEGIN "Console Has Been Placed on Hold" V_END);
  518. ast_moh_start(chan, data, pvt->mohinterpret);
  519. break;
  520. case AST_CONTROL_UNHOLD:
  521. ast_verb(1, V_BEGIN "Console Has Been Retrieved from Hold" V_END);
  522. ast_moh_stop(chan);
  523. break;
  524. default:
  525. ast_log(LOG_WARNING, "Don't know how to display condition %d on %s\n",
  526. cond, ast_channel_name(chan));
  527. /* The core will play inband indications for us if appropriate */
  528. res = -1;
  529. }
  530. return res;
  531. }
  532. static int console_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  533. {
  534. struct console_pvt *pvt = ast_channel_tech_pvt(newchan);
  535. pvt->owner = newchan;
  536. return 0;
  537. }
  538. /*!
  539. * split a string in extension-context, returns pointers to malloc'ed
  540. * strings.
  541. * If we do not have 'overridecontext' then the last @ is considered as
  542. * a context separator, and the context is overridden.
  543. * This is usually not very necessary as you can play with the dialplan,
  544. * and it is nice not to need it because you have '@' in SIP addresses.
  545. * Return value is the buffer address.
  546. *
  547. * \note came from chan_oss
  548. */
  549. static char *ast_ext_ctx(struct console_pvt *pvt, const char *src, char **ext, char **ctx)
  550. {
  551. if (ext == NULL || ctx == NULL)
  552. return NULL; /* error */
  553. *ext = *ctx = NULL;
  554. if (src && *src != '\0')
  555. *ext = ast_strdup(src);
  556. if (*ext == NULL)
  557. return NULL;
  558. if (!pvt->overridecontext) {
  559. /* parse from the right */
  560. *ctx = strrchr(*ext, '@');
  561. if (*ctx)
  562. *(*ctx)++ = '\0';
  563. }
  564. return *ext;
  565. }
  566. static struct console_pvt *get_active_pvt(void)
  567. {
  568. struct console_pvt *pvt;
  569. ast_rwlock_rdlock(&active_lock);
  570. pvt = ref_pvt(active_pvt);
  571. ast_rwlock_unlock(&active_lock);
  572. return pvt;
  573. }
  574. static char *cli_console_autoanswer(struct ast_cli_entry *e, int cmd,
  575. struct ast_cli_args *a)
  576. {
  577. struct console_pvt *pvt;
  578. char *res = CLI_SUCCESS;
  579. switch (cmd) {
  580. case CLI_INIT:
  581. e->command = "console {set|show} autoanswer [on|off]";
  582. e->usage =
  583. "Usage: console {set|show} autoanswer [on|off]\n"
  584. " Enables or disables autoanswer feature. If used without\n"
  585. " argument, displays the current on/off status of autoanswer.\n"
  586. " The default value of autoanswer is in 'oss.conf'.\n";
  587. return NULL;
  588. case CLI_GENERATE:
  589. return NULL;
  590. }
  591. pvt = get_active_pvt();
  592. if (!pvt) {
  593. ast_cli(a->fd, "No console device is set as active.\n");
  594. return CLI_FAILURE;
  595. }
  596. if (a->argc == e->args - 1) {
  597. ast_cli(a->fd, "Auto answer is %s.\n", pvt->autoanswer ? "on" : "off");
  598. unref_pvt(pvt);
  599. return CLI_SUCCESS;
  600. }
  601. if (a->argc != e->args) {
  602. unref_pvt(pvt);
  603. return CLI_SHOWUSAGE;
  604. }
  605. if (!strcasecmp(a->argv[e->args-1], "on"))
  606. pvt->autoanswer = 1;
  607. else if (!strcasecmp(a->argv[e->args - 1], "off"))
  608. pvt->autoanswer = 0;
  609. else
  610. res = CLI_SHOWUSAGE;
  611. unref_pvt(pvt);
  612. return res;
  613. }
  614. static char *cli_console_flash(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  615. {
  616. struct console_pvt *pvt;
  617. if (cmd == CLI_INIT) {
  618. e->command = "console flash";
  619. e->usage =
  620. "Usage: console flash\n"
  621. " Flashes the call currently placed on the console.\n";
  622. return NULL;
  623. } else if (cmd == CLI_GENERATE) {
  624. return NULL;
  625. }
  626. if (a->argc != e->args) {
  627. return CLI_SHOWUSAGE;
  628. }
  629. pvt = get_active_pvt();
  630. if (!pvt) {
  631. ast_cli(a->fd, "No console device is set as active\n");
  632. return CLI_FAILURE;
  633. }
  634. if (!pvt->owner) {
  635. ast_cli(a->fd, "No call to flash\n");
  636. unref_pvt(pvt);
  637. return CLI_FAILURE;
  638. }
  639. pvt->hookstate = 0;
  640. ast_queue_control(pvt->owner, AST_CONTROL_FLASH);
  641. unref_pvt(pvt);
  642. return CLI_SUCCESS;
  643. }
  644. static char *cli_console_dial(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  645. {
  646. char *s = NULL;
  647. const char *mye = NULL, *myc = NULL;
  648. struct console_pvt *pvt;
  649. if (cmd == CLI_INIT) {
  650. e->command = "console dial";
  651. e->usage =
  652. "Usage: console dial [extension[@context]]\n"
  653. " Dials a given extension (and context if specified)\n";
  654. return NULL;
  655. } else if (cmd == CLI_GENERATE) {
  656. return NULL;
  657. }
  658. if (a->argc > e->args + 1) {
  659. return CLI_SHOWUSAGE;
  660. }
  661. pvt = get_active_pvt();
  662. if (!pvt) {
  663. ast_cli(a->fd, "No console device is currently set as active\n");
  664. return CLI_FAILURE;
  665. }
  666. if (pvt->owner) { /* already in a call */
  667. int i;
  668. struct ast_frame f = { AST_FRAME_DTMF };
  669. const char *s;
  670. if (a->argc == e->args) { /* argument is mandatory here */
  671. ast_cli(a->fd, "Already in a call. You can only dial digits until you hangup.\n");
  672. unref_pvt(pvt);
  673. return CLI_FAILURE;
  674. }
  675. s = a->argv[e->args];
  676. /* send the string one char at a time */
  677. for (i = 0; i < strlen(s); i++) {
  678. f.subclass.integer = s[i];
  679. ast_queue_frame(pvt->owner, &f);
  680. }
  681. unref_pvt(pvt);
  682. return CLI_SUCCESS;
  683. }
  684. /* if we have an argument split it into extension and context */
  685. if (a->argc == e->args + 1) {
  686. char *ext = NULL, *con = NULL;
  687. s = ast_ext_ctx(pvt, a->argv[e->args], &ext, &con);
  688. ast_debug(1, "provided '%s', exten '%s' context '%s'\n",
  689. a->argv[e->args], mye, myc);
  690. mye = ext;
  691. myc = con;
  692. }
  693. /* supply default values if needed */
  694. if (ast_strlen_zero(mye))
  695. mye = pvt->exten;
  696. if (ast_strlen_zero(myc))
  697. myc = pvt->context;
  698. if (ast_exists_extension(NULL, myc, mye, 1, NULL)) {
  699. console_pvt_lock(pvt);
  700. pvt->hookstate = 1;
  701. console_new(pvt, mye, myc, AST_STATE_RINGING, NULL);
  702. console_pvt_unlock(pvt);
  703. } else
  704. ast_cli(a->fd, "No such extension '%s' in context '%s'\n", mye, myc);
  705. free(s);
  706. unref_pvt(pvt);
  707. return CLI_SUCCESS;
  708. }
  709. static char *cli_console_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  710. {
  711. struct console_pvt *pvt;
  712. if (cmd == CLI_INIT) {
  713. e->command = "console hangup";
  714. e->usage =
  715. "Usage: console hangup\n"
  716. " Hangs up any call currently placed on the console.\n";
  717. return NULL;
  718. } else if (cmd == CLI_GENERATE) {
  719. return NULL;
  720. }
  721. if (a->argc != e->args) {
  722. return CLI_SHOWUSAGE;
  723. }
  724. pvt = get_active_pvt();
  725. if (!pvt) {
  726. ast_cli(a->fd, "No console device is set as active\n");
  727. return CLI_FAILURE;
  728. }
  729. if (!pvt->owner && !pvt->hookstate) {
  730. ast_cli(a->fd, "No call to hang up\n");
  731. unref_pvt(pvt);
  732. return CLI_FAILURE;
  733. }
  734. pvt->hookstate = 0;
  735. if (pvt->owner)
  736. ast_queue_hangup(pvt->owner);
  737. unref_pvt(pvt);
  738. return CLI_SUCCESS;
  739. }
  740. static char *cli_console_mute(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  741. {
  742. const char *s;
  743. struct console_pvt *pvt;
  744. char *res = CLI_SUCCESS;
  745. if (cmd == CLI_INIT) {
  746. e->command = "console {mute|unmute}";
  747. e->usage =
  748. "Usage: console {mute|unmute}\n"
  749. " Mute/unmute the microphone.\n";
  750. return NULL;
  751. } else if (cmd == CLI_GENERATE) {
  752. return NULL;
  753. }
  754. if (a->argc != e->args) {
  755. return CLI_SHOWUSAGE;
  756. }
  757. pvt = get_active_pvt();
  758. if (!pvt) {
  759. ast_cli(a->fd, "No console device is set as active\n");
  760. return CLI_FAILURE;
  761. }
  762. s = a->argv[e->args-1];
  763. if (!strcasecmp(s, "mute"))
  764. pvt->muted = 1;
  765. else if (!strcasecmp(s, "unmute"))
  766. pvt->muted = 0;
  767. else
  768. res = CLI_SHOWUSAGE;
  769. ast_verb(1, V_BEGIN "The Console is now %s" V_END,
  770. pvt->muted ? "Muted" : "Unmuted");
  771. unref_pvt(pvt);
  772. return res;
  773. }
  774. static char *cli_list_available(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  775. {
  776. PaDeviceIndex idx, num, def_input, def_output;
  777. if (cmd == CLI_INIT) {
  778. e->command = "console list available";
  779. e->usage =
  780. "Usage: console list available\n"
  781. " List all available devices.\n";
  782. return NULL;
  783. } else if (cmd == CLI_GENERATE)
  784. return NULL;
  785. if (a->argc != e->args)
  786. return CLI_SHOWUSAGE;
  787. ast_cli(a->fd, "\n"
  788. "=============================================================\n"
  789. "=== Available Devices =======================================\n"
  790. "=============================================================\n"
  791. "===\n");
  792. num = Pa_GetDeviceCount();
  793. if (!num) {
  794. ast_cli(a->fd, "(None)\n");
  795. return CLI_SUCCESS;
  796. }
  797. def_input = Pa_GetDefaultInputDevice();
  798. def_output = Pa_GetDefaultOutputDevice();
  799. for (idx = 0; idx < num; idx++) {
  800. const PaDeviceInfo *dev = Pa_GetDeviceInfo(idx);
  801. if (!dev)
  802. continue;
  803. ast_cli(a->fd, "=== ---------------------------------------------------------\n"
  804. "=== Device Name: %s\n", dev->name);
  805. if (dev->maxInputChannels)
  806. ast_cli(a->fd, "=== ---> %sInput Device\n", (idx == def_input) ? "Default " : "");
  807. if (dev->maxOutputChannels)
  808. ast_cli(a->fd, "=== ---> %sOutput Device\n", (idx == def_output) ? "Default " : "");
  809. ast_cli(a->fd, "=== ---------------------------------------------------------\n===\n");
  810. }
  811. ast_cli(a->fd, "=============================================================\n\n");
  812. return CLI_SUCCESS;
  813. }
  814. static char *cli_list_devices(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  815. {
  816. struct ao2_iterator i;
  817. struct console_pvt *pvt;
  818. if (cmd == CLI_INIT) {
  819. e->command = "console list devices";
  820. e->usage =
  821. "Usage: console list devices\n"
  822. " List all configured devices.\n";
  823. return NULL;
  824. } else if (cmd == CLI_GENERATE)
  825. return NULL;
  826. if (a->argc != e->args)
  827. return CLI_SHOWUSAGE;
  828. ast_cli(a->fd, "\n"
  829. "=============================================================\n"
  830. "=== Configured Devices ======================================\n"
  831. "=============================================================\n"
  832. "===\n");
  833. i = ao2_iterator_init(pvts, 0);
  834. while ((pvt = ao2_iterator_next(&i))) {
  835. console_pvt_lock(pvt);
  836. ast_cli(a->fd, "=== ---------------------------------------------------------\n"
  837. "=== Device Name: %s\n"
  838. "=== ---> Active: %s\n"
  839. "=== ---> Input Device: %s\n"
  840. "=== ---> Output Device: %s\n"
  841. "=== ---> Context: %s\n"
  842. "=== ---> Extension: %s\n"
  843. "=== ---> CallerID Num: %s\n"
  844. "=== ---> CallerID Name: %s\n"
  845. "=== ---> MOH Interpret: %s\n"
  846. "=== ---> Language: %s\n"
  847. "=== ---> Parkinglot: %s\n"
  848. "=== ---> Muted: %s\n"
  849. "=== ---> Auto-Answer: %s\n"
  850. "=== ---> Override Context: %s\n"
  851. "=== ---------------------------------------------------------\n===\n",
  852. pvt->name, (pvt == active_pvt) ? "Yes" : "No",
  853. pvt->input_device, pvt->output_device, pvt->context,
  854. pvt->exten, pvt->cid_num, pvt->cid_name, pvt->mohinterpret,
  855. pvt->language, pvt->parkinglot, pvt->muted ? "Yes" : "No", pvt->autoanswer ? "Yes" : "No",
  856. pvt->overridecontext ? "Yes" : "No");
  857. console_pvt_unlock(pvt);
  858. unref_pvt(pvt);
  859. }
  860. ao2_iterator_destroy(&i);
  861. ast_cli(a->fd, "=============================================================\n\n");
  862. return CLI_SUCCESS;
  863. }
  864. /*!
  865. * \brief answer command from the console
  866. */
  867. static char *cli_console_answer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  868. {
  869. struct console_pvt *pvt;
  870. switch (cmd) {
  871. case CLI_INIT:
  872. e->command = "console answer";
  873. e->usage =
  874. "Usage: console answer\n"
  875. " Answers an incoming call on the console channel.\n";
  876. return NULL;
  877. case CLI_GENERATE:
  878. return NULL; /* no completion */
  879. }
  880. pvt = get_active_pvt();
  881. if (!pvt) {
  882. ast_cli(a->fd, "No console device is set as active\n");
  883. return CLI_FAILURE;
  884. }
  885. if (a->argc != e->args) {
  886. unref_pvt(pvt);
  887. return CLI_SHOWUSAGE;
  888. }
  889. if (!pvt->owner) {
  890. ast_cli(a->fd, "No one is calling us\n");
  891. unref_pvt(pvt);
  892. return CLI_FAILURE;
  893. }
  894. pvt->hookstate = 1;
  895. ast_indicate(pvt->owner, -1);
  896. ast_queue_control(pvt->owner, AST_CONTROL_ANSWER);
  897. unref_pvt(pvt);
  898. return CLI_SUCCESS;
  899. }
  900. /*!
  901. * \brief Console send text CLI command
  902. *
  903. * \note concatenate all arguments into a single string. argv is NULL-terminated
  904. * so we can use it right away
  905. */
  906. static char *cli_console_sendtext(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  907. {
  908. char buf[TEXT_SIZE];
  909. struct console_pvt *pvt;
  910. struct ast_frame f = {
  911. .frametype = AST_FRAME_TEXT,
  912. .data.ptr = buf,
  913. .src = "console_send_text",
  914. };
  915. int len;
  916. if (cmd == CLI_INIT) {
  917. e->command = "console send text";
  918. e->usage =
  919. "Usage: console send text <message>\n"
  920. " Sends a text message for display on the remote terminal.\n";
  921. return NULL;
  922. } else if (cmd == CLI_GENERATE) {
  923. return NULL;
  924. }
  925. pvt = get_active_pvt();
  926. if (!pvt) {
  927. ast_cli(a->fd, "No console device is set as active\n");
  928. return CLI_FAILURE;
  929. }
  930. if (a->argc < e->args + 1) {
  931. unref_pvt(pvt);
  932. return CLI_SHOWUSAGE;
  933. }
  934. if (!pvt->owner) {
  935. ast_cli(a->fd, "Not in a call\n");
  936. unref_pvt(pvt);
  937. return CLI_FAILURE;
  938. }
  939. ast_join(buf, sizeof(buf) - 1, a->argv + e->args);
  940. if (ast_strlen_zero(buf)) {
  941. unref_pvt(pvt);
  942. return CLI_SHOWUSAGE;
  943. }
  944. len = strlen(buf);
  945. buf[len] = '\n';
  946. f.datalen = len + 1;
  947. ast_queue_frame(pvt->owner, &f);
  948. unref_pvt(pvt);
  949. return CLI_SUCCESS;
  950. }
  951. static void set_active(struct console_pvt *pvt, const char *value)
  952. {
  953. if (pvt == &globals) {
  954. ast_log(LOG_ERROR, "active is only valid as a per-device setting\n");
  955. return;
  956. }
  957. if (!ast_true(value))
  958. return;
  959. ast_rwlock_wrlock(&active_lock);
  960. if (active_pvt)
  961. unref_pvt(active_pvt);
  962. active_pvt = ref_pvt(pvt);
  963. ast_rwlock_unlock(&active_lock);
  964. }
  965. static char *cli_console_active(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  966. {
  967. struct console_pvt *pvt;
  968. switch (cmd) {
  969. case CLI_INIT:
  970. e->command = "console {set|show} active";
  971. e->usage =
  972. "Usage: console {set|show} active [<device>]\n"
  973. " Set or show the active console device for the Asterisk CLI.\n";
  974. return NULL;
  975. case CLI_GENERATE:
  976. if (a->pos == e->args) {
  977. struct ao2_iterator i;
  978. int x = 0;
  979. char *res = NULL;
  980. i = ao2_iterator_init(pvts, 0);
  981. while ((pvt = ao2_iterator_next(&i))) {
  982. if (++x > a->n && !strncasecmp(pvt->name, a->word, strlen(a->word)))
  983. res = ast_strdup(pvt->name);
  984. unref_pvt(pvt);
  985. if (res) {
  986. ao2_iterator_destroy(&i);
  987. return res;
  988. }
  989. }
  990. ao2_iterator_destroy(&i);
  991. }
  992. return NULL;
  993. }
  994. if (a->argc < e->args)
  995. return CLI_SHOWUSAGE;
  996. if (a->argc == 3) {
  997. pvt = get_active_pvt();
  998. if (!pvt)
  999. ast_cli(a->fd, "No device is currently set as the active console device.\n");
  1000. else {
  1001. console_pvt_lock(pvt);
  1002. ast_cli(a->fd, "The active console device is '%s'.\n", pvt->name);
  1003. console_pvt_unlock(pvt);
  1004. pvt = unref_pvt(pvt);
  1005. }
  1006. return CLI_SUCCESS;
  1007. }
  1008. if (!(pvt = find_pvt(a->argv[e->args - 1]))) {
  1009. ast_cli(a->fd, "Could not find a device called '%s'.\n", a->argv[e->args]);
  1010. return CLI_FAILURE;
  1011. }
  1012. set_active(pvt, "yes");
  1013. console_pvt_lock(pvt);
  1014. ast_cli(a->fd, "The active console device has been set to '%s'\n", pvt->name);
  1015. console_pvt_unlock(pvt);
  1016. unref_pvt(pvt);
  1017. return CLI_SUCCESS;
  1018. }
  1019. static struct ast_cli_entry cli_console[] = {
  1020. AST_CLI_DEFINE(cli_console_dial, "Dial an extension from the console"),
  1021. AST_CLI_DEFINE(cli_console_hangup, "Hangup a call on the console"),
  1022. AST_CLI_DEFINE(cli_console_mute, "Disable/Enable mic input"),
  1023. AST_CLI_DEFINE(cli_console_answer, "Answer an incoming console call"),
  1024. AST_CLI_DEFINE(cli_console_sendtext, "Send text to a connected party"),
  1025. AST_CLI_DEFINE(cli_console_flash, "Send a flash to the connected party"),
  1026. AST_CLI_DEFINE(cli_console_autoanswer, "Turn autoanswer on or off"),
  1027. AST_CLI_DEFINE(cli_list_available, "List available devices"),
  1028. AST_CLI_DEFINE(cli_list_devices, "List configured devices"),
  1029. AST_CLI_DEFINE(cli_console_active, "View or Set the active console device"),
  1030. };
  1031. /*!
  1032. * \brief Set default values for a pvt struct
  1033. *
  1034. * \note This function expects the pvt lock to be held.
  1035. */
  1036. static void set_pvt_defaults(struct console_pvt *pvt)
  1037. {
  1038. if (pvt == &globals) {
  1039. ast_string_field_set(pvt, mohinterpret, "default");
  1040. ast_string_field_set(pvt, context, "default");
  1041. ast_string_field_set(pvt, exten, "s");
  1042. ast_string_field_set(pvt, language, "");
  1043. ast_string_field_set(pvt, cid_num, "");
  1044. ast_string_field_set(pvt, cid_name, "");
  1045. ast_string_field_set(pvt, parkinglot, "");
  1046. pvt->overridecontext = 0;
  1047. pvt->autoanswer = 0;
  1048. } else {
  1049. ast_mutex_lock(&globals_lock);
  1050. ast_string_field_set(pvt, mohinterpret, globals.mohinterpret);
  1051. ast_string_field_set(pvt, context, globals.context);
  1052. ast_string_field_set(pvt, exten, globals.exten);
  1053. ast_string_field_set(pvt, language, globals.language);
  1054. ast_string_field_set(pvt, cid_num, globals.cid_num);
  1055. ast_string_field_set(pvt, cid_name, globals.cid_name);
  1056. ast_string_field_set(pvt, parkinglot, globals.parkinglot);
  1057. pvt->overridecontext = globals.overridecontext;
  1058. pvt->autoanswer = globals.autoanswer;
  1059. ast_mutex_unlock(&globals_lock);
  1060. }
  1061. }
  1062. static void store_callerid(struct console_pvt *pvt, const char *value)
  1063. {
  1064. char cid_name[256];
  1065. char cid_num[256];
  1066. ast_callerid_split(value, cid_name, sizeof(cid_name),
  1067. cid_num, sizeof(cid_num));
  1068. ast_string_field_set(pvt, cid_name, cid_name);
  1069. ast_string_field_set(pvt, cid_num, cid_num);
  1070. }
  1071. /*!
  1072. * \brief Store a configuration parameter in a pvt struct
  1073. *
  1074. * \note This function expects the pvt lock to be held.
  1075. */
  1076. static void store_config_core(struct console_pvt *pvt, const char *var, const char *value)
  1077. {
  1078. if (pvt == &globals && !ast_jb_read_conf(&global_jbconf, var, value))
  1079. return;
  1080. CV_START(var, value);
  1081. CV_STRFIELD("context", pvt, context);
  1082. CV_STRFIELD("extension", pvt, exten);
  1083. CV_STRFIELD("mohinterpret", pvt, mohinterpret);
  1084. CV_STRFIELD("language", pvt, language);
  1085. CV_F("callerid", store_callerid(pvt, value));
  1086. CV_BOOL("overridecontext", pvt->overridecontext);
  1087. CV_BOOL("autoanswer", pvt->autoanswer);
  1088. CV_STRFIELD("parkinglot", pvt, parkinglot);
  1089. if (pvt != &globals) {
  1090. CV_F("active", set_active(pvt, value))
  1091. CV_STRFIELD("input_device", pvt, input_device);
  1092. CV_STRFIELD("output_device", pvt, output_device);
  1093. }
  1094. ast_log(LOG_WARNING, "Unknown option '%s'\n", var);
  1095. CV_END;
  1096. }
  1097. static void pvt_destructor(void *obj)
  1098. {
  1099. struct console_pvt *pvt = obj;
  1100. ast_string_field_free_memory(pvt);
  1101. }
  1102. static int init_pvt(struct console_pvt *pvt, const char *name)
  1103. {
  1104. pvt->thread = AST_PTHREADT_NULL;
  1105. if (ast_string_field_init(pvt, 32))
  1106. return -1;
  1107. ast_string_field_set(pvt, name, S_OR(name, ""));
  1108. return 0;
  1109. }
  1110. static void build_device(struct ast_config *cfg, const char *name)
  1111. {
  1112. struct ast_variable *v;
  1113. struct console_pvt *pvt;
  1114. int new = 0;
  1115. if ((pvt = find_pvt(name))) {
  1116. console_pvt_lock(pvt);
  1117. set_pvt_defaults(pvt);
  1118. pvt->destroy = 0;
  1119. } else {
  1120. if (!(pvt = ao2_alloc(sizeof(*pvt), pvt_destructor)))
  1121. return;
  1122. init_pvt(pvt, name);
  1123. set_pvt_defaults(pvt);
  1124. new = 1;
  1125. }
  1126. for (v = ast_variable_browse(cfg, name); v; v = v->next)
  1127. store_config_core(pvt, v->name, v->value);
  1128. if (new)
  1129. ao2_link(pvts, pvt);
  1130. else
  1131. console_pvt_unlock(pvt);
  1132. unref_pvt(pvt);
  1133. }
  1134. static int pvt_mark_destroy_cb(void *obj, void *arg, int flags)
  1135. {
  1136. struct console_pvt *pvt = obj;
  1137. pvt->destroy = 1;
  1138. return 0;
  1139. }
  1140. static void destroy_pvts(void)
  1141. {
  1142. struct ao2_iterator i;
  1143. struct console_pvt *pvt;
  1144. i = ao2_iterator_init(pvts, 0);
  1145. while ((pvt = ao2_iterator_next(&i))) {
  1146. if (pvt->destroy) {
  1147. ao2_unlink(pvts, pvt);
  1148. ast_rwlock_wrlock(&active_lock);
  1149. if (active_pvt == pvt)
  1150. active_pvt = unref_pvt(pvt);
  1151. ast_rwlock_unlock(&active_lock);
  1152. }
  1153. unref_pvt(pvt);
  1154. }
  1155. ao2_iterator_destroy(&i);
  1156. }
  1157. /*!
  1158. * \brief Load the configuration
  1159. * \param reload if this was called due to a reload
  1160. * \retval 0 success
  1161. * \retval -1 failure
  1162. */
  1163. static int load_config(int reload)
  1164. {
  1165. struct ast_config *cfg;
  1166. struct ast_variable *v;
  1167. struct ast_flags config_flags = { 0 };
  1168. char *context = NULL;
  1169. /* default values */
  1170. memcpy(&global_jbconf, &default_jbconf, sizeof(global_jbconf));
  1171. ast_mutex_lock(&globals_lock);
  1172. set_pvt_defaults(&globals);
  1173. ast_mutex_unlock(&globals_lock);
  1174. if (!(cfg = ast_config_load(config_file, config_flags))) {
  1175. ast_log(LOG_NOTICE, "Unable to open configuration file %s!\n", config_file);
  1176. return -1;
  1177. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  1178. ast_log(LOG_NOTICE, "Config file %s has an invalid format\n", config_file);
  1179. return -1;
  1180. }
  1181. ao2_callback(pvts, OBJ_NODATA, pvt_mark_destroy_cb, NULL);
  1182. ast_mutex_lock(&globals_lock);
  1183. for (v = ast_variable_browse(cfg, "general"); v; v = v->next)
  1184. store_config_core(&globals, v->name, v->value);
  1185. ast_mutex_unlock(&globals_lock);
  1186. while ((context = ast_category_browse(cfg, context))) {
  1187. if (strcasecmp(context, "general"))
  1188. build_device(cfg, context);
  1189. }
  1190. ast_config_destroy(cfg);
  1191. destroy_pvts();
  1192. return 0;
  1193. }
  1194. static int pvt_hash_cb(const void *obj, const int flags)
  1195. {
  1196. const struct console_pvt *pvt = obj;
  1197. return ast_str_case_hash(pvt->name);
  1198. }
  1199. static int pvt_cmp_cb(void *obj, void *arg, int flags)
  1200. {
  1201. struct console_pvt *pvt = obj, *pvt2 = arg;
  1202. return !strcasecmp(pvt->name, pvt2->name) ? CMP_MATCH | CMP_STOP : 0;
  1203. }
  1204. static void stop_streams(void)
  1205. {
  1206. struct console_pvt *pvt;
  1207. struct ao2_iterator i;
  1208. i = ao2_iterator_init(pvts, 0);
  1209. while ((pvt = ao2_iterator_next(&i))) {
  1210. if (pvt->hookstate)
  1211. stop_stream(pvt);
  1212. unref_pvt(pvt);
  1213. }
  1214. ao2_iterator_destroy(&i);
  1215. }
  1216. static int unload_module(void)
  1217. {
  1218. console_tech.capabilities = ast_format_cap_destroy(console_tech.capabilities);
  1219. ast_channel_unregister(&console_tech);
  1220. ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console));
  1221. stop_streams();
  1222. Pa_Terminate();
  1223. /* Will unref all the pvts so they will get destroyed, too */
  1224. ao2_ref(pvts, -1);
  1225. pvt_destructor(&globals);
  1226. return 0;
  1227. }
  1228. static int load_module(void)
  1229. {
  1230. struct ast_format tmpfmt;
  1231. PaError res;
  1232. if (!(console_tech.capabilities = ast_format_cap_alloc())) {
  1233. return AST_MODULE_LOAD_DECLINE;
  1234. }
  1235. ast_format_cap_add(console_tech.capabilities, ast_format_set(&tmpfmt, AST_FORMAT_SLINEAR16, 0));
  1236. init_pvt(&globals, NULL);
  1237. if (!(pvts = ao2_container_alloc(NUM_PVT_BUCKETS, pvt_hash_cb, pvt_cmp_cb)))
  1238. goto return_error;
  1239. if (load_config(0))
  1240. goto return_error;
  1241. res = Pa_Initialize();
  1242. if (res != paNoError) {
  1243. ast_log(LOG_WARNING, "Failed to initialize audio system - (%d) %s\n",
  1244. res, Pa_GetErrorText(res));
  1245. goto return_error_pa_init;
  1246. }
  1247. if (ast_channel_register(&console_tech)) {
  1248. ast_log(LOG_ERROR, "Unable to register channel type 'Console'\n");
  1249. goto return_error_chan_reg;
  1250. }
  1251. if (ast_cli_register_multiple(cli_console, ARRAY_LEN(cli_console)))
  1252. goto return_error_cli_reg;
  1253. return AST_MODULE_LOAD_SUCCESS;
  1254. return_error_cli_reg:
  1255. ast_cli_unregister_multiple(cli_console, ARRAY_LEN(cli_console));
  1256. return_error_chan_reg:
  1257. ast_channel_unregister(&console_tech);
  1258. return_error_pa_init:
  1259. Pa_Terminate();
  1260. return_error:
  1261. if (pvts)
  1262. ao2_ref(pvts, -1);
  1263. pvts = NULL;
  1264. pvt_destructor(&globals);
  1265. return AST_MODULE_LOAD_DECLINE;
  1266. }
  1267. static int reload(void)
  1268. {
  1269. return load_config(1);
  1270. }
  1271. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Console Channel Driver",
  1272. .load = load_module,
  1273. .unload = unload_module,
  1274. .reload = reload,
  1275. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  1276. );