Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Functions
resource_events.h File Reference

Generated file - declares stubs to be implemented in res/ari/resource_events.c. More...

#include "asterisk/ari.h"
Include dependency graph for resource_events.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ast_ari_events_event_websocket_args
 
struct  ast_ari_events_user_event_args
 

Functions

void ast_ari_events_user_event (struct ast_variable *headers, struct ast_ari_events_user_event_args *args, struct ast_ari_response *response)
 Generate a user event. More...
 
int ast_ari_events_user_event_parse_body (struct ast_json *body, struct ast_ari_events_user_event_args *args)
 Body parsing function for /events/user/{eventName}. More...
 
int ast_ari_websocket_events_event_websocket_attempted (struct ast_tcptls_session_instance *ser, struct ast_variable *headers, struct ast_ari_events_event_websocket_args *args, const char *session_id)
 WebSocket connection for events. More...
 
void ast_ari_websocket_events_event_websocket_dtor (void)
 WebSocket connection for events. More...
 
void ast_ari_websocket_events_event_websocket_established (struct ast_ari_websocket_session *session, struct ast_variable *headers, struct ast_ari_events_event_websocket_args *args)
 WebSocket connection for events. More...
 
int ast_ari_websocket_events_event_websocket_init (void)
 WebSocket connection for events. More...
 

Detailed Description

Generated file - declares stubs to be implemented in res/ari/resource_events.c.

WebSocket resource

Author
David M. Lee, II dlee@.nosp@m.digi.nosp@m.um.co.nosp@m.m

Definition in file resource_events.h.

Function Documentation

◆ ast_ari_events_user_event()

void ast_ari_events_user_event ( struct ast_variable headers,
struct ast_ari_events_user_event_args args,
struct ast_ari_response response 
)

Generate a user event.

Parameters
headersHTTP headers
argsSwagger parameters
[out]responseHTTP response

Definition at line 532 of file resource_events.c.

References ast_ari_events_user_event_args::application, ast_ari_events_user_event_parse_body(), ast_ari_response_error(), ast_ari_response_no_content(), ast_json_object_get(), ast_strlen_zero, ast_ari_events_user_event_args::event_name, NULL, ast_ari_events_user_event_args::source, ast_ari_events_user_event_args::source_count, STASIS_APP_USER_APP_NOT_FOUND, stasis_app_user_event(), STASIS_APP_USER_EVENT_SOURCE_BAD_SCHEME, STASIS_APP_USER_EVENT_SOURCE_NOT_FOUND, STASIS_APP_USER_INTERNAL_ERROR, STASIS_APP_USER_OK, STASIS_APP_USER_USEREVENT_INVALID, and ast_ari_events_user_event_args::variables.

Referenced by ast_ari_events_user_event_cb().

535 {
536  enum stasis_app_user_event_res res;
537  struct ast_json *json_variables = NULL;
538 
539  if (args->variables) {
541  json_variables = ast_json_object_get(args->variables, "variables");
542  }
543 
544  if (ast_strlen_zero(args->application)) {
545  ast_ari_response_error(response, 400, "Bad Request",
546  "Missing parameter application");
547  return;
548  }
549 
551  args->event_name,
552  args->source, args->source_count,
553  json_variables);
554 
555  switch (res) {
556  case STASIS_APP_USER_OK:
557  ast_ari_response_no_content(response);
558  break;
559 
561  ast_ari_response_error(response, 404, "Not Found",
562  "Application not found");
563  break;
564 
566  ast_ari_response_error(response, 422, "Unprocessable Entity",
567  "Event source was not found");
568  break;
569 
571  ast_ari_response_error(response, 400, "Bad Request",
572  "Invalid event source URI scheme");
573  break;
574 
576  ast_ari_response_error(response, 400, "Bad Request",
577  "Invalid userevnet data");
578  break;
579 
581  default:
582  ast_ari_response_error(response, 500, "Internal Server Error",
583  "Error processing request");
584  }
585 }
#define NULL
Definition: resample.c:96
#define ast_strlen_zero(foo)
Definition: strings.h:52
int ast_ari_events_user_event_parse_body(struct ast_json *body, struct ast_ari_events_user_event_args *args)
Body parsing function for /events/user/{eventName}.
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_response_no_content(struct ast_ari_response *response)
Fill in a No Content (204) ast_ari_response.
Definition: res_ari.c:284
stasis_app_user_event_res
Return code for stasis_app_user_event.
Definition: stasis_app.h:255
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, ...).
enum stasis_app_user_event_res stasis_app_user_event(const char *app_name, const char *event_name, const char **source_uris, int sources_count, struct ast_json *json_variables)
Generate a Userevent for stasis app (echo to AMI)
Definition: res_stasis.c:2095

