pbx_switch.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2016, CFWare, LLC
  5. *
  6. * Corey Farrell <git@cfware.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. *
  20. * \brief PBX switch routines.
  21. *
  22. * \author Corey Farrell <git@cfware.com>
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/_private.h"
  30. #include "asterisk/cli.h"
  31. #include "asterisk/linkedlists.h"
  32. #include "asterisk/pbx.h"
  33. #include "pbx_private.h"
  34. static AST_RWLIST_HEAD_STATIC(switches, ast_switch);
  35. struct ast_switch *pbx_findswitch(const char *sw)
  36. {
  37. struct ast_switch *asw;
  38. AST_RWLIST_RDLOCK(&switches);
  39. AST_RWLIST_TRAVERSE(&switches, asw, list) {
  40. if (!strcasecmp(asw->name, sw))
  41. break;
  42. }
  43. AST_RWLIST_UNLOCK(&switches);
  44. return asw;
  45. }
  46. /*
  47. * Append to the list. We don't have a tail pointer because we need
  48. * to scan the list anyways to check for duplicates during insertion.
  49. */
  50. int ast_register_switch(struct ast_switch *sw)
  51. {
  52. struct ast_switch *tmp;
  53. AST_RWLIST_WRLOCK(&switches);
  54. AST_RWLIST_TRAVERSE(&switches, tmp, list) {
  55. if (!strcasecmp(tmp->name, sw->name)) {
  56. AST_RWLIST_UNLOCK(&switches);
  57. ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name);
  58. return -1;
  59. }
  60. }
  61. AST_RWLIST_INSERT_TAIL(&switches, sw, list);
  62. AST_RWLIST_UNLOCK(&switches);
  63. return 0;
  64. }
  65. void ast_unregister_switch(struct ast_switch *sw)
  66. {
  67. AST_RWLIST_WRLOCK(&switches);
  68. AST_RWLIST_REMOVE(&switches, sw, list);
  69. AST_RWLIST_UNLOCK(&switches);
  70. }
  71. /*! \brief handle_show_switches: CLI support for listing registered dial plan switches */
  72. static char *handle_show_switches(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  73. {
  74. struct ast_switch *sw;
  75. switch (cmd) {
  76. case CLI_INIT:
  77. e->command = "core show switches";
  78. e->usage =
  79. "Usage: core show switches\n"
  80. " List registered switches\n";
  81. return NULL;
  82. case CLI_GENERATE:
  83. return NULL;
  84. }
  85. AST_RWLIST_RDLOCK(&switches);
  86. if (AST_RWLIST_EMPTY(&switches)) {
  87. AST_RWLIST_UNLOCK(&switches);
  88. ast_cli(a->fd, "There are no registered alternative switches\n");
  89. return CLI_SUCCESS;
  90. }
  91. ast_cli(a->fd, "\n -= Registered Asterisk Alternative Switches =-\n");
  92. AST_RWLIST_TRAVERSE(&switches, sw, list)
  93. ast_cli(a->fd, "%s: %s\n", sw->name, sw->description);
  94. AST_RWLIST_UNLOCK(&switches);
  95. return CLI_SUCCESS;
  96. }
  97. static struct ast_cli_entry sw_cli[] = {
  98. AST_CLI_DEFINE(handle_show_switches, "Show alternative switches"),
  99. };
  100. static void unload_pbx_switch(void)
  101. {
  102. ast_cli_unregister_multiple(sw_cli, ARRAY_LEN(sw_cli));
  103. }
  104. int load_pbx_switch(void)
  105. {
  106. ast_cli_register_multiple(sw_cli, ARRAY_LEN(sw_cli));
  107. ast_register_cleanup(unload_pbx_switch);
  108. return 0;
  109. }