Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2018 IBM 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 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 17 | #include "config.h" |
| 18 | |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 19 | #include "argument.hpp" |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 20 | #include "certs_manager.hpp" |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 21 | |
| 22 | #include <iostream> |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 23 | #include <locale> |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 24 | #include <sdeventplus/event.hpp> |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 25 | #include <string> |
| 26 | |
| 27 | static void ExitWithError(const char* err, char** argv) |
| 28 | { |
| 29 | phosphor::certs::util::ArgumentParser::usage(argv); |
| 30 | std::cerr << std::endl; |
| 31 | std::cerr << "ERROR: " << err << std::endl; |
| 32 | exit(EXIT_FAILURE); |
| 33 | } |
| 34 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 35 | inline void capitalize(std::string& s) |
| 36 | { |
| 37 | s[0] = std::toupper(s[0]); |
| 38 | } |
| 39 | |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 40 | int main(int argc, char** argv) |
| 41 | { |
| 42 | // Read arguments. |
| 43 | auto options = phosphor::certs::util::ArgumentParser(argc, argv); |
| 44 | |
| 45 | // Parse arguments |
| 46 | auto type = std::move((options)["type"]); |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 47 | if ((type == phosphor::certs::util::ArgumentParser::empty_string) || |
| 48 | !((type == phosphor::certs::SERVER) || |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 49 | (type == phosphor::certs::CLIENT) || |
| 50 | (type == phosphor::certs::AUTHORITY))) |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 51 | { |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 52 | ExitWithError("type not specified or invalid.", argv); |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | auto endpoint = std::move((options)["endpoint"]); |
| 56 | if (endpoint == phosphor::certs::util::ArgumentParser::empty_string) |
| 57 | { |
| 58 | ExitWithError("endpoint not specified.", argv); |
| 59 | } |
| 60 | |
| 61 | auto path = std::move((options)["path"]); |
| 62 | if (path == phosphor::certs::util::ArgumentParser::empty_string) |
| 63 | { |
| 64 | ExitWithError("path not specified.", argv); |
| 65 | } |
| 66 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 67 | // unit is an optional parameter |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 68 | auto unit = std::move((options)["unit"]); |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 69 | auto bus = sdbusplus::bus::new_default(); |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 70 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 71 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 72 | // Add sdbusplus ObjectManager |
| 73 | sdbusplus::server::manager::manager objManager(bus, objPath.c_str()); |
| 74 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 75 | // Get default event loop |
| 76 | auto event = sdeventplus::Event::get_default(); |
| 77 | |
| 78 | // Attach the bus to sd_event to service user requests |
| 79 | bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL); |
| 80 | |
| 81 | phosphor::certs::Manager manager(bus, event, objPath.c_str(), type, |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 82 | std::move(unit), std::move(path)); |
| 83 | |
| 84 | // Adjusting Interface name as per std convention |
| 85 | capitalize(type); |
| 86 | capitalize(endpoint); |
| 87 | auto busName = std::string(BUSNAME) + '.' + type + '.' + endpoint; |
| 88 | bus.request_name(busName.c_str()); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 89 | event.loop(); |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 90 | return 0; |
| 91 | } |