Fixed manager channelvars support.
[asterisk/asterisk.git] / main / stasis_channels.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2013, Digium, Inc.
5  *
6  * Matt Jordan <mjordan@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 Stasis Messages and Data Types for Channel Objects
22  *
23  * \author \verbatim Matt Jordan <mjordan@digium.com> \endverbatim
24  *
25  */
26
27 /*** MODULEINFO
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34
35 #include "asterisk/stasis.h"
36 #include "asterisk/astobj2.h"
37 #include "asterisk/stasis_channels.h"
38
39 #define NUM_MULTI_CHANNEL_BLOB_BUCKETS 7
40
41 /*! \brief Message type for channel snapshot messages */
42 static struct stasis_message_type *channel_snapshot_type;
43
44 /*! \brief Message type for channel blob messages */
45 static struct stasis_message_type *channel_blob_type;
46
47 /*! \brief Message type for channel dial messages */
48 static struct stasis_message_type *channel_dial_type;
49
50 /*! \brief Topic for all channels */
51 struct stasis_topic *channel_topic_all;
52
53 /*! \brief Caching topic for all channels */
54 struct stasis_caching_topic *channel_topic_all_cached;
55
56 struct stasis_message_type *ast_channel_dial_type(void)
57 {
58         return channel_dial_type;
59 }
60
61 struct stasis_message_type *ast_channel_blob_type(void)
62 {
63         return channel_blob_type;
64 }
65
66 struct stasis_message_type *ast_channel_snapshot_type(void)
67 {
68         return channel_snapshot_type;
69 }
70
71 struct stasis_topic *ast_channel_topic_all(void)
72 {
73         return channel_topic_all;
74 }
75
76 struct stasis_caching_topic *ast_channel_topic_all_cached(void)
77 {
78         return channel_topic_all_cached;
79 }
80
81 static const char *channel_snapshot_get_id(struct stasis_message *message)
82 {
83         struct ast_channel_snapshot *snapshot;
84         if (ast_channel_snapshot_type() != stasis_message_type(message)) {
85                 return NULL;
86         }
87         snapshot = stasis_message_data(message);
88         return snapshot->uniqueid;
89 }
90
91 /*! \internal \brief Hash function for \ref ast_channel_snapshot objects */
92 static int channel_snapshot_hash_cb(const void *obj, const int flags)
93 {
94         const struct ast_channel_snapshot *snapshot = obj;
95         const char *name = (flags & OBJ_KEY) ? obj : snapshot->name;
96         return ast_str_case_hash(name);
97 }
98
99 /*! \internal \brief Comparison function for \ref ast_channel_snapshot objects */
100 static int channel_snapshot_cmp_cb(void *obj, void *arg, int flags)
101 {
102         struct ast_channel_snapshot *left = obj;
103         struct ast_channel_snapshot *right = arg;
104         const char *match = (flags & OBJ_KEY) ? arg : right->name;
105         return strcasecmp(left->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
106 }
107
108 static void channel_snapshot_dtor(void *obj)
109 {
110         struct ast_channel_snapshot *snapshot = obj;
111         ast_string_field_free_memory(snapshot);
112 }
113
114 struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *chan)
115 {
116         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
117
118         snapshot = ao2_alloc(sizeof(*snapshot), channel_snapshot_dtor);
119         if (!snapshot || ast_string_field_init(snapshot, 1024)) {
120                 return NULL;
121         }
122
123         ast_string_field_set(snapshot, name, ast_channel_name(chan));
124         ast_string_field_set(snapshot, accountcode, ast_channel_accountcode(chan));
125         ast_string_field_set(snapshot, peeraccount, ast_channel_peeraccount(chan));
126         ast_string_field_set(snapshot, userfield, ast_channel_userfield(chan));
127         ast_string_field_set(snapshot, uniqueid, ast_channel_uniqueid(chan));
128         ast_string_field_set(snapshot, linkedid, ast_channel_linkedid(chan));
129         ast_string_field_set(snapshot, parkinglot, ast_channel_parkinglot(chan));
130         ast_string_field_set(snapshot, hangupsource, ast_channel_hangupsource(chan));
131         if (ast_channel_appl(chan)) {
132                 ast_string_field_set(snapshot, appl, ast_channel_appl(chan));
133         }
134         if (ast_channel_data(chan)) {
135                 ast_string_field_set(snapshot, data, ast_channel_data(chan));
136         }
137         ast_string_field_set(snapshot, context, ast_channel_context(chan));
138         ast_string_field_set(snapshot, exten, ast_channel_exten(chan));
139
140         ast_string_field_set(snapshot, caller_name,
141                 S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, ""));
142         ast_string_field_set(snapshot, caller_number,
143                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, ""));
144
145         ast_string_field_set(snapshot, connected_name,
146                 S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, ""));
147         ast_string_field_set(snapshot, connected_number,
148                 S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, ""));
149
150         snapshot->creationtime = ast_channel_creationtime(chan);
151         snapshot->state = ast_channel_state(chan);
152         snapshot->priority = ast_channel_priority(chan);
153         snapshot->amaflags = ast_channel_amaflags(chan);
154         snapshot->hangupcause = ast_channel_hangupcause(chan);
155         snapshot->flags = *ast_channel_flags(chan);
156         snapshot->caller_pres = ast_party_id_presentation(&ast_channel_caller(chan)->id);
157
158         snapshot->manager_vars = ast_channel_get_manager_vars(chan);
159
160         ao2_ref(snapshot, +1);
161         return snapshot;
162 }
163
164 static void publish_message_for_channel_topics(struct stasis_message *message, struct ast_channel *chan)
165 {
166         if (chan) {
167                 stasis_publish(ast_channel_topic(chan), message);
168         } else {
169                 stasis_publish(ast_channel_topic_all(), message);
170         }
171 }
172
173 static void channel_blob_dtor(void *obj)
174 {
175         struct ast_channel_blob *event = obj;
176         ao2_cleanup(event->snapshot);
177         ast_json_unref(event->blob);
178 }
179
180 void ast_channel_publish_dial(struct ast_channel *caller, struct ast_channel *peer, const char *dialstring, const char *dialstatus)
181 {
182         RAII_VAR(struct ast_multi_channel_blob *, payload, NULL, ao2_cleanup);
183         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
184         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
185         struct ast_channel_snapshot *caller_snapshot;
186         struct ast_channel_snapshot *peer_snapshot;
187
188         ast_assert(peer != NULL);
189         blob = ast_json_pack("{s: s, s: s, s: s}",
190                              "type", "dial",
191                              "dialstatus", S_OR(dialstatus, ""),
192                              "dialstring", S_OR(dialstring, ""));
193         if (!blob) {
194                 return;
195         }
196         payload = ast_multi_channel_blob_create(blob);
197         if (!payload) {
198                 return;
199         }
200
201         if (caller) {
202                 caller_snapshot = ast_channel_snapshot_create(caller);
203                 if (!caller_snapshot) {
204                         return;
205                 }
206                 ast_multi_channel_blob_add_channel(payload, "caller", caller_snapshot);
207         }
208
209         peer_snapshot = ast_channel_snapshot_create(peer);
210         if (!peer_snapshot) {
211                 return;
212         }
213         ast_multi_channel_blob_add_channel(payload, "peer", peer_snapshot);
214
215         msg = stasis_message_create(ast_channel_dial_type(), payload);
216         if (!msg) {
217                 return;
218         }
219
220         publish_message_for_channel_topics(msg, caller);
221 }
222
223 struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
224                                                struct ast_json *blob)
225 {
226         RAII_VAR(struct ast_channel_blob *, obj, NULL, ao2_cleanup);
227         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
228         struct ast_json *type;
229
230         ast_assert(blob != NULL);
231
232         type = ast_json_object_get(blob, "type");
233         if (type == NULL) {
234                 ast_log(LOG_ERROR, "Invalid ast_channel_blob; missing type field\n");
235                 return NULL;
236         }
237
238         obj = ao2_alloc(sizeof(*obj), channel_blob_dtor);
239         if (!obj) {
240                 return NULL;
241         }
242
243         if (chan) {
244                 obj->snapshot = ast_channel_snapshot_create(chan);
245                 if (obj->snapshot == NULL) {
246                         return NULL;
247                 }
248         }
249
250         obj->blob = ast_json_ref(blob);
251
252         msg = stasis_message_create(ast_channel_blob_type(), obj);
253         if (!msg) {
254                 return NULL;
255         }
256
257         ao2_ref(msg, +1);
258         return msg;
259 }
260
261 const char *ast_channel_blob_json_type(struct ast_channel_blob *obj)
262 {
263         if (obj == NULL) {
264                 return NULL;
265         }
266
267         return ast_json_string_get(ast_json_object_get(obj->blob, "type"));
268 }
269
270 /*! \brief A channel snapshot wrapper object used in \ref ast_multi_channel_blob objects */
271 struct channel_role_snapshot {
272         struct ast_channel_snapshot *snapshot;  /*!< A channel snapshot */
273         char role[0];                                                   /*!< The role assigned to the channel */
274 };
275
276 /*! \brief A multi channel blob data structure for multi_channel_blob stasis messages */
277 struct ast_multi_channel_blob {
278         struct ao2_container *channel_snapshots;        /*!< A container holding the snapshots */
279         struct ast_json *blob;                                          /*< A blob of JSON data */
280 };
281
282 /*! \internal \brief Standard comparison function for \ref channel_role_snapshot objects */
283 static int channel_role_single_cmp_cb(void *obj, void *arg, int flags)
284 {
285         struct channel_role_snapshot *left = obj;
286         struct channel_role_snapshot *right = arg;
287         const char *match = (flags & OBJ_KEY) ? arg : right->role;
288         return strcasecmp(left->role, match) ? 0 : (CMP_MATCH | CMP_STOP);
289 }
290
291 /*! \internal \brief Multi comparison function for \ref channel_role_snapshot objects */
292 static int channel_role_multi_cmp_cb(void *obj, void *arg, int flags)
293 {
294         struct channel_role_snapshot *left = obj;
295         struct channel_role_snapshot *right = arg;
296         const char *match = (flags & OBJ_KEY) ? arg : right->role;
297         return strcasecmp(left->role, match) ? 0 : (CMP_MATCH);
298 }
299
300 /*! \internal \brief Hash function for \ref channel_role_snapshot objects */
301 static int channel_role_hash_cb(const void *obj, const int flags)
302 {
303         const struct channel_role_snapshot *snapshot = obj;
304         const char *name = (flags & OBJ_KEY) ? obj : snapshot->role;
305         return ast_str_case_hash(name);
306 }
307
308 /*! \internal \brief Destructor for \ref ast_multi_channel_blob objects */
309 static void multi_channel_blob_dtor(void *obj)
310 {
311         struct ast_multi_channel_blob *multi_blob = obj;
312
313         ao2_cleanup(multi_blob->channel_snapshots);
314         ast_json_unref(multi_blob->blob);
315 }
316
317 struct ast_multi_channel_blob *ast_multi_channel_blob_create(struct ast_json *blob)
318 {
319         RAII_VAR(struct ast_multi_channel_blob *, obj,
320                         ao2_alloc(sizeof(*obj), multi_channel_blob_dtor),
321                         ao2_cleanup);
322         struct ast_json *type;
323
324         ast_assert(blob != NULL);
325
326         if (!obj) {
327                 return NULL;
328         }
329
330         type = ast_json_object_get(blob, "type");
331         if (type == NULL) {
332                 ast_log(LOG_ERROR, "Invalid ast_multi_channel_blob; missing type field\n");
333                 return NULL;
334         }
335
336         obj->channel_snapshots = ao2_container_alloc(NUM_MULTI_CHANNEL_BLOB_BUCKETS,
337                         channel_role_hash_cb, channel_role_single_cmp_cb);
338         if (!obj->channel_snapshots) {
339                 return NULL;
340         }
341
342         obj->blob = ast_json_ref(blob);
343
344         ao2_ref(obj, +1);
345         return obj;
346 }
347
348 static void channel_role_snapshot_dtor(void *obj)
349 {
350         struct channel_role_snapshot *role_snapshot = obj;
351         ao2_cleanup(role_snapshot->snapshot);
352 }
353
354 void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj, const char *role, struct ast_channel_snapshot *snapshot)
355 {
356         RAII_VAR(struct channel_role_snapshot *, role_snapshot, NULL, ao2_cleanup);
357         int role_len = strlen(role) + 1;
358
359         if (!obj || ast_strlen_zero(role) || !snapshot) {
360                 return;
361         }
362
363         role_snapshot = ao2_alloc(sizeof(*role_snapshot) + role_len, channel_role_snapshot_dtor);
364         if (!role_snapshot) {
365                 return;
366         }
367         ast_copy_string(role_snapshot->role, role, role_len);
368         role_snapshot->snapshot = snapshot;
369         ao2_ref(role_snapshot->snapshot, +1);
370         ao2_link(obj->channel_snapshots, role_snapshot);
371 }
372
373 struct ast_channel_snapshot *ast_multi_channel_blob_get_channel(struct ast_multi_channel_blob *obj, const char *role)
374 {
375         struct channel_role_snapshot *role_snapshot;
376
377         if (!obj || ast_strlen_zero(role)) {
378                 return NULL;
379         }
380         role_snapshot = ao2_find(obj->channel_snapshots, role, OBJ_KEY);
381         /* Note that this function does not increase the ref count on snapshot */
382         if (!role_snapshot) {
383                 return NULL;
384         }
385         ao2_ref(role_snapshot, -1);
386         return role_snapshot->snapshot;
387 }
388
389 struct ao2_container *ast_multi_channel_blob_get_channels(struct ast_multi_channel_blob *obj, const char *role)
390 {
391         RAII_VAR(struct ao2_container *, ret_container,
392                 ao2_container_alloc(NUM_MULTI_CHANNEL_BLOB_BUCKETS, channel_snapshot_hash_cb, channel_snapshot_cmp_cb),
393                 ao2_cleanup);
394         struct ao2_iterator *it_role_snapshots;
395         struct channel_role_snapshot *role_snapshot;
396         char *arg;
397
398         if (!obj || ast_strlen_zero(role) || !ret_container) {
399                 return NULL;
400         }
401         arg = ast_strdupa(role);
402
403         it_role_snapshots = ao2_callback(obj->channel_snapshots, OBJ_MULTIPLE | OBJ_KEY, channel_role_multi_cmp_cb, arg);
404         if (!it_role_snapshots) {
405                 return NULL;
406         }
407
408         while ((role_snapshot = ao2_iterator_next(it_role_snapshots))) {
409                 ao2_link(ret_container, role_snapshot->snapshot);
410                 ao2_ref(role_snapshot, -1);
411         }
412         ao2_iterator_destroy(it_role_snapshots);
413
414         ao2_ref(ret_container, +1);
415         return ret_container;
416 }
417
418 struct ast_json *ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj)
419 {
420         if (!obj) {
421                 return NULL;
422         }
423         return obj->blob;
424 }
425
426 const char *ast_multi_channel_blob_get_type(struct ast_multi_channel_blob *obj)
427 {
428         if (!obj) {
429                 return NULL;
430         }
431
432         return ast_json_string_get(ast_json_object_get(obj->blob, "type"));
433 }
434
435 void ast_channel_publish_varset(struct ast_channel *chan, const char *name, const char *value)
436 {
437         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
438         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
439
440         ast_assert(name != NULL);
441         ast_assert(value != NULL);
442
443         blob = ast_json_pack("{s: s, s: s, s: s}",
444                              "type", "varset",
445                              "variable", name,
446                              "value", value);
447         if (!blob) {
448                 ast_log(LOG_ERROR, "Error creating message\n");
449                 return;
450         }
451
452         msg = ast_channel_blob_create(chan, ast_json_ref(blob));
453
454         if (!msg) {
455                 return;
456         }
457
458         publish_message_for_channel_topics(msg, chan);
459 }
460
461 void ast_stasis_channels_shutdown(void)
462 {
463         ao2_cleanup(channel_snapshot_type);
464         channel_snapshot_type = NULL;
465         ao2_cleanup(channel_blob_type);
466         channel_blob_type = NULL;
467         ao2_cleanup(channel_dial_type);
468         channel_dial_type = NULL;
469         ao2_cleanup(channel_topic_all);
470         channel_topic_all = NULL;
471         channel_topic_all_cached = stasis_caching_unsubscribe(channel_topic_all_cached);
472 }
473
474 void ast_stasis_channels_init(void)
475 {
476         channel_snapshot_type = stasis_message_type_create("ast_channel_snapshot");
477         channel_blob_type = stasis_message_type_create("ast_channel_blob");
478         channel_dial_type = stasis_message_type_create("ast_channel_dial");
479         channel_topic_all = stasis_topic_create("ast_channel_topic_all");
480         channel_topic_all_cached = stasis_caching_topic_create(channel_topic_all, channel_snapshot_get_id);
481 }