| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#include "asterisk.h" |
| 25 |
|
| 26 |
#include "asterisk/astobj2.h" |
| 27 |
#include "asterisk/module.h" |
| 28 |
#include "asterisk/sorcery.h" |
| 29 |
#include "asterisk/cli.h" |
| 30 |
#include "asterisk/format.h" |
| 31 |
#include "asterisk/format_cap.h" |
| 32 |
#include "asterisk/res_aeap.h" |
| 33 |
|
| 34 |
#include "res_aeap/general.h" |
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
static struct ast_sorcery *aeap_sorcery; |
| 86 |
|
| 87 |
struct ast_sorcery *ast_aeap_sorcery(void) { |
| 88 |
return aeap_sorcery; |
| 89 |
} |
| 90 |
|
| 91 |
struct ast_aeap_client_config |
| 92 |
{ |
| 93 |
SORCERY_OBJECT(details); |
| 94 |
AST_DECLARE_STRING_FIELDS( |
| 95 |
|
| 96 |
AST_STRING_FIELD(url); |
| 97 |
|
| 98 |
AST_STRING_FIELD(protocol); |
| 99 |
); |
| 100 |
|
| 101 |
struct ast_format_cap *codecs; |
| 102 |
}; |
| 103 |
|
| 104 |
static void client_config_destructor(void *obj) |
| 105 |
{ |
| 106 |
struct ast_aeap_client_config *cfg = obj; |
| 107 |
|
| 108 |
ast_string_field_free_memory(cfg); |
| 109 |
ao2_cleanup(cfg->codecs); |
| 110 |
} |
| 111 |
|
| 112 |
static void *client_config_alloc(const char *name) |
| 113 |
{ |
| 114 |
struct ast_aeap_client_config *cfg; |
| 115 |
|
| 116 |
cfg = ast_sorcery_generic_alloc(sizeof(*cfg), client_config_destructor); |
| 117 |
if (!cfg) { |
| 118 |
return NULL; |
| 119 |
} |
| 120 |
|
| 121 |
if (ast_string_field_init(cfg, 512)) { |
| 122 |
ao2_ref(cfg, -1); |
| 123 |
return NULL; |
| 124 |
} |
| 125 |
|
| 126 |
if (!(cfg->codecs = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) { |
| 127 |
ao2_ref(cfg, -1); |
| 128 |
return NULL; |
| 129 |
} |
| 130 |
|
| 131 |
return cfg; |
| 132 |
} |
| 133 |
|
| 134 |
static int client_config_apply(const struct ast_sorcery *sorcery, void *obj) |
| 135 |
{ |
| 136 |
struct ast_aeap_client_config *cfg = obj; |
| 137 |
|
| 138 |
if (ast_strlen_zero(cfg->url)) { |
| 139 |
ast_log(LOG_ERROR, "AEAP - URL must be present for '%s'\n", ast_sorcery_object_get_id(cfg)); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
|
| 143 |
if (!ast_begins_with(cfg->url, "ws")) { |
| 144 |
ast_log(LOG_ERROR, "AEAP - URL must be ws or wss for '%s'\n", ast_sorcery_object_get_id(cfg)); |
| 145 |
return -1; |
| 146 |
} |
| 147 |
|
| 148 |
return 0; |
| 149 |
} |
| 150 |
|
| 151 |
const struct ast_format_cap *ast_aeap_client_config_codecs(const struct ast_aeap_client_config *cfg) |
| 152 |
{ |
| 153 |
return cfg->codecs; |
| 154 |
} |
| 155 |
|
| 156 |
int ast_aeap_client_config_has_protocol(const struct ast_aeap_client_config *cfg, |
| 157 |
const char *protocol) |
| 158 |
{ |
| 159 |
return !strcmp(protocol, cfg->protocol); |
| 160 |
} |
| 161 |
|
| 162 |
struct ao2_container *ast_aeap_client_configs_get(const char *protocol) |
| 163 |
{ |
| 164 |
struct ao2_container *container; |
| 165 |
struct ast_variable *var; |
| 166 |
|
| 167 |
var = protocol ? ast_variable_new("protocol ==", protocol, "") : NULL; |
| 168 |
|
| 169 |
container = ast_sorcery_retrieve_by_fields(aeap_sorcery, |
| 170 |
AEAP_CONFIG_CLIENT, AST_RETRIEVE_FLAG_MULTIPLE | AST_RETRIEVE_FLAG_ALL, var); |
| 171 |
|
| 172 |
ast_variables_destroy(var); |
| 173 |
|
| 174 |
return container; |
| 175 |
} |
| 176 |
|
| 177 |
static struct ast_aeap_client_config *client_config_get(const char *id) |
| 178 |
{ |
| 179 |
return ast_sorcery_retrieve_by_id(aeap_sorcery, AEAP_CONFIG_CLIENT, id); |
| 180 |
} |
| 181 |
|
| 182 |
static char *aeap_tab_complete_name(const char *word, struct ao2_container *container) |
| 183 |
{ |
| 184 |
void *obj; |
| 185 |
struct ao2_iterator it; |
| 186 |
int wordlen = strlen(word); |
| 187 |
int ret; |
| 188 |
|
| 189 |
it = ao2_iterator_init(container, 0); |
| 190 |
while ((obj = ao2_iterator_next(&it))) { |
| 191 |
if (!strncasecmp(word, ast_sorcery_object_get_id(obj), wordlen)) { |
| 192 |
ret = ast_cli_completion_add(ast_strdup(ast_sorcery_object_get_id(obj))); |
| 193 |
if (ret) { |
| 194 |
ao2_ref(obj, -1); |
| 195 |
break; |
| 196 |
} |
| 197 |
} |
| 198 |
ao2_ref(obj, -1); |
| 199 |
} |
| 200 |
ao2_iterator_destroy(&it); |
| 201 |
|
| 202 |
ao2_ref(container, -1); |
| 203 |
|
| 204 |
return NULL; |
| 205 |
} |
| 206 |
|
| 207 |
static int aeap_cli_show(void *obj, void *arg, int flags) |
| 208 |
{ |
| 209 |
struct ast_cli_args *a = arg; |
| 210 |
struct ast_variable *options; |
| 211 |
struct ast_variable *i; |
| 212 |
|
| 213 |
if (!obj) { |
| 214 |
ast_cli(a->fd, "No AEAP configuration found\n"); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
options = ast_variable_list_sort(ast_sorcery_objectset_create(aeap_sorcery, obj)); |
| 219 |
if (!options) { |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
ast_cli(a->fd, "%s: %s\n", ast_sorcery_object_get_type(obj), |
| 224 |
ast_sorcery_object_get_id(obj)); |
| 225 |
|
| 226 |
for (i = options; i; i = i->next) { |
| 227 |
ast_cli(a->fd, "\t%s: %s\n", i->name, i->value); |
| 228 |
} |
| 229 |
|
| 230 |
ast_cli(a->fd, "\n"); |
| 231 |
|
| 232 |
ast_variables_destroy(options); |
| 233 |
|
| 234 |
return 0; |
| 235 |
} |
| 236 |
|
| 237 |
static char *client_config_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 238 |
{ |
| 239 |
struct ast_aeap_client_config *cfg; |
| 240 |
|
| 241 |
switch(cmd) { |
| 242 |
case CLI_INIT: |
| 243 |
e->command = "aeap show client"; |
| 244 |
e->usage = |
| 245 |
"Usage: aeap show client <id>\n" |
| 246 |
" Show the AEAP settings for a given client\n"; |
| 247 |
return NULL; |
| 248 |
case CLI_GENERATE: |
| 249 |
if (a->pos == 3) { |
| 250 |
return aeap_tab_complete_name(a->word, ast_aeap_client_configs_get(NULL)); |
| 251 |
} else { |
| 252 |
return NULL; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
if (a->argc != 4) { |
| 257 |
return CLI_SHOWUSAGE; |
| 258 |
} |
| 259 |
|
| 260 |
cfg = client_config_get(a->argv[3]); |
| 261 |
aeap_cli_show(cfg, a, 0); |
| 262 |
ao2_cleanup(cfg); |
| 263 |
|
| 264 |
return CLI_SUCCESS; |
| 265 |
} |
| 266 |
|
| 267 |
static char *client_config_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 268 |
{ |
| 269 |
struct ao2_container *container; |
| 270 |
|
| 271 |
switch(cmd) { |
| 272 |
case CLI_INIT: |
| 273 |
e->command = "aeap show clients"; |
| 274 |
e->usage = |
| 275 |
"Usage: aeap show clients\n" |
| 276 |
" Show all configured AEAP clients\n"; |
| 277 |
return NULL; |
| 278 |
case CLI_GENERATE: |
| 279 |
return NULL; |
| 280 |
} |
| 281 |
|
| 282 |
if (a->argc != 3) { |
| 283 |
return CLI_SHOWUSAGE; |
| 284 |
} |
| 285 |
|
| 286 |
container = ast_aeap_client_configs_get(NULL); |
| 287 |
if (!container || ao2_container_count(container) == 0) { |
| 288 |
ast_cli(a->fd, "No AEAP clients found\n"); |
| 289 |
ao2_cleanup(container); |
| 290 |
return CLI_SUCCESS; |
| 291 |
} |
| 292 |
|
| 293 |
ao2_callback(container, OBJ_NODATA, aeap_cli_show, a); |
| 294 |
ao2_ref(container, -1); |
| 295 |
|
| 296 |
return CLI_SUCCESS; |
| 297 |
} |
| 298 |
|
| 299 |
static struct ast_cli_entry aeap_cli[] = { |
| 300 |
AST_CLI_DEFINE(client_config_show, "Show AEAP client configuration by id"), |
| 301 |
AST_CLI_DEFINE(client_config_show_all, "Show all AEAP client configurations"), |
| 302 |
}; |
| 303 |
|
| 304 |
static struct ast_aeap *aeap_create(const char *id, const struct ast_aeap_params *params, |
| 305 |
int connect, int timeout) |
| 306 |
{ |
| 307 |
struct ast_aeap_client_config *cfg; |
| 308 |
struct ast_aeap *aeap; |
| 309 |
const char *url = NULL; |
| 310 |
const char *protocol = NULL; |
| 311 |
|
| 312 |
cfg = client_config_get(id); |
| 313 |
if (cfg) { |
| 314 |
url = cfg->url; |
| 315 |
protocol = cfg->protocol; |
| 316 |
} |
| 317 |
|
| 318 |
#ifdef TEST_FRAMEWORK |
| 319 |
else if (ast_begins_with(id, "_aeap_test_")) { |
| 320 |
url = "ws://127.0.0.1:8088/ws"; |
| 321 |
protocol = id; |
| 322 |
} |
| 323 |
#endif |
| 324 |
|
| 325 |
if (!url && !protocol) { |
| 326 |
ast_log(LOG_ERROR, "AEAP: unable to get configuration for '%s'\n", id); |
| 327 |
return NULL; |
| 328 |
} |
| 329 |
|
| 330 |
aeap = connect ? ast_aeap_create_and_connect(url, params, url, protocol, timeout) : |
| 331 |
ast_aeap_create(url, params); |
| 332 |
|
| 333 |
ao2_cleanup(cfg); |
| 334 |
return aeap; |
| 335 |
} |
| 336 |
|
| 337 |
struct ast_aeap *ast_aeap_create_by_id(const char *id, const struct ast_aeap_params *params) |
| 338 |
{ |
| 339 |
return aeap_create(id, params, 0, 0); |
| 340 |
} |
| 341 |
|
| 342 |
struct ast_aeap *ast_aeap_create_and_connect_by_id(const char *id, |
| 343 |
const struct ast_aeap_params *params, int timeout) |
| 344 |
{ |
| 345 |
return aeap_create(id, params, 1, timeout); |
| 346 |
} |
| 347 |
|
| 348 |
struct ast_variable *ast_aeap_custom_fields_get(const char *id) |
| 349 |
{ |
| 350 |
struct ast_aeap_client_config *cfg; |
| 351 |
struct ast_variable *vars; |
| 352 |
|
| 353 |
cfg = client_config_get(id); |
| 354 |
if (!cfg) { |
| 355 |
ast_log(LOG_WARNING, "AEAP: no client configuration '%s' to get fields\n", id); |
| 356 |
return NULL; |
| 357 |
} |
| 358 |
|
| 359 |
vars = ast_sorcery_objectset_create(aeap_sorcery, cfg); |
| 360 |
|
| 361 |
ao2_ref(cfg, -1); |
| 362 |
return vars; |
| 363 |
} |
| 364 |
|
| 365 |
static int reload_module(void) |
| 366 |
{ |
| 367 |
ast_sorcery_reload(aeap_sorcery); |
| 368 |
|
| 369 |
return 0; |
| 370 |
} |
| 371 |
|
| 372 |
static int unload_module(void) |
| 373 |
{ |
| 374 |
ast_sorcery_unref(aeap_sorcery); |
| 375 |
aeap_sorcery = NULL; |
| 376 |
|
| 377 |
ast_cli_unregister_multiple(aeap_cli, ARRAY_LEN(aeap_cli)); |
| 378 |
|
| 379 |
aeap_general_finalize(); |
| 380 |
|
| 381 |
return 0; |
| 382 |
} |
| 383 |
|
| 384 |
static int load_module(void) |
| 385 |
{ |
| 386 |
if (aeap_general_initialize()) { |
| 387 |
return AST_MODULE_LOAD_DECLINE; |
| 388 |
} |
| 389 |
|
| 390 |
if (!(aeap_sorcery = ast_sorcery_open())) |
| 391 |
{ |
| 392 |
ast_log(LOG_ERROR, "AEAP - failed to open sorcery\n"); |
| 393 |
return AST_MODULE_LOAD_DECLINE; |
| 394 |
} |
| 395 |
|
| 396 |
ast_sorcery_apply_default(aeap_sorcery, AEAP_CONFIG_CLIENT, "config", "aeap.conf,criteria=type=client"); |
| 397 |
|
| 398 |
if (ast_sorcery_object_register(aeap_sorcery, "client", client_config_alloc, |
| 399 |
NULL, client_config_apply)) { |
| 400 |
ast_log(LOG_ERROR, "AEAP - failed to register client sorcery object\n"); |
| 401 |
return AST_MODULE_LOAD_DECLINE; |
| 402 |
} |
| 403 |
|
| 404 |
ast_sorcery_object_field_register(aeap_sorcery, AEAP_CONFIG_CLIENT, "type", "", OPT_NOOP_T, 0, 0); |
| 405 |
ast_sorcery_object_field_register(aeap_sorcery, AEAP_CONFIG_CLIENT, "url", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_aeap_client_config, url)); |
| 406 |
ast_sorcery_object_field_register(aeap_sorcery, AEAP_CONFIG_CLIENT, "protocol", "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_aeap_client_config, protocol)); |
| 407 |
ast_sorcery_object_field_register(aeap_sorcery, AEAP_CONFIG_CLIENT, "codecs", "", OPT_CODEC_T, 1, FLDSET(struct ast_aeap_client_config, codecs)); |
| 408 |
|
| 409 |
ast_sorcery_load(aeap_sorcery); |
| 410 |
|
| 411 |
ast_cli_register_multiple(aeap_cli, ARRAY_LEN(aeap_cli)); |
| 412 |
|
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, |
| 417 |
"Asterisk External Application Protocol Module for Asterisk", |
| 418 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 419 |
.load = load_module, |
| 420 |
.unload = unload_module, |
| 421 |
.reload = reload_module, |
| 422 |
.load_pri = AST_MODPRI_CHANNEL_DEPEND, |
| 423 |
.requires = "res_http_websocket", |
| 424 |
); |