Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 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 | #include "power_control.hpp" |
| 17 | |
James Feist | 676d2c3 | 2019-02-14 10:07:27 -0800 | [diff] [blame] | 18 | bool PowerControl::forcePowerOff() |
Kuiying Wang | 80f6d92 | 2018-11-13 16:34:07 +0800 | [diff] [blame] | 19 | { |
James Feist | 676d2c3 | 2019-02-14 10:07:27 -0800 | [diff] [blame] | 20 | return true; |
Kuiying Wang | 80f6d92 | 2018-11-13 16:34:07 +0800 | [diff] [blame] | 21 | } |
| 22 | |
Kuiying Wang | 6ce6ab4 | 2019-01-29 11:10:13 +0800 | [diff] [blame^] | 23 | int32_t PowerControl::triggerReset() |
Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +0800 | [diff] [blame] | 24 | { |
| 25 | int ret = 0; |
| 26 | int count = 0; |
| 27 | char buf = '0'; |
| 28 | |
Kuiying Wang | 6ce6ab4 | 2019-01-29 11:10:13 +0800 | [diff] [blame^] | 29 | phosphor::logging::log<phosphor::logging::level::DEBUG>("triggerReset"); |
| 30 | |
| 31 | ret = ::lseek(reset_out_fd, 0, SEEK_SET); |
| 32 | if (ret < 0) |
| 33 | { |
| 34 | phosphor::logging::log<phosphor::logging::level::ERR>("lseek error!"); |
| 35 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 36 | IOError(); |
| 37 | } |
| 38 | |
| 39 | buf = '0'; |
| 40 | |
| 41 | ret = ::write(reset_out_fd, &buf, sizeof(buf)); |
| 42 | if (ret < 0) |
| 43 | { |
| 44 | phosphor::logging::log<phosphor::logging::level::ERR>("write error!"); |
| 45 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 46 | IOError(); |
| 47 | } |
| 48 | |
| 49 | std::this_thread::sleep_for(std::chrono::milliseconds(RESET_PULSE_TIME_MS)); |
| 50 | |
| 51 | buf = '1'; |
| 52 | ret = ::write(reset_out_fd, &buf, sizeof(buf)); |
| 53 | if (ret < 0) |
| 54 | { |
| 55 | phosphor::logging::log<phosphor::logging::level::ERR>("write error!"); |
| 56 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 57 | IOError(); |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | int32_t PowerControl::setPowerState(int newState) |
| 63 | { |
| 64 | int ret = 0; |
| 65 | int count = 0; |
| 66 | char buf = '0'; |
| 67 | |
| 68 | if (newState < 0 || newState >= powerStateMax) |
| 69 | { |
| 70 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 71 | "error! invalid parameter!"); |
| 72 | return -1; |
| 73 | } |
| 74 | |
Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +0800 | [diff] [blame] | 75 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 76 | "setPowerState", phosphor::logging::entry("NEWSTATE=%d", newState)); |
| 77 | |
Kuiying Wang | 6ce6ab4 | 2019-01-29 11:10:13 +0800 | [diff] [blame^] | 78 | if (powerStateReset == newState) |
| 79 | { |
| 80 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 81 | "setPowerState system reset"); |
| 82 | triggerReset(); |
| 83 | return 0; |
| 84 | } |
| 85 | |
Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +0800 | [diff] [blame] | 86 | if (state() == newState) |
| 87 | { |
| 88 | phosphor::logging::log<phosphor::logging::level::WARNING>( |
| 89 | "Same powerstate", |
| 90 | phosphor::logging::entry("NEWSTATE=%d", newState)); |
| 91 | return 0; |
| 92 | } |
Patrick Venture | 8fbed2e | 2018-11-01 19:25:31 -0700 | [diff] [blame] | 93 | |
Kuiying Wang | 6ce6ab4 | 2019-01-29 11:10:13 +0800 | [diff] [blame^] | 94 | state(newState); |
| 95 | |
Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +0800 | [diff] [blame] | 96 | ret = ::lseek(power_up_fd, 0, SEEK_SET); |
| 97 | if (ret < 0) |
| 98 | { |
Patrick Venture | 8fbed2e | 2018-11-01 19:25:31 -0700 | [diff] [blame] | 99 | phosphor::logging::log<phosphor::logging::level::ERR>("lseek error!"); |
Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +0800 | [diff] [blame] | 100 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 101 | IOError(); |
| 102 | } |
| 103 | |
Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +0800 | [diff] [blame] | 104 | buf = '0'; |
Kuiying Wang | 6ce6ab4 | 2019-01-29 11:10:13 +0800 | [diff] [blame^] | 105 | |
Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +0800 | [diff] [blame] | 106 | ret = ::write(power_up_fd, &buf, sizeof(buf)); |
| 107 | if (ret < 0) |
| 108 | { |
Kuiying Wang | 6ce6ab4 | 2019-01-29 11:10:13 +0800 | [diff] [blame^] | 109 | phosphor::logging::log<phosphor::logging::level::ERR>("write error!"); |
Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +0800 | [diff] [blame] | 110 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 111 | IOError(); |
| 112 | } |
| 113 | |
Kuiying Wang | 6ce6ab4 | 2019-01-29 11:10:13 +0800 | [diff] [blame^] | 114 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 115 | "setPowerState power on"); |
| 116 | std::this_thread::sleep_for(std::chrono::milliseconds(POWER_PULSE_TIME_MS)); |
Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +0800 | [diff] [blame] | 117 | |
| 118 | buf = '1'; |
| 119 | ret = ::write(power_up_fd, &buf, sizeof(buf)); |
| 120 | if (ret < 0) |
| 121 | { |
Kuiying Wang | 6ce6ab4 | 2019-01-29 11:10:13 +0800 | [diff] [blame^] | 122 | phosphor::logging::log<phosphor::logging::level::ERR>("write error!"); |
Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +0800 | [diff] [blame] | 123 | throw sdbusplus::xyz::openbmc_project::Chassis::Common::Error:: |
| 124 | IOError(); |
| 125 | } |
| 126 | |
Kuiying Wang | 6ce6ab4 | 2019-01-29 11:10:13 +0800 | [diff] [blame^] | 127 | if (0 == newState) |
| 128 | { |
| 129 | /* |
| 130 | * For power off, currently there is a known issue, the "long-press" |
| 131 | * power button cannot power off the host, a workaround is perform force |
| 132 | * power off after waitting for a while |
| 133 | */ |
| 134 | std::this_thread::sleep_for( |
| 135 | std::chrono::milliseconds(POWER_PULSE_TIME_MS)); |
| 136 | if (1 == pGood()) |
| 137 | { // still on, force off! |
| 138 | phosphor::logging::log<phosphor::logging::level::DEBUG>( |
| 139 | "Perform force power off"); |
| 140 | count = 0; |
| 141 | do |
| 142 | { |
| 143 | if (count++ > 5) |
| 144 | { |
| 145 | phosphor::logging::log<phosphor::logging::level::ERR>( |
| 146 | "forcePowerOff error!"); |
| 147 | throw sdbusplus::xyz::openbmc_project::Chassis::Common:: |
| 148 | Error::IOError(); |
| 149 | } |
| 150 | ret = forcePowerOff(); |
| 151 | std::this_thread::sleep_for( |
| 152 | std::chrono::milliseconds(POLLING_INTERVAL_MS)); |
| 153 | } while (ret != 0); |
| 154 | } |
| 155 | } |
Kuiying Wang | 64ff7ce | 2018-08-15 11:17:06 +0800 | [diff] [blame] | 156 | return 0; |
| 157 | } |