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 if (!ast_get_format_for_file_ext(options->format)) {
411 ast_ari_response_error(
412 response, 422, "Unprocessable Entity",
413 "specified format is unknown on this system");
417 recording = stasis_app_control_record(control, options);
418 if (recording == NULL) {
421 /* While the arguments are invalid, we should have
422 * caught them prior to calling record.
424 ast_ari_response_error(
425 response, 500, "Internal Server Error",
426 "Error parsing request");
429 ast_ari_response_error(response, 409, "Conflict",
430 "Recording '%s' already exists and can not be overwritten",
434 ast_ari_response_error(
435 response, 500, "Internal Server Error",
439 ast_ari_response_error(
440 response, 400, "Bad Request",
441 "Recording name invalid");
445 "Unrecognized recording error: %s\n",
447 ast_ari_response_error(
448 response, 500, "Internal Server Error",
449 "Internal Server Error");
455 uri_name_maxlen = strlen(args->name) * 3;
456 uri_encoded_name = ast_malloc(uri_name_maxlen);
457 if (!uri_encoded_name) {
458 ast_ari_response_error(
459 response, 500, "Internal Server Error",
463 ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen,
466 ast_asprintf(&recording_url, "/recordings/live/%s", uri_encoded_name);
467 if (!recording_url) {
468 ast_ari_response_error(
469 response, 500, "Internal Server Error",
474 json = stasis_app_recording_to_json(recording);
476 ast_ari_response_error(
477 response, 500, "Internal Server Error",
482 ast_ari_response_created(response, recording_url, json);
485 void ast_ari_get_channel(struct ast_variable *headers,
486 struct ast_get_channel_args *args,
487 struct ast_ari_response *response)
489 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
490 struct stasis_cache *cache;
491 struct ast_channel_snapshot *snapshot;
493 cache = ast_channel_cache();
495 ast_ari_response_error(
496 response, 500, "Internal Server Error",
497 "Message bus not initialized");
501 msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
504 ast_ari_response_error(
505 response, 404, "Not Found",
506 "Channel not found");
510 snapshot = stasis_message_data(msg);
511 ast_assert(snapshot != NULL);
513 ast_ari_response_ok(response,
514 ast_channel_snapshot_to_json(snapshot));
517 void ast_ari_delete_channel(struct ast_variable *headers,
518 struct ast_delete_channel_args *args,
519 struct ast_ari_response *response)
521 RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
523 chan = ast_channel_get_by_name(args->channel_id);
525 ast_ari_response_error(
526 response, 404, "Not Found",
527 "Channel not found");
531 ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
533 ast_ari_response_no_content(response);
536 void ast_ari_get_channels(struct ast_variable *headers,
537 struct ast_get_channels_args *args,
538 struct ast_ari_response *response)
540 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
541 RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
542 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
543 struct ao2_iterator i;
546 cache = ast_channel_cache();
548 ast_ari_response_error(
549 response, 500, "Internal Server Error",
550 "Message bus not initialized");
555 snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type());
557 ast_ari_response_alloc_failed(response);
561 json = ast_json_array_create();
563 ast_ari_response_alloc_failed(response);
567 i = ao2_iterator_init(snapshots, 0);
568 while ((obj = ao2_iterator_next(&i))) {
569 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
570 struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
571 int r = ast_json_array_append(
572 json, ast_channel_snapshot_to_json(snapshot));
574 ast_ari_response_alloc_failed(response);
578 ao2_iterator_destroy(&i);
580 ast_ari_response_ok(response, ast_json_ref(json));
583 void ast_ari_originate(struct ast_variable *headers,
584 struct ast_originate_args *args,
585 struct ast_ari_response *response)
588 char dialdevice[AST_CHANNEL_NAME];
589 char *caller_id = NULL;
590 char *cid_num = NULL;
591 char *cid_name = NULL;
595 struct ast_channel *chan;
596 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
598 if (ast_strlen_zero(args->endpoint)) {
599 ast_ari_response_error(response, 400, "Bad Request",
600 "Endpoint must be specified");
604 dialtech = ast_strdupa(args->endpoint);
605 if ((stuff = strchr(dialtech, '/'))) {
607 ast_copy_string(dialdevice, stuff, sizeof(dialdevice));
610 if (ast_strlen_zero(dialtech) || ast_strlen_zero(dialdevice)) {
611 ast_ari_response_error(response, 400, "Bad Request",
612 "Invalid endpoint specified");
616 if (args->timeout > 0) {
617 timeout = args->timeout * 1000;
618 } else if (args->timeout == -1) {
622 if (!ast_strlen_zero(args->caller_id)) {
623 caller_id = ast_strdupa(args->caller_id);
624 ast_callerid_parse(caller_id, &cid_name, &cid_num);
626 if (ast_is_shrinkable_phonenumber(cid_num)) {
627 ast_shrink_phone_number(cid_num);
631 if (!ast_strlen_zero(args->app)) {
632 const char *app = "Stasis";
634 RAII_VAR(struct ast_str *, appdata, ast_str_create(64), ast_free);
637 ast_ari_response_alloc_failed(response);
641 ast_str_set(&appdata, 0, "%s", args->app);
642 if (!ast_strlen_zero(args->app_args)) {
643 ast_str_append(&appdata, 0, ",%s", args->app_args);
646 /* originate a channel, putting it into an application */
647 if (ast_pbx_outgoing_app(dialtech, NULL, dialdevice, timeout, app, ast_str_buffer(appdata), NULL, 0, cid_num, cid_name, NULL, NULL, &chan)) {
648 ast_ari_response_alloc_failed(response);
651 } else if (!ast_strlen_zero(args->extension)) {
652 /* originate a channel, sending it to an extension */
653 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)) {
654 ast_ari_response_alloc_failed(response);
658 ast_ari_response_error(response, 400, "Bad Request",
659 "Application or extension must be specified");
663 if (!ast_strlen_zero(args->app)) {
664 /* channel: + channel ID + null terminator */
665 char uri[9 + strlen(ast_channel_uniqueid(chan))];
666 const char *uris[1] = { uri, };
668 sprintf(uri, "channel:%s", ast_channel_uniqueid(chan));
669 stasis_app_subscribe(args->app, uris, 1, NULL);
672 snapshot = ast_channel_snapshot_create(chan);
673 ast_ari_response_ok(response, ast_channel_snapshot_to_json(snapshot));
675 ast_channel_unlock(chan);
676 ast_channel_unref(chan);
679 void ast_ari_get_channel_var(struct ast_variable *headers, struct ast_get_channel_var_args *args, struct ast_ari_response *response)
681 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
682 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
683 RAII_VAR(char *, value, NULL, ast_free);
685 ast_assert(response != NULL);
687 if (ast_strlen_zero(args->variable)) {
688 ast_ari_response_error(
689 response, 400, "Bad Request",
690 "Variable name is required");
694 control = find_control(response, args->channel_id);
695 if (control == NULL) {
696 /* response filled in by find_control */
700 value = stasis_app_control_get_channel_var(control, args->variable);
702 if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
703 ast_ari_response_alloc_failed(response);
707 ast_ari_response_ok(response, ast_json_ref(json));
710 void ast_ari_set_channel_var(struct ast_variable *headers, struct ast_set_channel_var_args *args, struct ast_ari_response *response)
712 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
714 ast_assert(response != NULL);
716 if (ast_strlen_zero(args->variable)) {
717 ast_ari_response_error(
718 response, 400, "Bad Request",
719 "Variable name is required");
723 control = find_control(response, args->channel_id);
724 if (control == NULL) {
725 /* response filled in by find_control */
729 if (stasis_app_control_set_channel_var(control, args->variable, args->value)) {
730 ast_ari_response_error(
731 response, 400, "Bad Request",
732 "Failed to execute function");
736 ast_ari_response_no_content(response);