Debian Codegraph @

res/res_ari_device_states.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2012 - 2013, Digium, Inc.
5 *
6 * Kevin Harwell <[email protected]>
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 /*
20 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
21 * !!!!! DO NOT EDIT !!!!!
22 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23 * This file is generated by a mustache template. Please see the original
24 * template in rest-api-templates/res_ari_resource.c.mustache
25 */
26
27 /*! \file
28 *
29 * \brief Device state resources
30 *
31 * \author Kevin Harwell <[email protected]>
32 */
33
34 /*** MODULEINFO
35 <depend type="module">res_ari</depend>
36 <depend type="module">res_ari_model</depend>
37 <depend type="module">res_stasis</depend>
38 <depend type="module">res_stasis_device_state</depend>
39 <support_level>core</support_level>
40 ***/
41
42 #include "asterisk.h"
43
44 #include "asterisk/app.h"
45 #include "asterisk/module.h"
46 #include "asterisk/stasis_app.h"
47 #include "ari/resource_device_states.h"
48 #if defined(AST_DEVMODE)
49 #include "ari/ari_model_validators.h"
50 #endif
51
52 #define MAX_VALS 128
53
54 /*!
55 * \brief Parameter parsing callback for /deviceStates.
56 * \param ser TCP/TLS session object
57 * \param get_params GET parameters in the HTTP request.
58 * \param path_vars Path variables extracted from the request.
59 * \param headers HTTP headers.
60 * \param body
61 * \param[out] response Response to the HTTP request.
62 */
63 static void ast_ari_device_states_list_cb(
64 struct ast_tcptls_session_instance *ser,
65 struct ast_variable *get_params, struct ast_variable *path_vars,
66 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
67 {
68 struct ast_ari_device_states_list_args args = {};
69 #if defined(AST_DEVMODE)
70 int is_valid;
71 int code;
72 #endif /* AST_DEVMODE */
73
74 ast_ari_device_states_list(headers, &args, response);
75 #if defined(AST_DEVMODE)
76 code = response->response_code;
77
78 switch (code) {
79 case 0: /* Implementation is still a stub, or the code wasn't set */
80 is_valid = response->message == NULL;
81 break;
82 case 500: /* Internal Server Error */
83 case 501: /* Not Implemented */
84 is_valid = 1;
85 break;
86 default:
87 if (200 <= code && code <= 299) {
88 is_valid = ast_ari_validate_list(response->message,
89 ast_ari_validate_device_state_fn());
90 } else {
91 ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates\n", code);
92 is_valid = 0;
93 }
94 }
95
96 if (!is_valid) {
97 ast_log(LOG_ERROR, "Response validation failed for /deviceStates\n");
98 ast_ari_response_error(response, 500,
99 "Internal Server Error", "Response validation failed");
100 }
101 #endif /* AST_DEVMODE */
102
103 fin: __attribute__((unused))
104 return;
105 }
106 /*!
107 * \brief Parameter parsing callback for /deviceStates/{deviceName}.
108 * \param ser TCP/TLS session object
109 * \param get_params GET parameters in the HTTP request.
110 * \param path_vars Path variables extracted from the request.
111 * \param headers HTTP headers.
112 * \param body
113 * \param[out] response Response to the HTTP request.
114 */
115 static void ast_ari_device_states_get_cb(
116 struct ast_tcptls_session_instance *ser,
117 struct ast_variable *get_params, struct ast_variable *path_vars,
118 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
119 {
120 struct ast_ari_device_states_get_args args = {};
121 struct ast_variable *i;
122 #if defined(AST_DEVMODE)
123 int is_valid;
124 int code;
125 #endif /* AST_DEVMODE */
126
127 for (i = path_vars; i; i = i->next) {
128 if (strcmp(i->name, "deviceName") == 0) {
129 args.device_name = (i->value);
130 } else
131 {}
132 }
133 ast_ari_device_states_get(headers, &args, response);
134 #if defined(AST_DEVMODE)
135 code = response->response_code;
136
137 switch (code) {
138 case 0: /* Implementation is still a stub, or the code wasn't set */
139 is_valid = response->message == NULL;
140 break;
141 case 500: /* Internal Server Error */
142 case 501: /* Not Implemented */
143 is_valid = 1;
144 break;
145 default:
146 if (200 <= code && code <= 299) {
147 is_valid = ast_ari_validate_device_state(
148 response->message);
149 } else {
150 ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates/{deviceName}\n", code);
151 is_valid = 0;
152 }
153 }
154
155 if (!is_valid) {
156 ast_log(LOG_ERROR, "Response validation failed for /deviceStates/{deviceName}\n");
157 ast_ari_response_error(response, 500,
158 "Internal Server Error", "Response validation failed");
159 }
160 #endif /* AST_DEVMODE */
161
162 fin: __attribute__((unused))
163 return;
164 }
165 int ast_ari_device_states_update_parse_body(
166 struct ast_json *body,
167 struct ast_ari_device_states_update_args *args)
168 {
169 struct ast_json *field;
170 /* Parse query parameters out of it */
171 field = ast_json_object_get(body, "deviceState");
172 if (field) {
173 args->device_state = ast_json_string_get(field);
174 }
175 return 0;
176 }
177
178 /*!
179 * \brief Parameter parsing callback for /deviceStates/{deviceName}.
180 * \param ser TCP/TLS session object
181 * \param get_params GET parameters in the HTTP request.
182 * \param path_vars Path variables extracted from the request.
183 * \param headers HTTP headers.
184 * \param body
185 * \param[out] response Response to the HTTP request.
186 */
187 static void ast_ari_device_states_update_cb(
188 struct ast_tcptls_session_instance *ser,
189 struct ast_variable *get_params, struct ast_variable *path_vars,
190 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
191 {
192 struct ast_ari_device_states_update_args args = {};
193 struct ast_variable *i;
194 #if defined(AST_DEVMODE)
195 int is_valid;
196 int code;
197 #endif /* AST_DEVMODE */
198
199 for (i = get_params; i; i = i->next) {
200 if (strcmp(i->name, "deviceState") == 0) {
201 args.device_state = (i->value);
202 } else
203 {}
204 }
205 for (i = path_vars; i; i = i->next) {
206 if (strcmp(i->name, "deviceName") == 0) {
207 args.device_name = (i->value);
208 } else
209 {}
210 }
211 if (ast_ari_device_states_update_parse_body(body, &args)) {
212 ast_ari_response_alloc_failed(response);
213 goto fin;
214 }
215 ast_ari_device_states_update(headers, &args, response);
216 #if defined(AST_DEVMODE)
217 code = response->response_code;
218
219 switch (code) {
220 case 0: /* Implementation is still a stub, or the code wasn't set */
221 is_valid = response->message == NULL;
222 break;
223 case 500: /* Internal Server Error */
224 case 501: /* Not Implemented */
225 case 404: /* Device name is missing */
226 case 409: /* Uncontrolled device specified */
227 is_valid = 1;
228 break;
229 default:
230 if (200 <= code && code <= 299) {
231 is_valid = ast_ari_validate_void(
232 response->message);
233 } else {
234 ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates/{deviceName}\n", code);
235 is_valid = 0;
236 }
237 }
238
239 if (!is_valid) {
240 ast_log(LOG_ERROR, "Response validation failed for /deviceStates/{deviceName}\n");
241 ast_ari_response_error(response, 500,
242 "Internal Server Error", "Response validation failed");
243 }
244 #endif /* AST_DEVMODE */
245
246 fin: __attribute__((unused))
247 return;
248 }
249 /*!
250 * \brief Parameter parsing callback for /deviceStates/{deviceName}.
251 * \param ser TCP/TLS session object
252 * \param get_params GET parameters in the HTTP request.
253 * \param path_vars Path variables extracted from the request.
254 * \param headers HTTP headers.
255 * \param body
256 * \param[out] response Response to the HTTP request.
257 */
258 static void ast_ari_device_states_delete_cb(
259 struct ast_tcptls_session_instance *ser,
260 struct ast_variable *get_params, struct ast_variable *path_vars,
261 struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
262 {
263 struct ast_ari_device_states_delete_args args = {};
264 struct ast_variable *i;
265 #if defined(AST_DEVMODE)
266 int is_valid;
267 int code;
268 #endif /* AST_DEVMODE */
269
270 for (i = path_vars; i; i = i->next) {
271 if (strcmp(i->name, "deviceName") == 0) {
272 args.device_name = (i->value);
273 } else
274 {}
275 }
276 ast_ari_device_states_delete(headers, &args, response);
277 #if defined(AST_DEVMODE)
278 code = response->response_code;
279
280 switch (code) {
281 case 0: /* Implementation is still a stub, or the code wasn't set */
282 is_valid = response->message == NULL;
283 break;
284 case 500: /* Internal Server Error */
285 case 501: /* Not Implemented */
286 case 404: /* Device name is missing */
287 case 409: /* Uncontrolled device specified */
288 is_valid = 1;
289 break;
290 default:
291 if (200 <= code && code <= 299) {
292 is_valid = ast_ari_validate_void(
293 response->message);
294 } else {
295 ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates/{deviceName}\n", code);
296 is_valid = 0;
297 }
298 }
299
300 if (!is_valid) {
301 ast_log(LOG_ERROR, "Response validation failed for /deviceStates/{deviceName}\n");
302 ast_ari_response_error(response, 500,
303 "Internal Server Error", "Response validation failed");
304 }
305 #endif /* AST_DEVMODE */
306
307 fin: __attribute__((unused))
308 return;
309 }
310
311 /*! \brief REST handler for /api-docs/deviceStates.json */
312 static struct stasis_rest_handlers deviceStates_deviceName = {
313 .path_segment = "deviceName",
314 .is_wildcard = 1,
315 .callbacks = {
316 [AST_HTTP_GET] = ast_ari_device_states_get_cb,
317 [AST_HTTP_PUT] = ast_ari_device_states_update_cb,
318 [AST_HTTP_DELETE] = ast_ari_device_states_delete_cb,
319 },
320 .num_children = 0,
321 .children = { }
322 };
323 /*! \brief REST handler for /api-docs/deviceStates.json */
324 static struct stasis_rest_handlers deviceStates = {
325 .path_segment = "deviceStates",
326 .callbacks = {
327 [AST_HTTP_GET] = ast_ari_device_states_list_cb,
328 },
329 .num_children = 1,
330 .children = { &deviceStates_deviceName, }
331 };
332
333 static int unload_module(void)
334 {
335 ast_ari_remove_handler(&deviceStates);
336 return 0;
337 }
338
339 static int load_module(void)
340 {
341 int res = 0;
342
343
344 res |= ast_ari_add_handler(&deviceStates);
345 if (res) {
346 unload_module();
347 return AST_MODULE_LOAD_DECLINE;
348 }
349
350 return AST_MODULE_LOAD_SUCCESS;
351 }
352
353 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "RESTful API module - Device state resources",
354 .support_level = AST_MODULE_SUPPORT_CORE,
355 .load = load_module,
356 .unload = unload_module,
357 .requires = "res_ari,res_ari_model,res_stasis,res_stasis_device_state",
358 );