Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #pragma once
19 :
20 : #include <optional>
21 :
22 : #include <transport/raw/Base.h>
23 : #include <transport/raw/PeerAddress.h>
24 : #if CONFIG_NETWORK_LAYER_BLE
25 : #include <ble/Ble.h>
26 : #endif // CONFIG_NETWORK_LAYER_BLE
27 :
28 : #include <crypto/CHIPCryptoPAL.h>
29 : #include <lib/support/SetupDiscriminator.h>
30 : #include <lib/support/logging/CHIPLogging.h>
31 : #include <messaging/ReliableMessageProtocolConfig.h>
32 : #include <protocols/secure_channel/PASESession.h>
33 :
34 : namespace chip {
35 :
36 : // The largest supported value for Rendezvous discriminators
37 : const uint16_t kMaxRendezvousDiscriminatorValue = 0xFFF;
38 :
39 : // The largest supported value for session idle interval and session active interval
40 : inline constexpr uint32_t kMaxSessionIdleInterval = 3600000;
41 :
42 : class RendezvousParameters
43 : {
44 : public:
45 0 : RendezvousParameters() = default;
46 :
47 : bool HasSetupPINCode() const { return mSetupPINCode != 0; }
48 0 : uint32_t GetSetupPINCode() const { return mSetupPINCode; }
49 0 : RendezvousParameters & SetSetupPINCode(uint32_t setupPINCode)
50 : {
51 0 : mSetupPINCode = setupPINCode;
52 0 : return *this;
53 : }
54 :
55 : bool HasPeerAddress() const { return mPeerAddress.IsInitialized(); }
56 0 : Transport::PeerAddress GetPeerAddress() const { return mPeerAddress; }
57 0 : RendezvousParameters & SetPeerAddress(const Transport::PeerAddress & peerAddress)
58 : {
59 0 : mPeerAddress = peerAddress;
60 0 : return *this;
61 : }
62 :
63 : // Discriminators in RendezvousParameters are always long (12-bit)
64 : // discriminators.
65 0 : bool HasDiscriminator() const { return mSetupDiscriminator.has_value(); }
66 :
67 : // Obtains the long version of the discriminator, or 0 if short.
68 : // WARNING: This is lossy and a bad idea to use. The correct method to use
69 : // is GetSetupDiscriminator(). This method exists for public
70 : // API backwards compatibility.
71 : uint16_t GetDiscriminator() const
72 : {
73 : if (!mSetupDiscriminator.has_value())
74 : {
75 : ChipLogError(Discovery,
76 : "Get RendezvousParameters::GetDiscriminator() called without discriminator in params (inconsistent). "
77 : "Using value 0 to avoid crash! Ensure discriminator is set!");
78 : return 0;
79 : }
80 :
81 : if (mSetupDiscriminator.value().IsShortDiscriminator())
82 : {
83 : ChipLogError(Discovery,
84 : "Get RendezvousParameters::GetDiscriminator() called with SHORT discriminator (inconsistent). Using value "
85 : "0 to avoid crash! Call GetSetupDiscriminator() to avoid loss.");
86 : return 0;
87 : }
88 :
89 : return mSetupDiscriminator.value().GetLongValue();
90 : }
91 :
92 0 : std::optional<SetupDiscriminator> GetSetupDiscriminator() const
93 : {
94 0 : if (!mSetupDiscriminator.has_value())
95 : {
96 0 : ChipLogError(
97 : Discovery,
98 : "Get RendezvousParameters::GetSetupDiscriminator() called without discriminator in params (inconsistent).");
99 : }
100 0 : return mSetupDiscriminator;
101 : }
102 :
103 : RendezvousParameters & SetSetupDiscriminator(SetupDiscriminator discriminator)
104 : {
105 : mSetupDiscriminator = discriminator;
106 : return *this;
107 : }
108 :
109 : RendezvousParameters & SetDiscriminator(uint16_t discriminator)
110 : {
111 : SetupDiscriminator tempDiscriminator;
112 : tempDiscriminator.SetLongValue(discriminator);
113 : mSetupDiscriminator = tempDiscriminator;
114 : return *this;
115 : }
116 :
117 : bool HasPASEVerifier() const { return mHasPASEVerifier; }
118 : const Crypto::Spake2pVerifier & GetPASEVerifier() const { return mPASEVerifier; }
119 : RendezvousParameters & SetPASEVerifier(Crypto::Spake2pVerifier & verifier)
120 : {
121 : memmove(&mPASEVerifier, &verifier, sizeof(verifier));
122 : mHasPASEVerifier = true;
123 : return *this;
124 : }
125 :
126 : #if CONFIG_NETWORK_LAYER_BLE
127 0 : bool HasBleLayer() const { return mBleLayer != nullptr; }
128 : Ble::BleLayer * GetBleLayer() const { return mBleLayer; }
129 : RendezvousParameters & SetBleLayer(Ble::BleLayer * value)
130 : {
131 : mBleLayer = value;
132 : return *this;
133 : }
134 :
135 0 : bool HasConnectionObject() const { return mConnectionObject != BLE_CONNECTION_UNINITIALIZED; }
136 0 : BLE_CONNECTION_OBJECT GetConnectionObject() const { return mConnectionObject; }
137 0 : RendezvousParameters & SetConnectionObject(BLE_CONNECTION_OBJECT connObj)
138 : {
139 0 : mConnectionObject = connObj;
140 0 : return *this;
141 : }
142 :
143 0 : bool HasDiscoveredObject() const { return mDiscoveredObject != BLE_CONNECTION_UNINITIALIZED; }
144 0 : BLE_CONNECTION_OBJECT GetDiscoveredObject() const { return mDiscoveredObject; }
145 0 : RendezvousParameters & SetDiscoveredObject(BLE_CONNECTION_OBJECT connObj)
146 : {
147 0 : mDiscoveredObject = connObj;
148 0 : return *this;
149 : }
150 : #else
151 : bool HasConnectionObject() const { return false; }
152 : bool HasDiscoveredObject() const { return false; }
153 : #endif // CONFIG_NETWORK_LAYER_BLE
154 :
155 : bool HasMRPConfig() const { return mMRPConfig.HasValue(); }
156 0 : ReliableMessageProtocolConfig GetMRPConfig() const { return mMRPConfig.ValueOr(GetDefaultMRPConfig()); }
157 0 : RendezvousParameters & SetIdleInterval(System::Clock::Milliseconds32 interval)
158 : {
159 0 : if (!mMRPConfig.HasValue())
160 : {
161 0 : mMRPConfig.Emplace(GetDefaultMRPConfig());
162 : }
163 0 : mMRPConfig.Value().mIdleRetransTimeout = interval;
164 0 : return *this;
165 : }
166 :
167 0 : RendezvousParameters & SetActiveInterval(System::Clock::Milliseconds32 interval)
168 : {
169 0 : if (!mMRPConfig.HasValue())
170 : {
171 0 : mMRPConfig.Emplace(GetDefaultMRPConfig());
172 : }
173 0 : mMRPConfig.Value().mActiveRetransTimeout = interval;
174 0 : return *this;
175 : }
176 :
177 : private:
178 : Transport::PeerAddress mPeerAddress; ///< the peer node address
179 : uint32_t mSetupPINCode = 0; ///< the target peripheral setup PIN Code
180 : std::optional<SetupDiscriminator> mSetupDiscriminator;
181 :
182 : Crypto::Spake2pVerifier mPASEVerifier;
183 : bool mHasPASEVerifier = false;
184 :
185 : Optional<ReliableMessageProtocolConfig> mMRPConfig;
186 :
187 : #if CONFIG_NETWORK_LAYER_BLE
188 : Ble::BleLayer * mBleLayer = nullptr;
189 : BLE_CONNECTION_OBJECT mConnectionObject = BLE_CONNECTION_UNINITIALIZED;
190 : BLE_CONNECTION_OBJECT mDiscoveredObject = BLE_CONNECTION_UNINITIALIZED;
191 : #endif // CONFIG_NETWORK_LAYER_BLE
192 : };
193 :
194 : } // namespace chip
|