devicestate.h

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (C) 1999 - 2005, Digium, Inc.
00005  *
00006  * Mark Spencer <markster@digium.com>
00007  *
00008  * See http://www.asterisk.org for more information about
00009  * the Asterisk project. Please do not directly contact
00010  * any of the maintainers of this project for assistance;
00011  * the project provides a web site, mailing lists and IRC
00012  * channels for your use.
00013  *
00014  * This program is free software, distributed under the terms of
00015  * the GNU General Public License Version 2. See the LICENSE file
00016  * at the top of the source tree.
00017  */
00018 
00019 /*! \file
00020  * \brief Device state management
00021  *
00022  * To subscribe to device state changes, use the stasis_subscribe
00023  * method.  For an example, see apps/app_queue.c.
00024  *
00025  * \todo Currently, when the state of a device changes, the device state provider
00026  * calls one of the functions defined here to queue an object to say that the
00027  * state of a device has changed.  However, this does not include the new state.
00028  * Another thread processes these device state change objects and calls the
00029  * device state provider's callback to figure out what the new state is.  It
00030  * would make a lot more sense for the new state to be included in the original
00031  * function call that says the state of a device has changed.  However, it
00032  * will take a lot of work to change this.
00033  *
00034  * \arg See \ref AstExtState
00035  */
00036 
00037 #ifndef _ASTERISK_DEVICESTATE_H
00038 #define _ASTERISK_DEVICESTATE_H
00039 
00040 #include "asterisk/channelstate.h"
00041 #include "asterisk/utils.h"
00042 
00043 #if defined(__cplusplus) || defined(c_plusplus)
00044 extern "C" {
00045 #endif
00046 
00047 /*! \brief Device States
00048  *  \note The order of these states may not change because they are included
00049  *        in Asterisk events which may be transmitted across the network to
00050  *        other servers.
00051  */
00052 enum ast_device_state {
00053    AST_DEVICE_UNKNOWN,      /*!< Device is valid but channel didn't know state */
00054    AST_DEVICE_NOT_INUSE,    /*!< Device is not used */
00055    AST_DEVICE_INUSE,        /*!< Device is in use */
00056    AST_DEVICE_BUSY,         /*!< Device is busy */
00057    AST_DEVICE_INVALID,      /*!< Device is invalid */
00058    AST_DEVICE_UNAVAILABLE,  /*!< Device is unavailable */
00059    AST_DEVICE_RINGING,      /*!< Device is ringing */
00060    AST_DEVICE_RINGINUSE,    /*!< Device is ringing *and* in use */
00061    AST_DEVICE_ONHOLD,       /*!< Device is on hold */
00062    AST_DEVICE_TOTAL,        /*!< Total num of device states, used for testing */
00063 };
00064 
00065 /*! \brief Device State Cachability
00066  *  \note This is used to define the cachability of a device state when set.
00067  */
00068 enum ast_devstate_cache {
00069    AST_DEVSTATE_NOT_CACHABLE,  /*!< This device state is not cachable */
00070    AST_DEVSTATE_CACHABLE,      /*!< This device state is cachable */
00071 };
00072 
00073 /*! \brief Devicestate provider call back */
00074 typedef enum ast_device_state (*ast_devstate_prov_cb_type)(const char *data);
00075 
00076 /*!
00077  * \brief Convert channel state to devicestate
00078  *
00079  * \param chanstate Current channel state
00080  * \since 1.6.1
00081  */
00082 enum ast_device_state ast_state_chan2dev(enum ast_channel_state chanstate);
00083 
00084 /*!
00085  * \brief Convert device state to text string for output
00086  *
00087  * \param devstate Current device state
00088  */
00089 const char *devstate2str(enum ast_device_state devstate) attribute_pure __attribute__((deprecated));
00090 const char *ast_devstate2str(enum ast_device_state devstate) attribute_pure;
00091 
00092 /*!
00093  * \brief Convert device state to text string that is easier to parse
00094  *
00095  * \param devstate Current device state
00096  */
00097 const char *ast_devstate_str(enum ast_device_state devstate) attribute_pure;
00098 
00099 /*!
00100  * \brief Convert device state from text to integer value
00101  *
00102  * \param val The text representing the device state.  Valid values are anything
00103  *        that comes after AST_DEVICE_ in one of the defined values.
00104  *
00105  * \return The AST_DEVICE_ integer value
00106  */
00107 enum ast_device_state ast_devstate_val(const char *val);
00108 
00109 /*!
00110  * \brief Search the Channels by Name
00111  *
00112  * \param device like a dial string
00113  *
00114  * Search the Device in active channels by compare the channel name against
00115  * the device name. Compared are only the first chars to the first '-' char.
00116  *
00117  * \retval AST_DEVICE_UNKNOWN if no channel found
00118  * \retval AST_DEVICE_INUSE if a channel is found
00119  */
00120 enum ast_device_state ast_parse_device_state(const char *device);
00121 
00122 /*!
00123  * \brief Asks a channel for device state
00124  *
00125  * \param device like a dial string
00126  *
00127  * Asks a channel for device state, data is normally a number from a dial string
00128  * used by the low level module
00129  * Tries the channel device state callback if not supported search in the
00130  * active channels list for the device.
00131  *
00132  * \retval an AST_DEVICE_??? state
00133  */
00134 enum ast_device_state ast_device_state(const char *device);
00135 
00136 /*!
00137  * \brief Tells Asterisk the State for Device is changed
00138  *
00139  * \param state the new state of the device
00140  * \param cachable whether this device state is cachable
00141  * \param fmt device name like a dial string with format parameters
00142  *
00143  * The new state of the device will be sent off to any subscribers
00144  * of device states.  It will also be stored in the internal event
00145  * cache.
00146  *
00147  * \retval 0 on success
00148  * \retval -1 on failure
00149  */
00150 int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt, ...)
00151    __attribute__((format(printf, 3, 4)));
00152 
00153 /*!
00154  * \brief Tells Asterisk the State for Device is changed
00155  *
00156  * \param state the new state of the device
00157  * \param cachable whether this device state is cachable
00158  * \param device device name like a dial string with format parameters
00159  *
00160  * The new state of the device will be sent off to any subscribers
00161  * of device states.  It will also be stored in the internal event
00162  * cache.
00163  *
00164  * \retval 0 on success
00165  * \retval -1 on failure
00166  */
00167 int ast_devstate_changed_literal(enum ast_device_state state, enum ast_devstate_cache cachable, const char *device);
00168 
00169 /*!
00170  * \brief Tells Asterisk the State for Device is changed.
00171  * (Accept change notification, add it to change queue.)
00172  *
00173  * \param fmt device name like a dial string with format parameters
00174  *
00175  * Asterisk polls the new extension states and calls the registered
00176  * callbacks for the changed extensions
00177  *
00178  * \retval 0 on success
00179  * \retval -1 on failure
00180  *
00181  * \note This is deprecated in favor of ast_devstate_changed()
00182  * \version 1.6.1 deprecated
00183  */
00184 int ast_device_state_changed(const char *fmt, ...)
00185    __attribute__((deprecated,format(printf, 1, 2)));
00186 
00187 /*!
00188  * \brief Tells Asterisk the State for Device is changed
00189  *
00190  * \param device device name like a dial string
00191  *
00192  * Asterisk polls the new extension states and calls the registered
00193  * callbacks for the changed extensions
00194  *
00195  * \retval 0 on success
00196  * \retval -1 on failure
00197  *
00198  * \note This is deprecated in favor of ast_devstate_changed_literal()
00199  * \version 1.6.1 deprecated
00200  */
00201 int ast_device_state_changed_literal(const char *device)
00202    __attribute__((deprecated));
00203 
00204 /*!
00205  * \brief Add device state provider
00206  *
00207  * \param label to use in hint, like label:object
00208  * \param callback Callback
00209  *
00210  * \retval 0 success
00211  * \retval -1 failure
00212  */
00213 int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback);
00214 
00215 /*!
00216  * \brief Remove device state provider
00217  *
00218  * \param label to use in hint, like label:object
00219  *
00220  * \retval -1 on failure
00221  * \retval 0 on success
00222  */
00223 int ast_devstate_prov_del(const char *label);
00224 
00225 /*!
00226  * \brief An object to hold state when calculating aggregate device state
00227  */
00228 struct ast_devstate_aggregate;
00229 
00230 /*!
00231  * \brief Initialize aggregate device state
00232  *
00233  * \param[in] agg the state object
00234  *
00235  * \return nothing
00236  * \since 1.6.1
00237  */
00238 void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg);
00239 
00240 /*!
00241  * \brief Add a device state to the aggregate device state
00242  *
00243  * \param[in] agg the state object
00244  * \param[in] state the state to add
00245  *
00246  * \return nothing
00247  * \since 1.6.1
00248  */
00249 void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state);
00250 
00251 /*!
00252  * \brief Get the aggregate device state result
00253  *
00254  * \param[in] agg the state object
00255  *
00256  * \return the aggregate device state after adding some number of device states.
00257  * \since 1.6.1
00258  */
00259 enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg);
00260 
00261 /*!
00262  * \brief You shouldn't care about the contents of this struct
00263  *
00264  * This struct is only here so that it can be easily declared on the stack.
00265  */
00266 struct ast_devstate_aggregate {
00267    unsigned int ringing:1;
00268    unsigned int inuse:1;
00269    enum ast_device_state state;
00270 };
00271 
00272 /*!
00273  * \brief The structure that contains device state
00274  * \since 12
00275  */
00276 struct ast_device_state_message {
00277    /*! The name of the device */
00278    const char *device;
00279    /*!
00280     * \brief The EID of the server where this message originated.
00281     *
00282     * \note A NULL EID means aggregate state.
00283     */
00284    const struct ast_eid *eid;
00285    /*! The state of the device */
00286    enum ast_device_state state;
00287    /*! Flag designating the cachability of this device state */
00288    enum ast_devstate_cache cachable;
00289    /*! The device and eid data is stuffed here when the struct is allocated. */
00290    struct ast_eid stuff[0];
00291 };
00292 
00293 /*!
00294  * \brief Get the Stasis topic for device state messages
00295  * \retval The topic for device state messages
00296  * \retval NULL if it has not been allocated
00297  * \since 12
00298  */
00299 struct stasis_topic *ast_device_state_topic_all(void);
00300 
00301 /*!
00302  * \brief Get the Stasis topic for device state messages for a specific device
00303  * \param uniqueid The device for which to get the topic
00304  * \retval The topic structure for MWI messages for a given device
00305  * \retval NULL if it failed to be found or allocated
00306  * \since 12
00307  */
00308 struct stasis_topic *ast_device_state_topic(const char *device);
00309 
00310 /*!
00311  * \brief Get the Stasis caching topic for device state messages
00312  * \retval The caching topic for device state messages
00313  * \retval NULL if it has not been allocated
00314  * \since 12
00315  */
00316 struct stasis_topic *ast_device_state_topic_cached(void);
00317 
00318 /*!
00319  * \brief Backend cache for ast_device_state_topic_cached()
00320  * \retval Cache of \ref ast_device_state_message.
00321  * \since 12
00322  */
00323 struct stasis_cache *ast_device_state_cache(void);
00324 
00325 /*!
00326  * \brief Get the Stasis message type for device state messages
00327  * \retval The message type for device state messages
00328  * \retval NULL if it has not been allocated
00329  * \since 12
00330  */
00331 struct stasis_message_type *ast_device_state_message_type(void);
00332 
00333 /*!
00334  * \brief Clear the device from the stasis cache.
00335  * \param The device to clear
00336  * \retval 0 if successful
00337  * \retval -1 nothing to clear
00338  * \since 12
00339  */
00340 int ast_device_state_clear_cache(const char *device);
00341 
00342 /*!
00343  * \brief Initialize the device state core
00344  * \retval 0 Success
00345  * \retval -1 Failure
00346  * \since 12
00347  */
00348 int devstate_init(void);
00349 
00350 /*!
00351  * \brief Publish a device state update
00352  * \param[in] device The device name
00353  * \param[in] state The state of the device
00354  * \param[in] cachable Whether the device state can be cached
00355  * \retval 0 Success
00356  * \retval -1 Failure
00357  * \since 12
00358  */
00359 #define ast_publish_device_state(device, state, cachable) \
00360    ast_publish_device_state_full(device, state, cachable, &ast_eid_default)
00361 
00362 /*!
00363  * \brief Publish a device state update with EID
00364  * \param[in] device The device name
00365  * \param[in] state The state of the device
00366  * \param[in] cachable Whether the device state can be cached
00367  * \param[in] eid The EID of the server that originally published the message
00368  * \retval 0 Success
00369  * \retval -1 Failure
00370  * \since 12
00371  */
00372 int ast_publish_device_state_full(
00373          const char *device,
00374          enum ast_device_state state,
00375          enum ast_devstate_cache cachable,
00376          struct ast_eid *eid);
00377 
00378 #if defined(__cplusplus) || defined(c_plusplus)
00379 }
00380 #endif
00381 
00382 #endif /* _ASTERISK_DEVICESTATE_H */

Generated on Thu Apr 16 06:27:32 2015 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6