Matt Spinler | 936a504 | 2017-05-25 10:17:13 -0500 | [diff] [blame] | 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 | |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 17 | /** |
| 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 Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 26 | #include "argument.hpp" |
| 27 | #include "gpio.hpp" |
| 28 | |
Patrick Williams | 39084b4 | 2023-05-10 07:50:58 -0500 | [diff] [blame] | 29 | #include <phosphor-logging/log.hpp> |
| 30 | |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 31 | #include <algorithm> |
| 32 | #include <chrono> |
| 33 | #include <iostream> |
| 34 | #include <map> |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 35 | #include <thread> |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 36 | |
| 37 | using namespace phosphor::gpio; |
| 38 | using namespace phosphor::logging; |
| 39 | |
| 40 | typedef void (*gpioFunction)(GPIO&, unsigned int); |
| 41 | using gpioFunctionMap = std::map<std::string, gpioFunction>; |
| 42 | |
| 43 | /** |
| 44 | * Sets a GPIO low |
| 45 | * |
| 46 | * @param[in] gpio - the GPIO object |
| 47 | * @param[in] delayInMS - Unused in this function |
| 48 | */ |
Brad Bishop | 0d68645 | 2019-10-21 12:11:09 -0400 | [diff] [blame] | 49 | void low(GPIO& gpio, unsigned int) |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 50 | { |
| 51 | gpio.set(GPIO::Value::low); |
| 52 | } |
| 53 | |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 54 | /** |
| 55 | * Sets a GPIO high |
| 56 | * |
| 57 | * @param[in] gpio - the GPIO object |
| 58 | * @param[in] delayInMS - Unused in this function |
| 59 | */ |
Brad Bishop | 0d68645 | 2019-10-21 12:11:09 -0400 | [diff] [blame] | 60 | void high(GPIO& gpio, unsigned int) |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 61 | { |
| 62 | gpio.set(GPIO::Value::high); |
| 63 | } |
| 64 | |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 65 | /** |
| 66 | * Sets a GPIO high, then delays, then sets it low |
| 67 | * |
| 68 | * @param[in] gpio - the GPIO object |
| 69 | * @param[in] delayInMS - The delay in between the sets |
| 70 | */ |
| 71 | void highLow(GPIO& gpio, unsigned int delayInMS) |
| 72 | { |
| 73 | gpio.set(GPIO::Value::high); |
| 74 | |
| 75 | std::chrono::milliseconds delay{delayInMS}; |
| 76 | std::this_thread::sleep_for(delay); |
| 77 | |
| 78 | gpio.set(GPIO::Value::low); |
| 79 | } |
| 80 | |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 81 | /** |
| 82 | * Sets a GPIO low, then delays, then sets it high |
| 83 | * |
| 84 | * @param[in] gpio - the GPIO to write |
| 85 | * @param[in] delayInMS - The delay in between the sets |
| 86 | */ |
| 87 | void lowHigh(GPIO& gpio, unsigned int delayInMS) |
| 88 | { |
| 89 | gpio.set(GPIO::Value::low); |
| 90 | |
| 91 | std::chrono::milliseconds delay{delayInMS}; |
| 92 | std::this_thread::sleep_for(delay); |
| 93 | |
| 94 | gpio.set(GPIO::Value::high); |
| 95 | } |
| 96 | |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 97 | /** |
| 98 | * The actions supported by this program |
| 99 | */ |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 100 | static const gpioFunctionMap functions{ |
| 101 | {"low", low}, {"high", high}, {"low_high", lowHigh}, {"high_low", highLow}}; |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Prints usage and exits the program |
| 105 | * |
| 106 | * @param[in] err - the error message to print |
| 107 | * @param[in] argv - argv from main() |
| 108 | */ |
| 109 | void exitWithError(const char* err, char** argv) |
| 110 | { |
| 111 | std::cerr << "ERROR: " << err << "\n"; |
| 112 | ArgumentParser::usage(argv); |
| 113 | exit(EXIT_FAILURE); |
| 114 | } |
| 115 | |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 116 | /** |
| 117 | * Returns the number value of the argument passed in. |
| 118 | * |
| 119 | * @param[in] name - the argument name |
| 120 | * @param[in] parser - the argument parser |
| 121 | * @param[in] argv - arv from main() |
| 122 | */ |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 123 | template <typename T> |
| 124 | T getValueFromArg(const char* name, ArgumentParser& parser, char** argv) |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 125 | { |
| 126 | char* p = NULL; |
| 127 | auto val = strtol(parser[name].c_str(), &p, 10); |
| 128 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 129 | // strol sets p on error, also we don't allow negative values |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 130 | if (*p || (val < 0)) |
| 131 | { |
| 132 | using namespace std::string_literals; |
| 133 | std::string msg = "Invalid "s + name + " value passed in"; |
| 134 | exitWithError(msg.c_str(), argv); |
| 135 | } |
| 136 | return static_cast<T>(val); |
| 137 | } |
| 138 | |
Matt Spinler | 936a504 | 2017-05-25 10:17:13 -0500 | [diff] [blame] | 139 | int main(int argc, char** argv) |
| 140 | { |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 141 | ArgumentParser args(argc, argv); |
| 142 | |
| 143 | auto path = args["path"]; |
| 144 | if (path == ArgumentParser::emptyString) |
| 145 | { |
| 146 | exitWithError("GPIO device path not specified", argv); |
| 147 | } |
| 148 | |
| 149 | auto action = args["action"]; |
| 150 | if (action == ArgumentParser::emptyString) |
| 151 | { |
| 152 | exitWithError("Action not specified", argv); |
| 153 | } |
| 154 | |
| 155 | if (args["gpio"] == ArgumentParser::emptyString) |
| 156 | { |
| 157 | exitWithError("GPIO not specified", argv); |
| 158 | } |
| 159 | |
| 160 | auto gpioNum = getValueFromArg<gpioNum_t>("gpio", args, argv); |
| 161 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 162 | // Not all actions require a delay, so not required |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 163 | unsigned int delay = 0; |
| 164 | if (args["delay"] != ArgumentParser::emptyString) |
| 165 | { |
| 166 | delay = getValueFromArg<decltype(delay)>("delay", args, argv); |
| 167 | } |
| 168 | |
| 169 | auto function = functions.find(action); |
| 170 | if (function == functions.end()) |
| 171 | { |
| 172 | exitWithError("Invalid action value passed in", argv); |
| 173 | } |
| 174 | |
| 175 | GPIO gpio{path, gpioNum, GPIO::Direction::output}; |
| 176 | |
| 177 | try |
| 178 | { |
| 179 | function->second(gpio, delay); |
| 180 | } |
Patrick Williams | 6755414 | 2021-10-06 13:00:15 -0500 | [diff] [blame] | 181 | catch (const std::runtime_error& e) |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 182 | { |
| 183 | std::cerr << e.what(); |
| 184 | return -1; |
| 185 | } |
| 186 | |
Matt Spinler | 936a504 | 2017-05-25 10:17:13 -0500 | [diff] [blame] | 187 | return 0; |
| 188 | } |