/************************************************************************/
/* Copyright 2013-2015 MBARI						*/
/************************************************************************/
/* Summary  : Send BEDS events via YModem                               */
/* Filename : bedsym.c							*/
/* Author   : Robert Herlien (rah)					*/
/* Project  : Benthic Event Detection System (BEDS)			*/
/* Revision : 1.0							*/
/* Created  : 09/24/2015 from bedszm.c					*/
/*									    */
/* 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.            */
/*									    */
/************************************************************************/
/* Modification History:						*/
/* 24sept2015 rah - created from bedszm.h				*/
/************************************************************************/

#include <cfxpico.h>		/* Persistor PicoDOS definitions	*/
#include <mbariTypes.h>		/* MBARI type definitions		*/
#include <beds.h>		/* BEDS controller definitions		*/
#include <syslog.h>		/* BEDS System logger			*/
#include <file.h>		/* BEDS file I/O definitions		*/
#include <fileUtils.h>		/* BEDS file utilities definitions	*/
#include <modem.h>		/* BEDS acoustic modem software		*
#include <bedsym.h>		/* YModem definitions                   */
#include <dirent.h>


/****************************************/
/*	External Data			*/
/****************************************/

Extern MBool	zmSetRemote;	/* Use remote commands to set SRegs for ZModem*/

int ymSendFiles(char *name);


/************************************************************************/
/* Function    : dsYSend						*/
/* Purpose     : Func sent to dirScan to send file via YModem		*/
/* Inputs      : dirent ptr (per dirScan)				*/
/* Outputs     : TRUE to continue, FALSE to stop			*/
/************************************************************************/
MLocal MBool dsYSend(struct dirent *de, int *rtnp)
{
    char *name;
    char cmdbuf[32];

    name = de->d_name;
    if ((de->d_attr & (_A_SUBDIR | _A_VOLID)) != 0)
        return(TRUE);

    sprintf(cmdbuf, "YS %s\r", name);
    printf("Set your terminal program for YModem receive:  %s\n", name);
    sysLogPrintf("Sending %s via YModem\n", name);

    *rtnp = execstr(cmdbuf);

    sysLogPrintf("%s sent via YModem, result = %d\n", name, *rtnp);

    return(*rtnp == 0 ? TRUE : FALSE); /* If failed protocol, stop	*/

} /*dsYSend() */


/************************************************************************/
/* Function    : ymSendFiles						*/
/* Purpose     : Send multiple files via YModem prototcol		*/
/* Inputs      : Name (wildcards supported)                             */
/* Outputs     : OK or ERROR						*/
/************************************************************************/
int ymSendFiles(char *name)
{
    int       rtn = 0;
    int	remModem = -1, remS3, remS8;

    sysLogPrintf("Starting YModem send for %s\n", name);

    RTCDelayMicroSeconds(500000);
    if (zmSetRemote && isModemPort() && modemCommandMode())
    {
	if ((remModem = getSReg(14)) >= 0)
	{
	    remS3 = getRemoteSReg(remModem, 3);
	    remS8 = getRemoteSReg(remModem, 8);

	    if ((remS3 >= 0) && (remS3 & 0x80))
		setRemoteSReg(remModem, 3, remS3 & 0x7f);
	    if (remS8 > 10)
		setRemoteSReg(remModem, 8, 10);
	}

	modemOnlineMode();
    }

    dirScan(name, dsYSend, &rtn);

    CIOoflush();
    sysLogPrintf("YModem finished, rtn = %d\n", rtn);

    if (zmSetRemote && isModemPort() && (remModem >= 0) && 
	((remS3 & 0x80) || (remS8 > 10)))
	if (modemCommandMode())
	{
	    if ((remS3 >= 0) && (remS3 & 0x80))
		setRemoteSReg(remModem, 3, remS3);
	    if (remS8 > 10)
		setRemoteSReg(remModem, 8, remS8);

	    modemOnlineMode();
	}

    return(rtn);
}
