xref: /openbmc/phosphor-host-postd/7seg.cpp (revision 0ea7357e)
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 
17*0ea7357eSPatrick Williams #include <lpcsnoop/snoop_listen.hpp>
18*0ea7357eSPatrick Williams 
19938c0e43SBenjamin Fair #include <cstdint>
20938c0e43SBenjamin Fair #include <cstdio>
2118119accSPatrick Williams #include <filesystem>
22938c0e43SBenjamin Fair #include <string>
23938c0e43SBenjamin Fair 
2418119accSPatrick Williams namespace fs = std::filesystem;
25938c0e43SBenjamin Fair 
DisplayDbusValue(FILE * f,postcode_t postcodes)26f8d0e0bfSwukaihua-fii-na static void DisplayDbusValue(FILE* f, postcode_t postcodes)
27938c0e43SBenjamin Fair {
28e2abf442SNan Zhou     auto postcode = std::get<primary_post_code_t>(postcodes);
29938c0e43SBenjamin Fair     // Uses cstdio instead of streams because the device file has
30938c0e43SBenjamin Fair     // very strict requirements about the data format and streaming
31938c0e43SBenjamin Fair     // abstractions tend to muck it up.
32938c0e43SBenjamin Fair     if (f)
33938c0e43SBenjamin Fair     {
34938c0e43SBenjamin Fair         int rc = std::fprintf(f, "%d%02x\n", (postcode > 0xff),
35938c0e43SBenjamin Fair                               static_cast<uint8_t>(postcode & 0xff));
36938c0e43SBenjamin Fair         if (rc < 0)
37938c0e43SBenjamin Fair         {
38938c0e43SBenjamin Fair             std::fprintf(stderr, "failed to write 7seg value: rc=%d\n", rc);
39938c0e43SBenjamin Fair         }
40938c0e43SBenjamin Fair         std::fflush(f);
41938c0e43SBenjamin Fair     }
42938c0e43SBenjamin Fair }
43938c0e43SBenjamin Fair 
44938c0e43SBenjamin Fair /*
45938c0e43SBenjamin Fair  * This is the entry point for the application.
46938c0e43SBenjamin Fair  *
47938c0e43SBenjamin Fair  * This application simply creates an object that registers for incoming value
48938c0e43SBenjamin Fair  * updates for the POST code dbus object.
49938c0e43SBenjamin Fair  */
main(int argc,const char * argv[])50938c0e43SBenjamin Fair int main(int argc, const char* argv[])
51938c0e43SBenjamin Fair {
52938c0e43SBenjamin Fair     if (argc != 2 || !fs::exists(argv[1]))
53938c0e43SBenjamin Fair     {
54938c0e43SBenjamin Fair         std::fprintf(stderr, "usage: %s <device_node>\n", argv[0]);
55938c0e43SBenjamin Fair         return -1;
56938c0e43SBenjamin Fair     }
57938c0e43SBenjamin Fair 
58f8d0e0bfSwukaihua-fii-na     static bool sig_recv = false;
59f8d0e0bfSwukaihua-fii-na     FILE* f = std::fopen(argv[1], "r+");
60938c0e43SBenjamin Fair 
61938c0e43SBenjamin Fair     auto ListenBus = sdbusplus::bus::new_default();
62938c0e43SBenjamin Fair     std::unique_ptr<lpcsnoop::SnoopListen> snoop =
63f8d0e0bfSwukaihua-fii-na         std::make_unique<lpcsnoop::SnoopListen>(ListenBus, DisplayDbusValue, f);
64938c0e43SBenjamin Fair 
65f8d0e0bfSwukaihua-fii-na     signal(SIGINT, [](int signum) {
66f8d0e0bfSwukaihua-fii-na         if (signum == SIGINT)
67f8d0e0bfSwukaihua-fii-na         {
685efea6a3SWilly Tu             sig_recv = true;
69f8d0e0bfSwukaihua-fii-na         }
70f8d0e0bfSwukaihua-fii-na     });
71f8d0e0bfSwukaihua-fii-na 
72f8d0e0bfSwukaihua-fii-na     while (!sig_recv)
73938c0e43SBenjamin Fair     {
74938c0e43SBenjamin Fair         ListenBus.process_discard();
75938c0e43SBenjamin Fair         ListenBus.wait();
76938c0e43SBenjamin Fair     }
77938c0e43SBenjamin Fair 
78f8d0e0bfSwukaihua-fii-na     std::fclose(f);
79938c0e43SBenjamin Fair     return 0;
80938c0e43SBenjamin Fair }
81