Skip to content

Commit

Permalink
cpu/native: add args to async_read callback
Browse files Browse the repository at this point in the history
this makes it possible to pass some generic pointer that's given back as
an argument when the callback is called.
  • Loading branch information
Toon Stegen committed Jul 7, 2016
1 parent 3ad29e1 commit 7020b7c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
6 changes: 4 additions & 2 deletions cpu/native/async_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

static int _next_index;
static int _fds[ASYNC_READ_NUMOF];
static void *_args[ASYNC_READ_NUMOF];
static native_async_read_callback_t _native_async_read_callbacks[ASYNC_READ_NUMOF];

#ifdef __MACH__
Expand Down Expand Up @@ -55,7 +56,7 @@ static void _async_io_isr(void) {
if (real_select(max_fd + 1, &rfds, NULL, NULL, &timeout) > 0) {
for (int i = 0; i < _next_index; i++) {
if (FD_ISSET(_fds[i], &rfds)) {
_native_async_read_callbacks[i](_fds[i]);
_native_async_read_callbacks[i](_fds[i], _args[i]);
}
}
}
Expand Down Expand Up @@ -86,12 +87,13 @@ void native_async_read_continue(int fd) {
#endif
}

void native_async_read_add_handler(int fd, native_async_read_callback_t handler) {
void native_async_read_add_handler(int fd, void *arg, native_async_read_callback_t handler) {
if (_next_index >= ASYNC_READ_NUMOF) {
err(EXIT_FAILURE, "native_async_read_add_handler(): too many callbacks");
}

_fds[_next_index] = fd;
_args[_next_index] = arg;
_native_async_read_callbacks[_next_index] = handler;

#ifdef __MACH__
Expand Down
5 changes: 3 additions & 2 deletions cpu/native/include/async_read.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extern "C" {
/**
* @brief asynchronus read callback type
*/
typedef void (*native_async_read_callback_t)(int fd);
typedef void (*native_async_read_callback_t)(int fd, void *arg);

/**
* @brief initialize asynchronus read system
Expand Down Expand Up @@ -61,10 +61,11 @@ void native_async_read_continue(int fd);
* @brief start monitoring of file descriptor
*
* @param[in] fd The file descriptor to monitor
* @param[in] arg Pointer to be passed as arguments to the callback
* @param[in] handler The callback function to be called when the file
* descriptor is ready to read.
*/
void native_async_read_add_handler(int fd, native_async_read_callback_t handler);
void native_async_read_add_handler(int fd, void *arg, native_async_read_callback_t handler);

#ifdef __cplusplus
}
Expand Down
5 changes: 3 additions & 2 deletions cpu/native/netdev2_tap/netdev2_tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ void netdev2_tap_setup(netdev2_tap_t *dev, const netdev2_tap_params_t *params) {
strncpy(dev->tap_name, *(params->tap_name), IFNAMSIZ);
}

static void _tap_isr(int fd) {
static void _tap_isr(int fd, void *arg) {
(void) fd;
(void) arg;

netdev2_t *netdev = (netdev2_t *)&netdev2_tap;

Expand Down Expand Up @@ -393,7 +394,7 @@ static int _init(netdev2_t *netdev)

/* configure signal handler for fds */
native_async_read_setup();
native_async_read_add_handler(dev->tap_fd, _tap_isr);
native_async_read_add_handler(dev->tap_fd, NULL, _tap_isr);

#ifdef MODULE_NETSTATS_L2
memset(&netdev->stats, 0, sizeof(netstats_t));
Expand Down
5 changes: 3 additions & 2 deletions cpu/native/periph/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ void tty_uart_setup(uart_t uart, const char *filename)
tty_device_filenames[uart] = strndup(filename, PATH_MAX - 1);
}

static void io_signal_handler(int fd)
static void io_signal_handler(int fd, void *arg)
{
uart_t uart;
(void) arg;

for (uart = 0; uart < UART_NUMOF; uart++) {
if (tty_fds[uart] == fd) {
Expand Down Expand Up @@ -151,7 +152,7 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
uart_config[uart].arg = arg;

native_async_read_setup();
native_async_read_add_handler(tty_fds[uart], io_signal_handler);
native_async_read_add_handler(tty_fds[uart], NULL, io_signal_handler);

return 0;
}
Expand Down

0 comments on commit 7020b7c

Please sign in to comment.