Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 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 | |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 18 | #include <getopt.h> |
| 19 | |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 20 | int main(int argc, char* argv[]) |
| 21 | { |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 22 | int arg; |
| 23 | int optIndex = 0; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 24 | int ret = 0; |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 25 | int node = 0; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 26 | |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 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': |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 37 | node = std::stoi(optarg); |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 38 | break; |
| 39 | default: |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 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 | |
Patrick Williams | e9feb95 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 58 | sdbusplus::bus_t bus = sdbusplus::bus::new_default(); |
Kumar Thangavel | 4e08156 | 2022-12-01 12:56:07 +0530 | [diff] [blame] | 59 | |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 60 | std::string dbusObjName = DBUS_OBJECT_NAME + std::to_string(node); |
Kumar Thangavel | 4e08156 | 2022-12-01 12:56:07 +0530 | [diff] [blame] | 61 | sdbusplus::server::manager_t m{bus, dbusObjName.c_str()}; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 62 | |
Jonathan Doman | c7fed5c | 2022-11-23 11:14:42 -0800 | [diff] [blame] | 63 | intfName = DBUS_INTF_NAME + std::to_string(node); |
Kumar Thangavel | fd45f78 | 2020-09-01 22:59:00 +0530 | [diff] [blame] | 64 | |
| 65 | bus.request_name(intfName.c_str()); |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 66 | |
Bonnie Lo | 13cb853 | 2022-12-15 17:00:40 +0800 | [diff] [blame] | 67 | PostCode postCode{bus, dbusObjName.c_str(), eventP, node}; |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 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 | } |
Patrick Williams | 7cc8ea6 | 2021-10-06 12:40:56 -0500 | [diff] [blame] | 80 | catch (const std::exception& e) |
Kuiying Wang | 3a04440 | 2019-02-19 15:00:11 +0800 | [diff] [blame] | 81 | { |
| 82 | phosphor::logging::log<phosphor::logging::level::ERR>(e.what()); |
| 83 | return -1; |
| 84 | } |
| 85 | return 0; |
| 86 | } |