xref: /openbmc/phosphor-host-postd/7seg.cpp (revision aebf87cc)
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 <filesystem>
20 #include <lpcsnoop/snoop_listen.hpp>
21 #include <string>
22 
23 namespace fs = std::filesystem;
24 
25 static void DisplayDbusValue(FILE* f, postcode_t postcodes)
26 {
27     auto postcode = std::get<primary_post_code_t>(postcodes);
28     // Uses cstdio instead of streams because the device file has
29     // very strict requirements about the data format and streaming
30     // abstractions tend to muck it up.
31     if (f)
32     {
33         int rc = std::fprintf(f, "%d%02x\n", (postcode > 0xff),
34                               static_cast<uint8_t>(postcode & 0xff));
35         if (rc < 0)
36         {
37             std::fprintf(stderr, "failed to write 7seg value: rc=%d\n", rc);
38         }
39         std::fflush(f);
40     }
41 }
42 
43 /*
44  * This is the entry point for the application.
45  *
46  * This application simply creates an object that registers for incoming value
47  * updates for the POST code dbus object.
48  */
49 int main(int argc, const char* argv[])
50 {
51     if (argc != 2 || !fs::exists(argv[1]))
52     {
53         std::fprintf(stderr, "usage: %s <device_node>\n", argv[0]);
54         return -1;
55     }
56 
57     static bool sig_recv = false;
58     FILE* f = std::fopen(argv[1], "r+");
59 
60     auto ListenBus = sdbusplus::bus::new_default();
61     std::unique_ptr<lpcsnoop::SnoopListen> snoop =
62         std::make_unique<lpcsnoop::SnoopListen>(ListenBus, DisplayDbusValue, f);
63 
64     signal(SIGINT, [](int signum) {
65         if (signum == SIGINT)
66         {
67             sig_recv = true;
68         }
69     });
70 
71     while (!sig_recv)
72     {
73         ListenBus.process_discard();
74         ListenBus.wait();
75     }
76 
77     std::fclose(f);
78     return 0;
79 }
80