From ebfa2df1138b1c69074bc8f7116a2664f60f435a Mon Sep 17 00:00:00 2001 From: Tomasz Bursztyka Date: Wed, 22 Nov 2017 20:50:53 +0100 Subject: [PATCH] net/ieee802154: Use helpers to call radio API functions Now the code is a bit cleaner. Signed-off-by: Tomasz Bursztyka --- subsys/net/ip/l2/ieee802154/ieee802154.c | 17 ++++++----------- subsys/net/ip/l2/ieee802154/ieee802154_mgmt.c | 17 ++++++----------- .../ip/l2/ieee802154/ieee802154_radio_aloha.c | 6 ++---- .../ip/l2/ieee802154/ieee802154_radio_csma_ca.c | 8 +++----- .../ip/l2/ieee802154/ieee802154_radio_utils.h | 11 ++++++----- 5 files changed, 23 insertions(+), 36 deletions(-) diff --git a/subsys/net/ip/l2/ieee802154/ieee802154.c b/subsys/net/ip/l2/ieee802154/ieee802154.c index 7020800fb4b..a0a997d1b18 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154.c +++ b/subsys/net/ip/l2/ieee802154/ieee802154.c @@ -85,12 +85,9 @@ static inline void ieee802154_acknowledge(struct net_if *iface, net_pkt_frag_insert(pkt, frag); if (ieee802154_create_ack_frame(iface, pkt, mpdu->mhr.fs->sequence)) { - const struct ieee802154_radio_api *radio = - iface->dev->driver_api; - net_buf_add(frag, IEEE802154_ACK_PKT_LENGTH); - radio->tx(iface->dev, pkt, frag); + ieee802154_tx(iface, pkt, frag); } net_pkt_unref(pkt); @@ -293,7 +290,6 @@ static u16_t ieee802154_reserve(struct net_if *iface, void *data) static int ieee802154_enable(struct net_if *iface, bool state) { - const struct ieee802154_radio_api *radio = iface->dev->driver_api; struct ieee802154_context *ctx = net_if_l2_data(iface); NET_DBG("iface %p %s", iface, state ? "up" : "down"); @@ -303,10 +299,10 @@ static int ieee802154_enable(struct net_if *iface, bool state) } if (state) { - return radio->start(iface->dev); + return ieee802154_start(iface); } - return radio->stop(iface->dev); + return ieee802154_stop(iface); } NET_L2_INIT(IEEE802154_L2, @@ -316,8 +312,8 @@ NET_L2_INIT(IEEE802154_L2, void ieee802154_init(struct net_if *iface) { struct ieee802154_context *ctx = net_if_l2_data(iface); - const struct ieee802154_radio_api *radio = iface->dev->driver_api; const u8_t *mac = iface->link_addr.addr; + s16_t tx_power = CONFIG_NET_L2_IEEE802154_RADIO_DFLT_TX_POWER; u8_t long_addr[8]; NET_DBG("Initializing IEEE 802.15.4 stack on iface %p", iface); @@ -336,8 +332,7 @@ void ieee802154_init(struct net_if *iface) memcpy(ctx->ext_addr, long_addr, 8); ieee802154_filter_ieee_addr(iface, ctx->ext_addr); - if (!radio->set_txpower(iface->dev, - CONFIG_NET_L2_IEEE802154_RADIO_DFLT_TX_POWER)) { - ctx->tx_power = CONFIG_NET_L2_IEEE802154_RADIO_DFLT_TX_POWER; + if (!ieee802154_set_tx_power(iface, tx_power)) { + ctx->tx_power = tx_power; } } diff --git a/subsys/net/ip/l2/ieee802154/ieee802154_mgmt.c b/subsys/net/ip/l2/ieee802154/ieee802154_mgmt.c index 78d717b1dff..090ca5b6745 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154_mgmt.c +++ b/subsys/net/ip/l2/ieee802154/ieee802154_mgmt.c @@ -83,8 +83,6 @@ NET_MGMT_REGISTER_REQUEST_HANDLER(NET_REQUEST_IEEE802154_CANCEL_SCAN, static int ieee802154_scan(u32_t mgmt_request, struct net_if *iface, void *data, size_t len) { - struct ieee802154_radio_api *radio = - (struct ieee802154_radio_api *)iface->dev->driver_api; struct ieee802154_context *ctx = net_if_l2_data(iface); struct ieee802154_req_params *scan = (struct ieee802154_req_params *)data; @@ -119,7 +117,7 @@ static int ieee802154_scan(u32_t mgmt_request, struct net_if *iface, ieee802154_filter_pan_id(iface, IEEE802154_BROADCAST_PAN_ID); - if (radio->start(iface->dev)) { + if (ieee802154_start(iface)) { NET_DBG("Could not start device"); ret = -EIO; @@ -135,7 +133,7 @@ static int ieee802154_scan(u32_t mgmt_request, struct net_if *iface, scan->channel = channel; NET_DBG("Scanning channel %u", channel); - radio->set_channel(iface->dev, channel); + ieee802154_set_channel(iface, channel); /* Active scan sends a beacon request */ if (mgmt_request == NET_REQUEST_IEEE802154_ACTIVE_SCAN) { @@ -165,7 +163,7 @@ static int ieee802154_scan(u32_t mgmt_request, struct net_if *iface, /* Let's come back to context's settings */ ieee802154_filter_pan_id(iface, ctx->pan_id); - radio->set_channel(iface->dev, ctx->channel); + ieee802154_set_channel(iface, ctx->channel); out: ctx->scan_ctx = NULL; @@ -223,8 +221,6 @@ enum net_verdict ieee802154_handle_mac_command(struct net_if *iface, static int ieee802154_associate(u32_t mgmt_request, struct net_if *iface, void *data, size_t len) { - struct ieee802154_radio_api *radio = - (struct ieee802154_radio_api *)iface->dev->driver_api; struct ieee802154_context *ctx = net_if_l2_data(iface); struct ieee802154_req_params *req = (struct ieee802154_req_params *)data; @@ -246,7 +242,7 @@ static int ieee802154_associate(u32_t mgmt_request, struct net_if *iface, params.pan_id = req->pan_id; /* Set channel first */ - if (radio->set_channel(iface->dev, req->channel)) { + if (ieee802154_set_channel(iface, req->channel)) { ret = -EIO; goto out; } @@ -371,7 +367,6 @@ static int ieee802154_set_parameters(u32_t mgmt_request, struct net_if *iface, void *data, size_t len) { - const struct ieee802154_radio_api *radio = iface->dev->driver_api; struct ieee802154_context *ctx = net_if_l2_data(iface); u16_t value; int ret = 0; @@ -393,7 +388,7 @@ static int ieee802154_set_parameters(u32_t mgmt_request, return -EINVAL; } - ret = radio->set_channel(iface->dev, value); + ret = ieee802154_set_channel(iface, value); if (!ret) { ctx->channel = value; } @@ -419,7 +414,7 @@ static int ieee802154_set_parameters(u32_t mgmt_request, } } else if (mgmt_request == NET_REQUEST_IEEE802154_SET_TX_POWER) { if (ctx->tx_power != (s16_t)value) { - ret = radio->set_txpower(iface->dev, (s16_t)value); + ret = ieee802154_set_tx_power(iface, (s16_t)value); if (!ret) { ctx->tx_power = (s16_t)value; } diff --git a/subsys/net/ip/l2/ieee802154/ieee802154_radio_aloha.c b/subsys/net/ip/l2/ieee802154/ieee802154_radio_aloha.c index 72be7f917a2..efd93e9d519 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154_radio_aloha.c +++ b/subsys/net/ip/l2/ieee802154/ieee802154_radio_aloha.c @@ -14,9 +14,8 @@ #include -#include - #include "ieee802154_frame.h" +#include "ieee802154_utils.h" #include "ieee802154_radio_utils.h" static inline int aloha_tx_fragment(struct net_if *iface, @@ -26,7 +25,6 @@ static inline int aloha_tx_fragment(struct net_if *iface, u8_t retries = CONFIG_NET_L2_IEEE802154_RADIO_TX_RETRIES; struct ieee802154_context *ctx = net_if_l2_data(iface); bool ack_required = prepare_for_ack(ctx, pkt, frag); - const struct ieee802154_radio_api *radio = iface->dev->driver_api; int ret = -EIO; NET_DBG("frag %p", frag); @@ -34,7 +32,7 @@ static inline int aloha_tx_fragment(struct net_if *iface, while (retries) { retries--; - ret = radio->tx(iface->dev, pkt, frag); + ret = ieee802154_tx(iface, pkt, frag); if (ret) { continue; } diff --git a/subsys/net/ip/l2/ieee802154/ieee802154_radio_csma_ca.c b/subsys/net/ip/l2/ieee802154/ieee802154_radio_csma_ca.c index 53fec64bfd0..ef5a966a7bb 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154_radio_csma_ca.c +++ b/subsys/net/ip/l2/ieee802154/ieee802154_radio_csma_ca.c @@ -17,9 +17,8 @@ #include #include -#include - #include "ieee802154_frame.h" +#include "ieee802154_utils.h" #include "ieee802154_radio_utils.h" static inline int csma_ca_tx_fragment(struct net_if *iface, @@ -30,7 +29,6 @@ static inline int csma_ca_tx_fragment(struct net_if *iface, const u8_t max_be = CONFIG_NET_L2_IEEE802154_RADIO_CSMA_CA_MAX_BE; u8_t retries = CONFIG_NET_L2_IEEE802154_RADIO_TX_RETRIES; struct ieee802154_context *ctx = net_if_l2_data(iface); - const struct ieee802154_radio_api *radio = iface->dev->driver_api; bool ack_required = prepare_for_ack(ctx, pkt, frag); u8_t be = CONFIG_NET_L2_IEEE802154_RADIO_CSMA_CA_MIN_BE; u8_t nb = 0; @@ -49,7 +47,7 @@ loop: } while (1) { - if (!radio->cca(iface->dev)) { + if (!ieee802154_cca(iface)) { break; } @@ -61,7 +59,7 @@ loop: } } - ret = radio->tx(iface->dev, pkt, frag); + ret = ieee802154_tx(iface, pkt, frag); if (ret) { continue; } diff --git a/subsys/net/ip/l2/ieee802154/ieee802154_radio_utils.h b/subsys/net/ip/l2/ieee802154/ieee802154_radio_utils.h index 21e01caa616..8fc8dc08143 100644 --- a/subsys/net/ip/l2/ieee802154/ieee802154_radio_utils.h +++ b/subsys/net/ip/l2/ieee802154/ieee802154_radio_utils.h @@ -7,6 +7,8 @@ #ifndef __IEEE802154_RADIO_UTILS_H__ #define __IEEE802154_RADIO_UTILS_H__ +#include "ieee802154_utils.h" + typedef int (ieee802154_radio_tx_frag_t)(struct net_if *iface, struct net_pkt *pkt, struct net_buf *frag); @@ -33,12 +35,11 @@ static inline bool prepare_for_ack(struct ieee802154_context *ctx, static inline int wait_for_ack(struct net_if *iface, bool ack_required) { - const struct ieee802154_radio_api *radio = iface->dev->driver_api; struct ieee802154_context *ctx = net_if_l2_data(iface); if (!ack_required || - (radio->get_capabilities(iface->dev) & IEEE802154_HW_TX_RX_ACK)) { + (ieee802154_get_hw_capabilities(iface) & IEEE802154_HW_TX_RX_ACK)) { return 0; } @@ -79,15 +80,15 @@ static inline int tx_packet_fragments(struct net_if *iface, struct net_pkt *pkt, ieee802154_radio_tx_frag_t *tx_func) { - const struct ieee802154_radio_api *radio = iface->dev->driver_api; int ret = 0; struct net_buf *frag; frag = pkt->frags; while (frag) { if (IS_ENABLED(CONFIG_NET_L2_IEEE802154_RADIO_CSMA_CA) && - radio->get_capabilities(iface->dev) & IEEE802154_HW_CSMA) { - ret = radio->tx(iface->dev, pkt, frag); + ieee802154_get_hw_capabilities(iface) & + IEEE802154_HW_CSMA) { + ret = ieee802154_tx(iface, pkt, frag); } else { ret = tx_func(iface, pkt, frag); }