| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
#include "asterisk.h" |
| 38 |
|
| 39 |
#include "asterisk/module.h" |
| 40 |
#include "asterisk/channel.h" |
| 41 |
#include "asterisk/pbx.h" |
| 42 |
#include "asterisk/utils.h" |
| 43 |
#include "asterisk/linkedlists.h" |
| 44 |
#include "asterisk/devicestate.h" |
| 45 |
#include "asterisk/cli.h" |
| 46 |
#include "asterisk/astdb.h" |
| 47 |
#include "asterisk/app.h" |
| 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 |
|
| 106 |
|
| 107 |
|
| 108 |
static const char astdb_family[] = "CustomDevstate"; |
| 109 |
|
| 110 |
static int devstate_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 111 |
{ |
| 112 |
ast_copy_string(buf, ast_devstate_str(ast_device_state(data)), len); |
| 113 |
|
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
static int devstate_write(struct ast_channel *chan, const char *cmd, char *data, const char *value) |
| 118 |
{ |
| 119 |
size_t len = strlen("Custom:"); |
| 120 |
enum ast_device_state state_val; |
| 121 |
|
| 122 |
if (strncasecmp(data, "Custom:", len)) { |
| 123 |
ast_log(LOG_WARNING, "The DEVICE_STATE function can only be used to set 'Custom:' device state!\n"); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
data += len; |
| 127 |
if (ast_strlen_zero(data)) { |
| 128 |
ast_log(LOG_WARNING, "DEVICE_STATE function called with no custom device name!\n"); |
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
state_val = ast_devstate_val(value); |
| 133 |
|
| 134 |
if (state_val == AST_DEVICE_UNKNOWN) { |
| 135 |
ast_log(LOG_ERROR, "DEVICE_STATE function given invalid state value '%s'\n", value); |
| 136 |
return -1; |
| 137 |
} |
| 138 |
|
| 139 |
ast_db_put(astdb_family, data, value); |
| 140 |
|
| 141 |
ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", data); |
| 142 |
|
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
enum { |
| 147 |
HINT_OPT_NAME = (1 << 0), |
| 148 |
}; |
| 149 |
|
| 150 |
AST_APP_OPTIONS(hint_options, BEGIN_OPTIONS |
| 151 |
AST_APP_OPTION('n', HINT_OPT_NAME), |
| 152 |
END_OPTIONS ); |
| 153 |
|
| 154 |
static int hint_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) |
| 155 |
{ |
| 156 |
char *exten, *context; |
| 157 |
AST_DECLARE_APP_ARGS(args, |
| 158 |
AST_APP_ARG(exten); |
| 159 |
AST_APP_ARG(options); |
| 160 |
); |
| 161 |
struct ast_flags opts = { 0, }; |
| 162 |
int res; |
| 163 |
|
| 164 |
if (ast_strlen_zero(data)) { |
| 165 |
ast_log(LOG_WARNING, "The HINT function requires an extension\n"); |
| 166 |
return -1; |
| 167 |
} |
| 168 |
|
| 169 |
AST_STANDARD_APP_ARGS(args, data); |
| 170 |
|
| 171 |
if (ast_strlen_zero(args.exten)) { |
| 172 |
ast_log(LOG_WARNING, "The HINT function requires an extension\n"); |
| 173 |
return -1; |
| 174 |
} |
| 175 |
|
| 176 |
context = exten = args.exten; |
| 177 |
strsep(&context, "@"); |
| 178 |
if (ast_strlen_zero(context)) |
| 179 |
context = "default"; |
| 180 |
|
| 181 |
if (!ast_strlen_zero(args.options)) |
| 182 |
ast_app_parse_options(hint_options, &opts, NULL, args.options); |
| 183 |
|
| 184 |
if (ast_test_flag(&opts, HINT_OPT_NAME)) |
| 185 |
res = ast_get_hint(NULL, 0, buf, len, chan, context, exten); |
| 186 |
else |
| 187 |
res = ast_get_hint(buf, len, NULL, 0, chan, context, exten); |
| 188 |
|
| 189 |
return !res; |
| 190 |
} |
| 191 |
|
| 192 |
static enum ast_device_state custom_devstate_callback(const char *data) |
| 193 |
{ |
| 194 |
char buf[256] = ""; |
| 195 |
|
| 196 |
|
| 197 |
ast_db_get(astdb_family, data, buf, sizeof(buf)); |
| 198 |
|
| 199 |
return ast_devstate_val(buf); |
| 200 |
} |
| 201 |
|
| 202 |
static char *handle_cli_devstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 203 |
{ |
| 204 |
struct ast_db_entry *db_entry, *db_tree; |
| 205 |
|
| 206 |
switch (cmd) { |
| 207 |
case CLI_INIT: |
| 208 |
e->command = "devstate list"; |
| 209 |
e->usage = |
| 210 |
"Usage: devstate list\n" |
| 211 |
" List all custom device states that have been set by using\n" |
| 212 |
" the DEVICE_STATE dialplan function.\n"; |
| 213 |
return NULL; |
| 214 |
case CLI_GENERATE: |
| 215 |
return NULL; |
| 216 |
} |
| 217 |
|
| 218 |
if (a->argc != e->args) |
| 219 |
return CLI_SHOWUSAGE; |
| 220 |
|
| 221 |
ast_cli(a->fd, "\n" |
| 222 |
"---------------------------------------------------------------------\n" |
| 223 |
"--- Custom Device States --------------------------------------------\n" |
| 224 |
"---------------------------------------------------------------------\n" |
| 225 |
"---\n"); |
| 226 |
|
| 227 |
db_entry = db_tree = ast_db_gettree(astdb_family, NULL); |
| 228 |
for (; db_entry; db_entry = db_entry->next) { |
| 229 |
const char *dev_name = strrchr(db_entry->key, '/') + 1; |
| 230 |
if (dev_name <= (const char *) 1) |
| 231 |
continue; |
| 232 |
ast_cli(a->fd, "--- Name: 'Custom:%s' State: '%s'\n" |
| 233 |
"---\n", dev_name, db_entry->data); |
| 234 |
} |
| 235 |
ast_db_freetree(db_tree); |
| 236 |
db_tree = NULL; |
| 237 |
|
| 238 |
ast_cli(a->fd, |
| 239 |
"---------------------------------------------------------------------\n" |
| 240 |
"---------------------------------------------------------------------\n" |
| 241 |
"\n"); |
| 242 |
|
| 243 |
return CLI_SUCCESS; |
| 244 |
} |
| 245 |
|
| 246 |
static char *handle_cli_devstate_change(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 247 |
{ |
| 248 |
size_t len; |
| 249 |
const char *dev, *state; |
| 250 |
enum ast_device_state state_val; |
| 251 |
|
| 252 |
switch (cmd) { |
| 253 |
case CLI_INIT: |
| 254 |
e->command = "devstate change"; |
| 255 |
e->usage = |
| 256 |
"Usage: devstate change <device> <state>\n" |
| 257 |
" Change a custom device to a new state.\n" |
| 258 |
" The possible values for the state are:\n" |
| 259 |
"UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n" |
| 260 |
"RINGINUSE | ONHOLD\n" |
| 261 |
"\n" |
| 262 |
"Examples:\n" |
| 263 |
" devstate change Custom:mystate1 INUSE\n" |
| 264 |
" devstate change Custom:mystate1 NOT_INUSE\n" |
| 265 |
" \n"; |
| 266 |
return NULL; |
| 267 |
case CLI_GENERATE: |
| 268 |
{ |
| 269 |
static const char * const cmds[] = { "UNKNOWN", "NOT_INUSE", "INUSE", "BUSY", |
| 270 |
"UNAVAILABLE", "RINGING", "RINGINUSE", "ONHOLD", NULL }; |
| 271 |
|
| 272 |
if (a->pos == e->args + 1) |
| 273 |
return ast_cli_complete(a->word, cmds, a->n); |
| 274 |
|
| 275 |
return NULL; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
if (a->argc != e->args + 2) |
| 280 |
return CLI_SHOWUSAGE; |
| 281 |
|
| 282 |
len = strlen("Custom:"); |
| 283 |
dev = a->argv[e->args]; |
| 284 |
state = a->argv[e->args + 1]; |
| 285 |
|
| 286 |
if (strncasecmp(dev, "Custom:", len)) { |
| 287 |
ast_cli(a->fd, "The devstate command can only be used to set 'Custom:' device state!\n"); |
| 288 |
return CLI_FAILURE; |
| 289 |
} |
| 290 |
|
| 291 |
dev += len; |
| 292 |
if (ast_strlen_zero(dev)) |
| 293 |
return CLI_SHOWUSAGE; |
| 294 |
|
| 295 |
state_val = ast_devstate_val(state); |
| 296 |
|
| 297 |
if (state_val == AST_DEVICE_UNKNOWN) |
| 298 |
return CLI_SHOWUSAGE; |
| 299 |
|
| 300 |
ast_cli(a->fd, "Changing %s to %s\n", dev, state); |
| 301 |
|
| 302 |
ast_db_put(astdb_family, dev, state); |
| 303 |
|
| 304 |
ast_devstate_changed(state_val, AST_DEVSTATE_CACHABLE, "Custom:%s", dev); |
| 305 |
|
| 306 |
return CLI_SUCCESS; |
| 307 |
} |
| 308 |
|
| 309 |
static struct ast_cli_entry cli_funcdevstate[] = { |
| 310 |
AST_CLI_DEFINE(handle_cli_devstate_list, "List currently known custom device states"), |
| 311 |
AST_CLI_DEFINE(handle_cli_devstate_change, "Change a custom device state"), |
| 312 |
}; |
| 313 |
|
| 314 |
static struct ast_custom_function devstate_function = { |
| 315 |
.name = "DEVICE_STATE", |
| 316 |
.read = devstate_read, |
| 317 |
.write = devstate_write, |
| 318 |
}; |
| 319 |
|
| 320 |
static struct ast_custom_function hint_function = { |
| 321 |
.name = "HINT", |
| 322 |
.read = hint_read, |
| 323 |
}; |
| 324 |
|
| 325 |
static int unload_module(void) |
| 326 |
{ |
| 327 |
int res = 0; |
| 328 |
|
| 329 |
res |= ast_custom_function_unregister(&devstate_function); |
| 330 |
res |= ast_custom_function_unregister(&hint_function); |
| 331 |
res |= ast_devstate_prov_del("Custom"); |
| 332 |
res |= ast_cli_unregister_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate)); |
| 333 |
|
| 334 |
return res; |
| 335 |
} |
| 336 |
|
| 337 |
static int load_module(void) |
| 338 |
{ |
| 339 |
int res = 0; |
| 340 |
struct ast_db_entry *db_entry, *db_tree; |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
db_entry = db_tree = ast_db_gettree(astdb_family, NULL); |
| 345 |
for (; db_entry; db_entry = db_entry->next) { |
| 346 |
const char *dev_name = strrchr(db_entry->key, '/') + 1; |
| 347 |
if (dev_name <= (const char *) 1) |
| 348 |
continue; |
| 349 |
ast_devstate_changed(ast_devstate_val(db_entry->data), |
| 350 |
AST_DEVSTATE_CACHABLE, "Custom:%s", dev_name); |
| 351 |
} |
| 352 |
ast_db_freetree(db_tree); |
| 353 |
db_tree = NULL; |
| 354 |
|
| 355 |
res |= ast_custom_function_register(&devstate_function); |
| 356 |
res |= ast_custom_function_register(&hint_function); |
| 357 |
res |= ast_devstate_prov_add("Custom", custom_devstate_callback); |
| 358 |
res |= ast_cli_register_multiple(cli_funcdevstate, ARRAY_LEN(cli_funcdevstate)); |
| 359 |
|
| 360 |
return res; |
| 361 |
} |
| 362 |
|
| 363 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Gets or sets a device state in the dialplan", |
| 364 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 365 |
.load = load_module, |
| 366 |
.unload = unload_module, |
| 367 |
.load_pri = AST_MODPRI_DEVSTATE_PROVIDER, |
| 368 |
); |