﻿B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=12.8
@EndOfDesignText@
Sub Class_Globals
	
	Private const T_UINT_1 = 1, T_UINT_2 = 2, T_UINT_4 = 3, T_NINT_1 = 4, T_NINT_2 = 5, _
		          T_INT_4 = 6, T_FLOAT_4 = 7, T_STRING = 8, T_LONG_BYTES = 9, T_SHORT_BYTES = 0x40, T_SMALL_UINT = 0x80 As Int
	Private const MARK_BEGIN = 0x7E, MARK_END = 0x7F As Byte
	Private raf As RandomAccessFile
	Private smallbuffer(4) As Byte
	Private out As OutputStream
	
	
	Private SmartCardClass As JavaObject
	Private SmartCardIstance As JavaObject
	Private BuildingLibrary As JavaObject
	
	Private StringUtil As JavaObject
	
	Dim vccMode(1) As Byte
	Dim slotNumber As Int = -1
	Dim slotFinded = False
	
	Dim Atr(41) As Byte
	Dim apduResp(512) As Byte
	Dim tmpBuffer() As Byte
	Dim errorValue As Int = 0
	Dim errorString As String = ""
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
	
	SmartCardClass.InitializeContext
	SmartCardIstance.InitializeContext
	StringUtil.InitializeContext
	StringUtil.InitializeStatic("vpos.apipackage.StringUtil")
	
	'BuildingLibrary.InitializeNewInstance("vpos.apipackage.BuildConfig", Null)
''	Log(SmartCardClass.RunMethod("BuildConfig",Null))
	SmartCardClass.InitializeNewInstance("vpos.apipackage.PosApiHelper", Null)
	
	
	'VCC_Mode Read Card Voltage：
	'1---5V;
	'2---3V;
	'3---1.8V;
	vccMode(0) = 3

End Sub

Public Sub connect
	
End Sub

Public Sub GetSmartCardSlot As Int

	Dim resolver As JavaObject
	resolver.InitializeContext
	resolver.InitializeNewInstance("vpos.apipackage.PosApiHelper", Null)
	Private iccSmartCard As JavaObject
	iccSmartCard.InitializeContext
	iccSmartCard.InitializeNewInstance(	"vpos.apipackage.PosApiHelper", Null)
	For i = 0 To 2
		Dim Slot(1) As Byte
		Slot(0) = i
		Dim success As Int = resolver.RunMethod("IccOpen",Array(Slot(0), vccMode(0),Atr))
		resolver.RunMethod("IccClose",Array(Slot(0)))
		If success = 0 Then
			slotFinded = True
			Log("Open slot PDAM" & i)
			slotNumber = i
		End If
	Next
	Return slotNumber
End Sub
	
	
Public Sub openSmartCard(slot1 As Int) As Int
	Dim slot(1) As Byte
	slot(0) = slot1
	slotNumber = slot1
	'return 
	' 0 Initialization success.
	' (-2403) Channel Error
	' (-2405) The card Is pulled out Or Not
	' (-2404) Protocol error
	' (-2500) Voltage mode error of IC card reset
	' (-2503) Communication failure.
	Dim resposeicc As Int = SmartCardClass.RunMethod("IccOpen", Array(slot(0), vccMode(0),Atr))
	errors(resposeicc)
	If resposeicc = 0 Then 
		slotFinded = True
	End If
	Log("ATR = "& bytesToHexString(Atr))
	Return resposeicc
End Sub

Public Sub checkSmartCard(slot1 As Int)
	Dim slot(1) As Byte
	slot(0) = slot1
	slotFinded = False
	Dim resposeicc As Int = SmartCardClass.RunMethod("IccCheck",Array(slot(0)))
	errors(resposeicc)
	If resposeicc = 0 Then
		slotFinded = True
	End If
	Return resposeicc
End Sub


