From 686c81caf71ad2600362d0a794aff445966c3a31 Mon Sep 17 00:00:00 2001 From: Jose Alamos Date: Tue, 22 Jun 2021 10:07:07 +0200 Subject: [PATCH] socket_zep: avoid explicit cast to netdev --- cpu/native/socket_zep/socket_zep.c | 22 ++++++++++++++-------- tests/socket_zep/main.c | 8 ++++---- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/cpu/native/socket_zep/socket_zep.c b/cpu/native/socket_zep/socket_zep.c index 786fbcf5bd..4e9a95b174 100644 --- a/cpu/native/socket_zep/socket_zep.c +++ b/cpu/native/socket_zep/socket_zep.c @@ -104,7 +104,8 @@ static size_t _prep_vector(socket_zep_t *dev, const iolist_t *iolist, static int _send(netdev_t *netdev, const iolist_t *iolist) { - socket_zep_t *dev = (socket_zep_t *)netdev; + netdev_ieee802154_t *netdev_ieee802154 = container_of(netdev, netdev_ieee802154_t, netdev); + socket_zep_t *dev = container_of(netdev_ieee802154, socket_zep_t, netdev); unsigned n = iolist_count(iolist); struct iovec v[n + 2]; int res; @@ -179,7 +180,8 @@ static inline bool _dst_not_me(socket_zep_t *dev, const void *buf) static int _recv(netdev_t *netdev, void *buf, size_t len, void *info) { - socket_zep_t *dev = (socket_zep_t *)netdev; + netdev_ieee802154_t *netdev_ieee802154 = container_of(netdev, netdev_ieee802154_t, netdev); + socket_zep_t *dev = container_of(netdev_ieee802154, socket_zep_t, netdev); int size = 0; DEBUG("socket_zep::recv(%p, %p, %u, %p)\n", (void *)netdev, buf, @@ -263,7 +265,8 @@ static int _recv(netdev_t *netdev, void *buf, size_t len, void *info) static void _isr(netdev_t *netdev) { if (netdev->event_callback) { - socket_zep_t *dev = (socket_zep_t *)netdev; + netdev_ieee802154_t *netdev_ieee802154 = container_of(netdev, netdev_ieee802154_t, netdev); + socket_zep_t *dev = container_of(netdev_ieee802154, socket_zep_t, netdev); DEBUG("socket_zep::isr: firing %u\n", (unsigned)dev->last_event); netdev->event_callback(netdev, dev->last_event); @@ -275,7 +278,7 @@ static void _socket_isr(int fd, void *arg) { (void)fd; (void)arg; - netdev_t *netdev = (netdev_t *)arg; + netdev_t *netdev = arg; DEBUG("socket_zep::_socket_isr: %d, %p (netdev == %p)\n", fd, arg, (void *)netdev); @@ -283,7 +286,8 @@ static void _socket_isr(int fd, void *arg) return; } if (netdev->event_callback) { - socket_zep_t *dev = (socket_zep_t *)netdev; + netdev_ieee802154_t *netdev_ieee802154 = container_of(netdev, netdev_ieee802154_t, netdev); + socket_zep_t *dev = container_of(netdev_ieee802154, socket_zep_t, netdev); dev->last_event = NETDEV_EVENT_RX_COMPLETE; netdev_trigger_event_isr(netdev); @@ -292,7 +296,8 @@ static void _socket_isr(int fd, void *arg) static int _init(netdev_t *netdev) { - socket_zep_t *dev = (socket_zep_t *)netdev; + netdev_ieee802154_t *netdev_ieee802154 = container_of(netdev, netdev_ieee802154_t, netdev); + socket_zep_t *dev = container_of(netdev_ieee802154, socket_zep_t, netdev); netdev_ieee802154_reset(&dev->netdev); @@ -305,14 +310,15 @@ static int _init(netdev_t *netdev) static int _get(netdev_t *netdev, netopt_t opt, void *value, size_t max_len) { assert(netdev != NULL); - return netdev_ieee802154_get((netdev_ieee802154_t *)netdev, opt, value, max_len); + return netdev_ieee802154_get(container_of(netdev, netdev_ieee802154_t, netdev), + opt, value, max_len); } static int _set(netdev_t *netdev, netopt_t opt, const void *value, size_t value_len) { assert(netdev != NULL); - return netdev_ieee802154_set((netdev_ieee802154_t *)netdev, opt, + return netdev_ieee802154_set(container_of(netdev, netdev_ieee802154_t, netdev), opt, value, value_len); } diff --git a/tests/socket_zep/main.c b/tests/socket_zep/main.c index 3243584525..fab8d17ab9 100644 --- a/tests/socket_zep/main.c +++ b/tests/socket_zep/main.c @@ -48,7 +48,7 @@ static void _print_info(netdev_t *netdev); static void test_init(void) { const socket_zep_params_t *p = &socket_zep_params[0]; - netdev_t *netdev = (netdev_t *)(&_dev); + netdev_t *netdev = &_dev.netdev.netdev; printf("Initializing socket ZEP with (local: [%s]:%s, remote: [%s]:%s)\n", p->local_addr, p->local_port, p->remote_addr, p->remote_port); @@ -60,7 +60,7 @@ static void test_init(void) static void test_send__iolist_NULL(void) { - netdev_t *netdev = (netdev_t *)(&_dev); + netdev_t *netdev = &_dev.netdev.netdev; puts("Send zero-length packet"); int res = netdev->driver->send(netdev, NULL); @@ -77,7 +77,7 @@ static void test_send__iolist_not_NULL(void) iolist[0].iol_next = &iolist[1]; - netdev_t *netdev = (netdev_t *)(&_dev); + netdev_t *netdev = &_dev.netdev.netdev; puts("Send 'Hello\\0World\\0'"); int res = netdev->driver->send(netdev, iolist); @@ -91,7 +91,7 @@ static void test_recv(void) { puts("Waiting for an incoming message (use `make test`)"); while (1) { - netdev_t *netdev = (netdev_t *)(&_dev); + netdev_t *netdev = &_dev.netdev.netdev; msg_t msg; msg_receive(&msg);