func_extstate.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2007, Digium, Inc.
  5. *
  6. * Modified from func_devstate.c by Russell Bryant <russell@digium.com>
  7. * Adam Gundy <adam@starsilk.net>
  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. *
  20. * \brief Get the state of a hinted extension for dialplan control
  21. *
  22. * \author Adam Gundy <adam@starsilk.net>
  23. *
  24. * \ingroup functions
  25. */
  26. /*** MODULEINFO
  27. <support_level>core</support_level>
  28. ***/
  29. #include "asterisk.h"
  30. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  31. #include "asterisk/module.h"
  32. #include "asterisk/channel.h"
  33. #include "asterisk/pbx.h"
  34. #include "asterisk/utils.h"
  35. #include "asterisk/devicestate.h"
  36. /*** DOCUMENTATION
  37. <function name="EXTENSION_STATE" language="en_US">
  38. <synopsis>
  39. Get an extension's state.
  40. </synopsis>
  41. <syntax argsep="@">
  42. <parameter name="extension" required="true" />
  43. <parameter name="context">
  44. <para>If it is not specified defaults to <literal>default</literal>.</para>
  45. </parameter>
  46. </syntax>
  47. <description>
  48. <para>The EXTENSION_STATE function can be used to retrieve the state from any
  49. hinted extension. For example:</para>
  50. <para>NoOp(1234@default has state ${EXTENSION_STATE(1234)})</para>
  51. <para>NoOp(4567@home has state ${EXTENSION_STATE(4567@home)})</para>
  52. <para>The possible values returned by this function are:</para>
  53. <para>UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING |
  54. RINGINUSE | HOLDINUSE | ONHOLD</para>
  55. </description>
  56. </function>
  57. ***/
  58. static const char *ast_extstate_str(int state)
  59. {
  60. const char *res = "UNKNOWN";
  61. switch (state) {
  62. case AST_EXTENSION_NOT_INUSE:
  63. res = "NOT_INUSE";
  64. break;
  65. case AST_EXTENSION_INUSE:
  66. res = "INUSE";
  67. break;
  68. case AST_EXTENSION_BUSY:
  69. res = "BUSY";
  70. break;
  71. case AST_EXTENSION_UNAVAILABLE:
  72. res = "UNAVAILABLE";
  73. break;
  74. case AST_EXTENSION_RINGING:
  75. res = "RINGING";
  76. break;
  77. case AST_EXTENSION_INUSE | AST_EXTENSION_RINGING:
  78. res = "RINGINUSE";
  79. break;
  80. case AST_EXTENSION_INUSE | AST_EXTENSION_ONHOLD:
  81. res = "HOLDINUSE";
  82. break;
  83. case AST_EXTENSION_ONHOLD:
  84. res = "ONHOLD";
  85. break;
  86. }
  87. return res;
  88. }
  89. static int extstate_read(struct ast_channel *chan, const char *cmd, char *data,
  90. char *buf, size_t len)
  91. {
  92. char *exten, *context;
  93. if (ast_strlen_zero(data)) {
  94. ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
  95. return -1;
  96. }
  97. context = exten = data;
  98. strsep(&context, "@");
  99. if (ast_strlen_zero(context))
  100. context = "default";
  101. if (ast_strlen_zero(exten)) {
  102. ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
  103. return -1;
  104. }
  105. ast_copy_string(buf,
  106. ast_extstate_str(ast_extension_state(chan, context, exten)), len);
  107. return 0;
  108. }
  109. static struct ast_custom_function extstate_function = {
  110. .name = "EXTENSION_STATE",
  111. .read = extstate_read,
  112. .read_max = 12,
  113. };
  114. static int unload_module(void)
  115. {
  116. int res;
  117. res = ast_custom_function_unregister(&extstate_function);
  118. return res;
  119. }
  120. static int load_module(void)
  121. {
  122. int res;
  123. res = ast_custom_function_register(&extstate_function);
  124. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  125. }
  126. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets an extension's state in the dialplan");