dial.c 34 KB

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