Benjamin Fair | 938c0e4 | 2020-04-01 17:44:46 -0700 | [diff] [blame] | 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 | |
Patrick Williams | 0ea7357 | 2023-05-10 07:50:44 -0500 | [diff] [blame] | 17 | #include <lpcsnoop/snoop_listen.hpp> |
| 18 | |
Benjamin Fair | 938c0e4 | 2020-04-01 17:44:46 -0700 | [diff] [blame] | 19 | #include <cstdint> |
| 20 | #include <cstdio> |
Patrick Williams | 18119ac | 2022-06-16 17:10:04 -0500 | [diff] [blame] | 21 | #include <filesystem> |
Benjamin Fair | 938c0e4 | 2020-04-01 17:44:46 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | |
Patrick Williams | 18119ac | 2022-06-16 17:10:04 -0500 | [diff] [blame] | 24 | namespace fs = std::filesystem; |
Benjamin Fair | 938c0e4 | 2020-04-01 17:44:46 -0700 | [diff] [blame] | 25 | |
wukaihua-fii-na | f8d0e0b | 2022-05-11 14:43:09 +0800 | [diff] [blame] | 26 | static void DisplayDbusValue(FILE* f, postcode_t postcodes) |
Benjamin Fair | 938c0e4 | 2020-04-01 17:44:46 -0700 | [diff] [blame] | 27 | { |
Nan Zhou | e2abf44 | 2021-03-15 22:47:21 -0700 | [diff] [blame] | 28 | auto postcode = std::get<primary_post_code_t>(postcodes); |
Benjamin Fair | 938c0e4 | 2020-04-01 17:44:46 -0700 | [diff] [blame] | 29 | // 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 Fair | 938c0e4 | 2020-04-01 17:44:46 -0700 | [diff] [blame] | 32 | 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 Fair | 938c0e4 | 2020-04-01 17:44:46 -0700 | [diff] [blame] | 41 | } |
| 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 | */ |
| 50 | int 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-na | f8d0e0b | 2022-05-11 14:43:09 +0800 | [diff] [blame] | 58 | static bool sig_recv = false; |
| 59 | FILE* f = std::fopen(argv[1], "r+"); |
Benjamin Fair | 938c0e4 | 2020-04-01 17:44:46 -0700 | [diff] [blame] | 60 | |
| 61 | auto ListenBus = sdbusplus::bus::new_default(); |
| 62 | std::unique_ptr<lpcsnoop::SnoopListen> snoop = |
wukaihua-fii-na | f8d0e0b | 2022-05-11 14:43:09 +0800 | [diff] [blame] | 63 | std::make_unique<lpcsnoop::SnoopListen>(ListenBus, DisplayDbusValue, f); |
Benjamin Fair | 938c0e4 | 2020-04-01 17:44:46 -0700 | [diff] [blame] | 64 | |
wukaihua-fii-na | f8d0e0b | 2022-05-11 14:43:09 +0800 | [diff] [blame] | 65 | signal(SIGINT, [](int signum) { |
| 66 | if (signum == SIGINT) |
| 67 | { |
Willy Tu | 5efea6a | 2022-07-11 16:15:29 -0700 | [diff] [blame] | 68 | sig_recv = true; |
wukaihua-fii-na | f8d0e0b | 2022-05-11 14:43:09 +0800 | [diff] [blame] | 69 | } |
| 70 | }); |
| 71 | |
| 72 | while (!sig_recv) |
Benjamin Fair | 938c0e4 | 2020-04-01 17:44:46 -0700 | [diff] [blame] | 73 | { |
| 74 | ListenBus.process_discard(); |
| 75 | ListenBus.wait(); |
| 76 | } |
| 77 | |
wukaihua-fii-na | f8d0e0b | 2022-05-11 14:43:09 +0800 | [diff] [blame] | 78 | std::fclose(f); |
Benjamin Fair | 938c0e4 | 2020-04-01 17:44:46 -0700 | [diff] [blame] | 79 | return 0; |
| 80 | } |