| 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 |
#include "asterisk.h" |
| 32 |
|
| 33 |
#include "asterisk/module.h" |
| 34 |
#include "asterisk/channel.h" |
| 35 |
#include "asterisk/pbx.h" |
| 36 |
#include "asterisk/utils.h" |
| 37 |
#include "asterisk/app.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
static int timeout_read(struct ast_channel *chan, const char *cmd, char *data, |
| 79 |
char *buf, size_t len) |
| 80 |
{ |
| 81 |
struct timeval myt; |
| 82 |
|
| 83 |
if (!chan) |
| 84 |
return -1; |
| 85 |
|
| 86 |
if (!data) { |
| 87 |
ast_log(LOG_ERROR, "Must specify type of timeout to get.\n"); |
| 88 |
return -1; |
| 89 |
} |
| 90 |
|
| 91 |
switch (*data) { |
| 92 |
case 'a': |
| 93 |
case 'A': |
| 94 |
if (ast_tvzero(*ast_channel_whentohangup(chan))) { |
| 95 |
ast_copy_string(buf, "0", len); |
| 96 |
} else { |
| 97 |
myt = ast_tvnow(); |
| 98 |
snprintf(buf, len, "%.3f", ast_tvdiff_ms(*ast_channel_whentohangup(chan), myt) / 1000.0); |
| 99 |
} |
| 100 |
break; |
| 101 |
|
| 102 |
case 'r': |
| 103 |
case 'R': |
| 104 |
if (ast_channel_pbx(chan)) { |
| 105 |
snprintf(buf, len, "%.3f", ast_channel_pbx(chan)->rtimeoutms / 1000.0); |
| 106 |
} |
| 107 |
break; |
| 108 |
|
| 109 |
case 'd': |
| 110 |
case 'D': |
| 111 |
if (ast_channel_pbx(chan)) { |
| 112 |
snprintf(buf, len, "%.3f", ast_channel_pbx(chan)->dtimeoutms / 1000.0); |
| 113 |
} |
| 114 |
break; |
| 115 |
|
| 116 |
default: |
| 117 |
ast_log(LOG_ERROR, "Unknown timeout type specified.\n"); |
| 118 |
return -1; |
| 119 |
} |
| 120 |
|
| 121 |
return 0; |
| 122 |
} |
| 123 |
|
| 124 |
static int timeout_write(struct ast_channel *chan, const char *cmd, char *data, |
| 125 |
const char *value) |
| 126 |
{ |
| 127 |
double x = 0.0; |
| 128 |
long sec = 0L; |
| 129 |
char timestr[64]; |
| 130 |
struct ast_tm myt; |
| 131 |
struct timeval when = {0,}; |
| 132 |
int res; |
| 133 |
|
| 134 |
if (!chan) |
| 135 |
return -1; |
| 136 |
|
| 137 |
if (!data) { |
| 138 |
ast_log(LOG_ERROR, "Must specify type of timeout to set.\n"); |
| 139 |
return -1; |
| 140 |
} |
| 141 |
|
| 142 |
if (!value) |
| 143 |
return -1; |
| 144 |
|
| 145 |
res = sscanf(value, "%30ld%30lf", &sec, &x); |
| 146 |
if (res == 0 || sec < 0) { |
| 147 |
when.tv_sec = 0; |
| 148 |
when.tv_usec = 0; |
| 149 |
} else if (res == 1) { |
| 150 |
when.tv_sec = sec; |
| 151 |
} else if (res == 2) { |
| 152 |
when.tv_sec = sec; |
| 153 |
when.tv_usec = x * 1000000; |
| 154 |
} |
| 155 |
|
| 156 |
switch (*data) { |
| 157 |
case 'a': |
| 158 |
case 'A': |
| 159 |
ast_channel_lock(chan); |
| 160 |
ast_channel_setwhentohangup_tv(chan, when); |
| 161 |
ast_channel_unlock(chan); |
| 162 |
if (VERBOSITY_ATLEAST(3)) { |
| 163 |
if (!ast_tvzero(*ast_channel_whentohangup(chan))) { |
| 164 |
when = ast_tvadd(when, ast_tvnow()); |
| 165 |
ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S.%3q %Z", |
| 166 |
ast_localtime(&when, &myt, NULL)); |
| 167 |
ast_verb(3, "Channel will hangup at %s.\n", timestr); |
| 168 |
} else { |
| 169 |
ast_verb(3, "Channel hangup cancelled.\n"); |
| 170 |
} |
| 171 |
} |
| 172 |
break; |
| 173 |
|
| 174 |
case 'r': |
| 175 |
case 'R': |
| 176 |
if (ast_channel_pbx(chan)) { |
| 177 |
ast_channel_pbx(chan)->rtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000; |
| 178 |
ast_verb(3, "Response timeout set to %.3f\n", ast_channel_pbx(chan)->rtimeoutms / 1000.0); |
| 179 |
} |
| 180 |
break; |
| 181 |
|
| 182 |
case 'd': |
| 183 |
case 'D': |
| 184 |
if (ast_channel_pbx(chan)) { |
| 185 |
ast_channel_pbx(chan)->dtimeoutms = when.tv_sec * 1000 + when.tv_usec / 1000; |
| 186 |
ast_verb(3, "Digit timeout set to %.3f\n", ast_channel_pbx(chan)->dtimeoutms / 1000.0); |
| 187 |
} |
| 188 |
break; |
| 189 |
|
| 190 |
default: |
| 191 |
ast_log(LOG_ERROR, "Unknown timeout type specified.\n"); |
| 192 |
break; |
| 193 |
} |
| 194 |
|
| 195 |
return 0; |
| 196 |
} |
| 197 |
|
| 198 |
static struct ast_custom_function timeout_function = { |
| 199 |
.name = "TIMEOUT", |
| 200 |
.read = timeout_read, |
| 201 |
.read_max = 22, |
| 202 |
.write = timeout_write, |
| 203 |
}; |
| 204 |
|
| 205 |
static int unload_module(void) |
| 206 |
{ |
| 207 |
return ast_custom_function_unregister(&timeout_function); |
| 208 |
} |
| 209 |
|
| 210 |
static int load_module(void) |
| 211 |
{ |
| 212 |
return ast_custom_function_register(&timeout_function); |
| 213 |
} |
| 214 |
|
| 215 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel timeout dialplan functions"); |