Skip to content

Commit

Permalink
Merge branch 'master' into Add-CC13x4-Platform-Support-1
Browse files Browse the repository at this point in the history
  • Loading branch information
adabreuti authored May 12, 2023
2 parents 6e8737e + fcf58d3 commit 099255e
Show file tree
Hide file tree
Showing 22 changed files with 986 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object ChipClient {
if (!this::androidPlatform.isInitialized && context != null) {
//force ChipDeviceController load jni
ChipDeviceController.loadJni()
androidPlatform = AndroidChipPlatform(AndroidBleManager(), PreferencesKeyValueStoreManager(context), PreferencesConfigurationManager(context), NsdManagerServiceResolver(context), NsdManagerServiceBrowser(context), ChipMdnsCallbackImpl(), DiagnosticDataProviderImpl(context))
androidPlatform = AndroidChipPlatform(AndroidBleManager(context), PreferencesKeyValueStoreManager(context), PreferencesConfigurationManager(context), NsdManagerServiceResolver(context), NsdManagerServiceBrowser(context), ChipMdnsCallbackImpl(), DiagnosticDataProviderImpl(context))
}

return androidPlatform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ class Argument {
isValidArgument = numLong.toInt() >= minValue && numLong.toInt() <= maxValue
}

ArgumentType.STRING -> {
val stringBuffer = this.value as StringBuffer
stringBuffer.append(value)
val str = stringBuffer.toString()
isValidArgument = value == str
}

ArgumentType.BOOL -> {
val atomicBoolean = this.value as AtomicBoolean
try {
atomicBoolean.set(value.toBoolean())
isValidArgument = true
} catch (e: Exception) {
isValidArgument = false
}
}

ArgumentType.ADDRESS -> isValidArgument = try {
val ipAddress = this.value as IPAddress
ipAddress.setAddress(InetAddress.getByName(value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@ import com.matter.controller.commands.common.CredentialsIssuer

class PairCodeCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) :
PairingCommand(controller, "code", credsIssue, PairingModeType.CODE, PairingNetworkType.NONE) {
override fun runCommand() {}
override fun runCommand() {
currentCommissioner()
.pairDeviceWithCode(
getNodeId(),
getOnboardingPayload(),
null,
getWifiNetworkCredentials(),
)
currentCommissioner().setCompletionListener(this)
waitCompleteMs(getTimeoutMillis())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@ import com.matter.controller.commands.common.CredentialsIssuer

class PairCodeThreadCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) :
PairingCommand(controller, "code-thread", credsIssue, PairingModeType.CODE, PairingNetworkType.THREAD) {
override fun runCommand() {}
override fun runCommand() {
currentCommissioner()
.pairDeviceWithCode(
getNodeId(),
getOnboardingPayload(),
null,
getThreadNetworkCredentials(),
)
currentCommissioner().setCompletionListener(this)
waitCompleteMs(getTimeoutMillis())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@ import com.matter.controller.commands.common.CredentialsIssuer

class PairCodeWifiCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) :
PairingCommand(controller, "code-wifi", credsIssue, PairingModeType.CODE, PairingNetworkType.WIFI) {
override fun runCommand() {}
override fun runCommand() {
currentCommissioner()
.pairDeviceWithCode(
getNodeId(),
getOnboardingPayload(),
null,
getWifiNetworkCredentials(),
)
currentCommissioner().setCompletionListener(this)
waitCompleteMs(getTimeoutMillis())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.matter.controller.commands.pairing

import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.NetworkCredentials
import com.matter.controller.commands.common.CredentialsIssuer
import com.matter.controller.commands.common.IPAddress
import com.matter.controller.commands.common.MatterCommand
Expand Down Expand Up @@ -227,6 +228,22 @@ abstract class PairingCommand(
return timeoutMillis.get()
}

fun getOnboardingPayload(): String {
return onboardingPayload.toString()
}

fun getWifiNetworkCredentials(): NetworkCredentials {
return NetworkCredentials.forWiFi(NetworkCredentials.WiFiCredentials(ssid.toString(), password.toString()))
}

fun getThreadNetworkCredentials(): NetworkCredentials {
return NetworkCredentials.forThread(NetworkCredentials.ThreadCredentials(operationalDataset.toString().hexToByteArray()))
}

private fun String.hexToByteArray(): ByteArray {
return chunked(2).map { byteStr -> byteStr.toUByte(16).toByte() }.toByteArray()
}

companion object {
private val logger = Logger.getLogger(PairingCommand::class.java.name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(self, config: TestParserBuilderConfig = TestParserBuilderConfig()):
self.__tests = copy.copy(config.tests)
self.__config = config
self.__duration = 0
self.__done = False
self.done = False

def __iter__(self):
self.__config.hooks.start(len(self.__tests))
Expand All @@ -76,9 +76,9 @@ def __next__(self):
if len(self.__tests):
return self.__get_test_parser(self.__tests.pop(0))

if not self.__done:
if not self.done:
self.__config.hooks.stop(round(self.__duration))
self.__done = True
self.done = True

raise StopIteration

Expand Down
2 changes: 1 addition & 1 deletion scripts/py_matter_yamltests/matter_yamltests/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def run(self, parser_builder_config: TestParserBuilderConfig, runner_config: Tes
duration = round((time.time() - start) * 1000)
runner_config.hooks.stop(duration)

return True
return parser_builder.done

async def _run(self, parser: TestParser, config: TestRunnerConfig):
status = True
Expand Down
36 changes: 36 additions & 0 deletions src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,42 @@ JNI_METHOD(void, pairDeviceWithAddress)
}
}

JNI_METHOD(void, pairDeviceWithCode)
(JNIEnv * env, jobject self, jlong handle, jlong deviceId, jstring setUpCode, jbyteArray csrNonce, jobject networkCredentials)
{
chip::DeviceLayer::StackLock lock;
CHIP_ERROR err = CHIP_NO_ERROR;
AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);

ChipLogProgress(Controller, "pairDeviceWithCode() called");

JniUtfString setUpCodeJniString(env, setUpCode);

CommissioningParameters commissioningParams = wrapper->GetCommissioningParameters();
if (csrNonce != nullptr)
{
JniByteArray jniCsrNonce(env, csrNonce);
commissioningParams.SetCSRNonce(jniCsrNonce.byteSpan());
}

if (networkCredentials != nullptr)
{
wrapper->ApplyNetworkCredentials(commissioningParams, networkCredentials);
}

if (wrapper->GetDeviceAttestationDelegateBridge() != nullptr)
{
commissioningParams.SetDeviceAttestationDelegate(wrapper->GetDeviceAttestationDelegateBridge());
}
err = wrapper->Controller()->PairDevice(deviceId, setUpCodeJniString.c_str(), commissioningParams, DiscoveryType::kAll);

if (err != CHIP_NO_ERROR)
{
ChipLogError(Controller, "Failed to pair the device.");
JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err);
}
}

JNI_METHOD(void, establishPaseConnection)(JNIEnv * env, jobject self, jlong handle, jlong deviceId, jint connObj, jlong pinCode)
{
chip::DeviceLayer::StackLock lock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ public void pairDeviceWithAddress(
deviceControllerPtr, deviceId, address, port, discriminator, pinCode, csrNonce);
}

/**
* Pair a device connected using the scanned QR code or manual entry code.
*
* @param deviceId the node ID to assign to the device
* @param setupCode the scanned QR code or manual entry code
* @param csrNonce the 32-byte CSR nonce to use, or null if we want to use an internally randomly
* generated CSR nonce.
* @param networkCredentials the credentials (Wi-Fi or Thread) to be provisioned
*/
public void pairDeviceWithCode(
long deviceId,
String setupCode,
@Nullable byte[] csrNonce,
@Nullable NetworkCredentials networkCredentials) {
pairDeviceWithCode(deviceControllerPtr, deviceId, setupCode, csrNonce, networkCredentials);
}

public void establishPaseConnection(long deviceId, int connId, long setupPincode) {
if (connectionId == 0) {
connectionId = connId;
Expand Down Expand Up @@ -996,6 +1013,13 @@ private native void pairDeviceWithAddress(
long pinCode,
@Nullable byte[] csrNonce);

private native void pairDeviceWithCode(
long deviceControllerPtr,
long deviceId,
String setupCode,
@Nullable byte[] csrNonce,
@Nullable NetworkCredentials networkCredentials);

private native void establishPaseConnection(
long deviceControllerPtr, long deviceId, int connId, long setupPincode);

Expand Down
13 changes: 13 additions & 0 deletions src/darwin/Framework/CHIP/MTRDeviceControllerFactory.mm
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
static NSString * const kErrorDACVerifierInit = @"Init failure while creating the device attestation verifier";
static NSString * const kErrorGroupProviderInit = @"Init failure while initializing group data provider";
static NSString * const kErrorControllersInit = @"Init controllers array failure";
static NSString * const kErrorCertificateValidityPolicyInit = @"Init certificate validity policy failure";
static NSString * const kErrorControllerFactoryInit = @"Init failure while initializing controller factory";
static NSString * const kErrorKeystoreInit = @"Init failure while initializing persistent storage keystore";
static NSString * const kErrorCertStoreInit = @"Init failure while initializing persistent storage operational certificate store";
Expand Down Expand Up @@ -86,6 +87,7 @@ @interface MTRDeviceControllerFactory ()
@property (readonly) MTROperationalBrowser * operationalBrowser;
@property () chip::Credentials::DeviceAttestationVerifier * deviceAttestationVerifier;
@property (readonly) BOOL advertiseOperational;
@property (nonatomic, readonly) Credentials::IgnoreCertificateValidityPeriodPolicy * certificateValidityPolicy;

- (BOOL)findMatchingFabric:(FabricTable &)fabricTable
params:(MTRDeviceControllerStartupParams *)params
Expand Down Expand Up @@ -150,6 +152,11 @@ - (instancetype)init
return nil;
}

_certificateValidityPolicy = new Credentials::IgnoreCertificateValidityPeriodPolicy();
if ([self checkForInitError:(_certificateValidityPolicy != nil) logMsg:kErrorCertificateValidityPolicyInit]) {
return nil;
}

return self;
}

Expand Down Expand Up @@ -204,6 +211,11 @@ - (void)cleanupInitObjects
delete _sessionKeystore;
_sessionKeystore = nullptr;
}

if (_certificateValidityPolicy) {
delete _certificateValidityPolicy;
_certificateValidityPolicy = nullptr;
}
}

- (void)cleanupStartupObjects
Expand Down Expand Up @@ -443,6 +455,7 @@ - (BOOL)startControllerFactory:(MTRDeviceControllerFactoryParams *)startupParams
params.fabricIndependentStorage = _persistentStorageDelegateBridge;
params.operationalKeystore = _keystore;
params.opCertStore = _opCertStore;
params.certificateValidityPolicy = _certificateValidityPolicy;
errorCode = _controllerFactory->Init(params);
if (errorCode != CHIP_NO_ERROR) {
MTR_LOG_ERROR("Error: %@", kErrorControllerFactoryInit);
Expand Down
Loading

0 comments on commit 099255e

Please sign in to comment.