dial.c 32 KB

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