blob: a99c603aebef5d683edcad9cd4520338f0f51c81 [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
58 for (auto groupGpioConfig : gpioDefs)
Matt Spinler8605bdf2018-11-05 14:55:46 -060059 {
Naveen Mosesdd5495c2021-12-03 22:40:46 +053060 std::string formFactorName = groupGpioConfig["name"];
61 buttonConfig buttonCfg;
62 auto groupGpio = groupGpioConfig["gpio_config"];
63
64 for (auto gpioConfig : groupGpio)
65 {
66 gpioInfo gpioCfg;
67 gpioCfg.number = getGpioNum(gpioConfig["pin"]);
68 gpioCfg.direction = gpioConfig["direction"];
69 buttonCfg.formFactorName = formFactorName;
70 buttonCfg.gpios.push_back(gpioCfg);
71 }
Naveen Mosesdd5495c2021-12-03 22:40:46 +053072
Naveen Mosesa1af3292021-12-15 11:47:01 +053073 buttonInterfaces.emplace_back(ButtonFactory::instance().createInstance(
74 formFactorName, bus, eventP, buttonCfg));
Matt Spinler8605bdf2018-11-05 14:55:46 -060075 }
Kuiying Wanga9d39e32018-08-14 13:47:32 +080076
77 try
78 {
79 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
80 ret = sd_event_loop(eventP.get());
81 if (ret < 0)
82 {
83 phosphor::logging::log<phosphor::logging::level::ERR>(
84 "Error occurred during the sd_event_loop",
85 phosphor::logging::entry("RET=%d", ret));
86 }
87 }
Patrick Williams6d724ce2021-10-06 12:40:26 -050088 catch (const std::exception& e)
Kuiying Wanga9d39e32018-08-14 13:47:32 +080089 {
90 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
91 ret = -1;
92 }
93 return ret;
94}