示例#1
0
static int app_dhcp_main(void)
{
	uint32_t timeleft = 15000;
	struct dhcpc_state state;
	void *dhcp_handle;
	int ret;
	dhcp_handle = dhcpc_open(NET_DEVNAME);
	ret = dhcpc_request(dhcp_handle, &state);
	while (ret != OK) {
		usleep(10);
		//sys_msleep(10);
		timeleft -= 10;

		if (timeleft <= 0)
			break;
	}

	if (timeleft <= 0) {
		dhcpc_close(dhcp_handle);
		return -1;
	}

	netlib_set_ipv4addr(NET_DEVNAME, &state.ipaddr);
	netlib_set_ipv4netmask(NET_DEVNAME, &state.netmask);
	netlib_set_dripv4addr(NET_DEVNAME, &state.default_router);

	printf("IP address : %s ----\n", inet_ntoa(state.ipaddr));
	return 1;
}
示例#2
0
/****************************************************************************
 * Name: dhcp_client_start
 ****************************************************************************/
int dhcp_client_start(const char *intf)
{
	struct dhcpc_state state;
	int ret;
	void *dhcp_hnd = NULL;
	ndbg("[DHCPC] External DHCPC application started\n");
	dhcp_hnd = dhcpc_open(intf);
	if (dhcp_hnd) {
		ret = dhcpc_request(dhcp_hnd, &state);
		if (ret != OK) {
			ndbg("[DHCPC] get IP address fail\n");
			dhcpc_close(dhcp_hnd);
			return -1;
		}
		DHCPC_SET_IP4ADDR(intf, state.ipaddr, state.netmask, state.default_router);
		ndbg("[DHCPC] IP address : %s ----\n", inet_ntoa(state.ipaddr));
		dhcpc_close(dhcp_hnd);
	} else {
		ndbg("[DHCPC] Invalid dhcp handle\n");
		return -1;
	}

	return OK;
}
示例#3
0
static int tcpecho_netsetup()
{
  /* If this task is excecutated as an NSH built-in function, then the
   * network has already been configured by NSH's start-up logic.
   */

#ifndef CONFIG_NSH_BUILTIN_APPS
  struct in_addr addr;
#if defined(CONFIG_EXAMPLES_TCPECHO_DHCPC) || defined(CONFIG_EXAMPLES_TCPECHO_NOMAC)
  uint8_t mac[IFHWADDRLEN];
#endif
#ifdef CONFIG_EXAMPLES_TCPECHO_DHCPC
  struct dhcpc_state ds;
  void *handle;
#endif

/* Many embedded network interfaces must have a software assigned MAC */

#ifdef CONFIG_EXAMPLES_TCPECHO_NOMAC
  mac[0] = 0x00;
  mac[1] = 0xe0;
  mac[2] = 0xde;
  mac[3] = 0xad;
  mac[4] = 0xbe;
  mac[5] = 0xef;
  netlib_setmacaddr("eth0", mac);
#endif

  /* Set up our host address */

#ifdef CONFIG_EXAMPLES_TCPECHO_DHCPC
  addr.s_addr = 0;
#else
  addr.s_addr = HTONL(CONFIG_EXAMPLES_TCPECHO_IPADDR);
#endif
  netlib_set_ipv4addr("eth0", &addr);

  /* Set up the default router address */

  addr.s_addr = HTONL(CONFIG_EXAMPLES_TCPECHO_DRIPADDR);
  netlib_set_dripv4addr("eth0", &addr);

  /* Setup the subnet mask */

  addr.s_addr = HTONL(CONFIG_EXAMPLES_TCPECHO_NETMASK);
  netlib_set_ipv4netmask("eth0", &addr);

#ifdef CONFIG_EXAMPLES_TCPECHO_DHCPC
  /* Get the MAC address of the NIC */

  netlib_getmacaddr("eth0", mac);

  /* Set up the DHCPC modules */

  handle = dhcpc_open(&mac, IFHWADDRLEN);

  /* Get an IP address.  Note:  there is no logic here for renewing the address in this
   * example.  The address should be renewed in ds.lease_time/2 seconds.
   */

  if (!handle)
    {
      return ERROR;
    }

  if (dhcpc_request(handle, &ds) != OK)
    {
      return ERROR;
    }

  netlib_set_ipv4addr("eth0", &ds.ipaddr);

  if (ds.netmask.s_addr != 0)
    {
      netlib_set_ipv4netmask("eth0", &ds.netmask);
    }

  if (ds.default_router.s_addr != 0)
    {
      netlib_set_dripv4addr("eth0", &ds.default_router);
    }

  if (ds.dnsaddr.s_addr != 0)
    {
      netlib_set_ipv4dnsaddr(&ds.dnsaddr);
    }

  dhcpc_close(handle);
  printf("IP: %s\n", inet_ntoa(ds.ipaddr));

#endif /* CONFIG_EXAMPLES_TCPECHO_DHCPC */
#endif /* CONFIG_NSH_BUILTIN_APPS */

  return OK;
}
示例#4
0
int user_start(int argc, char *argv[])
{
    struct in_addr addr;
#if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_EXAMPLE_UIP_NOMAC)
    uint8_t mac[IFHWADDRLEN];
#endif
#ifdef CONFIG_EXAMPLE_UIP_DHCPC
    void *handle;
#endif

    /* Many embedded network interfaces must have a software assigned MAC */

#ifdef CONFIG_EXAMPLE_UIP_NOMAC
    mac[0] = 0x00;
    mac[1] = 0xe0;
    mac[2] = 0xb0;
    mac[3] = 0x0b;
    mac[4] = 0xba;
    mac[5] = 0xbe;
    uip_setmacaddr("eth0", mac);
#endif

    /* Set up our host address */

#ifdef CONFIG_EXAMPLE_UIP_DHCPC
    addr.s_addr = 0;
#else
    addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_IPADDR);
