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::Optional<SessionHandle> CommissioneeDeviceProxy::DetachSecureSession()
70 : {
71 0 : auto session = mSecureSession.Get();
72 0 : mSecureSession.Release();
73 0 : mState = ConnectionState::NotConnected;
74 0 : mPairing.Clear();
75 0 : return session;
76 : }
77 :
78 0 : void CommissioneeDeviceProxy::UpdateDeviceData(const Transport::PeerAddress & addr, const ReliableMessageProtocolConfig & config)
79 : {
80 0 : mDeviceAddress = addr;
81 :
82 : // Initialize PASE session state with any MRP parameters that DNS-SD has provided.
83 : // It can be overridden by PASE session protocol messages that include MRP parameters.
84 0 : mPairing.SetRemoteMRPConfig(config);
85 :
86 0 : if (!mSecureSession)
87 : {
88 : // Nothing needs to be done here. It's not an error to not have a
89 : // secureSession. For one thing, we could have gotten an different
90 : // UpdateAddress already and that caused connections to be torn down and
91 : // whatnot.
92 0 : return;
93 : }
94 :
95 0 : Transport::SecureSession * secureSession = mSecureSession.Get().Value()->AsSecureSession();
96 0 : secureSession->SetPeerAddress(addr);
97 : }
98 :
99 0 : CHIP_ERROR CommissioneeDeviceProxy::SetConnected(const SessionHandle & session)
100 : {
101 0 : VerifyOrReturnError(mState == ConnectionState::Connecting, CHIP_ERROR_INCORRECT_STATE);
102 0 : VerifyOrReturnError(session->AsSecureSession()->IsPASESession(), CHIP_ERROR_INVALID_ARGUMENT);
103 :
104 0 : if (!mSecureSession.Grab(session))
105 : {
106 0 : mState = ConnectionState::NotConnected;
107 0 : return CHIP_ERROR_INTERNAL;
108 : }
109 :
110 0 : mState = ConnectionState::SecureConnected;
111 0 : return CHIP_NO_ERROR;
112 : }
113 :
114 0 : CommissioneeDeviceProxy::~CommissioneeDeviceProxy()
115 : {
116 0 : auto session = GetSecureSession();
117 0 : if (session.HasValue())
118 : {
119 0 : session.Value()->AsSecureSession()->MarkForEviction();
120 : }
121 0 : }
122 :
123 0 : CHIP_ERROR CommissioneeDeviceProxy::SetPeerId(ByteSpan rcac, ByteSpan noc)
124 : {
125 : CompressedFabricId compressedFabricId;
126 : NodeId nodeId;
127 0 : ReturnErrorOnFailure(Credentials::ExtractNodeIdCompressedFabricIdFromOpCerts(rcac, noc, compressedFabricId, nodeId));
128 0 : mPeerId = PeerId().SetCompressedFabricId(compressedFabricId).SetNodeId(nodeId);
129 0 : return CHIP_NO_ERROR;
130 : }
131 :
132 : } // namespace chip
|