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 : kCommissioningNode, 41 : kCommissioningFailed, 42 : }; 43 : 44 : using PeerAddress = ::chip::Transport::PeerAddress; 45 : 46 : /** 47 : * Represents information in the TargetAppList of the Identification Declaration message 48 : */ 49 : struct TargetAppInfo 50 : { 51 : uint16_t vendorId = 0; 52 : uint16_t productId = 0; 53 : }; 54 : 55 : /** 56 : * Defines the handling state of a UDC Client. 57 : * 58 : * Information contained within the state: 59 : * - PeerAddress represents how to talk to the UDC client 60 : * - PeerInstanceName is the DNS-SD instance name of the UDC client 61 : * - ExpirationTimeMs is a timestamp of when this object should expire. 62 : * - UDCClientProcessingState contains the current state of processing this UDC Client 63 : * 64 : */ 65 : class UDCClientState 66 : { 67 : public: 68 : UDCClientState() : mPeerAddress(PeerAddress::Uninitialized()) {} 69 : 70 : UDCClientState(UDCClientState &&) = default; 71 11 : UDCClientState(const UDCClientState &) = default; 72 : UDCClientState & operator=(const UDCClientState &) = default; 73 : UDCClientState & operator=(UDCClientState &&) = default; 74 : 75 0 : const PeerAddress GetPeerAddress() const { return mPeerAddress; } 76 0 : void SetPeerAddress(const PeerAddress & address) { mPeerAddress = address; } 77 : 78 0 : const char * GetInstanceName() const { return mInstanceName; } 79 0 : void SetInstanceName(const char * instanceName) { Platform::CopyString(mInstanceName, instanceName); } 80 : 81 0 : const char * GetDeviceName() const { return mDeviceName; } 82 0 : void SetDeviceName(const char * deviceName) { Platform::CopyString(mDeviceName, deviceName); } 83 : 84 0 : uint16_t GetLongDiscriminator() const { return mLongDiscriminator; } 85 0 : void SetLongDiscriminator(uint16_t value) { mLongDiscriminator = value; } 86 : 87 0 : uint16_t GetVendorId() const { return mVendorId; } 88 0 : void SetVendorId(uint16_t value) { mVendorId = value; } 89 : 90 0 : uint16_t GetProductId() const { return mProductId; } 91 0 : void SetProductId(uint16_t value) { mProductId = value; } 92 : 93 : uint16_t GetCdPort() const { return mCdPort; } 94 0 : void SetCdPort(uint16_t port) { mCdPort = port; } 95 : 96 0 : const uint8_t * GetRotatingId() const { return mRotatingId; } 97 : size_t GetRotatingIdLength() const { return mRotatingIdLen; } 98 0 : void SetRotatingId(const uint8_t * rotatingId, size_t rotatingIdLen) 99 : { 100 0 : size_t maxSize = ArraySize(mRotatingId); 101 0 : mRotatingIdLen = (maxSize < rotatingIdLen) ? maxSize : rotatingIdLen; 102 0 : memcpy(mRotatingId, rotatingId, mRotatingIdLen); 103 0 : } 104 : 105 : const char * GetPairingInst() const { return mPairingInst; } 106 0 : void SetPairingInst(const char * pairingInst) { Platform::CopyString(mPairingInst, pairingInst); } 107 : 108 : uint16_t GetPairingHint() const { return mPairingHint; } 109 0 : void SetPairingHint(uint16_t pairingHint) { mPairingHint = pairingHint; } 110 : 111 : bool GetTargetAppInfo(uint8_t index, TargetAppInfo & info) const 112 : { 113 : if (index < mNumTargetAppInfos) 114 : { 115 : info.vendorId = mTargetAppInfos[index].vendorId; 116 : info.productId = mTargetAppInfos[index].productId; 117 : return true; 118 : } 119 : return false; 120 : } 121 : uint8_t GetNumTargetAppInfos() const { return mNumTargetAppInfos; } 122 : 123 0 : bool AddTargetAppInfo(TargetAppInfo vid) 124 : { 125 0 : if (mNumTargetAppInfos >= sizeof(mTargetAppInfos)) 126 : { 127 : // already at max 128 0 : return false; 129 : } 130 0 : mTargetAppInfos[mNumTargetAppInfos].vendorId = vid.vendorId; 131 0 : mTargetAppInfos[mNumTargetAppInfos].productId = vid.productId; 132 0 : mNumTargetAppInfos++; 133 0 : return true; 134 : } 135 : 136 0 : UDCClientProcessingState GetUDCClientProcessingState() const { return mUDCClientProcessingState; } 137 0 : void SetUDCClientProcessingState(UDCClientProcessingState state) { mUDCClientProcessingState = state; } 138 : 139 : System::Clock::Timestamp GetExpirationTime() const { return mExpirationTime; } 140 0 : void SetExpirationTime(System::Clock::Timestamp value) { mExpirationTime = value; } 141 : 142 0 : bool IsInitialized(System::Clock::Timestamp currentTime) 143 : { 144 : // if state is not the "not-initialized" and it has not expired 145 0 : return (mUDCClientProcessingState != UDCClientProcessingState::kNotInitialized && mExpirationTime > currentTime); 146 : } 147 : 148 0 : void SetNoPasscode(bool newValue) { mNoPasscode = newValue; }; 149 : bool GetNoPasscode() const { return mNoPasscode; }; 150 : 151 0 : void SetCdUponPasscodeDialog(bool newValue) { mCdUponPasscodeDialog = newValue; }; 152 : bool GetCdUponPasscodeDialog() const { return mCdUponPasscodeDialog; }; 153 : 154 0 : void SetCommissionerPasscode(bool newValue) { mCommissionerPasscode = newValue; }; 155 : bool GetCommissionerPasscode() const { return mCommissionerPasscode; }; 156 : 157 0 : void SetCommissionerPasscodeReady(bool newValue) { mCommissionerPasscodeReady = newValue; }; 158 : bool GetCommissionerPasscodeReady() const { return mCommissionerPasscodeReady; }; 159 : 160 0 : void SetCancelPasscode(bool newValue) { mCancelPasscode = newValue; }; 161 : bool GetCancelPasscode() const { return mCancelPasscode; }; 162 : 163 : void SetCachedCommissionerPasscode(uint32_t newValue) { mCachedCommissionerPasscode = newValue; }; 164 : uint32_t GetCachedCommissionerPasscode() const { return mCachedCommissionerPasscode; }; 165 : 166 : /** 167 : * Reset the connection state to a completely uninitialized status. 168 : */ 169 : void Reset() 170 : { 171 : mPeerAddress = PeerAddress::Uninitialized(); 172 : mLongDiscriminator = 0; 173 : mVendorId = 0; 174 : mProductId = 0; 175 : mRotatingIdLen = 0; 176 : mCdPort = 0; 177 : mDeviceName[0] = '\0'; 178 : mPairingInst[0] = '\0'; 179 : mPairingHint = 0; 180 : mNoPasscode = false; 181 : mCdUponPasscodeDialog = false; 182 : mCommissionerPasscode = false; 183 : mCommissionerPasscodeReady = false; 184 : mCancelPasscode = false; 185 : mExpirationTime = System::Clock::kZero; 186 : mUDCClientProcessingState = UDCClientProcessingState::kNotInitialized; 187 : mCachedCommissionerPasscode = 0; 188 : } 189 : 190 : private: 191 : PeerAddress mPeerAddress; 192 : char mInstanceName[Dnssd::Commission::kInstanceNameMaxLength + 1]; 193 : char mDeviceName[Dnssd::kMaxDeviceNameLen + 1]; 194 : uint16_t mLongDiscriminator = 0; 195 : uint16_t mVendorId = 0; 196 : uint16_t mProductId = 0; 197 : uint16_t mCdPort = 0; 198 : uint8_t mRotatingId[chip::Dnssd::kMaxRotatingIdLen]; 199 : size_t mRotatingIdLen = 0; 200 : char mPairingInst[chip::Dnssd::kMaxPairingInstructionLen + 1] = {}; 201 : uint16_t mPairingHint = 0; 202 : 203 : constexpr static size_t kMaxTargetAppInfos = 10; 204 : uint8_t mNumTargetAppInfos = 0; // number of vendor Ids 205 : TargetAppInfo mTargetAppInfos[kMaxTargetAppInfos]; 206 : 207 : bool mNoPasscode = false; 208 : bool mCdUponPasscodeDialog = false; 209 : bool mCommissionerPasscode = false; 210 : bool mCommissionerPasscodeReady = false; 211 : bool mCancelPasscode = false; 212 : 213 : UDCClientProcessingState mUDCClientProcessingState; 214 : System::Clock::Timestamp mExpirationTime = System::Clock::kZero; 215 : 216 : uint32_t mCachedCommissionerPasscode = 0; 217 : }; 218 : 219 : } // namespace UserDirectedCommissioning 220 : } // namespace Protocols 221 : } // namespace chip