Skip to content

Commit

Permalink
Add groupsetting API
Browse files Browse the repository at this point in the history
  • Loading branch information
joonhaengHeo committed Sep 14, 2023
1 parent 3bafe21 commit 42d8945
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 32 deletions.
173 changes: 148 additions & 25 deletions src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,16 @@ JNI_METHOD(jlong, getGroupDevicePointer)(JNIEnv * env, jobject self, jlong handl
return reinterpret_cast<jlong>(device);
}

JNI_METHOD(void, releaseGroupDevicePointer)(JNIEnv * env, jobject self, jlong devicePtr)
{
chip::DeviceLayer::StackLock lock;
GroupDeviceProxy * device = reinterpret_cast<GroupDeviceProxy *>(devicePtr);
if (device != NULL)
{
delete device;
}
}

JNI_METHOD(jobject, getAvailableGroupIds)(JNIEnv * env, jobject self, jlong handle)
{
chip::DeviceLayer::StackLock lock;
Expand Down Expand Up @@ -1344,18 +1354,21 @@ JNI_METHOD(jobject, findKeySetId)(JNIEnv * env, jobject self, jlong handle, jint
chip::Credentials::GroupDataProvider::GroupKey groupKey;
GroupId groupId = static_cast<GroupId>(jGroupId);
jobject wrapperKeyId;
while (iter->Next(groupKey))
{
if (groupKey.group_id == groupId)

if (iter) {
while (iter->Next(groupKey))
{
jobject jKeyId;
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", static_cast<jint>(groupKey.keyset_id), jKeyId);
chip::JniReferences::GetInstance().CreateOptional(jKeyId, wrapperKeyId);
iter->Release();
return wrapperKeyId;
if (groupKey.group_id == groupId)
{
jobject jKeyId;
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", static_cast<jint>(groupKey.keyset_id), jKeyId);
chip::JniReferences::GetInstance().CreateOptional(jKeyId, wrapperKeyId);
iter->Release();
return wrapperKeyId;
}
}
iter->Release();
}
iter->Release();
chip::JniReferences::GetInstance().CreateOptional(nullptr, wrapperKeyId);
return wrapperKeyId;
}
Expand All @@ -1377,6 +1390,77 @@ JNI_METHOD(jboolean, addGroup)(JNIEnv * env, jobject self, jlong handle, jint jG
return err == CHIP_NO_ERROR ? JNI_TRUE : JNI_FALSE;
}

JNI_METHOD(jboolean, removeGroup)(JNIEnv * env, jobject self, jlong handle, jint jGroupId)
{
chip::DeviceLayer::StackLock lock;
AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);

chip::Credentials::GroupDataProvider * groupDataProvider = chip::Credentials::GetGroupDataProvider();
CHIP_ERROR err = groupDataProvider->RemoveGroupInfo(wrapper->Controller()->GetFabricIndex(), static_cast<GroupId>(jGroupId));

return err == CHIP_NO_ERROR ? JNI_TRUE : JNI_FALSE;
}

JNI_METHOD(jobject, getKeySetIds)(JNIEnv * env, jobject self, jlong handle)
{
chip::DeviceLayer::StackLock lock;
AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);

CHIP_ERROR err = CHIP_NO_ERROR;
chip::Credentials::GroupDataProvider * groupDataProvider = chip::Credentials::GetGroupDataProvider();
auto it = groupDataProvider->IterateKeySets(wrapper->Controller()->GetFabricIndex());

jobject keySetIds;
err = chip::JniReferences::GetInstance().CreateArrayList(keySetIds);

chip::Credentials::GroupDataProvider::KeySet keySet;

if (it)
{
while (it->Next(keySet))
{
jobject jKeySetId;
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", static_cast<jint>(keySet.keyset_id), jKeySetId);
chip::JniReferences::GetInstance().AddToList(keySetIds, jKeySetId);
}
it->Release();
}

return keySetIds;
}

JNI_METHOD(jobject, getKeySecurityPolicy)(JNIEnv * env, jobject self, jlong handle, int jKeySetId)
{
chip::DeviceLayer::StackLock lock;
AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);

chip::Credentials::GroupDataProvider * groupDataProvider = chip::Credentials::GetGroupDataProvider();
auto it = groupDataProvider->IterateKeySets(wrapper->Controller()->GetFabricIndex());

