Asterisk - The Open Source Telephony Project GIT-master-57e2743
Loading...
Searching...
No Matches
Functions | Variables
res_manager_devicestate.c File Reference
#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/manager.h"
#include "asterisk/stasis.h"
#include "asterisk/devicestate.h"
#include "asterisk/mwi.h"
#include "asterisk/conversions.h"
Include dependency graph for res_manager_devicestate.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int action_devicestatechange (struct mansession *s, const struct message *m)
 
static int action_devicestatelist (struct mansession *s, const struct message *m)
 
static int action_mailboxstatechange (struct mansession *s, const struct message *m)
 
struct ast_module * AST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Manager Device State Topic Forwarder" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEVSTATE_CONSUMER, }
 
static const struct ast_module_info * ast_module_info = &__mod_info
 
static struct stasis_forward * topic_forwarder
 

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 348 of file res_manager_devicestate.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 348 of file res_manager_devicestate.c.

◆ action_devicestatechange()

static int action_devicestatechange ( struct mansession *  s,
const struct message *  m 
)
static

Definition at line 199 of file res_manager_devicestate.c.

200{
201 struct ast_eid eid;
202 enum ast_device_state state_val;
203 const char *device = astman_get_header(m, "Device");
204 const char *state = astman_get_header(m, "State");
205 const char *cachable = astman_get_header(m, "Cachable");
206 const char *entity_id = astman_get_header(m, "EntityID");
207
208 if (ast_strlen_zero(device) || ast_strlen_zero(state)) {
209 astman_send_error(s, m, "Missing device or device state");
210 return 0;
211 }
212 if (ast_strlen_zero(entity_id) || ast_str_to_eid(&eid, entity_id)) {
213 astman_send_error(s, m, "Missing or invalid entity ID");
214 return 0;
215 }
216 if (!strchr(device, '/') && !strchr(device, ':')) {
217 astman_send_error(s, m, "Invalid device name");
218 return 0;
219 }
220
221 state_val = ast_devstate_val(state);
222 /* ast_devstate_val returns AST_DEVICE_UNKNOWN if the device state value is not valid,
223 * but AST_DEVICE_UNKNOWN is also a legitimate value, so allow it if that's what the input actually was. */
224 if (state_val == AST_DEVICE_UNKNOWN && strcasecmp(state, "UNKNOWN")) {
225 astman_send_error(s, m, "Invalid device state value");
226 return 0;
227 }
228
230 astman_send_error(s, m, "Entity ID is ourself (must be from a different Asterisk system)");
231 return 0;
232 }
233
235 astman_send_error(s, m, "Failed to publish device state update");
236 return 0;
237 }
238
239 astman_send_ack(s, m, "Updated or set device state");
240 return 0;
241}
@ AST_DEVSTATE_CACHABLE
Definition devicestate.h:70
@ AST_DEVSTATE_NOT_CACHABLE
Definition devicestate.h:69
enum ast_device_state ast_devstate_val(const char *val)
Convert device state from text to integer value.
ast_device_state
Device States.
Definition devicestate.h:52
@ AST_DEVICE_UNKNOWN
Definition devicestate.h:53
int ast_publish_device_state_full(const char *device, enum ast_device_state state, enum ast_devstate_cache cachable, struct ast_eid *eid)
Publish a device state update with EID.
void astman_send_error(struct mansession *s, const struct message *m, char *error)
Send error in manager transaction.
Definition manager.c:1921
void astman_send_ack(struct mansession *s, const struct message *m, char *msg)
Send ack in manager transaction.
Definition manager.c:1953
const char * astman_get_header(const struct message *m, char *var)
Get header from manager transaction.
Definition manager.c:1582
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
int attribute_pure ast_false(const char *val)
Make sure something is false. Determine if a string containing a boolean value is "false"....
Definition utils.c:2250
An Entity ID is essentially a MAC address, brief and unique.
Definition utils.h:853
unsigned char eid[6]
Definition utils.h:854
int ast_eid_cmp(const struct ast_eid *eid1, const struct ast_eid *eid2)
Compare two EIDs.
Definition utils.c:3128
struct ast_eid ast_eid_default
Global EID.
Definition options.c:94
int ast_str_to_eid(struct ast_eid *eid, const char *s)
Convert a string into an EID.
Definition utils.c:3111

References AST_DEVICE_UNKNOWN, AST_DEVSTATE_CACHABLE, AST_DEVSTATE_NOT_CACHABLE, ast_devstate_val(), ast_eid_cmp(), ast_eid_default, ast_false(), ast_publish_device_state_full(), ast_str_to_eid(), ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), and ast_eid::eid.

Referenced by load_module().

◆ action_devicestatelist()

