-
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.
[ota-requestor] Add "ota query" shell command (#11810)
* Enable OTA Provider client cluster in lighting-app * Include client callbacks * Add shell commands for OTA requestor role ota query <fabric-index> <node-id> <ip-address> <udp-port> Connect to OTA provider node <node-id> and query a new software/firmware image. If available, automatically download and apply the upgrade. For nRF Connect platform use DFU Target library to store a downloaded image in the secondary image slot in flash, so that it can be swapped with the primary/current image by MCUBoot bootloader on a subsequent boot. * Enable OTA requestor for lighting-app * Restyled by gn * Enable OTA Requestor only when built with BUILD_WITH_DFU * Address review comments * Add endpoint-id as another "query" command argument. * Add "show-update" command to view udpate token of the current update. * Add "apply" command for sending ApplyUpdate request. * Re-generate ZAP Co-authored-by: Restyled.io <[email protected]>
- Loading branch information
Showing
26 changed files
with
1,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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# | ||
# Copyright (c) 2021 Project CHIP Authors | ||
# | ||
# 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. | ||
# | ||
|
||
CONFIG_CHIP_OTA_REQUESTOR=y |
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
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
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,142 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* | ||
* 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/support/TypeTraits.h> | ||
#include <lib/support/logging/CHIPLogging.h> | ||
#include <messaging/ExchangeContext.h> | ||
#include <protocols/bdx/BdxMessages.h> | ||
#include <protocols/bdx/BdxTransferSession.h> | ||
#include <protocols/bdx/TransferFacilitator.h> | ||
|
||
#include <dfu/dfu_target.h> | ||
#include <dfu/dfu_target_mcuboot.h> | ||
|
||
namespace chip { | ||
namespace Shell { | ||
|
||
class DFUManager : public bdx::Initiator | ||
{ | ||
public: | ||
void SetInitialExchange(Messaging::ExchangeContext * ec) { mExchangeCtx = ec; } | ||
CHIP_ERROR ApplyUpdate(); | ||
CHIP_ERROR DiscardUpdate(); | ||
|
||
private: | ||
void HandleTransferSessionOutput(bdx::TransferSession::OutputEvent & event) override; | ||
|
||
uint8_t mDfuBuffer[1024]; | ||
bool mIsTransferComplete = false; | ||
}; | ||
|
||
inline CHIP_ERROR DFUManager::ApplyUpdate() | ||
{ | ||
return System::MapErrorZephyr(dfu_target_done(true)); | ||
} | ||
|
||
inline CHIP_ERROR DFUManager::DiscardUpdate() | ||
{ | ||
return System::MapErrorZephyr(dfu_target_reset()); | ||
} | ||
|
||
inline void DFUManager::HandleTransferSessionOutput(bdx::TransferSession::OutputEvent & event) | ||
{ | ||
using OutputEventType = bdx::TransferSession::OutputEventType; | ||
using MessageType = bdx::MessageType; | ||
using SendMessageFlags = Messaging::SendMessageFlags; | ||
|
||
CHIP_ERROR error = CHIP_NO_ERROR; | ||
int status = 0; | ||
|
||
switch (event.EventType) | ||
{ | ||
case OutputEventType::kNone: | ||
if (mIsTransferComplete) | ||
{ | ||
ChipLogProgress(BDX, "Transfer complete!"); | ||
mTransfer.Reset(); | ||
mIsTransferComplete = false; | ||
} | ||
break; | ||
case OutputEventType::kMsgToSend: { | ||
Messaging::SendFlags flags; | ||
flags.Set(SendMessageFlags::kFromInitiator, event.msgTypeData.MessageType == to_underlying(MessageType::ReceiveInit)); | ||
flags.Set(SendMessageFlags::kExpectResponse, event.msgTypeData.MessageType != to_underlying(MessageType::BlockAckEOF)); | ||
|
||
VerifyOrReturn(mExchangeCtx != nullptr, ChipLogError(BDX, "Exchange context is null")); | ||
error = | ||
mExchangeCtx->SendMessage(event.msgTypeData.ProtocolId, event.msgTypeData.MessageType, std::move(event.MsgData), flags); | ||
VerifyOrReturn(error == CHIP_NO_ERROR, ChipLogError(BDX, "SendMessage() failed: %" CHIP_ERROR_FORMAT, error.Format())); | ||
break; | ||
} | ||
case OutputEventType::kAcceptReceived: | ||
ChipLogProgress(BDX, "Starting transfer of %uB", static_cast<unsigned>(event.transferAcceptData.Length)); | ||
|
||
dfu_target_mcuboot_set_buf(mDfuBuffer, sizeof(mDfuBuffer)); | ||
dfu_target_reset(); | ||
status = dfu_target_init(DFU_TARGET_IMAGE_TYPE_MCUBOOT, event.transferAcceptData.Length, nullptr); | ||
VerifyOrReturn(status == 0, ChipLogError(BDX, "dfu_target_init() failed: %d", status)); | ||
|
||
error = mTransfer.PrepareBlockQuery(); | ||
VerifyOrReturn(error == CHIP_NO_ERROR, | ||
ChipLogError(BDX, "PrepareBlockQuery() failed: %" CHIP_ERROR_FORMAT, error.Format())); | ||
break; | ||
case OutputEventType::kBlockReceived: | ||
ChipLogProgress(BDX, "Received %uB (total: %ukB)", static_cast<unsigned>(event.blockdata.Length), | ||
static_cast<unsigned>(mTransfer.GetNumBytesProcessed()) / 1024u); | ||
|
||
status = dfu_target_write(event.blockdata.Data, event.blockdata.Length); | ||
VerifyOrReturn(status == 0, ChipLogError(BDX, "dfu_target_write() failed: %d", status)); | ||
|
||
if (event.blockdata.IsEof) | ||
{ | ||
error = mTransfer.PrepareBlockAck(); | ||
VerifyOrReturn(error == CHIP_NO_ERROR, | ||
ChipLogError(BDX, "PrepareBlockAck() failed: %" CHIP_ERROR_FORMAT, error.Format())); | ||
mIsTransferComplete = true; | ||
} | ||
else | ||
{ | ||
error = mTransfer.PrepareBlockQuery(); | ||
VerifyOrReturn(error == CHIP_NO_ERROR, | ||
ChipLogError(BDX, "PrepareBlockQuery() failed: %" CHIP_ERROR_FORMAT, error.Format())); | ||
} | ||
break; | ||
case OutputEventType::kStatusReceived: | ||
ChipLogError(BDX, "Received status %" PRIu16, to_underlying(event.statusData.statusCode)); | ||
mTransfer.Reset(); | ||
dfu_target_reset(); | ||
break; | ||
case OutputEventType::kInternalError: | ||
ChipLogError(BDX, "Transfer stopped due to internal error"); | ||
mTransfer.Reset(); | ||
dfu_target_reset(); | ||
break; | ||
case OutputEventType::kTransferTimeout: | ||
ChipLogError(BDX, "Transfer timed out"); | ||
mTransfer.Reset(); | ||
dfu_target_reset(); | ||
break; | ||
default: | ||
ChipLogError(BDX, "Unexpected event type: %" PRIu16, to_underlying(event.EventType)); | ||
break; | ||
} | ||
} | ||
|
||
} // namespace Shell | ||
} // namespace chip |
Oops, something went wrong.