Skip to content

Commit 2dd296a

Browse files
author
Ahmed Mones
committed
AL-225: support micro usb fingerprint
1 parent e6808fb commit 2dd296a

File tree

1 file changed

+67
-42
lines changed

1 file changed

+67
-42
lines changed

fingerprint/src/main/java/com/fingerprint/scanner/HfSecurityFingerprintScanner.kt

Lines changed: 67 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ import com.fingerprint.utils.returnUnit
3232
internal class HfSecurityFingerprintScanner(
3333
private val usbDeviceCommunicator: UsbDeviceCommunicator
3434
) : FingerprintScanner {
35-
private var deviceType: Int = 0
35+
var deviceType: DeviceType = DeviceType.Unknown
36+
private set
3637
private var imageType: ScannedImageType = ScannedImageType.Normal
3738
private var device: UsbDevice? = null
39+
3840
override val deviceInfo: FingerprintDeviceInfo
3941
get() = FingerprintDeviceInfo(
42+
model = deviceType.name,
4043
vendorId = device?.vendorId,
4144
productId = device?.productId,
42-
model = if (deviceType in 1..2) "HF4000" else "Unknown",
4345
product = device?.productName.removeQuestionMark(),
4446
manufacturer = device?.manufacturerName.removeQuestionMark()
4547
)
@@ -89,10 +91,14 @@ internal class HfSecurityFingerprintScanner(
8991
getBrightness().also { Log.i("DEBUGGING", "BRIGHTNESS -> $it") } in MIN_CLEAN_REQUIRED_BRIGHTNESS_THRESHOLD..MAX_CLEAN_REQUIRED_BRIGHTNESS_THRESHOLD
9092

9193
override fun captureImage(imageType: ScannedImageType): Boolean = runCatching {
92-
this.imageType = imageType
93-
val command = ByteArray(10).apply { this[0] = imageType.captureCommand }
94+
this.imageType = if (deviceType == DeviceType.HF4000_V1)
95+
ScannedImageType.Normal
96+
else
97+
imageType
98+
9499
val sendData = ByteArray(MAX_PACKAGE_SIZE)
95100
val receiveData = ByteArray(MAX_PACKAGE_SIZE)
101+
val command = ByteArray(10).also { array -> array[0] = this.imageType.captureCommand }
96102

97103
fillPackage(sendData, FILL_PACKAGE_COMMAND.toInt(), 1, command)
98104

@@ -106,30 +112,38 @@ internal class HfSecurityFingerprintScanner(
106112

107113
private fun initializeUsbDeviceType(device: UsbDevice): Boolean {
108114
deviceType = when {
109-
(device.vendorId == 1107) && (device.productId == 36869) -> 0
110-
(device.vendorId in listOf(8201, 8457)) && (device.productId == 30264) -> 1
111-
(device.vendorId == 1155) && (device.productId == 22304) -> 2
115+
(device.vendorId == 1107) && (device.productId == 36869) -> DeviceType.OtherTypes
116+
device.vendorId in listOf(
117+
8201,
118+
8457
119+
) && (device.productId == 30264) -> DeviceType.HF4000_V1
120+
121+
(device.vendorId == 1155) && (device.productId == 22304) -> DeviceType.HF4000_V2
112122
else -> return false
113123
}
114124
return true
115125
}
116126

117127
private fun sendUsbData(dataBuffer: ByteArray, length: Int): Int = when (deviceType) {
118-
0 -> {
119-
val buffer = ByteArray(10)
120-
usbDeviceCommunicator.sendUsbControlMessage(
121-
request = 0,
122-
value = length,
123-
buffer = buffer
124-
)
125-
usbDeviceCommunicator.writeUsbDevice(dataBuffer, length, TIMEOUT)
126-
}
127-
// HF4000
128-
1, 2 -> sendUsbDataForTypeOneAndTwo(length, dataBuffer)
128+
DeviceType.HF4000_V1,
129+
DeviceType.HF4000_V2 -> sendUsbDataForHF4000(length, dataBuffer)
130+
131+
DeviceType.OtherTypes -> sendUsbDataForOtherTypes(length, dataBuffer)
132+
129133
else -> RETURN_FAIL
130134
}
131135

132-
private fun sendUsbDataForTypeOneAndTwo(length: Int, dataBuffer: ByteArray): Int {
136+
private fun sendUsbDataForOtherTypes(length: Int, dataBuffer: ByteArray): Int {
137+
val buffer = ByteArray(10)
138+
usbDeviceCommunicator.sendUsbControlMessage(
139+
request = 0,
140+
value = length,
141+
buffer = buffer
142+
)
143+
return usbDeviceCommunicator.writeUsbDevice(dataBuffer, length, TIMEOUT)
144+
}
145+
146+
private fun sendUsbDataForHF4000(length: Int, dataBuffer: ByteArray): Int {
133147
val commandStatusWrapper = ByteArray(CSW_LENGTH)
134148
val commandBlockWrapper = createCommandBlockWrapper(
135149
length = length,
@@ -155,7 +169,7 @@ internal class HfSecurityFingerprintScanner(
155169
) return RETURN_FAIL
156170
commandStatusWrapper[3] = 0x43
157171

158-
if (deviceType == 1)
172+
if (deviceType == DeviceType.HF4000_V1)
159173
for (i in 0..<12)
160174
if (commandStatusWrapper[i] != commandBlockWrapper[i])
161175
return RETURN_FAIL
@@ -166,24 +180,27 @@ internal class HfSecurityFingerprintScanner(
166180
dataBuffer: ByteArray,
167181
length: Int,
168182
timeout: Int = TIMEOUT
169-
): Boolean =
170-
when (deviceType) {
171-
0 -> {
172-
val buffer = ByteArray(10)
173-
usbDeviceCommunicator.sendUsbControlMessage(
174-
request = 1,
175-
value = length,
176-
buffer = buffer
177-
)
183+
): Boolean = when (deviceType) {
184+
DeviceType.HF4000_V1,
185+
DeviceType.HF4000_V2 -> receiveUsbDataForHF4000(length, timeout, dataBuffer)
178186

179-
usbDeviceCommunicator.readUsbBulkData(dataBuffer, length, TIMEOUT) >= 0
180-
}
187+
DeviceType.OtherTypes -> receiveUsbDataForOtherTypes(length, dataBuffer)
181188

182-
1, 2 -> receiveUsbDataForTypeOneAndTwo(length, timeout, dataBuffer)
183-
else -> false
184-
}
189+
else -> false
190+
}
191+
192+
private fun receiveUsbDataForOtherTypes(length: Int, dataBuffer: ByteArray): Boolean {
193+
val buffer = ByteArray(10)
194+
usbDeviceCommunicator.sendUsbControlMessage(
195+
request = 1,
196+
value = length,
197+
buffer = buffer
198+
)
185199

186-
private fun receiveUsbDataForTypeOneAndTwo(
200+
return usbDeviceCommunicator.readUsbBulkData(dataBuffer, length, TIMEOUT) >= 0
201+
}
202+
203+
private fun receiveUsbDataForHF4000(
187204
length: Int,
188205
timeout: Int,
189206
dataBuffer: ByteArray
@@ -207,7 +224,7 @@ internal class HfSecurityFingerprintScanner(
207224
|| commandStatusWrapper[CSW_STATUS_INDEX] != EMPTY_BYTE
208225
) return false
209226

210-
if (deviceType == 1)
227+
if (deviceType == DeviceType.HF4000_V1)
211228
for (i in 4..<8)
212229
if (commandStatusWrapper[i] != commandBlockWrapper[i])
213230
return false
@@ -216,7 +233,7 @@ internal class HfSecurityFingerprintScanner(
216233

217234
private fun receiveUsbImage(dataBuffer: ByteArray, length: Int): Boolean {
218235
when (deviceType) {
219-
0 -> {
236+
DeviceType.OtherTypes -> {
220237
val buffer = ByteArray(10)
221238
val n = 8
222239
val len = length / n
@@ -238,8 +255,8 @@ internal class HfSecurityFingerprintScanner(
238255
}
239256
return result >= 0
240257
}
241-
// HF4000
242-
1, 2 -> {
258+
259+
DeviceType.HF4000_V1, DeviceType.HF4000_V2 -> {
243260
var result = false
244261
val n = 8
245262
val len = length / n
@@ -253,8 +270,9 @@ internal class HfSecurityFingerprintScanner(
253270
}
254271
return result
255272
}
273+
274+
else -> return false
256275
}
257-
return false
258276
}
259277

260278
private fun encodeData(
@@ -417,15 +435,22 @@ internal class HfSecurityFingerprintScanner(
417435
return verifyResponsePackage(receiveData)
418436
}
419437

438+
enum class DeviceType {
439+
HF4000_V1,
440+
HF4000_V2,
441+
OtherTypes,
442+
Unknown
443+
}
444+
420445
companion object {
421446
private const val MIN_CLEAN_REQUIRED_BRIGHTNESS_THRESHOLD = 337_000f
422447
private const val MAX_CLEAN_REQUIRED_BRIGHTNESS_THRESHOLD = 475_000f
423448

424449
fun isHfSecurityDevice(vendorId: Int, productId: Int): Boolean = when (vendorId) {
425450
1107 -> productId == 36869
426-
8201 -> productId == 30264
451+
8201 -> productId == 30264 // HF4000 V1 (Micro-USB)
427452
8457 -> productId == 30264
428-
1155 -> productId in listOf(22304, 22240)
453+
1155 -> productId in listOf(22304, 22240) // HF4000 V2 (USB-C)
429454
else -> false
430455
}
431456
}

0 commit comments

Comments
 (0)