blob: be4b6b91c3fba47a41eb3b29e9456696bd9a244c [file] [log] [blame]
Kuiying Wang3a044402019-02-19 15:00:11 +08001/*
2// Copyright (c) 2019 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#include "post_code.hpp"
17
Kumar Thangavelfd45f782020-09-01 22:59:00 +053018#include <getopt.h>
19
Kuiying Wang3a044402019-02-19 15:00:11 +080020int main(int argc, char* argv[])
21{
Kumar Thangavelfd45f782020-09-01 22:59:00 +053022 int arg;
23 int optIndex = 0;
Kuiying Wang3a044402019-02-19 15:00:11 +080024 int ret = 0;
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080025 int node = 0;
Kuiying Wang3a044402019-02-19 15:00:11 +080026
Kumar Thangavelfd45f782020-09-01 22:59:00 +053027 std::string intfName;
28
29 static struct option longOpts[] = {{"host", required_argument, 0, 'h'},
30 {0, 0, 0, 0}};
31
32 while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
33 {
34 switch (arg)
35 {
36 case 'h':
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080037 node = std::stoi(optarg);
Kumar Thangavelfd45f782020-09-01 22:59:00 +053038 break;
39 default:
40 break;
41 }
42 }
43
Kuiying Wang3a044402019-02-19 15:00:11 +080044 phosphor::logging::log<phosphor::logging::level::INFO>(
45 "Start post code manager service...");
46
47 sd_event* event = nullptr;
48 ret = sd_event_default(&event);
49 if (ret < 0)
50 {
51 phosphor::logging::log<phosphor::logging::level::ERR>(
52 "Error creating a default sd_event handler");
53 return ret;
54 }
55 EventPtr eventP{event};
56 event = nullptr;
57
Patrick Williamse9feb952022-07-22 19:26:54 -050058 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Kumar Thangavel4e081562022-12-01 12:56:07 +053059
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080060 std::string dbusObjName = DBUS_OBJECT_NAME + std::to_string(node);
Kumar Thangavel4e081562022-12-01 12:56:07 +053061 sdbusplus::server::manager_t m{bus, dbusObjName.c_str()};
Kuiying Wang3a044402019-02-19 15:00:11 +080062
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080063 intfName = DBUS_INTF_NAME + std::to_string(node);
Kumar Thangavelfd45f782020-09-01 22:59:00 +053064
65 bus.request_name(intfName.c_str());
Kuiying Wang3a044402019-02-19 15:00:11 +080066
Bonnie Lo13cb8532022-12-15 17:00:40 +080067 PostCode postCode{bus, dbusObjName.c_str(), eventP, node};
Kuiying Wang3a044402019-02-19 15:00:11 +080068
69 try
70 {
71 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
72 ret = sd_event_loop(eventP.get());
73 if (ret < 0)
74 {
75 phosphor::logging::log<phosphor::logging::level::ERR>(
76 "Error occurred during the sd_event_loop",
77 phosphor::logging::entry("RET=%d", ret));
78 }
79 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -050080 catch (const std::exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +080081 {
82 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
83 return -1;
84 }
85 return 0;
86}