blob: 6509c84922a2703b44715afa05e4f871b51ee7b1 [file] [log] [blame]
/*
* 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))
{
VerifyOrReturnError(commissioneeDeviceProxy->GetSecureSession().HasValue(), CHIP_ERROR_INCORRECT_STATE);
return CloseSession(*commissioneeDeviceProxy->GetExchangeManager(), commissioneeDeviceProxy->GetSecureSession().Value());
}
return CurrentCommissioner().GetConnectedDevice(mDestinationId, &mOnDeviceConnectedCallback,
&mOnDeviceConnectionFailureCallback);
}
CHIP_ERROR CloseSessionCommand::CloseSession(Messaging::ExchangeManager & exchangeMgr, const SessionHandle & sessionHandle)
{
// 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 = exchangeMgr.NewContext(sessionHandle, 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, Messaging::ExchangeManager & exchangeMgr,
SessionHandle & sessionHandle)
{
auto * command = reinterpret_cast<CloseSessionCommand *>(context);
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnDeviceConnectedFn: context is null"));
CHIP_ERROR err = command->CloseSession(exchangeMgr, sessionHandle);
VerifyOrReturn(CHIP_NO_ERROR == err, command->SetCommandExitStatus(err));
}
void CloseSessionCommand::OnDeviceConnectionFailureFn(void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR err)
{
LogErrorOnFailure(err);
auto * command = reinterpret_cast<CloseSessionCommand *>(context);
VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnDeviceConnectionFailureFn: context is null"));
command->SetCommandExitStatus(err);
}