res_manager_devicestate.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2014, Digium, Inc.
  5. *
  6. * Mark Michelson <mmichelson@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. /*** MODULEINFO
  19. <support_level>core</support_level>
  20. ***/
  21. /*** DOCUMENTATION
  22. <manager name="DeviceStateList" language="en_US">
  23. <synopsis>
  24. List the current known device states.
  25. </synopsis>
  26. <syntax>
  27. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  28. </syntax>
  29. <description>
  30. <para>This will list out all known device states in a
  31. sequence of <replaceable>DeviceStateChange</replaceable> events.
  32. When finished, a <replaceable>DeviceStateListComplete</replaceable> event
  33. will be emitted.</para>
  34. </description>
  35. <see-also>
  36. <ref type="managerEvent">DeviceStateChange</ref>
  37. <ref type="function">DEVICE_STATE</ref>
  38. </see-also>
  39. <responses>
  40. <list-elements>
  41. <xi:include xpointer="xpointer(/docs/managerEvent[@name='DeviceStateChange'])" />
  42. </list-elements>
  43. <managerEvent name="DeviceStateListComplete" language="en_US">
  44. <managerEventInstance class="EVENT_FLAG_COMMAND">
  45. <synopsis>
  46. Indicates the end of the list the current known extension states.
  47. </synopsis>
  48. <syntax>
  49. <parameter name="EventList">
  50. <para>Conveys the status of the event list.</para>
  51. </parameter>
  52. <parameter name="ListItems">
  53. <para>Conveys the number of statuses reported.</para>
  54. </parameter>
  55. </syntax>
  56. </managerEventInstance>
  57. </managerEvent>
  58. </responses>
  59. </manager>
  60. ***/
  61. #include "asterisk.h"
  62. #include "asterisk/module.h"
  63. #include "asterisk/manager.h"
  64. #include "asterisk/stasis.h"
  65. #include "asterisk/devicestate.h"
  66. static struct stasis_forward *topic_forwarder;
  67. static int action_devicestatelist(struct mansession *s, const struct message *m)
  68. {
  69. RAII_VAR(struct ao2_container *, device_states, NULL, ao2_cleanup);
  70. const char *action_id = astman_get_header(m, "ActionID");
  71. struct stasis_message *msg;
  72. struct ao2_iterator it_states;
  73. int count = 0;
  74. device_states = stasis_cache_dump_by_eid(ast_device_state_cache(),
  75. ast_device_state_message_type(), NULL);
  76. if (!device_states) {
  77. astman_send_error(s, m, "Memory Allocation Failure");
  78. return 0;
  79. }
  80. astman_send_listack(s, m, "Device State Changes will follow", "start");
  81. it_states = ao2_iterator_init(device_states, 0);
  82. for (; (msg = ao2_iterator_next(&it_states)); ao2_ref(msg, -1)) {
  83. struct ast_manager_event_blob *blob = stasis_message_to_ami(msg);
  84. if (!blob) {
  85. continue;
  86. }
  87. count++;
  88. astman_append(s, "Event: %s\r\n", blob->manager_event);
  89. if (!ast_strlen_zero(action_id)) {
  90. astman_append(s, "ActionID: %s\r\n", action_id);
  91. }
  92. astman_append(s, "%s\r\n", blob->extra_fields);
  93. ao2_ref(blob, -1);
  94. }
  95. ao2_iterator_destroy(&it_states);
  96. astman_send_list_complete_start(s, m, "DeviceStateListComplete", count);
  97. astman_send_list_complete_end(s);
  98. return 0;
  99. }
  100. static int unload_module(void)
  101. {
  102. topic_forwarder = stasis_forward_cancel(topic_forwarder);
  103. ast_manager_unregister("DeviceStateList");
  104. return 0;
  105. }
  106. static int load_module(void)
  107. {
  108. struct stasis_topic *manager_topic;
  109. manager_topic = ast_manager_get_topic();
  110. if (!manager_topic) {
  111. return AST_MODULE_LOAD_DECLINE;
  112. }
  113. topic_forwarder = stasis_forward_all(ast_device_state_topic_all(), manager_topic);
  114. if (!topic_forwarder) {
  115. return AST_MODULE_LOAD_DECLINE;
  116. }
  117. if (ast_manager_register_xml("DeviceStateList", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING,
  118. action_devicestatelist)) {
  119. topic_forwarder = stasis_forward_cancel(topic_forwarder);
  120. return AST_MODULE_LOAD_DECLINE;
  121. }
  122. return AST_MODULE_LOAD_SUCCESS;
  123. }
  124. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Manager Device State Topic Forwarder",
  125. .support_level = AST_MODULE_SUPPORT_CORE,
  126. .load = load_module,
  127. .unload = unload_module,
  128. .load_pri = AST_MODPRI_DEVSTATE_CONSUMER,
  129. );