1 /**
2  * Copyright © 2020 IBM Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "presence_detection.hpp"
18 
19 #include "action_environment.hpp"
20 #include "action_utils.hpp"
21 #include "chassis.hpp"
22 #include "device.hpp"
23 #include "exception_utils.hpp"
24 #include "system.hpp"
25 
26 #include <exception>
27 
28 namespace phosphor::power::regulators
29 {
30 
31 bool PresenceDetection::execute(Services& services, System& system,
32                                 Chassis& /*chassis*/, Device& device)
33 {
34     // If no presence value is cached
35     if (!isPresent.has_value())
36     {
37         // Initially assume device is present
38         isPresent = true;
39 
40         // Execute actions to find device presence
41         try
42         {
43             // Create ActionEnvironment
44             ActionEnvironment environment{system.getIDMap(), device.getID(),
45                                           services};
46 
47             // Execute the actions and cache resulting value
48             isPresent = action_utils::execute(actions, environment);
49         }
50         catch (const std::exception& e)
51         {
52             // Log error messages in journal
53             services.getJournal().logError(exception_utils::getMessages(e));
54             services.getJournal().logError("Unable to determine presence of " +
55                                            device.getID());
56 
57             // TODO: Create error log entry
58         }
59     }
60 
61     return isPresent.value();
62 }
63 
64 } // namespace phosphor::power::regulators
65