Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | |
| 3 | import sys |
| 4 | #from gi.repository import GObject |
| 5 | import gobject |
| 6 | import dbus |
| 7 | import dbus.service |
| 8 | import dbus.mainloop.glib |
| 9 | import os |
| 10 | import Openbmc |
| 11 | |
| 12 | ## Abstract class, must subclass |
| 13 | class SensorValue(Openbmc.DbusProperties): |
| 14 | IFACE_NAME = 'org.openbmc.SensorValue' |
| 15 | def __init__(self,bus,name): |
| 16 | #Openbmc.DbusProperties.__init__(self) |
| 17 | self.Set(SensorValue.IFACE_NAME,'units',"") |
| 18 | self.Set(SensorValue.IFACE_NAME,'error',False) |
| 19 | |
| 20 | @dbus.service.method(IFACE_NAME, |
| 21 | in_signature='v', out_signature='') |
| 22 | def setValue(self,value): |
| 23 | self.Set(SensorValue.IFACE_NAME,'value',value) |
| 24 | |
| 25 | @dbus.service.method(IFACE_NAME, |
| 26 | in_signature='', out_signature='v') |
| 27 | def getValue(self): |
| 28 | return self.Get(SensorValue.IFACE_NAME,'value') |
| 29 | |
| 30 | class SensorThresholds(Openbmc.DbusProperties): |
| 31 | IFACE_NAME = 'org.openbmc.SensorThresholds' |
| 32 | def __init__(self,bus,name): |
| 33 | self.Set(SensorThresholds.IFACE_NAME,'thresholds_enabled',False) |
| 34 | self.Set(SensorThresholds.IFACE_NAME,'warning_upper',0) |
| 35 | self.Set(SensorThresholds.IFACE_NAME,'warning_lower',0) |
| 36 | self.Set(SensorThresholds.IFACE_NAME,'critical_upper',0) |
| 37 | self.Set(SensorThresholds.IFACE_NAME,'critical_lower',0) |
| 38 | self.Set(SensorThresholds.IFACE_NAME,'critical_lower',0) |
| 39 | self.Set(SensorThresholds.IFACE_NAME,'threshold_state',"NORMAL") |
| 40 | self.Set(SensorThresholds.IFACE_NAME,'worst_threshold_state',"NORMAL") |
| 41 | |
| 42 | @dbus.service.method(IFACE_NAME, |
| 43 | in_signature='', out_signature='') |
| 44 | def resetThresholdState(self): |
| 45 | self.Set(SensorThresholds.IFACE_NAME,'worst_threshold_state',"NORMAL") |
| 46 | |
| 47 | def check_thresholds(self,value): |
| 48 | iface = SensorThresholds.IFACE_NAME |
| 49 | if (self.Get(iface,'thresholds_enabled') == False): |
| 50 | return False |
| 51 | rtn = False |
| 52 | current_state = "NORMAL" |
| 53 | if (value >= self.properties[iface]['critical_upper']): |
| 54 | current_state = "CRITICAL" |
| 55 | rtn = True |
| 56 | elif (value <= self.properties[iface]['critical_lower']): |
| 57 | current_state = "CRITICAL" |
| 58 | rtn = True |
| 59 | elif (value >= self.properties[iface]['warning_upper']): |
| 60 | current_state = "WARNING" |
| 61 | rtn = True |
| 62 | elif (value <= self.properties[iface]['warning_lower']): |
| 63 | current_state = "WARNING" |
| 64 | rtn = True |
| 65 | self.Set(iface,'threshold_state',current_state) |
| 66 | worst = self.properties[iface]['worst_threshold_state'] |
| 67 | if (current_state == "CRITICAL" or |
| 68 | (current_state == "WARNING" and worst != "CRITICAL")): |
| 69 | self.Set(iface,'worst_threshold_state',current_state) |
| 70 | |
| 71 | return rtn |
| 72 | |
| 73 | class VirtualSensor(SensorValue): |
| 74 | def __init__(self,bus,name): |
| 75 | Openbmc.DbusProperties.__init__(self) |
| 76 | SensorValue.__init__(self,bus,name) |
| 77 | dbus.service.Object.__init__(self,bus,name) |
| 78 | |
| 79 | class HwmonSensor(SensorValue,SensorThresholds): |
| 80 | IFACE_NAME = 'org.openbmc.HwmonSensor' |
| 81 | def __init__(self,bus,name): |
| 82 | Openbmc.DbusProperties.__init__(self) |
| 83 | SensorValue.__init__(self,bus,name) |
| 84 | SensorThresholds.__init__(self,bus,name) |
| 85 | self.Set(HwmonSensor.IFACE_NAME,'scale',1) |
| 86 | self.Set(HwmonSensor.IFACE_NAME,'offset',0) |
| 87 | self.Set(HwmonSensor.IFACE_NAME,'filename','') |
| 88 | self.value_dirty = False |
| 89 | |
| 90 | # need to cache value to know if changed |
| 91 | self.value = None |
| 92 | dbus.service.Object.__init__(self,bus,name) |
| 93 | |
| 94 | @dbus.service.method(SensorValue.IFACE_NAME, |
| 95 | in_signature='v', out_signature='') |
| 96 | def setValue(self,value): |
| 97 | self.value_dirty = True |
| 98 | SensorValue.setValue(self,value) |
| 99 | |
| 100 | ## Called by sensor process to update value from polling |
| 101 | ## if returns not None, then sensor process will update hwmon value |
| 102 | @dbus.service.method(IFACE_NAME, |
| 103 | in_signature='v', out_signature='(bv)') |
| 104 | def setByPoll(self,value): |
| 105 | scale = self.properties[HwmonSensor.IFACE_NAME]['scale'] |
| 106 | offset = self.properties[HwmonSensor.IFACE_NAME]['offset'] |
| 107 | if (self.value_dirty == True): |
| 108 | ## new value externally set, so just return to hwmon |
| 109 | ## process to write value |
| 110 | self.value_dirty = False |
| 111 | val = (self.properties[SensorValue.IFACE_NAME]['value']-offset) * scale |
| 112 | return [True,val] |
| 113 | else: |
| 114 | val = (value/scale) + offset |
| 115 | if (val != self.value): |
| 116 | SensorValue.setValue(self,val) |
| 117 | self.check_thresholds(val) |
| 118 | self.value = val |
| 119 | |
| 120 | return [False,0] |
| 121 | |
| 122 | CONTROL_IFACE = 'org.openbmc.Control' |
Chris Austen | 4c9ea5d | 2015-12-09 23:26:08 -0600 | [diff] [blame] | 123 | class PowerCap(VirtualSensor): |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 124 | def __init__(self,bus,name): |
| 125 | VirtualSensor.__init__(self,bus,name) |
Chris Austen | 4c9ea5d | 2015-12-09 23:26:08 -0600 | [diff] [blame] | 126 | self.setValue(100) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 127 | |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 128 | |
| 129 | class BootProgressSensor(VirtualSensor): |
| 130 | def __init__(self,bus,name): |
| 131 | VirtualSensor.__init__(self,bus,name) |
| 132 | self.setValue("Off") |
| 133 | bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState") |
| 134 | |
| 135 | def SystemStateHandler(self,state): |
| 136 | if (state == "HOST_POWERED_OFF"): |
| 137 | self.setValue("Off") |
| 138 | |
| 139 | |
| 140 | ##override setValue method |
| 141 | @dbus.service.method(SensorValue.IFACE_NAME, |
| 142 | in_signature='v', out_signature='') |
| 143 | def setValue(self,value): |
| 144 | SensorValue.setValue(self,value) |
| 145 | if (value == "FW Progress, Starting OS"): |
| 146 | self.GotoSystemState("HOST_BOOTED") |
| 147 | |
| 148 | @dbus.service.signal(CONTROL_IFACE,signature='s') |
| 149 | def GotoSystemState(self,state): |
| 150 | pass |
| 151 | |
| 152 | class OccStatusSensor(VirtualSensor): |
| 153 | def __init__(self,bus,name): |
| 154 | VirtualSensor.__init__(self,bus,name) |
| 155 | self.setValue("Disabled") |
| 156 | bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState") |
| 157 | |
| 158 | def SystemStateHandler(self,state): |
| 159 | if (state == "HOST_POWERED_OFF"): |
| 160 | self.setValue("Disabled") |
| 161 | |
| 162 | |
| 163 | ##override setValue method |
| 164 | @dbus.service.method(SensorValue.IFACE_NAME, |
| 165 | in_signature='v', out_signature='') |
| 166 | def setValue(self,value): |
| 167 | if (value == "Enabled"): |
| 168 | print "Installing OCC device" |
| 169 | os.system("echo occ-i2c 0x50 > /sys/bus/i2c/devices/i2c-3/new_device") |
| 170 | os.system("echo occ-i2c 0x51 > /sys/bus/i2c/devices/i2c-3/new_device") |
| 171 | else: |
| 172 | print "Deleting OCC device" |
| 173 | os.system("echo 0x50 > /sys/bus/i2c/devices/i2c-3/delete_device") |
| 174 | os.system("echo 0x51 > /sys/bus/i2c/devices/i2c-3/delete_device") |
| 175 | |
| 176 | |
| 177 | SensorValue.setValue(self,value) |
| 178 | |
| 179 | @dbus.service.signal(CONTROL_IFACE,signature='s') |
| 180 | def GotoSystemState(self,state): |
| 181 | pass |
| 182 | |
| 183 | class BootCountSensor(VirtualSensor): |
| 184 | def __init__(self,bus,name): |
| 185 | VirtualSensor.__init__(self,bus,name) |
Chris Austen | 4c9ea5d | 2015-12-09 23:26:08 -0600 | [diff] [blame] | 186 | self.setValue(2) |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 187 | |
| 188 | class OperatingSystemStatusSensor(VirtualSensor): |
| 189 | def __init__(self,bus,name): |
| 190 | VirtualSensor.__init__(self,bus,name) |
| 191 | self.setValue("Off") |
Chris Austen | c0448f0 | 2016-01-12 16:01:30 -0600 | [diff] [blame^] | 192 | bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState") |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 193 | |
Chris Austen | c0448f0 | 2016-01-12 16:01:30 -0600 | [diff] [blame^] | 194 | def SystemStateHandler(self,state): |
| 195 | if (state == "HOST_POWERED_OFF"): |
| 196 | self.setValue("Off") |