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