blob: d1a7568ad824cebd99a1cf8770c9845b3b91c898 [file] [log] [blame]
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -05001/**
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 Othayothcfbc8dc2018-09-03 07:22:27 -050017#include "config.h"
18
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050019#include "argument.hpp"
Nan Zhoucf06ccd2021-12-28 16:25:45 -080020#include "certificate.hpp"
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050021#include "certs_manager.hpp"
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050022
23#include <iostream>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050024#include <locale>
Marri Devender Raof4682712019-03-19 05:00:28 -050025#include <sdeventplus/event.hpp>
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050026#include <string>
27
Nan Zhoucf06ccd2021-12-28 16:25:45 -080028static void exitWithError(const char* err, char** argv)
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050029{
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 Zhoucf06ccd2021-12-28 16:25:45 -080036inline std::string capitalize(const std::string& s)
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050037{
Nan Zhoucf06ccd2021-12-28 16:25:45 -080038 std::string res = s;
39 if (!res.empty())
40 {
41 res[0] = std::toupper(res[0]);
42 }
43 return res;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050044}
45
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050046int main(int argc, char** argv)
47{
48 // Read arguments.
49 auto options = phosphor::certs::util::ArgumentParser(argc, argv);
50
51 // Parse arguments
Nan Zhoucf06ccd2021-12-28 16:25:45 -080052 const std::string& typeStr = (options)["typeStr"];
53 phosphor::certs::CertificateType type =
54 phosphor::certs::stringToCertificateType(typeStr);
55 if (type == phosphor::certs::CertificateType::Unsupported)
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050056 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -080057 exitWithError("type not specified or invalid.", argv);
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050058 }
59
Nan Zhoucf06ccd2021-12-28 16:25:45 -080060 const std::string& endpoint = (options)["endpoint"];
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050061 if (endpoint == phosphor::certs::util::ArgumentParser::empty_string)
62 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -080063 exitWithError("endpoint not specified.", argv);
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050064 }
65
Nan Zhoucf06ccd2021-12-28 16:25:45 -080066 const std::string& path = (options)["path"];
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050067 if (path == phosphor::certs::util::ArgumentParser::empty_string)
68 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -080069 exitWithError("path not specified.", argv);
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050070 }
71
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050072 // unit is an optional parameter
Nan Zhoucf06ccd2021-12-28 16:25:45 -080073 const std::string& unit = (options)["unit"];
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050074 auto bus = sdbusplus::bus::new_default();
Nan Zhoucf06ccd2021-12-28 16:25:45 -080075 auto objPath =
76 std::string(objectNamePrefix) + '/' + typeStr + '/' + endpoint;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050077
Marri Devender Rao6ceec402019-02-01 03:15:19 -060078 // Add sdbusplus ObjectManager
79 sdbusplus::server::manager::manager objManager(bus, objPath.c_str());
80
Marri Devender Raof4682712019-03-19 05:00:28 -050081 // 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 Zhoucf06ccd2021-12-28 16:25:45 -080087 phosphor::certs::Manager manager(bus, event, objPath.c_str(), type, unit,
88 path);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050089
90 // Adjusting Interface name as per std convention
Nan Zhoucf06ccd2021-12-28 16:25:45 -080091 auto busName = std::string(busNamePrefix) + '.' + capitalize(typeStr) +
92 '.' + capitalize(endpoint);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050093 bus.request_name(busName.c_str());
Marri Devender Raof4682712019-03-19 05:00:28 -050094 event.loop();
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050095 return 0;
96}