blob: 99f88a92b25d5be15a6603a3e90cf037f8a7cc34 [file] [log] [blame]
Alistair Popple7980dc12015-10-08 09:48:20 +11001#!/usr/bin/env python
2
3import sys
4import subprocess
5import dbus
6import string
7import os
8import fcntl
9import glib
10import gobject
11import dbus.service
12import dbus.mainloop.glib
13
14DBUS_NAME = 'org.openbmc.HostIpmi'
15OBJ_NAME = '/org/openbmc/HostIpmi/1'
16
17def print_packet(seq, netfn, cmd, data):
18 print 'seq: 0x%02x\nnetfn: 0x%02x\ncmd: 0x%02x\ndata: [%s]' % (
19 seq, netfn, cmd,
20 ", ".join(['0x%02x' % x for x in data]))
21
22
23class Ipmid(dbus.service.Object):
24 def __init__(self, bus, name):
25 dbus.service.Object.__init__(self,bus,name)
26
27 def setReader(self, reader):
28 self.reader = reader
29
30 @dbus.service.signal(DBUS_NAME, "yyyay")
31 def ReceivedMessage(self, seq, netfn, cmd, data):
32 print("IPMI packet from host. Seq = 0x%x Netfn = 0x%x Cmd = 0x%x" %
33 (ord(seq), ord(netfn), ord(cmd)))
34
35 @dbus.service.method(DBUS_NAME, "", "")
36 def test(self):
37 print("TEST")
38
39 @dbus.service.method(DBUS_NAME, "yyyay", "x")
40 def sendMessage(self, seq, netfn, cmd, data):
41 print("IPMI packet sent to host. Seq = 0x%x Netfn = 0x%x Cmd = 0x%x" %
42 (int(seq), int(netfn), int(cmd)))
43
44 self.reader.write(seq, netfn, cmd, data)
45
46 return 0
47
48class BtReader(object):
49 def __init__(self, ipmi_obj):
50 self.ipmi_obj = ipmi_obj
51 flags = os.O_NONBLOCK | os.O_RDWR
52 self.bt = os.open("/dev/bt", flags)
53 glib.io_add_watch(self.bt, glib.IO_IN, self.io_callback)
54 ipmi_obj.setReader(self)
55
56 def write(self, seq, netfn, cmd, data):
57 # Untested
58 val = chr(netfn) + chr(seq) + chr(cmd)
59 val += reduce(lambda a, b: a + chr(b), data, "")
60 val = chr(len(val)) + val
61 os.write(self.bt, val)
62
63 def io_callback(self, fd, condition):
64 data = os.read(self.bt, 128)
65 self.ipmi_obj.ReceivedMessage(data[2], data[1], data[3], data[4:])
66 return True
67
68def main():
69 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
70 bus = dbus.SessionBus()
71 name = dbus.service.BusName(DBUS_NAME, bus)
72 obj = Ipmid(bus, OBJ_NAME)
73 r = BtReader(obj)
74 mainloop = gobject.MainLoop()
75 print("Started")
76 mainloop.run()
77
78if __name__ == '__main__':
79 sys.exit(main())