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_channels.h"
43 #include "asterisk/causes.h"
44 #include "resource_channels.h"
49 * \brief Finds the control object for a channel, filling the response with an
50 * error, if appropriate.
51 * \param[out] response Response to fill with an error if control is not found.
52 * \param channel_id ID of the channel to lookup.
53 * \return Channel control object.
54 * \return \c NULL if control object does not exist.
56 static struct stasis_app_control *find_control(
57 struct ast_ari_response *response,
58 const char *channel_id)
60 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
62 ast_assert(response != NULL);
64 control = stasis_app_control_find_by_channel_id(channel_id);
65 if (control == NULL) {
66 /* Distinguish between 404 and 409 errors */
67 RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
68 chan = ast_channel_get_by_name(channel_id);
70 ast_ari_response_error(response, 404, "Not Found",
75 ast_ari_response_error(response, 409, "Conflict",
76 "Channel not in Stasis application");
84 void ast_ari_channels_continue_in_dialplan(
85 struct ast_variable *headers,
86 struct ast_ari_channels_continue_in_dialplan_args *args,
87 struct ast_ari_response *response)
89 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
91 ast_assert(response != NULL);
93 control = find_control(response, args->channel_id);
94 if (control == NULL) {
98 if (stasis_app_control_continue(control, args->context, args->extension, args->priority)) {
99 ast_ari_response_alloc_failed(response);
103 ast_ari_response_no_content(response);
106 void ast_ari_channels_answer(struct ast_variable *headers,
107 struct ast_ari_channels_answer_args *args,
108 struct ast_ari_response *response)
110 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
112 control = find_control(response, args->channel_id);
113 if (control == NULL) {
117 if (stasis_app_control_answer(control) != 0) {
118 ast_ari_response_error(
119 response, 500, "Internal Server Error",
120 "Failed to answer channel");
124 ast_ari_response_no_content(response);
127 void ast_ari_channels_ring(struct ast_variable *headers,
128 struct ast_ari_channels_ring_args *args,
129 struct ast_ari_response *response)
131 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
133 control = find_control(response, args->channel_id);
134 if (control == NULL) {
138 stasis_app_control_ring(control);
140 ast_ari_response_no_content(response);
143 void ast_ari_channels_ring_stop(struct ast_variable *headers,
144 struct ast_ari_channels_ring_stop_args *args,
145 struct ast_ari_response *response)
147 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
149 control = find_control(response, args->channel_id);
150 if (control == NULL) {
154 stasis_app_control_ring_stop(control);
156 ast_ari_response_no_content(response);
159 void ast_ari_channels_mute(struct ast_variable *headers,
160 struct ast_ari_channels_mute_args *args,
161 struct ast_ari_response *response)
163 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
164 unsigned int direction = 0;
165 enum ast_frame_type frametype = AST_FRAME_VOICE;
167 control = find_control(response, args->channel_id);
168 if (control == NULL) {
172 if (ast_strlen_zero(args->direction)) {
173 ast_ari_response_error(
174 response, 400, "Bad Request",
175 "Direction is required");
179 if (!strcmp(args->direction, "in")) {
180 direction = AST_MUTE_DIRECTION_READ;
181 } else if (!strcmp(args->direction, "out")) {
182 direction = AST_MUTE_DIRECTION_WRITE;
183 } else if (!strcmp(args->direction, "both")) {
184 direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
186 ast_ari_response_error(
187 response, 400, "Bad Request",
188 "Invalid direction specified");
192 stasis_app_control_mute(control, direction, frametype);
194 ast_ari_response_no_content(response);
197 void ast_ari_channels_unmute(struct ast_variable *headers,
198 struct ast_ari_channels_unmute_args *args,
199 struct ast_ari_response *response)
201 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
202 unsigned int direction = 0;
203 enum ast_frame_type frametype = AST_FRAME_VOICE;
205 control = find_control(response, args->channel_id);
206 if (control == NULL) {
210 if (ast_strlen_zero(args->direction)) {
211 ast_ari_response_error(
212 response, 400, "Bad Request",
213 "Direction is required");
217 if (!strcmp(args->direction, "in")) {
218 direction = AST_MUTE_DIRECTION_READ;
219 } else if (!strcmp(args->direction, "out")) {
220 direction = AST_MUTE_DIRECTION_WRITE;
221 } else if (!strcmp(args->direction, "both")) {
222 direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
224 ast_ari_response_error(
225 response, 400, "Bad Request",
226 "Invalid direction specified");
230 stasis_app_control_unmute(control, direction, frametype);
232 ast_ari_response_no_content(response);
235 void ast_ari_channels_send_dtmf(struct ast_variable *headers,
236 struct ast_ari_channels_send_dtmf_args *args,
237 struct ast_ari_response *response)
239 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
241 control = find_control(response, args->channel_id);
242 if (control == NULL) {
246 if (ast_strlen_zero(args->dtmf)) {
247 ast_ari_response_error(
248 response, 400, "Bad Request",
253 stasis_app_control_dtmf(control, args->dtmf, args->before, args->between, args->duration, args->after);
255 ast_ari_response_no_content(response);
258 void ast_ari_channels_hold(struct ast_variable *headers,
259 struct ast_ari_channels_hold_args *args,
260 struct ast_ari_response *response)
262 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
264 control = find_control(response, args->channel_id);
265 if (control == NULL) {
266 /* Response filled in by find_control */
270 stasis_app_control_hold(control);
272 ast_ari_response_no_content(response);
275 void ast_ari_channels_unhold(struct ast_variable *headers,
276 struct ast_ari_channels_unhold_args *args,
277 struct ast_ari_response *response)
279 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
281 control = find_control(response, args->channel_id);
282 if (control == NULL) {
283 /* Response filled in by find_control */
287 stasis_app_control_unhold(control);
289 ast_ari_response_no_content(response);
292 void ast_ari_channels_start_moh(struct ast_variable *headers,
293 struct ast_ari_channels_start_moh_args *args,
294 struct ast_ari_response *response)
296 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
298 control = find_control(response, args->channel_id);
299 if (control == NULL) {
300 /* Response filled in by find_control */
304 stasis_app_control_moh_start(control, args->moh_class);
305 ast_ari_response_no_content(response);
308 void ast_ari_channels_stop_moh(struct ast_variable *headers,
309 struct ast_ari_channels_stop_moh_args *args,
310 struct ast_ari_response *response)
312 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
314 control = find_control(response, args->channel_id);
315 if (control == NULL) {
316 /* Response filled in by find_control */
320 stasis_app_control_moh_stop(control);
321 ast_ari_response_no_content(response);
324 void ast_ari_channels_start_silence(struct ast_variable *headers,
325 struct ast_ari_channels_start_silence_args *args,
326 struct ast_ari_response *response)
328 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
330 control = find_control(response, args->channel_id);
331 if (control == NULL) {
332 /* Response filled in by find_control */
336 stasis_app_control_silence_start(control);
337 ast_ari_response_no_content(response);
340 void ast_ari_channels_stop_silence(struct ast_variable *headers,
341 struct ast_ari_channels_stop_silence_args *args,
342 struct ast_ari_response *response)
344 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
346 control = find_control(response, args->channel_id);
347 if (control == NULL) {
348 /* Response filled in by find_control */
352 stasis_app_control_silence_stop(control);
353 ast_ari_response_no_content(response);
356 void ast_ari_channels_play(struct ast_variable *headers,
357 struct ast_ari_channels_play_args *args,
358 struct ast_ari_response *response)
360 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
361 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
362 RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
363 RAII_VAR(char *, playback_url, NULL, ast_free);
364 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
365 const char *language;
367 ast_assert(response != NULL);
369 control = find_control(response, args->channel_id);
370 if (control == NULL) {
371 /* Response filled in by find_control */
375 snapshot = stasis_app_control_get_snapshot(control);
377 ast_ari_response_error(
378 response, 404, "Not Found",
379 "Channel not found");
383 if (args->skipms < 0) {
384 ast_ari_response_error(
385 response, 400, "Bad Request",
386 "skipms cannot be negative");
390 if (args->offsetms < 0) {
391 ast_ari_response_error(
392 response, 400, "Bad Request",
393 "offsetms cannot be negative");
397 language = S_OR(args->lang, snapshot->language);
399 playback = stasis_app_control_play_uri(control, args->media, language,
400 args->channel_id, STASIS_PLAYBACK_TARGET_CHANNEL, args->skipms, args->offsetms);
402 ast_ari_response_error(
403 response, 500, "Internal Server Error",
404 "Failed to queue media for playback");
408 ast_asprintf(&playback_url, "/playback/%s",
409 stasis_app_playback_get_id(playback));
411 ast_ari_response_error(
412 response, 500, "Internal Server Error",
417 json = stasis_app_playback_to_json(playback);
419 ast_ari_response_error(
420 response, 500, "Internal Server Error",
425 ast_ari_response_created(response, playback_url, json);
428 void ast_ari_channels_record(struct ast_variable *headers,
429 struct ast_ari_channels_record_args *args,
430 struct ast_ari_response *response)
432 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
433 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
434 RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
435 RAII_VAR(char *, recording_url, NULL, ast_free);
436 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
437 RAII_VAR(struct stasis_app_recording_options *, options, NULL,
439 RAII_VAR(char *, uri_encoded_name, NULL, ast_free);
440 size_t uri_name_maxlen;
442 ast_assert(response != NULL);
444 if (args->max_duration_seconds < 0) {
445 ast_ari_response_error(
446 response, 400, "Bad Request",
447 "max_duration_seconds cannot be negative");
451 if (args->max_silence_seconds < 0) {
452 ast_ari_response_error(
453 response, 400, "Bad Request",
454 "max_silence_seconds cannot be negative");
458 control = find_control(response, args->channel_id);
459 if (control == NULL) {
460 /* Response filled in by find_control */
464 options = stasis_app_recording_options_create(args->name, args->format);
465 if (options == NULL) {
466 ast_ari_response_error(
467 response, 500, "Internal Server Error",
470 options->max_silence_seconds = args->max_silence_seconds;
471 options->max_duration_seconds = args->max_duration_seconds;
472 options->terminate_on =
473 stasis_app_recording_termination_parse(args->terminate_on);
475 stasis_app_recording_if_exists_parse(args->if_exists);
476 options->beep = args->beep;
478 if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) {
479 ast_ari_response_error(
480 response, 400, "Bad Request",
481 "terminateOn invalid");
485 if (options->if_exists == -1) {
486 ast_ari_response_error(
487 response, 400, "Bad Request",
492 if (!ast_get_format_for_file_ext(options->format)) {
493 ast_ari_response_error(
494 response, 422, "Unprocessable Entity",
495 "specified format is unknown on this system");
499 recording = stasis_app_control_record(control, options);
500 if (recording == NULL) {
503 /* While the arguments are invalid, we should have
504 * caught them prior to calling record.
506 ast_ari_response_error(
507 response, 500, "Internal Server Error",
508 "Error parsing request");
511 ast_ari_response_error(response, 409, "Conflict",
512 "Recording '%s' already exists and can not be overwritten",
516 ast_ari_response_error(
517 response, 500, "Internal Server Error",
521 ast_ari_response_error(
522 response, 400, "Bad Request",
523 "Recording name invalid");
527 "Unrecognized recording error: %s\n",
529 ast_ari_response_error(
530 response, 500, "Internal Server Error",
531 "Internal Server Error");
537 uri_name_maxlen = strlen(args->name) * 3;
538 uri_encoded_name = ast_malloc(uri_name_maxlen);
539 if (!uri_encoded_name) {
540 ast_ari_response_error(
541 response, 500, "Internal Server Error",
545 ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen,
548 ast_asprintf(&recording_url, "/recordings/live/%s", uri_encoded_name);
549 if (!recording_url) {
550 ast_ari_response_error(
551 response, 500, "Internal Server Error",
556 json = stasis_app_recording_to_json(recording);
558 ast_ari_response_error(
559 response, 500, "Internal Server Error",
564 ast_ari_response_created(response, recording_url, json);
567 void ast_ari_channels_get(struct ast_variable *headers,
568 struct ast_ari_channels_get_args *args,
569 struct ast_ari_response *response)
571 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
572 struct stasis_cache *cache;
573 struct ast_channel_snapshot *snapshot;
575 cache = ast_channel_cache();
577 ast_ari_response_error(
578 response, 500, "Internal Server Error",
579 "Message bus not initialized");
583 msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
586 ast_ari_response_error(
587 response, 404, "Not Found",
588 "Channel not found");
592 snapshot = stasis_message_data(msg);
593 ast_assert(snapshot != NULL);
595 ast_ari_response_ok(response,
596 ast_channel_snapshot_to_json(snapshot, NULL));
599 void ast_ari_channels_hangup(struct ast_variable *headers,
600 struct ast_ari_channels_hangup_args *args,
601 struct ast_ari_response *response)
603 RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
606 chan = ast_channel_get_by_name(args->channel_id);
608 ast_ari_response_error(
609 response, 404, "Not Found",
610 "Channel not found");
614 if (ast_strlen_zero(args->reason) || !strcmp(args->reason, "normal")) {
615 cause = AST_CAUSE_NORMAL;
616 } else if (!strcmp(args->reason, "busy")) {
617 cause = AST_CAUSE_BUSY;
618 } else if (!strcmp(args->reason, "congestion")) {
619 cause = AST_CAUSE_CONGESTION;
621 ast_ari_response_error(
622 response, 400, "Invalid Reason",
623 "Invalid reason for hangup provided");
627 ast_channel_hangupcause_set(chan, cause);
628 ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
630 ast_ari_response_no_content(response);
633 void ast_ari_channels_list(struct ast_variable *headers,
634 struct ast_ari_channels_list_args *args,
635 struct ast_ari_response *response)
637 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
638 RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
639 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
640 struct ao2_iterator i;
642 struct stasis_message_sanitizer *sanitize = stasis_app_get_sanitizer();
644 cache = ast_channel_cache();
646 ast_ari_response_error(
647 response, 500, "Internal Server Error",
648 "Message bus not initialized");
653 snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type());
655 ast_ari_response_alloc_failed(response);
659 json = ast_json_array_create();
661 ast_ari_response_alloc_failed(response);
665 for (i = ao2_iterator_init(snapshots, 0);
666 (obj = ao2_iterator_next(&i)); ao2_cleanup(obj)) {
667 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
668 struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
671 if (sanitize && sanitize->channel_snapshot
672 && sanitize->channel_snapshot(snapshot)) {
676 r = ast_json_array_append(
677 json, ast_channel_snapshot_to_json(snapshot, NULL));
679 ast_ari_response_alloc_failed(response);
681 ao2_iterator_destroy(&i);
685 ao2_iterator_destroy(&i);
687 ast_ari_response_ok(response, ast_json_ref(json));
690 void ast_ari_channels_originate(struct ast_variable *headers,
691 struct ast_ari_channels_originate_args *args,
692 struct ast_ari_response *response)
695 char dialdevice[AST_CHANNEL_NAME];
696 char *caller_id = NULL;
697 char *cid_num = NULL;
698 char *cid_name = NULL;
702 struct ast_channel *chan;
703 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
705 if (ast_strlen_zero(args->endpoint)) {
706 ast_ari_response_error(response, 400, "Bad Request",
707 "Endpoint must be specified");
711 dialtech = ast_strdupa(args->endpoint);
712 if ((stuff = strchr(dialtech, '/'))) {
714 ast_copy_string(dialdevice, stuff, sizeof(dialdevice));
717 if (ast_strlen_zero(dialtech) || ast_strlen_zero(dialdevice)) {
718 ast_ari_response_error(response, 400, "Bad Request",
719 "Invalid endpoint specified");
723 if (args->timeout > 0) {
724 timeout = args->timeout * 1000;
725 } else if (args->timeout == -1) {
729 if (!ast_strlen_zero(args->caller_id)) {
730 caller_id = ast_strdupa(args->caller_id);
731 ast_callerid_parse(caller_id, &cid_name, &cid_num);
733 if (ast_is_shrinkable_phonenumber(cid_num)) {
734 ast_shrink_phone_number(cid_num);
738 if (!ast_strlen_zero(args->app)) {
739 const char *app = "Stasis";
741 RAII_VAR(struct ast_str *, appdata, ast_str_create(64), ast_free);
744 ast_ari_response_alloc_failed(response);
748 ast_str_set(&appdata, 0, "%s", args->app);
749 if (!ast_strlen_zero(args->app_args)) {
750 ast_str_append(&appdata, 0, ",%s", args->app_args);
753 /* originate a channel, putting it into an application */
754 if (ast_pbx_outgoing_app(dialtech, NULL, dialdevice, timeout, app, ast_str_buffer(appdata), NULL, 0, cid_num, cid_name, NULL, NULL, &chan)) {
755 ast_ari_response_alloc_failed(response);
758 } else if (!ast_strlen_zero(args->extension)) {
759 /* originate a channel, sending it to an extension */
760 if (ast_pbx_outgoing_exten(dialtech, NULL, dialdevice, timeout, S_OR(args->context, "default"), args->extension, args->priority ? args->priority : 1, NULL, 0, cid_num, cid_name, NULL, NULL, &chan, 0)) {
761 ast_ari_response_alloc_failed(response);
765 ast_ari_response_error(response, 400, "Bad Request",
766 "Application or extension must be specified");
770 snapshot = ast_channel_snapshot_create(chan);
771 ast_channel_unlock(chan);
773 if (!ast_strlen_zero(args->app)) {
774 /* channel: + channel ID + null terminator */
775 char uri[9 + strlen(ast_channel_uniqueid(chan))];
776 const char *uris[1] = { uri, };
778 sprintf(uri, "channel:%s", ast_channel_uniqueid(chan));
779 stasis_app_subscribe(args->app, uris, 1, NULL);
782 ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot, NULL));
783 ast_channel_unref(chan);
786 void ast_ari_channels_get_channel_var(struct ast_variable *headers,
787 struct ast_ari_channels_get_channel_var_args *args,
788 struct ast_ari_response *response)
790 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
791 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
792 RAII_VAR(char *, value, NULL, ast_free);
794 ast_assert(response != NULL);
796 if (ast_strlen_zero(args->variable)) {
797 ast_ari_response_error(
798 response, 400, "Bad Request",
799 "Variable name is required");
803 control = find_control(response, args->channel_id);
804 if (control == NULL) {
805 /* response filled in by find_control */
809 value = stasis_app_control_get_channel_var(control, args->variable);
811 if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
812 ast_ari_response_alloc_failed(response);
816 ast_ari_response_ok(response, ast_json_ref(json));
819 void ast_ari_channels_set_channel_var(struct ast_variable *headers,
820 struct ast_ari_channels_set_channel_var_args *args,
821 struct ast_ari_response *response)
823 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
825 ast_assert(response != NULL);
827 if (ast_strlen_zero(args->variable)) {
828 ast_ari_response_error(
829 response, 400, "Bad Request",
830 "Variable name is required");
834 control = find_control(response, args->channel_id);
835 if (control == NULL) {
836 /* response filled in by find_control */
840 if (stasis_app_control_set_channel_var(control, args->variable, args->value)) {
841 ast_ari_response_error(
842 response, 400, "Bad Request",
843 "Failed to execute function");
847 ast_ari_response_no_content(response);