133569758SPatrick Venture /**
233569758SPatrick Venture * Copyright 2017 Google Inc.
333569758SPatrick Venture *
433569758SPatrick Venture * Licensed under the Apache License, Version 2.0 (the "License");
533569758SPatrick Venture * you may not use this file except in compliance with the License.
633569758SPatrick Venture * You may obtain a copy of the License at
733569758SPatrick Venture *
833569758SPatrick Venture * http://www.apache.org/licenses/LICENSE-2.0
933569758SPatrick Venture *
1033569758SPatrick Venture * Unless required by applicable law or agreed to in writing, software
1133569758SPatrick Venture * distributed under the License is distributed on an "AS IS" BASIS,
1233569758SPatrick Venture * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1333569758SPatrick Venture * See the License for the specific language governing permissions and
1433569758SPatrick Venture * limitations under the License.
1533569758SPatrick Venture */
1633569758SPatrick Venture
17b5754fd4SPatrick Venture #include "lpcsnoop/snoop_listen.hpp"
18b5754fd4SPatrick Venture
19f69ad7e6SBenjamin Fair #include <cinttypes>
2033569758SPatrick Venture #include <cstdio>
2133569758SPatrick Venture #include <iostream>
2233569758SPatrick Venture #include <memory>
2333569758SPatrick Venture
24afff574eSKun Yi /* Example PostCode handler which simply prints them */
printPostcode(FILE *,postcode_t postcode)25f8d0e0bfSwukaihua-fii-na static void printPostcode(FILE*, postcode_t postcode)
2633569758SPatrick Venture {
27afff574eSKun Yi /* Print output to verify the example program is receiving values. */
28*fe51495eSCosmo Chou auto code = std::get<0>(postcode);
29*fe51495eSCosmo Chou std::string hexCode;
30*fe51495eSCosmo Chou hexCode.reserve(2 * code.size());
31*fe51495eSCosmo Chou for (const auto& byte : code)
32*fe51495eSCosmo Chou {
33*fe51495eSCosmo Chou hexCode += std::format("{:02x}", byte);
34*fe51495eSCosmo Chou }
35*fe51495eSCosmo Chou std::printf("recv: 0x%s\n", hexCode.c_str());
3633569758SPatrick Venture }
3733569758SPatrick Venture
38afff574eSKun Yi /*
39afff574eSKun Yi * One can also specify custom handler that operates on
40aebf87ccSPatrick Williams * sdbusplus::message_t type and pass them to constructor.
41afff574eSKun Yi * e.g.
42afff574eSKun Yi *
43aebf87ccSPatrick Williams * static void PrintMessageMap(sdbusplus::message_t& m)
44afff574eSKun Yi * {
45afff574eSKun Yi * std::string messageBusName;
4659c5d9ffSPatrick Williams * std::map<std::string, std::variant<uint64_t>> messageData;
47afff574eSKun Yi *
48afff574eSKun Yi * m.read(messageBusName, messageData);
49afff574eSKun Yi *
50afff574eSKun Yi * std::cout << "Got message from " << messageBusName << std::endl;
51afff574eSKun Yi * for (const auto& kv : messageData)
52afff574eSKun Yi * {
53afff574eSKun Yi * std::cout << "Key: " << kv.first << std::endl;
54afff574eSKun Yi * std::cout << "Value: " << get<uint64_t>(kv.second) << std::endl;
55afff574eSKun Yi * }
56afff574eSKun Yi * }
57afff574eSKun Yi *
58afff574eSKun Yi * lpcsnoop::SnoopListen snoop(ListenBus, PrintMessageMap);
59afff574eSKun Yi */
6033569758SPatrick Venture
6133569758SPatrick Venture /*
6233569758SPatrick Venture * This is the entry point for the application.
6333569758SPatrick Venture *
6433569758SPatrick Venture * This application simply creates an object that registers for incoming value
6533569758SPatrick Venture * updates for the POST code dbus object.
6633569758SPatrick Venture */
main()67cce09623SKun Yi int main()
6833569758SPatrick Venture {
6933569758SPatrick Venture auto ListenBus = sdbusplus::bus::new_default();
70afff574eSKun Yi lpcsnoop::SnoopListen snoop(ListenBus, printPostcode);
7133569758SPatrick Venture
7233569758SPatrick Venture while (true)
7333569758SPatrick Venture {
7433569758SPatrick Venture ListenBus.process_discard();
7533569758SPatrick Venture ListenBus.wait();
7633569758SPatrick Venture }
7733569758SPatrick Venture
7833569758SPatrick Venture return 0;
7933569758SPatrick Venture }
80