Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 1 | # Contributors Listed Below - COPYRIGHT 2016 |
| 2 | # [+] International Business Machines Corp. |
| 3 | # |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. See the License for the specific language governing |
| 15 | # permissions and limitations under the License. |
| 16 | |
| 17 | import os |
| 18 | import subprocess |
| 19 | import dbus |
| 20 | import dbus.service |
| 21 | from obmc.dbuslib.bindings import DbusProperties |
| 22 | |
| 23 | |
| 24 | ## Abstract class, must subclass |
| 25 | class SensorValue(DbusProperties): |
| 26 | IFACE_NAME = 'org.openbmc.SensorValue' |
| 27 | |
| 28 | def __init__(self, bus, name): |
| 29 | self.Set(SensorValue.IFACE_NAME, 'units', "") |
| 30 | self.Set(SensorValue.IFACE_NAME, 'error', False) |
| 31 | |
| 32 | @dbus.service.method( |
| 33 | IFACE_NAME, in_signature='v', out_signature='') |
| 34 | def setValue(self, value): |
| 35 | self.Set(SensorValue.IFACE_NAME, 'value', value) |
| 36 | |
| 37 | @dbus.service.method( |
| 38 | IFACE_NAME, in_signature='', out_signature='v') |
| 39 | def getValue(self): |
| 40 | return self.Get(SensorValue.IFACE_NAME, 'value') |
| 41 | |
| 42 | |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 43 | class VirtualSensor(SensorValue): |
| 44 | def __init__(self, bus, name): |
| 45 | DbusProperties.__init__(self) |
| 46 | SensorValue.__init__(self, bus, name) |
| 47 | dbus.service.Object.__init__(self, bus, name) |
| 48 | |
| 49 | |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 50 | CONTROL_IFACE = 'org.openbmc.Control' |
| 51 | |
| 52 | |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 53 | class BootProgressSensor(VirtualSensor): |
| 54 | def __init__(self, bus, name): |
| 55 | VirtualSensor.__init__(self, bus, name) |
| 56 | self.setValue("Off") |
| 57 | bus.add_signal_receiver( |
| 58 | self.SystemStateHandler, signal_name="GotoSystemState") |
| 59 | |
| 60 | def SystemStateHandler(self, state): |
| 61 | if (state == "HOST_POWERED_OFF"): |
| 62 | self.setValue("Off") |
| 63 | |
| 64 | ##override setValue method |
| 65 | @dbus.service.method( |
| 66 | SensorValue.IFACE_NAME, |
| 67 | in_signature='v', out_signature='') |
| 68 | def setValue(self, value): |
| 69 | SensorValue.setValue(self, value) |
| 70 | if (value == "FW Progress, Starting OS"): |
| 71 | self.GotoSystemState("HOST_BOOTED") |
Yi Li | 57e3bf9 | 2016-09-08 17:58:00 +0800 | [diff] [blame] | 72 | self.BootProgress(value) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 73 | |
| 74 | @dbus.service.signal(CONTROL_IFACE, signature='s') |
| 75 | def GotoSystemState(self, state): |
| 76 | pass |
| 77 | |
Yi Li | 57e3bf9 | 2016-09-08 17:58:00 +0800 | [diff] [blame] | 78 | @dbus.service.signal(CONTROL_IFACE, signature='s') |
| 79 | def BootProgress(self, state): |
| 80 | pass |
| 81 | |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 82 | |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 83 | class BootCountSensor(VirtualSensor): |
| 84 | def __init__(self, bus, name): |
| 85 | VirtualSensor.__init__(self, bus, name) |
Andrew Geissler | 9070d1b | 2017-05-22 14:46:34 -0500 | [diff] [blame] | 86 | # Default boot count is 2. Add 1 onto this to allow for an |
| 87 | # SBE side switch boot attempt |
| 88 | self.setValue(3) |
| 89 | |
| 90 | ## override setValue method for debug purposes |
| 91 | @dbus.service.method( |
| 92 | SensorValue.IFACE_NAME, in_signature='v', out_signature='') |
| 93 | def setValue(self, value): |
CamVan Nguyen | 9172f3e | 2018-02-27 15:18:58 -0600 | [diff] [blame^] | 94 | print("Setting boot count to " + str(value)) |
Brad Bishop | 10ad8c3 | 2017-07-01 00:23:06 -0400 | [diff] [blame] | 95 | SensorValue.setValue(self, value) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 96 | |
| 97 | |
| 98 | class OperatingSystemStatusSensor(VirtualSensor): |
| 99 | def __init__(self, bus, name): |
| 100 | VirtualSensor.__init__(self, bus, name) |
| 101 | self.setValue("Off") |
| 102 | bus.add_signal_receiver( |
| 103 | self.SystemStateHandler, signal_name="GotoSystemState") |
| 104 | |
| 105 | def SystemStateHandler(self, state): |
| 106 | if (state == "HOST_POWERED_OFF"): |
| 107 | self.setValue("Off") |
Dhruvaraj S | dc0dc12 | 2017-03-21 02:13:29 -0500 | [diff] [blame] | 108 | |
Brad Bishop | 10ad8c3 | 2017-07-01 00:23:06 -0400 | [diff] [blame] | 109 | |
Dhruvaraj S | dc0dc12 | 2017-03-21 02:13:29 -0500 | [diff] [blame] | 110 | class PowerSupplyRedundancySensor(VirtualSensor): |
| 111 | def __init__(self, bus, name): |
| 112 | VirtualSensor.__init__(self, bus, name) |
Gunnar Mills | dab1c6d | 2018-01-26 15:28:26 -0600 | [diff] [blame] | 113 | self.bus = bus |
| 114 | self.bus_name = 'xyz.openbmc_project.Settings' |
| 115 | self.obj_path = '/xyz/openbmc_project/control/power_supply_redundancy' |
| 116 | self.iface = 'xyz.openbmc_project.Control.PowerSupplyRedundancy' |
| 117 | self.prop_iface = 'org.freedesktop.DBus.Properties' |
| 118 | self.property_name = 'PowerSupplyRedundancyEnabled' |
| 119 | super(PowerSupplyRedundancySensor, self).setValue(self.getValue()) |
| 120 | |
| 121 | # Override setValue method |
| 122 | @dbus.service.method( |
| 123 | SensorValue.IFACE_NAME, in_signature='v', out_signature='') |
| 124 | def setValue(self, value): |
| 125 | if (value == "Enabled"): |
| 126 | intf = self.getPowerSupplyInterface() |
| 127 | intf.Set(self.iface, self.property_name, True) |
| 128 | elif (value == "Disabled"): |
| 129 | intf = self.getPowerSupplyInterface() |
| 130 | intf.Set(self.iface, self.property_name, False) |
| 131 | else: |
CamVan Nguyen | 9172f3e | 2018-02-27 15:18:58 -0600 | [diff] [blame^] | 132 | print("Invalid Power Supply Redundancy value") |
Gunnar Mills | dab1c6d | 2018-01-26 15:28:26 -0600 | [diff] [blame] | 133 | return |
| 134 | super(PowerSupplyRedundancySensor, self).setValue(value) |
| 135 | |
| 136 | # Override getValue method |
| 137 | @dbus.service.method( |
| 138 | SensorValue.IFACE_NAME, in_signature='', out_signature='v') |
| 139 | def getValue(self): |
| 140 | intf = self.getPowerSupplyInterface() |
| 141 | value = intf.Get(self.iface, self.property_name) |
| 142 | if (value == 1): |
| 143 | return "Enabled" |
| 144 | elif (value == 0): |
| 145 | return "Disabled" |
| 146 | else: |
CamVan Nguyen | 9172f3e | 2018-02-27 15:18:58 -0600 | [diff] [blame^] | 147 | print("Unable to determine Power Supply Redundancy value") |
Gunnar Mills | dab1c6d | 2018-01-26 15:28:26 -0600 | [diff] [blame] | 148 | return "" |
| 149 | |
| 150 | def getPowerSupplyInterface(self): |
| 151 | obj = self.bus.get_object(self.bus_name, self.obj_path, |
| 152 | introspect=True) |
| 153 | return dbus.Interface(obj, self.prop_iface) |
| 154 | |
Brad Bishop | 10ad8c3 | 2017-07-01 00:23:06 -0400 | [diff] [blame] | 155 | |
Jayanth Othayoth | 857e4b8 | 2017-03-27 08:25:45 -0500 | [diff] [blame] | 156 | class PowerSupplyDeratingSensor(VirtualSensor): |
| 157 | def __init__(self, bus, name): |
| 158 | VirtualSensor.__init__(self, bus, name) |
Deepak Kodihalli | d67740e | 2018-02-23 08:05:19 -0600 | [diff] [blame] | 159 | super(PowerSupplyDeratingSensor, self).setValue(90) |
Jayanth Othayoth | 857e4b8 | 2017-03-27 08:25:45 -0500 | [diff] [blame] | 160 | |
| 161 | ## override setValue method |
| 162 | @dbus.service.method( |
| 163 | SensorValue.IFACE_NAME, in_signature='v', out_signature='') |
| 164 | def setValue(self, value): |
CamVan Nguyen | 9172f3e | 2018-02-27 15:18:58 -0600 | [diff] [blame^] | 165 | print("Setting Power Supply Derating is not allowed") |
Jayanth Othayoth | 857e4b8 | 2017-03-27 08:25:45 -0500 | [diff] [blame] | 166 | |
Brad Bishop | 10ad8c3 | 2017-07-01 00:23:06 -0400 | [diff] [blame] | 167 | |
Jayanth Othayoth | 7a57f95 | 2017-03-28 07:15:29 -0500 | [diff] [blame] | 168 | class TurboAllowedSensor(VirtualSensor): |
| 169 | def __init__(self, bus, name): |
| 170 | VirtualSensor.__init__(self, bus, name) |
| 171 | self.setValue(0) |
| 172 | |
| 173 | ## override setValue method |
| 174 | @dbus.service.method( |
| 175 | SensorValue.IFACE_NAME, in_signature='b', out_signature='') |
| 176 | def setValue(self, value): |
Brad Bishop | 10ad8c3 | 2017-07-01 00:23:06 -0400 | [diff] [blame] | 177 | super(TurboAllowedSensor, self).setValue(value) |