/* demo1 for X-Server Light. just support echo server for TCP port 10000/10001/10002. test with XS_09300.LIB */ #include #include #include "module.h" void do_echo( int skt , int mode); #define BUFSIZE 1460 /* read/write buffer size */ /* typedef struct { unsigned port; int socket; int state; int connect; void (*CallBackFun)(int skt,int mode); } TCP_SERVER; */ TCP_SERVER TcpServer={ 10001, /* port */ -1, /* socket */ 0, /* state */ 0, /* connect */ do_echo/* CallBackFun */ }; /* for xs_09001.lib ~ xs_09003.lib need the next line. int TcpServerNumber=sizeof(TcpServer)/sizeof(TCP_SERVER); */ unsigned char MyIp[4]; long LedTime; void ShowIp(void) { static int idx=0; int data; data=MyIp[idx]; idx++; #ifndef _NO_LED5_ Show5DigitLedWithDot(1,idx); Show5DigitLed(5,data%10); data/=10; if(data){ Show5DigitLed(4,data%10); data/=10; if(data) Show5DigitLed(3,data); else Show5DigitLed(3,16); } else { Show5DigitLed(4,16); Show5DigitLed(3,16); } Show5DigitLed(2,16); #endif if(idx>=4) idx=0; } /* demo for use command 19 or 23 */ int UserCmd19_23(PTCPREADDATA p) { int skt; unsigned char *buf; int length,i; skt=p->Socket; buf=p->ReadUartChar; length=p->Length; /* Just reply the inpput command here. */ XS_WriteSocket(skt,buf,length); return 1; } char *GetModuleName(void) /* User's program must support this function for UDP search */ { return "i-7188E-XS" ; } char *GetAliasName(void) /* User's program must support this function for UDP search */ { return "test1" ; } void XS_UserInit(int argc,char *argv[]) { extern int bAcceptBroadcast; extern unsigned long ACKDELAY; /* vaariable in tcp.c */ extern long MAXTXTOUT; void UserLoop(void); char version[20]; char MsgTest[]="command"; int MsgTest_len=sizeof(MsgTest); InitLib(); Print("sizeof(MsgTest)=%d\r\n",MsgTest_len); bAcceptBroadcast=0; ACKDELAY=200; XS_GetVersion(version); Print("[X-Server library]: version=%s ",version); XS_GetLibDate(version); Print("date=%s\r\n",version); GetTcpipLibDate(version); Print("Tcpip library version:%X, Library Date is %s\r\n",GetTcpipLibVer(),version); GetIp(MyIp); Print("IP=%d.%d.%d.%d\r\n",MyIp[0],MyIp[1],MyIp[2],MyIp[3]); LedTime=GetTimeTicks(); /* for xs_09001.lib ~ xs_09003.lib NEED NOT the next 4 lines. */ XS_AddSystemLoopFun(UserLoop); /* need add this line for the library 0.9.100 date after 2004/06/02 */ XS_AddServer(&TcpServer); XS_AddServer(&Port10K); /* 使用內建的 port 10000 功能*/ // bNeedPassword=1; // enable password function. // pXS_CheckPassword=XS_CheckPassword; // 使用 LIB 內建的 password 檢查副程式 // pXS_Port10kCmd[0]=XS_SetPassword_00; // 使用 LIB 內建的 password 設定副程式 // PasswordMsg="Need Password:"; // 設定詢問 password 時顯示的訊息 pXS_Port10kCmd[33]=XS_LoadFile_33; // 設定使用 command 33 (LOAD 檔案功能) pXS_Port10kCmd[19]=UserCmd19_23; // 設定給 command 19 使用。(跟原 x-server 的 command 19 有些不同。) pXS_Port10kCmd[23]=UserCmd19_23; // 設定給 command 23 使用。 pXS_Port10kCmd[17]=XS_Inport_17; // 設定使用 XS 提供的 command 17 pXS_Port10kCmd[18]=XS_Outport_18;// 設定使用 XS 提供的 command 18 /* Add UDP search function */ XS_AddUdpSocket(&XS_UdpSearch); bAcceptBroadcast=1; // 記得要設定成可以接收廣播封包 // EnableWDT(); MAXTXTOUT=6000UL; // for MMC if (!pc_ertfs_init()){ Print("pc_ertfs_init failed"); } else { extern unsigned long MAXSIZE; Print("[MaxSize=%lu]",MAXSIZE); } /* For Ver. 0.9.3.00 must add the following 2 lines. */ XS_AddSystemLoopFun(XS_SocketLoopFun); XS_StartSocket(); /* function for */ } void XS_UserEnd(void) { XS_StopSocket();/* For Ver. 0.9.3.00 must add this line. */ DisableWDT(); } unsigned status_8019as=0; int Socket0=-1; extern int errno; long st,old_dt; unsigned char TestData='0'; void UserLoop(void) { long tmpt; int key,err; RefreshWDT(); if(Kbhit()){ switch((key=Getch())){ case 27: QuitMain=1; break; default: if(Socket0>=0){ err= writesocket(Socket0,&key,1); if (err < 0) { /* write error */ Print( "echo write error %d,errno=%d\n\r", err,errno ); XS_CloseSocket(Socket0); Socket0=-1; } else { Print("\r\n",err); } } break; } } if(Socket0>=0){ err=inp(0x304); if(err!=status_8019as){ status_8019as=err; Print("[TSR=%02X]\r\n",status_8019as); st=GetTimeTicks(); old_dt=0; } else { long dt=GetTimeTicks()-st; if(dt-old_dt>1000){ old_dt=dt; Print("\r[%lu ms]",old_dt); #if 0 if(status_8019as==0x53){ err= writesocket(Socket0,&TestData,1); TestData++; if(TestData=='9'+1) TestData='0'; if (err < 0) { /* write error */ Print( "echo write error %d,errno=%d\n\r", err,errno ); XS_CloseSocket(Socket0); Socket0=-1; } } #endif } } } #if 1 tmpt=GetTimeTicks(); if(tmpt-LedTime>=500){ LedTime+=500; LedToggle(); ShowIp(); } #endif } /************************************************************************* * do_echo * * when client is connected, do echo here *************************************************************************/ char buf[BUFSIZE],SendBack[BUFSIZE+2]; PCFD out_fd=-1; int LinkCnt=0; char fname[13]; void do_echo( int skt ,int mode) { int i, cc, err, Sendlen; if(!mode){ LinkCnt++; sprintf(fname,"test%04u.txt",LinkCnt); if ((out_fd = pc_open(fname,(word) (PO_BINARY|PO_WRONLY|PO_CREAT|PO_APPEND),(word) (PS_IWRITE | PS_IREAD) ) ) < 0){ Sendlen=sprintf(SendBack,"open file %s for write error\r\n",fname); } else { Sendlen=sprintf(SendBack,"open file %s for write success,fd=%d\r\n",fname,out_fd); } goto SendMessage; } else { err = cc = readsocket( skt, buf, sizeof(buf)-1); if (err <= 0) { /* error or disconnected by remote side */ XS_CloseSocket(skt); if(out_fd >= 0){ pc_close(out_fd); out_fd=-1; Print("Close file %s\r\n",fname); } } else { if(out_fd >= 0){ buf[cc]='\n'; err=pc_write(out_fd,buf,cc+1); if (err != cc+1){ Print("write error(len=%d,err=%d)\r\n",cc,err); } else { Print("Write %d byte OK.\r\n",err); } } for( i=0; i < cc; i++ ) { SendBack[i]=buf[i]; } Sendlen=cc; SendMessage: err= writesocket(skt,SendBack,Sendlen); if (err < 0) { /* write error */ Print( "echo write error %d\n\r", err ); XS_CloseSocket(skt); if(out_fd >= 0){ pc_close(out_fd); out_fd=-1; Print("Close file %s\r\n",fname); } } } } } /* XS 0.9.200 or later must add the function main() */ void main(int argc,char *argv[]) { XS_main(argc,argv); /* call the XS library main function. */ }