Line data Source code
1 : /* 2 : * 3 : * Copyright (c) 2020-2021 Project CHIP Authors 4 : * All rights reserved. 5 : * 6 : * Licensed under the Apache License, Version 2.0 (the "License"); 7 : * you may not use this file except in compliance with the License. 8 : * You may obtain a copy of the License at 9 : * 10 : * http://www.apache.org/licenses/LICENSE-2.0 11 : * 12 : * Unless required by applicable law or agreed to in writing, software 13 : * distributed under the License is distributed on an "AS IS" BASIS, 14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 : * See the License for the specific language governing permissions and 16 : * limitations under the License. 17 : */ 18 : 19 : /** 20 : * @file 21 : * This file contains implementation of Device class. The objects of this 22 : * class will be used by Controller applications to interact with CHIP 23 : * devices. The class provides mechanism to construct, send and receive 24 : * messages to and from the corresponding CHIP devices. 25 : */ 26 : 27 : #include <controller/CommissioneeDeviceProxy.h> 28 : 29 : #include <app/CommandSender.h> 30 : #include <app/ReadPrepareParams.h> 31 : #include <app/util/DataModelHandler.h> 32 : #include <lib/core/CHIPCore.h> 33 : #include <lib/core/CHIPEncoding.h> 34 : #include <lib/core/CHIPSafeCasts.h> 35 : #include <lib/core/ErrorStr.h> 36 : #include <lib/support/CodeUtils.h> 37 : #include <lib/support/SafeInt.h> 38 : #include <lib/support/logging/CHIPLogging.h> 39 : 40 : using namespace chip::Callback; 41 : 42 : namespace chip { 43 : 44 0 : CHIP_ERROR CommissioneeDeviceProxy::SendCommands(app::CommandSender * commandObj, Optional<System::Clock::Timeout> timeout) 45 : { 46 0 : VerifyOrReturnError(mSecureSession, CHIP_ERROR_INCORRECT_STATE); 47 0 : VerifyOrReturnError(commandObj != nullptr, CHIP_ERROR_INVALID_ARGUMENT); 48 0 : VerifyOrReturnError(mSecureSession, CHIP_ERROR_MISSING_SECURE_SESSION); 49 0 : return commandObj->SendCommandRequest(mSecureSession.Get().Value(), timeout); 50 : } 51 : 52 0 : void CommissioneeDeviceProxy::OnSessionReleased() 53 : { 54 0 : mState = ConnectionState::NotConnected; 55 0 : } 56 : 57 0 : void CommissioneeDeviceProxy::CloseSession() 58 : { 59 0 : VerifyOrReturn(mState == ConnectionState::SecureConnected); 60 0 : if (mSecureSession) 61 : { 62 0 : mSecureSession->AsSecureSession()->MarkForEviction(); 63 : } 64 : 65 0 : mState = ConnectionState::NotConnected; 66 0 : mPairing.Clear(); 67 : } 68 : 69 0 : CHIP_ERROR CommissioneeDeviceProxy::UpdateDeviceData(const Transport::PeerAddress & addr, 70 : const ReliableMessageProtocolConfig & config) 71 : { 72 0 : mDeviceAddress = addr; 73 : 74 : // Initialize PASE session state with any MRP parameters that DNS-SD has provided. 75 : // It can be overridden by PASE session protocol messages that include MRP parameters. 76 0 : mPairing.SetRemoteMRPConfig(config); 77 : 78 0 : if (!mSecureSession) 79 : { 80 : // Nothing needs to be done here. It's not an error to not have a 81 : // secureSession. For one thing, we could have gotten an different 82 : // UpdateAddress already and that caused connections to be torn down and 83 : // whatnot. 84 0 : return CHIP_NO_ERROR; 85 : } 86 : 87 0 : Transport::SecureSession * secureSession = mSecureSession.Get().Value()->AsSecureSession(); 88 0 : secureSession->SetPeerAddress(addr); 89 : 90 0 : return CHIP_NO_ERROR; 91 : } 92 : 93 0 : CHIP_ERROR CommissioneeDeviceProxy::SetConnected(const SessionHandle & session) 94 : { 95 0 : VerifyOrReturnError(mState == ConnectionState::Connecting, CHIP_ERROR_INCORRECT_STATE); 96 0 : VerifyOrReturnError(session->AsSecureSession()->IsPASESession(), CHIP_ERROR_INVALID_ARGUMENT); 97 : 98 0 : if (!mSecureSession.Grab(session)) 99 : { 100 0 : mState = ConnectionState::NotConnected; 101 0 : return CHIP_ERROR_INTERNAL; 102 : } 103 : 104 0 : mState = ConnectionState::SecureConnected; 105 0 : return CHIP_NO_ERROR; 106 : } 107 : 108 0 : CommissioneeDeviceProxy::~CommissioneeDeviceProxy() 109 : { 110 0 : auto session = GetSecureSession(); 111 0 : if (session.HasValue()) 112 : { 113 0 : session.Value()->AsSecureSession()->MarkForEviction(); 114 : } 115 0 : } 116 : 117 0 : CHIP_ERROR CommissioneeDeviceProxy::SetPeerId(ByteSpan rcac, ByteSpan noc) 118 : { 119 : CompressedFabricId compressedFabricId; 120 : NodeId nodeId; 121 0 : ReturnErrorOnFailure(Credentials::ExtractNodeIdCompressedFabricIdFromOpCerts(rcac, noc, compressedFabricId, nodeId)); 122 0 : mPeerId = PeerId().SetCompressedFabricId(compressedFabricId).SetNodeId(nodeId); 123 0 : return CHIP_NO_ERROR; 124 : } 125 : 126 : } // namespace chip