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
00031
00032
00033
00034 #include "asterisk.h"
00035
00036 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 328209 $")
00037
00038 #include <sys/ioctl.h>
00039
00040 #include "asterisk/lock.h"
00041 #include "asterisk/file.h"
00042 #include "asterisk/channel.h"
00043 #include "asterisk/pbx.h"
00044 #include "asterisk/module.h"
00045 #include "asterisk/app.h"
00046 #include "asterisk/devicestate.h"
00047
00048 static const char app[] = "ChanIsAvail";
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 static int chanavail_exec(struct ast_channel *chan, const char *data)
00103 {
00104 int inuse=-1, option_state=0, string_compare=0, option_all_avail=0;
00105 int status;
00106 char *info, tmp[512], trychan[512], *peers, *tech, *number, *rest, *cur;
00107 struct ast_str *tmp_availchan = ast_str_alloca(2048);
00108 struct ast_str *tmp_availorig = ast_str_alloca(2048);
00109 struct ast_str *tmp_availstat = ast_str_alloca(2048);
00110 struct ast_str *tmp_availcause = ast_str_alloca(2048);
00111 struct ast_channel *tempchan;
00112 AST_DECLARE_APP_ARGS(args,
00113 AST_APP_ARG(reqchans);
00114 AST_APP_ARG(options);
00115 );
00116
00117 if (ast_strlen_zero(data)) {
00118 ast_log(LOG_WARNING, "ChanIsAvail requires an argument (DAHDI/1&DAHDI/2)\n");
00119 return -1;
00120 }
00121
00122 info = ast_strdupa(data);
00123
00124 AST_STANDARD_APP_ARGS(args, info);
00125
00126 if (args.options) {
00127 if (strchr(args.options, 'a')) {
00128 option_all_avail = 1;
00129 }
00130 if (strchr(args.options, 's')) {
00131 option_state = 1;
00132 }
00133 if (strchr(args.options, 't')) {
00134 string_compare = 1;
00135 }
00136 }
00137 peers = args.reqchans;
00138 if (peers) {
00139 cur = peers;
00140 do {
00141
00142 rest = strchr(cur, '&');
00143 if (rest) {
00144 *rest = 0;
00145 rest++;
00146 }
00147 tech = cur;
00148 number = strchr(tech, '/');
00149 if (!number) {
00150 ast_log(LOG_WARNING, "ChanIsAvail argument takes format ([technology]/[device])\n");
00151 return -1;
00152 }
00153 *number = '\0';
00154 number++;
00155
00156 if (string_compare) {
00157
00158
00159
00160 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
00161 status = inuse = ast_parse_device_state(trychan);
00162 } else if (option_state) {
00163
00164
00165
00166
00167 snprintf(trychan, sizeof(trychan), "%s/%s",cur,number);
00168 status = inuse = ast_device_state(trychan);
00169 }
00170 snprintf(tmp, sizeof(tmp), "%d", status);
00171 ast_str_append(&tmp_availstat, 0, "%s%s", ast_str_strlen(tmp_availstat) ? "&" : "", tmp);
00172 if ((inuse <= 1) && (tempchan = ast_request(tech, chan->nativeformats, chan, number, &status))) {
00173 ast_str_append(&tmp_availchan, 0, "%s%s", ast_str_strlen(tmp_availchan) ? "&" : "", tempchan->name);
00174
00175 snprintf(tmp, sizeof(tmp), "%s/%s", tech, number);
00176 ast_str_append(&tmp_availorig, 0, "%s%s", ast_str_strlen(tmp_availorig) ? "&" : "", tmp);
00177
00178 snprintf(tmp, sizeof(tmp), "%d", status);
00179 ast_str_append(&tmp_availcause, 0, "%s%s", ast_str_strlen(tmp_availcause) ? "&" : "", tmp);
00180
00181 ast_hangup(tempchan);
00182 tempchan = NULL;
00183
00184 if (!option_all_avail) {
00185 break;
00186 }
00187 }
00188 cur = rest;
00189 } while (cur);
00190 }
00191
00192 pbx_builtin_setvar_helper(chan, "AVAILCHAN", ast_str_buffer(tmp_availchan));
00193
00194 pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", ast_str_buffer(tmp_availorig));
00195 pbx_builtin_setvar_helper(chan, "AVAILSTATUS", ast_str_buffer(tmp_availstat));
00196 pbx_builtin_setvar_helper(chan, "AVAILCAUSECODE", ast_str_buffer(tmp_availcause));
00197
00198 return 0;
00199 }
00200
00201 static int unload_module(void)
00202 {
00203 return ast_unregister_application(app);
00204 }
00205
00206 static int load_module(void)
00207 {
00208 return ast_register_application_xml(app, chanavail_exec) ?
00209 AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
00210 }
00211
00212 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Check channel availability");