1939a6431SVijay Khemka /** 2939a6431SVijay Khemka * Copyright © 2019 Facebook 3939a6431SVijay Khemka * 4939a6431SVijay Khemka * Licensed under the Apache License, Version 2.0 (the "License"); 5939a6431SVijay Khemka * you may not use this file except in compliance with the License. 6939a6431SVijay Khemka * You may obtain a copy of the License at 7939a6431SVijay Khemka * 8939a6431SVijay Khemka * http://www.apache.org/licenses/LICENSE-2.0 9939a6431SVijay Khemka * 10939a6431SVijay Khemka * Unless required by applicable law or agreed to in writing, software 11939a6431SVijay Khemka * distributed under the License is distributed on an "AS IS" BASIS, 12939a6431SVijay Khemka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13939a6431SVijay Khemka * See the License for the specific language governing permissions and 14939a6431SVijay Khemka * limitations under the License. 15939a6431SVijay Khemka */ 16939a6431SVijay Khemka 17939a6431SVijay Khemka #include "gpioMon.hpp" 18939a6431SVijay Khemka 19939a6431SVijay Khemka #include <CLI/CLI.hpp> 20854404e0SEd Tanous #include <boost/asio/io_context.hpp> 21939a6431SVijay Khemka #include <nlohmann/json.hpp> 22939a6431SVijay Khemka #include <phosphor-logging/log.hpp> 23939a6431SVijay Khemka 24*39084b4aSPatrick Williams #include <fstream> 25*39084b4aSPatrick Williams 26939a6431SVijay Khemka using namespace phosphor::logging; 27939a6431SVijay Khemka 28939a6431SVijay Khemka namespace phosphor 29939a6431SVijay Khemka { 30939a6431SVijay Khemka namespace gpio 31939a6431SVijay Khemka { 32939a6431SVijay Khemka 33939a6431SVijay Khemka std::map<std::string, int> polarityMap = { 34939a6431SVijay Khemka /**< Only watch falling edge events. */ 35939a6431SVijay Khemka {"FALLING", GPIOD_LINE_REQUEST_EVENT_FALLING_EDGE}, 36939a6431SVijay Khemka /**< Only watch rising edge events. */ 37939a6431SVijay Khemka {"RISING", GPIOD_LINE_REQUEST_EVENT_RISING_EDGE}, 38939a6431SVijay Khemka /**< Monitor both types of events. */ 39939a6431SVijay Khemka {"BOTH", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES}}; 40939a6431SVijay Khemka 41939a6431SVijay Khemka } 42939a6431SVijay Khemka } // namespace phosphor 43939a6431SVijay Khemka 44939a6431SVijay Khemka int main(int argc, char** argv) 45939a6431SVijay Khemka { 46854404e0SEd Tanous boost::asio::io_context io; 47939a6431SVijay Khemka 48939a6431SVijay Khemka CLI::App app{"Monitor GPIO line for requested state change"}; 49939a6431SVijay Khemka 50939a6431SVijay Khemka std::string gpioFileName; 51939a6431SVijay Khemka 52939a6431SVijay Khemka /* Add an input option */ 53939a6431SVijay Khemka app.add_option("-c,--config", gpioFileName, "Name of config json file") 54939a6431SVijay Khemka ->required() 55939a6431SVijay Khemka ->check(CLI::ExistingFile); 56939a6431SVijay Khemka 57939a6431SVijay Khemka /* Parse input parameter */ 58939a6431SVijay Khemka try 59939a6431SVijay Khemka { 60939a6431SVijay Khemka app.parse(argc, argv); 61939a6431SVijay Khemka } 6267554144SPatrick Williams catch (const CLI::Error& e) 63939a6431SVijay Khemka { 64939a6431SVijay Khemka return app.exit(e); 65939a6431SVijay Khemka } 66939a6431SVijay Khemka 67939a6431SVijay Khemka /* Get list of gpio config details from json file */ 68939a6431SVijay Khemka std::ifstream file(gpioFileName); 69939a6431SVijay Khemka if (!file) 70939a6431SVijay Khemka { 71939a6431SVijay Khemka log<level::ERR>("GPIO monitor config file not found", 72939a6431SVijay Khemka entry("GPIO_MON_FILE=%s", gpioFileName.c_str())); 73939a6431SVijay Khemka return -1; 74939a6431SVijay Khemka } 75939a6431SVijay Khemka 76939a6431SVijay Khemka nlohmann::json gpioMonObj; 77939a6431SVijay Khemka file >> gpioMonObj; 78939a6431SVijay Khemka file.close(); 79939a6431SVijay Khemka 80939a6431SVijay Khemka std::vector<std::unique_ptr<phosphor::gpio::GpioMonitor>> gpios; 81939a6431SVijay Khemka 82939a6431SVijay Khemka for (auto& obj : gpioMonObj) 83939a6431SVijay Khemka { 84939a6431SVijay Khemka /* GPIO Line message */ 85939a6431SVijay Khemka std::string lineMsg = "GPIO Line "; 86939a6431SVijay Khemka 87939a6431SVijay Khemka /* GPIO line */ 88939a6431SVijay Khemka gpiod_line* line = NULL; 89939a6431SVijay Khemka 90939a6431SVijay Khemka /* Log message string */ 91939a6431SVijay Khemka std::string errMsg; 92939a6431SVijay Khemka 93939a6431SVijay Khemka /* GPIO line configuration, default to monitor both edge */ 94939a6431SVijay Khemka struct gpiod_line_request_config config 95939a6431SVijay Khemka { 96939a6431SVijay Khemka "gpio_monitor", GPIOD_LINE_REQUEST_EVENT_BOTH_EDGES, 0 97939a6431SVijay Khemka }; 98939a6431SVijay Khemka 99939a6431SVijay Khemka /* flag to monitor */ 100939a6431SVijay Khemka bool flag = false; 101939a6431SVijay Khemka 102939a6431SVijay Khemka /* target to start */ 103939a6431SVijay Khemka std::string target; 104939a6431SVijay Khemka 105a66ac0fcSDelphine CC Chiu /* multi targets to start */ 106a66ac0fcSDelphine CC Chiu std::map<std::string, std::vector<std::string>> targets; 107a66ac0fcSDelphine CC Chiu 108939a6431SVijay Khemka if (obj.find("LineName") == obj.end()) 109939a6431SVijay Khemka { 110939a6431SVijay Khemka /* If there is no line Name defined then gpio num nd chip 111939a6431SVijay Khemka * id must be defined. GpioNum is integer mapping to the 112939a6431SVijay Khemka * GPIO key configured by the kernel 113939a6431SVijay Khemka */ 114939a6431SVijay Khemka if (obj.find("GpioNum") == obj.end() || 115939a6431SVijay Khemka obj.find("ChipId") == obj.end()) 116939a6431SVijay Khemka { 117939a6431SVijay Khemka log<level::ERR>( 118939a6431SVijay Khemka "Failed to find line name or gpio number", 119939a6431SVijay Khemka entry("GPIO_JSON_FILE_NAME=%s", gpioFileName.c_str())); 120939a6431SVijay Khemka return -1; 121939a6431SVijay Khemka } 122939a6431SVijay Khemka 123939a6431SVijay Khemka std::string chipIdStr = obj["ChipId"]; 124939a6431SVijay Khemka int gpioNum = obj["GpioNum"]; 125939a6431SVijay Khemka 126939a6431SVijay Khemka lineMsg += std::to_string(gpioNum); 127939a6431SVijay Khemka 128939a6431SVijay Khemka /* Get the GPIO line */ 129939a6431SVijay Khemka line = gpiod_line_get(chipIdStr.c_str(), gpioNum); 130939a6431SVijay Khemka } 131939a6431SVijay Khemka else 132939a6431SVijay Khemka { 133939a6431SVijay Khemka /* Find the GPIO line */ 134939a6431SVijay Khemka std::string lineName = obj["LineName"]; 135939a6431SVijay Khemka lineMsg += lineName; 136939a6431SVijay Khemka line = gpiod_line_find(lineName.c_str()); 137939a6431SVijay Khemka } 138939a6431SVijay Khemka 139939a6431SVijay Khemka if (line == NULL) 140939a6431SVijay Khemka { 141939a6431SVijay Khemka errMsg = "Failed to find the " + lineMsg; 142939a6431SVijay Khemka log<level::ERR>(errMsg.c_str()); 143939a6431SVijay Khemka return -1; 144939a6431SVijay Khemka } 145939a6431SVijay Khemka 146939a6431SVijay Khemka /* Get event to be monitored, if it is not defined then 147939a6431SVijay Khemka * Both rising falling edge will be monitored. 148939a6431SVijay Khemka */ 149939a6431SVijay Khemka if (obj.find("EventMon") != obj.end()) 150939a6431SVijay Khemka { 151939a6431SVijay Khemka std::string eventStr = obj["EventMon"]; 152939a6431SVijay Khemka auto findEvent = phosphor::gpio::polarityMap.find(eventStr); 153939a6431SVijay Khemka if (findEvent == phosphor::gpio::polarityMap.end()) 154939a6431SVijay Khemka { 155939a6431SVijay Khemka errMsg = "Incorrect GPIO monitor event defined " + lineMsg; 156939a6431SVijay Khemka log<level::ERR>(errMsg.c_str()); 157939a6431SVijay Khemka return -1; 158939a6431SVijay Khemka } 159939a6431SVijay Khemka 160939a6431SVijay Khemka config.request_type = findEvent->second; 161939a6431SVijay Khemka } 162939a6431SVijay Khemka 163939a6431SVijay Khemka /* Get flag if monitoring needs to continue after first event */ 164939a6431SVijay Khemka if (obj.find("Continue") != obj.end()) 165939a6431SVijay Khemka { 166939a6431SVijay Khemka flag = obj["Continue"]; 167939a6431SVijay Khemka } 168939a6431SVijay Khemka 169939a6431SVijay Khemka /* Parse out target argument. It is fine if the user does not 170939a6431SVijay Khemka * pass this if they are not interested in calling into any target 171939a6431SVijay Khemka * on meeting a condition. 172939a6431SVijay Khemka */ 173939a6431SVijay Khemka if (obj.find("Target") != obj.end()) 174939a6431SVijay Khemka { 175939a6431SVijay Khemka target = obj["Target"]; 176939a6431SVijay Khemka } 177939a6431SVijay Khemka 178a66ac0fcSDelphine CC Chiu /* Parse out the targets argument if multi-targets are needed.*/ 179a66ac0fcSDelphine CC Chiu if (obj.find("Targets") != obj.end()) 180a66ac0fcSDelphine CC Chiu { 181a66ac0fcSDelphine CC Chiu obj.at("Targets").get_to(targets); 182a66ac0fcSDelphine CC Chiu } 183a66ac0fcSDelphine CC Chiu 184939a6431SVijay Khemka /* Create a monitor object and let it do all the rest */ 185939a6431SVijay Khemka gpios.push_back(std::make_unique<phosphor::gpio::GpioMonitor>( 186a66ac0fcSDelphine CC Chiu line, config, io, target, targets, lineMsg, flag)); 187939a6431SVijay Khemka } 188939a6431SVijay Khemka io.run(); 189939a6431SVijay Khemka 190939a6431SVijay Khemka return 0; 191939a6431SVijay Khemka } 192