automotive-dlt
dlt_daemon_event_handler.c File Reference
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/epoll.h>
#include <sys/syslog.h>
#include "dlt_common.h"
#include "dlt-daemon.h"
#include "dlt-daemon_cfg.h"
#include "dlt_daemon_common.h"
#include "dlt_daemon_connection.h"
#include "dlt_daemon_connection_types.h"
#include "dlt_daemon_event_handler.h"
#include "dlt_daemon_event_handler_types.h"
Include dependency graph for dlt_daemon_event_handler.c:

Go to the source code of this file.

Macros

#define DLT_EPOLL_TIMEOUT_MSEC   1000
 

Functions

int dlt_daemon_prepare_event_handling (DltEventHandler *ev)
 Prepare the event handler. More...
 
int dlt_daemon_handle_event (DltEventHandler *pEvent, DltDaemon *daemon, DltDaemonLocal *daemon_local)
 Catch and process incoming events. More...
 
DltConnectiondlt_event_handler_find_connection (DltEventHandler *ev, int fd)
 Find connection with a specific fd in the connection list. More...
 
DltConnectiondlt_event_handler_find_connection_by_id (DltEventHandler *ev, DltConnectionId id)
 Find connection with a specific id in the connection list. More...
 
STATIC int dlt_daemon_remove_connection (DltEventHandler *ev, DltConnection *to_remove)
 Remove a connection from the list and destroy it. More...
 
void dlt_event_handler_cleanup_connections (DltEventHandler *ev)
 Destroy the connection list. More...
 
STATIC void dlt_daemon_add_connection (DltEventHandler *ev, DltConnection *connection)
 Add a new connection to the list. More...
 
int dlt_connection_check_activate (DltEventHandler *evhdl, DltConnection *con, int activation_type)
 Check for connection activation. More...
 
int dlt_event_handler_register_connection (DltEventHandler *evhdl, DltDaemonLocal *daemon_local, DltConnection *connection, int mask)
 Registers a connection for event handling and takes its ownership. More...
 
int dlt_event_handler_unregister_connection (DltEventHandler *evhdl, DltDaemonLocal *daemon_local, int fd)
 Unregisters a connection from the event handler and destroys it. More...
 

Detailed Description

Author
Frederic Berat fbera.nosp@m.t@de.nosp@m..adit.nosp@m.-jv..nosp@m.com

Definition in file dlt_daemon_event_handler.c.

Macro Definition Documentation

#define DLT_EPOLL_TIMEOUT_MSEC   1000

The maximum amount of time to wait for an epoll event. Set to 1 second to avoid unnecessary wake ups.

Definition at line 52 of file dlt_daemon_event_handler.c.

Referenced by dlt_daemon_handle_event().

Function Documentation

int dlt_connection_check_activate ( DltEventHandler evhdl,
DltConnection con,
int  activation_type 
)

Check for connection activation.

If the connection is active and it's not allowed anymore or it the user ask for deactivation, the connection will be deactivated. If the connection is inactive, the user asks for activation and it's allowed for it to be activated, the connection will be activated.

Parameters
evhdlThe event handler structure.
conThe connection to act on
activation_typeThe type of activation requested ((DE)ACTIVATE)
Returns
0 on success, -1 otherwise

Definition at line 335 of file dlt_daemon_event_handler.c.

References ACTIVATE, ACTIVE, DEACTIVATE, DLT_DAEMON_TEXTBUFSIZE, dlt_log(), DltEventHandler::epfd, DltConnection::ev_mask, DltReceiver::fd, DltConnection::id, INACTIVE, NULL, DltConnection::receiver, DltConnection::status, and DltConnection::type.

Referenced by dlt_event_handler_register_connection(), and dlt_event_handler_unregister_connection().

STATIC void dlt_daemon_add_connection ( DltEventHandler ev,
DltConnection connection 
)

Add a new connection to the list.

The connection is added at the tail of the list.

Parameters
evThe event handler structure where the connection list is.
connectionThe connection to be added.

Definition at line 309 of file dlt_daemon_event_handler.c.

References DltEventHandler::connections, DltConnection::next, and NULL.

Referenced by dlt_event_handler_register_connection().

int dlt_daemon_handle_event ( DltEventHandler pEvent,
DltDaemon daemon,
DltDaemonLocal daemon_local 
)

Catch and process incoming events.

This function waits for events on all connections. Once an event raise, the callback for the specific connection is called, or the connection is destroyed if a hangup occurs.

Parameters
daemonStructure to be passed to the callback.
daemon_localStructure containing needed information.
pEventEvent handler structure.
Returns
0 on success, -1 otherwise. May be interrupted.

Definition at line 91 of file dlt_daemon_event_handler.c.

References dlt_connection_get_callback(), DLT_CONNECTION_TYPE_MAX, DLT_DAEMON_TEXTBUFSIZE, DLT_EPOLL_MAX_EVENTS, DLT_EPOLL_TIMEOUT_MSEC, dlt_event_handler_find_connection_by_id(), dlt_event_handler_unregister_connection(), dlt_log(), DLT_RETURN_ERROR, DltEventHandler::epfd, DltEventHandler::events, DltDaemonLocal::flags, NULL, str, and DltDaemonFlags::vflag.

Referenced by main().

int dlt_daemon_prepare_event_handling ( DltEventHandler ev)

