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 (!strcmp(args->direction, "in")) {
156 direction = AST_MUTE_DIRECTION_READ;
157 } else if (!strcmp(args->direction, "out")) {
158 direction = AST_MUTE_DIRECTION_WRITE;
159 } else if (!strcmp(args->direction, "both")) {
160 direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
162 ast_ari_response_error(
163 response, 400, "Bad Request",
164 "Invalid direction specified");
168 stasis_app_control_mute(control, direction, frametype);
170 ast_ari_response_no_content(response);
173 void ast_ari_unmute_channel(struct ast_variable *headers, struct ast_unmute_channel_args *args, struct ast_ari_response *response)
175 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
176 unsigned int direction = 0;
177 enum ast_frame_type frametype = AST_FRAME_VOICE;
179 control = find_control(response, args->channel_id);
180 if (control == NULL) {
184 if (!strcmp(args->direction, "in")) {
185 direction = AST_MUTE_DIRECTION_READ;
186 } else if (!strcmp(args->direction, "out")) {
187 direction = AST_MUTE_DIRECTION_WRITE;
188 } else if (!strcmp(args->direction, "both")) {
189 direction = AST_MUTE_DIRECTION_READ | AST_MUTE_DIRECTION_WRITE;
191 ast_ari_response_error(
192 response, 400, "Bad Request",
193 "Invalid direction specified");
197 stasis_app_control_unmute(control, direction, frametype);
199 ast_ari_response_no_content(response);
202 void ast_ari_hold_channel(struct ast_variable *headers, struct ast_hold_channel_args *args, struct ast_ari_response *response)
204 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
206 control = find_control(response, args->channel_id);
207 if (control == NULL) {
208 /* Response filled in by find_control */
212 stasis_app_control_hold(control);
214 ast_ari_response_no_content(response);
217 void ast_ari_unhold_channel(struct ast_variable *headers, struct ast_unhold_channel_args *args, struct ast_ari_response *response)
219 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
221 control = find_control(response, args->channel_id);
222 if (control == NULL) {
223 /* Response filled in by find_control */
227 stasis_app_control_unhold(control);
229 ast_ari_response_no_content(response);
232 void ast_ari_moh_start_channel(struct ast_variable *headers, struct ast_moh_start_channel_args *args, struct ast_ari_response *response)
234 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
236 control = find_control(response, args->channel_id);
237 if (control == NULL) {
238 /* Response filled in by find_control */
242 stasis_app_control_moh_start(control, args->moh_class);
243 ast_ari_response_no_content(response);
246 void ast_ari_moh_stop_channel(struct ast_variable *headers, struct ast_moh_stop_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_stop(control);
257 ast_ari_response_no_content(response);
260 void ast_ari_play_on_channel(struct ast_variable *headers,
261 struct ast_play_on_channel_args *args,
262 struct ast_ari_response *response)
264 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
265 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
266 RAII_VAR(struct stasis_app_playback *, playback, NULL, ao2_cleanup);
267 RAII_VAR(char *, playback_url, NULL, ast_free);
268 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
269 const char *language;
271 ast_assert(response != NULL);
273 control = find_control(response, args->channel_id);
274 if (control == NULL) {
275 /* Response filled in by find_control */
279 snapshot = stasis_app_control_get_snapshot(control);
281 ast_ari_response_error(
282 response, 404, "Not Found",
283 "Channel not found");
287 if (args->skipms < 0) {
288 ast_ari_response_error(
289 response, 400, "Bad Request",
290 "skipms cannot be negative");
294 if (args->offsetms < 0) {
295 ast_ari_response_error(
296 response, 400, "Bad Request",
297 "offsetms cannot be negative");
301 language = S_OR(args->lang, snapshot->language);
303 playback = stasis_app_control_play_uri(control, args->media, language,
304 args->channel_id, STASIS_PLAYBACK_TARGET_CHANNEL, args->skipms, args->offsetms);
306 ast_ari_response_error(
307 response, 500, "Internal Server Error",
308 "Failed to queue media for playback");
312 ast_asprintf(&playback_url, "/playback/%s",
313 stasis_app_playback_get_id(playback));
315 ast_ari_response_error(
316 response, 500, "Internal Server Error",
321 json = stasis_app_playback_to_json(playback);
323 ast_ari_response_error(
324 response, 500, "Internal Server Error",
329 ast_ari_response_created(response, playback_url, json);
332 void ast_ari_record_channel(struct ast_variable *headers,
333 struct ast_record_channel_args *args,
334 struct ast_ari_response *response)
336 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
337 RAII_VAR(struct ast_channel_snapshot *, snapshot, NULL, ao2_cleanup);
338 RAII_VAR(struct stasis_app_recording *, recording, NULL, ao2_cleanup);
339 RAII_VAR(char *, recording_url, NULL, ast_free);
340 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
341 RAII_VAR(struct stasis_app_recording_options *, options, NULL,
343 RAII_VAR(char *, uri_encoded_name, NULL, ast_free);
344 size_t uri_name_maxlen;
346 ast_assert(response != NULL);
348 if (args->max_duration_seconds < 0) {
349 ast_ari_response_error(
350 response, 400, "Bad Request",
351 "max_duration_seconds cannot be negative");
355 if (args->max_silence_seconds < 0) {
356 ast_ari_response_error(
357 response, 400, "Bad Request",
358 "max_silence_seconds cannot be negative");
362 control = find_control(response, args->channel_id);
363 if (control == NULL) {
364 /* Response filled in by find_control */
368 options = stasis_app_recording_options_create(args->name, args->format);
369 if (options == NULL) {
370 ast_ari_response_error(
371 response, 500, "Internal Server Error",
374 options->max_silence_seconds = args->max_silence_seconds;
375 options->max_duration_seconds = args->max_duration_seconds;
376 options->terminate_on =
377 stasis_app_recording_termination_parse(args->terminate_on);
379 stasis_app_recording_if_exists_parse(args->if_exists);
380 options->beep = args->beep;
382 if (options->terminate_on == STASIS_APP_RECORDING_TERMINATE_INVALID) {
383 ast_ari_response_error(
384 response, 400, "Bad Request",
385 "terminateOn invalid");
389 if (options->if_exists == -1) {
390 ast_ari_response_error(
391 response, 400, "Bad Request",
396 recording = stasis_app_control_record(control, options);
397 if (recording == NULL) {
400 /* While the arguments are invalid, we should have
401 * caught them prior to calling record.
403 ast_ari_response_error(
404 response, 500, "Internal Server Error",
405 "Error parsing request");
408 ast_ari_response_error(response, 409, "Conflict",
409 "Recording '%s' already in progress",
413 ast_ari_response_error(
414 response, 500, "Internal Server Error",
418 ast_ari_response_error(
419 response, 400, "Bad Request",
420 "Recording name invalid");
424 "Unrecognized recording error: %s\n",
426 ast_ari_response_error(
427 response, 500, "Internal Server Error",
428 "Internal Server Error");
434 uri_name_maxlen = strlen(args->name) * 3;
435 uri_encoded_name = ast_malloc(uri_name_maxlen);
436 if (!uri_encoded_name) {
437 ast_ari_response_error(
438 response, 500, "Internal Server Error",
442 ast_uri_encode(args->name, uri_encoded_name, uri_name_maxlen,
445 ast_asprintf(&recording_url, "/recordings/live/%s", uri_encoded_name);
446 if (!recording_url) {
447 ast_ari_response_error(
448 response, 500, "Internal Server Error",
453 json = stasis_app_recording_to_json(recording);
455 ast_ari_response_error(
456 response, 500, "Internal Server Error",
461 ast_ari_response_created(response, recording_url, json);
464 void ast_ari_get_channel(struct ast_variable *headers,
465 struct ast_get_channel_args *args,
466 struct ast_ari_response *response)
468 RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
469 struct stasis_cache *cache;
470 struct ast_channel_snapshot *snapshot;
472 cache = ast_channel_cache();
474 ast_ari_response_error(
475 response, 500, "Internal Server Error",
476 "Message bus not initialized");
480 msg = stasis_cache_get(cache, ast_channel_snapshot_type(),
483 ast_ari_response_error(
484 response, 404, "Not Found",
485 "Channel not found");
489 snapshot = stasis_message_data(msg);
490 ast_assert(snapshot != NULL);
492 ast_ari_response_ok(response,
493 ast_channel_snapshot_to_json(snapshot));
496 void ast_ari_delete_channel(struct ast_variable *headers,
497 struct ast_delete_channel_args *args,
498 struct ast_ari_response *response)
500 RAII_VAR(struct ast_channel *, chan, NULL, ao2_cleanup);
502 chan = ast_channel_get_by_name(args->channel_id);
504 ast_ari_response_error(
505 response, 404, "Not Found",
506 "Channel not found");
510 ast_softhangup(chan, AST_SOFTHANGUP_EXPLICIT);
512 ast_ari_response_no_content(response);
515 void ast_ari_get_channels(struct ast_variable *headers,
516 struct ast_get_channels_args *args,
517 struct ast_ari_response *response)
519 RAII_VAR(struct stasis_cache *, cache, NULL, ao2_cleanup);
520 RAII_VAR(struct ao2_container *, snapshots, NULL, ao2_cleanup);
521 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
522 struct ao2_iterator i;
525 cache = ast_channel_cache();
527 ast_ari_response_error(
528 response, 500, "Internal Server Error",
529 "Message bus not initialized");
534 snapshots = stasis_cache_dump(cache, ast_channel_snapshot_type());
536 ast_ari_response_alloc_failed(response);
540 json = ast_json_array_create();
542 ast_ari_response_alloc_failed(response);
546 i = ao2_iterator_init(snapshots, 0);
547 while ((obj = ao2_iterator_next(&i))) {
548 RAII_VAR(struct stasis_message *, msg, obj, ao2_cleanup);
549 struct ast_channel_snapshot *snapshot = stasis_message_data(msg);
550 int r = ast_json_array_append(
551 json, ast_channel_snapshot_to_json(snapshot));
553 ast_ari_response_alloc_failed(response);
557 ao2_iterator_destroy(&i);
559 ast_ari_response_ok(response, ast_json_ref(json));
562 void ast_ari_originate(struct ast_variable *headers,
563 struct ast_originate_args *args,
564 struct ast_ari_response *response)
567 char dialdevice[AST_CHANNEL_NAME];
568 char *caller_id = NULL;
569 char *cid_num = NULL;
570 char *cid_name = NULL;
575 if (ast_strlen_zero(args->endpoint)) {
576 ast_ari_response_error(response, 400, "Bad Request",
577 "Endpoint must be specified");
581 dialtech = ast_strdupa(args->endpoint);
582 if ((stuff = strchr(dialtech, '/'))) {
584 ast_copy_string(dialdevice, stuff, sizeof(dialdevice));
587 if (ast_strlen_zero(dialtech) || ast_strlen_zero(dialdevice)) {
588 ast_ari_response_error(response, 400, "Bad Request",
589 "Invalid endpoint specified");
593 if (args->timeout > 0) {
594 timeout = args->timeout * 1000;
595 } else if (args->timeout == -1) {
599 if (!ast_strlen_zero(args->caller_id)) {
600 caller_id = ast_strdupa(args->caller_id);
601 ast_callerid_parse(caller_id, &cid_name, &cid_num);
603 if (ast_is_shrinkable_phonenumber(cid_num)) {
604 ast_shrink_phone_number(cid_num);
608 if (!ast_strlen_zero(args->app)) {
609 const char *app = "Stasis";
611 RAII_VAR(struct ast_str *, appdata, ast_str_create(64), ast_free);
614 ast_ari_response_alloc_failed(response);
618 ast_str_set(&appdata, 0, "%s", args->app);
619 if (!ast_strlen_zero(args->app_args)) {
620 ast_str_append(&appdata, 0, ",%s", args->app_args);
623 /* originate a channel, putting it into an application */
624 if (ast_pbx_outgoing_app(dialtech, NULL, dialdevice, timeout, app, ast_str_buffer(appdata), NULL, 0, cid_num, cid_name, NULL, NULL, NULL)) {
625 ast_ari_response_alloc_failed(response);
628 } else if (!ast_strlen_zero(args->extension)) {
629 /* originate a channel, sending it to an extension */
630 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, NULL, 0)) {
631 ast_ari_response_alloc_failed(response);
635 ast_ari_response_error(response, 400, "Bad Request",
636 "Application or extension must be specified");
640 ast_ari_response_no_content(response);
643 void ast_ari_get_channel_var(struct ast_variable *headers, struct ast_get_channel_var_args *args, struct ast_ari_response *response)
645 RAII_VAR(struct ast_json *, json, NULL, ast_json_unref);
646 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
647 RAII_VAR(char *, value, NULL, ast_free);
649 ast_assert(response != NULL);
651 if (ast_strlen_zero(args->variable)) {
652 ast_ari_response_error(
653 response, 400, "Bad Request",
654 "Variable name is required");
658 control = find_control(response, args->channel_id);
659 if (control == NULL) {
660 /* response filled in by find_control */
664 value = stasis_app_control_get_channel_var(control, args->variable);
666 if (!(json = ast_json_pack("{s: s}", "value", S_OR(value, "")))) {
667 ast_ari_response_alloc_failed(response);
671 ast_ari_response_ok(response, ast_json_ref(json));
674 void ast_ari_set_channel_var(struct ast_variable *headers, struct ast_set_channel_var_args *args, struct ast_ari_response *response)
676 RAII_VAR(struct stasis_app_control *, control, NULL, ao2_cleanup);
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 if (stasis_app_control_set_channel_var(control, args->variable, args->value)) {
694 ast_ari_response_error(
695 response, 400, "Bad Request",
696 "Failed to execute function");
700 ast_ari_response_no_content(response);