blob: 6ed2e553f3df216847937b8b93f84d69c022f27c [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;
Amithash Prasadb6616cd2025-05-29 15:56:51 -070026 PostCodeHandlers handlers;
Kuiying Wang3a044402019-02-19 15:00:11 +080027
Kumar Thangavelfd45f782020-09-01 22:59:00 +053028 std::string intfName;
29
30 static struct option longOpts[] = {{"host", required_argument, 0, 'h'},
Amithash Prasadb6616cd2025-05-29 15:56:51 -070031 {"config", required_argument, 0, 'c'},
Kumar Thangavelfd45f782020-09-01 22:59:00 +053032 {0, 0, 0, 0}};
33
Amithash Prasadb6616cd2025-05-29 15:56:51 -070034 while ((arg = getopt_long(argc, argv, "h:c:", longOpts, &optIndex)) != -1)
Kumar Thangavelfd45f782020-09-01 22:59:00 +053035 {
36 switch (arg)
37 {
38 case 'h':
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080039 node = std::stoi(optarg);
Kumar Thangavelfd45f782020-09-01 22:59:00 +053040 break;
Amithash Prasadb6616cd2025-05-29 15:56:51 -070041 case 'c':
42 handlers.load(optarg);
43 break;
Kumar Thangavelfd45f782020-09-01 22:59:00 +053044 default:
45 break;
46 }
47 }
48
Kuiying Wang3a044402019-02-19 15:00:11 +080049 phosphor::logging::log<phosphor::logging::level::INFO>(
50 "Start post code manager service...");
51
52 sd_event* event = nullptr;
53 ret = sd_event_default(&event);
54 if (ret < 0)
55 {
56 phosphor::logging::log<phosphor::logging::level::ERR>(
57 "Error creating a default sd_event handler");
58 return ret;
59 }
60 EventPtr eventP{event};
61 event = nullptr;
62
Patrick Williamse9feb952022-07-22 19:26:54 -050063 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
Kumar Thangavel4e081562022-12-01 12:56:07 +053064
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080065 std::string dbusObjName = DBUS_OBJECT_NAME + std::to_string(node);
Kumar Thangavel4e081562022-12-01 12:56:07 +053066 sdbusplus::server::manager_t m{bus, dbusObjName.c_str()};
Kuiying Wang3a044402019-02-19 15:00:11 +080067
Jonathan Domanc7fed5c2022-11-23 11:14:42 -080068 intfName = DBUS_INTF_NAME + std::to_string(node);
Kumar Thangavelfd45f782020-09-01 22:59:00 +053069
70 bus.request_name(intfName.c_str());
Kuiying Wang3a044402019-02-19 15:00:11 +080071
Amithash Prasadb6616cd2025-05-29 15:56:51 -070072 PostCode postCode{bus, dbusObjName.c_str(), eventP, node, handlers};
Kuiying Wang3a044402019-02-19 15:00:11 +080073
74 try
75 {
76 bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
77 ret = sd_event_loop(eventP.get());
78 if (ret < 0)
79 {
80 phosphor::logging::log<phosphor::logging::level::ERR>(
81 "Error occurred during the sd_event_loop",
82 phosphor::logging::entry("RET=%d", ret));
83 }
84 }
Patrick Williams7cc8ea62021-10-06 12:40:56 -050085 catch (const std::exception& e)
Kuiying Wang3a044402019-02-19 15:00:11 +080086 {
87 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
88 return -1;
89 }
90 return 0;
91}