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     intfName = DBUS_INTF_NAME + std::to_string(postcodeDataHolderObj->node);
64 
65     bus.request_name(intfName.c_str());
66 
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 (const std::exception& e)
81     {
82         phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
83         return -1;
84     }
85     return 0;
86 }
87