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 
17*f69ad7e6SBenjamin Fair #include <cinttypes>
1833569758SPatrick Venture #include <cstdio>
1933569758SPatrick Venture #include <iostream>
2033569758SPatrick Venture #include <memory>
2133569758SPatrick Venture 
2233569758SPatrick Venture #include <sdbusplus/bus.hpp>
2333569758SPatrick Venture #include <sdbusplus/message.hpp>
2433569758SPatrick Venture #include <sdbusplus/server.hpp>
2533569758SPatrick Venture 
2633569758SPatrick Venture #include "lpcsnoop/snoop.hpp"
2733569758SPatrick Venture 
2833569758SPatrick Venture /*
2933569758SPatrick Venture  * Handle incoming dbus signal we care about.
3033569758SPatrick Venture  */
3133569758SPatrick Venture static int DbusHandleSignal(sd_bus_message* msg, void* data, sd_bus_error* err);
3233569758SPatrick Venture 
3333569758SPatrick Venture /*
3433569758SPatrick Venture  * Get the match signal for dbus.
3533569758SPatrick Venture  */
3633569758SPatrick Venture static std::string GetMatch(void);
3733569758SPatrick Venture 
3833569758SPatrick Venture // Example object that listens for dbus updates.
3933569758SPatrick Venture class SnoopListen
4033569758SPatrick Venture {
4133569758SPatrick Venture   public:
4233569758SPatrick Venture     SnoopListen(sdbusplus::bus::bus& bus) :
4333569758SPatrick Venture         _bus(bus), _signal(bus, GetMatch().c_str(), DbusHandleSignal, this)
4433569758SPatrick Venture     {
4533569758SPatrick Venture     }
4633569758SPatrick Venture 
4733569758SPatrick Venture   private:
4833569758SPatrick Venture     sdbusplus::bus::bus& _bus;
4933569758SPatrick Venture     sdbusplus::server::match::match _signal;
5033569758SPatrick Venture };
5133569758SPatrick Venture 
5233569758SPatrick Venture /*
5333569758SPatrick Venture  * This is the entry point for the application.
5433569758SPatrick Venture  *
5533569758SPatrick Venture  * This application simply creates an object that registers for incoming value
5633569758SPatrick Venture  * updates for the POST code dbus object.
5733569758SPatrick Venture  */
5833569758SPatrick Venture int main(int argc, char* argv[])
5933569758SPatrick Venture {
6033569758SPatrick Venture     auto ListenBus = sdbusplus::bus::new_default();
6133569758SPatrick Venture     SnoopListen snoop(ListenBus);
6233569758SPatrick Venture 
6333569758SPatrick Venture     while (true)
6433569758SPatrick Venture     {
6533569758SPatrick Venture         ListenBus.process_discard();
6633569758SPatrick Venture         ListenBus.wait();
6733569758SPatrick Venture     }
6833569758SPatrick Venture 
6933569758SPatrick Venture     return 0;
7033569758SPatrick Venture }
7133569758SPatrick Venture 
7233569758SPatrick Venture static int DbusHandleSignal(sd_bus_message* msg, void* data, sd_bus_error* err)
7333569758SPatrick Venture {
7433569758SPatrick Venture     auto sdbpMsg = sdbusplus::message::message(msg);
7533569758SPatrick Venture 
7633569758SPatrick Venture     std::string msgSensor, busName{SNOOP_BUSNAME};
7733569758SPatrick Venture     std::map<std::string, sdbusplus::message::variant<uint64_t>> msgData;
7833569758SPatrick Venture     sdbpMsg.read(msgSensor, msgData);
7933569758SPatrick Venture 
8033569758SPatrick Venture     if (msgSensor == busName)
8133569758SPatrick Venture     {
8233569758SPatrick Venture         auto valPropMap = msgData.find("Value");
8333569758SPatrick Venture         if (valPropMap != msgData.end())
8433569758SPatrick Venture         {
8533569758SPatrick Venture             uint64_t rawValue = sdbusplus::message::variant_ns::get<uint64_t>(
8633569758SPatrick Venture                 valPropMap->second);
8733569758SPatrick Venture 
8833569758SPatrick Venture             /* Print output to verify the example program is receiving values.
8933569758SPatrick Venture              */
90*f69ad7e6SBenjamin Fair             std::printf("recv: 0x%" PRIx64 "\n", rawValue);
9133569758SPatrick Venture         }
9233569758SPatrick Venture     }
9333569758SPatrick Venture 
9433569758SPatrick Venture     return 0;
9533569758SPatrick Venture }
9633569758SPatrick Venture 
9733569758SPatrick Venture static std::string GetMatch(void)
9833569758SPatrick Venture {
9933569758SPatrick Venture     return "type='signal',interface='org.freedesktop.DBus.Properties',"
10033569758SPatrick Venture            "member='PropertiesChanged',path='" SNOOP_OBJECTPATH "'";
10133569758SPatrick Venture }
102