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

📄 cli_main.c

📁 在freescale 的ne64上开发的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <string.h>
#include <stdio.h>
#include <stdlib.h> /*atoi*/
#include "cli_lib.h"
#include "hw_api.h"
#include "timers.h"

UINT8 cmd_node  = 0;/*node mode*/
UINT8 cmd_part  = 1;/*how many keyworks in the command*/
UINT8 character_num = 0;/*command char num*/
UINT32 timeout_times  = 0;/*time out times*/
UINT8 cli_reset  = 0;/*cli reset flag*/
UINT8 password_retry  = 0;
UINT32 serialNum=0;
UINT32 random_number  = 0;
UINT8 hostname[16]={'F','e','n','g','i','n','e',0};
UINT8 password[8] ={'1','2','3','4','5',0,0,0};
UINT8 command[CMD_MAX_LEN+1]; /*buffer for store command*/
/*tinyang*/
UINT8 cmd_index[5];/*store preview command*/
UINT8 cmd_point = 0;
UINT8 cmd_full = 0;
UINT8 current_point = 0;
UINT8 *cmd[CMD_MAX_PART];
UINT8 cli_timer;

const struct tCmdDB cmd_db[]={

	{"help", "Help commands of current node\n", ALL_NODE,1,list_cmd},
	//{"list", "List commands of current node\n", ALL_NODE,1,list_cmd},
	//{"end", "End current node command\n", ALL_NODE,1,end_cmd},
	//{"exit", "Exit from current node\n",ALL_NODE,1,quit_cmd},
	{"quit", "Quit from current node\n",ALL_NODE,1,quit_cmd},
	{"config", "Enter config node\n", SHOW_NODE,1,config_cmd},
	{"show running-config", "Show information\nRunning configuration\n",SHOW_NODE,2,show_run_cmd},
	{"show startup-config", "#\nStartup configuration\n",SHOW_NODE,2,show_start_cmd},
	{"show interface", "#\nInterface infomation\n",SHOW_NODE,2,show_if_cmd},
	{"show interface statistics", "#\n#\nStatistics",SHOW_NODE,3,show_if_statis_cmd},
	{"show version", "#\nVersion information\n",SHOW_NODE,2,show_version_cmd},
	{"show system", "#\nsystem information\n",SHOW_NODE,2,show_system_cmd},
	{"show log", "#\nsystem log\n",SHOW_NODE,2,show_log_cmd},
	{"ping <A.B.C.D> ", "ping\nip address\n",ALL_NODE, 2,ping_host},
	{"reboot", "Reboot System", ALL_NODE,1,reboot_cmd},
	{"write file", "Write to nvram\nSave file", CONFIG_NODE,2,write_file_cmd},
	{"erase startup-config", "Erase configuration\nErase startup configuration",CONFIG_NODE,2,erase_file_cmd},
	{"hostname <HOSTNAME>", "Config hostname\nhostname string\n",CONFIG_NODE,2,hostname_cmd},
	{"password <PASSWORD>", "Config password\npassword string\n",CONFIG_NODE,2,password_cmd},
	{"description <STRING>", "Device description\nDescription String\n",CONFIG_NODE, 2, device_descr_cmd},
	{"ip-address <A.B.C.D> <A.B.C.D>","ip address\naddress,such as 192.168.1.1\nnetmask such as 255.255.255.0\n",CONFIG_NODE,3,set_ipaddress_cmd},
	{"gateway <A.B.C.D>","Config gateway\ngateway addrss,such as 192.168.1.254\n",CONFIG_NODE,2,set_gateway_cmd},
	{"no ip-address", "no\nip-address,snmp config invalid\n",CONFIG_NODE, 2,no_ip_address},

	/*add snmp config command*/
	{"snmp trap-server <1-5> <A.B.C.D> <1-65535>", "snmp\ntrap-server\nserver ID\nTrap server address\nTrap server port\n",CONFIG_NODE, 5,set_snmp_trap},
	{"snmp trap-server <1-5> <A.B.C.D>", "#\n#\n#\n#\ndefault is 162\n",CONFIG_NODE, 4,set_snmp_trap},
	{"no snmp trap-server <1-5>", "#\nsnmp\ntrap-server\nserver ID\n",CONFIG_NODE, 4,no_set_trap},
	{"snmp community <1-5> <community> (ro|rw)", "#\ncommunity\nID\ncommunity name\nread-only or read_write\n",CONFIG_NODE, 5,set_snmp_community},
	{"no snmp community <1-5>", "#\n#\ncommunity\ncommunity ID\n",CONFIG_NODE, 4,no_set_community},
#if 1
	/*assign vlan id command*/
	{"management vlan <0-4095> <0-4095>", "Management vlan\nvlan ID\nfirst vlan ID, 0: no first vlan tag\nsecond vlan ID,0:no second vlan tag\n",CONFIG_NODE, 4,set_manager_vid},
	{"lfp enable", "LFP(link fault pass-through) config\nenable\n",CONFIG_NODE, 2 ,lfp_enable_cmd},
	{"lfp disable", "#\ndisable\n",CONFIG_NODE, 2 ,lfp_disable_cmd},
	{"fef enable", "FEF(far end fault) config\nenable\n",CONFIG_NODE, 2 ,fef_enable_cmd},
	{"fef disable", "#\ndisable\n",CONFIG_NODE, 2 ,fef_disable_cmd},
	{"dot3ah enable", "dot3ah OAM\ndot3ah OAM enable\n",CONFIG_NODE, 2, dot3ah_enable_cmd},
	{"dot3ah disable", "#\ndot3ah OAM disable\n",CONFIG_NODE, 2, dot3ah_disable_cmd},
	{"dot3ah port <1-2>", "#\nOAM port\nport, 1 or 2\n",CONFIG_NODE, 3, dot3ah_port_cmd},	
#endif
#if 1
	{"port <LIST> shutdown","Port config\nPortList:such as 1,2 or 1-2\nShutdown port\n",CONFIG_NODE, 3 ,port_shutdown_cmd},
	{"port <LIST> no-shutdown","#\n#\nNo-shutdown Port\n",CONFIG_NODE,3, port_no_shutdown_cmd},
	{"port <LIST> full enable", "#\n#\nFull duplex\nAuto-nego enable\n",CONFIG_NODE, 4, port_full_enable_cmd},
	{"port <LIST> full disable", "#\n#\n#\nAuto-nego disable\n",CONFIG_NODE, 4, port_full_disable_cmd},
	{"port <LIST> half enable", "#\n#\nHalf duplex\nAuto-nego enable\n",CONFIG_NODE, 4, port_half_enable_cmd},
	{"port <LIST> half disable", "#\n#\n#\nAuto-nego disable\n",CONFIG_NODE, 4, port_half_disable_cmd},
	{"port <LIST> speed (10|100|1000)", "#\n#\nPort Speed\n10M,100M or 1000M\n",CONFIG_NODE, 4, port_speed_cmd},
	{"port <LIST> rate-tx <0-3907>", "#\n#\nRate-tx\n0:No-limit,1-3907:n*64Kbps\n",CONFIG_NODE, 4 ,port_rate_tx_cmd},
	{"port <LIST> rate-bcast <0-3907>", "#\n#\nBroadcast strom control\n0:No-limit,1-3907:n*64Kbps\n",CONFIG_NODE, 4 ,port_rate_bcast_cmd},
	{"port <LIST> rate-m-bcast <0-3907>", "#\n#\nMulti-broadcast strom control\n0:No-limit,1-3907:n*64Kbps\n",CONFIG_NODE, 4 ,port_rate_mbcast_cmd},
	{"port <LIST> flowcontrol enable", "#\n#\nFlow control\nenable\n",CONFIG_NODE, 4 ,port_flow_enable_cmd},
	{"port <LIST> flowcontrol disable", "#\n#\n#\ndisable\n",CONFIG_NODE, 4 ,port_flow_disable_cmd},
#endif
#if 1
	{"show reg <0-31> <0-31>", "show\nchip reg\nphy addr\nreg addr\n",SHADOW_NODE,4,show_reg_cmd},
	{"set reg <0-31> <0-31> <0x0-0xFFFF>", "set\nchip reg\nphy addr\nreg addr\nvalue\n",SHADOW_NODE,5,set_reg_cmd},
	//{"show mcu <0x0-0xFFFF>", "#\nmcu reg\nreg addr\n",SHADOW_NODE,3,show_mcureg_cmd},
	//{"set mcu <0x0-0xFFFF> <0x0-0xFF>", "#\nmcu reg\nreg addr\nvalue\n",SHADOW_NODE,4,set_mcureg_cmd},
	{"mac (AA:BB:CC:DD:EE:FF)","Config mac address\nmac address:such as 00:04:67:00:00:01 or 00.04.67.00.00.01\n",SHADOW_NODE,2,set_mac_cmd},
	{"port <LIST> <1-2> <1-3> <1-4> <0-4096>", "PortConfig\nPortList\n1-twoFiber,2-singleFiber\n1-multiMode,2-singleMode,3-cwdmWave\n1-SC,2-FC,3-ST,4-LC\ndistance",SHADOW_NODE, 6, port_info_cmd},
	{"port <LIST> <0-4096> <0-4096>", "#\n#\nfiber tx wavelength\nfiber rx wavelength\n",SHADOW_NODE, 4, port_wavelength_cmd},
/*	{"show mac","#\nmac address\n",SHADOW_NODE,2,show_mac_cmd},*/
	{"dot3ah mode active", "dot3ah OAM\nOAM mode\nactive mode\n",SHADOW_NODE, 3, dot3ah_active_cmd},
	{"dot3ah mode passive", "#\n#\npassive mode",SHADOW_NODE, 3, dot3ah_passive_cmd},
	{"show dot3ah", "#\ndot3ah information\n",SHADOW_NODE, 2, show_dot3ah_cmd},
	{"debug <0-512>", "Display debug information",SHADOW_NODE, 2, debug_cmd},
#endif
	{0,0,0,0,0},
};

