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 153 : virtual ~Provider() = default;
48 :
49 : // `context` pointers will be guaranteed valid until Shutdown is called()
50 212 : virtual CHIP_ERROR Startup(InteractionModelContext context)
51 : {
52 212 : mContext = context;
53 212 : 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 : virtual 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 : /// `handler` is used to send back the reply.
83 : /// - returning `std::nullopt` means that return value was placed in handler directly.
84 : /// This includes cases where command handling and value return will be done asynchronously.
85 : /// - returning a value other than Success implies an error reply (error and data are mutually exclusive)
86 : ///
87 : /// Preconditions:
88 : /// - `request.path` MUST refer to a command that actually exists. This is because in practice
89 : /// callers must do ACL and flag checks (e.g. for timed invoke) before calling this function.
90 : ///
91 : /// Callers that do not care about those checks should use `ProviderMetadataTree::AcceptedCommands`
92 : /// to check for command existence.
93 : ///
94 : /// - TODO: as interfaces are updated, we may want to make the above requirement more
95 : /// relaxed, as it seems desirable for users of this interface to have guaranteed
96 : /// behavior (like error on invalid paths) whereas today this seems unclear as some
97 : /// command intercepts do not validate that the command is in fact accepted on the
98 : /// endpoint provided.
99 : ///
100 : /// Return value expectations:
101 : /// - if a response has been placed into `handler` then std::nullopt MUST be returned. In particular
102 : /// note that CHIP_NO_ERROR is NOT the same as std::nullopt:
103 : /// > CHIP_NO_ERROR means handler had no status set and we expect the caller to AddStatus(success)
104 : /// > std::nullopt means that handler has added an appropriate data/status response
105 : /// - if a value is returned (not nullopt) then the handler response MUST NOT be filled. The caller
106 : /// will then issue `handler->AddStatus(request.path, <return_value>->GetStatusCode())`. This is a
107 : /// convenience to make writing Invoke calls easier.
108 : virtual std::optional<ActionReturnStatus> Invoke(const InvokeRequest & request, chip::TLV::TLVReader & input_arguments,
109 : CommandHandler * handler) = 0;
110 :
111 : private:
112 : InteractionModelContext mContext = { nullptr };
113 : };
114 :
115 : } // namespace DataModel
116 : } // namespace app
117 : } // namespace chip
|