| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#include "asterisk.h" |
| 35 |
|
| 36 |
#include <sys/ioctl.h> |
| 37 |
|
| 38 |
#include "asterisk/lock.h" |
| 39 |
#include "asterisk/file.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 |
|
| 46 |
static const char app[] = "ChanIsAvail"; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
static int chanavail_exec(struct ast_channel *chan, const char *data) |
| 106 |
{ |
| 107 |
int inuse = -1; |
| 108 |
int option_state = 0; |
| 109 |
int string_compare = 0; |
| 110 |
int option_all_avail = 0; |
| 111 |
int status; |
| 112 |
char *info; |
| 113 |
char trychan[512]; |
| 114 |
char *rest; |
| 115 |
char *tech; |
| 116 |
char *number; |
| 117 |
struct ast_str *tmp_availchan = ast_str_alloca(2048); |
| 118 |
struct ast_str *tmp_availorig = ast_str_alloca(2048); |
| 119 |
struct ast_str *tmp_availstat = ast_str_alloca(2048); |
| 120 |
struct ast_str *tmp_availcause = ast_str_alloca(2048); |
| 121 |
struct ast_channel *tempchan; |
| 122 |
struct ast_custom_function *cdr_prop_func = ast_custom_function_find("CDR_PROP"); |
| 123 |
struct ast_format_cap *caps = NULL; |
| 124 |
AST_DECLARE_APP_ARGS(args, |
| 125 |
AST_APP_ARG(reqchans); |
| 126 |
AST_APP_ARG(options); |
| 127 |
); |
| 128 |
|
| 129 |
info = ast_strdupa(data ?: ""); |
| 130 |
|
| 131 |
AST_STANDARD_APP_ARGS(args, info); |
| 132 |
|
| 133 |
ao2_lock(chan); |
| 134 |
caps = ao2_bump(ast_channel_nativeformats(chan)); |
| 135 |
ao2_unlock(chan); |
| 136 |
|
| 137 |
if (args.options) { |
| 138 |
if (strchr(args.options, 'a')) { |
| 139 |
option_all_avail = 1; |
| 140 |
} |
| 141 |
if (strchr(args.options, 's')) { |
| 142 |
option_state = 1; |
| 143 |
} |
| 144 |
if (strchr(args.options, 't')) { |
| 145 |
string_compare = 1; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
rest = args.reqchans; |
| 150 |
if (!rest) { |
| 151 |
rest = ""; |
| 152 |
} |
| 153 |
while ((tech = strsep(&rest, "&"))) { |
| 154 |
tech = ast_strip(tech); |
| 155 |
|
| 156 |
number = strchr(tech, '/'); |
| 157 |
if (!number) { |
| 158 |
if (!ast_strlen_zero(tech)) { |
| 159 |
ast_log(LOG_WARNING, "Invalid ChanIsAvail technology/resource argument: '%s'\n", |
| 160 |
tech); |
| 161 |
} |
| 162 |
|
| 163 |
ast_str_append(&tmp_availstat, 0, "%s%d", |
| 164 |
ast_str_strlen(tmp_availstat) ? "&" : "", AST_DEVICE_INVALID); |
| 165 |
continue; |
| 166 |
} |
| 167 |
*number++ = '\0'; |
| 168 |
|
| 169 |
status = AST_DEVICE_UNKNOWN; |
| 170 |
|
| 171 |
if (string_compare) { |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
snprintf(trychan, sizeof(trychan), "%s/%s", tech, number); |
| 176 |
status = inuse = ast_parse_device_state(trychan); |
| 177 |
} else if (option_state) { |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
snprintf(trychan, sizeof(trychan), "%s/%s", tech, number); |
| 183 |
status = inuse = ast_device_state(trychan); |
| 184 |
} |
| 185 |
ast_str_append(&tmp_availstat, 0, "%s%d", ast_str_strlen(tmp_availstat) ? "&" : "", status); |
| 186 |
|
| 187 |
if ((inuse <= (int) AST_DEVICE_NOT_INUSE) |
| 188 |
&& (tempchan = ast_request(tech, caps, NULL, chan, number, &status))) { |
| 189 |
|
| 190 |
ast_str_append(&tmp_availchan, 0, "%s%s", |
| 191 |
ast_str_strlen(tmp_availchan) ? "&" : "", ast_channel_name(tempchan)); |
| 192 |
|
| 193 |
ast_str_append(&tmp_availorig, 0, "%s%s/%s", |
| 194 |
ast_str_strlen(tmp_availorig) ? "&" : "", tech, number); |
| 195 |
|
| 196 |
ast_str_append(&tmp_availcause, 0, "%s%d", |
| 197 |
ast_str_strlen(tmp_availcause) ? "&" : "", status); |
| 198 |
|
| 199 |
|
| 200 |
if (cdr_prop_func) { |
| 201 |
ast_func_write(tempchan, "CDR_PROP(disable)", "1"); |
| 202 |
} |
| 203 |
|
| 204 |
ast_hangup(tempchan); |
| 205 |
tempchan = NULL; |
| 206 |
|
| 207 |
if (!option_all_avail) { |
| 208 |
break; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
ao2_cleanup(caps); |
| 215 |
|
| 216 |
pbx_builtin_setvar_helper(chan, "AVAILCHAN", ast_str_buffer(tmp_availchan)); |
| 217 |
|
| 218 |
pbx_builtin_setvar_helper(chan, "AVAILORIGCHAN", ast_str_buffer(tmp_availorig)); |
| 219 |
pbx_builtin_setvar_helper(chan, "AVAILSTATUS", ast_str_buffer(tmp_availstat)); |
| 220 |
pbx_builtin_setvar_helper(chan, "AVAILCAUSECODE", ast_str_buffer(tmp_availcause)); |
| 221 |
|
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
static int unload_module(void) |
| 226 |
{ |
| 227 |
return ast_unregister_application(app); |
| 228 |
} |
| 229 |
|
| 230 |
static int load_module(void) |
| 231 |
{ |
| 232 |
return ast_register_application_xml(app, chanavail_exec) ? |
| 233 |
AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS; |
| 234 |
} |
| 235 |
|
| 236 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Check channel availability", |
| 237 |
.support_level = AST_MODULE_SUPPORT_EXTENDED, |
| 238 |
.load = load_module, |
| 239 |
.unload = unload_module, |
| 240 |
.optional_modules = "func_cdr" |
| 241 |
); |