!  test1.f90 
!
!  FUNCTIONS:
!	test1      - Entry point of console application.
!

!****************************************************************************
!
!  PROGRAM: test1
!
!  PURPOSE:  Entry point for the console application.
!			 SenseLock EL API test
!****************************************************************************

	program test1

	use s4module

	implicit none


	interface 
	subroutine readfromfile(buffer, bytesRead)
		!DEC$ ATTRIBUTES REFERENCE :: buffer
		 character*(*) buffer
		 integer*4		bytesRead
	end subroutine

	subroutine readfromfile2(buffer, bytesRead)
		!DEC$ ATTRIBUTES REFERENCE :: buffer
		 character*(*) buffer
		 integer*4		bytesRead
	end subroutine
	end interface

	include 's4.fi'

	! Variables
	
    integer*4           ret
 	integer*4			dwListSize
	INTEGER				ierr
	type(SENSE4_CONTEXT), ALLOCATABLE :: s4ctx(:)
	integer				num 
	character*8			userpin /'12345678'/
	character*24		devpin /'123456781234567812345678'/
	integer*4			bytesReturn
	character			buffer(400)
	character*5			inBuff /'1234'/
	character*10		outBuff
	type(S4CREATEDIRINFO) crtInfo
	type(S4OPENINFO)      openInfo
	
	 ALLOCATE (s4ctx(16), STAT = ierr)
	 IF (ierr.eq.0) then
		 ret = S4Enum(s4ctx(1), dwListSize)
		 if (ret /= 0) then
			ret = S4Enum(s4ctx(1), dwListSize)
			if (ret /= 0) then
				goto 200
			else
			    PRINT '(A12)', 'Enumerate device successfully!'
			end if
		end if
	else
		goto 200
	 end if

	openInfo.dwS4OpenInfoSize = 8
	openInfo.dwShareMode      = S4_EXCLUSIZE_MODE

	ret = S4OpenEx(s4ctx(1), openInfo)
	if (ret /= 0) then
		goto 200
	else
		PRINT '(A12)', 'Open device successfully!'
	end if

	crtInfo.dwS4CreateDirInfoSize  = 12
	ret = S4CreateDirEx(s4ctx(1), "\", 0, S4_CREATE_ROOT_DIR, crtInfo)
	if (ret /= 0) then
	    ret = S4ChangeDir(s4ctx(1), "\")
		if (ret /= 0) then
			goto 100
		end if

		ret = S4VerifyPin(s4ctx(1), devpin, 24, S4_DEV_PIN)
		if (ret /= 0) then
			goto 100
		end if

		ret = S4EraseDir(s4ctx(1), 0)
		if (ret /= 0) then
			goto 100
		end if

		ret = S4CreateDirEx(s4ctx(1), "\", 0, S4_CREATE_ROOT_DIR, crtInfo)
		if (ret /= 0) then
			goto 100
		end if
	end if

	PRINT '(A14)', 'Create MF successfully!'

	ret = S4VerifyPin(s4ctx(1), devpin, 24, S4_DEV_PIN)
	if (ret /= 0) then
		goto 100
	else
		PRINT '(A15)', 'Verify DEV PIN successfully!'
	end if

	CALL readfromfile(buffer(1), bytesReturn)

	ret = S4WriteFile(s4ctx(1), "0001",0, buffer(1), bytesReturn, bytesReturn, bytesReturn, S4_CREATE_NEW, S4_EXE_FILE)
	if (ret /= 0) then
		goto 100
	else
		PRINT '(A22)', 'Download VM_bin 0001 successfully!'
	end if

	bytesReturn = 0
	CALL readfromfile2(buffer(1), bytesReturn)
	ret = S4WriteFile(s4ctx(1), "0002",0, buffer(1), bytesReturn, 1000, bytesReturn, S4_CREATE_NEW, S4_XA_EXE_FILE)
	if (ret /= 0) then
		goto 100
	else
		PRINT '(A22)', 'Download XA_bin file 0002 successfully!'
	end if

	ret = S4VerifyPin(s4ctx(1), userpin, 8, S4_USER_PIN)
	if (ret /= 0) then
		goto 100
	else
		PRINT '(A16)', 'Verify USER PIN successfully!'
	end if
	
	ret = S4ExecuteEx(s4ctx(1), "0001", S4_VM_EXE, inBuff, 4, outBuff, 4, bytesReturn)
	if (ret /= 0) then
		goto 100
	else
		PRINT '(A22)', 'Execute VM_bin file 0001 successfully!'
	end if


	ret = S4ExecuteEx(s4ctx(1), "0002", S4_XA_EXE, inBuff, 4, outBuff, 4, bytesReturn)
	if (ret /= 0) then
		goto 100
	else
		PRINT '(A22)', 'Execute XA_bin file 0002 successfully!'
	end if

100 ret = S4Close(s4ctx(1))

200	DEALLOCATE (s4ctx, STAT = ierr)

	PRINT '(A8)', 'Test completed.'

	end program test1

subroutine readfromfile(buffer, bytesRead)
	
	!DEC$ ATTRIBUTES REFERENCE :: buffer

	use kernel32
	
	character*(*) buffer
	integer*4		bytesRead
	
	 type(T_SECURITY_ATTRIBUTES)  lpSecurityAttributes
	 integer(4) pfile
	 logical(4) b_ret
	 integer(4) NumberOfBytesRead
	 type(T_OVERLAPPED)    lpOverlapped
	 integer(4)	fileLen

	 NumberOfBytesRead = 0
 
 
	 pfile =  CreateFile( &
	  "demo_v20.bin"C, & 
	  GENERIC_READ,  &
	  0, &
	  lpSecurityAttributes, &
	  OPEN_EXISTING, &
	  FILE_ATTRIBUTE_NORMAL, &
	  NULL)

	 b_ret = GetFileSize(pfile, fileLen)
	 fileLen = b_Ret
 
	 b_ret = ReadFile (pfile ,LOC(buffer), fileLen, NumberOfBytesRead ,lpOverlapped) 
 
	bytesRead = fileLen

	 b_ret = CloseHandle(pfile)

end 

subroutine readfromfile2(buffer, bytesRead)
	
	!DEC$ ATTRIBUTES REFERENCE :: buffer

	use kernel32
	
	character*(*) buffer
	integer*4		bytesRead
	
	 type(T_SECURITY_ATTRIBUTES)  lpSecurityAttributes
	 integer(4) pfile
	 logical(4) b_ret
	 integer(4) NumberOfBytesRead
	 type(T_OVERLAPPED)    lpOverlapped
	 integer(4)	fileLen

	 NumberOfBytesRead = 0
 
	 pfile =  CreateFile( &
	  "demo_v30.bin"C, & 
	  GENERIC_READ,  &
	  0, &
	  lpSecurityAttributes, &
	  OPEN_EXISTING, &
	  FILE_ATTRIBUTE_NORMAL, &
	  NULL)

	 b_ret = GetFileSize(pfile, fileLen)
	 fileLen = b_Ret
 
	 b_ret = ReadFile (pfile ,LOC(buffer), fileLen, NumberOfBytesRead ,lpOverlapped) 
 
	bytesRead = fileLen

	 b_ret = CloseHandle(pfile)

end 