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