2007-09-09 00:41:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* examples/uip/main.c
|
|
|
|
*
|
2010-08-11 03:28:40 +02:00
|
|
|
* Copyright (C) 2007, 2009-2010 Gregory Nutt. All rights reserved.
|
2007-09-09 00:41:00 +02:00
|
|
|
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
|
|
|
*
|
|
|
|
* Based on uIP which also has a BSD style license:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2001, Adam Dunkels.
|
|
|
|
* All rights reserved.
|
2007-08-27 01:12:17 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
2007-09-09 00:41:00 +02:00
|
|
|
*
|
2007-08-27 01:12:17 +02:00
|
|
|
* 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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Adam Dunkels.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
|
|
|
|
*
|
2007-09-09 00:41:00 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-01 00:27:55 +01:00
|
|
|
#include <nuttx/config.h>
|
2009-12-15 20:00:40 +01:00
|
|
|
|
2007-10-26 23:21:34 +02:00
|
|
|
#include <sys/ioctl.h>
|
2009-12-15 20:00:40 +01:00
|
|
|
#include <stdint.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
#include <stdio.h>
|
2007-10-27 20:56:04 +02:00
|
|
|
#include <string.h>
|
2007-09-01 20:06:15 +02:00
|
|
|
#include <unistd.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
#include <time.h>
|
2007-11-04 23:59:30 +01:00
|
|
|
#include <debug.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-30 21:46:29 +01:00
|
|
|
#include <net/if.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
#include <net/uip/uip.h>
|
|
|
|
#include <net/uip/uip-arp.h>
|
2007-10-26 23:21:34 +02:00
|
|
|
#include <net/uip/uip-lib.h>
|
2010-08-11 03:28:40 +02:00
|
|
|
#ifdef CONFIG_EXAMPLE_UIP_DHCPC
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
/* Here we include the header file for the application(s) we use in
|
|
|
|
* our project as defined in the config/<board-name>/defconfig file
|
|
|
|
*/
|
|
|
|
|
2007-11-27 20:19:30 +01:00
|
|
|
/* DHCPC may be used in conjunction with any other feature (or not) */
|
|
|
|
|
2009-03-28 22:28:43 +01:00
|
|
|
#ifdef CONFIG_EXAMPLE_UIP_DHCPC
|
2007-11-27 20:19:30 +01:00
|
|
|
# include <net/uip/resolv.h>
|
|
|
|
# include <net/uip/dhcpc.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-28 22:28:43 +01:00
|
|
|
/* Include uIP webserver definitions */
|
2007-11-27 20:19:30 +01:00
|
|
|
|
2009-03-28 22:28:43 +01:00
|
|
|
#include <net/uip/httpd.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2007-11-04 17:31:24 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-03-21 16:59:45 +01:00
|
|
|
#ifdef CONFIG_CPP_HAVE_VARARGS
|
|
|
|
# ifdef CONFIG_DEBUG
|
|
|
|
# define message(...) lib_lowprintf(__VA_ARGS__)
|
|
|
|
# else
|
|
|
|
# define message(...) printf(__VA_ARGS__)
|
|
|
|
# endif
|
2007-11-30 21:46:29 +01:00
|
|
|
#else
|
2009-03-21 16:59:45 +01:00
|
|
|
# ifdef CONFIG_DEBUG
|
|
|
|
# define message lib_lowprintf
|
|
|
|
# else
|
|
|
|
# define message (void)
|
|
|
|
# endif
|
2007-11-30 21:46:29 +01:00
|
|
|
#endif
|
|
|
|
|
2007-10-27 20:56:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2007-09-21 02:37:49 +02:00
|
|
|
* user_initialize
|
2007-10-27 20:56:04 +02:00
|
|
|
****************************************************************************/
|
2007-09-21 02:37:49 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_HAVE_WEAKFUNCTIONS
|
|
|
|
void user_initialize(void)
|
|
|
|
{
|
|
|
|
/* Stub that must be provided only if the toolchain does
|
|
|
|
* not support weak functions.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-10-27 20:56:04 +02:00
|
|
|
/****************************************************************************
|
2007-09-21 02:37:49 +02:00
|
|
|
* user_start
|
2007-10-27 20:56:04 +02:00
|
|
|
****************************************************************************/
|
2007-09-21 02:37:49 +02:00
|
|
|
|
2007-10-27 20:56:04 +02:00
|
|
|
int user_start(int argc, char *argv[])
|
2007-08-27 01:12:17 +02:00
|
|
|
{
|
2007-09-17 00:12:04 +02:00
|
|
|
struct in_addr addr;
|
2007-11-19 19:17:23 +01:00
|
|
|
#if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_EXAMPLE_UIP_NOMAC)
|
2009-12-15 20:00:40 +01:00
|
|
|
uint8_t mac[IFHWADDRLEN];
|
2007-08-31 01:57:58 +02:00
|
|
|
#endif
|
2009-03-28 22:28:43 +01:00
|
|
|
#ifdef CONFIG_EXAMPLE_UIP_DHCPC
|
2007-08-31 01:57:58 +02:00
|
|
|
void *handle;
|
2007-08-27 01:12:17 +02:00
|
|
|
#endif
|
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
/* Many embedded network interfaces must have a software assigned MAC */
|
2007-10-26 23:21:34 +02:00
|
|
|
|
2007-11-19 19:17:23 +01:00
|
|
|
#ifdef CONFIG_EXAMPLE_UIP_NOMAC
|
2007-10-26 23:21:34 +02:00
|
|
|
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 */
|
|
|
|
|
2009-03-28 22:28:43 +01:00
|
|
|
#ifdef CONFIG_EXAMPLE_UIP_DHCPC
|
2007-11-30 21:46:29 +01:00
|
|
|
addr.s_addr = 0;
|
2009-03-28 22:28:43 +01:00
|
|
|
#else
|
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_IPADDR);
|
2007-11-30 21:46:29 +01:00
|
|
|
#endif
|
2007-09-17 00:12:04 +02:00
|
|
|
uip_sethostaddr("eth0", &addr);
|
|
|
|
|
2007-10-26 23:21:34 +02:00
|
|
|
/* Set up the default router address */
|
|
|
|
|
2007-11-04 23:59:30 +01:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_DRIPADDR);
|
2007-10-26 23:21:34 +02:00
|
|
|
uip_setdraddr("eth0", &addr);
|
|
|
|
|
|
|
|
/* Setup the subnet mask */
|
|
|
|
|
2007-11-04 23:59:30 +01:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_NETMASK);
|
2007-10-26 23:21:34 +02:00
|
|
|
uip_setnetmask("eth0", &addr);
|
2007-08-27 01:12:17 +02:00
|
|
|
|
2009-03-28 22:28:43 +01:00
|
|
|
#ifdef CONFIG_EXAMPLE_UIP_DHCPC
|
2007-11-27 20:19:30 +01:00
|
|
|
/* Set up the resolver */
|
|
|
|
|
|
|
|
resolv_init();
|
|
|
|
|
2007-11-01 00:27:55 +01:00
|
|
|
/* Get the MAC address of the NIC */
|
|
|
|
|
|
|
|
uip_getmacaddr("eth0", mac);
|
|
|
|
|
2007-11-27 20:19:30 +01:00
|
|
|
/* Set up the DHCPC modules */
|
2007-11-01 00:27:55 +01:00
|
|
|
|
2007-10-26 23:21:34 +02:00
|
|
|
handle = dhcpc_open(&mac, IFHWADDRLEN);
|
2007-11-01 00:27:55 +01:00
|
|
|
|
2010-08-10 18:39:34 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2007-11-01 00:27:55 +01:00
|
|
|
|
2009-03-21 20:56:54 +01:00
|
|
|
printf("Getting IP address\n");
|
2007-09-01 20:06:15 +02:00
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
struct dhcpc_state ds;
|
|
|
|
(void)dhcpc_request(handle, &ds);
|
2007-12-01 00:15:06 +01:00
|
|
|
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);
|
|
|
|
}
|
2007-09-01 20:06:15 +02:00
|
|
|
dhcpc_close(handle);
|
2010-08-11 03:28:40 +02:00
|
|
|
printf("IP: %s\n", inet_ntoa(ds.ipaddr));
|
2007-09-01 20:06:15 +02:00
|
|
|
}
|
2007-11-27 20:19:30 +01:00
|
|
|
#endif
|
|
|
|
|
2009-12-17 21:28:00 +01:00
|
|
|
#ifdef CONFIG_NET_TCP
|
2009-03-21 20:56:54 +01:00
|
|
|
printf("Starting webserver\n");
|
2007-11-27 20:19:30 +01:00
|
|
|
httpd_init();
|
|
|
|
httpd_listen();
|
2009-12-17 21:28:00 +01:00
|
|
|
#endif
|
2007-08-27 01:12:17 +02:00
|
|
|
|
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
sleep(3);
|
|
|
|
printf("main: Still running\n");
|
2007-12-11 15:49:02 +01:00
|
|
|
#if CONFIG_NFILE_DESCRIPTORS > 0
|
2007-12-10 20:39:26 +01:00
|
|
|
fflush(stdout);
|
2007-12-11 15:49:02 +01:00
|
|
|
#endif
|
2007-08-27 01:12:17 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|