-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial kotlin setuppayload implementation
- Loading branch information
1 parent
2affb17
commit 9ac113b
Showing
8 changed files
with
614 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
Main-Class: com.matter.controller.MainKt | ||
Class-Path: ../lib/third_party/connectedhomeip/src/controller/java/CHIPController.jar ../lib/third_party/connectedhomeip/src/setup_payload/java/SetupPayloadParser.jar ../lib/third_party/connectedhomeip/third_party/java_deps/stub_src/Android.jar ../lib/third_party/connectedhomeip/third_party/java_deps/json-20220924.jar ../lib/third_party/connectedhomeip/third_party/java_deps/jsr305-3.0.2.jar ../lib/third_party/connectedhomeip/third_party/java_deps/kotlin-stdlib-1.8.10.jar | ||
Class-Path: ../lib/third_party/connectedhomeip/src/controller/java/CHIPController.jar ../lib/third_party/connectedhomeip/src/setup_payload/java/SetupPayload.jar ../lib/third_party/connectedhomeip/third_party/java_deps/stub_src/Android.jar ../lib/third_party/connectedhomeip/third_party/java_deps/json-20220924.jar ../lib/third_party/connectedhomeip/third_party/java_deps/jsr305-3.0.2.jar ../lib/third_party/connectedhomeip/third_party/java_deps/kotlin-stdlib-1.8.10.jar | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
src/controller/java/src/chip/setuppayload/DiscoveryCapability.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package chip.setuppayload | ||
|
||
/** | ||
* Enum values for possible bits in the onboarding paylod's discovery capabilities bitmask. | ||
*/ | ||
enum class DiscoveryCapability { | ||
SOFT_AP, | ||
BLE, | ||
ON_NETWORK | ||
} |
17 changes: 17 additions & 0 deletions
17
src/controller/java/src/chip/setuppayload/OptionalQRCodeInfo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package chip.setuppayload | ||
|
||
class OptionalQRCodeInfo { | ||
enum class OptionalQRCodeInfoType { | ||
TYPE_UNKNOWN, | ||
TYPE_STRING, | ||
TYPE_INT32, | ||
TYPE_INT64, | ||
TYPE_UINT32, | ||
TYPE_UINT64 | ||
} | ||
|
||
var tag = 0 | ||
var type: OptionalQRCodeInfoType? = null | ||
var data: String? = null | ||
var int32 = 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package chip.setuppayload | ||
|
||
/** Class to hold the data from the scanned QR code or manual entry code. */ | ||
class SetupPayload( | ||
/** Version info of the SetupPayload: version SHALL be 0 */ | ||
var version: Int = 0, | ||
|
||
/** The CHIP device vendor ID: Vendor ID SHALL be between 1 and 0xFFF4. */ | ||
var vendorId: Int = 0, | ||
|
||
/** The CHIP device product ID: Product ID SHALL BE greater than 0. */ | ||
var productId: Int = 0, | ||
|
||
/** Commissioning flow: 0 = standard, 1 = requires user action, 2 = custom */ | ||
var commissioningFlow: Int = 0, | ||
|
||
/** | ||
* The CHIP device supported rendezvous flags: At least one DiscoveryCapability must be included. | ||
*/ | ||
var discoveryCapabilities: Set<DiscoveryCapability> = emptySet(), | ||
|
||
/** The CHIP device discriminator: */ | ||
var discriminator: Int = 0, | ||
|
||
/** | ||
* If hasShortDiscriminator is true, the discriminator value contains just the high 4 bits of the | ||
* full discriminator. For example, if hasShortDiscriminator is true and discriminator is 0xA, | ||
* then the full discriminator can be anything in the range 0xA00 to 0xAFF. | ||
*/ | ||
var hasShortDiscriminator: Boolean = false, | ||
|
||
/** | ||
* The CHIP device setup PIN code: setupPINCode SHALL be greater than 0. Also invalid setupPINCode | ||
* is {000000000, 11111111, 22222222, 33333333, 44444444, 55555555, 66666666, 77777777, 88888888, | ||
* 99999999, 12345678, 87654321}. | ||
*/ | ||
var setupPinCode: Long = 0 | ||
) { | ||
var optionalQRCodeInfo: HashMap<Int, OptionalQRCodeInfo> | ||
|
||
init { | ||
optionalQRCodeInfo = HashMap() | ||
} | ||
|
||
constructor( | ||
version: Int, | ||
vendorId: Int, | ||
productId: Int, | ||
commissioningFlow: Int, | ||
discoveryCapabilities: Set<DiscoveryCapability>, | ||
discriminator: Int, | ||
setupPinCode: Long | ||
) : this( | ||
version, | ||
vendorId, | ||
productId, | ||
commissioningFlow, | ||
discoveryCapabilities, | ||
discriminator, | ||
false, | ||
setupPinCode | ||
) | ||
|
||
fun addOptionalQRCodeInfo(info: OptionalQRCodeInfo) { | ||
optionalQRCodeInfo[info.tag] = info | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
src/controller/java/src/chip/setuppayload/SetupPayloadParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package chip.setuppayload | ||
|
||
import java.util.logging.Level | ||
import java.util.logging.Logger | ||
|
||
/** Parser for scanned QR code or manual entry code. */ | ||
class SetupPayloadParser { | ||
/** | ||
* Returns [SetupPayload] parsed from the QR code string. If an invalid element is included | ||
* in the QRCode Parse result, SetupPayloadException occurs. Refer to [SetupPayload] for the | ||
* description of the invalid element. | ||
*/ | ||
@Throws(UnrecognizedQrCodeException::class, SetupPayloadException::class) | ||
fun parseQrCode(qrCodeString: String): SetupPayload { | ||
return fetchPayloadFromQrCode(qrCodeString, false) | ||
} | ||
|
||
/** | ||
* Returns [SetupPayload] parsed from the QR code string. | ||
* | ||
* @param qrCodeString the QRCode for commissioning device. | ||
* @param allowInvalidPayload Allow invalid payload components. If this value is true, payload | ||
* element validation is not checked. Consider saying that the payload must still parse | ||
* correctly, but this skips validation of the content past parsing (i.e. it does not validate | ||
* ranges for individual elements). Refer to [SetupPayload] for the description of the | ||
* invalid element. | ||
*/ | ||
@Throws(UnrecognizedQrCodeException::class, SetupPayloadException::class) | ||
fun parseQrCode(qrCodeString: String, allowInvalidPayload: Boolean): SetupPayload { | ||
return fetchPayloadFromQrCode(qrCodeString, allowInvalidPayload) | ||
} | ||
|
||
/** | ||
* Returns [SetupPayload] parsed from the manual entry code string. If an SetupPINCode has | ||
* invalid value, SetupPayloadException occurs. Refer to [SetupPayload] for the description | ||
* of the invalid element. | ||
*/ | ||
@Throws(InvalidEntryCodeFormatException::class, SetupPayloadException::class) | ||
fun parseManualEntryCode(entryCodeString: String): SetupPayload { | ||
return fetchPayloadFromManualEntryCode(entryCodeString, false) | ||
} | ||
|
||
/** | ||
* Returns [SetupPayload] parsed from the manual entry code string. | ||
* | ||
* @param entryCodeString the manual Pairing Code for commissioning device. | ||
* @param allowInvalidPayload Allow invalid payload components. If this value is true, payload | ||
* element validation is not checked. Consider saying that the payload must still parse | ||
* correctly, but this skips validation of the content past parsing (i.e. it does not validate | ||
* ranges for individual elements). Refer to [SetupPayload] for the description of the | ||
* invalid element. | ||
*/ | ||
@Throws(InvalidEntryCodeFormatException::class, SetupPayloadException::class) | ||
fun parseManualEntryCode(entryCodeString: String, allowInvalidPayload: Boolean): SetupPayload { | ||
return fetchPayloadFromManualEntryCode(entryCodeString, allowInvalidPayload) | ||
} | ||
|
||
/** Get QR code string from [SetupPayload]. */ | ||
@Throws(SetupPayloadException::class) | ||
external fun getQrCodeFromPayload(payload: SetupPayload): String? | ||
|
||
/** Get manual entry code string from [SetupPayload]. */ | ||
@Throws(SetupPayloadException::class) | ||
external fun getManualEntryCodeFromPayload(payload: SetupPayload): String? | ||
|
||
@Throws(UnrecognizedQrCodeException::class, SetupPayloadException::class) | ||
private external fun fetchPayloadFromQrCode( | ||
qrCodeString: String, isAllowInvalidPayload: Boolean | ||
): SetupPayload | ||
|
||
@Throws(InvalidEntryCodeFormatException::class, SetupPayloadException::class) | ||
private external fun fetchPayloadFromManualEntryCode( | ||
entryCodeString: String, isAllowInvalidPayload: Boolean | ||
): SetupPayload | ||
|
||
class UnrecognizedQrCodeException(qrCode: String) : | ||
Exception(String.format("Invalid QR code string: %s", qrCode), null) { | ||
companion object { | ||
private const val serialVersionUID = 1L | ||
} | ||
} | ||
|
||
class InvalidEntryCodeFormatException(entryCode: String) : | ||
Exception(String.format("Invalid format for entry code string: %s", entryCode), null) { | ||
companion object { | ||
private const val serialVersionUID = 1L | ||
} | ||
} | ||
|
||
class SetupPayloadException(var errorCode: Int, message: String?) : | ||
Exception(message ?: String.format("Error Code %d", errorCode)) { | ||
companion object { | ||
private const val serialVersionUID = 1L | ||
} | ||
} | ||
|
||
companion object { | ||
private val LOGGER: Logger = Logger.getLogger(SetupPayloadParser::class.java.getSimpleName()) | ||
|
||
init { | ||
try { | ||
System.loadLibrary("SetupPayloadParser") | ||
} catch (e: UnsatisfiedLinkError) { | ||
LOGGER.log(Level.SEVERE, "Cannot load library.", e) | ||
} | ||
} | ||
} | ||
} |