res_manager_devicestate.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_append(s, "Event: DeviceStateListComplete\r\n");
  97. if (!ast_strlen_zero(action_id)) {
  98. astman_append(s, "ActionID: %s\r\n", action_id);
  99. }
  100. astman_append(s, "EventList: Complete\r\n"
  101. "ListItems: %d\r\n\r\n", count);
  102. return 0;
  103. }
  104. static int unload_module(void)
  105. {
  106. topic_forwarder = stasis_forward_cancel(topic_forwarder);
  107. ast_manager_unregister("DeviceStateList");
  108. return 0;
  109. }
  110. static int load_module(void)
  111. {
  112. struct stasis_topic *manager_topic;
  113. manager_topic = ast_manager_get_topic();
  114. if (!manager_topic) {
  115. return AST_MODULE_LOAD_DECLINE;
  116. }
  117. topic_forwarder = stasis_forward_all(ast_device_state_topic_all(), manager_topic);
  118. if (!topic_forwarder) {
  119. return AST_MODULE_LOAD_DECLINE;
  120. }
  121. if (ast_manager_register_xml("DeviceStateList", EVENT_FLAG_CALL | EVENT_FLAG_REPORTING,
  122. action_devicestatelist)) {
  123. topic_forwarder = stasis_forward_cancel(topic_forwarder);
  124. return AST_MODULE_LOAD_DECLINE;
  125. }
  126. return AST_MODULE_LOAD_SUCCESS;
  127. }
  128. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Manager Device State Topic Forwarder",
  129. .support_level = AST_MODULE_SUPPORT_CORE,
  130. .load = load_module,
  131. .unload = unload_module,
  132. .load_pri = AST_MODPRI_DEVSTATE_CONSUMER,
  133. );