blob: f5fbf18b0d357e1501746ab277bc7553dd31d270 [file] [log] [blame]
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +05301/**
2 * Copyright © 2016 IBM 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 */
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053016#include <systemd/sd-event.h>
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053017#include <phosphor-logging/log.hpp>
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053018#include "softoff.hpp"
19#include "config.h"
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053020#include "timer.hpp"
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053021
Vishwanatha Subbanna7cc9d712017-01-24 18:48:40 +053022using namespace phosphor::logging;
23
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053024int main(int argc, char** argv)
25{
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053026 // systemd event handler
27 sd_event* events = nullptr;
28
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053029 // Get a handle to system dbus.
30 auto bus = sdbusplus::bus::new_default();
31
32 // Add systemd object manager.
33 sdbusplus::server::manager::manager(bus, SOFTOFF_OBJPATH);
34
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053035 // sd_event object
36 auto r = sd_event_default(&events);
37 if (r < 0)
38 {
39 log<level::ERR>("Failure to create sd_event handler",
40 entry("ERROR=%s", strerror(-r)));
41 return -1;
42 }
43
44 // Attach the bus to sd_event to service user requests
45 bus.attach_event(events, SD_EVENT_PRIORITY_NORMAL);
46
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053047 // Create the SoftPowerOff object.
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053048 phosphor::ipmi::SoftPowerOff powerObj(bus, events, SOFTOFF_OBJPATH);
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053049
50 /** @brief Claim the bus */
51 bus.request_name(SOFTOFF_BUSNAME);
52
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053053 /** @brief Wait for client requests until this application has processed
54 * at least one successful SoftPowerOff
55 */
56 while(!powerObj.isCompleted() && !powerObj.isTimerExpired())
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053057 {
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053058 // -1 denotes wait for ever
59 r = sd_event_run(events, (uint64_t)-1);
60 if (r < 0)
61 {
62 log<level::ERR>("Failure in processing request",
63 entry("ERROR=%s", strerror(-r)));
64 break;
65 }
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053066 }
Vishwanatha Subbannabcb76882017-01-25 16:29:43 +053067
68 // Cleanup the event handler
69 events = sd_event_unref(events);
70
Vishwanatha Subbanna506aa0f2017-01-24 14:58:25 +053071 return 0;
72}