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> |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 24 | #include <string> |
| 25 | |
| 26 | static void ExitWithError(const char* err, char** argv) |
| 27 | { |
| 28 | phosphor::certs::util::ArgumentParser::usage(argv); |
| 29 | std::cerr << std::endl; |
| 30 | std::cerr << "ERROR: " << err << std::endl; |
| 31 | exit(EXIT_FAILURE); |
| 32 | } |
| 33 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 34 | inline void capitalize(std::string& s) |
| 35 | { |
| 36 | s[0] = std::toupper(s[0]); |
| 37 | } |
| 38 | |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 39 | int main(int argc, char** argv) |
| 40 | { |
| 41 | // Read arguments. |
| 42 | auto options = phosphor::certs::util::ArgumentParser(argc, argv); |
| 43 | |
| 44 | // Parse arguments |
| 45 | auto type = std::move((options)["type"]); |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 46 | if ((type == phosphor::certs::util::ArgumentParser::empty_string) || |
| 47 | !((type == phosphor::certs::SERVER) || |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 48 | (type == phosphor::certs::CLIENT) || |
| 49 | (type == phosphor::certs::AUTHORITY))) |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 50 | { |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 51 | ExitWithError("type not specified or invalid.", argv); |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | auto endpoint = std::move((options)["endpoint"]); |
| 55 | if (endpoint == phosphor::certs::util::ArgumentParser::empty_string) |
| 56 | { |
| 57 | ExitWithError("endpoint not specified.", argv); |
| 58 | } |
| 59 | |
| 60 | auto path = std::move((options)["path"]); |
| 61 | if (path == phosphor::certs::util::ArgumentParser::empty_string) |
| 62 | { |
| 63 | ExitWithError("path not specified.", argv); |
| 64 | } |
| 65 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 66 | // unit is an optional parameter |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 67 | auto unit = std::move((options)["unit"]); |
| 68 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 69 | auto bus = sdbusplus::bus::new_default(); |
| 70 | |
| 71 | auto objPath = std::string(OBJPATH) + '/' + type + '/' + endpoint; |
| 72 | |
| 73 | phosphor::certs::Manager manager(bus, objPath.c_str(), type, |
| 74 | std::move(unit), std::move(path)); |
| 75 | |
| 76 | // Adjusting Interface name as per std convention |
| 77 | capitalize(type); |
| 78 | capitalize(endpoint); |
| 79 | auto busName = std::string(BUSNAME) + '.' + type + '.' + endpoint; |
| 80 | bus.request_name(busName.c_str()); |
| 81 | |
| 82 | while (true) |
| 83 | { |
| 84 | // process dbus calls / signals discarding unhandled |
| 85 | bus.process_discard(); |
| 86 | bus.wait(); |
| 87 | } |
Jayanth Othayoth | 0aa0d11 | 2018-09-03 03:47:27 -0500 | [diff] [blame] | 88 | return 0; |
| 89 | } |