Skip to content

Commit

Permalink
Add store, delete ICDClientinfo method
Browse files Browse the repository at this point in the history
  • Loading branch information
joonhaengHeo committed Jun 12, 2024
1 parent 4156766 commit a2dac3f
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ class UnpairDeviceFragment : Fragment() {

// Remove ICD Client info
if (addressUpdateFragment.isICDDevice()) {
ChipICDClient.removeICDClientInfo(
deviceController.fabricIndex,
addressUpdateFragment.deviceId
)
ChipICDClient.clearICDClientInfo(deviceController.fabricIndex, addressUpdateFragment.deviceId)

Log.d(TAG, "ICDClientInfo : ${ChipICDClient.getICDClientInfo(deviceController.fabricIndex)}")
}
Expand Down
85 changes: 82 additions & 3 deletions src/controller/java/AndroidICDClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
#include "AndroidICDClient.h"

#include <app/icd/client/ICDClientInfo.h>
#include <lib/support/JniTypeWrappers.h>

chip::app::DefaultICDClientStorage sICDClientStorage;
static CHIP_ERROR ParseICDClientInfo(JNIEnv * env, jint jFabricIndex, jobject jIcdClientInfo, chip::app::ICDClientInfo & icdClientInfo);

jobject getICDClientInfo(JNIEnv * env, const char * icdClientInfoSign, jint jFabricIndex)
{
Expand Down Expand Up @@ -93,17 +95,94 @@ jobject getICDClientInfo(JNIEnv * env, const char * icdClientInfoSign, jint jFab
return jInfo;
}

jlong removeICDClientInfo(JNIEnv * env, jint jFabricIndex, jlong jNodeId)
CHIP_ERROR StoreICDEntryWithKey(JNIEnv * env, jint jFabricIndex, jobject jicdClientInfo, jbyteArray jKey)
{
CHIP_ERROR err = CHIP_NO_ERROR;

chip::app::ICDClientInfo clientInfo;
chip::JniByteArray jniKey(env, jKey);

err = ParseICDClientInfo(env, jFabricIndex, jicdClientInfo, clientInfo);
VerifyOrReturnValue(err == CHIP_NO_ERROR, err, ChipLogError(Controller, "Failed to parse ICD Client info: %" CHIP_ERROR_FORMAT, err.Format()));

err = getICDClientStorage()->SetKey(clientInfo, jniKey.byteSpan());

if (err == CHIP_NO_ERROR)
{
err = getICDClientStorage()->StoreEntry(clientInfo);
}
else
{
getICDClientStorage()->RemoveKey(clientInfo);
ChipLogError(Controller, "Failed to persist symmetric key with error: %" CHIP_ERROR_FORMAT, err.Format());
}

return err;
}

CHIP_ERROR RemoveICDEntryWithKey(JNIEnv * env, jint jFabricIndex, jobject jicdClientInfo)
{
CHIP_ERROR err = CHIP_NO_ERROR;

chip::app::ICDClientInfo info;
err = ParseICDClientInfo(env, jFabricIndex, jicdClientInfo, info);
VerifyOrReturnValue(err == CHIP_NO_ERROR, err, ChipLogError(Controller, "Failed to parse ICD Client info: %" CHIP_ERROR_FORMAT, err.Format()));

getICDClientStorage()->RemoveKey(info);

return err;
}

CHIP_ERROR ClearICDClientInfo(JNIEnv * env, jint jFabricIndex, jlong jNodeId)
{
CHIP_ERROR err = CHIP_NO_ERROR;

chip::ScopedNodeId scopedNodeId(static_cast<chip::NodeId>(jNodeId), static_cast<chip::FabricIndex>(jFabricIndex));
err = getICDClientStorage()->DeleteEntry(scopedNodeId);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Controller, "removeICDClientInfo error!: %" CHIP_ERROR_FORMAT, err.Format());
ChipLogError(Controller, "ClearICDClientInfo error!: %" CHIP_ERROR_FORMAT, err.Format());
}
return static_cast<jlong>(err.AsInteger());
return err;
}

