blob: 2cdb4901f4b46b2587d4bad09c19242ef9b43cb1 [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 PostCodeDataHolder* postcodeDataHolderObj =
23 postcodeDataHolderObj->getInstance();
24
25 int arg;
26 int optIndex = 0;
Kuiying Wang3a044402019-02-19 15:00:11 +080027 int ret = 0;
28
Kumar Thangavelfd45f782020-09-01 22:59:00 +053029 std::string intfName;
30
31 static struct option longOpts[] = {{"host", required_argument, 0, 'h'},
32 {0, 0, 0, 0}};
33
34 while ((arg = getopt_long(argc, argv, "h:", longOpts, &optIndex)) != -1)
35 {
36 switch (arg)
37 {
38 case 'h':
39 postcodeDataHolderObj->node = std::stoi(optarg);
40 break;
41 default:
42 break;
43 }
44 }
45
Kuiying Wang3a044402019-02-19 15:00:11 +080046 phosphor::logging::log<phosphor::logging::level::INFO>(
47 "Start post code manager service...");
48
49 sd_event* event = nullptr;
50 ret = sd_event_default(&event);
51 if (ret < 0)
52 {
53 phosphor::logging::log<phosphor::logging::level::ERR>(
54 "Error creating a default sd_event handler");
55 return ret;
56 }
57 EventPtr eventP{event};
58 event = nullptr;
59
60 sdbusplus::bus::bus bus = sdbusplus::bus::new_default();
61 sdbusplus::server::manager_t m{bus, DBUS_OBJECT_NAME};
62
Jonathan Doman08125ca2021-01-07 17:49:15 -080063 intfName = DBUS_INTF_NAME + std::to_string(postcodeDataHolderObj->node);
Kumar Thangavelfd45f782020-09-01 22:59:00 +053064
65 bus.request_name(intfName.c_str());
Kuiying Wang3a044402019-02-19 15:00:11 +080066
67 PostCode postCode{bus, DBUS_OBJECT_NAME, eventP};
68
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 }
80 catch (std::exception& e)
81 {
82 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
83 return -1;
84 }
85 return 0;
86}