13e5422edSVishwanatha Subbanna #include "config.h"
294df8c90SGunnar Mills 
394df8c90SGunnar Mills #include "occ_pass_through.hpp"
494df8c90SGunnar Mills 
594df8c90SGunnar Mills #include "elog-errors.hpp"
694df8c90SGunnar Mills 
794df8c90SGunnar Mills #include <errno.h>
894df8c90SGunnar Mills #include <fcntl.h>
9a8857c50SChris Cain #include <fmt/core.h>
1094df8c90SGunnar Mills #include <unistd.h>
1194df8c90SGunnar Mills 
1294df8c90SGunnar Mills #include <org/open_power/OCC/Device/error.hpp>
1394df8c90SGunnar Mills #include <phosphor-logging/elog.hpp>
1494df8c90SGunnar Mills #include <phosphor-logging/log.hpp>
15*b5ca1015SGeorge Liu 
16*b5ca1015SGeorge Liu #include <algorithm>
17*b5ca1015SGeorge Liu #include <memory>
1894df8c90SGunnar Mills #include <string>
19a8857c50SChris Cain 
206b492fbfSDeepak Kodihalli namespace open_power
216b492fbfSDeepak Kodihalli {
226b492fbfSDeepak Kodihalli namespace occ
236b492fbfSDeepak Kodihalli {
246b492fbfSDeepak Kodihalli 
25f3b7514eSGeorge Liu PassThrough::PassThrough(const char* path) :
26f3b7514eSGeorge Liu     Iface(utils::getBus(), path), path(path),
273e5422edSVishwanatha Subbanna     devicePath(OCC_DEV_PATH + std::to_string((this->path.back() - '0') + 1)),
28a8857c50SChris Cain     occInstance(this->path.back() - '0'),
293e5422edSVishwanatha Subbanna     activeStatusSignal(
30f3b7514eSGeorge Liu         utils::getBus(),
31f3b7514eSGeorge Liu         sdbusRule::propertiesChanged(path, "org.open_power.OCC.Status"),
3294df8c90SGunnar Mills         std::bind(std::mem_fn(&PassThrough::activeStatusEvent), this,
33a8857c50SChris Cain                   std::placeholders::_1)),
34f3b7514eSGeorge Liu     occCmd(occInstance, path)
356b492fbfSDeepak Kodihalli {
3638b08d79SVishwanatha Subbanna     // Nothing to do.
3738b08d79SVishwanatha Subbanna }
3838b08d79SVishwanatha Subbanna 
396b492fbfSDeepak Kodihalli std::vector<int32_t> PassThrough::send(std::vector<int32_t> command)
406b492fbfSDeepak Kodihalli {
4167d50ad6SVishwanatha Subbanna     std::vector<int32_t> response{};
4267d50ad6SVishwanatha Subbanna 
437d700e26SVishwanatha Subbanna     // OCC only understands [bytes] so need array of bytes. Doing this
447d700e26SVishwanatha Subbanna     // because rest-server currently treats all int* as 32 bit integer.
45a8857c50SChris Cain     std::vector<uint8_t> cmdInBytes, rsp;
467d700e26SVishwanatha Subbanna     cmdInBytes.resize(command.size());
477d700e26SVishwanatha Subbanna 
487d700e26SVishwanatha Subbanna     // Populate uint8_t version of vector.
497d700e26SVishwanatha Subbanna     std::transform(command.begin(), command.end(), cmdInBytes.begin(),
507d700e26SVishwanatha Subbanna                    [](decltype(cmdInBytes)::value_type x) { return x; });
517d700e26SVishwanatha Subbanna 
52a8857c50SChris Cain     rsp = send(cmdInBytes);
53a8857c50SChris Cain 
54a8857c50SChris Cain     response.resize(rsp.size());
55a8857c50SChris Cain     std::transform(rsp.begin(), rsp.end(), response.begin(),
56a8857c50SChris Cain                    [](decltype(response)::value_type x) { return x; });
57a8857c50SChris Cain 
58a8857c50SChris Cain     return response;
596b492fbfSDeepak Kodihalli }
606b492fbfSDeepak Kodihalli 
61a8857c50SChris Cain std::vector<uint8_t> PassThrough::send(std::vector<uint8_t> command)
6267d50ad6SVishwanatha Subbanna {
63a8857c50SChris Cain     using namespace phosphor::logging;
64a8857c50SChris Cain     using namespace sdbusplus::org::open_power::OCC::Device::Error;
65a8857c50SChris Cain 
66a8857c50SChris Cain     std::vector<uint8_t> response{};
67a8857c50SChris Cain 
68a8857c50SChris Cain     log<level::DEBUG>(
69a8857c50SChris Cain         fmt::format("PassThrough::send() Sending 0x{:02X} command to OCC{}",
70a8857c50SChris Cain                     command.front(), occInstance)
71a8857c50SChris Cain             .c_str());
72a8857c50SChris Cain     CmdStatus status = occCmd.send(command, response);
73a8857c50SChris Cain     if (status == CmdStatus::SUCCESS)
7467d50ad6SVishwanatha Subbanna     {
75a8857c50SChris Cain         if (response.size() >= 5)
7667d50ad6SVishwanatha Subbanna         {
77*b5ca1015SGeorge Liu             log<level::DEBUG>(
78*b5ca1015SGeorge Liu                 fmt::format("PassThrough::send() response had {} bytes",
79a8857c50SChris Cain                             response.size())
80a8857c50SChris Cain                     .c_str());
8167d50ad6SVishwanatha Subbanna         }
8267d50ad6SVishwanatha Subbanna         else
8367d50ad6SVishwanatha Subbanna         {
84a8857c50SChris Cain             log<level::ERR>("PassThrough::send() Invalid OCC response");
85a8857c50SChris Cain             dump_hex(response);
8667d50ad6SVishwanatha Subbanna         }
8767d50ad6SVishwanatha Subbanna     }
88a8857c50SChris Cain     else
89a8857c50SChris Cain     {
90a8857c50SChris Cain         if (status == CmdStatus::OPEN_FAILURE)
91a8857c50SChris Cain         {
92a8857c50SChris Cain             log<level::WARNING>("PassThrough::send() - OCC not active yet");
93a8857c50SChris Cain         }
94a8857c50SChris Cain         else
95a8857c50SChris Cain         {
96a8857c50SChris Cain             log<level::ERR>("PassThrough::send() - OCC command failed!");
97a8857c50SChris Cain         }
98a8857c50SChris Cain     }
994f4712d8SEddie James 
10067d50ad6SVishwanatha Subbanna     return response;
10167d50ad6SVishwanatha Subbanna }
10267d50ad6SVishwanatha Subbanna 
1033e5422edSVishwanatha Subbanna // Called at OCC Status change signal
1043e5422edSVishwanatha Subbanna void PassThrough::activeStatusEvent(sdbusplus::message::message& msg)
1053e5422edSVishwanatha Subbanna {
1063e5422edSVishwanatha Subbanna     std::string statusInterface;
107e0962703SPatrick Williams     std::map<std::string, std::variant<bool>> msgData;
1083e5422edSVishwanatha Subbanna     msg.read(statusInterface, msgData);
1093e5422edSVishwanatha Subbanna 
1103e5422edSVishwanatha Subbanna     auto propertyMap = msgData.find("OccActive");
1113e5422edSVishwanatha Subbanna     if (propertyMap != msgData.end())
1123e5422edSVishwanatha Subbanna     {
1133e5422edSVishwanatha Subbanna         // Extract the OccActive property
114305ff8b1SPatrick Williams         if (std::get<bool>(propertyMap->second))
1153e5422edSVishwanatha Subbanna         {
1164f4712d8SEddie James             occActive = true;
1173e5422edSVishwanatha Subbanna         }
1183e5422edSVishwanatha Subbanna         else
1193e5422edSVishwanatha Subbanna         {
1204f4712d8SEddie James             occActive = false;
1213e5422edSVishwanatha Subbanna         }
1223e5422edSVishwanatha Subbanna     }
1233e5422edSVishwanatha Subbanna     return;
1243e5422edSVishwanatha Subbanna }
1253e5422edSVishwanatha Subbanna 
1266b492fbfSDeepak Kodihalli } // namespace occ
1276b492fbfSDeepak Kodihalli } // namespace open_power
128