Index: funcs/func_devstate.c =================================================================== --- funcs/func_devstate.c (revision 209992) +++ funcs/func_devstate.c (working copy) @@ -39,6 +39,7 @@ #include "asterisk/pbx.h" #include "asterisk/utils.h" #include "asterisk/linkedlists.h" +#include "asterisk/event.h" #include "asterisk/devicestate.h" #include "asterisk/cli.h" #include "asterisk/astdb.h" @@ -96,6 +97,7 @@ static const char astdb_family[] = "CustomDevstate"; +static struct ast_event_sub *custom_devstate_subscription; static int devstate_read(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t len) { @@ -188,6 +190,19 @@ return ast_devstate_val(buf); } +static void custom_devstate_sub_callback(const struct ast_event *ast_event, void *data) +{ + const char *device = ast_event_get_ie_str(ast_event, AST_EVENT_IE_DEVICE); + + if(!strncmp(device, "Custom:", 7)) { + + const char *dev_state = ast_devstate_str(ast_event_get_ie_uint(ast_event, + AST_EVENT_IE_STATE)); + const char *dev_name = strrchr(device, ':') + 1; + ast_db_put(astdb_family, dev_name, dev_state); + } +} + static char *handle_cli_devstate_list(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) { struct ast_db_entry *db_entry, *db_tree; @@ -315,6 +330,10 @@ { int res = 0; + if (custom_devstate_subscription) { + ast_event_unsubscribe(custom_devstate_subscription); + } + res |= ast_custom_function_unregister(&devstate_function); res |= ast_custom_function_unregister(&hint_function); res |= ast_devstate_prov_del("Custom"); @@ -340,7 +359,10 @@ } ast_db_freetree(db_tree); db_tree = NULL; - + custom_devstate_subscription = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE, + custom_devstate_sub_callback, "custom_devstate_subscription", NULL, + AST_EVENT_IE_END); + res |= ast_custom_function_register(&devstate_function); res |= ast_custom_function_register(&hint_function); res |= ast_devstate_prov_add("Custom", custom_devstate_callback);