Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2021-2022 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 : /**
19 : * @brief Defines processing state managed by Commissioner for an active User Directed Commissioning (UDC) client.
20 : */
21 :
22 : #pragma once
23 :
24 : #include <lib/dnssd/Resolver.h>
25 : #include <transport/raw/Base.h>
26 : #include <transport/raw/PeerAddress.h>
27 :
28 : namespace chip {
29 : namespace Protocols {
30 : namespace UserDirectedCommissioning {
31 :
32 : enum class UDCClientProcessingState : uint8_t
33 : {
34 : kNotInitialized,
35 : kDiscoveringNode,
36 : kNodeDiscoveryFailed,
37 : kPromptingUser,
38 : kUserDeclined,
39 : kObtainingOnboardingPayload,
40 : kWaitingForCommissionerPasscodeReady,
41 : kCommissioningNode,
42 : kCommissioningFailed,
43 : };
44 :
45 : using PeerAddress = ::chip::Transport::PeerAddress;
46 :
47 : enum class TargetAppCheckState : uint8_t
48 : {
49 : kNotInitialized,
50 : kAppNotFound,
51 : kAppFoundPasscodeReturned,
52 : kAppFoundNoPasscode,
53 : };
54 :
55 : /**
56 : * Represents information in the TargetAppList of the Identification Declaration message
57 : */
58 : struct TargetAppInfo
59 : {
60 : uint16_t vendorId = 0;
61 : uint16_t productId = 0;
62 : TargetAppCheckState checkState = TargetAppCheckState::kNotInitialized;
63 : uint32_t passcode = 0;
64 : bool foundApp = false;
65 : };
66 :
67 : /**
68 : * Defines the handling state of a UDC Client.
69 : *
70 : * Information contained within the state:
71 : * - PeerAddress represents how to talk to the UDC client
72 : * - PeerInstanceName is the DNS-SD instance name of the UDC client
73 : * - ExpirationTimeMs is a timestamp of when this object should expire.
74 : * - UDCClientProcessingState contains the current state of processing this UDC Client
75 : *
76 : */
77 : class UDCClientState
78 : {
79 : public:
80 : UDCClientState() : mPeerAddress(PeerAddress::Uninitialized()) {}
81 :
82 : UDCClientState(UDCClientState &&) = default;
83 4 : UDCClientState(const UDCClientState &) = default;
84 : UDCClientState & operator=(const UDCClientState &) = default;
85 : UDCClientState & operator=(UDCClientState &&) = default;
86 :
87 0 : const PeerAddress GetPeerAddress() const { return mPeerAddress; }
88 0 : void SetPeerAddress(const PeerAddress & address) { mPeerAddress = address; }
89 :
90 0 : const char * GetInstanceName() const { return mInstanceName; }
91 0 : void SetInstanceName(const char * instanceName) { Platform::CopyString(mInstanceName, instanceName); }
92 :
93 0 : const char * GetDeviceName() const { return mDeviceName; }
94 0 : void SetDeviceName(const char * deviceName) { Platform::CopyString(mDeviceName, deviceName); }
95 :
96 0 : uint16_t GetLongDiscriminator() const { return mLongDiscriminator; }
97 0 : void SetLongDiscriminator(uint16_t value) { mLongDiscriminator = value; }
98 :
99 0 : uint16_t GetVendorId() const { return mVendorId; }
100 0 : void SetVendorId(uint16_t value) { mVendorId = value; }
101 :
102 0 : uint16_t GetProductId() const { return mProductId; }
103 0 : void SetProductId(uint16_t value) { mProductId = value; }
104 :
105 : uint16_t GetCdPort() const { return mCdPort; }
106 0 : void SetCdPort(uint16_t port) { mCdPort = port; }
107 :
108 0 : const uint8_t * GetRotatingId() const { return mRotatingId; }
109 : size_t GetRotatingIdLength() const { return mRotatingIdLen; }
110 0 : void SetRotatingId(const uint8_t * rotatingId, size_t rotatingIdLen)
111 : {
112 0 : size_t maxSize = ArraySize(mRotatingId);
113 0 : mRotatingIdLen = (maxSize < rotatingIdLen) ? maxSize : rotatingIdLen;
114 0 : memcpy(mRotatingId, rotatingId, mRotatingIdLen);
115 0 : }
116 :
117 : const char * GetPairingInst() const { return mPairingInst; }
118 0 : void SetPairingInst(const char * pairingInst) { Platform::CopyString(mPairingInst, pairingInst); }
119 :
120 : uint16_t GetPairingHint() const { return mPairingHint; }
121 0 : void SetPairingHint(uint16_t pairingHint) { mPairingHint = pairingHint; }
122 :
123 : bool GetTargetAppInfo(uint8_t index, TargetAppInfo & info) const
124 : {
125 : if (index < mNumTargetAppInfos)
126 : {
127 : info.vendorId = mTargetAppInfos[index].vendorId;
128 : info.productId = mTargetAppInfos[index].productId;
129 : info.checkState = mTargetAppInfos[index].checkState;
130 : info.passcode = mTargetAppInfos[index].passcode;
131 : info.foundApp = mTargetAppInfos[index].foundApp;
132 : return true;
133 : }
134 : return false;
135 : }
136 : void SetTargetAppInfoState(uint8_t index, TargetAppCheckState checkState)
137 : {
138 : if (index < mNumTargetAppInfos)
139 : {
140 : mTargetAppInfos[index].checkState = checkState;
141 : }
142 : }
143 : uint8_t GetNumTargetAppInfos() const { return mNumTargetAppInfos; }
144 :
145 0 : bool AddTargetAppInfo(TargetAppInfo vid)
146 : {
147 0 : if (mNumTargetAppInfos >= kMaxTargetAppInfos)
148 : {
149 : // already at max
150 0 : return false;
151 : }
152 0 : mTargetAppInfos[mNumTargetAppInfos].vendorId = vid.vendorId;
153 0 : mTargetAppInfos[mNumTargetAppInfos].productId = vid.productId;
154 0 : mTargetAppInfos[mNumTargetAppInfos].checkState = TargetAppCheckState::kNotInitialized;
155 0 : mTargetAppInfos[mNumTargetAppInfos].passcode = 0;
156 0 : mTargetAppInfos[mNumTargetAppInfos].foundApp = false;
157 0 : mNumTargetAppInfos++;
158 0 : return true;
159 : }
160 :
161 0 : UDCClientProcessingState GetUDCClientProcessingState() const { return mUDCClientProcessingState; }
162 0 : void SetUDCClientProcessingState(UDCClientProcessingState state) { mUDCClientProcessingState = state; }
163 :
164 : System::Clock::Timestamp GetExpirationTime() const { return mExpirationTime; }
165 0 : void SetExpirationTime(System::Clock::Timestamp value) { mExpirationTime = value; }
166 :
167 0 : bool IsInitialized(System::Clock::Timestamp currentTime)
168 : {
169 : // if state is not the "not-initialized" and it has not expired
170 0 : return (mUDCClientProcessingState != UDCClientProcessingState::kNotInitialized && mExpirationTime > currentTime);
171 : }
172 :
173 0 : void SetNoPasscode(bool newValue) { mNoPasscode = newValue; };
174 : bool GetNoPasscode() const { return mNoPasscode; };
175 :
176 0 : void SetCdUponPasscodeDialog(bool newValue) { mCdUponPasscodeDialog = newValue; };
177 : bool GetCdUponPasscodeDialog() const { return mCdUponPasscodeDialog; };
178 :
179 0 : void SetCommissionerPasscode(bool newValue) { mCommissionerPasscode = newValue; };
180 : bool GetCommissionerPasscode() const { return mCommissionerPasscode; };
181 :
182 0 : void SetCommissionerPasscodeReady(bool newValue) { mCommissionerPasscodeReady = newValue; };
183 : bool GetCommissionerPasscodeReady() const { return mCommissionerPasscodeReady; };
184 :
185 0 : void SetCancelPasscode(bool newValue) { mCancelPasscode = newValue; };
186 : bool GetCancelPasscode() const { return mCancelPasscode; };
187 :
188 : void SetCachedCommissionerPasscode(uint32_t newValue) { mCachedCommissionerPasscode = newValue; };
189 : uint32_t GetCachedCommissionerPasscode() const { return mCachedCommissionerPasscode; };
190 :
191 : /**
192 : * Reset the connection state to a completely uninitialized status.
193 : */
194 0 : void Reset()
195 : {
196 0 : mPeerAddress = PeerAddress::Uninitialized();
197 0 : mLongDiscriminator = 0;
198 0 : mVendorId = 0;
199 0 : mProductId = 0;
200 0 : mRotatingIdLen = 0;
201 0 : mCdPort = 0;
202 0 : mDeviceName[0] = '\0';
203 0 : mPairingInst[0] = '\0';
204 0 : mPairingHint = 0;
205 0 : mNoPasscode = false;
206 0 : mCdUponPasscodeDialog = false;
207 0 : mCommissionerPasscode = false;
208 0 : mCommissionerPasscodeReady = false;
209 0 : mCancelPasscode = false;
210 0 : mExpirationTime = System::Clock::kZero;
211 0 : mUDCClientProcessingState = UDCClientProcessingState::kNotInitialized;
212 0 : mCachedCommissionerPasscode = 0;
213 0 : mNumTargetAppInfos = 0;
214 0 : }
215 :
216 : private:
217 : PeerAddress mPeerAddress;
218 : char mInstanceName[Dnssd::Commission::kInstanceNameMaxLength + 1];
219 : char mDeviceName[Dnssd::kMaxDeviceNameLen + 1];
220 : uint16_t mLongDiscriminator = 0;
221 : uint16_t mVendorId = 0;
222 : uint16_t mProductId = 0;
223 : uint16_t mCdPort = 0;
224 : uint8_t mRotatingId[chip::Dnssd::kMaxRotatingIdLen];
225 : size_t mRotatingIdLen = 0;
226 : char mPairingInst[chip::Dnssd::kMaxPairingInstructionLen + 1] = {};
227 : uint16_t mPairingHint = 0;
228 :
229 : constexpr static size_t kMaxTargetAppInfos = CHIP_DEVICE_CONFIG_UDC_MAX_TARGET_APPS;
230 : uint8_t mNumTargetAppInfos = 0; // number of vendor Ids
231 : TargetAppInfo mTargetAppInfos[kMaxTargetAppInfos];
232 :
233 : bool mNoPasscode = false;
234 : bool mCdUponPasscodeDialog = false;
235 : bool mCommissionerPasscode = false;
236 : bool mCommissionerPasscodeReady = false;
237 : bool mCancelPasscode = false;
238 :
239 : UDCClientProcessingState mUDCClientProcessingState;
240 : System::Clock::Timestamp mExpirationTime = System::Clock::kZero;
241 :
242 : uint32_t mCachedCommissionerPasscode = 0;
243 : };
244 :
245 : } // namespace UserDirectedCommissioning
246 : } // namespace Protocols
247 : } // namespace chip
|