Patrick Venture | 3356975 | 2018-03-12 18:56:14 -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 | |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame^] | 17 | #include <cinttypes> |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 18 | #include <cstdio> |
| 19 | #include <iostream> |
| 20 | #include <memory> |
| 21 | |
| 22 | #include <sdbusplus/bus.hpp> |
| 23 | #include <sdbusplus/message.hpp> |
| 24 | #include <sdbusplus/server.hpp> |
| 25 | |
| 26 | #include "lpcsnoop/snoop.hpp" |
| 27 | |
| 28 | /* |
| 29 | * Handle incoming dbus signal we care about. |
| 30 | */ |
| 31 | static int DbusHandleSignal(sd_bus_message* msg, void* data, sd_bus_error* err); |
| 32 | |
| 33 | /* |
| 34 | * Get the match signal for dbus. |
| 35 | */ |
| 36 | static std::string GetMatch(void); |
| 37 | |
| 38 | // Example object that listens for dbus updates. |
| 39 | class SnoopListen |
| 40 | { |
| 41 | public: |
| 42 | SnoopListen(sdbusplus::bus::bus& bus) : |
| 43 | _bus(bus), _signal(bus, GetMatch().c_str(), DbusHandleSignal, this) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | sdbusplus::bus::bus& _bus; |
| 49 | sdbusplus::server::match::match _signal; |
| 50 | }; |
| 51 | |
| 52 | /* |
| 53 | * This is the entry point for the application. |
| 54 | * |
| 55 | * This application simply creates an object that registers for incoming value |
| 56 | * updates for the POST code dbus object. |
| 57 | */ |
| 58 | int main(int argc, char* argv[]) |
| 59 | { |
| 60 | auto ListenBus = sdbusplus::bus::new_default(); |
| 61 | SnoopListen snoop(ListenBus); |
| 62 | |
| 63 | while (true) |
| 64 | { |
| 65 | ListenBus.process_discard(); |
| 66 | ListenBus.wait(); |
| 67 | } |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | static int DbusHandleSignal(sd_bus_message* msg, void* data, sd_bus_error* err) |
| 73 | { |
| 74 | auto sdbpMsg = sdbusplus::message::message(msg); |
| 75 | |
| 76 | std::string msgSensor, busName{SNOOP_BUSNAME}; |
| 77 | std::map<std::string, sdbusplus::message::variant<uint64_t>> msgData; |
| 78 | sdbpMsg.read(msgSensor, msgData); |
| 79 | |
| 80 | if (msgSensor == busName) |
| 81 | { |
| 82 | auto valPropMap = msgData.find("Value"); |
| 83 | if (valPropMap != msgData.end()) |
| 84 | { |
| 85 | uint64_t rawValue = sdbusplus::message::variant_ns::get<uint64_t>( |
| 86 | valPropMap->second); |
| 87 | |
| 88 | /* Print output to verify the example program is receiving values. |
| 89 | */ |
Benjamin Fair | f69ad7e | 2018-07-13 13:41:10 -0700 | [diff] [blame^] | 90 | std::printf("recv: 0x%" PRIx64 "\n", rawValue); |
Patrick Venture | 3356975 | 2018-03-12 18:56:14 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | static std::string GetMatch(void) |
| 98 | { |
| 99 | return "type='signal',interface='org.freedesktop.DBus.Properties'," |
| 100 | "member='PropertiesChanged',path='" SNOOP_OBJECTPATH "'"; |
| 101 | } |