chip::Credentials::GroupDataProvider::KeySet keySet;

uint16_t keySetId = static_cast<uint16_t>(jKeySetId);
jobject wrapperKeyPolicy;

if (it)
{
while (it->Next(keySet))
{
if (keySet.keyset_id == keySetId)
{
jobject jKeyPolicy;
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", static_cast<jint>(keySet.policy), jKeyPolicy);
chip::JniReferences::GetInstance().CreateOptional(jKeyPolicy, wrapperKeyPolicy);
it->Release();
return wrapperKeyPolicy;
}
}
it->Release();
}
chip::JniReferences::GetInstance().CreateOptional(nullptr, wrapperKeyPolicy);
return wrapperKeyPolicy;
}

JNI_METHOD(jboolean, bindKeySet)(JNIEnv * env, jobject self, jlong handle, jint jGroupId, jint jKeySetId)
{
chip::DeviceLayer::StackLock lock;
Expand All @@ -1391,6 +1475,39 @@ JNI_METHOD(jboolean, bindKeySet)(JNIEnv * env, jobject self, jlong handle, jint
return err == CHIP_NO_ERROR ? JNI_TRUE : JNI_FALSE;
}

JNI_METHOD(jboolean, unbindKeySet)(JNIEnv * env, jobject self, jlong handle, jint jGroupId, jint jKeySetId)
{
chip::DeviceLayer::StackLock lock;
AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);

size_t index = 0;
chip::Credentials::GroupDataProvider * groupDataProvider = chip::Credentials::GetGroupDataProvider();
auto iter = groupDataProvider->IterateGroupKeys(wrapper->Controller()->GetFabricIndex());
size_t maxCount = iter->Count();
chip::Credentials::GroupDataProvider::GroupKey groupKey;

GroupId groupId = static_cast<GroupId>(jGroupId);
uint16_t keysetId = static_cast<uint16_t>(jKeySetId);

while (iter->Next(groupKey))
{
if (groupKey.group_id == groupId && groupKey.keyset_id == keysetId)
{
break;
}
index++;
}
iter->Release();
if (index >= maxCount)
{
return JNI_FALSE;
}

CHIP_ERROR err = groupDataProvider->RemoveGroupKeyAt(wrapper->Controller()->GetFabricIndex(), index);

return err == CHIP_NO_ERROR ? JNI_TRUE : JNI_FALSE;
}

