Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions | Variables
command.c File Reference

Stasis application command support. More...

#include "asterisk.h"
#include "command.h"
#include "asterisk/lock.h"
#include "asterisk/stasis_app_impl.h"
Include dependency graph for command.c:

Go to the source code of this file.

Data Structures

struct  stasis_app_command
 

Functions

void command_complete (struct stasis_app_command *command, int retval)
 
struct stasis_app_commandcommand_create (stasis_app_command_cb callback, void *data, command_data_destructor_fn data_destructor)
 
static void command_dtor (void *obj)
 
void command_invoke (struct stasis_app_command *command, struct stasis_app_control *control, struct ast_channel *chan)
 
int command_join (struct stasis_app_command *command)
 
struct ao2_containercommand_prestart_get_container (struct ast_channel *chan)
 Get the Stasis() prestart commands for a channel. More...
 
int command_prestart_queue_command (struct ast_channel *chan, stasis_app_command_cb command_fn, void *data, command_data_destructor_fn data_destructor)
 Queue a Stasis() prestart command for a channel. More...
 
static void command_queue_prestart_destroy (void *obj)
 

Variables

static const struct ast_datastore_info command_queue_prestart
 

Detailed Description

Stasis application command support.

Author
David M. Lee, II dlee@.nosp@m.digi.nosp@m.um.co.nosp@m.m

Definition in file command.c.

Function Documentation

◆ command_complete()

void command_complete ( struct stasis_app_command command,
int  retval 
)

Definition at line 77 of file command.c.

References ast_cond_signal, ast_mutex_lock, ast_mutex_unlock, stasis_app_command::condition, stasis_app_command::is_done, stasis_app_command::lock, and stasis_app_command::retval.

Referenced by command_invoke(), control_flush_queue(), and exec_command_on_condition().

78 {
79  ast_mutex_lock(&command->lock);
80  command->is_done = 1;
81  command->retval = retval;
82  ast_cond_signal(&command->condition);
83  ast_mutex_unlock(&command->lock);
84 }
unsigned int is_done
Definition: command.c:40
#define ast_mutex_lock(a)
Definition: lock.h:187
#define ast_cond_signal(cond)
Definition: lock.h:201
ast_mutex_t lock
Definition: command.c:34
static ENTRY retval
Definition: hsearch.c:50
ast_cond_t condition
Definition: command.c:35
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ command_create()

struct stasis_app_command* command_create ( stasis_app_command_cb  callback,
void *  data,
command_data_destructor_fn  data_destructor 
)

Definition at line 55 of file command.c.

References ao2_alloc, ast_cond_init, ast_mutex_init, stasis_app_command::callback, command_dtor(), stasis_app_command::condition, stasis_app_command::data, stasis_app_command::data_destructor, stasis_app_command::lock, and NULL.

Referenced by command_prestart_queue_command(), and exec_command_on_condition().

57 {
58  struct stasis_app_command *command;
59 
60  command = ao2_alloc(sizeof(*command), command_dtor);
61  if (!command) {
62  if (data_destructor) {
64  }
65  return NULL;
66  }
67 
68  ast_mutex_init(&command->lock);
69  ast_cond_init(&command->condition, 0);
70  command->callback = callback;
71  command->data = data;
73 
74  return command;
75 }
#define ast_cond_init(cond, attr)
Definition: lock.h:199
#define NULL
Definition: resample.c:96
command_data_destructor_fn data_destructor
Definition: command.c:38
static void command_dtor(void *obj)
Definition: command.c:43
ast_mutex_t lock
Definition: command.c:34
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:411
ast_cond_t condition
Definition: command.c:35
#define ast_mutex_init(pmutex)
Definition: lock.h:184
stasis_app_command_cb callback
Definition: command.c:36

◆ command_dtor()

static void command_dtor ( void *  obj)
static

Definition at line 43 of file command.c.

References ast_cond_destroy, ast_mutex_destroy, stasis_app_command::condition, stasis_app_command::data, stasis_app_command::data_destructor, and stasis_app_command::lock.

Referenced by command_create().

44 {
45  struct stasis_app_command *command = obj;
46 
47  if (command->data_destructor) {
48  command->data_destructor(command->data);
49  }
50 
51  ast_mutex_destroy(&command->lock);
52  ast_cond_destroy(&command->condition);
53 }
command_data_destructor_fn data_destructor
Definition: command.c:38
ast_mutex_t lock
Definition: command.c:34
#define ast_cond_destroy(cond)
Definition: lock.h:200
ast_cond_t condition
Definition: command.c:35
#define ast_mutex_destroy(a)
Definition: lock.h:186

◆ command_invoke()

void command_invoke ( struct stasis_app_command command,
struct stasis_app_control control,
struct ast_channel chan 
)

Definition at line 101 of file command.c.

References stasis_app_command::callback, command_complete(), stasis_app_command::data, stasis_app_command::data_destructor, NULL, and stasis_app_command::retval.

Referenced by control_dispatch_all(), and control_prestart_dispatch_all().

103 {
104  int retval = command->callback(control, chan, command->data);
105  if (command->data_destructor) {
106  command->data_destructor(command->data);
107  command->data_destructor = NULL;
108  }
109  command_complete(command, retval);
110 }
void command_complete(struct stasis_app_command *command, int retval)
Definition: command.c:77
#define NULL
Definition: resample.c:96
command_data_destructor_fn data_destructor
Definition: command.c:38
static ENTRY retval
Definition: hsearch.c:50
stasis_app_command_cb callback
Definition: command.c:36