#endif
    uip_sethostaddr("eth0", &addr);

    /* Set up the default router address */

    addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_DRIPADDR);
    uip_setdraddr("eth0", &addr);

    /* Setup the subnet mask */

    addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_NETMASK);
    uip_setnetmask("eth0", &addr);

#ifdef CONFIG_EXAMPLE_UIP_DHCPC
    /* Set up the resolver */

    resolv_init();

    /* Get the MAC address of the NIC */

    uip_getmacaddr("eth0", mac);

    /* Set up the DHCPC modules */

    handle = dhcpc_open(&mac, IFHWADDRLEN);

    /* Get an IP address.  Note:  there is no logic here for renewing the address in this
     * example.  The address should be renewed in ds.lease_time/2 seconds.
     */

    printf("Getting IP address\n");
    if (handle)
    {
        struct dhcpc_state ds;
        (void)dhcpc_request(handle, &ds);
        uip_sethostaddr("eth1", &ds.ipaddr);
        if (ds.netmask.s_addr != 0)
        {
            uip_setnetmask("eth0", &ds.netmask);
        }
        if (ds.default_router.s_addr != 0)
        {
            uip_setdraddr("eth0", &ds.default_router);
        }
        if (ds.dnsaddr.s_addr != 0)
        {
            resolv_conf(&ds.dnsaddr);
        }
        dhcpc_close(handle);
        printf("IP: %s\n", inet_ntoa(ds.ipaddr));
    }
#endif

#ifdef CONFIG_NET_TCP
    printf("Starting webserver\n");
    httpd_init();
    httpd_listen();
