app_originate.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008, Roberto Casas.
  5. * Copyright (C) 2008, Digium, Inc.
  6. *
  7. * Roberto Casas <roberto.casas@diaple.com>
  8. * Russell Bryant <russell@digium.com>
  9. *
  10. * See http://www.asterisk.org for more information about
  11. * the Asterisk project. Please do not directly contact
  12. * any of the maintainers of this project for assistance;
  13. * the project provides a web site, mailing lists and IRC
  14. * channels for your use.
  15. *
  16. * This program is free software, distributed under the terms of
  17. * the GNU General Public License Version 2. See the LICENSE file
  18. * at the top of the source tree.
  19. */
  20. /*!
  21. * \file
  22. * \brief Originate application
  23. *
  24. * \author Roberto Casas <roberto.casas@diaple.com>
  25. * \author Russell Bryant <russell@digium.com>
  26. *
  27. * \ingroup applications
  28. *
  29. * \todo Make a way to be able to set variables (and functions) on the outbound
  30. * channel, similar to the Variable headers for the AMI Originate, and the
  31. * Set options for call files.
  32. */
  33. /*** MODULEINFO
  34. <support_level>core</support_level>
  35. ***/
  36. #include "asterisk.h"
  37. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  38. #include "asterisk/file.h"
  39. #include "asterisk/channel.h"
  40. #include "asterisk/pbx.h"
  41. #include "asterisk/module.h"
  42. #include "asterisk/app.h"
  43. #include "asterisk/format_cache.h"
  44. static const char app_originate[] = "Originate";
  45. /*** DOCUMENTATION
  46. <application name="Originate" language="en_US">
  47. <synopsis>
  48. Originate a call.
  49. </synopsis>
  50. <syntax>
  51. <parameter name="tech_data" required="true">
  52. <para>Channel technology and data for creating the outbound channel.
  53. For example, SIP/1234.</para>
  54. </parameter>
  55. <parameter name="type" required="true">
  56. <para>This should be <literal>app</literal> or <literal>exten</literal>, depending on whether the outbound channel should be connected to an application or extension.</para>
  57. </parameter>
  58. <parameter name="arg1" required="true">
  59. <para>If the type is <literal>app</literal>, then this is the application name. If the type is <literal>exten</literal>, then this is the context that the channel will be sent to.</para>
  60. </parameter>
  61. <parameter name="arg2" required="false">
  62. <para>If the type is <literal>app</literal>, then this is the data passed as arguments to the application. If the type is <literal>exten</literal>, then this is the extension that the channel will be sent to.</para>
  63. </parameter>
  64. <parameter name="arg3" required="false">
  65. <para>If the type is <literal>exten</literal>, then this is the priority that the channel is sent to. If the type is <literal>app</literal>, then this parameter is ignored.</para>
  66. </parameter>
  67. <parameter name="timeout" required="false">
  68. <para>Timeout in seconds. Default is 30 seconds.</para>
  69. </parameter>
  70. </syntax>
  71. <description>
  72. <para>This application originates an outbound call and connects it to a specified extension or application. This application will block until the outgoing call fails or gets answered. At that point, this application will exit with the status variable set and dialplan processing will continue.</para>
  73. <para>This application sets the following channel variable before exiting:</para>
  74. <variablelist>
  75. <variable name="ORIGINATE_STATUS">
  76. <para>This indicates the result of the call origination.</para>
  77. <value name="FAILED"/>
  78. <value name="SUCCESS"/>
  79. <value name="BUSY"/>
  80. <value name="CONGESTION"/>
  81. <value name="HANGUP"/>
  82. <value name="RINGING"/>
  83. <value name="UNKNOWN">
  84. In practice, you should never see this value. Please report it to the issue tracker if you ever see it.
  85. </value>
  86. </variable>
  87. </variablelist>
  88. </description>
  89. </application>
  90. ***/
  91. static int originate_exec(struct ast_channel *chan, const char *data)
  92. {
  93. AST_DECLARE_APP_ARGS(args,
  94. AST_APP_ARG(tech_data);
  95. AST_APP_ARG(type);
  96. AST_APP_ARG(arg1);
  97. AST_APP_ARG(arg2);
  98. AST_APP_ARG(arg3);
  99. AST_APP_ARG(timeout);
  100. );
  101. char *parse;
  102. char *chantech, *chandata;
  103. int res = -1;
  104. int continue_in_dialplan = 0;
  105. int outgoing_status = 0;
  106. unsigned int timeout = 30;
  107. static const char default_exten[] = "s";
  108. struct ast_format_cap *cap_slin = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  109. ast_autoservice_start(chan);
  110. if (!cap_slin) {
  111. goto return_cleanup;
  112. }
  113. ast_format_cap_append(cap_slin, ast_format_slin, 0);
  114. ast_format_cap_append(cap_slin, ast_format_slin12, 0);
  115. ast_format_cap_append(cap_slin, ast_format_slin16, 0);
  116. ast_format_cap_append(cap_slin, ast_format_slin24, 0);
  117. ast_format_cap_append(cap_slin, ast_format_slin32, 0);
  118. ast_format_cap_append(cap_slin, ast_format_slin44, 0);
  119. ast_format_cap_append(cap_slin, ast_format_slin48, 0);
  120. ast_format_cap_append(cap_slin, ast_format_slin96, 0);
  121. ast_format_cap_append(cap_slin, ast_format_slin192, 0);
  122. if (ast_strlen_zero(data)) {
  123. ast_log(LOG_ERROR, "Originate() requires arguments\n");
  124. goto return_cleanup;
  125. }
  126. parse = ast_strdupa(data);
  127. AST_STANDARD_APP_ARGS(args, parse);
  128. if (args.argc < 3) {
  129. ast_log(LOG_ERROR, "Incorrect number of arguments\n");
  130. goto return_cleanup;
  131. }
  132. if (!ast_strlen_zero(args.timeout)) {
  133. if(sscanf(args.timeout, "%u", &timeout) != 1) {
  134. ast_log(LOG_NOTICE, "Invalid timeout: '%s'. Setting timeout to 30 seconds\n", args.timeout);
  135. timeout = 30;
  136. }
  137. }
  138. chandata = ast_strdupa(args.tech_data);
  139. chantech = strsep(&chandata, "/");
  140. if (ast_strlen_zero(chandata) || ast_strlen_zero(chantech)) {
  141. ast_log(LOG_ERROR, "Channel Tech/Data invalid: '%s'\n", args.tech_data);
  142. goto return_cleanup;
  143. }
  144. if (strcasecmp(args.type, "exten") && strcasecmp(args.type, "app")) {
  145. ast_log(LOG_ERROR, "Incorrect type, it should be 'exten' or 'app': %s\n",
  146. args.type);
  147. goto return_cleanup;
  148. }
  149. if (!strcasecmp(args.type, "exten")) {
  150. int priority = 1; /* Initialized in case priority not specified */
  151. const char *exten = args.arg2;
  152. if (args.argc == 5) {
  153. /* Context/Exten/Priority all specified */
  154. if (sscanf(args.arg3, "%30d", &priority) != 1) {
  155. ast_log(LOG_ERROR, "Invalid priority: '%s'\n", args.arg3);
  156. goto return_cleanup;
  157. }
  158. } else if (args.argc == 3) {
  159. /* Exten not specified */
  160. exten = default_exten;
  161. }
  162. ast_debug(1, "Originating call to '%s/%s' and connecting them to extension %s,%s,%d\n",
  163. chantech, chandata, args.arg1, exten, priority);
  164. res = ast_pbx_outgoing_exten(chantech, cap_slin, chandata,
  165. timeout * 1000, args.arg1, exten, priority, &outgoing_status,
  166. AST_OUTGOING_WAIT, NULL, NULL, NULL, NULL, NULL, 0, NULL);
  167. } else {
  168. ast_debug(1, "Originating call to '%s/%s' and connecting them to %s(%s)\n",
  169. chantech, chandata, args.arg1, S_OR(args.arg2, ""));
  170. res = ast_pbx_outgoing_app(chantech, cap_slin, chandata,
  171. timeout * 1000, args.arg1, args.arg2, &outgoing_status,
  172. AST_OUTGOING_WAIT, NULL, NULL, NULL, NULL, NULL, NULL);
  173. }
  174. /*
  175. * Getting here means that we have passed the various validation checks and
  176. * have at least attempted the dial. If we have a reason (outgoing_status),
  177. * we clear our error indicator so that we ultimately report the right thing
  178. * to the caller.
  179. */
  180. if (res && outgoing_status) {
  181. res = 0;
  182. }
  183. /* We need to exit cleanly if we've gotten this far */
  184. continue_in_dialplan = 1;
  185. return_cleanup:
  186. if (res) {
  187. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "FAILED");
  188. } else {
  189. switch (outgoing_status) {
  190. case 0:
  191. case AST_CONTROL_ANSWER:
  192. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "SUCCESS");
  193. break;
  194. case AST_CONTROL_BUSY:
  195. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "BUSY");
  196. break;
  197. case AST_CONTROL_CONGESTION:
  198. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "CONGESTION");
  199. break;
  200. case AST_CONTROL_HANGUP:
  201. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "HANGUP");
  202. break;
  203. case AST_CONTROL_RINGING:
  204. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "RINGING");
  205. break;
  206. default:
  207. ast_log(LOG_WARNING, "Unknown originate status result of '%d'\n",
  208. outgoing_status);
  209. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "UNKNOWN");
  210. break;
  211. }
  212. }
  213. ao2_cleanup(cap_slin);
  214. ast_autoservice_stop(chan);
  215. return continue_in_dialplan ? 0 : -1;
  216. }
  217. static int unload_module(void)
  218. {
  219. return ast_unregister_application(app_originate);
  220. }
  221. static int load_module(void)
  222. {
  223. int res;
  224. res = ast_register_application_xml(app_originate, originate_exec);
  225. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  226. }
  227. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Originate call");