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