blob: 300756cf13551efbc997051bab49a41cced45277 [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
17def print_packet(seq, netfn, cmd, data):
18 print 'seq: 0x%02x\nnetfn: 0x%02x\ncmd: 0x%02x\ndata: [%s]' % (
19 seq, netfn, cmd,
20 ", ".join(['0x%02x' % x for x in data]))
21
22
23class IpmiDebug(dbus.service.Object):
24 def __init__(self,bus,name):
25 dbus.service.Object.__init__(self,bus,name)
26
27 @dbus.service.signal(DBUS_NAME, "yyyay")
28 def ReceivedMessage(self, seq, netfn, cmd, data):
29 print "IPMI packet from host:"
30 print_packet(seq, netfn, cmd, data)
31
32 @dbus.service.method(DBUS_NAME, "yyyay", "x")
33 def sendMessage(self, seq, netfn, cmd, data):
34 print "IPMI packet sent to host:"
35 print_packet(seq, netfn, cmd, data)
36 return 0
37
38class ConsoleReader(object):
39 def __init__(self, ipmi_obj):
40 self.buffer = ''
41 self.seq = 0
42 self.ipmi_obj = ipmi_obj
43 flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL)
44 flags |= os.O_NONBLOCK
45 fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags)
46 glib.io_add_watch(sys.stdin, glib.IO_IN, self.io_callback)
47
48 def io_callback(self, fd, condition):
49 chunk = fd.read()
50 for char in chunk:
51 self.buffer += char
52 if char == '\n':
53 self.line(self.buffer)
54 self.buffer = ''
55
56 return True
57
58 def line(self, data):
59 s = data.split(' ')
60 if len(s) < 2:
61 print "Not enough bytes to form a valid IPMI packet"
62 return
63 try:
64 data = [int(c, 16) for c in s]
65 except ValueError:
66 return
67 self.seq += 1
68 self.ipmi_obj.ReceivedMessage(self.seq, data[0], data[1], data[2:])
69
70def main():
71 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Adriana Kobylak2cb27752015-10-19 16:23:14 -050072 bus = dbus.SystemBus()
Jeremy Kerr0ffd4f82015-09-14 14:00:57 +080073 name = dbus.service.BusName(DBUS_NAME, bus)
74 obj = IpmiDebug(bus, OBJ_NAME)
75 mainloop = gobject.MainLoop()
76 r = ConsoleReader(obj)
77
78 print ("Enter IPMI packet as hex values. First two bytes will be used"
79 "as netfn and cmd")
80 mainloop.run()
81
82if __name__ == '__main__':
83 sys.exit(main())