ble_gatt_client
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
mainloop.h File Reference
#include <signal.h>
#include <sys/epoll.h>

Go to the source code of this file.

Typedefs

typedef void(* mainloop_destroy_func )(void *user_data)
 
typedef void(* mainloop_event_func )(int fd, uint32_t events, void *user_data)
 
typedef void(* mainloop_timeout_func )(int id, void *user_data)
 
typedef void(* mainloop_signal_func )(int signum, void *user_data)
 

Functions

void mainloop_init (void)
 
void mainloop_quit (void)
 
void mainloop_exit_success (void)
 
void mainloop_exit_failure (void)
 
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)
 
int mainloop_add_timeout (unsigned int msec, mainloop_timeout_func callback, void *user_data, mainloop_destroy_func destroy)
 
int mainloop_modify_timeout (int fd, 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)
 

Typedef Documentation

typedef void(* mainloop_destroy_func)(void *user_data)

Definition at line 28 of file mainloop.h.

typedef void(* mainloop_event_func)(int fd, uint32_t events, void *user_data)

Definition at line 30 of file mainloop.h.

typedef void(* mainloop_signal_func)(int signum, void *user_data)

Definition at line 32 of file mainloop.h.

typedef void(* mainloop_timeout_func)(int id, void *user_data)

Definition at line 31 of file mainloop.h.

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  fd,
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().