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 definitions for DeviceProxy for a device that's undergoing 22 : * commissioning process. The objects of this will be used by Controller applications 23 : * to interact with the device. The class provides mechanism to construct, send and receive 24 : * messages to and from the corresponding CHIP devices. 25 : */ 26 : 27 : #pragma once 28 : 29 : #include <app/CommandSender.h> 30 : #include <app/DeviceProxy.h> 31 : #include <app/util/basic-types.h> 32 : #include <controller/CHIPDeviceControllerSystemState.h> 33 : #include <controller/OperationalCredentialsDelegate.h> 34 : #include <lib/core/CHIPCallback.h> 35 : #include <lib/core/CHIPCore.h> 36 : #include <lib/support/DLLUtil.h> 37 : #include <messaging/ExchangeContext.h> 38 : #include <messaging/ExchangeMgr.h> 39 : #include <messaging/Flags.h> 40 : #include <protocols/secure_channel/PASESession.h> 41 : #include <transport/SessionHolder.h> 42 : #include <transport/SessionManager.h> 43 : #include <transport/TransportMgr.h> 44 : #include <transport/raw/MessageHeader.h> 45 : #include <transport/raw/UDP.h> 46 : 47 : namespace chip { 48 : 49 : inline constexpr size_t kAttestationNonceLength = 32; 50 : 51 : struct ControllerDeviceInitParams 52 : { 53 : SessionManager * sessionManager = nullptr; 54 : Messaging::ExchangeManager * exchangeMgr = nullptr; 55 : }; 56 : 57 : class CommissioneeDeviceProxy : public DeviceProxy, public SessionDelegate 58 : { 59 : public: 60 : ~CommissioneeDeviceProxy() override; 61 0 : CommissioneeDeviceProxy() : mSecureSession(*this) {} 62 : CommissioneeDeviceProxy(const CommissioneeDeviceProxy &) = delete; 63 : 64 : /** 65 : * @brief 66 : * Send the command in internal command sender. 67 : */ 68 : CHIP_ERROR SendCommands(app::CommandSender * commandObj, Optional<System::Clock::Timeout> timeout) override; 69 : 70 : /** 71 : * @brief 72 : * Initialize a new device object with secure session manager, inet layer object, 73 : * and other device specific parameters. This variant of function is typically used when 74 : * a new device is paired, and the corresponding device object needs to updated with 75 : * all device specifc parameters (address, port, interface etc). 76 : * 77 : * This is not done as part of constructor so that the controller can have a list of 78 : * uninitialized/unpaired device objects. The object is initialized only when the device 79 : * is actually paired. 80 : * 81 : * @param[in] params Wrapper object for transport manager etc. 82 : * @param[in] deviceId Node ID of the device 83 : * @param[in] peerAddress The location of the peer. MUST be of type Transport::Type::kUdp 84 : */ 85 0 : void Init(ControllerDeviceInitParams params, NodeId deviceId, const Transport::PeerAddress & peerAddress) 86 : { 87 0 : mSessionManager = params.sessionManager; 88 0 : mExchangeMgr = params.exchangeMgr; 89 0 : mPeerId = PeerId().SetNodeId(deviceId); 90 0 : mState = ConnectionState::Connecting; 91 : 92 0 : mDeviceAddress = peerAddress; 93 0 : } 94 : 95 : /** 96 : * @brief 97 : * Called when the associated session is released 98 : * 99 : * The receiver should release all resources associated with the connection. 100 : */ 101 : void OnSessionReleased() override; 102 : 103 : /** 104 : * In case there exists an open session to the device, mark it as expired. 105 : */ 106 : void CloseSession(); 107 : 108 0 : void Disconnect() override { CloseSession(); } 109 : 110 : /** 111 : * @brief 112 : * Update data of the device. 113 : * 114 : * This function will set new IP address, port and MRP retransmission intervals of the device. 115 : * 116 : * @param[in] addr Address of the device to be set. 117 : * @param[in] config MRP parameters 118 : * 119 : * @return CHIP_NO_ERROR if the data has been updated, an error code otherwise. 120 : */ 121 : CHIP_ERROR UpdateDeviceData(const Transport::PeerAddress & addr, const ReliableMessageProtocolConfig & config); 122 : 123 : /** 124 : * @brief 125 : * Called to indicate this proxy has been paired successfully. 126 : * 127 : * This stores the session details in the session manager. 128 : */ 129 : CHIP_ERROR SetConnected(const SessionHandle & session); 130 : 131 0 : bool IsSecureConnected() const override { return mState == ConnectionState::SecureConnected; } 132 : 133 0 : bool IsSessionSetupInProgress() const { return mState == ConnectionState::Connecting; } 134 : 135 0 : NodeId GetDeviceId() const override { return mPeerId.GetNodeId(); } 136 : PeerId GetPeerId() const { return mPeerId; } 137 : CHIP_ERROR SetPeerId(ByteSpan rcac, ByteSpan noc) override; 138 0 : const Transport::PeerAddress & GetPeerAddress() const { return mDeviceAddress; } 139 : 140 0 : chip::Optional<SessionHandle> GetSecureSession() const override { return mSecureSession.Get(); } 141 : 142 0 : Messaging::ExchangeManager * GetExchangeManager() const override { return mExchangeMgr; } 143 : 144 0 : PASESession & GetPairing() { return mPairing; } 145 : 146 0 : Transport::Type GetDeviceTransportType() const { return mDeviceAddress.GetTransportType(); } 147 : 148 : private: 149 : enum class ConnectionState 150 : { 151 : NotConnected, 152 : Connecting, 153 : SecureConnected, 154 : }; 155 : 156 : /* Compressed fabric ID and node ID assigned to the device. */ 157 : PeerId mPeerId; 158 : 159 : /** Address used to communicate with the device. 160 : */ 161 : Transport::PeerAddress mDeviceAddress = Transport::PeerAddress::UDP(Inet::IPAddress::Any); 162 : 163 : ConnectionState mState = ConnectionState::NotConnected; 164 : 165 : PASESession mPairing; 166 : 167 : SessionManager * mSessionManager = nullptr; 168 : 169 : Messaging::ExchangeManager * mExchangeMgr = nullptr; 170 : 171 : SessionHolderWithDelegate mSecureSession; 172 : }; 173 : 174 : } // namespace chip