/src/openweave-core/src/system/SystemFaultInjection.cpp
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (c) 2016-2017 Nest Labs, Inc. |
4 | | * All rights reserved. |
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 | | /** |
20 | | * @file |
21 | | * Implementation of the fault-injection utilities for Weave System Layer. |
22 | | */ |
23 | | |
24 | | #include <string.h> |
25 | | #include <nlassert.h> |
26 | | #include <SystemLayer/SystemFaultInjection.h> |
27 | | |
28 | | #if WEAVE_SYSTEM_CONFIG_TEST |
29 | | |
30 | | #include "SystemLayerPrivate.h" |
31 | | |
32 | | namespace nl { |
33 | | namespace Weave { |
34 | | namespace System { |
35 | | namespace FaultInjection { |
36 | | |
37 | | using ::nl::FaultInjection::Record; |
38 | | using ::nl::FaultInjection::Manager; |
39 | | using ::nl::FaultInjection::Name; |
40 | | |
41 | | static Record sFaultRecordArray[kFault_NumberOfFaultIdentifiers]; |
42 | | static Manager sManager; |
43 | | static int32_t sFault_AsyncEvent_Arguments[1]; |
44 | | static const Name sManagerName = "WeaveSys"; |
45 | | static const Name sFaultNames[] = { |
46 | | "PacketBufferNew", |
47 | | "TimeoutImmediate", |
48 | | "AsyncEvent", |
49 | | }; |
50 | | |
51 | | static int32_t (*sGetNumEventsAvailable)(void); |
52 | | static void (*sInjectAsyncEvent)(int32_t index); |
53 | | |
54 | | Manager& GetManager(void) |
55 | 24.1k | { |
56 | 24.1k | if (0 == sManager.GetNumFaults()) |
57 | 5 | { |
58 | 5 | sManager.Init(kFault_NumberOfFaultIdentifiers, |
59 | 5 | sFaultRecordArray, |
60 | 5 | sManagerName, |
61 | 5 | sFaultNames); |
62 | | |
63 | 5 | memset(&sFault_AsyncEvent_Arguments, 0, sizeof(sFault_AsyncEvent_Arguments)); |
64 | 5 | sFaultRecordArray[kFault_AsyncEvent].mArguments = sFault_AsyncEvent_Arguments; |
65 | 5 | sFaultRecordArray[kFault_AsyncEvent].mLengthOfArguments = |
66 | 5 | static_cast<uint16_t>(sizeof(sFault_AsyncEvent_Arguments)/sizeof(sFault_AsyncEvent_Arguments[0])); |
67 | 5 | } |
68 | | |
69 | 24.1k | return sManager; |
70 | 24.1k | } |
71 | | |
72 | | void InjectAsyncEvent(void) |
73 | 0 | { |
74 | 0 | int32_t numEventsAvailable = 0; |
75 | 0 | nl::Weave::System::FaultInjection::Id faultID = kFault_AsyncEvent; |
76 | |
|
77 | 0 | if (sGetNumEventsAvailable) |
78 | 0 | { |
79 | 0 | numEventsAvailable = sGetNumEventsAvailable(); |
80 | |
|
81 | 0 | if (numEventsAvailable) |
82 | 0 | { |
83 | 0 | nl::FaultInjection::Manager &mgr = nl::Weave::System::FaultInjection::GetManager(); |
84 | 0 | const nl::FaultInjection::Record *record = &(mgr.GetFaultRecords()[faultID]); |
85 | |
|
86 | 0 | if (record->mNumArguments == 0) |
87 | 0 | { |
88 | 0 | int32_t maxEventIndex = numEventsAvailable - 1; |
89 | |
|
90 | 0 | mgr.StoreArgsAtFault(faultID, 1, &maxEventIndex); |
91 | 0 | } |
92 | |
|
93 | 0 | nlFAULT_INJECT_WITH_ARGS(mgr, faultID, |
94 | | // Code executed with the Manager's lock: |
95 | 0 | int32_t index = 0; |
96 | 0 | if (numFaultArgs > 0) |
97 | 0 | { |
98 | 0 | index = faultArgs[0]; |
99 | 0 | } |
100 | 0 | , |
101 | | // Code executed without the Manager's lock: |
102 | 0 | if (sInjectAsyncEvent) |
103 | 0 | { |
104 | 0 | sInjectAsyncEvent(index); |
105 | 0 | } |
106 | 0 | ); |
107 | 0 | } |
108 | 0 | } |
109 | 0 | } |
110 | | |
111 | | void SetAsyncEventCallbacks(int32_t (*aGetNumEventsAvailable)(void), void (*aInjectAsyncEvent)(int32_t index)) |
112 | 0 | { |
113 | 0 | sGetNumEventsAvailable = aGetNumEventsAvailable; |
114 | 0 | sInjectAsyncEvent = aInjectAsyncEvent; |
115 | 0 | } |
116 | | |
117 | | |
118 | | } // namespace FaultInjection |
119 | | } // namespace System |
120 | | } // namespace Weave |
121 | | } // namespace nl |
122 | | |
123 | | #endif // WEAVE_SYSTEM_CONFIG_TEST |