xref: /openbmc/phosphor-post-code-manager/src/main.cpp (revision fd45f78858304f67a0e80224a99cbe76eed82af0)
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 
20 int main(int argc, char* argv[])
21 {
22     PostCodeDataHolder* postcodeDataHolderObj =
23         postcodeDataHolderObj->getInstance();
24 
25     int arg;
26     int optIndex = 0;
27     int ret = 0;
28 
29     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 
46     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 
63     if (postcodeDataHolderObj->node == 0)
64     {
65         intfName = DBUS_INTF_NAME;
66     }
67     else
68     {
69         intfName = DBUS_INTF_NAME + std::to_string(postcodeDataHolderObj->node);
70     }
71 
72     bus.request_name(intfName.c_str());
73 
74     PostCode postCode{bus, DBUS_OBJECT_NAME, eventP};
75 
76     try
77     {
78         bus.attach_event(eventP.get(), SD_EVENT_PRIORITY_NORMAL);
79         ret = sd_event_loop(eventP.get());
80         if (ret < 0)
81         {
82             phosphor::logging::log<phosphor::logging::level::ERR>(
83                 "Error occurred during the sd_event_loop",
84                 phosphor::logging::entry("RET=%d", ret));
85         }
86     }
87     catch (std::exception& e)
88     {
89         phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
90         return -1;
91     }
92     return 0;
93 }
94