blob: 0fc6a86503540a56bce99f8f8bc4a6baa8d557ae [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 name = dbus.service.BusName(DBUS_NAME, bus)
90 obj = IpmiDebug(bus, OBJ_NAME)
91 mainloop = gobject.MainLoop()
92 r = ConsoleReader(obj)
93
Jeremy Kerre34da632015-10-27 11:52:08 +080094 print ("Enter IPMI packet as hex values. First three bytes will be used"
95 "as netfn and cmd.\nlun will be zero.")
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080096 mainloop.run()
97
98if __name__ == '__main__':
99 sys.exit(main())