static int action_devicestatelist ( struct mansession *  s,
const struct message *  m 
)
static

Definition at line 157 of file res_manager_devicestate.c.

158{
159 RAII_VAR(struct ao2_container *, device_states, NULL, ao2_cleanup);
160 const char *action_id = astman_get_header(m, "ActionID");
161 struct stasis_message *msg;
162 struct ao2_iterator it_states;
163 int count = 0;
164
167 if (!device_states) {
168 astman_send_error(s, m, "Memory Allocation Failure");
169 return 0;
170 }
171
172 astman_send_listack(s, m, "Device State Changes will follow", "start");
173
174 it_states = ao2_iterator_init(device_states, 0);
175 for (; (msg = ao2_iterator_next(&it_states)); ao2_ref(msg, -1)) {
177
178 if (!blob) {
179 continue;
180 }
181
182 count++;
183
184 astman_append(s, "Event: %s\r\n", blob->manager_event);
185 if (!ast_strlen_zero(action_id)) {
186 astman_append(s, "ActionID: %s\r\n", action_id);
187 }
188 astman_append(s, "%s\r\n", blob->extra_fields);
189 ao2_ref(blob, -1);
190 }
191 ao2_iterator_destroy(&it_states);
192
193 astman_send_list_complete_start(s, m, "DeviceStateListComplete", count);
195
196 return 0;
197}
#define ao2_iterator_next(iter)
Definition astobj2.h:1911
#define ao2_cleanup(obj)
Definition astobj2.h:1934
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition astobj2.h:459
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
struct stasis_message_type * ast_device_state_message_type(void)
Get the Stasis message type for device state messages.
struct stasis_cache * ast_device_state_cache(void)
Backend cache for ast_device_state_topic_cached()
void astman_send_listack(struct mansession *s, const struct message *m, char *msg, char *listflag)
Send ack in manager transaction to begin a list.
Definition manager.c:1963
void astman_send_list_complete_start(struct mansession *s, const struct message *m, const char *event_name, int count)
Start the list complete event.
Definition manager.c:1999
void astman_send_list_complete_end(struct mansession *s)
End the list complete event.
Definition manager.c:2007
void astman_append(struct mansession *s, const char *fmt,...)
Definition manager.c:1842
#define NULL
Definition resample.c:96
struct ast_manager_event_blob * stasis_message_to_ami(struct stasis_message *msg)
Build the AMI representation of the message.
struct ao2_container * stasis_cache_dump_by_eid(struct stasis_cache *cache, struct stasis_message_type *type, const struct ast_eid *eid)
Dump cached items to a subscription for a specific entity.
Generic container type.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition astobj2.h:1821
Struct containing info for an AMI event to send out.
Definition manager.h:504
const ast_string_field extra_fields
Definition manager.h:509
const char * manager_event
Definition manager.h:506
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition utils.h:981

References ao2_cleanup, ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ast_device_state_cache(), ast_device_state_message_type(), ast_strlen_zero(), astman_append(), astman_get_header(), astman_send_error(), astman_send_list_complete_end(), astman_send_list_complete_start(), astman_send_listack(), ast_manager_event_blob::extra_fields, ast_manager_event_blob::manager_event, NULL, RAII_VAR, stasis_cache_dump_by_eid(), and stasis_message_to_ami().

Referenced by load_module().

◆ action_mailboxstatechange()

static int action_mailboxstatechange ( struct mansession *  s,
const struct message *  m 
)
static

Definition at line 243 of file res_manager_devicestate.c.

244{
245 struct ast_eid eid;
246 int new_msgs, old_msgs;
247 char *context, *mailbox;
248 const char *mbox = astman_get_header(m, "Mailbox");
249 const char *newmsgs = astman_get_header(m, "NewMessages");
250 const char *oldmsgs = astman_get_header(m, "OldMessages");
251 const char *entity_id = astman_get_header(m, "EntityID");
252
253 if (ast_strlen_zero(mbox) || ast_strlen_zero(newmsgs) || ast_strlen_zero(oldmsgs)) {
254 astman_send_error(s, m, "Missing required parameters");
255 return 0;
256 }
257 if (ast_strlen_zero(entity_id) || ast_str_to_eid(&eid, entity_id)) {
258 astman_send_error(s, m, "Missing or invalid entity ID");
259 return 0;
260 }
261
262 if (ast_str_to_int(newmsgs, &new_msgs) || ast_str_to_int(oldmsgs, &old_msgs)) {
263 astman_send_error(s, m, "Invalid mailbox counts");
264 return 0;
265 }
266 if (new_msgs < 0 || old_msgs < 0) {
267 astman_send_error(s, m, "Invalid mailbox counts (must be non-negative)");
268 return 0;
269 }
270
272 astman_send_error(s, m, "Entity ID is ourself (must be from a different Asterisk system)");
273 return 0;
274 }
275
277 mailbox = strsep(&context, "@");
278
279 /* Mailbox is mandatory, context is optional */
280 if (ast_strlen_zero(mailbox)) {
281 astman_send_error(s, m, "A mailbox is required");
282 return 0;
283 }
284
285 if (ast_publish_mwi_state_full(mailbox, context, new_msgs, old_msgs, NULL, &eid)) {
286 astman_send_error(s, m, "Failed to publish mailbox state update");
287 return 0;
288 }
289
290 astman_send_ack(s, m, "Updated mailbox state");
291 return 0;
292}
static const char * mbox(struct ast_vm_user *vmu, int id)
char * strsep(char **str, const char *delims)
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
int ast_str_to_int(const char *str, int *res)
Convert the given string to a signed integer.
Definition conversions.c:44
int ast_publish_mwi_state_full(const char *mailbox, const char *context, int new_msgs, int old_msgs, const char *channel_id, struct ast_eid *eid)
Publish a MWI state update via stasis with all parameters.
Definition mwi.c:393

