Refactor CEL bridge events on top of Stasis-Core
[asterisk/asterisk.git] / main / parking.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 Parking Core
22  *
23  * \author Jonathan Rose <jrose@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include "asterisk/_private.h"
31 #include "asterisk/astobj2.h"
32 #include "asterisk/pbx.h"
33 #include "asterisk/bridging.h"
34 #include "asterisk/parking.h"
35 #include "asterisk/channel.h"
36 #include "asterisk/_private.h"
37
38 /*! \brief Message type for parked calls */
39 STASIS_MESSAGE_TYPE_DEFN(ast_parked_call_type);
40
41 /*! \brief Topic for parking lots */
42 static struct stasis_topic *parking_topic;
43
44 /*! \brief Function Callback for handling blind transfers to park applications */
45 static ast_park_blind_xfer_fn ast_park_blind_xfer_func = NULL;
46
47 /*! \brief Function Callback for handling a bridge channel trying to park itself */
48 static ast_bridge_channel_park_fn ast_bridge_channel_park_func = NULL;
49
50 static void parking_stasis_cleanup(void)
51 {
52         STASIS_MESSAGE_TYPE_CLEANUP(ast_parked_call_type);
53         ao2_cleanup(parking_topic);
54         parking_topic = NULL;
55 }
56
57 int ast_parking_stasis_init(void)
58 {
59         if (STASIS_MESSAGE_TYPE_INIT(ast_parked_call_type)) {
60                 return -1;
61         }
62
63         parking_topic = stasis_topic_create("ast_parking");
64         if (!parking_topic) {
65                 return -1;
66         }
67         ast_register_cleanup(parking_stasis_cleanup);
68         return 0;
69 }
70
71 struct stasis_topic *ast_parking_topic(void)
72 {
73         return parking_topic;
74 }
75
76 /*! \brief Destructor for parked_call_payload objects */
77 static void parked_call_payload_destructor(void *obj)
78 {
79         struct ast_parked_call_payload *park_obj = obj;
80
81         ao2_cleanup(park_obj->parkee);
82         ao2_cleanup(park_obj->parker);
83         ast_string_field_free_memory(park_obj);
84 }
85
86 struct ast_parked_call_payload *ast_parked_call_payload_create(enum ast_parked_call_event_type event_type,
87                 struct ast_channel_snapshot *parkee_snapshot, struct ast_channel_snapshot *parker_snapshot,
88                 struct ast_channel_snapshot *retriever_snapshot, const char *parkinglot,
89                 unsigned int parkingspace, unsigned long int timeout,
90                 unsigned long int duration)
91 {
92         RAII_VAR(struct ast_parked_call_payload *, payload, NULL, ao2_cleanup);
93
94         payload = ao2_alloc(sizeof(*payload), parked_call_payload_destructor);
95         if (!payload) {
96                 return NULL;
97         }
98
99         if (ast_string_field_init(payload, 32)) {
100                 return NULL;
101         }
102
103         payload->event_type = event_type;
104
105         ao2_ref(parkee_snapshot, +1);
106         payload->parkee = parkee_snapshot;
107
108         if (parker_snapshot) {
109                 ao2_ref(parker_snapshot, +1);
110                 payload->parker = parker_snapshot;
111         }
112
113         if (retriever_snapshot) {
114                 ao2_ref(retriever_snapshot, +1);
115                 payload->retriever = retriever_snapshot;
116         }
117
118         if (parkinglot) {
119                 ast_string_field_set(payload, parkinglot, parkinglot);
120         }
121
122         payload->parkingspace = parkingspace;
123         payload->timeout = timeout;
124         payload->duration = duration;
125
126         /* Bump the ref count by one since RAII_VAR is going to eat one when we leave. */
127         ao2_ref(payload, +1);
128         return payload;
129 }
130
131 void ast_install_park_blind_xfer_func(ast_park_blind_xfer_fn park_blind_xfer_func)
132 {
133         ast_park_blind_xfer_func = park_blind_xfer_func;
134 }
135
136 void ast_install_bridge_channel_park_func(ast_bridge_channel_park_fn bridge_channel_park_func)
137 {
138         ast_bridge_channel_park_func = bridge_channel_park_func;
139 }
140
141 void ast_uninstall_park_blind_xfer_func(void)
142 {
143         ast_park_blind_xfer_func = NULL;
144 }
145
146 void ast_uninstall_bridge_channel_park_func(void)
147 {
148         ast_bridge_channel_park_func = NULL;
149 }
150
151 int ast_park_blind_xfer(struct ast_bridge *bridge, struct ast_bridge_channel *parker,
152                 struct ast_exten *park_exten)
153 {
154         static int warned = 0;
155         if (ast_park_blind_xfer_func) {
156                 return ast_park_blind_xfer_func(bridge, parker, park_exten);
157         }
158
159         if (warned++ % 10 == 0) {
160                 ast_verb(3, "%s attempted to blind transfer to a parking extension, but no parking blind transfer function is loaded.\n",
161                         ast_channel_name(parker->chan));
162         }
163
164         return -1;
165 }
166
167 struct ast_exten *ast_get_parking_exten(const char *exten_str, struct ast_channel *chan, const char *context)
168 {
169         struct ast_exten *exten;
170         struct pbx_find_info q = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
171         const char *app_at_exten;
172
173         ast_debug(4, "Checking if %s@%s is a parking exten\n", exten_str, context);
174         exten = pbx_find_extension(chan, NULL, &q, context, exten_str, 1, NULL, NULL,
175                 E_MATCH);
176         if (!exten) {
177                 return NULL;
178         }
179
180         app_at_exten = ast_get_extension_app(exten);
181         if (!app_at_exten || strcasecmp(PARK_APPLICATION, app_at_exten)) {
182                 return NULL;
183         }
184
185         return exten;
186 }
187
188 void ast_bridge_channel_park(struct ast_bridge_channel *bridge_channel, const char *parkee_uuid, const char *parker_uuid, const char *app_data)
189 {
190         /* Run installable function */
191         if (ast_bridge_channel_park_func) {
192                 return ast_bridge_channel_park_func(bridge_channel, parkee_uuid, parker_uuid, app_data);
193         }
194 }