blob: 1a4adb8e0baa24f2bd60c2b5f1de23e379013804 [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 "argument.hpp"
18#include "evdevpp/evdev.hpp"
Matthew Barthebd15372020-05-28 11:42:38 -050019// TODO https://github.com/openbmc/phosphor-fan-presence/issues/22
20// #include "sdevent/event.hpp"
21// #include "sdevent/io.hpp"
Brad Bishopf138c562017-06-15 23:08:09 -040022#include "utility.hpp"
23
Matthew Barth11fc0a72020-05-26 10:55:54 -050024#include <algorithm>
25#include <cassert>
26#include <iostream>
27#include <iterator>
28#include <memory>
29
Brad Bishopf138c562017-06-15 23:08:09 -040030namespace phosphor
31{
32namespace fan
33{
34namespace util
35{
36
37ArgumentParser::ArgumentParser(int argc, char** argv)
38{
39 auto option = 0;
40 while (-1 != (option = getopt_long(argc, argv, optionstr, options, NULL)))
41 {
42 if ((option == '?') || (option == 'h'))
43 {
44 usage(argv);
William A. Kennington III3e781062018-10-19 17:18:34 -070045 exit(1);
Brad Bishopf138c562017-06-15 23:08:09 -040046 }
47
48 auto i = &options[0];
49 while ((i->val != option) && (i->val != 0))
50 {
51 ++i;
52 }
53
54 if (i->val)
55 {
56 arguments[i->name] = (i->has_arg ? optarg : true_string);
57 }
58 }
59}
60
61const std::string& ArgumentParser::operator[](const std::string& opt)
62{
63 auto i = arguments.find(opt);
64 if (i == arguments.end())
65 {
66 return empty_string;
67 }
68 else
69 {
70 return i->second;
71 }
72}
73
74void ArgumentParser::usage(char** argv)
75{
76 std::cerr << "Usage: " << argv[0] << " [options]\n";
77 std::cerr << "Options:\n";
78 std::cerr << " --path evdev devpath\n";
79 std::cerr << " --type evdev type\n";
80 std::cerr << " --code evdev code\n";
81 std::cerr << std::flush;
82}
83
Matthew Barth11fc0a72020-05-26 10:55:54 -050084const option ArgumentParser::options[] = {
85 {"path", required_argument, NULL, 'p'},
86 {"type", required_argument, NULL, 't'},
87 {"code", required_argument, NULL, 'c'},
88 {0, 0, 0, 0},
Brad Bishopf138c562017-06-15 23:08:09 -040089};
90
91const char* ArgumentParser::optionstr = "p:t:c:";
92
93const std::string ArgumentParser::true_string = "true";
94const std::string ArgumentParser::empty_string = "";
95
96static void exit_with_error(const char* err, char** argv)
97{
98 ArgumentParser::usage(argv);
99 std::cerr << "\n";
100 std::cerr << "ERROR: " << err << "\n";
William A. Kennington III3e781062018-10-19 17:18:34 -0700101 exit(1);
Brad Bishopf138c562017-06-15 23:08:09 -0400102}
103
104} // namespace util
105} // namespace fan
106} // namespace phosphor
107
108int main(int argc, char* argv[])
109{
110 using namespace phosphor::fan::util;
111
112 auto options = std::make_unique<ArgumentParser>(argc, argv);
113 auto path = (*options)["path"];
114 auto stype = (*options)["type"];
115 auto scode = (*options)["code"];
116 unsigned int type = EV_KEY;
117
118 if (path == ArgumentParser::empty_string)
119 {
120 exit_with_error("Path not specified or invalid.", argv);
121 }
122 if (stype != ArgumentParser::empty_string)
123 {
124 type = stoul(stype);
125 }
126
127 if (scode == ArgumentParser::empty_string)
128 {
129 exit_with_error("Keycode not specified or invalid.", argv);
130 }
131 options.reset();
132
Matthew Barthebd15372020-05-28 11:42:38 -0500133 // TODO https://github.com/openbmc/phosphor-fan-presence/issues/22
134 // auto loop = sdevent::event::newDefault();
Brad Bishopf138c562017-06-15 23:08:09 -0400135 phosphor::fan::util::FileDescriptor fd(
Matthew Barth11fc0a72020-05-26 10:55:54 -0500136 open(path.c_str(), O_RDONLY | O_NONBLOCK));
Brad Bishopf138c562017-06-15 23:08:09 -0400137 auto evdev = evdevpp::evdev::newFromFD(fd());
Matthew Barthebd15372020-05-28 11:42:38 -0500138 // sdevent::event::io::IO callback(loop, fd(), [&evdev](auto& s) {
139 // unsigned int type, code, value;
140 // std::tie(type, code, value) = evdev.next();
141 // std::cout << "type: " << libevdev_event_type_get_name(type)
142 // << " code: " << libevdev_event_code_get_name(type, code)
143 // << " value: " << value << "\n";
144 // });
Brad Bishopf138c562017-06-15 23:08:09 -0400145
146 auto value = evdev.fetch(type, stoul(scode));
Matthew Barth11fc0a72020-05-26 10:55:54 -0500147 std::cout << "type: " << libevdev_event_type_get_name(type)
148 << " code: " << libevdev_event_code_get_name(type, stoul(scode))
149 << " value: " << value << "\n";
Brad Bishopf138c562017-06-15 23:08:09 -0400150
Matthew Barthebd15372020-05-28 11:42:38 -0500151 // loop.loop();
Brad Bishopf138c562017-06-15 23:08:09 -0400152
153 return 0;
154}