This patch implements the REST API's for POST /channels/{channelId}/play
[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         ast_string_field_set(snapshot, language, ast_channel_language(chan));
138
139         snapshot->creationtime = ast_channel_creationtime(chan);
140         snapshot->state = ast_channel_state(chan);
141         snapshot->priority = ast_channel_priority(chan);
142         snapshot->amaflags = ast_channel_amaflags(chan);
143         snapshot->hangupcause = ast_channel_hangupcause(chan);
144         snapshot->flags = *ast_channel_flags(chan);
145         snapshot->caller_pres = ast_party_id_presentation(&ast_channel_caller(chan)->id);
146
147         snapshot->manager_vars = ast_channel_get_manager_vars(chan);
148
149         ao2_ref(snapshot, +1);
150         return snapshot;
151 }
152
153 struct ast_channel_snapshot *ast_channel_snapshot_get_latest(
154         const char *uniqueid)
155 {
156         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
157         struct ast_channel_snapshot *snapshot;
158
159         msg = stasis_cache_get(ast_channel_topic_all_cached(),
160                 ast_channel_snapshot_type(), uniqueid);
161
162         if (!msg) {
163                 return NULL;
164         }
165
166         snapshot = stasis_message_data(msg);
167         if (!snapshot) {
168                 return NULL;
169         }
170
171         ao2_ref(snapshot, +1);
172         return snapshot;
173 }
174
175 static void publish_message_for_channel_topics(struct stasis_message *message, struct ast_channel *chan)
176 {
177         if (chan) {
178                 stasis_publish(ast_channel_topic(chan), message);
179         } else {
180                 stasis_publish(ast_channel_topic_all(), message);
181         }
182 }
183
184 static void channel_blob_dtor(void *obj)
185 {
186         struct ast_channel_blob *event = obj;
187         ao2_cleanup(event->snapshot);
188         ast_json_unref(event->blob);
189 }
190
191 void ast_channel_publish_dial(struct ast_channel *caller, struct ast_channel *peer, const char *dialstring, const char *dialstatus)
192 {
193         RAII_VAR(struct ast_multi_channel_blob *, payload, NULL, ao2_cleanup);
194         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
195         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
196         struct ast_channel_snapshot *caller_snapshot;
197         struct ast_channel_snapshot *peer_snapshot;
198
199         ast_assert(peer != NULL);
200         blob = ast_json_pack("{s: s, s: s}",
201                              "dialstatus", S_OR(dialstatus, ""),
202                              "dialstring", S_OR(dialstring, ""));
203         if (!blob) {
204                 return;
205         }
206         payload = ast_multi_channel_blob_create(blob);
207         if (!payload) {
208                 return;
209         }
210
211         if (caller) {
212                 caller_snapshot = ast_channel_snapshot_create(caller);
213                 if (!caller_snapshot) {
214                         return;
215                 }
216                 ast_multi_channel_blob_add_channel(payload, "caller", caller_snapshot);
217         }
218
219         peer_snapshot = ast_channel_snapshot_create(peer);
220         if (!peer_snapshot) {
221                 return;
222         }
223         ast_multi_channel_blob_add_channel(payload, "peer", peer_snapshot);
224
225         msg = stasis_message_create(ast_channel_dial_type(), payload);
226         if (!msg) {
227                 return;
228         }
229
230         publish_message_for_channel_topics(msg, caller);
231 }
232
233 static struct stasis_message *channel_blob_create(
234         struct ast_channel_snapshot *snapshot,
235         struct stasis_message_type *type, struct ast_json *blob)
236 {
237         RAII_VAR(struct ast_channel_blob *, obj, NULL, ao2_cleanup);
238         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
239
240         if (blob == NULL) {
241                 blob = ast_json_null();
242         }
243
244         obj = ao2_alloc(sizeof(*obj), channel_blob_dtor);
245         if (!obj) {
246                 return NULL;
247         }
248
249         if (snapshot) {
250                 ao2_ref(snapshot, +1);
251                 obj->snapshot = snapshot;
252         }
253
254         obj->blob = ast_json_ref(blob);
255
256         msg = stasis_message_create(type, obj);
257         if (!msg) {
258                 return NULL;
259         }
260
261         ao2_ref(msg, +1);
262         return msg;
263 }
264
265 struct stasis_message *ast_channel_blob_create(struct ast_channel *chan,
266         struct stasis_message_type *type, struct ast_json *blob)
267 {
268         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
269
270         if (chan != NULL) {
271                 snapshot = ast_channel_snapshot_create(chan);
272                 if (snapshot == NULL) {
273                         return NULL;
274                 }
275         }
276
277         return channel_blob_create(snapshot, type, blob);
278 }
279
280 struct stasis_message *ast_channel_blob_create_from_cache(
281         const char *uniqueid, struct stasis_message_type *type,
282         struct ast_json *blob)
283 {
284         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
285
286         snapshot = ast_channel_snapshot_get_latest(uniqueid);
287         if (snapshot == NULL) {
288                 return NULL;
289         }
290
291         return channel_blob_create(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 static void channel_role_snapshot_dtor(void *obj)
366 {
367         struct channel_role_snapshot *role_snapshot = obj;
368         ao2_cleanup(role_snapshot->snapshot);
369 }
370
371 void ast_multi_channel_blob_add_channel(struct ast_multi_channel_blob *obj, const char *role, struct ast_channel_snapshot *snapshot)
372 {
373         RAII_VAR(struct channel_role_snapshot *, role_snapshot, NULL, ao2_cleanup);
374         int role_len = strlen(role) + 1;
375
376         if (!obj || ast_strlen_zero(role) || !snapshot) {
377                 return;
378         }
379
380         role_snapshot = ao2_alloc(sizeof(*role_snapshot) + role_len, channel_role_snapshot_dtor);
381         if (!role_snapshot) {
382                 return;
383         }
384         ast_copy_string(role_snapshot->role, role, role_len);
385         role_snapshot->snapshot = snapshot;
386         ao2_ref(role_snapshot->snapshot, +1);
387         ao2_link(obj->channel_snapshots, role_snapshot);
388 }
389
390 struct ast_channel_snapshot *ast_multi_channel_blob_get_channel(struct ast_multi_channel_blob *obj, const char *role)
391 {
392         struct channel_role_snapshot *role_snapshot;
393
394         if (!obj || ast_strlen_zero(role)) {
395                 return NULL;
396         }
397         role_snapshot = ao2_find(obj->channel_snapshots, role, OBJ_KEY);
398         /* Note that this function does not increase the ref count on snapshot */
399         if (!role_snapshot) {
400                 return NULL;
401         }
402         ao2_ref(role_snapshot, -1);
403         return role_snapshot->snapshot;
404 }
405
406 struct ao2_container *ast_multi_channel_blob_get_channels(struct ast_multi_channel_blob *obj, const char *role)
407 {
408         RAII_VAR(struct ao2_container *, ret_container,
409                 ao2_container_alloc(NUM_MULTI_CHANNEL_BLOB_BUCKETS, channel_snapshot_hash_cb, channel_snapshot_cmp_cb),
410                 ao2_cleanup);
411         struct ao2_iterator *it_role_snapshots;
412         struct channel_role_snapshot *role_snapshot;
413         char *arg;
414
415         if (!obj || ast_strlen_zero(role) || !ret_container) {
416                 return NULL;
417         }
418         arg = ast_strdupa(role);
419
420         it_role_snapshots = ao2_callback(obj->channel_snapshots, OBJ_MULTIPLE | OBJ_KEY, channel_role_multi_cmp_cb, arg);
421         if (!it_role_snapshots) {
422                 return NULL;
423         }
424
425         while ((role_snapshot = ao2_iterator_next(it_role_snapshots))) {
426                 ao2_link(ret_container, role_snapshot->snapshot);
427                 ao2_ref(role_snapshot, -1);
428         }
429         ao2_iterator_destroy(it_role_snapshots);
430
431         ao2_ref(ret_container, +1);
432         return ret_container;
433 }
434
435 struct ast_json *ast_multi_channel_blob_get_json(struct ast_multi_channel_blob *obj)
436 {
437         if (!obj) {
438                 return NULL;
439         }
440         return obj->blob;
441 }
442
443 void ast_channel_publish_snapshot(struct ast_channel *chan)
444 {
445         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
446         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
447
448         snapshot = ast_channel_snapshot_create(chan);
449         if (!snapshot) {
450                 return;
451         }
452
453         message = stasis_message_create(ast_channel_snapshot_type(), snapshot);
454         if (!message) {
455                 return;
456         }
457
458         ast_assert(ast_channel_topic(chan) != NULL);
459         stasis_publish(ast_channel_topic(chan), message);
460 }
461
462
463 void ast_channel_publish_varset(struct ast_channel *chan, const char *name, const char *value)
464 {
465         RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
466         RAII_VAR(struct ast_json *, blob, NULL, ast_json_unref);
467
468         ast_assert(name != NULL);
469         ast_assert(value != NULL);
470
471         blob = ast_json_pack("{s: s, s: s}",
472                              "variable", name,
473                              "value", value);
474         if (!blob) {
475                 ast_log(LOG_ERROR, "Error creating message\n");
476                 return;
477         }
478
479         msg = ast_channel_blob_create(chan, ast_channel_varset_type(),
480                 ast_json_ref(blob));
481
482         if (!msg) {
483                 return;
484         }
485
486         publish_message_for_channel_topics(msg, chan);
487 }
488
489 void ast_publish_channel_state(struct ast_channel *chan)
490 {
491         RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
492         RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
493
494         ast_assert(chan != NULL);
495         if (!chan) {
496                 return;
497         }
498
499         snapshot = ast_channel_snapshot_create(chan);
500         if (!snapshot) {
501                 return;
502         }
503
504         message = stasis_message_create(ast_channel_snapshot_type(), snapshot);
505         if (!message) {
506                 return;
507         }
508
509         ast_assert(ast_channel_topic(chan) != NULL);
510         stasis_publish(ast_channel_topic(chan), message);
511 }
512
513 struct ast_json *ast_channel_snapshot_to_json(const struct ast_channel_snapshot *snapshot)
514 {
515         RAII_VAR(struct ast_json *, json_chan, NULL, ast_json_unref);
516
517         if (snapshot == NULL) {
518                 return NULL;
519         }
520
521         json_chan = ast_json_pack("{ s: s, s: s, s: s, s: s, s: s, s: s, s: s,"
522                                   "  s: s, s: s, s: s, s: s, s: o, s: o, s: o,"
523                                   "  s: o"
524                                   "}",
525                                   "name", snapshot->name,
526                                   "state", ast_state2str(snapshot->state),
527                                   "accountcode", snapshot->accountcode,
528                                   "peeraccount", snapshot->peeraccount,
529                                   "userfield", snapshot->userfield,
530                                   "uniqueid", snapshot->uniqueid,
531                                   "linkedid", snapshot->linkedid,
532                                   "parkinglot", snapshot->parkinglot,
533                                   "hangupsource", snapshot->hangupsource,
534                                   "appl", snapshot->appl,
535                                   "data", snapshot->data,
536                                   "dialplan", ast_json_dialplan_cep(snapshot->context, snapshot->exten, snapshot->priority),
537                                   "caller", ast_json_name_number(snapshot->caller_name, snapshot->caller_number),
538                                   "connected", ast_json_name_number(snapshot->connected_name, snapshot->connected_number),
539                                   "creationtime", ast_json_timeval(snapshot->creationtime, NULL));
540
541         return ast_json_ref(json_chan);
542 }
543
544 int ast_channel_snapshot_cep_equal(
545         const struct ast_channel_snapshot *old_snapshot,
546         const struct ast_channel_snapshot *new_snapshot)
547 {
548         ast_assert(old_snapshot != NULL);
549         ast_assert(new_snapshot != NULL);
550
551         /* We actually get some snapshots with CEP set, but before the
552          * application is set. Since empty application is invalid, we treat
553          * setting the application from nothing as a CEP change.
554          */
555         if (ast_strlen_zero(old_snapshot->appl) &&
556             !ast_strlen_zero(new_snapshot->appl)) {
557                 return 0;
558         }
559
560         return old_snapshot->priority == new_snapshot->priority &&
561                 strcmp(old_snapshot->context, new_snapshot->context) == 0 &&
562                 strcmp(old_snapshot->exten, new_snapshot->exten) == 0;
563 }
564
565 int ast_channel_snapshot_caller_id_equal(
566         const struct ast_channel_snapshot *old_snapshot,
567         const struct ast_channel_snapshot *new_snapshot)
568 {
569         ast_assert(old_snapshot != NULL);
570         ast_assert(new_snapshot != NULL);
571         return strcmp(old_snapshot->caller_number, new_snapshot->caller_number) == 0 &&
572                 strcmp(old_snapshot->caller_name, new_snapshot->caller_name) == 0;
573 }
574
575 void ast_stasis_channels_shutdown(void)
576 {
577         channel_topic_all_cached = stasis_caching_unsubscribe_and_join(channel_topic_all_cached);
578         ao2_cleanup(channel_topic_all);
579         channel_topic_all = NULL;
580         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_snapshot_type);
581         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dial_type);
582         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_varset_type);
583         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_user_event_type);
584         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_hangup_request_type);
585         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_begin_type);
586         STASIS_MESSAGE_TYPE_CLEANUP(ast_channel_dtmf_end_type);
587 }
588
589 void ast_stasis_channels_init(void)
590 {
591         STASIS_MESSAGE_TYPE_INIT(ast_channel_snapshot_type);
592         STASIS_MESSAGE_TYPE_INIT(ast_channel_dial_type);
593         STASIS_MESSAGE_TYPE_INIT(ast_channel_varset_type);
594         STASIS_MESSAGE_TYPE_INIT(ast_channel_user_event_type);
595         STASIS_MESSAGE_TYPE_INIT(ast_channel_hangup_request_type);
596         STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_begin_type);
597         STASIS_MESSAGE_TYPE_INIT(ast_channel_dtmf_end_type);
598
599         channel_topic_all = stasis_topic_create("ast_channel_topic_all");
600         channel_topic_all_cached = stasis_caching_topic_create(channel_topic_all, channel_snapshot_get_id);
601 }