| 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 |
#include "asterisk.h" |
| 33 |
|
| 34 |
#include "asterisk/module.h" |
| 35 |
#include "asterisk/channel.h" |
| 36 |
#include "asterisk/pbx.h" |
| 37 |
#include "asterisk/app.h" |
| 38 |
#include "asterisk/frame.h" |
| 39 |
#include "asterisk/stasis.h" |
| 40 |
#include "asterisk/stasis_channels.h" |
| 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 |
struct hold_intercept_data { |
| 71 |
int framehook_id; |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
static const struct ast_datastore_info hold_intercept_datastore = { |
| 76 |
.type = "hold_intercept", |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
static int remove_hold_intercept(struct ast_channel *chan) |
| 81 |
{ |
| 82 |
struct ast_datastore *datastore = NULL; |
| 83 |
struct hold_intercept_data *data; |
| 84 |
SCOPED_CHANNELLOCK(chan_lock, chan); |
| 85 |
|
| 86 |
datastore = ast_channel_datastore_find(chan, &hold_intercept_datastore, NULL); |
| 87 |
if (!datastore) { |
| 88 |
ast_log(AST_LOG_WARNING, "Cannot remove HOLD_INTERCEPT from %s: HOLD_INTERCEPT not currently enabled\n", |
| 89 |
ast_channel_name(chan)); |
| 90 |
return -1; |
| 91 |
} |
| 92 |
data = datastore->data; |
| 93 |
|
| 94 |
if (ast_framehook_detach(chan, data->framehook_id)) { |
| 95 |
ast_log(AST_LOG_WARNING, "Failed to remove HOLD_INTERCEPT framehook from channel %s\n", |
| 96 |
ast_channel_name(chan)); |
| 97 |
return -1; |
| 98 |
} |
| 99 |
|
| 100 |
if (ast_channel_datastore_remove(chan, datastore)) { |
| 101 |
ast_log(AST_LOG_WARNING, "Failed to remove HOLD_INTERCEPT datastore from channel %s\n", |
| 102 |
ast_channel_name(chan)); |
| 103 |
return -1; |
| 104 |
} |
| 105 |
ast_datastore_free(datastore); |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
static struct ast_frame *hold_intercept_framehook(struct ast_channel *chan, |
| 112 |
struct ast_frame *f, enum ast_framehook_event event, void *data) |
| 113 |
{ |
| 114 |
int frame_type; |
| 115 |
|
| 116 |
if (!f || (event != AST_FRAMEHOOK_EVENT_WRITE)) { |
| 117 |
return f; |
| 118 |
} |
| 119 |
|
| 120 |
if (f->frametype != AST_FRAME_CONTROL) { |
| 121 |
return f; |
| 122 |
} |
| 123 |
|
| 124 |
frame_type = f->subclass.integer; |
| 125 |
if (frame_type != AST_CONTROL_HOLD && frame_type != AST_CONTROL_UNHOLD) { |
| 126 |
return f; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
ast_frfree(f); |
| 131 |
f = &ast_null_frame; |
| 132 |
|
| 133 |
ast_channel_publish_cached_blob(chan, |
| 134 |
frame_type == AST_CONTROL_HOLD ? ast_channel_hold_type() : ast_channel_unhold_type(), |
| 135 |
NULL); |
| 136 |
|
| 137 |
return f; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
static int hold_intercept_framehook_consume(void *data, enum ast_frame_type type) |
| 142 |
{ |
| 143 |
return (type == AST_FRAME_CONTROL ? 1 : 0); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
static int set_hold_intercept(struct ast_channel *chan) |
| 148 |
{ |
| 149 |
struct ast_datastore *datastore; |
| 150 |
struct hold_intercept_data *data; |
| 151 |
static struct ast_framehook_interface hold_framehook_interface = { |
| 152 |
.version = AST_FRAMEHOOK_INTERFACE_VERSION, |
| 153 |
.event_cb = hold_intercept_framehook, |
| 154 |
.consume_cb = hold_intercept_framehook_consume, |
| 155 |
.disable_inheritance = 1, |
| 156 |
}; |
| 157 |
SCOPED_CHANNELLOCK(chan_lock, chan); |
| 158 |
|
| 159 |
datastore = ast_channel_datastore_find(chan, &hold_intercept_datastore, NULL); |
| 160 |
if (datastore) { |
| 161 |
ast_log(AST_LOG_WARNING, "HOLD_INTERCEPT already set on '%s'\n", |
| 162 |
ast_channel_name(chan)); |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
datastore = ast_datastore_alloc(&hold_intercept_datastore, NULL); |
| 167 |
if (!datastore) { |
| 168 |
return -1; |
| 169 |
} |
| 170 |
|
| 171 |
data = ast_calloc(1, sizeof(*data)); |
| 172 |
if (!data) { |
| 173 |
ast_datastore_free(datastore); |
| 174 |
return -1; |
| 175 |
} |
| 176 |
|
| 177 |
data->framehook_id = ast_framehook_attach(chan, &hold_framehook_interface); |
| 178 |
if (data->framehook_id < 0) { |
| 179 |
ast_log(AST_LOG_WARNING, "Failed to attach HOLD_INTERCEPT framehook to '%s'\n", |
| 180 |
ast_channel_name(chan)); |
| 181 |
ast_datastore_free(datastore); |
| 182 |
ast_free(data); |
| 183 |
return -1; |
| 184 |
} |
| 185 |
datastore->data = data; |
| 186 |
|
| 187 |
ast_channel_datastore_add(chan, datastore); |
| 188 |
|
| 189 |
return 0; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
static int hold_intercept_fn_write(struct ast_channel *chan, const char *function, |
| 194 |
char *data, const char *value) |
| 195 |
{ |
| 196 |
int res; |
| 197 |
|
| 198 |
if (!chan) { |
| 199 |
return -1; |
| 200 |
} |
| 201 |
|
| 202 |
if (ast_strlen_zero(data)) { |
| 203 |
ast_log(AST_LOG_WARNING, "HOLD_INTERCEPT requires an argument\n"); |
| 204 |
return -1; |
| 205 |
} |
| 206 |
|
| 207 |
if (!strcasecmp(data, "set")) { |
| 208 |
res = set_hold_intercept(chan); |
| 209 |
} else if (!strcasecmp(data, "remove")) { |
| 210 |
res = remove_hold_intercept(chan); |
| 211 |
} else { |
| 212 |
ast_log(AST_LOG_WARNING, "HOLD_INTERCEPT: unknown option %s\n", data); |
| 213 |
res = -1; |
| 214 |
} |
| 215 |
|
| 216 |
return res; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
static struct ast_custom_function hold_intercept_function = { |
| 221 |
.name = "HOLD_INTERCEPT", |
| 222 |
.write = hold_intercept_fn_write, |
| 223 |
}; |
| 224 |
|
| 225 |
|
| 226 |
static int unload_module(void) |
| 227 |
{ |
| 228 |
return ast_custom_function_unregister(&hold_intercept_function); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
static int load_module(void) |
| 233 |
{ |
| 234 |
return ast_custom_function_register(&hold_intercept_function) ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS; |
| 235 |
} |
| 236 |
|
| 237 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Hold interception dialplan function"); |