JNI_METHOD(jboolean, addKeySet)(JNIEnv * env, jobject self, jlong handle, jint jKeySetId, jint jKeyPolicy, jlong validityTime, jbyteArray epochKey)
{
chip::DeviceLayer::StackLock lock;
Expand Down Expand Up @@ -1422,33 +1539,39 @@ JNI_METHOD(jboolean, addKeySet)(JNIEnv * env, jobject self, jlong handle, jint j
return JNI_TRUE;
}

JNI_METHOD(jobject, getKeysetIds)(JNIEnv * env, jobject self, jlong handle)
JNI_METHOD(jboolean, removeKeySet)(JNIEnv * env, jobject self, jlong handle, jint jKeySetId)
{
chip::DeviceLayer::StackLock lock;
AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);

CHIP_ERROR err = CHIP_NO_ERROR;
chip::Credentials::GroupDataProvider * groupDataProvider = chip::Credentials::GetGroupDataProvider();
auto it = groupDataProvider->IterateKeySets(wrapper->Controller()->GetFabricIndex());

jobject keySetIds;
err = chip::JniReferences::GetInstance().CreateArrayList(keySetIds);

chip::Credentials::GroupDataProvider::KeySet keySet;

if (it)
{
while (it->Next(keySet))
size_t index = 0;
auto iter = groupDataProvider->IterateGroupKeys(wrapper->Controller()->GetFabricIndex());
uint16_t keysetId = static_cast<uint16_t>(jKeySetId);
chip::Credentials::GroupDataProvider::GroupKey groupKey;
if (iter) {
while (iter->Next(groupKey))
{
jobject jKeySetId;
chip::JniReferences::GetInstance().CreateBoxedObject<jint>("java/lang/Integer", "(I)V", static_cast<jint>(keySet.keyset_id), jKeySetId);
chip::JniReferences::GetInstance().AddToList(keySetIds, jKeySetId);
ChipLogProgress(Controller, "keySet : %u, %u", keySet.keyset_id, static_cast<uint8_t>(keySet.policy));
if (groupKey.keyset_id == keysetId)
{
err = groupDataProvider->RemoveGroupKeyAt(wrapper->Controller()->GetFabricIndex(), index);
if (err != CHIP_NO_ERROR)
{
break;
}
}
index++;
}
it->Release();
iter->Release();
if (err == CHIP_NO_ERROR) {
err = groupDataProvider->RemoveKeySet(wrapper->Controller()->GetFabricIndex(), keysetId);
}
return err == CHIP_NO_ERROR ? JNI_TRUE : JNI_FALSE;
}

return keySetIds;
return JNI_FALSE;
}

JNI_METHOD(jint, getFabricIndex)(JNIEnv * env, jobject self, jlong handle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,36 @@ public void releaseConnectedDevicePointer(long devicePtr) {
releaseOperationalDevicePointer(devicePtr);
}

public enum GroupKeySecurityPolicy {
TrustFirst(0), CacheAndSync(1);

private int id;

GroupKeySecurityPolicy(int id) {
this.id = id;
}

int getID() {
return id;
}


public static GroupKeySecurityPolicy value(int id) {
for (GroupKeySecurityPolicy policy : GroupKeySecurityPolicy.values()) {
if (policy.id == id) {
return policy;
}
}
return null;
}
}

public long getGroupDevicePointer(int groupId) {
return getGroupDevicePointer(deviceControllerPtr, groupId);
}

public native void releaseGroupDevicePointer(long devicePtr);

public List<Integer> getAvailableGroupIds() {
return getAvailableGroupIds(deviceControllerPtr);
}
Expand All @@ -364,20 +390,32 @@ public boolean addGroup(int groupId, String groupName) {
return addGroup(deviceControllerPtr, groupId, groupName);
}

public boolean removeGroup(int groupId) {
return removeGroup(deviceControllerPtr, groupId);
}

public List<Integer> getKeySetIds() {
return getKeySetIds(deviceControllerPtr);
}

public Optional<GroupKeySecurityPolicy> getKeySecurityPolicy(int keySetId) {
return getKeySecurityPolicy(deviceControllerPtr, keySetId).map(id -> GroupKeySecurityPolicy.value(id));
}

public boolean bindKeySet(int groupId, int keySetId) {
return bindKeySet(deviceControllerPtr, groupId, keySetId);
}

public enum GroupKeySecurityPolicy {
TrustFirst, CacheAndSync
public boolean unbindKeySet(int groupId, int keySetId) {
return unbindKeySet(deviceControllerPtr, groupId, keySetId);
}

public boolean addKeySet(int keySetId, int keyPolicy, long validityTime, byte[] epochKey) {
return addKeySet(deviceControllerPtr, keySetId, keyPolicy, validityTime, epochKey);
public boolean addKeySet(int keySetId, GroupKeySecurityPolicy keyPolicy, long validityTime, byte[] epochKey) {
return addKeySet(deviceControllerPtr, keySetId, keyPolicy.getID(), validityTime, epochKey);
}

public List<Integer> getKeysetIds() {
return getKeysetIds(deviceControllerPtr);
public boolean removeKeySet(int keySetId) {
return removeKeySet(deviceControllerPtr, keySetId);
}

public void onConnectDeviceComplete() {
Expand Down Expand Up @@ -1199,11 +1237,19 @@ private native void getConnectedDevicePointer(

private native boolean addGroup(long deviceControllerPtr, int groupId, String groupName);

private native boolean removeGroup(long deviceControllerPtr, int groupId);

private native List<Integer> getKeySetIds(long deviceControllerPtr);

private native Optional<Integer> getKeySecurityPolicy(long deviceControllerPtr, int keySetId);

private native boolean bindKeySet(long deviceControllerPtr, int groupId, int keySetId);

private native boolean unbindKeySet(long deviceControllerPtr, int groupId, int keySetId);

private native boolean addKeySet(long deviceControllerPtr, int keySetId, int keyPolicy, long validityTime, byte[] epochKey);

private native List<Integer> getKeysetIds(long deviceControllerPtr);
private native boolean removeKeySet(long deviceControllerPtr, int keySetId);

private native void deleteDeviceController(long deviceControllerPtr);

Expand Down

0 comments on commit 42d8945

Please sign in to comment.