// ProductInfoCtl.cpp 


#include "wtypes.h"
#include <sys/stat.h>
#include <stdio.h>
#include <windows.h>
#include <OLEAUTO.H> //only needed for SAFEARRAY structure
#include "ModeSet.h"



char    FileBuffer[128];

#define TSIZE   1024

short   LastError;

typedef enum
    {
      NONE,
      FailToConnect,
    } LASTERROR;



short __stdcall PortSet(LPCTSTR IPAddress, long DataRate, short DataBits, LPCTSTR Parity, short StopBits, LPCTSTR FlowControl) 
{
    char    stopbit[ 30 ];
    char    control[ 30 ];
    char    parity [ 30 ];

    strcpy( stopbit, ((StopBits == 1) ? "-cstopb" : "cstopb") );

    switch( *FlowControl )
      {
        case 'H':                                           /* Hardware */
            strcpy( control, "clocal crtscts -ixon" );
            break;
                                                            
        case 'X':                                           /* Xon/Xoff */
            strcpy( control, "clocal ixon -crtscts" );
            break;

        default:
            strcpy( control, "clocal -crtscts -ixon" );
        }


    switch( *Parity )
      {
        case 'E':                                           /* Even     */
            strcpy( parity, "parenb -parodd" );
            break;
                                                            
        case 'O':                                           /* Odd      */
            strcpy( parity, "parenb parodd" );
            break;

        default:
            strcpy( parity, "-parenb" );
        }


    sprintf( FileBuffer, 
	         "stty -F /dev/ttyS0 ispeed %d ospeed %d cs%d %s %s icrnl hupcl -icanon %s cread -echo -echoe -echok min 1\n", 
              DataRate, DataRate, DataBits, stopbit, parity, control );

    return PushBuffer( IPAddress, FileBuffer, "setscanner2" ); 
}


short __stdcall svOpen(LPCSTR IPAddress) 
{
        LastError = NONE;
        
        if ( !connectUnit(IPAddress) )
        {
            LastError = FailToConnect; 
            return 0; 
        }

        NumberOfModes = InitModeArray( );

        return NumberOfModes;
}



BOOL __stdcall svClose( void ) 
{
        return disconnectUnit( );
}


BSTR  __stdcall GetMode(LPCTSTR ModeName) 
{
        char    TempBuffer[TSIZE];

        getMode( ModeName, TempBuffer, TSIZE);

        return SysAllocStringLen( (const OLECHAR *) TempBuffer, (strlen(TempBuffer) + 1) / 2 ); 
}


BOOL __stdcall svSetMode(LPCTSTR ModeName, LPCTSTR ModeValue) 
{
    char    Mode[ 255 ];

    sprintf( Mode, "%s = %s", ModeName, ModeValue );
    SendPacket( pkt, Mode );
    return RecvPacket( pkt );
}



BOOL __stdcall svCommit( void )
{
        svSetMode( "Commit", "TRUE" );
        return TRUE;
}

BOOL __stdcall svRestart( void ) 
{
    SendPacket( pkt, "RESTART=TRUE" );
    return TRUE;
}

short __stdcall GetModeSize(LPCTSTR ModeName) 
{
    int i;

    i = GetNameIndex( ModeName );
    return mode[ i ].Size;
}


BSTR __stdcall GetModeType(LPCTSTR ModeName) 
{
        char    TempBuffer[TSIZE];

        getModeType( ModeName, TempBuffer, TSIZE);
        return SysAllocStringLen( (const OLECHAR *) TempBuffer, (strlen(TempBuffer) + 1) / 2 ); 
}


BSTR __stdcall GetFirstModeName( void )
{
        char    TempBuffer[TSIZE];

        getFirstModeName( TempBuffer, TSIZE);
        return SysAllocStringLen( (const OLECHAR *) TempBuffer, (strlen(TempBuffer) + 1) / 2 ); 
}

 BSTR __stdcall GetNextModeName( void ) 
{
        char    TempBuffer[TSIZE];

        getNextModeName( TempBuffer, TSIZE);
        return SysAllocStringLen( (const OLECHAR *) TempBuffer, (strlen(TempBuffer) + 1) / 2 ); 
}



BSTR __stdcall GetModeHelp(LPCTSTR ModeName) 
{
        char    TempBuffer[TSIZE];
        
        getModeHelp( ModeName, TempBuffer, TSIZE);
        return SysAllocStringLen( (const OLECHAR *) TempBuffer, (strlen(TempBuffer) + 1) / 2 ); 
}


BSTR __stdcall GetNVRamMode(LPCTSTR ModeName) 
{
        char    TempBuffer[TSIZE];
        
        getNVRamMode( ModeName, TempBuffer, TSIZE);
        return SysAllocStringLen( (const OLECHAR *) TempBuffer, (strlen(TempBuffer) + 1) / 2 ); 
}


short __stdcall svGetLastError() 
{
	return LastError;
}



void __stdcall SendTerminate() 
{
    int	total  = 0;

    memset( pkt,0,BUFFER_SIZE);

    total = sizeof( pkt->header ) + 1;
    pkt->header.token = SLT_TERMINATE;
    pkt->header.length = htonl( total);

    sendPacket( (char *) pkt, ntohl( pkt->header.length ));
}


BOOL  SendMode( LPCTSTR Mode )
{
    int	  total  = 0;

    memset( pkt,0,BUFFER_SIZE);

    total = sizeof( pkt->header ) + strlen( Mode ) + 1;
    pkt->header.token = SLT_MODESETQUERY;
    pkt->header.length = htonl( total);

    if( *Mode )
	    strcpy( pkt->body.modesetquery.modesetquery, Mode );

    return sendPacket( (char *) pkt, ntohl( pkt->header.length ));
}


