chan_console.c 39 KB

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