📄 ftp_callback.c
字号:
#include <stdio.h>
#include "hpserver.h"
#include "ether.h"
#include "netutil.h"
#include "net.h"
#include "ip.h"
#include "tcp.h"
#include "ftp.h"
/************************************************************************
/* Function Name : ftp_on_error *
/* *
/* Arguments : *
/* st_ftp *ftp:Point to st_ftp structure. *
/* Return : *
/* Return false to close FTP client. *
/* *
/* Comment : *
/* This function is a sample FTP on error callback function for *
/* FTP client. *
/************************************************************************/
char ftp_on_error(st_ftp *ftp)
{
printf("\nftp error last_command=%s,return code=%s",ftp->last_command,ftp->last_return_code);
return 0;
}
/************************************************************************
/* Function Name : ftp_on_data_send *
/* *
/* Arguments : *
/* st_ftp *ftp:Point to st_ftp structure. *
/* Return : *
/* Return false to close FTP client. *
/* *
/* Comment : *
/* This function is a sample FTP on data send callback function for*
/* FTP client. *
/* *
/************************************************************************/
char ftp_on_data_send(TSOCK *ts,char start_of_send_flag)
{
/*The header file of file to be send by FTP*/
#include "cs00.h"
int tmp;
static unsigned int upload_count;
/*This is the first time that FTP system call this function.*/
if (start_of_send_flag)
upload_count=0;
/*Calculate the max number of bytes to be put in TX buffer.*/
tmp=buff_freelen(&ts->txb);
if (tmp>(CS00_SIZE-upload_count))
tmp=(CS00_SIZE-upload_count);
/*Put data to transmit buffer.*/
if (tmp)
{
buff_in(&ts->txb,&cs00_jpg[upload_count],tmp);
upload_count+=tmp;
return 1;
}
/*All data have been transmitting.*/
else if (upload_count>=CS00_SIZE)
return 0;
/*Wait for more TX buffer.*/
else return 1;
}
/************************************************************************
/* Function Name : ftp_on_data_receive *
/* *
/* Arguments : *
/* st_ftp *ftp:Point to st_ftp structure. *
/* Return : *
/* None. *
/* *
/* Comment : *
/* This function is a sample FTP on receive send callback function *
/* for FTP client. *
/* *
/************************************************************************/
char ftp_on_data_receive(TSOCK *ts,char end_of_data_flag)
{
int tmp=1;
char char_tmp;
/*Get data from RX buffer and display it.*/
while (tmp)
{
tmp=buff_out(&ts->rxb,&char_tmp,1);
printf("%c",char_tmp);
}
/*Receiving data complete.*/
if (end_of_data_flag)
return 0;
/*Wait for more data.*/
else
return 1;
}
/************************************************************************
/* Function Name : ftp_demo *
/* *
/* Arguments : *
/* st_ftp *ftp:Point to st_ftp structure. *
/* Return : *
/* None. *
/* *
/* Comment : *
/* This function start a FTP client and add some command to FTP *
/* command queue.This is a sample for FTP client. *
/* *
/************************************************************************/
void ftp_demo(st_ftp *ftp)
{
/*Open and start a FTP client.*/
if (ftp_open(ftp,1)==FTP_SUCCESS)
{
printf("\nTry to connect to FTP server....");
/*Add NLST command to get file list.*/
en_queue_command(ftp,"NLST",FTP_GET_FILE,ftp_on_data_receive);
/*Add TYPE I command to set data transmitting as binary.*/
en_queue_command(ftp,"TYPE I",FTP_NO_DATA,0);
/*Add STOR CS00.jpg command to upload CS00.jpg to FTP server.*/
en_queue_command(ftp,"STOR CS00.jpg",FTP_SEND_FILE,ftp_on_data_send);
/*Add quit command to close FTP server.*/
en_queue_command(ftp,"quit",FTP_QUIT,0);
}
else
printf("\nFail to Open FTP client");
}
/************************************************************************
/* Function Name : ftp_task *
/* *
/* Arguments : *
/* st_ftp *ftp:Point to st_ftp structure. *
/* Return : *
/* None. *
/* *
/* Comment : *
/* This function check FTP status and display message. *
/* *
/************************************************************************/
void ftp_task(st_ftp *ftp)
{
TSOCK *socket;
socket=ftp->Command_socket;
/*FTP client is stop by quit command , that mean all command have finished.*/
if (ftp->status==FTP_STOP_BY_QUIT)
{
printf("\n FTP successful");
ftp->status=FTP_STOP;
}
/*
Some error occure and FTP client is stop.
You can see last st_ftp.last_command and st_ftp.ftp->last_return_code
to debug.
*/
else if (ftp->status==FTP_STOP_BY_ERROR)
{
printf("\nFTP TCP Socket Error");
printf("\nFTP last command is:%s",ftp->last_command);
printf("\nFTP server return code is:%s",ftp->last_return_code);
ftp->status=FTP_STOP;
}
else if ((socket->state==TCP_CLOSED)&&(ftp->status!=FTP_STOP))
{
printf("\nCan't connect to FTP Server");
ftp->status=FTP_STOP;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -