Example #1
0
int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
{
  FAR const char *devfmt;
#ifdef CONFIG_NET_USER_DEVFMT
  FAR const char devfmt_str[IFNAMSIZ];
#endif
  int devnum;

  if (dev)
    {
#ifdef CONFIG_NET_MULTILINK
      /* We are supporting multiple network devices and using different link
       * level protocols.  Set the protocol usd by the device and the size
       * level protocols.  Set the protocol used by the device and the size
       * of the link header used by this protocol.
       */

      switch (lltype)
        {
#ifdef CONFIG_NET_LOOPBACK
          case NET_LL_LOOPBACK:   /* Local loopback */
            dev->d_llhdrlen = 0;
            dev->d_mtu      = NET_LO_MTU;
#ifdef CONFIG_NET_TCP
            dev->d_recvwndo = NET_LO_TCP_RECVWNDO;
#endif
            devfmt          = NETDEV_LO_FORMAT;
            break;
#endif

#ifdef CONFIG_NET_ETHERNET
          case NET_LL_ETHERNET:   /* Ethernet */
            dev->d_llhdrlen = ETH_HDRLEN;
            dev->d_mtu      = CONFIG_NET_ETH_MTU;
#ifdef CONFIG_NET_TCP
            dev->d_recvwndo = CONFIG_NET_ETH_TCP_RECVWNDO;
#endif
            devfmt          = NETDEV_ETH_FORMAT;
            break;
#endif

#ifdef CONFIG_NET_6LOWPAN
          case NET_LL_6LOWPAN:    /* IEEE 802.15.4 */
            dev->d_llhdrlen = 0;  /* REVISIT */
            dev->d_mtu      = CONFIG_NET_6LOWPAN_MTU;
#ifdef CONFIG_NET_TCP
            dev->d_recvwndo = CONFIG_NET_6LOWPAN_TCP_RECVWNDO;
#endif
            devfmt          = NETDEV_WPAN_FORMAT;
            break;
#endif

#ifdef CONFIG_NET_SLIP
          case NET_LL_SLIP:       /* Serial Line Internet Protocol (SLIP) */
            dev->d_llhdrlen = 0;
            dev->d_mtu      = CONFIG_NET_SLIP_MTU;
#ifdef CONFIG_NET_TCP
            dev->d_recvwndo = CONFIG_NET_SLIP_TCP_RECVWNDO;
#endif
            devfmt          = NETDEV_SLIP_FORMAT;
            break;
#endif

#ifdef CONFIG_NET_TUN
          case NET_LL_TUN:        /* Virtual Network Device (TUN) */
            dev->d_llhdrlen = 0;
            dev->d_mtu      = CONFIG_NET_TUN_MTU;
#ifdef CONFIG_NET_TCP
            dev->d_recvwndo = CONFIG_NET_TUN_TCP_RECVWNDO;
#endif
            devfmt          = NETDEV_TUN_FORMAT;
            break;
#endif

          default:
            nerr("ERROR: Unrecognized link type: %d\n", lltype);
            return -EINVAL;
        }

      /* Remember the verified link type */

      dev->d_lltype = (uint8_t)lltype;

#else
     /* Use the default device name */

     devfmt = NETDEV_DEFAULT_FORMAT;
#endif

      /* There are no clients of the device yet */

      dev->d_conncb = NULL;
      dev->d_devcb = NULL;

      /* Get the next available device number and assign a device name to
       * the interface
       */

      net_lock();

#ifdef CONFIG_NET_MULTILINK
#  ifdef CONFIG_NET_LOOPBACK
      /* The local loopback device is a special case:  There can be only one
       * local loopback device so it is unnumbered.
       */

      if (lltype == NET_LL_LOOPBACK)
        {
          devnum = 0;
        }
      else
#  endif
        {
          devnum = find_devnum(devfmt);
        }
#else
      /* There is only a single link type.  Finding the next network device
       * number is simple.
       */

      devnum = g_next_devnum++;
#endif

#ifdef CONFIG_NET_USER_DEVFMT
      if (*dev->d_ifname)
        {
          strncpy(devfmt_str, dev->d_ifname, IFNAMSIZ);
          devfmt = devfmt_str;
        }
#endif

      snprintf(dev->d_ifname, IFNAMSIZ, devfmt, devnum);

      /* Add the device to the list of known network devices */

      dev->flink  = g_netdevices;
      g_netdevices = dev;

      /* Configure the device for IGMP support */

#ifdef CONFIG_NET_IGMP
      igmp_devinit(dev);
#endif
      net_unlock();

#ifdef CONFIG_NET_ETHERNET
      ninfo("Registered MAC: %02x:%02x:%02x:%02x:%02x:%02x as dev: %s\n",
            dev->d_mac.ether_addr_octet[0], dev->d_mac.ether_addr_octet[1],
            dev->d_mac.ether_addr_octet[2], dev->d_mac.ether_addr_octet[3],
            dev->d_mac.ether_addr_octet[4], dev->d_mac.ether_addr_octet[5],
            dev->d_ifname);
#else
      ninfo("Registered dev: %s\n", dev->d_ifname);
#endif
      return OK;
    }

  return -EINVAL;
}
int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
{
  FAR const char *devfmt;
#ifdef CONFIG_NET_USER_DEVFMT
  FAR const char devfmt_str[IFNAMSIZ];
#endif
  int devnum;

  if (dev)
    {
#ifdef CONFIG_NET_MULTILINK
      /* We are supporting multiple network devices and using different link
       * level protocols.  Set the protocol usd by the device and the size
       * level protocols.  Set the protocol used by the device and the size
       * of the link header used by this protocol.
       */

      switch (lltype)
        {
#ifdef CONFIG_NET_ETHERNET
          case NET_LL_ETHERNET:  /* Ethernet */
            dev->d_llhdrlen = ETH_HDRLEN;
            dev->d_mtu      = CONFIG_NET_ETH_MTU;
#ifdef CONFIG_NET_TCP_STACK
            dev->d_recvwndo = CONFIG_NET_ETH_TCP_RECVWNDO;
#endif
            devfmt          = NETDEV_ETH_FORMAT;
            break;
#endif

#ifdef CONFIG_NET_SLIP
          case NET_LL_SLIP:      /* Serial Line Internet Protocol (SLIP) */
            dev->d_llhdrlen = 0;
            dev->d_mtu      = CONFIG_NET_SLIP_MTU;
#ifdef CONFIG_NET_TCP_STACK
            dev->d_recvwndo = CONFIG_NET_SLIP_TCP_RECVWNDO;
#endif
            devfmt          = NETDEV_SLIP_FORMAT;
            break;
#endif

#ifdef CONFIG_NET_TUN
          case NET_LL_TUN:       /* Virtual Network Device (TUN) */
            dev->d_llhdrlen = 0;
            dev->d_mtu      = CONFIG_NET_TUN_MTU;
#ifdef CONFIG_NET_TCP
            dev->d_recvwndo = CONFIG_NET_TUN_TCP_RECVWNDO;
#endif
            devfmt          = NETDEV_TUN_FORMAT;
            break;
#endif

#if 0                            /* REVISIT: Not yet supported */
          case NET_LL_PPP:       /* Point-to-Point Protocol (PPP) */
            dev->d_llhdrlen = 0;
            dev->d_mtu      = CONFIG_NET_PPP_MTU;
#ifdef CONFIG_NET_TCP_STACK
            dev->d_recvwndo = CONFIG_NET_PPP_TCP_RECVWNDO;
#endif
            devfmt          = NETDEV_PPP_FORMAT;
            break;
#endif

          default:
            nlldbg("ERROR: Unrecognized link type: %d\n", lltype);
            return -EINVAL;
        }

      /* Remember the verified link type */

      dev->d_lltype = (uint8_t)lltype;

#else
     /* Use the default device name */

     devfmt = NETDEV_DEFAULT_FORMAT;
#endif

      /* Get the next available device number and sssign a device name to
       * the interface
       */

      netdev_semtake();
#ifdef CONFIG_NET_MULTILINK
      devnum = find_devnum(devfmt);
#else
      devnum = g_next_devnum++;
#endif
#ifdef CONFIG_NET_USER_DEVFMT
      if (*dev->d_ifname)
        {
          strncpy(devfmt_str, dev->d_ifname, IFNAMSIZ);
          devfmt = devfmt_str;
        }
#endif

      snprintf(dev->d_ifname, IFNAMSIZ, devfmt, devnum );

      /* Add the device to the list of known network devices */

      dev->flink  = g_netdevices;
      g_netdevices = dev;

      /* Configure the device for IGMP support */

#ifdef CONFIG_NET_IGMP
      igmp_devinit(dev);
#endif
      netdev_semgive();

#ifdef CONFIG_NET_ETHERNET
      nlldbg("Registered MAC: %02x:%02x:%02x:%02x:%02x:%02x as dev: %s\n",
             dev->d_mac.ether_addr_octet[0], dev->d_mac.ether_addr_octet[1],
             dev->d_mac.ether_addr_octet[2], dev->d_mac.ether_addr_octet[3],
             dev->d_mac.ether_addr_octet[4], dev->d_mac.ether_addr_octet[5],
             dev->d_ifname);
#else
      nlldbg("Registered dev: %s\n", dev->d_ifname);
#endif
      return OK;
    }

  return -EINVAL;
}