devicestate.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. /*! \file
  19. * \brief Device state management
  20. *
  21. * To subscribe to device state changes, use the stasis_subscribe
  22. * method. For an example, see apps/app_queue.c.
  23. *
  24. * \todo Currently, when the state of a device changes, the device state provider
  25. * calls one of the functions defined here to queue an object to say that the
  26. * state of a device has changed. However, this does not include the new state.
  27. * Another thread processes these device state change objects and calls the
  28. * device state provider's callback to figure out what the new state is. It
  29. * would make a lot more sense for the new state to be included in the original
  30. * function call that says the state of a device has changed. However, it
  31. * will take a lot of work to change this.
  32. *
  33. * \arg See \ref AstExtState
  34. */
  35. #ifndef _ASTERISK_DEVICESTATE_H
  36. #define _ASTERISK_DEVICESTATE_H
  37. #include "asterisk/channelstate.h"
  38. #include "asterisk/utils.h"
  39. #if defined(__cplusplus) || defined(c_plusplus)
  40. extern "C" {
  41. #endif
  42. /*! \brief Device States
  43. * \note The order of these states may not change because they are included
  44. * in Asterisk events which may be transmitted across the network to
  45. * other servers.
  46. */
  47. enum ast_device_state {
  48. AST_DEVICE_UNKNOWN, /*!< Device is valid but channel didn't know state */
  49. AST_DEVICE_NOT_INUSE, /*!< Device is not used */
  50. AST_DEVICE_INUSE, /*!< Device is in use */
  51. AST_DEVICE_BUSY, /*!< Device is busy */
  52. AST_DEVICE_INVALID, /*!< Device is invalid */
  53. AST_DEVICE_UNAVAILABLE, /*!< Device is unavailable */
  54. AST_DEVICE_RINGING, /*!< Device is ringing */
  55. AST_DEVICE_RINGINUSE, /*!< Device is ringing *and* in use */
  56. AST_DEVICE_ONHOLD, /*!< Device is on hold */
  57. AST_DEVICE_TOTAL, /*!< Total num of device states, used for testing */
  58. };
  59. /*! \brief Device State Cachability
  60. * \note This is used to define the cachability of a device state when set.
  61. */
  62. enum ast_devstate_cache {
  63. AST_DEVSTATE_NOT_CACHABLE, /*!< This device state is not cachable */
  64. AST_DEVSTATE_CACHABLE, /*!< This device state is cachable */
  65. };
  66. /*! \brief Devicestate provider call back */
  67. typedef enum ast_device_state (*ast_devstate_prov_cb_type)(const char *data);
  68. /*!
  69. * \brief Convert channel state to devicestate
  70. *
  71. * \param chanstate Current channel state
  72. * \since 1.6.1
  73. */
  74. enum ast_device_state ast_state_chan2dev(enum ast_channel_state chanstate);
  75. /*!
  76. * \brief Convert device state to text string for output
  77. *
  78. * \param devstate Current device state
  79. */
  80. const char *devstate2str(enum ast_device_state devstate) attribute_pure __attribute__((deprecated));
  81. const char *ast_devstate2str(enum ast_device_state devstate) attribute_pure;
  82. /*!
  83. * \brief Convert device state to text string that is easier to parse
  84. *
  85. * \param devstate Current device state
  86. */
  87. const char *ast_devstate_str(enum ast_device_state devstate) attribute_pure;
  88. /*!
  89. * \brief Convert device state from text to integer value
  90. *
  91. * \param val The text representing the device state. Valid values are anything
  92. * that comes after AST_DEVICE_ in one of the defined values.
  93. *
  94. * \return The AST_DEVICE_ integer value
  95. */
  96. enum ast_device_state ast_devstate_val(const char *val);
  97. /*!
  98. * \brief Search the Channels by Name
  99. *
  100. * \param device like a dial string
  101. *
  102. * Search the Device in active channels by compare the channel name against
  103. * the device name. Compared are only the first chars to the first '-' char.
  104. *
  105. * \retval AST_DEVICE_UNKNOWN if no channel found
  106. * \retval AST_DEVICE_INUSE if a channel is found
  107. */
  108. enum ast_device_state ast_parse_device_state(const char *device);
  109. /*!
  110. * \brief Asks a channel for device state
  111. *
  112. * \param device like a dial string
  113. *
  114. * Asks a channel for device state, data is normally a number from a dial string
  115. * used by the low level module
  116. * Tries the channel device state callback if not supported search in the
  117. * active channels list for the device.
  118. *
  119. * \retval an AST_DEVICE_??? state
  120. */
  121. enum ast_device_state ast_device_state(const char *device);
  122. /*!
  123. * \brief Tells Asterisk the State for Device is changed
  124. *
  125. * \param state the new state of the device
  126. * \param cachable whether this device state is cachable
  127. * \param fmt device name like a dial string with format parameters
  128. *
  129. * The new state of the device will be sent off to any subscribers
  130. * of device states. It will also be stored in the internal event
  131. * cache.
  132. *
  133. * \retval 0 on success
  134. * \retval -1 on failure
  135. */
  136. int ast_devstate_changed(enum ast_device_state state, enum ast_devstate_cache cachable, const char *fmt, ...)
  137. __attribute__((format(printf, 3, 4)));
  138. /*!
  139. * \brief Tells Asterisk the State for Device is changed
  140. *
  141. * \param state the new state of the device
  142. * \param cachable whether this device state is cachable
  143. * \param device device name like a dial string with format parameters
  144. *
  145. * The new state of the device will be sent off to any subscribers
  146. * of device states. It will also be stored in the internal event
  147. * cache.
  148. *
  149. * \retval 0 on success
  150. * \retval -1 on failure
  151. */
  152. int ast_devstate_changed_literal(enum ast_device_state state, enum ast_devstate_cache cachable, const char *device);
  153. /*!
  154. * \brief Tells Asterisk the State for Device is changed.
  155. * (Accept change notification, add it to change queue.)
  156. *
  157. * \param fmt device name like a dial string with format parameters
  158. *
  159. * Asterisk polls the new extension states and calls the registered
  160. * callbacks for the changed extensions
  161. *
  162. * \retval 0 on success
  163. * \retval -1 on failure
  164. *
  165. * \note This is deprecated in favor of ast_devstate_changed()
  166. * \version 1.6.1 deprecated
  167. */
  168. int ast_device_state_changed(const char *fmt, ...)
  169. __attribute__((deprecated,format(printf, 1, 2)));
  170. /*!
  171. * \brief Tells Asterisk the State for Device is changed
  172. *
  173. * \param device device name like a dial string
  174. *
  175. * Asterisk polls the new extension states and calls the registered
  176. * callbacks for the changed extensions
  177. *
  178. * \retval 0 on success
  179. * \retval -1 on failure
  180. *
  181. * \note This is deprecated in favor of ast_devstate_changed_literal()
  182. * \version 1.6.1 deprecated
  183. */
  184. int ast_device_state_changed_literal(const char *device)
  185. __attribute__((deprecated));
  186. /*!
  187. * \brief Add device state provider
  188. *
  189. * \param label to use in hint, like label:object
  190. * \param callback Callback
  191. *
  192. * \retval 0 success
  193. * \retval -1 failure
  194. */
  195. int ast_devstate_prov_add(const char *label, ast_devstate_prov_cb_type callback);
  196. /*!
  197. * \brief Remove device state provider
  198. *
  199. * \param label to use in hint, like label:object
  200. *
  201. * \retval -1 on failure
  202. * \retval 0 on success
  203. */
  204. int ast_devstate_prov_del(const char *label);
  205. /*!
  206. * \brief An object to hold state when calculating aggregate device state
  207. */
  208. struct ast_devstate_aggregate;
  209. /*!
  210. * \brief Initialize aggregate device state
  211. *
  212. * \param[in] agg the state object
  213. *
  214. * \return nothing
  215. * \since 1.6.1
  216. */
  217. void ast_devstate_aggregate_init(struct ast_devstate_aggregate *agg);
  218. /*!
  219. * \brief Add a device state to the aggregate device state
  220. *
  221. * \param[in] agg the state object
  222. * \param[in] state the state to add
  223. *
  224. * \return nothing
  225. * \since 1.6.1
  226. */
  227. void ast_devstate_aggregate_add(struct ast_devstate_aggregate *agg, enum ast_device_state state);
  228. /*!
  229. * \brief Get the aggregate device state result
  230. *
  231. * \param[in] agg the state object
  232. *
  233. * \return the aggregate device state after adding some number of device states.
  234. * \since 1.6.1
  235. */
  236. enum ast_device_state ast_devstate_aggregate_result(struct ast_devstate_aggregate *agg);
  237. /*!
  238. * \brief You shouldn't care about the contents of this struct
  239. *
  240. * This struct is only here so that it can be easily declared on the stack.
  241. */
  242. struct ast_devstate_aggregate {
  243. unsigned int ringing:1;
  244. unsigned int inuse:1;
  245. enum ast_device_state state;
  246. };
  247. /*!
  248. * \brief The structure that contains device state
  249. * \since 12
  250. */
  251. struct ast_device_state_message {
  252. /*! The name of the device */
  253. const char *device;
  254. /*!
  255. * \brief The EID of the server where this message originated.
  256. *
  257. * \note A NULL EID means aggregate state.
  258. */
  259. const struct ast_eid *eid;
  260. /*! The state of the device */
  261. enum ast_device_state state;
  262. /*! Flag designating the cachability of this device state */
  263. enum ast_devstate_cache cachable;
  264. /*! The device and eid data is stuffed here when the struct is allocated. */
  265. struct ast_eid stuff[0];
  266. };
  267. /*!
  268. * \brief Get the Stasis topic for device state messages
  269. * \retval The topic for device state messages
  270. * \retval NULL if it has not been allocated
  271. * \since 12
  272. */
  273. struct stasis_topic *ast_device_state_topic_all(void);
  274. /*!
  275. * \brief Get the Stasis topic for device state messages for a specific device
  276. * \param uniqueid The device for which to get the topic
  277. * \retval The topic structure for MWI messages for a given device
  278. * \retval NULL if it failed to be found or allocated
  279. * \since 12
  280. */
  281. struct stasis_topic *ast_device_state_topic(const char *device);
  282. /*!
  283. * \brief Get the Stasis caching topic for device state messages
  284. * \retval The caching topic for device state messages
  285. * \retval NULL if it has not been allocated
  286. * \since 12
  287. */
  288. struct stasis_topic *ast_device_state_topic_cached(void);
  289. /*!
  290. * \brief Backend cache for ast_device_state_topic_cached()
  291. * \retval Cache of \ref ast_device_state_message.
  292. * \since 12
  293. */
  294. struct stasis_cache *ast_device_state_cache(void);
  295. /*!
  296. * \brief Get the Stasis message type for device state messages
  297. * \retval The message type for device state messages
  298. * \retval NULL if it has not been allocated
  299. * \since 12
  300. */
  301. struct stasis_message_type *ast_device_state_message_type(void);
  302. /*!
  303. * \brief Clear the device from the stasis cache.
  304. * \param The device to clear
  305. * \retval 0 if successful
  306. * \retval -1 nothing to clear
  307. * \since 12
  308. */
  309. int ast_device_state_clear_cache(const char *device);
  310. /*!
  311. * \brief Initialize the device state core
  312. * \retval 0 Success
  313. * \retval -1 Failure
  314. * \since 12
  315. */
  316. int devstate_init(void);
  317. /*!
  318. * \brief Publish a device state update
  319. * \param[in] device The device name
  320. * \param[in] state The state of the device
  321. * \param[in] cachable Whether the device state can be cached
  322. * \retval 0 Success
  323. * \retval -1 Failure
  324. * \since 12
  325. */
  326. #define ast_publish_device_state(device, state, cachable) \
  327. ast_publish_device_state_full(device, state, cachable, &ast_eid_default)
  328. /*!
  329. * \brief Publish a device state update with EID
  330. * \param[in] device The device name
  331. * \param[in] state The state of the device
  332. * \param[in] cachable Whether the device state can be cached
  333. * \param[in] eid The EID of the server that originally published the message
  334. * \retval 0 Success
  335. * \retval -1 Failure
  336. * \since 12
  337. */
  338. int ast_publish_device_state_full(
  339. const char *device,
  340. enum ast_device_state state,
  341. enum ast_devstate_cache cachable,
  342. struct ast_eid *eid);
  343. #if defined(__cplusplus) || defined(c_plusplus)
  344. }
  345. #endif
  346. #endif /* _ASTERISK_DEVICESTATE_H */