blob: d31f513ed71b57d693f4cea38ab5c3b0c1497970 [file] [log] [blame]
Norman James323ed972015-12-09 09:06:37 -06001#!/usr/bin/python -u
2
3import sys
Yi Lif3be5ac2016-01-19 21:17:01 +08004import subprocess
Norman James323ed972015-12-09 09:06:37 -06005#from gi.repository import GObject
6import gobject
7import dbus
8import dbus.service
9import dbus.mainloop.glib
10import os
11import Openbmc
12
13## Abstract class, must subclass
14class SensorValue(Openbmc.DbusProperties):
15 IFACE_NAME = 'org.openbmc.SensorValue'
16 def __init__(self,bus,name):
17 #Openbmc.DbusProperties.__init__(self)
18 self.Set(SensorValue.IFACE_NAME,'units',"")
19 self.Set(SensorValue.IFACE_NAME,'error',False)
20
21 @dbus.service.method(IFACE_NAME,
22 in_signature='v', out_signature='')
23 def setValue(self,value):
24 self.Set(SensorValue.IFACE_NAME,'value',value)
25
26 @dbus.service.method(IFACE_NAME,
27 in_signature='', out_signature='v')
28 def getValue(self):
29 return self.Get(SensorValue.IFACE_NAME,'value')
30
31class SensorThresholds(Openbmc.DbusProperties):
32 IFACE_NAME = 'org.openbmc.SensorThresholds'
33 def __init__(self,bus,name):
34 self.Set(SensorThresholds.IFACE_NAME,'thresholds_enabled',False)
Norman James72567ba2016-01-13 16:57:48 -060035 self.Set(SensorThresholds.IFACE_NAME,'emergency_enabled',False)
Norman James323ed972015-12-09 09:06:37 -060036 self.Set(SensorThresholds.IFACE_NAME,'warning_upper',0)
37 self.Set(SensorThresholds.IFACE_NAME,'warning_lower',0)
38 self.Set(SensorThresholds.IFACE_NAME,'critical_upper',0)
39 self.Set(SensorThresholds.IFACE_NAME,'critical_lower',0)
40 self.Set(SensorThresholds.IFACE_NAME,'critical_lower',0)
41 self.Set(SensorThresholds.IFACE_NAME,'threshold_state',"NORMAL")
42 self.Set(SensorThresholds.IFACE_NAME,'worst_threshold_state',"NORMAL")
43
44 @dbus.service.method(IFACE_NAME,
45 in_signature='', out_signature='')
46 def resetThresholdState(self):
47 self.Set(SensorThresholds.IFACE_NAME,'worst_threshold_state',"NORMAL")
48
49 def check_thresholds(self,value):
50 iface = SensorThresholds.IFACE_NAME
51 if (self.Get(iface,'thresholds_enabled') == False):
52 return False
53 rtn = False
54 current_state = "NORMAL"
55 if (value >= self.properties[iface]['critical_upper']):
56 current_state = "CRITICAL"
Norman James323ed972015-12-09 09:06:37 -060057 rtn = True
Norman James72567ba2016-01-13 16:57:48 -060058 elif (value <= self.properties[iface]['critical_lower']):
59 current_state = "CRITICAL"
60 rtn = True
61 elif (value >= self.properties[iface]['warning_upper']):
62 current_state = "WARNING"
63 rtn = True
64 elif (value <= self.properties[iface]['warning_lower']):
65 current_state = "WARNING"
66 rtn = True
67
68 if (self.Get(iface,'threshold_state') != current_state and
69 current_state == "CRITICAL" and
70 self.Get(iface,'emergency_enabled') == True):
71 self.Emergency()
72
Norman James323ed972015-12-09 09:06:37 -060073 self.Set(iface,'threshold_state',current_state)
74 worst = self.properties[iface]['worst_threshold_state']
75 if (current_state == "CRITICAL" or
76 (current_state == "WARNING" and worst != "CRITICAL")):
77 self.Set(iface,'worst_threshold_state',current_state)
78
79 return rtn
80
Norman James72567ba2016-01-13 16:57:48 -060081 @dbus.service.signal(IFACE_NAME,signature='')
82 def Emergency(self):
83 pass
84
85
Norman James323ed972015-12-09 09:06:37 -060086class VirtualSensor(SensorValue):
87 def __init__(self,bus,name):
88 Openbmc.DbusProperties.__init__(self)
89 SensorValue.__init__(self,bus,name)
90 dbus.service.Object.__init__(self,bus,name)
91
92class HwmonSensor(SensorValue,SensorThresholds):
93 IFACE_NAME = 'org.openbmc.HwmonSensor'
94 def __init__(self,bus,name):
95 Openbmc.DbusProperties.__init__(self)
96 SensorValue.__init__(self,bus,name)
97 SensorThresholds.__init__(self,bus,name)
98 self.Set(HwmonSensor.IFACE_NAME,'scale',1)
99 self.Set(HwmonSensor.IFACE_NAME,'offset',0)
100 self.Set(HwmonSensor.IFACE_NAME,'filename','')
101 self.value_dirty = False
102
103 # need to cache value to know if changed
104 self.value = None
105 dbus.service.Object.__init__(self,bus,name)
106
107 @dbus.service.method(SensorValue.IFACE_NAME,
108 in_signature='v', out_signature='')
109 def setValue(self,value):
110 self.value_dirty = True
111 SensorValue.setValue(self,value)
112
113 ## Called by sensor process to update value from polling
114 ## if returns not None, then sensor process will update hwmon value
115 @dbus.service.method(IFACE_NAME,
116 in_signature='v', out_signature='(bv)')
117 def setByPoll(self,value):
118 scale = self.properties[HwmonSensor.IFACE_NAME]['scale']
119 offset = self.properties[HwmonSensor.IFACE_NAME]['offset']
120 if (self.value_dirty == True):
121 ## new value externally set, so just return to hwmon
122 ## process to write value
123 self.value_dirty = False
124 val = (self.properties[SensorValue.IFACE_NAME]['value']-offset) * scale
125 return [True,val]
126 else:
127 val = (value/scale) + offset
128 if (val != self.value):
129 SensorValue.setValue(self,val)
130 self.check_thresholds(val)
131 self.value = val
132
133 return [False,0]
134
135CONTROL_IFACE = 'org.openbmc.Control'
Chris Austen4c9ea5d2015-12-09 23:26:08 -0600136class PowerCap(VirtualSensor):
Yi Lif3be5ac2016-01-19 21:17:01 +0800137 def __init__(self, bus, name):
138 VirtualSensor.__init__(self, bus, name)
139 SensorValue.setValue(self, 0)
140 self.sysfs_attr = "/sys/class/hwmon/hwmon3/user_powercap"
141 ##override setValue method
142 @dbus.service.method(SensorValue.IFACE_NAME,
143 in_signature='v', out_signature='')
144 def setValue(self, value):
145 try:
146 cmd_str = "echo "+str(value)+" > "+self.sysfs_attr
147 ret = subprocess.check_output(cmd_str, shell=True)
148 except subprocess.CalledProcessError as powerexc:
149 print "Set PowerCap Error", powerexc.returncode,
150 powerexc.output
151 return
152 print "Set PowerCap: ", value
153 SensorValue.setValue(self, value)
Norman James323ed972015-12-09 09:06:37 -0600154
155class BootProgressSensor(VirtualSensor):
156 def __init__(self,bus,name):
157 VirtualSensor.__init__(self,bus,name)
158 self.setValue("Off")
159 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
160
161 def SystemStateHandler(self,state):
162 if (state == "HOST_POWERED_OFF"):
163 self.setValue("Off")
164
165
166 ##override setValue method
167 @dbus.service.method(SensorValue.IFACE_NAME,
168 in_signature='v', out_signature='')
169 def setValue(self,value):
170 SensorValue.setValue(self,value)
171 if (value == "FW Progress, Starting OS"):
172 self.GotoSystemState("HOST_BOOTED")
173
174 @dbus.service.signal(CONTROL_IFACE,signature='s')
175 def GotoSystemState(self,state):
176 pass
177
178class OccStatusSensor(VirtualSensor):
179 def __init__(self,bus,name):
180 VirtualSensor.__init__(self,bus,name)
181 self.setValue("Disabled")
182 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
183
184 def SystemStateHandler(self,state):
185 if (state == "HOST_POWERED_OFF"):
186 self.setValue("Disabled")
187
188
189 ##override setValue method
190 @dbus.service.method(SensorValue.IFACE_NAME,
191 in_signature='v', out_signature='')
192 def setValue(self,value):
193 if (value == "Enabled"):
194 print "Installing OCC device"
195 os.system("echo occ-i2c 0x50 > /sys/bus/i2c/devices/i2c-3/new_device")
196 os.system("echo occ-i2c 0x51 > /sys/bus/i2c/devices/i2c-3/new_device")
197 else:
198 print "Deleting OCC device"
199 os.system("echo 0x50 > /sys/bus/i2c/devices/i2c-3/delete_device")
200 os.system("echo 0x51 > /sys/bus/i2c/devices/i2c-3/delete_device")
201
202
203 SensorValue.setValue(self,value)
204
205 @dbus.service.signal(CONTROL_IFACE,signature='s')
206 def GotoSystemState(self,state):
207 pass
208
209class BootCountSensor(VirtualSensor):
210 def __init__(self,bus,name):
211 VirtualSensor.__init__(self,bus,name)
Chris Austen4c9ea5d2015-12-09 23:26:08 -0600212 self.setValue(2)
Norman James323ed972015-12-09 09:06:37 -0600213
214class OperatingSystemStatusSensor(VirtualSensor):
215 def __init__(self,bus,name):
216 VirtualSensor.__init__(self,bus,name)
217 self.setValue("Off")
Chris Austenc0448f02016-01-12 16:01:30 -0600218 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James323ed972015-12-09 09:06:37 -0600219
Chris Austenc0448f02016-01-12 16:01:30 -0600220 def SystemStateHandler(self,state):
221 if (state == "HOST_POWERED_OFF"):
222 self.setValue("Off")