void cli_timer_task(void);

extern UINT8 waitInput;
extern UINT16 debug;

/********Function of taskDelay ******************/
void taskDelay(UINT32 time)
{
    UINT32 i, j;
    for(i = 0; i < time; i ++)
    {
        for (j = 0; j < 7; j ++);
    }
}

/********Function of print_motd ******************/
void  print_motd(void)
{
	printf("\n\r");
	printf("          ");
	printf("********************************************\n\r");printf("          ");
	printf("*          F-engine OL100CR  V1.3          *\n\r");printf("          ");
	printf("*                                          *\n\r");printf("          ");
	printf("*  Copyright 2001-2006 FiberHome Networks  *\n\r");printf("          ");
	printf("********************************************\n\r");
	printf("User Access Verification\n\r");	
}
/********Function of store_cmd ******************/
void restore_cmd(UINT8 mode)
{
	UINT8 index;
	UINT8 i;
	if(mode == PREVIEW_CMD)
	{
		if(current_point == 0)
		{
			if(cmd_full == 1)
				current_point = 4;
			else if(cmd_point == 0)
				return;
			else
				current_point = cmd_point -1;
		}
		else
			current_point--;
		index = cmd_index[current_point];
		
	}
	#if 0
	else if(mode == NEXT_CMD)
	{
		if(current_point == 4)
		{
			if(cmd_full == 1)
				current_point = 0;
			else
				return;
		}
		else
			current_point++;
		index = cmd_index[current_point];
	}
	#endif
	printf(cmd_db[index].cmd_str);
	strcpy(command,cmd_db[index].cmd_str);
	character_num=strlen(command);

	cmd_part=1;
	for(i=0;i<character_num;i++)
	{
		if(command[i]==BLANK_CHAR)
		{	
			cmd_part++;			
		}
	}
}
/********Function of prompt_node ******************/
void  prompt_node(void)
{
	switch(cmd_node)
	{
	case AUTH_NODE1:
		printf("Username:");
		break;
	case AUTH_NODE2:
		printf("Password:");
		break;
	case SHOW_NODE:
		printf(hostname);
		printf("#");
		break;
	case CONFIG_NODE:
		printf(hostname);
		printf("(config)#");
		break;
	case SHADOW_NODE:
		printf(hostname);
		printf("(Super)#");
		break;
	}
}

