Line 8... |
Line 8... |
* Software 'as is' without warranty. Author liable for nothing.
|
* Software 'as is' without warranty. Author liable for nothing.
|
* DESCRIPTION:
|
* DESCRIPTION:
|
* Plasma FTP server and FTP client and TFTP server and client
|
* Plasma FTP server and FTP client and TFTP server and client
|
* and Telnet server.
|
* and Telnet server.
|
*--------------------------------------------------------------------*/
|
*--------------------------------------------------------------------*/
|
|
#undef INCLUDE_FILESYS
|
#define INCLUDE_FILESYS
|
#define INCLUDE_FILESYS
|
#ifdef WIN32
|
#ifdef WIN32
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <string.h>
|
#include <string.h>
|
Line 19... |
Line 20... |
#define _LIBC
|
#define _LIBC
|
#endif
|
#endif
|
#include "rtos.h"
|
#include "rtos.h"
|
#include "tcpip.h"
|
#include "tcpip.h"
|
|
|
|
#ifdef DLL_SETUP
|
|
static void ConsoleRun(IPSocket *socket, char *argv[]);
|
|
#endif
|
|
|
//******************* FTP Server ************************
|
//******************* FTP Server ************************
|
typedef struct {
|
typedef struct {
|
IPSocket *socket;
|
IPSocket *socket;
|
int ip, port, bytes, done, canReceive;
|
int ip, port, bytes, done, canReceive;
|
Line 408... |
Line 412... |
#define COMMAND_BUFFER_SIZE 80
|
#define COMMAND_BUFFER_SIZE 80
|
#define COMMAND_BUFFER_COUNT 10
|
#define COMMAND_BUFFER_COUNT 10
|
static char CommandHistory[400];
|
static char CommandHistory[400];
|
static char *CommandPtr[COMMAND_BUFFER_COUNT];
|
static char *CommandPtr[COMMAND_BUFFER_COUNT];
|
static int CommandIndex;
|
static int CommandIndex;
|
|
|
|
typedef void (*ConsoleFunc)(IPSocket *socket, char *argv[]);
|
|
typedef struct {
|
|
char *name;
|
|
ConsoleFunc func;
|
|
} TelnetFunc_t;
|
static TelnetFunc_t *TelnetFuncList;
|
static TelnetFunc_t *TelnetFuncList;
|
static int BusyBox(IPSocket *socket, char *command);
|
|
|
|
|
|
static void TelnetServer(IPSocket *socket)
|
static void TelnetServer(IPSocket *socket)
|
{
|
{
|
uint8 buf[COMMAND_BUFFER_SIZE+4];
|
uint8 buf[COMMAND_BUFFER_SIZE+4];
|
char bufOut[COMMAND_BUFFER_SIZE+32];
|
char bufOut[32];
|
int bytes, i, j, length, rc;
|
int bytes, i, j, length, found;
|
char *ptr, *command = socket->userPtr;
|
char *ptr, *command = socket->userPtr;
|
|
char *argv[10];
|
|
|
bytes = IPRead(socket, buf, sizeof(buf)-1);
|
bytes = IPRead(socket, buf, sizeof(buf)-1);
|
if(bytes == 0)
|
if(bytes == 0)
|
{
|
{
|
if(socket->userData)
|
if(socket->userPtr)
|
return;
|
return;
|
socket->userData = 1;
|
|
socket->userPtr = command = (char*)malloc(COMMAND_BUFFER_SIZE);
|
socket->userPtr = command = (char*)malloc(COMMAND_BUFFER_SIZE);
|
if(command == NULL)
|
if(command == NULL)
|
{
|
{
|
IPClose(socket);
|
IPClose(socket);
|
return;
|
return;
|
Line 445... |
Line 455... |
command[0] = 0;
|
command[0] = 0;
|
return;
|
return;
|
}
|
}
|
if(command == NULL)
|
if(command == NULL)
|
return;
|
return;
|
|
socket->dontFlush = 0;
|
buf[bytes] = 0;
|
buf[bytes] = 0;
|
length = (int)strlen(command);
|
length = (int)strlen(command);
|
for(j = 0; j < bytes; ++j)
|
for(j = 0; j < bytes; ++j)
|
{
|
{
|
if(buf[j] == 255)
|
if(buf[j] == 255)
|
Line 505... |
Line 516... |
}
|
}
|
}
|
}
|
ptr = strstr(command, "\r\n");
|
ptr = strstr(command, "\r\n");
|
if(ptr)
|
if(ptr)
|
{
|
{
|
|
// Save command in CommandHistory
|
ptr[0] = 0;
|
ptr[0] = 0;
|
bufOut[0] = 0;
|
|
length = (int)strlen(command);
|
length = (int)strlen(command);
|
if(command[0] && length < COMMAND_BUFFER_SIZE)
|
if(length == 0)
|
|
{
|
|
IPPrintf(socket, "-> ");
|
|
continue;
|
|
}
|
|
if(length < COMMAND_BUFFER_SIZE)
|
{
|
{
|
// Save command in CommandHistory
|
|
memmove(CommandHistory + length + 1, CommandHistory,
|
memmove(CommandHistory + length + 1, CommandHistory,
|
sizeof(CommandHistory) - length - 1);
|
sizeof(CommandHistory) - length - 1);
|
strcpy(CommandHistory, command);
|
strcpy(CommandHistory, command);
|
CommandHistory[sizeof(CommandHistory)-1] = 0;
|
CommandHistory[sizeof(CommandHistory)-1] = 0;
|
for(i = COMMAND_BUFFER_COUNT-2; i >= 0; --i)
|
for(i = COMMAND_BUFFER_COUNT-2; i >= 0; --i)
|
Line 525... |
Line 540... |
else
|
else
|
CommandPtr[i+1] = CommandPtr[i] + length + 1;
|
CommandPtr[i+1] = CommandPtr[i] + length + 1;
|
}
|
}
|
CommandPtr[0] = CommandHistory;
|
CommandPtr[0] = CommandHistory;
|
}
|
}
|
if(strncmp(command, "help", 4) == 0)
|
|
|
//Start command
|
|
for(i = 0; i < 10; ++i)
|
|
argv[i] = "";
|
|
i = 0;
|
|
argv[i++] = command;
|
|
for(ptr = command; *ptr && i < 10; ++ptr)
|
{
|
{
|
sprintf((char*)bufOut, "Commands: help, exit, cat, cp, ls, mkdir, rm, flasherase");
|
if(*ptr == ' ')
|
for(i = 0; TelnetFuncList[i].name; ++i)
|
|
{
|
{
|
strcat((char*)bufOut, ", ");
|
*ptr = 0;
|
strcat((char*)bufOut, TelnetFuncList[i].name);
|
argv[i++] = ptr + 1;
|
}
|
}
|
}
|
}
|
else if(strncmp(command, "exit", 4) == 0)
|
if(argv[0][0] == 0)
|
{
|
{
|
free(command);
|
IPPrintf(socket, "-> ");
|
socket->userPtr = NULL;
|
continue;
|
IPClose(socket);
|
|
return;
|
|
}
|
}
|
else if(command[0])
|
found = 0;
|
{
|
|
strcpy(bufOut, command);
|
|
ptr = strstr(bufOut, " ");
|
|
if(ptr)
|
|
ptr[0] = 0;
|
|
for(i = 0; TelnetFuncList[i].name; ++i)
|
for(i = 0; TelnetFuncList[i].name; ++i)
|
{
|
{
|
if(strcmp(bufOut, TelnetFuncList[i].name) == 0)
|
if(strcmp(command, TelnetFuncList[i].name) == 0 &&
|
|
TelnetFuncList[i].func)
|
{
|
{
|
TelnetFuncList[i].func(socket, command);
|
found = 1;
|
|
TelnetFuncList[i].func(socket, argv);
|
break;
|
break;
|
}
|
}
|
}
|
}
|
bufOut[0] = 0;
|
#ifdef DLL_SETUP
|
if(TelnetFuncList[i].name == NULL)
|
if(found == 0)
|
{
|
{
|
rc = BusyBox(socket, command);
|
strcpy(buf, "/flash/bin/");
|
if(rc)
|
strcat(buf, argv[0]);
|
sprintf((char*)bufOut, "Unknown command (%s)", command);
|
argv[0] = buf;
|
|
ConsoleRun(socket, argv);
|
}
|
}
|
}
|
#endif
|
strcat((char*)bufOut, "\r\n-> ");
|
|
IPPrintf(socket, (char*)bufOut);
|
|
command[0] = 0;
|
command[0] = 0;
|
length = 0;
|
length = 0;
|
CommandIndex = 0;
|
CommandIndex = 0;
|
}
|
if(socket->dontFlush == 0)
|
}
|
IPPrintf(socket, "\r\n-> ");
|
|
} //command entered
|
|
} //bytes
|
IPWriteFlush(socket);
|
IPWriteFlush(socket);
|
}
|
}
|
|
|
|
|
void TelnetInit(TelnetFunc_t *funcList)
|
void TelnetInit(TelnetFunc_t *funcList)
|
Line 588... |
Line 604... |
|
|
static uint8 myBuffer[1024*3];
|
static uint8 myBuffer[1024*3];
|
static IPSocket *socketTelnet;
|
static IPSocket *socketTelnet;
|
|
|
|
|
//Implements cat, cp, rm, mkdir, ls
|
static void ConsoleHelp(IPSocket *socket, char *argv[])
|
int BusyBox(IPSocket *socket, char *command)
|
|
{
|
{
|
FILE *fileIn, *fileOut;
|
char buf[200];
|
int bytes, bytes2;
|
int i;
|
char bufA[COMMAND_BUFFER_SIZE];
|
strcpy(buf, "Commands: ");
|
char bufB[COMMAND_BUFFER_SIZE];
|
for(i = 0; TelnetFuncList[i].name; ++i)
|
char bufC[COMMAND_BUFFER_SIZE];
|
|
char bufD[COMMAND_BUFFER_SIZE];
|
|
memset(bufA, 0, sizeof(bufA));
|
|
memset(bufB, 0, sizeof(bufB));
|
|
memset(bufC, 0, sizeof(bufC));
|
|
memset(bufD, 0, sizeof(bufD));
|
|
sscanf(command, "%s %s %s %s", bufA, bufB, bufC, bufD);
|
|
if(strcmp(bufA, "cat") == 0)
|
|
{
|
{
|
fileIn = fopen(bufB, "r");
|
if(TelnetFuncList[i].func)
|
if(fileIn)
|
|
{
|
{
|
|
if(i)
|
|
strcat(buf, ", ");
|
|
strcat(buf, TelnetFuncList[i].name);
|
|
}
|
|
}
|
|
IPPrintf(socket, buf);
|
|
}
|
|
|
|
|
|
static void ConsoleExit(IPSocket *socket, char *argv[])
|
|
{
|
|
free(argv[0]);
|
|
socket->userPtr = NULL;
|
|
IPClose(socket);
|
|
}
|
|
|
|
|
|
static void ConsoleCat(IPSocket *socket, char *argv[])
|
|
{
|
|
FILE *file;
|
|
uint8 buf[200];
|
|
int bytes;
|
|
|
|
file = fopen(argv[1], "r");
|
|
if(file == NULL)
|
|
return;
|
for(;;)
|
for(;;)
|
{
|
{
|
bytes = fread(bufD, 1, sizeof(bufD), fileIn);
|
bytes = fread(buf, 1, sizeof(buf), file);
|
if(bytes == 0)
|
if(bytes == 0)
|
break;
|
break;
|
bytes2 = IPWrite(socket, (uint8*)bufD, bytes);
|
IPWrite(socket, buf, bytes);
|
if(bytes != bytes2)
|
|
printf("(%d %d) ", bytes, bytes2);
|
|
}
|
}
|
fclose(fileIn);
|
fclose(file);
|
}
|
|
return 0;
|
|
}
|
}
|
else if(strcmp(bufA, "cp") == 0)
|
|
{
|
|
fileIn = fopen(bufB, "r");
|
static void ConsoleCp(IPSocket *socket, char *argv[])
|
if(fileIn)
|
|
{
|
{
|
fileOut = fopen(bufC, "w");
|
FILE *fileIn, *fileOut;
|
|
uint8 buf[200];
|
|
int bytes;
|
|
|
|
fileIn = fopen(argv[1], "r");
|
|
if(fileIn == NULL)
|
|
return;
|
|
fileOut = fopen(argv[2], "w");
|
if(fileOut)
|
if(fileOut)
|
{
|
{
|
for(;;)
|
for(;;)
|
{
|
{
|
bytes = fread(bufD, 1, sizeof(bufD), fileIn);
|
bytes = fread(buf, 1, sizeof(buf), fileIn);
|
if(bytes == 0)
|
if(bytes == 0)
|
break;
|
break;
|
bytes2 = fwrite(bufD, 1, bytes, fileOut);
|
fwrite(buf, 1, bytes, fileOut);
|
}
|
}
|
fclose(fileOut);
|
fclose(fileOut);
|
}
|
}
|
fclose(fileIn);
|
fclose(fileIn);
|
}
|
}
|
return 0;
|
|
}
|
|
else if(strcmp(bufA, "rm") == 0)
|
static void ConsoleRm(IPSocket *socket, char *argv[])
|
{
|
{
|
OS_fdelete(bufB);
|
(void)socket;
|
return 0;
|
OS_fdelete(argv[1]);
|
}
|
}
|
else if(strcmp(bufA, "mkdir") == 0)
|
|
|
|
|
static void ConsoleMkdir(IPSocket *socket, char *argv[])
|
{
|
{
|
OS_fmkdir(bufB);
|
(void)socket;
|
return 0;
|
OS_fmkdir(argv[1]);
|
}
|
}
|
else if(strcmp(bufA, "ls") == 0)
|
|
{
|
|
fileIn = fopen(bufB, "r");
|
static void ConsoleLs(IPSocket *socket, char *argv[])
|
if(fileIn)
|
|
{
|
{
|
|
FILE *file;
|
|
uint8 buf[200];
|
|
int bytes;
|
|
|
|
file = fopen(argv[1], "r");
|
|
if(file == NULL)
|
|
return;
|
for(;;)
|
for(;;)
|
{
|
{
|
bytes = OS_fdir(fileIn, bufD);
|
bytes = OS_fdir(file, buf);
|
if(bytes)
|
if(bytes)
|
break;
|
break;
|
strcat(bufD, " ");
|
strcat(buf, " ");
|
IPWrite(socket, (uint8*)bufD, strlen(bufD));
|
IPWrite(socket, buf, (int)strlen((char*)buf));
|
}
|
}
|
fclose(fileIn);
|
fclose(file);
|
}
|
|
return 0;
|
|
}
|
}
|
|
|
|
|
#ifdef INCLUDE_FLASH
|
#ifdef INCLUDE_FLASH
|
else if(strcmp(bufA, "flasherase") == 0)
|
static void ConsoleFlashErase(IPSocket *socket, char *argv[])
|
{
|
{
|
strcpy(bufD, "\r\nErasing");
|
int bytes;
|
IPWrite(socket, (uint8*)bufD, strlen(bufD));
|
(void)argv;
|
IPWriteFlush(socket);
|
IPPrintf(socket, "\r\nErasing");
|
strcpy(bufD, ".");
|
|
for(bytes = 1024*128; bytes < 1024*1024*16; bytes += 1024*128)
|
for(bytes = 1024*128; bytes < 1024*1024*16; bytes += 1024*128)
|
{
|
{
|
IPWrite(socket, (uint8*)bufD, strlen(bufD));
|
IPPrintf(socket, ".");
|
IPWriteFlush(socket);
|
|
FlashErase(bytes);
|
FlashErase(bytes);
|
}
|
}
|
strcpy(bufD, "\r\nMust Reboot\r\n");
|
IPPrintf(socket, "\r\nMust Reboot\r\n");
|
IPWrite(socket, (uint8*)bufD, strlen(bufD));
|
|
IPWriteFlush(socket);
|
|
OS_ThreadSleep(OS_WAIT_FOREVER);
|
OS_ThreadSleep(OS_WAIT_FOREVER);
|
return 0;
|
|
}
|
}
|
#endif
|
#endif
|
return -1;
|
|
}
|
|
|
|
|
|
void TransferDone(uint8 *data, int bytes)
|
void TransferDone(uint8 *data, int bytes)
|
{
|
{
|
printf("TransferDone(0x%x, %d)\n", data, bytes);
|
printf("TransferDone(0x%x, %d)\n", data, bytes);
|
Line 702... |
Line 737... |
data[500] = 0;
|
data[500] = 0;
|
printf("%s\n", data);
|
printf("%s\n", data);
|
}
|
}
|
|
|
|
|
static void ConsoleInfo(IPSocket *socket, char *command)
|
static void ConsoleMath(IPSocket *socket, char *argv[])
|
{
|
{
|
(void)command;
|
int v1, v2, ch;
|
IPPrintf(socket, "Steve was here!");
|
if(argv[3][0] == 0)
|
}
|
|
|
|
|
|
static void ConsoleMath(IPSocket *socket, char *command)
|
|
{
|
|
int v1, v2;
|
|
char buf[20], buf2[20];
|
|
(void)socket;
|
|
if(strlen(command) < 6)
|
|
{
|
{
|
IPPrintf(socket, "Usage: math <number> <operator> <value>\r\n");
|
IPPrintf(socket, "Usage: math <number> <operator> <value>\r\n");
|
return;
|
return;
|
}
|
}
|
sscanf(command, "%s%d%s%d", buf, &v1, buf2, &v2);
|
v1 = atoi(argv[1]);
|
if(buf2[0] == '+')
|
ch = argv[2][0];
|
|
v2 = atoi(argv[3]);
|
|
if(ch == '+')
|
v1 += v2;
|
v1 += v2;
|
else if(buf2[0] == '-')
|
else if(ch == '-')
|
v1 -= v2;
|
v1 -= v2;
|
else if(buf2[0] == '*')
|
else if(ch == '*')
|
v1 *= v2;
|
v1 *= v2;
|
else if(buf2[0] == '/')
|
else if(ch == '/')
|
{
|
{
|
if(v2 != 0)
|
if(v2 != 0)
|
v1 /= v2;
|
v1 /= v2;
|
}
|
}
|
sprintf((char*)myBuffer, "%d", v1);
|
IPPrintf(socket, "%d", v1);
|
IPPrintf(socket, (char*)myBuffer);
|
|
}
|
}
|
|
|
|
|
void PingCallback(IPSocket *socket)
|
void PingCallback(IPSocket *socket)
|
{
|
{
|
IPSocket *socket2 = socket->userPtr;
|
IPSocket *socket2 = socket->userPtr;
|
//printf("Ping Reply\n");
|
|
IPClose(socket);
|
IPClose(socket);
|
if(socket2)
|
if(socket2)
|
IPPrintf(socket2, "Ping Reply");
|
IPPrintf(socket2, "Ping Reply");
|
else
|
else
|
printf("Ping Reply\n");
|
printf("Ping Reply\n");
|
Line 764... |
Line 790... |
myBuffer[0] = 'A';
|
myBuffer[0] = 'A';
|
IPWrite(socketPing, myBuffer, 1);
|
IPWrite(socketPing, myBuffer, 1);
|
}
|
}
|
|
|
|
|
static void ConsolePing(IPSocket *socket, char *command)
|
static void ConsolePing(IPSocket *socket, char *argv[])
|
{
|
{
|
int ip0, ip1, ip2, ip3;
|
int ip0, ip1, ip2, ip3;
|
char buf[20];
|
|
IPSocket *socketPing;
|
IPSocket *socketPing;
|
if('0' <= command[5] && command[5] <= '9')
|
if('0' <= argv[1][0] && argv[1][0] <= '9')
|
{
|
{
|
sscanf(command, "%s %d.%d.%d.%d", buf, &ip0, &ip1, &ip2, &ip3);
|
sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
|
ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
|
ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
|
socketPing = IPOpen(IP_MODE_PING, ip0, 0, PingCallback);
|
socketPing = IPOpen(IP_MODE_PING, ip0, 0, PingCallback);
|
socketPing->userPtr = socket;
|
socketPing->userPtr = socket;
|
myBuffer[0] = 'A';
|
myBuffer[0] = 'A';
|
IPWrite(socketPing, myBuffer, 1);
|
IPWrite(socketPing, myBuffer, 1);
|
IPPrintf(socket, "Sent ping");
|
IPPrintf(socket, "Sent ping");
|
}
|
}
|
else
|
else
|
{
|
{
|
IPResolve(command+5, DnsResultCallback, socket);
|
IPResolve(argv[1], DnsResultCallback, socket);
|
IPPrintf(socket, "Sent DNS request");
|
IPPrintf(socket, "Sent DNS request");
|
}
|
}
|
}
|
}
|
|
|
|
|
Line 794... |
Line 819... |
data[length] = 0;
|
data[length] = 0;
|
IPPrintf(socketTelnet, "Transfer Done");
|
IPPrintf(socketTelnet, "Transfer Done");
|
}
|
}
|
|
|
|
|
static void ConsoleFtp(IPSocket *socket, char *command)
|
static void ConsoleFtp(IPSocket *socket, char *argv[])
|
{
|
{
|
char buf[40], user[40], passwd[40], name[COMMAND_BUFFER_SIZE];
|
|
int ip0, ip1, ip2, ip3;
|
int ip0, ip1, ip2, ip3;
|
if(strlen(command) < 10)
|
if(argv[1][0] == 0)
|
{
|
{
|
IPPrintf(socket, "ftp #.#.#.# User Password File");
|
IPPrintf(socket, "ftp #.#.#.# User Password File");
|
return;
|
return;
|
}
|
}
|
sscanf(command, "%s %d.%d.%d.%d %s %s %s",
|
sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
|
buf, &ip0, &ip1, &ip2, &ip3, user, passwd, name);
|
|
ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
|
ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
|
socketTelnet = socket;
|
socketTelnet = socket;
|
FtpTransfer(ip0, user, passwd, name, myBuffer, sizeof(myBuffer)-1,
|
FtpTransfer(ip0, argv[2], argv[3], argv[4], myBuffer, sizeof(myBuffer)-1,
|
0, ConsoleTransferDone);
|
0, ConsoleTransferDone);
|
}
|
}
|
|
|
|
|
static void ConsoleTftp(IPSocket *socket, char *command)
|
static void ConsoleTftp(IPSocket *socket, char *argv[])
|
{
|
{
|
char buf[COMMAND_BUFFER_SIZE], name[80];
|
|
int ip0, ip1, ip2, ip3;
|
int ip0, ip1, ip2, ip3;
|
if(strlen(command) < 10)
|
if(argv[1][0] == 0)
|
{
|
{
|
IPPrintf(socket, "tftp #.#.#.# File");
|
IPPrintf(socket, "tftp #.#.#.# File");
|
return;
|
return;
|
}
|
}
|
sscanf(command, "%s %d.%d.%d.%d %s",
|
sscanf(argv[1], "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
|
buf, &ip0, &ip1, &ip2, &ip3, name);
|
|
ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
|
ip0 = (ip0 << 24) | (ip1 << 16) | (ip2 << 8) | ip3;
|
socketTelnet = socket;
|
socketTelnet = socket;
|
TftpTransfer(ip0, name, myBuffer, sizeof(myBuffer)-1, ConsoleTransferDone);
|
TftpTransfer(ip0, argv[2], myBuffer, sizeof(myBuffer)-1, ConsoleTransferDone);
|
}
|
|
|
|
|
|
static void ConsoleShow(IPSocket *socket, char *command)
|
|
{
|
|
(void)command;
|
|
IPPrintf(socket, (char*)myBuffer);
|
|
}
|
}
|
|
|
|
|
static void ConsoleClear(IPSocket *socket, char *command)
|
static void ConsoleMkfile(IPSocket *socket, char *argv[])
|
{
|
|
(void)socket;
|
|
(void)command;
|
|
memset(myBuffer, 0, sizeof(myBuffer));
|
|
}
|
|
|
|
|
|
static void ConsoleMkfile(IPSocket *socket, char *command)
|
|
{
|
{
|
OS_FILE *file;
|
OS_FILE *file;
|
(void)command;
|
(void)argv;
|
file = fopen("myfile.txt", "w");
|
file = fopen("myfile.txt", "w");
|
fwrite("Hello World!", 1, 12, file);
|
fwrite("Hello World!", 1, 12, file);
|
fclose(file);
|
fclose(file);
|
IPPrintf(socket, "Created myfile.txt");
|
IPPrintf(socket, "Created myfile.txt");
|
}
|
}
|
|
|
|
|
#ifdef DLL_SETUP
|
#ifdef DLL_SETUP
|
#define DLL_ADDRESS 0x10100000
|
|
#include "dll.h"
|
#include "dll.h"
|
static void ConsoleRun(IPSocket *socket, char *command)
|
|
|
static void ConsoleRun(IPSocket *socket, char *argv[])
|
{
|
{
|
OS_FILE *file;
|
FILE *file;
|
char buf[COMMAND_BUFFER_SIZE], name[80], arg0[80], arg1[80];
|
int bytes, i;
|
int bytes;
|
uint8 code[128];
|
DllFunc funcPtr;
|
DllFunc funcPtr;
|
|
DllInfo info;
|
memset(arg0, 0, sizeof(arg0));
|
int *bss;
|
memset(arg1, 0, sizeof(arg1));
|
char *command, *ptr;
|
sscanf(command, "%s %s %s %s", buf, name, arg0, arg1);
|
|
file = fopen(name, "r");
|
if(strcmp(argv[0], "run") == 0)
|
|
++argv;
|
|
info.socket = socket;
|
|
info.dllFuncList = DllFuncList;
|
|
file = fopen(argv[0], "r");
|
if(file == NULL)
|
if(file == NULL)
|
{
|
{
|
IPPrintf(socket, "Can't find %s", name);
|
IPPrintf(socket, "Can't find %s", argv[0]);
|
return;
|
return;
|
}
|
}
|
memset((void*)DLL_ADDRESS, 0, 1024*256);
|
|
bytes = fread((uint8*)DLL_ADDRESS, 1, 1024*1024*8, file);
|
bytes = fread(code, 1, sizeof(code), file);
|
|
funcPtr = (DllFunc)code;
|
|
funcPtr(&info); //call entry() to fill in info
|
|
|
|
memcpy(info.entry, code, bytes);
|
|
bytes = fread(info.entry + bytes, 1, 1024*1024*8, file) + bytes;
|
fclose(file);
|
fclose(file);
|
funcPtr = (DllFunc)DLL_ADDRESS;
|
printf("address=0x%x bytes=%d\n", (int)info.entry, bytes);
|
funcPtr(DllFList, socket, arg0, arg1);
|
for(bss = info.bssStart; bss < info.bssEnd; ++bss)
|
|
*bss = 0;
|
|
*info.pDllF = DllFuncList;
|
|
|
|
//Register new command
|
|
command = argv[0];
|
|
for(;;)
|
|
{
|
|
ptr = strstr(command, "/");
|
|
if(ptr == NULL)
|
|
break;
|
|
command = ptr + 1;
|
|
}
|
|
for(i = 0; TelnetFuncList[i].name; ++i)
|
|
{
|
|
if(TelnetFuncList[i].name[0] == 0 ||
|
|
strcmp(TelnetFuncList[i].name, command) == 0)
|
|
{
|
|
TelnetFuncList[i].name = (char*)malloc(40);
|
|
strcpy(TelnetFuncList[i].name, command);
|
|
TelnetFuncList[i].func = (ConsoleFunc)info.startPtr;
|
|
break;
|
|
}
|
|
}
|
|
|
|
socket->userFunc = socket->funcPtr;
|
|
info.startPtr(socket, argv);
|
}
|
}
|
#endif
|
#endif
|
|
|
|
|
|
#ifdef EDIT_FILE
|
|
extern void EditFile(IPSocket *socket, char *argv[]);
|
|
#endif
|
|
|
static TelnetFunc_t MyFuncs[] = {
|
static TelnetFunc_t MyFuncs[] = {
|
{"info", 0, ConsoleInfo},
|
{"cat", ConsoleCat},
|
{"math", 0, ConsoleMath},
|
{"cp", ConsoleCp},
|
{"ping", 0, ConsolePing},
|
{"exit", ConsoleExit},
|
{"ftp", 0, ConsoleFtp},
|
#ifdef INCLUDE_FLASH
|
{"tftp", 0, ConsoleTftp},
|
{"flashErase", ConsoleFlashErase},
|
{"show", 0, ConsoleShow},
|
#endif
|
{"clear", 0, ConsoleClear},
|
{"ftp", ConsoleFtp},
|
{"mkfile", 0, ConsoleMkfile},
|
{"help", ConsoleHelp},
|
|
{"ls", ConsoleLs},
|
|
{"math", ConsoleMath},
|
|
{"mkdir", ConsoleMkdir},
|
|
{"mkfile", ConsoleMkfile},
|
|
{"ping", ConsolePing},
|
|
{"rm", ConsoleRm},
|
|
{"tftp", ConsoleTftp},
|
#ifdef DLL_SETUP
|
#ifdef DLL_SETUP
|
{"run", 0, ConsoleRun},
|
{"run", ConsoleRun},
|
|
#endif
|
|
#ifdef EDIT_FILE
|
|
{"edit", EditFile},
|
#endif
|
#endif
|
{NULL, 0, NULL}
|
{"", NULL},
|
|
{"", NULL},
|
|
{"", NULL},
|
|
{"", NULL},
|
|
{"", NULL},
|
|
{"", NULL},
|
|
{"", NULL},
|
|
{"", NULL},
|
|
{"", NULL},
|
|
{"", NULL},
|
|
{"", NULL},
|
|
{NULL, NULL}
|
};
|
};
|
|
|
|
|
void ConsoleInit(void)
|
void ConsoleInit(void)
|
{
|
{
|