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