-
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.
[Android] Implement Java TLV Decoder API (#27586)
* Implement Android TLV Decoder * Restyled by google-java-format * Restyled by clang-format * fix merge conflict * restyle --------- Co-authored-by: Restyled.io <commits@restyled.io> Co-authored-by: Andrei Litvin <andy314@gmail.com>
- Loading branch information
Showing
5 changed files
with
150 additions
and
6 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
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include "lib/core/CHIPError.h" | ||
#include "lib/support/JniTypeWrappers.h" | ||
|
||
// #include <lib/support/CHIPMem.h> | ||
// #include <lib/support/CodeUtils.h> | ||
#include <lib/support/JniReferences.h> | ||
#include <lib/support/logging/CHIPLogging.h> | ||
|
||
#include "CHIPAttributeTLVValueDecoder.h" | ||
#include "CHIPEventTLVValueDecoder.h" | ||
|
||
#include <jni.h> | ||
|
||
using namespace chip; | ||
|
||
#define JNI_METHOD(RETURN, METHOD_NAME) \ | ||
extern "C" JNIEXPORT RETURN JNICALL Java_chip_devicecontroller_ChipTLVValueDecoder_##METHOD_NAME | ||
|
||
extern CHIP_ERROR ParseAttributePath(jobject attributePath, EndpointId & outEndpointId, ClusterId & outClusterId, | ||
AttributeId & outAttributeId); | ||
extern CHIP_ERROR ParseEventPath(jobject eventPath, EndpointId & outEndpointId, ClusterId & outClusterId, EventId & outEventId, | ||
bool & outIsUrgent); | ||
|
||
JNI_METHOD(jobject, decodeAttributeValue)(JNIEnv * env, jclass clazz, jobject attributePath, jbyteArray jTlv) | ||
{ | ||
EndpointId endpointId; | ||
ClusterId clusterId; | ||
AttributeId attributeId; | ||
CHIP_ERROR err = ParseAttributePath(attributePath, endpointId, clusterId, attributeId); | ||
if (err != CHIP_NO_ERROR) | ||
{ | ||
ChipLogProgress(Controller, "decode error attributePath"); | ||
return nullptr; | ||
} | ||
|
||
JniByteArray tlv(env, jTlv); | ||
|
||
chip::app::ConcreteAttributePath path(endpointId, clusterId, attributeId); | ||
|
||
chip::TLV::TLVReader reader; | ||
reader.Init(tlv.byteSpan()); | ||
reader.Next(); | ||
|
||
jobject ret = DecodeAttributeValue(path, reader, &err); | ||
|
||
if (err != CHIP_NO_ERROR) | ||
{ | ||
ChipLogProgress(Controller, "decode error attributeValue"); | ||
return nullptr; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
JNI_METHOD(jobject, decodeEventValue)(JNIEnv * env, jclass clazz, jobject eventPath, jbyteArray jTlv) | ||
{ | ||
EndpointId endpointId; | ||
ClusterId clusterId; | ||
EventId eventId; | ||
bool isUrgent; | ||
CHIP_ERROR err = ParseEventPath(eventPath, endpointId, clusterId, eventId, isUrgent); | ||
if (err != CHIP_NO_ERROR) | ||
{ | ||
return nullptr; | ||
} | ||
|
||
JniByteArray tlv(env, jTlv); | ||
|
||
chip::app::ConcreteEventPath path(endpointId, clusterId, eventId); | ||
chip::TLV::TLVReader reader; | ||
|
||
reader.Init(tlv.byteSpan()); | ||
reader.Next(); | ||
|
||
jobject ret = DecodeEventValue(path, reader, &err); | ||
|
||
if (err != CHIP_NO_ERROR) | ||
{ | ||
return nullptr; | ||
} | ||
|
||
return ret; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/controller/java/src/chip/devicecontroller/ChipTLVValueDecoder.java
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,28 @@ | ||
/* | ||
* Copyright (c) 2020-2023 Project CHIP Authors | ||
* All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package chip.devicecontroller; | ||
|
||
import chip.devicecontroller.model.ChipAttributePath; | ||
import chip.devicecontroller.model.ChipEventPath; | ||
|
||
public class ChipTLVValueDecoder { | ||
public static native <T> T decodeAttributeValue(ChipAttributePath attributePath, byte[] tlv); | ||
|
||
public static native <T> T decodeEventValue(ChipEventPath eventPath, byte[] tlv); | ||
} |