◆ ast_ari_events_user_event_parse_body()

int ast_ari_events_user_event_parse_body ( struct ast_json body,
struct ast_ari_events_user_event_args args 
)

Body parsing function for /events/user/{eventName}.

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 241 of file res_ari_events.c.

References ast_ari_events_user_event_args::application, ast_free, AST_JSON_ARRAY, ast_json_array_get(), ast_json_array_size(), ast_json_object_get(), ast_json_string_get(), ast_json_typeof(), ast_malloc, ast_ari_events_user_event_args::source, and ast_ari_events_user_event_args::source_count.

Referenced by ast_ari_events_user_event().

244 {
245  struct ast_json *field;
246  /* Parse query parameters out of it */
247  field = ast_json_object_get(body, "application");
248  if (field) {
249  args->application = ast_json_string_get(field);
250  }
251  field = ast_json_object_get(body, "source");
252  if (field) {
253  /* If they were silly enough to both pass in a query param and a
254  * JSON body, free up the query value.
255  */
256  ast_free(args->source);
257  if (ast_json_typeof(field) == AST_JSON_ARRAY) {
258  /* Multiple param passed as array */
259  size_t i;
260  args->source_count = ast_json_array_size(field);
261  args->source = ast_malloc(sizeof(*args->source) * args->source_count);
262 
263  if (!args->source) {
264  return -1;
265  }
266 
267  for (i = 0; i < args->source_count; ++i) {
268  args->source[i] = ast_json_string_get(ast_json_array_get(field, i));
269  }
270  } else {
271  /* Multiple param passed as single value */
272  args->source_count = 1;
273  args->source = ast_malloc(sizeof(*args->source) * args->source_count);
274  if (!args->source) {
275  return -1;
276  }
277  args->source[0] = ast_json_string_get(field);
278  }
279  }
280  return 0;
281 }
const char * ast_json_string_get(const struct ast_json *string)
Get the value of a JSON string.
Definition: json.c:273
#define ast_malloc(len)
A wrapper for malloc()
Definition: astmm.h:193
#define ast_free(a)
Definition: astmm.h:182
enum ast_json_type ast_json_typeof(const struct ast_json *value)
Get the type of value.
Definition: json.c:78
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
size_t ast_json_array_size(const struct ast_json *array)
Get the size of a JSON array.
Definition: json.c:356
Abstract JSON element (object, array, string, int, ...).
struct ast_json * ast_json_array_get(const struct ast_json *array, size_t index)
Get an element from an array.
Definition: json.c:360

◆ ast_ari_websocket_events_event_websocket_attempted()

int ast_ari_websocket_events_event_websocket_attempted ( struct ast_tcptls_session_instance ser,
struct ast_variable headers,
struct ast_ari_events_event_websocket_args args,
const char *  session_id 
)

WebSocket connection for events.

Parameters
serHTTP TCP/TLS Server Session
headersHTTP headers
argsSwagger parameters
session_idThe id of the current session.
Return values
0success
non-zeroerror

Definition at line 488 of file resource_events.c.

References ast_debug, and event_session_alloc().

Referenced by ast_ari_events_event_websocket_ws_attempted_cb().

491 {
492  ast_debug(3, "/events WebSocket attempted\n");
493 
494  /* Create the event session */
495  return event_session_alloc(ser, args, session_id);
496 }
static int event_session_alloc(struct ast_tcptls_session_instance *ser, struct ast_ari_events_event_websocket_args *args, const char *session_id)
Creates an event_session object and registers its apps with Stasis.
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452

◆ ast_ari_websocket_events_event_websocket_dtor()

void ast_ari_websocket_events_event_websocket_dtor ( void  )

WebSocket connection for events.

Returns
Nothing

Definition at line 465 of file resource_events.c.

References ao2_callback, ao2_cleanup, event_session_shutdown_cb(), NULL, OBJ_MULTIPLE, and OBJ_NODATA.

Referenced by load_module(), and unload_module().

466 {
468 
471 }
#define ao2_callback(c, flags, cb_fn, arg)
Definition: astobj2.h:1716
#define NULL
Definition: resample.c:96
static int event_session_shutdown_cb(void *session, void *arg, int flags)
#define ao2_cleanup(obj)
Definition: astobj2.h:1958
static struct ao2_container * event_session_registry
Local registry for created event_session objects.