#endif

    while(1)
    {
        sleep(3);
        printf("main: Still running\n");
#if CONFIG_NFILE_DESCRIPTORS > 0
        fflush(stdout);
#endif
    }
    return 0;
}
示例#5
0
int discover_main(int argc, char *argv[])
{
  /* If this task is excecutated as an NSH built-in function, then the
   * network has already been configured by NSH's start-up logic.
   */

#ifndef CONFIG_NSH_BUILTIN_APPS
  struct in_addr addr;
#if defined(CONFIG_EXAMPLES_DISCOVER_DHCPC) || defined(CONFIG_EXAMPLES_DISCOVER_NOMAC)
  uint8_t mac[IFHWADDRLEN];
#endif
#ifdef CONFIG_EXAMPLES_DISCOVER_DHCPC
  void *handle;
#endif

/* Many embedded network interfaces must have a software assigned MAC */

#ifdef CONFIG_EXAMPLES_DISCOVER_NOMAC
  mac[0] = 0x00;
  mac[1] = 0xe0;
  mac[2] = 0xde;
  mac[3] = 0xad;
  mac[4] = 0xbe;
  mac[5] = 0xef;
  uip_setmacaddr("eth0", mac);
#endif

  /* Set up our host address */

#ifdef CONFIG_EXAMPLES_DISCOVER_DHCPC
  addr.s_addr = 0;
#else
  addr.s_addr = HTONL(CONFIG_EXAMPLES_DISCOVER_IPADDR);
#endif
  uip_sethostaddr("eth0", &addr);

  /* Set up the default router address */

  addr.s_addr = HTONL(CONFIG_EXAMPLES_DISCOVER_DRIPADDR);
  uip_setdraddr("eth0", &addr);

  /* Setup the subnet mask */

  addr.s_addr = HTONL(CONFIG_EXAMPLES_DISCOVER_NETMASK);
  uip_setnetmask("eth0", &addr);

#ifdef CONFIG_EXAMPLES_DISCOVER_DHCPC
  /* Set up the resolver */

  dns_bind();

  /* Get the MAC address of the NIC */

  uip_getmacaddr("eth0", mac);

  /* Set up the DHCPC modules */

  handle = dhcpc_open(&mac, IFHWADDRLEN);

  /* Get an IP address.  Note:  there is no logic here for renewing the address in this
   * example.  The address should be renewed in ds.lease_time/2 seconds.
   */

  printf("Getting IP address\n");
  if (handle)
    {
      struct dhcpc_state ds;
      (void)dhcpc_request(handle, &ds);
      uip_sethostaddr("eth1", &ds.ipaddr);

      if (ds.netmask.s_addr != 0)
        {
          uip_setnetmask("eth0", &ds.netmask);
        }

      if (ds.default_router.s_addr != 0)
        {
          uip_setdraddr("eth0", &ds.default_router);
        }

      if (ds.dnsaddr.s_addr != 0)
        {
          dns_setserver(&ds.dnsaddr);
        }

      dhcpc_close(handle);
      printf("IP: %s\n", inet_ntoa(ds.ipaddr));
    }

#endif /* CONFIG_EXAMPLES_DISCOVER_DHCPC */
#endif /* CONFIG_NSH_BUILTIN_APPS */

  if (discover_start(NULL) < 0)
    {
      ndbg("Could not start discover daemon.\n");
      return ERROR;
    }

  return OK;
}
示例#6
0
static inline void wlan_bringup(void)
{
#if defined(CONFIG_EXAMPLES_WLAN_DHCPC) || defined(CONFIG_EXAMPLES_WLAN_NOMAC)
  uint8_t mac[IFHWADDRLEN];
#endif
  struct in_addr addr;
#ifdef CONFIG_EXAMPLES_WLAN_DHCPC
  void *handle;
#endif

  /* Many embedded network interfaces must have a software assigned
   * MAC
   */

#ifdef CONFIG_EXAMPLES_WLAN_NOMAC
  mac[0] = 0x00;
  mac[1] = 0xe0;
  mac[2] = 0xde;
  mac[3] = 0xad;
  mac[4] = 0xbe;
  mac[5] = 0xef;
  uip_setmacaddr("eth0", mac);
#endif

  /* Set up the default router address */

  addr.s_addr = HTONL(CONFIG_EXAMPLES_WLAN_DRIPADDR);
  uip_setdraddr("eth0", &addr);

  /* Setup the subnet mask */

  addr.s_addr = HTONL(CONFIG_EXAMPLES_WLAN_NETMASK);
  uip_setnetmask("eth0", &addr);

  /* Set up our host address */

#ifdef CONFIG_EXAMPLES_WLAN_DHCPC
  addr.s_addr = 0;
#else
  addr.s_addr = HTONL(CONFIG_EXAMPLES_WLAN_IPADDR);
#endif
  uip_sethostaddr("eth0", &addr);

#ifdef CONFIG_EXAMPLES_WLAN_DHCPC
  /* Set up the resolver */

  resolv_init();

  /* Get the MAC address of the NIC */

  uip_getmacaddr("eth0", mac);

  /* Set up the DHCPC modules */

  handle = dhcpc_open(&mac, IFHWADDRLEN);

  /* Get an IP address.  Note:  there is no logic here for renewing
   * the address in this example.  The address should be renewed in
   * ds.lease_time/2 seconds.
   */

  printf("Getting IP address\n");
  if (handle)
    {
      struct dhcpc_state ds;
      (void)dhcpc_request(handle, &ds);
      uip_sethostaddr("eth1", &ds.ipaddr);
      if (ds.netmask.s_addr != 0)
        {
          uip_setnetmask("eth0", &ds.netmask);
        }
      if (ds.default_router.s_addr != 0)
        {
          uip_setdraddr("eth0", &ds.default_router);
        }
      if (ds.dnsaddr.s_addr != 0)
        {
          resolv_conf(&ds.dnsaddr);
        }
      dhcpc_close(handle);
      printf("IP: %s\n", inet_ntoa(ds.ipaddr));
    }
#endif
}