blob: 74c241bc43ab9ec5a457f535f2ee9097d01ab147 [file] [log] [blame]
Bob King462e5922020-11-19 17:48:30 +08001/**
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"
Shawn McCarney81a2f902021-03-23 21:41:34 -050023#include "error_logging_utils.hpp"
Bob King462e5922020-11-19 17:48:30 +080024#include "exception_utils.hpp"
25#include "system.hpp"
26
27#include <exception>
28
29namespace phosphor::power::regulators
30{
31
32bool PresenceDetection::execute(Services& services, System& system,
33 Chassis& /*chassis*/, Device& device)
34{
35 // If no presence value is cached
36 if (!isPresent.has_value())
37 {
38 // Initially assume device is present
39 isPresent = true;
40
41 // Execute actions to find device presence
42 try
43 {
44 // Create ActionEnvironment
45 ActionEnvironment environment{system.getIDMap(), device.getID(),
46 services};
47
48 // Execute the actions and cache resulting value
49 isPresent = action_utils::execute(actions, environment);
50 }
51 catch (const std::exception& e)
52 {
53 // Log error messages in journal
54 services.getJournal().logError(exception_utils::getMessages(e));
55 services.getJournal().logError("Unable to determine presence of " +
56 device.getID());
57
Shawn McCarney81a2f902021-03-23 21:41:34 -050058 // Create error log entry
59 error_logging_utils::logError(std::current_exception(),
60 Entry::Level::Warning, services);
Bob King462e5922020-11-19 17:48:30 +080061 }
62 }
63
64 return isPresent.value();
65}
66
67} // namespace phosphor::power::regulators