Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [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 IpmiDebug(dbus.service.Object): |
| 24 | def __init__(self,bus,name): |
| 25 | dbus.service.Object.__init__(self,bus,name) |
| 26 | |
| 27 | @dbus.service.signal(DBUS_NAME, "yyyay") |
| 28 | def ReceivedMessage(self, seq, netfn, cmd, data): |
| 29 | print "IPMI packet from host:" |
| 30 | print_packet(seq, netfn, cmd, data) |
| 31 | |
| 32 | @dbus.service.method(DBUS_NAME, "yyyay", "x") |
| 33 | def sendMessage(self, seq, netfn, cmd, data): |
| 34 | print "IPMI packet sent to host:" |
| 35 | print_packet(seq, netfn, cmd, data) |
| 36 | return 0 |
| 37 | |
| 38 | class ConsoleReader(object): |
| 39 | def __init__(self, ipmi_obj): |
| 40 | self.buffer = '' |
| 41 | self.seq = 0 |
| 42 | self.ipmi_obj = ipmi_obj |
| 43 | flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL) |
| 44 | flags |= os.O_NONBLOCK |
| 45 | fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags) |
| 46 | glib.io_add_watch(sys.stdin, glib.IO_IN, self.io_callback) |
| 47 | |
| 48 | def io_callback(self, fd, condition): |
| 49 | chunk = fd.read() |
| 50 | for char in chunk: |
| 51 | self.buffer += char |
| 52 | if char == '\n': |
| 53 | self.line(self.buffer) |
| 54 | self.buffer = '' |
| 55 | |
| 56 | return True |
| 57 | |
| 58 | def line(self, data): |
| 59 | s = data.split(' ') |
| 60 | if len(s) < 2: |
| 61 | print "Not enough bytes to form a valid IPMI packet" |
| 62 | return |
| 63 | try: |
| 64 | data = [int(c, 16) for c in s] |
| 65 | except ValueError: |
| 66 | return |
| 67 | self.seq += 1 |
| 68 | self.ipmi_obj.ReceivedMessage(self.seq, data[0], data[1], data[2:]) |
| 69 | |
| 70 | def main(): |
| 71 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Adriana Kobylak | 2cb2775 | 2015-10-19 16:23:14 -0500 | [diff] [blame] | 72 | bus = dbus.SystemBus() |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 73 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 74 | obj = IpmiDebug(bus, OBJ_NAME) |
| 75 | mainloop = gobject.MainLoop() |
| 76 | r = ConsoleReader(obj) |
| 77 | |
| 78 | print ("Enter IPMI packet as hex values. First two bytes will be used" |
| 79 | "as netfn and cmd") |
| 80 | mainloop.run() |
| 81 | |
| 82 | if __name__ == '__main__': |
| 83 | sys.exit(main()) |