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 | |
| 43 | class SensorThresholds(DbusProperties): |
| 44 | IFACE_NAME = 'org.openbmc.SensorThresholds' |
| 45 | |
| 46 | def __init__(self, bus, name): |
| 47 | self.Set( |
| 48 | SensorThresholds.IFACE_NAME, |
| 49 | 'thresholds_enabled', |
| 50 | False) |
| 51 | self.Set( |
| 52 | SensorThresholds.IFACE_NAME, |
| 53 | 'emergency_enabled', |
| 54 | False) |
| 55 | self.Set( |
| 56 | SensorThresholds.IFACE_NAME, |
| 57 | 'warning_upper', |
| 58 | 0) |
| 59 | self.Set( |
| 60 | SensorThresholds.IFACE_NAME, |
| 61 | 'warning_lower', |
| 62 | 0) |
| 63 | self.Set( |
| 64 | SensorThresholds.IFACE_NAME, |
| 65 | 'critical_upper', |
| 66 | 0) |
| 67 | self.Set( |
| 68 | SensorThresholds.IFACE_NAME, |
| 69 | 'critical_lower', |
| 70 | 0) |
| 71 | self.Set( |
| 72 | SensorThresholds.IFACE_NAME, |
| 73 | 'critical_lower', |
| 74 | 0) |
| 75 | self.Set( |
| 76 | SensorThresholds.IFACE_NAME, |
| 77 | 'threshold_state', |
| 78 | "NORMAL") |
| 79 | self.Set( |
| 80 | SensorThresholds.IFACE_NAME, |
| 81 | 'worst_threshold_state', |
| 82 | "NORMAL") |
| 83 | |
| 84 | @dbus.service.method( |
| 85 | IFACE_NAME, in_signature='', out_signature='') |
| 86 | def resetThresholdState(self): |
| 87 | self.Set( |
| 88 | SensorThresholds.IFACE_NAME, |
| 89 | 'worst_threshold_state', |
| 90 | "NORMAL") |
| 91 | |
| 92 | def check_thresholds(self, value): |
| 93 | iface = SensorThresholds.IFACE_NAME |
| 94 | if (self.Get(iface, 'thresholds_enabled') is False): |
| 95 | return False |
| 96 | rtn = False |
| 97 | current_state = "NORMAL" |
| 98 | if (value >= self.properties[iface]['critical_upper']): |
| 99 | current_state = "CRITICAL" |
| 100 | rtn = True |
| 101 | elif (value <= self.properties[iface]['critical_lower']): |
| 102 | current_state = "CRITICAL" |
| 103 | rtn = True |
| 104 | elif (value >= self.properties[iface]['warning_upper']): |
| 105 | current_state = "WARNING" |
| 106 | rtn = True |
| 107 | elif (value <= self.properties[iface]['warning_lower']): |
| 108 | current_state = "WARNING" |
| 109 | rtn = True |
| 110 | |
| 111 | if self.Get(iface, 'threshold_state') != current_state and \ |
| 112 | current_state == "CRITICAL" and \ |
| 113 | self.Get(iface, 'emergency_enabled') is True: |
Yi Li | 3cb85e9 | 2016-07-01 16:47:16 +0800 | [diff] [blame] | 114 | message = type(self).__name__ + \ |
| 115 | ' SensorThresholds.check_thresholds(): trigger emergency' + \ |
| 116 | ' signal, sensor value: ' + str(value) |
| 117 | self.Emergency(message) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 118 | |
| 119 | self.Set(iface, 'threshold_state', current_state) |
| 120 | worst = self.properties[iface]['worst_threshold_state'] |
| 121 | if (current_state == "CRITICAL" or |
| 122 | (current_state == "WARNING" and worst != "CRITICAL")): |
| 123 | self.Set(iface, 'worst_threshold_state', current_state) |
| 124 | |
| 125 | return rtn |
| 126 | |
Yi Li | 3cb85e9 | 2016-07-01 16:47:16 +0800 | [diff] [blame] | 127 | @dbus.service.signal(IFACE_NAME, signature='s') |
| 128 | def Emergency(self, message): |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 129 | pass |
| 130 | |
| 131 | |
| 132 | class VirtualSensor(SensorValue): |
| 133 | def __init__(self, bus, name): |
| 134 | DbusProperties.__init__(self) |
| 135 | SensorValue.__init__(self, bus, name) |
| 136 | dbus.service.Object.__init__(self, bus, name) |
| 137 | |
| 138 | |
| 139 | class HwmonSensor(SensorValue, SensorThresholds): |
| 140 | IFACE_NAME = 'org.openbmc.HwmonSensor' |
| 141 | |
| 142 | def __init__(self, bus, name): |
| 143 | DbusProperties.__init__(self) |
| 144 | SensorValue.__init__(self, bus, name) |
| 145 | SensorThresholds.__init__(self, bus, name) |
| 146 | self.Set(HwmonSensor.IFACE_NAME, 'scale', 1) |
| 147 | self.Set(HwmonSensor.IFACE_NAME, 'offset', 0) |
| 148 | self.Set(HwmonSensor.IFACE_NAME, 'filename', '') |
| 149 | self.value_dirty = False |
| 150 | |
| 151 | # need to cache value to know if changed |
| 152 | self.value = None |
| 153 | dbus.service.Object.__init__(self, bus, name) |
| 154 | |
| 155 | @dbus.service.method( |
| 156 | SensorValue.IFACE_NAME, in_signature='v', out_signature='') |
| 157 | def setValue(self, value): |
| 158 | self.value_dirty = True |
| 159 | SensorValue.setValue(self, value) |
| 160 | |
| 161 | ## Called by sensor process to update value from polling |
| 162 | ## if returns not None, then sensor process will update hwmon value |
| 163 | @dbus.service.method( |
| 164 | IFACE_NAME, in_signature='v', out_signature='(bv)') |
| 165 | def setByPoll(self, value): |
| 166 | scale = self.properties[HwmonSensor.IFACE_NAME]['scale'] |
| 167 | offset = self.properties[HwmonSensor.IFACE_NAME]['offset'] |
Edward A. James | ecf8bd8 | 2016-08-15 16:22:25 -0500 | [diff] [blame] | 168 | try: |
| 169 | adjust = self.properties[HwmonSensor.IFACE_NAME]['adjust'] |
| 170 | if adjust == 0: |
| 171 | adjust = 1 |
| 172 | except: |
| 173 | adjust = 1 |
| 174 | |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 175 | if self.value_dirty: |
| 176 | ## new value externally set, so just return to hwmon |
| 177 | ## process to write value |
| 178 | self.value_dirty = False |
Edward A. James | ecf8bd8 | 2016-08-15 16:22:25 -0500 | [diff] [blame] | 179 | val = int(( |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 180 | self.properties[SensorValue.IFACE_NAME]['value'] |
Edward A. James | ecf8bd8 | 2016-08-15 16:22:25 -0500 | [diff] [blame] | 181 | - offset) * adjust) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 182 | return [True, val] |
| 183 | else: |
Edward A. James | ecf8bd8 | 2016-08-15 16:22:25 -0500 | [diff] [blame] | 184 | # Keep the val as integer. adjust may be floating point |
| 185 | val = int(value/adjust + offset) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 186 | if (val != self.value): |
| 187 | SensorValue.setValue(self, val) |
Edward A. James | ecf8bd8 | 2016-08-15 16:22:25 -0500 | [diff] [blame] | 188 | # scale the value to check threshold |
| 189 | self.check_thresholds(val * (10 ** scale)) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 190 | self.value = val |
| 191 | |
| 192 | return [False, 0] |
| 193 | |
| 194 | |
| 195 | CONTROL_IFACE = 'org.openbmc.Control' |
| 196 | |
| 197 | |
| 198 | class PowerCap(VirtualSensor): |
| 199 | def __init__(self, bus, name): |
| 200 | VirtualSensor.__init__(self, bus, name) |
| 201 | SensorValue.setValue(self, 0) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 202 | |
| 203 | ##override setValue method |
| 204 | @dbus.service.method( |
| 205 | SensorValue.IFACE_NAME, in_signature='v', out_signature='') |
| 206 | def setValue(self, value): |
Jayanth Othayoth | 0a3eeb8 | 2017-04-18 06:13:34 -0500 | [diff] [blame^] | 207 | raise Exception("ERROR: Setting Power Cap is not allowed") |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 208 | |
| 209 | class BootProgressSensor(VirtualSensor): |
| 210 | def __init__(self, bus, name): |
| 211 | VirtualSensor.__init__(self, bus, name) |
| 212 | self.setValue("Off") |
| 213 | bus.add_signal_receiver( |
| 214 | self.SystemStateHandler, signal_name="GotoSystemState") |
| 215 | |
| 216 | def SystemStateHandler(self, state): |
| 217 | if (state == "HOST_POWERED_OFF"): |
| 218 | self.setValue("Off") |
| 219 | |
| 220 | ##override setValue method |
| 221 | @dbus.service.method( |
| 222 | SensorValue.IFACE_NAME, |
| 223 | in_signature='v', out_signature='') |
| 224 | def setValue(self, value): |
| 225 | SensorValue.setValue(self, value) |
| 226 | if (value == "FW Progress, Starting OS"): |
| 227 | self.GotoSystemState("HOST_BOOTED") |
Yi Li | 57e3bf9 | 2016-09-08 17:58:00 +0800 | [diff] [blame] | 228 | self.BootProgress(value) |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 229 | |
| 230 | @dbus.service.signal(CONTROL_IFACE, signature='s') |
| 231 | def GotoSystemState(self, state): |
| 232 | pass |
| 233 | |
Yi Li | 57e3bf9 | 2016-09-08 17:58:00 +0800 | [diff] [blame] | 234 | @dbus.service.signal(CONTROL_IFACE, signature='s') |
| 235 | def BootProgress(self, state): |
| 236 | pass |
| 237 | |
Brad Bishop | 179b39b | 2016-05-12 16:45:57 -0400 | [diff] [blame] | 238 | |
| 239 | class OccStatusSensor(VirtualSensor): |
| 240 | def __init__(self, bus, name, sysfs=None): |
| 241 | ## default path. can be override |
| 242 | if sysfs is None: |
| 243 | self.sysfs_attr = "/sys/class/i2c-adapter/i2c-3/3-0050/online" |
| 244 | else: |
| 245 | self.sysfs_attr = sysfs |
| 246 | VirtualSensor.__init__(self, bus, name) |
| 247 | self.setValue("Disabled") |
| 248 | bus.add_signal_receiver( |
| 249 | self.SystemStateHandler, signal_name="GotoSystemState") |
| 250 | |
| 251 | def SystemStateHandler(self, state): |
| 252 | if (state == "HOST_POWERED_OFF"): |
| 253 | self.setValue("Disabled") |
| 254 | |
| 255 | ##override setValue method |
| 256 | @dbus.service.method( |
| 257 | SensorValue.IFACE_NAME, in_signature='v', out_signature='') |
| 258 | def setValue(self, value): |
| 259 | if (value == "Enabled"): |
| 260 | print "Installing OCC device" |
| 261 | os.system("echo 1 > " + self.sysfs_attr) |
| 262 | else: |
| 263 | print "Deleting OCC device" |
| 264 | os.system("echo 0 > " + self.sysfs_attr) |
| 265 | SensorValue.setValue(self, value) |
| 266 | |
| 267 | @dbus.service.signal(CONTROL_IFACE, signature='s') |
| 268 | def GotoSystemState(self, state): |
| 269 | pass |
| 270 | |
| 271 | |
| 272 | class BootCountSensor(VirtualSensor): |
| 273 | def __init__(self, bus, name): |
| 274 | VirtualSensor.__init__(self, bus, name) |
| 275 | self.setValue(2) |
| 276 | |
| 277 | |
| 278 | class OperatingSystemStatusSensor(VirtualSensor): |
| 279 | def __init__(self, bus, name): |
| 280 | VirtualSensor.__init__(self, bus, name) |
| 281 | self.setValue("Off") |
| 282 | bus.add_signal_receiver( |
| 283 | self.SystemStateHandler, signal_name="GotoSystemState") |
| 284 | |
| 285 | def SystemStateHandler(self, state): |
| 286 | if (state == "HOST_POWERED_OFF"): |
| 287 | self.setValue("Off") |
Dhruvaraj S | dc0dc12 | 2017-03-21 02:13:29 -0500 | [diff] [blame] | 288 | |
| 289 | class PowerSupplyRedundancySensor(VirtualSensor): |
| 290 | def __init__(self, bus, name): |
| 291 | VirtualSensor.__init__(self, bus, name) |
Dhruvaraj Subhashchandran | 93daccf | 2017-03-28 04:02:53 -0500 | [diff] [blame] | 292 | super(PowerSupplyRedundancySensor,self).setValue("Disabled") |
Dhruvaraj S | dc0dc12 | 2017-03-21 02:13:29 -0500 | [diff] [blame] | 293 | |
| 294 | ## override setValue method |
| 295 | @dbus.service.method( |
| 296 | SensorValue.IFACE_NAME, in_signature='v', out_signature='') |
| 297 | def setValue(self, value): |
| 298 | print "Setting Power Supply Redundancy is not allowed" |
Jayanth Othayoth | 7a57f95 | 2017-03-28 07:15:29 -0500 | [diff] [blame] | 299 | |
Jayanth Othayoth | 857e4b8 | 2017-03-27 08:25:45 -0500 | [diff] [blame] | 300 | class PowerSupplyDeratingSensor(VirtualSensor): |
| 301 | def __init__(self, bus, name): |
| 302 | VirtualSensor.__init__(self, bus, name) |
| 303 | super(PowerSupplyDeratingSensor,self).setValue(10) |
| 304 | |
| 305 | ## override setValue method |
| 306 | @dbus.service.method( |
| 307 | SensorValue.IFACE_NAME, in_signature='v', out_signature='') |
| 308 | def setValue(self, value): |
| 309 | print "Setting Power Supply Derating is not allowed" |
| 310 | |
Jayanth Othayoth | 7a57f95 | 2017-03-28 07:15:29 -0500 | [diff] [blame] | 311 | class TurboAllowedSensor(VirtualSensor): |
| 312 | def __init__(self, bus, name): |
| 313 | VirtualSensor.__init__(self, bus, name) |
| 314 | self.setValue(0) |
| 315 | |
| 316 | ## override setValue method |
| 317 | @dbus.service.method( |
| 318 | SensorValue.IFACE_NAME, in_signature='b', out_signature='') |
| 319 | def setValue(self, value): |
| 320 | super(TurboAllowedSensor,self).setValue(value) |