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 | |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 29 | #include <algorithm> |
| 30 | #include <chrono> |
| 31 | #include <iostream> |
| 32 | #include <map> |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 33 | #include <phosphor-logging/log.hpp> |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 34 | #include <thread> |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 35 | |
| 36 | using namespace phosphor::gpio; |
| 37 | using namespace phosphor::logging; |
| 38 | |
| 39 | typedef void (*gpioFunction)(GPIO&, unsigned int); |
| 40 | using 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 Bishop | 0d68645 | 2019-10-21 12:11:09 -0400 | [diff] [blame] | 48 | void low(GPIO& gpio, unsigned int) |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 49 | { |
| 50 | gpio.set(GPIO::Value::low); |
| 51 | } |
| 52 | |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 53 | /** |
| 54 | * Sets a GPIO high |
| 55 | * |
| 56 | * @param[in] gpio - the GPIO object |
| 57 | * @param[in] delayInMS - Unused in this function |
| 58 | */ |
Brad Bishop | 0d68645 | 2019-10-21 12:11:09 -0400 | [diff] [blame] | 59 | void high(GPIO& gpio, unsigned int) |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 60 | { |
| 61 | gpio.set(GPIO::Value::high); |
| 62 | } |
| 63 | |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 64 | /** |
| 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 | */ |
| 70 | void 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 Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 80 | /** |
| 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 | */ |
| 86 | void 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 Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 96 | /** |
| 97 | * The actions supported by this program |
| 98 | */ |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 99 | static const gpioFunctionMap functions{ |
| 100 | {"low", low}, {"high", high}, {"low_high", lowHigh}, {"high_low", highLow}}; |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 101 | |
| 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 | */ |
| 108 | void exitWithError(const char* err, char** argv) |
| 109 | { |
| 110 | std::cerr << "ERROR: " << err << "\n"; |
| 111 | ArgumentParser::usage(argv); |
| 112 | exit(EXIT_FAILURE); |
| 113 | } |
| 114 | |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 115 | /** |
| 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 Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 122 | template <typename T> |
| 123 | T getValueFromArg(const char* name, ArgumentParser& parser, char** argv) |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 124 | { |
| 125 | char* p = NULL; |
| 126 | auto val = strtol(parser[name].c_str(), &p, 10); |
| 127 | |
Patrick Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 128 | // strol sets p on error, also we don't allow negative values |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 129 | 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 Spinler | 936a504 | 2017-05-25 10:17:13 -0500 | [diff] [blame] | 138 | int main(int argc, char** argv) |
| 139 | { |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 140 | 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 Venture | dace680 | 2018-11-01 16:52:10 -0700 | [diff] [blame] | 161 | // Not all actions require a delay, so not required |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 162 | 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 Williams | 6755414 | 2021-10-06 13:00:15 -0500 | [diff] [blame^] | 180 | catch (const std::runtime_error& e) |
Matt Spinler | e3b859c | 2017-05-25 11:15:43 -0500 | [diff] [blame] | 181 | { |
| 182 | std::cerr << e.what(); |
| 183 | return -1; |
| 184 | } |
| 185 | |
Matt Spinler | 936a504 | 2017-05-25 10:17:13 -0500 | [diff] [blame] | 186 | return 0; |
| 187 | } |