blob: b87fec847fb46c8475cd7ca03a18733da3eac458 [file] [log] [blame]
Norman Jamesce46e3e2015-08-30 22:25:55 -05001import dbus
2
3BUS_PREFIX = 'org.openbmc'
Norman James362a80f2015-09-14 14:04:39 -05004OBJ_PREFIX = "/org/openbmc"
Norman Jamesce46e3e2015-08-30 22:25:55 -05005GPIO_DEV = '/sys/class/gpio'
Norman James5e792e32015-10-07 17:36:17 -05006BUS = "system"
Norman Jamesce46e3e2015-08-30 22:25:55 -05007
Norman James969227c2015-10-08 15:10:47 -05008def getSystemName():
9 #use filename as system name, strip off path and ext
10 parts = __file__.replace('.pyc','').replace('.py','').split('/')
11 return parts[len(parts)-1]
12
Norman James5e792e32015-10-07 17:36:17 -050013def getDBus():
14 bus = None
15 if (BUS == "session"):
16 bus = dbus.SessionBus()
17 else:
18 bus = dbus.SystemBus()
19 return bus
20
Norman Jamesa3e47c42015-10-18 14:43:10 -050021class DbusProperties(dbus.service.Object):
22 def __init__(self):
23 dbus.service.Object.__init__(self)
24 self.properties = {}
Norman James323ed972015-12-09 09:06:37 -060025 self.object_path = ""
Norman James362a80f2015-09-14 14:04:39 -050026
Norman Jamesa3e47c42015-10-18 14:43:10 -050027 @dbus.service.method(dbus.PROPERTIES_IFACE,
28 in_signature='ss', out_signature='v')
29 def Get(self, interface_name, property_name):
30 d = self.GetAll(interface_name)
31 try:
32 v = d[property_name]
33 return v
34 except:
35 raise dbus.exceptions.DBusException(
36 "org.freedesktop.UnknownPropery: "+property_name)
37
38 @dbus.service.method(dbus.PROPERTIES_IFACE,
39 in_signature='s', out_signature='a{sv}')
40 def GetAll(self, interface_name):
41 try:
42 d = self.properties[interface_name]
43 return d
44 except:
45 raise dbus.exceptions.DBusException(
46 "org.freedesktop.UnknownInterface: "+interface_name)
47
48 @dbus.service.method(dbus.PROPERTIES_IFACE,
49 in_signature='ssv')
50 def Set(self, interface_name, property_name, new_value):
51 if (self.properties.has_key(interface_name) == False):
52 self.properties[interface_name] = {}
53 try:
54 old_value = self.properties[interface_name][property_name]
55 if (old_value != new_value):
56 self.properties[interface_name][property_name] = new_value
57 self.PropertiesChanged(interface_name,{ property_name: new_value }, [])
58
59 except:
60 self.properties[interface_name][property_name] = new_value
Norman Jamesf81d4982015-10-26 17:12:14 -050061 self.PropertiesChanged(interface_name,{ property_name: new_value }, [])
Norman Jamesa3e47c42015-10-18 14:43:10 -050062
Norman Jamesf81d4982015-10-26 17:12:14 -050063 @dbus.service.method("org.openbmc.Object.Properties",
Norman James2656f332015-10-26 06:42:41 -050064 in_signature='sa{sv}')
Norman Jamesf81d4982015-10-26 17:12:14 -050065 def SetMultiple(self, interface_name, prop_dict):
Norman James2656f332015-10-26 06:42:41 -050066 if (self.properties.has_key(interface_name) == False):
67 self.properties[interface_name] = {}
68
69 value_changed = False
70 for property_name in prop_dict:
71 new_value = prop_dict[property_name]
72 try:
73 old_value = self.properties[interface_name][property_name]
74 if (old_value != new_value):
75 self.properties[interface_name][property_name] = new_value
76 value_changed = True
77
78 except:
79 self.properties[interface_name][property_name] = new_value
80 value_changed = True
81 if (value_changed == True):
82 self.PropertiesChanged(interface_name, prop_dict, [])
83
Norman Jamesa3e47c42015-10-18 14:43:10 -050084 @dbus.service.signal(dbus.PROPERTIES_IFACE,
85 signature='sa{sv}as')
86 def PropertiesChanged(self, interface_name, changed_properties,
87 invalidated_properties):
88 pass
89
Norman James323ed972015-12-09 09:06:37 -060090class DbusObjectManager(dbus.service.Object):
91 def __init__(self):
92 dbus.service.Object.__init__(self)
93 self.objects = {}
94
95 def add(self,object_path,obj):
96 self.objects[object_path] = obj
97 self.InterfacesAdded(object_path,obj.properties)
98
99 def remove(self,object_path):
100 obj = self.objects.pop(object_path,None)
101 obj.remove_from_connection()
102 self.InterfacesRemoved(object_path,obj.properties.keys())
103
104 def get(self,object_path):
105 return self.objects[object_path]
106
107 @dbus.service.method("org.freedesktop.DBus.ObjectManager",
108 in_signature='', out_signature='a{oa{sa{sv}}}')
109 def GetManagedObjects(self):
110 data = {}
111 for objpath in self.objects.keys():
112 data[objpath] = self.objects[objpath].properties
113 return data
114
115 @dbus.service.signal("org.freedesktop.DBus.ObjectManager",
116 signature='oa{sa{sv}}')
117 def InterfacesAdded(self,object_path,properties):
118 self.ObjectAdded(object_path,"")
119
120 @dbus.service.signal("org.freedesktop.DBus.ObjectManager",
121 signature='oas')
122 def InterfacesRemoved(self,object_path,interfaces):
123 pass
124
125 ## Legacy support, need to eventually refactor out
Norman James633f04d2015-10-31 17:34:34 -0500126 @dbus.service.signal("org.openbmc.Object.ObjectMapper",
127 signature='ss')
128 def ObjectAdded(self,object_path,interface_name):
129 pass
Norman Jamesa3e47c42015-10-18 14:43:10 -0500130
Norman James323ed972015-12-09 09:06:37 -0600131 ## flattens interfaces
132 @dbus.service.method('org.openbmc.Object.Enumerate',
133 in_signature='', out_signature='a{sa{sv}}')
134 def enumerate(self):
135 data = {}
136 for objpath in self.objects.keys():
137 props = self.objects[objpath].properties
138 data[objpath] = { }
139 for iface in props.keys():
140 data[objpath].update(props[iface])
141
142 return data
143
144
Norman Jamesa3e47c42015-10-18 14:43:10 -0500145
Norman James90baede2015-09-02 20:32:49 -0500146