⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fshell.c

📁 Embeded MCU Tcpip code.
💻 C
📖 第 1 页 / 共 2 页
字号:
/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU */
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR */
/* ELIGIBILITY FOR ANY PURPOSES.                                             */
/*                 (C) Fujitsu Microelectronics Europe GmbH                  */
/*---------------------------------------------------------------------------
  FShell, the Fujitsu Shell for OpenTCP stack  
  
  FSHELL.C

/*---------------------------------------------------------------------------*/


#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "../inet/datatypes.h"
#include "../inet/globalvariables.h"
#include "../inet/debug.h"
#include "../inet/system.h"
#include "../inet/tcp_ip.h"
#include "../inet/telnetd/telnetd.h"
#include "../inet/shell/fshell.h"
#include "../../help.h"
#include "../../stats.h"
#include "../inet/timers.h"
#include "../inet/http/http_server.h"
#include "../../flash.h"

struct _connection FShellConnects;       /* Structure to manage Shell Session */

/* Shell command constants */
const char help[]       = "help";
const char info[]       = "info";
const char ethstats[]   = "ethstats";
const char closeconn[]  = "exit";
const char sysinfo[]    = "sys";
const char ipserver[]   = "ipserver";
const char ipclient[]   = "ipclient";
const char httpses[]    = "httpses";
const char flashinit[]  = "flashinit";
const char hellomsg[] = "This is the Fujitsu Shell. Copyright 2008 Fujitsu LTD.\r\nFor command reference type \"help\"\r\n";


/* Text constants */
const char helptext[] = "\r\nFujitsu Shell commands:\r\n\
  help       --> This helptext\r\n\
  sys        --> Info about running system\r\n\
  info       --> Fujitsu disclaimer\r\n\
  ethstats   --> Ethernet statistics\r\n\
  ipserver   --> Print IP Address of Telnet Server\r\n
  ipclient   --> Print IP Address of Telnet Client\r\n
  httpses    --> Show status of http sessions\r\n
  flashinit  --> Erases Satellite Flash memory\r\n
  exit       --> Disconnect\r\n
Note: You can abbreviate commands like ipserver ==> ips\r\n";

UINT8 option_answer[3];

/*-----------------------------------------------------------------------------
 UINT8 shell_init(void)
 This is the shell_init function. It is called once from the main thread and
 reserves memory for the fshell session data.
 Return value: none
-----------------------------------------------------------------------------*/
UINT8 shell_init(void)
{
  UINT16 i;
  
  /* Register Shell callbacks */
  telnetd_registershell(fshellconnect, fshellcommand, fshelldata, fshellclose);
  
  /* Delete structure variables */
  FShellConnects.ipaddr = 0;
  FShellConnects.port = 0;
  FShellConnects.connected = 0;
  FShellConnects.helloprompt = 0;
  FShellConnects.shell_prompt_ready = 0;
  FShellConnects.datainqueue = 0;
  FShellConnects.datalength = 0;
  FShellConnects.tmem = NULL;
  for (i=0; i<RX_BUFFER_SIZE; i++)
    FShellConnects.rx_buffer[i] = 0;
  FShellConnects.buffer_ptr = 0;
  
  /* Information message */
  printf("fshell: register callbacks\n");
  FShellConnects.shell_prompt_ready = 0;

  /* there is no point where we can get an error, so return 1 */
  return(1);
}

/*-----------------------------------------------------------------------------
 void shell_run(void)
 This is the shell_run function. It is called periodically from the main thread
 and handles the shell process.
 Return value: none
-----------------------------------------------------------------------------*/
void shell_run(void)
{
  
  if(FShellConnects.connected)
  {  
    /* Show hello message on connect*/
    if((FShellConnects.connected == 1) && (FShellConnects.helloprompt == 0))
    {
      telnetd_sendstring(hellomsg);
      FShellConnects.helloprompt = 1;
      FShellConnects.shell_prompt_ready = 1;
      return;
    }
    
    /* Flush data in transaction queue */
    if(FShellConnects.tmem && FShellConnects.datainqueue)
    {
      telnetd_sendstring(FShellConnects.tmem);
      FShellConnects.datainqueue = 0;
      return;
    }
    
    /* free() memory */
    if((FShellConnects.tmem) && (FShellConnects.datainqueue == 0))
    {
      free(FShellConnects.tmem);
      FShellConnects.tmem = NULL;
      return;
    }
    
    /* Show prompt after every command */
    if((FShellConnects.shell_prompt_ready))
    {
      
      if (telnetd_sendstring(SHELLPROMPT) == 1)
        FShellConnects.shell_prompt_ready = 0;
      return;
    }  
  }
}

