blob: 537bb19a800709785c0928db9cc5de923ff5aed7 [file] [log] [blame]
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +08001#!/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
Patrick Williams75fe8cc2022-07-22 16:12:12 -050014DBUS_NAME = "org.openbmc.HostIpmi"
15OBJ_NAME = "/org/openbmc/HostIpmi/1"
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080016
Adriana Kobylak24341f92018-01-26 15:07:23 -060017
Jeremy Kerre34da632015-10-27 11:52:08 +080018def header(seq, netfn, lun, cmd):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050019 return ("seq: 0x%02x\nnetfn: 0x%02x\n\nlun: 0x%02d\ncmd: 0x%02x\n") % (
20 seq,
21 netfn,
22 lun,
23 cmd,
24 )
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080025
26
Jeremy Kerre34da632015-10-27 11:52:08 +080027def print_request(seq, netfn, lun, cmd, data):
28 str = header(seq, netfn, lun, cmd)
Patrick Williams75fe8cc2022-07-22 16:12:12 -050029 str += "data: [%s]" % ", ".join(["0x%02x" % x for x in data])
30 print(str)
Jeremy Kerre34da632015-10-27 11:52:08 +080031
Adriana Kobylak24341f92018-01-26 15:07:23 -060032
Jeremy Kerre34da632015-10-27 11:52:08 +080033def print_response(seq, netfn, lun, cmd, cc, data):
34 str = header(seq, netfn, lun, cmd)
Patrick Williams75fe8cc2022-07-22 16:12:12 -050035 str += "cc: 0x%02x\ndata: [%s]" % (
36 cc,
37 ", ".join(["0x%02x" % x for x in data]),
38 )
39 print(str)
Jeremy Kerre34da632015-10-27 11:52:08 +080040
Adriana Kobylak24341f92018-01-26 15:07:23 -060041
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080042class IpmiDebug(dbus.service.Object):
Adriana Kobylak24341f92018-01-26 15:07:23 -060043 def __init__(self, bus, name):
44 dbus.service.Object.__init__(self, bus, name)
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080045
Jeremy Kerre34da632015-10-27 11:52:08 +080046 @dbus.service.signal(DBUS_NAME, "yyyyay")
47 def ReceivedMessage(self, seq, netfn, lun, cmd, data):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050048 print("IPMI packet from host:")
Jeremy Kerre34da632015-10-27 11:52:08 +080049 print_request(seq, netfn, lun, cmd, data)
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080050
Jeremy Kerre34da632015-10-27 11:52:08 +080051 @dbus.service.method(DBUS_NAME, "yyyyyay", "x")
52 def sendMessage(self, seq, netfn, lun, cmd, ccode, data):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050053 print("IPMI packet sent to host:")
Jeremy Kerre34da632015-10-27 11:52:08 +080054 print_response(seq, netfn, lun, cmd, ccode, data)
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080055 return 0
56
Jeremy Kerre34da632015-10-27 11:52:08 +080057 @dbus.service.method(DBUS_NAME)
58 def setAttention(self):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050059 print("IPMI SMS_ATN set")
Jeremy Kerre34da632015-10-27 11:52:08 +080060
Adriana Kobylak24341f92018-01-26 15:07:23 -060061
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080062class ConsoleReader(object):
63 def __init__(self, ipmi_obj):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050064 self.buffer = ""
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080065 self.seq = 0
66 self.ipmi_obj = ipmi_obj
67 flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
68 flags |= os.O_NONBLOCK
69 fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags)
70 glib.io_add_watch(sys.stdin, glib.IO_IN, self.io_callback)
71
72 def io_callback(self, fd, condition):
73 chunk = fd.read()
74 for char in chunk:
75 self.buffer += char
Patrick Williams75fe8cc2022-07-22 16:12:12 -050076 if char == "\n":
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080077 self.line(self.buffer)
Patrick Williams75fe8cc2022-07-22 16:12:12 -050078 self.buffer = ""
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080079
80 return True
81
82 def line(self, data):
Patrick Williams75fe8cc2022-07-22 16:12:12 -050083 s = data.split(" ")
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080084 if len(s) < 2:
Patrick Williams75fe8cc2022-07-22 16:12:12 -050085 print("Not enough bytes to form a valid IPMI packet")
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080086 return
87 try:
88 data = [int(c, 16) for c in s]
89 except ValueError:
90 return
91 self.seq += 1
Jeremy Kerre34da632015-10-27 11:52:08 +080092 self.ipmi_obj.ReceivedMessage(self.seq, data[0], 0, data[1], data[2:])
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080093
Adriana Kobylak24341f92018-01-26 15:07:23 -060094
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080095def main():
96 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Adriana Kobylak2cb27752015-10-19 16:23:14 -050097 bus = dbus.SystemBus()
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080098 obj = IpmiDebug(bus, OBJ_NAME)
99 mainloop = gobject.MainLoop()
100 r = ConsoleReader(obj)
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400101
102 obj.unmask_signals()
Brad Bishop70852a32016-06-29 22:58:51 -0400103 name = dbus.service.BusName(DBUS_NAME, bus)
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +0800104
Patrick Williams75fe8cc2022-07-22 16:12:12 -0500105 print(
106 "Enter IPMI packet as hex values. First three bytes will be used"
107 "as netfn and cmd.\nlun will be zero."
108 )
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +0800109 mainloop.run()
110
Adriana Kobylak24341f92018-01-26 15:07:23 -0600111
Patrick Williams75fe8cc2022-07-22 16:12:12 -0500112if __name__ == "__main__":
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +0800113 sys.exit(main())
Brad Bishop53066752016-09-21 08:48:04 -0400114
115# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4