blob: 31c212b784587c6ae7c4fdb6bfd8e798683089ca [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
Nan Zhou014be0b2021-12-28 18:00:14 -080023#include <stdlib.h>
24#include <systemd/sd-event.h>
25
26#include <cctype>
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050027#include <iostream>
Nan Zhou014be0b2021-12-28 18:00:14 -080028#include <sdbusplus/bus.hpp>
29#include <sdbusplus/server/manager.hpp>
Marri Devender Raof4682712019-03-19 05:00:28 -050030#include <sdeventplus/event.hpp>
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050031#include <string>
Nan Zhou014be0b2021-12-28 18:00:14 -080032#include <utility>
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050033
Nan Zhoucf06ccd2021-12-28 16:25:45 -080034static void exitWithError(const char* err, char** argv)
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050035{
36 phosphor::certs::util::ArgumentParser::usage(argv);
37 std::cerr << std::endl;
38 std::cerr << "ERROR: " << err << std::endl;
39 exit(EXIT_FAILURE);
40}
41
Nan Zhoucf06ccd2021-12-28 16:25:45 -080042inline std::string capitalize(const std::string& s)
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050043{
Nan Zhoucf06ccd2021-12-28 16:25:45 -080044 std::string res = s;
45 if (!res.empty())
46 {
47 res[0] = std::toupper(res[0]);
48 }
49 return res;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050050}
51
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050052int main(int argc, char** argv)
53{
54 // Read arguments.
55 auto options = phosphor::certs::util::ArgumentParser(argc, argv);
56
57 // Parse arguments
Nan Zhoucf06ccd2021-12-28 16:25:45 -080058 const std::string& typeStr = (options)["typeStr"];
59 phosphor::certs::CertificateType type =
60 phosphor::certs::stringToCertificateType(typeStr);
61 if (type == phosphor::certs::CertificateType::Unsupported)
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050062 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -080063 exitWithError("type not specified or invalid.", argv);
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050064 }
65
Nan Zhoucf06ccd2021-12-28 16:25:45 -080066 const std::string& endpoint = (options)["endpoint"];
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050067 if (endpoint == phosphor::certs::util::ArgumentParser::empty_string)
68 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -080069 exitWithError("endpoint not specified.", argv);
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050070 }
71
Nan Zhoucf06ccd2021-12-28 16:25:45 -080072 const std::string& path = (options)["path"];
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050073 if (path == phosphor::certs::util::ArgumentParser::empty_string)
74 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -080075 exitWithError("path not specified.", argv);
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -050076 }
77
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050078 // unit is an optional parameter
Nan Zhoucf06ccd2021-12-28 16:25:45 -080079 const std::string& unit = (options)["unit"];
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050080 auto bus = sdbusplus::bus::new_default();
Nan Zhoucf06ccd2021-12-28 16:25:45 -080081 auto objPath =
82 std::string(objectNamePrefix) + '/' + typeStr + '/' + endpoint;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050083
Marri Devender Rao6ceec402019-02-01 03:15:19 -060084 // Add sdbusplus ObjectManager
85 sdbusplus::server::manager::manager objManager(bus, objPath.c_str());
86
Marri Devender Raof4682712019-03-19 05:00:28 -050087 // Get default event loop
88 auto event = sdeventplus::Event::get_default();
89
90 // Attach the bus to sd_event to service user requests
91 bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
92
Nan Zhoucf06ccd2021-12-28 16:25:45 -080093 phosphor::certs::Manager manager(bus, event, objPath.c_str(), type, unit,
94 path);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050095
96 // Adjusting Interface name as per std convention
Nan Zhoucf06ccd2021-12-28 16:25:45 -080097 auto busName = std::string(busNamePrefix) + '.' + capitalize(typeStr) +
98 '.' + capitalize(endpoint);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050099 bus.request_name(busName.c_str());
Marri Devender Raof4682712019-03-19 05:00:28 -0500100 event.loop();
Jayanth Othayoth0aa0d112018-09-03 03:47:27 -0500101 return 0;
102}