dial.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2007, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@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. /*! \file
  19. *
  20. * \brief Dialing API
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include <sys/time.h>
  30. #include <signal.h>
  31. #include "asterisk/channel.h"
  32. #include "asterisk/utils.h"
  33. #include "asterisk/lock.h"
  34. #include "asterisk/linkedlists.h"
  35. #include "asterisk/dial.h"
  36. #include "asterisk/pbx.h"
  37. #include "asterisk/musiconhold.h"
  38. #include "asterisk/app.h"
  39. #include "asterisk/causes.h"
  40. #include "asterisk/stasis_channels.h"
  41. /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
  42. struct ast_dial {
  43. int num; /*!< Current number to give to next dialed channel */
  44. int timeout; /*!< Maximum time allowed for dial attempts */
  45. int actual_timeout; /*!< Actual timeout based on all factors (ie: channels) */
  46. enum ast_dial_result state; /*!< Status of dial */
  47. void *options[AST_DIAL_OPTION_MAX]; /*!< Global options */
  48. ast_dial_state_callback state_callback; /*!< Status callback */
  49. void *user_data; /*!< Attached user data */
  50. AST_LIST_HEAD(, ast_dial_channel) channels; /*!< Channels being dialed */
  51. pthread_t thread; /*!< Thread (if running in async) */
  52. ast_callid callid; /*!< callid (if running in async) */
  53. ast_mutex_t lock; /*! Lock to protect the thread information above */
  54. };
  55. /*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
  56. struct ast_dial_channel {
  57. int num; /*!< Unique number for dialed channel */
  58. int timeout; /*!< Maximum time allowed for attempt */
  59. char *tech; /*!< Technology being dialed */
  60. char *device; /*!< Device being dialed */
  61. void *options[AST_DIAL_OPTION_MAX]; /*!< Channel specific options */
  62. int cause; /*!< Cause code in case of failure */
  63. unsigned int is_running_app:1; /*!< Is this running an application? */
  64. char *assignedid1; /*!< UniqueID to assign channel */
  65. char *assignedid2; /*!< UniqueID to assign 2nd channel */
  66. struct ast_channel *owner; /*!< Asterisk channel */
  67. AST_LIST_ENTRY(ast_dial_channel) list; /*!< Linked list information */
  68. };
  69. /*! \brief Typedef for dial option enable */
  70. typedef void *(*ast_dial_option_cb_enable)(void *data);
  71. /*! \brief Typedef for dial option disable */
  72. typedef int (*ast_dial_option_cb_disable)(void *data);
  73. /*! \brief Structure for 'ANSWER_EXEC' option */
  74. struct answer_exec_struct {
  75. char app[AST_MAX_APP]; /*!< Application name */
  76. char *args; /*!< Application arguments */
  77. };
  78. /*! \brief Enable function for 'ANSWER_EXEC' option */
  79. static void *answer_exec_enable(void *data)
  80. {
  81. struct answer_exec_struct *answer_exec = NULL;
  82. char *app = ast_strdupa((char*)data), *args = NULL;
  83. /* Not giving any data to this option is bad, mmmk? */
  84. if (ast_strlen_zero(app))
  85. return NULL;
  86. /* Create new data structure */
  87. if (!(answer_exec = ast_calloc(1, sizeof(*answer_exec))))
  88. return NULL;
  89. /* Parse out application and arguments */
  90. if ((args = strchr(app, ','))) {
  91. *args++ = '\0';
  92. answer_exec->args = ast_strdup(args);
  93. }
  94. /* Copy application name */
  95. ast_copy_string(answer_exec->app, app, sizeof(answer_exec->app));
  96. return answer_exec;
  97. }
  98. /*! \brief Disable function for 'ANSWER_EXEC' option */
  99. static int answer_exec_disable(void *data)
  100. {
  101. struct answer_exec_struct *answer_exec = data;
  102. /* Make sure we have a value */
  103. if (!answer_exec)
  104. return -1;
  105. /* If arguments are present, free them too */
  106. if (answer_exec->args)
  107. ast_free(answer_exec->args);
  108. /* This is simple - just free the structure */
  109. ast_free(answer_exec);
  110. return 0;
  111. }
  112. static void *music_enable(void *data)
  113. {
  114. return ast_strdup(data);
  115. }
  116. static int music_disable(void *data)
  117. {
  118. if (!data)
  119. return -1;
  120. ast_free(data);
  121. return 0;
  122. }
  123. static void *predial_enable(void *data)
  124. {
  125. return ast_strdup(data);
  126. }
  127. static int predial_disable(void *data)
  128. {
  129. if (!data) {
  130. return -1;
  131. }
  132. ast_free(data);
  133. return 0;
  134. }
  135. /*! \brief Application execution function for 'ANSWER_EXEC' option */
  136. static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
  137. {
  138. struct ast_channel *chan = dial_channel->owner;
  139. struct ast_app *ast_app = pbx_findapp(app);
  140. /* If the application was not found, return immediately */
  141. if (!ast_app)
  142. return;
  143. /* All is well... execute the application */
  144. pbx_exec(chan, ast_app, args);
  145. /* If another thread is not taking over hang up the channel */
  146. ast_mutex_lock(&dial->lock);
  147. if (dial->thread != AST_PTHREADT_STOP) {
  148. ast_hangup(chan);
  149. dial_channel->owner = NULL;
  150. }
  151. ast_mutex_unlock(&dial->lock);
  152. return;
  153. }
  154. struct ast_option_types {
  155. enum ast_dial_option option;
  156. ast_dial_option_cb_enable enable;
  157. ast_dial_option_cb_disable disable;
  158. };
  159. /*!
  160. * \brief Map options to respective handlers (enable/disable).
  161. *
  162. * \note This list MUST be perfectly kept in order with enum
  163. * ast_dial_option, or else madness will happen.
  164. */
  165. static const struct ast_option_types option_types[] = {
  166. { AST_DIAL_OPTION_RINGING, NULL, NULL }, /*!< Always indicate ringing to caller */
  167. { AST_DIAL_OPTION_ANSWER_EXEC, answer_exec_enable, answer_exec_disable }, /*!< Execute application upon answer in async mode */
  168. { AST_DIAL_OPTION_MUSIC, music_enable, music_disable }, /*!< Play music to the caller instead of ringing */
  169. { AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL, NULL }, /*!< Disable call forwarding on channels */
  170. { AST_DIAL_OPTION_PREDIAL, predial_enable, predial_disable }, /*!< Execute a subroutine on the outbound channels prior to dialing */
  171. { AST_DIAL_OPTION_DIAL_REPLACES_SELF, NULL, NULL }, /*!< The dial operation is a replacement for the requester */
  172. { AST_DIAL_OPTION_SELF_DESTROY, NULL, NULL}, /*!< Destroy self at end of ast_dial_run */
  173. { AST_DIAL_OPTION_MAX, NULL, NULL }, /*!< Terminator of list */
  174. };
  175. /*! \brief Maximum number of channels we can watch at a time */
  176. #define AST_MAX_WATCHERS 256
  177. /*! \brief Macro for finding the option structure to use on a dialed channel */
  178. #define FIND_RELATIVE_OPTION(dial, dial_channel, ast_dial_option) (dial_channel->options[ast_dial_option] ? dial_channel->options[ast_dial_option] : dial->options[ast_dial_option])
  179. /*! \brief Macro that determines whether a channel is the caller or not */
  180. #define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
  181. /*! \brief New dialing structure
  182. * \note Create a dialing structure
  183. * \return Returns a calloc'd ast_dial structure, NULL on failure
  184. */
  185. struct ast_dial *ast_dial_create(void)
  186. {
  187. struct ast_dial *dial = NULL;
  188. /* Allocate new memory for structure */
  189. if (!(dial = ast_calloc(1, sizeof(*dial))))
  190. return NULL;
  191. /* Initialize list of channels */
  192. AST_LIST_HEAD_INIT(&dial->channels);
  193. /* Initialize thread to NULL */
  194. dial->thread = AST_PTHREADT_NULL;
  195. /* No timeout exists... yet */
  196. dial->timeout = -1;
  197. dial->actual_timeout = -1;
  198. /* Can't forget about the lock */
  199. ast_mutex_init(&dial->lock);
  200. return dial;
  201. }
  202. /*! \brief Append a channel
  203. * \note Appends a channel to a dialing structure
  204. * \return Returns channel reference number on success, -1 on failure
  205. */
  206. int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device, const struct ast_assigned_ids *assignedids)
  207. {
  208. struct ast_dial_channel *channel = NULL;
  209. /* Make sure we have required arguments */
  210. if (!dial || !tech || !device)
  211. return -1;
  212. /* Allocate new memory for dialed channel structure */
  213. if (!(channel = ast_calloc(1, sizeof(*channel))))
  214. return -1;
  215. /* Record technology and device for when we actually dial */
  216. channel->tech = ast_strdup(tech);
  217. channel->device = ast_strdup(device);
  218. /* Store the assigned id */
  219. if (assignedids && !ast_strlen_zero(assignedids->uniqueid)) {
  220. channel->assignedid1 = ast_strdup(assignedids->uniqueid);
  221. if (!ast_strlen_zero(assignedids->uniqueid2)) {
  222. channel->assignedid2 = ast_strdup(assignedids->uniqueid2);
  223. }
  224. }
  225. /* Grab reference number from dial structure */
  226. channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
  227. /* No timeout exists... yet */
  228. channel->timeout = -1;
  229. /* Insert into channels list */
  230. AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
  231. return channel->num;
  232. }
  233. /*! \brief Helper function that requests all channels */
  234. static int begin_dial_prerun(struct ast_dial_channel *channel, struct ast_channel *chan, struct ast_format_cap *cap, const char *predial_string)
  235. {
  236. char numsubst[AST_MAX_EXTENSION];
  237. struct ast_format_cap *cap_all_audio = NULL;
  238. struct ast_format_cap *cap_request;
  239. struct ast_assigned_ids assignedids = {
  240. .uniqueid = channel->assignedid1,
  241. .uniqueid2 = channel->assignedid2,
  242. };
  243. /* Copy device string over */
  244. ast_copy_string(numsubst, channel->device, sizeof(numsubst));
  245. if (cap && ast_format_cap_count(cap)) {
  246. cap_request = cap;
  247. } else if (chan) {
  248. cap_request = ast_channel_nativeformats(chan);
  249. } else {
  250. cap_all_audio = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  251. ast_format_cap_append_by_type(cap_all_audio, AST_MEDIA_TYPE_AUDIO);
  252. cap_request = cap_all_audio;
  253. }
  254. /* If we fail to create our owner channel bail out */
  255. if (!(channel->owner = ast_request(channel->tech, cap_request, &assignedids, chan, numsubst, &channel->cause))) {
  256. ao2_cleanup(cap_all_audio);
  257. return -1;
  258. }
  259. cap_request = NULL;
  260. ao2_cleanup(cap_all_audio);
  261. if (chan) {
  262. ast_channel_lock_both(chan, channel->owner);
  263. } else {
  264. ast_channel_lock(channel->owner);
  265. }
  266. ast_channel_stage_snapshot(channel->owner);
  267. ast_channel_appl_set(channel->owner, "AppDial2");
  268. ast_channel_data_set(channel->owner, "(Outgoing Line)");
  269. memset(ast_channel_whentohangup(channel->owner), 0, sizeof(*ast_channel_whentohangup(channel->owner)));
  270. /* Inherit everything from he who spawned this dial */
  271. if (chan) {
  272. ast_channel_inherit_variables(chan, channel->owner);
  273. ast_channel_datastore_inherit(chan, channel->owner);
  274. /* Copy over callerid information */
  275. ast_party_redirecting_copy(ast_channel_redirecting(channel->owner), ast_channel_redirecting(chan));
  276. ast_channel_dialed(channel->owner)->transit_network_select = ast_channel_dialed(chan)->transit_network_select;
  277. ast_connected_line_copy_from_caller(ast_channel_connected(channel->owner), ast_channel_caller(chan));
  278. ast_channel_language_set(channel->owner, ast_channel_language(chan));
  279. if (channel->options[AST_DIAL_OPTION_DIAL_REPLACES_SELF]) {
  280. ast_channel_req_accountcodes(channel->owner, chan, AST_CHANNEL_REQUESTOR_REPLACEMENT);
  281. } else {
  282. ast_channel_req_accountcodes(channel->owner, chan, AST_CHANNEL_REQUESTOR_BRIDGE_PEER);
  283. }
  284. if (ast_strlen_zero(ast_channel_musicclass(channel->owner)))
  285. ast_channel_musicclass_set(channel->owner, ast_channel_musicclass(chan));
  286. ast_channel_adsicpe_set(channel->owner, ast_channel_adsicpe(chan));
  287. ast_channel_transfercapability_set(channel->owner, ast_channel_transfercapability(chan));
  288. ast_channel_unlock(chan);
  289. }
  290. ast_channel_stage_snapshot_done(channel->owner);
  291. ast_channel_unlock(channel->owner);
  292. if (!ast_strlen_zero(predial_string)) {
  293. const char *predial_callee = ast_app_expand_sub_args(chan, predial_string);
  294. if (!predial_callee) {
  295. ast_log(LOG_ERROR, "Could not expand subroutine arguments in predial request '%s'\n", predial_string);
  296. }
  297. ast_autoservice_start(chan);
  298. ast_pre_call(channel->owner, predial_callee);
  299. ast_autoservice_stop(chan);
  300. ast_free((char *) predial_callee);
  301. }
  302. return 0;
  303. }
  304. int ast_dial_prerun(struct ast_dial *dial, struct ast_channel *chan, struct ast_format_cap *cap)
  305. {
  306. struct ast_dial_channel *channel;
  307. int res = -1;
  308. char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
  309. if (!ast_strlen_zero(predial_string)) {
  310. ast_replace_subargument_delimiter(predial_string);
  311. }
  312. AST_LIST_LOCK(&dial->channels);
  313. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  314. if ((res = begin_dial_prerun(channel, chan, cap, predial_string))) {
  315. break;
  316. }
  317. }
  318. AST_LIST_UNLOCK(&dial->channels);
  319. return res;
  320. }
  321. /*! \brief Helper function that does the beginning dialing per-appended channel */
  322. static int begin_dial_channel(struct ast_dial_channel *channel, struct ast_channel *chan, int async, const char *predial_string)
  323. {
  324. char numsubst[AST_MAX_EXTENSION];
  325. int res = 1;
  326. /* If no owner channel exists yet execute pre-run */
  327. if (!channel->owner && begin_dial_prerun(channel, chan, NULL, predial_string)) {
  328. return 0;
  329. }
  330. /* Copy device string over */
  331. ast_copy_string(numsubst, channel->device, sizeof(numsubst));
  332. /* Attempt to actually call this device */
  333. if ((res = ast_call(channel->owner, numsubst, 0))) {
  334. res = 0;
  335. ast_hangup(channel->owner);
  336. channel->owner = NULL;
  337. } else {
  338. if (chan) {
  339. ast_poll_channel_add(chan, channel->owner);
  340. }
  341. ast_channel_publish_dial(async ? NULL : chan, channel->owner, channel->device, NULL);
  342. res = 1;
  343. ast_verb(3, "Called %s\n", numsubst);
  344. }
  345. return res;
  346. }
  347. /*! \brief Helper function that does the beginning dialing per dial structure */
  348. static int begin_dial(struct ast_dial *dial, struct ast_channel *chan, int async)
  349. {
  350. struct ast_dial_channel *channel = NULL;
  351. int success = 0;
  352. char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
  353. if (!ast_strlen_zero(predial_string)) {
  354. ast_replace_subargument_delimiter(predial_string);
  355. }
  356. /* Iterate through channel list, requesting and calling each one */
  357. AST_LIST_LOCK(&dial->channels);
  358. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  359. success += begin_dial_channel(channel, chan, async, predial_string);
  360. }
  361. AST_LIST_UNLOCK(&dial->channels);
  362. /* If number of failures matches the number of channels, then this truly failed */
  363. return success;
  364. }
  365. /*! \brief Helper function to handle channels that have been call forwarded */
  366. static int handle_call_forward(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_channel *chan)
  367. {
  368. struct ast_channel *original = channel->owner;
  369. char *tmp = ast_strdupa(ast_channel_call_forward(channel->owner));
  370. char *tech = "Local", *device = tmp, *stuff;
  371. char *predial_string = dial->options[AST_DIAL_OPTION_PREDIAL];
  372. if (!ast_strlen_zero(predial_string)) {
  373. ast_replace_subargument_delimiter(predial_string);
  374. }
  375. /* If call forwarding is disabled just drop the original channel and don't attempt to dial the new one */
  376. if (FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING)) {
  377. ast_hangup(original);
  378. channel->owner = NULL;
  379. return 0;
  380. }
  381. /* Figure out the new destination */
  382. if ((stuff = strchr(tmp, '/'))) {
  383. *stuff++ = '\0';
  384. tech = tmp;
  385. device = stuff;
  386. } else {
  387. const char *forward_context;
  388. char destination[AST_MAX_CONTEXT + AST_MAX_EXTENSION + 1];
  389. ast_channel_lock(original);
  390. forward_context = pbx_builtin_getvar_helper(original, "FORWARD_CONTEXT");
  391. snprintf(destination, sizeof(destination), "%s@%s", tmp, S_OR(forward_context, ast_channel_context(original)));
  392. ast_channel_unlock(original);
  393. device = ast_strdupa(destination);
  394. }
  395. /* Drop old destination information */
  396. ast_free(channel->tech);
  397. ast_free(channel->device);
  398. ast_free(channel->assignedid1);
  399. channel->assignedid1 = NULL;
  400. ast_free(channel->assignedid2);
  401. channel->assignedid2 = NULL;
  402. /* Update the dial channel with the new destination information */
  403. channel->tech = ast_strdup(tech);
  404. channel->device = ast_strdup(device);
  405. AST_LIST_UNLOCK(&dial->channels);
  406. /* Drop the original channel */
  407. channel->owner = NULL;
  408. /* Finally give it a go... send it out into the world */
  409. begin_dial_channel(channel, chan, chan ? 0 : 1, predial_string);
  410. ast_channel_publish_dial_forward(chan, original, channel->owner, NULL, "CANCEL",
  411. ast_channel_call_forward(original));
  412. ast_hangup(original);
  413. return 0;
  414. }
  415. /*! \brief Helper function that finds the dialed channel based on owner */
  416. static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
  417. {
  418. struct ast_dial_channel *channel = NULL;
  419. AST_LIST_LOCK(&dial->channels);
  420. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  421. if (channel->owner == owner)
  422. break;
  423. }
  424. AST_LIST_UNLOCK(&dial->channels);
  425. return channel;
  426. }
  427. static void set_state(struct ast_dial *dial, enum ast_dial_result state)
  428. {
  429. dial->state = state;
  430. if (dial->state_callback)
  431. dial->state_callback(dial);
  432. }
  433. /*! \brief Helper function that handles control frames WITH owner */
  434. static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
  435. {
  436. if (fr->frametype == AST_FRAME_CONTROL) {
  437. switch (fr->subclass.integer) {
  438. case AST_CONTROL_ANSWER:
  439. ast_verb(3, "%s answered %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
  440. AST_LIST_LOCK(&dial->channels);
  441. AST_LIST_REMOVE(&dial->channels, channel, list);
  442. AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
  443. AST_LIST_UNLOCK(&dial->channels);
  444. ast_channel_publish_dial(chan, channel->owner, channel->device, "ANSWER");
  445. set_state(dial, AST_DIAL_RESULT_ANSWERED);
  446. break;
  447. case AST_CONTROL_BUSY:
  448. ast_verb(3, "%s is busy\n", ast_channel_name(channel->owner));
  449. ast_channel_publish_dial(chan, channel->owner, channel->device, "BUSY");
  450. ast_hangup(channel->owner);
  451. channel->cause = AST_CAUSE_USER_BUSY;
  452. channel->owner = NULL;
  453. break;
  454. case AST_CONTROL_CONGESTION:
  455. ast_verb(3, "%s is circuit-busy\n", ast_channel_name(channel->owner));
  456. ast_channel_publish_dial(chan, channel->owner, channel->device, "CONGESTION");
  457. ast_hangup(channel->owner);
  458. channel->cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
  459. channel->owner = NULL;
  460. break;
  461. case AST_CONTROL_INCOMPLETE:
  462. ast_verb(3, "%s dialed Incomplete extension %s\n", ast_channel_name(channel->owner), ast_channel_exten(channel->owner));
  463. ast_indicate(chan, AST_CONTROL_INCOMPLETE);
  464. break;
  465. case AST_CONTROL_RINGING:
  466. ast_verb(3, "%s is ringing\n", ast_channel_name(channel->owner));
  467. if (!dial->options[AST_DIAL_OPTION_MUSIC])
  468. ast_indicate(chan, AST_CONTROL_RINGING);
  469. set_state(dial, AST_DIAL_RESULT_RINGING);
  470. break;
  471. case AST_CONTROL_PROGRESS:
  472. ast_verb(3, "%s is making progress, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
  473. ast_indicate(chan, AST_CONTROL_PROGRESS);
  474. set_state(dial, AST_DIAL_RESULT_PROGRESS);
  475. break;
  476. case AST_CONTROL_VIDUPDATE:
  477. ast_verb(3, "%s requested a video update, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
  478. ast_indicate(chan, AST_CONTROL_VIDUPDATE);
  479. break;
  480. case AST_CONTROL_SRCUPDATE:
  481. ast_verb(3, "%s requested a source update, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
  482. ast_indicate(chan, AST_CONTROL_SRCUPDATE);
  483. break;
  484. case AST_CONTROL_CONNECTED_LINE:
  485. ast_verb(3, "%s connected line has changed, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
  486. if (ast_channel_connected_line_sub(channel->owner, chan, fr, 1) &&
  487. ast_channel_connected_line_macro(channel->owner, chan, fr, 1, 1)) {
  488. ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, fr->data.ptr, fr->datalen);
  489. }
  490. break;
  491. case AST_CONTROL_REDIRECTING:
  492. ast_verb(3, "%s redirecting info has changed, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
  493. if (ast_channel_redirecting_sub(channel->owner, chan, fr, 1) &&
  494. ast_channel_redirecting_macro(channel->owner, chan, fr, 1, 1)) {
  495. ast_indicate_data(chan, AST_CONTROL_REDIRECTING, fr->data.ptr, fr->datalen);
  496. }
  497. break;
  498. case AST_CONTROL_PROCEEDING:
  499. ast_verb(3, "%s is proceeding, passing it to %s\n", ast_channel_name(channel->owner), ast_channel_name(chan));
  500. ast_indicate(chan, AST_CONTROL_PROCEEDING);
  501. set_state(dial, AST_DIAL_RESULT_PROCEEDING);
  502. break;
  503. case AST_CONTROL_HOLD:
  504. ast_verb(3, "Call on %s placed on hold\n", ast_channel_name(chan));
  505. ast_indicate_data(chan, AST_CONTROL_HOLD, fr->data.ptr, fr->datalen);
  506. break;
  507. case AST_CONTROL_UNHOLD:
  508. ast_verb(3, "Call on %s left from hold\n", ast_channel_name(chan));
  509. ast_indicate(chan, AST_CONTROL_UNHOLD);
  510. break;
  511. case AST_CONTROL_OFFHOOK:
  512. case AST_CONTROL_FLASH:
  513. break;
  514. case AST_CONTROL_PVT_CAUSE_CODE:
  515. ast_indicate_data(chan, AST_CONTROL_PVT_CAUSE_CODE, fr->data.ptr, fr->datalen);
  516. break;
  517. case -1:
  518. /* Prod the channel */
  519. ast_indicate(chan, -1);
  520. break;
  521. default:
  522. break;
  523. }
  524. }
  525. }
  526. /*! \brief Helper function that handles control frames WITHOUT owner */
  527. static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
  528. {
  529. /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
  530. if (fr->frametype != AST_FRAME_CONTROL)
  531. return;
  532. switch (fr->subclass.integer) {
  533. case AST_CONTROL_ANSWER:
  534. ast_verb(3, "%s answered\n", ast_channel_name(channel->owner));
  535. AST_LIST_LOCK(&dial->channels);
  536. AST_LIST_REMOVE(&dial->channels, channel, list);
  537. AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
  538. AST_LIST_UNLOCK(&dial->channels);
  539. ast_channel_publish_dial(NULL, channel->owner, channel->device, "ANSWER");
  540. set_state(dial, AST_DIAL_RESULT_ANSWERED);
  541. break;
  542. case AST_CONTROL_BUSY:
  543. ast_verb(3, "%s is busy\n", ast_channel_name(channel->owner));
  544. ast_channel_publish_dial(NULL, channel->owner, channel->device, "BUSY");
  545. ast_hangup(channel->owner);
  546. channel->cause = AST_CAUSE_USER_BUSY;
  547. channel->owner = NULL;
  548. break;
  549. case AST_CONTROL_CONGESTION:
  550. ast_verb(3, "%s is circuit-busy\n", ast_channel_name(channel->owner));
  551. ast_channel_publish_dial(NULL, channel->owner, channel->device, "CONGESTION");
  552. ast_hangup(channel->owner);
  553. channel->cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
  554. channel->owner = NULL;
  555. break;
  556. case AST_CONTROL_INCOMPLETE:
  557. /*
  558. * Nothing to do but abort the call since we have no
  559. * controlling channel to ask for more digits.
  560. */
  561. ast_verb(3, "%s dialed Incomplete extension %s\n",
  562. ast_channel_name(channel->owner), ast_channel_exten(channel->owner));
  563. ast_hangup(channel->owner);
  564. channel->cause = AST_CAUSE_UNALLOCATED;
  565. channel->owner = NULL;
  566. break;
  567. case AST_CONTROL_RINGING:
  568. ast_verb(3, "%s is ringing\n", ast_channel_name(channel->owner));
  569. set_state(dial, AST_DIAL_RESULT_RINGING);
  570. break;
  571. case AST_CONTROL_PROGRESS:
  572. ast_verb(3, "%s is making progress\n", ast_channel_name(channel->owner));
  573. set_state(dial, AST_DIAL_RESULT_PROGRESS);
  574. break;
  575. case AST_CONTROL_PROCEEDING:
  576. ast_verb(3, "%s is proceeding\n", ast_channel_name(channel->owner));
  577. set_state(dial, AST_DIAL_RESULT_PROCEEDING);
  578. break;
  579. default:
  580. break;
  581. }
  582. }
  583. /*! \brief Helper function to handle when a timeout occurs on dialing attempt */
  584. static int handle_timeout_trip(struct ast_dial *dial, struct timeval start)
  585. {
  586. struct ast_dial_channel *channel = NULL;
  587. int diff = ast_tvdiff_ms(ast_tvnow(), start), lowest_timeout = -1, new_timeout = -1;
  588. /* If there is no difference yet return the dial timeout so we can go again, we were likely interrupted */
  589. if (!diff) {
  590. return dial->timeout;
  591. }
  592. /* If the global dial timeout tripped switch the state to timeout so our channel loop will drop every channel */
  593. if (diff >= dial->timeout) {
  594. set_state(dial, AST_DIAL_RESULT_TIMEOUT);
  595. new_timeout = 0;
  596. }
  597. /* Go through dropping out channels that have met their timeout */
  598. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  599. if (dial->state == AST_DIAL_RESULT_TIMEOUT || diff >= channel->timeout) {
  600. ast_hangup(channel->owner);
  601. channel->cause = AST_CAUSE_NO_ANSWER;
  602. channel->owner = NULL;
  603. } else if ((lowest_timeout == -1) || (lowest_timeout > channel->timeout)) {
  604. lowest_timeout = channel->timeout;
  605. }
  606. }
  607. /* Calculate the new timeout using the lowest timeout found */
  608. if (lowest_timeout >= 0)
  609. new_timeout = lowest_timeout - diff;
  610. return new_timeout;
  611. }
  612. const char *ast_hangup_cause_to_dial_status(int hangup_cause)
  613. {
  614. switch(hangup_cause) {
  615. case AST_CAUSE_BUSY:
  616. return "BUSY";
  617. case AST_CAUSE_CONGESTION:
  618. return "CONGESTION";
  619. case AST_CAUSE_NO_ROUTE_DESTINATION:
  620. case AST_CAUSE_UNREGISTERED:
  621. return "CHANUNAVAIL";
  622. case AST_CAUSE_NO_ANSWER:
  623. default:
  624. return "NOANSWER";
  625. }
  626. }
  627. /*! \brief Helper function that basically keeps tabs on dialing attempts */
  628. static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
  629. {
  630. int timeout = -1;
  631. struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
  632. struct ast_dial_channel *channel = NULL;
  633. struct answer_exec_struct *answer_exec = NULL;
  634. struct timeval start;
  635. set_state(dial, AST_DIAL_RESULT_TRYING);
  636. /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
  637. if (dial->options[AST_DIAL_OPTION_RINGING]) {
  638. set_state(dial, AST_DIAL_RESULT_RINGING);
  639. if (chan)
  640. ast_indicate(chan, AST_CONTROL_RINGING);
  641. } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
  642. !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
  643. char *original_moh = ast_strdupa(ast_channel_musicclass(chan));
  644. ast_indicate(chan, -1);
  645. ast_channel_musicclass_set(chan, dial->options[AST_DIAL_OPTION_MUSIC]);
  646. ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
  647. ast_channel_musicclass_set(chan, original_moh);
  648. }
  649. /* Record start time for timeout purposes */
  650. start = ast_tvnow();
  651. /* We actually figured out the maximum timeout we can do as they were added, so we can directly access the info */
  652. timeout = dial->actual_timeout;
  653. /* Go into an infinite loop while we are trying */
  654. while ((dial->state != AST_DIAL_RESULT_UNANSWERED) && (dial->state != AST_DIAL_RESULT_ANSWERED) && (dial->state != AST_DIAL_RESULT_HANGUP) && (dial->state != AST_DIAL_RESULT_TIMEOUT)) {
  655. int pos = 0, count = 0;
  656. struct ast_frame *fr = NULL;
  657. /* Set up channel structure array */
  658. pos = count = 0;
  659. if (chan)
  660. cs[pos++] = chan;
  661. /* Add channels we are attempting to dial */
  662. AST_LIST_LOCK(&dial->channels);
  663. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  664. if (channel->owner) {
  665. cs[pos++] = channel->owner;
  666. count++;
  667. }
  668. }
  669. AST_LIST_UNLOCK(&dial->channels);
  670. /* If we have no outbound channels in progress, switch state to unanswered and stop */
  671. if (!count) {
  672. set_state(dial, AST_DIAL_RESULT_UNANSWERED);
  673. break;
  674. }
  675. /* Just to be safe... */
  676. if (dial->thread == AST_PTHREADT_STOP)
  677. break;
  678. /* Wait for frames from channels */
  679. who = ast_waitfor_n(cs, pos, &timeout);
  680. /* Check to see if our thread is being canceled */
  681. if (dial->thread == AST_PTHREADT_STOP)
  682. break;
  683. /* If the timeout no longer exists OR if we got no channel it basically means the timeout was tripped, so handle it */
  684. if (!timeout || !who) {
  685. timeout = handle_timeout_trip(dial, start);
  686. continue;
  687. }
  688. /* Find relative dial channel */
  689. if (!chan || !IS_CALLER(chan, who))
  690. channel = find_relative_dial_channel(dial, who);
  691. /* See if this channel has been forwarded elsewhere */
  692. if (!ast_strlen_zero(ast_channel_call_forward(who))) {
  693. handle_call_forward(dial, channel, chan);
  694. continue;
  695. }
  696. /* Attempt to read in a frame */
  697. if (!(fr = ast_read(who))) {
  698. /* If this is the caller then we switch state to hangup and stop */
  699. if (chan && IS_CALLER(chan, who)) {
  700. set_state(dial, AST_DIAL_RESULT_HANGUP);
  701. break;
  702. }
  703. if (chan)
  704. ast_poll_channel_del(chan, channel->owner);
  705. ast_channel_publish_dial(chan, who, channel->device, ast_hangup_cause_to_dial_status(ast_channel_hangupcause(who)));
  706. ast_hangup(who);
  707. channel->owner = NULL;
  708. continue;
  709. }
  710. /* Process the frame */
  711. if (chan)
  712. handle_frame(dial, channel, fr, chan);
  713. else
  714. handle_frame_ownerless(dial, channel, fr);
  715. /* Free the received frame and start all over */
  716. ast_frfree(fr);
  717. }
  718. /* Do post-processing from loop */
  719. if (dial->state == AST_DIAL_RESULT_ANSWERED) {
  720. /* Hangup everything except that which answered */
  721. AST_LIST_LOCK(&dial->channels);
  722. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  723. if (!channel->owner || channel->owner == who)
  724. continue;
  725. if (chan)
  726. ast_poll_channel_del(chan, channel->owner);
  727. ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
  728. ast_hangup(channel->owner);
  729. channel->cause = AST_CAUSE_ANSWERED_ELSEWHERE;
  730. channel->owner = NULL;
  731. }
  732. AST_LIST_UNLOCK(&dial->channels);
  733. /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
  734. if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
  735. channel->is_running_app = 1;
  736. answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
  737. channel->is_running_app = 0;
  738. }
  739. if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
  740. !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
  741. ast_moh_stop(chan);
  742. }
  743. } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
  744. /* Hangup everything */
  745. AST_LIST_LOCK(&dial->channels);
  746. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  747. if (!channel->owner)
  748. continue;
  749. if (chan)
  750. ast_poll_channel_del(chan, channel->owner);
  751. ast_channel_publish_dial(chan, channel->owner, channel->device, "CANCEL");
  752. ast_hangup(channel->owner);
  753. channel->cause = AST_CAUSE_NORMAL_CLEARING;
  754. channel->owner = NULL;
  755. }
  756. AST_LIST_UNLOCK(&dial->channels);
  757. }
  758. if (dial->options[AST_DIAL_OPTION_SELF_DESTROY]) {
  759. enum ast_dial_result state = dial->state;
  760. ast_dial_destroy(dial);
  761. return state;
  762. }
  763. return dial->state;
  764. }
  765. /*! \brief Dial async thread function */
  766. static void *async_dial(void *data)
  767. {
  768. struct ast_dial *dial = data;
  769. if (dial->callid) {
  770. ast_callid_threadassoc_add(dial->callid);
  771. }
  772. /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
  773. monitor_dial(dial, NULL);
  774. return NULL;
  775. }
  776. /*! \brief Execute dialing synchronously or asynchronously
  777. * \note Dials channels in a dial structure.
  778. * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
  779. */
  780. enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
  781. {
  782. enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
  783. /* Ensure required arguments are passed */
  784. if (!dial) {
  785. ast_debug(1, "invalid #1\n");
  786. return AST_DIAL_RESULT_INVALID;
  787. }
  788. /* If there are no channels to dial we can't very well try to dial them */
  789. if (AST_LIST_EMPTY(&dial->channels)) {
  790. ast_debug(1, "invalid #2\n");
  791. return AST_DIAL_RESULT_INVALID;
  792. }
  793. /* Dial each requested channel */
  794. if (!begin_dial(dial, chan, async))
  795. return AST_DIAL_RESULT_FAILED;
  796. /* If we are running async spawn a thread and send it away... otherwise block here */
  797. if (async) {
  798. /* reference be released at dial destruction if it isn't NULL */
  799. dial->callid = ast_read_threadstorage_callid();
  800. dial->state = AST_DIAL_RESULT_TRYING;
  801. /* Try to create a thread */
  802. if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
  803. /* Failed to create the thread - hangup all dialed channels and return failed */
  804. ast_dial_hangup(dial);
  805. res = AST_DIAL_RESULT_FAILED;
  806. }
  807. } else {
  808. res = monitor_dial(dial, chan);
  809. }
  810. return res;
  811. }
  812. /*! \brief Return channel that answered
  813. * \note Returns the Asterisk channel that answered
  814. * \param dial Dialing structure
  815. */
  816. struct ast_channel *ast_dial_answered(struct ast_dial *dial)
  817. {
  818. if (!dial)
  819. return NULL;
  820. return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
  821. }
  822. /*! \brief Steal the channel that answered
  823. * \note Returns the Asterisk channel that answered and removes it from the dialing structure
  824. * \param dial Dialing structure
  825. */
  826. struct ast_channel *ast_dial_answered_steal(struct ast_dial *dial)
  827. {
  828. struct ast_channel *chan = NULL;
  829. if (!dial)
  830. return NULL;
  831. if (dial->state == AST_DIAL_RESULT_ANSWERED) {
  832. chan = AST_LIST_FIRST(&dial->channels)->owner;
  833. AST_LIST_FIRST(&dial->channels)->owner = NULL;
  834. }
  835. return chan;
  836. }
  837. /*! \brief Return state of dial
  838. * \note Returns the state of the dial attempt
  839. * \param dial Dialing structure
  840. */
  841. enum ast_dial_result ast_dial_state(struct ast_dial *dial)
  842. {
  843. return dial->state;
  844. }
  845. /*! \brief Cancel async thread
  846. * \note Cancel a running async thread
  847. * \param dial Dialing structure
  848. */
  849. enum ast_dial_result ast_dial_join(struct ast_dial *dial)
  850. {
  851. pthread_t thread;
  852. /* If the dial structure is not running in async, return failed */
  853. if (dial->thread == AST_PTHREADT_NULL)
  854. return AST_DIAL_RESULT_FAILED;
  855. /* Record thread */
  856. thread = dial->thread;
  857. /* Boom, commence locking */
  858. ast_mutex_lock(&dial->lock);
  859. /* Stop the thread */
  860. dial->thread = AST_PTHREADT_STOP;
  861. /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
  862. AST_LIST_LOCK(&dial->channels);
  863. if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
  864. struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
  865. if (chan) {
  866. ast_channel_lock(chan);
  867. ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
  868. ast_channel_unlock(chan);
  869. }
  870. } else {
  871. /* Now we signal it with SIGURG so it will break out of it's waitfor */
  872. pthread_kill(thread, SIGURG);
  873. }
  874. AST_LIST_UNLOCK(&dial->channels);
  875. /* Yay done with it */
  876. ast_mutex_unlock(&dial->lock);
  877. /* Finally wait for the thread to exit */
  878. pthread_join(thread, NULL);
  879. /* Yay thread is all gone */
  880. dial->thread = AST_PTHREADT_NULL;
  881. return dial->state;
  882. }
  883. /*! \brief Hangup channels
  884. * \note Hangup all active channels
  885. * \param dial Dialing structure
  886. */
  887. void ast_dial_hangup(struct ast_dial *dial)
  888. {
  889. struct ast_dial_channel *channel = NULL;
  890. if (!dial)
  891. return;
  892. AST_LIST_LOCK(&dial->channels);
  893. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  894. ast_hangup(channel->owner);
  895. channel->owner = NULL;
  896. }
  897. AST_LIST_UNLOCK(&dial->channels);
  898. return;
  899. }
  900. /*! \brief Destroys a dialing structure
  901. * \note Destroys (free's) the given ast_dial structure
  902. * \param dial Dialing structure to free
  903. * \return Returns 0 on success, -1 on failure
  904. */
  905. int ast_dial_destroy(struct ast_dial *dial)
  906. {
  907. int i = 0;
  908. struct ast_dial_channel *channel = NULL;
  909. if (!dial)
  910. return -1;
  911. /* Hangup and deallocate all the dialed channels */
  912. AST_LIST_LOCK(&dial->channels);
  913. AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
  914. /* Disable any enabled options */
  915. for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
  916. if (!channel->options[i])
  917. continue;
  918. if (option_types[i].disable)
  919. option_types[i].disable(channel->options[i]);
  920. channel->options[i] = NULL;
  921. }
  922. /* Hang up channel if need be */
  923. ast_hangup(channel->owner);
  924. channel->owner = NULL;
  925. /* Free structure */
  926. ast_free(channel->tech);
  927. ast_free(channel->device);
  928. ast_free(channel->assignedid1);
  929. ast_free(channel->assignedid2);
  930. AST_LIST_REMOVE_CURRENT(list);
  931. ast_free(channel);
  932. }
  933. AST_LIST_TRAVERSE_SAFE_END;
  934. AST_LIST_UNLOCK(&dial->channels);
  935. /* Disable any enabled options globally */
  936. for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
  937. if (!dial->options[i])
  938. continue;
  939. if (option_types[i].disable)
  940. option_types[i].disable(dial->options[i]);
  941. dial->options[i] = NULL;
  942. }
  943. /* Lock be gone! */
  944. ast_mutex_destroy(&dial->lock);
  945. /* Free structure */
  946. ast_free(dial);
  947. return 0;
  948. }
  949. /*! \brief Enables an option globally
  950. * \param dial Dial structure to enable option on
  951. * \param option Option to enable
  952. * \param data Data to pass to this option (not always needed)
  953. * \return Returns 0 on success, -1 on failure
  954. */
  955. int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
  956. {
  957. /* If the option is already enabled, return failure */
  958. if (dial->options[option])
  959. return -1;
  960. /* Execute enable callback if it exists, if not simply make sure the value is set */
  961. if (option_types[option].enable)
  962. dial->options[option] = option_types[option].enable(data);
  963. else
  964. dial->options[option] = (void*)1;
  965. return 0;
  966. }
  967. /*! \brief Helper function for finding a channel in a dial structure based on number
  968. */
  969. static struct ast_dial_channel *find_dial_channel(struct ast_dial *dial, int num)
  970. {
  971. struct ast_dial_channel *channel = AST_LIST_LAST(&dial->channels);
  972. /* We can try to predict programmer behavior, the last channel they added is probably the one they wanted to modify */
  973. if (channel->num == num)
  974. return channel;
  975. /* Hrm not at the end... looking through the list it is! */
  976. AST_LIST_LOCK(&dial->channels);
  977. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  978. if (channel->num == num)
  979. break;
  980. }
  981. AST_LIST_UNLOCK(&dial->channels);
  982. return channel;
  983. }
  984. /*! \brief Enables an option per channel
  985. * \param dial Dial structure
  986. * \param num Channel number to enable option on
  987. * \param option Option to enable
  988. * \param data Data to pass to this option (not always needed)
  989. * \return Returns 0 on success, -1 on failure
  990. */
  991. int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
  992. {
  993. struct ast_dial_channel *channel = NULL;
  994. /* Ensure we have required arguments */
  995. if (!dial || AST_LIST_EMPTY(&dial->channels))
  996. return -1;
  997. if (!(channel = find_dial_channel(dial, num)))
  998. return -1;
  999. /* If the option is already enabled, return failure */
  1000. if (channel->options[option])
  1001. return -1;
  1002. /* Execute enable callback if it exists, if not simply make sure the value is set */
  1003. if (option_types[option].enable)
  1004. channel->options[option] = option_types[option].enable(data);
  1005. else
  1006. channel->options[option] = (void*)1;
  1007. return 0;
  1008. }
  1009. /*! \brief Disables an option globally
  1010. * \param dial Dial structure to disable option on
  1011. * \param option Option to disable
  1012. * \return Returns 0 on success, -1 on failure
  1013. */
  1014. int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
  1015. {
  1016. /* If the option is not enabled, return failure */
  1017. if (!dial->options[option]) {
  1018. return -1;
  1019. }
  1020. /* Execute callback of option to disable if it exists */
  1021. if (option_types[option].disable)
  1022. option_types[option].disable(dial->options[option]);
  1023. /* Finally disable option on the structure */
  1024. dial->options[option] = NULL;
  1025. return 0;
  1026. }
  1027. /*! \brief Disables an option per channel
  1028. * \param dial Dial structure
  1029. * \param num Channel number to disable option on
  1030. * \param option Option to disable
  1031. * \return Returns 0 on success, -1 on failure
  1032. */
  1033. int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
  1034. {
  1035. struct ast_dial_channel *channel = NULL;
  1036. /* Ensure we have required arguments */
  1037. if (!dial || AST_LIST_EMPTY(&dial->channels))
  1038. return -1;
  1039. if (!(channel = find_dial_channel(dial, num)))
  1040. return -1;
  1041. /* If the option is not enabled, return failure */
  1042. if (!channel->options[option])
  1043. return -1;
  1044. /* Execute callback of option to disable it if it exists */
  1045. if (option_types[option].disable)
  1046. option_types[option].disable(channel->options[option]);
  1047. /* Finally disable the option on the structure */
  1048. channel->options[option] = NULL;
  1049. return 0;
  1050. }
  1051. int ast_dial_reason(struct ast_dial *dial, int num)
  1052. {
  1053. struct ast_dial_channel *channel;
  1054. if (!dial || AST_LIST_EMPTY(&dial->channels) || !(channel = find_dial_channel(dial, num))) {
  1055. return -1;
  1056. }
  1057. return channel->cause;
  1058. }
  1059. struct ast_channel *ast_dial_get_channel(struct ast_dial *dial, int num)
  1060. {
  1061. struct ast_dial_channel *channel;
  1062. if (!dial || AST_LIST_EMPTY(&dial->channels) || !(channel = find_dial_channel(dial, num))) {
  1063. return NULL;
  1064. }
  1065. return channel->owner;
  1066. }
  1067. void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
  1068. {
  1069. dial->state_callback = callback;
  1070. }
  1071. void ast_dial_set_user_data(struct ast_dial *dial, void *user_data)
  1072. {
  1073. dial->user_data = user_data;
  1074. }
  1075. void *ast_dial_get_user_data(struct ast_dial *dial)
  1076. {
  1077. return dial->user_data;
  1078. }
  1079. /*! \brief Set the maximum time (globally) allowed for trying to ring phones
  1080. * \param dial The dial structure to apply the time limit to
  1081. * \param timeout Maximum time allowed
  1082. * \return nothing
  1083. */
  1084. void ast_dial_set_global_timeout(struct ast_dial *dial, int timeout)
  1085. {
  1086. dial->timeout = timeout;
  1087. if (dial->timeout > 0 && (dial->actual_timeout > dial->timeout || dial->actual_timeout == -1))
  1088. dial->actual_timeout = dial->timeout;
  1089. return;
  1090. }
  1091. /*! \brief Set the maximum time (per channel) allowed for trying to ring the phone
  1092. * \param dial The dial structure the channel belongs to
  1093. * \param num Channel number to set timeout on
  1094. * \param timeout Maximum time allowed
  1095. * \return nothing
  1096. */
  1097. void ast_dial_set_timeout(struct ast_dial *dial, int num, int timeout)
  1098. {
  1099. struct ast_dial_channel *channel = NULL;
  1100. if (!(channel = find_dial_channel(dial, num)))
  1101. return;
  1102. channel->timeout = timeout;
  1103. if (channel->timeout > 0 && (dial->actual_timeout > channel->timeout || dial->actual_timeout == -1))
  1104. dial->actual_timeout = channel->timeout;
  1105. return;
  1106. }