Public Sub commandSmartCard(cmd() As Byte, Lc As Byte , Data() As Byte, Le As String)
	Dim bc As ByteConverter
	
	If Not (slotFinded) Then
		 Return 
		 Log("slot not open")
	End If
	Log(Le)
	Dim Lebytes() As Byte = bc.HexToBytes(Le)
	Dim apduSend1(cmd.Length+1+Data.Length+Lebytes.Length) As Byte
	Dim bytePos As Int = 0
	For i = 0 To cmd.Length -1
		apduSend1(bytePos) = cmd(i)
		bytePos = bytePos + 1
	Next
	For i = 0 To Data.Length -1
		apduSend1(bytePos) = Data(i)
		bytePos = bytePos + 1
	Next
	apduSend1(bytePos) = Lc
	bytePos = bytePos + 1
	For i = 0 To Lebytes.Length-1
	   apduSend1(bytePos) = Lebytes(i)
	Next
	
	
	Log("APDU " & bytesToHexString( apduSend1 ) )
	'openSmartCard(slotNumber)
	Dim slot(1) As Byte
	slot(0) = slotNumber

	Dim resposeicc As Int = SmartCardClass.RunMethod("IccCommand", Array(slot(0),apduSend1 ,apduResp))
	errors(resposeicc)
	If resposeicc = 0 Then
		slotFinded = True
	End If

	Log("buffer = " & bytesToHexString( apduResp ))

End Sub

Public Sub closeSmartCard
	Dim slot(1) As Byte
	slot(0) = slotNumber
	
	'return 
	' 0 Initialization success.
	' (-2403) Channel Error
	' (-2405) The card Is pulled out Or Not
	' (-2404) Protocol error
	' (-2500) Voltage mode error of IC card reset
	' (-2503) Communication failure.
	Dim resposeicc As Int = SmartCardClass.RunMethod("IccClose", Array(slot(0)))
	errors(resposeicc)
	slotFinded = False
	Return resposeicc
End Sub

Public Sub closeForceSmartCard(slot1)
	Dim slot(1) As Byte
	slot(0) = slot1
	
	'return 
	' 0 Initialization success.
	' (-2403) Channel Error
	' (-2405) The card Is pulled out Or Not
	' (-2404) Protocol error
	' (-2500) Voltage mode error of IC card reset
	' (-2503) Communication failure.
	Dim resposeicc As Int = SmartCardClass.RunMethod("IccClose", Array(slot(0)))
	errors(resposeicc)
	slotFinded = False
	Return resposeicc
End Sub

'Public Sub chackCard
'	Dim buffer(6) As Byte
'	buffer(0) = 0
'	buffer(1) = 0x84
'	buffer(2) = 0
'	buffer(3) = 0
'	buffer(4) = 0
'	buffer(5) = 4
'	commandSmartCard(buffer)
'End Sub


Public Sub ScardApplet

	Dim cmd(4) As Byte
	Dim Data(16) As Byte
	Dim Lc As Byte
	Dim Le As String
	
	cmd(0) = 0x00
	cmd(1) = 0xa4
	cmd(2) = 0x04
	cmd(3) = 0x00
	
	Data(0) = 0xA0
	Data(1) = 0x00
	Data(2) = 0x00
	Data(3) = 0x07
	Data(4) = 0x48
	Data(5) = 0x46
	Data(6) = 0x4A
	Data(7) = 0x49
	Data(8) = 0x2D
	Data(9) = 0x54
	Data(10) = 0x61
	Data(11) = 0x78
	Data(12) = 0x43
	Data(13) = 0x6F
	Data(14) = 0x72
	Data(15) = 0x65
	Lc  = Data.Length
    Le = "00"

''	BuildCommandArray(class,Instruction,P1P2,Data,Le)
	commandSmartCard(cmd, Lc, Data, Le)
	
End Sub

Public Sub VerifyPin(Pin As String)
	Dim Cmd(6+Pin.Length) As Byte
	Dim class(1) As Byte
	Dim Instruction(1) As Byte
	Dim P1P2(2) As Byte
	Dim Data(Pin.Length) As Byte
	Dim Le(1) As Byte
	class(0) = 0x88
	
	Instruction(0) = 0x11
	
	P1P2(0) = 0x04
	P1P2(1) = 0x00
	For i = 0 To Pin.Length -1
		Dim tmpint As Int = Pin.SubString2(i,i+1)
		Data(i) = tmpint
	Next
	
	Le(0) = 0x00
	BuildCommandArray(class,Instruction,P1P2,Data,Le)
	
