xref: /openbmc/phosphor-fan-presence/evdevpp/test/evmon.cpp (revision fbf4703f3de7fbdbd8388e946bd71c3b760b174c)
1 /**
2  * Copyright © 2017 IBM Corporation
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 "evdevpp/evdev.hpp"
18 
19 #include <CLI/CLI.hpp>
20 // TODO https://github.com/openbmc/phosphor-fan-presence/issues/22
21 // #include "sdevent/event.hpp"
22 // #include "sdevent/io.hpp"
23 #include "utility.hpp"
24 
25 #include <algorithm>
26 #include <cassert>
27 #include <iostream>
28 #include <iterator>
29 #include <memory>
30 
31 int main(int argc, char* argv[])
32 {
33     CLI::App app{"evmon utility"};
34 
35     std::string path{};
36     std::string stype{};
37     std::string scode{};
38 
39     app.add_option("-p,--path", path, "evdev devpath")->required();
40     app.add_option("-t,--type", stype, "evdev type")->required();
41     app.add_option("-c,--code", scode, "evdev code")->required();
42 
43     try
44     {
45         app.parse(argc, argv);
46     }
47     catch (const CLI::Error& e)
48     {
49         return app.exit(e);
50     }
51 
52     unsigned int type = EV_KEY;
53     if (!stype.empty())
54     {
55         type = stoul(stype);
56     }
57 
58     // TODO https://github.com/openbmc/phosphor-fan-presence/issues/22
59     // auto loop = sdevent::event::newDefault();
60     phosphor::fan::util::FileDescriptor fd(
61         open(path.c_str(), O_RDONLY | O_NONBLOCK));
62     auto evdev = evdevpp::evdev::newFromFD(fd());
63     // sdevent::event::io::IO callback(loop, fd(), [&evdev](auto& s) {
64     //     unsigned int type, code, value;
65     //     std::tie(type, code, value) = evdev.next();
66     //     std::cout << "type: " << libevdev_event_type_get_name(type)
67     //               << " code: " << libevdev_event_code_get_name(type, code)
68     //               << " value: " << value << "\n";
69     // });
70 
71     auto value = evdev.fetch(type, stoul(scode));
72     std::cout << "type: " << libevdev_event_type_get_name(type)
73               << " code: " << libevdev_event_code_get_name(type, stoul(scode))
74               << " value: " << value << "\n";
75 
76     // loop.loop();
77 
78     return 0;
79 }
80