ThirdParty/sw_services/lwip211/examples/lwip_example_tftp_client.c
static void tftp_handle_write_req_resp(struct udp_pcb *pcb, struct pbuf *p,
		ip_addr_t *ip, u16_t port)
{
	FRESULT Res;
	static u8_t file_open = 0, last_packet_sent = 0;
	char fname[20];

	/* Open file on the first ack */
	if (!file_open) {
		sprintf(fname, "sample%d.txt", tftp_arg.file_count);

		/* Overwrite the file if already exists */
		Res = f_open(&tftp_arg.fil, fname, FA_READ);
		if (Res) {
			xil_printf("TFTP_WRQ: Unable to open file %s\r\n",
					fname);
			tftp_send_error_packet(pcb, ip, port,
					TFTP_ERR_FILE_NOT_FOUND);
			tftp_in_process = 0;
			return;
		}
		file_open = 1;
	}

	/* Read new data if TFTP_ACK packet for previous block is received */
	if (tftp_arg.block == tftp_get_block_value(p->payload)) {
		/* Do not read more if ACK is for last packet */
		if (last_packet_sent) {
			xil_printf("TFTP_WRQ: Transfer completed\r\n\r\n");

			/* Cleanup */
			f_close(&tftp_arg.fil);
			file_open = 0;
			last_packet_sent = 0;
			tftp_in_process = 0;
			return;
		}

		/* read data from file */
		Res = f_read(&tftp_arg.fil, tftp_arg.data,
				TFTP_DATA_PACKET_MSG_LEN, &tftp_arg.data_len);
		if (Res) {
			xil_printf("TFTP_WRQ: File read error\r\n");
			f_close(&tftp_arg.fil);
			file_open = 0;
			tftp_in_process = 0;
			return;
		}
		tftp_arg.block++;
	}

	/* send data packet */
	tftp_send_data_packet(pcb, ip, port);

	/* If the packet length is less than maximum data packet length then
	 * we have read whole file. Wait for the ACK packet before cleanup */
	if (tftp_arg.data_len < TFTP_DATA_PACKET_MSG_LEN)
		last_packet_sent = 1;
}
/* Send next packet on receiving TFTP_ACK from host machine for previous
 * TFTP_DATA packet */
Send next packet on receiving TFTP_ACK from host machine for previous
TFTP_DATA packet
Send next packet on receiving TFTP_ACK from host machine for previous TFTP_DATA packet
static void tftp_handle_write_req_resp(struct udp_pcb *pcb, struct pbuf *p,
		ip_addr_t *ip, u16_t port)
{
	FRESULT Res;
	static u8_t file_open = 0, last_packet_sent = 0;
	char fname[20];
	if (!file_open) {
		sprintf(fname, "sample%d.txt", tftp_arg.file_count);
		Res = f_open(&tftp_arg.fil, fname, FA_READ);
		if (Res) {
			xil_printf("TFTP_WRQ: Unable to open file %s\r\n",
					fname);
			tftp_send_error_packet(pcb, ip, port,
					TFTP_ERR_FILE_NOT_FOUND);
			tftp_in_process = 0;
			return;
		}
		file_open = 1;
	}
	if (tftp_arg.block == tftp_get_block_value(p->payload)) {
		if (last_packet_sent) {
			xil_printf("TFTP_WRQ: Transfer completed\r\n\r\n");
			f_close(&tftp_arg.fil);
			file_open = 0;
			last_packet_sent = 0;
			tftp_in_process = 0;
			return;
		}
		Res = f_read(&tftp_arg.fil, tftp_arg.data,
				TFTP_DATA_PACKET_MSG_LEN, &tftp_arg.data_len);
		if (Res) {
			xil_printf("TFTP_WRQ: File read error\r\n");
			f_close(&tftp_arg.fil);
			file_open = 0;
			tftp_in_process = 0;
			return;
		}
		tftp_arg.block++;
	}
	tftp_send_data_packet(pcb, ip, port);
	if (tftp_arg.data_len < TFTP_DATA_PACKET_MSG_LEN)
		last_packet_sent = 1;
}
static void tftp_handle_write_req_resp ( struct udp_pcb * pcb , struct pbuf * p , ip_addr_t * ip , u16_t port ) { FRESULT Res ; static u8_t file_open = 0 , last_packet_sent = 0 ; char fname [ 20 ] ; if ( ! file_open ) { sprintf ( fname , " " , tftp_arg . file_count ) ; Res = f_open ( & tftp_arg . fil , fname , FA_READ ) ; if ( Res ) { xil_printf ( " \r \n " , fname ) ; tftp_send_error_packet ( pcb , ip , port , TFTP_ERR_FILE_NOT_FOUND ) ; tftp_in_process = 0 ; return ; } file_open = 1 ; } if ( tftp_arg . block == tftp_get_block_value ( p -> payload ) ) { if ( last_packet_sent ) { xil_printf ( " \r \n \r \n " ) ; f_close ( & tftp_arg . fil ) ; file_open = 0 ; last_packet_sent = 0 ; tftp_in_process = 0 ; return ; } Res = f_read ( & tftp_arg . fil , tftp_arg . data , TFTP_DATA_PACKET_MSG_LEN , & tftp_arg . data_len ) ; if ( Res ) { xil_printf ( " \r \n " ) ; f_close ( & tftp_arg . fil ) ; file_open = 0 ; tftp_in_process = 0 ; return ; } tftp_arg . block ++ ; } tftp_send_data_packet ( pcb , ip , port ) ; if ( tftp_arg . data_len < TFTP_DATA_PACKET_MSG_LEN ) last_packet_sent = 1 ; }
Send next packet on receiving TFTP_ACK from host machine for previous
 TFTP_DATA packet
Send next packet on receiving TFTP_ACK from host machine for previous TFTP_DATA packet
/* Open file on the first ack */ /* Overwrite the file if already exists */ /* Read new data if TFTP_ACK packet for previous block is received */ /* Do not read more if ACK is for last packet */ /* Cleanup */ /* read data from file */ /* send data packet */ /* If the packet length is less than maximum data packet length then
	 * we have read whole file. Wait for the ACK packet before cleanup */