''	commandSmartCard(bytesToHexString(tmpBuffer))

End Sub

Public Sub BuildCommandArray(class() As Byte, Instruction() As Byte, P1P2() As Byte, Data() As Byte, Le() As Byte)
	Dim arrayLen As Int = class.Length + Instruction.Length + P1P2.Length + Data.Length + Le.Length+1
	Dim tmpBuffer(arrayLen) As Byte
	tmpBuffer(0) = class(0)
	tmpBuffer(1) = Instruction(0)
	tmpBuffer(2) = P1P2(0)
	tmpBuffer(3) = P1P2(1)
	For ki = 0 To Data.Length-1
	   tmpBuffer(4+ki) = Data(ki)
	Next
	tmpBuffer(4+Data.Length) = Data.Length
	For ki = 0 To Le.Length-1
	   tmpBuffer(5+Data.Length) = Le(ki)
	Next
	Return
End Sub

Public Sub BuildCommandArray1(class() As Byte, Instruction() As Byte, P1P2() As Byte, Data() As Byte, Le() As Byte)
	Dim arrayLen As Int = class.Length + Instruction.Length + P1P2.Length + Data.Length + Le.Length+1
	Dim tmpBuffer(arrayLen) As Byte
	tmpBuffer(0) = class(0)
	tmpBuffer(1) = Instruction(0)
	tmpBuffer(2) = P1P2(0)
	tmpBuffer(3) = P1P2(1)
	tmpBuffer(4) = Data.Length
	For ki = 0 To Data.Length-1
	   tmpBuffer(5+ki) = Data(ki)
	Next
	For ki = 0 To Le.Length-1
	   tmpBuffer(5+Data.Length) = Le(ki)
	Next
	Return
End Sub


Public Sub bytesToHexString(data() As Byte) As String
	Dim bc As ByteConverter
	Return bc.HexFromBytes(data)
End Sub


Public Sub getBytesFromString(data As String, encoding As String) As Byte
	Return StringUtil.RunMethod("getBytesFromString", Array(data))
End Sub


Public Sub hexStringToBytes(hexstr As String) As Byte
	Log(hexstr)
	Dim tmpByte() As Byte = StringUtil.RunMethod("hexStringToBytes", Array(hexstr))
	Return
End Sub


Public Sub ConvertBytesToArray (Bytes() As Byte) As Object()
	If Bytes.Length = 0 Or Bytes(0) <> MARK_BEGIN Or Bytes(Bytes.Length - 1) <> MARK_END Then
		Log("Invalid input!")
		Return Array()
	End If
	raf.Initialize3(Bytes, True)
	raf.CurrentPosition = 1
	Dim length As Int = raf.ReadUnsignedByte(raf.CurrentPosition)
	Dim result(length) As Object
	For i = 0 To length - 1
		Dim recordType As Int = raf.ReadUnsignedByte(raf.CurrentPosition)
		Dim o As Object
		Select recordType
			Case T_UINT_1
				o = raf.ReadUnsignedByte(raf.CurrentPosition)
			Case T_UINT_2
				o = Bit.And(0xFFFF, raf.ReadShort(raf.CurrentPosition))
			Case T_UINT_4
				Dim lng As Long = raf.ReadInt(raf.CurrentPosition)
				'convert the signed int to unsigned long
				If lng < 0 Then lng = lng + 4294967296
				o = lng
			Case T_INT_4
				o = raf.ReadInt(raf.CurrentPosition)
			Case T_NINT_1
				o = Bit.Or(0xFFFFFF00, raf.ReadSignedByte(raf.CurrentPosition))
			Case T_NINT_2
				o = Bit.Or(0xFFFF0000, raf.ReadShort(raf.CurrentPosition))
			Case T_FLOAT_4
				o = raf.ReadFloat(raf.CurrentPosition)
			Case T_STRING
				For b = raf.CurrentPosition To Bytes.Length - 1
					If Bytes(b) = 0 Then Exit
				Next
				o = BytesToString(Bytes, raf.CurrentPosition, b - raf.CurrentPosition, "utf8")
				raf.CurrentPosition = b + 1
			Case T_LONG_BYTES
				Dim bb(Bit.And(0xFFFF, raf.ReadShort(raf.CurrentPosition))) As Byte
				raf.ReadBytes(bb, 0, bb.Length, raf.CurrentPosition)
				o = bb
			Case Else
				If Bit.And(recordType, T_SMALL_UINT) = T_SMALL_UINT Then
					o = Bit.And(recordType, T_SMALL_UINT - 1)
				Else If Bit.And(recordType, T_SHORT_BYTES) = T_SHORT_BYTES Then
					Dim bb (Bit.And(recordType, T_SHORT_BYTES - 1)) As Byte
					raf.ReadBytes(bb, 0, bb.Length, raf.CurrentPosition)
					o = bb
				Else
					Log("Invalid field: " & recordType)
				End If
		End Select
		result(i) = o
	Next
	Return result
