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