parking_ui.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * Jonathan Rose <jrose@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. /*! \file
  19. *
  20. * \brief Call Parking CLI commands
  21. *
  22. * \author Jonathan Rose <jrose@digium.com>
  23. */
  24. #include "asterisk.h"
  25. #include "res_parking.h"
  26. #include "asterisk/config.h"
  27. #include "asterisk/config_options.h"
  28. #include "asterisk/utils.h"
  29. #include "asterisk/module.h"
  30. #include "asterisk/cli.h"
  31. #include "asterisk/astobj2.h"
  32. #include "asterisk/features.h"
  33. #include "asterisk/manager.h"
  34. static void display_parked_call(struct parked_user *user, int fd)
  35. {
  36. ast_cli(fd, " Space : %d\n", user->parking_space);
  37. ast_cli(fd, " Channel : %s\n", ast_channel_name(user->chan));
  38. ast_cli(fd, " Parker Dial String : %s\n", user->parker_dial_string);
  39. ast_cli(fd, "\n");
  40. }
  41. static int display_parked_users_cb(void *obj, void *arg, int flags)
  42. {
  43. int *fd = arg;
  44. struct parked_user *user = obj;
  45. display_parked_call(user, *fd);
  46. return 0;
  47. }
  48. static void display_parking_lot(struct parking_lot *lot, int fd)
  49. {
  50. ast_cli(fd, "Parking Lot: %s\n--------------------------------------------------------------------------\n", lot->name);
  51. ast_cli(fd, "Parking Extension : %s\n", lot->cfg->parkext);
  52. ast_cli(fd, "Parking Context : %s\n", lot->cfg->parking_con);
  53. ast_cli(fd, "Parking Spaces : %d-%d\n", lot->cfg->parking_start, lot->cfg->parking_stop);
  54. ast_cli(fd, "Parking Time : %u sec\n", lot->cfg->parkingtime);
  55. ast_cli(fd, "Comeback to Origin : %s\n", lot->cfg->comebacktoorigin ? "yes" : "no");
  56. ast_cli(fd, "Comeback Context : %s%s\n", lot->cfg->comebackcontext, lot->cfg->comebacktoorigin ? " (comebacktoorigin=yes, not used)" : "");
  57. ast_cli(fd, "Comeback Dial Time : %u sec\n", lot->cfg->comebackdialtime);
  58. ast_cli(fd, "MusicOnHold Class : %s\n", lot->cfg->mohclass);
  59. ast_cli(fd, "Enabled : %s\n", (lot->mode == PARKINGLOT_DISABLED) ? "no" : "yes");
  60. ast_cli(fd, "Dynamic : %s\n", (lot->mode == PARKINGLOT_DYNAMIC) ? "yes" : "no");
  61. ast_cli(fd, "\n");
  62. }
  63. static int display_parking_lot_cb(void *obj, void *arg, int flags)
  64. {
  65. int *fd = arg;
  66. struct parking_lot *lot = obj;
  67. display_parking_lot(lot, *fd);
  68. return 0;
  69. }
  70. static void cli_display_parking_lot(int fd, const char *name)
  71. {
  72. RAII_VAR(struct parking_lot *, lot, NULL, ao2_cleanup);
  73. lot = parking_lot_find_by_name(name);
  74. /* If the parking lot couldn't be found with the search, also abort. */
  75. if (!lot) {
  76. ast_cli(fd, "Could not find parking lot '%s'\n\n", name);
  77. return;
  78. }
  79. display_parking_lot(lot, fd);
  80. ast_cli(fd, "Parked Calls\n------------\n");
  81. if (!ao2_container_count(lot->parked_users)) {
  82. ast_cli(fd, " (none)\n");
  83. ast_cli(fd, "\n\n");
  84. return;
  85. }
  86. ao2_callback(lot->parked_users, OBJ_MULTIPLE | OBJ_NODATA, display_parked_users_cb, &fd);
  87. ast_cli(fd, "\n");
  88. }
  89. static void cli_display_parking_global(int fd)
  90. {
  91. ast_cli(fd, "Parking General Options\n"
  92. "-----------------------\n");
  93. ast_cli(fd, "Dynamic Parking : %s\n", parking_dynamic_lots_enabled() ? "yes" : "no");
  94. ast_cli(fd, "\n");
  95. }
  96. static void cli_display_parking_lot_list(int fd)
  97. {
  98. struct ao2_container *lot_container;
  99. lot_container = get_parking_lot_container();
  100. if (!lot_container) {
  101. ast_cli(fd, "Failed to obtain parking lot list.\n\n");
  102. return;
  103. }
  104. ao2_callback(lot_container, OBJ_MULTIPLE | OBJ_NODATA, display_parking_lot_cb, &fd);
  105. ast_cli(fd, "\n");
  106. }
  107. struct parking_lot_complete {
  108. int seeking; /*! Nth match to return. */
  109. int which; /*! Which match currently on. */
  110. };
  111. static int complete_parking_lot_search(void *obj, void *arg, void *data, int flags)
  112. {
  113. struct parking_lot_complete *search = data;
  114. if (++search->which > search->seeking) {
  115. return CMP_MATCH;
  116. }
  117. return 0;
  118. }
  119. static char *complete_parking_lot(const char *word, int seeking)
  120. {
  121. char *ret = NULL;
  122. struct parking_lot *lot;
  123. struct ao2_container *global_lots = get_parking_lot_container();
  124. struct parking_lot_complete search = {
  125. .seeking = seeking,
  126. };
  127. lot = ao2_callback_data(global_lots, ast_strlen_zero(word) ? 0 : OBJ_PARTIAL_KEY,
  128. complete_parking_lot_search, (char *) word, &search);
  129. if (!lot) {
  130. return NULL;
  131. }
  132. ret = ast_strdup(lot->name);
  133. ao2_ref(lot, -1);
  134. return ret;
  135. }
  136. /*! \brief command parking show \<name\> */
  137. static char *handle_show_parking_lot_cmd(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  138. {
  139. switch (cmd) {
  140. case CLI_INIT:
  141. e->command = "parking show";
  142. e->usage =
  143. "Usage: parking show [name]\n"
  144. " Shows a list of parking lots or details of a specific parking lot.";
  145. return NULL;
  146. case CLI_GENERATE:
  147. if (a->pos == 2) {
  148. return complete_parking_lot(a->word, a->n);
  149. }
  150. return NULL;
  151. }
  152. ast_cli(a->fd, "\n");
  153. if (a->argc == 2) {
  154. cli_display_parking_global(a->fd);
  155. cli_display_parking_lot_list(a->fd);
  156. return CLI_SUCCESS;
  157. }
  158. if (a->argc == 3) {
  159. cli_display_parking_lot(a->fd, a->argv[2]);
  160. return CLI_SUCCESS;
  161. }
  162. return CLI_SHOWUSAGE;
  163. }
  164. static struct ast_cli_entry cli_parking_lot[] = {
  165. AST_CLI_DEFINE(handle_show_parking_lot_cmd, "Show a parking lot or a list of all parking lots."),
  166. };
  167. int load_parking_ui(void)
  168. {
  169. return ast_cli_register_multiple(cli_parking_lot, ARRAY_LEN(cli_parking_lot));
  170. }
  171. void unload_parking_ui(void)
  172. {
  173. ast_cli_unregister_multiple(cli_parking_lot, ARRAY_LEN(cli_parking_lot));
  174. }