#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ModeSet.h"



MODE      mode[255];

int       NumberOfModes;



typedef struct
  {
    GROUPS group;
    char  *name;
  } GROUPSNAME;



static  GROUPSNAME  GroupsName[]  =
  {
    { NOTAB,              "No Tab"               },              
    { INFORMATION,        "Information"          },           
    { CONTROL,            "Control"              },   
    { ADVANCEDCONTROL,    "Advanced Control"     },  
    { INTERNAL,           "Internal"             },
    { TEXT,               "Text"                 },
    { CLOCK,              "Clock"                },   
    { BARCODERESPONSE,    "Barcode Response"     },  
    { TOUCHSCREEN,        "TouchScreen"          },   
    { PERIPHERAL,         "Peripheral"           },   
    { ADVANCEDPERIPHERAL, "Advanced Peripheral"  },  
    { PIPCONTROL,         "PIP Control"          },   
    { ADVANCEDPIPCONTROL, "Advanced PIP Control" },
    { UNITNETWORKSETUP,   "Unit Network Setup"   }, 
    { PIPSERVERSETUP,     "PIP Server Setup"     },
    { IMAGESERVERSETUP,   "Image Server Setup"   }, 
    { WIRELESS,           "Wireless"             }
  };


typedef struct
  {
    char  *mode;
    GROUPS group;
    BOOL   report;
    BOOL   advanced;
    BOOL   ReadOnly;
  } MODEPROPERTIES;


