app_originate.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 outgoing_status = 0;
  105. unsigned int timeout = 30;
  106. static const char default_exten[] = "s";
  107. struct ast_format_cap *cap_slin = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
  108. ast_autoservice_start(chan);
  109. if (!cap_slin) {
  110. goto return_cleanup;
  111. }
  112. ast_format_cap_append(cap_slin, ast_format_slin, 0);
  113. ast_format_cap_append(cap_slin, ast_format_slin12, 0);
  114. ast_format_cap_append(cap_slin, ast_format_slin16, 0);
  115. ast_format_cap_append(cap_slin, ast_format_slin24, 0);
  116. ast_format_cap_append(cap_slin, ast_format_slin32, 0);
  117. ast_format_cap_append(cap_slin, ast_format_slin44, 0);
  118. ast_format_cap_append(cap_slin, ast_format_slin48, 0);
  119. ast_format_cap_append(cap_slin, ast_format_slin96, 0);
  120. ast_format_cap_append(cap_slin, ast_format_slin192, 0);
  121. if (ast_strlen_zero(data)) {
  122. ast_log(LOG_ERROR, "Originate() requires arguments\n");
  123. goto return_cleanup;
  124. }
  125. parse = ast_strdupa(data);
  126. AST_STANDARD_APP_ARGS(args, parse);
  127. if (args.argc < 3) {
  128. ast_log(LOG_ERROR, "Incorrect number of arguments\n");
  129. goto return_cleanup;
  130. }
  131. if (!ast_strlen_zero(args.timeout)) {
  132. if(sscanf(args.timeout, "%u", &timeout) != 1) {
  133. ast_log(LOG_NOTICE, "Invalid timeout: '%s'. Setting timeout to 30 seconds\n", args.timeout);
  134. timeout = 30;
  135. }
  136. }
  137. chandata = ast_strdupa(args.tech_data);
  138. chantech = strsep(&chandata, "/");
  139. if (ast_strlen_zero(chandata) || ast_strlen_zero(chantech)) {
  140. ast_log(LOG_ERROR, "Channel Tech/Data invalid: '%s'\n", args.tech_data);
  141. goto return_cleanup;
  142. }
  143. if (!strcasecmp(args.type, "exten")) {
  144. int priority = 1; /* Initialized in case priority not specified */
  145. const char *exten = args.arg2;
  146. if (args.argc == 5) {
  147. /* Context/Exten/Priority all specified */
  148. if (sscanf(args.arg3, "%30d", &priority) != 1) {
  149. ast_log(LOG_ERROR, "Invalid priority: '%s'\n", args.arg3);
  150. goto return_cleanup;
  151. }
  152. } else if (args.argc == 3) {
  153. /* Exten not specified */
  154. exten = default_exten;
  155. }
  156. ast_debug(1, "Originating call to '%s/%s' and connecting them to extension %s,%s,%d\n",
  157. chantech, chandata, args.arg1, exten, priority);
  158. ast_pbx_outgoing_exten(chantech, cap_slin, chandata,
  159. timeout * 1000, args.arg1, exten, priority, &outgoing_status, 1, NULL,
  160. NULL, NULL, NULL, NULL, 0, NULL);
  161. } else if (!strcasecmp(args.type, "app")) {
  162. ast_debug(1, "Originating call to '%s/%s' and connecting them to %s(%s)\n",
  163. chantech, chandata, args.arg1, S_OR(args.arg2, ""));
  164. ast_pbx_outgoing_app(chantech, cap_slin, chandata,
  165. timeout * 1000, args.arg1, args.arg2, &outgoing_status, 1, NULL,
  166. NULL, NULL, NULL, NULL, NULL);
  167. } else {
  168. ast_log(LOG_ERROR, "Incorrect type, it should be 'exten' or 'app': %s\n",
  169. args.type);
  170. goto return_cleanup;
  171. }
  172. res = 0;
  173. return_cleanup:
  174. if (res) {
  175. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "FAILED");
  176. } else {
  177. switch (outgoing_status) {
  178. case 0:
  179. case AST_CONTROL_ANSWER:
  180. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "SUCCESS");
  181. break;
  182. case AST_CONTROL_BUSY:
  183. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "BUSY");
  184. break;
  185. case AST_CONTROL_CONGESTION:
  186. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "CONGESTION");
  187. break;
  188. case AST_CONTROL_HANGUP:
  189. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "HANGUP");
  190. break;
  191. case AST_CONTROL_RINGING:
  192. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "RINGING");
  193. break;
  194. default:
  195. ast_log(LOG_WARNING, "Unknown originate status result of '%d'\n",
  196. outgoing_status);
  197. pbx_builtin_setvar_helper(chan, "ORIGINATE_STATUS", "UNKNOWN");
  198. break;
  199. }
  200. }
  201. ao2_cleanup(cap_slin);
  202. ast_autoservice_stop(chan);
  203. return res;
  204. }
  205. static int unload_module(void)
  206. {
  207. return ast_unregister_application(app_originate);
  208. }
  209. static int load_module(void)
  210. {
  211. int res;
  212. res = ast_register_application_xml(app_originate, originate_exec);
  213. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  214. }
  215. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Originate call");