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(connected_name); /*!< Connected Line Name */
56 AST_STRING_FIELD(connected_number); /*!< Connected Line Number */
57 AST_STRING_FIELD(language); /*!< The default spoken language for the channel */
60 struct timeval creationtime; /*!< The time of channel creation */
61 enum ast_channel_state state; /*!< State of line */
62 int priority; /*!< Dialplan: Current extension priority */
63 int amaflags; /*!< AMA flags for billing */
64 int hangupcause; /*!< Why is the channel hanged up. See causes.h */
65 int caller_pres; /*!< Caller ID presentation. */
66 struct ast_flags flags; /*!< channel flags of AST_FLAG_ type */
67 struct varshead *manager_vars; /*!< Variables to be appended to manager events */
72 * \brief Blob of data associated with a channel.
74 * This blob is actually shared amongst several \ref stasis_message_type's.
76 struct ast_channel_blob {
77 /*! Channel blob is associated with (or NULL for global/all channels) */
78 struct ast_channel_snapshot *snapshot;
79 /*! JSON blob of data */
80 struct ast_json *blob;
85 * \brief A set of channels with blob objects - see \ref ast_channel_blob
87 struct ast_multi_channel_blob;
91 * \brief A topic which publishes the events for all channels.
92 * \retval Topic for all channel events.
94 struct stasis_topic *ast_channel_topic_all(void);
98 * \brief A caching topic which caches \ref ast_channel_snapshot messages from
99 * ast_channel_events_all(void).
101 * \retval Topic for all channel events.
103 struct stasis_caching_topic *ast_channel_topic_all_cached(void);
107 * \brief Message type for \ref ast_channel_snapshot.
109 * \retval Message type for \ref ast_channel_snapshot.
111 struct stasis_message_type *ast_channel_snapshot_type(void);
115 * \brief Generate a snapshot of the channel state. This is an ao2 object, so
116 * ao2_cleanup() to deallocate.
118 * \param chan The channel from which to generate a snapshot
120 * \retval pointer on success (must be ast_freed)
121 * \retval NULL on error
123 struct ast_channel_snapshot *ast_channel_snapshot_create(
124 struct ast_channel *chan);
128 * \brief Obtain the latest \ref ast_channel_snapshot from the \ref stasis cache. This is
129 * an ao2 object, so use \ref ao2_cleanup() to deallocate.
131 * \param unique_id The channel's unique ID
133 * \retval A \ref ast_channel_snapshot on success
134 * \retval NULL on error
136 struct ast_channel_snapshot *ast_channel_snapshot_get_latest(const char *uniqueid);
140 * \brief Creates a \ref ast_channel_blob message.
142 * The given \a blob should be treated as immutable and not modified after it is
143 * put into the message.
145 * \param chan Channel blob is associated with, or \c NULL for global/all channels.
146 * \param type Message type for this blob.
147 * \param blob JSON object representing the data, or \c NULL for no data. If
148 * \c NULL, ast_json_null() is put into the object.
150 * \return \ref ast_channel_blob message.
151 * \return \c NULL on error
153 struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
154 struct stasis_message_type *type, struct ast_json *blob);
158 * \brief Creates a \ref ast_channel_blob message using the current cached
159 * \ref ast_channel_snapshot for the passed in \ref ast_channel
161 * The given \a blob should be treated as immutable and not modified after it is
162 * put into the message.
164 * \param chan Channel blob is associated with, or \c NULL for global/all channels.
165 * \param type Message type for this blob.
166 * \param blob JSON object representing the data, or \c NULL for no data. If
167 * \c NULL, ast_json_null() is put into the object.
169 * \param chan Channel blob is associated with
170 * \param blob JSON object representing the data.
171 * \return \ref ast_channel_blob message.
172 * \return \c NULL on error
174 struct stasis_message *ast_channel_cached_blob_create(struct ast_channel *chan,
175 struct stasis_message_type *type, struct ast_json *blob);
179 * \brief Create a \ref ast_channel_blob message, pulling channel state from
182 * \param uniqueid Uniqueid of the channel.
183 * \param type Message type for this blob.
184 * \param blob JSON object representing the data, or \c NULL for no data. If
185 * \c NULL, ast_json_null() is put into the object.
187 * \return \ref ast_channel_blob message.
188 * \return \c NULL on error
190 struct stasis_message *ast_channel_blob_create_from_cache(
191 const char *uniqueid, struct stasis_message_type *type,
192 struct ast_json *blob);
196 * \brief Create a \ref ast_multi_channel_blob suitable for a \ref stasis_message.
198 * The given \a blob should be treated as immutable and not modified after it is
199 * put into the message.
201 * \param blob The JSON blob that defines the data of this \ref ast_multi_channel_blob
203 * \return \ref ast_multi_channel_blob object
204 * \return \c NULL on error
206 struct ast_multi_channel_blob *ast_multi_channel_blob_create(struct ast_json *blob);
210 * \brief Retrieve a channel snapshot associated with a specific role from a
211 * \ref ast_multi_channel_blob
213 * \note The reference count of the \ref ast_channel_snapshot returned from
214 * this function is not changed. The caller of this function does not own the
215 * reference to the snapshot.
217 * \param obj The \ref ast_multi_channel_blob containing the channel snapshot
219 * \param role The role associated with the channel snapshot
221 * \retval \ref ast_channel_snapshot matching the role on success
222 * \retval NULL on error or not found for the role specified
224 struct ast_channel_snapshot *ast_multi_channel_blob_get_channel(
225 struct ast_multi_channel_blob *obj, const char *role);
229 * \brief Retrieve all channel snapshots associated with a specific role from
230 * a \ref ast_multi_channel_blob
232 * \note Because this function returns an ao2_container (hashed by channel name)
233 * of all channel snapshots that matched the passed in role, the reference of
234 * the snapshots is increased by this function. The caller of this function must
235 * release the reference to the snapshots by disposing of the container
238 * \param obj The \ref ast_multi_channel_blob containing the channel snapshots to
240 * \param role The role associated with the channel snapshots
242 * \retval A container containing all \ref ast_channel_snapshot objects matching
243 * the role on success.
244 * \retval NULL on error or not found for the role specified
246 struct ao2_container *ast_multi_channel_blob_get_channels(
247 struct ast_multi_channel_blob *obj, const char *role);
251 * \brief Retrieve the JSON blob from a \ref ast_multi_channel_blob.
252 * Returned \ref ast_json is still owned by \a obj
254 * \param obj Channel blob object.
255 * \return Type field value from the blob.
256 * \return \c NULL on error.
258 struct ast_json *ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj);
262 * \brief Add a \ref ast_channel_snapshot to a \ref ast_multi_channel_blob object
264 * \note This will increase the reference count by 1 for the channel snapshot. It is
265 * assumed that the \ref ast_multi_channel_blob will own a reference to the object.
267 * \param obj The \ref ast_multi_channel_blob object that will reference the snapshot
268 * \param role A \a role that the snapshot has in the multi channel relationship
269 * \param snapshot The \ref ast_channel_snapshot being added to the
270 * \ref ast_multi_channel_blob object
272 void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj,
273 const char *role, struct ast_channel_snapshot *snapshot);
277 * \brief Publish a \ref ast_channel_snapshot for a channel.
279 * \param chan Channel to publish.
281 void ast_channel_publish_snapshot(struct ast_channel *chan);
285 * \brief Publish a \ref ast_channel_varset for a channel.
287 * \param chan Channel to pulish the event for, or \c NULL for 'none'.
288 * \param variable Name of the variable being set
289 * \param value Value.
291 void ast_channel_publish_varset(struct ast_channel *chan,
292 const char *variable, const char *value);
296 * \brief Message type for when a channel dials another channel
298 * \retval A stasis message type
300 struct stasis_message_type *ast_channel_dial_type(void);
304 * \brief Message type for when a variable is set on a channel.
306 * \retval A stasis message type
308 struct stasis_message_type *ast_channel_varset_type(void);
312 * \brief Message type for when a custom user event is sent on a channel.
314 * \retval A stasis message type
316 struct stasis_message_type *ast_channel_user_event_type(void);
320 * \brief Message type for when a hangup is requested on a channel.
322 * \retval A stasis message type
324 struct stasis_message_type *ast_channel_hangup_request_type(void);
328 * \brief Message type for when DTMF begins on a channel.
330 * \retval A stasis message type
332 struct stasis_message_type *ast_channel_dtmf_begin_type(void);
336 * \brief Message type for when DTMF ends on a channel.
338 * \retval A stasis message type
340 struct stasis_message_type *ast_channel_dtmf_end_type(void);
344 * \brief Message type for when a channel is placed on hold.
346 * \retval A stasis message type
348 struct stasis_message_type *ast_channel_hold_type(void);
352 * \brief Message type for when a channel is removed from hold.
354 * \retval A stasis message type
356 struct stasis_message_type *ast_channel_unhold_type(void);
360 * \brief Message type for when a channel starts spying on another channel
362 * \retval A stasis message type
364 struct stasis_message_type *ast_channel_chanspy_start_type(void);
368 * \brief Message type for when a channel stops spying on another channel
370 * \retval A stasis message type
372 struct stasis_message_type *ast_channel_chanspy_stop_type(void);
376 * \brief Message type for a fax operation
378 * \retval A stasis message type
380 struct stasis_message_type *ast_channel_fax_type(void);
384 * \brief Message type for hangup handler related actions
386 * \retval A stasis message type
388 struct stasis_message_type *ast_channel_hangup_handler_type(void);
392 * \brief Message type for starting monitor on a channel
394 * \retval A stasis message type
396 struct stasis_message_type *ast_channel_monitor_start_type(void);
400 * \brief Message type for stopping monitor on a channel
402 * \retval A stasis message type
404 struct stasis_message_type *ast_channel_monitor_stop_type(void);
408 * \brief Message type for starting music on hold on a channel
410 * \retval A stasis message type
412 struct stasis_message_type *ast_channel_moh_start_type(void);
416 * \brief Message type for stopping music on hold on a channel
418 * \retval A stasis message type
420 struct stasis_message_type *ast_channel_moh_stop_type(void);
424 * \brief Publish in the \ref ast_channel_topic or \ref ast_channel_topic_all
425 * topics a stasis message for the channels involved in a dial operation.
427 * \param caller The channel performing the dial operation
428 * \param peer The channel being dialed
429 * \param dialstring When beginning a dial, the information passed to the
430 * dialing application
431 * \param dialstatus The current status of the dial operation (NULL if no
434 void ast_channel_publish_dial(struct ast_channel *caller,
435 struct ast_channel *peer,
436 const char *dialstring,
437 const char *dialstatus);
441 * \brief Publish in the \ref ast_channel_topic a \ref ast_channel_snapshot
442 * message indicating a change in channel state
444 * \param chan The channel whose state has changed
446 void ast_publish_channel_state(struct ast_channel *chan);
451 * \brief Build a JSON object from a \ref ast_channel_snapshot.
452 * \return JSON object representing channel snapshot.
453 * \return \c NULL on error
455 struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot);
458 * \brief Compares the context, exten and priority of two snapshots.
461 * \param old_snapshot Old snapshot
462 * \param new_snapshot New snapshot
464 * \return True (non-zero) if context, exten or priority are identical.
465 * \return False (zero) if context, exten and priority changed.
467 int ast_channel_snapshot_cep_equal(
468 const struct ast_channel_snapshot *old_snapshot,
469 const struct ast_channel_snapshot *new_snapshot);
472 * \brief Compares the callerid info of two snapshots.
475 * \param old_snapshot Old snapshot
476 * \param new_snapshot New snapshot
478 * \return True (non-zero) if callerid are identical.
479 * \return False (zero) if callerid changed.
481 int ast_channel_snapshot_caller_id_equal(
482 const struct ast_channel_snapshot *old_snapshot,
483 const struct ast_channel_snapshot *new_snapshot);
486 * \brief Dispose of the stasis channel topics and message types
488 void ast_stasis_channels_shutdown(void);
491 * \brief Initialize the stasis channel topic and message types
493 void ast_stasis_channels_init(void);
495 #endif /* STASIS_CHANNELS_H_ */