xref: /openbmc/phosphor-host-postd/7seg.cpp (revision 0d14a59e)
1 /**
2  * Copyright 2017 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cstdint>
18 #include <cstdio>
19 #include <experimental/filesystem>
20 #include <lpcsnoop/snoop_listen.hpp>
21 #include <string>
22 
23 static const char* device_node_path;
24 
25 namespace fs = std::experimental::filesystem;
26 
27 static void DisplayDbusValue(uint64_t postcode)
28 {
29     // Uses cstdio instead of streams because the device file has
30     // very strict requirements about the data format and streaming
31     // abstractions tend to muck it up.
32     FILE* f = std::fopen(device_node_path, "r+");
33     if (f)
34     {
35         int rc = std::fprintf(f, "%d%02x\n", (postcode > 0xff),
36                               static_cast<uint8_t>(postcode & 0xff));
37         if (rc < 0)
38         {
39             std::fprintf(stderr, "failed to write 7seg value: rc=%d\n", rc);
40         }
41         std::fflush(f);
42         std::fclose(f);
43     }
44 }
45 
46 /*
47  * This is the entry point for the application.
48  *
49  * This application simply creates an object that registers for incoming value
50  * updates for the POST code dbus object.
51  */
52 int main(int argc, const char* argv[])
53 {
54     if (argc != 2 || !fs::exists(argv[1]))
55     {
56         std::fprintf(stderr, "usage: %s <device_node>\n", argv[0]);
57         return -1;
58     }
59 
60     device_node_path = argv[1];
61 
62     auto ListenBus = sdbusplus::bus::new_default();
63     std::unique_ptr<lpcsnoop::SnoopListen> snoop =
64         std::make_unique<lpcsnoop::SnoopListen>(ListenBus, DisplayDbusValue);
65 
66     while (true)
67     {
68         ListenBus.process_discard();
69         ListenBus.wait();
70     }
71 
72     return 0;
73 }
74