static MODEPROPERTIES ModeProperties[]   =
  {
    { "productName",            INFORMATION,           TRUE,   TRUE,   FALSE },
    { "Version",                INFORMATION,           TRUE,   FALSE,  TRUE  },
    { "buildInfo",              INFORMATION,           TRUE,   FALSE,  TRUE  },
    { "dateTime",               INFORMATION,           TRUE,   TRUE,   FALSE },
    { "serialNumber",           INFORMATION,           TRUE,   TRUE,   FALSE },
    { "FlashHash",              INFORMATION,           TRUE,   FALSE,  TRUE  },
    { "unitID",	                INFORMATION,           TRUE,   TRUE,   FALSE },  
    { "user1",	                INFORMATION,           TRUE,   FALSE,  FALSE },  

    { "ShowShortPoll",          CONTROL,               TRUE,   FALSE,  FALSE },
    { "ShowLongPoll",           CONTROL,               TRUE,   FALSE,  FALSE },
    { "DisplaySetup",           CONTROL,               TRUE,   FALSE,  FALSE },
    { "DisplaySetup2",          CONTROL,               TRUE,   FALSE,  FALSE },
    { "DisplayAll",             CONTROL,               TRUE,   FALSE,  FALSE },
    { "ShowLogo",               CONTROL,               TRUE,   FALSE,  FALSE },
    { "invalidate",             CONTROL,               FALSE,  FALSE,  FALSE },
    { "commit",	                CONTROL,               FALSE,  FALSE,  FALSE },  
    { "restart",                CONTROL,               FALSE,  FALSE,  FALSE },
    
    { "BypassNCRprefix",        ADVANCEDCONTROL,       TRUE,   FALSE,  FALSE },
    { "NoBarcodes",             ADVANCEDCONTROL,       TRUE,   FALSE,  FALSE },

    { "eventStart",             PERIPHERAL,            TRUE,   FALSE,  FALSE },
    { "eventEnd",               PERIPHERAL,            TRUE,   FALSE,  FALSE },
    { "WallPaperEvent",         PERIPHERAL,            TRUE,   FALSE,  FALSE },
    
    { "Port2Function",          ADVANCEDPERIPHERAL,    TRUE,   TRUE,   FALSE },
    { "CommConfig",             ADVANCEDPERIPHERAL,    TRUE,   FALSE,  FALSE },
    
    { "PointerHScale",          TOUCHSCREEN,           TRUE,   FALSE,  TRUE  },
    { "PointerVScale",          TOUCHSCREEN,           TRUE,   FALSE,  TRUE  },
    { "LastPointerH",           TOUCHSCREEN,           FALSE,  FALSE,  FALSE },
    { "LastPointerV",           TOUCHSCREEN,           FALSE,  FALSE,  FALSE },
    { "PointerIsDown",          TOUCHSCREEN,           FALSE,  FALSE,  FALSE },

    { "ScanShowClock",          CLOCK,                 TRUE,   FALSE,  FALSE },
    { "ClockVPos",              CLOCK,                 TRUE,   FALSE,  FALSE },
    { "ClockHPos",              CLOCK,                 TRUE,   FALSE,  FALSE },
    { "Clock_Fcolor",           CLOCK,                 TRUE,   FALSE,  FALSE },
    { "Clock_Bcolor",           CLOCK,                 TRUE,   FALSE,  FALSE },
    { "ClockFormat",            CLOCK,                 TRUE,   FALSE,  FALSE },
    { "ClockFontFile",          CLOCK,                 TRUE,   TRUE,   FALSE },
    { "ClockUpdateInterval",    CLOCK,                 TRUE,   FALSE,  FALSE },
    { "ClockTransparent",       CLOCK,                 TRUE,   FALSE,  FALSE },

    { "fontFile",               TEXT,                  TRUE,   TRUE,   FALSE },
    { "textRows",               TEXT,                  TRUE,   FALSE,  FALSE },
    { "textCols",               TEXT,                  TRUE,   FALSE,  FALSE },
    { "textHPos",               TEXT,                  TRUE,   FALSE,  FALSE },
    { "textVPos",               TEXT,                  TRUE,   FALSE,  FALSE },
    { "textForegnd",            TEXT,                  TRUE,   FALSE,  FALSE },
    { "textBackgnd",            TEXT,                  TRUE,   FALSE,  FALSE },
    { "TextDisplay",            TEXT,                  TRUE,   FALSE,  FALSE },
    { "textReset",              TEXT,                  FALSE,  FALSE,  FALSE },
    { "TextTransparent",        TEXT,                  TRUE,   FALSE,  FALSE },

    { "POStimeout",             BARCODERESPONSE,       TRUE,   FALSE,  FALSE },
    { "msgChecking",            BARCODERESPONSE,       TRUE,   FALSE,  FALSE },
    { "msgUnavail",             BARCODERESPONSE,       TRUE,   FALSE,  FALSE },

    { "sourceFile",             INTERNAL,              FALSE,  FALSE,  FALSE },
    { "destinationFile",        INTERNAL,              FALSE,  FALSE,  FALSE },
    { "GiveHelpText",           INTERNAL,              FALSE,  FALSE,  FALSE },
    { "forceHelpText",          INTERNAL,              FALSE,  FALSE,  FALSE },
    { "UnitStatus",             INTERNAL,              FALSE,  FALSE,  FALSE },
    { "SlideShowFiles",         INTERNAL,              FALSE,  FALSE,  FALSE },

    { "RegisterINI",            PIPCONTROL,            TRUE,   FALSE,  FALSE },
    { "RegisterStart",          PIPCONTROL,            TRUE,   FALSE,  FALSE },
    { "TrivialComm",            PIPCONTROL,            TRUE,   FALSE,  FALSE },
    { "sendUnitID",             PIPCONTROL,            TRUE,   FALSE,  FALSE },

    { "KeepAliveTime",          ADVANCEDPIPCONTROL,    TRUE,   FALSE,  FALSE },
    { "SingleQTrans",           ADVANCEDPIPCONTROL,    TRUE,   FALSE,  FALSE },
    { "IgnoreAckNak",           ADVANCEDPIPCONTROL,    TRUE,   FALSE,  FALSE },
    { "IgnoreResponseChars",    ADVANCEDPIPCONTROL,    TRUE,   FALSE,  FALSE },
    { "QueryPadTo",             ADVANCEDPIPCONTROL,    TRUE,   FALSE,  FALSE },
    { "QueryPrefix",            ADVANCEDPIPCONTROL,    TRUE,   FALSE,  FALSE },
    { "Timeout",                ADVANCEDPIPCONTROL,    FALSE,  FALSE,  FALSE },
    { "sendError",              ADVANCEDPIPCONTROL,    FALSE,  FALSE,  FALSE },
    { "sendResponse",           ADVANCEDPIPCONTROL,    FALSE,  FALSE,  FALSE },
    { "forceError",             ADVANCEDPIPCONTROL,    FALSE,  FALSE,  FALSE },
    { "forceResponse",          ADVANCEDPIPCONTROL,    FALSE,  FALSE,  FALSE },

    { "NTPhost",                UNITNETWORKSETUP,      TRUE,   FALSE,  FALSE },
    { "NTPTZoffset",            UNITNETWORKSETUP,      TRUE,   FALSE,  FALSE },
    { "unitIP",	                UNITNETWORKSETUP,      TRUE,   FALSE,  FALSE },  
    { "unitMask",               UNITNETWORKSETUP,      TRUE,   FALSE,  FALSE },
    { "gatewayIP",              UNITNETWORKSETUP,      TRUE,   FALSE,  FALSE },
    { "domain",	                UNITNETWORKSETUP,      TRUE,   FALSE,  FALSE },  
    { "DNS",	                  UNITNETWORKSETUP,      TRUE,   FALSE,  FALSE },    
    { "WINSserverIP",           UNITNETWORKSETUP,      TRUE,   FALSE,  FALSE },
    { "FTPUserName",            UNITNETWORKSETUP,      TRUE,   TRUE,   FALSE },
    { "FTPPassword",            UNITNETWORKSETUP,      TRUE,   TRUE,   FALSE },
    { "FTPSuperName",           UNITNETWORKSETUP,      TRUE,   TRUE,   FALSE },
    { "FTPSuperPwd",            UNITNETWORKSETUP,      TRUE,   TRUE,   FALSE },
    { "ServerType",             UNITNETWORKSETUP,      TRUE,   FALSE,  FALSE },

    { "sloppiIP",               PIPSERVERSETUP,        TRUE,   FALSE,  FALSE },
    { "sloppiport",             PIPSERVERSETUP,        TRUE,   FALSE,  FALSE },
    { "SLOPPITimeout",          PIPSERVERSETUP,        TRUE,   FALSE,  FALSE },

    { "windowsServ",            IMAGESERVERSETUP,      TRUE,   FALSE,  FALSE },
    { "shareName",              IMAGESERVERSETUP,      TRUE,   FALSE,  FALSE },
    { "FTPServerIP",            IMAGESERVERSETUP,      TRUE,   FALSE,  FALSE },
    { "userName",               IMAGESERVERSETUP,      TRUE,   TRUE,   FALSE },
    { "password",               IMAGESERVERSETUP,      TRUE,   TRUE,   FALSE },
    { "QFXServer",              IMAGESERVERSETUP,      TRUE,   FALSE,  FALSE },
    { "QFXport",                IMAGESERVERSETUP,      TRUE,   FALSE,  FALSE },
    { "QFXTimeout",             IMAGESERVERSETUP,      TRUE,   FALSE,  FALSE },

    { "NetworkName",            WIRELESS,              TRUE,   FALSE,  FALSE },
    { "Diversity",              WIRELESS,              TRUE,   FALSE,  FALSE },
    { "Wireless",               WIRELESS,              TRUE,   FALSE,  FALSE },
    { "WEPEncryption",          WIRELESS,              TRUE,   FALSE,  FALSE },
    { "WEPKey1",                WIRELESS,              TRUE,   FALSE,  FALSE },
    { "WEPKey2",                WIRELESS,              TRUE,   FALSE,  FALSE },
    { "WEPKey3",                WIRELESS,              TRUE,   FALSE,  FALSE },
    { "WEPKey4",                WIRELESS,              TRUE,   FALSE,  FALSE },
    { "WEPKeyIndex",            WIRELESS,              TRUE,   FALSE,  FALSE },
    { NULL }
  };

