blob: cf38e1fd3951ef98096d04ae27f5a3c7de2b582e [file] [log] [blame]
Kuiying Wanga9d39e32018-08-14 13:47:32 +08001/*
2// Copyright (c) 2018 Intel 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
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080017#include "button_config.hpp"
Naveen Mosesa1af3292021-12-15 11:47:01 +053018#include "button_factory.hpp"
Kuiying Wanga9d39e32018-08-14 13:47:32 +080019
Naveen Mosesdd5495c2021-12-03 22:40:46 +053020#include <nlohmann/json.hpp>
Naveen Mosesa1af3292021-12-15 11:47:01 +053021#include <phosphor-logging/elog-errors.hpp>
Naveen Mosesa6d4e652022-04-13 19:27:25 +053022#include <phosphor-logging/lg2.hpp>
George Liu5b98f4d2022-06-20 13:31:14 +080023
24#include <fstream>
Naveen Mosesdd5495c2021-12-03 22:40:46 +053025static constexpr auto gpioDefFile = "/etc/default/obmc/gpio/gpio_defs.json";
26
George Liu94afa4b2022-06-20 13:36:43 +080027int main(void)
Kuiying Wanga9d39e32018-08-14 13:47:32 +080028{
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080029 nlohmann::json gpioDefs;
30 nlohmann::json cpldDefs;
31
Kuiying Wanga9d39e32018-08-14 13:47:32 +080032 int ret = 0;
33
Naveen Mosesa6d4e652022-04-13 19:27:25 +053034 lg2::info("Start Phosphor buttons service...");
Kuiying Wanga9d39e32018-08-14 13:47:32 +080035
36 sd_event* event = nullptr;
37 ret = sd_event_default(&event);
38 if (ret < 0)
39 {
Naveen Mosesa6d4e652022-04-13 19:27:25 +053040 lg2::error("Error creating a default sd_event handler");
Kuiying Wanga9d39e32018-08-14 13:47:32 +080041 return ret;
42 }
43 EventPtr eventP{event};
44 event = nullptr;
45
Patrick Williams9a529a62022-07-22 19:26:54 -050046 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
47 sdbusplus::server::manager_t objManager{
Kuiying Wanga9d39e32018-08-14 13:47:32 +080048 bus, "/xyz/openbmc_project/Chassis/Buttons"};
49
50 bus.request_name("xyz.openbmc_project.Chassis.Buttons");
Naveen Mosesa1af3292021-12-15 11:47:01 +053051 std::vector<std::unique_ptr<ButtonIface>> buttonInterfaces;
Kuiying Wanga9d39e32018-08-14 13:47:32 +080052
Naveen Mosesdd5495c2021-12-03 22:40:46 +053053 std::ifstream gpios{gpioDefFile};
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080054 auto configDefJson = nlohmann::json::parse(gpios, nullptr, true);
55 gpioDefs = configDefJson["gpio_definitions"];
56 cpldDefs = configDefJson["cpld_definitions"];
57
58 // load cpld config from gpio defs json file and create button interface
59 for (const auto& cpldConfig : cpldDefs)
60 {
61 std::string formFactorName = cpldConfig["name"];
62
63 ButtonConfig buttonCfg;
64 buttonCfg.type = ConfigType::cpld;
65 buttonCfg.formFactorName = formFactorName;
66 buttonCfg.extraJsonInfo = cpldConfig;
67
68 CpldInfo cpldCfg;
69 cpldCfg.registerName = cpldConfig["register_name"];
70
71 cpldCfg.i2cAddress = cpldConfig["i2c_address"].get<int>();
72 cpldCfg.i2cBus = cpldConfig["i2c_bus"].get<int>();
73 buttonCfg.cpld = cpldCfg;
74
75 auto tempButtonIf = ButtonFactory::instance().createInstance(
76 formFactorName, bus, eventP, buttonCfg);
77 if (tempButtonIf)
78 {
79 buttonInterfaces.emplace_back(std::move(tempButtonIf));
80 }
81 }
Naveen Mosesdd5495c2021-12-03 22:40:46 +053082
83 // load gpio config from gpio defs json file and create button interface
84 // objects based on the button form factor type
Naveen Mosesdd5495c2021-12-03 22:40:46 +053085
Naveen Moseseea8a4a2022-02-18 01:14:15 +053086 for (const auto& gpioConfig : gpioDefs)
Matt Spinler8605bdf2018-11-05 14:55:46 -060087 {
Naveen Moseseea8a4a2022-02-18 01:14:15 +053088 std::string formFactorName = gpioConfig["name"];
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080089 ButtonConfig buttonCfg;
Naveen Moseseea8a4a2022-02-18 01:14:15 +053090 buttonCfg.formFactorName = formFactorName;
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053091 buttonCfg.extraJsonInfo = gpioConfig;
Delphine CC Chiuccd7db02023-02-09 14:48:53 +080092 buttonCfg.type = ConfigType::gpio;
Naveen Mosesdd5495c2021-12-03 22:40:46 +053093
Naveen Moseseea8a4a2022-02-18 01:14:15 +053094 /* The folloing code checks if the gpio config read
95 from json file is single gpio config or group gpio config,
96 based on that further data is processed. */
Naveen Mosesa6d4e652022-04-13 19:27:25 +053097 lg2::debug("Found button config : {FORM_FACTOR_NAME}",
98 "FORM_FACTOR_NAME", buttonCfg.formFactorName);
Naveen Moseseea8a4a2022-02-18 01:14:15 +053099 if (gpioConfig.contains("group_gpio_config"))
100 {
101 const auto& groupGpio = gpioConfig["group_gpio_config"];
102
103 for (const auto& config : groupGpio)
104 {
Delphine CC Chiuccd7db02023-02-09 14:48:53 +0800105 GpioInfo gpioCfg;
Naveen Moseseea8a4a2022-02-18 01:14:15 +0530106 gpioCfg.number = getGpioNum(config["pin"]);
107 gpioCfg.direction = config["direction"];
Naveen Mosesd219fa32022-07-20 00:01:46 +0530108 gpioCfg.name = config["name"];
109 gpioCfg.polarity = (config["polarity"] == "active_high")
110 ? GpioPolarity::activeHigh
111 : GpioPolarity::activeLow;
Naveen Moseseea8a4a2022-02-18 01:14:15 +0530112 buttonCfg.gpios.push_back(gpioCfg);
113 }
114 }
115 else
Naveen Mosesdd5495c2021-12-03 22:40:46 +0530116 {
Delphine CC Chiuccd7db02023-02-09 14:48:53 +0800117 GpioInfo gpioCfg;
Naveen Mosesdd5495c2021-12-03 22:40:46 +0530118 gpioCfg.number = getGpioNum(gpioConfig["pin"]);
119 gpioCfg.direction = gpioConfig["direction"];
Naveen Mosesdd5495c2021-12-03 22:40:46 +0530120 buttonCfg.gpios.push_back(gpioCfg);
121 }
Naveen Moseseea8a4a2022-02-18 01:14:15 +0530122 auto tempButtonIf = ButtonFactory::instance().createInstance(
123 formFactorName, bus, eventP, buttonCfg);
124 /* There are additional gpio configs present in some platforms
125 that are not supported in phosphor-buttons.
126 But they may be used by other applications. so skipping such configs
127 if present in gpio_defs.json file*/
128 if (tempButtonIf)
129 {
130 buttonInterfaces.emplace_back(std::move(tempButtonIf));
131 }
Matt Spinler8605bdf2018-11-05 14:55:46 -0600132 }
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800133
134 try
135 {
136 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
137 ret = sd_event_loop(eventP.get());
138 if (ret < 0)
139 {
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530140 lg2::error("Error occurred during the sd_event_loop : {RESULT}",
141 "RESULT", ret);
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800142 }
143 }
Patrick Williams6d724ce2021-10-06 12:40:26 -0500144 catch (const std::exception& e)
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800145 {
146 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
147 ret = -1;
148 }
149 return ret;
150}