blob: a8b1001f85c6f3d02f845f1c6346af6843d4efb4 [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
14DBUS_NAME = 'org.openbmc.HostIpmi'
15OBJ_NAME = '/org/openbmc/HostIpmi/1'
16
Adriana Kobylak24341f92018-01-26 15:07:23 -060017
Jeremy Kerre34da632015-10-27 11:52:08 +080018def 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 Kerr0ffd4f82015-09-14 14:00:57 +080022
23
Jeremy Kerre34da632015-10-27 11:52:08 +080024def 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 Kobylak24341f92018-01-26 15:07:23 -060029
Jeremy Kerre34da632015-10-27 11:52:08 +080030def 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 Kobylak24341f92018-01-26 15:07:23 -060037
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080038class IpmiDebug(dbus.service.Object):
Adriana Kobylak24341f92018-01-26 15:07:23 -060039 def __init__(self, bus, name):
40 dbus.service.Object.__init__(self, bus, name)
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080041
Jeremy Kerre34da632015-10-27 11:52:08 +080042 @dbus.service.signal(DBUS_NAME, "yyyyay")
43 def ReceivedMessage(self, seq, netfn, lun, cmd, data):
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080044 print "IPMI packet from host:"
Jeremy Kerre34da632015-10-27 11:52:08 +080045 print_request(seq, netfn, lun, cmd, data)
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080046
Jeremy Kerre34da632015-10-27 11:52:08 +080047 @dbus.service.method(DBUS_NAME, "yyyyyay", "x")
48 def sendMessage(self, seq, netfn, lun, cmd, ccode, data):
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080049 print "IPMI packet sent to host:"
Jeremy Kerre34da632015-10-27 11:52:08 +080050 print_response(seq, netfn, lun, cmd, ccode, data)
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080051 return 0
52
Jeremy Kerre34da632015-10-27 11:52:08 +080053 @dbus.service.method(DBUS_NAME)
54 def setAttention(self):
55 print "IPMI SMS_ATN set"
56
Adriana Kobylak24341f92018-01-26 15:07:23 -060057
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080058class 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 Kerre34da632015-10-27 11:52:08 +080088 self.ipmi_obj.ReceivedMessage(self.seq, data[0], 0, data[1], data[2:])
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080089
Adriana Kobylak24341f92018-01-26 15:07:23 -060090
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080091def main():
92 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Adriana Kobylak2cb27752015-10-19 16:23:14 -050093 bus = dbus.SystemBus()
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080094 obj = IpmiDebug(bus, OBJ_NAME)
95 mainloop = gobject.MainLoop()
96 r = ConsoleReader(obj)
Brad Bishopf0f3efe2016-06-29 23:20:24 -040097
98 obj.unmask_signals()
Brad Bishop70852a32016-06-29 22:58:51 -040099 name = dbus.service.BusName(DBUS_NAME, bus)
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +0800100
Jeremy Kerre34da632015-10-27 11:52:08 +0800101 print ("Enter IPMI packet as hex values. First three bytes will be used"
Adriana Kobylak24341f92018-01-26 15:07:23 -0600102 "as netfn and cmd.\nlun will be zero.")
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +0800103 mainloop.run()
104
Adriana Kobylak24341f92018-01-26 15:07:23 -0600105
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +0800106if __name__ == '__main__':
107 sys.exit(main())
Brad Bishop53066752016-09-21 08:48:04 -0400108
109# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4