2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2012 - 2013, Digium, Inc.
6 * David M. Lee, II <dlee@digium.com>
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.
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.
21 * \brief Implementation for ARI stubs.
23 * \author David M. Lee, II <dlee@digium.com>
27 <depend type="module">res_stasis_app_playback</depend>
28 <support_level>core</support_level>
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include "asterisk/file.h"
36 #include "asterisk/pbx.h"
37 #include "asterisk/bridge.h"
38 #include "asterisk/callerid.h"
39 #include "asterisk/stasis_app.h"
40 #include "asterisk/stasis_app_playback.h"
41 #include "asterisk/stasis_app_recording.h"
42 #include "asterisk/stasis_app_snoop.h"
43 #include "asterisk/stasis_channels.h"
44 #include "asterisk/causes.h"
45 #include "resource_channels.h"
50 * \brief Finds the control object for a channel, filling the response with an
51 * error, if appropriate.
52 * \param[out] response Response to fill with an error if control is not found.
53 * \param channel_id ID of the channel to lookup.
54 * \return Channel control object.
55 * \return \c NULL if control object does not exist.
57 static struct stasis_app_control *find_control(
58 struct ast_ari_response *response,
59 const char *channel_id)
61 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
63 ast_assert(response != NULL);
65 control = stasis_app_control_find_by_channel_id(channel_id);
66 if (control == NULL) {
67 /* Distinguish between 404 and 409 errors */
68 RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
69 chan = ast_channel_get_by_name(channel_id);
71 ast_ari_response_error(response, 404, "Not Found",
76 ast_ari_response_error(response, 409, "Conflict",
77 "Channel not in Stasis application");
85 void ast_ari_channels_continue_in_dialplan(
86 struct ast_variable *headers,
87 struct ast_ari_channels_continue_in_dialplan_args *args,
88 struct ast_ari_response *response)
90 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
92 ast_assert(response != NULL);
94 control = find_control(response, args->channel_id);
95 if (control == NULL) {
99 if (stasis_app_control_continue(control, args->context, args->extension, args->priority)) {
100 ast_ari_response_alloc_failed(response);
104 ast_ari_response_no_content(response);
107 void ast_ari_channels_answer(struct ast_variable *headers,
108 struct ast_ari_channels_answer_args *args,
109 struct ast_ari_response *response)
111 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
113 control = find_control(response, args->channel_id);
114 if (control == NULL) {
118 if (stasis_app_control_answer(control) != 0) {
119 ast_ari_response_error(
120 response, 500, "Internal Server Error",
121 "Failed to answer channel");
125 ast_ari_response_no_content(response);
128 void ast_ari_channels_ring(struct ast_variable *headers,
129 struct ast_ari_channels_ring_args *args,
130 struct ast_ari_response *response)
132 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
134 control = find_control(response, args->channel_id);
135 if (control == NULL) {
139 stasis_app_control_ring(control);
141 ast_ari_response_no_content(response);
144 void ast_ari_channels_ring_stop(struct ast_variable *headers,
145 struct ast_ari_channels_ring_stop_args *args,
146 struct ast_ari_response *response)
148 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
150 control = find_control(response, args->channel_id);
151 if (control == NULL) {
155 stasis_app_control_ring_stop(control);
157 ast_ari_response_no_content(response);
160 void ast_ari_channels_mute(struct ast_variable *headers,
161 struct ast_ari_channels_mute_args *args,
162 struct ast_ari_response *response)
164 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
165 unsigned int direction = 0;
166 enum ast_frame_type frametype = AST_FRAME_VOICE;
168 control = find_control(response, args->channel_id);
169 if (control == NULL) {
173 if (ast_strlen_zero(args->direction)) {
174 ast_ari_response_error(
175 response, 400, "Bad Request",
176 "Direction is required");
180 if (!strcmp(args->direction, "in")) {
181 direction = AST_MUTE_DIRECTION_READ;
182 } else if (!strcmp(args->direction, "out")) {
183 direction = AST_MUTE_DIRECTION_WRITE;
184 } else if (!strcmp(args->direction, "both")) {
185 direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
187 ast_ari_response_error(
188 response, 400, "Bad Request",
189 "Invalid direction specified");
193 stasis_app_control_mute(control, direction, frametype);
195 ast_ari_response_no_content(response);
198 void ast_ari_channels_unmute(struct ast_variable *headers,
199 struct ast_ari_channels_unmute_args *args,
200 struct ast_ari_response *response)
202 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
203 unsigned int direction = 0;
204 enum ast_frame_type frametype = AST_FRAME_VOICE;
206 control = find_control(response, args->channel_id);
207 if (control == NULL) {
211 if (ast_strlen_zero(args->direction)) {
212 ast_ari_response_error(
213 response, 400, "Bad Request",
214 "Direction is required");
218 if (!strcmp(args->direction, "in")) {
219 direction = AST_MUTE_DIRECTION_READ;
220 } else if (!strcmp(args->direction, "out")) {
221 direction = AST_MUTE_DIRECTION_WRITE;
222 } else if (!strcmp(args->direction, "both")) {
223 direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
225 ast_ari_response_error(
226 response, 400, "Bad Request",
227 "Invalid direction specified");
231 stasis_app_control_unmute(control, direction, frametype);
233 ast_ari_response_no_content(response);
236 void ast_ari_channels_send_dtmf(struct ast_variable *headers,
237 struct ast_ari_channels_send_dtmf_args *args,
238 struct ast_ari_response *response)
240 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
242 control = find_control(response, args->channel_id);
243 if (control == NULL) {
247 if (ast_strlen_zero(args->dtmf)) {
248 ast_ari_response_error(
249 response, 400, "Bad Request",
254 stasis_app_control_dtmf(control, args->dtmf, args->before, args->between, args->duration, args->after);
256 ast_ari_response_no_content(response);
259 void ast_ari_channels_hold(struct ast_variable *headers,
260 struct ast_ari_channels_hold_args *args,
261 struct ast_ari_response *response)
263 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
265 control = find_control(response, args->channel_id);
266 if (control == NULL) {
267 /* Response filled in by find_control */
271 stasis_app_control_hold(control);
273 ast_ari_response_no_content(response);
276 void ast_ari_channels_unhold(struct ast_variable *headers,
277 struct ast_ari_channels_unhold_args *args,
278 struct ast_ari_response *response)
280 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
282 control = find_control(response, args->channel_id);
283 if (control == NULL) {
284 /* Response filled in by find_control */
288 stasis_app_control_unhold(control);
290 ast_ari_response_no_content(response);
293 void ast_ari_channels_start_moh(struct ast_variable *headers,
294 struct ast_ari_channels_start_moh_args *args,
295 struct ast_ari_response *response)
297 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
299 control = find_control(response, args->channel_id);
300 if (control == NULL) {
301 /* Response filled in by find_control */
305 stasis_app_control_moh_start(control, args->moh_class);
306 ast_ari_response_no_content(response);
309 void ast_ari_channels_stop_moh(struct ast_variable *headers,
310 struct ast_ari_channels_stop_moh_args *args,
311 struct ast_ari_response *response)
313 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
315 control = find_control(response, args->channel_id);
316 if (control == NULL) {
317 /* Response filled in by find_control */
321 stasis_app_control_moh_stop(control);
322 ast_ari_response_no_content(response);
325 void ast_ari_channels_start_silence(struct ast_variable *headers,
326 struct ast_ari_channels_start_silence_args *args,
327 struct ast_ari_response *response)
329 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
331 control = find_control(response, args->channel_id);
332 if (control == NULL) {
333 /* Response filled in by find_control */
337 stasis_app_control_silence_start(control);
338 ast_ari_response_no_content(response);
341 void ast_ari_channels_stop_silence(struct ast_variable *headers,
342 struct ast_ari_channels_stop_silence_args *args,
343 struct ast_ari_response *response)
345 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
347 control = find_control(response, args->channel_id);
348 if (control == NULL) {
349 /* Response filled in by find_control */
353 stasis_app_control_silence_stop(control);
354 ast_ari_response_no_content(response);
357 void ast_ari_channels_play(struct ast_variable *headers,
358 struct ast_ari_channels_play_args *args,
359 struct ast_ari_response *response)
361 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
362 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
363 RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
364 RAII_VAR(char *, playback_url, NULL, ast_free);
365 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
366 const char *language;
368 ast_assert(response != NULL);
370 control = find_control(response, args->channel_id);
371 if (control == NULL) {
372 /* Response filled in by find_control */
376 snapshot = stasis_app_control_get_snapshot(control);
378 ast_ari_response_error(
379 response, 404, "Not Found",
380 "Channel not found");
384 if (args->skipms < 0) {
385 ast_ari_response_error(
386 response, 400, "Bad Request",
387 "skipms cannot be negative");
391 if (args->offsetms < 0) {
392 ast_ari_response_error(
393 response, 400, "Bad Request",
394 "offsetms cannot be negative");
398 language = S_OR(args->lang, snapshot->language);
400 playback = stasis_app_control_play_uri(control, args->media, language,
401 args->channel_id, STASIS_PLAYBACK_TARGET_CHANNEL, args->skipms, args->offsetms);
403 ast_ari_response_error(
404 response, 500, "Internal Server Error",
405 "Failed to queue media for playback");
409 ast_asprintf(&playback_url, "/playback/%s",
410 stasis_app_playback_get_id(playback));
412 ast_ari_response_error(
413 response, 500, "Internal Server Error",
418 json = stasis_app_playback_to_json(playback);
420 ast_ari_response_error(
421 response, 500, "Internal Server Error",
426 ast_ari_response_created(response, playback_url, json);
429 void ast_ari_channels_record(struct ast_variable *headers,
430 struct ast_ari_channels_record_args *args,
431 struct ast_ari_response *response)
433 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
434 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
435 RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
436 RAII_VAR(char *, recording_url, NULL, ast_free);
437 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
438 RAII_VAR(struct stasis_app_recording_options *, options, NULL,
440 RAII_VAR(char *, uri_encoded_name, NULL, ast_free);
441 size_t uri_name_maxlen;
443 ast_assert(response != NULL);
445 if (args->max_duration_seconds < 0) {
446 ast_ari_response_error(
447 response, 400, "Bad Request",
448 "max_duration_seconds cannot be negative");
452 if (args->max_silence_seconds < 0) {
453 ast_ari_response_error(
454 response, 400, "Bad Request",
455 "max_silence_seconds cannot be negative");
459 control = find_control(response, args->channel_id);
460 if (control == NULL) {
461 /* Response filled in by find_control */
465 options = stasis_app_recording_options_create(args->name, args->format);
466 if (options == NULL) {
467 ast_ari_response_error(
468 response, 500, "Internal Server Error",
471 options->max_silence_seconds = args->max_silence_seconds;
472 options->max_duration_seconds = args->max_duration_seconds;
473 options->terminate_on =
474 stasis_app_recording_termination_parse(args->terminate_on);
476 stasis_app_recording_if_exists_parse(args->if_exists);
477 options->beep = args->beep;
479 if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) {
480 ast_ari_response_error(
481 response, 400, "Bad Request",
482 "terminateOn invalid");
486 if (options->if_exists == -1) {
487 ast_ari_response_error(
488 response, 400, "Bad Request",
493 if (!ast_get_format_for_file_ext(options->format)) {
494 ast_ari_response_error(
495 response, 422, "Unprocessable Entity",
496 "specified format is unknown on this system");
500 recording = stasis_app_control_record(control, options);
501 if (recording == NULL) {
504 /* While the arguments are invalid, we should have
505 * caught them prior to calling record.
507 ast_ari_response_error(
508 response, 500, "Internal Server Error",
509 "Error parsing request");
512 ast_ari_response_error(response, 409, "Conflict",
513 "Recording '%s' already exists and can not be overwritten",
517 ast_ari_response_error(
518 response, 500, "Internal Server Error",
522 ast_ari_response_error(
523 response, 400, "Bad Request",
524 "Recording name invalid");
528 "Unrecognized recording error: %s\n",
530 ast_ari_response_error(
531 response, 500, "Internal Server Error",
532 "Internal Server Error");
538 uri_name_maxlen = strlen(args->name) * 3;
539 uri_encoded_name = ast_malloc(uri_name_maxlen);
540 if (!uri_encoded_name) {
541 ast_ari_response_error(
542 response, 500, "Internal Server Error",
546 ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen,
549 ast_asprintf(&recording_url, "/recordings/live/%s", uri_encoded_name);
550 if (!recording_url) {
551 ast_ari_response_error(
552 response, 500, "Internal Server Error",
557 json = stasis_app_recording_to_json(recording);
559 ast_ari_response_error(
560 response, 500, "Internal Server Error",
565 ast_ari_response_created(response, recording_url, json);
568 void ast_ari_channels_get(struct ast_variable *headers,
569 struct ast_ari_channels_get_args *args,
570 struct ast_ari_response *response)
572 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
573 struct stasis_cache *cache;
574 struct ast_channel_snapshot *snapshot;
576 cache = ast_channel_cache();
578 ast_ari_response_error(
579 response, 500, "Internal Server Error",
580 "Message bus not initialized");
584 msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
587 ast_ari_response_error(
588 response, 404, "Not Found",
589 "Channel not found");
593 snapshot = stasis_message_data(msg);
594 ast_assert(snapshot != NULL);
596 ast_ari_response_ok(response,
597 ast_channel_snapshot_to_json(snapshot, NULL));
600 void ast_ari_channels_hangup(struct ast_variable *headers,
601 struct ast_ari_channels_hangup_args *args,
602 struct ast_ari_response *response)
604 RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
607 chan = ast_channel_get_by_name(args->channel_id);
609 ast_ari_response_error(
610 response, 404, "Not Found",
611 "Channel not found");
615 if (ast_strlen_zero(args->reason) || !strcmp(args->reason, "normal")) {
616 cause = AST_CAUSE_NORMAL;
617 } else if (!strcmp(args->reason, "busy")) {
618 cause = AST_CAUSE_BUSY;
619 } else if (!strcmp(args->reason, "congestion")) {
620 cause = AST_CAUSE_CONGESTION;
622 ast_ari_response_error(
623 response, 400, "Invalid Reason",
624 "Invalid reason for hangup provided");
628 ast_channel_hangupcause_set(chan, cause);
629 ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
631 ast_ari_response_no_content(response);
634 void ast_ari_channels_list(struct ast_variable *headers,
635 struct ast_ari_channels_list_args *args,
636 struct ast_ari_response *response)
638 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
639 RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
640 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
641 struct ao2_iterator i;
643 struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
645 cache = ast_channel_cache();
647 ast_ari_response_error(
648 response, 500, "Internal Server Error",
649 "Message bus not initialized");
654 snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type());
656 ast_ari_response_alloc_failed(response);
660 json = ast_json_array_create();
662 ast_ari_response_alloc_failed(response);
666 for (i = ao2_iterator_init(snapshots, 0);
667 (obj = ao2_iterator_next(&i)); ao2_cleanup(obj)) {
668 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
669 struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
672 if (sanitize && sanitize->channel_snapshot
673 && sanitize->channel_snapshot(snapshot)) {
677 r = ast_json_array_append(
678 json, ast_channel_snapshot_to_json(snapshot, NULL));
680 ast_ari_response_alloc_failed(response);
682 ao2_iterator_destroy(&i);
686 ao2_iterator_destroy(&i);
688 ast_ari_response_ok(response, ast_json_ref(json));
691 static int ari_channels_set_channel_var(struct ast_channel *chan,
692 const char *variable, const char *value, struct ast_ari_response *response)
694 if (pbx_builtin_setvar_helper(chan, variable, value)) {
695 ast_ari_response_error(
696 response, 400, "Bad Request",
697 "Unable to set channel variable %s=%s", variable, value);
704 static int ari_channels_set_channel_vars(struct ast_channel *chan,
705 struct ast_json *variables, struct ast_ari_response *response)
707 struct ast_json_iter *i;
714 for (i = ast_json_object_iter(variables); i;
715 i = ast_json_object_iter_next(variables, i)) {
716 if (ari_channels_set_channel_var(
717 chan, ast_json_object_iter_key(i),
718 ast_json_string_get(ast_json_object_iter_value(i)),
720 /* response filled in by called function */
728 void ast_ari_channels_originate(struct ast_variable *headers,
729 struct ast_ari_channels_originate_args *args,
730 struct ast_ari_response *response)
733 char dialdevice[AST_CHANNEL_NAME];
734 char *caller_id = NULL;
735 char *cid_num = NULL;
736 char *cid_name = NULL;
738 RAII_VAR(struct ast_format_cap *, cap,
739 ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
740 struct ast_format tmp_fmt;
743 struct ast_channel *chan;
744 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
747 ast_ari_response_alloc_failed(response);
750 ast_format_cap_add(cap, ast_format_set(&tmp_fmt, AST_FORMAT_SLINEAR, 0));
752 if (ast_strlen_zero(args->endpoint)) {
753 ast_ari_response_error(response, 400, "Bad Request",
754 "Endpoint must be specified");
758 dialtech = ast_strdupa(args->endpoint);
759 if ((stuff = strchr(dialtech, '/'))) {
761 ast_copy_string(dialdevice, stuff, sizeof(dialdevice));
764 if (ast_strlen_zero(dialtech) || ast_strlen_zero(dialdevice)) {
765 ast_ari_response_error(response, 400, "Bad Request",
766 "Invalid endpoint specified");
770 if (args->timeout > 0) {
771 timeout = args->timeout * 1000;
772 } else if (args->timeout == -1) {
776 if (!ast_strlen_zero(args->caller_id)) {
777 caller_id = ast_strdupa(args->caller_id);
778 ast_callerid_parse(caller_id, &cid_name, &cid_num);
780 if (ast_is_shrinkable_phonenumber(cid_num)) {
781 ast_shrink_phone_number(cid_num);
785 if (!ast_strlen_zero(args->app)) {
786 const char *app = "Stasis";
788 RAII_VAR(struct ast_str *, appdata, ast_str_create(64), ast_free);
791 ast_ari_response_alloc_failed(response);
795 ast_str_set(&appdata, 0, "%s", args->app);
796 if (!ast_strlen_zero(args->app_args)) {
797 ast_str_append(&appdata, 0, ",%s", args->app_args);
800 /* originate a channel, putting it into an application */
801 if (ast_pbx_outgoing_app(dialtech, cap, dialdevice, timeout, app, ast_str_buffer(appdata), NULL, 0, cid_num, cid_name, NULL, NULL, &chan)) {
802 ast_ari_response_alloc_failed(response);
805 } else if (!ast_strlen_zero(args->extension)) {
806 /* originate a channel, sending it to an extension */
807 if (ast_pbx_outgoing_exten(dialtech, cap, dialdevice, timeout, S_OR(args->context, "default"), args->extension, args->priority ? args->priority : 1, NULL, 0, cid_num, cid_name, NULL, NULL, &chan, 0)) {
808 ast_ari_response_alloc_failed(response);
812 ast_ari_response_error(response, 400, "Bad Request",
813 "Application or extension must be specified");
817 if (ari_channels_set_channel_vars(chan, args->variables, response)) {
818 /* response filled in by called function */
822 snapshot = ast_channel_snapshot_create(chan);
823 ast_channel_unlock(chan);
825 if (!ast_strlen_zero(args->app)) {
826 /* channel: + channel ID + null terminator */
827 char uri[9 + strlen(ast_channel_uniqueid(chan))];
828 const char *uris[1] = { uri, };
830 sprintf(uri, "channel:%s", ast_channel_uniqueid(chan));
831 stasis_app_subscribe(args->app, uris, 1, NULL);
834 ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
835 ast_channel_unref(chan);
838 void ast_ari_channels_get_channel_var(struct ast_variable *headers,
839 struct ast_ari_channels_get_channel_var_args *args,
840 struct ast_ari_response *response)
842 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
843 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
844 RAII_VAR(char *, value, NULL, ast_free);
846 ast_assert(response != NULL);
848 if (ast_strlen_zero(args->variable)) {
849 ast_ari_response_error(
850 response, 400, "Bad Request",
851 "Variable name is required");
855 control = find_control(response, args->channel_id);
856 if (control == NULL) {
857 /* response filled in by find_control */
861 value = stasis_app_control_get_channel_var(control, args->variable);
863 if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
864 ast_ari_response_alloc_failed(response);
868 ast_ari_response_ok(response, ast_json_ref(json));
871 void ast_ari_channels_set_channel_var(struct ast_variable *headers,
872 struct ast_ari_channels_set_channel_var_args *args,
873 struct ast_ari_response *response)
875 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
877 ast_assert(response != NULL);
879 if (ast_strlen_zero(args->variable)) {
880 ast_ari_response_error(
881 response, 400, "Bad Request",
882 "Variable name is required");
886 control = find_control(response, args->channel_id);
887 if (control == NULL) {
888 /* response filled in by find_control */
892 if (stasis_app_control_set_channel_var(control, args->variable, args->value)) {
893 ast_ari_response_error(
894 response, 400, "Bad Request",
895 "Failed to execute function");
899 ast_ari_response_no_content(response);
902 void ast_ari_channels_snoop_channel(struct ast_variable *headers, struct ast_ari_channels_snoop_channel_args *args, struct ast_ari_response *response)
904 enum stasis_app_snoop_direction spy, whisper;
905 RAII_VAR(struct ast_channel *, chan, NULL, ast_channel_cleanup);
906 RAII_VAR(struct ast_channel *, snoop, NULL, ast_channel_cleanup);
907 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
909 ast_assert(response != NULL);
911 if (ast_strlen_zero(args->spy) || !strcmp(args->spy, "none")) {
912 spy = STASIS_SNOOP_DIRECTION_NONE;
913 } else if (!strcmp(args->spy, "both")) {
914 spy = STASIS_SNOOP_DIRECTION_BOTH;
915 } else if (!strcmp(args->spy, "out")) {
916 spy = STASIS_SNOOP_DIRECTION_OUT;
917 } else if (!strcmp(args->spy, "in")) {
918 spy = STASIS_SNOOP_DIRECTION_IN;
920 ast_ari_response_error(
921 response, 400, "Bad Request",
922 "Invalid direction specified for spy");
926 if (ast_strlen_zero(args->whisper) || !strcmp(args->whisper, "none")) {
927 whisper = STASIS_SNOOP_DIRECTION_NONE;
928 } else if (!strcmp(args->whisper, "both")) {
929 whisper = STASIS_SNOOP_DIRECTION_BOTH;
930 } else if (!strcmp(args->whisper, "out")) {
931 whisper = STASIS_SNOOP_DIRECTION_OUT;
932 } else if (!strcmp(args->whisper, "in")) {
933 whisper = STASIS_SNOOP_DIRECTION_IN;
935 ast_ari_response_error(
936 response, 400, "Bad Request",
937 "Invalid direction specified for whisper");
941 if (spy == STASIS_SNOOP_DIRECTION_NONE && whisper == STASIS_SNOOP_DIRECTION_NONE) {
942 ast_ari_response_error(
943 response, 400, "Bad Request",
944 "Direction must be specified for at least spy or whisper");
946 } else if (ast_strlen_zero(args->app)) {
947 ast_ari_response_error(
948 response, 400, "Bad Request",
949 "Application name is required");
953 chan = ast_channel_get_by_name(args->channel_id);
955 ast_ari_response_error(
956 response, 404, "Channel Not Found",
957 "Provided channel was not found");
961 snoop = stasis_app_control_snoop(chan, spy, whisper, args->app, args->app_args);
963 ast_ari_response_error(
964 response, 500, "Internal error",
965 "Snoop channel could not be created");
969 snapshot = ast_channel_snapshot_create(snoop);
970 ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));