6f68d52e4decb06c4b8373e761080abceefa5938
[asterisk/asterisk.git] / res / ari / resource_device_states.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2012 - 2013, Digium, Inc.
5  *
6  * Kevin Harwell <kharwell@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief /api-docs/deviceStates.{format} implementation- Device state resources
22  *
23  * \author Kevin Harwell <kharwell@digium.com>
24  */
25
26 /*** MODULEINFO
27         <depend type="module">res_stasis_device_states</depend>
28         <support_level>core</support_level>
29  ***/
30
31 #include "asterisk.h"
32
33 ASTERISK_REGISTER_FILE()
34
35 #include "resource_device_states.h"
36 #include "asterisk/stasis_app_device_state.h"
37
38 void ast_ari_device_states_list(
39         struct ast_variable *headers,
40         struct ast_ari_device_states_list_args *args,
41         struct ast_ari_response *response)
42 {
43         struct ast_json *json;
44
45         if (!(json = stasis_app_device_states_to_json())) {
46                 ast_ari_response_error(response, 500,
47                         "Internal Server Error", "Error building response");
48                 return;
49         }
50
51         ast_ari_response_ok(response, json);
52 }
53
54 void ast_ari_device_states_get(struct ast_variable *headers,
55         struct ast_ari_device_states_get_args *args,
56         struct ast_ari_response *response)
57 {
58         struct ast_json *json;
59
60         if (!(json = stasis_app_device_state_to_json(
61                       args->device_name, ast_device_state(args->device_name)))) {
62                 ast_ari_response_error(response, 500,
63                         "Internal Server Error", "Error building response");
64                 return;
65         }
66
67         ast_ari_response_ok(response, json);
68 }
69
70 void ast_ari_device_states_update(struct ast_variable *headers,
71         struct ast_ari_device_states_update_args *args,
72         struct ast_ari_response *response)
73 {
74         switch (stasis_app_device_state_update(
75                         args->device_name, args->device_state)) {
76         case STASIS_DEVICE_STATE_NOT_CONTROLLED:
77                 ast_ari_response_error(response, 409,
78                         "Conflict", "Uncontrolled device specified");
79                 return;
80         case STASIS_DEVICE_STATE_MISSING:
81                 ast_ari_response_error(response, 404,
82                         "Not Found", "Device name is missing");
83                 return;
84         case STASIS_DEVICE_STATE_UNKNOWN:
85                 ast_ari_response_error(response, 500, "Internal Server Error",
86                                        "Unknown device");
87                 return;
88         case STASIS_DEVICE_STATE_OK:
89         case STASIS_DEVICE_STATE_SUBSCRIBERS: /* shouldn't be returned for update */
90                 ast_ari_response_no_content(response);
91         }
92 }
93
94 void ast_ari_device_states_delete(struct ast_variable *headers,
95         struct ast_ari_device_states_delete_args *args,
96         struct ast_ari_response *response)
97 {
98         switch (stasis_app_device_state_delete(args->device_name)) {
99         case STASIS_DEVICE_STATE_NOT_CONTROLLED:
100                 ast_ari_response_error(response, 409,
101                         "Conflict", "Uncontrolled device specified");
102                 return;
103         case STASIS_DEVICE_STATE_MISSING:
104                 ast_ari_response_error(response, 404,
105                         "Not Found", "Device name is missing");
106                 return;
107         case STASIS_DEVICE_STATE_SUBSCRIBERS:
108                 ast_ari_response_error(response, 500,
109                         "Internal Server Error",
110                         "Cannot delete device with subscribers");
111                 return;
112         case STASIS_DEVICE_STATE_OK:
113         case STASIS_DEVICE_STATE_UNKNOWN:
114                 ast_ari_response_no_content(response);
115         }
116 }