BOOL __stdcall SendModeSet(LPCTSTR ModeName, LPCTSTR ModeValue) 
{
    char  Mode[ 255 ];

    sprintf( Mode, "%s = %s", ModeName, ModeValue );

    return SendMode( Mode );
}

BSTR __stdcall SendModeQuery(LPCTSTR ModeName) 
{
    char     *p;
    int       total;
    char    TempBuffer[TSIZE];

    SendMode( ModeName );

    RecvPacket( pkt );

    p = pkt->body.moderesponse.moderesponse;

    total = strcspn( p, " =\t" ); 

    p   += total;
    p   += strspn( p, "= \t" );
    total = strcspn( p, "\n\r\t" );

    strncpy( TempBuffer, p, total );
    TempBuffer[ total ] = 0;
    ClearTrailingWhiteSpace( TempBuffer );

    return SysAllocStringLen( (const OLECHAR *) TempBuffer, (strlen(TempBuffer) + 1) / 2 ); 
}

BOOL __stdcall SendRegistration() 
{
    int	  total  = 0;

    memset( pkt,0,BUFFER_SIZE);

    total = sizeof( pkt->header ) + 1;
    pkt->header.token = SLT_REGISTRATION;
    pkt->header.length = htonl( total);

    return sendPacket( (char *) pkt, ntohl( pkt->header.length ));
}

BOOL __stdcall SendDataNOP() 
{
    int	  total  = 0;

    memset( pkt,0,BUFFER_SIZE);

    total = sizeof( pkt->header ) + 1;
    pkt->header.token    = SLT_REGISTRATION;
    pkt->body.data.type	 = SLD_NOP;
    pkt->header.length   = htonl( total);

    return sendPacket( (char *) pkt, ntohl( pkt->header.length ));

	return TRUE;
}


BOOL SendBuffer( char *file, int fileSize, char *TargetFileName, SLOPPIFILE FileType )
{
    char  *cbuf;
    int		TargetPathSize = 0;
    
    memset( pkt,0,BUFFER_SIZE);

    cbuf = pkt->body.data.data.file.theFile;


    if( FileType == SLF_GIF )
    {
      if ( file )
        memcpy( (cbuf + TargetPathSize), file, fileSize + 1);
    }
    else 
    {
      if ( TargetFileName )
	      strcpy( cbuf, TargetFileName );

	    TargetPathSize = strlen( TargetFileName ) + 1;

      if ( file )
        memcpy( (cbuf + TargetPathSize), file, fileSize );
    }

    pkt->header.length			  = htonl(fileSize + sizeof (SLPACKET) + TargetPathSize);
    pkt->header.token			    = SLT_DATA;
    pkt->body.data.type		    = SLD_FILE;
    pkt->body.data.data.file.file.length   = htonl(fileSize);
    pkt->body.data.data.file.file.token    = (SLOPPITOKEN) FileType;

    return sendPacket( (char *)pkt, ntohl( pkt->header.length ));
}

BOOL localSendFile( LPCTSTR File, LPCTSTR TargetFileName, SLOPPIFILE FileType )
{
    struct      stat	cstat;
    char       *buf;
    FILE       *filePtr;
    BOOL        status = FALSE;

    if ( stat( File, &cstat ))
        return status;
  
    buf = ( char *) malloc( cstat.st_size );

    if ( !buf )
        return status;

    filePtr = fopen( File, "rb" );

    if ( filePtr )
    {
      if ( fread( buf, cstat.st_size, 1, filePtr ) )
        status = SendBuffer( (char*) buf, cstat.st_size, (char *) TargetFileName, FileType );
    
      fclose( filePtr );
    }

	  free( buf );

    return status;
}



BOOL __stdcall SendImageToDisplay( LPCTSTR File ) 
{
    return localSendFile(File, File, SLF_GIF );
}



BOOL __stdcall SendFile(LPCTSTR File, LPCTSTR TargetFileName) 
{
    return localSendFile(File, TargetFileName, SLF_FILE );
}


BOOL SendText( char *Text, SLOPPIDATA TextType )
{
    char  *cbuf;
    
    memset( pkt,0,BUFFER_SIZE);

    cbuf = pkt->body.data.data.info;

    if ( Text )
	    strcpy( cbuf, Text );

    pkt->header.length			  = htonl( strlen(Text) + 1 + sizeof (SLPACKET) );
    pkt->header.token			    = SLT_DATA;
    pkt->body.data.type		    = TextType;

    return sendPacket( (char *)pkt, ntohl( pkt->header.length ));
}


BOOL __stdcall SendDisplayText(LPCTSTR Text) 
{
    return SendText( (char *) Text, SLD_DISPLAYTEXT );
}


BOOL __stdcall SendSpecialText(LPCTSTR Text) 
{
    return SendText( (char *) Text, SLD_SPECIALTEXT );
}

BOOL __stdcall Push(LPCTSTR IPAddress, LPCTSTR File, LPCTSTR TargetFileName) 
{
	// TODO: Add your dispatch handler code here

	return PushFile( IPAddress, File, TargetFileName );
}


short __stdcall ReOpen(LPCTSTR IPAddress) 
{
	// TODO: Add your dispatch handler code here

    LastError = NONE;

    if ( !connectUnit(IPAddress) )
    {
        LastError = FailToConnect; 
        return 0; 
    }

    SendPacket( pkt, "ForceResponse=TRUE" );
    RecvPacket( pkt );
    SendPacket( pkt, "ForceError=TRUE" );
    RecvPacket( pkt );
    SendPacket( pkt, "ForceHelpText=TRUE" );
    RecvPacket( pkt );
     
    return TRUE;
}




