blob: cfa224ee29c3c12fbc15a3e2c9eb94ebe11172f2 [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 <fstream>
21#include <nlohmann/json.hpp>
Naveen Mosesa1af3292021-12-15 11:47:01 +053022#include <phosphor-logging/elog-errors.hpp>
Naveen Mosesdd5495c2021-12-03 22:40:46 +053023static constexpr auto gpioDefFile = "/etc/default/obmc/gpio/gpio_defs.json";
24
Kuiying Wanga9d39e32018-08-14 13:47:32 +080025int main(int argc, char* argv[])
26{
27 int ret = 0;
28
29 phosphor::logging::log<phosphor::logging::level::INFO>(
Naveen Mosesa1af3292021-12-15 11:47:01 +053030 "Start Phosphor buttons service...");
Kuiying Wanga9d39e32018-08-14 13:47:32 +080031
32 sd_event* event = nullptr;
33 ret = sd_event_default(&event);
34 if (ret < 0)
35 {
36 phosphor::logging::log<phosphor::logging::level::ERR>(
37 "Error creating a default sd_event handler");
38 return ret;
39 }
40 EventPtr eventP{event};
41 event = nullptr;
42
43 sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
44 sdbusplus::server::manager::manager objManager{
45 bus, "/xyz/openbmc_project/Chassis/Buttons"};
46
47 bus.request_name("xyz.openbmc_project.Chassis.Buttons");
Naveen Mosesa1af3292021-12-15 11:47:01 +053048 //
49 std::vector<std::unique_ptr<ButtonIface>> buttonInterfaces;
Kuiying Wanga9d39e32018-08-14 13:47:32 +080050
Naveen Mosesdd5495c2021-12-03 22:40:46 +053051 std::ifstream gpios{gpioDefFile};
52 auto json = nlohmann::json::parse(gpios, nullptr, true);
53 auto gpioDefs = json["gpio_definitions"];
54
55 // load gpio config from gpio defs json file and create button interface
56 // objects based on the button form factor type
Naveen Mosesdd5495c2021-12-03 22:40:46 +053057
Naveen Moseseea8a4a2022-02-18 01:14:15 +053058 for (const auto& gpioConfig : gpioDefs)
Matt Spinler8605bdf2018-11-05 14:55:46 -060059 {
Naveen Moseseea8a4a2022-02-18 01:14:15 +053060 std::string formFactorName = gpioConfig["name"];
Naveen Mosesdd5495c2021-12-03 22:40:46 +053061 buttonConfig buttonCfg;
Naveen Moseseea8a4a2022-02-18 01:14:15 +053062 buttonCfg.formFactorName = formFactorName;
Naveen Mosesdd5495c2021-12-03 22:40:46 +053063
Naveen Moseseea8a4a2022-02-18 01:14:15 +053064 /* The folloing code checks if the gpio config read
65 from json file is single gpio config or group gpio config,
66 based on that further data is processed. */
67 if (gpioConfig.contains("group_gpio_config"))
68 {
69 const auto& groupGpio = gpioConfig["group_gpio_config"];
70
71 for (const auto& config : groupGpio)
72 {
73 gpioInfo gpioCfg;
74 gpioCfg.number = getGpioNum(config["pin"]);
75 gpioCfg.direction = config["direction"];
76 buttonCfg.gpios.push_back(gpioCfg);
77 }
78 }
79 else
Naveen Mosesdd5495c2021-12-03 22:40:46 +053080 {
81 gpioInfo gpioCfg;
82 gpioCfg.number = getGpioNum(gpioConfig["pin"]);
83 gpioCfg.direction = gpioConfig["direction"];
Naveen Mosesdd5495c2021-12-03 22:40:46 +053084 buttonCfg.gpios.push_back(gpioCfg);
85 }
Naveen Moseseea8a4a2022-02-18 01:14:15 +053086 auto tempButtonIf = ButtonFactory::instance().createInstance(
87 formFactorName, bus, eventP, buttonCfg);
88 /* There are additional gpio configs present in some platforms
89 that are not supported in phosphor-buttons.
90 But they may be used by other applications. so skipping such configs
91 if present in gpio_defs.json file*/
92 if (tempButtonIf)
93 {
94 buttonInterfaces.emplace_back(std::move(tempButtonIf));
95 }
Matt Spinler8605bdf2018-11-05 14:55:46 -060096 }
Kuiying Wanga9d39e32018-08-14 13:47:32 +080097
98 try
99 {
100 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
101 ret = sd_event_loop(eventP.get());
102 if (ret < 0)
103 {
104 phosphor::logging::log<phosphor::logging::level::ERR>(
105 "Error occurred during the sd_event_loop",
106 phosphor::logging::entry("RET=%d", ret));
107 }
108 }
Patrick Williams6d724ce2021-10-06 12:40:26 -0500109 catch (const std::exception& e)
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800110 {
111 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
112 ret = -1;
113 }
114 return ret;
115}