Fixed some extra field assertion when the event WebSocket is connected
[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}",
178                              "dialstatus", S_OR(dialstatus, ""),
179                              "dialstring", S_OR(dialstring, ""));
180         if (!blob) {
181                 return;
182         }
183         payload = ast_multi_channel_blob_create(blob);
184         if (!payload) {
185                 return;
186         }
187
188         if (caller) {
189                 caller_snapshot = ast_channel_snapshot_create(caller);
190                 if (!caller_snapshot) {
191                         return;
192                 }
193                 ast_multi_channel_blob_add_channel(payload, "caller", caller_snapshot);
194         }
195
196         peer_snapshot = ast_channel_snapshot_create(peer);
197         if (!peer_snapshot) {
198                 return;
199         }
200         ast_multi_channel_blob_add_channel(payload, "peer", peer_snapshot);
201
202         msg = stasis_message_create(ast_channel_dial_type(), payload);
203         if (!msg) {
204                 return;
205         }
206
207         publish_message_for_channel_topics(msg, caller);
208 }
209
210 struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
211         struct stasis_message_type *type, struct ast_json *blob)
212 {
213         RAII_VAR(struct ast_channel_blob *, obj, NULL, ao2_cleanup);
214         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
215
216         if (blob == NULL) {
217                 blob = ast_json_null();
218         }
219
220         obj = ao2_alloc(sizeof(*obj), channel_blob_dtor);
221         if (!obj) {
222                 return NULL;
223         }
224
225         if (chan) {
226                 obj->snapshot = ast_channel_snapshot_create(chan);
227                 if (obj->snapshot == NULL) {
228                         return NULL;
229                 }
230         }
231
232         obj->blob = ast_json_ref(blob);
233
234         msg = stasis_message_create(type, obj);
235         if (!msg) {
236                 return NULL;
237         }
238
239         ao2_ref(msg, +1);
240         return msg;
241 }
242
243 /*! \brief A channel snapshot wrapper object used in \ref ast_multi_channel_blob objects */
244 struct channel_role_snapshot {
245         struct ast_channel_snapshot *snapshot;  /*!< A channel snapshot */
246         char role[0];                                                   /*!< The role assigned to the channel */
247 };
248
249 /*! \brief A multi channel blob data structure for multi_channel_blob stasis messages */
250 struct ast_multi_channel_blob {
251         struct ao2_container *channel_snapshots;        /*!< A container holding the snapshots */
252         struct ast_json *blob;                                          /*< A blob of JSON data */
253 };
254
255 /*! \internal \brief Standard comparison function for \ref channel_role_snapshot objects */
256 static int channel_role_single_cmp_cb(void *obj, void *arg, int flags)
257 {
258         struct channel_role_snapshot *left = obj;
259         struct channel_role_snapshot *right = arg;
260         const char *match = (flags & OBJ_KEY) ? arg : right->role;
261         return strcasecmp(left->role, match) ? 0 : (CMP_MATCH | CMP_STOP);
262 }
263
264 /*! \internal \brief Multi comparison function for \ref channel_role_snapshot objects */
265 static int channel_role_multi_cmp_cb(void *obj, void *arg, int flags)
266 {
267         struct channel_role_snapshot *left = obj;
268         struct channel_role_snapshot *right = arg;
269         const char *match = (flags & OBJ_KEY) ? arg : right->role;
270         return strcasecmp(left->role, match) ? 0 : (CMP_MATCH);
271 }
272
273 /*! \internal \brief Hash function for \ref channel_role_snapshot objects */
274 static int channel_role_hash_cb(const void *obj, const int flags)
275 {
276         const struct channel_role_snapshot *snapshot = obj;
277         const char *name = (flags & OBJ_KEY) ? obj : snapshot->role;
278         return ast_str_case_hash(name);
279 }
280
281 /*! \internal \brief Destructor for \ref ast_multi_channel_blob objects */
282 static void multi_channel_blob_dtor(void *obj)
283 {
284         struct ast_multi_channel_blob *multi_blob = obj;
285
286         ao2_cleanup(multi_blob->channel_snapshots);
287         ast_json_unref(multi_blob->blob);
288 }
289
290 struct ast_multi_channel_blob *ast_multi_channel_blob_create(struct ast_json *blob)
291 {
292         RAII_VAR(struct ast_multi_channel_blob *, obj,
293                         ao2_alloc(sizeof(*obj), multi_channel_blob_dtor),
294                         ao2_cleanup);
295
296         ast_assert(blob != NULL);
297
298         if (!obj) {
299                 return NULL;
300         }
301
302         obj->channel_snapshots = ao2_container_alloc(NUM_MULTI_CHANNEL_BLOB_BUCKETS,
303                         channel_role_hash_cb, channel_role_single_cmp_cb);
304         if (!obj->channel_snapshots) {
305                 return NULL;
306         }
307
308         obj->blob = ast_json_ref(blob);
309
310         ao2_ref(obj, +1);
311         return obj;
312 }
313
314 static void channel_role_snapshot_dtor(void *obj)
315 {
316         struct channel_role_snapshot *role_snapshot = obj;
317         ao2_cleanup(role_snapshot->snapshot);
318 }
319
320 void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj, const char *role, struct ast_channel_snapshot *snapshot)
321 {
322         RAII_VAR(struct channel_role_snapshot *, role_snapshot, NULL, ao2_cleanup);
323         int role_len = strlen(role) + 1;
324
325         if (!obj || ast_strlen_zero(role) || !snapshot) {
326                 return;
327         }
328
329         role_snapshot = ao2_alloc(sizeof(*role_snapshot) + role_len, channel_role_snapshot_dtor);
330         if (!role_snapshot) {
331                 return;
332         }
333         ast_copy_string(role_snapshot->role, role, role_len);
334         role_snapshot->snapshot = snapshot;
335         ao2_ref(role_snapshot->snapshot, +1);
336         ao2_link(obj->channel_snapshots, role_snapshot);
337 }
338
339 struct ast_channel_snapshot *ast_multi_channel_blob_get_channel(struct ast_multi_channel_blob *obj, const char *role)
340 {
341         struct channel_role_snapshot *role_snapshot;
342
343         if (!obj || ast_strlen_zero(role)) {
344                 return NULL;
345         }
346         role_snapshot = ao2_find(obj->channel_snapshots, role, OBJ_KEY);
347         /* Note that this function does not increase the ref count on snapshot */
348         if (!role_snapshot) {
349                 return NULL;
350         }
351         ao2_ref(role_snapshot, -1);
352         return role_snapshot->snapshot;
353 }
354
355 struct ao2_container *ast_multi_channel_blob_get_channels(struct ast_multi_channel_blob *obj, const char *role)
356 {
357         RAII_VAR(struct ao2_container *, ret_container,
358                 ao2_container_alloc(NUM_MULTI_CHANNEL_BLOB_BUCKETS, channel_snapshot_hash_cb, channel_snapshot_cmp_cb),
359                 ao2_cleanup);
360         struct ao2_iterator *it_role_snapshots;
361         struct channel_role_snapshot *role_snapshot;
362         char *arg;
363
364         if (!obj || ast_strlen_zero(role) || !ret_container) {
365                 return NULL;
366         }
367         arg = ast_strdupa(role);
368
369         it_role_snapshots = ao2_callback(obj->channel_snapshots, OBJ_MULTIPLE | OBJ_KEY, channel_role_multi_cmp_cb, arg);
370         if (!it_role_snapshots) {
371                 return NULL;
372         }
373
374         while ((role_snapshot = ao2_iterator_next(it_role_snapshots))) {
375                 ao2_link(ret_container, role_snapshot->snapshot);
376                 ao2_ref(role_snapshot, -1);
377         }
378         ao2_iterator_destroy(it_role_snapshots);
379
380         ao2_ref(ret_container, +1);
381         return ret_container;
382 }
383
384 struct ast_json *ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj)
385 {
386         if (!obj) {
387                 return NULL;
388         }
389         return obj->blob;
390 }
391
392 void ast_channel_publish_varset(struct ast_channel *chan, const char *name, const char *value)
393 {
394         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
395         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
396
397         ast_assert(name != NULL);
398         ast_assert(value != NULL);
399
400         blob = ast_json_pack("{s: s, s: s}",
401                              "variable", name,
402                              "value", value);
403         if (!blob) {
404                 ast_log(LOG_ERROR, "Error creating message\n");
405                 return;
406         }
407
408         msg = ast_channel_blob_create(chan, ast_channel_varset_type(),
409                 ast_json_ref(blob));
410
411         if (!msg) {
412                 return;
413         }
414
415         publish_message_for_channel_topics(msg, chan);
416 }
417
418 void ast_publish_channel_state(struct ast_channel *chan)
419 {
420         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
421         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
422
423         ast_assert(chan != NULL);
424         if (!chan) {
425                 return;
426         }
427
428         snapshot = ast_channel_snapshot_create(chan);
429         if (!snapshot) {
430                 return;
431         }
432
433         message = stasis_message_create(ast_channel_snapshot_type(), snapshot);
434         if (!message) {
435                 return;
436         }
437
438         ast_assert(ast_channel_topic(chan) != NULL);
439         stasis_publish(ast_channel_topic(chan), message);
440 }
441
442 struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot)
443 {
444         RAII_VAR(struct ast_json *, json_chan, NULL, ast_json_unref);
445
446         if (snapshot == NULL) {
447                 return NULL;
448         }
449
450         json_chan = ast_json_pack("{ s: s, s: s, s: s, s: s, s: s, s: s, s: s,"
451                                   "  s: s, s: s, s: s, s: s, s: o, s: o, s: o,"
452                                   "  s: o"
453                                   "}",
454                                   "name", snapshot->name,
455                                   "state", ast_state2str(snapshot->state),
456                                   "accountcode", snapshot->accountcode,
457                                   "peeraccount", snapshot->peeraccount,
458                                   "userfield", snapshot->userfield,
459                                   "uniqueid", snapshot->uniqueid,
460                                   "linkedid", snapshot->linkedid,
461                                   "parkinglot", snapshot->parkinglot,
462                                   "hangupsource", snapshot->hangupsource,
463                                   "appl", snapshot->appl,
464                                   "data", snapshot->data,
465                                   "dialplan", ast_json_dialplan_cep(snapshot->context, snapshot->exten, snapshot->priority),
466                                   "caller", ast_json_name_number(snapshot->caller_name, snapshot->caller_number),
467                                   "connected", ast_json_name_number(snapshot->connected_name, snapshot->connected_number),
468                                   "creationtime", ast_json_timeval(snapshot->creationtime, NULL));
469
470         return ast_json_ref(json_chan);
471 }
472
473 int ast_channel_snapshot_cep_equal(
474         const struct ast_channel_snapshot *old_snapshot,
475         const struct ast_channel_snapshot *new_snapshot)
476 {
477         ast_assert(old_snapshot != NULL);
478         ast_assert(new_snapshot != NULL);
479
480         /* We actually get some snapshots with CEP set, but before the
481          * application is set. Since empty application is invalid, we treat
482          * setting the application from nothing as a CEP change.
483          */
484         if (ast_strlen_zero(old_snapshot->appl) &&
485             !ast_strlen_zero(new_snapshot->appl)) {
486                 return 0;
487         }
488
489         return old_snapshot->priority == new_snapshot->priority &&
490                 strcmp(old_snapshot->context, new_snapshot->context) == 0 &&
491                 strcmp(old_snapshot->exten, new_snapshot->exten) == 0;
492 }
493
494 int ast_channel_snapshot_caller_id_equal(
495         const struct ast_channel_snapshot *old_snapshot,
496         const struct ast_channel_snapshot *new_snapshot)
497 {
498         ast_assert(old_snapshot != NULL);
499         ast_assert(new_snapshot != NULL);
500         return strcmp(old_snapshot->caller_number, new_snapshot->caller_number) == 0 &&
501                 strcmp(old_snapshot->caller_name, new_snapshot->caller_name) == 0;
502 }
503
504 void ast_stasis_channels_shutdown(void)
505 {
506         channel_topic_all_cached = stasis_caching_unsubscribe_and_join(channel_topic_all_cached);
507         ao2_cleanup(channel_topic_all);
508         channel_topic_all = NULL;
509         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_snapshot_type);
510         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dial_type);
511         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_varset_type);
512         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_user_event_type);
513         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hangup_request_type);
514         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_begin_type);
515         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_end_type);
516 }
517
518 void ast_stasis_channels_init(void)
519 {
520         STASIS_MESSAGE_TYPE_INIT(ast_channel_snapshot_type);
521         STASIS_MESSAGE_TYPE_INIT(ast_channel_dial_type);
522         STASIS_MESSAGE_TYPE_INIT(ast_channel_varset_type);
523         STASIS_MESSAGE_TYPE_INIT(ast_channel_user_event_type);
524         STASIS_MESSAGE_TYPE_INIT(ast_channel_hangup_request_type);
525         STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_begin_type);
526         STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_end_type);
527
528         channel_topic_all = stasis_topic_create("ast_channel_topic_all");
529         channel_topic_all_cached = stasis_caching_topic_create(channel_topic_all, channel_snapshot_get_id);
530 }