◆ command_join()

int command_join ( struct stasis_app_command command)

Definition at line 86 of file command.c.

References ast_cond_wait, ast_mutex_lock, ast_mutex_unlock, stasis_app_command::condition, stasis_app_command::is_done, stasis_app_command::lock, and stasis_app_command::retval.

Referenced by app_send_command_on_condition().

87 {
88  int ret;
89 
90  ast_mutex_lock(&command->lock);
91  while (!command->is_done) {
92  ast_cond_wait(&command->condition, &command->lock);
93  }
94 
95  ret = command->retval;
96  ast_mutex_unlock(&command->lock);
97 
98  return ret;
99 }
unsigned int is_done
Definition: command.c:40
#define ast_cond_wait(cond, mutex)
Definition: lock.h:203
#define ast_mutex_lock(a)
Definition: lock.h:187
ast_mutex_t lock
Definition: command.c:34
ast_cond_t condition
Definition: command.c:35
#define ast_mutex_unlock(a)
Definition: lock.h:188

◆ command_prestart_get_container()

struct ao2_container* command_prestart_get_container ( struct ast_channel chan)

Get the Stasis() prestart commands for a channel.

Precondition
chan must be locked
Parameters
chanThe channel from which to get prestart commands
Returns
The command prestart container for chan (must be ao2_cleanup()'d)

Definition at line 160 of file command.c.

References ao2_bump, ast_channel_datastore_find(), ast_datastore::data, and NULL.

Referenced by control_prestart_dispatch_all().

161 {
163 
164  if (!datastore) {
165  return NULL;
166  }
167 
168  return ao2_bump(datastore->data);
169 }
static const struct ast_datastore_info command_queue_prestart
Definition: command.c:118
Structure for a data store object.
Definition: datastore.h:68
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2404
#define NULL
Definition: resample.c:96
#define ao2_bump(obj)
Definition: astobj2.h:491
void * data
Definition: datastore.h:70

◆ command_prestart_queue_command()

int command_prestart_queue_command ( struct ast_channel chan,
stasis_app_command_cb  command_fn,
void *  data,
command_data_destructor_fn  data_destructor 
)

Queue a Stasis() prestart command for a channel.

Precondition
chan must be locked
Parameters
chanThe channel on which to queue the prestart command
command_fnThe callback to call for the command
dataThe data to pass to the command callback
data_destructorOptional function which will be called on the data in either the event of command completion or failure to schedule or complete the command
Return values
zeroon success
non-zeroon failure

Definition at line 123 of file command.c.

References AO2_ALLOC_OPT_LOCK_MUTEX, ao2_cleanup, ao2_container_alloc_list, ao2_link, ast_channel_datastore_add(), ast_channel_datastore_find(), ast_datastore_alloc, command_create(), ast_datastore::data, NULL, and RAII_VAR.

Referenced by bridge_stasis_queue_join_action().

125 {
126  struct ast_datastore *datastore;
127  struct ao2_container *command_queue;
128  RAII_VAR(struct stasis_app_command *, command,
129  command_create(command_fn, data, data_destructor), ao2_cleanup);
130 
131  if (!command) {
132  return -1;
133  }
134 
136  if (datastore) {
137  command_queue = datastore->data;
138  ao2_link(command_queue, command);
139  return 0;
140  }
141 
143  if (!command_queue) {
144  return -1;
145  }
146 
148  if (!datastore) {
149  ao2_cleanup(command_queue);
150  return -1;
151  }
152  ast_channel_datastore_add(chan, datastore);
153 
154  datastore->data = command_queue;
155  ao2_link(command_queue, command);
156 
157  return 0;
158 }
static const struct ast_datastore_info command_queue_prestart
Definition: command.c:118
#define ao2_container_alloc_list(ao2_options, container_options, sort_fn, cmp_fn)
Definition: astobj2.h:1335
Structure for a data store object.
Definition: datastore.h:68
struct ast_datastore * ast_channel_datastore_find(struct ast_channel *chan, const struct ast_datastore_info *info, const char *uid)
Find a datastore on a channel.
Definition: channel.c:2404
#define NULL
Definition: resample.c:96
#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:911
struct stasis_app_command * command_create(stasis_app_command_cb callback, void *data, command_data_destructor_fn data_destructor)
Definition: command.c:55
void * data
Definition: datastore.h:70
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
#define ast_datastore_alloc(info, uid)
Definition: datastore.h:89
Generic container type.
int ast_channel_datastore_add(struct ast_channel *chan, struct ast_datastore *datastore)
Add a datastore to a channel.
Definition: channel.c:2390
#define ao2_link(container, obj)
Definition: astobj2.h:1549

◆ command_queue_prestart_destroy()

static void command_queue_prestart_destroy ( void *  obj)
static

Definition at line 112 of file command.c.

References ao2_cleanup.

113 {
114  /* Clean up the container */
115  ao2_cleanup(obj);
116 }
#define ao2_cleanup(obj)
Definition: astobj2.h:1958

Variable Documentation

◆ command_queue_prestart

const struct ast_datastore_info command_queue_prestart
static
Initial value:
= {
.type = "stasis-command-prestart-queue",
}
static void command_queue_prestart_destroy(void *obj)
Definition: command.c:112

Definition at line 118 of file command.c.