blob: c62c27efd1842c794fdb0691195038308198f0b6 [file] [log] [blame]
Brad Bishopf138c562017-06-15 23:08:09 -04001/**
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
Brad Bishopf138c562017-06-15 23:08:09 -040017#include "evdevpp/evdev.hpp"
George Liu8426b2d2023-08-15 17:47:13 +080018
19#include <CLI/CLI.hpp>
Matthew Barthebd15372020-05-28 11:42:38 -050020// TODO https://github.com/openbmc/phosphor-fan-presence/issues/22
21// #include "sdevent/event.hpp"
22// #include "sdevent/io.hpp"
Brad Bishopf138c562017-06-15 23:08:09 -040023#include "utility.hpp"
24
Matthew Barth11fc0a72020-05-26 10:55:54 -050025#include <algorithm>
26#include <cassert>
27#include <iostream>
28#include <iterator>
29#include <memory>
30
Brad Bishopf138c562017-06-15 23:08:09 -040031int main(int argc, char* argv[])
32{
George Liu8426b2d2023-08-15 17:47:13 +080033 CLI::App app{"evmon utility"};
Brad Bishopf138c562017-06-15 23:08:09 -040034
George Liu8426b2d2023-08-15 17:47:13 +080035 std::string path{};
36 std::string stype{};
37 std::string scode{};
Brad Bishopf138c562017-06-15 23:08:09 -040038
George Liu8426b2d2023-08-15 17:47:13 +080039 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
Brad Bishopf138c562017-06-15 23:08:09 -040044 {
George Liu8426b2d2023-08-15 17:47:13 +080045 app.parse(argc, argv);
Brad Bishopf138c562017-06-15 23:08:09 -040046 }
George Liu8426b2d2023-08-15 17:47:13 +080047 catch (const CLI::Error& e)
48 {
49 return app.exit(e);
50 }
51
52 unsigned int type = EV_KEY;
53 if (!stype.empty())
Brad Bishopf138c562017-06-15 23:08:09 -040054 {
55 type = stoul(stype);
56 }
57
Matthew Barthebd15372020-05-28 11:42:38 -050058 // TODO https://github.com/openbmc/phosphor-fan-presence/issues/22
59 // auto loop = sdevent::event::newDefault();
Brad Bishopf138c562017-06-15 23:08:09 -040060 phosphor::fan::util::FileDescriptor fd(
Matthew Barth11fc0a72020-05-26 10:55:54 -050061 open(path.c_str(), O_RDONLY | O_NONBLOCK));
Brad Bishopf138c562017-06-15 23:08:09 -040062 auto evdev = evdevpp::evdev::newFromFD(fd());
Matthew Barthebd15372020-05-28 11:42:38 -050063 // 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 // });
Brad Bishopf138c562017-06-15 23:08:09 -040070
71 auto value = evdev.fetch(type, stoul(scode));
Matthew Barth11fc0a72020-05-26 10:55:54 -050072 std::cout << "type: " << libevdev_event_type_get_name(type)
73 << " code: " << libevdev_event_code_get_name(type, stoul(scode))
74 << " value: " << value << "\n";
Brad Bishopf138c562017-06-15 23:08:09 -040075
Matthew Barthebd15372020-05-28 11:42:38 -050076 // loop.loop();
Brad Bishopf138c562017-06-15 23:08:09 -040077
78 return 0;
79}