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