Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 Facebook |
| 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 | |
| 17 | #include "gpioMon.hpp" |
| 18 | |
| 19 | #include <CLI/CLI.hpp> |
Ed Tanous | 854404e | 2023-02-28 13:37:51 -0800 | [diff] [blame] | 20 | #include <boost/asio/io_context.hpp> |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 21 | #include <nlohmann/json.hpp> |
George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 22 | #include <phosphor-logging/lg2.hpp> |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 23 | |
Patrick Williams | 39084b4 | 2023-05-10 07:50:58 -0500 | [diff] [blame] | 24 | #include <fstream> |
| 25 | |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 26 | namespace phosphor |
| 27 | { |
| 28 | namespace gpio |
| 29 | { |
| 30 | |
| 31 | std::map<std::string, int> polarityMap = { |
| 32 | /**< Only watch falling edge events. */ |
| 33 | {"FALLING", GPIOD_LINE_REQUEST_EVENT_FALLING_EDGE}, |
| 34 | /**< Only watch rising edge events. */ |
| 35 | {"RISING", GPIOD_LINE_REQUEST_EVENT_RISING_EDGE}, |
| 36 | /**< Monitor both types of events. */ |
| 37 | {"BOTH", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES}}; |
| 38 | |
| 39 | } |
| 40 | } // namespace phosphor |
| 41 | |
| 42 | int main(int argc, char** argv) |
| 43 | { |
Ed Tanous | 854404e | 2023-02-28 13:37:51 -0800 | [diff] [blame] | 44 | boost::asio::io_context io; |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 45 | |
| 46 | CLI::App app{"Monitor GPIO line for requested state change"}; |
| 47 | |
| 48 | std::string gpioFileName; |
| 49 | |
| 50 | /* Add an input option */ |
| 51 | app.add_option("-c,--config", gpioFileName, "Name of config json file") |
| 52 | ->required() |
| 53 | ->check(CLI::ExistingFile); |
| 54 | |
| 55 | /* Parse input parameter */ |
| 56 | try |
| 57 | { |
| 58 | app.parse(argc, argv); |
| 59 | } |
Patrick Williams | 6755414 | 2021-10-06 13:00:15 -0500 | [diff] [blame] | 60 | catch (const CLI::Error& e) |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 61 | { |
| 62 | return app.exit(e); |
| 63 | } |
| 64 | |
| 65 | /* Get list of gpio config details from json file */ |
| 66 | std::ifstream file(gpioFileName); |
| 67 | if (!file) |
| 68 | { |
George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 69 | lg2::error("GPIO monitor config file not found: {FILE}", "FILE", |
| 70 | gpioFileName); |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 71 | return -1; |
| 72 | } |
| 73 | |
| 74 | nlohmann::json gpioMonObj; |
| 75 | file >> gpioMonObj; |
| 76 | file.close(); |
| 77 | |
| 78 | std::vector<std::unique_ptr<phosphor::gpio::GpioMonitor>> gpios; |
| 79 | |
| 80 | for (auto& obj : gpioMonObj) |
| 81 | { |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 82 | /* GPIO Line message */ |
| 83 | std::string lineMsg = "GPIO Line "; |
| 84 | |
| 85 | /* GPIO line */ |
Jayanth Othayoth | cf33c59 | 2024-12-18 07:27:03 -0600 | [diff] [blame] | 86 | gpiod_line* line = nullptr; |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 87 | |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 88 | /* GPIO line configuration, default to monitor both edge */ |
Patrick Williams | 1c88803 | 2024-12-18 11:21:41 -0500 | [diff] [blame^] | 89 | struct gpiod_line_request_config config{ |
| 90 | "gpio_monitor", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES, 0}; |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 91 | |
| 92 | /* flag to monitor */ |
| 93 | bool flag = false; |
| 94 | |
| 95 | /* target to start */ |
| 96 | std::string target; |
| 97 | |
Delphine CC Chiu | a66ac0f | 2023-01-09 17:12:23 +0800 | [diff] [blame] | 98 | /* multi targets to start */ |
| 99 | std::map<std::string, std::vector<std::string>> targets; |
| 100 | |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 101 | if (obj.find("LineName") == obj.end()) |
| 102 | { |
| 103 | /* If there is no line Name defined then gpio num nd chip |
| 104 | * id must be defined. GpioNum is integer mapping to the |
| 105 | * GPIO key configured by the kernel |
| 106 | */ |
| 107 | if (obj.find("GpioNum") == obj.end() || |
| 108 | obj.find("ChipId") == obj.end()) |
| 109 | { |
George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 110 | lg2::error("Failed to find line name or gpio number: {FILE}", |
| 111 | "FILE", gpioFileName); |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | std::string chipIdStr = obj["ChipId"]; |
| 116 | int gpioNum = obj["GpioNum"]; |
| 117 | |
| 118 | lineMsg += std::to_string(gpioNum); |
| 119 | |
| 120 | /* Get the GPIO line */ |
| 121 | line = gpiod_line_get(chipIdStr.c_str(), gpioNum); |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | /* Find the GPIO line */ |
| 126 | std::string lineName = obj["LineName"]; |
| 127 | lineMsg += lineName; |
| 128 | line = gpiod_line_find(lineName.c_str()); |
| 129 | } |
| 130 | |
Jayanth Othayoth | cf33c59 | 2024-12-18 07:27:03 -0600 | [diff] [blame] | 131 | if (line == nullptr) |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 132 | { |
Yang Chen | 38cd74c | 2024-11-07 14:42:24 +0800 | [diff] [blame] | 133 | lg2::error("Failed to find the {GPIO}", "GPIO", lineMsg); |
| 134 | continue; |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* Get event to be monitored, if it is not defined then |
| 138 | * Both rising falling edge will be monitored. |
| 139 | */ |
| 140 | if (obj.find("EventMon") != obj.end()) |
| 141 | { |
| 142 | std::string eventStr = obj["EventMon"]; |
| 143 | auto findEvent = phosphor::gpio::polarityMap.find(eventStr); |
| 144 | if (findEvent == phosphor::gpio::polarityMap.end()) |
| 145 | { |
George Liu | 2a8848c | 2023-08-01 13:49:28 +0800 | [diff] [blame] | 146 | lg2::error("{GPIO}: event missing: {EVENT}", "GPIO", lineMsg, |
| 147 | "EVENT", eventStr); |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 148 | return -1; |
| 149 | } |
| 150 | |
| 151 | config.request_type = findEvent->second; |
| 152 | } |
| 153 | |
| 154 | /* Get flag if monitoring needs to continue after first event */ |
| 155 | if (obj.find("Continue") != obj.end()) |
| 156 | { |
| 157 | flag = obj["Continue"]; |
| 158 | } |
| 159 | |
| 160 | /* Parse out target argument. It is fine if the user does not |
| 161 | * pass this if they are not interested in calling into any target |
| 162 | * on meeting a condition. |
| 163 | */ |
| 164 | if (obj.find("Target") != obj.end()) |
| 165 | { |
| 166 | target = obj["Target"]; |
| 167 | } |
| 168 | |
Delphine CC Chiu | a66ac0f | 2023-01-09 17:12:23 +0800 | [diff] [blame] | 169 | /* Parse out the targets argument if multi-targets are needed.*/ |
| 170 | if (obj.find("Targets") != obj.end()) |
| 171 | { |
| 172 | obj.at("Targets").get_to(targets); |
| 173 | } |
| 174 | |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 175 | /* Create a monitor object and let it do all the rest */ |
| 176 | gpios.push_back(std::make_unique<phosphor::gpio::GpioMonitor>( |
Delphine CC Chiu | a66ac0f | 2023-01-09 17:12:23 +0800 | [diff] [blame] | 177 | line, config, io, target, targets, lineMsg, flag)); |
Vijay Khemka | 939a643 | 2019-10-09 17:45:45 -0700 | [diff] [blame] | 178 | } |
| 179 | io.run(); |
| 180 | |
| 181 | return 0; |
| 182 | } |