6loWPAN: Add network IOCTL support to set the node address
This commit is contained in:
parent
07338ff2a9
commit
194c2d86a8
@ -114,6 +114,12 @@ int netlib_setmacaddr(FAR const char *ifname, FAR const uint8_t *macaddr);
|
|||||||
int netlib_getmacaddr(FAR const char *ifname, FAR uint8_t *macaddr);
|
int netlib_getmacaddr(FAR const char *ifname, FAR uint8_t *macaddr);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef CONFIG_NET_ETHERNET
|
||||||
|
/* Get IEEE802.15.4 MAC driver node address */
|
||||||
|
|
||||||
|
int netlib_setnodeaddr(FAR const char *ifname, FAR const uint8_t *nodeaddr);
|
||||||
|
#endif
|
||||||
|
|
||||||
/* IP address support */
|
/* IP address support */
|
||||||
|
|
||||||
#ifdef CONFIG_NET_IPv4
|
#ifdef CONFIG_NET_IPv4
|
||||||
|
@ -37,7 +37,7 @@
|
|||||||
-include $(TOPDIR)/Make.defs
|
-include $(TOPDIR)/Make.defs
|
||||||
include $(APPDIR)/Make.defs
|
include $(APPDIR)/Make.defs
|
||||||
|
|
||||||
# UIP Library
|
# Network Library
|
||||||
|
|
||||||
ASRCS =
|
ASRCS =
|
||||||
CSRCS = netlib_ipv4addrconv.c netlib_ethaddrconv.c netlib_parsehttpurl.c
|
CSRCS = netlib_ipv4addrconv.c netlib_ethaddrconv.c netlib_parsehttpurl.c
|
||||||
@ -80,12 +80,16 @@ ifeq ($(CONFIG_NETDEV_WIRELESS_IOCTL),y)
|
|||||||
CSRCS += netlib_getessid.c netlib_setessid.c
|
CSRCS += netlib_getessid.c netlib_setessid.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# No MAC address support for SLIP (Ethernet only)
|
# MAC address support(Ethernet and 6loWPAN only)
|
||||||
|
|
||||||
ifeq ($(CONFIG_NET_ETHERNET),y)
|
ifeq ($(CONFIG_NET_ETHERNET),y)
|
||||||
CSRCS += netlib_setmacaddr.c netlib_getmacaddr.c
|
CSRCS += netlib_setmacaddr.c netlib_getmacaddr.c
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(CONFIG_NET_6LOWPAN),y)
|
||||||
|
CSRCS += netlib_setnodeaddr.c
|
||||||
|
endif
|
||||||
|
|
||||||
# IGMP support
|
# IGMP support
|
||||||
|
|
||||||
ifeq ($(CONFIG_NET_IGMP),y)
|
ifeq ($(CONFIG_NET_IGMP),y)
|
||||||
|
@ -52,9 +52,6 @@
|
|||||||
|
|
||||||
#include "netutils/netlib.h"
|
#include "netutils/netlib.h"
|
||||||
|
|
||||||
/****************************************************************************
|
|
||||||
* Pre-processor Definitions
|
|
||||||
****************************************************************************/
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
107
netutils/netlib/netlib_setnodeaddr.c
Normal file
107
netutils/netlib/netlib_setnodeaddr.c
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
/****************************************************************************
|
||||||
|
* netutils/netlib/netlib_setnodeaddr.c
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
|
||||||
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
||||||
|
* used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||||
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||||
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||||
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||||
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||||
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||||
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||||
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Included Files
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <net/if.h>
|
||||||
|
|
||||||
|
#include "netutils/netlib.h"
|
||||||
|
|
||||||
|
#if defined(CONFIG_NET_6LOWPAN) && CONFIG_NSOCKET_DESCRIPTORS > 0
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Public Functions
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: netlib_setnodeaddr
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Set the 6loWPAN IEEE802.15.4 MAC network driver node address
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* ifname The name of the interface to use
|
||||||
|
* nodeaddr Node address to set, size must be CONFIG_NET_6LOWPAN_RIMEADDR_SIZE
|
||||||
|
*
|
||||||
|
* Return:
|
||||||
|
* 0 on success; -1 on failure
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
int netlib_setnodeaddr(FAR const char *ifname, FAR const uint8_t *nodeaddr)
|
||||||
|
{
|
||||||
|
int ret = ERROR;
|
||||||
|
|
||||||
|
if (ifname && nodeaddr)
|
||||||
|
{
|
||||||
|
/* Get a socket (only so that we get access to the INET subsystem) */
|
||||||
|
|
||||||
|
int sockfd = socket(PF_INET6, NETLIB_SOCK_IOCTL, 0);
|
||||||
|
if (sockfd >= 0)
|
||||||
|
{
|
||||||
|
struct ifreq req;
|
||||||
|
|
||||||
|
/* Put the driver name into the request */
|
||||||
|
|
||||||
|
strncpy(req.ifr_name, ifname, IFNAMSIZ);
|
||||||
|
|
||||||
|
/* Put the new MAC address into the request */
|
||||||
|
|
||||||
|
req.ifr_hwaddr.sa_family = AF_INET6;
|
||||||
|
memcpy(&req.ifr_hwaddr.sa_data, nodeaddr, CONFIG_NET_6LOWPAN_RIMEADDR_SIZE);
|
||||||
|
|
||||||
|
/* Perform the ioctl to set the MAC address */
|
||||||
|
|
||||||
|
ret = ioctl(sockfd, SIOCSIFHWADDR, (unsigned long)&req);
|
||||||
|
close(sockfd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* CONFIG_NET_6LOWPAN && CONFIG_NSOCKET_DESCRIPTORS */
|
@ -84,6 +84,20 @@
|
|||||||
* Pre-processor Definitions
|
* Pre-processor Definitions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
/* Only Ethernet and 6loWPAN have MAC layer addresses */
|
||||||
|
|
||||||
|
#undef HAVE_MAC
|
||||||
|
#if defined(CONFIG_NET_ETHERNET) || defined(CONFIG_NET_6LOWPAN)
|
||||||
|
# define HAVE_MAC 1
|
||||||
|
# if defined(CONFIG_NET_6LOWPAN)
|
||||||
|
# if (CONFIG_NET_6LOWPAN_RIMEADDR_SIZE == 2) && CONFIG_NSH_MACADDR > 0xffff
|
||||||
|
# error Invalid 6loWPAN node address for SIZE == 2
|
||||||
|
# elif (CONFIG_NET_6LOWPAN_RIMEADDR_SIZE == 8) && CONFIG_NSH_MACADDR > 0xffffffffffffffffull
|
||||||
|
# error Invalid 6loWPAN node address for SIZE == 8
|
||||||
|
# endif
|
||||||
|
# endif
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(CONFIG_NSH_DRIPADDR) && !defined(CONFIG_NSH_DNSIPADDR)
|
#if defined(CONFIG_NSH_DRIPADDR) && !defined(CONFIG_NSH_DNSIPADDR)
|
||||||
# define CONFIG_NSH_DNSIPADDR CONFIG_NSH_DRIPADDR
|
# define CONFIG_NSH_DNSIPADDR CONFIG_NSH_DRIPADDR
|
||||||
#endif
|
#endif
|
||||||
@ -226,19 +240,25 @@ static void nsh_netinit_configure(void)
|
|||||||
#ifdef CONFIG_NET_IPv4
|
#ifdef CONFIG_NET_IPv4
|
||||||
struct in_addr addr;
|
struct in_addr addr;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(CONFIG_NSH_DHCPC)
|
#if defined(CONFIG_NSH_DHCPC)
|
||||||
FAR void *handle;
|
FAR void *handle;
|
||||||
#endif
|
#endif
|
||||||
#if (defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_NOMAC)) && \
|
|
||||||
defined(CONFIG_NET_ETHERNET)
|
#if (defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_NOMAC)) && defined(HAVE_MAC)
|
||||||
|
#if defined(CONFIG_NET_ETHERNET)
|
||||||
uint8_t mac[IFHWADDRLEN];
|
uint8_t mac[IFHWADDRLEN];
|
||||||
|
#elif defined(CONFIG_NET_6LOWPAN)
|
||||||
|
uint8_t nodeaddr[CONFIG_NET_6LOWPAN_RIMEADDR_SIZE];
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ninfo("Entry\n");
|
ninfo("Entry\n");
|
||||||
|
|
||||||
/* Many embedded network interfaces must have a software assigned MAC */
|
/* Many embedded network interfaces must have a software assigned MAC */
|
||||||
|
|
||||||
#if defined(CONFIG_NSH_NOMAC) && defined(CONFIG_NET_ETHERNET)
|
#if defined(CONFIG_NSH_NOMAC) && defined(HAVE_MAC)
|
||||||
|
#if defined(CONFIG_NET_ETHERNET)
|
||||||
/* Use the configured, fixed MAC address */
|
/* Use the configured, fixed MAC address */
|
||||||
|
|
||||||
mac[0] = (CONFIG_NSH_MACADDR >> (8 * 5)) & 0xff;
|
mac[0] = (CONFIG_NSH_MACADDR >> (8 * 5)) & 0xff;
|
||||||
@ -252,7 +272,29 @@ static void nsh_netinit_configure(void)
|
|||||||
|
|
||||||
netlib_setmacaddr(NET_DEVNAME, mac);
|
netlib_setmacaddr(NET_DEVNAME, mac);
|
||||||
|
|
||||||
#endif /* CONFIG_NSH_NOMAC && CONFIG_NET_ETHERNET */
|
#elif defined(CONFIG_NET_6LOWPAN)
|
||||||
|
/* Use the configured, fixed MAC address */
|
||||||
|
|
||||||
|
#if CONFIG_NET_6LOWPAN_RIMEADDR_SIZE == 2
|
||||||
|
nodeaddr[0] = (CONFIG_NSH_MACADDR >> (8 * 1)) & 0xff;
|
||||||
|
nodeaddr[1] = (CONFIG_NSH_MACADDR >> (8 * 0)) & 0xff;
|
||||||
|
#elif CONFIG_NET_6LOWPAN_RIMEADDR_SIZE == 8
|
||||||
|
nodeaddr[0] = (CONFIG_NSH_MACADDR >> (8 * 7)) & 0xff;
|
||||||
|
nodeaddr[1] = (CONFIG_NSH_MACADDR >> (8 * 6)) & 0xff;
|
||||||
|
nodeaddr[2] = (CONFIG_NSH_MACADDR >> (8 * 5)) & 0xff;
|
||||||
|
nodeaddr[3] = (CONFIG_NSH_MACADDR >> (8 * 4)) & 0xff;
|
||||||
|
nodeaddr[4] = (CONFIG_NSH_MACADDR >> (8 * 3)) & 0xff;
|
||||||
|
nodeaddr[5] = (CONFIG_NSH_MACADDR >> (8 * 2)) & 0xff;
|
||||||
|
nodeaddr[6] = (CONFIG_NSH_MACADDR >> (8 * 1)) & 0xff;
|
||||||
|
nodeaddr[7] = (CONFIG_NSH_MACADDR >> (8 * 0)) & 0xff;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Set the 6loWPAN node address */
|
||||||
|
|
||||||
|
netlib_setnodeaddr(NET_DEVNAME, nodeaddr);
|
||||||
|
|
||||||
|
#endif /* CONFIG_NET_ETHERNET */
|
||||||
|
#endif /* CONFIG_NSH_NOMAC && HAVE_MAC */
|
||||||
|
|
||||||
#ifdef CONFIG_NET_IPv4
|
#ifdef CONFIG_NET_IPv4
|
||||||
/* Set up our host address */
|
/* Set up our host address */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user