blob: 35bd7e9f60fadc3ff423e2e029822d3e3bb6e53c [file] [log] [blame]
Norman Jamesb51b9a52015-10-18 14:47:35 -05001#!/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 new_str = ""
22 for x in data:
23 new_str += "0x%s " % (x.encode('hex'))
24 print new_str
25
26
27class Ipmid(dbus.service.Object):
28 def __init__(self, bus, name):
29 dbus.service.Object.__init__(self,bus,name)
30
31 def setReader(self, reader):
32 self.reader = reader
33
34 @dbus.service.signal(DBUS_NAME, "yyyay")
35 def ReceivedMessage(self, seq, netfn, cmd, data):
Norman Jamesd5246882015-10-26 06:42:15 -050036 #print("IPMI packet from host. Seq = 0x%x Netfn = 0x%x Cmd = 0x%x" %
37 # (ord(seq), ord(netfn), ord(cmd)))
Norman Jamesb51b9a52015-10-18 14:47:35 -050038 print_packet(seq, netfn, cmd, data)
39
40 @dbus.service.method(DBUS_NAME, "", "")
41 def test(self):
42 print("TEST")
43
44 @dbus.service.method(DBUS_NAME, "yyyay", "x")
45 def sendMessage(self, seq, netfn, cmd, data):
Norman Jamesd5246882015-10-26 06:42:15 -050046 #print("IPMI packet sent to host. Seq = 0x%x Netfn = 0x%x Cmd = 0x%x" %
47 # (int(seq), int(netfn), int(cmd)))
Norman Jamesb51b9a52015-10-18 14:47:35 -050048 #print_packet(seq, netfn, cmd, data)
49
50 self.reader.write(seq, netfn, cmd, data)
51
52 return 0
53
54class BtReader(object):
55 def __init__(self, ipmi_obj):
56 self.ipmi_obj = ipmi_obj
57 flags = os.O_NONBLOCK | os.O_RDWR
Norman Jamesd5246882015-10-26 06:42:15 -050058 self.bt = os.open("/dev/bt-host", flags)
Norman Jamesb51b9a52015-10-18 14:47:35 -050059 glib.io_add_watch(self.bt, glib.IO_IN, self.io_callback)
60 ipmi_obj.setReader(self)
61
62 def write(self, seq, netfn, cmd, data):
63 # Untested
64 val = chr(netfn) + chr(seq) + chr(cmd)
65 val += reduce(lambda a, b: a + chr(b), data, "")
66 val = chr(len(val)) + val
67 os.write(self.bt, val)
68
69 def io_callback(self, fd, condition):
70 data = os.read(self.bt, 128)
71 if (len(data) > 2):
72 self.ipmi_obj.ReceivedMessage(data[2], data[1], data[3], data[4:])
73 return True
74
75def main():
76 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
77 bus = dbus.SystemBus()
78 name = dbus.service.BusName(DBUS_NAME, bus)
79 obj = Ipmid(bus, OBJ_NAME)
80 r = BtReader(obj)
81 mainloop = gobject.MainLoop()
82 print("Started")
83 mainloop.run()
84
85if __name__ == '__main__':
86 sys.exit(main())