ble_gatt_client
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
mainloop.c File Reference

set of function to manage the epool loop More...

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/signalfd.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>
#include "mainloop.h"

Go to the source code of this file.

Data Structures

struct  mainloop_data
 mainloop file descriptor event data structure More...
 
struct  timeout_data
 
struct  signal_data
 

Macros

#define MAX_EPOLL_EVENTS   10
 
#define MAX_MAINLOOP_ENTRIES   128
 

Functions

void mainloop_init (void)
 
void mainloop_quit (void)
 
void mainloop_exit_success (void)
 
void mainloop_exit_failure (void)
 
static void signal_callback (int fd, uint32_t events, void *user_data)
 
int mainloop_run (void)
 
int mainloop_add_fd (int fd, uint32_t events, mainloop_event_func callback, void *user_data, mainloop_destroy_func destroy)
 
int mainloop_modify_fd (int fd, uint32_t events)
 
int mainloop_remove_fd (int fd)
 
static void timeout_destroy (void *user_data)
 
static void timeout_callback (int fd, uint32_t events, void *user_data)
 
static int timeout_set (int fd, unsigned int msec)
 
int mainloop_add_timeout (unsigned int msec, mainloop_timeout_func callback, void *user_data, mainloop_destroy_func destroy)
 
int mainloop_modify_timeout (int id, unsigned int msec)
 
int mainloop_remove_timeout (int id)
 
int mainloop_set_signal (sigset_t *mask, mainloop_signal_func callback, void *user_data, mainloop_destroy_func destroy)
 

Variables

static int epoll_fd
 
static int epoll_terminate
 
static int exit_status
 
static struct mainloop_datamainloop_list [MAX_MAINLOOP_ENTRIES]
 array of file descriptor event stub More...
 
static struct signal_datasignal_data
 

Detailed Description

set of function to manage the epool loop

Author
Gilbert Brault

Definition in file mainloop.c.

Macro Definition Documentation

#define MAX_EPOLL_EVENTS   10

Definition at line 50 of file mainloop.c.

#define MAX_MAINLOOP_ENTRIES   128

Function Documentation

int mainloop_add_fd ( int  fd,
uint32_t  events,
mainloop_event_func  callback,
void *  user_data,
mainloop_destroy_func  destroy 
)

trigger an event to be processed by the mainloop_run function and create a mainloop_list entry

See also
EPOLL_EVENTS_DOC for events description
Parameters
fd"file descriptor" source of the event (socket)
eventsevent flags EPOOL type like EPOLLIN, EPOLLOUT...
callbackfunction to call back by the event processor
user_dataassociated data
destroymanagement function to unallocate user_data
Returns
0 success else <0 error

Definition at line 252 of file mainloop.c.

References mainloop_data::callback, mainloop_data::destroy, epoll_fd, mainloop_data::events, mainloop_data::fd, MAX_MAINLOOP_ENTRIES, and mainloop_data::user_data.

Referenced by io_new(), main(), mainloop_add_timeout(), and mainloop_run().

int mainloop_add_timeout ( unsigned int  msec,
mainloop_timeout_func  callback,
void *  user_data,
mainloop_destroy_func  destroy 
)
void mainloop_exit_failure ( void  )

set exit_status to EXIT_FAILURE set epoll_terminate to 1 (mainloop_run exit looping)

Definition at line 135 of file mainloop.c.

References epoll_terminate, and exit_status.

void mainloop_exit_success ( void  )

set exit_status to EXIT_SUCCESS set epoll_terminate to 1 (mainloop_run exit looping)

Definition at line 125 of file mainloop.c.

References epoll_terminate, and exit_status.

void mainloop_init ( void  )

create the epoll resource (epoll_fd global variable) initialize mainloop_list (global variable) event table set epoll_terminate to 0 (mainloop_run looping)

Definition at line 101 of file mainloop.c.

References epoll_fd, epoll_terminate, and MAX_MAINLOOP_ENTRIES.

Referenced by main().

int mainloop_modify_fd ( int  fd,
uint32_t  events 
)

