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