Line data Source code
1 : /*
2 : * Copyright (c) 2024 Project CHIP Authors
3 : * All rights reserved.
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 : #pragma once
18 :
19 : #include <lib/core/CHIPError.h>
20 : #include <lib/core/TLVReader.h>
21 : #include <lib/core/TLVWriter.h>
22 :
23 : #include <app/AttributeValueDecoder.h>
24 : #include <app/AttributeValueEncoder.h>
25 : #include <app/CommandHandler.h>
26 : #include <app/EventPathParams.h>
27 :
28 : #include <app/data-model-provider/ActionReturnStatus.h>
29 : #include <app/data-model-provider/Context.h>
30 : #include <app/data-model-provider/OperationTypes.h>
31 : #include <app/data-model-provider/ProviderMetadataTree.h>
32 :
33 : namespace chip {
34 : namespace app {
35 : namespace DataModel {
36 :
37 : /// Represents operations against a matter-defined data model.
38 : ///
39 : /// Class is SINGLE-THREADED:
40 : /// - operations are assumed to only be ever run in a single event-loop
41 : /// thread or equivalent
42 : /// - class is allowed to attempt to cache indexes/locations for faster
43 : /// lookups of things (e.g during iterations)
44 : class Provider : public ProviderMetadataTree
45 : {
46 : public:
47 161 : virtual ~Provider() = default;
48 :
49 : // `context` pointers will be guaranteed valid until Shutdown is called()
50 218 : virtual CHIP_ERROR Startup(InteractionModelContext context)
51 : {
52 218 : mContext = context;
53 218 : return CHIP_NO_ERROR;
54 : }
55 : virtual CHIP_ERROR Shutdown() = 0;
56 :
57 : // During the transition phase, we expect a large subset of code to require access to
58 : // event emitting, path marking and other operations
59 2577 : [[nodiscard]] const InteractionModelContext & CurrentContext() const { return mContext; }
60 :
61 : /// NOTE: this code is NOT required to handle `List` global attributes:
62 : /// AcceptedCommandsList, GeneratedCommandsList OR AttributeList
63 : ///
64 : /// Users of DataModel::Provider are expected to get these lists
65 : /// from ProviderMetadataTree (in particular IM Reads of these
66 : /// attributes will the automatically filled from metadata).
67 : ///
68 : /// Return value notes:
69 : /// ActionReturnStatus::IsOutOfSpaceEncodingResponse
70 : /// - Indicates that list encoding had insufficient buffer space to encode elements.
71 : /// - encoder::GetState().AllowPartialData() determines if these errors are permanent (no partial
72 : /// data allowed) or further encoding can be retried (AllowPartialData true for list encoding)
73 : virtual ActionReturnStatus ReadAttribute(const ReadAttributeRequest & request, AttributeValueEncoder & encoder) = 0;
74 :
75 : /// Requests a write of an attribute.
76 : ///
77 : /// When this is invoked, caller is expected to have already done some validations:
78 : /// - cluster `data version` has been checked for the incoming request if applicable
79 : /// - validation of ACL/timed interaction flags/writability, if those checks are desired.
80 : virtual ActionReturnStatus WriteAttribute(const WriteAttributeRequest & request, AttributeValueDecoder & decoder) = 0;
81 :
82 : /// Indicates the start/end of a series of list operations. This function will be called either before the first
83 : /// Write operation or after the last one of a series of consecutive attribute data of the same attribute.
84 : ///
85 : /// 1) This function will be called if the client tries to set a nullable list attribute to null.
86 : /// 2) This function will only be called at the beginning and end of a series of consecutive attribute data
87 : /// blocks for the same attribute, no matter what list operations those data blocks represent.
88 : /// 3) The opType argument indicates the type of notification (Start, Failure, Success).
89 : virtual void ListAttributeWriteNotification(const ConcreteAttributePath & aPath, ListWriteOperation opType) = 0;
90 :
91 : /// `handler` is used to send back the reply.
92 : /// - returning `std::nullopt` means that return value was placed in handler directly.
93 : /// This includes cases where command handling and value return will be done asynchronously.
94 : /// - returning a value other than Success implies an error reply (error and data are mutually exclusive)
95 : ///
96 : /// Preconditions:
97 : /// - `request.path` MUST refer to a command that actually exists. This is because in practice
98 : /// callers must do ACL and flag checks (e.g. for timed invoke) before calling this function.
99 : ///
100 : /// Callers that do not care about those checks should use `ProviderMetadataTree::AcceptedCommands`
101 : /// to check for command existence.
102 : ///
103 : /// - TODO: as interfaces are updated, we may want to make the above requirement more
104 : /// relaxed, as it seems desirable for users of this interface to have guaranteed
105 : /// behavior (like error on invalid paths) whereas today this seems unclear as some
106 : /// command intercepts do not validate that the command is in fact accepted on the
107 : /// endpoint provided.
108 : ///
109 : /// Return value expectations:
110 : /// - if a response has been placed into `handler` then std::nullopt MUST be returned. In particular
111 : /// note that CHIP_NO_ERROR is NOT the same as std::nullopt:
112 : /// > CHIP_NO_ERROR means handler had no status set and we expect the caller to AddStatus(success)
113 : /// > std::nullopt means that handler has added an appropriate data/status response
114 : /// - if a value is returned (not nullopt) then the handler response MUST NOT be filled. The caller
115 : /// will then issue `handler->AddStatus(request.path, <return_value>->GetStatusCode())`. This is a
116 : /// convenience to make writing Invoke calls easier.
117 : virtual std::optional<ActionReturnStatus> InvokeCommand(const InvokeRequest & request, chip::TLV::TLVReader & input_arguments,
118 : CommandHandler * handler) = 0;
119 :
120 : protected:
121 : InteractionModelContext mContext = {};
122 : };
123 :
124 : } // namespace DataModel
125 : } // namespace app
126 : } // namespace chip
|