xref: /openbmc/phosphor-host-postd/7seg.cpp (revision f8d0e0bf)
1938c0e43SBenjamin Fair /**
2938c0e43SBenjamin Fair  * Copyright 2017 Google Inc.
3938c0e43SBenjamin Fair  *
4938c0e43SBenjamin Fair  * Licensed under the Apache License, Version 2.0 (the "License");
5938c0e43SBenjamin Fair  * you may not use this file except in compliance with the License.
6938c0e43SBenjamin Fair  * You may obtain a copy of the License at
7938c0e43SBenjamin Fair  *
8938c0e43SBenjamin Fair  *     http://www.apache.org/licenses/LICENSE-2.0
9938c0e43SBenjamin Fair  *
10938c0e43SBenjamin Fair  * Unless required by applicable law or agreed to in writing, software
11938c0e43SBenjamin Fair  * distributed under the License is distributed on an "AS IS" BASIS,
12938c0e43SBenjamin Fair  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13938c0e43SBenjamin Fair  * See the License for the specific language governing permissions and
14938c0e43SBenjamin Fair  * limitations under the License.
15938c0e43SBenjamin Fair  */
16938c0e43SBenjamin Fair 
17938c0e43SBenjamin Fair #include <cstdint>
18938c0e43SBenjamin Fair #include <cstdio>
1918119accSPatrick Williams #include <filesystem>
20938c0e43SBenjamin Fair #include <lpcsnoop/snoop_listen.hpp>
21938c0e43SBenjamin Fair #include <string>
22938c0e43SBenjamin Fair 
2318119accSPatrick Williams namespace fs = std::filesystem;
24938c0e43SBenjamin Fair 
25*f8d0e0bfSwukaihua-fii-na static void DisplayDbusValue(FILE* f, postcode_t postcodes)
26938c0e43SBenjamin Fair {
27e2abf442SNan Zhou     auto postcode = std::get<primary_post_code_t>(postcodes);
28938c0e43SBenjamin Fair     // Uses cstdio instead of streams because the device file has
29938c0e43SBenjamin Fair     // very strict requirements about the data format and streaming
30938c0e43SBenjamin Fair     // abstractions tend to muck it up.
31938c0e43SBenjamin Fair     if (f)
32938c0e43SBenjamin Fair     {
33938c0e43SBenjamin Fair         int rc = std::fprintf(f, "%d%02x\n", (postcode > 0xff),
34938c0e43SBenjamin Fair                               static_cast<uint8_t>(postcode & 0xff));
35938c0e43SBenjamin Fair         if (rc < 0)
36938c0e43SBenjamin Fair         {
37938c0e43SBenjamin Fair             std::fprintf(stderr, "failed to write 7seg value: rc=%d\n", rc);
38938c0e43SBenjamin Fair         }
39938c0e43SBenjamin Fair         std::fflush(f);
40938c0e43SBenjamin Fair     }
41938c0e43SBenjamin Fair }
42938c0e43SBenjamin Fair 
43938c0e43SBenjamin Fair /*
44938c0e43SBenjamin Fair  * This is the entry point for the application.
45938c0e43SBenjamin Fair  *
46938c0e43SBenjamin Fair  * This application simply creates an object that registers for incoming value
47938c0e43SBenjamin Fair  * updates for the POST code dbus object.
48938c0e43SBenjamin Fair  */
49938c0e43SBenjamin Fair int main(int argc, const char* argv[])
50938c0e43SBenjamin Fair {
51938c0e43SBenjamin Fair     if (argc != 2 || !fs::exists(argv[1]))
52938c0e43SBenjamin Fair     {
53938c0e43SBenjamin Fair         std::fprintf(stderr, "usage: %s <device_node>\n", argv[0]);
54938c0e43SBenjamin Fair         return -1;
55938c0e43SBenjamin Fair     }
56938c0e43SBenjamin Fair 
57*f8d0e0bfSwukaihua-fii-na     static bool sig_recv = false;
58*f8d0e0bfSwukaihua-fii-na     FILE* f = std::fopen(argv[1], "r+");
59938c0e43SBenjamin Fair 
60938c0e43SBenjamin Fair     auto ListenBus = sdbusplus::bus::new_default();
61938c0e43SBenjamin Fair     std::unique_ptr<lpcsnoop::SnoopListen> snoop =
62*f8d0e0bfSwukaihua-fii-na         std::make_unique<lpcsnoop::SnoopListen>(ListenBus, DisplayDbusValue, f);
63938c0e43SBenjamin Fair 
64*f8d0e0bfSwukaihua-fii-na     signal(SIGINT, [](int signum) {
65*f8d0e0bfSwukaihua-fii-na         if (signum == SIGINT)
66*f8d0e0bfSwukaihua-fii-na         {
67*f8d0e0bfSwukaihua-fii-na             sig_recv = true:
68*f8d0e0bfSwukaihua-fii-na         }
69*f8d0e0bfSwukaihua-fii-na     });
70*f8d0e0bfSwukaihua-fii-na 
71*f8d0e0bfSwukaihua-fii-na     while (!sig_recv)
72938c0e43SBenjamin Fair     {
73938c0e43SBenjamin Fair         ListenBus.process_discard();
74938c0e43SBenjamin Fair         ListenBus.wait();
75938c0e43SBenjamin Fair     }
76938c0e43SBenjamin Fair 
77*f8d0e0bfSwukaihua-fii-na     std::fclose(f);
78938c0e43SBenjamin Fair     return 0;
79938c0e43SBenjamin Fair }
80