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 : /**
109 : * Detaches the underlying session (if any) from this proxy and returns it.
110 : */
111 : chip::Optional<SessionHandle> DetachSecureSession();
112 :
113 0 : void Disconnect() override { CloseSession(); }
114 :
115 : /**
116 : * @brief
117 : * Update data of the device.
118 : *
119 : * This function will set new IP address, port and MRP retransmission intervals of the device.
120 : *
121 : * @param[in] addr Address of the device to be set.
122 : * @param[in] config MRP parameters
123 : *
124 : * @return CHIP_NO_ERROR if the data has been updated, an error code otherwise.
125 : */
126 : CHIP_ERROR UpdateDeviceData(const Transport::PeerAddress & addr, const ReliableMessageProtocolConfig & config);
127 :
128 : /**
129 : * @brief
130 : * Called to indicate this proxy has been paired successfully.
131 : *
132 : * This stores the session details in the session manager.
133 : */
134 : CHIP_ERROR SetConnected(const SessionHandle & session);
135 :
136 0 : bool IsSecureConnected() const override { return mState == ConnectionState::SecureConnected; }
137 :
138 0 : bool IsSessionSetupInProgress() const { return mState == ConnectionState::Connecting; }
139 :
140 0 : NodeId GetDeviceId() const override { return mPeerId.GetNodeId(); }
141 : PeerId GetPeerId() const { return mPeerId; }
142 : CHIP_ERROR SetPeerId(ByteSpan rcac, ByteSpan noc) override;
143 0 : const Transport::PeerAddress & GetPeerAddress() const { return mDeviceAddress; }
144 :
145 0 : chip::Optional<SessionHandle> GetSecureSession() const override { return mSecureSession.Get(); }
146 :
147 0 : Messaging::ExchangeManager * GetExchangeManager() const override { return mExchangeMgr; }
148 :
149 0 : PASESession & GetPairing() { return mPairing; }
150 :
151 0 : Transport::Type GetDeviceTransportType() const { return mDeviceAddress.GetTransportType(); }
152 :
153 : private:
154 : enum class ConnectionState
155 : {
156 : NotConnected,
157 : Connecting,
158 : SecureConnected,
159 : };
160 :
161 : /* Compressed fabric ID and node ID assigned to the device. */
162 : PeerId mPeerId;
163 :
164 : /** Address used to communicate with the device.
165 : */
166 : Transport::PeerAddress mDeviceAddress = Transport::PeerAddress::UDP(Inet::IPAddress::Any);
167 :
168 : ConnectionState mState = ConnectionState::NotConnected;
169 :
170 : PASESession mPairing;
171 :
172 : SessionManager * mSessionManager = nullptr;
173 :
174 : Messaging::ExchangeManager * mExchangeMgr = nullptr;
175 :
176 : SessionHolderWithDelegate mSecureSession;
177 : };
178 :
179 : } // namespace chip
|