Skip to content

Commit

Permalink
Add BDXTransferServer to src/protocols/bdx
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple committed Jan 19, 2024
1 parent a0fa83f commit 1d1a95a
Show file tree
Hide file tree
Showing 8 changed files with 589 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/protocols/bdx/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ static_library("bdx") {
sources = [
"BdxMessages.cpp",
"BdxMessages.h",
"BdxTransferProxy.h",
"BdxTransferProxyDiagnosticLog.cpp",
"BdxTransferProxyDiagnosticLog.h",
"BdxTransferServer.cpp",
"BdxTransferServer.h",
"BdxTransferSession.cpp",
"BdxTransferSession.h",
"BdxUri.cpp",
Expand Down
48 changes: 48 additions & 0 deletions src/protocols/bdx/BdxTransferProxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 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.
*
*/

#pragma once

#include <lib/core/CHIPError.h>
#include <lib/core/DataModelTypes.h>
#include <lib/core/NodeId.h>
#include <lib/support/Span.h>

namespace chip {
namespace bdx {

/**
* A proxy object to control the state of a specific BDX transfer.
*/

class BDXTransferProxy
{
public:
virtual ~BDXTransferProxy(){};

virtual CHIP_ERROR Accept() = 0;
virtual CHIP_ERROR Reject(CHIP_ERROR error) = 0;
virtual CHIP_ERROR Continue() = 0;

virtual CharSpan GetFileDesignator() const = 0;
virtual FabricIndex GetFabricIndex() const = 0;
virtual NodeId GetPeerNodeId() const = 0;
};

} // namespace bdx
} // namespace chip
98 changes: 98 additions & 0 deletions src/protocols/bdx/BdxTransferProxyDiagnosticLog.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
*
* Copyright (c) 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.
*/

#include "BdxTransferProxyDiagnosticLog.h"

#include <lib/core/CHIPSafeCasts.h>
#include <lib/support/CHIPMemString.h>
#include <platform/LockTracker.h>

namespace chip {
namespace bdx {

CHIP_ERROR BDXTransferProxyDiagnosticLog::Init(TransferSession * transferSession)
{
VerifyOrReturnError(nullptr != transferSession, CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(nullptr == mTransfer, CHIP_ERROR_INCORRECT_STATE);

uint16_t fileDesignatorLength = 0;
auto fileDesignator = transferSession->GetFileDesignator(fileDesignatorLength);

VerifyOrReturnError(fileDesignatorLength > 0, CHIP_ERROR_INVALID_STRING_LENGTH);
VerifyOrReturnError(fileDesignatorLength <= DiagnosticLogs::kMaxFileDesignatorLen, CHIP_ERROR_INVALID_STRING_LENGTH);

mTransfer = transferSession;
mFileDesignatorLen = static_cast<uint8_t>(fileDesignatorLength);
Platform::CopyString(mFileDesignator, ByteSpan(fileDesignator, fileDesignatorLength));
return CHIP_NO_ERROR;
}

CHIP_ERROR BDXTransferProxyDiagnosticLog::Accept()
{
ReturnErrorOnFailure(EnsureState());
VerifyOrReturnError(nullptr != mTransfer, CHIP_ERROR_INCORRECT_STATE);

TransferSession::TransferAcceptData acceptData;
acceptData.ControlMode = TransferControlFlags::kSenderDrive;
acceptData.MaxBlockSize = mTransfer->GetTransferBlockSize();
acceptData.StartOffset = mTransfer->GetStartOffset();
acceptData.Length = mTransfer->GetTransferLength();

return mTransfer->AcceptTransfer(acceptData);
}

CHIP_ERROR BDXTransferProxyDiagnosticLog::Reject(CHIP_ERROR error)
{
ReturnErrorOnFailure(EnsureState());
VerifyOrReturnError(nullptr != mTransfer, CHIP_ERROR_INCORRECT_STATE);

auto statusCode = GetBdxStatusCodeFromChipError(error);
return mTransfer->AbortTransfer(statusCode);
}

CHIP_ERROR BDXTransferProxyDiagnosticLog::Continue()
{
ReturnErrorOnFailure(EnsureState());
VerifyOrReturnError(nullptr != mTransfer, CHIP_ERROR_INCORRECT_STATE);

return mTransfer->PrepareBlockAck();
}

void BDXTransferProxyDiagnosticLog::Reset()
{
ReturnOnFailure(EnsureState());
VerifyOrReturn(nullptr != mTransfer);

memset(mFileDesignator, 0, sizeof(mFileDesignator));
mFileDesignatorLen = 0;
mTransfer = nullptr;
mFabricIndex = kUndefinedFabricIndex;
mPeerNodeId = kUndefinedNodeId;
}

CHIP_ERROR BDXTransferProxyDiagnosticLog::EnsureState() const
{
assertChipStackLockedByCurrentThread();
VerifyOrReturnError(kUndefinedFabricIndex != mFabricIndex, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(kUndefinedNodeId != mPeerNodeId, CHIP_ERROR_INCORRECT_STATE);

return CHIP_NO_ERROR;
}

} // namespace bdx
} // namespace chip
56 changes: 56 additions & 0 deletions src/protocols/bdx/BdxTransferProxyDiagnosticLog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 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.
*
*/

#pragma once

#include <protocols/bdx/BdxTransferProxy.h>
#include <protocols/bdx/BdxTransferSession.h>
#include <protocols/bdx/DiagnosticLogs.h>

namespace chip {
namespace bdx {

class BDXTransferProxyDiagnosticLog : public BDXTransferProxy
{
public:
CHIP_ERROR Init(TransferSession * transferSession);
void Reset();

void SetFabricIndex(FabricIndex fabricIndex) { mFabricIndex = fabricIndex; }
void SetPeerNodeId(NodeId nodeId) { mPeerNodeId = nodeId; }

CHIP_ERROR Accept() override;
CHIP_ERROR Reject(CHIP_ERROR error) override;
CHIP_ERROR Continue() override;

FabricIndex GetFabricIndex() const override { return mFabricIndex; }
NodeId GetPeerNodeId() const override { return mPeerNodeId; }
CharSpan GetFileDesignator() const override { return CharSpan(mFileDesignator, mFileDesignatorLen); };

private:
CHIP_ERROR EnsureState() const;

uint8_t mFileDesignatorLen = 0;
char mFileDesignator[DiagnosticLogs::kMaxFileDesignatorLen + 1] = {};
TransferSession * mTransfer = nullptr;
FabricIndex mFabricIndex = kUndefinedFabricIndex;
NodeId mPeerNodeId = kUndefinedNodeId;
};

} // namespace bdx
} // namespace chip
Loading

0 comments on commit 1d1a95a

Please sign in to comment.