Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / system / SystemFaultInjection.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2016-2017 Nest Labs, Inc.
5  *
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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  */
18
19 #include <system/SystemConfig.h>
20
21 /**
22  *    @file
23  *      Implementation of the fault-injection utilities for CHIP System Layer.
24  */
25 #if CHIP_SYSTEM_CONFIG_TEST
26 /* module header, also carries config, comes first */
27 #include <system/SystemFaultInjection.h>
28
29 #include "SystemLayerPrivate.h"
30
31 #include <nlassert.h>
32 #include <string.h>
33
34 namespace chip {
35 namespace System {
36 namespace FaultInjection {
37
38 using nl::FaultInjection::Manager;
39 using nl::FaultInjection::Name;
40 using nl::FaultInjection::Record;
41
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[] = {
47     "PacketBufferNew",
48     "TimeoutImmediate",
49     "AsyncEvent",
50 };
51
52 static int32_t (*sGetNumEventsAvailable)();
53 static void (*sInjectAsyncEvent)(int32_t index);
54
55 Manager & GetManager()
56 {
57     if (0 == sManager.GetNumFaults())
58     {
59         sManager.Init(kFault_NumberOfFaultIdentifiers, sFaultRecordArray, sManagerName, sFaultNames);
60
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]));
65     }
66
67     return sManager;
68 }
69
70 void InjectAsyncEvent()
71 {
72     int32_t numEventsAvailable               = 0;
73     chip::System::FaultInjection::Id faultID = kFault_AsyncEvent;
74
75     if (sGetNumEventsAvailable)
76     {
77         numEventsAvailable = sGetNumEventsAvailable();
78
79         if (numEventsAvailable)
80         {
81             FaultInjection::Manager & mgr         = chip::System::FaultInjection::GetManager();
82             const FaultInjection::Record * record = &(mgr.GetFaultRecords()[faultID]);
83
84             if (record->mNumArguments == 0)
85             {
86                 int32_t maxEventIndex = numEventsAvailable - 1;
87
88                 mgr.StoreArgsAtFault(faultID, 1, &maxEventIndex);
89             }
90
91             nlFAULT_INJECT_WITH_ARGS(
92                 mgr, faultID,
93                 // Code executed with the Manager's lock:
94                 int32_t index = 0;
95                 if (numFaultArgs > 0) { index = faultArgs[0]; },
96                 // Code executed without the Manager's lock:
97                 if (sInjectAsyncEvent) { sInjectAsyncEvent(index); });
98         }
99     }
100 }
101
102 void SetAsyncEventCallbacks(int32_t (*aGetNumEventsAvailable)(), void (*aInjectAsyncEvent)(int32_t index))
103 {
104     sGetNumEventsAvailable = aGetNumEventsAvailable;
105     sInjectAsyncEvent      = aInjectAsyncEvent;
106 }
107
108 } // namespace FaultInjection
109 } // namespace System
110 } // namespace chip
111
112 #endif // CHIP_SYSTEM_CONFIG_TEST