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 <lib/core/Optional.h>
29 : #include <lib/support/SetupDiscriminator.h>
30 : #include <lib/support/logging/CHIPLogging.h>
31 : #include <messaging/ReliableMessageProtocolConfig.h>
32 :
33 : namespace chip {
34 :
35 : // The largest supported value for Rendezvous discriminators
36 : const uint16_t kMaxRendezvousDiscriminatorValue = 0xFFF;
37 :
38 : // The largest supported value for session idle interval and session active interval
39 : inline constexpr uint32_t kMaxSessionIdleInterval = 3600000;
40 :
41 : class RendezvousParameters
42 : {
43 : public:
44 0 : RendezvousParameters() = default;
45 :
46 : bool HasSetupPINCode() const { return mSetupPINCode != 0; }
47 0 : uint32_t GetSetupPINCode() const { return mSetupPINCode; }
48 0 : RendezvousParameters & SetSetupPINCode(uint32_t setupPINCode)
49 : {
50 0 : mSetupPINCode = setupPINCode;
51 0 : return *this;
52 : }
53 :
54 : bool HasPeerAddress() const { return mPeerAddress.IsInitialized(); }
55 0 : Transport::PeerAddress GetPeerAddress() const { return mPeerAddress; }
56 0 : RendezvousParameters & SetPeerAddress(const Transport::PeerAddress & peerAddress)
57 : {
58 0 : mPeerAddress = peerAddress;
59 0 : return *this;
60 : }
61 :
62 : // Discriminators in RendezvousParameters are always long (12-bit)
63 : // discriminators.
64 0 : bool HasDiscriminator() const { return mSetupDiscriminator.has_value(); }
65 :
66 : // Obtains the long version of the discriminator, or 0 if short.
67 : // WARNING: This is lossy and a bad idea to use. The correct method to use
68 : // is GetSetupDiscriminator(). This method exists for public
69 : // API backwards compatibility.
70 : uint16_t GetDiscriminator() const
71 : {
72 : if (!mSetupDiscriminator.has_value())
73 : {
74 : ChipLogError(Discovery,
75 : "Get RendezvousParameters::GetDiscriminator() called without discriminator in params (inconsistent). "
76 : "Using value 0 to avoid crash! Ensure discriminator is set!");
77 : return 0;
78 : }
79 :
80 : if (mSetupDiscriminator.value().IsShortDiscriminator())
81 : {
82 : ChipLogError(Discovery,
83 : "Get RendezvousParameters::GetDiscriminator() called with SHORT discriminator (inconsistent). Using value "
84 : "0 to avoid crash! Call GetSetupDiscriminator() to avoid loss.");
85 : return 0;
86 : }
87 :
88 : return mSetupDiscriminator.value().GetLongValue();
89 : }
90 :
91 0 : std::optional<SetupDiscriminator> GetSetupDiscriminator() const
92 : {
93 0 : if (!mSetupDiscriminator.has_value())
94 : {
95 0 : ChipLogError(
96 : Discovery,
97 : "Get RendezvousParameters::GetSetupDiscriminator() called without discriminator in params (inconsistent).");
98 : }
99 0 : return mSetupDiscriminator;
100 : }
101 :
102 0 : RendezvousParameters & SetSetupDiscriminator(SetupDiscriminator discriminator)
103 : {
104 0 : mSetupDiscriminator = discriminator;
105 0 : return *this;
106 : }
107 :
108 : RendezvousParameters & SetDiscriminator(uint16_t discriminator)
109 : {
110 : SetupDiscriminator tempDiscriminator;
111 : tempDiscriminator.SetLongValue(discriminator);
112 : mSetupDiscriminator = tempDiscriminator;
113 : return *this;
114 : }
115 :
116 : #if CONFIG_NETWORK_LAYER_BLE
117 0 : bool HasBleLayer() const { return mBleLayer != nullptr; }
118 : Ble::BleLayer * GetBleLayer() const { return mBleLayer; }
119 : RendezvousParameters & SetBleLayer(Ble::BleLayer * value)
120 : {
121 : mBleLayer = value;
122 : return *this;
123 : }
124 :
125 0 : bool HasConnectionObject() const { return mConnectionObject != BLE_CONNECTION_UNINITIALIZED; }
126 0 : BLE_CONNECTION_OBJECT GetConnectionObject() const { return mConnectionObject; }
127 0 : RendezvousParameters & SetConnectionObject(BLE_CONNECTION_OBJECT connObj)
128 : {
129 0 : mConnectionObject = connObj;
130 0 : return *this;
131 : }
132 :
133 0 : bool HasDiscoveredObject() const { return mDiscoveredObject != BLE_CONNECTION_UNINITIALIZED; }
134 0 : BLE_CONNECTION_OBJECT GetDiscoveredObject() const { return mDiscoveredObject; }
135 0 : RendezvousParameters & SetDiscoveredObject(BLE_CONNECTION_OBJECT connObj)
136 : {
137 0 : mDiscoveredObject = connObj;
138 0 : return *this;
139 : }
140 : #else
141 : bool HasConnectionObject() const { return false; }
142 : bool HasDiscoveredObject() const { return false; }
143 : #endif // CONFIG_NETWORK_LAYER_BLE
144 :
145 : bool HasMRPConfig() const { return mMRPConfig.HasValue(); }
146 0 : ReliableMessageProtocolConfig GetMRPConfig() const { return mMRPConfig.ValueOr(GetDefaultMRPConfig()); }
147 0 : RendezvousParameters & SetIdleInterval(System::Clock::Milliseconds32 interval)
148 : {
149 0 : if (!mMRPConfig.HasValue())
150 : {
151 0 : mMRPConfig.Emplace(GetDefaultMRPConfig());
152 : }
153 0 : mMRPConfig.Value().mIdleRetransTimeout = interval;
154 0 : return *this;
155 : }
156 :
157 0 : RendezvousParameters & SetActiveInterval(System::Clock::Milliseconds32 interval)
158 : {
159 0 : if (!mMRPConfig.HasValue())
160 : {
161 0 : mMRPConfig.Emplace(GetDefaultMRPConfig());
162 : }
163 0 : mMRPConfig.Value().mActiveRetransTimeout = interval;
164 0 : return *this;
165 : }
166 :
167 : private:
168 : Transport::PeerAddress mPeerAddress; ///< the peer node address
169 : uint32_t mSetupPINCode = 0; ///< the target peripheral setup PIN Code
170 : std::optional<SetupDiscriminator> mSetupDiscriminator;
171 :
172 : Optional<ReliableMessageProtocolConfig> mMRPConfig;
173 :
174 : #if CONFIG_NETWORK_LAYER_BLE
175 : Ble::BleLayer * mBleLayer = nullptr;
176 : BLE_CONNECTION_OBJECT mConnectionObject = BLE_CONNECTION_UNINITIALIZED;
177 : BLE_CONNECTION_OBJECT mDiscoveredObject = BLE_CONNECTION_UNINITIALIZED;
178 : #endif // CONFIG_NETWORK_LAYER_BLE
179 : };
180 :
181 : } // namespace chip
|