blob: 624c9e7a6049dc8527a7cf78f9bab8e7f81ae6e9 [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>
George Liu5b98f4d2022-06-20 13:31:14 +080022
23#include <fstream>
Naveen Mosesdd5495c2021-12-03 22:40:46 +053024static constexpr auto gpioDefFile = "/etc/default/obmc/gpio/gpio_defs.json";
25
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053026using namespace phosphor::logging;
27nlohmann::json gpioDefs;
28
Kuiying Wanga9d39e32018-08-14 13:47:32 +080029int main(int argc, char* argv[])
30{
31 int ret = 0;
32
33 phosphor::logging::log<phosphor::logging::level::INFO>(
Naveen Mosesa1af3292021-12-15 11:47:01 +053034 "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 {
40 phosphor::logging::log<phosphor::logging::level::ERR>(
41 "Error creating a default sd_event handler");
42 return ret;
43 }
44 EventPtr eventP{event};
45 event = nullptr;
46
47 sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
48 sdbusplus::server::manager::manager objManager{
49 bus, "/xyz/openbmc_project/Chassis/Buttons"};
50
51 bus.request_name("xyz.openbmc_project.Chassis.Buttons");
Naveen Mosesa1af3292021-12-15 11:47:01 +053052 //
53 std::vector<std::unique_ptr<ButtonIface>> buttonInterfaces;
Kuiying Wanga9d39e32018-08-14 13:47:32 +080054
Naveen Mosesdd5495c2021-12-03 22:40:46 +053055 std::ifstream gpios{gpioDefFile};
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053056 auto gpioDefJson = nlohmann::json::parse(gpios, nullptr, true);
57 gpioDefs = gpioDefJson["gpio_definitions"];
Naveen Mosesdd5495c2021-12-03 22:40:46 +053058
59 // load gpio config from gpio defs json file and create button interface
60 // objects based on the button form factor type
Naveen Mosesdd5495c2021-12-03 22:40:46 +053061
Naveen Moseseea8a4a2022-02-18 01:14:15 +053062 for (const auto& gpioConfig : gpioDefs)
Matt Spinler8605bdf2018-11-05 14:55:46 -060063 {
Naveen Moseseea8a4a2022-02-18 01:14:15 +053064 std::string formFactorName = gpioConfig["name"];
Naveen Mosesdd5495c2021-12-03 22:40:46 +053065 buttonConfig buttonCfg;
Naveen Moseseea8a4a2022-02-18 01:14:15 +053066 buttonCfg.formFactorName = formFactorName;
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053067 buttonCfg.extraJsonInfo = gpioConfig;
Naveen Mosesdd5495c2021-12-03 22:40:46 +053068
Naveen Moseseea8a4a2022-02-18 01:14:15 +053069 /* The folloing code checks if the gpio config read
70 from json file is single gpio config or group gpio config,
71 based on that further data is processed. */
72 if (gpioConfig.contains("group_gpio_config"))
73 {
74 const auto& groupGpio = gpioConfig["group_gpio_config"];
75
76 for (const auto& config : groupGpio)
77 {
78 gpioInfo gpioCfg;
79 gpioCfg.number = getGpioNum(config["pin"]);
80 gpioCfg.direction = config["direction"];
81 buttonCfg.gpios.push_back(gpioCfg);
82 }
83 }
84 else
Naveen Mosesdd5495c2021-12-03 22:40:46 +053085 {
86 gpioInfo gpioCfg;
87 gpioCfg.number = getGpioNum(gpioConfig["pin"]);
88 gpioCfg.direction = gpioConfig["direction"];
Naveen Mosesdd5495c2021-12-03 22:40:46 +053089 buttonCfg.gpios.push_back(gpioCfg);
90 }
Naveen Moseseea8a4a2022-02-18 01:14:15 +053091 auto tempButtonIf = ButtonFactory::instance().createInstance(
92 formFactorName, bus, eventP, buttonCfg);
93 /* There are additional gpio configs present in some platforms
94 that are not supported in phosphor-buttons.
95 But they may be used by other applications. so skipping such configs
96 if present in gpio_defs.json file*/
97 if (tempButtonIf)
98 {
99 buttonInterfaces.emplace_back(std::move(tempButtonIf));
100 }
Matt Spinler8605bdf2018-11-05 14:55:46 -0600101 }
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800102
103 try
104 {
105 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
106 ret = sd_event_loop(eventP.get());
107 if (ret < 0)
108 {
109 phosphor::logging::log<phosphor::logging::level::ERR>(
110 "Error occurred during the sd_event_loop",
111 phosphor::logging::entry("RET=%d", ret));
112 }
113 }
Patrick Williams6d724ce2021-10-06 12:40:26 -0500114 catch (const std::exception& e)
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800115 {
116 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
117 ret = -1;
118 }
119 return ret;
120}