dial.c 41 KB

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