dial.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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. /*! \brief Main dialing structure. Contains global options, channels being dialed, and more! */
  40. struct ast_dial {
  41. int num; /*!< Current number to give to next dialed channel */
  42. int timeout; /*!< Maximum time allowed for dial attempts */
  43. int actual_timeout; /*!< Actual timeout based on all factors (ie: channels) */
  44. enum ast_dial_result state; /*!< Status of dial */
  45. void *options[AST_DIAL_OPTION_MAX]; /*!< Global options */
  46. ast_dial_state_callback state_callback; /*!< Status callback */
  47. AST_LIST_HEAD(, ast_dial_channel) channels; /*!< Channels being dialed */
  48. pthread_t thread; /*!< Thread (if running in async) */
  49. ast_mutex_t lock; /*! Lock to protect the thread information above */
  50. };
  51. /*! \brief Dialing channel structure. Contains per-channel dialing options, asterisk channel, and more! */
  52. struct ast_dial_channel {
  53. int num; /*!< Unique number for dialed channel */
  54. int timeout; /*!< Maximum time allowed for attempt */
  55. char *tech; /*!< Technology being dialed */
  56. char *device; /*!< Device being dialed */
  57. void *options[AST_DIAL_OPTION_MAX]; /*!< Channel specific options */
  58. int cause; /*!< Cause code in case of failure */
  59. unsigned int is_running_app:1; /*!< Is this running an application? */
  60. struct ast_channel *owner; /*!< Asterisk channel */
  61. AST_LIST_ENTRY(ast_dial_channel) list; /*!< Linked list information */
  62. };
  63. /*! \brief Typedef for dial option enable */
  64. typedef void *(*ast_dial_option_cb_enable)(void *data);
  65. /*! \brief Typedef for dial option disable */
  66. typedef int (*ast_dial_option_cb_disable)(void *data);
  67. /*! \brief Structure for 'ANSWER_EXEC' option */
  68. struct answer_exec_struct {
  69. char app[AST_MAX_APP]; /*!< Application name */
  70. char *args; /*!< Application arguments */
  71. };
  72. /*! \brief Enable function for 'ANSWER_EXEC' option */
  73. static void *answer_exec_enable(void *data)
  74. {
  75. struct answer_exec_struct *answer_exec = NULL;
  76. char *app = ast_strdupa((char*)data), *args = NULL;
  77. /* Not giving any data to this option is bad, mmmk? */
  78. if (ast_strlen_zero(app))
  79. return NULL;
  80. /* Create new data structure */
  81. if (!(answer_exec = ast_calloc(1, sizeof(*answer_exec))))
  82. return NULL;
  83. /* Parse out application and arguments */
  84. if ((args = strchr(app, ','))) {
  85. *args++ = '\0';
  86. answer_exec->args = ast_strdup(args);
  87. }
  88. /* Copy application name */
  89. ast_copy_string(answer_exec->app, app, sizeof(answer_exec->app));
  90. return answer_exec;
  91. }
  92. /*! \brief Disable function for 'ANSWER_EXEC' option */
  93. static int answer_exec_disable(void *data)
  94. {
  95. struct answer_exec_struct *answer_exec = data;
  96. /* Make sure we have a value */
  97. if (!answer_exec)
  98. return -1;
  99. /* If arguments are present, free them too */
  100. if (answer_exec->args)
  101. ast_free(answer_exec->args);
  102. /* This is simple - just free the structure */
  103. ast_free(answer_exec);
  104. return 0;
  105. }
  106. static void *music_enable(void *data)
  107. {
  108. return ast_strdup(data);
  109. }
  110. static int music_disable(void *data)
  111. {
  112. if (!data)
  113. return -1;
  114. ast_free(data);
  115. return 0;
  116. }
  117. /*! \brief Application execution function for 'ANSWER_EXEC' option */
  118. static void answer_exec_run(struct ast_dial *dial, struct ast_dial_channel *dial_channel, char *app, char *args)
  119. {
  120. struct ast_channel *chan = dial_channel->owner;
  121. struct ast_app *ast_app = pbx_findapp(app);
  122. /* If the application was not found, return immediately */
  123. if (!ast_app)
  124. return;
  125. /* All is well... execute the application */
  126. pbx_exec(chan, ast_app, args);
  127. /* If another thread is not taking over hang up the channel */
  128. ast_mutex_lock(&dial->lock);
  129. if (dial->thread != AST_PTHREADT_STOP) {
  130. ast_hangup(chan);
  131. dial_channel->owner = NULL;
  132. }
  133. ast_mutex_unlock(&dial->lock);
  134. return;
  135. }
  136. /*! \brief Options structure - maps options to respective handlers (enable/disable). This list MUST be perfectly kept in order, or else madness will happen. */
  137. static const struct ast_option_types {
  138. enum ast_dial_option option;
  139. ast_dial_option_cb_enable enable;
  140. ast_dial_option_cb_disable disable;
  141. } option_types[] = {
  142. { AST_DIAL_OPTION_RINGING, NULL, NULL }, /*!< Always indicate ringing to caller */
  143. { AST_DIAL_OPTION_ANSWER_EXEC, answer_exec_enable, answer_exec_disable }, /*!< Execute application upon answer in async mode */
  144. { AST_DIAL_OPTION_MUSIC, music_enable, music_disable }, /*!< Play music to the caller instead of ringing */
  145. { AST_DIAL_OPTION_DISABLE_CALL_FORWARDING, NULL, NULL }, /*!< Disable call forwarding on channels */
  146. { AST_DIAL_OPTION_MAX, NULL, NULL }, /*!< Terminator of list */
  147. };
  148. /*! \brief Maximum number of channels we can watch at a time */
  149. #define AST_MAX_WATCHERS 256
  150. /*! \brief Macro for finding the option structure to use on a dialed channel */
  151. #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])
  152. /*! \brief Macro that determines whether a channel is the caller or not */
  153. #define IS_CALLER(chan, owner) (chan == owner ? 1 : 0)
  154. /*! \brief New dialing structure
  155. * \note Create a dialing structure
  156. * \return Returns a calloc'd ast_dial structure, NULL on failure
  157. */
  158. struct ast_dial *ast_dial_create(void)
  159. {
  160. struct ast_dial *dial = NULL;
  161. /* Allocate new memory for structure */
  162. if (!(dial = ast_calloc(1, sizeof(*dial))))
  163. return NULL;
  164. /* Initialize list of channels */
  165. AST_LIST_HEAD_INIT(&dial->channels);
  166. /* Initialize thread to NULL */
  167. dial->thread = AST_PTHREADT_NULL;
  168. /* No timeout exists... yet */
  169. dial->timeout = -1;
  170. dial->actual_timeout = -1;
  171. /* Can't forget about the lock */
  172. ast_mutex_init(&dial->lock);
  173. return dial;
  174. }
  175. /*! \brief Append a channel
  176. * \note Appends a channel to a dialing structure
  177. * \return Returns channel reference number on success, -1 on failure
  178. */
  179. int ast_dial_append(struct ast_dial *dial, const char *tech, const char *device)
  180. {
  181. struct ast_dial_channel *channel = NULL;
  182. /* Make sure we have required arguments */
  183. if (!dial || !tech || !device)
  184. return -1;
  185. /* Allocate new memory for dialed channel structure */
  186. if (!(channel = ast_calloc(1, sizeof(*channel))))
  187. return -1;
  188. /* Record technology and device for when we actually dial */
  189. channel->tech = ast_strdup(tech);
  190. channel->device = ast_strdup(device);
  191. /* Grab reference number from dial structure */
  192. channel->num = ast_atomic_fetchadd_int(&dial->num, +1);
  193. /* No timeout exists... yet */
  194. channel->timeout = -1;
  195. /* Insert into channels list */
  196. AST_LIST_INSERT_TAIL(&dial->channels, channel, list);
  197. return channel->num;
  198. }
  199. /*! \brief Helper function that does the beginning dialing per-appended channel */
  200. static int begin_dial_channel(struct ast_dial_channel *channel, struct ast_channel *chan)
  201. {
  202. char numsubst[AST_MAX_EXTENSION];
  203. int res = 1;
  204. struct ast_format_cap *cap_all_audio = NULL;
  205. struct ast_format_cap *cap_request;
  206. /* Copy device string over */
  207. ast_copy_string(numsubst, channel->device, sizeof(numsubst));
  208. if (chan) {
  209. cap_request = chan->nativeformats;
  210. } else {
  211. cap_all_audio = ast_format_cap_alloc_nolock();
  212. ast_format_cap_add_all_by_type(cap_all_audio, AST_FORMAT_TYPE_AUDIO);
  213. cap_request = cap_all_audio;
  214. }
  215. /* If we fail to create our owner channel bail out */
  216. if (!(channel->owner = ast_request(channel->tech, cap_request, chan, numsubst, &channel->cause))) {
  217. cap_all_audio = ast_format_cap_destroy(cap_all_audio);
  218. return -1;
  219. }
  220. cap_request = NULL;
  221. cap_all_audio = ast_format_cap_destroy(cap_all_audio);
  222. channel->owner->appl = "AppDial2";
  223. channel->owner->data = "(Outgoing Line)";
  224. memset(&channel->owner->whentohangup, 0, sizeof(channel->owner->whentohangup));
  225. /* Inherit everything from he who spawned this dial */
  226. if (chan) {
  227. ast_channel_inherit_variables(chan, channel->owner);
  228. ast_channel_datastore_inherit(chan, channel->owner);
  229. /* Copy over callerid information */
  230. ast_party_redirecting_copy(&channel->owner->redirecting, &chan->redirecting);
  231. channel->owner->dialed.transit_network_select = chan->dialed.transit_network_select;
  232. ast_connected_line_copy_from_caller(&channel->owner->connected, &chan->caller);
  233. ast_string_field_set(channel->owner, language, chan->language);
  234. ast_string_field_set(channel->owner, accountcode, chan->accountcode);
  235. if (ast_strlen_zero(channel->owner->musicclass))
  236. ast_string_field_set(channel->owner, musicclass, chan->musicclass);
  237. channel->owner->adsicpe = chan->adsicpe;
  238. channel->owner->transfercapability = chan->transfercapability;
  239. }
  240. /* Attempt to actually call this device */
  241. if ((res = ast_call(channel->owner, numsubst, 0))) {
  242. res = 0;
  243. ast_hangup(channel->owner);
  244. channel->owner = NULL;
  245. } else {
  246. if (chan)
  247. ast_poll_channel_add(chan, channel->owner);
  248. res = 1;
  249. ast_verb(3, "Called %s\n", numsubst);
  250. }
  251. return res;
  252. }
  253. /*! \brief Helper function that does the beginning dialing per dial structure */
  254. static int begin_dial(struct ast_dial *dial, struct ast_channel *chan)
  255. {
  256. struct ast_dial_channel *channel = NULL;
  257. int success = 0;
  258. /* Iterate through channel list, requesting and calling each one */
  259. AST_LIST_LOCK(&dial->channels);
  260. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  261. success += begin_dial_channel(channel, chan);
  262. }
  263. AST_LIST_UNLOCK(&dial->channels);
  264. /* If number of failures matches the number of channels, then this truly failed */
  265. return success;
  266. }
  267. /*! \brief Helper function to handle channels that have been call forwarded */
  268. static int handle_call_forward(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_channel *chan)
  269. {
  270. struct ast_channel *original = channel->owner;
  271. char *tmp = ast_strdupa(channel->owner->call_forward);
  272. char *tech = "Local", *device = tmp, *stuff;
  273. /* If call forwarding is disabled just drop the original channel and don't attempt to dial the new one */
  274. if (FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_DISABLE_CALL_FORWARDING)) {
  275. ast_hangup(original);
  276. channel->owner = NULL;
  277. return 0;
  278. }
  279. /* Figure out the new destination */
  280. if ((stuff = strchr(tmp, '/'))) {
  281. *stuff++ = '\0';
  282. tech = tmp;
  283. device = stuff;
  284. }
  285. /* Drop old destination information */
  286. ast_free(channel->tech);
  287. ast_free(channel->device);
  288. /* Update the dial channel with the new destination information */
  289. channel->tech = ast_strdup(tech);
  290. channel->device = ast_strdup(device);
  291. AST_LIST_UNLOCK(&dial->channels);
  292. /* Finally give it a go... send it out into the world */
  293. begin_dial_channel(channel, chan);
  294. /* Drop the original channel */
  295. ast_hangup(original);
  296. return 0;
  297. }
  298. /*! \brief Helper function that finds the dialed channel based on owner */
  299. static struct ast_dial_channel *find_relative_dial_channel(struct ast_dial *dial, struct ast_channel *owner)
  300. {
  301. struct ast_dial_channel *channel = NULL;
  302. AST_LIST_LOCK(&dial->channels);
  303. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  304. if (channel->owner == owner)
  305. break;
  306. }
  307. AST_LIST_UNLOCK(&dial->channels);
  308. return channel;
  309. }
  310. static void set_state(struct ast_dial *dial, enum ast_dial_result state)
  311. {
  312. dial->state = state;
  313. if (dial->state_callback)
  314. dial->state_callback(dial);
  315. }
  316. /*! \brief Helper function that handles control frames WITH owner */
  317. static void handle_frame(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr, struct ast_channel *chan)
  318. {
  319. if (fr->frametype == AST_FRAME_CONTROL) {
  320. switch (fr->subclass.integer) {
  321. case AST_CONTROL_ANSWER:
  322. ast_verb(3, "%s answered %s\n", channel->owner->name, chan->name);
  323. AST_LIST_LOCK(&dial->channels);
  324. AST_LIST_REMOVE(&dial->channels, channel, list);
  325. AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
  326. AST_LIST_UNLOCK(&dial->channels);
  327. set_state(dial, AST_DIAL_RESULT_ANSWERED);
  328. break;
  329. case AST_CONTROL_BUSY:
  330. ast_verb(3, "%s is busy\n", channel->owner->name);
  331. ast_hangup(channel->owner);
  332. channel->owner = NULL;
  333. break;
  334. case AST_CONTROL_CONGESTION:
  335. ast_verb(3, "%s is circuit-busy\n", channel->owner->name);
  336. ast_hangup(channel->owner);
  337. channel->owner = NULL;
  338. break;
  339. case AST_CONTROL_INCOMPLETE:
  340. ast_verb(3, "%s dialed Incomplete extension %s\n", channel->owner->name, channel->owner->exten);
  341. ast_indicate(chan, AST_CONTROL_INCOMPLETE);
  342. break;
  343. case AST_CONTROL_RINGING:
  344. ast_verb(3, "%s is ringing\n", channel->owner->name);
  345. if (!dial->options[AST_DIAL_OPTION_MUSIC])
  346. ast_indicate(chan, AST_CONTROL_RINGING);
  347. set_state(dial, AST_DIAL_RESULT_RINGING);
  348. break;
  349. case AST_CONTROL_PROGRESS:
  350. ast_verb(3, "%s is making progress, passing it to %s\n", channel->owner->name, chan->name);
  351. ast_indicate(chan, AST_CONTROL_PROGRESS);
  352. set_state(dial, AST_DIAL_RESULT_PROGRESS);
  353. break;
  354. case AST_CONTROL_VIDUPDATE:
  355. ast_verb(3, "%s requested a video update, passing it to %s\n", channel->owner->name, chan->name);
  356. ast_indicate(chan, AST_CONTROL_VIDUPDATE);
  357. break;
  358. case AST_CONTROL_SRCUPDATE:
  359. if (option_verbose > 2)
  360. ast_verbose (VERBOSE_PREFIX_3 "%s requested a source update, passing it to %s\n", channel->owner->name, chan->name);
  361. ast_indicate(chan, AST_CONTROL_SRCUPDATE);
  362. break;
  363. case AST_CONTROL_CONNECTED_LINE:
  364. ast_verb(3, "%s connected line has changed, passing it to %s\n", channel->owner->name, chan->name);
  365. if (ast_channel_connected_line_macro(channel->owner, chan, fr, 1, 1)) {
  366. ast_indicate_data(chan, AST_CONTROL_CONNECTED_LINE, fr->data.ptr, fr->datalen);
  367. }
  368. break;
  369. case AST_CONTROL_REDIRECTING:
  370. ast_verb(3, "%s redirecting info has changed, passing it to %s\n", channel->owner->name, chan->name);
  371. if (ast_channel_redirecting_macro(channel->owner, chan, fr, 1, 1)) {
  372. ast_indicate_data(chan, AST_CONTROL_REDIRECTING, fr->data.ptr, fr->datalen);
  373. }
  374. break;
  375. case AST_CONTROL_PROCEEDING:
  376. ast_verb(3, "%s is proceeding, passing it to %s\n", channel->owner->name, chan->name);
  377. ast_indicate(chan, AST_CONTROL_PROCEEDING);
  378. set_state(dial, AST_DIAL_RESULT_PROCEEDING);
  379. break;
  380. case AST_CONTROL_HOLD:
  381. ast_verb(3, "Call on %s placed on hold\n", chan->name);
  382. ast_indicate(chan, AST_CONTROL_HOLD);
  383. break;
  384. case AST_CONTROL_UNHOLD:
  385. ast_verb(3, "Call on %s left from hold\n", chan->name);
  386. ast_indicate(chan, AST_CONTROL_UNHOLD);
  387. break;
  388. case AST_CONTROL_OFFHOOK:
  389. case AST_CONTROL_FLASH:
  390. break;
  391. case -1:
  392. /* Prod the channel */
  393. ast_indicate(chan, -1);
  394. break;
  395. default:
  396. break;
  397. }
  398. }
  399. return;
  400. }
  401. /*! \brief Helper function that handles control frames WITHOUT owner */
  402. static void handle_frame_ownerless(struct ast_dial *dial, struct ast_dial_channel *channel, struct ast_frame *fr)
  403. {
  404. /* If we have no owner we can only update the state of the dial structure, so only look at control frames */
  405. if (fr->frametype != AST_FRAME_CONTROL)
  406. return;
  407. switch (fr->subclass.integer) {
  408. case AST_CONTROL_ANSWER:
  409. ast_verb(3, "%s answered\n", channel->owner->name);
  410. AST_LIST_LOCK(&dial->channels);
  411. AST_LIST_REMOVE(&dial->channels, channel, list);
  412. AST_LIST_INSERT_HEAD(&dial->channels, channel, list);
  413. AST_LIST_UNLOCK(&dial->channels);
  414. set_state(dial, AST_DIAL_RESULT_ANSWERED);
  415. break;
  416. case AST_CONTROL_BUSY:
  417. ast_verb(3, "%s is busy\n", channel->owner->name);
  418. ast_hangup(channel->owner);
  419. channel->owner = NULL;
  420. break;
  421. case AST_CONTROL_CONGESTION:
  422. ast_verb(3, "%s is circuit-busy\n", channel->owner->name);
  423. ast_hangup(channel->owner);
  424. channel->owner = NULL;
  425. break;
  426. case AST_CONTROL_RINGING:
  427. ast_verb(3, "%s is ringing\n", channel->owner->name);
  428. set_state(dial, AST_DIAL_RESULT_RINGING);
  429. break;
  430. case AST_CONTROL_PROGRESS:
  431. ast_verb(3, "%s is making progress\n", channel->owner->name);
  432. set_state(dial, AST_DIAL_RESULT_PROGRESS);
  433. break;
  434. case AST_CONTROL_PROCEEDING:
  435. ast_verb(3, "%s is proceeding\n", channel->owner->name);
  436. set_state(dial, AST_DIAL_RESULT_PROCEEDING);
  437. break;
  438. default:
  439. break;
  440. }
  441. return;
  442. }
  443. /*! \brief Helper function to handle when a timeout occurs on dialing attempt */
  444. static int handle_timeout_trip(struct ast_dial *dial, struct timeval start)
  445. {
  446. struct ast_dial_channel *channel = NULL;
  447. int diff = ast_tvdiff_ms(ast_tvnow(), start), lowest_timeout = -1, new_timeout = -1;
  448. /* If the global dial timeout tripped switch the state to timeout so our channel loop will drop every channel */
  449. if (diff >= dial->timeout) {
  450. set_state(dial, AST_DIAL_RESULT_TIMEOUT);
  451. new_timeout = 0;
  452. }
  453. /* Go through dropping out channels that have met their timeout */
  454. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  455. if (dial->state == AST_DIAL_RESULT_TIMEOUT || diff >= channel->timeout) {
  456. ast_hangup(channel->owner);
  457. channel->owner = NULL;
  458. } else if ((lowest_timeout == -1) || (lowest_timeout > channel->timeout)) {
  459. lowest_timeout = channel->timeout;
  460. }
  461. }
  462. /* Calculate the new timeout using the lowest timeout found */
  463. if (lowest_timeout >= 0)
  464. new_timeout = lowest_timeout - diff;
  465. return new_timeout;
  466. }
  467. /*! \brief Helper function that basically keeps tabs on dialing attempts */
  468. static enum ast_dial_result monitor_dial(struct ast_dial *dial, struct ast_channel *chan)
  469. {
  470. int timeout = -1;
  471. struct ast_channel *cs[AST_MAX_WATCHERS], *who = NULL;
  472. struct ast_dial_channel *channel = NULL;
  473. struct answer_exec_struct *answer_exec = NULL;
  474. struct timeval start;
  475. set_state(dial, AST_DIAL_RESULT_TRYING);
  476. /* If the "always indicate ringing" option is set, change state to ringing and indicate to the owner if present */
  477. if (dial->options[AST_DIAL_OPTION_RINGING]) {
  478. set_state(dial, AST_DIAL_RESULT_RINGING);
  479. if (chan)
  480. ast_indicate(chan, AST_CONTROL_RINGING);
  481. } else if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
  482. !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
  483. char *original_moh = ast_strdupa(chan->musicclass);
  484. ast_indicate(chan, -1);
  485. ast_string_field_set(chan, musicclass, dial->options[AST_DIAL_OPTION_MUSIC]);
  486. ast_moh_start(chan, dial->options[AST_DIAL_OPTION_MUSIC], NULL);
  487. ast_string_field_set(chan, musicclass, original_moh);
  488. }
  489. /* Record start time for timeout purposes */
  490. start = ast_tvnow();
  491. /* We actually figured out the maximum timeout we can do as they were added, so we can directly access the info */
  492. timeout = dial->actual_timeout;
  493. /* Go into an infinite loop while we are trying */
  494. 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)) {
  495. int pos = 0, count = 0;
  496. struct ast_frame *fr = NULL;
  497. /* Set up channel structure array */
  498. pos = count = 0;
  499. if (chan)
  500. cs[pos++] = chan;
  501. /* Add channels we are attempting to dial */
  502. AST_LIST_LOCK(&dial->channels);
  503. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  504. if (channel->owner) {
  505. cs[pos++] = channel->owner;
  506. count++;
  507. }
  508. }
  509. AST_LIST_UNLOCK(&dial->channels);
  510. /* If we have no outbound channels in progress, switch state to unanswered and stop */
  511. if (!count) {
  512. set_state(dial, AST_DIAL_RESULT_UNANSWERED);
  513. break;
  514. }
  515. /* Just to be safe... */
  516. if (dial->thread == AST_PTHREADT_STOP)
  517. break;
  518. /* Wait for frames from channels */
  519. who = ast_waitfor_n(cs, pos, &timeout);
  520. /* Check to see if our thread is being cancelled */
  521. if (dial->thread == AST_PTHREADT_STOP)
  522. break;
  523. /* If the timeout no longer exists OR if we got no channel it basically means the timeout was tripped, so handle it */
  524. if (!timeout || !who) {
  525. timeout = handle_timeout_trip(dial, start);
  526. continue;
  527. }
  528. /* Find relative dial channel */
  529. if (!chan || !IS_CALLER(chan, who))
  530. channel = find_relative_dial_channel(dial, who);
  531. /* See if this channel has been forwarded elsewhere */
  532. if (!ast_strlen_zero(who->call_forward)) {
  533. handle_call_forward(dial, channel, chan);
  534. continue;
  535. }
  536. /* Attempt to read in a frame */
  537. if (!(fr = ast_read(who))) {
  538. /* If this is the caller then we switch state to hangup and stop */
  539. if (chan && IS_CALLER(chan, who)) {
  540. set_state(dial, AST_DIAL_RESULT_HANGUP);
  541. break;
  542. }
  543. if (chan)
  544. ast_poll_channel_del(chan, channel->owner);
  545. ast_hangup(who);
  546. channel->owner = NULL;
  547. continue;
  548. }
  549. /* Process the frame */
  550. if (chan)
  551. handle_frame(dial, channel, fr, chan);
  552. else
  553. handle_frame_ownerless(dial, channel, fr);
  554. /* Free the received frame and start all over */
  555. ast_frfree(fr);
  556. }
  557. /* Do post-processing from loop */
  558. if (dial->state == AST_DIAL_RESULT_ANSWERED) {
  559. /* Hangup everything except that which answered */
  560. AST_LIST_LOCK(&dial->channels);
  561. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  562. if (!channel->owner || channel->owner == who)
  563. continue;
  564. if (chan)
  565. ast_poll_channel_del(chan, channel->owner);
  566. ast_hangup(channel->owner);
  567. channel->owner = NULL;
  568. }
  569. AST_LIST_UNLOCK(&dial->channels);
  570. /* If ANSWER_EXEC is enabled as an option, execute application on answered channel */
  571. if ((channel = find_relative_dial_channel(dial, who)) && (answer_exec = FIND_RELATIVE_OPTION(dial, channel, AST_DIAL_OPTION_ANSWER_EXEC))) {
  572. channel->is_running_app = 1;
  573. answer_exec_run(dial, channel, answer_exec->app, answer_exec->args);
  574. channel->is_running_app = 0;
  575. }
  576. if (chan && dial->options[AST_DIAL_OPTION_MUSIC] &&
  577. !ast_strlen_zero(dial->options[AST_DIAL_OPTION_MUSIC])) {
  578. ast_moh_stop(chan);
  579. }
  580. } else if (dial->state == AST_DIAL_RESULT_HANGUP) {
  581. /* Hangup everything */
  582. AST_LIST_LOCK(&dial->channels);
  583. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  584. if (!channel->owner)
  585. continue;
  586. if (chan)
  587. ast_poll_channel_del(chan, channel->owner);
  588. ast_hangup(channel->owner);
  589. channel->owner = NULL;
  590. }
  591. AST_LIST_UNLOCK(&dial->channels);
  592. }
  593. return dial->state;
  594. }
  595. /*! \brief Dial async thread function */
  596. static void *async_dial(void *data)
  597. {
  598. struct ast_dial *dial = data;
  599. /* This is really really simple... we basically pass monitor_dial a NULL owner and it changes it's behavior */
  600. monitor_dial(dial, NULL);
  601. return NULL;
  602. }
  603. /*! \brief Execute dialing synchronously or asynchronously
  604. * \note Dials channels in a dial structure.
  605. * \return Returns dial result code. (TRYING/INVALID/FAILED/ANSWERED/TIMEOUT/UNANSWERED).
  606. */
  607. enum ast_dial_result ast_dial_run(struct ast_dial *dial, struct ast_channel *chan, int async)
  608. {
  609. enum ast_dial_result res = AST_DIAL_RESULT_TRYING;
  610. /* Ensure required arguments are passed */
  611. if (!dial || (!chan && !async)) {
  612. ast_debug(1, "invalid #1\n");
  613. return AST_DIAL_RESULT_INVALID;
  614. }
  615. /* If there are no channels to dial we can't very well try to dial them */
  616. if (AST_LIST_EMPTY(&dial->channels)) {
  617. ast_debug(1, "invalid #2\n");
  618. return AST_DIAL_RESULT_INVALID;
  619. }
  620. /* Dial each requested channel */
  621. if (!begin_dial(dial, chan))
  622. return AST_DIAL_RESULT_FAILED;
  623. /* If we are running async spawn a thread and send it away... otherwise block here */
  624. if (async) {
  625. dial->state = AST_DIAL_RESULT_TRYING;
  626. /* Try to create a thread */
  627. if (ast_pthread_create(&dial->thread, NULL, async_dial, dial)) {
  628. /* Failed to create the thread - hangup all dialed channels and return failed */
  629. ast_dial_hangup(dial);
  630. res = AST_DIAL_RESULT_FAILED;
  631. }
  632. } else {
  633. res = monitor_dial(dial, chan);
  634. }
  635. return res;
  636. }
  637. /*! \brief Return channel that answered
  638. * \note Returns the Asterisk channel that answered
  639. * \param dial Dialing structure
  640. */
  641. struct ast_channel *ast_dial_answered(struct ast_dial *dial)
  642. {
  643. if (!dial)
  644. return NULL;
  645. return ((dial->state == AST_DIAL_RESULT_ANSWERED) ? AST_LIST_FIRST(&dial->channels)->owner : NULL);
  646. }
  647. /*! \brief Steal the channel that answered
  648. * \note Returns the Asterisk channel that answered and removes it from the dialing structure
  649. * \param dial Dialing structure
  650. */
  651. struct ast_channel *ast_dial_answered_steal(struct ast_dial *dial)
  652. {
  653. struct ast_channel *chan = NULL;
  654. if (!dial)
  655. return NULL;
  656. if (dial->state == AST_DIAL_RESULT_ANSWERED) {
  657. chan = AST_LIST_FIRST(&dial->channels)->owner;
  658. AST_LIST_FIRST(&dial->channels)->owner = NULL;
  659. }
  660. return chan;
  661. }
  662. /*! \brief Return state of dial
  663. * \note Returns the state of the dial attempt
  664. * \param dial Dialing structure
  665. */
  666. enum ast_dial_result ast_dial_state(struct ast_dial *dial)
  667. {
  668. return dial->state;
  669. }
  670. /*! \brief Cancel async thread
  671. * \note Cancel a running async thread
  672. * \param dial Dialing structure
  673. */
  674. enum ast_dial_result ast_dial_join(struct ast_dial *dial)
  675. {
  676. pthread_t thread;
  677. /* If the dial structure is not running in async, return failed */
  678. if (dial->thread == AST_PTHREADT_NULL)
  679. return AST_DIAL_RESULT_FAILED;
  680. /* Record thread */
  681. thread = dial->thread;
  682. /* Boom, commence locking */
  683. ast_mutex_lock(&dial->lock);
  684. /* Stop the thread */
  685. dial->thread = AST_PTHREADT_STOP;
  686. /* If the answered channel is running an application we have to soft hangup it, can't just poke the thread */
  687. AST_LIST_LOCK(&dial->channels);
  688. if (AST_LIST_FIRST(&dial->channels)->is_running_app) {
  689. struct ast_channel *chan = AST_LIST_FIRST(&dial->channels)->owner;
  690. if (chan) {
  691. ast_channel_lock(chan);
  692. ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
  693. ast_channel_unlock(chan);
  694. }
  695. } else {
  696. /* Now we signal it with SIGURG so it will break out of it's waitfor */
  697. pthread_kill(thread, SIGURG);
  698. }
  699. AST_LIST_UNLOCK(&dial->channels);
  700. /* Yay done with it */
  701. ast_mutex_unlock(&dial->lock);
  702. /* Finally wait for the thread to exit */
  703. pthread_join(thread, NULL);
  704. /* Yay thread is all gone */
  705. dial->thread = AST_PTHREADT_NULL;
  706. return dial->state;
  707. }
  708. /*! \brief Hangup channels
  709. * \note Hangup all active channels
  710. * \param dial Dialing structure
  711. */
  712. void ast_dial_hangup(struct ast_dial *dial)
  713. {
  714. struct ast_dial_channel *channel = NULL;
  715. if (!dial)
  716. return;
  717. AST_LIST_LOCK(&dial->channels);
  718. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  719. if (channel->owner) {
  720. ast_hangup(channel->owner);
  721. channel->owner = NULL;
  722. }
  723. }
  724. AST_LIST_UNLOCK(&dial->channels);
  725. return;
  726. }
  727. /*! \brief Destroys a dialing structure
  728. * \note Destroys (free's) the given ast_dial structure
  729. * \param dial Dialing structure to free
  730. * \return Returns 0 on success, -1 on failure
  731. */
  732. int ast_dial_destroy(struct ast_dial *dial)
  733. {
  734. int i = 0;
  735. struct ast_dial_channel *channel = NULL;
  736. if (!dial)
  737. return -1;
  738. /* Hangup and deallocate all the dialed channels */
  739. AST_LIST_LOCK(&dial->channels);
  740. AST_LIST_TRAVERSE_SAFE_BEGIN(&dial->channels, channel, list) {
  741. /* Disable any enabled options */
  742. for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
  743. if (!channel->options[i])
  744. continue;
  745. if (option_types[i].disable)
  746. option_types[i].disable(channel->options[i]);
  747. channel->options[i] = NULL;
  748. }
  749. /* Hang up channel if need be */
  750. if (channel->owner) {
  751. ast_hangup(channel->owner);
  752. channel->owner = NULL;
  753. }
  754. /* Free structure */
  755. ast_free(channel->tech);
  756. ast_free(channel->device);
  757. AST_LIST_REMOVE_CURRENT(list);
  758. ast_free(channel);
  759. }
  760. AST_LIST_TRAVERSE_SAFE_END;
  761. AST_LIST_UNLOCK(&dial->channels);
  762. /* Disable any enabled options globally */
  763. for (i = 0; i < AST_DIAL_OPTION_MAX; i++) {
  764. if (!dial->options[i])
  765. continue;
  766. if (option_types[i].disable)
  767. option_types[i].disable(dial->options[i]);
  768. dial->options[i] = NULL;
  769. }
  770. /* Lock be gone! */
  771. ast_mutex_destroy(&dial->lock);
  772. /* Free structure */
  773. ast_free(dial);
  774. return 0;
  775. }
  776. /*! \brief Enables an option globally
  777. * \param dial Dial structure to enable option on
  778. * \param option Option to enable
  779. * \param data Data to pass to this option (not always needed)
  780. * \return Returns 0 on success, -1 on failure
  781. */
  782. int ast_dial_option_global_enable(struct ast_dial *dial, enum ast_dial_option option, void *data)
  783. {
  784. /* If the option is already enabled, return failure */
  785. if (dial->options[option])
  786. return -1;
  787. /* Execute enable callback if it exists, if not simply make sure the value is set */
  788. if (option_types[option].enable)
  789. dial->options[option] = option_types[option].enable(data);
  790. else
  791. dial->options[option] = (void*)1;
  792. return 0;
  793. }
  794. /*! \brief Helper function for finding a channel in a dial structure based on number
  795. */
  796. static struct ast_dial_channel *find_dial_channel(struct ast_dial *dial, int num)
  797. {
  798. struct ast_dial_channel *channel = AST_LIST_LAST(&dial->channels);
  799. /* We can try to predict programmer behavior, the last channel they added is probably the one they wanted to modify */
  800. if (channel->num == num)
  801. return channel;
  802. /* Hrm not at the end... looking through the list it is! */
  803. AST_LIST_LOCK(&dial->channels);
  804. AST_LIST_TRAVERSE(&dial->channels, channel, list) {
  805. if (channel->num == num)
  806. break;
  807. }
  808. AST_LIST_UNLOCK(&dial->channels);
  809. return channel;
  810. }
  811. /*! \brief Enables an option per channel
  812. * \param dial Dial structure
  813. * \param num Channel number to enable option on
  814. * \param option Option to enable
  815. * \param data Data to pass to this option (not always needed)
  816. * \return Returns 0 on success, -1 on failure
  817. */
  818. int ast_dial_option_enable(struct ast_dial *dial, int num, enum ast_dial_option option, void *data)
  819. {
  820. struct ast_dial_channel *channel = NULL;
  821. /* Ensure we have required arguments */
  822. if (!dial || AST_LIST_EMPTY(&dial->channels))
  823. return -1;
  824. if (!(channel = find_dial_channel(dial, num)))
  825. return -1;
  826. /* If the option is already enabled, return failure */
  827. if (channel->options[option])
  828. return -1;
  829. /* Execute enable callback if it exists, if not simply make sure the value is set */
  830. if (option_types[option].enable)
  831. channel->options[option] = option_types[option].enable(data);
  832. else
  833. channel->options[option] = (void*)1;
  834. return 0;
  835. }
  836. /*! \brief Disables an option globally
  837. * \param dial Dial structure to disable option on
  838. * \param option Option to disable
  839. * \return Returns 0 on success, -1 on failure
  840. */
  841. int ast_dial_option_global_disable(struct ast_dial *dial, enum ast_dial_option option)
  842. {
  843. /* If the option is not enabled, return failure */
  844. if (!dial->options[option]) {
  845. return -1;
  846. }
  847. /* Execute callback of option to disable if it exists */
  848. if (option_types[option].disable)
  849. option_types[option].disable(dial->options[option]);
  850. /* Finally disable option on the structure */
  851. dial->options[option] = NULL;
  852. return 0;
  853. }
  854. /*! \brief Disables an option per channel
  855. * \param dial Dial structure
  856. * \param num Channel number to disable option on
  857. * \param option Option to disable
  858. * \return Returns 0 on success, -1 on failure
  859. */
  860. int ast_dial_option_disable(struct ast_dial *dial, int num, enum ast_dial_option option)
  861. {
  862. struct ast_dial_channel *channel = NULL;
  863. /* Ensure we have required arguments */
  864. if (!dial || AST_LIST_EMPTY(&dial->channels))
  865. return -1;
  866. if (!(channel = find_dial_channel(dial, num)))
  867. return -1;
  868. /* If the option is not enabled, return failure */
  869. if (!channel->options[option])
  870. return -1;
  871. /* Execute callback of option to disable it if it exists */
  872. if (option_types[option].disable)
  873. option_types[option].disable(channel->options[option]);
  874. /* Finally disable the option on the structure */
  875. channel->options[option] = NULL;
  876. return 0;
  877. }
  878. void ast_dial_set_state_callback(struct ast_dial *dial, ast_dial_state_callback callback)
  879. {
  880. dial->state_callback = callback;
  881. }
  882. /*! \brief Set the maximum time (globally) allowed for trying to ring phones
  883. * \param dial The dial structure to apply the time limit to
  884. * \param timeout Maximum time allowed
  885. * \return nothing
  886. */
  887. void ast_dial_set_global_timeout(struct ast_dial *dial, int timeout)
  888. {
  889. dial->timeout = timeout;
  890. if (dial->timeout > 0 && (dial->actual_timeout > dial->timeout || dial->actual_timeout == -1))
  891. dial->actual_timeout = dial->timeout;
  892. return;
  893. }
  894. /*! \brief Set the maximum time (per channel) allowed for trying to ring the phone
  895. * \param dial The dial structure the channel belongs to
  896. * \param num Channel number to set timeout on
  897. * \param timeout Maximum time allowed
  898. * \return nothing
  899. */
  900. void ast_dial_set_timeout(struct ast_dial *dial, int num, int timeout)
  901. {
  902. struct ast_dial_channel *channel = NULL;
  903. if (!(channel = find_dial_channel(dial, num)))
  904. return;
  905. channel->timeout = timeout;
  906. if (channel->timeout > 0 && (dial->actual_timeout > channel->timeout || dial->actual_timeout == -1))
  907. dial->actual_timeout = channel->timeout;
  908. return;
  909. }