End Sub






Private Sub errors(errornum As Int)
	
	If errornum = 0 Then errorString = "successfully"
	
	'IccOpen
	If errornum = -2403  Then errorString = "Channel Error"
	If errornum = -2405  Then errorString = "The card Is pulled out Or Not"
	If errornum = -2404  Then errorString = "Protocol error"
	If errornum = -2500  Then errorString = "Voltage mode error of IC card reset"
	If errornum = -2503  Then errorString = "Communication failure."
	'IccCommand
	If errornum = -2503 Then errorString = "Communication timeout"
	If errornum = -2405 Then errorString = "The cards are put out in the transaction"
	If errornum = -2401 Then errorString = "Parity error"
	If errornum = -2403 Then errorString = "Select Channel error"
	If errornum = -2400 Then errorString = "Sending data too long（LC)"
	If errornum = -2404 Then errorString = "The Protocol error（is Not T = 0 Or T = 1)"
	If errornum = -2406 Then errorString = "No reset card"
	
	'Print
	If errornum = -4001 Then errorString = "PRINT BUSY"
	If errornum = -4002 Then errorString = "PRINT NOPAPER"
	If errornum = -4003 Then errorString = "PRINT DATAERR"
	If errornum = -4004 Then errorString = "PRINT FAULT"
	If errornum = -4005 Then errorString = "PRINT TOOHEAT"
	If errornum = -4006 Then errorString = "PRINT UNFINISHED"
	If errornum = -4007 Then errorString = "PRINT NOFONTLIB"
	If errornum = -4008 Then errorString = "PRINT BUFFOVERFLOW"
	If errornum = -4009 Then errorString = "PRINT SETFONTERR"
	If errornum = -4010 Then errorString = "PRINT GETFONTERR"
	
	'BMP
	If errornum = -4003 Then errorString = "PRN_DATAERR"
	If errornum = -4004 Then errorString = "PRN_FAULT"
	If errornum = -4008 Then errorString = "PRN_BUFFOVERFLOW"
	
	
	'PrintStart
	If errornum = -1001 Or errornum = 1001 Then errorString = "send fail"
	If errornum = -1002 Or errornum = 1002 Then errorString = "receive timeout"
	If errornum = -1 Then errorString = "Short of paper"
	If errornum = -2 Then errorString = "The temperature Is too high"
	If errornum = -3 Then errorString = "The voltage Is too low"
	If errornum = 8/9 Then errorString = "Instruction reply disorder"
	If errornum = -1023 Then errorString = "status error"
	If errornum = -1021 Then errorString = "Short of paper"
	If errornum = -1000 Or errornum = -1016 Or errornum = -1001 Or errornum = -1002 Or errornum = -1003 Or errornum = -1004 Or errornum = -1019 Or errornum = -1017 Or errornum = -1018 Or errornum = -1020 Then errorString = "print timeout"
	If errornum = -1007 Or errornum = -1008 Or errornum = -1009 Or errornum = -1010 Or errornum = -1011 Or errornum = -1012 Then errorString = "Print times exceeds limit"
	If errornum = -1022 Then errorString = "heat error"
	If errornum = -1015 Or errornum = -1014 Then errorString = "Short of paper"

	Log("error = "  & errorString)
	errorValue = errornum
	
End Sub
