81c58e8b0cbc5a147d160b94568727fb07f9b7f5
[asterisk/asterisk.git] / apps / app_stasis.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012 - 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@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
19 /*! \file
20  *
21  * \brief Stasis dialplan application.
22  *
23  * \author David M. Lee, II <dlee@digium.com>
24  */
25
26 /*** MODULEINFO
27         <depend>res_stasis</depend>
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 #include "asterisk/app.h"
34 #include "asterisk/module.h"
35 #include "asterisk/pbx.h"
36 #include "asterisk/stasis.h"
37 #include "asterisk/stasis_app_impl.h"
38
39 /*** DOCUMENTATION
40         <application name="Stasis" language="en_US">
41                 <synopsis>Invoke an external Stasis application.</synopsis>
42                 <syntax>
43                         <parameter name="app_name" required="true">
44                                 <para>Name of the application to invoke.</para>
45                         </parameter>
46                         <parameter name="args">
47                                 <para>Optional comma-delimited arguments for the
48                                 application invocation.</para>
49                         </parameter>
50                 </syntax>
51                 <description>
52                         <para>Invoke a Stasis application.</para>
53                         <para>This application will set the following channel variable upon
54                         completion:</para>
55                         <variablelist>
56                                 <variable name="STASISSTATUS">
57                                         <para>This indicates the status of the execution of the
58                                         Stasis application.</para>
59                                         <value name="SUCCESS">
60                                                 The channel has exited Stasis without any failures in
61                                                 Stasis.
62                                         </value>
63                                         <value name="FAILED">
64                                                 A failure occurred when executing the Stasis
65                                                 The app registry is not instantiated; The app
66                                                 application. Some (not all) possible reasons for this:
67                                                 requested is not registered; The app requested is not
68                                                 active; Stasis couldn't send a start message.
69                                         </value>
70                                 </variable>
71                         </variablelist>
72                 </description>
73         </application>
74  ***/
75
76 /*! \brief Maximum number of arguments for the Stasis dialplan application */
77 #define MAX_ARGS 128
78
79 /*! \brief Dialplan application name */
80 static const char *stasis = "Stasis";
81
82 /*! /brief Stasis dialplan application callback */
83 static int app_exec(struct ast_channel *chan, const char *data)
84 {
85         char *parse = NULL;
86         int ret = -1;
87
88         AST_DECLARE_APP_ARGS(args,
89                 AST_APP_ARG(app_name);
90                 AST_APP_ARG(app_argv)[MAX_ARGS];
91         );
92
93         ast_assert(chan != NULL);
94         ast_assert(data != NULL);
95
96         pbx_builtin_setvar_helper(chan, "STASISSTATUS", "");
97
98         /* parse the arguments */
99         parse = ast_strdupa(data);
100         AST_STANDARD_APP_ARGS(args, parse);
101
102         if (args.argc < 1) {
103                 ast_log(LOG_WARNING, "Stasis app_name argument missing\n");
104         } else {
105                 ret = stasis_app_exec(chan,
106                                       args.app_name,
107                                       args.argc - 1,
108                                       args.app_argv);
109         }
110
111         if (ret) {
112                 /* set ret to 0 so pbx_core doesnt hangup the channel */
113                 if (!ast_check_hangup(chan)) {
114                         ret = 0;
115                 } else {
116                         ret = -1;
117                 }
118                 pbx_builtin_setvar_helper(chan, "STASISSTATUS", "FAILED");
119         } else {
120                 pbx_builtin_setvar_helper(chan, "STASISSTATUS", "SUCCESS");
121         }
122
123         return ret;
124 }
125
126 static int load_module(void)
127 {
128         int r = 0;
129
130         stasis_app_ref();
131         r |= ast_register_application_xml(stasis, app_exec);
132         return r;
133 }
134
135 static int unload_module(void)
136 {
137         int r = 0;
138         r |= ast_unregister_application(stasis);
139         stasis_app_unref();
140         return r;
141 }
142
143 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Stasis dialplan application",
144         .support_level = AST_MODULE_SUPPORT_CORE,
145         .load = load_module,
146         .unload = unload_module,
147         .nonoptreq = "res_stasis",
148 );