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/dial.h"
38 #include "asterisk/bridge.h"
39 #include "asterisk/callerid.h"
40 #include "asterisk/stasis_app.h"
41 #include "asterisk/stasis_app_playback.h"
42 #include "asterisk/stasis_app_recording.h"
43 #include "asterisk/stasis_channels.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_dial(struct ast_variable *headers, struct ast_dial_args *args, struct ast_ari_response *response)
86 struct stasis_app_control *control;
88 control = find_control(response, args->channel_id);
89 if (control == NULL) {
93 if (stasis_app_control_dial(control, args->endpoint, args->extension, args->context, args->timeout)) {
94 ast_ari_response_alloc_failed(response);
98 ast_ari_response_no_content(response);
101 void ast_ari_continue_in_dialplan(
102 struct ast_variable *headers,
103 struct ast_continue_in_dialplan_args *args,
104 struct ast_ari_response *response)
106 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
108 ast_assert(response != NULL);
110 control = find_control(response, args->channel_id);
111 if (control == NULL) {
115 if (stasis_app_control_continue(control, args->context, args->extension, args->priority)) {
116 ast_ari_response_alloc_failed(response);
120 ast_ari_response_no_content(response);
123 void ast_ari_answer_channel(struct ast_variable *headers,
124 struct ast_answer_channel_args *args,
125 struct ast_ari_response *response)
127 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
129 control = find_control(response, args->channel_id);
130 if (control == NULL) {
134 if (stasis_app_control_answer(control) != 0) {
135 ast_ari_response_error(
136 response, 500, "Internal Server Error",
137 "Failed to answer channel");
141 ast_ari_response_no_content(response);
144 void ast_ari_mute_channel(struct ast_variable *headers, struct ast_mute_channel_args *args, struct ast_ari_response *response)
146 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
147 unsigned int direction = 0;
148 enum ast_frame_type frametype = AST_FRAME_VOICE;
150 control = find_control(response, args->channel_id);
151 if (control == NULL) {
155 if (ast_strlen_zero(args->direction)) {
156 ast_ari_response_error(
157 response, 400, "Bad Request",
158 "Direction is required");
162 if (!strcmp(args->direction, "in")) {
163 direction = AST_MUTE_DIRECTION_READ;
164 } else if (!strcmp(args->direction, "out")) {
165 direction = AST_MUTE_DIRECTION_WRITE;
166 } else if (!strcmp(args->direction, "both")) {
167 direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
169 ast_ari_response_error(
170 response, 400, "Bad Request",
171 "Invalid direction specified");
175 stasis_app_control_mute(control, direction, frametype);
177 ast_ari_response_no_content(response);
180 void ast_ari_unmute_channel(struct ast_variable *headers, struct ast_unmute_channel_args *args, struct ast_ari_response *response)
182 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
183 unsigned int direction = 0;
184 enum ast_frame_type frametype = AST_FRAME_VOICE;
186 control = find_control(response, args->channel_id);
187 if (control == NULL) {
191 if (ast_strlen_zero(args->direction)) {
192 ast_ari_response_error(
193 response, 400, "Bad Request",
194 "Direction is required");
198 if (!strcmp(args->direction, "in")) {
199 direction = AST_MUTE_DIRECTION_READ;
200 } else if (!strcmp(args->direction, "out")) {
201 direction = AST_MUTE_DIRECTION_WRITE;
202 } else if (!strcmp(args->direction, "both")) {
203 direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
205 ast_ari_response_error(
206 response, 400, "Bad Request",
207 "Invalid direction specified");
211 stasis_app_control_unmute(control, direction, frametype);
213 ast_ari_response_no_content(response);
216 void ast_ari_hold_channel(struct ast_variable *headers, struct ast_hold_channel_args *args, struct ast_ari_response *response)
218 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
220 control = find_control(response, args->channel_id);
221 if (control == NULL) {
222 /* Response filled in by find_control */
226 stasis_app_control_hold(control);
228 ast_ari_response_no_content(response);
231 void ast_ari_unhold_channel(struct ast_variable *headers, struct ast_unhold_channel_args *args, struct ast_ari_response *response)
233 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
235 control = find_control(response, args->channel_id);
236 if (control == NULL) {
237 /* Response filled in by find_control */
241 stasis_app_control_unhold(control);
243 ast_ari_response_no_content(response);
246 void ast_ari_moh_start_channel(struct ast_variable *headers, struct ast_moh_start_channel_args *args, struct ast_ari_response *response)
248 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
250 control = find_control(response, args->channel_id);
251 if (control == NULL) {
252 /* Response filled in by find_control */
256 stasis_app_control_moh_start(control, args->moh_class);
257 ast_ari_response_no_content(response);
260 void ast_ari_moh_stop_channel(struct ast_variable *headers, struct ast_moh_stop_channel_args *args, 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_moh_stop(control);
271 ast_ari_response_no_content(response);
274 void ast_ari_play_on_channel(struct ast_variable *headers,
275 struct ast_play_on_channel_args *args,
276 struct ast_ari_response *response)
278 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
279 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
280 RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
281 RAII_VAR(char *, playback_url, NULL, ast_free);
282 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
283 const char *language;
285 ast_assert(response != NULL);
287 control = find_control(response, args->channel_id);
288 if (control == NULL) {
289 /* Response filled in by find_control */
293 snapshot = stasis_app_control_get_snapshot(control);
295 ast_ari_response_error(
296 response, 404, "Not Found",
297 "Channel not found");
301 if (args->skipms < 0) {
302 ast_ari_response_error(
303 response, 400, "Bad Request",
304 "skipms cannot be negative");
308 if (args->offsetms < 0) {
309 ast_ari_response_error(
310 response, 400, "Bad Request",
311 "offsetms cannot be negative");
315 language = S_OR(args->lang, snapshot->language);
317 playback = stasis_app_control_play_uri(control, args->media, language,
318 args->channel_id, STASIS_PLAYBACK_TARGET_CHANNEL, args->skipms, args->offsetms);
320 ast_ari_response_error(
321 response, 500, "Internal Server Error",
322 "Failed to queue media for playback");
326 ast_asprintf(&playback_url, "/playback/%s",
327 stasis_app_playback_get_id(playback));
329 ast_ari_response_error(
330 response, 500, "Internal Server Error",
335 json = stasis_app_playback_to_json(playback);
337 ast_ari_response_error(
338 response, 500, "Internal Server Error",
343 ast_ari_response_created(response, playback_url, json);
346 void ast_ari_record_channel(struct ast_variable *headers,
347 struct ast_record_channel_args *args,
348 struct ast_ari_response *response)
350 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
351 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
352 RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
353 RAII_VAR(char *, recording_url, NULL, ast_free);
354 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
355 RAII_VAR(struct stasis_app_recording_options *, options, NULL,
357 RAII_VAR(char *, uri_encoded_name, NULL, ast_free);
358 size_t uri_name_maxlen;
360 ast_assert(response != NULL);
362 if (args->max_duration_seconds < 0) {
363 ast_ari_response_error(
364 response, 400, "Bad Request",
365 "max_duration_seconds cannot be negative");
369 if (args->max_silence_seconds < 0) {
370 ast_ari_response_error(
371 response, 400, "Bad Request",
372 "max_silence_seconds cannot be negative");
376 control = find_control(response, args->channel_id);
377 if (control == NULL) {
378 /* Response filled in by find_control */
382 options = stasis_app_recording_options_create(args->name, args->format);
383 if (options == NULL) {
384 ast_ari_response_error(
385 response, 500, "Internal Server Error",
388 options->max_silence_seconds = args->max_silence_seconds;
389 options->max_duration_seconds = args->max_duration_seconds;
390 options->terminate_on =
391 stasis_app_recording_termination_parse(args->terminate_on);
393 stasis_app_recording_if_exists_parse(args->if_exists);
394 options->beep = args->beep;
396 if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) {
397 ast_ari_response_error(
398 response, 400, "Bad Request",
399 "terminateOn invalid");
403 if (options->if_exists == -1) {
404 ast_ari_response_error(
405 response, 400, "Bad Request",
410 recording = stasis_app_control_record(control, options);
411 if (recording == NULL) {
414 /* While the arguments are invalid, we should have
415 * caught them prior to calling record.
417 ast_ari_response_error(
418 response, 500, "Internal Server Error",
419 "Error parsing request");
422 ast_ari_response_error(response, 409, "Conflict",
423 "Recording '%s' already in progress",
427 ast_ari_response_error(
428 response, 500, "Internal Server Error",
432 ast_ari_response_error(
433 response, 400, "Bad Request",
434 "Recording name invalid");
438 "Unrecognized recording error: %s\n",
440 ast_ari_response_error(
441 response, 500, "Internal Server Error",
442 "Internal Server Error");
448 uri_name_maxlen = strlen(args->name) * 3;
449 uri_encoded_name = ast_malloc(uri_name_maxlen);
450 if (!uri_encoded_name) {
451 ast_ari_response_error(
452 response, 500, "Internal Server Error",
456 ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen,
459 ast_asprintf(&recording_url, "/recordings/live/%s", uri_encoded_name);
460 if (!recording_url) {
461 ast_ari_response_error(
462 response, 500, "Internal Server Error",
467 json = stasis_app_recording_to_json(recording);
469 ast_ari_response_error(
470 response, 500, "Internal Server Error",
475 ast_ari_response_created(response, recording_url, json);
478 void ast_ari_get_channel(struct ast_variable *headers,
479 struct ast_get_channel_args *args,
480 struct ast_ari_response *response)
482 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
483 struct stasis_cache *cache;
484 struct ast_channel_snapshot *snapshot;
486 cache = ast_channel_cache();
488 ast_ari_response_error(
489 response, 500, "Internal Server Error",
490 "Message bus not initialized");
494 msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
497 ast_ari_response_error(
498 response, 404, "Not Found",
499 "Channel not found");
503 snapshot = stasis_message_data(msg);
504 ast_assert(snapshot != NULL);
506 ast_ari_response_ok(response,
507 ast_channel_snapshot_to_json(snapshot));
510 void ast_ari_delete_channel(struct ast_variable *headers,
511 struct ast_delete_channel_args *args,
512 struct ast_ari_response *response)
514 RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
516 chan = ast_channel_get_by_name(args->channel_id);
518 ast_ari_response_error(
519 response, 404, "Not Found",
520 "Channel not found");
524 ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
526 ast_ari_response_no_content(response);
529 void ast_ari_get_channels(struct ast_variable *headers,
530 struct ast_get_channels_args *args,
531 struct ast_ari_response *response)
533 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
534 RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
535 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
536 struct ao2_iterator i;
539 cache = ast_channel_cache();
541 ast_ari_response_error(
542 response, 500, "Internal Server Error",
543 "Message bus not initialized");
548 snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type());
550 ast_ari_response_alloc_failed(response);
554 json = ast_json_array_create();
556 ast_ari_response_alloc_failed(response);
560 i = ao2_iterator_init(snapshots, 0);
561 while ((obj = ao2_iterator_next(&i))) {
562 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
563 struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
564 int r = ast_json_array_append(
565 json, ast_channel_snapshot_to_json(snapshot));
567 ast_ari_response_alloc_failed(response);
571 ao2_iterator_destroy(&i);
573 ast_ari_response_ok(response, ast_json_ref(json));
576 void ast_ari_originate(struct ast_variable *headers,
577 struct ast_originate_args *args,
578 struct ast_ari_response *response)
581 char dialdevice[AST_CHANNEL_NAME];
582 char *caller_id = NULL;
583 char *cid_num = NULL;
584 char *cid_name = NULL;
588 struct ast_channel *chan;
589 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
591 if (ast_strlen_zero(args->endpoint)) {
592 ast_ari_response_error(response, 400, "Bad Request",
593 "Endpoint must be specified");
597 dialtech = ast_strdupa(args->endpoint);
598 if ((stuff = strchr(dialtech, '/'))) {
600 ast_copy_string(dialdevice, stuff, sizeof(dialdevice));
603 if (ast_strlen_zero(dialtech) || ast_strlen_zero(dialdevice)) {
604 ast_ari_response_error(response, 400, "Bad Request",
605 "Invalid endpoint specified");
609 if (args->timeout > 0) {
610 timeout = args->timeout * 1000;
611 } else if (args->timeout == -1) {
615 if (!ast_strlen_zero(args->caller_id)) {
616 caller_id = ast_strdupa(args->caller_id);
617 ast_callerid_parse(caller_id, &cid_name, &cid_num);
619 if (ast_is_shrinkable_phonenumber(cid_num)) {
620 ast_shrink_phone_number(cid_num);
624 if (!ast_strlen_zero(args->app)) {
625 const char *app = "Stasis";
627 RAII_VAR(struct ast_str *, appdata, ast_str_create(64), ast_free);
630 ast_ari_response_alloc_failed(response);
634 ast_str_set(&appdata, 0, "%s", args->app);
635 if (!ast_strlen_zero(args->app_args)) {
636 ast_str_append(&appdata, 0, ",%s", args->app_args);
639 /* originate a channel, putting it into an application */
640 if (ast_pbx_outgoing_app(dialtech, NULL, dialdevice, timeout, app, ast_str_buffer(appdata), NULL, 0, cid_num, cid_name, NULL, NULL, &chan)) {
641 ast_ari_response_alloc_failed(response);
644 } else if (!ast_strlen_zero(args->extension)) {
645 /* originate a channel, sending it to an extension */
646 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)) {
647 ast_ari_response_alloc_failed(response);
651 ast_ari_response_error(response, 400, "Bad Request",
652 "Application or extension must be specified");
656 if (!ast_strlen_zero(args->app)) {
657 /* channel: + channel ID + null terminator */
658 char uri[9 + strlen(ast_channel_uniqueid(chan))];
659 const char *uris[1] = { uri, };
661 sprintf(uri, "channel:%s", ast_channel_uniqueid(chan));
662 stasis_app_subscribe(args->app, uris, 1, NULL);
665 snapshot = ast_channel_snapshot_create(chan);
666 ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot));
668 ast_channel_unlock(chan);
669 ast_channel_unref(chan);
672 void ast_ari_get_channel_var(struct ast_variable *headers, struct ast_get_channel_var_args *args, struct ast_ari_response *response)
674 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
675 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
676 RAII_VAR(char *, value, NULL, ast_free);
678 ast_assert(response != NULL);
680 if (ast_strlen_zero(args->variable)) {
681 ast_ari_response_error(
682 response, 400, "Bad Request",
683 "Variable name is required");
687 control = find_control(response, args->channel_id);
688 if (control == NULL) {
689 /* response filled in by find_control */
693 value = stasis_app_control_get_channel_var(control, args->variable);
695 if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
696 ast_ari_response_alloc_failed(response);
700 ast_ari_response_ok(response, ast_json_ref(json));
703 void ast_ari_set_channel_var(struct ast_variable *headers, struct ast_set_channel_var_args *args, struct ast_ari_response *response)
705 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
707 ast_assert(response != NULL);
709 if (ast_strlen_zero(args->variable)) {
710 ast_ari_response_error(
711 response, 400, "Bad Request",
712 "Variable name is required");
716 control = find_control(response, args->channel_id);
717 if (control == NULL) {
718 /* response filled in by find_control */
722 if (stasis_app_control_set_channel_var(control, args->variable, args->value)) {
723 ast_ari_response_error(
724 response, 400, "Bad Request",
725 "Failed to execute function");
729 ast_ari_response_no_content(response);