blob: d81cb2ed3a3c9a33723e1bffb1b5d432f03aceae [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
Jeremy Kerre34da632015-10-27 11:52:08 +080017def header(seq, netfn, lun, cmd):
18 return (
19 'seq: 0x%02x\nnetfn: 0x%02x\n\nlun: 0x%02d\ncmd: 0x%02x\n') % (
20 seq, netfn, lun, cmd)
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080021
22
Jeremy Kerre34da632015-10-27 11:52:08 +080023def print_request(seq, netfn, lun, cmd, data):
24 str = header(seq, netfn, lun, cmd)
25 str += 'data: [%s]' % ', '.join(['0x%02x' % x for x in data])
26 print str
27
28def print_response(seq, netfn, lun, cmd, cc, data):
29 str = header(seq, netfn, lun, cmd)
30 str += 'cc: 0x%02x\ndata: [%s]' % (
31 cc, ', '.join(['0x%02x' % x for x in data])
32 )
33 print str
34
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080035class IpmiDebug(dbus.service.Object):
36 def __init__(self,bus,name):
37 dbus.service.Object.__init__(self,bus,name)
38
Jeremy Kerre34da632015-10-27 11:52:08 +080039 @dbus.service.signal(DBUS_NAME, "yyyyay")
40 def ReceivedMessage(self, seq, netfn, lun, cmd, data):
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080041 print "IPMI packet from host:"
Jeremy Kerre34da632015-10-27 11:52:08 +080042 print_request(seq, netfn, lun, cmd, data)
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080043
Jeremy Kerre34da632015-10-27 11:52:08 +080044 @dbus.service.method(DBUS_NAME, "yyyyyay", "x")
45 def sendMessage(self, seq, netfn, lun, cmd, ccode, data):
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080046 print "IPMI packet sent to host:"
Jeremy Kerre34da632015-10-27 11:52:08 +080047 print_response(seq, netfn, lun, cmd, ccode, data)
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080048 return 0
49
Jeremy Kerre34da632015-10-27 11:52:08 +080050 @dbus.service.method(DBUS_NAME)
51 def setAttention(self):
52 print "IPMI SMS_ATN set"
53
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080054class ConsoleReader(object):
55 def __init__(self, ipmi_obj):
56 self.buffer = ''
57 self.seq = 0
58 self.ipmi_obj = ipmi_obj
59 flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
60 flags |= os.O_NONBLOCK
61 fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags)
62 glib.io_add_watch(sys.stdin, glib.IO_IN, self.io_callback)
63
64 def io_callback(self, fd, condition):
65 chunk = fd.read()
66 for char in chunk:
67 self.buffer += char
68 if char == '\n':
69 self.line(self.buffer)
70 self.buffer = ''
71
72 return True
73
74 def line(self, data):
75 s = data.split(' ')
76 if len(s) < 2:
77 print "Not enough bytes to form a valid IPMI packet"
78 return
79 try:
80 data = [int(c, 16) for c in s]
81 except ValueError:
82 return
83 self.seq += 1
Jeremy Kerre34da632015-10-27 11:52:08 +080084 self.ipmi_obj.ReceivedMessage(self.seq, data[0], 0, data[1], data[2:])
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080085
86def main():
87 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Adriana Kobylak2cb27752015-10-19 16:23:14 -050088 bus = dbus.SystemBus()
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080089 obj = IpmiDebug(bus, OBJ_NAME)
90 mainloop = gobject.MainLoop()
91 r = ConsoleReader(obj)
Brad Bishopf0f3efe2016-06-29 23:20:24 -040092
93 obj.unmask_signals()
Brad Bishop70852a32016-06-29 22:58:51 -040094 name = dbus.service.BusName(DBUS_NAME, bus)
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080095
Jeremy Kerre34da632015-10-27 11:52:08 +080096 print ("Enter IPMI packet as hex values. First three bytes will be used"
97 "as netfn and cmd.\nlun will be zero.")
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080098 mainloop.run()
99
100if __name__ == '__main__':
101 sys.exit(main())
Brad Bishop53066752016-09-21 08:48:04 -0400102
103# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4