blob: db133b1ff7e6008919740091fcfadf477ee044ae [file] [log] [blame]
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +05301#include "config.h"
Gunnar Mills94df8c92018-09-14 14:50:03 -05002
3#include "occ_pass_through.hpp"
4
5#include "elog-errors.hpp"
6
7#include <errno.h>
8#include <fcntl.h>
Chris Caina8857c52021-01-27 11:53:05 -06009#include <fmt/core.h>
Gunnar Mills94df8c92018-09-14 14:50:03 -050010#include <unistd.h>
11
Gunnar Mills94df8c92018-09-14 14:50:03 -050012#include <org/open_power/OCC/Device/error.hpp>
13#include <phosphor-logging/elog.hpp>
14#include <phosphor-logging/log.hpp>
George Liub5ca1012021-09-10 12:53:11 +080015
16#include <algorithm>
17#include <memory>
Gunnar Mills94df8c92018-09-14 14:50:03 -050018#include <string>
Chris Caina8857c52021-01-27 11:53:05 -060019
Deepak Kodihalli6b492fb2017-03-18 01:09:28 -050020namespace open_power
21{
22namespace occ
23{
Deepak Kodihalli6b492fb2017-03-18 01:09:28 -050024
George Liuf3b75142021-06-10 11:22:50 +080025PassThrough::PassThrough(const char* path) :
26 Iface(utils::getBus(), path), path(path),
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +053027 devicePath(OCC_DEV_PATH + std::to_string((this->path.back() - '0') + 1)),
Chris Caina8857c52021-01-27 11:53:05 -060028 occInstance(this->path.back() - '0'),
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +053029 activeStatusSignal(
George Liuf3b75142021-06-10 11:22:50 +080030 utils::getBus(),
31 sdbusRule::propertiesChanged(path, "org.open_power.OCC.Status"),
Gunnar Mills94df8c92018-09-14 14:50:03 -050032 std::bind(std::mem_fn(&PassThrough::activeStatusEvent), this,
Chris Caina8857c52021-01-27 11:53:05 -060033 std::placeholders::_1)),
George Liuf3b75142021-06-10 11:22:50 +080034 occCmd(occInstance, path)
Deepak Kodihalli6b492fb2017-03-18 01:09:28 -050035{
Vishwanatha Subbanna38b08d72017-04-14 18:12:14 +053036 // Nothing to do.
37}
38
Deepak Kodihalli6b492fb2017-03-18 01:09:28 -050039std::vector<int32_t> PassThrough::send(std::vector<int32_t> command)
40{
Gunnar Mills94df8c92018-09-14 14:50:03 -050041 std::vector<int32_t> response{};
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +053042
Vishwanatha Subbanna7d700e22017-05-19 19:58:01 +053043 // OCC only understands [bytes] so need array of bytes. Doing this
44 // because rest-server currently treats all int* as 32 bit integer.
Chris Caina8857c52021-01-27 11:53:05 -060045 std::vector<uint8_t> cmdInBytes, rsp;
Vishwanatha Subbanna7d700e22017-05-19 19:58:01 +053046 cmdInBytes.resize(command.size());
47
48 // Populate uint8_t version of vector.
49 std::transform(command.begin(), command.end(), cmdInBytes.begin(),
Gunnar Mills94df8c92018-09-14 14:50:03 -050050 [](decltype(cmdInBytes)::value_type x) { return x; });
Vishwanatha Subbanna7d700e22017-05-19 19:58:01 +053051
Chris Caina8857c52021-01-27 11:53:05 -060052 rsp = send(cmdInBytes);
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +053053
Chris Caina8857c52021-01-27 11:53:05 -060054 response.resize(rsp.size());
55 std::transform(rsp.begin(), rsp.end(), response.begin(),
56 [](decltype(response)::value_type x) { return x; });
57
58 return response;
59}
60
61std::vector<uint8_t> PassThrough::send(std::vector<uint8_t> command)
62{
63 using namespace phosphor::logging;
64 using namespace sdbusplus::org::open_power::OCC::Device::Error;
65
66 std::vector<uint8_t> response{};
67
68 log<level::DEBUG>(
69 fmt::format("PassThrough::send() Sending 0x{:02X} command to OCC{}",
70 command.front(), occInstance)
71 .c_str());
72 CmdStatus status = occCmd.send(command, response);
73 if (status == CmdStatus::SUCCESS)
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +053074 {
Chris Caina8857c52021-01-27 11:53:05 -060075 if (response.size() >= 5)
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +053076 {
George Liub5ca1012021-09-10 12:53:11 +080077 log<level::DEBUG>(
78 fmt::format("PassThrough::send() response had {} bytes",
79 response.size())
80 .c_str());
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +053081 }
82 else
83 {
Chris Caina8857c52021-01-27 11:53:05 -060084 log<level::ERR>("PassThrough::send() Invalid OCC response");
85 dump_hex(response);
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +053086 }
87 }
Chris Caina8857c52021-01-27 11:53:05 -060088 else
89 {
90 if (status == CmdStatus::OPEN_FAILURE)
91 {
92 log<level::WARNING>("PassThrough::send() - OCC not active yet");
93 }
94 else
95 {
96 log<level::ERR>("PassThrough::send() - OCC command failed!");
97 }
98 }
Eddie James4f4712d2018-06-21 15:57:02 -050099
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +0530100 return response;
Deepak Kodihalli6b492fb2017-03-18 01:09:28 -0500101}
102
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +0530103// Called at OCC Status change signal
104void PassThrough::activeStatusEvent(sdbusplus::message::message& msg)
105{
106 std::string statusInterface;
Patrick Williamse0962702020-05-13 17:50:22 -0500107 std::map<std::string, std::variant<bool>> msgData;
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +0530108 msg.read(statusInterface, msgData);
109
110 auto propertyMap = msgData.find("OccActive");
111 if (propertyMap != msgData.end())
112 {
113 // Extract the OccActive property
Patrick Williams305ff8b2020-05-13 11:17:39 -0500114 if (std::get<bool>(propertyMap->second))
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +0530115 {
Eddie James4f4712d2018-06-21 15:57:02 -0500116 occActive = true;
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +0530117 }
118 else
119 {
Eddie James4f4712d2018-06-21 15:57:02 -0500120 occActive = false;
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +0530121 }
122 }
123 return;
124}
125
Deepak Kodihalli6b492fb2017-03-18 01:09:28 -0500126} // namespace occ
127} // namespace open_power