Matter SDK Coverage Report
Current view: top level - protocols/secure_channel - CASEServer.h (source / functions) Coverage Total Hit
Test: SHA:e98a48c2e59f85a25417956e1d105721433aa5d1 Lines: 91.7 % 12 11
Test Date: 2026-01-09 16:53:50 Functions: 71.4 % 7 5

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2021 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 <credentials/CertificateValidityPolicy.h>
      21              : #include <credentials/GroupDataProvider.h>
      22              : #include <messaging/ExchangeDelegate.h>
      23              : #include <messaging/ExchangeMgr.h>
      24              : #include <protocols/secure_channel/CASESession.h>
      25              : #include <system/SystemClock.h>
      26              : 
      27              : namespace chip {
      28              : 
      29              : class CASEServer : public SessionEstablishmentDelegate,
      30              :                    public Messaging::UnsolicitedMessageHandler,
      31              :                    public Messaging::ExchangeDelegate
      32              : {
      33              : public:
      34           17 :     CASEServer() {}
      35           17 :     ~CASEServer() override { Shutdown(); }
      36              : 
      37              :     /*
      38              :      * This method will shutdown this object, releasing the strong reference to the pinned SecureSession object.
      39              :      * It will also unregister the unsolicited handler and clear out the session object (which will release the weak
      40              :      * reference through the underlying SessionHolder).
      41              :      *
      42              :      */
      43           25 :     void Shutdown()
      44              :     {
      45           25 :         if (mExchangeManager != nullptr)
      46              :         {
      47            7 :             TEMPORARY_RETURN_IGNORED mExchangeManager->UnregisterUnsolicitedMessageHandlerForType(
      48              :                 Protocols::SecureChannel::MsgType::CASE_Sigma1);
      49            7 :             mExchangeManager = nullptr;
      50              :         }
      51              : 
      52           25 :         GetSession().Clear();
      53           25 :         mPinnedSecureSession.ClearValue();
      54           25 :     }
      55              : 
      56              :     CHIP_ERROR ListenForSessionEstablishment(Messaging::ExchangeManager * exchangeManager, SessionManager * sessionManager,
      57              :                                              FabricTable * fabrics, SessionResumptionStorage * sessionResumptionStorage,
      58              :                                              Credentials::CertificateValidityPolicy * policy,
      59              :                                              Credentials::GroupDataProvider * responderGroupDataProvider);
      60              : 
      61              :     //////////// SessionEstablishmentDelegate Implementation ///////////////
      62              :     void OnSessionEstablishmentError(CHIP_ERROR error) override;
      63              :     void OnSessionEstablished(const SessionHandle & session) override;
      64              : 
      65              :     //// UnsolicitedMessageHandler Implementation ////
      66              :     CHIP_ERROR OnUnsolicitedMessageReceived(const PayloadHeader & payloadHeader, ExchangeDelegate *& newDelegate) override;
      67              : 
      68              :     //// ExchangeDelegate Implementation ////
      69              :     CHIP_ERROR OnMessageReceived(Messaging::ExchangeContext * ec, const PayloadHeader & payloadHeader,
      70              :                                  System::PacketBufferHandle && payload) override;
      71            0 :     void OnResponseTimeout(Messaging::ExchangeContext * ec) override {}
      72            8 :     Messaging::ExchangeMessageDispatch & GetMessageDispatch() override { return GetSession().GetMessageDispatch(); }
      73              : 
      74          107 :     CASESession & GetSession() { return mPairingSession; }
      75              : 
      76              : private:
      77              :     Messaging::ExchangeManager * mExchangeManager                       = nullptr;
      78              :     SessionResumptionStorage * mSessionResumptionStorage                = nullptr;
      79              :     Credentials::CertificateValidityPolicy * mCertificateValidityPolicy = nullptr;
      80              : 
      81              :     //
      82              :     // When we're in the process of establishing a session, this is used
      83              :     // to maintain an additional, strong reference to the underlying SecureSession.
      84              :     // This is because the existing reference in PairingSession is a weak one
      85              :     // (i.e a SessionHolder) and can lose its reference if the session is evicted
      86              :     // for any reason.
      87              :     //
      88              :     // This initially points to a session that is not yet active. Upon activation, it
      89              :     // transfers ownership of the session to the SecureSessionManager and this reference
      90              :     // is released before simultaneously acquiring ownership of a new SecureSession.
      91              :     //
      92              :     Optional<SessionHandle> mPinnedSecureSession;
      93              : 
      94              :     CASESession mPairingSession;
      95              :     SessionManager * mSessionManager = nullptr;
      96              : 
      97              :     FabricTable * mFabrics                              = nullptr;
      98              :     Credentials::GroupDataProvider * mGroupDataProvider = nullptr;
      99              : 
     100              :     CHIP_ERROR InitCASEHandshake(Messaging::ExchangeContext * ec);
     101              : 
     102              :     /*
     103              :      * This will clean up any state from a previous session establishment
     104              :      * attempt (if any) and setup the machinery to listen for and handle
     105              :      * any session handshakes there-after.
     106              :      *
     107              :      * If a session had previously been established successfully, previouslyEstablishedPeer
     108              :      * should be set to the scoped node-id of the peer associated with that session.
     109              :      *
     110              :      */
     111              :     void PrepareForSessionEstablishment(const ScopedNodeId & previouslyEstablishedPeer = ScopedNodeId());
     112              : 
     113              :     // If we are in the middle of handshake and receive a Sigma1 then respond with Busy status code.
     114              :     // @param[in] ec              Exchange Context
     115              :     // @param[in] minimumWaitTime Minimum wait time reported to client before it can attempt to resend sigma1
     116              :     //
     117              :     // @return CHIP_NO_ERROR on success, error code otherwise
     118              :     CHIP_ERROR SendBusyStatusReport(Messaging::ExchangeContext * ec, System::Clock::Milliseconds16 minimumWaitTime);
     119              : };
     120              : 
     121              : } // namespace chip
        

Generated by: LCOV version 2.0-1