COMM-DRV/Lib: XferFiles() Iterative simplified transfer function

Description-

Transmit or receive file(s) with specified protocol. This function is used for transmitting and receiving files. For any file transfer session it must be called with three different commands with the same arguments. The first is to initialize some file transfer parameters. The second command is to actually perform the file transfer. The user should continue to call this function with the second command until it returns a status other than CDRVXFER_ERR_BUSY. Before the user exits the file transfer session, the third command should be sent to release any resources that were allocated. If the user needs to prematurely abort the transfer, the function with the third command should be called and the session exited.

Syntax-

stat = XferFiles(cmd,port,direction,protocol,fspec,XferInfo);

On Entry-

int port;

Port over which transfer will occur.

int cmd;

0 Initialize transfer. This function should be called once with this command for the start of every file transfer session.
1 Perform transfer. This function should be called with this command continuously until it no longer returns the status CDRVXFER_ERR_BUSY. To abort the transfer prematurely, simply stop calling this function with this command.
2 Cleanup when transfer complete or aborted. This function must be called at the end of a file transfer function.

int direction;

0 To send a file.
1 To receive a file.

int protocol;

0 XMODEM
1 XMODEM 1K
2 YMODEM
3 YMODEM Batch
4 YMODEM G
5 YMODEM G Batch
6 ZMODEM
7 XMODEM(Checksum)
8 ASCII File transfer

char *fspec;

Filename. If the filename has a path and files are being received, the files will reside in the directory in the path.

struct cdrvxfer_info *XferInfo;

Pointer to the file transfer control structure. Between calls to this function, this structure will contain updated information as follows. These values may be displayed or processed by the calling application. . Note that this structure is aligned/packed on a 4 byte (32 bit) boundary. As such make sure to configure the compiler or tool as necessary to ensure the correct alignment of structure elements.

On Exit-

int stat;

CDRVXFER_ERR_OK If the file transfer was successful.
CDRVXFER_ERR_SERIAL A serial I/O error has occurred. This could be caused by a broken serial communication cable, a bad UART or some other serial error.
CDRVXFER_ERR_OS An MSDOS error occurred. This may be caused by an error opening, writing, reading, or closing a file.
CDRVXFER_ERR_PROTOCOL A protocol error. This happens when an incompatibility between the file transfer protocol between the two transferring machines occur.
CDRVXFER_ERR_BUSY File transfer is still in progress.
CDRVXFER_ERR_CANCEL The remote issued a cancel.
CDRVXFER_ERR_INVPORT An invalid serial port was selected.
CDRVXFER_ERR_FNAME An invalid filename was given.
CDRVXFER_ERR_MEMORY A memory allocation error has occurred. This error may be caused due to a lack of memory. Most compilers causes an executable to allocate all memory on startup. You may want to use the exehdr program or some other compiler utility to adjust the maximum amount of memory the executable may allocate.

XferInfo->curblk Current block being transferred.
XferInfo->currenterr Error count for the file currently being transferred.
XferInfo->filesize Size of file that is being received. Note that some file transfer programs do not transmit the file size. In said case this value will be zero.
XferInfo->fspec Filename.
XferInfo->numbytes Number of bytes transferred in the current file.
XferInfo->offsetbytes Current offset in file.
XferInfo->totalerr Total of all errors for the current session.

See Also-

cdrvxfer_files()
cdrvxfer_getfiles()
cdrvxfer_sendfiles()
cdrvxfer_sfiles()
TransferFiles()

Example-

#include "comm.h"
main()
 {
 int direction=1;
 int port=0;
 int protocol=6;
 char *fspec="e:\\tmp\\*.*"";
 int stat;
 static struct cdrvxfer_info XferInfo;
/* Initialize the transfer */
 if ((stat = XferFiles(0,port,direction,protocol,fspec,&XferInfo))
 == CDRVXFER_ERR_OK)
//Transfer loop
 while(XferFiles(1,port,direction,protocol,fspec,
 &XferInfo) == CDRVXFER_ERR_BUSY)
 {
 printf("\rFilename=>%s ByteCnt=>%ld",
 XferInfo.fspec,XferInfo.numbytes);
 }
/* Close the file transfer */
 XferFiles(2,port,direction,protocol,fspec,&XferInfo);
 }