Norman James | b51b9a5 | 2015-10-18 14:47:35 -0500 | [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 | new_str = "" |
| 22 | for x in data: |
| 23 | new_str += "0x%s " % (x.encode('hex')) |
| 24 | print new_str |
| 25 | |
| 26 | |
| 27 | class Ipmid(dbus.service.Object): |
| 28 | def __init__(self, bus, name): |
| 29 | dbus.service.Object.__init__(self,bus,name) |
| 30 | |
| 31 | def setReader(self, reader): |
| 32 | self.reader = reader |
| 33 | |
| 34 | @dbus.service.signal(DBUS_NAME, "yyyay") |
| 35 | def ReceivedMessage(self, seq, netfn, cmd, data): |
Norman James | d524688 | 2015-10-26 06:42:15 -0500 | [diff] [blame] | 36 | #print("IPMI packet from host. Seq = 0x%x Netfn = 0x%x Cmd = 0x%x" % |
| 37 | # (ord(seq), ord(netfn), ord(cmd))) |
Norman James | b51b9a5 | 2015-10-18 14:47:35 -0500 | [diff] [blame] | 38 | print_packet(seq, netfn, cmd, data) |
| 39 | |
| 40 | @dbus.service.method(DBUS_NAME, "", "") |
| 41 | def test(self): |
| 42 | print("TEST") |
| 43 | |
| 44 | @dbus.service.method(DBUS_NAME, "yyyay", "x") |
| 45 | def sendMessage(self, seq, netfn, cmd, data): |
Norman James | d524688 | 2015-10-26 06:42:15 -0500 | [diff] [blame] | 46 | #print("IPMI packet sent to host. Seq = 0x%x Netfn = 0x%x Cmd = 0x%x" % |
| 47 | # (int(seq), int(netfn), int(cmd))) |
Norman James | b51b9a5 | 2015-10-18 14:47:35 -0500 | [diff] [blame] | 48 | #print_packet(seq, netfn, cmd, data) |
| 49 | |
| 50 | self.reader.write(seq, netfn, cmd, data) |
| 51 | |
| 52 | return 0 |
| 53 | |
| 54 | class BtReader(object): |
| 55 | def __init__(self, ipmi_obj): |
| 56 | self.ipmi_obj = ipmi_obj |
| 57 | flags = os.O_NONBLOCK | os.O_RDWR |
Norman James | d524688 | 2015-10-26 06:42:15 -0500 | [diff] [blame] | 58 | self.bt = os.open("/dev/bt-host", flags) |
Norman James | b51b9a5 | 2015-10-18 14:47:35 -0500 | [diff] [blame] | 59 | glib.io_add_watch(self.bt, glib.IO_IN, self.io_callback) |
| 60 | ipmi_obj.setReader(self) |
| 61 | |
| 62 | def write(self, seq, netfn, cmd, data): |
| 63 | # Untested |
| 64 | val = chr(netfn) + chr(seq) + chr(cmd) |
| 65 | val += reduce(lambda a, b: a + chr(b), data, "") |
| 66 | val = chr(len(val)) + val |
| 67 | os.write(self.bt, val) |
| 68 | |
| 69 | def io_callback(self, fd, condition): |
| 70 | data = os.read(self.bt, 128) |
| 71 | if (len(data) > 2): |
| 72 | self.ipmi_obj.ReceivedMessage(data[2], data[1], data[3], data[4:]) |
| 73 | return True |
| 74 | |
| 75 | def main(): |
| 76 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 77 | bus = dbus.SystemBus() |
| 78 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 79 | obj = Ipmid(bus, OBJ_NAME) |
| 80 | r = BtReader(obj) |
| 81 | mainloop = gobject.MainLoop() |
| 82 | print("Started") |
| 83 | mainloop.run() |
| 84 | |
| 85 | if __name__ == '__main__': |
| 86 | sys.exit(main()) |