Skip to content

Commit 7020b7c

Browse files
author
Toon Stegen
committed
cpu/native: add args to async_read callback
this makes it possible to pass some generic pointer that's given back as an argument when the callback is called.
1 parent 3ad29e1 commit 7020b7c

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

cpu/native/async_read.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
static int _next_index;
3030
static int _fds[ASYNC_READ_NUMOF];
31+
static void *_args[ASYNC_READ_NUMOF];
3132
static native_async_read_callback_t _native_async_read_callbacks[ASYNC_READ_NUMOF];
3233

3334
#ifdef __MACH__
@@ -55,7 +56,7 @@ static void _async_io_isr(void) {
5556
if (real_select(max_fd + 1, &rfds, NULL, NULL, &timeout) > 0) {
5657
for (int i = 0; i < _next_index; i++) {
5758
if (FD_ISSET(_fds[i], &rfds)) {
58-
_native_async_read_callbacks[i](_fds[i]);
59+
_native_async_read_callbacks[i](_fds[i], _args[i]);
5960
}
6061
}
6162
}
@@ -86,12 +87,13 @@ void native_async_read_continue(int fd) {
8687
#endif
8788
}
8889

89-
void native_async_read_add_handler(int fd, native_async_read_callback_t handler) {
90+
void native_async_read_add_handler(int fd, void *arg, native_async_read_callback_t handler) {
9091
if (_next_index >= ASYNC_READ_NUMOF) {
9192
err(EXIT_FAILURE, "native_async_read_add_handler(): too many callbacks");
9293
}
9394

9495
_fds[_next_index] = fd;
96+
_args[_next_index] = arg;
9597
_native_async_read_callbacks[_next_index] = handler;
9698

9799
#ifdef __MACH__

cpu/native/include/async_read.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extern "C" {
3232
/**
3333
* @brief asynchronus read callback type
3434
*/
35-
typedef void (*native_async_read_callback_t)(int fd);
35+
typedef void (*native_async_read_callback_t)(int fd, void *arg);
3636

3737
/**
3838
* @brief initialize asynchronus read system
@@ -61,10 +61,11 @@ void native_async_read_continue(int fd);
6161
* @brief start monitoring of file descriptor
6262
*
6363
* @param[in] fd The file descriptor to monitor
64+
* @param[in] arg Pointer to be passed as arguments to the callback
6465
* @param[in] handler The callback function to be called when the file
6566
* descriptor is ready to read.
6667
*/
67-
void native_async_read_add_handler(int fd, native_async_read_callback_t handler);
68+
void native_async_read_add_handler(int fd, void *arg, native_async_read_callback_t handler);
6869

6970
#ifdef __cplusplus
7071
}

cpu/native/netdev2_tap/netdev2_tap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,9 @@ void netdev2_tap_setup(netdev2_tap_t *dev, const netdev2_tap_params_t *params) {
307307
strncpy(dev->tap_name, *(params->tap_name), IFNAMSIZ);
308308
}
309309

310-
static void _tap_isr(int fd) {
310+
static void _tap_isr(int fd, void *arg) {
311311
(void) fd;
312+
(void) arg;
312313

313314
netdev2_t *netdev = (netdev2_t *)&netdev2_tap;
314315

@@ -393,7 +394,7 @@ static int _init(netdev2_t *netdev)
393394

394395
/* configure signal handler for fds */
395396
native_async_read_setup();
396-
native_async_read_add_handler(dev->tap_fd, _tap_isr);
397+
native_async_read_add_handler(dev->tap_fd, NULL, _tap_isr);
397398

398399
#ifdef MODULE_NETSTATS_L2
399400
memset(&netdev->stats, 0, sizeof(netstats_t));

cpu/native/periph/uart.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ void tty_uart_setup(uart_t uart, const char *filename)
5151
tty_device_filenames[uart] = strndup(filename, PATH_MAX - 1);
5252
}
5353

54-
static void io_signal_handler(int fd)
54+
static void io_signal_handler(int fd, void *arg)
5555
{
5656
uart_t uart;
57+
(void) arg;
5758

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

153154
native_async_read_setup();
154-
native_async_read_add_handler(tty_fds[uart], io_signal_handler);
155+
native_async_read_add_handler(tty_fds[uart], NULL, io_signal_handler);
155156

156157
return 0;
157158
}

0 commit comments

Comments
 (0)