/********Function of help_command ******************/
void help_command(UINT8 bComplete)
{
	UINT8 i=0;
	UINT8 j=0;
	UINT8 tmp_part=0;
	UINT8 cr_match=0;
	UINT8 match_part=0;
	UINT8 match_cmd=0;
	UINT8 match_idx=0;
	UINT8 *cp;
	UINT8 *dp;
	UINT8 *cp1;
	UINT8 *dp1;
	if(cmd_node==AUTH_NODE1 || cmd_node==AUTH_NODE2)
		return ;
	if(cmd_part>CMD_MAX_PART)
		return ;
	/*split cmd*/
	cmd[0]=command;
	j=1;
	for(i=0;i<character_num;i++)
	{
		if(command[i]==BLANK_CHAR)
		{
			command[i]=0;
			cmd[j]=(command+i+1);
			j++;
		}
	}
	/*found matched command*/
	printf("\n\r");
	match_cmd=0;
	i=0;
	while(cmd_db[i].cmd_str!=0)
	{
		tmp_part=cmd_part;
		if(command[character_num-1]==0)/*if the last char is blank*/
			tmp_part--;
		if((tmp_part>cmd_db[i].cmd_part) ||
			( (cmd_db[i].cmd_node!=cmd_node) &&(cmd_db[i].cmd_node!=ALL_NODE)))
		{
			i++;
			continue;
		}
		match_part=0;
		dp=cmd_db[i].cmd_str;
		for(j=0;j<tmp_part;j++)
		{
			if(strncmp(dp,cmd[j],strlen(cmd[j]))!=0)
			{
				if(dp[0]=='<' || dp[0]=='(')
					match_part++;
				else
					break;
			}
			else
				match_part++;
			cp=strchr(dp,BLANK_CHAR);
			if(cp==NULL)
				break;
			dp=cp+1;
		}
		if(match_part==tmp_part)
		{
			if((tmp_part==cmd_db[i].cmd_part) && (command[character_num-1]==0))
			{
				cr_match++;
				match_cmd++;
			}
			else
			{
				if(command[character_num-1]!=0)
					tmp_part=tmp_part-1;
				dp=cmd_db[i].cmd_str;
				dp1=cmd_db[i].help_str;
				cp=dp;
				cp1=dp1;
				while(tmp_part>0)
				{
					cp=strchr(dp,BLANK_CHAR);
					cp1=strchr(dp1,'\n');
					if(cp==NULL)
						break;
					dp=cp+1;
					dp1=cp1+1;
					tmp_part--;
				}
				if(*dp1!='#')
				{
					match_cmd++;
					match_idx=i;
					printf("  ");
					for(j=0;j<strlen(dp);j++)
					{
						if(*(dp+j)==BLANK_CHAR)
							break;
						printf("%c",*(dp+j));
					}
					for(;j<18;j++)
					{
						printf(" ");
					}
					for(j=0;j<strlen(dp1);j++)
					{
						if(*(dp1+j)=='\n')
							break;
						printf("%c",*(dp1+j));
					}
					printf("\n\r");
				}				
			}
		}
		i++;
	}
	if(cr_match==1)
		printf("  <cr>\n\r");

	if(match_cmd==1 && bComplete==1)
	{
		dp=cmd_db[match_idx].cmd_str;
		cp=dp;
		for(j=0;j<cmd_part-1;j++)
		{
			cp=strchr(dp,BLANK_CHAR);
			if(cp==NULL)
				break;
			dp=cp+1;
		}
		if((command[character_num-1]!=0) && (*dp!='<' && *dp!='('))/*if the last char is blank*/
		{
			for(i=0;i<strlen(dp);i++)
			{
				if(*(dp+i)==BLANK_CHAR)
				{
					break;
				}
				if(cmd[cmd_part-1][i]!=*(dp+i))
				{
					cmd[cmd_part-1][i]=*(dp+i);
					character_num++;
				}
			}
			cmd[cmd_part-1][i]=BLANK_CHAR;
			character_num++;
			cmd_part++;
		}

		//if(strlen(

	}
	/*recover*/
	for(i=0;i<character_num;i++)
	{
		if(command[i]==0)
			command[i]=BLANK_CHAR;
	}
}

