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> 20 #include <boost/asio/io_context.hpp> 21 #include <nlohmann/json.hpp> 22 #include <phosphor-logging/lg2.hpp> 23 24 #include <fstream> 25 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 { 44 boost::asio::io_context io; 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 } 60 catch (const CLI::Error& e) 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 { 69 lg2::error("GPIO monitor config file not found: {FILE}", "FILE", 70 gpioFileName); 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 { 82 /* GPIO Line message */ 83 std::string lineMsg = "GPIO Line "; 84 85 /* GPIO line */ 86 gpiod_line* line = NULL; 87 88 /* Log message string */ 89 std::string errMsg; 90 91 /* GPIO line configuration, default to monitor both edge */ 92 struct gpiod_line_request_config config 93 { 94 "gpio_monitor", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES, 0 95 }; 96 97 /* flag to monitor */ 98 bool flag = false; 99 100 /* target to start */ 101 std::string target; 102 103 /* multi targets to start */ 104 std::map<std::string, std::vector<std::string>> targets; 105 106 if (obj.find("LineName") == obj.end()) 107 { 108 /* If there is no line Name defined then gpio num nd chip 109 * id must be defined. GpioNum is integer mapping to the 110 * GPIO key configured by the kernel 111 */ 112 if (obj.find("GpioNum") == obj.end() || 113 obj.find("ChipId") == obj.end()) 114 { 115 lg2::error("Failed to find line name or gpio number: {FILE}", 116 "FILE", gpioFileName); 117 return -1; 118 } 119 120 std::string chipIdStr = obj["ChipId"]; 121 int gpioNum = obj["GpioNum"]; 122 123 lineMsg += std::to_string(gpioNum); 124 125 /* Get the GPIO line */ 126 line = gpiod_line_get(chipIdStr.c_str(), gpioNum); 127 } 128 else 129 { 130 /* Find the GPIO line */ 131 std::string lineName = obj["LineName"]; 132 lineMsg += lineName; 133 line = gpiod_line_find(lineName.c_str()); 134 } 135 136 if (line == NULL) 137 { 138 lg2::error("Failed to find the {GPIO}", "GPIO", errMsg); 139 return -1; 140 } 141 142 /* Get event to be monitored, if it is not defined then 143 * Both rising falling edge will be monitored. 144 */ 145 if (obj.find("EventMon") != obj.end()) 146 { 147 std::string eventStr = obj["EventMon"]; 148 auto findEvent = phosphor::gpio::polarityMap.find(eventStr); 149 if (findEvent == phosphor::gpio::polarityMap.end()) 150 { 151 lg2::error("{GPIO}: event missing: {EVENT}", "GPIO", lineMsg, 152 "EVENT", eventStr); 153 return -1; 154 } 155 156 config.request_type = findEvent->second; 157 } 158 159 /* Get flag if monitoring needs to continue after first event */ 160 if (obj.find("Continue") != obj.end()) 161 { 162 flag = obj["Continue"]; 163 } 164 165 /* Parse out target argument. It is fine if the user does not 166 * pass this if they are not interested in calling into any target 167 * on meeting a condition. 168 */ 169 if (obj.find("Target") != obj.end()) 170 { 171 target = obj["Target"]; 172 } 173 174 /* Parse out the targets argument if multi-targets are needed.*/ 175 if (obj.find("Targets") != obj.end()) 176 { 177 obj.at("Targets").get_to(targets); 178 } 179 180 /* Create a monitor object and let it do all the rest */ 181 gpios.push_back(std::make_unique<phosphor::gpio::GpioMonitor>( 182 line, config, io, target, targets, lineMsg, flag)); 183 } 184 io.run(); 185 186 return 0; 187 } 188