Skip to content

Commit

Permalink
[OTA] Serve the file specified by the file designator (#15662)
Browse files Browse the repository at this point in the history
  • Loading branch information
carol-apple authored Mar 3, 2022
1 parent cc9af1c commit 1fd382d
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ class BdxOtaSender : public chip::bdx::Responder
*/
uint64_t GetTransferLength(void);

/* ota-provider-common/OTAProviderExample.cpp requires this */
void SetFilepath(const char * path) {}

private:
// Inherited from bdx::TransferFacilitator
void HandleTransferSessionOutput(chip::bdx::TransferSession::OutputEvent & event) override;
Expand Down
27 changes: 12 additions & 15 deletions examples/ota-provider-app/ota-provider-common/BdxOtaSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,7 @@ using chip::bdx::TransferSession;

BdxOtaSender::BdxOtaSender()
{
memset(mFilepath, 0, kFilepathMaxLength);
}

void BdxOtaSender::SetFilepath(const char * path)
{
if (path != nullptr)
{
chip::Platform::CopyString(mFilepath, path);
}
else
{
memset(mFilepath, 0, kFilepathMaxLength);
}
memset(mFileDesignator, 0, chip::bdx::kMaxFileDesignatorLen);
}

void BdxOtaSender::HandleTransferSessionOutput(TransferSession::OutputEvent & event)
Expand Down Expand Up @@ -88,6 +76,15 @@ void BdxOtaSender::HandleTransferSessionOutput(TransferSession::OutputEvent & ev
acceptData.Length = mTransfer.GetTransferLength();
VerifyOrReturn(mTransfer.AcceptTransfer(acceptData) == CHIP_NO_ERROR,
ChipLogError(BDX, "%s: %s", __FUNCTION__, chip::ErrorStr(err)));

// Store the file designator used during block query
uint16_t fdl = 0;
const uint8_t * fd = mTransfer.GetFileDesignator(fdl);
VerifyOrReturn(fdl < chip::bdx::kMaxFileDesignatorLen,
ChipLogError(BDX, "Cannot store file designator with length = %d", fdl));
memcpy(mFileDesignator, fd, fdl);
mFileDesignator[fdl] = 0;

break;
}
case TransferSession::OutputEventType::kQueryReceived: {
Expand All @@ -110,7 +107,7 @@ void BdxOtaSender::HandleTransferSessionOutput(TransferSession::OutputEvent & ev
return;
}

std::ifstream otaFile(mFilepath, std::ifstream::in);
std::ifstream otaFile(mFileDesignator, std::ifstream::in);
VerifyOrReturn(otaFile.good(), ChipLogError(BDX, "%s: file read failed", __FUNCTION__));
otaFile.seekg(mNumBytesSent);
otaFile.read(reinterpret_cast<char *>(blockBuf->Start()), bytesToRead);
Expand Down Expand Up @@ -162,5 +159,5 @@ void BdxOtaSender::Reset()
}

mNumBytesSent = 0;
memset(mFilepath, 0, kFilepathMaxLength);
memset(mFileDesignator, 0, chip::bdx::kMaxFileDesignatorLen);
}
6 changes: 2 additions & 4 deletions examples/ota-provider-app/ota-provider-common/BdxOtaSender.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ class BdxOtaSender : public chip::bdx::Responder
public:
BdxOtaSender();

void SetFilepath(const char * path);

private:
// Inherited from bdx::TransferFacilitator
void HandleTransferSessionOutput(chip::bdx::TransferSession::OutputEvent & event) override;

void Reset();

static constexpr size_t kFilepathMaxLength = 256;
char mFilepath[kFilepathMaxLength];
// Null-terminated string representing file designator
char mFileDesignator[chip::bdx::kMaxFileDesignatorLen];

