Skip to content

Commit

Permalink
Restyled
Browse files Browse the repository at this point in the history
  • Loading branch information
joonhaengHeo committed Nov 29, 2024
1 parent de3da7c commit 7f85d1d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import com.google.chip.chiptool.databinding.DiagnosticLogFragmentBinding
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class DiagnosticLogFragment : Fragment() {
private val deviceController: ChipDeviceController
Expand Down Expand Up @@ -87,18 +88,18 @@ class DiagnosticLogFragment : Fragment() {
mDownloadFile?.let { showNotification(it) } ?: return
}

override fun onTransferData(
fabricIndex: Int,
nodeId: Long,
data: ByteArray
): Boolean {
override fun onTransferData(fabricIndex: Int, nodeId: Long, data: ByteArray): Boolean {
Log.d(TAG, "onTransferData : ${data.size}")
if (mDownloadFileOutputStream == null) {
Log.d(TAG, "mDownloadFileOutputStream or mDownloadFile is null")
return false
}
return addData(mDownloadFileOutputStream!!, data)
}

private fun addData(outputStream: FileOutputStream, data: ByteArray): Boolean {
try {
mDownloadFileOutputStream!!.write(data)
outputStream.write(data)
} catch (e: IOException) {
Log.d(TAG, "IOException", e)
return false
Expand Down
73 changes: 43 additions & 30 deletions src/controller/java/AndroidLogDownloadFromNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

#include "AndroidLogDownloadFromNode.h"

#include <controller/CHIPDeviceControllerFactory.h>
#include <protocols/bdx/BdxUri.h>
#include <app-common/zap-generated/cluster-enums.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <controller/CHIPDeviceControllerFactory.h>
#include <protocols/bdx/BdxUri.h>

using namespace chip::app::Clusters;

Expand All @@ -30,8 +30,9 @@ namespace Controller {

// Max Length is below 8
CharSpan toIntentCharSpan(DiagnosticLogs::IntentEnum intent)
{
switch (intent) {
{
switch (intent)
{
case DiagnosticLogs::IntentEnum::kEndUserSupport:
return CharSpan::fromCharString("EndUser");
case DiagnosticLogs::IntentEnum::kNetworkDiag:
Expand All @@ -43,16 +44,17 @@ CharSpan toIntentCharSpan(DiagnosticLogs::IntentEnum intent)
}
}

AndroidLogDownloadFromNode::AndroidLogDownloadFromNode(chip::Controller::DeviceController * controller, NodeId remoteNodeId, DiagnosticLogs::IntentEnum intent, uint16_t timeout, jobject jCallbackObject)
: mController(controller), mOnDeviceConnectedCallback(&OnDeviceConnectedFn, this),
mOnDeviceConnectionFailureCallback(&OnDeviceConnectionFailureFn, this),
mOnBdxTransferCallback(&OnBdxTransferCallback, this),
mOnBdxTransferSuccessCallback(&OnBdxTransferSuccessCallback, this),
AndroidLogDownloadFromNode::AndroidLogDownloadFromNode(chip::Controller::DeviceController * controller, NodeId remoteNodeId,
DiagnosticLogs::IntentEnum intent, uint16_t timeout,
jobject jCallbackObject) :
mController(controller),
mOnDeviceConnectedCallback(&OnDeviceConnectedFn, this), mOnDeviceConnectionFailureCallback(&OnDeviceConnectionFailureFn, this),
mOnBdxTransferCallback(&OnBdxTransferCallback, this), mOnBdxTransferSuccessCallback(&OnBdxTransferSuccessCallback, this),
mOnBdxTransferFailureCallback(&OnBdxTransferFailureCallback, this)
{
mRemoteNodeId = remoteNodeId;
mIntent = intent;
mTimeout = timeout;
mIntent = intent;
mTimeout = timeout;

if (mJavaCallback.Init(jCallbackObject) != CHIP_NO_ERROR)
{
Expand All @@ -61,9 +63,11 @@ AndroidLogDownloadFromNode::AndroidLogDownloadFromNode(chip::Controller::DeviceC
}
}

CHIP_ERROR AndroidLogDownloadFromNode::LogDownloadFromNode(DeviceController * controller, NodeId remoteNodeId, DiagnosticLogs::IntentEnum intent, uint16_t timeout, jobject jcallback)
CHIP_ERROR AndroidLogDownloadFromNode::LogDownloadFromNode(DeviceController * controller, NodeId remoteNodeId,
DiagnosticLogs::IntentEnum intent, uint16_t timeout, jobject jcallback)
{
VerifyOrReturnValue(controller != nullptr && jcallback != nullptr && remoteNodeId != kUndefinedNodeId, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnValue(controller != nullptr && jcallback != nullptr && remoteNodeId != kUndefinedNodeId,
CHIP_ERROR_INVALID_ARGUMENT);

auto * logDownload = new AndroidLogDownloadFromNode(controller, remoteNodeId, intent, timeout, jcallback);
VerifyOrReturnValue(logDownload != nullptr, CHIP_ERROR_NO_MEMORY);
Expand All @@ -84,10 +88,11 @@ CHIP_ERROR AndroidLogDownloadFromNode::GetConnectedDevice()
return mController->GetConnectedDevice(mRemoteNodeId, &mOnDeviceConnectedCallback, &mOnDeviceConnectionFailureCallback);
}

CHIP_ERROR AndroidLogDownloadFromNode::SendRetrieveLogsRequest(Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle)
CHIP_ERROR AndroidLogDownloadFromNode::SendRetrieveLogsRequest(Messaging::ExchangeManager & exchangeMgr,
const SessionHandle & sessionHandle)
{
DiagnosticLogs::Commands::RetrieveLogsRequest::Type request;
request.intent = mIntent;
request.intent = mIntent;
request.requestedProtocol = DiagnosticLogs::TransferProtocolEnum::kBdx;

CHIP_ERROR err = chip::bdx::MakeURI(mRemoteNodeId, toIntentCharSpan(mIntent), mFileDesignator);
Expand All @@ -97,7 +102,9 @@ CHIP_ERROR AndroidLogDownloadFromNode::SendRetrieveLogsRequest(Messaging::Exchan
FinishLogDownloadFromNode(err);
}

mBdxReceiver = new BdxDiagnosticLogsReceiver(&mOnBdxTransferCallback, &mOnBdxTransferSuccessCallback, &mOnBdxTransferFailureCallback, mController->GetFabricIndex(), mRemoteNodeId, mFileDesignator);
mBdxReceiver =
new BdxDiagnosticLogsReceiver(&mOnBdxTransferCallback, &mOnBdxTransferSuccessCallback, &mOnBdxTransferFailureCallback,
mController->GetFabricIndex(), mRemoteNodeId, mFileDesignator);
VerifyOrReturnValue(mBdxReceiver != nullptr, CHIP_ERROR_NO_MEMORY);

auto systemState = DeviceControllerFactory::GetInstance().GetSystemState();
Expand All @@ -107,15 +114,15 @@ CHIP_ERROR AndroidLogDownloadFromNode::SendRetrieveLogsRequest(Messaging::Exchan
{
mBdxReceiver->StartBDXTransferTimeout(mTimeout);
}

request.transferFileDesignator = MakeOptional(mFileDesignator);
ClusterBase cluster(exchangeMgr, sessionHandle, 0);

return cluster.InvokeCommand(request, this, OnResponseRetrieveLogs, OnCommandFailure);
}

void AndroidLogDownloadFromNode::OnDeviceConnectedFn(void * context, Messaging::ExchangeManager & exchangeMgr,
const SessionHandle & sessionHandle)
const SessionHandle & sessionHandle)
{
CHIP_ERROR err = CHIP_NO_ERROR;
auto * self = static_cast<AndroidLogDownloadFromNode *>(context);
Expand All @@ -139,7 +146,8 @@ void AndroidLogDownloadFromNode::OnDeviceConnectionFailureFn(void * context, con
self->FinishLogDownloadFromNode(err);
}

void AndroidLogDownloadFromNode::OnResponseRetrieveLogs(void * context, const DiagnosticLogs::Commands::RetrieveLogsResponse::DecodableType & data)
void AndroidLogDownloadFromNode::OnResponseRetrieveLogs(void * context,
const DiagnosticLogs::Commands::RetrieveLogsResponse::DecodableType & data)
{
auto * self = static_cast<AndroidLogDownloadFromNode *>(context);
VerifyOrReturn(self != nullptr,
Expand Down Expand Up @@ -189,7 +197,7 @@ void AndroidLogDownloadFromNode::OnCommandFailure(void * context, CHIP_ERROR err
void AndroidLogDownloadFromNode::FinishLogDownloadFromNode(CHIP_ERROR err)
{
CHIP_ERROR jniErr = CHIP_NO_ERROR;
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
JniLocalReferenceScope scope(env);

if (err == CHIP_NO_ERROR)
Expand All @@ -201,7 +209,8 @@ void AndroidLogDownloadFromNode::FinishLogDownloadFromNode(CHIP_ERROR err)

VerifyOrReturn(jniErr == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find onSuccess method"));

env->CallVoidMethod(mJavaCallback.ObjectRef(), onSuccessMethod, static_cast<jint>(mController->GetFabricIndex()), static_cast<jlong>(mRemoteNodeId));
env->CallVoidMethod(mJavaCallback.ObjectRef(), onSuccessMethod, static_cast<jint>(mController->GetFabricIndex()),
static_cast<jlong>(mRemoteNodeId));
return;
}

Expand All @@ -222,18 +231,21 @@ void AndroidLogDownloadFromNode::FinishLogDownloadFromNode(CHIP_ERROR err)
jniErr = JniReferences::GetInstance().FindMethod(env, mJavaCallback.ObjectRef(), "onError", "(IJJ)V", &onErrorMethod);
VerifyOrReturn(jniErr == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find onError method"));

env->CallVoidMethod(mJavaCallback.ObjectRef(), onErrorMethod, static_cast<jint>(mController->GetFabricIndex()), static_cast<jlong>(mRemoteNodeId), static_cast<jlong>(err.AsInteger()));
env->CallVoidMethod(mJavaCallback.ObjectRef(), onErrorMethod, static_cast<jint>(mController->GetFabricIndex()),
static_cast<jlong>(mRemoteNodeId), static_cast<jlong>(err.AsInteger()));
}

void AndroidLogDownloadFromNode::OnBdxTransferCallback(void * context, FabricIndex fabricIndex, NodeId remoteNodeId, const chip::ByteSpan & data, CHIP_ERROR *errInfoOnFailure)
void AndroidLogDownloadFromNode::OnBdxTransferCallback(void * context, FabricIndex fabricIndex, NodeId remoteNodeId,
const chip::ByteSpan & data, CHIP_ERROR * errInfoOnFailure)
{
auto * self = static_cast<AndroidLogDownloadFromNode *>(context);
VerifyOrReturn(self != nullptr, ChipLogProgress(Controller, "Send command failure callback with null context. Ignoring"));

self->OnTransferCallback(fabricIndex, remoteNodeId, data, errInfoOnFailure);
}

void AndroidLogDownloadFromNode::OnTransferCallback(FabricIndex fabricIndex, NodeId remoteNodeId, const chip::ByteSpan & data, CHIP_ERROR *errInfoOnFailure)
void AndroidLogDownloadFromNode::OnTransferCallback(FabricIndex fabricIndex, NodeId remoteNodeId, const chip::ByteSpan & data,
CHIP_ERROR * errInfoOnFailure)
{
VerifyOrReturn(mJavaCallback.HasValidObjectRef(), ChipLogError(Controller, "mJavaCallback is invalid"));

Expand All @@ -242,17 +254,17 @@ void AndroidLogDownloadFromNode::OnTransferCallback(FabricIndex fabricIndex, Nod

jmethodID onTransferDataMethod;
// Java method signature : boolean onTransferData(int fabricIndex, long nodeId, byte[] data)
*errInfoOnFailure = JniReferences::GetInstance().FindMethod(env, mJavaCallback.ObjectRef(), "onTransferData",
"(IJ[B)Z", &onTransferDataMethod);
*errInfoOnFailure =
JniReferences::GetInstance().FindMethod(env, mJavaCallback.ObjectRef(), "onTransferData", "(IJ[B)Z", &onTransferDataMethod);
VerifyOrReturn(*errInfoOnFailure == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find onTransferData method"));
chip::ByteArray dataByteArray(env, data);

jboolean ret = env->CallBooleanMethod(mJavaCallback.ObjectRef(), onTransferDataMethod, static_cast<jint>(fabricIndex), static_cast<jlong>(remoteNodeId), dataByteArray.jniValue());
jboolean ret = env->CallBooleanMethod(mJavaCallback.ObjectRef(), onTransferDataMethod, static_cast<jint>(fabricIndex),
static_cast<jlong>(remoteNodeId), dataByteArray.jniValue());

if (ret != JNI_TRUE)
{
ChipLogError(Controller, "Transfer will be rejected.")
*errInfoOnFailure = CHIP_ERROR_INTERNAL;
ChipLogError(Controller, "Transfer will be rejected.") * errInfoOnFailure = CHIP_ERROR_INTERNAL;
}
}

Expand All @@ -266,7 +278,8 @@ void AndroidLogDownloadFromNode::OnBdxTransferSuccessCallback(void * context, Fa
self->FinishLogDownloadFromNode(CHIP_NO_ERROR);
}

void AndroidLogDownloadFromNode::OnBdxTransferFailureCallback(void * context, FabricIndex fabricIndex, NodeId remoteNodeId, CHIP_ERROR status)
void AndroidLogDownloadFromNode::OnBdxTransferFailureCallback(void * context, FabricIndex fabricIndex, NodeId remoteNodeId,
CHIP_ERROR status)
{
ChipLogProgress(Controller, "OnBdxTransferFailureCallback: %" CHIP_ERROR_FORMAT, status.Format());

Expand Down
20 changes: 13 additions & 7 deletions src/controller/java/AndroidLogDownloadFromNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ class AndroidLogDownloadFromNode
* @param[in] timeout Download log timeout value. If this value is 0, controller does not handle timeouts.
* @param[in] callback The callback to call when Log Download data is received and when an error occurs
*/
static CHIP_ERROR LogDownloadFromNode(DeviceController * controller, NodeId remoteNodeId, app::Clusters::DiagnosticLogs::IntentEnum intent, uint16_t timeout, jobject jCallbackObject);
static CHIP_ERROR LogDownloadFromNode(DeviceController * controller, NodeId remoteNodeId,
app::Clusters::DiagnosticLogs::IntentEnum intent, uint16_t timeout,
jobject jCallbackObject);

private:
AndroidLogDownloadFromNode(DeviceController * controller, NodeId remoteNodeId, app::Clusters::DiagnosticLogs::IntentEnum intent, uint16_t timeout, jobject javaCallback);
AndroidLogDownloadFromNode(DeviceController * controller, NodeId remoteNodeId, app::Clusters::DiagnosticLogs::IntentEnum intent,
uint16_t timeout, jobject javaCallback);

DeviceController * mController;

Expand All @@ -67,20 +70,23 @@ class AndroidLogDownloadFromNode
char mFileDesignatorBuffer[bdx::DiagnosticLogs::kMaxFileDesignatorLen];
MutableCharSpan mFileDesignator = MutableCharSpan(mFileDesignatorBuffer, bdx::DiagnosticLogs::kMaxFileDesignatorLen);

BdxDiagnosticLogsReceiver *mBdxReceiver = nullptr;
BdxDiagnosticLogsReceiver * mBdxReceiver = nullptr;

CHIP_ERROR GetConnectedDevice();
CHIP_ERROR SendRetrieveLogsRequest(Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle);
void OnTransferCallback(FabricIndex fabricIndex, NodeId remoteNodeId, const chip::ByteSpan & data, CHIP_ERROR *errInfoOnFailure);
void OnTransferCallback(FabricIndex fabricIndex, NodeId remoteNodeId, const chip::ByteSpan & data,
CHIP_ERROR * errInfoOnFailure);
void FinishLogDownloadFromNode(CHIP_ERROR err);

static void OnDeviceConnectedFn(void * context, Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle);
static void OnDeviceConnectionFailureFn(void * context, const ScopedNodeId & peerId, CHIP_ERROR error);

static void OnResponseRetrieveLogs(void * context, const app::Clusters::DiagnosticLogs::Commands::RetrieveLogsResponse::DecodableType & data);
static void OnResponseRetrieveLogs(void * context,
const app::Clusters::DiagnosticLogs::Commands::RetrieveLogsResponse::DecodableType & data);
static void OnCommandFailure(void * context, CHIP_ERROR err);

static void OnBdxTransferCallback(void * context, FabricIndex fabricIndex, NodeId remoteNodeId, const chip::ByteSpan & data, CHIP_ERROR *errInfoOnFailure);
static void OnBdxTransferCallback(void * context, FabricIndex fabricIndex, NodeId remoteNodeId, const chip::ByteSpan & data,
CHIP_ERROR * errInfoOnFailure);
static void OnBdxTransferSuccessCallback(void * context, FabricIndex fabricIndex, NodeId remoteNodeId);
static void OnBdxTransferFailureCallback(void * context, FabricIndex fabricIndex, NodeId remoteNodeId, CHIP_ERROR status);
};
Expand Down
38 changes: 22 additions & 16 deletions src/controller/java/BdxDiagnosticLogsReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,26 @@ namespace Controller {

using namespace ::chip::DeviceLayer;

BdxDiagnosticLogsReceiver::BdxDiagnosticLogsReceiver(Callback::Callback<OnBdxTransfer> * onTransfer, Callback::Callback<OnBdxTransferSuccess> * onSuccess, Callback::Callback<OnBdxTransferFailure> * onFailure, chip::FabricIndex fabricIndex, chip::NodeId nodeId, chip::CharSpan fileDesignator)
BdxDiagnosticLogsReceiver::BdxDiagnosticLogsReceiver(Callback::Callback<OnBdxTransfer> * onTransfer,
Callback::Callback<OnBdxTransferSuccess> * onSuccess,
Callback::Callback<OnBdxTransferFailure> * onFailure,
chip::FabricIndex fabricIndex, chip::NodeId nodeId,
chip::CharSpan fileDesignator)
{
mOnBdxTransferCallback = onTransfer;
mOnBdxTransferCallback = onTransfer;
mOnBdxTransferSuccessCallback = onSuccess;
mOnBdxTransferFailureCallback = onFailure;

mFabricIndex = fabricIndex;
mNodeId = nodeId;
mFabricIndex = fabricIndex;
mNodeId = nodeId;
mFileDesignator = fileDesignator;
}

CHIP_ERROR BdxDiagnosticLogsReceiver::OnTransferBegin(chip::bdx::BDXTransferProxy * transfer)
{
chip::CharSpan fileDesignator = transfer->GetFileDesignator();
chip::FabricIndex fabricIndex = transfer->GetFabricIndex();
chip::NodeId nodeId = transfer->GetPeerNodeId();
chip::NodeId nodeId = transfer->GetPeerNodeId();

if (mFileDesignator.data_equal(fileDesignator) && mFabricIndex == fabricIndex && mNodeId == nodeId)
{
Expand All @@ -58,7 +62,7 @@ CHIP_ERROR BdxDiagnosticLogsReceiver::OnTransferEnd(chip::bdx::BDXTransferProxy
{
ChipLogProgress(Controller, "OnTransferEnd: %" CHIP_ERROR_FORMAT, error.Format());
chip::FabricIndex fabricIndex = transfer->GetFabricIndex();
chip::NodeId nodeId = transfer->GetPeerNodeId();
chip::NodeId nodeId = transfer->GetPeerNodeId();
if (error == CHIP_NO_ERROR)
{
mOnBdxTransferSuccessCallback->mCall(mOnBdxTransferCallback->mContext, fabricIndex, nodeId);
Expand All @@ -74,7 +78,7 @@ CHIP_ERROR BdxDiagnosticLogsReceiver::OnTransferData(chip::bdx::BDXTransferProxy
{
ChipLogProgress(Controller, "OnTransferData");
chip::FabricIndex fabricIndex = transfer->GetFabricIndex();
chip::NodeId nodeId = transfer->GetPeerNodeId();
chip::NodeId nodeId = transfer->GetPeerNodeId();

CHIP_ERROR err = CHIP_NO_ERROR;

Expand All @@ -93,21 +97,23 @@ CHIP_ERROR BdxDiagnosticLogsReceiver::OnTransferData(chip::bdx::BDXTransferProxy

CHIP_ERROR BdxDiagnosticLogsReceiver::StartBDXTransferTimeout(uint16_t timeoutInSeconds)
{
ChipLogProgress(Controller, "StartBDXTransferTimeout %u", timeoutInSeconds);
return chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds16(timeoutInSeconds), OnTransferTimeout, static_cast<void *>(this));
ChipLogProgress(Controller, "StartBDXTransferTimeout %u", timeoutInSeconds);
return chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds16(timeoutInSeconds), OnTransferTimeout,
static_cast<void *>(this));
}

void BdxDiagnosticLogsReceiver::CancelBDXTransferTimeout()
{
ChipLogProgress(Controller, "CancelBDXTransferTimeout");
chip::DeviceLayer::SystemLayer().CancelTimer(OnTransferTimeout, static_cast<void *>(this));
ChipLogProgress(Controller, "CancelBDXTransferTimeout");
chip::DeviceLayer::SystemLayer().CancelTimer(OnTransferTimeout, static_cast<void *>(this));
}

void BdxDiagnosticLogsReceiver::OnTransferTimeout(chip::System::Layer * layer, void * context)
{
ChipLogProgress(Controller, "OnTransferTimeout");
auto * self = static_cast<BdxDiagnosticLogsReceiver *>(context);
self->mOnBdxTransferFailureCallback->mCall(self->mOnBdxTransferFailureCallback->mContext, self->mFabricIndex, self->mNodeId, CHIP_ERROR_TIMEOUT);
ChipLogProgress(Controller, "OnTransferTimeout");
auto * self = static_cast<BdxDiagnosticLogsReceiver *>(context);
self->mOnBdxTransferFailureCallback->mCall(self->mOnBdxTransferFailureCallback->mContext, self->mFabricIndex, self->mNodeId,
CHIP_ERROR_TIMEOUT);
}
}
}
} // namespace Controller
} // namespace chip
Loading

0 comments on commit 7f85d1d

Please sign in to comment.