2cf79138dc7677edb9c9e506a0ed098507c1bac9
[asterisk/asterisk.git] / apps / app_chanisavail.c
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 1999 - 2005, Digium, Inc.
5 *
6 * Mark Spencer <markster@digium.com>
7 * James Golovich <james@gnuinter.net>
8 *
9 * See http://www.asterisk.org for more information about
10 * the Asterisk project. Please do not directly contact
11 * any of the maintainers of this project for assistance;
12 * the project provides a web site, mailing lists and IRC
13 * channels for your use.
14 *
15 * This program is free software, distributed under the terms of
16 * the GNU General Public License Version 2. See the LICENSE file
17 * at the top of the source tree.
18 */
19
20 /*! \file
21  * 
22  * \brief Check if Channel is Available
23  * 
24  * \author Mark Spencer <markster@digium.com>
25  * \author James Golovich <james@gnuinter.net>
26
27  * \ingroup applications
28  */
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include <errno.h>
35 #include <sys/ioctl.h>
36
37 #include "asterisk/lock.h"
38 #include "asterisk/file.h"
39 #include "asterisk/logger.h"
40 #include "asterisk/channel.h"
41 #include "asterisk/pbx.h"
42 #include "asterisk/module.h"
43 #include "asterisk/app.h"
44 #include "asterisk/devicestate.h"
45 #include "asterisk/options.h"
46
47 static char *app = "ChanIsAvail";
48
49 static char *synopsis = "Check channel availability";
50
51 static char *descrip = 
52 "  ChanIsAvail(Technology/resource[&Technology2/resource2...][,options]): \n"
53 "This application will check to see if any of the specified channels are\n"
54 "available.\n"
55 "  Options:\n"
56 "    s - Consider the channel unavailable if the channel is in use at all.\n"
57 "    t - Simply checks if specified channels exist in the channel list\n"
58 "        (implies option s).\n"
59 "This application sets the following channel variable upon completion:\n"
60 "  AVAILCHAN     - the name of the available channel, if one exists\n"
61 "  AVAILORIGCHAN - the canonical channel name that was used to create the channel\n"
62 "  AVAILSTATUS   - the status code for the available channel\n";
63
64
65 static int chanavail_exec(struct ast_channel *chan, void *data)
66 {
67         int res=-1, inuse=-1, option_state=0, string_compare=0;
68         int status;
69         char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur;
70         struct ast_channel *tempchan;
71         AST_DECLARE_APP_ARGS(args,
72                 AST_APP_ARG(reqchans);
73                 AST_APP_ARG(options);
74         );
75
76         if (ast_strlen_zero(data)) {
77                 ast_log(LOG_WARNING, "ChanIsAvail requires an argument (Zap/1&Zap/2)\n");
78                 return -1;
79         }
80
81         info = ast_strdupa(data); 
82
83         AST_STANDARD_APP_ARGS(args, info);
84
85         if (args.options) {
86                 if (strchr(args.options, 's'))
87                         option_state = 1;
88                 if (strchr(args.options, 't'))
89                         string_compare = 1;
90         }
91         peers = args.reqchans;
92         if (peers) {
93                 cur = peers;
94                 do {
95                         /* remember where to start next time */
96                         rest = strchr(cur, '&');
97                         if (rest) {
98                                 *rest = 0;
99                                 rest++;
100                         }
101                         tech = cur;
102                         number = strchr(tech, '/');
103                         if (!number) {
104                                 ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
105                                 return -1;
106                         }
107                         *number = '\0';
108                         number++;
109                         
110                         if (string_compare) {
111                                 /* ast_parse_device_state checks for "SIP/1234" as a channel name. 
112                                    ast_device_state will ask the SIP driver for the channel state. */
113
114                                 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
115                                 status = inuse = ast_parse_device_state(trychan);
116                         } else if (option_state) {
117                                 /* If the pbx says in use then don't bother trying further.
118                                    This is to permit testing if someone's on a call, even if the 
119                                    channel can permit more calls (ie callwaiting, sip calls, etc).  */
120                                
121                                 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
122                                 status = inuse = ast_device_state(trychan);
123                         }
124                         if ((inuse <= 1) && (tempchan = ast_request(tech, chan->nativeformats, number, &status))) {
125                                         pbx_builtin_setvar_helper(chan, "AVAILCHAN", tempchan->name);
126                                         /* Store the originally used channel too */
127                                         snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
128                                         pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", tmp);
129                                         snprintf(tmp, sizeof(tmp), "%d", status);
130                                         pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
131                                         ast_hangup(tempchan);
132                                         tempchan = NULL;
133                                         res = 1;
134                                         break;
135                         } else {
136                                 snprintf(tmp, sizeof(tmp), "%d", status);
137                                 pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
138                         }
139                         cur = rest;
140                 } while (cur);
141         }
142         if (res < 1) {
143                 pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
144                 pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", "");
145         }
146
147         return 0;
148 }
149
150 static int unload_module(void)
151 {
152         return ast_unregister_application(app);
153 }
154
155 static int load_module(void)
156 {
157         return ast_register_application(app, chanavail_exec, synopsis, descrip);
158 }
159
160 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Check channel availability");