chan_console.c 41 KB

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