CHIP_ERROR ParseICDClientInfo(JNIEnv * env, jint jFabricIndex, jobject jIcdClientInfo, chip::app::ICDClientInfo & icdClientInfo)
{
VerifyOrReturnError(jIcdClientInfo != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

jmethodID getPeerNodeIdMethod = nullptr;
jmethodID getStartCounterMethod = nullptr;
jmethodID getOffsetMethod = nullptr;
jmethodID getMonitoredSubjectMethod = nullptr;
jmethodID getIcdAesKeyMethod = nullptr;
jmethodID getIcdHmacKeyMethod = nullptr;

ReturnErrorOnFailure(chip::JniReferences::GetInstance().FindMethod(env, jIcdClientInfo, "getPeerNodeId", "()J", &getPeerNodeIdMethod));
ReturnErrorOnFailure(chip::JniReferences::GetInstance().FindMethod(env, jIcdClientInfo, "getStartCounter", "()J", &getStartCounterMethod));
ReturnErrorOnFailure(chip::JniReferences::GetInstance().FindMethod(env, jIcdClientInfo, "getOffset", "()J", &getOffsetMethod));
ReturnErrorOnFailure(chip::JniReferences::GetInstance().FindMethod(env, jIcdClientInfo, "getMonitoredSubject", "()J", &getMonitoredSubjectMethod));
ReturnErrorOnFailure(chip::JniReferences::GetInstance().FindMethod(env, jIcdClientInfo, "getIcdAesKey", "()[B", &getIcdAesKeyMethod));
ReturnErrorOnFailure(chip::JniReferences::GetInstance().FindMethod(env, jIcdClientInfo, "getIcdHmacKey", "()[B", &getIcdHmacKeyMethod));

jlong jPeerNodeId = env->CallLongMethod(jIcdClientInfo, getPeerNodeIdMethod);
jlong jStartCounter = env->CallLongMethod(jIcdClientInfo, getStartCounterMethod);
jlong jOffset = env->CallLongMethod(jIcdClientInfo, getOffsetMethod);
jlong jMonitoredSubject = env->CallLongMethod(jIcdClientInfo, getMonitoredSubjectMethod);
jbyteArray jIcdAesKey = static_cast<jbyteArray>(env->CallObjectMethod(jIcdClientInfo, getIcdAesKeyMethod));
jbyteArray jIcdHmacKey = static_cast<jbyteArray>(env->CallObjectMethod(jIcdClientInfo, getIcdHmacKeyMethod));

chip::ScopedNodeId scopedNodeId(static_cast<chip::NodeId>(jPeerNodeId), static_cast<chip::FabricIndex>(jFabricIndex));
chip::JniByteArray jniIcdAesKey(env, jIcdAesKey);
chip::JniByteArray jniIcdHmacKey(env, jIcdHmacKey);

icdClientInfo.peer_node = scopedNodeId;
icdClientInfo.start_icd_counter = static_cast<uint32_t>(jStartCounter);
icdClientInfo.offset = static_cast<uint32_t>(jOffset);
icdClientInfo.monitored_subject = static_cast<uint64_t>(jMonitoredSubject);
memcpy(icdClientInfo.aes_key_handle.AsMutable<chip::Crypto::Symmetric128BitsKeyByteArray>(), jniIcdAesKey.data(), sizeof(chip::Crypto::Symmetric128BitsKeyByteArray));
memcpy(icdClientInfo.hmac_key_handle.AsMutable<chip::Crypto::Symmetric128BitsKeyByteArray>(), jniIcdHmacKey.data(), sizeof(chip::Crypto::Symmetric128BitsKeyByteArray));

return CHIP_NO_ERROR;
}

chip::app::DefaultICDClientStorage * getICDClientStorage()
Expand Down
6 changes: 5 additions & 1 deletion src/controller/java/AndroidICDClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@

jobject getICDClientInfo(JNIEnv * env, const char * icdClientInfoSign, jint jFabricIndex);

jlong removeICDClientInfo(JNIEnv * env, jint jFabricIndex, jlong jNodeId);
CHIP_ERROR StoreICDEntryWithKey(JNIEnv * env, jint jFabricIndex, jobject jicdClientInfo, jbyteArray jKey);

CHIP_ERROR RemoveICDEntryWithKey(JNIEnv * env, jint jFabricIndex, jobject jicdClientInfo);

CHIP_ERROR ClearICDClientInfo(JNIEnv * env, jint jFabricIndex, jlong jNodeId);

chip::app::DefaultICDClientStorage * getICDClientStorage();
16 changes: 14 additions & 2 deletions src/controller/java/CHIPICDClient-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,20 @@ JNI_METHOD(jobject, getICDClientInfo)(JNIEnv * env, jobject self, jint jFabricIn
return getICDClientInfo(env, "chip/devicecontroller/ICDClientInfo", jFabricIndex);
}

JNI_METHOD(void, removeICDClientInfo)(JNIEnv * env, jobject self, jint jFabricIndex, jlong jNodeId)
JNI_METHOD(void, storeICDEntryWithKey)(JNIEnv * env, jobject self, jint jFabricIndex, jobject jicdClientInfo, jbyteArray jKey)
{
chip::DeviceLayer::StackLock lock;
removeICDClientInfo(env, jFabricIndex, jNodeId);
StoreICDEntryWithKey(env, jFabricIndex, jicdClientInfo, jKey);
}

JNI_METHOD(void, removeICDEntryWithKey)(JNIEnv * env, jobject self, jint jFabricIndex, jobject jicdClientInfo)
{
chip::DeviceLayer::StackLock lock;
RemoveICDEntryWithKey(env, jFabricIndex, jicdClientInfo);
}

JNI_METHOD(void, clearICDClientInfo)(JNIEnv * env, jobject self, jint jFabricIndex, jlong jNodeId)
{
chip::DeviceLayer::StackLock lock;
ClearICDClientInfo(env, jFabricIndex, jNodeId);
}
16 changes: 14 additions & 2 deletions src/controller/java/MatterICDClient-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,20 @@ JNI_METHOD(jobject, getICDClientInfo)(JNIEnv * env, jobject self, jint jFabricIn
return getICDClientInfo(env, "matter/controller/ICDClientInfo", jFabricIndex);
}

JNI_METHOD(void, removeICDClientInfo)(JNIEnv * env, jobject self, jint jFabricIndex, jlong jNodeId)
JNI_METHOD(void, storeICDEntryWithKey)(JNIEnv * env, jobject self, jint jFabricIndex, jobject jicdClientInfo, jbyteArray jKey)
{
chip::DeviceLayer::StackLock lock;
removeICDClientInfo(env, jFabricIndex, jNodeId);
StoreICDEntryWithKey(env, jFabricIndex, jicdClientInfo, jKey);
}

JNI_METHOD(void, removeICDEntryWithKey)(JNIEnv * env, jobject self, jint jFabricIndex, jobject jicdClientInfo)
{
chip::DeviceLayer::StackLock lock;
RemoveICDEntryWithKey(env, jFabricIndex, jicdClientInfo);
}

JNI_METHOD(void, clearICDClientInfo)(JNIEnv * env, jobject self, jint jFabricIndex, jlong jNodeId)
{
chip::DeviceLayer::StackLock lock;
ClearICDClientInfo(env, jFabricIndex, jNodeId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ public static boolean isPeerICDClient(int fabricIndex, long deviceId) {
return clientInfo.stream().anyMatch(info -> info.getPeerNodeId() == deviceId);
}

public static native void removeICDClientInfo(int fabricIndex, long deviceId);
public static native void storeICDEntryWithKey(int fabricIndex, ICDClientInfo icdClientInfo, byte[] key);

public static native void removeICDEntryWithKey(int fabricIndex, ICDClientInfo icdClientInfo);

public static native void clearICDClientInfo(int fabricIndex, long deviceId);

public static native List<ICDClientInfo> getICDClientInfo(int fabricIndex);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ object MatterICDClientImpl {
return clientInfo.firstOrNull { it.peerNodeId == deviceId } != null
}

external fun getICDClientInfo(fabricIndex: Int): List<ICDClientInfo>?
external fun storeICDEntryWithKey(fabricIndex: Int, icdClientInfo: ICDClientInfo, key: ByteArray)

external fun removeICDEntryWithKey(fabricIndex: Int, icdClientInfo: ICDClientInfo)

external fun removeICDClientInfo(fabricIndex: Int, deviceId: Long)
external fun clearICDClientInfo(fabricIndex: Int, deviceId: Long)

external fun getICDClientInfo(fabricIndex: Int): List<ICDClientInfo>?
}

0 comments on commit a2dac3f

Please sign in to comment.