◆ ast_ari_websocket_events_event_websocket_established()

void ast_ari_websocket_events_event_websocket_established ( struct ast_ari_websocket_session session,
struct ast_variable headers,
struct ast_ari_events_event_websocket_args args 
)

WebSocket connection for events.

Parameters
sessionARI WebSocket.
headersHTTP headers.
argsSwagger parameters.
session_idThe id of the current session.

Definition at line 498 of file resource_events.c.

References ao2_find, ao2_ref, ao2_unlink, ast_ari_websocket_session_id(), ast_ari_websocket_session_read(), ast_assert, ast_debug, ast_json_unref(), ast_log, event_session_cleanup(), event_session_update_websocket(), LOG_WARNING, NULL, OBJ_SEARCH_KEY, and session.

Referenced by ast_ari_events_event_websocket_ws_established_cb().

501 {
502  struct event_session *session;
503 
504  struct ast_json *msg;
505  const char *session_id;
506 
507  ast_debug(3, "/events WebSocket established\n");
508 
509  ast_assert(ws_session != NULL);
510 
511  session_id = ast_ari_websocket_session_id(ws_session);
512 
513  /* Find the event_session and update its websocket */
514  session = ao2_find(event_session_registry, session_id, OBJ_SEARCH_KEY);
515  if (session) {
517  event_session_update_websocket(session, ws_session);
518  } else {
520  "Failed to locate an event session for the provided websocket session\n");
521  }
522 
523  /* We don't process any input, but we'll consume it waiting for EOF */
524  while ((msg = ast_ari_websocket_session_read(ws_session))) {
525  ast_json_unref(msg);
526  }
527 
528  event_session_cleanup(session);
529  ao2_ref(session, -1);
530 }
const char * ast_ari_websocket_session_id(const struct ast_ari_websocket_session *session)
Get the Session ID for an ARI WebSocket.
The arg parameter is a search key, but is not an object.
Definition: astobj2.h:1105
void ast_json_unref(struct ast_json *value)
Decrease refcount on value. If refcount reaches zero, value is freed.
Definition: json.c:73
#define LOG_WARNING
Definition: logger.h:274
#define ast_assert(a)
Definition: utils.h:695
struct ast_json * ast_ari_websocket_session_read(struct ast_ari_websocket_session *session)
Read a message from an ARI WebSocket.
#define NULL
Definition: resample.c:96
static void event_session_cleanup(struct event_session *session)
Processes cleanup actions for a event_session object.
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
static struct ast_mansession session
static void event_session_update_websocket(struct event_session *session, struct ast_ari_websocket_session *ws_session)
Updates the websocket session for an event_session.
A wrapper for the /ref ast_ari_websocket_session.
#define ao2_ref(o, delta)
Definition: astobj2.h:464
#define ao2_unlink(container, obj)
Definition: astobj2.h:1598
#define ao2_find(container, arg, flags)
Definition: astobj2.h:1756
Abstract JSON element (object, array, string, int, ...).
static struct ao2_container * event_session_registry
Local registry for created event_session objects.

◆ ast_ari_websocket_events_event_websocket_init()

int ast_ari_websocket_events_event_websocket_init ( void  )

WebSocket connection for events.

Return values
0success
-1error

Definition at line 473 of file resource_events.c.

References AO2_ALLOC_OPT_LOCK_MUTEX, ao2_container_alloc_hash, ast_log, event_session_compare(), event_session_hash(), EVENT_SESSION_NUM_BUCKETS, LOG_WARNING, and NULL.

Referenced by load_module().

474 {
475  /* Try to instantiate the registry */
478  if (!event_session_registry) {
479  /* This is bad, bad. */
481  "Failed to allocate the local registry for websocket applications\n");
482  return -1;
483  }
484 
485  return 0;
486 }
#define LOG_WARNING
Definition: logger.h:274
#define NULL
Definition: resample.c:96
#define ast_log
Definition: astobj2.c:42
#define EVENT_SESSION_NUM_BUCKETS
#define ao2_container_alloc_hash(ao2_options, container_options, n_buckets, hash_fn, sort_fn, cmp_fn)
Definition: astobj2.h:1310
static int event_session_hash(const void *obj, const int flags)
AO2 hash function for event_session objects.
static int event_session_compare(void *obj, void *arg, int flags)
AO2 comparison function for event_session objects.
static struct ao2_container * event_session_registry
Local registry for created event_session objects.