-
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.
Add a way to send a CloseSession StatusReport via chip-tool. (#19641)
Specific changes: 1) Add a way to create a Protocols::Id from a 32-bit spec representation. 2) Change StatusReport to take Protocols::Id, not uint32_t. 3) Fix chip-tool interactive mode to log failures when a command fails to run. 4) Add a chip-tool command to send a CloseSession StatusReport.
- Loading branch information
1 parent
5748770
commit d3107fb
Showing
12 changed files
with
185 additions
and
21 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
88 changes: 88 additions & 0 deletions
88
examples/chip-tool/commands/pairing/CloseSessionCommand.cpp
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,88 @@ | ||
/* | ||
* Copyright (c) 2022 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 "CloseSessionCommand.h" | ||
#include <protocols/secure_channel/StatusReport.h> | ||
#include <system/SystemPacketBuffer.h> | ||
|
||
using namespace chip; | ||
using namespace chip::Protocols; | ||
|
||
CHIP_ERROR CloseSessionCommand::RunCommand() | ||
{ | ||
CommissioneeDeviceProxy * commissioneeDeviceProxy = nullptr; | ||
if (CHIP_NO_ERROR == CurrentCommissioner().GetDeviceBeingCommissioned(mDestinationId, &commissioneeDeviceProxy)) | ||
{ | ||
return CloseSession(commissioneeDeviceProxy); | ||
} | ||
|
||
return CurrentCommissioner().GetConnectedDevice(mDestinationId, &mOnDeviceConnectedCallback, | ||
&mOnDeviceConnectionFailureCallback); | ||
} | ||
|
||
CHIP_ERROR CloseSessionCommand::CloseSession(DeviceProxy * device) | ||
{ | ||
VerifyOrReturnError(device->GetSecureSession().HasValue(), CHIP_ERROR_INCORRECT_STATE); | ||
|
||
// TODO perhaps factor out this code into something on StatusReport that | ||
// takes an exchange and maybe a SendMessageFlags? | ||
SecureChannel::StatusReport statusReport(SecureChannel::GeneralStatusCode::kSuccess, SecureChannel::Id, | ||
SecureChannel::kProtocolCodeCloseSession); | ||
|
||
size_t reportSize = statusReport.Size(); | ||
Encoding::LittleEndian::PacketBufferWriter bbuf(MessagePacketBuffer::New(reportSize), reportSize); | ||
statusReport.WriteToBuffer(bbuf); | ||
|
||
System::PacketBufferHandle msg = bbuf.Finalize(); | ||
VerifyOrReturnError(!msg.IsNull(), CHIP_ERROR_NO_MEMORY); | ||
|
||
auto * exchange = device->GetExchangeManager()->NewContext(device->GetSecureSession().Value(), nullptr); | ||
VerifyOrReturnError(exchange != nullptr, CHIP_ERROR_NO_MEMORY); | ||
|
||
// Per spec, CloseSession reports are always sent with MRP disabled. | ||
CHIP_ERROR err = | ||
exchange->SendMessage(SecureChannel::MsgType::StatusReport, std::move(msg), Messaging::SendMessageFlags::kNoAutoRequestAck); | ||
if (err == CHIP_NO_ERROR) | ||
{ | ||
SetCommandExitStatus(CHIP_NO_ERROR); | ||
} | ||
else | ||
{ | ||
exchange->Close(); | ||
} | ||
|
||
return err; | ||
} | ||
|
||
void CloseSessionCommand::OnDeviceConnectedFn(void * context, OperationalDeviceProxy * device) | ||
{ | ||
auto * command = reinterpret_cast<CloseSessionCommand *>(context); | ||
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnDeviceConnectedFn: context is null")); | ||
|
||
CHIP_ERROR err = command->CloseSession(device); | ||
VerifyOrReturn(CHIP_NO_ERROR == err, command->SetCommandExitStatus(err)); | ||
} | ||
|
||
void CloseSessionCommand::OnDeviceConnectionFailureFn(void * context, PeerId peerId, CHIP_ERROR err) | ||
{ | ||
LogErrorOnFailure(err); | ||
|
||
auto * command = reinterpret_cast<CloseSessionCommand *>(context); | ||
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnDeviceConnectionFailureFn: context is null")); | ||
command->SetCommandExitStatus(err); | ||
} |
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,57 @@ | ||
/* | ||
* Copyright (c) 2022 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 "../common/CHIPCommand.h" | ||
#include <app/OperationalDeviceProxy.h> | ||
#include <lib/core/CHIPCallback.h> | ||
#include <lib/core/DataModelTypes.h> | ||
|
||
class CloseSessionCommand : public CHIPCommand | ||
{ | ||
public: | ||
CloseSessionCommand(CredentialIssuerCommands * credIssuerCommands) : | ||
CHIPCommand("close-session", credIssuerCommands), mOnDeviceConnectedCallback(OnDeviceConnectedFn, this), | ||
mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this) | ||
{ | ||
AddArgument("destination-id", 0, UINT64_MAX, &mDestinationId); | ||
AddArgument("timeout", 0, UINT64_MAX, &mTimeoutSecs, | ||
"Time, in seconds, before this command is considered to have timed out."); | ||
} | ||
|
||
/////////// CHIPCommand Interface ///////// | ||
CHIP_ERROR RunCommand() override; | ||
chip::System::Clock::Timeout GetWaitDuration() const override | ||
{ | ||
return chip::System::Clock::Seconds16(mTimeoutSecs.ValueOr(10)); | ||
} | ||
|
||
private: | ||
chip::NodeId mDestinationId; | ||
chip::Optional<uint16_t> mTimeoutSecs; | ||
|
||
static void OnDeviceConnectedFn(void * context, chip::OperationalDeviceProxy * device); | ||
static void OnDeviceConnectionFailureFn(void * context, PeerId peerId, CHIP_ERROR error); | ||
|
||
// Try to send the action CloseSession status report. | ||
CHIP_ERROR CloseSession(chip::DeviceProxy * device); | ||
|
||
chip::Callback::Callback<chip::OnDeviceConnected> mOnDeviceConnectedCallback; | ||
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback; | ||
}; |
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