int GetNameIndex( LPCTSTR name )
  {
    int i;

    if ( !name			||
	 !*name			||
	 isspace( name[ 0 ])	||
	 (name[ 0 ]  ==  '#'))
	return	-1;

    for ( i = 0; i < NumberOfModes; i++ )
    {
	if( stricmp( name, mode[ i ].Name ) == 0 )
	    return  i;
    }

    return -1;
}

void	ClearTrailingWhiteSpace( char *content )
{
    int	i;

    for ( i = strlen( content ) - 1; i >= 0; i-- )
    {
	if ( isspace( content[ i ] ))
	    content[ i ]    = 0;
	else
	    break;
    }

    return;
}



void	ReadNVRamContent( SLPACKET *pkt )
{
    char   *p;
    int	    i	= 0;
    int	    total;
    char    name[ 255 ];

    p = pkt->body.moderesponse.moderesponse;

    while( *p )
    {
	total		= strcspn( p, " =\t" );
	strncpy( name, p, total );
	name[ total ]	= 0;

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

	i   = GetNameIndex( name );
	if( i >= 0 )
	{
	    strncpy( mode[ i ].NVRamContent, p, total );
	    mode[ i ].NVRamContent[ total ] = 0;
	    ClearTrailingWhiteSpace( mode[ i ].NVRamContent );
	}

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


short BuildModeArray( void )
{
    char   *p;
    int	    i = 0;
    int	    total;
    char    temp[ 255 ];

    p	= pkt->body.moderesponse.moderesponse;

    while ( *p )
    {
	total = strcspn( p, " =" );
	//strncpy( temp, p, total );
	strncpy( mode[ i ].Name, p, total );
	mode[ i ].Name[ total ] = 0;

	p	+= total;
	p	+= strspn( p, "= " );
	total	= strcspn( p, " \t\n\r," );
	strncpy( mode[i].Type, p, total );
	mode[i].Type[ total ]	= 0;
	p	+= total;

	if ( stricmp( mode[ i ].Type, "STRING") == 0 )
	{
	    p	    += strspn( p, ", " );
	    total   = strcspn( p, " \t\n\r" );
	    strncpy( temp, p, total );
  	    temp[ total ]	= 0;
	    mode[ i ].Size	= atoi( temp );
	    p	+= total;
	}

	p += strspn( p, " \t" );
        total = strcspn( p, "\n\r" );
	strncpy( mode[i].Help, p, total );
        p += total;
	p += strspn( p, "\n\r" );
	i++;
    }

    return i;   // NumberOfModes
}


short InitModeArray ( void )
{

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

    SendPacket( pkt, "" );
    RecvPacket( pkt );
    NumberOfModes = BuildModeArray ();

    // Query the NVRam value 

    SendPacket( pkt, "*" );
    RecvPacket( pkt );

    ReadNVRamContent ( pkt );
    return NumberOfModes;
}

int enumVal( char *TrueFalse )
{
    int	ch;

    if ( TrueFalse )
    {
	ch  = toupper( *TrueFalse );
	switch( ch )
        {
	    case    'T':
	    case    'Y':
	    case    '1':
	    {
		return	SR_TRUE;
	    }

	    case    'F':
	    case    'N':
	    case    '0':
	    {
		return	SR_FALSE;
            }

	    default:
	    {
		return	SR_NONE;
	    }

        }
    }

    return  SR_NONE;
}



