Asterisk - The Open Source Telephony Project  21.4.1
res_ari_device_states.c
Go to the documentation of this file.
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 /*
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 <kharwell@digium.com>
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"
48 #if defined(AST_DEVMODE)
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  */
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,
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  */
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) {
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 }
166  struct ast_json *body,
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  */
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)) {
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  */
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 */
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 */
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();
348  }
349 
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 );
struct ast_variable * next
Asterisk main include file. File version handling, generic pbx functions.
static void ast_ari_device_states_update_cb(struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Parameter parsing callback for /deviceStates/{deviceName}.
void ast_ari_device_states_update(struct ast_variable *headers, struct ast_ari_device_states_update_args *args, struct ast_ari_response *response)
Change the state of a device controlled by ARI. (Note - implicitly creates the device state)...
Structure for variables, used for configurations and for channel variables.
Generated file - Build validators for ARI model objects.
void ast_ari_response_alloc_failed(struct ast_ari_response *response)
Fill in response with a 500 message for allocation failures.
Definition: res_ari.c:298
int ast_ari_validate_void(struct ast_json *json)
Validator for native Swagger void.
Definition: res_ari_model.c:91
static void ast_ari_device_states_delete_cb(struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Parameter parsing callback for /deviceStates/{deviceName}.
int response_code
Definition: ari.h:99
static void ast_ari_device_states_get_cb(struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Parameter parsing callback for /deviceStates/{deviceName}.
static void ast_ari_device_states_list_cb(struct ast_tcptls_session_instance *ser, struct ast_variable *get_params, struct ast_variable *path_vars, struct ast_variable *headers, struct ast_json *body, struct ast_ari_response *response)
Parameter parsing callback for /deviceStates.
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
int ast_ari_device_states_update_parse_body(struct ast_json *body, struct ast_ari_device_states_update_args *args)
Body parsing function for /deviceStates/{deviceName}.
describes a server instance
Definition: tcptls.h:150
int ast_ari_add_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:179
Generated file - declares stubs to be implemented in res/ari/resource_deviceStates.c.
int ast_ari_remove_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:202
static struct stasis_rest_handlers deviceStates_deviceName
REST handler for /api-docs/deviceStates.json.
void ast_ari_response_error(struct ast_ari_response *response, int response_code, const char *response_text, const char *message_fmt,...)
Fill in an error ast_ari_response.
Definition: res_ari.c:259
void ast_ari_device_states_delete(struct ast_variable *headers, struct ast_ari_device_states_delete_args *args, struct ast_ari_response *response)
Destroy a device-state controlled by ARI.
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
struct ast_json * message
Definition: ari.h:94
const char * path_segment
Definition: ari.h:71
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:407
void ast_ari_device_states_get(struct ast_variable *headers, struct ast_ari_device_states_get_args *args, struct ast_ari_response *response)
Retrieve the current state of a device.
ari_validator ast_ari_validate_device_state_fn(void)
Function pointer to ast_ari_validate_device_state().
Abstract JSON element (object, array, string, int, ...).
Stasis Application API. See Stasis Application API for detailed documentation.
int ast_ari_validate_list(struct ast_json *json, int(*fn)(struct ast_json *))
Validator for a Swagger List[]/JSON array.
void ast_ari_device_states_list(struct ast_variable *headers, struct ast_ari_device_states_list_args *args, struct ast_ari_response *response)
List all ARI controlled device states.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
int ast_ari_validate_device_state(struct ast_json *json)
Validator for DeviceState.
Application convenience functions, designed to give consistent look and feel to Asterisk apps...
Handler for a single RESTful path segment.
Definition: ari.h:69
static struct stasis_rest_handlers deviceStates
REST handler for /api-docs/deviceStates.json.