/* smtp_cli.c: 1. support echo server for TCP port 10000. 2. support SMTP client(send E-mail) Compiler: BC++ 3.1, Turbo C++ 1.01(3.01) Compile mode: Large Project: smtp_cli.c ..\lib\7188el.Lib ..\lib\tcpip32.lib ..\lib\FW_nnnnn.LIB Hordware: I-7188EX There are some data values should be set. SMTP_MyEmailAddr : source email address SMTP_DestEmailAddr[0] : destination email address 1 SMTP_DestEmailAddr[1] : destination email address 2 SMTP_DestEmailAddrNo : number of destination email address **max destination address number is 5 SMTP_Subject : email subject SMTP_MailData : email contents [Dec 19, 2008] by Liam [Aug,02,2011] by Nicholas */ #include #include #include "..\lib\7188e.h" #include "..\lib\Tcpip32.h" #include "..\lib\MFW.h" void do_echo( int skt , int mode); #define BUFSIZE 1460 /* read/write buffer size */ TCP_SERVER TcpServer[]={ { 10000, // port -1, // socket 0, // state 0, // connect do_echo // CallBackFun } }; unsigned char MyIp[4]; void ShowIp(void) { static int idx=0; int data; #if 0 data=MyIp[idx]; #endif data=netconf[nets[0].confix].Iaddr.c[idx]; #if 0 if(!idx) Puts("\r\n"); Print("%d.",data); #endif idx++; 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); if(idx>=4) idx=0; } #define ESTABLISHED 1 /* state machine states, see handbook */ #define FINWAIT_1 2 #define FINWAIT_2 3 #define CLOSED_WAIT 4 #define TIMEWAIT 5 #define LAST_ACK 6 #define CLOSED 7 #define SYN_SENT 8 #define SYN_RECEIVED 9 #define LISTEN 16 void ShowSocketStatus(void) { int i; Puts("\r\n"); for(i=0; i",SmtpClient.bConnected); #endif XS_AddSystemLoopFun(UserLoop); XS_AddSystemLoopFun(XS_SocketLoopFun); XS_StartSocket(); XS_AddServer(&TcpServer[0]); XS_AddClient(&SmtpClient); DT2_AddTimer(500,2,LedToggle); } void XS_UserEnd(void) { XS_StopSocket(); DisableWDT(); } char Command[100]; int idx=0; void UserLoop(void) { long tmpt; int data; int err; int XS_SendMail(void); if(Kbhit()) { switch(data=Getch()) { case 27: QuitMain=1; break; default: Command[idx]=data; if(idx<99) idx++; Putch(data); if(data=='\r') { Command[idx]=0; Putch('\n'); if(Command[0]=='/') { if(Command[1]=='S') { SMTP_MyName="ICPDAS_7188E"; #if 0 SMTP_MyEmailAddr="service@icpdas.com"; SMTP_DestEmailAddr[0]="service@icpdas.com"; SMTP_DestEmailAddr[1]="service@icpdas.com"; #endif SMTP_MyEmailAddr="jency_fang@icpdas.com"; SMTP_DestEmailAddr[0]="jency_fang@icpdas.com"; SMTP_DestEmailAddr[1]="jency_fang@icpdas.com"; SMTP_DestEmailAddrNo=2; SMTP_Subject="Test Mail from I-7188EX"; SMTP_MailData="This is a test mail send from I-7188EX."; // If there is no e-mail content, should use: // SMTP_MailData=""; // does't use: // SMTP_MailData=0; // The email content will be "(null)" err=XS_SendMail(); } else if(Command[1]=='i') { DT_AddTimer(4, 1000, 1, ShowIp); } else if(Command[1]=='0' && Command[2]=='0') { ShowSocketStatus(); } else { Print("Unsupport command\r\n"); } } else { Print("[do nothing]\r\n"); } idx=0; } break; } } } char buf[BUFSIZE],SendBack[BUFSIZE+2]; void DoCommand(char *cmd,int cmdlen,int skt) { if(cmdlen>=3) { if(cmd[1]=='0' && cmd[2]=='0') { } } } // when client is connected, do echo here void do_echo( int skt, int mode) { int i, cc, err, Sendlen; if(!mode) { Sendlen=sprintf(SendBack, "Welcome connect to port %u\r\nThere are %d clients connect to this port\r\n", TcpServer[Socket_Idx[skt]].port, TcpServer[Socket_Idx[skt]].connect); goto SendMessage; } else { err = cc = readsocket(skt, buf, sizeof(buf)-1); if(err <= 0) // error or disconnected by remote side { XS_CloseSocket(skt); } else { if(buf[0]=='/') { DoCommand(buf, cc, skt); } else { 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); } } } } } void main(int argc,char *argv[]) { XS_main(argc,argv); // call the XS library main function. }