Fix shutdown assertions in stasis-core
[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 /*!
42  * @{ \brief Define channel message types.
43  */
44 STASIS_MESSAGE_TYPE_DEFN(ast_channel_snapshot_type);
45 STASIS_MESSAGE_TYPE_DEFN(ast_channel_dial_type);
46 STASIS_MESSAGE_TYPE_DEFN(ast_channel_varset_type);
47 STASIS_MESSAGE_TYPE_DEFN(ast_channel_user_event_type);
48 STASIS_MESSAGE_TYPE_DEFN(ast_channel_hangup_request_type);
49 STASIS_MESSAGE_TYPE_DEFN(ast_channel_dtmf_begin_type);
50 STASIS_MESSAGE_TYPE_DEFN(ast_channel_dtmf_end_type);
51 /*! @} */
52
53 /*! \brief Topic for all channels */
54 struct stasis_topic *channel_topic_all;
55
56 /*! \brief Caching topic for all channels */
57 struct stasis_caching_topic *channel_topic_all_cached;
58
59 struct stasis_topic *ast_channel_topic_all(void)
60 {
61         return channel_topic_all;
62 }
63
64 struct stasis_caching_topic *ast_channel_topic_all_cached(void)
65 {
66         return channel_topic_all_cached;
67 }
68
69 static const char *channel_snapshot_get_id(struct stasis_message *message)
70 {
71         struct ast_channel_snapshot *snapshot;
72         if (ast_channel_snapshot_type() != stasis_message_type(message)) {
73                 return NULL;
74         }
75         snapshot = stasis_message_data(message);
76         return snapshot->uniqueid;
77 }
78
79 /*! \internal \brief Hash function for \ref ast_channel_snapshot objects */
80 static int channel_snapshot_hash_cb(const void *obj, const int flags)
81 {
82         const struct ast_channel_snapshot *snapshot = obj;
83         const char *name = (flags & OBJ_KEY) ? obj : snapshot->name;
84         return ast_str_case_hash(name);
85 }
86
87 /*! \internal \brief Comparison function for \ref ast_channel_snapshot objects */
88 static int channel_snapshot_cmp_cb(void *obj, void *arg, int flags)
89 {
90         struct ast_channel_snapshot *left = obj;
91         struct ast_channel_snapshot *right = arg;
92         const char *match = (flags & OBJ_KEY) ? arg : right->name;
93         return strcasecmp(left->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
94 }
95
96 static void channel_snapshot_dtor(void *obj)
97 {
98         struct ast_channel_snapshot *snapshot = obj;
99         ast_string_field_free_memory(snapshot);
100 }
101
102 struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *chan)
103 {
104         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
105
106         snapshot = ao2_alloc(sizeof(*snapshot), channel_snapshot_dtor);
107         if (!snapshot || ast_string_field_init(snapshot, 1024)) {
108                 return NULL;
109         }
110
111         ast_string_field_set(snapshot, name, ast_channel_name(chan));
112         ast_string_field_set(snapshot, accountcode, ast_channel_accountcode(chan));
113         ast_string_field_set(snapshot, peeraccount, ast_channel_peeraccount(chan));
114         ast_string_field_set(snapshot, userfield, ast_channel_userfield(chan));
115         ast_string_field_set(snapshot, uniqueid, ast_channel_uniqueid(chan));
116         ast_string_field_set(snapshot, linkedid, ast_channel_linkedid(chan));
117         ast_string_field_set(snapshot, parkinglot, ast_channel_parkinglot(chan));
118         ast_string_field_set(snapshot, hangupsource, ast_channel_hangupsource(chan));
119         if (ast_channel_appl(chan)) {
120                 ast_string_field_set(snapshot, appl, ast_channel_appl(chan));
121         }
122         if (ast_channel_data(chan)) {
123                 ast_string_field_set(snapshot, data, ast_channel_data(chan));
124         }
125         ast_string_field_set(snapshot, context, ast_channel_context(chan));
126         ast_string_field_set(snapshot, exten, ast_channel_exten(chan));
127
128         ast_string_field_set(snapshot, caller_name,
129                 S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, ""));
130         ast_string_field_set(snapshot, caller_number,
131                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, ""));
132
133         ast_string_field_set(snapshot, connected_name,
134                 S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, ""));
135         ast_string_field_set(snapshot, connected_number,
136                 S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, ""));
137
138         snapshot->creationtime = ast_channel_creationtime(chan);
139         snapshot->state = ast_channel_state(chan);
140         snapshot->priority = ast_channel_priority(chan);
141         snapshot->amaflags = ast_channel_amaflags(chan);
142         snapshot->hangupcause = ast_channel_hangupcause(chan);
143         snapshot->flags = *ast_channel_flags(chan);
144         snapshot->caller_pres = ast_party_id_presentation(&ast_channel_caller(chan)->id);
145
146         snapshot->manager_vars = ast_channel_get_manager_vars(chan);
147
148         ao2_ref(snapshot, +1);
149         return snapshot;
150 }
151
152 static void publish_message_for_channel_topics(struct stasis_message *message, struct ast_channel *chan)
153 {
154         if (chan) {
155                 stasis_publish(ast_channel_topic(chan), message);
156         } else {
157                 stasis_publish(ast_channel_topic_all(), message);
158         }
159 }
160
161 static void channel_blob_dtor(void *obj)
162 {
163         struct ast_channel_blob *event = obj;
164         ao2_cleanup(event->snapshot);
165         ast_json_unref(event->blob);
166 }
167
168 void ast_channel_publish_dial(struct ast_channel *caller, struct ast_channel *peer, const char *dialstring, const char *dialstatus)
169 {
170         RAII_VAR(struct ast_multi_channel_blob *, payload, NULL, ao2_cleanup);
171         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
172         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
173         struct ast_channel_snapshot *caller_snapshot;
174         struct ast_channel_snapshot *peer_snapshot;
175
176         ast_assert(peer != NULL);
177         blob = ast_json_pack("{s: s, s: s, s: s}",
178                              "type", "dial",
179                              "dialstatus", S_OR(dialstatus, ""),
180                              "dialstring", S_OR(dialstring, ""));
181         if (!blob) {
182                 return;
183         }
184         payload = ast_multi_channel_blob_create(blob);
185         if (!payload) {
186                 return;
187         }
188
189         if (caller) {
190                 caller_snapshot = ast_channel_snapshot_create(caller);
191                 if (!caller_snapshot) {
192                         return;
193                 }
194                 ast_multi_channel_blob_add_channel(payload, "caller", caller_snapshot);
195         }
196
197         peer_snapshot = ast_channel_snapshot_create(peer);
198         if (!peer_snapshot) {
199                 return;
200         }
201         ast_multi_channel_blob_add_channel(payload, "peer", peer_snapshot);
202
203         msg = stasis_message_create(ast_channel_dial_type(), payload);
204         if (!msg) {
205                 return;
206         }
207
208         publish_message_for_channel_topics(msg, caller);
209 }
210
211 struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
212         struct stasis_message_type *type, struct ast_json *blob)
213 {
214         RAII_VAR(struct ast_channel_blob *, obj, NULL, ao2_cleanup);
215         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
216
217         if (blob == NULL) {
218                 blob = ast_json_null();
219         }
220
221         obj = ao2_alloc(sizeof(*obj), channel_blob_dtor);
222         if (!obj) {
223                 return NULL;
224         }
225
226         if (chan) {
227                 obj->snapshot = ast_channel_snapshot_create(chan);
228                 if (obj->snapshot == NULL) {
229                         return NULL;
230                 }
231         }
232
233         obj->blob = ast_json_ref(blob);
234
235         msg = stasis_message_create(type, obj);
236         if (!msg) {
237                 return NULL;
238         }
239
240         ao2_ref(msg, +1);
241         return msg;
242 }
243
244 /*! \brief A channel snapshot wrapper object used in \ref ast_multi_channel_blob objects */
245 struct channel_role_snapshot {
246         struct ast_channel_snapshot *snapshot;  /*!< A channel snapshot */
247         char role[0];                                                   /*!< The role assigned to the channel */
248 };
249
250 /*! \brief A multi channel blob data structure for multi_channel_blob stasis messages */
251 struct ast_multi_channel_blob {
252         struct ao2_container *channel_snapshots;        /*!< A container holding the snapshots */
253         struct ast_json *blob;                                          /*< A blob of JSON data */
254 };
255
256 /*! \internal \brief Standard comparison function for \ref channel_role_snapshot objects */
257 static int channel_role_single_cmp_cb(void *obj, void *arg, int flags)
258 {
259         struct channel_role_snapshot *left = obj;
260         struct channel_role_snapshot *right = arg;
261         const char *match = (flags & OBJ_KEY) ? arg : right->role;
262         return strcasecmp(left->role, match) ? 0 : (CMP_MATCH | CMP_STOP);
263 }
264
265 /*! \internal \brief Multi comparison function for \ref channel_role_snapshot objects */
266 static int channel_role_multi_cmp_cb(void *obj, void *arg, int flags)
267 {
268         struct channel_role_snapshot *left = obj;
269         struct channel_role_snapshot *right = arg;
270         const char *match = (flags & OBJ_KEY) ? arg : right->role;
271         return strcasecmp(left->role, match) ? 0 : (CMP_MATCH);
272 }
273
274 /*! \internal \brief Hash function for \ref channel_role_snapshot objects */
275 static int channel_role_hash_cb(const void *obj, const int flags)
276 {
277         const struct channel_role_snapshot *snapshot = obj;
278         const char *name = (flags & OBJ_KEY) ? obj : snapshot->role;
279         return ast_str_case_hash(name);
280 }
281
282 /*! \internal \brief Destructor for \ref ast_multi_channel_blob objects */
283 static void multi_channel_blob_dtor(void *obj)
284 {
285         struct ast_multi_channel_blob *multi_blob = obj;
286
287         ao2_cleanup(multi_blob->channel_snapshots);
288         ast_json_unref(multi_blob->blob);
289 }
290
291 struct ast_multi_channel_blob *ast_multi_channel_blob_create(struct ast_json *blob)
292 {
293         RAII_VAR(struct ast_multi_channel_blob *, obj,
294                         ao2_alloc(sizeof(*obj), multi_channel_blob_dtor),
295                         ao2_cleanup);
296
297         ast_assert(blob != NULL);
298
299         if (!obj) {
300                 return NULL;
301         }
302
303         obj->channel_snapshots = ao2_container_alloc(NUM_MULTI_CHANNEL_BLOB_BUCKETS,
304                         channel_role_hash_cb, channel_role_single_cmp_cb);
305         if (!obj->channel_snapshots) {
306                 return NULL;
307         }
308
309         obj->blob = ast_json_ref(blob);
310
311         ao2_ref(obj, +1);
312         return obj;
313 }
314
315 static void channel_role_snapshot_dtor(void *obj)
316 {
317         struct channel_role_snapshot *role_snapshot = obj;
318         ao2_cleanup(role_snapshot->snapshot);
319 }
320
321 void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj, const char *role, struct ast_channel_snapshot *snapshot)
322 {
323         RAII_VAR(struct channel_role_snapshot *, role_snapshot, NULL, ao2_cleanup);
324         int role_len = strlen(role) + 1;
325
326         if (!obj || ast_strlen_zero(role) || !snapshot) {
327                 return;
328         }
329
330         role_snapshot = ao2_alloc(sizeof(*role_snapshot) + role_len, channel_role_snapshot_dtor);
331         if (!role_snapshot) {
332                 return;
333         }
334         ast_copy_string(role_snapshot->role, role, role_len);
335         role_snapshot->snapshot = snapshot;
336         ao2_ref(role_snapshot->snapshot, +1);
337         ao2_link(obj->channel_snapshots, role_snapshot);
338 }
339
340 struct ast_channel_snapshot *ast_multi_channel_blob_get_channel(struct ast_multi_channel_blob *obj, const char *role)
341 {
342         struct channel_role_snapshot *role_snapshot;
343
344         if (!obj || ast_strlen_zero(role)) {
345                 return NULL;
346         }
347         role_snapshot = ao2_find(obj->channel_snapshots, role, OBJ_KEY);
348         /* Note that this function does not increase the ref count on snapshot */
349         if (!role_snapshot) {
350                 return NULL;
351         }
352         ao2_ref(role_snapshot, -1);
353         return role_snapshot->snapshot;
354 }
355
356 struct ao2_container *ast_multi_channel_blob_get_channels(struct ast_multi_channel_blob *obj, const char *role)
357 {
358         RAII_VAR(struct ao2_container *, ret_container,
359                 ao2_container_alloc(NUM_MULTI_CHANNEL_BLOB_BUCKETS, channel_snapshot_hash_cb, channel_snapshot_cmp_cb),
360                 ao2_cleanup);
361         struct ao2_iterator *it_role_snapshots;
362         struct channel_role_snapshot *role_snapshot;
363         char *arg;
364
365         if (!obj || ast_strlen_zero(role) || !ret_container) {
366                 return NULL;
367         }
368         arg = ast_strdupa(role);
369
370         it_role_snapshots = ao2_callback(obj->channel_snapshots, OBJ_MULTIPLE | OBJ_KEY, channel_role_multi_cmp_cb, arg);
371         if (!it_role_snapshots) {
372                 return NULL;
373         }
374
375         while ((role_snapshot = ao2_iterator_next(it_role_snapshots))) {
376                 ao2_link(ret_container, role_snapshot->snapshot);
377                 ao2_ref(role_snapshot, -1);
378         }
379         ao2_iterator_destroy(it_role_snapshots);
380
381         ao2_ref(ret_container, +1);
382         return ret_container;
383 }
384
385 struct ast_json *ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj)
386 {
387         if (!obj) {
388                 return NULL;
389         }
390         return obj->blob;
391 }
392
393 void ast_channel_publish_varset(struct ast_channel *chan, const char *name, const char *value)
394 {
395         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
396         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
397
398         ast_assert(name != NULL);
399         ast_assert(value != NULL);
400
401         blob = ast_json_pack("{s: s, s: s, s: s}",
402                              "type", "varset",
403                              "variable", name,
404                              "value", value);
405         if (!blob) {
406                 ast_log(LOG_ERROR, "Error creating message\n");
407                 return;
408         }
409
410         msg = ast_channel_blob_create(chan, ast_channel_varset_type(),
411                 ast_json_ref(blob));
412
413         if (!msg) {
414                 return;
415         }
416
417         publish_message_for_channel_topics(msg, chan);
418 }
419
420 void ast_publish_channel_state(struct ast_channel *chan)
421 {
422         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
423         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
424
425         ast_assert(chan != NULL);
426         if (!chan) {
427                 return;
428         }
429
430         snapshot = ast_channel_snapshot_create(chan);
431         if (!snapshot) {
432                 return;
433         }
434
435         message = stasis_message_create(ast_channel_snapshot_type(), snapshot);
436         if (!message) {
437                 return;
438         }
439
440         ast_assert(ast_channel_topic(chan) != NULL);
441         stasis_publish(ast_channel_topic(chan), message);
442 }
443
444 struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot)
445 {
446         RAII_VAR(struct ast_json *, json_chan, NULL, ast_json_unref);
447
448         if (snapshot == NULL) {
449                 return NULL;
450         }
451
452         json_chan = ast_json_pack("{ s: s, s: s, s: s, s: s, s: s, s: s, s: s,"
453                                   "  s: s, s: s, s: s, s: s, s: o, s: o, s: o,"
454                                   "  s: o"
455                                   "}",
456                                   "name", snapshot->name,
457                                   "state", ast_state2str(snapshot->state),
458                                   "accountcode", snapshot->accountcode,
459                                   "peeraccount", snapshot->peeraccount,
460                                   "userfield", snapshot->userfield,
461                                   "uniqueid", snapshot->uniqueid,
462                                   "linkedid", snapshot->linkedid,
463                                   "parkinglot", snapshot->parkinglot,
464                                   "hangupsource", snapshot->hangupsource,
465                                   "appl", snapshot->appl,
466                                   "data", snapshot->data,
467                                   "dialplan", ast_json_dialplan_cep(snapshot->context, snapshot->exten, snapshot->priority),
468                                   "caller", ast_json_name_number(snapshot->caller_name, snapshot->caller_number),
469                                   "connected", ast_json_name_number(snapshot->connected_name, snapshot->connected_number),
470                                   "creationtime", ast_json_timeval(snapshot->creationtime, NULL));
471
472         return ast_json_ref(json_chan);
473 }
474
475 int ast_channel_snapshot_cep_equal(
476         const struct ast_channel_snapshot *old_snapshot,
477         const struct ast_channel_snapshot *new_snapshot)
478 {
479         ast_assert(old_snapshot != NULL);
480         ast_assert(new_snapshot != NULL);
481
482         /* We actually get some snapshots with CEP set, but before the
483          * application is set. Since empty application is invalid, we treat
484          * setting the application from nothing as a CEP change.
485          */
486         if (ast_strlen_zero(old_snapshot->appl) &&
487             !ast_strlen_zero(new_snapshot->appl)) {
488                 return 0;
489         }
490
491         return old_snapshot->priority == new_snapshot->priority &&
492                 strcmp(old_snapshot->context, new_snapshot->context) == 0 &&
493                 strcmp(old_snapshot->exten, new_snapshot->exten) == 0;
494 }
495
496 int ast_channel_snapshot_caller_id_equal(
497         const struct ast_channel_snapshot *old_snapshot,
498         const struct ast_channel_snapshot *new_snapshot)
499 {
500         ast_assert(old_snapshot != NULL);
501         ast_assert(new_snapshot != NULL);
502         return strcmp(old_snapshot->caller_number, new_snapshot->caller_number) == 0 &&
503                 strcmp(old_snapshot->caller_name, new_snapshot->caller_name) == 0;
504 }
505
506 void ast_stasis_channels_shutdown(void)
507 {
508         channel_topic_all_cached = stasis_caching_unsubscribe_and_join(channel_topic_all_cached);
509         ao2_cleanup(channel_topic_all);
510         channel_topic_all = NULL;
511         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_snapshot_type);
512         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dial_type);
513         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_varset_type);
514         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_user_event_type);
515         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hangup_request_type);
516         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_begin_type);
517         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_end_type);
518 }
519
520 void ast_stasis_channels_init(void)
521 {
522         STASIS_MESSAGE_TYPE_INIT(ast_channel_snapshot_type);
523         STASIS_MESSAGE_TYPE_INIT(ast_channel_dial_type);
524         STASIS_MESSAGE_TYPE_INIT(ast_channel_varset_type);
525         STASIS_MESSAGE_TYPE_INIT(ast_channel_user_event_type);
526         STASIS_MESSAGE_TYPE_INIT(ast_channel_hangup_request_type);
527         STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_begin_type);
528         STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_end_type);
529
530         channel_topic_all = stasis_topic_create("ast_channel_topic_all");
531         channel_topic_all_cached = stasis_caching_topic_create(channel_topic_all, channel_snapshot_get_id);
532 }