chan_console.c 39 KB

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