📄 tftps.c
字号:
if(tftps_file.finish)
{
tftps_deletesocket();
return 1;
}
/* get the block number from package */
receivedack = RECEIVE_NETWORK_B();
receivedack <<= 8;
receivedack |= RECEIVE_NETWORK_B();
dlen -= 2;
if(receivedack != (tftps_file.actualblock - 1))
{
printf("There is an ACK problem, actualblock %i, received Block: %i\n", tftps_file.actualblock, receivedack);
//tftps_deletesocket();
//return 0;
}
/* send... */
if(!tftps_file.finish && tftps_file.actualblock != 1)
{
tftps_sendblock();
return 1;
}
tftps_deletesocket();
return(1);
}
case TFTPS_OPCODE_WRQ: /* Write request? */
/* Get filename */
printf("We are in WRQ\n");
fname[0] = '\0';
for(i=0; i<dlen; i++)
{
if(i >= TFTPS_FILENAME_MAXLEN)
{
tftps_senderror(TFTPS_NOTDEFINED);
tftps_deletesocket();
return(1);
}
ch = RECEIVE_NETWORK_B();
fname[i] = ch;
if(ch == '\0')
{
i++;
break;
}
}
dlen -= i;
/* Check if space is available, otherwise send error message */
if(ffs_getSector() == -1)
{
tftps_senderror(TFTPS_DISKFULL);
tftps_deletesocket();
return(1);
}
/* Ok we have a free bank, open a file into empty bank. Otherwise kill conn */
if(ffs_SetFile(strlen((const char *)fname), fname) == -1)
{
tftps_senderror(TFTPS_NOTDEFINED);
tftps_deletesocket();
return(1);
}
/* !!!!!!!!!!!!!!!!!!!!!!!! */
/* Check here the filename */
/* if NOT OK, RETURN */
/* !!!!!!!!!!!!!!!!!!!!!!!! */
/* Check mode, only octet mode is supported */
if(dlen < 6)
{
tftps_senderror(TFTPS_NOTDEFINED);
tftps_deletesocket();
return(1);
}
if( (RECEIVE_NETWORK_B() != 'o') || (RECEIVE_NETWORK_B() != 'c') || (RECEIVE_NETWORK_B() != 't') ||
(RECEIVE_NETWORK_B() != 'e') || (RECEIVE_NETWORK_B() != 't') || (RECEIVE_NETWORK_B() != '\0') )
{
tftps_senderror(TFTPS_NOTDEFINED);
tftps_deletesocket();
return(1);
}
/* All OK, send ACK */
tftps.state = TFTPS_STATE_CONNECTED;
tftps.blocknumber = 0;
tftps.bytecount = 0;
tftps.retries = TFTPS_DEF_RETRIES;
init_timer(tftps.tmrhandle, TFTPS_TIMEOUT*TIMERTIC);
tftps_sendack();
tftps.blocknumber++;
return(1);
case TFTPS_OPCODE_DATA: /* Data Packet ? */
if(tftps.state != TFTPS_STATE_CONNECTED)
{
tftps_senderror(TFTPS_NOTDEFINED);
tftps_deletesocket();
return(1);
}
if(dlen < 2)
{
tftps_senderror(TFTPS_NOTDEFINED);
tftps_deletesocket();
return(1);
}
/* Get block number */
u16temp = RECEIVE_NETWORK_B();
u16temp <<= 8;
u16temp |= RECEIVE_NETWORK_B();
dlen -= 2;
if( (u16temp < tftps.blocknumber) && (tftps.blocknumber > 0) )
{
/* Duplicate msg, send ACK again */
if( tftps.retries > 0 )
{
tftps.retries--;
tftps.blocknumber--;
tftps_sendack();
tftps.blocknumber++;
}
else
{
tftps_senderror(TFTPS_NOTDEFINED);
tftps_deletesocket();
}
return(1);
}
if( u16temp != tftps.blocknumber )
{
/* Something really wrong */
tftps_senderror(TFTPS_NOTDEFINED);
tftps_deletesocket();
return(1);
}
/* !!!!!!!!!!!!!!!!!!!!!!!! */
/* Read the data here */
/* !!!!!!!!!!!!!!!!!!!!!!!! */
/* Receive Packet and write it to FLASH memory */
while(packetcount < dlen)
{
/* convert tftp data to 16Bit */
tftpdata = RECEIVE_NETWORK_B();
packetcount++;
tftpdata <<= 8;
tftpdata |= RECEIVE_NETWORK_B();
packetcount++;
/* reorganize number format (change endianess) */
tftpdata = switchword(tftpdata);
/* Pass word to the flash programming function */
ffs_Write(tftpdata);
}
/* count the filesize */
tftps_file.filesize += dlen;
if(tftps_file.filesize > MAXFILE)
{
tftps_senderror(TFTPS_DISKFULL);
tftps_deletesocket();
ffs_CloseFile(tftps_file.filesize);
return(1);
}
/* If last packet arrives ( < 512byte) save length to flash */
if( dlen < 512 )
{
/* Write filesize to flash */
ffs_CloseFile(tftps_file.filesize);
/* Other side Wants to close */
tftps_sendack();
tftps_deletesocket();
return(1);
}
/* All OK */
tftps.retries = TFTPS_DEF_RETRIES;
init_timer(tftps.tmrhandle, TFTPS_TIMEOUT*TIMERTIC);
tftps_sendack();
tftps.blocknumber++;
return(1);
case TFTPS_OPCODE_ERROR:
tftps_deletesocket();
return(1);
default:
/* Unsupported Opcode, Send error */
tftps_senderror(TFTPS_ILLEGALOPERATION);
tftps_deletesocket();
return(1);
}
}
void tftps_sendack (void)
{
/* Send a TFTP ACK packet */
net_buf[UDP_APP_OFFSET + 0] = 0; /* Opcode for ACK */
net_buf[UDP_APP_OFFSET + 1] = 4;
net_buf[UDP_APP_OFFSET + 2] = (UINT8)(tftps.blocknumber >> 8);
net_buf[UDP_APP_OFFSET + 3] = (UINT8)tftps.blocknumber;
udp_send(tftps.sochandle, tftps.remip, tftps.remport, &net_buf[UDP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - UDP_APP_OFFSET, 4);
}
void tftps_sendblock(void)
{
UINT16 blockcounter;
int data;
/* Fill UDP Buffer with tftp informations */
net_buf[UDP_APP_OFFSET + 0] = 0;
net_buf[UDP_APP_OFFSET + 1] = 3; /* Opcode for DATA */
net_buf[UDP_APP_OFFSET + 2] = (UINT8) (tftps_file.actualblock >> 8);
net_buf[UDP_APP_OFFSET + 3] = (UINT8) (tftps_file.actualblock);
for(blockcounter = 0; blockcounter < (tftps_file.flen - tftps_file.fpoint); blockcounter++)
{
if(blockcounter >= 512)
{
tftps_file.fpoint += blockcounter;
tftps_file.actualblock++;
break;
}
data = ffs_ReadFile();
if(data == -1)
{
tftps_senderror (0);
tftps_deletesocket();
return;
}
net_buf[UDP_APP_OFFSET + 4 + blockcounter] = data;
}
if(blockcounter < 512)
{
tftps_file.fpoint += blockcounter;
tftps_file.actualblock++;
tftps_file.finish = 1;
}
udp_send(tftps.sochandle, tftps.remip, tftps.remport, &net_buf[UDP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - UDP_APP_OFFSET, blockcounter + 4);
}
void tftps_senderror (UINT8 errno )
{
/* Send TFTP Error -packet */
net_buf[UDP_APP_OFFSET + 0] = 0; /* Opcode */
net_buf[UDP_APP_OFFSET + 1] = 5;
net_buf[UDP_APP_OFFSET + 2] = 0;
net_buf[UDP_APP_OFFSET + 3] = errno;
net_buf[UDP_APP_OFFSET + 4] = (UINT8)(tftps.blocknumber >> 8);
net_buf[UDP_APP_OFFSET + 5] = (UINT8)tftps.blocknumber;
net_buf[UDP_APP_OFFSET + 6] = '\0';
net_buf[UDP_APP_OFFSET + 7] = 0;
udp_send(tftps.sochandle, tftps.remip, tftps.remport, &net_buf[UDP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - UDP_APP_OFFSET, 7);
}
void tftps_deletesocket (void)
{
/* Clear Socket Data */
tftps.blocknumber = 0;
tftps.state = TFTPS_STATE_ENABLED;
tftps.retries = 0;
tftps.remip = 0;
tftps.remport = 0;
/* Clear file transfer data */
tftps_file.flen = 0;
tftps_file.fpoint = 0;
tftps_file.actualblock = 1;
tftps_file.finish = 0;
tftps_file.tftpfile = NULL;
tftps_file.haverrq = 0;
tftps_file.flashseek = 0;
tftps_file.receivedfsize = 0;
tftps_file.filesize = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -