blob: 501c3f5b6f74d81752675dc35acbc53f35287c2c [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
Patrick Williams0ea73572023-05-10 07:50:44 -050017#include <lpcsnoop/snoop_listen.hpp>
18
Benjamin Fair938c0e42020-04-01 17:44:46 -070019#include <cstdint>
20#include <cstdio>
Patrick Williams18119ac2022-06-16 17:10:04 -050021#include <filesystem>
Benjamin Fair938c0e42020-04-01 17:44:46 -070022#include <string>
23
Patrick Williams18119ac2022-06-16 17:10:04 -050024namespace fs = std::filesystem;
Benjamin Fair938c0e42020-04-01 17:44:46 -070025
wukaihua-fii-naf8d0e0b2022-05-11 14:43:09 +080026static void DisplayDbusValue(FILE* f, postcode_t postcodes)
Benjamin Fair938c0e42020-04-01 17:44:46 -070027{
Nan Zhoue2abf442021-03-15 22:47:21 -070028 auto postcode = std::get<primary_post_code_t>(postcodes);
Benjamin Fair938c0e42020-04-01 17:44:46 -070029 // 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.
Benjamin Fair938c0e42020-04-01 17:44:46 -070032 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);
Benjamin Fair938c0e42020-04-01 17:44:46 -070041 }
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 */
50int 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
wukaihua-fii-naf8d0e0b2022-05-11 14:43:09 +080058 static bool sig_recv = false;
59 FILE* f = std::fopen(argv[1], "r+");
Benjamin Fair938c0e42020-04-01 17:44:46 -070060
61 auto ListenBus = sdbusplus::bus::new_default();
62 std::unique_ptr<lpcsnoop::SnoopListen> snoop =
wukaihua-fii-naf8d0e0b2022-05-11 14:43:09 +080063 std::make_unique<lpcsnoop::SnoopListen>(ListenBus, DisplayDbusValue, f);
Benjamin Fair938c0e42020-04-01 17:44:46 -070064
wukaihua-fii-naf8d0e0b2022-05-11 14:43:09 +080065 signal(SIGINT, [](int signum) {
66 if (signum == SIGINT)
67 {
Willy Tu5efea6a2022-07-11 16:15:29 -070068 sig_recv = true;
wukaihua-fii-naf8d0e0b2022-05-11 14:43:09 +080069 }
70 });
71
72 while (!sig_recv)
Benjamin Fair938c0e42020-04-01 17:44:46 -070073 {
74 ListenBus.process_discard();
75 ListenBus.wait();
76 }
77
wukaihua-fii-naf8d0e0b2022-05-11 14:43:09 +080078 std::fclose(f);
Benjamin Fair938c0e42020-04-01 17:44:46 -070079 return 0;
80}