blob: ecb9402c0c5c7c57fb6254d874ebb237af796b61 [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
Naveen Mosesa1af3292021-12-15 11:47:01 +053017#include "button_factory.hpp"
Naveen Mosesdd5495c2021-12-03 22:40:46 +053018#include "gpio.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
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053027nlohmann::json gpioDefs;
28
George Liu94afa4b2022-06-20 13:36:43 +080029int main(void)
Kuiying Wanga9d39e32018-08-14 13:47:32 +080030{
31 int ret = 0;
32
Naveen Mosesa6d4e652022-04-13 19:27:25 +053033 lg2::info("Start Phosphor buttons service...");
Kuiying Wanga9d39e32018-08-14 13:47:32 +080034
35 sd_event* event = nullptr;
36 ret = sd_event_default(&event);
37 if (ret < 0)
38 {
Naveen Mosesa6d4e652022-04-13 19:27:25 +053039 lg2::error("Error creating a default sd_event handler");
Kuiying Wanga9d39e32018-08-14 13:47:32 +080040 return ret;
41 }
42 EventPtr eventP{event};
43 event = nullptr;
44
Patrick Williams9a529a62022-07-22 19:26:54 -050045 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
46 sdbusplus::server::manager_t objManager{
Kuiying Wanga9d39e32018-08-14 13:47:32 +080047 bus, "/xyz/openbmc_project/Chassis/Buttons"};
48
49 bus.request_name("xyz.openbmc_project.Chassis.Buttons");
Naveen Mosesa1af3292021-12-15 11:47:01 +053050 std::vector<std::unique_ptr<ButtonIface>> buttonInterfaces;
Kuiying Wanga9d39e32018-08-14 13:47:32 +080051
Naveen Mosesdd5495c2021-12-03 22:40:46 +053052 std::ifstream gpios{gpioDefFile};
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053053 auto gpioDefJson = nlohmann::json::parse(gpios, nullptr, true);
54 gpioDefs = gpioDefJson["gpio_definitions"];
Naveen Mosesdd5495c2021-12-03 22:40:46 +053055
56 // load gpio config from gpio defs json file and create button interface
57 // objects based on the button form factor type
Naveen Mosesdd5495c2021-12-03 22:40:46 +053058
Naveen Moseseea8a4a2022-02-18 01:14:15 +053059 for (const auto& gpioConfig : gpioDefs)
Matt Spinler8605bdf2018-11-05 14:55:46 -060060 {
Naveen Moseseea8a4a2022-02-18 01:14:15 +053061 std::string formFactorName = gpioConfig["name"];
Naveen Mosesdd5495c2021-12-03 22:40:46 +053062 buttonConfig buttonCfg;
Naveen Moseseea8a4a2022-02-18 01:14:15 +053063 buttonCfg.formFactorName = formFactorName;
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053064 buttonCfg.extraJsonInfo = gpioConfig;
Naveen Mosesdd5495c2021-12-03 22:40:46 +053065
Naveen Moseseea8a4a2022-02-18 01:14:15 +053066 /* The folloing code checks if the gpio config read
67 from json file is single gpio config or group gpio config,
68 based on that further data is processed. */
Naveen Mosesa6d4e652022-04-13 19:27:25 +053069 lg2::debug("Found button config : {FORM_FACTOR_NAME}",
70 "FORM_FACTOR_NAME", buttonCfg.formFactorName);
Naveen Moseseea8a4a2022-02-18 01:14:15 +053071 if (gpioConfig.contains("group_gpio_config"))
72 {
73 const auto& groupGpio = gpioConfig["group_gpio_config"];
74
75 for (const auto& config : groupGpio)
76 {
77 gpioInfo gpioCfg;
78 gpioCfg.number = getGpioNum(config["pin"]);
79 gpioCfg.direction = config["direction"];
Naveen Mosesd219fa32022-07-20 00:01:46 +053080 gpioCfg.name = config["name"];
81 gpioCfg.polarity = (config["polarity"] == "active_high")
82 ? GpioPolarity::activeHigh
83 : GpioPolarity::activeLow;
Naveen Moseseea8a4a2022-02-18 01:14:15 +053084 buttonCfg.gpios.push_back(gpioCfg);
85 }
86 }
87 else
Naveen Mosesdd5495c2021-12-03 22:40:46 +053088 {
89 gpioInfo gpioCfg;
90 gpioCfg.number = getGpioNum(gpioConfig["pin"]);
91 gpioCfg.direction = gpioConfig["direction"];
Naveen Mosesdd5495c2021-12-03 22:40:46 +053092 buttonCfg.gpios.push_back(gpioCfg);
93 }
Naveen Moseseea8a4a2022-02-18 01:14:15 +053094 auto tempButtonIf = ButtonFactory::instance().createInstance(
95 formFactorName, bus, eventP, buttonCfg);
96 /* There are additional gpio configs present in some platforms
97 that are not supported in phosphor-buttons.
98 But they may be used by other applications. so skipping such configs
99 if present in gpio_defs.json file*/
100 if (tempButtonIf)
101 {
102 buttonInterfaces.emplace_back(std::move(tempButtonIf));
103 }
Matt Spinler8605bdf2018-11-05 14:55:46 -0600104 }
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800105
106 try
107 {
108 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
109 ret = sd_event_loop(eventP.get());
110 if (ret < 0)
111 {
Naveen Mosesa6d4e652022-04-13 19:27:25 +0530112 lg2::error("Error occurred during the sd_event_loop : {RESULT}",
113 "RESULT", ret);
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800114 }
115 }
Patrick Williams6d724ce2021-10-06 12:40:26 -0500116 catch (const std::exception& e)
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800117 {
118 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
119 ret = -1;
120 }
121 return ret;
122}