3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2016-2017 Nest Labs, Inc.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * Header file for the fault-injection utilities for CHIP System Layer.
26 #include <system/SystemConfig.h>
28 #if CHIP_SYSTEM_CONFIG_TEST && CHIP_WITH_NLFAULTINJECTION
30 #include <nlfaultinjection.hpp>
32 #include <support/DLLUtil.h>
36 namespace FaultInjection {
38 using nl::FaultInjection::Manager;
41 * @brief Fault injection points
44 * Each point in the code at which a fault can be injected
45 * is identified by a member of this enum.
49 kFault_PacketBufferNew, /**< Fail the allocation of a PacketBuffer */
50 kFault_TimeoutImmediate, /**< Override the timeout value of a timer being started with 0 */
51 kFault_AsyncEvent, /**< Inject asynchronous events; when the fault is enabled, it expects
52 one integer argument, which is passed to application to signal the event
53 to be injected; @see CHIP_SYSTEM_FAULT_INJECT_ASYNC_EVENT */
54 kFault_NumberOfFaultIdentifiers,
57 DLL_EXPORT nl::FaultInjection::Manager & GetManager();
60 * Callback to the application that returns how many asynchronous events the application could
61 * inject at the time of the call.
63 * @return The number of events
65 typedef int32_t (*GetNumEventsAvailableCb)();
68 * Callback to the application to inject the asynchronous event specified by argument.
70 * @param[in] aEventIndex An index (0 to the value returned by GetNumEventsAvailableCb -1)
71 * that identifies the event to be injected.
73 typedef void (*InjectAsyncEventCb)(int32_t aEventIndex);
76 * Store the GetNumEventsAvailableCb and InjectAsyncEventCb callbacks used by
77 * @see CHIP_SYSTEM_FAULT_INJECT_ASYNC_EVENT
79 * @param[in] aGetNumEventsAvailable A GetNumEventsAvailableCb
80 * @param[in] aInjectAsyncEvent An InjectAsyncEventCb
83 DLL_EXPORT void SetAsyncEventCallbacks(GetNumEventsAvailableCb aGetNumEventsAvailable, InjectAsyncEventCb aInjectAsyncEvent);
86 * @see CHIP_SYSTEM_FAULT_INJECT_ASYNC_EVENT
88 DLL_EXPORT void InjectAsyncEvent();
90 } // namespace FaultInjection
95 * Execute the statements included if the System fault is
98 * @param[in] aFaultID A System fault-injection id
99 * @param[in] aStatements Statements to be executed if the fault is enabled.
101 #define CHIP_SYSTEM_FAULT_INJECT(aFaultId, aStatement) \
102 nlFAULT_INJECT(::chip::System::FaultInjection::GetManager(), aFaultId, aStatement)
105 * This macro implements the injection of asynchronous events.
107 * It polls the application by calling the GetNumEventsAvailableCb callback
108 * to know if there are asynchronous events that can be injected.
109 * If there are any, it instances kFault_AsyncEvent.
110 * If the fault is to be injected, the code injected calls the InjectAsyncEventCb
111 * callback passing the integer argument stored in the fault Record.
112 * If the fault is not configured (and therefore no arguments are stored in the Record)
113 * the macro stores the return value of GetNumEventsAvailableCb into the Records arguments,
114 * so that the application can log it from a callback installed into the fault.
116 #define CHIP_SYSTEM_FAULT_INJECT_ASYNC_EVENT() \
119 chip::System::FaultInjection::InjectAsyncEvent(); \
122 #else // CHIP_SYSTEM_CONFIG_TEST
124 #define CHIP_SYSTEM_FAULT_INJECT(aFaultId, aStatement)
126 #define CHIP_SYSTEM_FAULT_INJECT_ASYNC_EVENT()
128 #endif // CHIP_SYSTEM_CONFIG_TEST