Line data Source code
1 : /** 2 : * Copyright (c) 2020 Project CHIP Authors 3 : * All rights reserved. 4 : * 5 : * Licensed under the Apache License, Version 2.0 (the "License"); 6 : * you may not use this file except in compliance with the License. 7 : * You may obtain a copy of the License at 8 : * 9 : * http://www.apache.org/licenses/LICENSE-2.0 10 : * 11 : * Unless required by applicable law or agreed to in writing, software 12 : * distributed under the License is distributed on an "AS IS" BASIS, 13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 : * See the License for the specific language governing permissions and 15 : * limitations under the License. 16 : */ 17 : 18 : #include "UnsolicitedStatusHandler.h" 19 : #include <lib/support/TypeTraits.h> 20 : #include <protocols/secure_channel/Constants.h> 21 : #include <protocols/secure_channel/StatusReport.h> 22 : 23 : using namespace chip; 24 : using namespace chip::Protocols; 25 : using namespace chip::Protocols::SecureChannel; 26 : using namespace chip::Messaging; 27 : 28 1 : CHIP_ERROR UnsolicitedStatusHandler::Init(ExchangeManager * exchangeManager) 29 : { 30 1 : return exchangeManager->RegisterUnsolicitedMessageHandlerForType(SecureChannel::Id, to_underlying(MsgType::StatusReport), this); 31 : } 32 : 33 0 : CHIP_ERROR UnsolicitedStatusHandler::OnMessageReceived(ExchangeContext * ec, const PayloadHeader & payloadHeader, 34 : System::PacketBufferHandle && payload) 35 : { 36 0 : if (!payloadHeader.HasMessageType(MsgType::StatusReport)) 37 : { 38 0 : return CHIP_ERROR_INCORRECT_STATE; 39 : } 40 : 41 0 : auto session = ec->GetSessionHandle(); 42 0 : if (!session->IsSecureSession()) 43 : { 44 : // Nothing to do here. We only care about CloseSession on secure 45 : // sessions. 46 0 : return CHIP_NO_ERROR; 47 : } 48 : 49 0 : StatusReport report; 50 0 : ReturnErrorOnFailure(report.Parse(std::move(payload))); 51 : 52 0 : if (report.GetGeneralCode() == GeneralStatusCode::kSuccess && report.GetProtocolCode() == kProtocolCodeCloseSession) 53 : { 54 0 : ChipLogProgress(ExchangeManager, "Received CloseSession status message, closing session"); 55 0 : session->AsSecureSession()->MarkForEviction(); 56 0 : return CHIP_NO_ERROR; 57 : } 58 : 59 : // Just ignore this message. 60 0 : return CHIP_NO_ERROR; 61 0 : } 62 : 63 0 : void UnsolicitedStatusHandler::OnResponseTimeout(ExchangeContext * ec) 64 : { 65 : // We should never get here, since we never send messages ourselves. 66 0 : } 67 : 68 0 : CHIP_ERROR UnsolicitedStatusHandler::OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, 69 : ExchangeDelegate *& newDelegate) 70 : { 71 0 : newDelegate = this; 72 0 : return CHIP_NO_ERROR; 73 : }