STM32上FTP的是实现源代码.docx
《STM32上FTP的是实现源代码.docx》由会员分享,可在线阅读,更多相关《STM32上FTP的是实现源代码.docx(87页珍藏版)》请在冰豆网上搜索。
STM32上FTP的是实现源代码
//FTPserverwithsupportforonlyoneuser-wewanttouseitforfileuploads
#include"config.h"
#define_CLOSE_ERROR//testoption
#ifdefinedUSE_FTP
/*===================================================================*/
/*localdefinitions*/
/*===================================================================*/
#ifdefSTRING_OPTIMISATION//{14}
#define_fnBufferDec(x,y,z)fnBufferDec(x,y,z)//newdecimalstringroutine
#else
#define_fnBufferDec(x,y,z)fnDebugDec(x,y,z)//originaldecimalstringroutine
#endif
#if!
definedFTP_DISPLAY_USER_FILES
#undefINTERNAL_USER_FILES
#endif
#defineOWN_TASKTASK_FTP
#defineFTP_STATE_CLOSED0//FTPcommandstates
#defineFTP_STATE_CONNECTED1
#defineFTP_STATE_LOGIN2
#defineFTP_STATE_ACTIVE_USER3
#defineFTP_STATE_PREPARE_CLOSE4
#defineMSG_DO_NOTHING0//Messagestates
#defineMSG_REPEAT_DATA1
#defineMSG_REPEAT_CONTROL2
#defineMSG_SERVER_READY10//Commandmessages
#defineMSG_DIR_OK11
#defineMSG_ENTER_PASS12
#defineMSG_LOG_SUCCESS13
#defineMSG_LOG_FAILED14
#defineMSG_FTP_DATA15
#defineMSG_FTP_TYPE16
#defineMSG_FTP_DENIED17
#defineMSG_FTP_DIR18
#defineMSG_FTP_OK19
#defineMSG_DIR_CHANGED20
#defineMSG_FTP_UNKNOWN21
#defineMSG_DEL_OK22
#defineMSG_FILE_LENGTH23
#defineMSG_PASV_OK24
#defineMSG_BAD_DATA_CONNECTION25
#defineMSG_NOT_SUPPORTED26
#defineMSG_FTP_QUITTING27
#defineMSG_FTP_READY_FOR_RENAME28
#defineMSG_FTP_RENAME_SUCCESSFUL29
#defineMSG_DIR40//Datamessages
#defineMSG_UPLOAD41
#defineFIRST_DATA_MESSAGEMSG_DIR
#ifdefFTP_VERIFY_DATA_PORT//{4}ifcheckingisenablemodifyreturntypeoffnGetDataPort()
#defineRTN_DATA_PORTint
#else
#defineRTN_DATA_PORTvoid
#endif
#ifdefFTP_SUPPORTS_NAME_DISPLAY
staticconstCHARcFileType[]={'','1','','5','0','2','','5','0','2',''};
staticconstCHARcFileDate[]={'','M','a','y','','1','','2','0','0','9',''};//{13}
staticconstCHARcFileRights[]={'-','r','w','x','r','w','x','r','w','x'};
#defineLENGTH_OF_FILE_TYPEsizeof(cFileType)
#defineLENGTH_OF_FILE_LENGTH6//999kismaxpossiblefilesize
#defineLENGTH_OF_FILE_DATEsizeof(cFileDate)
#ifdefINTERNAL_USER_FILES//{13}
#defineLENGTH_OF_FILE_NAMEMAX_FILE_NAME_LENGTH
#else
#defineLENGTH_OF_FILE_NAME7//{13}reducedby1
#endif
#defineLENGTH_OF_FILE_INFO(sizeof(cFileRights)+LENGTH_OF_FILE_TYPE+LENGTH_OF_FILE_LENGTH+LENGTH_OF_FILE_DATE+LENGTH_OF_FILE_NAME)
#ifndefFILE_NAMES_PER_FTP_FRAME
#defineFILE_NAMES_PER_FTP_FRAME((LAN_BUFFER_SIZE-ETH_HEADER_LEN-IP_MIN_HLEN-MIN_TCP_HLEN-22)/LENGTH_OF_FILE_INFO)
#endif
#defineFTP_DATA_BUFFER_LENGTH(FILE_NAMES_PER_FTP_FRAME*LENGTH_OF_FILE_INFO)//spacefor6filestobedisplayedin1message
#else
#defineFTP_DATA_BUFFER_LENGTH100
#defineFILE_NAMES_PER_FTP_FRAMEFTP_DATA_BUFFER_LENGTH//{10}
#endif
#defineFTP_MODE_ASCII0
#defineFTP_MODE_BINARY1
#defineDATA_INACTIVE0
#defineDO_LIST1
#defineDO_SAVE2
#defineDOING_SAVE3
#defineSAVE_COMPLETE4
#defineWAIT_COMPLETE5
#defineDO_UPLOAD6
#defineDOING_UPLOAD7
#ifdefSUB_FILE_SIZE
#defineSUBFILE_WRITE,ucSubFileInProgress
#defineSUB_FILE_ON,SUB_FILE_TYPE
#else
#defineSUBFILE_WRITE
#defineSUB_FILE_ON
#endif
/*===================================================================*/
/*localstructuredefinitions*/
/*===================================================================*/
typedefstructstTCP_FTP_MESSAGE//definitionofadataframestructure
{
TCP_HEADERtTCP_Header;//reserveheaderspace
unsignedcharucTCP_Message[FTP_DATA_BUFFER_LENGTH];//spaceforcontent
}TCP_FTP_MESSAGE;
/*===================================================================*/
/*localfunctionprototypedeclarations*/
/*===================================================================*/
staticintfnFTPListener(USOCKETcSocket,unsignedcharucEvent,unsignedchar*ucIp_Data,unsignedshortusPortLen);
staticintfnFTP_Data_Listener(USOCKETcSocket,unsignedcharucEvent,unsignedchar*ucIp_Data,unsignedshortusPortLen);
staticRTN_DATA_PORTfnGetDataPort(CHAR*ucIP_Port);
staticsignedshortfnSendFTP(unsignedcharucMsg);
/*===================================================================*/
/*constants*/
/*===================================================================*/
#ifdefSUPPORT_MIME_IDENTIFIER
staticunsignedcharucMimeType;
#endif
#ifdefFTP_UTFAT//{17}
staticUTDIRECTORY*ptr_utDirectory=0;//directoryobjectforasingleuser
staticUTLISTDIRECTORYutListDirectory;//listdirectoryobjectforasingleuser
staticUTLISTDIRECTORYutListLastDirectory;//backupincaseofneedtorepeat
staticFILE_LISTINGfileList={0};
staticUTFILEutFile={0};
#endif
staticconstCHARcFTPServerReady[]=FTP_WELCOME_RESPONSE;
staticconstCHARcFTPBadPass[]="530LogBAD.\r\n";
staticconstCHARcFTPEnterPass[]="331Enterpass.\r\n";
staticconstCHARcFTPloginSuccessful[]="230LogOK.\r\n";
staticconstCHARcFTPUnknownCommand[]="500What?
.\r\n";
staticconstCHARcFTPType[]="215UNIXType:
L8\r\n";//pretendtobeUNIX...
staticconstCHARcFTPDenied[]="550Denied.\r\n";
staticconstCHARcFTPDir[]="257\x22/\x22\r\n";//pathname
staticconstCHARcFTPOK[]="200OK.\r\n";
staticconstCHARcFTPDirChanged[]="250Dirchanged.\r\n";
staticconstCHARcFTP_Data[]="150Data.\r\n";
staticconstCHARcFTP_DIR_OK[]="226OK.\r\n";
staticconstCHARcFTPTerminating[]="221Bye\r\n";
#ifndefFTP_PASV_SUPPORT
staticconstCHARcFTP_Not_Supported[]="502Nodo.\r\n";
#endif
#ifdefFTP_VERIFY_DATA_PORT
staticconstCHARcFTP_Bad_Port[]="425Baddataport\r\n";//{4}
#endif
#ifdefFTP_SUPPORTS_DELETE//{9}
staticconstCHARcFTPDelOK[]="250DelOK.\r\n";
#endif
#ifdefFTP_UTFAT//{17}
staticconstCHARcFTP_Rename_OK[]="350Rdy\r\n";
staticconstCHARcFTP_Rename_Success[]="250OK\r\n";
#endif
staticconstCHARcFTP_DIR[]="-rEmpty\r\n";//wedisplaythiswhenadirectoryisempty{7}
staticunsignedshortusFTP_DataPort;//dataportnumber
staticunsignedcharucFTP_state=FTP_STATE_CLOSED;
staticUSOCKETFTP_TCP_socket=-1;
staticUSOCKETFTP_TCP_Data_socket=-1;
staticunsignedcharucFTP_action;
staticunsignedcharipFTP_data[IPV4_LENGTH];//dataportIPaddress
staticunsignedcharucLastControl=MSG_DO_NOTHING;
staticunsignedcharucControlQueue=MSG_DO_NOTHING;
staticMAX_FILE_SYSTEM_OFFSETFileOffset;
#ifdefFTP_SUPPORTS_DOWNLOAD
staticunsignedshortusLastSentSize;
#endif
staticunsignedcharucFilesSent;
staticMEMORY_RANGE_POINTERptrFile;
#ifdefSUB_FILE_SIZE
staticunsignedcharucSubFileInProgress=0;
#endif
#ifdefFTP_USER_LOGIN
staticsignedcharcPasswordAccess;//{3}
staticunsignedcharucFTP_mode;
#endif
#ifdefFTP_PASV_SUPPORT
#defineMODE_ACTIVE_FTP0x00
#defineMODE_PASV0x01
#defineDATA_CONNECTED0x02
staticunsignedcharucMode=MODE_ACTIVE_FTP;
#endif
#ifdefFTP_DATA_WINDOWS//{12}
externTCP_CONTROL*present_tcp;//globalpointertopresenttcpsocket'scontrolstructure
#endif
#ifdef_WINDOWS
externintuser_files_not_in_code;//{15}
#endif
#ifdefINTERNAL_USER_FILES//{13}
externUSER_FILE*ptrUserFiles;//globalpointertouserfileset
#endif
externvoidfnStartFtp(unsignedshortusFTPTimeout,unsignedcharucFTP_operating_mode)//{5}
{
if(FTP_TCP_socket!
=-1){
return;
}
#ifdefFTP_UTFAT//{17}
if(ptr_utDirectory==0){
ptr_utDirectory=utAllocateDirectory(DISK_D,0);//allocateadirectoryforusebythismoduleassociatedwithD:
-nopathstring
}
#endif
FTP_TCP_socket=fnGetTCP_Socket(TOS_MINIMISE_DELAY,usFTPTimeout,fnFTPListener);//getTCPSocketandlistenonFTPcontrolport
fnTCP_Listen(FTP_TCP_socket,FTP_CONTROL_PORT,0);
ucFTP_state=FTP_STATE_CLOSED;//ensurewestartinclosedstate
#ifdefFTP_USER_LOGIN
ucFTP_mode=ucFTP_operating_mode;//saveoperatingmode
#endif
FTP_TCP_Data_socket=fnGetTCP_Socket(TOS_MAXIMISE_THROUGHPUT,TCP_DEFAULT_TIMEOUT,fnFTP_Data_Listener);//reserveFTPdatasocket
}
externvoidfnStopFtp(void)
{
fnReleaseTCP_Socket(FTP_TCP_socket);//weclosethesocketssothatFTPiseffectivelydead.
fnReleaseTCP_Socket(FTP_TCP_Data_socket);
FTP_TCP_socket=-1;
}
staticunsignedcharfnGetQueue(void)
{
unsignedcharucMsg;
if(ucLastControl!
=MSG_DO_NOTHING){//ifFTPisstillwaitingforanACKdon'treleaseanythingfromqueue
returnMSG_DO_NOTHING;
}
ucMsg=ucControlQueue;
ucControlQueue=MSG_DO_NOTHING;
returnucMsg;
}
//Thedatasocketgeneratessomemessagesforthecontrolsocket.Sincethedatasockethasnoinformation
//aboutthestateofthecontrolsocketitusesasmallqueuetosendmessages(onlyoneplaceinqueue)
//
staticvoidfnQueueSendFTP(unsignedcharucControlMsg)
{
if(ucLastControl==MSG_DO_NOTHING){//ifthecontrolsocketisnotwaitingforanackitcansendthemessageimmediately
fnSendFTP(ucControlMsg);//immediatelysendcontrolmessage
ucControlQueue=MSG_DO_NOTHING;//nothingelseinthequeue
}
else{
ucControlQueue=ucControlMsg;//wequeuethemessagetobesentoncethecontrolsocketcan
}
}
#ifdefinedMANAGED_FILES&&definedMANAGED_FTP_DELETES//{20}
staticvoidfnDelete_terminated(intiHandle,intiResult)
{
fnSendFTP(MSG_DEL_OK);//deleteterminated
}
#endif
//LocallistenertoTCPFTPcommandport
//
staticintfnFTPListener(USOCKETSocket,unsignedcharucEvent,unsignedchar*ucIp_Data,unsignedshortusPortLen)
{
unsignedshortusNextData;
if(Socket!
=FTP_TCP_socket)returnAPP_REJECT;//ignoreifnotoursockethandle
switch(ucEvent){
caseTCP_EVENT_CONNECTE