//命令结构体
struct CMD_STRUCT{
char name; /* Command Name */
char *usage;/* Usage message */
void (*CmdFun)(char *para);/* Command execute function */
};
//命令列表
struct CMD_STRUCT CmdTbl[] = {
{'h', "h - help, list all cmd\n\r", HelpCmdExeFun},
{'s', "s - view log status\n\r", StatusCmdExeFun},
{'f', "f - find in root dir\n\r", FindCmdExeFun},
{'o', "o - open directory\n\r", OpenCmdExeFun},
{'g', "g - get specific file\n\r", GetCmdExeFun},
{'b', "b - back to root directory\n\r", BackCmdExeFun},
{'n', "n - next 10 dirent\n\r", NextCmdExeFun},
{'t', "t - go to top of directory\n\r", TopCmdExeFun},
{'d', "d - download a file from pc\n\r", DownloadCmdExeFun},
{'e', "e - exit shell\n\r", NULL}
};
//打印当前目录名字
static void ShowCurDirectory(void)
{
USART_PutStr(COMMUNICATE_USART, "---------------- in directory : ");
USART_PutStr(COMMUNICATE_USART, CirDirName);
USART_PutStr(COMMUNICATE_USART, " ----------------\n\r");
}
//帮助
void HelpCmdExeFun(char *para)
{
int n;
USART_PutStr(COMMUNICATE_USART, "---------------- here is all cmd supported ----------------\n\r");
for(n=0; n<sizeof(CmdTbl)/sizeof(struct CMD_STRUCT); n++){
USART_PutStr(COMMUNICATE_USART, CmdTbl[n].usage);
}
}
//查看日志系统状态
void StatusCmdExeFun(char *para)
{
unsigned int num;
num = 100 * yaffs_freespace(ROOT_DIR_NAME) / yaffs_totalspace(ROOT_DIR_NAME);
USART_PutStr(COMMUNICATE_USART, "---------------- here is log system status ----------------\n\r");
if(1 == pSystemUnitModule->LogFileSystemCtrl)
USART_PutStr(COMMUNICATE_USART, "log system status : open\n\r");
else
USART_PutStr(COMMUNICATE_USART, "log system status : close\n\r");
USART_PutStr(COMMUNICATE_USART, "yaffs space use status : ");
USART_PutChar(COMMUNICATE_USART, '0' + num/10%10 );
USART_PutChar(COMMUNICATE_USART, '0' + num%10 );
USART_PutChar(COMMUNICATE_USART, '%' );
USART_PutStr(COMMUNICATE_USART, "\n\r");
}
//在根目录中查找参数中指定的文件夹或文件,并列出以它开始的10个目录项
void FindCmdExeFun(char *DirentName)
{
struct yaffs_dirent *pDirent;
int n;
char NameBuff[64];
//切换到根目录
yaffs_closedir(pCurDirectory);
pCurDirectory = yaffs_opendir(ROOT_DIR_NAME);
yaffs_rewinddir(pCurDirectory);
//设置当前目录名字
strcpy(CirDirName, ROOT_DIR_NAME);
//打印当前目录名字
ShowCurDirectory();
//名字连接,例如:将根目录"/nand/"和文件夹"2010-01-26"连接为"/nand/2010-01-26"
strcpy(NameBuff, ROOT_DIR_NAME);
strncpy(NameBuff + strlen(ROOT_DIR_NAME), DirentName, sizeof(NameBuff)-strlen(ROOT_DIR_NAME));
do{
pDirent = yaffs_readdir(pCurDirectory);
if(0 == strcmp(pDirent->d_name, DirentName))
break;
}while(NULL != pDirent);
if(NULL == pDirent){
USART_PutStr(COMMUNICATE_USART, "not find\n\r");
return;
}
//打印找到的目录项名字
strncpy(ListFileName[0], pDirent->d_name, FILE_NAME_MAXLEN);
ListFileName[0][FILE_NAME_MAXLEN-1] = 0;
USART_PutChar(COMMUNICATE_USART, '0');
if(YAFFS_OBJECT_TYPE_DIRECTORY == pDirent->d_type)
USART_PutStr(COMMUNICATE_USART, " - dir - ");
else
USART_PutStr(COMMUNICATE_USART, " - file - ");
USART_PutStr(COMMUNICATE_USART, ListFileName[0]);
USART_PutStr(COMMUNICATE_USART, "\n\r");
//打印紧跟找到目录后面的9个目录项名字
for(n=1; n<DIRENT_MAXNUM; n++){
pDirent = yaffs_readdir(pCurDirectory);
if(NULL == pDirent){
ListFileName[n][0] = 0;
}else{
strncpy(ListFileName[n], pDirent->d_name, FILE_NAME_MAXLEN);
ListFileName[n][FILE_NAME_MAXLEN-1] = 0;
USART_PutChar(COMMUNICATE_USART, '0' + n);
if(YAFFS_OBJECT_TYPE_DIRECTORY == pDirent->d_type)
USART_PutStr(COMMUNICATE_USART, " - dir - ");
else
USART_PutStr(COMMUNICATE_USART, " - file - ");
USART_PutStr(COMMUNICATE_USART, ListFileName[n]);
USART_PutStr(COMMUNICATE_USART, "\n\r");
}
}
if(NULL == pDirent){
USART_PutStr(COMMUNICATE_USART, "Note: reach the bottom of directory, no more file\n\r");
}
}
//打开目录
void OpenCmdExeFun(char *para)
{
struct yaffs_dirent *pDirent;
yaffs_DIR *pTempDirectory;
int n;
char NameBuff[64];
//目录连接,例如:将"/nand/"和"2011-01-25"连接为"/nand/2011-01-25/"
strcpy(NameBuff, CirDirName);
strncpy(NameBuff + strlen(NameBuff), ListFileName[*para - '0'], sizeof(NameBuff)-strlen(NameBuff));
strcpy(NameBuff + strlen(NameBuff), "/");
pTempDirectory = yaffs_opendir(NameBuff);
if(NULL == pTempDirectory){
USART_PutStr(COMMUNICATE_USART, "faild to open directory\n\r");
return;
}else{
yaffs_closedir(pCurDirectory);
pCurDirectory = pTempDirectory;
}
//设置当前目录名字
strncpy(CirDirName, NameBuff, sizeof(CirDirName));
yaffs_rewinddir(pCurDirectory);
NextCmdExeFun(NULL);
}
//上传文件
void GetCmdExeFun(char *para)
{
char NameBuff[64];
//名字连接,例如:将"/nand/2011-01-25/"和"lw-c360.txt"连接为"/nand/2011-01-25/lw-c360.txt"
strcpy(NameBuff, CirDirName);
strncpy(NameBuff + strlen(NameBuff), ListFileName[*para - '0'], sizeof(NameBuff)-strlen(NameBuff));
//打印要上传的文件名
USART_PutStr(COMMUNICATE_USART, "Please open zmodem to receive the file : ");
USART_PutStr(COMMUNICATE_USART, ListFileName[*para - '0']);
USART_PutStr(COMMUNICATE_USART, "\n\r");
if(0 == ZmodemSendOneFile(NameBuff)){//ZMODEMOK
USART_PutStr(COMMUNICATE_USART, "send file ok\n\r");
}else{
USART_PutStr(COMMUNICATE_USART, "send file error\n\r");
}
}
//返回根目录
void BackCmdExeFun(char *para)
{
yaffs_closedir(pCurDirectory);
pCurDirectory = yaffs_opendir(ROOT_DIR_NAME);
yaffs_rewinddir(pCurDirectory);
//设置当前目录名字
strncpy(CirDirName, ROOT_DIR_NAME, sizeof(CirDirName));
NextCmdExeFun(NULL);
}
//下10个目录项
void NextCmdExeFun(char *para)
{
struct yaffs_dirent *pDirent;
int n;
//打印当前目录名字
ShowCurDirectory();
for(n=0; n<DIRENT_MAXNUM; n++){
pDirent = yaffs_readdir(pCurDirectory);
if(NULL == pDirent){
ListFileName[n][0] = 0;
}else{
strncpy(ListFileName[n], pDirent->d_name, FILE_NAME_MAXLEN);
ListFileName[n][FILE_NAME_MAXLEN-1] = 0;
USART_PutChar(COMMUNICATE_USART, '0' + n);
if(YAFFS_OBJECT_TYPE_DIRECTORY == pDirent->d_type)
USART_PutStr(COMMUNICATE_USART, " - dir - ");
else
USART_PutStr(COMMUNICATE_USART, " - file - ");
USART_PutStr(COMMUNICATE_USART, ListFileName[n]);
USART_PutStr(COMMUNICATE_USART, "\n\r");
}
}
if(NULL == pDirent){
USART_PutStr(COMMUNICATE_USART, "Note: reach the bottom of directory, no more file\n\r");
}
}
//返回目录开始位置
void TopCmdExeFun(char *para)
{
yaffs_rewinddir(pCurDirectory);
NextCmdExeFun(NULL);
}
//从PC机下载一个文件
void DownloadCmdExeFun(char *para)
{
ZmodemReceiveFiles();
}
/*
* shell命令界面,进入shell界面之前,需要设置COMMUNICATE_USART
* 工作串口波特率为115200,并关闭COMMUNICATE_USART工作串口中断。
*/
void shell(void)
{
int n, CmdIndex;
unsigned char CmdBuffer[32];
//执行帮助命令
HelpCmdExeFun(NULL);
//打开根目录
pCurDirectory = yaffs_opendir(ROOT_DIR_NAME);
yaffs_rewinddir(pCurDirectory);
strncpy(CirDirName, ROOT_DIR_NAME, sizeof(CirDirName));
while(1){
readline_into_buffer("lw360>", CmdBuffer);
if(0 == CmdBuffer[0]){
continue;
}
//检查命令
if( ('f' == CmdBuffer[0] || 'o' == CmdBuffer[0] || 'g' == CmdBuffer[0]) ){
//f、o、g命令和参数之间用1个空格隔开
if(' ' != CmdBuffer[1]){
USART_PutStr(COMMUNICATE_USART, "cmd error, you can input h for help\n\r");
continue;
}
}else{
//其他命令不带参数,第二个字符必须是结束符'/0'
if(0 != CmdBuffer[1]){
USART_PutStr(COMMUNICATE_USART, "cmd error, you can input h for help\n\r");
continue;
}
}
//查找命令
CmdIndex = -1;
for(n=0; n<sizeof(CmdTbl)/sizeof(struct CMD_STRUCT); n++){
if(CmdTbl[n].name == CmdBuffer[0]){
CmdIndex = n;
break;
}
}
if(-1 == CmdIndex){
//查找命令失败
USART_PutStr(COMMUNICATE_USART, "cmd error, you can input h for help\n\r");
continue;
}
//如果是exit命令,则退出shell界面,机器进入正常工作模式
if('e' == CmdTbl[CmdIndex].name){
yaffs_closedir(pCurDirectory);
USART_PutStr(COMMUNICATE_USART, "Exit shell, return to work mode, good bye!^_^\n\r");
delay_ms(1000);
return;
}
//检查enter和get命令参数
if( ('o' == CmdTbl[CmdIndex].name || 'g' == CmdTbl[CmdIndex].name)\
&& (CmdBuffer[2]<'0' || '9'<CmdBuffer[2]) ){
USART_PutStr(COMMUNICATE_USART, "cmd para error\n\r");
continue;
}
//执行命令
(CmdTbl[CmdIndex].CmdFun)(&CmdBuffer[2]);
USART_PutStr(COMMUNICATE_USART, "-\n\r");
}//end while
} |