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