Asterisk - The Open Source Telephony Project GIT-master-6144b6b
Loading...
Searching...
No Matches
extension_state_autohints.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2026, Sangoma Technologies Corporation
5 *
6 * Joshua Colp <jcolp@sangoma.com>
7 *
8 * See https://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19/*** MODULEINFO
20 <support_level>core</support_level>
21 ***/
22
23#include "asterisk.h"
24#include "asterisk/_private.h"
25#include "asterisk/pbx.h"
26#include "asterisk/stasis.h"
27#include "asterisk/vector.h"
28#include "pbx_private.h"
29
30/*! \brief Lock to protect the autohints vector */
32
33/*! \brief Contexts which have autohints enabled */
35
36/*! \brief Subscription to receive updates so we can create hints as needed on autohint enabled contexts */
38
39/*! \brief The static registrar for the added dialplan hints */
40static const char registrar[] = "autohints";
41
42/*!
43 * \internal
44 * \brief Callback for device state updates to create hints for autohint enabled contexts.
45 *
46 * \param userdata The user data passed to the subscription.
47 * \param sub The subscription that received the message.
48 * \param msg The message received by the subscription.
49 */
51 struct stasis_message *msg)
52{
53 struct ast_device_state_message *device_state;
54 char *virtual_device, *type, *device_name;
55 int i;
56
58 return;
59 }
60
61 device_state = stasis_message_data(msg);
62
63 /* We only care about the aggregate state */
64 if (device_state->eid) {
65 return;
66 }
67
68 type = ast_strdupa(device_state->device);
69 if (ast_strlen_zero(type)) {
70 return;
71 }
72
73 /* Determine if this is a virtual/custom device or a real device */
74 virtual_device = strchr(type, ':');
75 device_name = strchr(type, '/');
76 if (virtual_device && (!device_name || (virtual_device < device_name))) {
77 device_name = virtual_device;
78 }
79
80 /* Invalid device state name - not a virtual/custom device and not a real device */
81 if (ast_strlen_zero(device_name)) {
82 return;
83 }
84 *device_name++ = '\0';
85
87
89 for (i = 0; i < AST_VECTOR_SIZE(&extension_state_autohints); i++) {
91 struct pbx_find_info q = { .stacklen = 0 };
92
93 if (pbx_find_extension(NULL, NULL, &q, ast_get_context_name(context), device_name,
94 PRIORITY_HINT, NULL, "", E_MATCH)) {
95 continue;
96 }
97
98 /* The device has no hint in the context referenced by this autohint so create one */
100 device_state->device, ast_strdup(device_state->device), ast_free_ptr, registrar);
101 }
103
105}
106
107/*!
108 * \internal
109 * \brief Compare an autohint's context name to a provided context name for searching the autohints vector
110 */
111#define AUTOHINT_CMP_CONTEXT_NAME(elem, name) (!strcmp(ast_get_context_name(elem), name))
112
114{
116
118
119 /* Since we store a pointer to the context we remove the old one by name and append the new one, easy */
123
125 /* We have at least one context enabled with autohints and no subscription, so subscribe */
127 }
129
131}
132
158
159/*!
160 * \internal
161 * \brief Clean up the autohints extension state system, called at shutdown.
162 */
164{
168 }
169 /* The vector just contains pointers so no need to free the individual items */
172}
173
175{
176 /* Most likely there will be at most one context configured for autohints, or zero */
178 return -1;
179 }
180
182 return 0;
183}
Prototypes for public functions only of internal interest,.
Asterisk main include file. File version handling, generic pbx functions.
int ast_register_cleanup(void(*func)(void))
Register a function to be executed before Asterisk gracefully exits.
Definition clicompat.c:19
#define ast_strdup(str)
A wrapper for strdup()
Definition astmm.h:241
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
void ast_free_ptr(void *ptr)
free() wrapper
Definition astmm.c:1739
static const char type[]
struct stasis_message_type * ast_device_state_message_type(void)
Get the Stasis message type for device state messages.
struct stasis_topic * ast_device_state_topic_all(void)
Get the Stasis topic for device state messages.
@ E_MATCH
Definition extconf.h:217
static struct @376 extension_state_autohints
Contexts which have autohints enabled.
int ast_extension_state_autohints_init(void)
static const char registrar[]
The static registrar for the added dialplan hints.
#define AUTOHINT_CMP_CONTEXT_NAME(elem, name)
static void extension_state_autohints_cleanup(void)
void pbx_extension_state_autohint_set(struct ast_context *context)
static struct stasis_subscription * autohints_subscription
Subscription to receive updates so we can create hints as needed on autohint enabled contexts.
void pbx_extension_state_autohint_remove(struct ast_context *context, unsigned int forced)
static ast_mutex_t extension_state_autohints_lock
Lock to protect the autohints vector.
static void extension_state_autohints_device_state_cb(void *userdata, struct stasis_subscription *sub, struct stasis_message *msg)
#define ast_mutex_unlock(a)
Definition lock.h:197
#define ast_mutex_lock(a)
Definition lock.h:196
#define AST_MUTEX_DEFINE_STATIC(mutex)
Definition lock.h:527
Core PBX routines and definitions.
int ast_add_extension(const char *context, int replace, const char *extension, int priority, const char *label, const char *callerid, const char *application, void *data, void(*datad)(void *), const char *registrar)
Add and extension to an extension context.
Definition pbx.c:5206
int ast_context_destroy_by_name(const char *context, const char *registrar)
Destroy a context by name.
Definition pbx.c:6491
int ast_wrlock_contexts(void)
Write locks the context list.
Definition pbx.c:6621
const char * ast_get_context_name(struct ast_context *con)
Definition ael_main.c:421
int ast_rdlock_contexts(void)
Read locks the context list.
Definition pbx.c:6626
#define PRIORITY_HINT
Definition pbx.h:54
struct ast_exten * pbx_find_extension(struct ast_channel *chan, struct ast_context *bypass, struct pbx_find_info *q, const char *context, const char *exten, int priority, const char *label, const char *callerid, enum ext_match_t action)
Definition ael_main.c:152
int ast_unlock_contexts(void)
Unlocks contexts.
Definition pbx.c:6631
Private include file for pbx.
static struct stasis_subscription * sub
Statsd channel stats. Exmaple of how to subscribe to Stasis events.
#define NULL
Definition resample.c:96
Stasis Message Bus API. See Stasis Message Bus API for detailed documentation.
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
struct stasis_subscription * stasis_unsubscribe(struct stasis_subscription *subscription)
Cancel a subscription.
Definition stasis.c:1049
#define stasis_subscribe(topic, callback, data)
Definition stasis.h:649
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
ast_context: An extension context
Definition pbx.c:254
The structure that contains device state.
const struct ast_eid * eid
The EID of the server where this message originated.
Vector container support.
#define AST_VECTOR_ELEM_CLEANUP_NOOP(elem)
Vector element cleanup that does nothing.
Definition vector.h:582
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition vector.h:620
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition vector.h:185
#define AST_VECTOR_REMOVE_CMP_UNORDERED(vec, value, cmp, cleanup)
Remove an element from a vector that matches the given comparison.
Definition vector.h:499
#define AST_VECTOR_ELEM_DEFAULT_CMP(elem, value)
Default comparator for AST_VECTOR_REMOVE_ELEM_UNORDERED()
Definition vector.h:575
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition vector.h:124
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition vector.h:267
#define AST_VECTOR(name, type)
Define a vector structure.
Definition vector.h:44
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition vector.h:691