blob: 82cbed47693a547b5f39aedc86c71689f15dfc18 [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>
Matt Spinlere3b859c2017-05-25 11:15:43 -050033#include <phosphor-logging/log.hpp>
Patrick Venturedace6802018-11-01 16:52:10 -070034#include <thread>
Matt Spinlere3b859c2017-05-25 11:15:43 -050035
36using namespace phosphor::gpio;
37using namespace phosphor::logging;
38
39typedef void (*gpioFunction)(GPIO&, unsigned int);
40using gpioFunctionMap = std::map<std::string, gpioFunction>;
41
42/**
43 * Sets a GPIO low
44 *
45 * @param[in] gpio - the GPIO object
46 * @param[in] delayInMS - Unused in this function
47 */
Brad Bishop0d686452019-10-21 12:11:09 -040048void low(GPIO& gpio, unsigned int)
Matt Spinlere3b859c2017-05-25 11:15:43 -050049{
50 gpio.set(GPIO::Value::low);
51}
52
Matt Spinlere3b859c2017-05-25 11:15:43 -050053/**
54 * Sets a GPIO high
55 *
56 * @param[in] gpio - the GPIO object
57 * @param[in] delayInMS - Unused in this function
58 */
Brad Bishop0d686452019-10-21 12:11:09 -040059void high(GPIO& gpio, unsigned int)
Matt Spinlere3b859c2017-05-25 11:15:43 -050060{
61 gpio.set(GPIO::Value::high);
62}
63
Matt Spinlere3b859c2017-05-25 11:15:43 -050064/**
65 * Sets a GPIO high, then delays, then sets it low
66 *
67 * @param[in] gpio - the GPIO object
68 * @param[in] delayInMS - The delay in between the sets
69 */
70void highLow(GPIO& gpio, unsigned int delayInMS)
71{
72 gpio.set(GPIO::Value::high);
73
74 std::chrono::milliseconds delay{delayInMS};
75 std::this_thread::sleep_for(delay);
76
77 gpio.set(GPIO::Value::low);
78}
79
Matt Spinlere3b859c2017-05-25 11:15:43 -050080/**
81 * Sets a GPIO low, then delays, then sets it high
82 *
83 * @param[in] gpio - the GPIO to write
84 * @param[in] delayInMS - The delay in between the sets
85 */
86void lowHigh(GPIO& gpio, unsigned int delayInMS)
87{
88 gpio.set(GPIO::Value::low);
89
90 std::chrono::milliseconds delay{delayInMS};
91 std::this_thread::sleep_for(delay);
92
93 gpio.set(GPIO::Value::high);
94}
95
Matt Spinlere3b859c2017-05-25 11:15:43 -050096/**
97 * The actions supported by this program
98 */
Patrick Venturedace6802018-11-01 16:52:10 -070099static const gpioFunctionMap functions{
100 {"low", low}, {"high", high}, {"low_high", lowHigh}, {"high_low", highLow}};
Matt Spinlere3b859c2017-05-25 11:15:43 -0500101
102/**
103 * Prints usage and exits the program
104 *
105 * @param[in] err - the error message to print
106 * @param[in] argv - argv from main()
107 */
108void exitWithError(const char* err, char** argv)
109{
110 std::cerr << "ERROR: " << err << "\n";
111 ArgumentParser::usage(argv);
112 exit(EXIT_FAILURE);
113}
114
Matt Spinlere3b859c2017-05-25 11:15:43 -0500115/**
116 * Returns the number value of the argument passed in.
117 *
118 * @param[in] name - the argument name
119 * @param[in] parser - the argument parser
120 * @param[in] argv - arv from main()
121 */
Patrick Venturedace6802018-11-01 16:52:10 -0700122template <typename T>
123T getValueFromArg(const char* name, ArgumentParser& parser, char** argv)
Matt Spinlere3b859c2017-05-25 11:15:43 -0500124{
125 char* p = NULL;
126 auto val = strtol(parser[name].c_str(), &p, 10);
127
Patrick Venturedace6802018-11-01 16:52:10 -0700128 // strol sets p on error, also we don't allow negative values
Matt Spinlere3b859c2017-05-25 11:15:43 -0500129 if (*p || (val < 0))
130 {
131 using namespace std::string_literals;
132 std::string msg = "Invalid "s + name + " value passed in";
133 exitWithError(msg.c_str(), argv);
134 }
135 return static_cast<T>(val);
136}
137
Matt Spinler936a5042017-05-25 10:17:13 -0500138int main(int argc, char** argv)
139{
Matt Spinlere3b859c2017-05-25 11:15:43 -0500140 ArgumentParser args(argc, argv);
141
142 auto path = args["path"];
143 if (path == ArgumentParser::emptyString)
144 {
145 exitWithError("GPIO device path not specified", argv);
146 }
147
148 auto action = args["action"];
149 if (action == ArgumentParser::emptyString)
150 {
151 exitWithError("Action not specified", argv);
152 }
153
154 if (args["gpio"] == ArgumentParser::emptyString)
155 {
156 exitWithError("GPIO not specified", argv);
157 }
158
159 auto gpioNum = getValueFromArg<gpioNum_t>("gpio", args, argv);
160
Patrick Venturedace6802018-11-01 16:52:10 -0700161 // Not all actions require a delay, so not required
Matt Spinlere3b859c2017-05-25 11:15:43 -0500162 unsigned int delay = 0;
163 if (args["delay"] != ArgumentParser::emptyString)
164 {
165 delay = getValueFromArg<decltype(delay)>("delay", args, argv);
166 }
167
168 auto function = functions.find(action);
169 if (function == functions.end())
170 {
171 exitWithError("Invalid action value passed in", argv);
172 }
173
174 GPIO gpio{path, gpioNum, GPIO::Direction::output};
175
176 try
177 {
178 function->second(gpio, delay);
179 }
Patrick Williams67554142021-10-06 13:00:15 -0500180 catch (const std::runtime_error& e)
Matt Spinlere3b859c2017-05-25 11:15:43 -0500181 {
182 std::cerr << e.what();
183 return -1;
184 }
185
Matt Spinler936a5042017-05-25 10:17:13 -0500186 return 0;
187}