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

Generated by: LCOV version 2.0-1