2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * Matt Jordan <mjordan@digium.com>
8 * See http://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.
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.
20 #ifndef STASIS_CHANNELS_H_
21 #define STASIS_CHANNELS_H_
23 #include "asterisk/stringfields.h"
24 #include "asterisk/stasis.h"
25 #include "asterisk/json.h"
26 #include "asterisk/channel.h"
28 /*! \addtogroup StasisTopicsAndMessages
34 * \brief Structure representing a snapshot of channel state.
36 * While not enforced programmatically, this object is shared across multiple
37 * threads, and should be treated as an immutable object.
39 struct ast_channel_snapshot {
40 AST_DECLARE_STRING_FIELDS(
41 AST_STRING_FIELD(name); /*!< ASCII unique channel name */
42 AST_STRING_FIELD(accountcode); /*!< Account code for billing */
43 AST_STRING_FIELD(peeraccount); /*!< Peer account code for billing */
44 AST_STRING_FIELD(userfield); /*!< Userfield for CEL billing */
45 AST_STRING_FIELD(uniqueid); /*!< Unique Channel Identifier */
46 AST_STRING_FIELD(linkedid); /*!< Linked Channel Identifier -- gets propagated by linkage */
47 AST_STRING_FIELD(parkinglot); /*!< Default parking lot, if empty, default parking lot */
48 AST_STRING_FIELD(hangupsource); /*!< Who is responsible for hanging up this channel */
49 AST_STRING_FIELD(appl); /*!< Current application */
50 AST_STRING_FIELD(data); /*!< Data passed to current application */
51 AST_STRING_FIELD(context); /*!< Dialplan: Current extension context */
52 AST_STRING_FIELD(exten); /*!< Dialplan: Current extension number */
53 AST_STRING_FIELD(caller_name); /*!< Caller ID Name */
54 AST_STRING_FIELD(caller_number); /*!< Caller ID Number */
55 AST_STRING_FIELD(caller_ani); /*!< Caller ID ANI Number */
56 AST_STRING_FIELD(caller_rdnis); /*!< Caller ID RDNIS Number */
57 AST_STRING_FIELD(caller_dnid); /*!< Caller ID DNID Number */
58 AST_STRING_FIELD(connected_name); /*!< Connected Line Name */
59 AST_STRING_FIELD(connected_number); /*!< Connected Line Number */
60 AST_STRING_FIELD(language); /*!< The default spoken language for the channel */
63 struct timeval creationtime; /*!< The time of channel creation */
64 enum ast_channel_state state; /*!< State of line */
65 int priority; /*!< Dialplan: Current extension priority */
66 int amaflags; /*!< AMA flags for billing */
67 int hangupcause; /*!< Why is the channel hanged up. See causes.h */
68 int caller_pres; /*!< Caller ID presentation. */
69 struct ast_flags flags; /*!< channel flags of AST_FLAG_ type */
70 struct varshead *manager_vars; /*!< Variables to be appended to manager events */
75 * \brief Blob of data associated with a channel.
77 * This blob is actually shared amongst several \ref stasis_message_type's.
79 struct ast_channel_blob {
80 /*! Channel blob is associated with (or NULL for global/all channels) */
81 struct ast_channel_snapshot *snapshot;
82 /*! JSON blob of data */
83 struct ast_json *blob;
88 * \brief A set of channels with blob objects - see \ref ast_channel_blob
90 struct ast_multi_channel_blob;
94 * \brief A topic which publishes the events for all channels.
95 * \retval Topic for all channel events.
97 struct stasis_topic *ast_channel_topic_all(void);
101 * \brief A caching topic which caches \ref ast_channel_snapshot messages from
102 * ast_channel_events_all(void).
104 * \retval Topic for all channel events.
106 struct stasis_caching_topic *ast_channel_topic_all_cached(void);
110 * \brief A caching topic which caches \ref ast_channel_snapshot messages from
111 * ast_channel_events_all(void) and indexes them by name.
113 * \retval Topic for all channel events.
115 struct stasis_caching_topic *ast_channel_topic_all_cached_by_name(void);
119 * \brief Message type for \ref ast_channel_snapshot.
121 * \retval Message type for \ref ast_channel_snapshot.
123 struct stasis_message_type *ast_channel_snapshot_type(void);
127 * \brief Generate a snapshot of the channel state. This is an ao2 object, so
128 * ao2_cleanup() to deallocate.
130 * \param chan The channel from which to generate a snapshot
132 * \retval pointer on success (must be ast_freed)
133 * \retval NULL on error
135 struct ast_channel_snapshot *ast_channel_snapshot_create(
136 struct ast_channel *chan);
140 * \brief Obtain the latest \ref ast_channel_snapshot from the \ref stasis cache. This is
141 * an ao2 object, so use \ref ao2_cleanup() to deallocate.
143 * \param unique_id The channel's unique ID
145 * \retval A \ref ast_channel_snapshot on success
146 * \retval NULL on error
148 struct ast_channel_snapshot *ast_channel_snapshot_get_latest(const char *uniqueid);
152 * \brief Obtain the latest \ref ast_channel_snapshot from the \ref stasis cache. This is
153 * an ao2 object, so use \ref ao2_cleanup() to deallocate.
155 * \param name The channel's name
157 * \retval A \ref ast_channel_snapshot on success
158 * \retval NULL on error
160 struct ast_channel_snapshot *ast_channel_snapshot_get_latest_by_name(const char *name);
164 * \brief Creates a \ref ast_channel_blob message.
166 * The given \a blob should be treated as immutable and not modified after it is
167 * put into the message.
169 * \param chan Channel blob is associated with, or \c NULL for global/all channels.
170 * \param type Message type for this blob.
171 * \param blob JSON object representing the data, or \c NULL for no data. If
172 * \c NULL, ast_json_null() is put into the object.
174 * \return \ref ast_channel_blob message.
175 * \return \c NULL on error
177 struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
178 struct stasis_message_type *type, struct ast_json *blob);
182 * \brief Create a \ref ast_channel_blob message, pulling channel state from
185 * \param uniqueid Uniqueid of the channel.
186 * \param type Message type for this blob.
187 * \param blob JSON object representing the data, or \c NULL for no data. If
188 * \c NULL, ast_json_null() is put into the object.
190 * \return \ref ast_channel_blob message.
191 * \return \c NULL on error
193 struct stasis_message *ast_channel_blob_create_from_cache(
194 const char *uniqueid, struct stasis_message_type *type,
195 struct ast_json *blob);
199 * \brief Create a \ref ast_multi_channel_blob suitable for a \ref stasis_message.
201 * The given \a blob should be treated as immutable and not modified after it is
202 * put into the message.
204 * \param blob The JSON blob that defines the data of this \ref ast_multi_channel_blob
206 * \return \ref ast_multi_channel_blob object
207 * \return \c NULL on error
209 struct ast_multi_channel_blob *ast_multi_channel_blob_create(struct ast_json *blob);
213 * \brief Retrieve a channel snapshot associated with a specific role from a
214 * \ref ast_multi_channel_blob
216 * \note The reference count of the \ref ast_channel_snapshot returned from
217 * this function is not changed. The caller of this function does not own the
218 * reference to the snapshot.
220 * \param obj The \ref ast_multi_channel_blob containing the channel snapshot
222 * \param role The role associated with the channel snapshot
224 * \retval \ref ast_channel_snapshot matching the role on success
225 * \retval NULL on error or not found for the role specified
227 struct ast_channel_snapshot *ast_multi_channel_blob_get_channel(
228 struct ast_multi_channel_blob *obj, const char *role);
232 * \brief Retrieve all channel snapshots associated with a specific role from
233 * a \ref ast_multi_channel_blob
235 * \note Because this function returns an ao2_container (hashed by channel name)
236 * of all channel snapshots that matched the passed in role, the reference of
237 * the snapshots is increased by this function. The caller of this function must
238 * release the reference to the snapshots by disposing of the container
241 * \param obj The \ref ast_multi_channel_blob containing the channel snapshots to
243 * \param role The role associated with the channel snapshots
245 * \retval A container containing all \ref ast_channel_snapshot objects matching
246 * the role on success.
247 * \retval NULL on error or not found for the role specified
249 struct ao2_container *ast_multi_channel_blob_get_channels(
250 struct ast_multi_channel_blob *obj, const char *role);
254 * \brief Retrieve the JSON blob from a \ref ast_multi_channel_blob.
255 * Returned \ref ast_json is still owned by \a obj
257 * \param obj Channel blob object.
258 * \return Type field value from the blob.
259 * \return \c NULL on error.
261 struct ast_json *ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj);
265 * \brief Add a \ref ast_channel_snapshot to a \ref ast_multi_channel_blob object
267 * \note This will increase the reference count by 1 for the channel snapshot. It is
268 * assumed that the \ref ast_multi_channel_blob will own a reference to the object.
270 * \param obj The \ref ast_multi_channel_blob object that will reference the snapshot
271 * \param role A \a role that the snapshot has in the multi channel relationship
272 * \param snapshot The \ref ast_channel_snapshot being added to the
273 * \ref ast_multi_channel_blob object
275 void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj,
276 const char *role, struct ast_channel_snapshot *snapshot);
279 * \brief Publish a channel blob message.
282 * \param chan Channel publishing the blob.
283 * \param type Type of stasis message.
284 * \param blob The blob being published. (NULL if no blob)
288 void ast_channel_publish_blob(struct ast_channel *chan, struct stasis_message_type *type,
289 struct ast_json *blob);
293 * \brief Publish a \ref ast_channel_snapshot for a channel.
295 * \param chan Channel to publish.
297 void ast_channel_publish_snapshot(struct ast_channel *chan);
301 * \brief Publish a \ref ast_channel_varset for a channel.
303 * \param chan Channel to pulish the event for, or \c NULL for 'none'.
304 * \param variable Name of the variable being set
305 * \param value Value.
307 void ast_channel_publish_varset(struct ast_channel *chan,
308 const char *variable, const char *value);
312 * \brief Message type for when a channel dials another channel
314 * \retval A stasis message type
316 struct stasis_message_type *ast_channel_dial_type(void);
320 * \brief Message type for when a variable is set on a channel.
322 * \retval A stasis message type
324 struct stasis_message_type *ast_channel_varset_type(void);
328 * \brief Message type for when a custom user event is sent on a channel.
330 * \retval A stasis message type
332 struct stasis_message_type *ast_channel_user_event_type(void);
336 * \brief Message type for when a hangup is requested on a channel.
338 * \retval A stasis message type
340 struct stasis_message_type *ast_channel_hangup_request_type(void);
344 * \brief Message type for when DTMF begins on a channel.
346 * \retval A stasis message type
348 struct stasis_message_type *ast_channel_dtmf_begin_type(void);
352 * \brief Message type for when DTMF ends on a channel.
354 * \retval A stasis message type
356 struct stasis_message_type *ast_channel_dtmf_end_type(void);
360 * \brief Message type for when a channel is placed on hold.
362 * \retval A stasis message type
364 struct stasis_message_type *ast_channel_hold_type(void);
368 * \brief Message type for when a channel is removed from hold.
370 * \retval A stasis message type
372 struct stasis_message_type *ast_channel_unhold_type(void);
376 * \brief Message type for when a channel starts spying on another channel
378 * \retval A stasis message type
380 struct stasis_message_type *ast_channel_chanspy_start_type(void);
384 * \brief Message type for when a channel stops spying on another channel
386 * \retval A stasis message type
388 struct stasis_message_type *ast_channel_chanspy_stop_type(void);
392 * \brief Message type for a fax operation
394 * \retval A stasis message type
396 struct stasis_message_type *ast_channel_fax_type(void);
400 * \brief Message type for hangup handler related actions
402 * \retval A stasis message type
404 struct stasis_message_type *ast_channel_hangup_handler_type(void);
408 * \brief Message type for starting monitor on a channel
410 * \retval A stasis message type
412 struct stasis_message_type *ast_channel_monitor_start_type(void);
416 * \brief Message type for stopping monitor on a channel
418 * \retval A stasis message type
420 struct stasis_message_type *ast_channel_monitor_stop_type(void);
424 * \brief Message type for starting music on hold on a channel
426 * \retval A stasis message type
428 struct stasis_message_type *ast_channel_moh_start_type(void);
432 * \brief Message type for stopping music on hold on a channel
434 * \retval A stasis message type
436 struct stasis_message_type *ast_channel_moh_stop_type(void);
440 * \brief Publish in the \ref ast_channel_topic or \ref ast_channel_topic_all
441 * topics a stasis message for the channels involved in a dial operation.
443 * \param caller The channel performing the dial operation
444 * \param peer The channel being dialed
445 * \param dialstring When beginning a dial, the information passed to the
446 * dialing application
447 * \param dialstatus The current status of the dial operation (NULL if no
450 void ast_channel_publish_dial(struct ast_channel *caller,
451 struct ast_channel *peer,
452 const char *dialstring,
453 const char *dialstatus);
457 * \brief Publish in the \ref ast_channel_topic a \ref ast_channel_snapshot
458 * message indicating a change in channel state
460 * \param chan The channel whose state has changed
462 void ast_publish_channel_state(struct ast_channel *chan);
467 * \brief Build a JSON object from a \ref ast_channel_snapshot.
468 * \return JSON object representing channel snapshot.
469 * \return \c NULL on error
471 struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot);
474 * \brief Compares the context, exten and priority of two snapshots.
477 * \param old_snapshot Old snapshot
478 * \param new_snapshot New snapshot
480 * \return True (non-zero) if context, exten or priority are identical.
481 * \return False (zero) if context, exten and priority changed.
483 int ast_channel_snapshot_cep_equal(
484 const struct ast_channel_snapshot *old_snapshot,
485 const struct ast_channel_snapshot *new_snapshot);
488 * \brief Compares the callerid info of two snapshots.
491 * \param old_snapshot Old snapshot
492 * \param new_snapshot New snapshot
494 * \return True (non-zero) if callerid are identical.
495 * \return False (zero) if callerid changed.
497 int ast_channel_snapshot_caller_id_equal(
498 const struct ast_channel_snapshot *old_snapshot,
499 const struct ast_channel_snapshot *new_snapshot);
502 * \brief Initialize the stasis channel topic and message types
504 void ast_stasis_channels_init(void);
506 #endif /* STASIS_CHANNELS_H_ */