blob: e1efd3e769ef30b021b682e83f70c3ba57e2ab52 [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
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053025using namespace phosphor::logging;
26nlohmann::json gpioDefs;
27
Kuiying Wanga9d39e32018-08-14 13:47:32 +080028int main(int argc, char* argv[])
29{
30 int ret = 0;
31
32 phosphor::logging::log<phosphor::logging::level::INFO>(
Naveen Mosesa1af3292021-12-15 11:47:01 +053033 "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 {
39 phosphor::logging::log<phosphor::logging::level::ERR>(
40 "Error creating a default sd_event handler");
41 return ret;
42 }
43 EventPtr eventP{event};
44 event = nullptr;
45
46 sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
47 sdbusplus::server::manager::manager objManager{
48 bus, "/xyz/openbmc_project/Chassis/Buttons"};
49
50 bus.request_name("xyz.openbmc_project.Chassis.Buttons");
Naveen Mosesa1af3292021-12-15 11:47:01 +053051 //
52 std::vector<std::unique_ptr<ButtonIface>> buttonInterfaces;
Kuiying Wanga9d39e32018-08-14 13:47:32 +080053
Naveen Mosesdd5495c2021-12-03 22:40:46 +053054 std::ifstream gpios{gpioDefFile};
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053055 auto gpioDefJson = nlohmann::json::parse(gpios, nullptr, true);
56 gpioDefs = gpioDefJson["gpio_definitions"];
Naveen Mosesdd5495c2021-12-03 22:40:46 +053057
58 // load gpio config from gpio defs json file and create button interface
59 // objects based on the button form factor type
Naveen Mosesdd5495c2021-12-03 22:40:46 +053060
Naveen Moseseea8a4a2022-02-18 01:14:15 +053061 for (const auto& gpioConfig : gpioDefs)
Matt Spinler8605bdf2018-11-05 14:55:46 -060062 {
Naveen Moseseea8a4a2022-02-18 01:14:15 +053063 std::string formFactorName = gpioConfig["name"];
Naveen Mosesdd5495c2021-12-03 22:40:46 +053064 buttonConfig buttonCfg;
Naveen Moseseea8a4a2022-02-18 01:14:15 +053065 buttonCfg.formFactorName = formFactorName;
Naveen Moses3bd1cfc2022-02-14 18:04:20 +053066 buttonCfg.extraJsonInfo = gpioConfig;
Naveen Mosesdd5495c2021-12-03 22:40:46 +053067
Naveen Moseseea8a4a2022-02-18 01:14:15 +053068 /* The folloing code checks if the gpio config read
69 from json file is single gpio config or group gpio config,
70 based on that further data is processed. */
71 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"];
80 buttonCfg.gpios.push_back(gpioCfg);
81 }
82 }
83 else
Naveen Mosesdd5495c2021-12-03 22:40:46 +053084 {
85 gpioInfo gpioCfg;
86 gpioCfg.number = getGpioNum(gpioConfig["pin"]);
87 gpioCfg.direction = gpioConfig["direction"];
Naveen Mosesdd5495c2021-12-03 22:40:46 +053088 buttonCfg.gpios.push_back(gpioCfg);
89 }
Naveen Moseseea8a4a2022-02-18 01:14:15 +053090 auto tempButtonIf = ButtonFactory::instance().createInstance(
91 formFactorName, bus, eventP, buttonCfg);
92 /* There are additional gpio configs present in some platforms
93 that are not supported in phosphor-buttons.
94 But they may be used by other applications. so skipping such configs
95 if present in gpio_defs.json file*/
96 if (tempButtonIf)
97 {
98 buttonInterfaces.emplace_back(std::move(tempButtonIf));
99 }
Matt Spinler8605bdf2018-11-05 14:55:46 -0600100 }
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800101
102 try
103 {
104 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
105 ret = sd_event_loop(eventP.get());
106 if (ret < 0)
107 {
108 phosphor::logging::log<phosphor::logging::level::ERR>(
109 "Error occurred during the sd_event_loop",
110 phosphor::logging::entry("RET=%d", ret));
111 }
112 }
Patrick Williams6d724ce2021-10-06 12:40:26 -0500113 catch (const std::exception& e)
Kuiying Wanga9d39e32018-08-14 13:47:32 +0800114 {
115 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
116 ret = -1;
117 }
118 return ret;
119}