2007-09-09 00:41:00 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* examples/uip/main.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2007 Gregory Nutt. All rights reserved.
|
|
|
|
* 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>
|
2007-10-26 23:21:34 +02:00
|
|
|
#include <sys/ioctl.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>
|
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) */
|
|
|
|
|
|
|
|
#if defined(CONFIG_EXAMPLE_UIP_DHCPC)
|
|
|
|
# include <net/uip/resolv.h>
|
|
|
|
# include <net/uip/dhcpc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Pick the netutils feature under test (which may be DHCPC) */
|
|
|
|
|
2007-08-27 01:12:17 +02:00
|
|
|
#if defined(CONFIG_EXAMPLE_UIP_SMTP)
|
|
|
|
# include <net/uip/smtp.h>
|
|
|
|
#elif defined(CONFIG_EXAMPLE_UIP_TELNETD)
|
|
|
|
# include <net/uip/telnetd.h>
|
|
|
|
#elif defined(CONFIG_EXAMPLE_UIP_WEBSERVER)
|
|
|
|
# include <net/uip/httpd.h>
|
|
|
|
#elif defined(CONFIG_EXAMPLE_UIP_WEBCLIENT)
|
2007-11-01 00:27:55 +01:00
|
|
|
# include <net/uip/resolv.h>
|
2007-08-27 01:12:17 +02:00
|
|
|
# include <net/uip/webclient.h>
|
2007-11-27 20:19:30 +01:00
|
|
|
#elif !defined(CONFIG_EXAMPLE_UIP_DHCPC)
|
2007-10-27 20:56:04 +02:00
|
|
|
# error "No network application specified"
|
2007-08-27 01:12:17 +02:00
|
|
|
#endif
|
|
|
|
|
2007-11-04 17:31:24 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-11-30 21:46:29 +01:00
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
# define message(...) lib_lowprintf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
# define message(...) printf(__VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
|
2007-10-27 20:56:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_EXAMPLE_UIP_SMTP)
|
|
|
|
static const char g_host_name[] = "localhost";
|
|
|
|
static const char g_recipient[] = "spudmonkey@racsa.co.cr";
|
|
|
|
static const char g_sender[] = "nuttx-testing@example.com";
|
|
|
|
static const char g_subject[] = "Testing SMTP from NuttX";
|
|
|
|
static const char g_msg_body[] = "Test message sent by NuttX\r\n";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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)
|
2007-10-26 23:21:34 +02:00
|
|
|
uint8 mac[IFHWADDRLEN];
|
2007-08-31 01:57:58 +02:00
|
|
|
#endif
|
2007-09-01 20:06:15 +02:00
|
|
|
#if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_EXAMPLE_UIP_SMTP)
|
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 */
|
|
|
|
|
2007-11-30 21:46:29 +01:00
|
|
|
#if !defined(CONFIG_EXAMPLE_UIP_DHCPC)
|
2007-11-04 23:59:30 +01:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_IPADDR);
|
2007-11-30 21:46:29 +01:00
|
|
|
#else
|
|
|
|
addr.s_addr = 0;
|
|
|
|
#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
|
|
|
|
2007-11-27 20:19:30 +01:00
|
|
|
#if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_EXAMPLE_UIP_WEBCLIENT)
|
|
|
|
/* Set up the resolver */
|
|
|
|
|
|
|
|
resolv_init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_EXAMPLE_UIP_DHCPC)
|
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
|
|
|
|
|
|
|
/* Get an IP address */
|
|
|
|
|
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);
|
|
|
|
}
|
2007-11-27 20:19:30 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CONFIG_EXAMPLE_UIP_WEBSERVER)
|
|
|
|
httpd_init();
|
|
|
|
httpd_listen();
|
|
|
|
#elif defined(CONFIG_EXAMPLE_UIP_TELNETD)
|
|
|
|
telnetd_init();
|
2007-08-27 01:12:17 +02:00
|
|
|
#elif defined(CONFIG_EXAMPLE_UIP_SMTP)
|
2007-10-26 23:21:34 +02:00
|
|
|
uip_ipaddr(addr.s_addr, 127, 0, 0, 1);
|
2007-09-01 20:06:15 +02:00
|
|
|
handle = smtp_open();
|
2007-08-31 01:57:58 +02:00
|
|
|
if (handle)
|
|
|
|
{
|
2007-10-27 20:56:04 +02:00
|
|
|
smtp_configure(handle, g_host_name, &addr.s_addr);
|
|
|
|
smtp_send(handle, g_recipient, NULL, g_sender, g_subject,
|
|
|
|
g_msg_body, strlen(g_msg_body));
|
2007-08-31 01:57:58 +02:00
|
|
|
smtp_close(handle);
|
|
|
|
}
|
2007-08-27 01:12:17 +02:00
|
|
|
#elif defined(CONFIG_EXAMPLE_UIP_WEBCLIENT)
|
|
|
|
webclient_init();
|
2007-11-27 20:19:30 +01:00
|
|
|
addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_DNSADDR);
|
2007-11-01 00:27:55 +01:00
|
|
|
resolv_conf(&addr);
|
2007-11-27 20:19:30 +01:00
|
|
|
resolv_query(CONFIG_EXAMPLE_UIP_SERVERURL);
|
2007-08-27 01:12:17 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-10-27 20:56:04 +02:00
|
|
|
#if defined(CONFIG_EXAMPLE_UIP_WEBCLIENT)
|
2007-08-27 01:12:17 +02:00
|
|
|
void webclient_closed(void)
|
|
|
|
{
|
|
|
|
printf("Webclient: connection closed\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void webclient_aborted(void)
|
|
|
|
{
|
|
|
|
printf("Webclient: connection aborted\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void webclient_timedout(void)
|
|
|
|
{
|
|
|
|
printf("Webclient: connection timed out\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void webclient_connected(void)
|
|
|
|
{
|
|
|
|
printf("Webclient: connected, waiting for data...\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void webclient_datahandler(char *data, uint16 len)
|
|
|
|
{
|
|
|
|
printf("Webclient: got %d bytes of data.\n", len);
|
|
|
|
}
|
2007-10-27 20:56:04 +02:00
|
|
|
#endif
|