Matter SDK Coverage Report
Current view: top level - app - CommandHandler.h (source / functions) Coverage Total Hit
Test: SHA:0a260ffe149e0486eee9e451428453f0b12a3d3c Lines: 31.2 % 16 5
Test Date: 2025-06-06 07:10:05 Functions: 40.0 % 10 4

            Line data    Source code
       1              : /*
       2              :  *
       3              :  *    Copyright (c) 2020 Project CHIP Authors
       4              :  *    All rights reserved.
       5              :  *
       6              :  *    Licensed under the Apache License, Version 2.0 (the "License");
       7              :  *    you may not use this file except in compliance with the License.
       8              :  *    You may obtain a copy of the License at
       9              :  *
      10              :  *        http://www.apache.org/licenses/LICENSE-2.0
      11              :  *
      12              :  *    Unless required by applicable law or agreed to in writing, software
      13              :  *    distributed under the License is distributed on an "AS IS" BASIS,
      14              :  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      15              :  *    See the License for the specific language governing permissions and
      16              :  *    limitations under the License.
      17              :  */
      18              : #pragma once
      19              : 
      20              : #include <access/SubjectDescriptor.h>
      21              : #include <app/ConcreteCommandPath.h>
      22              : #include <app/data-model/EncodableToTLV.h>
      23              : #include <app/data-model/Encode.h>
      24              : #include <app/data-model/FabricScoped.h>
      25              : #include <lib/core/CHIPCore.h>
      26              : #include <lib/support/CodeUtils.h>
      27              : #include <lib/support/IntrusiveList.h>
      28              : #include <lib/support/logging/CHIPLogging.h>
      29              : #include <messaging/ExchangeContext.h>
      30              : #include <protocols/interaction_model/StatusCode.h>
      31              : 
      32              : namespace chip {
      33              : namespace app {
      34              : 
      35              : /**
      36              :  *  A handler for incoming Invoke interactions.  This handles incoming Invoke
      37              :  *  Request messages and generates Invoke Response messages.
      38              :  *
      39              :  *  Allows adding responses (status, or server to client command) to be sent in
      40              :  *  the Invoke Response message: see the various "Add*" methods.
      41              :  *
      42              :  *  Allows adding the responses asynchronously when using `CommandHandler::Handle`
      43              :  *  (see documentation for `CommandHandler::Handle` for details)
      44              :  *
      45              :  *  Upgrading notes: this class has moved to an interface from a previous more complex
      46              :  *  implementation. If upgrading code between versions, please see docs/upgrading.md
      47              :  */
      48              : class CommandHandler
      49              : {
      50              : public:
      51           57 :     virtual ~CommandHandler() = default;
      52              : 
      53              :     /**
      54              :      * Class that allows asynchronous command processing before sending a
      55              :      * response.  When such processing is desired:
      56              :      *
      57              :      * 1) Create a Handle initialized with the CommandHandler that delivered the
      58              :      *    incoming command.
      59              :      * 2) Ensure the Handle, or some Handle it's moved into via the move
      60              :      *    constructor or move assignment operator, remains alive during the
      61              :      *    course of the asynchronous processing.
      62              :      * 3) Ensure that the ConcreteCommandPath involved will be known when
      63              :      *    sending the response.
      64              :      * 4) When ready to send the response:
      65              :      *    * Ensure that no other Matter tasks are running in parallel (e.g. by
      66              :      *      running on the Matter event loop or holding the Matter stack lock).
      67              :      *    * Call Get() to get the CommandHandler.
      68              :      *    * Check that Get() did not return null.
      69              :      *    * Add the response to the CommandHandler via one of the Add* methods.
      70              :      *    * Let the Handle get destroyed, or manually call Handle::Release() if
      71              :      *      destruction of the Handle is not desirable for some reason.
      72              :      *
      73              :      * The Invoke Response will not be sent until all outstanding Handles have
      74              :      * been destroyed or have had Release called.
      75              :      */
      76              :     class Handle : public IntrusiveListNodeBase<>
      77              :     {
      78              :     public:
      79              :         Handle() {}
      80              :         Handle(const Handle & handle) = delete;
      81              :         Handle(Handle && handle)
      82              :         {
      83              :             Init(handle.mpHandler);
      84              :             handle.Release();
      85              :         }
      86              :         Handle(decltype(nullptr)) {}
      87              :         Handle(CommandHandler * handler);
      88           36 :         ~Handle() { Release(); }
      89              : 
      90              :         Handle & operator=(Handle && handle)
      91              :         {
      92              :             Release();
      93              :             Init(handle.mpHandler);
      94              : 
      95              :             handle.Release();
      96              :             return *this;
      97              :         }
      98              : 
      99              :         Handle & operator=(decltype(nullptr))
     100              :         {
     101              :             Release();
     102              :             return *this;
     103              :         }
     104              : 
     105              :         /**
     106              :          * Get the CommandHandler object it holds. Get() may return a nullptr if the CommandHandler object it holds is no longer
     107              :          * valid.
     108              :          */
     109              :         CommandHandler * Get();
     110              : 
     111              :         void Release();
     112              : 
     113           87 :         void Invalidate() { mpHandler = nullptr; }
     114              : 
     115              :     private:
     116              :         void Init(CommandHandler * handler);
     117              : 
     118              :         CommandHandler * mpHandler = nullptr;
     119              :     };
     120              : 
     121              :     /**
     122              :      * Adds the given command status and returns any failures in adding statuses (e.g. out
     123              :      * of buffer space) to the caller. `context` is an optional (if not nullptr)
     124              :      * debug string to include in logging.
     125              :      */
     126              :     virtual CHIP_ERROR FallibleAddStatus(const ConcreteCommandPath & aRequestCommandPath,
     127              :                                          const Protocols::InteractionModel::ClusterStatusCode & aStatus,
     128              :                                          const char * context = nullptr) = 0;
     129           14 :     CHIP_ERROR FallibleAddStatus(const ConcreteCommandPath & aRequestCommandPath, const Protocols::InteractionModel::Status aStatus,
     130              :                                  const char * context = nullptr)
     131              :     {
     132           14 :         return FallibleAddStatus(aRequestCommandPath, Protocols::InteractionModel::ClusterStatusCode{ aStatus }, context);
     133              :     }
     134              : 
     135              :     /**
     136              :      * Adds an IM global or Cluster status when the caller is unable to handle any failures. Logging is performed
     137              :      * and failure to register the status is checked with VerifyOrDie. `context` is an optional (if not nullptr)
     138              :      * debug string to include in logging.
     139              :      */
     140              :     virtual void AddStatus(const ConcreteCommandPath & aRequestCommandPath,
     141              :                            const Protocols::InteractionModel::ClusterStatusCode & aStatus, const char * context = nullptr) = 0;
     142            0 :     void AddStatus(const ConcreteCommandPath & aRequestCommandPath, const Protocols::InteractionModel::Status aStatus,
     143              :                    const char * context = nullptr)
     144              :     {
     145            0 :         AddStatus(aRequestCommandPath, Protocols::InteractionModel::ClusterStatusCode{ aStatus }, context);
     146            0 :     }
     147              : 
     148              :     /**
     149              :      * Sets the response to indicate Success with a cluster-specific status code `aClusterStatus` included.
     150              :      *
     151              :      * NOTE: For regular success, what you want is AddStatus/FailibleAddStatus(aRequestCommandPath,
     152              :      * InteractionModel::Status::Success).
     153              :      */
     154            0 :     virtual CHIP_ERROR AddClusterSpecificSuccess(const ConcreteCommandPath & aRequestCommandPath, ClusterStatus aClusterStatus)
     155              :     {
     156            0 :         return FallibleAddStatus(aRequestCommandPath,
     157            0 :                                  Protocols::InteractionModel::ClusterStatusCode::ClusterSpecificSuccess(aClusterStatus));
     158              :     }
     159              : 
     160              :     /**
     161              :      * Sets the response to indicate Failure with a cluster-specific status code `aClusterStatus` included.
     162              :      */
     163            0 :     virtual CHIP_ERROR AddClusterSpecificFailure(const ConcreteCommandPath & aRequestCommandPath, ClusterStatus aClusterStatus)
     164              :     {
     165            0 :         return FallibleAddStatus(aRequestCommandPath,
     166            0 :                                  Protocols::InteractionModel::ClusterStatusCode::ClusterSpecificFailure(aClusterStatus));
     167              :     }
     168              : 
     169              :     /**
     170              :      * GetAccessingFabricIndex() may only be called during synchronous command
     171              :      * processing.  Anything that runs async (while holding a
     172              :      * CommandHandler::Handle or equivalent) must not call this method, because
     173              :      * it will not work right if the session we're using was evicted.
     174              :      */
     175              :     virtual FabricIndex GetAccessingFabricIndex() const = 0;
     176              : 
     177              :     /**
     178              :      * API for adding a data response.  The `aEncodable` is generally expected to encode
     179              :      * a ClusterName::Commands::CommandName::Type struct, however any object should work.
     180              :      *
     181              :      * @param [in] aRequestCommandPath the concrete path of the command we are
     182              :      *             responding to.
     183              :      * @param [in] aResponseCommandId the command whose content is being encoded.
     184              :      * @param [in] aEncodable - an encodable that places the command data structure
     185              :      *             for `aResponseCommandId` into a TLV Writer.
     186              :      *
     187              :      * If you have no great way of handling the returned CHIP_ERROR, consider
     188              :      * using `AddResponse` which will automatically reply with `Failure` in
     189              :      * case AddResponseData fails.
     190              :      */
     191              :     virtual CHIP_ERROR AddResponseData(const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
     192              :                                        const DataModel::EncodableToTLV & aEncodable) = 0;
     193              : 
     194              :     /**
     195              :      * Attempts to encode a response to a command.
     196              :      *
     197              :      * `aRequestCommandPath` represents the request path (endpoint/cluster/commandid) and the reply
     198              :      * will preserve the same path and switch the command id to aResponseCommandId.
     199              :      *
     200              :      * As this command does not return any error codes, it must try its best to encode the reply
     201              :      * and if it fails, it MUST encode a `Protocols::InteractionModel::Status::Failure` as a
     202              :      * reply (i.e. a reply is guaranteed to be sent).
     203              :      *
     204              :      * Above is the main difference from AddResponseData: AddResponse will auto-reply with failure while
     205              :      * AddResponseData allows the caller to try to deal with any CHIP_ERRORs.
     206              :      */
     207              :     virtual void AddResponse(const ConcreteCommandPath & aRequestCommandPath, CommandId aResponseCommandId,
     208              :                              const DataModel::EncodableToTLV & aEncodable) = 0;
     209              : 
     210              :     /**
     211              :      * Check whether the InvokeRequest we are handling is a timed invoke.
     212              :      */
     213              :     virtual bool IsTimedInvoke() const = 0;
     214              : 
     215              :     /**
     216              :      * @brief Flush acks right away for a slow command
     217              :      *
     218              :      * Some commands that do heavy lifting of storage/crypto should
     219              :      * ack right away to improve reliability and reduce needless retries. This
     220              :      * method can be manually called in commands that are especially slow to
     221              :      * immediately schedule an acknowledgement (if needed) since the delayed
     222              :      * stand-alone ack timer may actually not hit soon enough due to blocking command
     223              :      * execution.
     224              :      *
     225              :      */
     226              :     virtual void FlushAcksRightAwayOnSlowCommand() = 0;
     227              : 
     228              :     virtual Access::SubjectDescriptor GetSubjectDescriptor() const = 0;
     229              : 
     230              :     /**
     231              :      * Gets the inner exchange context object, without ownership.
     232              :      *
     233              :      * WARNING: This is dangerous, since it is directly interacting with the
     234              :      *          exchange being managed automatically by mpResponder and
     235              :      *          if not done carefully, may end up with use-after-free errors.
     236              :      *
     237              :      * @return The inner exchange context, might be nullptr if no
     238              :      *         exchange context has been assigned or the context
     239              :      *         has been released.
     240              :      */
     241              :     virtual Messaging::ExchangeContext * GetExchangeContext() const = 0;
     242              : 
     243              :     /**
     244              :      * API for adding a data response.  The template parameter T is generally
     245              :      * expected to be a ClusterName::Commands::CommandName::Type struct, but any
     246              :      * object that can be encoded using the DataModel::Encode machinery and
     247              :      * exposes the right command id will work.
     248              :      *
     249              :      * If you have no great way of handling the returned CHIP_ERROR, consider
     250              :      * using `AddResponse` which will automatically reply with `Failure` in
     251              :      * case AddResponseData fails.
     252              :      *
     253              :      * @param [in] aRequestCommandPath the concrete path of the command we are
     254              :      *             responding to.
     255              :      *
     256              :      *             The response path will be the same as the request, except the
     257              :      *             reply command ID used will be `CommandData::GetCommandId()` assumed
     258              :      *             to be a member of the templated type
     259              :      *
     260              :      * @param [in] aData the data for the response. It is expected to provide
     261              :      *             `GetCommandData` as a STATIC on its type as well as encode the
     262              :      *             correct data structure for building a reply.
     263              :      */
     264              :     template <typename CommandData>
     265              :     CHIP_ERROR AddResponseData(const ConcreteCommandPath & aRequestCommandPath, const CommandData & aData)
     266              :     {
     267              :         EncodableResponseCommandPayload<CommandData> encoder(aData);
     268              :         return AddResponseData(aRequestCommandPath, CommandData::GetCommandId(), encoder);
     269              :     }
     270              : 
     271              :     /**
     272              :      * API for adding a response.  This will try to encode a data response (response command), and if that fails
     273              :      * it will encode a Protocols::InteractionModel::Status::Failure status response instead.
     274              :      *
     275              :      * Above is the main difference from AddResponseData: AddResponse will auto-reply with failure while
     276              :      * AddResponseData allows the caller to try to deal with any CHIP_ERRORs.
     277              :      *
     278              :      * The template parameter T is generally expected to be a ClusterName::Commands::CommandName::Type struct, but any object that
     279              :      * can be encoded using the DataModel::Encode machinery and exposes the right command id will work.
     280              :      *
     281              :      * Since the function will call AddStatus when it fails to encode the data, it cannot send any response when it fails to encode
     282              :      * a status code since another AddStatus call will also fail. The error from AddStatus will just be logged.
     283              :      *
     284              :      * @param [in] aRequestCommandPath the concrete path of the command we are
     285              :      *             responding to.
     286              :      * @param [in] aData the data for the response.
     287              :      */
     288              :     template <typename CommandData>
     289              :     void AddResponse(const ConcreteCommandPath & aRequestCommandPath, const CommandData & aData)
     290              :     {
     291              :         EncodableResponseCommandPayload<CommandData> encodable(aData);
     292              :         AddResponse(aRequestCommandPath, CommandData::GetCommandId(), encodable);
     293              :     }
     294              : 
     295              : protected:
     296              :     // Encoding a response command payload requires a fabric index, in general,
     297              :     // because any fabric-scoped fields in the payload need it to deal with
     298              :     // their fabric-sensitive fields.
     299              :     template <typename CommandData>
     300              :     class EncodableResponseCommandPayload : public DataModel::EncodableToTLV
     301              :     {
     302              :     public:
     303              :         EncodableResponseCommandPayload(const CommandData & value) : mValue(value) {}
     304              : 
     305              :         CHIP_ERROR EncodeTo(DataModel::FabricAwareTLVWriter & writer, TLV::Tag tag) const final
     306              :         {
     307              :             return DataModel::EncodeResponseCommandPayload(writer, tag, mValue);
     308              :         }
     309              : 
     310              :         CHIP_ERROR EncodeTo(TLV::TLVWriter & writer, TLV::Tag tag) const final
     311              :         {
     312              :             // Not used, keep it as small as we can.
     313              :             return CHIP_ERROR_INCORRECT_STATE;
     314              :         }
     315              : 
     316              :     private:
     317              :         const CommandData & mValue;
     318              :     };
     319              : 
     320              :     /**
     321              :      * IncrementHoldOff will increase the inner refcount of the CommandHandler.
     322              :      *
     323              :      * Users should use CommandHandler::Handle for management the lifespan of the CommandHandler.
     324              :      * DefRef should be released in reasonable time, and Close() should only be called when the refcount reached 0.
     325              :      */
     326            0 :     virtual void IncrementHoldOff(Handle * apHandle) {}
     327              : 
     328              :     /**
     329              :      * DecrementHoldOff is used by CommandHandler::Handle for decreasing the refcount of the CommandHandler.
     330              :      * When refcount reached 0, CommandHandler will send the response to the peer and shutdown.
     331              :      */
     332            0 :     virtual void DecrementHoldOff(Handle * apHandle) {}
     333              : };
     334              : 
     335              : } // namespace app
     336              : } // namespace chip
        

Generated by: LCOV version 2.0-1