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 static void ari_channels_handle_play(
358 const char *args_channel_id,
359 const char *args_media,
360 const char *args_lang,
363 const char *args_playback_id,
364 struct ast_ari_response *response)
366 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
367 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
368 RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
369 RAII_VAR(char *, playback_url, NULL, ast_free);
370 struct ast_json *json;
371 const char *language;
373 ast_assert(response != NULL);
375 control = find_control(response, args_channel_id);
376 if (control == NULL) {
377 /* Response filled in by find_control */
381 snapshot = stasis_app_control_get_snapshot(control);
383 ast_ari_response_error(
384 response, 404, "Not Found",
385 "Channel not found");
389 if (args_skipms < 0) {
390 ast_ari_response_error(
391 response, 400, "Bad Request",
392 "skipms cannot be negative");
396 if (args_offsetms < 0) {
397 ast_ari_response_error(
398 response, 400, "Bad Request",
399 "offsetms cannot be negative");
403 language = S_OR(args_lang, snapshot->language);
405 playback = stasis_app_control_play_uri(control, args_media, language,
406 args_channel_id, STASIS_PLAYBACK_TARGET_CHANNEL, args_skipms, args_offsetms, args_playback_id);
408 ast_ari_response_error(
409 response, 500, "Internal Server Error",
410 "Failed to queue media for playback");
414 ast_asprintf(&playback_url, "/playback/%s",
415 stasis_app_playback_get_id(playback));
417 ast_ari_response_error(
418 response, 500, "Internal Server Error",
423 json = stasis_app_playback_to_json(playback);
425 ast_ari_response_error(
426 response, 500, "Internal Server Error",
431 ast_ari_response_created(response, playback_url, json);
434 void ast_ari_channels_play(struct ast_variable *headers,
435 struct ast_ari_channels_play_args *args,
436 struct ast_ari_response *response)
438 ari_channels_handle_play(
448 void ast_ari_channels_play_with_id(struct ast_variable *headers,
449 struct ast_ari_channels_play_with_id_args *args,
450 struct ast_ari_response *response)
452 ari_channels_handle_play(
462 void ast_ari_channels_record(struct ast_variable *headers,
463 struct ast_ari_channels_record_args *args,
464 struct ast_ari_response *response)
466 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
467 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
468 RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
469 RAII_VAR(char *, recording_url, NULL, ast_free);
470 struct ast_json *json;
471 RAII_VAR(struct stasis_app_recording_options *, options, NULL,
473 RAII_VAR(char *, uri_encoded_name, NULL, ast_free);
474 size_t uri_name_maxlen;
476 ast_assert(response != NULL);
478 if (args->max_duration_seconds < 0) {
479 ast_ari_response_error(
480 response, 400, "Bad Request",
481 "max_duration_seconds cannot be negative");
485 if (args->max_silence_seconds < 0) {
486 ast_ari_response_error(
487 response, 400, "Bad Request",
488 "max_silence_seconds cannot be negative");
492 control = find_control(response, args->channel_id);
493 if (control == NULL) {
494 /* Response filled in by find_control */
498 options = stasis_app_recording_options_create(args->name, args->format);
499 if (options == NULL) {
500 ast_ari_response_error(
501 response, 500, "Internal Server Error",
504 ast_string_field_build(options, target, "channel:%s", args->channel_id);
505 options->max_silence_seconds = args->max_silence_seconds;
506 options->max_duration_seconds = args->max_duration_seconds;
507 options->terminate_on =
508 stasis_app_recording_termination_parse(args->terminate_on);
510 stasis_app_recording_if_exists_parse(args->if_exists);
511 options->beep = args->beep;
513 if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) {
514 ast_ari_response_error(
515 response, 400, "Bad Request",
516 "terminateOn invalid");
520 if (options->if_exists == -1) {
521 ast_ari_response_error(
522 response, 400, "Bad Request",
527 if (!ast_get_format_for_file_ext(options->format)) {
528 ast_ari_response_error(
529 response, 422, "Unprocessable Entity",
530 "specified format is unknown on this system");
534 recording = stasis_app_control_record(control, options);
535 if (recording == NULL) {
538 /* While the arguments are invalid, we should have
539 * caught them prior to calling record.
541 ast_ari_response_error(
542 response, 500, "Internal Server Error",
543 "Error parsing request");
546 ast_ari_response_error(response, 409, "Conflict",
547 "Recording '%s' already exists and can not be overwritten",
551 ast_ari_response_error(
552 response, 500, "Internal Server Error",
556 ast_ari_response_error(
557 response, 400, "Bad Request",
558 "Recording name invalid");
562 "Unrecognized recording error: %s\n",
564 ast_ari_response_error(
565 response, 500, "Internal Server Error",
566 "Internal Server Error");
572 uri_name_maxlen = strlen(args->name) * 3;
573 uri_encoded_name = ast_malloc(uri_name_maxlen);
574 if (!uri_encoded_name) {
575 ast_ari_response_error(
576 response, 500, "Internal Server Error",
580 ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen,
583 ast_asprintf(&recording_url, "/recordings/live/%s", uri_encoded_name);
584 if (!recording_url) {
585 ast_ari_response_error(
586 response, 500, "Internal Server Error",
591 json = stasis_app_recording_to_json(recording);
593 ast_ari_response_error(
594 response, 500, "Internal Server Error",
599 ast_ari_response_created(response, recording_url, json);
602 void ast_ari_channels_get(struct ast_variable *headers,
603 struct ast_ari_channels_get_args *args,
604 struct ast_ari_response *response)
606 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
607 struct stasis_cache *cache;
608 struct ast_channel_snapshot *snapshot;
610 cache = ast_channel_cache();
612 ast_ari_response_error(
613 response, 500, "Internal Server Error",
614 "Message bus not initialized");
618 msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
621 ast_ari_response_error(
622 response, 404, "Not Found",
623 "Channel not found");
627 snapshot = stasis_message_data(msg);
628 ast_assert(snapshot != NULL);
630 ast_ari_response_ok(response,
631 ast_channel_snapshot_to_json(snapshot, NULL));
634 void ast_ari_channels_hangup(struct ast_variable *headers,
635 struct ast_ari_channels_hangup_args *args,
636 struct ast_ari_response *response)
638 RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
641 chan = ast_channel_get_by_name(args->channel_id);
643 ast_ari_response_error(
644 response, 404, "Not Found",
645 "Channel not found");
649 if (ast_strlen_zero(args->reason) || !strcmp(args->reason, "normal")) {
650 cause = AST_CAUSE_NORMAL;
651 } else if (!strcmp(args->reason, "busy")) {
652 cause = AST_CAUSE_BUSY;
653 } else if (!strcmp(args->reason, "congestion")) {
654 cause = AST_CAUSE_CONGESTION;
656 ast_ari_response_error(
657 response, 400, "Invalid Reason",
658 "Invalid reason for hangup provided");
662 ast_channel_hangupcause_set(chan, cause);
663 ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
665 ast_ari_response_no_content(response);
668 void ast_ari_channels_list(struct ast_variable *headers,
669 struct ast_ari_channels_list_args *args,
670 struct ast_ari_response *response)
672 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
673 RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
674 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
675 struct ao2_iterator i;
677 struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
679 cache = ast_channel_cache();
681 ast_ari_response_error(
682 response, 500, "Internal Server Error",
683 "Message bus not initialized");
688 snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type());
690 ast_ari_response_alloc_failed(response);
694 json = ast_json_array_create();
696 ast_ari_response_alloc_failed(response);
700 i = ao2_iterator_init(snapshots, 0);
701 while ((obj = ao2_iterator_next(&i))) {
702 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
703 struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
706 if (sanitize && sanitize->channel_snapshot
707 && sanitize->channel_snapshot(snapshot)) {
711 r = ast_json_array_append(
712 json, ast_channel_snapshot_to_json(snapshot, NULL));
714 ast_ari_response_alloc_failed(response);
715 ao2_iterator_destroy(&i);
719 ao2_iterator_destroy(&i);
721 ast_ari_response_ok(response, ast_json_ref(json));
724 static int json_to_ast_variables(struct ast_json *json_variables, struct ast_variable **variables)
726 struct ast_variable *current = NULL;
727 struct ast_json_iter *it_json_var;
729 for (it_json_var = ast_json_object_iter(json_variables); it_json_var;
730 it_json_var = ast_json_object_iter_next(json_variables, it_json_var)) {
731 struct ast_variable *new_var;
733 new_var = ast_variable_new(ast_json_object_iter_key(it_json_var),
734 ast_json_string_get(ast_json_object_iter_value(it_json_var)),
737 ast_variables_destroy(*variables);
743 *variables = new_var;
744 current = *variables;
746 current->next = new_var;
754 static void ari_channels_handle_originate_with_id(const char *args_endpoint,
755 const char *args_extension,
756 const char *args_context,
758 const char *args_app,
759 const char *args_app_args,
760 const char *args_caller_id,
762 struct ast_variable *variables,
763 const char *args_channel_id,
764 const char *args_other_channel_id,
765 struct ast_ari_response *response)
768 char dialdevice[AST_CHANNEL_NAME];
769 char *caller_id = NULL;
770 char *cid_num = NULL;
771 char *cid_name = NULL;
773 RAII_VAR(struct ast_format_cap *, cap,
774 ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_NOLOCK), ast_format_cap_destroy);
775 struct ast_format tmp_fmt;
777 struct ast_channel *chan;
778 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
779 struct ast_assigned_ids assignedids = {args_channel_id, args_other_channel_id};
781 if ((!ast_strlen_zero(assignedids.uniqueid) && strlen(assignedids.uniqueid) >= AST_MAX_UNIQUEID) ||
782 (!ast_strlen_zero(assignedids.uniqueid) && strlen(assignedids.uniqueid2) >= AST_MAX_UNIQUEID)) {
783 ast_log(LOG_WARNING, "Uniqueid length exceeds maximum of %d\n", AST_MAX_UNIQUEID);
787 ast_ari_response_alloc_failed(response);
790 ast_format_cap_add(cap, ast_format_set(&tmp_fmt, AST_FORMAT_SLINEAR, 0));
792 if (ast_strlen_zero(args_endpoint)) {
793 ast_ari_response_error(response, 400, "Bad Request",
794 "Endpoint must be specified");
798 dialtech = ast_strdupa(args_endpoint);
799 if ((stuff = strchr(dialtech, '/'))) {
801 ast_copy_string(dialdevice, stuff, sizeof(dialdevice));
804 if (ast_strlen_zero(dialtech) || ast_strlen_zero(dialdevice)) {
805 ast_ari_response_error(response, 400, "Bad Request",
806 "Invalid endpoint specified");
810 if (args_timeout > 0) {
811 timeout = args_timeout * 1000;
812 } else if (args_timeout == -1) {
816 if (!ast_strlen_zero(args_caller_id)) {
817 caller_id = ast_strdupa(args_caller_id);
818 ast_callerid_parse(caller_id, &cid_name, &cid_num);
820 if (ast_is_shrinkable_phonenumber(cid_num)) {
821 ast_shrink_phone_number(cid_num);
825 if (!ast_strlen_zero(args_app)) {
826 const char *app = "Stasis";
828 RAII_VAR(struct ast_str *, appdata, ast_str_create(64), ast_free);
831 ast_ari_response_alloc_failed(response);
835 ast_str_set(&appdata, 0, "%s", args_app);
836 if (!ast_strlen_zero(args_app_args)) {
837 ast_str_append(&appdata, 0, ",%s", args_app_args);
840 /* originate a channel, putting it into an application */
841 if (ast_pbx_outgoing_app(dialtech, cap, dialdevice, timeout, app, ast_str_buffer(appdata), NULL, 0, cid_num, cid_name, variables, NULL, &chan, &assignedids)) {
842 ast_ari_response_alloc_failed(response);
845 } else if (!ast_strlen_zero(args_extension)) {
846 /* originate a channel, sending it to an extension */
847 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, variables, NULL, &chan, 0, &assignedids)) {
848 ast_ari_response_alloc_failed(response);
852 ast_ari_response_error(response, 400, "Bad Request",
853 "Application or extension must be specified");
857 snapshot = ast_channel_snapshot_create(chan);
858 ast_channel_unlock(chan);
860 if (!ast_strlen_zero(args_app)) {
861 /* channel: + channel ID + null terminator */
862 char uri[9 + strlen(ast_channel_uniqueid(chan))];
863 const char *uris[1] = { uri, };
865 sprintf(uri, "channel:%s", ast_channel_uniqueid(chan));
866 stasis_app_subscribe(args_app, uris, 1, NULL);
869 ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
870 ast_channel_unref(chan);
873 void ast_ari_channels_originate_with_id(struct ast_variable *headers,
874 struct ast_ari_channels_originate_with_id_args *args,
875 struct ast_ari_response *response)
877 RAII_VAR(struct ast_variable *, variables, NULL, ast_variables_destroy);
879 /* Parse any query parameters out of the body parameter */
880 if (args->variables) {
881 struct ast_json *json_variables;
883 ast_ari_channels_originate_with_id_parse_body(args->variables, args);
884 json_variables = ast_json_object_get(args->variables, "variables");
885 if (json_variables) {
886 if (json_to_ast_variables(json_variables, &variables)) {
887 ast_log(AST_LOG_ERROR, "Unable to convert 'variables' in JSON body to channel variables\n");
888 ast_ari_response_alloc_failed(response);
894 ari_channels_handle_originate_with_id(
905 args->other_channel_id,
909 void ast_ari_channels_originate(struct ast_variable *headers,
910 struct ast_ari_channels_originate_args *args,
911 struct ast_ari_response *response)
913 RAII_VAR(struct ast_variable *, variables, NULL, ast_variables_destroy);
915 /* Parse any query parameters out of the body parameter */
916 if (args->variables) {
917 struct ast_json *json_variables;
919 ast_ari_channels_originate_parse_body(args->variables, args);
920 json_variables = ast_json_object_get(args->variables, "variables");
921 if (json_variables) {
922 if (json_to_ast_variables(json_variables, &variables)) {
923 ast_log(AST_LOG_ERROR, "Unable to convert 'variables' in JSON body to channel variables\n");
924 ast_ari_response_alloc_failed(response);
930 ari_channels_handle_originate_with_id(
941 args->other_channel_id,
945 void ast_ari_channels_get_channel_var(struct ast_variable *headers,
946 struct ast_ari_channels_get_channel_var_args *args,
947 struct ast_ari_response *response)
949 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
950 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
951 RAII_VAR(char *, value, NULL, ast_free);
953 ast_assert(response != NULL);
955 if (ast_strlen_zero(args->variable)) {
956 ast_ari_response_error(
957 response, 400, "Bad Request",
958 "Variable name is required");
962 control = find_control(response, args->channel_id);
963 if (control == NULL) {
964 /* response filled in by find_control */
968 value = stasis_app_control_get_channel_var(control, args->variable);
970 if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
971 ast_ari_response_alloc_failed(response);
975 ast_ari_response_ok(response, ast_json_ref(json));
978 void ast_ari_channels_set_channel_var(struct ast_variable *headers,
979 struct ast_ari_channels_set_channel_var_args *args,
980 struct ast_ari_response *response)
982 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
984 ast_assert(response != NULL);
986 if (ast_strlen_zero(args->variable)) {
987 ast_ari_response_error(
988 response, 400, "Bad Request",
989 "Variable name is required");
993 control = find_control(response, args->channel_id);
994 if (control == NULL) {
995 /* response filled in by find_control */
999 if (stasis_app_control_set_channel_var(control, args->variable, args->value)) {
1000 ast_ari_response_error(
1001 response, 400, "Bad Request",
1002 "Failed to execute function");
1006 ast_ari_response_no_content(response);
1009 static void ari_channels_handle_snoop_channel(
1010 const char *args_channel_id,
1011 const char *args_spy,
1012 const char *args_whisper,
1013 const char *args_app,
1014 const char *args_app_args,
1015 const char *args_snoop_id,
1016 struct ast_ari_response *response)
1018 enum stasis_app_snoop_direction spy, whisper;
1019 RAII_VAR(struct ast_channel *, chan, NULL, ast_channel_cleanup);
1020 RAII_VAR(struct ast_channel *, snoop, NULL, ast_channel_cleanup);
1021 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
1023 ast_assert(response != NULL);
1025 if (ast_strlen_zero(args_spy) || !strcmp(args_spy, "none")) {
1026 spy = STASIS_SNOOP_DIRECTION_NONE;
1027 } else if (!strcmp(args_spy, "both")) {
1028 spy = STASIS_SNOOP_DIRECTION_BOTH;
1029 } else if (!strcmp(args_spy, "out")) {
1030 spy = STASIS_SNOOP_DIRECTION_OUT;
1031 } else if (!strcmp(args_spy, "in")) {
1032 spy = STASIS_SNOOP_DIRECTION_IN;
1034 ast_ari_response_error(
1035 response, 400, "Bad Request",
1036 "Invalid direction specified for spy");
1040 if (ast_strlen_zero(args_whisper) || !strcmp(args_whisper, "none")) {
1041 whisper = STASIS_SNOOP_DIRECTION_NONE;
1042 } else if (!strcmp(args_whisper, "both")) {
1043 whisper = STASIS_SNOOP_DIRECTION_BOTH;
1044 } else if (!strcmp(args_whisper, "out")) {
1045 whisper = STASIS_SNOOP_DIRECTION_OUT;
1046 } else if (!strcmp(args_whisper, "in")) {
1047 whisper = STASIS_SNOOP_DIRECTION_IN;
1049 ast_ari_response_error(
1050 response, 400, "Bad Request",
1051 "Invalid direction specified for whisper");
1055 if (spy == STASIS_SNOOP_DIRECTION_NONE && whisper == STASIS_SNOOP_DIRECTION_NONE) {
1056 ast_ari_response_error(
1057 response, 400, "Bad Request",
1058 "Direction must be specified for at least spy or whisper");
1060 } else if (ast_strlen_zero(args_app)) {
1061 ast_ari_response_error(
1062 response, 400, "Bad Request",
1063 "Application name is required");
1067 chan = ast_channel_get_by_name(args_channel_id);
1069 ast_ari_response_error(
1070 response, 404, "Channel Not Found",
1071 "Provided channel was not found");
1075 snoop = stasis_app_control_snoop(chan, spy, whisper, args_app, args_app_args,
1077 if (snoop == NULL) {
1078 ast_ari_response_error(
1079 response, 500, "Internal error",
1080 "Snoop channel could not be created");
1084 snapshot = ast_channel_snapshot_create(snoop);
1085 ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
1088 void ast_ari_channels_snoop_channel(struct ast_variable *headers,
1089 struct ast_ari_channels_snoop_channel_args *args,
1090 struct ast_ari_response *response)
1092 ari_channels_handle_snoop_channel(
1102 void ast_ari_channels_snoop_channel_with_id(struct ast_variable *headers,
1103 struct ast_ari_channels_snoop_channel_with_id_args *args,
1104 struct ast_ari_response *response)
1106 ari_channels_handle_snoop_channel(