blob: da8b2ba57abd5b0d26d691f221247c12b6908f85 [file] [log] [blame]
Benjamin Fair938c0e42020-04-01 17:44:46 -07001/**
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>
Patrick Williams18119ac2022-06-16 17:10:04 -050019#include <filesystem>
Benjamin Fair938c0e42020-04-01 17:44:46 -070020#include <lpcsnoop/snoop_listen.hpp>
21#include <string>
22
Patrick Williams18119ac2022-06-16 17:10:04 -050023namespace fs = std::filesystem;
Benjamin Fair938c0e42020-04-01 17:44:46 -070024
wukaihua-fii-naf8d0e0b2022-05-11 14:43:09 +080025static void DisplayDbusValue(FILE* f, postcode_t postcodes)
Benjamin Fair938c0e42020-04-01 17:44:46 -070026{
Nan Zhoue2abf442021-03-15 22:47:21 -070027 auto postcode = std::get<primary_post_code_t>(postcodes);
Benjamin Fair938c0e42020-04-01 17:44:46 -070028 // 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.
Benjamin Fair938c0e42020-04-01 17:44:46 -070031 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);
Benjamin Fair938c0e42020-04-01 17:44:46 -070040 }
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 */
49int 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
wukaihua-fii-naf8d0e0b2022-05-11 14:43:09 +080057 static bool sig_recv = false;
58 FILE* f = std::fopen(argv[1], "r+");
Benjamin Fair938c0e42020-04-01 17:44:46 -070059
60 auto ListenBus = sdbusplus::bus::new_default();
61 std::unique_ptr<lpcsnoop::SnoopListen> snoop =
wukaihua-fii-naf8d0e0b2022-05-11 14:43:09 +080062 std::make_unique<lpcsnoop::SnoopListen>(ListenBus, DisplayDbusValue, f);
Benjamin Fair938c0e42020-04-01 17:44:46 -070063
wukaihua-fii-naf8d0e0b2022-05-11 14:43:09 +080064 signal(SIGINT, [](int signum) {
65 if (signum == SIGINT)
66 {
Willy Tu5efea6a2022-07-11 16:15:29 -070067 sig_recv = true;
wukaihua-fii-naf8d0e0b2022-05-11 14:43:09 +080068 }
69 });
70
71 while (!sig_recv)
Benjamin Fair938c0e42020-04-01 17:44:46 -070072 {
73 ListenBus.process_discard();
74 ListenBus.wait();
75 }
76
wukaihua-fii-naf8d0e0b2022-05-11 14:43:09 +080077 std::fclose(f);
Benjamin Fair938c0e42020-04-01 17:44:46 -070078 return 0;
79}