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 
17 #include "config.h"
18 
19 #include "argument.hpp"
20 #include "certificate.hpp"
21 #include "certs_manager.hpp"
22 
23 #include <systemd/sd-event.h>
24 
25 #include <cctype>
26 #include <sdbusplus/bus.hpp>
27 #include <sdbusplus/server/manager.hpp>
28 #include <sdeventplus/event.hpp>
29 #include <string>
30 #include <utility>
31 
32 inline std::string capitalize(const std::string& s)
33 {
34     std::string res = s;
35     if (!res.empty())
36     {
37         res[0] = std::toupper(res[0]);
38     }
39     return res;
40 }
41 
42 int main(int argc, char** argv)
43 {
44     phosphor::certs::Arguments arguments;
45     if (phosphor::certs::processArguments(argc, argv, arguments) != 0)
46     {
47         std::exit(EXIT_FAILURE);
48     }
49 
50     auto bus = sdbusplus::bus::new_default();
51     auto objPath = std::string(objectNamePrefix) + '/' + arguments.typeStr +
52                    '/' + arguments.endpoint;
53     // Add sdbusplus ObjectManager
54     sdbusplus::server::manager_t objManager(bus, objPath.c_str());
55 
56     // Get default event loop
57     auto event = sdeventplus::Event::get_default();
58 
59     // Attach the bus to sd_event to service user requests
60     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
61     phosphor::certs::Manager manager(
62         bus, event, objPath.c_str(),
63         phosphor::certs::stringToCertificateType(arguments.typeStr),
64         arguments.unit, arguments.path);
65 
66     // Adjusting Interface name as per std convention
67     auto busName = std::string(busNamePrefix) + '.' +
68                    capitalize(arguments.typeStr) + '.' +
69                    capitalize(arguments.endpoint);
70     bus.request_name(busName.c_str());
71     event.loop();
72     return 0;
73 }
74