e96d84e4ff46f77fa1b4e0145e1c33d033424f22
[asterisk/asterisk.git] / res / res_stasis_json_events.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012 - 2013, Digium, Inc.
5  *
6  * David M. Lee, II <dlee@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 /*
20  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21  * !!!!!                               DO NOT EDIT                        !!!!!
22  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23  * This file is generated by a mustache template. Please see the original
24  * template in rest-api-templates/res_stasis_http_resource.c.mustache
25  */
26
27 /*! \file
28  *
29  * \brief WebSocket resource
30  *
31  * \author David M. Lee, II <dlee@digium.com>
32  */
33
34 /*** MODULEINFO
35         <support_level>core</support_level>
36  ***/
37
38 #include "asterisk.h"
39
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41
42 #include "asterisk/module.h"
43 #include "asterisk/json.h"
44 #include "stasis_json/resource_events.h"
45 #include "asterisk/stasis_channels.h"
46 #include "asterisk/stasis_bridging.h"
47
48 struct ast_json *stasis_json_event_channel_userevent_create(
49         struct ast_channel_snapshot *channel_snapshot,
50         struct ast_json *blob
51         )
52 {
53         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
54         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
55         struct ast_json *validator;
56         int ret;
57
58         ast_assert(channel_snapshot != NULL);
59         ast_assert(blob != NULL);
60         ast_assert(ast_json_object_get(blob, "channel") == NULL);
61         ast_assert(ast_json_object_get(blob, "type") == NULL);
62
63         validator = ast_json_object_get(blob, "eventname");
64         if (validator) {
65                 /* do validation? XXX */
66         } else {
67                 /* fail message generation if the required parameter doesn't exist */
68                 return NULL;
69         }
70
71         event = ast_json_deep_copy(blob);
72         if (!event) {
73                 return NULL;
74         }
75
76         ret = ast_json_object_set(event,
77                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
78         if (ret) {
79                 return NULL;
80         }
81
82         message = ast_json_pack("{s: o}", "channel_userevent", ast_json_ref(event));
83         if (!message) {
84                 return NULL;
85         }
86
87         return ast_json_ref(message);
88 }
89
90 struct ast_json *stasis_json_event_bridge_created_create(
91         struct ast_bridge_snapshot *bridge_snapshot
92         )
93 {
94         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
95         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
96         int ret;
97
98         ast_assert(bridge_snapshot != NULL);
99
100         event = ast_json_object_create();
101         if (!event) {
102                 return NULL;
103         }
104
105         ret = ast_json_object_set(event,
106                 "bridge", ast_bridge_snapshot_to_json(bridge_snapshot));
107         if (ret) {
108                 return NULL;
109         }
110
111         message = ast_json_pack("{s: o}", "bridge_created", ast_json_ref(event));
112         if (!message) {
113                 return NULL;
114         }
115
116         return ast_json_ref(message);
117 }
118
119 struct ast_json *stasis_json_event_playback_finished_create(
120         struct ast_json *blob
121         )
122 {
123         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
124         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
125         struct ast_json *validator;
126
127         ast_assert(blob != NULL);
128         ast_assert(ast_json_object_get(blob, "type") == NULL);
129
130         validator = ast_json_object_get(blob, "playback");
131         if (validator) {
132                 /* do validation? XXX */
133         } else {
134                 /* fail message generation if the required parameter doesn't exist */
135                 return NULL;
136         }
137
138         event = ast_json_deep_copy(blob);
139         if (!event) {
140                 return NULL;
141         }
142
143         message = ast_json_pack("{s: o}", "playback_finished", ast_json_ref(event));
144         if (!message) {
145                 return NULL;
146         }
147
148         return ast_json_ref(message);
149 }
150
151 struct ast_json *stasis_json_event_channel_snapshot_create(
152         struct ast_channel_snapshot *channel_snapshot
153         )
154 {
155         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
156         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
157         int ret;
158
159         ast_assert(channel_snapshot != NULL);
160
161         event = ast_json_object_create();
162         if (!event) {
163                 return NULL;
164         }
165
166         ret = ast_json_object_set(event,
167                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
168         if (ret) {
169                 return NULL;
170         }
171
172         message = ast_json_pack("{s: o}", "channel_snapshot", ast_json_ref(event));
173         if (!message) {
174                 return NULL;
175         }
176
177         return ast_json_ref(message);
178 }
179
180 struct ast_json *stasis_json_event_channel_caller_id_create(
181         struct ast_channel_snapshot *channel_snapshot,
182         struct ast_json *blob
183         )
184 {
185         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
186         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
187         struct ast_json *validator;
188         int ret;
189
190         ast_assert(channel_snapshot != NULL);
191         ast_assert(blob != NULL);
192         ast_assert(ast_json_object_get(blob, "channel") == NULL);
193         ast_assert(ast_json_object_get(blob, "type") == NULL);
194
195         validator = ast_json_object_get(blob, "caller_presentation_txt");
196         if (validator) {
197                 /* do validation? XXX */
198         } else {
199                 /* fail message generation if the required parameter doesn't exist */
200                 return NULL;
201         }
202
203         validator = ast_json_object_get(blob, "caller_presentation");
204         if (validator) {
205                 /* do validation? XXX */
206         } else {
207                 /* fail message generation if the required parameter doesn't exist */
208                 return NULL;
209         }
210
211         event = ast_json_deep_copy(blob);
212         if (!event) {
213                 return NULL;
214         }
215
216         ret = ast_json_object_set(event,
217                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
218         if (ret) {
219                 return NULL;
220         }
221
222         message = ast_json_pack("{s: o}", "channel_caller_id", ast_json_ref(event));
223         if (!message) {
224                 return NULL;
225         }
226
227         return ast_json_ref(message);
228 }
229
230 struct ast_json *stasis_json_event_playback_started_create(
231         struct ast_json *blob
232         )
233 {
234         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
235         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
236         struct ast_json *validator;
237
238         ast_assert(blob != NULL);
239         ast_assert(ast_json_object_get(blob, "type") == NULL);
240
241         validator = ast_json_object_get(blob, "playback");
242         if (validator) {
243                 /* do validation? XXX */
244         } else {
245                 /* fail message generation if the required parameter doesn't exist */
246                 return NULL;
247         }
248
249         event = ast_json_deep_copy(blob);
250         if (!event) {
251                 return NULL;
252         }
253
254         message = ast_json_pack("{s: o}", "playback_started", ast_json_ref(event));
255         if (!message) {
256                 return NULL;
257         }
258
259         return ast_json_ref(message);
260 }
261
262 struct ast_json *stasis_json_event_bridge_destroyed_create(
263         struct ast_bridge_snapshot *bridge_snapshot
264         )
265 {
266         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
267         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
268         int ret;
269
270         ast_assert(bridge_snapshot != NULL);
271
272         event = ast_json_object_create();
273         if (!event) {
274                 return NULL;
275         }
276
277         ret = ast_json_object_set(event,
278                 "bridge", ast_bridge_snapshot_to_json(bridge_snapshot));
279         if (ret) {
280                 return NULL;
281         }
282
283         message = ast_json_pack("{s: o}", "bridge_destroyed", ast_json_ref(event));
284         if (!message) {
285                 return NULL;
286         }
287
288         return ast_json_ref(message);
289 }
290
291 struct ast_json *stasis_json_event_application_replaced_create(
292         struct ast_json *blob
293         )
294 {
295         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
296         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
297         struct ast_json *validator;
298
299         ast_assert(blob != NULL);
300         ast_assert(ast_json_object_get(blob, "type") == NULL);
301
302         validator = ast_json_object_get(blob, "application");
303         if (validator) {
304                 /* do validation? XXX */
305         } else {
306                 /* fail message generation if the required parameter doesn't exist */
307                 return NULL;
308         }
309
310         event = ast_json_deep_copy(blob);
311         if (!event) {
312                 return NULL;
313         }
314
315         message = ast_json_pack("{s: o}", "application_replaced", ast_json_ref(event));
316         if (!message) {
317                 return NULL;
318         }
319
320         return ast_json_ref(message);
321 }
322
323 struct ast_json *stasis_json_event_channel_destroyed_create(
324         struct ast_channel_snapshot *channel_snapshot,
325         struct ast_json *blob
326         )
327 {
328         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
329         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
330         struct ast_json *validator;
331         int ret;
332
333         ast_assert(channel_snapshot != NULL);
334         ast_assert(blob != NULL);
335         ast_assert(ast_json_object_get(blob, "channel") == NULL);
336         ast_assert(ast_json_object_get(blob, "type") == NULL);
337
338         validator = ast_json_object_get(blob, "cause");
339         if (validator) {
340                 /* do validation? XXX */
341         } else {
342                 /* fail message generation if the required parameter doesn't exist */
343                 return NULL;
344         }
345
346         validator = ast_json_object_get(blob, "cause_txt");
347         if (validator) {
348                 /* do validation? XXX */
349         } else {
350                 /* fail message generation if the required parameter doesn't exist */
351                 return NULL;
352         }
353
354         event = ast_json_deep_copy(blob);
355         if (!event) {
356                 return NULL;
357         }
358
359         ret = ast_json_object_set(event,
360                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
361         if (ret) {
362                 return NULL;
363         }
364
365         message = ast_json_pack("{s: o}", "channel_destroyed", ast_json_ref(event));
366         if (!message) {
367                 return NULL;
368         }
369
370         return ast_json_ref(message);
371 }
372
373 struct ast_json *stasis_json_event_channel_varset_create(
374         struct ast_channel_snapshot *channel_snapshot,
375         struct ast_json *blob
376         )
377 {
378         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
379         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
380         struct ast_json *validator;
381         int ret;
382
383         ast_assert(channel_snapshot != NULL);
384         ast_assert(blob != NULL);
385         ast_assert(ast_json_object_get(blob, "channel") == NULL);
386         ast_assert(ast_json_object_get(blob, "type") == NULL);
387
388         validator = ast_json_object_get(blob, "variable");
389         if (validator) {
390                 /* do validation? XXX */
391         } else {
392                 /* fail message generation if the required parameter doesn't exist */
393                 return NULL;
394         }
395
396         validator = ast_json_object_get(blob, "value");
397         if (validator) {
398                 /* do validation? XXX */
399         } else {
400                 /* fail message generation if the required parameter doesn't exist */
401                 return NULL;
402         }
403
404         event = ast_json_deep_copy(blob);
405         if (!event) {
406                 return NULL;
407         }
408
409         ret = ast_json_object_set(event,
410                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
411         if (ret) {
412                 return NULL;
413         }
414
415         message = ast_json_pack("{s: o}", "channel_varset", ast_json_ref(event));
416         if (!message) {
417                 return NULL;
418         }
419
420         return ast_json_ref(message);
421 }
422
423 struct ast_json *stasis_json_event_channel_left_bridge_create(
424         struct ast_bridge_snapshot *bridge_snapshot,
425         struct ast_channel_snapshot *channel_snapshot
426         )
427 {
428         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
429         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
430         int ret;
431
432         ast_assert(channel_snapshot != NULL);
433         ast_assert(bridge_snapshot != NULL);
434
435         event = ast_json_object_create();
436         if (!event) {
437                 return NULL;
438         }
439
440         ret = ast_json_object_set(event,
441                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
442         if (ret) {
443                 return NULL;
444         }
445
446         ret = ast_json_object_set(event,
447                 "bridge", ast_bridge_snapshot_to_json(bridge_snapshot));
448         if (ret) {
449                 return NULL;
450         }
451
452         message = ast_json_pack("{s: o}", "channel_left_bridge", ast_json_ref(event));
453         if (!message) {
454                 return NULL;
455         }
456
457         return ast_json_ref(message);
458 }
459
460 struct ast_json *stasis_json_event_channel_created_create(
461         struct ast_channel_snapshot *channel_snapshot
462         )
463 {
464         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
465         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
466         int ret;
467
468         ast_assert(channel_snapshot != NULL);
469
470         event = ast_json_object_create();
471         if (!event) {
472                 return NULL;
473         }
474
475         ret = ast_json_object_set(event,
476                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
477         if (ret) {
478                 return NULL;
479         }
480
481         message = ast_json_pack("{s: o}", "channel_created", ast_json_ref(event));
482         if (!message) {
483                 return NULL;
484         }
485
486         return ast_json_ref(message);
487 }
488
489 struct ast_json *stasis_json_event_stasis_start_create(
490         struct ast_channel_snapshot *channel_snapshot,
491         struct ast_json *blob
492         )
493 {
494         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
495         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
496         struct ast_json *validator;
497         int ret;
498
499         ast_assert(channel_snapshot != NULL);
500         ast_assert(blob != NULL);
501         ast_assert(ast_json_object_get(blob, "channel") == NULL);
502         ast_assert(ast_json_object_get(blob, "type") == NULL);
503
504         validator = ast_json_object_get(blob, "args");
505         if (validator) {
506                 /* do validation? XXX */
507         } else {
508                 /* fail message generation if the required parameter doesn't exist */
509                 return NULL;
510         }
511
512         event = ast_json_deep_copy(blob);
513         if (!event) {
514                 return NULL;
515         }
516
517         ret = ast_json_object_set(event,
518                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
519         if (ret) {
520                 return NULL;
521         }
522
523         message = ast_json_pack("{s: o}", "stasis_start", ast_json_ref(event));
524         if (!message) {
525                 return NULL;
526         }
527
528         return ast_json_ref(message);
529 }
530
531 struct ast_json *stasis_json_event_channel_dialplan_create(
532         struct ast_channel_snapshot *channel_snapshot,
533         struct ast_json *blob
534         )
535 {
536         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
537         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
538         struct ast_json *validator;
539         int ret;
540
541         ast_assert(channel_snapshot != NULL);
542         ast_assert(blob != NULL);
543         ast_assert(ast_json_object_get(blob, "channel") == NULL);
544         ast_assert(ast_json_object_get(blob, "type") == NULL);
545
546         validator = ast_json_object_get(blob, "application");
547         if (validator) {
548                 /* do validation? XXX */
549         } else {
550                 /* fail message generation if the required parameter doesn't exist */
551                 return NULL;
552         }
553
554         validator = ast_json_object_get(blob, "application_data");
555         if (validator) {
556                 /* do validation? XXX */
557         } else {
558                 /* fail message generation if the required parameter doesn't exist */
559                 return NULL;
560         }
561
562         event = ast_json_deep_copy(blob);
563         if (!event) {
564                 return NULL;
565         }
566
567         ret = ast_json_object_set(event,
568                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
569         if (ret) {
570                 return NULL;
571         }
572
573         message = ast_json_pack("{s: o}", "channel_dialplan", ast_json_ref(event));
574         if (!message) {
575                 return NULL;
576         }
577
578         return ast_json_ref(message);
579 }
580
581 struct ast_json *stasis_json_event_channel_state_change_create(
582         struct ast_channel_snapshot *channel_snapshot
583         )
584 {
585         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
586         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
587         int ret;
588
589         ast_assert(channel_snapshot != NULL);
590
591         event = ast_json_object_create();
592         if (!event) {
593                 return NULL;
594         }
595
596         ret = ast_json_object_set(event,
597                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
598         if (ret) {
599                 return NULL;
600         }
601
602         message = ast_json_pack("{s: o}", "channel_state_change", ast_json_ref(event));
603         if (!message) {
604                 return NULL;
605         }
606
607         return ast_json_ref(message);
608 }
609
610 struct ast_json *stasis_json_event_channel_hangup_request_create(
611         struct ast_channel_snapshot *channel_snapshot,
612         struct ast_json *blob
613         )
614 {
615         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
616         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
617         struct ast_json *validator;
618         int ret;
619
620         ast_assert(channel_snapshot != NULL);
621         ast_assert(blob != NULL);
622         ast_assert(ast_json_object_get(blob, "channel") == NULL);
623         ast_assert(ast_json_object_get(blob, "type") == NULL);
624
625         validator = ast_json_object_get(blob, "soft");
626         if (validator) {
627                 /* do validation? XXX */
628         }
629
630         validator = ast_json_object_get(blob, "cause");
631         if (validator) {
632                 /* do validation? XXX */
633         }
634
635         event = ast_json_deep_copy(blob);
636         if (!event) {
637                 return NULL;
638         }
639
640         ret = ast_json_object_set(event,
641                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
642         if (ret) {
643                 return NULL;
644         }
645
646         message = ast_json_pack("{s: o}", "channel_hangup_request", ast_json_ref(event));
647         if (!message) {
648                 return NULL;
649         }
650
651         return ast_json_ref(message);
652 }
653
654 struct ast_json *stasis_json_event_channel_entered_bridge_create(
655         struct ast_bridge_snapshot *bridge_snapshot,
656         struct ast_channel_snapshot *channel_snapshot
657         )
658 {
659         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
660         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
661         int ret;
662
663         ast_assert(channel_snapshot != NULL);
664         ast_assert(bridge_snapshot != NULL);
665
666         event = ast_json_object_create();
667         if (!event) {
668                 return NULL;
669         }
670
671         ret = ast_json_object_set(event,
672                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
673         if (ret) {
674                 return NULL;
675         }
676
677         ret = ast_json_object_set(event,
678                 "bridge", ast_bridge_snapshot_to_json(bridge_snapshot));
679         if (ret) {
680                 return NULL;
681         }
682
683         message = ast_json_pack("{s: o}", "channel_entered_bridge", ast_json_ref(event));
684         if (!message) {
685                 return NULL;
686         }
687
688         return ast_json_ref(message);
689 }
690
691 struct ast_json *stasis_json_event_channel_dtmf_received_create(
692         struct ast_channel_snapshot *channel_snapshot,
693         struct ast_json *blob
694         )
695 {
696         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
697         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
698         struct ast_json *validator;
699         int ret;
700
701         ast_assert(channel_snapshot != NULL);
702         ast_assert(blob != NULL);
703         ast_assert(ast_json_object_get(blob, "channel") == NULL);
704         ast_assert(ast_json_object_get(blob, "type") == NULL);
705
706         validator = ast_json_object_get(blob, "digit");
707         if (validator) {
708                 /* do validation? XXX */
709         } else {
710                 /* fail message generation if the required parameter doesn't exist */
711                 return NULL;
712         }
713
714         event = ast_json_deep_copy(blob);
715         if (!event) {
716                 return NULL;
717         }
718
719         ret = ast_json_object_set(event,
720                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
721         if (ret) {
722                 return NULL;
723         }
724
725         message = ast_json_pack("{s: o}", "channel_dtmf_received", ast_json_ref(event));
726         if (!message) {
727                 return NULL;
728         }
729
730         return ast_json_ref(message);
731 }
732
733 struct ast_json *stasis_json_event_stasis_end_create(
734         struct ast_channel_snapshot *channel_snapshot
735         )
736 {
737         RAII_VAR(struct ast_json *, message, NULL, ast_json_unref);
738         RAII_VAR(struct ast_json *, event, NULL, ast_json_unref);
739         int ret;
740
741         ast_assert(channel_snapshot != NULL);
742
743         event = ast_json_object_create();
744         if (!event) {
745                 return NULL;
746         }
747
748         ret = ast_json_object_set(event,
749                 "channel", ast_channel_snapshot_to_json(channel_snapshot));
750         if (ret) {
751                 return NULL;
752         }
753
754         message = ast_json_pack("{s: o}", "stasis_end", ast_json_ref(event));
755         if (!message) {
756                 return NULL;
757         }
758
759         return ast_json_ref(message);
760 }
761
762 static int load_module(void)
763 {
764         return 0;
765 }
766
767 static int unload_module(void)
768 {
769         return 0;
770 }
771
772 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER,
773         "Stasis JSON Generators and Validators - WebSocket resource",
774         .load = load_module,
775         .unload = unload_module,
776         .load_pri = AST_MODPRI_DEFAULT,
777         );