Skip to content

Commit

Permalink
Remove DeviceAttestationCompletionCallback and DeviceAttestationFailu…
Browse files Browse the repository at this point in the history
…reCallback sub-interfaces from DeviceAttestationDelegate
  • Loading branch information
yufengwangca committed Feb 1, 2023
1 parent 34332ce commit 1c1ea4f
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.AttestationInfo
import chip.devicecontroller.DeviceAttestationDelegate.DeviceAttestationCompletionCallback
import chip.devicecontroller.DeviceAttestationDelegate.DeviceAttestationFailureCallback
import chip.devicecontroller.DeviceAttestationDelegate
import chip.devicecontroller.NetworkCredentials
import com.google.chip.chiptool.NetworkCredentialsParcelable
import com.google.chip.chiptool.ChipClient
Expand Down Expand Up @@ -87,8 +86,40 @@ class DeviceProvisioningFragment : Fragment() {
val port = 5540
val id = DeviceIdUtil.getNextAvailableId(requireContext())
val deviceController = ChipClient.getDeviceController(requireContext())

DeviceIdUtil.setNextAvailableId(requireContext(), id + 1)
deviceController.setCompletionListener(ConnectionCallback())

deviceController.setDeviceAttestationDelegate(DEVICE_ATTESTATION_FAILED_TIMEOUT,
{ devicePtr, attestationInfo, errorCode ->
Log.i(TAG, "Device attestation errorCode: $errorCode, " +
"Look at 'src/credentials/attestation_verifier/DeviceAttestationVerifier.h' " +
"AttestationVerificationResult enum to understand the errors")

if (errorCode != STATUS_PAIRING_SUCCESS) {
requireActivity().runOnUiThread(Runnable {
val alertDialog: AlertDialog? = activity?.let {
val builder = AlertDialog.Builder(it)
builder.apply {
setPositiveButton("Continue",
DialogInterface.OnClickListener { dialog, id ->
deviceController.continueCommissioning(devicePtr, true)
})
setNegativeButton("No",
DialogInterface.OnClickListener { dialog, id ->
deviceController.continueCommissioning(devicePtr, false)
})
}
builder.setTitle("Device Attestation")
builder.setMessage("Device Attestation failed for device under commissioning. Do you wish to continue pairing?")
// Create the AlertDialog
builder.create()
}
alertDialog?.show()
})
}
})

deviceController.pairDeviceWithAddress(
id,
deviceInfo.ipAddress,
Expand All @@ -103,7 +134,7 @@ class DeviceProvisioningFragment : Fragment() {
if (gatt != null) {
return
}

Log.i(TAG, "yujuan:startConnectingToDevice")
scope.launch {
val deviceController = ChipClient.getDeviceController(requireContext())
val bluetoothManager = BluetoothManager()
Expand Down Expand Up @@ -135,36 +166,42 @@ class DeviceProvisioningFragment : Fragment() {
if (wifi != null) {
network = NetworkCredentials.forWiFi(NetworkCredentials.WiFiCredentials(wifi.ssid, wifi.password))
}

val thread = networkParcelable.threadCredentials
if (thread != null) {
network = NetworkCredentials.forThread(NetworkCredentials.ThreadCredentials(thread.operationalDataset))
}
deviceController.setDeviceAttestationFailureCallback(DEVICE_ATTESTATION_FAILED_TIMEOUT
) { devicePtr, errorCode ->

deviceController.setDeviceAttestationDelegate(DEVICE_ATTESTATION_FAILED_TIMEOUT,
{ devicePtr, attestationInfo, errorCode ->
Log.i(TAG, "Device attestation errorCode: $errorCode, " +
"Look at 'src/credentials/attestation_verifier/DeviceAttestationVerifier.h' " +
"AttestationVerificationResult enum to understand the errors")
requireActivity().runOnUiThread(Runnable {
val alertDialog: AlertDialog? = activity?.let {
val builder = AlertDialog.Builder(it)
builder.apply {
setPositiveButton("Continue",
DialogInterface.OnClickListener { dialog, id ->
deviceController.continueCommissioning(devicePtr, true)
})
setNegativeButton("No",
DialogInterface.OnClickListener { dialog, id ->
deviceController.continueCommissioning(devicePtr, false)
})

if (errorCode != STATUS_PAIRING_SUCCESS) {
requireActivity().runOnUiThread(Runnable {
val alertDialog: AlertDialog? = activity?.let {
val builder = AlertDialog.Builder(it)
builder.apply {
setPositiveButton("Continue",
DialogInterface.OnClickListener { dialog, id ->
deviceController.continueCommissioning(devicePtr, true)
})
setNegativeButton("No",
DialogInterface.OnClickListener { dialog, id ->
deviceController.continueCommissioning(devicePtr, false)
})
}
builder.setTitle("Device Attestation")
builder.setMessage("Device Attestation failed for device under commissioning. Do you wish to continue pairing?")
// Create the AlertDialog
builder.create()
}
builder.setTitle("Device Attestation")
builder.setMessage("Device Attestation failed for device under commissioning. Do you wish to continue pairing?")
// Create the AlertDialog
builder.create()
}
alertDialog?.show()
})
}
alertDialog?.show()
})
}
})

deviceController.pairDevice(gatt, connId, deviceId, deviceInfo.setupPinCode, network)
DeviceIdUtil.setNextAvailableId(requireContext(), deviceId + 1)
}
Expand Down
31 changes: 8 additions & 23 deletions src/controller/java/DeviceAttestationDelegateBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,15 @@ void DeviceAttestationDelegateBridge::OnDeviceAttestationCompleted(
mResult = attestationResult;
if (mDeviceAttestationDelegate != nullptr)
{
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
jclass completionCallbackCls = nullptr;
JniReferences::GetInstance().GetClassRef(
env, "chip/devicecontroller/DeviceAttestationDelegate$DeviceAttestationCompletionCallback", completionCallbackCls);
VerifyOrReturn(completionCallbackCls != nullptr,
ChipLogError(Controller, "Could not find device attestation completion callback class."));
jclass failureCallbackCls = nullptr;
JniReferences::GetInstance().GetClassRef(
env, "chip/devicecontroller/DeviceAttestationDelegate$DeviceAttestationFailureCallback", failureCallbackCls);
VerifyOrReturn(failureCallbackCls != nullptr,
ChipLogError(Controller, "Could not find device attestation failure callback class."));
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
jclass deviceAttestationDelegateCls = nullptr;

if (env->IsInstanceOf(mDeviceAttestationDelegate, completionCallbackCls))
JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/DeviceAttestationDelegate",
deviceAttestationDelegateCls);
VerifyOrReturn(deviceAttestationDelegateCls != nullptr,
ChipLogError(Controller, "Could not find device attestation delegate class."));

if (env->IsInstanceOf(mDeviceAttestationDelegate, deviceAttestationDelegateCls))
{
jmethodID onDeviceAttestationCompletedMethod;
JniReferences::GetInstance().FindMethod(env, mDeviceAttestationDelegate, "onDeviceAttestationCompleted",
Expand All @@ -98,17 +94,6 @@ void DeviceAttestationDelegateBridge::OnDeviceAttestationCompleted(
env->CallVoidMethod(mDeviceAttestationDelegate, onDeviceAttestationCompletedMethod, reinterpret_cast<jlong>(device),
javaAttestationInfo, static_cast<jint>(attestationResult));
}
else if ((attestationResult != chip::Credentials::AttestationVerificationResult::kSuccess) &&
env->IsInstanceOf(mDeviceAttestationDelegate, failureCallbackCls))
{
jmethodID onDeviceAttestationFailedMethod;
JniReferences::GetInstance().FindMethod(env, mDeviceAttestationDelegate, "onDeviceAttestationFailed", "(JI)V",
&onDeviceAttestationFailedMethod);
VerifyOrReturn(onDeviceAttestationFailedMethod != nullptr,
ChipLogError(Controller, "Could not find deviceAttestation failed method"));
env->CallVoidMethod(mDeviceAttestationDelegate, onDeviceAttestationFailedMethod, reinterpret_cast<jlong>(device),
static_cast<jint>(attestationResult));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,42 +85,23 @@ public void setNOCChainIssuer(NOCChainIssuer issuer) {
}

/**
* If DeviceAttestationCompletionCallback is setted, then it will always be called when device
* attestation completes.
* If DeviceAttestationDelegate is setted, then it will always be called when device attestation
* completes. In case the device attestation fails, the client can decide to continue or stop the
* commissioning.
*
* <p>When {@link
* DeviceAttestationDelegate.DeviceAttestationCompletionCallback#onDeviceAttestationCompleted(long,
* long, AttestationInfo, int)} is received, {@link #continueCommissioning(long, boolean)} must be
* <p>When {@link DeviceAttestationDelegate#onDeviceAttestationCompleted(long, long,
* AttestationInfo, int)} is received, {@link #continueCommissioning(long, boolean)} must be
* called.
*
* @param failSafeExpiryTimeoutSecs the value to set for the fail-safe timer before
* onDeviceAttestationCompleted is invoked. The unit is seconds.
* @param completionCallback the callback will be invoked when deviceattestation completed with
* device info for additional verification.
* @param deviceAttestationDelegate the delegate for device attestation completed with device info
* for additional verification.
*/
public void setDeviceAttestationCompletionCallback(
int failSafeExpiryTimeoutSecs,
DeviceAttestationDelegate.DeviceAttestationCompletionCallback completionCallback) {
public void setDeviceAttestationDelegate(
int failSafeExpiryTimeoutSecs, DeviceAttestationDelegate deviceAttestationDelegate) {
setDeviceAttestationDelegate(
deviceControllerPtr, failSafeExpiryTimeoutSecs, completionCallback);
}

/**
* If DeviceAttestationFailureCallback is setted, then it will be called when device attestation
* fails, and the client can decide to continue or stop the commissioning.
*
* <p>When {@link
* DeviceAttestationDelegate.DeviceAttestationFailureCallback#onDeviceAttestationFailed(long,
* long, int)} is received, {@link #continueCommissioning(long, boolean)} must be called.
*
* @param failSafeExpiryTimeoutSecs the value to set for the fail-safe timer before
* onDeviceAttestationFailed is invoked. The unit is seconds.
* @param failureCallback the callback will be invoked when device attestation failed.
*/
public void setDeviceAttestationFailureCallback(
int failSafeExpiryTimeoutSecs,
DeviceAttestationDelegate.DeviceAttestationFailureCallback failureCallback) {
setDeviceAttestationDelegate(deviceControllerPtr, failSafeExpiryTimeoutSecs, failureCallback);
deviceControllerPtr, failSafeExpiryTimeoutSecs, deviceAttestationDelegate);
}

public void pairDevice(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package chip.devicecontroller;

/**
* Only one of the following delegate callbacks should be implemented.
* Delegate for device attestation verifiers.
*
* <p>If one of the following callbacks is implemented, {@link
* ChipDeviceController#continueCommissioning(long, boolean)} is expected to be called if
* commissioning should continue.
* <p>If DeviceAttestationDelegate is implemented, then onDeviceAttestationCompleted will always be
* called when device attestation completes.
*
* <p>If DeviceAttestationCompletionCallback is implemented, then it will always be called when
* device attestation completes.
*
* <p>If DeviceAttestationFailureCallback is implemented, then it will be called when device
* attestation fails, and the client can decide to continue or stop the commissioning.
* <p>If device attestation fails, {@link ChipDeviceController#continueCommissioning(long, boolean)}
* is expected to be called to continue or stop the commissioning.
*
* <p>For example:
*
Expand All @@ -24,30 +20,16 @@
* </pre>
*/
public interface DeviceAttestationDelegate {

public interface DeviceAttestationCompletionCallback extends DeviceAttestationDelegate {
/**
* The callback will be invoked when device attestation completed with device info for
* additional verification.
*
* <p>This allows the callback to stop commissioning after examining the device info (DAC, PAI,
* CD).
*
* @param devicePtr Handle of device being commissioned
* @param attestationInfo Attestation information for the device
* @param errorCode Error code on attestation failure. 0 if success.
*/
void onDeviceAttestationCompleted(
long devicePtr, AttestationInfo attestationInfo, int errorCode);
}

public interface DeviceAttestationFailureCallback extends DeviceAttestationDelegate {
/**
* The callback will be invoked when device attestation failed
*
* @param devicePtr Handle of device being commissioned
* @param errorCode Error code for the failure.
*/
void onDeviceAttestationFailed(long devicePtr, int errorCode);
}
/**
* The callback will be invoked when device attestation completed with device info for additional
* verification.
*
* <p>This allows the callback to stop commissioning after examining the device info (DAC, PAI,
* CD).
*
* @param devicePtr Handle of device being commissioned
* @param attestationInfo Attestation information for the device, null is errorCode is 0.
* @param errorCode Error code on attestation failure. 0 if success.
*/
void onDeviceAttestationCompleted(long devicePtr, AttestationInfo attestationInfo, int errorCode);
}

0 comments on commit 1c1ea4f

Please sign in to comment.