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 : mTemporaryCommissioningId = deviceId;
91 0 : mState = ConnectionState::Connecting;
92 :
93 0 : mDeviceAddress = peerAddress;
94 0 : }
95 :
96 : /**
97 : * @brief
98 : * Called when the associated session is released
99 : *
100 : * The receiver should release all resources associated with the connection.
101 : */
102 : void OnSessionReleased() override;
103 :
104 : /**
105 : * In case there exists an open session to the device, mark it as expired.
106 : */
107 : void CloseSession();
108 :
109 : /**
110 : * Detaches the underlying session (if any) from this proxy and returns it.
111 : */
112 : chip::Optional<SessionHandle> DetachSecureSession();
113 :
114 0 : void Disconnect() override { CloseSession(); }
115 :
116 : /**
117 : * @brief
118 : * Update data of the device.
119 : *
120 : * This function will set new IP address, port and MRP retransmission intervals of the device.
121 : *
122 : * @param[in] addr Address of the device to be set.
123 : * @param[in] config MRP parameters
124 : *
125 : * @return CHIP_NO_ERROR if the data has been updated, an error code otherwise.
126 : */
127 : CHIP_ERROR UpdateDeviceData(const Transport::PeerAddress & addr, const ReliableMessageProtocolConfig & config);
128 :
129 : /**
130 : * @brief
131 : * Called to indicate this proxy has been paired successfully.
132 : *
133 : * This stores the session details in the session manager.
134 : */
135 : CHIP_ERROR SetConnected(const SessionHandle & session);
136 :
137 0 : bool IsSecureConnected() const override { return mState == ConnectionState::SecureConnected; }
138 :
139 0 : bool IsSessionSetupInProgress() const { return mState == ConnectionState::Connecting; }
140 :
141 0 : NodeId GetDeviceId() const override { return mPeerId.GetNodeId(); }
142 : PeerId GetPeerId() const { return mPeerId; }
143 : CHIP_ERROR SetPeerId(ByteSpan rcac, ByteSpan noc) override;
144 0 : const Transport::PeerAddress & GetPeerAddress() const { return mDeviceAddress; }
145 :
146 0 : chip::Optional<SessionHandle> GetSecureSession() const override { return mSecureSession.Get(); }
147 :
148 0 : Messaging::ExchangeManager * GetExchangeManager() const override { return mExchangeMgr; }
149 :
150 0 : PASESession & GetPairing() { return mPairing; }
151 :
152 0 : Transport::Type GetDeviceTransportType() const { return mDeviceAddress.GetTransportType(); }
153 :
154 0 : NodeId GetTemporaryCommissioningId() const { return mTemporaryCommissioningId; }
155 :
156 : private:
157 : enum class ConnectionState
158 : {
159 : NotConnected,
160 : Connecting,
161 : SecureConnected,
162 : };
163 :
164 : /* Compressed fabric ID and node ID assigned to the device. */
165 : PeerId mPeerId;
166 :
167 : /*
168 : * mPeerId can change when we get a NOC, but for purposes of stopping
169 : * commissioning it's useful to allow clients to use the (possibly
170 : * temporary) ID they used as a commissioning process identifier.
171 : */
172 : NodeId mTemporaryCommissioningId;
173 :
174 : /** Address used to communicate with the device.
175 : */
176 : Transport::PeerAddress mDeviceAddress = Transport::PeerAddress::UDP(Inet::IPAddress::Any);
177 :
178 : ConnectionState mState = ConnectionState::NotConnected;
179 :
180 : PASESession mPairing;
181 :
182 : SessionManager * mSessionManager = nullptr;
183 :
184 : Messaging::ExchangeManager * mExchangeMgr = nullptr;
185 :
186 : SessionHolderWithDelegate mSecureSession;
187 : };
188 :
189 : } // namespace chip
|