pbx_switch.c 3.0 KB

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