/********Function of parse_command ******************/
UINT8 parse_command(void)
{
	UINT8 i=0;
	UINT8 j=0;
	UINT8 match_part=0;
	UINT8 match_cmd=0;
	UINT8 match_idx=0;
	UINT8 *cp;
	UINT8 *dp;	
	if(cmd_node==AUTH_NODE1 || cmd_node==AUTH_NODE2)
		return BAD_CMD;
	/*split command */
	cmd_part=1;
	if(command[character_num-1]==BLANK_CHAR)/*if the last char is blank*/
		cmd_part--;
	cmd[0]=command;
	j=1;
	for(i=0;i<character_num;i++)
	{
		if(command[i]==BLANK_CHAR)
		{
			command[i]=0;
			cmd[j]=(command+i+1);
			cmd_part++;
			j++;
		}
	}
	if(cmd_part>CMD_MAX_PART)
		return BAD_CMD;
	/*found matched command*/
	match_cmd=0;
	i=0;
	while(cmd_db[i].cmd_str!=0)
	{
		if((cmd_part!=cmd_db[i].cmd_part) ||
			( (cmd_db[i].cmd_node!=cmd_node) &&(cmd_db[i].cmd_node!=ALL_NODE)))
		{
			i++;
			continue;
		}
		match_part=0;
		dp=cmd_db[i].cmd_str;
		for(j=0;j<cmd_part;j++)
		{
			if(strncmp(dp,cmd[j],strlen(cmd[j]))!=0)
			{
				if(dp[0]=='<' || dp[0]=='(')

⌨️ 快捷键说明

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