Prepare the event handler.

This will create the epoll file descriptor.

Parameters
evThe event handler to prepare.
Returns
0 on success, -1 otherwise.

Definition at line 62 of file dlt_daemon_event_handler.c.

References DLT_EPOLL_MAX_EVENTS, dlt_log(), DLT_RETURN_ERROR, DltEventHandler::epfd, and NULL.

Referenced by main().

STATIC int dlt_daemon_remove_connection ( DltEventHandler ev,
DltConnection to_remove 
)

Remove a connection from the list and destroy it.

This function will first look for the connection in the event handler list, remove it from the list and then destroy it.

Parameters
evThe event handler structure where the list of connection is.
to_removeThe connection to remove from the list.
Returns
0 on success, -1 if the connection is not found.

Definition at line 250 of file dlt_daemon_event_handler.c.

References DltEventHandler::connections, dlt_connection_destroy(), dlt_log(), DLT_RETURN_ERROR, DltConnection::next, and NULL.

Referenced by dlt_event_handler_cleanup_connections(), and dlt_event_handler_unregister_connection().

void dlt_event_handler_cleanup_connections ( DltEventHandler ev)

Destroy the connection list.

This function runs through the connection list and destroy them one by one.

Parameters
evPointer to the event handler structure.

Definition at line 287 of file dlt_daemon_event_handler.c.

References DltEventHandler::connections, dlt_daemon_remove_connection(), and NULL.

Referenced by dlt_daemon_local_cleanup().

DltConnection* dlt_event_handler_find_connection ( DltEventHandler ev,
int  fd 
)

Find connection with a specific fd in the connection list.

There can be only one event per fd. We can then find a specific connection based on this fd. That allows to check if a specific fd has already been registered.

Parameters
evThe event handler structure where the list of connection is.
fdThe file descriptor of the connection to be found.
Returns
The found connection pointer, NULL otherwise.

Definition at line 201 of file dlt_daemon_event_handler.c.

References DltEventHandler::connections, DltReceiver::fd, DltConnection::next, NULL, and DltConnection::receiver.

Referenced by dlt_connection_create(), and dlt_event_handler_unregister_connection().

DltConnection* dlt_event_handler_find_connection_by_id ( DltEventHandler ev,
DltConnectionId  id 
)

Find connection with a specific id in the connection list.

There can be only one event per fd. We can then find a specific connection based on this fd. That allows to check if a specific fd has already been registered.

Parameters
evThe event handler structure where the list of connection is.
idThe identifier of the connection to be found.
Returns
The found connection pointer, NULL otherwise.

Definition at line 226 of file dlt_daemon_event_handler.c.

References DltEventHandler::connections, DltConnection::id, DltConnection::next, and NULL.

Referenced by dlt_daemon_handle_event().

int dlt_event_handler_register_connection ( DltEventHandler evhdl,
DltDaemonLocal daemon_local,
DltConnection connection,
int  mask 
)

Registers a connection for event handling and takes its ownership.

As we add the connection to the list of connection, we take its ownership. That's the only place where the connection pointer is stored. The connection is then used to create a new event trigger. If the connection is of type DLT_CONNECTION_CLIENT_MSG_TCP, we increase the daemon_local->client_connections counter. TODO: Move this counter inside the event handler structure.

Parameters
evhdlThe event handler structure where the connection list is.
daemon_localStructure containing needed information.
connectionThe connection to be registered.
maskThe bit mask of event to be registered.
Returns
0 on success, -1 otherwise.

Definition at line 429 of file dlt_daemon_event_handler.c.

References ACTIVATE, DltDaemonLocal::client_connections, dlt_connection_check_activate(), DLT_CONNECTION_CLIENT_MSG_SERIAL, DLT_CONNECTION_CLIENT_MSG_TCP, dlt_daemon_add_connection(), dlt_log(), DltConnection::ev_mask, INACTIVE, DltConnection::next, NULL, DltConnection::receiver, DltConnection::status, and DltConnection::type.

Referenced by dlt_connection_create().

int dlt_event_handler_unregister_connection ( DltEventHandler evhdl,
DltDaemonLocal daemon_local,
int  fd 
)

Unregisters a connection from the event handler and destroys it.

We first look for the connection to be unregistered, delete the event corresponding and then destroy the connection. If the connection is of type DLT_CONNECTION_CLIENT_MSG_TCP, we decrease the daemon_local->client_connections counter. TODO: Move this counter inside the event handler structure.

Parameters
evhdlThe event handler structure where the connection list is.
daemon_localStructure containing needed information.
fdThe file descriptor of the connection to be unregistered.
Returns
0 on success, -1 otherwise.

Definition at line 472 of file dlt_daemon_event_handler.c.

References DltDaemonLocal::client_connections, DEACTIVATE, dlt_connection_check_activate(), DLT_CONNECTION_CLIENT_MSG_SERIAL, DLT_CONNECTION_CLIENT_MSG_TCP, dlt_daemon_remove_connection(), dlt_event_handler_find_connection(), dlt_log(), DLT_RETURN_ERROR, NULL, and DltConnection::type.

Referenced by dlt_daemon_close_socket(), dlt_daemon_handle_event(), dlt_gateway_process_on_demand_request(), and dlt_gateway_process_passive_node_messages().