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.
19 #include <system/SystemConfig.h>
23 * Implementation of the fault-injection utilities for CHIP System Layer.
25 #if CHIP_SYSTEM_CONFIG_TEST
26 /* module header, also carries config, comes first */
27 #include <system/SystemFaultInjection.h>
29 #include "SystemLayerPrivate.h"
36 namespace FaultInjection {
38 using nl::FaultInjection::Manager;
39 using nl::FaultInjection::Name;
40 using nl::FaultInjection::Record;
42 static Record sFaultRecordArray[kFault_NumberOfFaultIdentifiers];
43 static Manager sManager;
44 static int32_t sFault_AsyncEvent_Arguments[1];
45 static const Name sManagerName = "CHIPSys";
46 static const Name sFaultNames[] = {
52 static int32_t (*sGetNumEventsAvailable)();
53 static void (*sInjectAsyncEvent)(int32_t index);
55 Manager & GetManager()
57 if (0 == sManager.GetNumFaults())
59 sManager.Init(kFault_NumberOfFaultIdentifiers, sFaultRecordArray, sManagerName, sFaultNames);
61 memset(&sFault_AsyncEvent_Arguments, 0, sizeof(sFault_AsyncEvent_Arguments));
62 sFaultRecordArray[kFault_AsyncEvent].mArguments = sFault_AsyncEvent_Arguments;
63 sFaultRecordArray[kFault_AsyncEvent].mLengthOfArguments =
64 static_cast<uint16_t>(sizeof(sFault_AsyncEvent_Arguments) / sizeof(sFault_AsyncEvent_Arguments[0]));
70 void InjectAsyncEvent()
72 int32_t numEventsAvailable = 0;
73 chip::System::FaultInjection::Id faultID = kFault_AsyncEvent;
75 if (sGetNumEventsAvailable)
77 numEventsAvailable = sGetNumEventsAvailable();
79 if (numEventsAvailable)
81 FaultInjection::Manager & mgr = chip::System::FaultInjection::GetManager();
82 const FaultInjection::Record * record = &(mgr.GetFaultRecords()[faultID]);
84 if (record->mNumArguments == 0)
86 int32_t maxEventIndex = numEventsAvailable - 1;
88 mgr.StoreArgsAtFault(faultID, 1, &maxEventIndex);
91 nlFAULT_INJECT_WITH_ARGS(
93 // Code executed with the Manager's lock:
95 if (numFaultArgs > 0) { index = faultArgs[0]; },
96 // Code executed without the Manager's lock:
97 if (sInjectAsyncEvent) { sInjectAsyncEvent(index); });
102 void SetAsyncEventCallbacks(int32_t (*aGetNumEventsAvailable)(), void (*aInjectAsyncEvent)(int32_t index))
104 sGetNumEventsAvailable = aGetNumEventsAvailable;
105 sInjectAsyncEvent = aInjectAsyncEvent;
108 } // namespace FaultInjection
109 } // namespace System
112 #endif // CHIP_SYSTEM_CONFIG_TEST