Vasu V | 57f47c4 | 2022-04-04 10:55:44 +0530 | [diff] [blame^] | 1 | /* |
| 2 | // Copyright (c) 2022 Intel 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 | #pragma once |
| 17 | |
| 18 | #include <systemd/sd-journal.h> |
| 19 | |
| 20 | #include <error_monitors/base_monitor.hpp> |
| 21 | #include <gpiod.hpp> |
| 22 | #include <host_error_monitor.hpp> |
| 23 | #include <sdbusplus/asio/object_server.hpp> |
| 24 | |
| 25 | namespace host_error_monitor::cpu_presence_monitor |
| 26 | { |
| 27 | class CPUPresenceMonitor : public host_error_monitor::base_monitor::BaseMonitor |
| 28 | { |
| 29 | int cpuPresent; |
| 30 | size_t cpuNum; |
| 31 | const static constexpr uint8_t beepCPUMIssing = 3; |
| 32 | |
| 33 | void logEvent() |
| 34 | { |
| 35 | std::string msg = "CPU " + std::to_string(cpuNum) + " missing"; |
| 36 | |
| 37 | sd_journal_send("MESSAGE=HostError: %s", msg.c_str(), "PRIORITY=%i", |
| 38 | LOG_INFO, "REDFISH_MESSAGE_ID=%s", |
| 39 | "OpenBMC.0.1.CPUError", "REDFISH_MESSAGE_ARGS=%s", |
| 40 | msg.c_str(), NULL); |
| 41 | } |
| 42 | |
| 43 | void CPUPresenceAssertHandler( |
| 44 | std::shared_ptr<sdbusplus::asio::connection> conn) |
| 45 | { |
| 46 | std::cerr << signalName << " asserted\n"; |
| 47 | // raising beep alert for base cpu missing |
| 48 | beep(conn, beepCPUMIssing); |
| 49 | logEvent(); |
| 50 | } |
| 51 | |
| 52 | bool getCPUPresence(const std::string& cpuPresenceName) |
| 53 | { |
| 54 | // Find the GPIO line |
| 55 | gpiod::line cpuPresenceLine = gpiod::find_line(cpuPresenceName); |
| 56 | if (!cpuPresenceLine) |
| 57 | { |
| 58 | std::cerr << "Failed to find the " << cpuPresenceName << " line.\n"; |
| 59 | return false; |
| 60 | } |
| 61 | |
| 62 | // Request GPIO input |
| 63 | try |
| 64 | { |
| 65 | cpuPresenceLine.request( |
| 66 | {"host-error-monitor", gpiod::line_request::DIRECTION_INPUT}); |
| 67 | } |
| 68 | catch (const std::exception&) |
| 69 | { |
| 70 | std::cerr << "Failed to request " << cpuPresenceName << " input\n"; |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | // CPU presence is low-assert |
| 75 | cpuPresent = !cpuPresenceLine.get_value(); |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | void checkCPUPresence(std::shared_ptr<sdbusplus::asio::connection> conn) |
| 81 | { |
| 82 | // Ignore this if the CPU present |
| 83 | if (!cpuPresent) |
| 84 | { |
| 85 | CPUPresenceAssertHandler(conn); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | public: |
| 90 | CPUPresenceMonitor(boost::asio::io_service& io, |
| 91 | std::shared_ptr<sdbusplus::asio::connection> conn, |
| 92 | const std::string& signalName, const size_t cpuNum) : |
| 93 | BaseMonitor(io, conn, signalName), |
| 94 | cpuNum(cpuNum) |
| 95 | { |
| 96 | if (!getCPUPresence(signalName)) |
| 97 | { |
| 98 | return; |
| 99 | } |
| 100 | checkCPUPresence(conn); |
| 101 | valid = true; |
| 102 | } |
| 103 | void hostOn() override |
| 104 | { |
| 105 | checkCPUPresence(conn); |
| 106 | } |
| 107 | }; |
| 108 | } // namespace host_error_monitor::cpu_presence_monitor |