1 /*
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
18 #include <getopt.h>
19
main(int argc,char * argv[])20 int main(int argc, char* argv[])
21 {
22 int arg;
23 int optIndex = 0;
24 int ret = 0;
25 int node = 0;
26
27 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':
37 node = std::stoi(optarg);
38 break;
39 default:
40 break;
41 }
42 }
43
44 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
58 sdbusplus::bus_t bus = sdbusplus::bus::new_default();
59
60 std::string dbusObjName = DBUS_OBJECT_NAME + std::to_string(node);
61 sdbusplus::server::manager_t m{bus, dbusObjName.c_str()};
62
63 intfName = DBUS_INTF_NAME + std::to_string(node);
64
65 bus.request_name(intfName.c_str());
66
67 PostCode postCode{bus, dbusObjName.c_str(), eventP, node};
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 (const std::exception& e)
81 {
82 phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
83 return -1;
84 }
85 return 0;
86 }
87