2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/nshlib/nsh_netinit.c
|
|
|
|
*
|
2015-01-18 21:17:00 +01:00
|
|
|
* Copyright (C) 2010-2012, 2014-2015 Gregory Nutt. All rights reserved.
|
2012-02-02 17:04:09 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
|
|
|
* This is influenced by similar logic from uIP:
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
* Copyright (c) 2003, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* 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 of the Institute 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 INSTITUTE 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 INSTITUTE 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>
|
|
|
|
|
2014-08-17 17:51:26 +02:00
|
|
|
/* Is network initialization debug forced on? */
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_NETINIT_DEBUG
|
|
|
|
# undef CONFIG_DEBUG_VERBOSE
|
|
|
|
# define CONFIG_DEBUG_VERBOSE 1
|
|
|
|
# undef CONFIG_DEBUG_NET
|
|
|
|
# define CONFIG_DEBUG_NET 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2014-08-06 19:59:41 +02:00
|
|
|
#include <pthread.h>
|
2014-08-17 22:48:27 +02:00
|
|
|
#include <semaphore.h>
|
2014-08-17 17:51:26 +02:00
|
|
|
#include <signal.h>
|
2014-08-17 22:48:27 +02:00
|
|
|
#include <assert.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
2014-07-05 03:13:08 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
2011-03-19 18:29:36 +01:00
|
|
|
|
2014-08-17 17:51:26 +02:00
|
|
|
#include <nuttx/net/mii.h>
|
|
|
|
|
2014-07-03 00:04:25 +02:00
|
|
|
#include <apps/netutils/netlib.h>
|
2012-11-03 01:00:56 +01:00
|
|
|
#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS)
|
2014-04-11 20:41:13 +02:00
|
|
|
# include <apps/netutils/dnsclient.h>
|
2011-03-19 18:29:36 +01:00
|
|
|
# include <apps/netutils/dhcpc.h>
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "nsh.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-08-17 22:48:27 +02:00
|
|
|
* Pre-processor Definitions
|
2011-03-18 20:46:25 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-28 14:20:36 +01:00
|
|
|
#if defined(CONFIG_NSH_DRIPADDR) && !defined(CONFIG_NSH_DNSIPADDR)
|
|
|
|
# define CONFIG_NSH_DNSIPADDR CONFIG_NSH_DRIPADDR
|
|
|
|
#endif
|
|
|
|
|
2014-06-27 20:46:54 +02:00
|
|
|
/* SLIP-specific configuration
|
|
|
|
*
|
|
|
|
* REVISIT: How will we handle Ethernet and SLIP networks together? In the
|
|
|
|
* future, NSH will need to be extended to handle multiple networks with
|
|
|
|
* mixed transports.
|
|
|
|
*/
|
|
|
|
|
2014-11-15 20:52:39 +01:00
|
|
|
#ifdef CONFIG_NET_MULILINK
|
|
|
|
# warning REVISIT: CONFIG_NET_MULILINK multilink support incomplete
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If both SLIP and Ethernet interfaces are present, only the Ethernet
|
|
|
|
* interface will be initialized.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_ETHERNET)
|
|
|
|
# define NET_DEVNAME "eth0"
|
|
|
|
#elif defined(CONFIG_NET_SLIP)
|
2014-06-27 20:46:54 +02:00
|
|
|
# define NET_DEVNAME "sl0"
|
|
|
|
# ifndef CONFIG_NSH_NOMAC
|
|
|
|
# error "CONFIG_NSH_NOMAC must be defined for SLIP"
|
|
|
|
# endif
|
2015-01-27 21:11:46 +01:00
|
|
|
#elif !defined(CONFIG_NET_LOCAL)
|
2014-11-15 20:52:39 +01:00
|
|
|
# error ERROR: No link layer protocol defined
|
2014-06-27 20:46:54 +02:00
|
|
|
#endif
|
|
|
|
|
2015-02-03 23:19:08 +01:00
|
|
|
/* We need a valid IP domain (any domain) to create a socket that we can use
|
|
|
|
* to comunicate with the network device.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if defined(CONFIG_NET_IPv4)
|
|
|
|
# define AF_INETX AF_INET
|
|
|
|
#elif defined(CONFIG_NET_IPv6)
|
|
|
|
# define AF_INETX AF_INET6
|
|
|
|
#endif
|
|
|
|
|
2014-08-17 17:51:26 +02:00
|
|
|
/* While the network is up, the network monitor really does nothing. It
|
|
|
|
* will wait for a very long time while waiting, it can be awakened by a
|
|
|
|
* signal indicating a change in network status.
|
|
|
|
*/
|
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
#define LONG_TIME_SEC (60*60) /* One hour in seconds */
|
|
|
|
#define SHORT_TIME_SEC (2) /* 2 seconds */
|
2014-08-17 17:51:26 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_NETINIT_MONITOR
|
|
|
|
static sem_t g_notify_sem;
|
|
|
|
#endif
|
|
|
|
|
2015-02-03 22:40:56 +01:00
|
|
|
#if defined(CONFIG_NET_IPv6) && !defined(CONFIG_NET_ICMPv6_AUTOCONF)
|
2015-02-03 15:01:55 +01:00
|
|
|
/* Host IPv6 address */
|
2015-01-19 16:03:10 +01:00
|
|
|
|
|
|
|
static const uint16_t g_ipv6_hostaddr[8] =
|
|
|
|
{
|
|
|
|
HTONS(CONFIG_NSH_IPv6ADDR_1),
|
|
|
|
HTONS(CONFIG_NSH_IPv6ADDR_2),
|
|
|
|
HTONS(CONFIG_NSH_IPv6ADDR_3),
|
|
|
|
HTONS(CONFIG_NSH_IPv6ADDR_4),
|
|
|
|
HTONS(CONFIG_NSH_IPv6ADDR_5),
|
|
|
|
HTONS(CONFIG_NSH_IPv6ADDR_6),
|
|
|
|
HTONS(CONFIG_NSH_IPv6ADDR_7),
|
|
|
|
HTONS(CONFIG_NSH_IPv6ADDR_8),
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Default routine IPv6 address */
|
|
|
|
|
|
|
|
static const uint16_t g_ipv6_draddr[8] =
|
|
|
|
{
|
|
|
|
HTONS(CONFIG_NSH_DRIPv6ADDR_1),
|
|
|
|
HTONS(CONFIG_NSH_DRIPv6ADDR_2),
|
|
|
|
HTONS(CONFIG_NSH_DRIPv6ADDR_3),
|
|
|
|
HTONS(CONFIG_NSH_DRIPv6ADDR_4),
|
|
|
|
HTONS(CONFIG_NSH_DRIPv6ADDR_5),
|
|
|
|
HTONS(CONFIG_NSH_DRIPv6ADDR_6),
|
|
|
|
HTONS(CONFIG_NSH_DRIPv6ADDR_7),
|
|
|
|
HTONS(CONFIG_NSH_DRIPv6ADDR_8),
|
|
|
|
};
|
|
|
|
|
|
|
|
/* IPv6 netmask */
|
|
|
|
|
|
|
|
static const uint16_t g_ipv6_netmask[8] =
|
|
|
|
{
|
|
|
|
HTONS(CONFIG_NSH_IPv6NETMASK_1),
|
|
|
|
HTONS(CONFIG_NSH_IPv6NETMASK_2),
|
|
|
|
HTONS(CONFIG_NSH_IPv6NETMASK_3),
|
|
|
|
HTONS(CONFIG_NSH_IPv6NETMASK_4),
|
|
|
|
HTONS(CONFIG_NSH_IPv6NETMASK_5),
|
|
|
|
HTONS(CONFIG_NSH_IPv6NETMASK_6),
|
|
|
|
HTONS(CONFIG_NSH_IPv6NETMASK_7),
|
|
|
|
HTONS(CONFIG_NSH_IPv6NETMASK_8),
|
|
|
|
};
|
2015-02-03 22:40:56 +01:00
|
|
|
#endif /* CONFIG_NET_IPv6 && !CONFIG_NET_ICMPv6_AUTOCONF*/
|
2015-01-19 16:03:10 +01:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-08-17 17:51:26 +02:00
|
|
|
* Name: nsh_netinit_configure
|
2011-03-18 20:46:25 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the network per the selected NuttX configuration
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-17 17:51:26 +02:00
|
|
|
static void nsh_netinit_configure(void)
|
2011-03-18 20:46:25 +01:00
|
|
|
{
|
2015-01-19 16:59:43 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2014-08-06 18:12:10 +02:00
|
|
|
struct in_addr addr;
|
2015-01-19 16:59:43 +01:00
|
|
|
#endif
|
2011-03-18 21:35:31 +01:00
|
|
|
#if defined(CONFIG_NSH_DHCPC)
|
2014-08-06 18:12:10 +02:00
|
|
|
FAR void *handle;
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
2014-11-15 20:52:39 +01:00
|
|
|
#if (defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_NOMAC)) && \
|
|
|
|
defined(CONFIG_NET_ETHERNET)
|
2014-08-06 18:12:10 +02:00
|
|
|
uint8_t mac[IFHWADDRLEN];
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
|
2014-08-06 19:59:41 +02:00
|
|
|
nvdbg("Entry\n");
|
|
|
|
|
2014-08-06 18:12:10 +02:00
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-11-15 20:52:39 +01:00
|
|
|
#if defined(CONFIG_NSH_NOMAC) && defined(CONFIG_NET_ETHERNET)
|
2014-08-06 18:12:10 +02:00
|
|
|
/* Use the configured, fixed MAC address */
|
|
|
|
|
2014-08-06 17:19:26 +02:00
|
|
|
mac[0] = (CONFIG_NSH_MACADDR >> (8 * 5)) & 0xff;
|
|
|
|
mac[1] = (CONFIG_NSH_MACADDR >> (8 * 4)) & 0xff;
|
|
|
|
mac[2] = (CONFIG_NSH_MACADDR >> (8 * 3)) & 0xff;
|
|
|
|
mac[3] = (CONFIG_NSH_MACADDR >> (8 * 2)) & 0xff;
|
|
|
|
mac[4] = (CONFIG_NSH_MACADDR >> (8 * 1)) & 0xff;
|
|
|
|
mac[5] = (CONFIG_NSH_MACADDR >> (8 * 0)) & 0xff;
|
2014-08-06 18:12:10 +02:00
|
|
|
|
|
|
|
/* Set the MAC address */
|
|
|
|
|
2014-07-03 00:52:02 +02:00
|
|
|
netlib_setmacaddr(NET_DEVNAME, mac);
|
2015-01-27 23:39:30 +01:00
|
|
|
|
|
|
|
#endif /* CONFIG_NSH_NOMAC && CONFIG_NET_ETHERNET */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2015-01-18 21:17:00 +01:00
|
|
|
#ifdef CONFIG_NET_IPv4
|
2011-03-18 20:46:25 +01:00
|
|
|
/* Set up our host address */
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#if !defined(CONFIG_NSH_DHCPC)
|
|
|
|
addr.s_addr = HTONL(CONFIG_NSH_IPADDR);
|
2011-03-18 20:46:25 +01:00
|
|
|
#else
|
|
|
|
addr.s_addr = 0;
|
|
|
|
#endif
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4addr(NET_DEVNAME, &addr);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Set up the default router address */
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
addr.s_addr = HTONL(CONFIG_NSH_DRIPADDR);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_dripv4addr(NET_DEVNAME, &addr);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
addr.s_addr = HTONL(CONFIG_NSH_NETMASK);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4netmask(NET_DEVNAME, &addr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_IPv6
|
2015-02-03 15:01:55 +01:00
|
|
|
#ifdef CONFIG_NET_ICMPv6_AUTOCONF
|
|
|
|
/* Perform ICMPv6 auto-configuration */
|
|
|
|
|
|
|
|
netlib_icmpv6_autoconfiguration(NET_DEVNAME);
|
|
|
|
|
2015-02-03 22:40:56 +01:00
|
|
|
#else /* CONFIG_NET_ICMPv6_AUTOCONF */
|
2015-02-03 15:01:55 +01:00
|
|
|
|
|
|
|
/* Set up our fixed host address */
|
2015-01-19 16:03:10 +01:00
|
|
|
|
|
|
|
netlib_set_ipv6addr(NET_DEVNAME,
|
|
|
|
(FAR const struct in6_addr *)g_ipv6_hostaddr);
|
|
|
|
|
|
|
|
/* Set up the default router address */
|
|
|
|
|
|
|
|
netlib_set_dripv6addr(NET_DEVNAME,
|
|
|
|
(FAR const struct in6_addr *)g_ipv6_draddr);
|
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
|
|
|
netlib_set_ipv6netmask(NET_DEVNAME,
|
|
|
|
(FAR const struct in6_addr *)g_ipv6_netmask);
|
2015-02-03 22:40:56 +01:00
|
|
|
|
|
|
|
#endif /* CONFIG_NET_ICMPv6_AUTOCONF */
|
|
|
|
#endif /* CONFIG_NET_IPv6 */
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2012-10-28 14:20:36 +01:00
|
|
|
#if defined(CONFIG_NSH_DHCPC) || defined(CONFIG_NSH_DNS)
|
2011-03-18 20:46:25 +01:00
|
|
|
/* Set up the resolver */
|
|
|
|
|
2014-04-11 20:25:32 +02:00
|
|
|
dns_bind();
|
2012-10-28 14:20:36 +01:00
|
|
|
#if defined(CONFIG_NSH_DNS)
|
|
|
|
addr.s_addr = HTONL(CONFIG_NSH_DNSIPADDR);
|
2014-04-11 20:25:32 +02:00
|
|
|
dns_setserver(&addr);
|
2012-10-28 14:20:36 +01:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif
|
|
|
|
|
2011-03-18 21:35:31 +01:00
|
|
|
#if defined(CONFIG_NSH_DHCPC)
|
2011-03-18 20:46:25 +01:00
|
|
|
/* Get the MAC address of the NIC */
|
|
|
|
|
2014-07-03 00:52:02 +02:00
|
|
|
netlib_getmacaddr(NET_DEVNAME, mac);
|
2011-03-18 20:46:25 +01:00
|
|
|
|
|
|
|
/* Set up the DHCPC modules */
|
|
|
|
|
|
|
|
handle = dhcpc_open(&mac, IFHWADDRLEN);
|
|
|
|
|
|
|
|
/* Get an IP address. Note that there is no logic for renewing the IP address in this
|
|
|
|
* example. The address should be renewed in ds.lease_time/2 seconds.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
struct dhcpc_state ds;
|
|
|
|
(void)dhcpc_request(handle, &ds);
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4addr(NET_DEVNAME, &ds.ipaddr);
|
2014-06-27 20:46:54 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
if (ds.netmask.s_addr != 0)
|
|
|
|
{
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_ipv4netmask(NET_DEVNAME, &ds.netmask);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-06-27 20:46:54 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
if (ds.default_router.s_addr != 0)
|
|
|
|
{
|
2015-01-18 21:17:00 +01:00
|
|
|
netlib_set_dripv4addr(NET_DEVNAME, &ds.default_router);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-06-27 20:46:54 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
if (ds.dnsaddr.s_addr != 0)
|
|
|
|
{
|
2014-04-11 20:25:32 +02:00
|
|
|
dns_setserver(&ds.dnsaddr);
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-06-27 20:46:54 +02:00
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
dhcpc_close(handle);
|
|
|
|
}
|
|
|
|
#endif
|
2012-09-17 20:18:44 +02:00
|
|
|
|
2014-08-06 19:59:41 +02:00
|
|
|
nvdbg("Exit\n");
|
2014-08-17 17:51:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_netinit_signal
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This signal handler responds to changes in PHY status.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_NETINIT_MONITOR
|
|
|
|
static void nsh_netinit_signal(int signo, FAR siginfo_t *siginfo,
|
|
|
|
FAR void * context)
|
|
|
|
{
|
2014-08-17 22:48:27 +02:00
|
|
|
int semcount;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* What is the count on the semaphore? Don't over-post */
|
|
|
|
|
|
|
|
ret = sem_getvalue(&g_notify_sem, &semcount);
|
|
|
|
nlldbg("Entry: semcount=%d\n", semcount);
|
|
|
|
|
|
|
|
if (ret == OK && semcount <= 0)
|
|
|
|
{
|
|
|
|
sem_post(&g_notify_sem);
|
|
|
|
}
|
2014-08-17 17:51:26 +02:00
|
|
|
|
|
|
|
nllvdbg("Exit\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_netinit_monitor
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Monitor link status, gracefully taking the link up and down as the
|
|
|
|
* link becomes available or as the link is lost.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_NETINIT_MONITOR
|
|
|
|
static int nsh_netinit_monitor(void)
|
|
|
|
{
|
2014-08-17 22:48:27 +02:00
|
|
|
struct timespec abstime;
|
|
|
|
struct timespec reltime;
|
2014-08-17 17:51:26 +02:00
|
|
|
struct ifreq ifr;
|
|
|
|
struct sigaction act;
|
|
|
|
bool devup;
|
|
|
|
int ret;
|
|
|
|
int sd;
|
|
|
|
|
|
|
|
nvdbg("Entry\n");
|
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
/* Initialize the notification semaphore */
|
|
|
|
|
|
|
|
DEBUGVERIFY(sem_init(&g_notify_sem, 0, 0));
|
|
|
|
|
2014-08-17 17:51:26 +02:00
|
|
|
/* Get a socket descriptor that we can use to communicate with the network
|
|
|
|
* interface driver.
|
|
|
|
*/
|
|
|
|
|
2015-02-03 23:19:08 +01:00
|
|
|
sd = socket(AF_INETX, SOCK_DGRAM, 0);
|
2014-08-17 17:51:26 +02:00
|
|
|
if (sd < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
|
|
|
ndbg("ERROR: Failed to create a socket: %d\n", ret);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attach a signal handler so that we do not lose PHY events */
|
|
|
|
|
|
|
|
act.sa_sigaction = nsh_netinit_signal;
|
|
|
|
act.sa_flags = SA_SIGINFO;
|
|
|
|
|
|
|
|
ret = sigaction(CONFIG_NSH_NETINIT_SIGNO, &act, NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
|
|
|
ndbg("ERROR: sigaction() failed: %d\n", ret);
|
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now loop, waiting for changes in link status */
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2014-08-18 00:51:56 +02:00
|
|
|
/* Configure to receive a signal on changes in link status */
|
2014-08-17 17:51:26 +02:00
|
|
|
|
|
|
|
strncpy(ifr.ifr_name, NET_DEVNAME, IFNAMSIZ);
|
|
|
|
|
2014-08-18 00:51:56 +02:00
|
|
|
ifr.ifr_mii_notify_pid = 0; /* PID=0 means this task */
|
|
|
|
ifr.ifr_mii_notify_signo = CONFIG_NSH_NETINIT_SIGNO;
|
|
|
|
ifr.ifr_mii_notify_arg = NULL;
|
|
|
|
|
|
|
|
ret = ioctl(sd, SIOCMIINOTIFY, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
|
|
|
ndbg("ERROR: ioctl(SIOCMIINOTIFY) failed: %d\n", ret);
|
|
|
|
goto errout_with_sigaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does the driver think that the link is up or down? */
|
|
|
|
|
2014-08-17 17:51:26 +02:00
|
|
|
ret = ioctl(sd, SIOCGIFFLAGS, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
|
|
|
ndbg("ERROR: ioctl(SIOCGIFFLAGS) failed: %d\n", ret);
|
|
|
|
goto errout_with_notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
devup = ((ifr.ifr_flags & IFF_UP) != 0);
|
|
|
|
|
|
|
|
/* Get the current PHY address in use. This probably does not change,
|
|
|
|
* but just in case...
|
|
|
|
*
|
|
|
|
* NOTE: We are assuming that the network device name is preserved in
|
|
|
|
* the ifr structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = ioctl(sd, SIOCGMIIPHY, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
|
|
|
ndbg("ERROR: ioctl(SIOCGMIIPHY) failed: %d\n", ret);
|
|
|
|
goto errout_with_notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the PHY status register */
|
|
|
|
|
|
|
|
ifr.ifr_mii_reg_num = MII_MSR;
|
|
|
|
|
|
|
|
ret = ioctl(sd, SIOCGMIIREG, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
|
|
|
ndbg("ERROR: ioctl(SIOCGMIIREG) failed: %d\n", ret);
|
|
|
|
goto errout_with_notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
nvdbg("%s: devup=%d PHY address=%02x MSR=%04x\n",
|
2014-08-17 19:26:02 +02:00
|
|
|
ifr.ifr_name, devup, ifr.ifr_mii_phy_id, ifr.ifr_mii_val_out);
|
2014-08-17 17:51:26 +02:00
|
|
|
|
|
|
|
/* Check for link up or down */
|
|
|
|
|
|
|
|
if ((ifr.ifr_mii_val_out & MII_MSR_LINKSTATUS) != 0)
|
|
|
|
{
|
|
|
|
/* Link up... does the drive think that the link is up? */
|
|
|
|
|
|
|
|
if (!devup)
|
|
|
|
{
|
|
|
|
/* No... We just transitioned from link down to link up.
|
|
|
|
* Bring the link up.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nvdbg("Bringing the link up\n");
|
|
|
|
|
|
|
|
ifr.ifr_flags = IFF_UP;
|
|
|
|
ret = ioctl(sd, SIOCSIFFLAGS, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
|
|
|
ndbg("ERROR: ioctl(SIOCSIFFLAGS) failed: %d\n", ret);
|
|
|
|
goto errout_with_notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And wait for a short delay. We will want to recheck the
|
|
|
|
* link status again soon.
|
|
|
|
*/
|
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
reltime.tv_sec = SHORT_TIME_SEC;
|
|
|
|
reltime.tv_nsec = 0;
|
2014-08-17 17:51:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The link is still up. Take a long, well-deserved rest */
|
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
reltime.tv_sec = LONG_TIME_SEC;
|
|
|
|
reltime.tv_nsec = 0;
|
2014-08-17 17:51:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Link down... Was the driver link state already down? */
|
|
|
|
|
|
|
|
if (devup)
|
|
|
|
{
|
|
|
|
/* No... we just transitioned from link up to link down. Take
|
|
|
|
* the link down.
|
|
|
|
*/
|
|
|
|
|
|
|
|
nvdbg("Taking the link down\n");
|
|
|
|
|
|
|
|
ifr.ifr_flags = IFF_DOWN;
|
|
|
|
ret = ioctl(sd, SIOCSIFFLAGS, (unsigned long)&ifr);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ret = -errno;
|
|
|
|
DEBUGASSERT(ret < 0);
|
|
|
|
|
|
|
|
ndbg("ERROR: ioctl(SIOCSIFFLAGS) failed: %d\n", ret);
|
|
|
|
goto errout_with_notification;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* In either case, wait for the short, configurable delay */
|
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
reltime.tv_sec = CONFIG_NSH_NETINIT_RETRYMSEC / 1000;
|
|
|
|
reltime.tv_nsec = (CONFIG_NSH_NETINIT_RETRYMSEC % 1000) * 1000000;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now wait for either the semaphore to be posted for a timed-out to
|
|
|
|
* occur.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sched_lock();
|
|
|
|
DEBUGVERIFY(clock_gettime(CLOCK_REALTIME, &abstime));
|
|
|
|
|
|
|
|
abstime.tv_sec += reltime.tv_sec;
|
|
|
|
abstime.tv_nsec += reltime.tv_nsec;
|
2015-02-20 14:07:36 +01:00
|
|
|
if (abstime.tv_nsec >= 1000000000)
|
2014-08-17 22:48:27 +02:00
|
|
|
{
|
|
|
|
abstime.tv_sec++;
|
|
|
|
abstime.tv_nsec -= 1000000000;
|
2014-08-17 17:51:26 +02:00
|
|
|
}
|
2014-08-17 22:48:27 +02:00
|
|
|
|
2014-08-18 00:51:56 +02:00
|
|
|
(void)sem_timedwait(&g_notify_sem, &abstime);
|
2014-08-17 22:48:27 +02:00
|
|
|
sched_unlock();
|
2014-08-17 17:51:26 +02:00
|
|
|
}
|
|
|
|
|
2014-08-17 22:48:27 +02:00
|
|
|
/* TODO: Stop the PHY notifications and remove the signal handler. */
|
2014-08-17 17:51:26 +02:00
|
|
|
|
|
|
|
errout_with_notification:
|
|
|
|
# warning Missing logic
|
|
|
|
errout_with_sigaction:
|
|
|
|
# warning Missing logic
|
|
|
|
errout_with_socket:
|
|
|
|
close(sd);
|
|
|
|
errout:
|
|
|
|
ndbg("Aborting\n");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_netinit_thread
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the network per the selected NuttX configuration
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-09-17 00:08:06 +02:00
|
|
|
#ifdef CONFIG_NSH_NETINIT_THREAD
|
2014-08-17 17:51:26 +02:00
|
|
|
static pthread_addr_t nsh_netinit_thread(pthread_addr_t arg)
|
|
|
|
{
|
|
|
|
nvdbg("Entry\n");
|
|
|
|
|
|
|
|
/* Configure the network */
|
|
|
|
|
|
|
|
nsh_netinit_configure();
|
|
|
|
|
|
|
|
#ifdef CONFIG_NSH_NETINIT_MONITOR
|
|
|
|
/* Monitor the network status */
|
|
|
|
|
|
|
|
nsh_netinit_monitor();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nvdbg("Exit\n");
|
|
|
|
return NULL;
|
2011-03-18 20:46:25 +01:00
|
|
|
}
|
2014-09-17 00:08:06 +02:00
|
|
|
#endif
|
2011-03-18 20:46:25 +01:00
|
|
|
|
2014-08-06 19:59:41 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nsh_netinit
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize the network per the selected NuttX configuration
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int nsh_netinit(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NSH_NETINIT_THREAD
|
|
|
|
struct sched_param sparam;
|
|
|
|
pthread_attr_t attr;
|
|
|
|
pthread_t tid;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Start the network initialization thread to perform the network bring-up
|
|
|
|
* asynchronously.
|
|
|
|
*/
|
|
|
|
|
|
|
|
pthread_attr_init(&attr);
|
|
|
|
sparam.sched_priority = CONFIG_NSH_NETINIT_THREAD_PRIORITY;
|
|
|
|
(void)pthread_attr_setschedparam(&attr, &sparam);
|
|
|
|
(void)pthread_attr_setstacksize(&attr, CONFIG_NSH_NETINIT_THREAD_STACKSIZE);
|
|
|
|
|
|
|
|
nvdbg("Starting netinit thread\n");
|
|
|
|
ret = pthread_create(&tid, &attr, nsh_netinit_thread, NULL);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
|
|
|
ndbg("ERROR: Failed to create netinit thread: %d\n", ret);
|
|
|
|
(void)nsh_netinit_thread(NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Detach the thread because we will not be joining to it */
|
|
|
|
|
|
|
|
(void)pthread_detach(tid);
|
|
|
|
|
|
|
|
/* Name the thread */
|
|
|
|
|
|
|
|
pthread_setname_np(tid, "netinit");
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* Perform network initialization sequentially */
|
|
|
|
|
2014-08-17 17:51:26 +02:00
|
|
|
nsh_netinit_configure();
|
2014-09-17 00:08:06 +02:00
|
|
|
return OK;
|
2014-08-06 19:59:41 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-03-18 20:46:25 +01:00
|
|
|
#endif /* CONFIG_NET */
|