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