summaryrefslogtreecommitdiff
path: root/res/ari/resource_device_states.c
blob: 9dec1b68d0541863968ba82a1612c121749de87e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2012 - 2013, Digium, Inc.
 *
 * Kevin Harwell <kharwell@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 *
 * \brief /api-docs/deviceStates.{format} implementation- Device state resources
 *
 * \author Kevin Harwell <kharwell@digium.com>
 */

/*** MODULEINFO
	<depend type="module">res_stasis_device_state</depend>
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

#include "resource_device_states.h"
#include "asterisk/stasis_app_device_state.h"

void ast_ari_device_states_list(
	struct ast_variable *headers,
	struct ast_ari_device_states_list_args *args,
	struct ast_ari_response *response)
{
	struct ast_json *json;

	if (!(json = stasis_app_device_states_to_json())) {
		ast_ari_response_error(response, 500,
			"Internal Server Error", "Error building response");
		return;
	}

	ast_ari_response_ok(response, json);
}

void ast_ari_device_states_get(struct ast_variable *headers,
	struct ast_ari_device_states_get_args *args,
	struct ast_ari_response *response)
{
	struct ast_json *json;

	if (!(json = stasis_app_device_state_to_json(
		      args->device_name, ast_device_state(args->device_name)))) {
		ast_ari_response_error(response, 500,
			"Internal Server Error", "Error building response");
		return;
	}

	ast_ari_response_ok(response, json);
}

void ast_ari_device_states_update(struct ast_variable *headers,
	struct ast_ari_device_states_update_args *args,
	struct ast_ari_response *response)
{
	switch (stasis_app_device_state_update(
			args->device_name, args->device_state)) {
	case STASIS_DEVICE_STATE_NOT_CONTROLLED:
		ast_ari_response_error(response, 409,
			"Conflict", "Uncontrolled device specified");
		return;
	case STASIS_DEVICE_STATE_MISSING:
		ast_ari_response_error(response, 404,
			"Not Found", "Device name is missing");
		return;
	case STASIS_DEVICE_STATE_UNKNOWN:
		ast_ari_response_error(response, 500, "Internal Server Error",
				       "Unknown device");
		return;
	case STASIS_DEVICE_STATE_OK:
	case STASIS_DEVICE_STATE_SUBSCRIBERS: /* shouldn't be returned for update */
		ast_ari_response_no_content(response);
	}
}

void ast_ari_device_states_delete(struct ast_variable *headers,
	struct ast_ari_device_states_delete_args *args,
	struct ast_ari_response *response)
{
	switch (stasis_app_device_state_delete(args->device_name)) {
	case STASIS_DEVICE_STATE_NOT_CONTROLLED:
		ast_ari_response_error(response, 409,
			"Conflict", "Uncontrolled device specified");
		return;
	case STASIS_DEVICE_STATE_MISSING:
		ast_ari_response_error(response, 404,
			"Not Found", "Device name is missing");
		return;
	case STASIS_DEVICE_STATE_SUBSCRIBERS:
		ast_ari_response_error(response, 500,
			"Internal Server Error",
			"Cannot delete device with subscribers");
		return;
	case STASIS_DEVICE_STATE_OK:
	case STASIS_DEVICE_STATE_UNKNOWN:
		ast_ari_response_no_content(response);
	}
}