Line data Source code
1 : /*
2 : * Copyright (c) 2022 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 :
18 : #pragma once
19 :
20 : #include <lib/core/CHIPError.h>
21 : #include <lib/support/IntrusiveList.h>
22 : #include <lib/support/Span.h>
23 : #include <stddef.h>
24 : #include <stdint.h>
25 :
26 : namespace chip {
27 :
28 : class TestEventTriggerHandler : public IntrusiveListNodeBase<IntrusiveMode::AutoUnlink>
29 : {
30 : public:
31 10 : virtual ~TestEventTriggerHandler() = default;
32 : /**
33 : * Handles the test event trigger based on `eventTrigger` provided.
34 : *
35 : * @param[in] eventTrigger Event trigger to handle.
36 : *
37 : * @return CHIP_NO_ERROR on success or another CHIP_ERROR on failure
38 : */
39 : virtual CHIP_ERROR HandleEventTrigger(uint64_t eventTrigger) = 0;
40 : };
41 :
42 : class TestEventTriggerDelegate
43 : {
44 : public:
45 : /* Expected byte size of the EnableKey */
46 : static constexpr size_t kEnableKeyLength = 16;
47 :
48 : virtual ~TestEventTriggerDelegate() {}
49 : /**
50 : * Checks to see if `enableKey` provided matches value chosen by the manufacturer.
51 : *
52 : * @param[in] enableKey Buffer of the key to verify.
53 : */
54 : virtual bool DoesEnableKeyMatch(const ByteSpan & enableKey) const = 0;
55 :
56 : /**
57 : * Handles the test event trigger based on `eventTrigger` provided.
58 : *
59 : * By default, this iterates over handlers added via `AddEventTriggerHandler`.
60 : *
61 : * If more specific behavior is desired, the method can be completely overridden.
62 : *
63 : * @param[in] eventTrigger Event trigger to handle.
64 : *
65 : * @return CHIP_NO_ERROR on success or another CHIP_ERROR on failure
66 : */
67 : virtual CHIP_ERROR HandleEventTriggers(uint64_t eventTrigger)
68 : {
69 : CHIP_ERROR last_error = CHIP_ERROR_INVALID_ARGUMENT;
70 : for (TestEventTriggerHandler & handler : mHandlerList)
71 : {
72 : last_error = handler.HandleEventTrigger(eventTrigger);
73 : if (last_error == CHIP_NO_ERROR)
74 : {
75 : break;
76 : }
77 : }
78 :
79 : return last_error;
80 : }
81 :
82 : CHIP_ERROR AddHandler(TestEventTriggerHandler * handler)
83 : {
84 : VerifyOrReturnError(!mHandlerList.Contains(handler), CHIP_ERROR_INVALID_ARGUMENT);
85 : mHandlerList.PushBack(handler);
86 : return CHIP_NO_ERROR;
87 : }
88 :
89 : void RemoveHandler(TestEventTriggerHandler * handler)
90 : {
91 : VerifyOrReturn(mHandlerList.Contains(handler));
92 : mHandlerList.Remove(handler);
93 : }
94 :
95 : void ClearAllHandlers() { mHandlerList.Clear(); }
96 :
97 : protected:
98 : IntrusiveList<TestEventTriggerHandler, IntrusiveMode::AutoUnlink> mHandlerList;
99 : };
100 :
101 : /**
102 : * @brief TestEventTriggerDelegate that owns its enable key in RAM.
103 : *
104 : */
105 : class SimpleTestEventTriggerDelegate : public TestEventTriggerDelegate
106 : {
107 : public:
108 : SimpleTestEventTriggerDelegate() { memset(&mEnableKey[0], 0, sizeof(mEnableKey)); }
109 :
110 : /**
111 : * @brief Initialize the delegate with a key
112 : *
113 : * @param enableKey - EnableKey to use for this instance.
114 : * @return CHIP_NO_ERROR on success, CHIP_ERROR_INVALID_ARGUMENT if enableKey is wrong size.
115 : */
116 : CHIP_ERROR Init(ByteSpan enableKey)
117 : {
118 : VerifyOrReturnError(enableKey.size() == sizeof(mEnableKey), CHIP_ERROR_INVALID_ARGUMENT);
119 : MutableByteSpan ourEnableKeySpan(mEnableKey);
120 : return CopySpanToMutableSpan(enableKey, ourEnableKeySpan);
121 : }
122 :
123 : bool DoesEnableKeyMatch(const ByteSpan & enableKey) const override { return enableKey.data_equal(ByteSpan(mEnableKey)); }
124 :
125 : private:
126 : uint8_t mEnableKey[TestEventTriggerDelegate::kEnableKeyLength];
127 : };
128 :
129 : } // namespace chip
|