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 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 17 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 18 | def header(seq, netfn, lun, cmd): |
| 19 | return ( |
| 20 | 'seq: 0x%02x\nnetfn: 0x%02x\n\nlun: 0x%02d\ncmd: 0x%02x\n') % ( |
| 21 | seq, netfn, lun, cmd) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 22 | |
| 23 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 24 | def print_request(seq, netfn, lun, cmd, data): |
| 25 | str = header(seq, netfn, lun, cmd) |
| 26 | str += 'data: [%s]' % ', '.join(['0x%02x' % x for x in data]) |
| 27 | print str |
| 28 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 29 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 30 | def print_response(seq, netfn, lun, cmd, cc, data): |
| 31 | str = header(seq, netfn, lun, cmd) |
| 32 | str += 'cc: 0x%02x\ndata: [%s]' % ( |
| 33 | cc, ', '.join(['0x%02x' % x for x in data]) |
| 34 | ) |
| 35 | print str |
| 36 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 37 | |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 38 | class IpmiDebug(dbus.service.Object): |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 39 | def __init__(self, bus, name): |
| 40 | dbus.service.Object.__init__(self, bus, name) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 41 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 42 | @dbus.service.signal(DBUS_NAME, "yyyyay") |
| 43 | def ReceivedMessage(self, seq, netfn, lun, cmd, data): |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 44 | print "IPMI packet from host:" |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 45 | print_request(seq, netfn, lun, cmd, data) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 46 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 47 | @dbus.service.method(DBUS_NAME, "yyyyyay", "x") |
| 48 | def sendMessage(self, seq, netfn, lun, cmd, ccode, data): |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 49 | print "IPMI packet sent to host:" |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 50 | print_response(seq, netfn, lun, cmd, ccode, data) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 51 | return 0 |
| 52 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 53 | @dbus.service.method(DBUS_NAME) |
| 54 | def setAttention(self): |
| 55 | print "IPMI SMS_ATN set" |
| 56 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 57 | |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 58 | class ConsoleReader(object): |
| 59 | def __init__(self, ipmi_obj): |
| 60 | self.buffer = '' |
| 61 | self.seq = 0 |
| 62 | self.ipmi_obj = ipmi_obj |
| 63 | flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL) |
| 64 | flags |= os.O_NONBLOCK |
| 65 | fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags) |
| 66 | glib.io_add_watch(sys.stdin, glib.IO_IN, self.io_callback) |
| 67 | |
| 68 | def io_callback(self, fd, condition): |
| 69 | chunk = fd.read() |
| 70 | for char in chunk: |
| 71 | self.buffer += char |
| 72 | if char == '\n': |
| 73 | self.line(self.buffer) |
| 74 | self.buffer = '' |
| 75 | |
| 76 | return True |
| 77 | |
| 78 | def line(self, data): |
| 79 | s = data.split(' ') |
| 80 | if len(s) < 2: |
| 81 | print "Not enough bytes to form a valid IPMI packet" |
| 82 | return |
| 83 | try: |
| 84 | data = [int(c, 16) for c in s] |
| 85 | except ValueError: |
| 86 | return |
| 87 | self.seq += 1 |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 88 | self.ipmi_obj.ReceivedMessage(self.seq, data[0], 0, data[1], data[2:]) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 89 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 90 | |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 91 | def main(): |
| 92 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Adriana Kobylak | 2cb2775 | 2015-10-19 16:23:14 -0500 | [diff] [blame] | 93 | bus = dbus.SystemBus() |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 94 | obj = IpmiDebug(bus, OBJ_NAME) |
| 95 | mainloop = gobject.MainLoop() |
| 96 | r = ConsoleReader(obj) |
Brad Bishop | f0f3efe | 2016-06-29 23:20:24 -0400 | [diff] [blame] | 97 | |
| 98 | obj.unmask_signals() |
Brad Bishop | 70852a3 | 2016-06-29 22:58:51 -0400 | [diff] [blame] | 99 | name = dbus.service.BusName(DBUS_NAME, bus) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 100 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 101 | print ("Enter IPMI packet as hex values. First three bytes will be used" |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 102 | "as netfn and cmd.\nlun will be zero.") |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 103 | mainloop.run() |
| 104 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 105 | |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 106 | if __name__ == '__main__': |
| 107 | sys.exit(main()) |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 108 | |
| 109 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |