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

	/* Verify block value */
	if (tftp_get_block_value(p->payload) != tftp_arg.block) {
		tftp_send_ack_packet(pcb, ip, port, tftp_arg.block);
		return;
	}

	/* 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_CREATE_ALWAYS | FA_WRITE);
		if (Res) {
			xil_printf("TFTP_RRQ: Unable to open file %s\r\n",
					fname);
			tftp_send_error_packet(pcb, ip, port,
					TFTP_ERR_DISKFULL);
			tftp_in_process = 0;
			return;
		}
		file_open = 1;
	}

	/* write data to file */
	f_write(&tftp_arg.fil, buf + TFTP_PACKET_HDR_LEN,
			p->len - TFTP_PACKET_HDR_LEN, &n);
	if (n != p->len - TFTP_PACKET_HDR_LEN) {
		xil_printf("TFTP_RRQ: Write to file error\r\n");
		tftp_send_error_packet(pcb, ip, port, TFTP_ERR_DISKFULL);

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

	/* send_ack_packet */
	tftp_send_ack_packet(pcb, ip, port, tftp_arg.block);
	tftp_arg.block++;

	/* If the data packet length is less than maximum data packet length,
	 * that means we have received whole file. Do cleanup */
	if (p->len < TFTP_DATA_PACKET_MSG_LEN) {
		xil_printf("TFTP_RRQ: Transfer completed\r\n\r\n");

		/* Cleanup */
		f_close(&tftp_arg.fil);
		file_open = 0;
		tftp_in_process = 0;
	}
}
/* Send TFTP_ACK packet on receiving TFTP_DATA from host machine */
Send TFTP_ACK packet on receiving TFTP_DATA from host machine
Send TFTP_ACK packet on receiving TFTP_DATA from host machine
static void tftp_handle_read_req_resp(struct udp_pcb *pcb, struct pbuf *p,
		ip_addr_t *ip, u16_t port)
{
	FRESULT Res;
	static u8_t file_open = 0;
	static char fname[20];
	char *buf = (char *)p->payload;
	u32_t n;
	if (tftp_get_block_value(p->payload) != tftp_arg.block) {
		tftp_send_ack_packet(pcb, ip, port, tftp_arg.block);
		return;
	}
	if (!file_open) {
		sprintf(fname, "sample%d.txt", tftp_arg.file_count);
		Res = f_open(&tftp_arg.fil, fname, FA_CREATE_ALWAYS | FA_WRITE);
		if (Res) {
			xil_printf("TFTP_RRQ: Unable to open file %s\r\n",
					fname);
			tftp_send_error_packet(pcb, ip, port,
					TFTP_ERR_DISKFULL);
			tftp_in_process = 0;
			return;
		}
		file_open = 1;
	}
	f_write(&tftp_arg.fil, buf + TFTP_PACKET_HDR_LEN,
			p->len - TFTP_PACKET_HDR_LEN, &n);
	if (n != p->len - TFTP_PACKET_HDR_LEN) {
		xil_printf("TFTP_RRQ: Write to file error\r\n");
		tftp_send_error_packet(pcb, ip, port, TFTP_ERR_DISKFULL);
		f_close(&tftp_arg.fil);
		file_open = 0;
		tftp_in_process = 0;
		return;
	}
	tftp_send_ack_packet(pcb, ip, port, tftp_arg.block);
	tftp_arg.block++;
	if (p->len < TFTP_DATA_PACKET_MSG_LEN) {
		xil_printf("TFTP_RRQ: Transfer completed\r\n\r\n");
		f_close(&tftp_arg.fil);
		file_open = 0;
		tftp_in_process = 0;
	}
}
static void tftp_handle_read_req_resp ( struct udp_pcb * pcb , struct pbuf * p , ip_addr_t * ip , u16_t port ) { FRESULT Res ; static u8_t file_open = 0 ; static char fname [ 20 ] ; char * buf = ( char * ) p -> payload ; u32_t n ; if ( tftp_get_block_value ( p -> payload ) != tftp_arg . block ) { tftp_send_ack_packet ( pcb , ip , port , tftp_arg . block ) ; return ; } if ( ! file_open ) { sprintf ( fname , " " , tftp_arg . file_count ) ; Res = f_open ( & tftp_arg . fil , fname , FA_CREATE_ALWAYS | FA_WRITE ) ; if ( Res ) { xil_printf ( " \r \n " , fname ) ; tftp_send_error_packet ( pcb , ip , port , TFTP_ERR_DISKFULL ) ; tftp_in_process = 0 ; return ; } file_open = 1 ; } f_write ( & tftp_arg . fil , buf + TFTP_PACKET_HDR_LEN , p -> len - TFTP_PACKET_HDR_LEN , & n ) ; if ( n != p -> len - TFTP_PACKET_HDR_LEN ) { xil_printf ( " \r \n " ) ; tftp_send_error_packet ( pcb , ip , port , TFTP_ERR_DISKFULL ) ; f_close ( & tftp_arg . fil ) ; file_open = 0 ; tftp_in_process = 0 ; return ; } tftp_send_ack_packet ( pcb , ip , port , tftp_arg . block ) ; tftp_arg . block ++ ; if ( p -> len < TFTP_DATA_PACKET_MSG_LEN ) { xil_printf ( " \r \n \r \n " ) ; f_close ( & tftp_arg . fil ) ; file_open = 0 ; tftp_in_process = 0 ; } }
Send TFTP_ACK packet on receiving TFTP_DATA from host machine
Send TFTP_ACK packet on receiving TFTP_DATA from host machine
/* Verify block value */ /* Open file on the first ack */ /* Overwrite the file if already exists */ /* write data to file */ /* Cleanup */ /* send_ack_packet */ /* If the data packet length is less than maximum data packet length,
	 * that means we have received whole file. Do cleanup */ /* Cleanup */