xref: /openbmc/openpower-proc-control/proc_control.cpp (revision 75912e83445c48de616fdb71dd4d7696beadf48e)
1 /**
2  * Copyright (C) 2017 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 #include "registration.hpp"
17 
18 #include <algorithm>
19 #include <functional>
20 #include <iostream>
21 #include <org/open_power/Proc/FSI/error.hpp>
22 #include <phosphor-logging/elog-errors.hpp>
23 #include <phosphor-logging/elog.hpp>
24 #include <phosphor-logging/log.hpp>
25 #include <xyz/openbmc_project/Common/Device/error.hpp>
26 #include <xyz/openbmc_project/Common/File/error.hpp>
27 #include <xyz/openbmc_project/Common/error.hpp>
28 
29 using namespace openpower::util;
30 
31 namespace common_error = sdbusplus::xyz::openbmc_project::Common::Error;
32 namespace device_error = sdbusplus::xyz::openbmc_project::Common::Device::Error;
33 namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
34 namespace fsi_error = sdbusplus::org::open_power::Proc::FSI::Error;
35 
36 void usage(char** argv, const ProcedureMap& procedures)
37 {
38     std::cerr << "Usage: " << argv[0] << " [action]\n";
39     std::cerr << "   actions:\n";
40 
41     for (const auto& p : procedures)
42     {
43         std::cerr << "     " << p.first << "\n";
44     }
45 }
46 
47 int main(int argc, char** argv)
48 {
49     using namespace phosphor::logging;
50     const ProcedureMap& procedures = Registration::getProcedures();
51 
52     if (argc != 2)
53     {
54         usage(argv, procedures);
55         return -1;
56     }
57 
58     std::string action{argv[1]};
59 
60     auto procedure = procedures.find(action);
61 
62     if (procedure == procedures.end())
63     {
64         usage(argv, procedures);
65         return -1;
66     }
67 
68     try
69     {
70         procedure->second();
71     }
72     catch (file_error::Seek& e)
73     {
74         commit<file_error::Seek>();
75         return -1;
76     }
77     catch (file_error::Open& e)
78     {
79         commit<file_error::Open>();
80         return -1;
81     }
82     catch (device_error::WriteFailure& e)
83     {
84         commit<device_error::WriteFailure>();
85         return -1;
86     }
87     catch (device_error::ReadFailure& e)
88     {
89         commit<device_error::ReadFailure>();
90         return -1;
91     }
92     catch (common_error::InvalidArgument& e)
93     {
94         commit<common_error::InvalidArgument>();
95         return -1;
96     }
97     catch (fsi_error::MasterDetectionFailure& e)
98     {
99         commit<fsi_error::MasterDetectionFailure>();
100         return -1;
101     }
102     catch (fsi_error::SlaveDetectionFailure& e)
103     {
104         commit<fsi_error::SlaveDetectionFailure>();
105         return -1;
106     }
107     catch (std::exception& e)
108     {
109         log<level::ERR>("exception raised", entry("EXCEPTION=%s", e.what()));
110         return -1;
111     }
112 
113     return 0;
114 }
115