📄 main.c
字号:
/*
* Copyright (c) 2005, Dennis Kuschel.
* 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. 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.
*
*/
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/memp.h"
#include "lwip/tcpip.h"
#include "lwip/ip_addr.h"
#include "lwip/stats.h"
#include "lwip/sockets.h"
#include "arch/perf.h"
#include "../../lwipport/netif/pktifw32/pktif.h"
#include "../../../e-soft/src/filesys.h"
#include "../../../e-soft/src/httpd.h"
#include "../../../e-soft/src/ftpd.h"
#include <stdlib.h>
#include <sys/stat.h>
#include <picoos.h>
/*-------------------------------------------------------------------------*/
/* imported functions */
extern void chargen_init(void);
extern err_t ethernetif_init(struct netif *netif);
extern int init_adapter(int adapter_num);
extern void shutdown_adapter(void);
extern void update_adapter(void);
extern void printIPaddr(unsigned int *ipaddr);
extern int network_setup(unsigned int *ipaddr,
unsigned int *netmask,
unsigned int *gatew);
extern sint_t statistics_servlet(SERVLET_t so);
extern void statsServlet_init(
struct netif *ethnetif, struct ip_addr ipaddr,
struct ip_addr netmask, struct ip_addr gatew );
extern sint_t index_servlet(SERVLET_t so);
/* demo servlets from e-soft/demo directory */
extern sint_t my_switch_servlet(SERVLET_t so);
extern sint_t my_counter_servlet(SERVLET_t so);
extern sint_t my_chargen_servlet1(SERVLET_t so);
extern sint_t my_chargen_servlet2(SERVLET_t so);
extern sint_t my_login_servlet(SERVLET_t so);
/* local functions */
int main_setup(void);
int main_program(void);
sint_t index_servlet2(SERVLET_t so);
sint_t index_servlet3(SERVLET_t so);
static void* load_file(char *filename);
static void httpd_thread(void *arg);
static void ftpd_thread(void *arg);
static void esoft_init(void);
static void tcpip_init_done(void * arg);
static void lwip_init(void);
/* global variables */
struct netif ethnetif;
struct ip_addr ipaddr;
struct ip_addr netmask;
struct ip_addr gatew;
#define RAMIMAGEFILE "driveimg.bin"
/*-------------------------------------------------------------------------*/
/* initialize empedded software tools
* (filesystem / ftpd / httpd)
*/
static void esoft_init(void)
{
void *img;
JIF_t jif;
int ok;
if (fsys_init(2*1024*1024+0x10000, 512, 128) != 0)
{
if (fsys_init(32768, 256, 64) != 0)
{
if (fsys_init(1024, 128, 32) != 0)
{
printf("ERROR: failed to start embedded filesystem\n");
return;
}
}
}
img = load_file(RAMIMAGEFILE);
if (img == NULL)
img = load_file("..\\" RAMIMAGEFILE);
if (img == NULL)
img = load_file("..\\..\\" RAMIMAGEFILE);
if (img == NULL)
img = load_file("..\\..\\..\\" RAMIMAGEFILE);
if (img != NULL)
{
if (fsys_addDriveImage(img, 1) != 0) {
free(img);
} else {
printf("Drive image loaded.\n");
}
}
else
{
printf("FATAL ERROR: failed to load file " RAMIMAGEFILE "\n");
}
/* start the HTTP server */
ok = 0;
if (sys_thread_new(httpd_thread, NULL, DEFAULT_THREAD_PRIO) != NULL)
{
jif = jiffies;
while (!httpd_running() && !POS_TIMEAFTER(jiffies, jif+HZ))
posTaskSleep(1);
if (httpd_running())
ok = 1;
}
if (ok)
{
/* install the servlets (note: we overload the index.html file!) */
hsl_create(index_servlet, "index.html");
hsl_create(index_servlet2, "index.htm");
hsl_create(index_servlet3, "/");
statsServlet_init(ðnetif, ipaddr, netmask, gatew);
hsl_create(statistics_servlet, "stats");
/* install e-soft demo servlets */
hsl_create(my_counter_servlet, "counter");
hsl_create(my_chargen_servlet1, "chargen1");
hsl_create(my_chargen_servlet2, "chargen2");
hsl_create(my_login_servlet, "logindemo");
}
else
{
printf("ERROR: failed to start embedded HTTP demon\n");
}
/* start the FTP server */
ok = 0;
if (sys_thread_new(ftpd_thread, NULL, DEFAULT_THREAD_PRIO) != NULL)
{
jif = jiffies;
while (!ftpd_running() && !POS_TIMEAFTER(jiffies, jif+HZ))
posTaskSleep(1);
if (ftpd_running())
ok = 1;
}
if (!ok)
{
printf("ERROR: failed to start embedded FTP demon\n");
}
}
static void httpd_thread(void *arg)
{
(void) arg;
POS_SETTASKNAME(posTaskGetCurrent(), "HTTP demon");
printf("HTTP demon started.\n");
if (httpd_start("/", 0x8000, 0x0800) != 0)
{
printf("HTTP demon: unexpected ERROR!!\n");
}
}
static void ftpd_thread(void *arg)
{
char *user = "test";
char *pass = "test";
(void) arg;
POS_SETTASKNAME(posTaskGetCurrent(), "FTP demon");
printf("FTP demon started. Login with user: %s, pass: %s\n", user, pass);
if (ftpd_start(user, pass) != 0)
{
printf("FTP demon: unexpected ERROR!!\n");
}
}
static void* load_file(char *filename)
{
struct stat st;
FILE *f;
void *m;
if (stat(filename, &st) != 0)
return NULL;
#undef malloc
m = malloc(st.st_size);
if (m == NULL)
return NULL;
f = fopen(filename, "rb");
if (f == NULL)
{
free(m);
return NULL;
}
if (fread(m, 1, st.st_size, f) <= 0)
{
fclose(f);
free(m);
return NULL;
}
fclose(f);
return m;
}
sint_t index_servlet2(SERVLET_t so)
{
return index_servlet(so);
}
sint_t index_servlet3(SERVLET_t so)
{
return index_servlet(so);
}
/*-------------------------------------------------------------------------*/
/* lwIP initialization
* (with ethernet interface)
*/
static void lwip_init(void)
{
sys_sem_t sem;
printf("Initializing lwIP... ");
#ifdef PERF
perf_init("lwip.perf");
#endif /* PERF */
#ifdef STATS
stats_init();
#endif /* STATS */
sys_init();
mem_init();
memp_init();
pbuf_init();
netif_init();
sem = sys_sem_new(0);
tcpip_init(tcpip_init_done, &sem);
sys_sem_wait(sem);
sys_sem_free(sem);
netif_add(ðnetif, &ipaddr, &netmask, &gatew, NULL,
&pktif_init, &tcpip_input);
netif_set_default(ðnetif);
#ifndef ETHARP_OLDSTYLE
netif_set_up(ðnetif);
#endif
printf("done.\n");
}
/* initialization callback routine
*/
static void tcpip_init_done(void *arg)
{
sys_sem_t *sem = arg;
/* Ready with initialization. Send a signal. */
sys_sem_signal(*sem);
}
/*-------------------------------------------------------------------------*/
/* main program
* (first task running in pico]OS context)
*/
int main_program(void)
{
static const char bar[4] = {'-','\\','|','/'};
int i;
printf("\n" POS_STARTUPSTRING "\n\n");
/* initialize lwIP TCP/IP stack */
lwip_init();
/* initialize chargen server application */
chargen_init();
/* initialize embedded tools */
esoft_init();
printf("all initialized.\n\n\n");
printf("!!! Please open a web browser and enter the URL http://");
printIPaddr((unsigned int*)&ipaddr);
printf("\n\n");
/* wait forever, return never */
i = 0;
for(;;)
{
printf("%c\r", bar[i]);
i = (i + 1) % 4;
posTaskSleep(MS(1000));
}
return 0;
}
/* main setup function
* (is called before pico]OS is started)
*/
int main_setup(void)
{
/* set up the Windows network (and get the IP address) */
if (network_setup((unsigned int*)&ipaddr,
(unsigned int*)&netmask,
(unsigned int*)&gatew) != 0)
{
printf("\nSomething failed on this machine.\n");
printf("Please make sure that you have the WinPcap driver installed.\n");
return 1;
}
return 0;
}
/*-------------------------------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -