/************************************************************************/
/* Copyright 1998-2018 MBARI											*/
/************************************************************************/
/* $Header: /home/cvs/oasis5/src/controller/include/ymodem.h,v 1.2 2017/03/21 18:47:44 bobh Exp $		*/
/* Summary	: Definitions for YModem Protocol							*/
/* Filename : ymodem.h													*/
/* Author	: Robert Herlien (rah)										*/
/* Project	: BEDS2														*/
/* Revision : 1.0														*/
/* Created	: 08/24/2016													*/
/*																			*/
/* MBARI provides this documentation and code "as is", with no warranty,	*/
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium	*/
/* Research Institute to assist in its use, correction, modification, or	*/
/* enhancement. This information should not be published or distributed to	*/
/* third parties without specific written permission from MBARI.			*/
/*																			*/
/* Comment	: Adapted from xmodem program by Steve Grandi, grandi@noao.edu*/
/************************************************************************/
/* Modification History:												*/
/* 20aug98 rah, created													*/
/* 24aug2016 rah, created OASIS5 version from OASIS3 version			*/
/* 13mar2018 rah, created BEDS2 version from OASIS5 version				*/
/************************************************************************/

#ifndef INCymodemh
#define INCymodemh				1

#include <beds.h>
#include "fatfs/ff.h"					/* FatFs file system definitions	*/

/*	ASCII Constants	 */
#define		 SOH		001 
#define		 STX		002
#define		 CTLC		003
#define		 EOT		004
#define		 ENQ		005
#define		 ACK		006
#define		 LF			012		  /* Unix LF/NL */
#define		 CR			015	 
#define		 NAK		025
#define		 SYN		026
#define		 CAN		030
#define		 ESC		033

/* Protocol flags		*/
#define KPKT	1				/* Use 1K packets				*/
#define CRCFLAG 2				/* Use CRC						*/
#define GFLAG	4				/* Use YModem-G					*/


/*	XMODEM Constants  */
#define		 ERRORMAX	10	  /* maximum errors tolerated while transferring a packet */
#define		 NAKMAX		30	  /* maximum times to wait for initial NAK when sending */
#define		 RESYNCMAX	30	  /* maximum times to wait after file name pkt */
#define		 RETRYMAX	5	  /* maximum retries to be made certain handshaking routines */
#define		 EOTMAX		10	  /* maximum times sender will send an EOT to end transfer */
#define		 CRCCHR		'C'	  /* CRC request character */
#define		 KCHR		'K'	  /* 1K block request character */
#define		 GCHR		'G'	  /* Ymodem-G request character */
#define		 SMPKTSIZE	128	  /* Size of small packets (orig Xmodem) */
#define		 LGPKTSIZE	1024  /* Size of large packets (ymodem) */
#define		 YBUFSIZE	LGPKTSIZE


/* Constants added for YModem Receive	*/
#define PKT_TMOUT				(2)
#define CONNECT_TMOUT			(5)
#define PKT_HDR		 			(3)     /* STX/SOH, PktNum, ~PktNum	*/
#define CRC_LEN					(2)
#define RCV_OVHD				(PKT_HDR + CRC_LEN)
#define YRCV_SIZE				(YBUFSIZE + 16)
#define FNAME_SIZE				(128)
#define YMAX_ERRS				(5)
#define YTRIES					(10)

typedef struct
{
	FIL		fp;
	Nat32	fileSize;
	Nat32	fileTime;
	Byte	fname[FNAME_SIZE];
	Byte	ybuf[YRCV_SIZE];
} YFileStruct;


/****************************************/
/* Function Declarations				*/
/****************************************/

CmdRtn	ymdmSend(int argc, char **argv);
Errno	ymdmSendFile(char *name, FSIZE_t fsize, char *ymdmBuf);
CmdRtn	ymdmRcv(int argc, char **argv);

#endif /* INCymodemh */