trigger an epoll event for an existing mainloop socket (exisiting mainloop_list[fd]) epool event "events" = events, "data.ptr" = mainloop_list[fd]

Parameters
fdsocket
eventsEPOLL event like EPOLLIN, EPOLLOUT...
Returns
0==Success <0 error

Definition at line 296 of file mainloop.c.

References epoll_fd, mainloop_data::events, mainloop_data::fd, and MAX_MAINLOOP_ENTRIES.

Referenced by io_callback(), io_set_disconnect_handler(), io_set_read_handler(), io_set_write_handler(), and mainloop_modify_timeout().

int mainloop_modify_timeout ( int  id,
unsigned int  msec 
)

Definition at line 429 of file mainloop.c.

References mainloop_modify_fd(), and timeout_set().

void mainloop_quit ( void  )

set epoll_terminate to 1 (mainloop_run exit looping)

Definition at line 116 of file mainloop.c.

References epoll_terminate.

Referenced by att_disconnect_cb(), cmd_quit(), prompt_read_cb(), signal_callback(), and signal_cb().

int mainloop_remove_fd ( int  fd)
int mainloop_remove_timeout ( int  id)

Definition at line 442 of file mainloop.c.

References mainloop_remove_fd().

int mainloop_run ( void  )

main loop wait for epoll events to exit the loop, set epoll_terminate to a <>0 value

See also
mainloop_exit_failure
mainloop_exit_success
Returns
exit_status EXIT_SUCCESS or EXIT_FAILURE

Definition at line 174 of file mainloop.c.

References mainloop_data::callback, mainloop_data::destroy, signal_data::destroy, epoll_fd, epoll_terminate, exit_status, mainloop_data::fd, signal_data::fd, mainloop_add_fd(), mainloop_remove_fd(), signal_data::mask, MAX_MAINLOOP_ENTRIES, signal_callback(), mainloop_data::user_data, and signal_data::user_data.

Referenced by main().

int mainloop_set_signal ( sigset_t *  mask,
mainloop_signal_func  callback,
void *  user_data,
mainloop_destroy_func  destroy 
)

set mainloop signal handler (signal_data) usally SIGINT and SIGTERM handler signal_data is a global variable

Parameters
maskevents to filter
callbackfunction call triggered by filtered events prototype callback(int signum, void *user_data)
user_datauser data to pass to callback
destroyuser data storage management
Returns
0 success else <0 error

Definition at line 457 of file mainloop.c.

References signal_data::callback, signal_data::destroy, signal_data::fd, signal_data::mask, and signal_data::user_data.

Referenced by main().

static void signal_callback ( int  fd,
uint32_t  events,
void *  user_data 
)
static
Parameters
fd
events
user_data

Definition at line 147 of file mainloop.c.

References signal_data::callback, mainloop_quit(), and signal_data::user_data.

Referenced by mainloop_run().

static void timeout_callback ( int  fd,
uint32_t  events,
void *  user_data 
)
static

Definition at line 357 of file mainloop.c.

References timeout_data::callback, timeout_data::fd, and timeout_data::user_data.

Referenced by mainloop_add_timeout().

static void timeout_destroy ( void *  user_data)
static

Definition at line 346 of file mainloop.c.

References timeout_data::destroy, timeout_data::fd, and timeout_data::user_data.

Referenced by mainloop_add_timeout().

static int timeout_set ( int  fd,
unsigned int  msec 
)
inlinestatic

Definition at line 374 of file mainloop.c.

Referenced by mainloop_add_timeout(), and mainloop_modify_timeout().

Variable Documentation

int epoll_fd
static
int epoll_terminate
static
int exit_status
static

Definition at line 54 of file mainloop.c.

Referenced by mainloop_exit_failure(), mainloop_exit_success(), and mainloop_run().

struct mainloop_data* mainloop_list[MAX_MAINLOOP_ENTRIES]
static

array of file descriptor event stub

Definition at line 77 of file mainloop.c.

struct signal_data* signal_data
static

Definition at line 94 of file mainloop.c.