uint32_t mNumBytesSent = 0;
};
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ EmberAfStatus OTAProviderExample::HandleQueryImage(chip::app::CommandHandler * c
ChipLogDetail(SoftwareUpdate, "Generated URI: %.*s", static_cast<int>(uri.size()), uri.data());

// Initialize the transfer session in prepartion for a BDX transfer
mBdxOtaSender.SetFilepath(otaFilePath);
BitFlags<TransferControlFlags> bdxFlags;
bdxFlags.Set(TransferControlFlags::kReceiverDrive);
CHIP_ERROR err = mBdxOtaSender.PrepareForTransfer(&chip::DeviceLayer::SystemLayer(), chip::bdx::TransferRole::kSender,
Expand Down
21 changes: 17 additions & 4 deletions src/app/clusters/ota-requestor/OTARequestor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ void OTARequestor::OnQueryImageResponse(void * context, const QueryImageResponse
requestorCore->mTargetVersion = update.softwareVersion;
requestorCore->mUpdateToken = updateToken;

// Store file designator needed for BDX transfers
MutableCharSpan fileDesignator(requestorCore->mFileDesignatorBuffer);
if (update.fileDesignator.size() > fileDesignator.size())
{
ChipLogError(SoftwareUpdate, "File designator size %zu is too large to store", update.fileDesignator.size());
requestorCore->RecordErrorUpdateState(UpdateFailureState::kQuerying, err);
return;
}
memcpy(fileDesignator.data(), update.fileDesignator.data(), update.fileDesignator.size());
fileDesignator.reduce_size(update.fileDesignator.size());
requestorCore->mFileDesignator = fileDesignator;

requestorCore->mOtaRequestorDriver->UpdateAvailable(update,
System::Clock::Seconds32(response.delayedActionTime.ValueOr(0)));
}
Expand Down Expand Up @@ -656,7 +668,9 @@ CHIP_ERROR OTARequestor::ExtractUpdateDescription(const QueryImageResponseDecoda

VerifyOrReturnError(response.imageURI.HasValue(), CHIP_ERROR_INVALID_ARGUMENT);
ReturnErrorOnFailure(bdx::ParseURI(response.imageURI.Value(), nodeId, fileDesignator));
update.imageURI = response.imageURI.Value();
VerifyOrReturnError(IsSpanUsable(fileDesignator), CHIP_ERROR_INVALID_ARGUMENT);
update.nodeId = nodeId;
update.fileDesignator = fileDesignator;

VerifyOrReturnError(response.softwareVersion.HasValue(), CHIP_ERROR_INVALID_ARGUMENT);
VerifyOrReturnError(response.softwareVersionString.HasValue(), CHIP_ERROR_INVALID_ARGUMENT);
Expand All @@ -681,9 +695,8 @@ CHIP_ERROR OTARequestor::StartDownload(OperationalDeviceProxy & deviceProxy)
TransferSession::TransferInitData initOptions;
initOptions.TransferCtlFlags = bdx::TransferControlFlags::kReceiverDrive;
initOptions.MaxBlockSize = mOtaRequestorDriver->GetMaxDownloadBlockSize();
char testFileDes[9] = { "test.txt" };
initOptions.FileDesLength = static_cast<uint16_t>(strlen(testFileDes));
initOptions.FileDesignator = reinterpret_cast<uint8_t *>(testFileDes);
initOptions.FileDesLength = static_cast<uint16_t>(mFileDesignator.size());
initOptions.FileDesignator = reinterpret_cast<const uint8_t *>(mFileDesignator.data());

chip::Messaging::ExchangeManager * exchangeMgr = deviceProxy.GetExchangeManager();
VerifyOrReturnError(exchangeMgr != nullptr, CHIP_ERROR_INCORRECT_STATE);
Expand Down
6 changes: 4 additions & 2 deletions src/app/clusters/ota-requestor/OTARequestor.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,10 @@ class OTARequestor : public OTARequestorInterface, public BDXDownloader::StateDe
BDXMessenger mBdxMessenger; // TODO: ideally this is held by the application
uint8_t mUpdateTokenBuffer[kMaxUpdateTokenLen];
ByteSpan mUpdateToken;
uint32_t mCurrentVersion = 0;
uint32_t mTargetVersion = 0;
uint32_t mCurrentVersion = 0;
uint32_t mTargetVersion = 0;
char mFileDesignatorBuffer[bdx::kMaxFileDesignatorLen];
CharSpan mFileDesignator;
OTAUpdateStateEnum mCurrentUpdateState = OTAUpdateStateEnum::kIdle;
Server * mServer = nullptr;
chip::Optional<bool> mRequestorCanConsent;
Expand Down
3 changes: 2 additions & 1 deletion src/include/platform/OTARequestorDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ namespace chip {
// The set of parameters needed for starting a BDX download.
struct UpdateDescription
{
CharSpan imageURI;
NodeId nodeId;
CharSpan fileDesignator;
uint32_t softwareVersion;
CharSpan softwareVersionStr;
ByteSpan updateToken;
Expand Down
3 changes: 1 addition & 2 deletions src/protocols/bdx/BdxMessages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
#include <utility>

namespace {
constexpr uint8_t kVersionMask = 0x0F;
constexpr uint8_t kMaxFileDesignatorLen = 32;
constexpr uint8_t kVersionMask = 0x0F;
} // namespace

using namespace chip;
Expand Down
2 changes: 2 additions & 0 deletions src/protocols/bdx/BdxMessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
namespace chip {
namespace bdx {

constexpr uint16_t kMaxFileDesignatorLen = 0xFF;

enum class MessageType : uint8_t
{
SendInit = 0x01,
Expand Down
5 changes: 5 additions & 0 deletions src/protocols/bdx/BdxTransferSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ class DLL_EXPORT TransferSession
uint64_t GetTransferLength() const { return mTransferLength; }
uint16_t GetTransferBlockSize() const { return mTransferMaxBlockSize; }
size_t GetNumBytesProcessed() const { return mNumBytesProcessed; }
const uint8_t * GetFileDesignator(uint16_t & fileDesignatorLen) const
{
fileDesignatorLen = mTransferRequestData.FileDesLength;
return mTransferRequestData.FileDesignator;
}

TransferSession();

Expand Down

0 comments on commit 1fd382d

Please sign in to comment.