Asterisk - The Open Source Telephony Project  18.5.0
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"
#include "ari/ari_model_validators.h"
Include dependency graph for res_ari_device_states.c:

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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .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. More...
 
static struct stasis_rest_handlers deviceStates_deviceName
 REST handler for /api-docs/deviceStates.json. More...
 

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.

Macro Definition Documentation

◆ MAX_VALS

#define MAX_VALS   128

Definition at line 52 of file res_ari_device_states.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 350 of file res_ari_device_states.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 350 of file res_ari_device_states.c.

◆ ast_ari_device_states_delete_cb()

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
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
[out]responseResponse to the HTTP request.

Definition at line 250 of file res_ari_device_states.c.

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

254 {
256  struct ast_variable *i;
257 #if defined(AST_DEVMODE)
258  int is_valid;
259  int code;
260 #endif /* AST_DEVMODE */
261 
262  for (i = path_vars; i; i = i->next) {
263  if (strcmp(i->name, "deviceName") == 0) {
264  args.device_name = (i->value);
265  } else
266  {}
267  }
268  ast_ari_device_states_delete(headers, &args, response);
269 #if defined(AST_DEVMODE)
270  code = response->response_code;
271 
272  switch (code) {
273  case 0: /* Implementation is still a stub, or the code wasn't set */
274  is_valid = response->message == NULL;
275  break;
276  case 500: /* Internal Server Error */
277  case 501: /* Not Implemented */
278  case 404: /* Device name is missing */
279  case 409: /* Uncontrolled device specified */
280  is_valid = 1;
281  break;
282  default:
283  if (200 <= code && code <= 299) {
284  is_valid = ast_ari_validate_void(
285  response->message);
286  } else {
287  ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates/{deviceName}\n", code);
288  is_valid = 0;
289  }
290  }
291 
292  if (!is_valid) {
293  ast_log(LOG_ERROR, "Response validation failed for /deviceStates/{deviceName}\n");
294  ast_ari_response_error(response, 500,
295  "Internal Server Error", "Response validation failed");
296  }
297 #endif /* AST_DEVMODE */
298 
299 fin: __attribute__((unused))
300  return;
301 }
struct ast_variable * next
Structure for variables, used for configurations and for channel variables.
const char * args
#define NULL
Definition: resample.c:96
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:98
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
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:93

◆ ast_ari_device_states_get_cb()

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
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
[out]responseResponse to the HTTP request.

Definition at line 111 of file res_ari_device_states.c.

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

115 {
117  struct ast_variable *i;
118 #if defined(AST_DEVMODE)
119  int is_valid;
120  int code;
121 #endif /* AST_DEVMODE */
122 
123  for (i = path_vars; i; i = i->next) {
124  if (strcmp(i->name, "deviceName") == 0) {
125  args.device_name = (i->value);
126  } else
127  {}
128  }
129  ast_ari_device_states_get(headers, &args, response);
130 #if defined(AST_DEVMODE)
131  code = response->response_code;
132 
133  switch (code) {
134  case 0: /* Implementation is still a stub, or the code wasn't set */
135  is_valid = response->message == NULL;
136  break;
137  case 500: /* Internal Server Error */
138  case 501: /* Not Implemented */
139  is_valid = 1;
140  break;
141  default:
142  if (200 <= code && code <= 299) {
144  response->message);
145  } else {
146  ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates/{deviceName}\n", code);
147  is_valid = 0;
148  }
149  }
150 
151  if (!is_valid) {
152  ast_log(LOG_ERROR, "Response validation failed for /deviceStates/{deviceName}\n");
153  ast_ari_response_error(response, 500,
154  "Internal Server Error", "Response validation failed");
155  }
156 #endif /* AST_DEVMODE */
157 
158 fin: __attribute__((unused))
159  return;
160 }
struct ast_variable * next
Structure for variables, used for configurations and for channel variables.
const char * args
#define NULL
Definition: resample.c:96
int response_code
Definition: ari.h:98
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
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:93
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.

◆ ast_ari_device_states_list_cb()

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
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
[out]responseResponse to the HTTP request.

Definition at line 61 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_log, LOG_ERROR, ast_ari_response::message, NULL, and ast_ari_response::response_code.

