Option Explicit

'******************************************************************************
' Remote card-update script
'
' (VBScript example file for the commandline-parameter awailable in 
' Infinity USB 1.40)
'
' Downloads a keyfile from a website. Enables very easy update of keycard
' applications, the user simply has to insert the card, and execute the script-
' file. This can be particular important in applications where keys are often
' changed. In this example the external EEprom of a GoldCard is updated, but 
' extending the script to update both PIC and ext. EEprom (or any other card)
' should be straightforward.
' Semi-automatic updates can be implemented by scheduling the script (run it 
' on a daily basis), and if a new key is found on the website, the user is 
' prompted to insert a keycard for updating.
'
' Copyright WB Electronics 2003 - support@wbe.dk
'******************************************************************************

dim objXMLhttp, strHexfile, strURL
set objXMLhttp = CreateObject("Microsoft.XMLHTTP") 

'URL for the hexfile. This hexfile is for demonstration purposes only, and contains
'h'55' in all locations.
strURL = "http://www.infinityusb.com/files/sdk/cmdline/goldext.hex"

'Get hex-file
objXMLhttp.open "GET", strURL, false
objXMLhttp.send 

'Display errormessage (if any)
if err.number = 0 then
	if objXMLhttp.status = 200 then
		strHexfile = objXMLhttp.responsetext
	else
		wscript.echo "Hex-file not found at: " & strURL & vbCrLf & "Keycard not updated!"
		wscript.quit
	end if
else
	wscript.echo err.description & vbCrLf & "Keycard not updated!"
	wscript.quit
end if 
set objXMLhttp = nothing 

'Write hex-file to disk
dim objFSO, objFSOFile
set objFSO = createobject("Scripting.FileSystemObject")

'Create ascii-file for writing, overwrite if already exists
set objFSOFile = objFSO.createtextfile("c:\goldext.hex", true, false)

objFSOFile.write strHexfile
objFSOFile.close

'Cleanup objects
set objFSOFile = nothing
set objFSO = nothing

'Write hex-file to GoldCard using Infinity USB (Phoenix)
dim wshShell, intError
set wshShell = CreateObject("WScript.Shell")

'Run Infinity USB in hidden mode
intError = wshShell.run("""c:\program files\Infinity USB\Infusb.exe"" -hidden -int:c:\goldext.hex -card:GoldCard -write", , true)

'Cleanup objects
set wshShell = nothing

'Display errormessage (if any)
select case intError
case 0
	wscript.echo("Keycard written successfully")
case 1
	wscript.echo("Keycard written successfully")
case 2
	wscript.echo("Errorcode: " & intError & " - Verification failed when writing card!")
case 3
	wscript.echo("Errorcode: " & intError & " - No card inserted or card was removed during programming!")
case 4
	wscript.echo("Errorcode: " & intError & " - Syntax error!")
case 5
	wscript.echo("Errorcode: " & intError & " - No hardware connected!")
case 10
	wscript.echo("Errorcode: " & intError & " - Error selecting card using the -card switch!")
case 11
	wscript.echo("Errorcode: " & intError & " - Error loading flash file using the -flash switch!")
case 12
	wscript.echo("Errorcode: " & intError & " - Error loading int. eeprom file using the -int switch!")
case 13
	wscript.echo("Errorcode: " & intError & " - Error loading ext. eeprom file using the -ext switch!")
case else
	wscript.echo("Errorcode: " & intError & " - Unknown error!")
end select


'Parameters for Infinity USB 1.40:
' -hidden           : Executes commands while Infinity USB is not displayed, must be used with -write switch
' -card:cardname    : Selects card. For instance goldcard or funcard
' -flash:filename   : Selects file to load for 1st file (flash on AVR-based - PIC on Microchip based cards)
' -int:filename     : Selects file to load for 2nd file (int. eeprom on AVR-based - ext. eeprom on Microchip based cards)
' -ext:filename     : Selects file to load for 3rd file (ext. eeprom on AVR-based - doesn't exist on Microchip based cards)
' -write            : Writes the selected files to the selected card
