00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "asterisk.h"
00031
00032 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 123332 $")
00033
00034 #include <sys/ioctl.h>
00035
00036 #include "asterisk/lock.h"
00037 #include "asterisk/file.h"
00038 #include "asterisk/channel.h"
00039 #include "asterisk/pbx.h"
00040 #include "asterisk/module.h"
00041 #include "asterisk/app.h"
00042 #include "asterisk/devicestate.h"
00043
00044 static char *app = "ChanIsAvail";
00045
00046 static char *synopsis = "Check channel availability";
00047
00048 static char *descrip =
00049 " ChanIsAvail(Technology/resource[&Technology2/resource2...][,options]): \n"
00050 "This application will check to see if any of the specified channels are\n"
00051 "available.\n"
00052 " Options:\n"
00053 " s - Consider the channel unavailable if the channel is in use at all.\n"
00054 " t - Simply checks if specified channels exist in the channel list\n"
00055 " (implies option s).\n"
00056 "This application sets the following channel variable upon completion:\n"
00057 " AVAILCHAN - the name of the available channel, if one exists\n"
00058 " AVAILORIGCHAN - the canonical channel name that was used to create the channel\n"
00059 " AVAILSTATUS - the status code for the available channel\n";
00060
00061
00062 static int chanavail_exec(struct ast_channel *chan, void *data)
00063 {
00064 int res=-1, inuse=-1, option_state=0, string_compare=0;
00065 int status;
00066 char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur;
00067 struct ast_channel *tempchan;
00068 AST_DECLARE_APP_ARGS(args,
00069 AST_APP_ARG(reqchans);
00070 AST_APP_ARG(options);
00071 );
00072
00073 if (ast_strlen_zero(data)) {
00074 ast_log(LOG_WARNING, "ChanIsAvail requires an argument (DAHDI/1&DAHDI/2)\n");
00075 return -1;
00076 }
00077
00078 info = ast_strdupa(data);
00079
00080 AST_STANDARD_APP_ARGS(args, info);
00081
00082 if (args.options) {
00083 if (strchr(args.options, 's'))
00084 option_state = 1;
00085 if (strchr(args.options, 't'))
00086 string_compare = 1;
00087 }
00088 peers = args.reqchans;
00089 if (peers) {
00090 cur = peers;
00091 do {
00092
00093 rest = strchr(cur, '&');
00094 if (rest) {
00095 *rest = 0;
00096 rest++;
00097 }
00098 tech = cur;
00099 number = strchr(tech, '/');
00100 if (!number) {
00101 ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
00102 return -1;
00103 }
00104 *number = '\0';
00105 number++;
00106
00107 if (string_compare) {
00108
00109
00110
00111 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
00112 status = inuse = ast_parse_device_state(trychan);
00113 } else if (option_state) {
00114
00115
00116
00117
00118 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
00119 status = inuse = ast_device_state(trychan);
00120 }
00121 if ((inuse <= 1) && (tempchan = ast_request(tech, chan->nativeformats, number, &status))) {
00122 pbx_builtin_setvar_helper(chan, "AVAILCHAN", tempchan->name);
00123
00124 snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
00125 pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", tmp);
00126 snprintf(tmp, sizeof(tmp), "%d", status);
00127 pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
00128 ast_hangup(tempchan);
00129 tempchan = NULL;
00130 res = 1;
00131 break;
00132 } else {
00133 snprintf(tmp, sizeof(tmp), "%d", status);
00134 pbx_builtin_setvar_helper(chan, "AVAILSTATUS", tmp);
00135 }
00136 cur = rest;
00137 } while (cur);
00138 }
00139 if (res < 1) {
00140 pbx_builtin_setvar_helper(chan, "AVAILCHAN", "");
00141 pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", "");
00142 }
00143
00144 return 0;
00145 }
00146
00147 static int unload_module(void)
00148 {
00149 return ast_unregister_application(app);
00150 }
00151
00152 static int load_module(void)
00153 {
00154 return ast_register_application(app, chanavail_exec, synopsis, descrip);
00155 }
00156
00157 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Check channel availability");