65 {
67 #if defined(AST_DEVMODE)
68  int is_valid;
69  int code;
70 #endif /* AST_DEVMODE */
71 
72  ast_ari_device_states_list(headers, &args, response);
73 #if defined(AST_DEVMODE)
74  code = response->response_code;
75 
76  switch (code) {
77  case 0: /* Implementation is still a stub, or the code wasn't set */
78  is_valid = response->message == NULL;
79  break;
80  case 500: /* Internal Server Error */
81  case 501: /* Not Implemented */
82  is_valid = 1;
83  break;
84  default:
85  if (200 <= code && code <= 299) {
86  is_valid = ast_ari_validate_list(response->message,
88  } else {
89  ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates\n", code);
90  is_valid = 0;
91  }
92  }
93 
94  if (!is_valid) {
95  ast_log(LOG_ERROR, "Response validation failed for /deviceStates\n");
96  ast_ari_response_error(response, 500,
97  "Internal Server Error", "Response validation failed");
98  }
99 #endif /* AST_DEVMODE */
100 
101 fin: __attribute__((unused))
102  return;
103 }
const char * args
#define NULL
Definition: resample.c:96
int response_code
Definition: ari.h:98
#define ast_log
Definition: astobj2.c:42
#define LOG_ERROR
Definition: logger.h:285
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:93
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.

◆ ast_ari_device_states_update_cb()

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
get_paramsGET parameters in the HTTP request.
path_varsPath variables extracted from the request.
headersHTTP headers.
[out]responseResponse to the HTTP request.

Definition at line 181 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_log, ast_ari_device_states_update_args::device_name, ast_ari_device_states_update_args::device_state, LOG_ERROR, ast_ari_response::message, ast_variable::name, ast_variable::next, NULL, ast_ari_response::response_code, and ast_variable::value.

185 {
187  struct ast_variable *i;
188 #if defined(AST_DEVMODE)
189  int is_valid;
190  int code;
191 #endif /* AST_DEVMODE */
192 
193  for (i = get_params; i; i = i->next) {
194  if (strcmp(i->name, "deviceState") == 0) {
195  args.device_state = (i->value);
196  } else
197  {}
198  }
199  for (i = path_vars; i; i = i->next) {
200  if (strcmp(i->name, "deviceName") == 0) {
201  args.device_name = (i->value);
202  } else
203  {}
204  }
205  if (ast_ari_device_states_update_parse_body(body, &args)) {
207  goto fin;
208  }
209  ast_ari_device_states_update(headers, &args, response);
210 #if defined(AST_DEVMODE)
211  code = response->response_code;
212 
213  switch (code) {
214  case 0: /* Implementation is still a stub, or the code wasn't set */
215  is_valid = response->message == NULL;
216  break;
217  case 500: /* Internal Server Error */
218  case 501: /* Not Implemented */
219  case 404: /* Device name is missing */
220  case 409: /* Uncontrolled device specified */
221  is_valid = 1;
222  break;
223  default:
224  if (200 <= code && code <= 299) {
225  is_valid = ast_ari_validate_void(
226  response->message);
227  } else {
228  ast_log(LOG_ERROR, "Invalid error response %d for /deviceStates/{deviceName}\n", code);
229  is_valid = 0;
230  }
231  }
232 
233  if (!is_valid) {
234  ast_log(LOG_ERROR, "Response validation failed for /deviceStates/{deviceName}\n");
235  ast_ari_response_error(response, 500,
236  "Internal Server Error", "Response validation failed");
237  }
238 #endif /* AST_DEVMODE */
239 
240 fin: __attribute__((unused))
241  return;
242 }
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.
const char * args
#define NULL
Definition: resample.c:96
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:98
#define ast_log
Definition: astobj2.c:42
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}.
#define LOG_ERROR
Definition: logger.h:285
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:93

◆ ast_ari_device_states_update_parse_body()

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 161 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().

164 {
165  struct ast_json *field;
166  /* Parse query parameters out of it */
167  field = ast_json_object_get(body, "deviceState");
168  if (field) {
169  args->device_state = ast_json_string_get(field);
170  }
171  return 0;
172 }
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
struct ast_json * ast_json_object_get(struct ast_json *object, const char *key)
Get a field from a JSON object.
Definition: json.c:397
Abstract JSON element (object, array, string, int, ...).

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 350 of file res_ari_device_states.c.

◆ load_module()

static int load_module ( void  )
static

Definition at line 331 of file res_ari_device_states.c.

References ast_ari_add_handler(), AST_MODFLAG_DEFAULT, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_CORE, ASTERISK_GPL_KEY, and unload_module().

332 {
333  int res = 0;
334 
335 
337  if (res) {
338  unload_module();
340  }
341 
343 }
int ast_ari_add_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:179
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static int unload_module(void)
static struct stasis_rest_handlers deviceStates
REST handler for /api-docs/deviceStates.json.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 325 of file res_ari_device_states.c.

References ast_ari_remove_handler().

Referenced by load_module().

326 {
328  return 0;
329 }
int ast_ari_remove_handler(struct stasis_rest_handlers *handler)
Definition: res_ari.c:202
static struct stasis_rest_handlers deviceStates
REST handler for /api-docs/deviceStates.json.

Variable Documentation

◆ __mod_info

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 = "30ef0c93b36035ec78c9cfd712d36d9b" , .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

Definition at line 350 of file res_ari_device_states.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 350 of file res_ari_device_states.c.

◆ deviceStates

struct stasis_rest_handlers deviceStates
static

REST handler for /api-docs/deviceStates.json.

Definition at line 316 of file res_ari_device_states.c.

◆ deviceStates_deviceName

struct stasis_rest_handlers deviceStates_deviceName
static

REST handler for /api-docs/deviceStates.json.

Definition at line 304 of file res_ari_device_states.c.