Matter SDK Coverage Report
Current view: top level - app/server - CommissioningWindowManager.h (source / functions) Coverage Total Hit
Test: SHA:b879ecb8e99e175eea0a293a888bda853da2b19c Lines: 90.9 % 11 10
Test Date: 2025-01-17 19:00:11 Functions: 100.0 % 5 5

            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              : #pragma once
      19              : 
      20              : #include <app/data-model/Nullable.h>
      21              : #include <app/server/AppDelegate.h>
      22              : #include <app/server/CommissioningModeProvider.h>
      23              : #include <crypto/CHIPCryptoPAL.h>
      24              : #include <lib/core/CHIPVendorIdentifiers.hpp>
      25              : #include <lib/core/ClusterEnums.h>
      26              : #include <lib/core/DataModelTypes.h>
      27              : #include <lib/dnssd/Advertiser.h>
      28              : #include <messaging/ExchangeDelegate.h>
      29              : #include <platform/CHIPDeviceConfig.h>
      30              : #include <protocols/secure_channel/RendezvousParameters.h>
      31              : #include <system/SystemClock.h>
      32              : 
      33              : namespace chip {
      34              : 
      35              : enum class CommissioningWindowAdvertisement
      36              : {
      37              :     kAllSupported,
      38              :     kDnssdOnly,
      39              : };
      40              : 
      41              : class Server;
      42              : 
      43              : class CommissioningWindowManager : public Messaging::UnsolicitedMessageHandler,
      44              :                                    public SessionEstablishmentDelegate,
      45              :                                    public app::CommissioningModeProvider,
      46              :                                    public SessionDelegate
      47              : {
      48              : public:
      49            1 :     CommissioningWindowManager() : mPASESession(*this) {}
      50              : 
      51            1 :     CHIP_ERROR Init(Server * server)
      52              :     {
      53            1 :         if (server == nullptr)
      54              :         {
      55            0 :             return CHIP_ERROR_INVALID_ARGUMENT;
      56              :         }
      57            1 :         mServer = server;
      58            1 :         return CHIP_NO_ERROR;
      59              :     }
      60              : 
      61              :     System::Clock::Seconds32 MaxCommissioningTimeout() const;
      62              : 
      63            6 :     System::Clock::Seconds32 MinCommissioningTimeout() const
      64              :     {
      65              :         // Specification section 5.4.2.3. Announcement Duration says 3 minutes.
      66            6 :         return mMinCommissioningTimeoutOverride.ValueOr(System::Clock::Seconds32(3 * 60));
      67              :     }
      68              : 
      69            1 :     void SetAppDelegate(AppDelegate * delegate) { mAppDelegate = delegate; }
      70              : 
      71              :     /**
      72              :      * Open the pairing window using default configured parameters.
      73              :      */
      74              :     CHIP_ERROR
      75              :     OpenBasicCommissioningWindow(
      76            1 :         System::Clock::Seconds32 commissioningTimeout      = System::Clock::Seconds32(CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS),
      77              :         CommissioningWindowAdvertisement advertisementMode = chip::CommissioningWindowAdvertisement::kAllSupported);
      78              : 
      79              :     /**
      80              :      * Open the pairing window using default configured parameters, triggered by
      81              :      * the Administrator Commmissioning cluster implementation.
      82              :      */
      83              :     CHIP_ERROR
      84              :     OpenBasicCommissioningWindowForAdministratorCommissioningCluster(System::Clock::Seconds32 commissioningTimeout,
      85              :                                                                      FabricIndex fabricIndex, VendorId vendorId);
      86              : 
      87              :     CHIP_ERROR OpenEnhancedCommissioningWindow(System::Clock::Seconds32 commissioningTimeout, uint16_t discriminator,
      88              :                                                Crypto::Spake2pVerifier & verifier, uint32_t iterations, chip::ByteSpan salt,
      89              :                                                FabricIndex fabricIndex, VendorId vendorId);
      90              : 
      91              :     void CloseCommissioningWindow();
      92              : 
      93              :     app::Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum CommissioningWindowStatusForCluster() const;
      94              : 
      95              :     bool IsCommissioningWindowOpen() const;
      96              : 
      97              :     const app::DataModel::Nullable<VendorId> & GetOpenerVendorId() const { return mOpenerVendorId; }
      98              : 
      99              :     const app::DataModel::Nullable<FabricIndex> & GetOpenerFabricIndex() const { return mOpenerFabricIndex; }
     100              : 
     101              :     void OnFabricRemoved(FabricIndex removedIndex);
     102              : 
     103              :     // CommissioningModeProvider implementation.
     104              :     Dnssd::CommissioningMode GetCommissioningMode() const override;
     105              : 
     106              :     //// UnsolicitedMessageHandler Implementation ////
     107              :     CHIP_ERROR OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader,
     108              :                                             Messaging::ExchangeDelegate *& newDelegate) override;
     109              :     void OnExchangeCreationFailed(Messaging::ExchangeDelegate * delegate) override;
     110              : 
     111              :     //////////// SessionEstablishmentDelegate Implementation ///////////////
     112              :     void OnSessionEstablishmentError(CHIP_ERROR error) override;
     113              :     void OnSessionEstablishmentStarted() override;
     114              :     void OnSessionEstablished(const SessionHandle & session) override;
     115              : 
     116              :     void Shutdown();
     117              : 
     118              :     void OnPlatformEvent(const DeviceLayer::ChipDeviceEvent * event);
     119              : 
     120              :     // For tests only, allow overriding the spec-defined minimum value of the
     121              :     // commissioning window timeout.
     122              :     void OverrideMinCommissioningTimeout(System::Clock::Seconds32 timeout) { mMinCommissioningTimeoutOverride.SetValue(timeout); }
     123              : 
     124              : private:
     125              :     //////////// SessionDelegate Implementation ///////////////
     126              :     void OnSessionReleased() override;
     127              : 
     128            6 :     void SetBLE(bool ble) { mIsBLE = ble; }
     129              : 
     130              :     CHIP_ERROR SetTemporaryDiscriminator(uint16_t discriminator);
     131              : 
     132              :     CHIP_ERROR RestoreDiscriminator();
     133              : 
     134              :     CHIP_ERROR StartAdvertisement();
     135              : 
     136              :     CHIP_ERROR StopAdvertisement(bool aShuttingDown);
     137              : 
     138              :     // Start a timer that will call HandleCommissioningWindowTimeout, and then
     139              :     // start advertising and listen for PASE.
     140              :     CHIP_ERROR OpenCommissioningWindow(System::Clock::Seconds32 commissioningTimeout);
     141              : 
     142              :     // Start advertising and listening for PASE connections.  Should only be
     143              :     // called when a commissioning window timeout timer is running.
     144              :     CHIP_ERROR AdvertiseAndListenForPASE();
     145              : 
     146              :     // Call AdvertiseAndListenForPASE, only if max attempts have not been reached.
     147              :     // Cleans up and calls app server delegate on failure.
     148              :     // err gives the current error we're attemping to recover from
     149              :     void HandleFailedAttempt(CHIP_ERROR err);
     150              : 
     151              :     // Helper for Shutdown and Cleanup.  Does not do anything with
     152              :     // advertisements, because Shutdown and Cleanup want to handle those
     153              :     // differently.
     154              :     void ResetState();
     155              : 
     156              :     void Cleanup();
     157              : 
     158              :     /**
     159              :      * Function that gets called when our commissioning window timeout timer
     160              :      * fires.
     161              :      *
     162              :      * This timer is started when a commissioning window is initially opened via
     163              :      * OpenEnhancedCommissioningWindow or OpenBasicCommissioningWindow.
     164              :      *
     165              :      * The timer is canceled when a PASE connection is established, because it
     166              :      * should not affect the actual commissioning process, and after a PASE
     167              :      * connection is established we will not re-enter commissioning mode without
     168              :      * a new call to OpenEnhancedCommissioningWindow or
     169              :      * OpenBasicCommissioningWindow.
     170              :      */
     171              :     static void HandleCommissioningWindowTimeout(chip::System::Layer * aSystemLayer, void * aAppState);
     172              : 
     173              :     /**
     174              :      * Helper to immediately expire the fail-safe if it's currently armed.
     175              :      */
     176              :     void ExpireFailSafeIfArmed();
     177              : 
     178              :     /**
     179              :      * Helpers to ensure the right attribute reporting happens when our state is
     180              :      * updated.
     181              :      */
     182              :     void UpdateWindowStatus(app::Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum aNewStatus);
     183              :     void UpdateOpenerVendorId(app::DataModel::Nullable<VendorId> aNewOpenerVendorId);
     184              :     void UpdateOpenerFabricIndex(app::DataModel::Nullable<FabricIndex> aNewOpenerFabricIndex);
     185              : 
     186              :     AppDelegate * mAppDelegate = nullptr;
     187              :     Server * mServer           = nullptr;
     188              : 
     189              :     app::Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum mWindowStatus =
     190              :         app::Clusters::AdministratorCommissioning::CommissioningWindowStatusEnum::kWindowNotOpen;
     191              : 
     192              :     bool mIsBLE = true;
     193              : 
     194              :     PASESession mPairingSession;
     195              : 
     196              :     uint8_t mFailedCommissioningAttempts = 0;
     197              : 
     198              :     bool mUseECM = false;
     199              :     Crypto::Spake2pVerifier mECMPASEVerifier;
     200              :     uint16_t mECMDiscriminator = 0;
     201              :     // mListeningForPASE is true only when we are listening for
     202              :     // PBKDFParamRequest messages or when we're in the middle of a PASE
     203              :     // handshake.
     204              :     bool mListeningForPASE = false;
     205              :     // Boolean that tracks whether we have a live commissioning timeout timer.
     206              :     bool mCommissioningTimeoutTimerArmed = false;
     207              :     uint32_t mECMIterations              = 0;
     208              :     uint32_t mECMSaltLength              = 0;
     209              :     uint8_t mECMSalt[Crypto::kSpake2p_Max_PBKDF_Salt_Length];
     210              : 
     211              :     // For tests only, so that we can test the commissioning window timeout
     212              :     // without having to wait 3 minutes.
     213              :     Optional<System::Clock::Seconds32> mMinCommissioningTimeoutOverride;
     214              : 
     215              :     // The PASE session we are using, so we can handle CloseSession properly.
     216              :     SessionHolderWithDelegate mPASESession;
     217              : 
     218              :     // Information about who opened the commissioning window.  These will only
     219              :     // be non-null if the window was opened via the operational credentials
     220              :     // cluster and the fabric index may be null even then if the fabric has been
     221              :     // removed.
     222              :     app::DataModel::Nullable<VendorId> mOpenerVendorId;
     223              :     app::DataModel::Nullable<FabricIndex> mOpenerFabricIndex;
     224              : };
     225              : 
     226              : } // namespace chip
        

Generated by: LCOV version 2.0-1