summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/beepcode-mgr/files/beepcode_mgr.cpp
blob: 8976a401547d4aa2615e82839aae888195996e58 (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* Copyright 2019 Intel
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include <fcntl.h>
#include <linux/input.h>

#include <boost/date_time/posix_time/posix_time.hpp>
#include <chrono>
#include <iostream>
#include <phosphor-logging/log.hpp>
#include <sdbusplus/asio/object_server.hpp>

static constexpr uint32_t defaultBeepFrequence = 2000;
static constexpr uint32_t defaultBeepDurationMs = 300;
// Duration between two beeps
static constexpr uint32_t defaultInterBeepDurationMs = 300;
// Duration between two 4-bit digitals
static constexpr uint32_t defaultInterDigitBeepDurationMs = 800;
// Duration between two patterns
static constexpr uint32_t defaultPostBeepDurationMs = 1000;

static constexpr uint8_t offBeepState = 0;
static constexpr uint8_t onBeepState = 1;
// finish 1 bit beep
static constexpr uint8_t interBeepState = 2;
// finish 4 bits beep
static constexpr uint8_t interDigitBeepState = 3;
// finish all bits beep
static constexpr uint8_t postBeepState = 4;

static const std::vector<uint32_t> beepDelayTable = {
    0, defaultBeepDurationMs, defaultInterBeepDurationMs,
    defaultInterDigitBeepDurationMs, defaultPostBeepDurationMs};

static constexpr uint32_t bpBitCount = 4;
static constexpr uint32_t bpShiftCount = 32;
static constexpr uint32_t bpMask = 0xf0000000;

// beep code priority
static constexpr uint8_t beepOff = 0;
static constexpr uint8_t beepVRWatchdogTimeout = 1;
static constexpr uint8_t beepPSUFailure = 2;
static constexpr uint8_t beepCPUMIssing = 3;
static constexpr uint8_t beepCPUCatError = 4;
static constexpr uint8_t beepCPUErr2 = 5;
static constexpr uint8_t beepVoltageMismatch = 6;
static constexpr uint8_t beepCPUConfigError = 7;
static constexpr uint8_t beepPowerFail = 8;
static constexpr uint8_t beepPowerGoodTimeOut = 9;
static constexpr uint8_t beepMax = 10;

// priority, abbrev name map
static const std::map<uint8_t, std::string> beepCodeNameList = {
    {beepVRWatchdogTimeout, "VRWatchdogTimeout"},
    {beepPSUFailure, "PSUFailure"},
    {beepCPUMIssing, "CPUMissing"},
    {beepCPUCatError, "CPUCatError"},
    {beepCPUErr2, "CPUErr2"},
    {beepVoltageMismatch, "VoltageMismatch"},
    {beepCPUConfigError, "CPUConfigError"},
    {beepPowerFail, "PowerFail"},
    {beepPowerGoodTimeOut, "PowerGoodTimeOut"},
};

// priority, code pattern map
static const std::map<uint8_t, std::string> beepCodePatternList = {
    {beepVRWatchdogTimeout, "1-5-1-2"}, {beepPSUFailure, "1-5-1-4"},
    {beepCPUMIssing, "1-5-2-1"},        {beepCPUCatError, "1-5-2-2"},
    {beepCPUErr2, "1-5-2-3"},           {beepVoltageMismatch, "1-5-2-4"},
    {beepCPUConfigError, "1-5-2-5"},    {beepPowerFail, "1-5-4-2"},
    {beepPowerGoodTimeOut, "1-5-4-4"},
};

static const std::vector<uint32_t> beepCodeTable = {
    0, 0x1512, 0x1514, 0x1521, 0x1522, 0x1523, 0x1524, 0x1525, 0x1542, 0x1544};

static constexpr char bpDevName[] = "/dev/input/event0";
static constexpr char bpBusName[] = "xyz.openbmc_project.BeepCode";
static constexpr char bpObjName[] = "/xyz/openbmc_project/BeepCode";
static constexpr char bpIntfName[] = "xyz.openbmc_project.BeepCode";
static constexpr char bpMethodName[] = "Beep";

static std::shared_ptr<sdbusplus::asio::dbus_interface> bpIface;
static boost::asio::io_service io;
static auto conn = std::make_shared<sdbusplus::asio::connection>(io);

class Beeper
{
  public:
    Beeper(boost::asio::io_service& io)
    {
        timer = std::make_unique<boost::asio::steady_timer>(io);
        fdBeepDev = -1;
        currentCount = 0;
        currentBeepCode = 0;
        currentMask = bpMask;
        currentShift = bpShiftCount;
        currentState = offBeepState;
        timerRunning = false;
    }

    ~Beeper()
    {
    }

    void beep(const uint8_t& beepPriority)
    {
        if (timerRunning)
        {
            pendingList.push_back(beepPriority);
            pendingList.sort(std::greater<uint8_t>());
            return;
        }

        performBeep(beepPriority);
    }

  private:
    void performBeep(const uint8_t& beepPriority)
    {
        currentBeepCode = beepCodeTable[beepPriority];
        currentCount = 0;
        currentMask = bpMask;
        currentShift = bpShiftCount;
        getCurrentCount();
        startBeep(defaultBeepFrequence);
        currentState = onBeepState;
        currentCount--;
        timerRunning = true;
        startBeepTimer();
    }

    void startBeepTimer()
    {
        timer->expires_after(
            std::chrono::milliseconds(beepDelayTable[currentState]));
        timer->async_wait([this](const boost::system::error_code& ec) {
            // timer timeout
            switch (currentState)
            {
                case onBeepState:
                    stopBeep();
                    if (currentCount == 0)
                    {
                        // finished the current 4-bit
                        if (currentBeepCode == 0)
                        {
                            // finished all bits
                            currentState = postBeepState;
                        }
                        else
                        {
                            // start next 4-bit
                            currentState = interDigitBeepState;
                            getCurrentCount();
                            currentCount--;
                        }
                    }
                    else
                    {
                        // still in 4-bit processing
                        currentCount--;
                        currentState = interBeepState;
                    }
                    startBeepTimer();
                    break;

                case interBeepState:
                case interDigitBeepState:
                    startBeep(defaultBeepFrequence);
                    currentState = onBeepState;
                    startBeepTimer();
                    break;
                case postBeepState:
                    if (pendingList.size() != 0)
                    {
                        // continue the next new beepcode
                        uint8_t beepPriority = pendingList.front();
                        pendingList.pop_front();
                        performBeep(beepPriority);
                    }
                    else
                    {
                        timerRunning = false;
                    }
                    break;

                default:
                    std::cerr << "Incorrect beepState: "
                              << static_cast<unsigned int>(currentState)
                              << std::endl;
                    break;
            }
        });
    }

    void startBeep(uint32_t freq)
    {
        if (fdBeepDev != -1)
        {
            std::cerr << "beep device is opening already!" << std::endl;
            ::close(fdBeepDev);
            fdBeepDev = -1;
        }

        if ((fdBeepDev = ::open(bpDevName, O_RDWR | O_CLOEXEC)) < 0)
        {
            phosphor::logging::log<phosphor::logging::level::ERR>(
                "Failed to open input device");
            return;
        }

        struct input_event event;
        event.type = EV_SND;
        event.code = SND_TONE;
        event.value = freq;

        if (::write(fdBeepDev, &event, sizeof(struct input_event)) !=
            sizeof(struct input_event))
        {
            phosphor::logging::log<phosphor::logging::level::ERR>(
                "Failed to write a tone sound event");
            ::close(fdBeepDev);
            fdBeepDev = -1;
            return;
        }
        return;
    }

    void stopBeep()
    {
        if (fdBeepDev == -1)
        {
            std::cerr << "beep device is closed!" << std::endl;
            return;
        }

        ::close(fdBeepDev);
        fdBeepDev = -1;
    }

    // Split the beep code based on bpBitCount, for example 0x1544,
    // currentCount=1, 5, 4, 4
    void getCurrentCount()
    {
        while (currentCount == 0)
        {
            currentCount = currentMask & currentBeepCode;
            currentShift -= bpBitCount;
            currentCount >>= currentShift;
            currentBeepCode = currentBeepCode & ~currentMask;
            currentMask >>= bpBitCount;
            if (currentMask == 0)
            {
                break;
            }
        }
    }

    int fdBeepDev;
    bool timerRunning;
    uint32_t currentCount;
    uint32_t currentBeepCode;
    uint32_t currentMask;
    uint32_t currentShift;
    uint8_t currentState;
    std::unique_ptr<boost::asio::steady_timer> timer;
    std::list<uint8_t> pendingList;
};

static Beeper beeper(io);

// dbus method
static void beep(const uint8_t& beepPriority)
{
    if ((beepPriority >= beepMax) || (beepPriority == beepOff))
    {
        std::cerr << "Incorrect input: "
                  << static_cast<unsigned int>(beepPriority) << std::endl;
        return;
    }

    beeper.beep(beepPriority);

    return;
}

int main(int argc, char** argv)
{
    phosphor::logging::log<phosphor::logging::level::INFO>(
        "Starting BeepCode service");

    conn->request_name(bpBusName);
    sdbusplus::asio::object_server server =
        sdbusplus::asio::object_server(conn);
    bpIface = server.add_interface(bpObjName, bpIntfName);

    bpIface->register_property("BeepCodeNameList", beepCodeNameList,
                               sdbusplus::asio::PropertyPermission::readOnly);
    bpIface->register_property("BeepCodePatternList", beepCodePatternList,
                               sdbusplus::asio::PropertyPermission::readOnly);
    bpIface->register_method(bpMethodName, beep);
    bpIface->initialize();

    io.run();
    return 0;
}