commit 10/04/2025
This commit is contained in:
@@ -3,10 +3,8 @@ package SecureDongle;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.tinylog.Logger;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static Config.SomeCodes.ToShort;
|
||||
import static Config.SomeCodes.Wait;
|
||||
|
||||
public class SecureDongle {
|
||||
|
||||
@@ -108,6 +106,7 @@ public class SecureDongle {
|
||||
* Close SecureDongle
|
||||
* @return true if success
|
||||
*/
|
||||
@SuppressWarnings("UnusedReturnValue")
|
||||
public boolean Close(){
|
||||
handle[0] = Handle;
|
||||
short result = SD.SecureDongle(LibSecureDongle.SD_CLOSE, handle, lp1, lp2, p1, p2, p3, p4, buffer);
|
||||
@@ -118,12 +117,40 @@ public class SecureDongle {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to User Data Zone (UDZ)
|
||||
* @param StartAddress start address of UDZ, zero based
|
||||
* @param length length of data to write, max 1000 bytes
|
||||
* @param data data to write
|
||||
* @return true if success
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public boolean Write(short StartAddress, short length, byte[] data){
|
||||
if (Opened){
|
||||
handle[0] = Handle;
|
||||
p1[0] = StartAddress>=0 ? StartAddress : 0;
|
||||
if (length<1) length=1;
|
||||
if (length>1000) length=1000;
|
||||
p2[0] = length;
|
||||
System.arraycopy(data, 0, buffer, 0, length);
|
||||
short result = SD.SecureDongle(LibSecureDongle.SD_WRITE, handle, lp1, lp2, p1, p2, p3, p4, buffer);
|
||||
if (result== LibSecureDongle.ERR_SUCCESS){
|
||||
//System.out.println("SecureDongle HardwareID="+HardwareID+" write success ");
|
||||
return true;
|
||||
} else if (event!=null) event.onDongleError("Write", result);
|
||||
} //else System.out.println("SecureDongle not opened");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Read from User Data Zone (UDZ)
|
||||
* @param StartAddress Start Address, zero based
|
||||
* @param Length Length of data to read, max 1000 bytes
|
||||
* @return byte array of data, length=0 if failed
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public byte[] Read(short StartAddress, short Length){
|
||||
if (Opened){
|
||||
handle[0] = Handle;
|
||||
@@ -143,35 +170,11 @@ public class SecureDongle {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to User Data Zone (UDZ)
|
||||
* @param StartAddress start address of UDZ, zero based
|
||||
* @param length length of data to write, max 1000 bytes
|
||||
* @param data data to write
|
||||
* @return true if success
|
||||
*/
|
||||
public boolean Write(short StartAddress, short length, byte[] data){
|
||||
if (Opened){
|
||||
handle[0] = Handle;
|
||||
p1[0] = StartAddress>=0 ? StartAddress : 0;
|
||||
if (length<1) length=1;
|
||||
if (length>1000) length=1000;
|
||||
p2[0] = length;
|
||||
System.arraycopy(data, 0, buffer, 0, length);
|
||||
short result = SD.SecureDongle(LibSecureDongle.SD_WRITE, handle, lp1, lp2, p1, p2, p3, p4, buffer);
|
||||
if (result== LibSecureDongle.ERR_SUCCESS){
|
||||
//System.out.println("SecureDongle HardwareID="+HardwareID+" write success ");
|
||||
return true;
|
||||
} else if (event!=null) event.onDongleError("Write", result);
|
||||
} //else System.out.println("SecureDongle not opened");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Random Number
|
||||
* @return short array of random number
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public short[] GenerateRandomNumber(){
|
||||
short[] random = new short[4];
|
||||
if (Opened){
|
||||
@@ -195,6 +198,7 @@ public class SecureDongle {
|
||||
* @param UserID UserID to write
|
||||
* @return true if success
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public boolean WriteUserID(int UserID){
|
||||
if (Opened){
|
||||
handle[0] = Handle;
|
||||
@@ -242,7 +246,7 @@ public class SecureDongle {
|
||||
* if dongle missing, will raise event onDongleMissing
|
||||
*/
|
||||
public void StartMonitor(){
|
||||
new Thread(()->{
|
||||
Thread tx = new Thread(()->{
|
||||
if (HardwareID==0) Find();
|
||||
Open();
|
||||
int firstUserID = ReadUserID();
|
||||
@@ -251,11 +255,7 @@ public class SecureDongle {
|
||||
ismonitoring = true;
|
||||
Logger.info("Start Monitoring UserID="+Integer.toHexString(firstUserID));
|
||||
while (ismonitoring){
|
||||
try {
|
||||
Thread.sleep(5000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Wait(5000);
|
||||
Open();
|
||||
int newUserID = ReadUserID();
|
||||
Close();
|
||||
@@ -267,7 +267,9 @@ public class SecureDongle {
|
||||
System.out.println("Stop Monitoring");
|
||||
} else System.out.println("Canceled Monitoring, UserID not found");
|
||||
|
||||
}).start();
|
||||
});
|
||||
tx.setDaemon(true);
|
||||
tx.start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user