xref: /openbmc/openpower-proc-control/proc_control.cpp (revision 1a9a5a6a144e81d8b61e6f999d8de7cba731ae6e)
1f716f32aSMatt Spinler /**
2e84b4ddbSPatrick Venture  * Copyright (C) 2017 IBM Corporation
3f716f32aSMatt Spinler  *
4f716f32aSMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
5f716f32aSMatt Spinler  * you may not use this file except in compliance with the License.
6f716f32aSMatt Spinler  * You may obtain a copy of the License at
7f716f32aSMatt Spinler  *
8f716f32aSMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
9f716f32aSMatt Spinler  *
10f716f32aSMatt Spinler  * Unless required by applicable law or agreed to in writing, software
11f716f32aSMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
12f716f32aSMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f716f32aSMatt Spinler  * See the License for the specific language governing permissions and
14f716f32aSMatt Spinler  * limitations under the License.
15f716f32aSMatt Spinler  */
16f78d9042SPatrick Venture #include "registration.hpp"
17f78d9042SPatrick Venture 
18ee401e9bSMatt Spinler #include <org/open_power/Proc/FSI/error.hpp>
19f78d9042SPatrick Venture #include <phosphor-logging/elog-errors.hpp>
20f78d9042SPatrick Venture #include <phosphor-logging/elog.hpp>
21f78d9042SPatrick Venture #include <phosphor-logging/log.hpp>
22a231ceb4SMatt Spinler #include <xyz/openbmc_project/Common/Device/error.hpp>
23a231ceb4SMatt Spinler #include <xyz/openbmc_project/Common/File/error.hpp>
24f78d9042SPatrick Venture #include <xyz/openbmc_project/Common/error.hpp>
25f716f32aSMatt Spinler 
265e5d4451SBrad Bishop #include <algorithm>
275e5d4451SBrad Bishop #include <functional>
285e5d4451SBrad Bishop #include <iostream>
295e5d4451SBrad Bishop 
30d9bdcf72SMatt Spinler using namespace openpower::util;
3116ab00cbSLakshminarayana R. Kammath 
32f78d9042SPatrick Venture namespace common_error = sdbusplus::xyz::openbmc_project::Common::Error;
33f78d9042SPatrick Venture namespace device_error = sdbusplus::xyz::openbmc_project::Common::Device::Error;
34f78d9042SPatrick Venture namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
35ee401e9bSMatt Spinler namespace fsi_error = sdbusplus::org::open_power::Proc::FSI::Error;
36f716f32aSMatt Spinler 
usage(char ** argv,const ProcedureMap & procedures)37d9bdcf72SMatt Spinler void usage(char** argv, const ProcedureMap& procedures)
38f716f32aSMatt Spinler {
39f716f32aSMatt Spinler     std::cerr << "Usage: " << argv[0] << " [action]\n";
40f716f32aSMatt Spinler     std::cerr << "   actions:\n";
41f716f32aSMatt Spinler 
42f716f32aSMatt Spinler     for (const auto& p : procedures)
43f716f32aSMatt Spinler     {
44d9bdcf72SMatt Spinler         std::cerr << "     " << p.first << "\n";
45f716f32aSMatt Spinler     }
46f716f32aSMatt Spinler }
47f716f32aSMatt Spinler 
main(int argc,char ** argv)48f716f32aSMatt Spinler int main(int argc, char** argv)
49f716f32aSMatt Spinler {
5018b0786aSDhruvaraj Subhashchandran     using namespace phosphor::logging;
51d9bdcf72SMatt Spinler     const ProcedureMap& procedures = Registration::getProcedures();
52d9bdcf72SMatt Spinler 
53f716f32aSMatt Spinler     if (argc != 2)
54f716f32aSMatt Spinler     {
55d9bdcf72SMatt Spinler         usage(argv, procedures);
56f716f32aSMatt Spinler         return -1;
57f716f32aSMatt Spinler     }
58f716f32aSMatt Spinler 
59f716f32aSMatt Spinler     std::string action{argv[1]};
60f716f32aSMatt Spinler 
61d9bdcf72SMatt Spinler     auto procedure = procedures.find(action);
62f716f32aSMatt Spinler 
63f716f32aSMatt Spinler     if (procedure == procedures.end())
64f716f32aSMatt Spinler     {
65d9bdcf72SMatt Spinler         usage(argv, procedures);
66f716f32aSMatt Spinler         return -1;
67f716f32aSMatt Spinler     }
68f716f32aSMatt Spinler 
69f716f32aSMatt Spinler     try
70f716f32aSMatt Spinler     {
71d9bdcf72SMatt Spinler         procedure->second();
72f716f32aSMatt Spinler     }
73*1a9a5a6aSPatrick Williams     catch (const file_error::Seek& e)
74f716f32aSMatt Spinler     {
75a231ceb4SMatt Spinler         commit<file_error::Seek>();
7618b0786aSDhruvaraj Subhashchandran         return -1;
7718b0786aSDhruvaraj Subhashchandran     }
78*1a9a5a6aSPatrick Williams     catch (const file_error::Open& e)
7918b0786aSDhruvaraj Subhashchandran     {
80a231ceb4SMatt Spinler         commit<file_error::Open>();
8118b0786aSDhruvaraj Subhashchandran         return -1;
8218b0786aSDhruvaraj Subhashchandran     }
83*1a9a5a6aSPatrick Williams     catch (const device_error::WriteFailure& e)
8418b0786aSDhruvaraj Subhashchandran     {
85a231ceb4SMatt Spinler         commit<device_error::WriteFailure>();
8618b0786aSDhruvaraj Subhashchandran         return -1;
8718b0786aSDhruvaraj Subhashchandran     }
88*1a9a5a6aSPatrick Williams     catch (const device_error::ReadFailure& e)
8918b0786aSDhruvaraj Subhashchandran     {
90a231ceb4SMatt Spinler         commit<device_error::ReadFailure>();
91f716f32aSMatt Spinler         return -1;
92f716f32aSMatt Spinler     }
93*1a9a5a6aSPatrick Williams     catch (const common_error::InvalidArgument& e)
947ce535cbSDhruvaraj Subhashchandran     {
95a231ceb4SMatt Spinler         commit<common_error::InvalidArgument>();
967ce535cbSDhruvaraj Subhashchandran         return -1;
977ce535cbSDhruvaraj Subhashchandran     }
98*1a9a5a6aSPatrick Williams     catch (const fsi_error::MasterDetectionFailure& e)
99ee401e9bSMatt Spinler     {
100ee401e9bSMatt Spinler         commit<fsi_error::MasterDetectionFailure>();
101ee401e9bSMatt Spinler         return -1;
102ee401e9bSMatt Spinler     }
103*1a9a5a6aSPatrick Williams     catch (const fsi_error::SlaveDetectionFailure& e)
104ee401e9bSMatt Spinler     {
105ee401e9bSMatt Spinler         commit<fsi_error::SlaveDetectionFailure>();
106ee401e9bSMatt Spinler         return -1;
107ee401e9bSMatt Spinler     }
108*1a9a5a6aSPatrick Williams     catch (const std::exception& e)
109b181d3bbSRamesh Iyyar     {
11078479605SMarri Devender Rao         log<level::ERR>("exception raised", entry("EXCEPTION=%s", e.what()));
111b181d3bbSRamesh Iyyar         return -1;
112b181d3bbSRamesh Iyyar     }
113ee401e9bSMatt Spinler 
114f716f32aSMatt Spinler     return 0;
115f716f32aSMatt Spinler }
116