50e43ce14f114595642d84fd5e1e1864ff25c8fd
[asterisk/asterisk.git] / include / asterisk / devicestate.h
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  * \brief Device state management
21  *
22  * To subscribe to device state changes, use the generic ast_event_subscribe
23  * method.  For an example, see apps/app_queue.c.
24  *
25  * \todo Currently, when the state of a device changes, the device state provider
26  * calls one of the functions defined here to queue an object to say that the 
27  * state of a device has changed.  However, this does not include the new state.
28  * Another thread processes these device state change objects and calls the
29  * device state provider's callback to figure out what the new state is.  It
30  * would make a lot more sense for the new state to be included in the original
31  * function call that says the state of a device has changed.  However, it
32  * will take a lot of work to change this.
33  *
34  * \arg See \ref AstExtState
35  */
36
37 #ifndef _ASTERISK_DEVICESTATE_H
38 #define _ASTERISK_DEVICESTATE_H
39
40 #if defined(__cplusplus) || defined(c_plusplus)
41 extern "C" {
42 #endif
43
44 /*! \brief Device States 
45  *  \note The order of these states may not change because they are included
46  *        in Asterisk events which may be transmitted across the network to
47  *        other servers.
48  */
49 enum ast_device_state {
50         AST_DEVICE_UNKNOWN,      /*!< Device is valid but channel didn't know state */
51         AST_DEVICE_NOT_INUSE,    /*!< Device is not used */
52         AST_DEVICE_INUSE,        /*!< Device is in use */
53         AST_DEVICE_BUSY,         /*!< Device is busy */
54         AST_DEVICE_INVALID,      /*!< Device is invalid */
55         AST_DEVICE_UNAVAILABLE,  /*!< Device is unavailable */
56         AST_DEVICE_RINGING,      /*!< Device is ringing */
57         AST_DEVICE_RINGINUSE,    /*!< Device is ringing *and* in use */
58         AST_DEVICE_ONHOLD,       /*!< Device is on hold */
59 };
60
61 /*! \brief Devicestate provider call back */
62 typedef enum ast_device_state (*ast_devstate_prov_cb_type)(const char *data);
63
64 /*! 
65  * \brief Convert device state to text string for output 
66  *
67  * \param devstate Current device state 
68  */
69 const char *devstate2str(enum ast_device_state devstate);
70
71 /*! 
72  * \brief Convert device state to text string that is easier to parse 
73  *
74  * \param devstate Current device state 
75  */
76 const char *ast_devstate_str(enum ast_device_state devstate);
77
78 /*! 
79  * \brief Convert device state from text to integer value
80  *
81  * \param val The text representing the device state.  Valid values are anything
82  *        that comes after AST_DEVICE_ in one of the defined values.
83  *
84  * \return The AST_DEVICE_ integer value
85  */
86 enum ast_device_state ast_devstate_val(const char *val);
87
88 /*! 
89  * \brief Search the Channels by Name
90  *
91  * \param device like a dial string
92  *
93  * Search the Device in active channels by compare the channel name against 
94  * the device name. Compared are only the first chars to the first '-' char.
95  *
96  * \retval AST_DEVICE_UNKNOWN if no channel found
97  * \retval AST_DEVICE_INUSE if a channel is found
98  */
99 enum ast_device_state ast_parse_device_state(const char *device);
100
101 /*! 
102  * \brief Asks a channel for device state
103  *
104  * \param device like a dial string
105  *
106  * Asks a channel for device state, data is normally a number from a dial string
107  * used by the low level module
108  * Tries the channel device state callback if not supported search in the
109  * active channels list for the device.
110  *
111  * \retval an AST_DEVICE_??? state 
112  * \retval -1 on failure
113  */
114 enum ast_device_state ast_device_state(const char *device);
115
116 /*! 
117  * \brief Tells Asterisk the State for Device is changed
118  *
119  * \param state the new state of the device
120  * \param fmt device name like a dial string with format parameters
121  *
122  * The new state of the device will be sent off to any subscribers
123  * of device states.  It will also be stored in the internal event
124  * cache.
125  *
126  * \retval 0 on success 
127  * \retval -1 on failure
128  */
129 int ast_devstate_changed(enum ast_device_state state, const char *fmt, ...)
130         __attribute__ ((format (printf, 2, 3)));
131
132 /*! 
133  * \brief Tells Asterisk the State for Device is changed
134  *
135  * \param state the new state of the device
136  * \param device device name like a dial string with format parameters
137  *
138  * The new state of the device will be sent off to any subscribers
139  * of device states.  It will also be stored in the internal event
140  * cache.
141  *
142  * \retval 0 on success 
143  * \retval -1 on failure
144  */
145 int ast_devstate_changed_literal(enum ast_device_state state, const char *device);
146
147 /*! 
148  * \brief Tells Asterisk the State for Device is changed
149  *
150  * \param fmt device name like a dial string with format parameters
151  *
152  * Asterisk polls the new extension states and calls the registered
153  * callbacks for the changed extensions
154  *
155  * \retval 0 on success 
156  * \retval -1 on failure
157  *
158  * \note This is deprecated in favor of ast_devstate_changed()
159  */
160 int ast_device_state_changed(const char *fmt, ...)
161         __attribute__ ((format (printf, 1, 2)));
162
163 /*! 
164  * \brief Tells Asterisk the State for Device is changed 
165  *
166  * \param device device name like a dial string
167  *
168  * Asterisk polls the new extension states and calls the registered
169  * callbacks for the changed extensions
170  *
171  * \retval 0 on success 
172  * \retval -1 on failure
173  *
174  * \note This is deprecated in favor of ast_devstate_changed_literal()
175  */
176 int ast_device_state_changed_literal(const char *device);
177
178 /*! 
179  * \brief Add device state provider 
180  *
181  * \param label to use in hint, like label:object
182  * \param callback Callback
183  *
184  * \retval 0 success
185  * \retval -1 failure
186  */ 
187 int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback);
188
189 /*! 
190  * \brief Remove device state provider 
191  *
192  * \param label to use in hint, like label:object
193  *
194  * \retval -1 on failure 
195  * \retval 0 on success
196  */ 
197 int ast_devstate_prov_del(const char *label);
198
199 /*!
200  * \brief An object to hold state when calculating aggregate device state
201  */
202 struct ast_devstate_aggregate;
203
204 /*!
205  * \brief Initialize aggregate device state
206  *
207  * \param agg the state object
208  *
209  * \return nothing
210  */
211 void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg);
212
213 /*!
214  * \brief Add a device state to the aggregate device state
215  *
216  * \param agg the state object
217  * \param state the state to add
218  *
219  * \return nothing
220  */
221 void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state);
222
223 /*!
224  * \brief Get the aggregate device state result
225  *
226  * \param agg the state object
227  *
228  * \return the aggregate device state after adding some number of device states.
229  */
230 enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg);
231
232 /*! 
233  * \brief You shouldn't care about the contents of this struct 
234  *
235  * This struct is only here so that it can be easily declared on the stack.
236  */
237 struct ast_devstate_aggregate {
238         unsigned int all_unavail:1;
239         unsigned int all_busy:1;
240         unsigned int all_free:1;
241         unsigned int all_on_hold:1;
242         unsigned int busy:1;
243         unsigned int in_use:1;
244         unsigned int ring:1;
245 };
246
247 #if defined(__cplusplus) || defined(c_plusplus)
248 }
249 #endif
250
251 #endif /* _ASTERISK_DEVICESTATE_H */