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