/*-----------------------------------------------------------------------------
 void shellcmd_exit(void)
 When shellcmd_exit() is called, the shell disconnects from the client and 
 deletes the session data.
 Return value: none
-----------------------------------------------------------------------------*/
void shellcmd_exit(void)
{
  fshellclose(0, 0);
  telnetd_close();
}

/*-----------------------------------------------------------------------------
 void fshellconnect(UINT32 tipaddr, UINT32 tport)
 This function is called when a client attempts to connect to the telnet server.
 It is called from the telnet server.
 Return value: none
-----------------------------------------------------------------------------*/
void fshellconnect(UINT32 tipaddr, UINT32 tport)
{
  /* Fill structure variables */
  FShellConnects.ipaddr = tipaddr;
  FShellConnects.port = tport;
  FShellConnects.connected = 1;
  FShellConnects.helloprompt = 0;
  
}

/*-----------------------------------------------------------------------------
 void fshellcommand(char* tcommand, UINT32 tlen)
 When the connected client send a command, this function is called from the
 telnet server.
 Return value: none
-----------------------------------------------------------------------------*/
void fshellcommand(char* tcommand, UINT32 tlen)
{  
  // no options supported at this time
  // here, we need to reply with DON'T / WON'T respectively
  
  // An option has minimum 3 byte: IAC, DO/WILL, option code
  if (tlen >=3)
  {
    option_answer[0] = 255;
    option_answer[2] = *(tcommand+2);
    if (*(tcommand+1) == 253) // DO request
    {
      option_answer[1] = 254;
    }
    else if (*(tcommand+1) == 251) // WILL request
    {
      option_answer[1] = 252;
    }
    else
    {
      option_answer[1] = *(tcommand+1);
    }
  }
  
  telnetd_sendstring((const char *)option_answer);
}

/*-----------------------------------------------------------------------------
 void fshelldata(char* tdata, UINT32 tlen)
 When the client sends data, this function is called from the telnet server.
 In this function, shell commands are parsed.
 Return value: none
-----------------------------------------------------------------------------*/
void fshelldata(char* tdata, UINT32 tlen)
{
  UINT16 i;

  /* check if only CR was transmitted */
  if (strstr((const char *)tdata, "\r") == (const char *)tdata)
  {
    if (FShellConnects.buffer_ptr == 0)
    {
      FShellConnects.shell_prompt_ready = 1;
      return;
    }
  }
  
  /* copy received byte(s) into buffer to re-assemble commands */
  if (tlen > (RX_BUFFER_SIZE - FShellConnects.buffer_ptr))
  {
    strncpy((char *)&FShellConnects.rx_buffer[FShellConnects.buffer_ptr], tdata, (RX_BUFFER_SIZE - FShellConnects.buffer_ptr));
    FShellConnects.buffer_ptr = RX_BUFFER_SIZE;
  }
  else
  {
    strncpy((char *)&FShellConnects.rx_buffer[FShellConnects.buffer_ptr], tdata, tlen);
    FShellConnects.buffer_ptr += tlen;
  }

  if (strstr(tdata, "\r") != NULL)
  {
    /* String "sys" */
    if(!strncmp(sysinfo, 
                (const char *)FShellConnects.rx_buffer, 
                strcspn((const char *)FShellConnects.rx_buffer, "\r")))
    {
      telnetd_sendstring(systeminfo);
      for (i=0; i<RX_BUFFER_SIZE; i++)
        FShellConnects.rx_buffer[i] = 0;
      FShellConnects.buffer_ptr = 0;
      FShellConnects.shell_prompt_ready = 1;
      return;
    }
  
    /* String "flashinit" */
    if(!strncmp(flashinit, 
                (const char *)FShellConnects.rx_buffer, 
                strcspn((const char *)FShellConnects.rx_buffer, "\r")))
    {
      UINT8 result;
      UINT16 txtlen = 0;
      
      /* if not in use, get new mem from malloc() */
      FShellConnects.tmem = (char*) malloc(sizeof(char)*350);
    
      if(FShellConnects.tmem == NULL)
      {
        printf("NO MEM!\n");
        for (i=0; i<RX_BUFFER_SIZE; i++)
          FShellConnects.rx_buffer[i] = 0;
        FShellConnects.buffer_ptr = 0;
        FShellConnects.shell_prompt_ready = 1;
        return;
      }
    
      printf("Initializing Satellite FLASH...\n");
      result = Sat_Flash_sector_erase(SB1);
      result |= Sat_Flash_sector_erase(SB2);
      result |= Sat_Flash_sector_erase(SB3);
  
      if(result <= 48)
        txtlen += sprintf(FShellConnects.tmem+txtlen, "SUCCESS: Satellite Flash return command %i\r\n", result);
      else
        txtlen += sprintf(FShellConnects.tmem+txtlen, "ERROR: Satellite Flash return command %i\r\n", result);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -