Sub Class_Globals
' ************
' JAVA VARS
'*************
Dim uManager As UsbManager
Dim uDevice As UsbDevice
Dim mReader As JavaObject
Dim device As JavaObject
Dim Manager As JavaObject
Dim Reader As JavaObject
Private EventName As String 'ignore
Private CallBack As Object 'ignore
Private openState As Boolean
Dim slotNum As Int
Dim prevState As Int
Dim currState As Int
Dim getState As Int
Dim Unknown As Int
Dim Absent As Int
Dim Present As Int
Dim Undefined As Int
Dim CARD_POWER_DOWN As Int = 0
Dim CARD_COLD_RESET As Int = 1
Dim CARD_WARM_RESET As Int = 2
Dim PROTOCOL_UNDEFINED As Int = 0
Dim PROTOCOL_T0 As Int = 1
Dim PROTOCOL_T1 As Int = 2
Dim PROTOCOL_RAW As Int = 65536
Dim PROTOCOL_TX As Int = 3
Dim PROTOCOL_DEFAULT As Int = -2147483648
Dim PROTOCOL_OPTIMAL As Int = 0
Dim CARD_UNKNOWN As Int = 0
Dim CARD_ABSENT As Int = 1
Dim CARD_PRESENT As Int = 2
Dim CARD_SWALLOWED As Int = 3
Dim CARD_POWERED As Int = 4
Dim CARD_NEGOTIABLE As Int = 5
Dim CARD_SPECIFIC As Int = 6
Dim IOCTL_CCID_ESCAPE As Int = 3500
Dim IOCTL_ACR128_READER_COMMAND As Int = 2079
Dim IOCTL_ACR83_GET_FIRMWARE_VERSION As Int = 2078
Dim IOCTL_ACR83_DISPLAY_LCD_MESSAGE As Int = 2079
Dim IOCTL_ACR83_READ_KEY As Int = 2080
Dim IOCTL_GET_FEATURE_REQUEST As Int = 3400
End Sub
'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(vCallback As Object, vEventName As String)
mReader.InitializeContext
Reader.InitializeContext
device.InitializeContext
uManager.Initialize
EventName = vEventName
CallBack = vCallback
'GetBA
verificaUsbDevice
NewReader
CreateEvent
End Sub
Sub NewReader As JavaObject
Private jo As JavaObject
jo.InitializeContext
Manager= GetContext.RunMethod("getSystemService", Array("usb"))
mReader = jo.InitializeNewInstance ("com.acs.smartcard.Reader",Array As Object (Manager))
Log(mReader)
End Sub
Sub CreateEvent As JavaObject
'Private CallBack As Object
'CallBack=mReader.CreateEvent ("com.acs.smartcard.Reader.OnStateChangeListener", "status", Null)
'Private cObject As Object = mReader.RunMethod("setOnStateChangeListener",Array As Object (CallBack))
End Sub
Sub OpenReader As JavaObject
openState = True
Log(uDevice)
Log(uDevice.IsInitialized)
Log(mReader.IsInitialized)
Log(mReader.RunMethod ("isSupported",Array As Object(uDevice)))
Log(mReader.RunMethod ("isOpened", Null))
'Try
device=mReader.RunMethod ("open",Array As Object(uDevice))
'Catch
' Log(LastException)
'End Try
'device.InitializeContext
Log("open = " & device)
Log("is Open "&device.RunMethod ("isOpened", Null))
Log("get device " & mReader.RunMethod ("getDevice", Null))
Log("get num slots " & mReader.RunMethod ("getNumSlots", Null))
Log("get reader name " & mReader.RunMethod ("getReaderName", Null))
'' Log(mReader.RunMethod ("power", Array(slotNum,CARD_COLD_RESET)))
setProtocol(PROTOCOL_T1)
End Sub
Sub CloseReader As JavaObject
openState = False
mReader.RunMethod ("close",Null)
End Sub
Sub transmit(cmd() As Byte)
Dim recvBuffer() As Byte
Log("slot "& slotNum)
Try
mReader.RunMethod ("transmit",Array As Object(slotNum, cmd, cmd.Length, recvBuffer, recvBuffer.Length))
Catch
Log(LastException)
End Try
Log(bytesToHexString(recvBuffer))
'sendBuffer() As Byte) , sendBufferLength As Int, recvBuffer() As Byte, recvBufferLength As Int) As Byte()
End Sub
Public Sub ScardApplet
Log("Scard Applet")
Dim cmd(23) As Byte
Dim Data1(16) As Byte
Dim Lc As Byte
Dim Le As Int
cmd(0) = 0x00
cmd(1) = 0xa4
cmd(2) = 0x04
cmd(3) = 0x00
cmd(4) = 0x10
cmd(5) = 0xA0
cmd(6) = 0x00
cmd(7) = 0x00
cmd(8) = 0x07
cmd(9) = 0x48
cmd(10) = 0x46
cmd(11) = 0x4A
cmd(12) = 0x49
cmd(13) = 0x2D
cmd(14) = 0x54
cmd(15) = 0x61
cmd(16) = 0x78
cmd(17) = 0x43
cmd(18) = 0x6F
cmd(19) = 0x72
cmd(20) = 0x65
cmd(21) = 0x65
cmd(22) = 0x00
transmit(cmd)
End Sub
Sub getProtocol As Int
Log("slot "& slotNum)
Log("protocol " & mReader.RunMethod ("getProtocol",Array (slotNum)))
End Sub
Sub setProtocol(Protocol As Int) As Int
Log("slot "& slotNum)
Try
Log("protocol " & mReader.RunMethod ("setProtocol",Array As Object(slotNum, Protocol)))
Catch
Log(LastException)
Log("set protocol false")
End Try
End Sub
Sub getAtr(slot1 As Int)
Log("slot "& slotNum)
Try
Log("Atr " & mReader.RunMethod ("getAtr",Array As Object(slot1)))
Catch
Log(LastException)
End Try
End Sub
Public Sub getStateReader
Return mReader.RunMethod ("getState", Array As Object (slotNum))
End Sub
Sub status_event(MethodName As String, Args() As Object)
Log("ovde je doslo")
If Not (openState) Then Return
For i = 0 To Args.Length-1
Log(Args(i))
Next
slotNum =Args(0)
prevState =Args(1)
currState =Args(2)
'getState As Object
Unknown =mReader.GetField ("CARD_UNKNOWN")
Absent =mReader.GetField ("CARD_ABSENT")
Present =mReader.GetField ("CARD_PRESENT")
Undefined =mReader.GetField ("PROTOCOL_UNDEFINED")
getState= getStateReader
Log(slotNum)
If currState=Unknown Then
Log("Card Unknown")
End If
If prevState > currState And currState<>Unknown Then
Log ("Card Removed")
Else
If currState<>Unknown Then
Log("Card Inserted")
End If
End If
End Sub
Sub verificaUsbDevice As Object
Dim usbdevices() As UsbDevice
usbdevices = uManager.GetDevices
For i = 0 To usbdevices.Length - 1
' Dim ud As UsbDevice
Dim ud As UsbDevice = usbdevices(i)
Dim inter As UsbInterface
inter = ud.GetInterface(i)
Log("class "& inter.InterfaceClass)
Log("subclass "& inter.InterfaceSubclass)
If inter.InterfaceClass = 11 And inter.InterfaceSubclass = 0 Then
'found our device and interface
''interface = inter
uDevice = ud
Log("Device name " & ud.DeviceName)
Log(ud.ProductId)
Log(ud.VendorId)
'Find correct endpoints
' For b = 0 To interface.EndpointCount - 1
' Dim endpoint As UsbEndpoint
' endpoint = interface.GetEndpoint(b)
' If endpoint.Type = Manager.USB_ENDPOINT_XFER_BULK Then
' If endpoint.Direction = Manager.USB_DIR_IN Then
' inEndpoint = endpoint
' Else If endpoint.Direction = Manager.USB_DIR_OUT Then
' outEndpoint = endpoint
' End If
' End If
' Next
End If
Next
If uManager.HasPermission(uDevice) = False Then
uManager.RequestPermission(uDevice)
End If
Return uDevice
End Sub
Sub GetContext As JavaObject
Return GetBA.GetField("context")
End Sub
Sub GetBA As JavaObject
Dim jo As JavaObject
Dim cls As String = CallBack
cls = cls.SubString("class ".Length)
jo.InitializeStatic(cls)
Return jo.GetFieldJO ("processBA")
End Sub
Public Sub bytesToHexString(data() As Byte) As String
Dim bc As ByteConverter
Return bc.HexFromBytes(data)
End Sub