mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #4366 from kaspar030/factor_out_common_netdev_ethernet_code
drivers: netdev2: factor out common netdev ethernet code
This commit is contained in:
commit
810c623092
@ -33,6 +33,7 @@ endif
|
||||
|
||||
ifneq (,$(filter netdev2_tap,$(USEMODULE)))
|
||||
USEMODULE += netif
|
||||
USEMODULE += netdev2_eth
|
||||
endif
|
||||
|
||||
ifneq (,$(filter gnrc_zep,$(USEMODULE)))
|
||||
|
@ -50,6 +50,7 @@
|
||||
|
||||
#include "net/eui64.h"
|
||||
#include "net/netdev2.h"
|
||||
#include "net/netdev2_eth.h"
|
||||
#include "net/ethernet.h"
|
||||
#include "net/ethernet/hdr.h"
|
||||
#include "netdev2_tap.h"
|
||||
@ -97,18 +98,6 @@ static inline int _set_promiscous(netdev2_t *netdev, int value)
|
||||
return value;
|
||||
}
|
||||
|
||||
static inline int _get_iid(netdev2_t *netdev, eui64_t *value, size_t max_len)
|
||||
{
|
||||
if (max_len < sizeof(eui64_t)) {
|
||||
return -EOVERFLOW;
|
||||
}
|
||||
|
||||
uint8_t addr[ETHERNET_ADDR_LEN];
|
||||
_get_mac_addr(netdev, addr);
|
||||
ethernet_get_iid(value, addr);
|
||||
|
||||
return sizeof(eui64_t);
|
||||
}
|
||||
static inline void _isr(netdev2_t *netdev)
|
||||
{
|
||||
if (netdev->event_callback) {
|
||||
@ -130,13 +119,6 @@ int _get(netdev2_t *dev, netopt_t opt, void *value, size_t max_len)
|
||||
int res = 0;
|
||||
|
||||
switch (opt) {
|
||||
case NETOPT_DEVICE_TYPE:
|
||||
{
|
||||
uint16_t *tgt = (uint16_t *)value;
|
||||
*tgt = NETDEV2_TYPE_ETHERNET;
|
||||
res = 2;
|
||||
break;
|
||||
}
|
||||
case NETOPT_ADDRESS:
|
||||
if (max_len < ETHERNET_ADDR_LEN) {
|
||||
res = -EINVAL;
|
||||
@ -146,24 +128,12 @@ int _get(netdev2_t *dev, netopt_t opt, void *value, size_t max_len)
|
||||
res = ETHERNET_ADDR_LEN;
|
||||
}
|
||||
break;
|
||||
case NETOPT_ADDR_LEN:
|
||||
case NETOPT_SRC_LEN:
|
||||
assert(max_len == 2);
|
||||
uint16_t *tgt = (uint16_t*)value;
|
||||
*tgt=6;
|
||||
res = sizeof(uint16_t);
|
||||
break;
|
||||
case NETOPT_PROMISCUOUSMODE:
|
||||
*((bool*)value) = (bool)_get_promiscous(dev);
|
||||
res = sizeof(bool);
|
||||
break;
|
||||
case NETOPT_IPV6_IID:
|
||||
return _get_iid(dev, value, max_len);
|
||||
case NETOPT_IS_WIRED:
|
||||
res = 1;
|
||||
break;
|
||||
default:
|
||||
res = -ENOTSUP;
|
||||
res = netdev2_eth_get(dev, opt, value, max_len);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ ifneq (,$(filter dht,$(USEMODULE)))
|
||||
endif
|
||||
|
||||
ifneq (,$(filter encx24j600,$(USEMODULE)))
|
||||
USEMODULE += netdev2_eth
|
||||
USEMODULE += xtimer
|
||||
endif
|
||||
|
||||
|
@ -29,6 +29,7 @@
|
||||
#include "xtimer.h"
|
||||
|
||||
#include "net/netdev2.h"
|
||||
#include "net/netdev2_eth.h"
|
||||
#include "net/eui64.h"
|
||||
#include "net/ethernet.h"
|
||||
//#include "net/ethernet/hdr.h"
|
||||
@ -55,7 +56,6 @@ static uint16_t reg_get(encx24j600_t *dev, uint8_t reg);
|
||||
static void reg_clear_bits(encx24j600_t *dev, uint8_t reg, uint16_t mask);
|
||||
static inline int _packets_available(encx24j600_t *dev);
|
||||
|
||||
static int _get_iid(netdev2_t *netdev, eui64_t *value, size_t max_len);
|
||||
static void _get_mac_addr(netdev2_t *dev, uint8_t* buf);
|
||||
|
||||
/* netdev2 interface */
|
||||
@ -64,7 +64,6 @@ static int _recv(netdev2_t *netdev, char* buf, int len);
|
||||
static int _init(netdev2_t *dev);
|
||||
static void _isr(netdev2_t *dev);
|
||||
int _get(netdev2_t *dev, netopt_t opt, void *value, size_t max_len);
|
||||
int _set(netdev2_t *dev, netopt_t opt, void *value, size_t value_len);
|
||||
|
||||
const static netdev2_driver_t netdev2_driver_encx24j600 = {
|
||||
.send = _send,
|
||||
@ -72,7 +71,7 @@ const static netdev2_driver_t netdev2_driver_encx24j600 = {
|
||||
.init = _init,
|
||||
.isr = _isr,
|
||||
.get = _get,
|
||||
.set = _set,
|
||||
.set = netdev2_eth_set,
|
||||
};
|
||||
|
||||
static inline void lock(encx24j600_t *dev) {
|
||||
@ -383,31 +382,11 @@ static int _recv(netdev2_t *netdev, char* buf, int len)
|
||||
return payload_len;
|
||||
}
|
||||
|
||||
static int _get_iid(netdev2_t *netdev, eui64_t *value, size_t max_len)
|
||||
{
|
||||
if (max_len < sizeof(eui64_t)) {
|
||||
return -EOVERFLOW;
|
||||
}
|
||||
|
||||
uint8_t addr[ETHERNET_ADDR_LEN];
|
||||
_get_mac_addr(netdev, addr);
|
||||
ethernet_get_iid(value, addr);
|
||||
|
||||
return sizeof(eui64_t);
|
||||
}
|
||||
|
||||
int _get(netdev2_t *dev, netopt_t opt, void *value, size_t max_len)
|
||||
{
|
||||
int res = 0;
|
||||
|
||||
switch (opt) {
|
||||
case NETOPT_DEVICE_TYPE:
|
||||
{
|
||||
uint16_t *tgt = (uint16_t *)value;
|
||||
*tgt = NETDEV2_TYPE_ETHERNET;
|
||||
res = 2;
|
||||
break;
|
||||
}
|
||||
case NETOPT_ADDRESS:
|
||||
if (max_len < ETHERNET_ADDR_LEN) {
|
||||
res = -EINVAL;
|
||||
@ -417,31 +396,10 @@ int _get(netdev2_t *dev, netopt_t opt, void *value, size_t max_len)
|
||||
res = ETHERNET_ADDR_LEN;
|
||||
}
|
||||
break;
|
||||
case NETOPT_ADDR_LEN:
|
||||
case NETOPT_SRC_LEN:
|
||||
assert(max_len == 2);
|
||||
uint16_t *tgt = (uint16_t*)value;
|
||||
*tgt=6;
|
||||
res = sizeof(uint16_t);
|
||||
break;
|
||||
case NETOPT_IPV6_IID:
|
||||
return _get_iid(dev, value, max_len);
|
||||
default:
|
||||
res = -ENOTSUP;
|
||||
res = netdev2_eth_get(dev, opt, value, max_len);
|
||||
break;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int _set(netdev2_t *dev, netopt_t opt, void *value, size_t value_len)
|
||||
{
|
||||
int res = 0;
|
||||
|
||||
switch (opt) {
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
65
drivers/include/net/netdev2_eth.h
Normal file
65
drivers/include/net/netdev2_eth.h
Normal file
@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||
* Public License v2.1. See the file LICENSE in the top level directory for
|
||||
* more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup drivers_netdev_netdev2
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Definitions for netdev2 common ethernet code
|
||||
*
|
||||
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*/
|
||||
|
||||
#ifndef NETDEV2_ETH_H
|
||||
#define NETDEV2_ETH_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "netdev2.h"
|
||||
#include "net/netopt.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Fallback function for netdev2 ethernet devices' _get function
|
||||
*
|
||||
* Supposed to be used by netdev2 drivers as default case.
|
||||
*
|
||||
* @warning Driver *MUST* implement NETOPT_ADDRESS case!
|
||||
*
|
||||
* @param[in] dev network device descriptor
|
||||
* @param[in] opt option type
|
||||
* @param[out] value pointer to store the option's value in
|
||||
* @param[in] max_len maximal amount of byte that fit into @p value
|
||||
*
|
||||
* @return number of bytes written to @p value
|
||||
* @return <0 on error
|
||||
*/
|
||||
int netdev2_eth_get(netdev2_t *dev, netopt_t opt, void *value, size_t max_len);
|
||||
|
||||
/**
|
||||
* @brief Fallback function for netdev2 ethernet devices' _set function
|
||||
*
|
||||
* @param[in] dev network device descriptor
|
||||
* @param[in] opt option type
|
||||
* @param[in] value value to set
|
||||
* @param[in] value_len the length of @p value
|
||||
*
|
||||
* @return number of bytes used from @p value
|
||||
* @return <0 on error
|
||||
*/
|
||||
int netdev2_eth_set(netdev2_t *dev, netopt_t opt, void *value, size_t value_len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
/** @} */
|
||||
#endif /* NETDEV2_ETH_H */
|
1
drivers/netdev2_eth/Makefile
Normal file
1
drivers/netdev2_eth/Makefile
Normal file
@ -0,0 +1 @@
|
||||
include $(RIOTBASE)/Makefile.base
|
106
drivers/netdev2_eth/netdev2_eth.c
Normal file
106
drivers/netdev2_eth/netdev2_eth.c
Normal file
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*
|
||||
* This file is subject to the terms and conditions of the GNU Lesser
|
||||
* General Public License v2.1. See the file LICENSE in the top level
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup driver_netdev2_eth
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Common code for netdev2 ethernet drivers
|
||||
*
|
||||
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "net/netdev2.h"
|
||||
#include "net/eui64.h"
|
||||
#include "net/ethernet.h"
|
||||
|
||||
#define ENABLE_DEBUG (0)
|
||||
#include "debug.h"
|
||||
|
||||
static int _get_iid(netdev2_t *netdev, eui64_t *value, size_t max_len)
|
||||
{
|
||||
if (max_len < sizeof(eui64_t)) {
|
||||
return -EOVERFLOW;
|
||||
}
|
||||
|
||||
uint8_t addr[ETHERNET_ADDR_LEN];
|
||||
netdev->driver->get(netdev, NETOPT_ADDRESS, addr, ETHERNET_ADDR_LEN);
|
||||
ethernet_get_iid(value, addr);
|
||||
|
||||
return sizeof(eui64_t);
|
||||
}
|
||||
|
||||
int netdev2_eth_get(netdev2_t *dev, netopt_t opt, void *value, size_t max_len)
|
||||
{
|
||||
int res = 0;
|
||||
|
||||
switch (opt) {
|
||||
case NETOPT_DEVICE_TYPE:
|
||||
{
|
||||
uint16_t *tgt = (uint16_t *)value;
|
||||
*tgt = NETDEV2_TYPE_ETHERNET;
|
||||
res = 2;
|
||||
break;
|
||||
}
|
||||
case NETOPT_ADDR_LEN:
|
||||
case NETOPT_SRC_LEN:
|
||||
{
|
||||
assert(max_len == 2);
|
||||
uint16_t *tgt = (uint16_t*)value;
|
||||
*tgt=6;
|
||||
res = sizeof(uint16_t);
|
||||
break;
|
||||
}
|
||||
case NETOPT_MAX_PACKET_SIZE:
|
||||
{
|
||||
assert(max_len >= 2);
|
||||
uint16_t *val = (uint16_t*) value;
|
||||
*val = ETHERNET_DATA_LEN;
|
||||
res = sizeof(uint16_t);
|
||||
break;
|
||||
}
|
||||
case NETOPT_IS_WIRED:
|
||||
{
|
||||
res = 1;
|
||||
break;
|
||||
}
|
||||
case NETOPT_IPV6_IID:
|
||||
{
|
||||
return _get_iid(dev, value, max_len);
|
||||
}
|
||||
default:
|
||||
{
|
||||
res = -ENOTSUP;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
int netdev2_eth_set(netdev2_t *dev, netopt_t opt, void *value, size_t value_len)
|
||||
{
|
||||
(void)dev;
|
||||
(void)value;
|
||||
(void)value_len;
|
||||
|
||||
int res = 0;
|
||||
|
||||
switch (opt) {
|
||||
default:
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
Loading…
Reference in New Issue
Block a user