From c2bdd49421d3d1bc2d3cc9365862246cb55a0864 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Wed, 19 Apr 2017 18:10:19 -0600 Subject: [PATCH] netlib and NSH: Add logic to set the IEEE802.15.4 PAN ID. --- include/netutils/netlib.h | 2 + netutils/netlib/Makefile | 3 +- netutils/netlib/netlib_getpanid.c | 109 +++++++++++++++++++++++++++ netutils/netlib/netlib_setnodeaddr.c | 2 +- netutils/netlib/netlib_setpanid.c | 109 +++++++++++++++++++++++++++ nshlib/Kconfig | 9 +++ nshlib/nsh_netinit.c | 5 +- 7 files changed, 236 insertions(+), 3 deletions(-) create mode 100644 netutils/netlib/netlib_getpanid.c create mode 100644 netutils/netlib/netlib_setpanid.c diff --git a/include/netutils/netlib.h b/include/netutils/netlib.h index 4e1ff9fa6..46225819c 100644 --- a/include/netutils/netlib.h +++ b/include/netutils/netlib.h @@ -117,7 +117,9 @@ int netlib_getmacaddr(FAR const char *ifname, FAR uint8_t *macaddr); #ifdef CONFIG_NET_6LOWPAN /* Get IEEE802.15.4 MAC driver node address */ +int netlib_getpanid(FAR const char *ifname, FAR uint16_t *panid); int netlib_setnodeaddr(FAR const char *ifname, FAR const uint8_t *nodeaddr); +int netlib_setpanid(FAR const char *ifname, uint16_t panid); bool netlib_nodeaddrconv(FAR const char *hwstr, FAR uint8_t *hw); #endif diff --git a/netutils/netlib/Makefile b/netutils/netlib/Makefile index b5a8f96d2..a2233036e 100644 --- a/netutils/netlib/Makefile +++ b/netutils/netlib/Makefile @@ -87,7 +87,8 @@ CSRCS += netlib_setmacaddr.c netlib_getmacaddr.c endif ifeq ($(CONFIG_NET_6LOWPAN),y) -CSRCS += netlib_setnodeaddr.c netlib_nodeaddrconv.c +CSRCS += netlib_getpanid.c netlib_setnodeaddr.c netlib_setpanid.c +CSRCS += netlib_nodeaddrconv.c endif # IGMP support diff --git a/netutils/netlib/netlib_getpanid.c b/netutils/netlib/netlib_getpanid.c new file mode 100644 index 000000000..cd0a5ba67 --- /dev/null +++ b/netutils/netlib/netlib_getpanid.c @@ -0,0 +1,109 @@ +/**************************************************************************** + * netutils/netlib/netlib_getpanid.c + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include "netutils/netlib.h" + +#if defined(CONFIG_NET_6LOWPAN) && CONFIG_NSOCKET_DESCRIPTORS > 0 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_getpanid + * + * Description: + * Return the current PAN ID + * + * Parameters: + * ifname The name of the interface to use + * panid The location to return the current PAN ID + * + * Return: + * 0 on success; -1 on failure. errno will be set on failure. + * + ****************************************************************************/ + +int netlib_getpanid(FAR const char *ifname, FAR uint16_t *panid) +{ + int ret = ERROR; + + if (ifname != NULL && panid != NULL) + { + /* 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 sixlowpan_req_s req; + + /* Put the driver name into the request */ + + strncpy(req.ifr_name, ifname, IFNAMSIZ); + + /* Perform the ioctl to get the current PAN ID */ + + ret = ioctl(sockfd, SIOCGWPANID, (unsigned long)((uintptr_t)&req)); + close(sockfd); + + /* Reuturn the current PAN ID */ + + *panid = req.u.panid.panid; + } + } + + return ret; +} + +#endif /* CONFIG_NET_6LOWPAN && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/netutils/netlib/netlib_setnodeaddr.c b/netutils/netlib/netlib_setnodeaddr.c index 436591eb9..81e8f88d8 100644 --- a/netutils/netlib/netlib_setnodeaddr.c +++ b/netutils/netlib/netlib_setnodeaddr.c @@ -96,7 +96,7 @@ int netlib_setnodeaddr(FAR const char *ifname, FAR const uint8_t *nodeaddr) req.ifr_hwaddr.sa_family = AF_INET6; memcpy(&req.ifr_hwaddr.sa_data, nodeaddr, NET_6LOWPAN_RIMEADDR_SIZE); - /* Perform the ioctl to set the MAC address */ + /* Perform the ioctl to set the node address */ ret = ioctl(sockfd, SIOCSIFHWADDR, (unsigned long)&req); close(sockfd); diff --git a/netutils/netlib/netlib_setpanid.c b/netutils/netlib/netlib_setpanid.c new file mode 100644 index 000000000..9ec7e988a --- /dev/null +++ b/netutils/netlib/netlib_setpanid.c @@ -0,0 +1,109 @@ +/**************************************************************************** + * netutils/netlib/netlib_setpanid.c + * + * Copyright (C) 2017 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include "netutils/netlib.h" + +#if defined(CONFIG_NET_6LOWPAN) && CONFIG_NSOCKET_DESCRIPTORS > 0 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_setpanid + * + * Description: + * Join the specified PAN ID + * + * Parameters: + * ifname The name of the interface to use + * panid The PAN ID to join + * + * Return: + * 0 on success; -1 on failure. errno will be set on failure. + * + ****************************************************************************/ + +int netlib_setpanid(FAR const char *ifname, uint16_t panid) +{ + int ret = ERROR; + + if (ifname != NULL) + { + /* 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 sixlowpan_req_s req; + + /* Put the driver name into the request */ + + strncpy(req.ifr_name, ifname, IFNAMSIZ); + + /* Put the new PAN ID into the request */ + + req.u.panid.panid = panid; + + /* Perform the ioctl to set the new PAN ID */ + + ret = ioctl(sockfd, SIOCSWPANID, (unsigned long)((uintptr_t)&req)); + close(sockfd); + } + } + + return ret; +} + +#endif /* CONFIG_NET_6LOWPAN && CONFIG_NSOCKET_DESCRIPTORS */ diff --git a/nshlib/Kconfig b/nshlib/Kconfig index 0a603c733..b402b5ac0 100644 --- a/nshlib/Kconfig +++ b/nshlib/Kconfig @@ -1456,6 +1456,15 @@ config NSH_MACADDR MAC address must provided with this selection. endif # NSH_NOMAC + +config NSH_PANID + hex "6loWPAN PAN ID" + default 0xface + depends on NET_6LOWPAN + range 0x0000 0xffff + ---help--- + Select the PAN ID to join upon initialization. + endif # NSH_NETINIT config NSH_MAX_ROUNDTRIP diff --git a/nshlib/nsh_netinit.c b/nshlib/nsh_netinit.c index f1f1f557f..fd2159f1f 100644 --- a/nshlib/nsh_netinit.c +++ b/nshlib/nsh_netinit.c @@ -295,8 +295,11 @@ static void nsh_netinit_configure(void) /* Set the 6loWPAN node address */ - netlib_setnodeaddr(NET_DEVNAME, nodeaddr); + (void)netlib_setnodeaddr(NET_DEVNAME, nodeaddr); + /* Set the 6loWPAN PAN ID */ + + (void)netlib_setpanid(NET_DEVNAME, CONFIG_NSH_PANID); #endif /* CONFIG_NET_ETHERNET */ #endif /* CONFIG_NSH_NOMAC && HAVE_MAC */