Asterisk - The Open Source Telephony Project  21.4.1
Macros | Functions | Variables
res_ari_device_states.c File Reference

Device state resources. More...

#include "asterisk.h"
#include "asterisk/app.h"
#include "asterisk/module.h"
#include "asterisk/stasis_app.h"
#include "ari/resource_device_states.h"

Go to the source code of this file.

Macros

#define MAX_VALS   128
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
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}. More...
 
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}. More...
 
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. More...
 
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}. More...
 
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}. More...
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_DEFAULT , .description = "RESTful API module - Device state resources" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "da6642af068ee5e6490c5b1d2cc1d238" , .support_level = AST_MODULE_SUPPORT_CORE, .load = load_module, .unload = unload_module, .requires = "res_ari,res_ari_model,res_stasis,res_stasis_device_state", }
 
static const struct ast_module_infoast_module_info = &__mod_info
 
static struct stasis_rest_handlers deviceStates
 REST handler for /api-docs/deviceStates.json.
 
static struct stasis_rest_handlers deviceStates_deviceName
 REST handler for /api-docs/deviceStates.json.
 

Detailed Description

Device state resources.

Author
Kevin Harwell kharw.nosp@m.ell@.nosp@m.digiu.nosp@m.m.co.nosp@m.m

Definition in file res_ari_device_states.c.

Function Documentation

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 
)
static

Parameter parsing callback for /deviceStates/{deviceName}.

Parameters
serTCP/TLS session object
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
body
[out]responseResponse to the HTTP request.

Definition at line 258 of file res_ari_device_states.c.

References ast_ari_device_states_delete(), ast_ari_response_error(), ast_ari_validate_void(), ast_ari_device_states_delete_args::device_name, ast_ari_response::message, ast_variable::name, ast_variable::next, ast_ari_response::response_code, and ast_variable::value.

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 }
struct ast_variable * next
Structure for variables, used for configurations and for channel variables.
int ast_ari_validate_void(struct ast_json *json)
Validator for native Swagger void.
Definition: res_ari_model.c:91
int response_code
Definition: ari.h:99
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.
struct ast_json * message
Definition: ari.h:94
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 
)
static

Parameter parsing callback for /deviceStates/{deviceName}.

Parameters
serTCP/TLS session object
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
body
[out]responseResponse to the HTTP request.

Definition at line 115 of file res_ari_device_states.c.

References ast_ari_device_states_get(), ast_ari_response_error(), ast_ari_validate_device_state(), ast_ari_device_states_get_args::device_name, ast_ari_response::message, ast_variable::name, ast_variable::next, ast_ari_response::response_code, and ast_variable::value.

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 }
struct ast_variable * next
Structure for variables, used for configurations and for channel variables.
int response_code
Definition: ari.h:99
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
struct ast_json * message
Definition: ari.h:94
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.
int ast_ari_validate_device_state(struct ast_json *json)
Validator for DeviceState.
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 
)
static

Parameter parsing callback for /deviceStates.

Parameters
serTCP/TLS session object
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
body
[out]responseResponse to the HTTP request.

Definition at line 63 of file res_ari_device_states.c.

References ast_ari_device_states_list(), ast_ari_response_error(), ast_ari_validate_device_state_fn(), ast_ari_validate_list(), ast_ari_response::message, and ast_ari_response::response_code.

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 }
int response_code
Definition: ari.h:99
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
struct ast_json * message
Definition: ari.h:94
ari_validator ast_ari_validate_device_state_fn(void)
Function pointer to ast_ari_validate_device_state().
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.
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 
)
static

Parameter parsing callback for /deviceStates/{deviceName}.

Parameters
serTCP/TLS session object
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
body
[out]responseResponse to the HTTP request.

Definition at line 187 of file res_ari_device_states.c.

References ast_ari_device_states_update(), ast_ari_device_states_update_parse_body(), ast_ari_response_alloc_failed(), ast_ari_response_error(), ast_ari_validate_void(), ast_ari_device_states_update_args::device_name, ast_ari_device_states_update_args::device_state, ast_ari_response::message, ast_variable::name, ast_variable::next, ast_ari_response::response_code, and ast_variable::value.

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 }
struct ast_variable * next
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.
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
int response_code
Definition: ari.h:99
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}.
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
struct ast_json * message
Definition: ari.h:94
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}.

Parameters
bodyThe JSON body from which to parse parameters.
[out]argsThe args structure to parse into.
Return values
zeroon success
non-zeroon failure

Definition at line 165 of file res_ari_device_states.c.

References ast_json_object_get(), ast_json_string_get(), and ast_ari_device_states_update_args::device_state.

Referenced by ast_ari_device_states_update_cb().

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 }
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:283
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
Abstract JSON element (object, array, string, int, ...).