📄 kscmds.c
字号:
// Chip ID and Revision.
//
// Parameters:
// None
//
// Return Vlaue:
// SUCCESS and PARMERROR
//
//--------------------------------------------------------------------------
int DisplayChipID ()
{
BYTE bData;
bData = ReadData (1);
printf ("\n KS8995M Family ID = 0x%x Chip ID = %x Revision ID = %x", GetChipID (), (bData & 0xf0) >> 4, (bData & 0x0e) >> 1 );
return SUCCESS;
}
//--------------------------------------------------------------------------
//
// int ParseMACAddress
//
// Description:
// Parse the command buffer and convert the mac address to two parts, low four bytes and high two bytes.
// return the values via function parameter pointers.
//
// Parameters:
// char *sBuf Command buffer
// char *sPrompt "mac=" string used for locating mac address sting in the command
// unsigned long *uMACLow Buffer to held the low 4 bytes of the MAC address. hh:hh:ll:ll:ll:ll
// unsigned long *uMACHigh Buffer to held the high 2 bytes of the MAC address
//
// Return Vlaue:
// SUCCESS and PARMERROR
//
//--------------------------------------------------------------------------
int ParseMACAddress ( char *sBuf, char *sPrompt, unsigned long *uMACLow, unsigned long *uMACHigh )
{
#define MACLENGTH 12 // MAC address has 16 hex digits
char *pStr;
char *pTemp =NULL;
int i;
pStr = sBuf;
if ( pStr = strstr ( sBuf, sPrompt ) ) // Found the parameter prompt
{
if ( pTemp = strchr ( pStr, BLANK ) ) // Looking for BLANK
{
*pTemp = '\0'; // Make it a null terminted string
}
// Prompts should always have a '=' at the end
if ( pStr = strchr ( pStr, DELIMITER ) )
{
pStr++; // Sip the '='
if ( strlen ( pStr ) != MACLENGTH ) // 16 hex digits
return PARMERROR;
for ( i = 0; i < MACLENGTH; i++ ) // Are they all hex digits
{
if ( !isxdigit ( pStr[1] ) )
return PARMERROR;
}
sscanf ( pStr, " %4x", uMACHigh );
sscanf ( pStr + 4, " %8x", uMACLow );
}
else
return PARMERROR;
}
else
return FAIL;
if ( pTemp )
*pTemp = BLANK;
return SUCCESS;
}
//--------------------------------------------------------------------------
//
// int ParseParameters
//
// Description:
// Parse the command buffer based on prompt sting. Convert it to int value and
// return the value via function parameter pointer.
//
// Parameters:
// char *sBuf Command buffer
// char *sPrompt Prompt string used for locating the value in the command buffer.
// unsigned long *uMACLow Buffer to held the low 4 bytes of the MAC address. hh:hh:ll:ll:ll:ll.
// unsigned long *uMACHigh Buffer to held the high 2 bytes of the MAC address.
//
// Return Vlaue:
// SUCCESS and PARMERROR
//
//--------------------------------------------------------------------------
int ParseParameters( char * sBuf, char *sPrompt, unsigned int *uValue )
{
char *pStr;
char *pTemp = NULL;
pStr = sBuf;
if ( pStr = strstr ( sBuf, sPrompt ) ) // Found the parameter prompt
{
if ( pTemp = strchr ( pStr, BLANK ) ) // Looking for BLANK
{
*pTemp = '\0'; // Make it a null terminted string
}
// Prompts should always have a '=' at the end
if ( pStr = strchr ( pStr, DELIMITER ) )
{
if ( GetHex ( pStr + 1, uValue ) != SUCCESS )
return PARMERROR;
}
else
return PARMERROR;
}
else
return FAIL;
if ( pTemp )
*pTemp = BLANK;
return SUCCESS;
}
//--------------------------------------------------------------------------
//
// int GetParameters
//
// Description:
// Parse the command buffer and convert the values to integer and return the
// values via function parameter pointers.
//
// Parameters:
// char *sBuf Command buffer. sBuf points to the a blank.
// int *elem1 The first integer value found in the command buffer
// int *elem2 The second integer value found in the command buffer
// int *elem3 The third integer value found in the command buffer
//
// Return Vlaue:
// FAIL or SUCCESS
//
//--------------------------------------------------------------------------
int GetParameters ( char *sBuf, int *elem1, int *elem2, int *elem3 )
{
char *sPtr;
int iTemp;
if ( !sBuf )
return FAIL;
sPtr = GetValue ( sBuf, &iTemp );
if ( iTemp == NOPARAMETER || !elem1 )
return FAIL;
*elem1 = iTemp;
if ( sPtr ) // More parameters
{
sPtr = GetValue ( sPtr, &iTemp );
if ( iTemp == NOPARAMETER || !elem2 )
return FAIL;
*elem2 = iTemp;
if ( sPtr ) // More Parameters
{
sPtr = GetValue ( sPtr, &iTemp );
if ( iTemp == NOPARAMETER || !elem3 )
return FAIL;
*elem3 = iTemp;
}
}
return SUCCESS;
}
//--------------------------------------------------------------------------
//
// int ProcessUpdateCmds
//
// Description:
// Search the Update Command table, find the command and call the routine
// associated to the command.
//
// Parameters:
// char *sCmdBuf Command buffer.
//
// Return Vlaue:
// CMDERROR or result of each command routine
//
//--------------------------------------------------------------------------
int ProcessUpdateCmds ( char *sCmdBuf )
{
acmdtable *ctPtr = UpdateCmds;
char *sPtr;
// Looking for the delimiter BLANK
sPtr = strchr ( sCmdBuf, BLANK);
if ( sPtr ) // BLANK found, this also means there is at least one parameter
{
*sPtr = '\0'; // Put the NULL end to the end of command
// It will not be restored back...
}
else
return CMDERROR;
while ( strlen ( ctPtr->cmd ) )
{
if ( !strcmp ( sCmdBuf, ctPtr->cmd ) )// Found it !
{
*sPtr = BLANK; // Restor the BLANK
return ( ctPtr->acmdfun ( sPtr ) );
}
ctPtr++;
}
return CMDERROR;
}
//--------------------------------------------------------------------------
//
// int ProcessSubCmds
//
// Description:
// Search the Command table pointed by *SubCMDTable, find the command and call the routine
// associated to the command.
//
// Parameters:
// char *sCmdBuf Command buffer.
//
// Return Vlaue:
// CMDERROR or result of each command routine
//
//--------------------------------------------------------------------------
int ProcessSubCmds ( char *sCmdBuf, cmdtable *SubCMDTable )
{
char *sPtr;
int elem1 = NOPARAMETER;
int elem2 = NOPARAMETER;
int elem3 = NOPARAMETER;
cmdtable *ctPtr = SubCMDTable;
if ( !ctPtr || !sCmdBuf )
return CMDERROR;
//
// extract command
//
// Looking for the delimiter BLANK
sPtr = strchr ( sCmdBuf, BLANK);
if ( sPtr ) // BLANK found, this also means there is at least one parameter
{
if ( GetParameters ( sPtr, &elem1, &elem2, &elem3 ) == FAIL )
return PARMERROR;
*sPtr = '\0'; // Put the NULL end to the end of command
}
while ( strlen (ctPtr->cmd) )
{
if ( !strcmp ( sCmdBuf, ctPtr->cmd ) )// Found it !
{
if ( sPtr )
*sPtr = BLANK; //Restore blank
return ( ctPtr->cmdfun ( elem1, elem2, elem3 ) );
}
ctPtr++;
}
if ( sPtr )
*sPtr = BLANK; //Restore blank
return CMDERROR;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// SPI Command Set
//
// Command format contains two portions:
// 1. Main command character
// d for dump commands
// r for read commands
// w for write commands
// u for update commands. These commands contain prompt for each value.
// 2. Sub command is the command after the main command character:
// at for analog test registers
// dtab for dynamic MAC address table
//
// some commands do not have sub command, they have only main command character:
// q for quit program
// s for start swirch
// h for help
// id for displaying chip ID.
//
// Command P1 P2 P3 Descriptions
//-----------------------------------------------------------------------------------------------------------------------
// dat Dump analog test registers.
// ddrop (s) Dump dropped packet MIB counters for all ports every "s" seconds until Ctrl-C is detected. Display only once is "s" is not specified.
// ddt Dump digital test registers.
// ddtab n1 n2 Dump dynamic MAC address table. Read a range of dynamic MAC address table. n2 is optional.
// dg Dump Global Control registers.
// dmac Dump MAC address registers.
// dmib Port, (s) Dump MIBS associated to port "Port" every "s" seconds until key 'q' is detected. Display only once if "s" is not specified,
// dmii (n) Dump the MII register "n" or all MII registers if "n" is not specified.
// dn Dump next 20 registers. The register pointer is set by "rr" and "dr" commands or use 0 as default.
// dp Port Dump port control and status registers associated to port number "Port".
// dr Reg Dump 20 registers starting with "Reg".
// dstab (n) Dump static MAC address table. Read the content of the nth entry of the static MAC address table if "n" is specified.
// dtos Dump TOS Priority Control registers.
// dvtab (n) Dump VLAN table. Read the content of the nth entry of the VLAN table if "n" is specified.
//
// h Display command list.
//
// id Display chip ID
//
//
// q Quit the program.
// rmib Port, n, (s) Read the specified MIB counter associated to port "Port" every "s" seconds until Ctrl-C is detected. Display only once if "s" is not specified. Default value for n is 0.
// rn Read next register content. The register pointer is set by the "rr" commande or use register 0 as default.
// rr Reg Read content of register "Reg".
//
// s Start the switch
// uvtab n fid= vid= mbr= on=
// Update VLAN table entry n.
// ustab n fid= ufid= over= on= mac=xxxxxxxxxx
// Update static MAC address table entry n.
//
// wmii n, Data Write Data to MII register "n".
// wn Data Write "Data" to next register. The register pointer is set by the "wr" command or use register 0 as default.
// wr Reg, Data Write "Data" to register "Reg".
// wstab n, Data1 Data2 Write "Data" to the nth entry of the static MAC address table.
// wvtab n, Data Write "Data" to the nth entry of the VLAN table.
// z Reset all MIB counters to 0.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void DisplayCmdList ()
{
printf ( "\ndall Dump all MIB counters" );
printf ( "\ndat Dump analog test registers" );
printf ( "\nddrop (s) Dump dropped packet MIB counters" );
printf ( "\nddt Dump digital test registers" );
printf ( "\nddtab n1 n2 Dump a range of ,from n1 to n2, dynamic MAC address table entries" );
printf ( "\ndg Dump Global Control registers" );
printf ( "\ndmac Dump MAC address registers" );
printf ( "\ndmib Port (s) Dump MIB counters associated to port" );
printf ( "\ndn Dump next 20 registers" );
printf ( "\ndp Port Dump port control and status registers" );
printf ( "\ndr Reg Dump 20 registers starting from Reg" );
printf ( "\ndstab (n) Dump static MAC address table" );
printf ( "\ndtos Dump TOS Priority Control registers" );
printf ( "\ndvtab (n) Dump VLAN table" );
printf ( "\nh Display command list" );
printf ( "\nid Display Chip ID");
printf ( "\nq Quit the program");
printf ( "\nrcnt Read the counter of dynamic MAC address table entry" );
printf ( "\nrmib Port n (s) Read the specified MIB counter" );
printf ( "\nrn Read next register content" );
printf ( "\nrr Reg Read content of the register" );
printf ( "\ns Start the switch" );
printf ( "\nx Repeat previous commad if it is available" );
printf ( "\nwn Data Write to next register" );
printf ( "\nwr Reg Data Write to register" );
printf ( "\nwstab n MAC Data Write to the nth entry of the static MAC address table" );
printf ( "\nwvtab n Data Write to the nth entry of the VLAN table" );
printf ( "\nuvtab n fid= vid= mbr= on=");
printf ( "\n Update VLAN table entry n" );
printf ( "\nustab n fid= ufid= over= on= mac=xxxxxxxxxx");
printf ( "\n Update static MAC address table entry n" );
printf ( "\nz Reset all MIB counters to 0" );
printf ( "\nNOTE: the parameters in parenthesis are optional.");
return;
}
//--------------------------------------------------------------------------
//
// int SPICmds
//
// Description:
// This is the main routine of command processing. It parse the first
// character of the command and dispatch it to command processing routines.
//
// Parameters:
// char *sCmd Command buffer.
//
// Return Vlaue:
// CMDERROR or result of each command routine
//
//--------------------------------------------------------------------------
int SPICmds ( char *sCmd )
{
char *sPtr = sCmd;
int uErrorcode = SUCCESS;
int i, j;
//
// Process main comamnd which is the first character
//
switch ( *sPtr++ )
{
case 'd': // Dump commands
uErrorcode = ProcessSubCmds ( sPtr, DumpCmds );
break;
case 'h':
DisplayCmdList ();
break;
case 'i':
if ( *sPtr == 'd' && *(sPtr+1) == '\0' )
DisplayChipID ();
else
uErrorcode = CMDERROR;
break;
case 'r':
uErrorcode = ProcessSubCmds ( sPtr, ReadCmds );
break;
case 's': // Start the switch
if ( *sPtr == '\0' )
Start95MSwitch ();
else
uErrorcode = CMDERROR;
break;
case 'u':
uErrorcode = ProcessUpdateCmds ( sPtr );
break;
case 'w':
uErrorcode = ProcessSubCmds ( sPtr, WriteCmds );
break;
case 'z': // Zero out all MIB counters for all ports
if ( *sPtr == '\0' )
{
for ( i = 0; i < PORTTOTAL; i++)
for ( j = 0; j < MIBCOUNTEROFFSET; j++)
GetMIBCounter ( (BYTE) i + 1, (BYTE) j );
giMIBOverflow = FALSE;
printf ("\nAll MIB counters have been reset");
}
else
uErrorcode = CMDERROR;
break;
default:
uErrorcode = CMDERROR;
break;
}
return uErrorcode;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -