📄 tftp_main.c
字号:
/*
* File: main.c
* Purpose: TFTP Network Stack user interface
*/
#include "src/init/m523xevb.h"
#include "src/init/stdlib.h"
#include "src/ethernet/nif.h"
#include "src/ethernet/fec.h"
#include "src/ethernet/tftp/arp.h"
#include "src/ethernet/tftp/ip.h"
#include "src/ethernet/tftp/udp.h"
#include "src/ethernet/tftp/tftp.h"
#include "src/ethernet/tftp/timer.h"
#include "src/ethernet/tftp/tftp_main.h"
#include "src/init/mcf523x/mcf523x_intc0.h"
/********************************************************************/
/* Define an interface to the FEC */
NIF fec_nif;
IP_INFO ip_info;
ARP_INFO arp_info;
static char input[MAX_LINE];
static const char PROMPT[] = "TFTP> ";
static const char HELPMSG[] = "\nEnter 'help' for help.\n\n";
static const char SYNTAX[] = "Error: Invalid syntax for: %s\n";
static const char INVCMD[] = "Error: No such command: %s\n";
static const char INVALUE[] = "Error: Invalid value: %s\n";
static const char HELPFORMAT[] = "%8s %-30s %s %s\n";
static const char INVOPT[] = "Error: Invalid set/show option: %s\n";
static const char OPTFMT[] = "%12s: ";
static const char MACFMT[] = "%02X:%02X:%02X:%02X:%02X:%02X\n";
static const char SETERR[] = "Error: Invalid MAC address: %s\n";
static const char IPFMT[] = "%d.%d.%d.%d\n";
static const char SETERR1[] = "Error: Invalid IP address: %s\n";
static const USERSPACE = 0x40000;
/* Begin Edit these settings for your system */
static IP_ADDR client = {10,81,65,31};
static IP_ADDR gateway = {10,81,67,254};
static IP_ADDR netmask = {255,255,252,0};
static IP_ADDR server = {10,81,65,29};
/* End of Edit these settings for your system */
static unsigned char mac[6] = {0x00, 0xCF, 0x52, 0x35, 0x00, 0x01};
static char filename[MAX_FN] = "test.txt";
uint32 md_last_address = USERSPACE;
CMD CMDTAB[] =
{
{"help", 0, 0, help, "Display this help message", ""},
{"read", 0, 1, read, "Read from TFTP host", "<filename>"},
{"md", 0, 1, md, "Memory display", "<address>"},
{"set", 0, 2, set, "Set parameter", "option value"},
{"show", 0, 0, show, "Show parameters", ""},
{"write", 1, 2, write, "Write to TFTP host", "bytes <filename>"},
{"quit", 0, 0, quit, "Exit TFTP", " "},
{"idle", 0, 0, idle, "Enable FEC and idle", " "}
};
const int NUM_CMD = sizeof(CMDTAB)/sizeof(CMD);
SETCMD SETCMDTAB[] =
{
{"server", set_server, "<server IP address>"},
{"client", set_client, "<board IP address>"},
{"gateway", set_gateway, "<gateway IP address>"},
{"netmask", set_netmask, "<netmask IP address>"},
{"mac", set_mac, "<ethernet address>"},
{"filename", set_filename, "<file to transfer>"}
};
const int NUM_SETCMD = sizeof(SETCMDTAB)/sizeof(SETCMD);
/********************************************************************/
int
main (void)
{
printf("\n\n");
printf("Running TFTP Network Stack\n");
printf("Built on %s %s\n",__DATE__, __TIME__);
#if (DEBUG)
printf("DEBUG prints ON\n");
#endif
printf(HELPMSG);
while (1)
{
printf(PROMPT);
run_cmd();
}
return(0);
}
/********************************************************************/
static void
get_user_input (char *userline)
{
char line[MAX_LINE];
int pos, ch;
pos = 0;
ch = (int)in_char();
while ( (ch != 0x0D /* CR */) &&
(ch != 0x0A /* LF/NL */) &&
(pos < MAX_LINE))
{
switch (ch)
{
case 0x08: /* Backspace */
case 0x7F: /* Delete */
if (pos > 0)
{
pos -= 1;
out_char(0x08); /* backspace */
out_char(' ');
out_char(0x08); /* backspace */
}
break;
default:
if ((pos+1) < MAX_LINE)
{
/* only printable characters */
if ((ch > 0x1f) && (ch < 0x80))
{
line[pos++] = (char)ch;
out_char((char)ch);
}
}
break;
}
ch = (int)in_char();
}
out_char(0x0D); /* CR */
out_char(0x0A); /* LF */
if (!pos && (strncasecmp(userline,"md",2) == 0))
{
/* Allow 'md' command to be repeated */
}
else
{
line[pos] = '\0';
strcpy(userline,line);
}
}
/********************************************************************/
static int
make_argv (char *cmdline, char *argv[])
{
int argc, i, in_text;
/* break cmdline into strings and argv */
/* it is permissible for argv to be NULL, in which case */
/* the purpose of this routine becomes to count args */
argc = 0;
i = 0;
in_text = FALSE;
while (cmdline[i] != '\0') /* getline() must place 0x00 on end */
{
if (((cmdline[i] == ' ') ||
(cmdline[i] == '\t')) )
{
if (in_text)
{
/* end of command line argument */
cmdline[i] = '\0';
in_text = FALSE;
}
else
{
/* still looking for next argument */
}
}
else
{
/* got non-whitespace character */
if (in_text)
{
}
else
{
/* start of an argument */
in_text = TRUE;
if (argc < MAX_ARGS)
{
if (argv != NULL)
argv[argc] = &cmdline[i];
argc++;
}
else
/*return argc;*/
break;
}
}
i++; /* proceed to next character */
}
if (argv != NULL)
argv[argc] = NULL;
return argc;
}
/********************************************************************/
static uint32
get_value (char *s, int *success, int base)
{
uint32 value;
char *p;
value = strtoul(s,&p,base);
if ((value == 0) && (p == s))
{
*success = FALSE;
return 0;
}
else
{
*success = TRUE;
return value;
}
}
/********************************************************************/
void
run_cmd (void)
{
int argc;
char *argv[MAX_ARGS + 1]; /* One extra for NULL terminator */
get_user_input(input);
argc = make_argv(input,argv);
if (argc)
{
int i;
for (i = 0; i < NUM_CMD; i++)
{
if (strcasecmp(CMDTAB[i].cmd,argv[0]) == 0)
{
if (((argc-1) >= CMDTAB[i].min_args) &&
((argc-1) <= CMDTAB[i].max_args))
{
CMDTAB[i].func(argc,argv);
return;
}
else
{
printf(SYNTAX,argv[0]);
return;
}
}
}
printf(INVCMD,argv[0]);
printf(HELPMSG);
}
}
/********************************************************************/
void
help (int argc, char **argv)
{
int index;
(void)argc;
(void)argv;
printf("\n");
for (index = 0; index < NUM_CMD; index++)
{
printf(HELPFORMAT,
CMDTAB[index].cmd,
CMDTAB[index].description,
CMDTAB[index].cmd,
CMDTAB[index].syntax);
}
printf("\n");
}
/********************************************************************/
void
read (int argc, char **argv)
{
uint32 addr = USERSPACE;
char *fn;
int ch;
if (argc == 2)
fn = (char *)&argv[1][0];
else
fn = filename;
/* Initialize the timer for network use */
timer_init(TIMER_NETWORK, TIMER_NETWORK_PERIOD, \
SYSTEM_CLOCK, TIMER_NETWORK_LEVEL);
/* Enable FEC Rx Frame interrupts to ColdFire core */
MCF_INTC0_ICR27 = MCF_INTC0_ICRn_IL(FEC_LEVEL);
MCF_INTC0_IMRL &= ~( MCF_INTC0_IMRL_INT_MASK27
|MCF_INTC0_IMRL_MASKALL );
/* Initialize network device */
fec_init(&fec_nif);
/* Write ethernet address in the NIF structure */
fec_nif.hwa[0] = mac[0];
fec_nif.hwa[1] = mac[1];
fec_nif.hwa[2] = mac[2];
fec_nif.hwa[3] = mac[3];
fec_nif.hwa[4] = mac[4];
fec_nif.hwa[5] = mac[5];
/* Initialize Network Buffers */
nbuf_init();
/* Initialize ARP */
arp_init(&arp_info);
nif_bind_protocol(&fec_nif,FRAME_ARP,(void *)arp_handler,(void *)&arp_info);
/* Initialize IP */
ip_init(&ip_info,client,gateway,netmask);
nif_bind_protocol(&fec_nif,FRAME_IP,(void *)ip_handler,(void *)&ip_info);
/* Initialize UDP */
udp_init();
printf("Reading %s from %d.%d.%d.%d to 0x%08X\n",
fn, server[0], server[1], server[2], server[3], addr);
/* Open the TFTP connection */
if (tftp_read(&fec_nif, fn, server))
{
while (1)
{
ch = tftp_in_char();
if (ch == -1)
{
/* We're done */
break;
}
else
{
*(unsigned char *)addr++ = (unsigned char)ch;
}
}
/* Close the TFTP connection */
tftp_end(TRUE);
}
}
/********************************************************************/
void
write (int argc, char **argv)
{
uint32 bytes;
int success;
char *fn;
bytes = get_value(argv[1],&success,10);
if (!success)
{
printf(INVALUE,argv[1]);
return;
}
if (argc == 3)
fn = (char *)&argv[2][0];
else
fn = filename;
/* Initialize the timer for network use */
timer_init(TIMER_NETWORK, TIMER_NETWORK_PERIOD, \
SYSTEM_CLOCK, TIMER_NETWORK_LEVEL);
/* Enable FEC Rx Frame interrupts to ColdFire core */
MCF_INTC0_ICR27 = MCF_INTC0_ICRn_IL(FEC_LEVEL);
MCF_INTC0_IMRL &= ~( MCF_INTC0_IMRL_INT_MASK27
|MCF_INTC0_IMRL_MASKALL );
/* Initialize network device */
fec_init(&fec_nif);
/* Write ethernet address in the NIF structure */
fec_nif.hwa[0] = mac[0];
fec_nif.hwa[1] = mac[1];
fec_nif.hwa[2] = mac[2];
fec_nif.hwa[3] = mac[3];
fec_nif.hwa[4] = mac[4];
fec_nif.hwa[5] = mac[5];
/* Initialize Network Buffers */
nbuf_init();
/* Initialize ARP */
arp_init(&arp_info);
nif_bind_protocol(&fec_nif,FRAME_ARP,(void *)arp_handler,(void *)&arp_info);
/* Initialize IP */
ip_init(&ip_info,client,gateway,netmask);
nif_bind_protocol(&fec_nif,FRAME_IP,(void *)ip_handler,(void *)&ip_info);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -