func_extstate.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include "asterisk.h"
  27. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  28. #include "asterisk/module.h"
  29. #include "asterisk/channel.h"
  30. #include "asterisk/pbx.h"
  31. #include "asterisk/utils.h"
  32. #include "asterisk/devicestate.h"
  33. static const char *ast_extstate_str(int state)
  34. {
  35. const char *res = "UNKNOWN";
  36. switch (state) {
  37. case AST_EXTENSION_NOT_INUSE:
  38. res = "NOT_INUSE";
  39. break;
  40. case AST_EXTENSION_INUSE:
  41. res = "INUSE";
  42. break;
  43. case AST_EXTENSION_BUSY:
  44. res = "BUSY";
  45. break;
  46. case AST_EXTENSION_UNAVAILABLE:
  47. res = "UNAVAILABLE";
  48. break;
  49. case AST_EXTENSION_RINGING:
  50. res = "RINGING";
  51. break;
  52. case AST_EXTENSION_INUSE | AST_EXTENSION_RINGING:
  53. res = "RINGINUSE";
  54. break;
  55. case AST_EXTENSION_INUSE | AST_EXTENSION_ONHOLD:
  56. res = "HOLDINUSE";
  57. break;
  58. case AST_EXTENSION_ONHOLD:
  59. res = "ONHOLD";
  60. break;
  61. }
  62. return res;
  63. }
  64. static int extstate_read(struct ast_channel *chan, const char *cmd, char *data,
  65. char *buf, size_t len)
  66. {
  67. char *exten, *context;
  68. if (ast_strlen_zero(data)) {
  69. ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
  70. return -1;
  71. }
  72. context = exten = data;
  73. strsep(&context, "@");
  74. if (ast_strlen_zero(context))
  75. context = "default";
  76. if (ast_strlen_zero(exten)) {
  77. ast_log(LOG_WARNING, "EXTENSION_STATE requires an extension\n");
  78. return -1;
  79. }
  80. ast_copy_string(buf,
  81. ast_extstate_str(ast_extension_state(chan, context, exten)), len);
  82. return 0;
  83. }
  84. static struct ast_custom_function extstate_function = {
  85. .name = "EXTENSION_STATE",
  86. .synopsis = "Get an extension's state",
  87. .syntax = "EXTENSION_STATE(extension[@context])",
  88. .desc =
  89. " The EXTENSION_STATE function can be used to retrieve the state from any\n"
  90. "hinted extension. For example:\n"
  91. " NoOp(1234@default has state ${EXTENSION_STATE(1234)})\n"
  92. " NoOp(4567@home has state ${EXTENSION_STATE(4567@home)})\n"
  93. "\n"
  94. " The possible values returned by this function are:\n"
  95. "UNKNOWN | NOT_INUSE | INUSE | BUSY | INVALID | UNAVAILABLE | RINGING\n"
  96. "RINGINUSE | HOLDINUSE | ONHOLD\n",
  97. .read = extstate_read,
  98. };
  99. static int unload_module(void)
  100. {
  101. int res;
  102. res = ast_custom_function_unregister(&extstate_function);
  103. return res;
  104. }
  105. static int load_module(void)
  106. {
  107. int res;
  108. res = ast_custom_function_register(&extstate_function);
  109. return res ? AST_MODULE_LOAD_DECLINE : AST_MODULE_LOAD_SUCCESS;
  110. }
  111. AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Gets an extension's state in the dialplan");