References ast_eid_cmp(), ast_eid_default, ast_publish_mwi_state_full(), ast_str_to_eid(), ast_str_to_int(), ast_strdupa, ast_strlen_zero(), astman_get_header(), astman_send_ack(), astman_send_error(), ast_eid::eid, mbox(), NULL, and strsep().

Referenced by load_module().

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 348 of file res_manager_devicestate.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 304 of file res_manager_devicestate.c.

305{
307
309 ast_log(LOG_ERROR, "Entity ID is not set.\n");
311 }
312
314 if (!manager_topic) {
316 }
318 if (!topic_forwarder) {
320 }
321
326 }
327
330 ast_manager_unregister("DeviceStateList");
332 }
335 ast_manager_unregister("DeviceStateList");
336 ast_manager_unregister("DeviceStateChange");
338 }
339
341}
#define ast_log
Definition astobj2.c:42
struct stasis_topic * ast_device_state_topic_all(void)
Get the Stasis topic for device state messages.
struct stasis_topic * ast_manager_get_topic(void)
Get the Stasis Message Bus API topic for AMI.
Definition manager.c:441
static struct stasis_topic * manager_topic
A stasis_topic that all topics AMI cares about will be forwarded to.
Definition manager.c:170
int ast_manager_unregister(const char *action)
Unregister a registered manager command.
Definition manager.c:7782
#define LOG_ERROR
#define EVENT_FLAG_REPORTING
Definition manager.h:84
#define ast_manager_register_xml(action, authority, func)
Register a manager callback using XML documentation to describe the manager.
Definition manager.h:193
#define EVENT_FLAG_CALL
Definition manager.h:76
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition module.h:78
static int action_devicestatelist(struct mansession *s, const struct message *m)
static int action_mailboxstatechange(struct mansession *s, const struct message *m)
static int action_devicestatechange(struct mansession *s, const struct message *m)
static struct stasis_forward * topic_forwarder
struct stasis_forward * stasis_forward_cancel(struct stasis_forward *forward)
Definition stasis.c:1626
struct stasis_forward * stasis_forward_all(struct stasis_topic *from_topic, struct stasis_topic *to_topic)
Create a subscription which forwards all messages from one topic to another.
Definition stasis.c:1656
int ast_eid_is_empty(const struct ast_eid *eid)
Check if EID is empty.
Definition utils.c:3133

References action_devicestatechange(), action_devicestatelist(), action_mailboxstatechange(), ast_device_state_topic_all(), ast_eid_default, ast_eid_is_empty(), ast_log, ast_manager_get_topic(), ast_manager_register_xml, ast_manager_unregister(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, EVENT_FLAG_CALL, EVENT_FLAG_REPORTING, LOG_ERROR, manager_topic, stasis_forward_all(), stasis_forward_cancel(), and topic_forwarder.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 294 of file res_manager_devicestate.c.

295{
297 ast_manager_unregister("DeviceStateList");
298 ast_manager_unregister("DeviceStateChange");
299 ast_manager_unregister("MailboxStateChange");
300
301 return 0;
302}

References ast_manager_unregister(), stasis_forward_cancel(), and topic_forwarder.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Manager Device State Topic Forwarder" , .key = ASTERISK_GPL_KEY , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEVSTATE_CONSUMER, }
static

Definition at line 348 of file res_manager_devicestate.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 348 of file res_manager_devicestate.c.

◆ topic_forwarder

struct stasis_forward* topic_forwarder
static

Definition at line 155 of file res_manager_devicestate.c.

Referenced by load_module(), and unload_module().