Alistair Popple | 7980dc1 | 2015-10-08 09:48:20 +1100 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import subprocess |
| 5 | import dbus |
| 6 | import string |
| 7 | import os |
| 8 | import fcntl |
| 9 | import glib |
| 10 | import gobject |
| 11 | import dbus.service |
| 12 | import dbus.mainloop.glib |
| 13 | |
| 14 | DBUS_NAME = 'org.openbmc.HostIpmi' |
| 15 | OBJ_NAME = '/org/openbmc/HostIpmi/1' |
| 16 | |
| 17 | def 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 | |
| 23 | class 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 | |
| 48 | class 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 | |
| 68 | def 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 | |
| 78 | if __name__ == '__main__': |
| 79 | sys.exit(main()) |