blob: d7cdc3deb682f1c4814390732a3a997d292c0dd2 [file] [log] [blame]
Matt Spinler936a5042017-05-25 10:17:13 -05001/**
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
Matt Spinlere3b859c2017-05-25 11:15:43 -050017/**
18 * This program is a utility for accessing GPIOs.
19 * Actions:
20 * low: Set a GPIO low
21 * high: Set a GPIO high
22 * low_high: Set a GPIO low, delay if requested, set it high
23 * high_low: Set a GPIO high, delay if requested, set it low
24 */
25
Patrick Venturedace6802018-11-01 16:52:10 -070026#include "argument.hpp"
27#include "gpio.hpp"
28
Matt Spinlere3b859c2017-05-25 11:15:43 -050029#include <algorithm>
30#include <chrono>
31#include <iostream>
32#include <map>
Patrick Venturedace6802018-11-01 16:52:10 -070033#include <thread>
Matt Spinlere3b859c2017-05-25 11:15:43 -050034
35using namespace phosphor::gpio;
Matt Spinlere3b859c2017-05-25 11:15:43 -050036
37typedef void (*gpioFunction)(GPIO&, unsigned int);
38using gpioFunctionMap = std::map<std::string, gpioFunction>;
39
40/**
41 * Sets a GPIO low
42 *
43 * @param[in] gpio - the GPIO object
44 * @param[in] delayInMS - Unused in this function
45 */
Brad Bishop0d686452019-10-21 12:11:09 -040046void low(GPIO& gpio, unsigned int)
Matt Spinlere3b859c2017-05-25 11:15:43 -050047{
48 gpio.set(GPIO::Value::low);
49}
50
Matt Spinlere3b859c2017-05-25 11:15:43 -050051/**
52 * Sets a GPIO high
53 *
54 * @param[in] gpio - the GPIO object
55 * @param[in] delayInMS - Unused in this function
56 */
Brad Bishop0d686452019-10-21 12:11:09 -040057void high(GPIO& gpio, unsigned int)
Matt Spinlere3b859c2017-05-25 11:15:43 -050058{
59 gpio.set(GPIO::Value::high);
60}
61
Matt Spinlere3b859c2017-05-25 11:15:43 -050062/**
63 * Sets a GPIO high, then delays, then sets it low
64 *
65 * @param[in] gpio - the GPIO object
66 * @param[in] delayInMS - The delay in between the sets
67 */
68void highLow(GPIO& gpio, unsigned int delayInMS)
69{
70 gpio.set(GPIO::Value::high);
71
72 std::chrono::milliseconds delay{delayInMS};
73 std::this_thread::sleep_for(delay);
74
75 gpio.set(GPIO::Value::low);
76}
77
Matt Spinlere3b859c2017-05-25 11:15:43 -050078/**
79 * Sets a GPIO low, then delays, then sets it high
80 *
81 * @param[in] gpio - the GPIO to write
82 * @param[in] delayInMS - The delay in between the sets
83 */
84void lowHigh(GPIO& gpio, unsigned int delayInMS)
85{
86 gpio.set(GPIO::Value::low);
87
88 std::chrono::milliseconds delay{delayInMS};
89 std::this_thread::sleep_for(delay);
90
91 gpio.set(GPIO::Value::high);
92}
93
Matt Spinlere3b859c2017-05-25 11:15:43 -050094/**
95 * The actions supported by this program
96 */
Patrick Venturedace6802018-11-01 16:52:10 -070097static const gpioFunctionMap functions{
98 {"low", low}, {"high", high}, {"low_high", lowHigh}, {"high_low", highLow}};
Matt Spinlere3b859c2017-05-25 11:15:43 -050099
100/**
101 * Prints usage and exits the program
102 *
103 * @param[in] err - the error message to print
104 * @param[in] argv - argv from main()
105 */
106void exitWithError(const char* err, char** argv)
107{
108 std::cerr << "ERROR: " << err << "\n";
109 ArgumentParser::usage(argv);
110 exit(EXIT_FAILURE);
111}
112
Matt Spinlere3b859c2017-05-25 11:15:43 -0500113/**
114 * Returns the number value of the argument passed in.
115 *
116 * @param[in] name - the argument name
117 * @param[in] parser - the argument parser
118 * @param[in] argv - arv from main()
119 */
Patrick Venturedace6802018-11-01 16:52:10 -0700120template <typename T>
121T getValueFromArg(const char* name, ArgumentParser& parser, char** argv)
Matt Spinlere3b859c2017-05-25 11:15:43 -0500122{
123 char* p = NULL;
124 auto val = strtol(parser[name].c_str(), &p, 10);
125
Patrick Venturedace6802018-11-01 16:52:10 -0700126 // strol sets p on error, also we don't allow negative values
Matt Spinlere3b859c2017-05-25 11:15:43 -0500127 if (*p || (val < 0))
128 {
129 using namespace std::string_literals;
130 std::string msg = "Invalid "s + name + " value passed in";
131 exitWithError(msg.c_str(), argv);
132 }
133 return static_cast<T>(val);
134}
135
Matt Spinler936a5042017-05-25 10:17:13 -0500136int main(int argc, char** argv)
137{
Matt Spinlere3b859c2017-05-25 11:15:43 -0500138 ArgumentParser args(argc, argv);
139
140 auto path = args["path"];
141 if (path == ArgumentParser::emptyString)
142 {
143 exitWithError("GPIO device path not specified", argv);
144 }
145
146 auto action = args["action"];
147 if (action == ArgumentParser::emptyString)
148 {
149 exitWithError("Action not specified", argv);
150 }
151
152 if (args["gpio"] == ArgumentParser::emptyString)
153 {
154 exitWithError("GPIO not specified", argv);
155 }
156
157 auto gpioNum = getValueFromArg<gpioNum_t>("gpio", args, argv);
158
Patrick Venturedace6802018-11-01 16:52:10 -0700159 // Not all actions require a delay, so not required
Matt Spinlere3b859c2017-05-25 11:15:43 -0500160 unsigned int delay = 0;
161 if (args["delay"] != ArgumentParser::emptyString)
162 {
163 delay = getValueFromArg<decltype(delay)>("delay", args, argv);
164 }
165
166 auto function = functions.find(action);
167 if (function == functions.end())
168 {
169 exitWithError("Invalid action value passed in", argv);
170 }
171
172 GPIO gpio{path, gpioNum, GPIO::Direction::output};
173
174 try
175 {
176 function->second(gpio, delay);
177 }
Patrick Williams67554142021-10-06 13:00:15 -0500178 catch (const std::runtime_error& e)
Matt Spinlere3b859c2017-05-25 11:15:43 -0500179 {
180 std::cerr << e.what();
181 return -1;
182 }
183
Matt Spinler936a5042017-05-25 10:17:13 -0500184 return 0;
185}