8327384d7a9161b1fac6db263174e0ef1419fd03
[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 /*** DOCUMENTATION
40         <managerEvent language="en_US" name="VarSet">
41                 <managerEventInstance class="EVENT_FLAG_DIALPLAN">
42                         <synopsis>Raised when a variable is set to a particular value.</synopsis>
43                         <syntax>
44                                 <xi:include xpointer="xpointer(/docs/managerEvent[@name='Newchannel']/managerEventInstance/syntax/parameter)" />
45                                 <parameter name="Variable">
46                                         <para>The variable being set.</para>
47                                 </parameter>
48                                 <parameter name="Value">
49                                         <para>The new value of the variable.</para>
50                                 </parameter>
51                         </syntax>
52                 </managerEventInstance>
53         </managerEvent>
54 ***/
55
56 #define NUM_MULTI_CHANNEL_BLOB_BUCKETS 7
57
58 /*! \brief Topic for all channels */
59 struct stasis_topic *channel_topic_all;
60
61 /*! \brief Caching topic for all channels */
62 struct stasis_caching_topic *channel_topic_all_cached;
63
64 /*! \brief Caching topic for all channels indexed by name */
65 struct stasis_caching_topic *channel_topic_all_cached_by_name;
66
67 struct stasis_topic *ast_channel_topic_all(void)
68 {
69         return channel_topic_all;
70 }
71
72 struct stasis_caching_topic *ast_channel_topic_all_cached(void)
73 {
74         return channel_topic_all_cached;
75 }
76
77 static const char *channel_snapshot_get_id(struct stasis_message *message)
78 {
79         struct ast_channel_snapshot *snapshot;
80         if (ast_channel_snapshot_type() != stasis_message_type(message)) {
81                 return NULL;
82         }
83         snapshot = stasis_message_data(message);
84         return snapshot->uniqueid;
85 }
86
87 struct stasis_caching_topic *ast_channel_topic_all_cached_by_name(void)
88 {
89         return channel_topic_all_cached_by_name;
90 }
91
92 static const char *channel_snapshot_get_name(struct stasis_message *message)
93 {
94         struct ast_channel_snapshot *snapshot;
95         if (ast_channel_snapshot_type() != stasis_message_type(message)) {
96                 return NULL;
97         }
98         snapshot = stasis_message_data(message);
99         return snapshot->name;
100 }
101
102 /*! \internal \brief Hash function for \ref ast_channel_snapshot objects */
103 static int channel_snapshot_hash_cb(const void *obj, const int flags)
104 {
105         const struct ast_channel_snapshot *snapshot = obj;
106         const char *name = (flags & OBJ_KEY) ? obj : snapshot->name;
107         return ast_str_case_hash(name);
108 }
109
110 /*! \internal \brief Comparison function for \ref ast_channel_snapshot objects */
111 static int channel_snapshot_cmp_cb(void *obj, void *arg, int flags)
112 {
113         struct ast_channel_snapshot *left = obj;
114         struct ast_channel_snapshot *right = arg;
115         const char *match = (flags & OBJ_KEY) ? arg : right->name;
116         return strcasecmp(left->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
117 }
118
119 static void channel_snapshot_dtor(void *obj)
120 {
121         struct ast_channel_snapshot *snapshot = obj;
122         ast_string_field_free_memory(snapshot);
123 }
124
125 struct ast_channel_snapshot *ast_channel_snapshot_create(struct ast_channel *chan)
126 {
127         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
128
129         /* no snapshots for dummy channels */
130         if (!ast_channel_tech(chan)) {
131                 return NULL;
132         }
133
134         snapshot = ao2_alloc(sizeof(*snapshot), channel_snapshot_dtor);
135         if (!snapshot || ast_string_field_init(snapshot, 1024)) {
136                 return NULL;
137         }
138
139         ast_string_field_set(snapshot, name, ast_channel_name(chan));
140         ast_string_field_set(snapshot, accountcode, ast_channel_accountcode(chan));
141         ast_string_field_set(snapshot, peeraccount, ast_channel_peeraccount(chan));
142         ast_string_field_set(snapshot, userfield, ast_channel_userfield(chan));
143         ast_string_field_set(snapshot, uniqueid, ast_channel_uniqueid(chan));
144         ast_string_field_set(snapshot, linkedid, ast_channel_linkedid(chan));
145         ast_string_field_set(snapshot, parkinglot, ast_channel_parkinglot(chan));
146         ast_string_field_set(snapshot, hangupsource, ast_channel_hangupsource(chan));
147         if (ast_channel_appl(chan)) {
148                 ast_string_field_set(snapshot, appl, ast_channel_appl(chan));
149         }
150         if (ast_channel_data(chan)) {
151                 ast_string_field_set(snapshot, data, ast_channel_data(chan));
152         }
153         ast_string_field_set(snapshot, context, ast_channel_context(chan));
154         ast_string_field_set(snapshot, exten, ast_channel_exten(chan));
155
156         ast_string_field_set(snapshot, caller_name,
157                 S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, ""));
158         ast_string_field_set(snapshot, caller_number,
159                 S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, ""));
160
161         ast_string_field_set(snapshot, connected_name,
162                 S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, ""));
163         ast_string_field_set(snapshot, connected_number,
164                 S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, ""));
165         ast_string_field_set(snapshot, language, ast_channel_language(chan));
166
167         snapshot->creationtime = ast_channel_creationtime(chan);
168         snapshot->state = ast_channel_state(chan);
169         snapshot->priority = ast_channel_priority(chan);
170         snapshot->amaflags = ast_channel_amaflags(chan);
171         snapshot->hangupcause = ast_channel_hangupcause(chan);
172         snapshot->flags = *ast_channel_flags(chan);
173         snapshot->caller_pres = ast_party_id_presentation(&ast_channel_caller(chan)->id);
174
175         snapshot->manager_vars = ast_channel_get_manager_vars(chan);
176
177         ao2_ref(snapshot, +1);
178         return snapshot;
179 }
180
181 static void publish_message_for_channel_topics(struct stasis_message *message, struct ast_channel *chan)
182 {
183         if (chan) {
184                 stasis_publish(ast_channel_topic(chan), message);
185         } else {
186                 stasis_publish(ast_channel_topic_all(), message);
187         }
188 }
189
190 static void channel_blob_dtor(void *obj)
191 {
192         struct ast_channel_blob *event = obj;
193         ao2_cleanup(event->snapshot);
194         ast_json_unref(event->blob);
195 }
196
197 void ast_channel_publish_dial(struct ast_channel *caller, struct ast_channel *peer, const char *dialstring, const char *dialstatus)
198 {
199         RAII_VAR(struct ast_multi_channel_blob *, payload, NULL, ao2_cleanup);
200         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
201         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
202         struct ast_channel_snapshot *caller_snapshot;
203         struct ast_channel_snapshot *peer_snapshot;
204
205         ast_assert(peer != NULL);
206         blob = ast_json_pack("{s: s, s: s}",
207                              "dialstatus", S_OR(dialstatus, ""),
208                              "dialstring", S_OR(dialstring, ""));
209         if (!blob) {
210                 return;
211         }
212         payload = ast_multi_channel_blob_create(blob);
213         if (!payload) {
214                 return;
215         }
216
217         if (caller) {
218                 caller_snapshot = ast_channel_snapshot_create(caller);
219                 if (!caller_snapshot) {
220                         return;
221                 }
222                 ast_multi_channel_blob_add_channel(payload, "caller", caller_snapshot);
223         }
224
225         peer_snapshot = ast_channel_snapshot_create(peer);
226         if (!peer_snapshot) {
227                 return;
228         }
229         ast_multi_channel_blob_add_channel(payload, "peer", peer_snapshot);
230
231         msg = stasis_message_create(ast_channel_dial_type(), payload);
232         if (!msg) {
233                 return;
234         }
235
236         publish_message_for_channel_topics(msg, caller);
237 }
238
239 static struct stasis_message *create_channel_blob_message(struct ast_channel_snapshot *snapshot,
240                 struct stasis_message_type *type,
241                 struct ast_json *blob)
242
243 {
244         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
245         RAII_VAR(struct ast_channel_blob *, obj, NULL, ao2_cleanup);
246
247         if (blob == NULL) {
248                 blob = ast_json_null();
249         }
250
251         obj = ao2_alloc(sizeof(*obj), channel_blob_dtor);
252         if (!obj) {
253                 return NULL;
254         }
255
256         if (snapshot) {
257                 obj->snapshot = snapshot;
258                 ao2_ref(obj->snapshot, +1);
259         }
260         obj->blob = ast_json_ref(blob);
261
262         msg = stasis_message_create(type, obj);
263         if (!msg) {
264                 return NULL;
265         }
266
267         ao2_ref(msg, +1);
268         return msg;
269 }
270
271 struct stasis_message *ast_channel_blob_create_from_cache(const char *channel_id,
272                                                struct stasis_message_type *type,
273                                                struct ast_json *blob)
274 {
275         RAII_VAR(struct ast_channel_snapshot *, snapshot,
276                         ast_channel_snapshot_get_latest(channel_id),
277                         ao2_cleanup);
278
279         return create_channel_blob_message(snapshot, type, blob);
280 }
281
282 struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
283         struct stasis_message_type *type, struct ast_json *blob)
284 {
285         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
286
287         if (chan) {
288                 snapshot = ast_channel_snapshot_create(chan);
289         }
290
291         return create_channel_blob_message(snapshot, type, blob);
292 }
293
294 /*! \brief A channel snapshot wrapper object used in \ref ast_multi_channel_blob objects */
295 struct channel_role_snapshot {
296         struct ast_channel_snapshot *snapshot;  /*!< A channel snapshot */
297         char role[0];                                                   /*!< The role assigned to the channel */
298 };
299
300 /*! \brief A multi channel blob data structure for multi_channel_blob stasis messages */
301 struct ast_multi_channel_blob {
302         struct ao2_container *channel_snapshots;        /*!< A container holding the snapshots */
303         struct ast_json *blob;                                          /*< A blob of JSON data */
304 };
305
306 /*! \internal \brief Standard comparison function for \ref channel_role_snapshot objects */
307 static int channel_role_single_cmp_cb(void *obj, void *arg, int flags)
308 {
309         struct channel_role_snapshot *left = obj;
310         struct channel_role_snapshot *right = arg;
311         const char *match = (flags & OBJ_KEY) ? arg : right->role;
312         return strcasecmp(left->role, match) ? 0 : (CMP_MATCH | CMP_STOP);
313 }
314
315 /*! \internal \brief Multi comparison function for \ref channel_role_snapshot objects */
316 static int channel_role_multi_cmp_cb(void *obj, void *arg, int flags)
317 {
318         struct channel_role_snapshot *left = obj;
319         struct channel_role_snapshot *right = arg;
320         const char *match = (flags & OBJ_KEY) ? arg : right->role;
321         return strcasecmp(left->role, match) ? 0 : (CMP_MATCH);
322 }
323
324 /*! \internal \brief Hash function for \ref channel_role_snapshot objects */
325 static int channel_role_hash_cb(const void *obj, const int flags)
326 {
327         const struct channel_role_snapshot *snapshot = obj;
328         const char *name = (flags & OBJ_KEY) ? obj : snapshot->role;
329         return ast_str_case_hash(name);
330 }
331
332 /*! \internal \brief Destructor for \ref ast_multi_channel_blob objects */
333 static void multi_channel_blob_dtor(void *obj)
334 {
335         struct ast_multi_channel_blob *multi_blob = obj;
336
337         ao2_cleanup(multi_blob->channel_snapshots);
338         ast_json_unref(multi_blob->blob);
339 }
340
341 struct ast_multi_channel_blob *ast_multi_channel_blob_create(struct ast_json *blob)
342 {
343         RAII_VAR(struct ast_multi_channel_blob *, obj,
344                         ao2_alloc(sizeof(*obj), multi_channel_blob_dtor),
345                         ao2_cleanup);
346
347         ast_assert(blob != NULL);
348
349         if (!obj) {
350                 return NULL;
351         }
352
353         obj->channel_snapshots = ao2_container_alloc(NUM_MULTI_CHANNEL_BLOB_BUCKETS,
354                         channel_role_hash_cb, channel_role_single_cmp_cb);
355         if (!obj->channel_snapshots) {
356                 return NULL;
357         }
358
359         obj->blob = ast_json_ref(blob);
360
361         ao2_ref(obj, +1);
362         return obj;
363 }
364
365 struct ast_channel_snapshot *ast_channel_snapshot_get_latest(const char *uniqueid)
366 {
367         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
368         struct ast_channel_snapshot *snapshot;
369
370         ast_assert(!ast_strlen_zero(uniqueid));
371
372         message = stasis_cache_get(ast_channel_topic_all_cached(),
373                         ast_channel_snapshot_type(),
374                         uniqueid);
375         if (!message) {
376                 return NULL;
377         }
378
379         snapshot = stasis_message_data(message);
380         if (!snapshot) {
381                 return NULL;
382         }
383         ao2_ref(snapshot, +1);
384         return snapshot;
385 }
386
387 struct ast_channel_snapshot *ast_channel_snapshot_get_latest_by_name(const char *name)
388 {
389         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
390         struct ast_channel_snapshot *snapshot;
391
392         ast_assert(!ast_strlen_zero(name));
393
394         message = stasis_cache_get(ast_channel_topic_all_cached_by_name(),
395                         ast_channel_snapshot_type(),
396                         name);
397         if (!message) {
398                 return NULL;
399         }
400
401         snapshot = stasis_message_data(message);
402         if (!snapshot) {
403                 return NULL;
404         }
405         ao2_ref(snapshot, +1);
406         return snapshot;
407 }
408
409 static void channel_role_snapshot_dtor(void *obj)
410 {
411         struct channel_role_snapshot *role_snapshot = obj;
412         ao2_cleanup(role_snapshot->snapshot);
413 }
414
415 void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj, const char *role, struct ast_channel_snapshot *snapshot)
416 {
417         RAII_VAR(struct channel_role_snapshot *, role_snapshot, NULL, ao2_cleanup);
418         int role_len = strlen(role) + 1;
419
420         if (!obj || ast_strlen_zero(role) || !snapshot) {
421                 return;
422         }
423
424         role_snapshot = ao2_alloc(sizeof(*role_snapshot) + role_len, channel_role_snapshot_dtor);
425         if (!role_snapshot) {
426                 return;
427         }
428         ast_copy_string(role_snapshot->role, role, role_len);
429         role_snapshot->snapshot = snapshot;
430         ao2_ref(role_snapshot->snapshot, +1);
431         ao2_link(obj->channel_snapshots, role_snapshot);
432 }
433
434 struct ast_channel_snapshot *ast_multi_channel_blob_get_channel(struct ast_multi_channel_blob *obj, const char *role)
435 {
436         struct channel_role_snapshot *role_snapshot;
437
438         if (!obj || ast_strlen_zero(role)) {
439                 return NULL;
440         }
441         role_snapshot = ao2_find(obj->channel_snapshots, role, OBJ_KEY);
442         /* Note that this function does not increase the ref count on snapshot */
443         if (!role_snapshot) {
444                 return NULL;
445         }
446         ao2_ref(role_snapshot, -1);
447         return role_snapshot->snapshot;
448 }
449
450 struct ao2_container *ast_multi_channel_blob_get_channels(struct ast_multi_channel_blob *obj, const char *role)
451 {
452         RAII_VAR(struct ao2_container *, ret_container,
453                 ao2_container_alloc(NUM_MULTI_CHANNEL_BLOB_BUCKETS, channel_snapshot_hash_cb, channel_snapshot_cmp_cb),
454                 ao2_cleanup);
455         struct ao2_iterator *it_role_snapshots;
456         struct channel_role_snapshot *role_snapshot;
457         char *arg;
458
459         if (!obj || ast_strlen_zero(role) || !ret_container) {
460                 return NULL;
461         }
462         arg = ast_strdupa(role);
463
464         it_role_snapshots = ao2_callback(obj->channel_snapshots, OBJ_MULTIPLE | OBJ_KEY, channel_role_multi_cmp_cb, arg);
465         if (!it_role_snapshots) {
466                 return NULL;
467         }
468
469         while ((role_snapshot = ao2_iterator_next(it_role_snapshots))) {
470                 ao2_link(ret_container, role_snapshot->snapshot);
471                 ao2_ref(role_snapshot, -1);
472         }
473         ao2_iterator_destroy(it_role_snapshots);
474
475         ao2_ref(ret_container, +1);
476         return ret_container;
477 }
478
479 struct ast_json *ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj)
480 {
481         if (!obj) {
482                 return NULL;
483         }
484         return obj->blob;
485 }
486
487 void ast_channel_publish_blob(struct ast_channel *chan, struct stasis_message_type *type, struct ast_json *blob)
488 {
489         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
490
491         if (!blob) {
492                 blob = ast_json_null();
493         }
494
495         message = ast_channel_blob_create(chan, type, blob);
496         if (message) {
497                 stasis_publish(ast_channel_topic(chan), message);
498         }
499 }
500
501 void ast_channel_publish_snapshot(struct ast_channel *chan)
502 {
503         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
504         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
505
506         snapshot = ast_channel_snapshot_create(chan);
507         if (!snapshot) {
508                 return;
509         }
510
511         message = stasis_message_create(ast_channel_snapshot_type(), snapshot);
512         if (!message) {
513                 return;
514         }
515
516         ast_assert(ast_channel_topic(chan) != NULL);
517         stasis_publish(ast_channel_topic(chan), message);
518 }
519
520 void ast_channel_publish_varset(struct ast_channel *chan, const char *name, const char *value)
521 {
522         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
523
524         ast_assert(name != NULL);
525         ast_assert(value != NULL);
526
527         blob = ast_json_pack("{s: s, s: s}",
528                              "variable", name,
529                              "value", value);
530         if (!blob) {
531                 ast_log(LOG_ERROR, "Error creating message\n");
532                 return;
533         }
534
535         ast_channel_publish_blob(chan, ast_channel_varset_type(), blob);
536 }
537
538 static struct ast_manager_event_blob *varset_to_ami(struct stasis_message *msg)
539 {
540         RAII_VAR(struct ast_str *, channel_event_string, NULL, ast_free);
541         struct ast_channel_blob *obj = stasis_message_data(msg);
542         const char *variable =
543                 ast_json_string_get(ast_json_object_get(obj->blob, "variable"));
544         const char *value =
545                 ast_json_string_get(ast_json_object_get(obj->blob, "value"));
546
547         if (obj->snapshot) {
548                 channel_event_string =
549                         ast_manager_build_channel_state_string(obj->snapshot);
550         } else {
551                 channel_event_string = ast_str_create(35);
552                 ast_str_set(&channel_event_string, 0,
553                             "Channel: none\r\n"
554                             "Uniqueid: none\r\n");
555         }
556
557         if (!channel_event_string) {
558                 return NULL;
559         }
560
561         return ast_manager_event_blob_create(EVENT_FLAG_DIALPLAN, "VarSet",
562                 "%s"
563                 "Variable: %s\r\n"
564                 "Value: %s\r\n",
565                 ast_str_buffer(channel_event_string), variable, value);
566 }
567
568 void ast_publish_channel_state(struct ast_channel *chan)
569 {
570         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
571         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
572
573         ast_assert(chan != NULL);
574         if (!chan) {
575                 return;
576         }
577
578         snapshot = ast_channel_snapshot_create(chan);
579         if (!snapshot) {
580                 return;
581         }
582
583         message = stasis_message_create(ast_channel_snapshot_type(), snapshot);
584         if (!message) {
585                 return;
586         }
587
588         ast_assert(ast_channel_topic(chan) != NULL);
589         stasis_publish(ast_channel_topic(chan), message);
590 }
591
592 struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot)
593 {
594         RAII_VAR(struct ast_json *, json_chan, NULL, ast_json_unref);
595
596         if (snapshot == NULL) {
597                 return NULL;
598         }
599
600         json_chan = ast_json_pack("{ s: s, s: s, s: s, s: s, s: s, s: s, s: s,"
601                                   "  s: s, s: s, s: s, s: s, s: o, s: o, s: o,"
602                                   "  s: o"
603                                   "}",
604                                   "name", snapshot->name,
605                                   "state", ast_state2str(snapshot->state),
606                                   "accountcode", snapshot->accountcode,
607                                   "peeraccount", snapshot->peeraccount,
608                                   "userfield", snapshot->userfield,
609                                   "uniqueid", snapshot->uniqueid,
610                                   "linkedid", snapshot->linkedid,
611                                   "parkinglot", snapshot->parkinglot,
612                                   "hangupsource", snapshot->hangupsource,
613                                   "appl", snapshot->appl,
614                                   "data", snapshot->data,
615                                   "dialplan", ast_json_dialplan_cep(snapshot->context, snapshot->exten, snapshot->priority),
616                                   "caller", ast_json_name_number(snapshot->caller_name, snapshot->caller_number),
617                                   "connected", ast_json_name_number(snapshot->connected_name, snapshot->connected_number),
618                                   "creationtime", ast_json_timeval(snapshot->creationtime, NULL));
619
620         return ast_json_ref(json_chan);
621 }
622
623 int ast_channel_snapshot_cep_equal(
624         const struct ast_channel_snapshot *old_snapshot,
625         const struct ast_channel_snapshot *new_snapshot)
626 {
627         ast_assert(old_snapshot != NULL);
628         ast_assert(new_snapshot != NULL);
629
630         /* We actually get some snapshots with CEP set, but before the
631          * application is set. Since empty application is invalid, we treat
632          * setting the application from nothing as a CEP change.
633          */
634         if (ast_strlen_zero(old_snapshot->appl) &&
635             !ast_strlen_zero(new_snapshot->appl)) {
636                 return 0;
637         }
638
639         return old_snapshot->priority == new_snapshot->priority &&
640                 strcmp(old_snapshot->context, new_snapshot->context) == 0 &&
641                 strcmp(old_snapshot->exten, new_snapshot->exten) == 0;
642 }
643
644 int ast_channel_snapshot_caller_id_equal(
645         const struct ast_channel_snapshot *old_snapshot,
646         const struct ast_channel_snapshot *new_snapshot)
647 {
648         ast_assert(old_snapshot != NULL);
649         ast_assert(new_snapshot != NULL);
650         return strcmp(old_snapshot->caller_number, new_snapshot->caller_number) == 0 &&
651                 strcmp(old_snapshot->caller_name, new_snapshot->caller_name) == 0;
652 }
653
654 /*!
655  * @{ \brief Define channel message types.
656  */
657 STASIS_MESSAGE_TYPE_DEFN(ast_channel_snapshot_type);
658 STASIS_MESSAGE_TYPE_DEFN(ast_channel_dial_type);
659 STASIS_MESSAGE_TYPE_DEFN(ast_channel_varset_type,
660         .to_ami = varset_to_ami,
661         );
662 STASIS_MESSAGE_TYPE_DEFN(ast_channel_user_event_type);
663 STASIS_MESSAGE_TYPE_DEFN(ast_channel_hangup_request_type);
664 STASIS_MESSAGE_TYPE_DEFN(ast_channel_dtmf_begin_type);
665 STASIS_MESSAGE_TYPE_DEFN(ast_channel_dtmf_end_type);
666 STASIS_MESSAGE_TYPE_DEFN(ast_channel_hold_type);
667 STASIS_MESSAGE_TYPE_DEFN(ast_channel_unhold_type);
668 STASIS_MESSAGE_TYPE_DEFN(ast_channel_chanspy_start_type);
669 STASIS_MESSAGE_TYPE_DEFN(ast_channel_chanspy_stop_type);
670 STASIS_MESSAGE_TYPE_DEFN(ast_channel_fax_type);
671 STASIS_MESSAGE_TYPE_DEFN(ast_channel_hangup_handler_type);
672 STASIS_MESSAGE_TYPE_DEFN(ast_channel_moh_start_type);
673 STASIS_MESSAGE_TYPE_DEFN(ast_channel_moh_stop_type);
674 STASIS_MESSAGE_TYPE_DEFN(ast_channel_monitor_start_type);
675 STASIS_MESSAGE_TYPE_DEFN(ast_channel_monitor_stop_type);
676
677 /*! @} */
678
679 static void stasis_channels_cleanup(void)
680 {
681         channel_topic_all_cached = stasis_caching_unsubscribe_and_join(channel_topic_all_cached);
682         channel_topic_all_cached_by_name = stasis_caching_unsubscribe_and_join(channel_topic_all_cached_by_name);
683         ao2_cleanup(channel_topic_all);
684         channel_topic_all = NULL;
685         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_snapshot_type);
686         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dial_type);
687         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_varset_type);
688         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_user_event_type);
689         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hangup_request_type);
690         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_begin_type);
691         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_end_type);
692         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hold_type);
693         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_unhold_type);
694         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_chanspy_start_type);
695         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_chanspy_stop_type);
696         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_fax_type);
697         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hangup_handler_type);
698         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_moh_start_type);
699         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_moh_stop_type);
700         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_monitor_start_type);
701         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_monitor_stop_type);
702 }
703
704 void ast_stasis_channels_init(void)
705 {
706         ast_register_cleanup(stasis_channels_cleanup);
707
708         STASIS_MESSAGE_TYPE_INIT(ast_channel_snapshot_type);
709         STASIS_MESSAGE_TYPE_INIT(ast_channel_dial_type);
710         STASIS_MESSAGE_TYPE_INIT(ast_channel_varset_type);
711         STASIS_MESSAGE_TYPE_INIT(ast_channel_user_event_type);
712         STASIS_MESSAGE_TYPE_INIT(ast_channel_hangup_request_type);
713         STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_begin_type);
714         STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_end_type);
715         STASIS_MESSAGE_TYPE_INIT(ast_channel_hold_type);
716         STASIS_MESSAGE_TYPE_INIT(ast_channel_unhold_type);
717         STASIS_MESSAGE_TYPE_INIT(ast_channel_chanspy_start_type);
718         STASIS_MESSAGE_TYPE_INIT(ast_channel_chanspy_stop_type);
719         STASIS_MESSAGE_TYPE_INIT(ast_channel_fax_type);
720         STASIS_MESSAGE_TYPE_INIT(ast_channel_hangup_handler_type);
721         STASIS_MESSAGE_TYPE_INIT(ast_channel_moh_start_type);
722         STASIS_MESSAGE_TYPE_INIT(ast_channel_moh_stop_type);
723         STASIS_MESSAGE_TYPE_INIT(ast_channel_monitor_start_type);
724         STASIS_MESSAGE_TYPE_INIT(ast_channel_monitor_stop_type);
725
726         channel_topic_all = stasis_topic_create("ast_channel_topic_all");
727         channel_topic_all_cached = stasis_caching_topic_create(channel_topic_all, channel_snapshot_get_id);
728         channel_topic_all_cached_by_name = stasis_caching_topic_create(channel_topic_all, channel_snapshot_get_name);
729 }