blob: 98e09fb9efdc287b940beacd5045580fd39e09e1 [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{
Bonnie Lo20189642022-11-10 13:32:42 +080022 PostCodeDataHolder& postcodeDataHolderObj =
Manojkiran Edabd706d72021-12-21 05:09:47 +053023 PostCodeDataHolder::getInstance();
Kumar Thangavelfd45f782020-09-01 22:59:00 +053024
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':
Manojkiran Edabd706d72021-12-21 05:09:47 +053039 postcodeDataHolderObj.node = std::stoi(optarg);
Kumar Thangavelfd45f782020-09-01 22:59:00 +053040 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
Patrick Williamse9feb952022-07-22 19:26:54 -050060 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Kumar Thangavel4e081562022-12-01 12:56:07 +053061
62 std::string dbusObjName =
63 DBUS_OBJECT_NAME + std::to_string(postcodeDataHolderObj.node);
64 sdbusplus::server::manager_t m{bus, dbusObjName.c_str()};
Kuiying Wang3a044402019-02-19 15:00:11 +080065
Manojkiran Edabd706d72021-12-21 05:09:47 +053066 intfName = DBUS_INTF_NAME + std::to_string(postcodeDataHolderObj.node);
Kumar Thangavelfd45f782020-09-01 22:59:00 +053067
68 bus.request_name(intfName.c_str());
Kuiying Wang3a044402019-02-19 15:00:11 +080069
Kumar Thangavel4e081562022-12-01 12:56:07 +053070 PostCode postCode{bus, dbusObjName.c_str(), eventP};
Kuiying Wang3a044402019-02-19 15:00:11 +080071
72 try
73 {
74 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
75 ret = sd_event_loop(eventP.get());
76 if (ret < 0)
77 {
78 phosphor::logging::log<phosphor::logging::level::ERR>(
79 "Error occurred during the sd_event_loop",
80 phosphor::logging::entry("RET=%d", ret));
81 }
82 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -050083 catch (const std::exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +080084 {
85 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
86 return -1;
87 }
88 return 0;
89}