xref: /openbmc/google-ipmi-sys/cable.cpp (revision 8d618532)
1a2056e9cSWilly Tu // Copyright 2021 Google LLC
2a2056e9cSWilly Tu //
3a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
4a2056e9cSWilly Tu // you may not use this file except in compliance with the License.
5a2056e9cSWilly Tu // You may obtain a copy of the License at
6a2056e9cSWilly Tu //
7a2056e9cSWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
8a2056e9cSWilly Tu //
9a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software
10a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
11a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a2056e9cSWilly Tu // See the License for the specific language governing permissions and
13a2056e9cSWilly Tu // limitations under the License.
144d49ae65SPatrick Venture 
154d49ae65SPatrick Venture #include "cable.hpp"
164d49ae65SPatrick Venture 
170e9aae5dSPatrick Venture #include "commands.hpp"
18d2037c6aSPatrick Venture #include "errors.hpp"
19d2037c6aSPatrick Venture #include "handler.hpp"
204d49ae65SPatrick Venture 
21444b5ea4SPatrick Williams #include <ipmid/api-types.hpp>
22*8d618532SMichael Shen #include <stdplus/print.hpp>
23444b5ea4SPatrick Williams 
244d49ae65SPatrick Venture #include <cstdint>
25ce07ee0aSPatrick Venture #include <cstring>
26b4e3704cSWilly Tu #include <span>
274d49ae65SPatrick Venture #include <string>
28ff3cd8e9SWilly Tu #include <vector>
294d49ae65SPatrick Venture 
304d49ae65SPatrick Venture namespace google
314d49ae65SPatrick Venture {
324d49ae65SPatrick Venture namespace ipmi
334d49ae65SPatrick Venture {
344d49ae65SPatrick Venture 
354d49ae65SPatrick Venture struct CableRequest
364d49ae65SPatrick Venture {
3745fad1bbSPatrick Venture     uint8_t ifNameLength;
384d49ae65SPatrick Venture } __attribute__((packed));
394d49ae65SPatrick Venture 
cableCheck(std::span<const uint8_t> data,const HandlerInterface * handler)40b4e3704cSWilly Tu Resp cableCheck(std::span<const uint8_t> data, const HandlerInterface* handler)
414d49ae65SPatrick Venture {
424d49ae65SPatrick Venture     // There is an IPMI LAN channel statistics command which could be used for
434d49ae65SPatrick Venture     // this type of check, however, we're not able to wait for the OpenBMC
444d49ae65SPatrick Venture     // implementation to stabilize related to the network management.
454d49ae65SPatrick Venture     //
464d49ae65SPatrick Venture     // There is a link status file, but it is "unknown" to start with...
474d49ae65SPatrick Venture     // The path we're checking: /sys/class/net/eth1/statistics/rx_packets
484d49ae65SPatrick Venture 
4945fad1bbSPatrick Venture     // This command is expecting: [0x00][len][ifName]
50ff3cd8e9SWilly Tu     // data should have [len][ifName]
51ff3cd8e9SWilly Tu     if (data.size() < sizeof(struct CableRequest))
524d49ae65SPatrick Venture     {
53*8d618532SMichael Shen         stdplus::print(stderr, "Invalid command length: {}\n", data.size());
54ff3cd8e9SWilly Tu         return ::ipmi::responseReqDataLenInvalid();
554d49ae65SPatrick Venture     }
564d49ae65SPatrick Venture 
574d49ae65SPatrick Venture     const auto request =
58ff3cd8e9SWilly Tu         reinterpret_cast<const struct CableRequest*>(data.data());
594d49ae65SPatrick Venture 
604d49ae65SPatrick Venture     // Sanity check the object contents.
6145fad1bbSPatrick Venture     if (request->ifNameLength == 0)
624d49ae65SPatrick Venture     {
63*8d618532SMichael Shen         stdplus::print(stderr, "Invalid string length: {}\n",
6445fad1bbSPatrick Venture                        request->ifNameLength);
65ff3cd8e9SWilly Tu         return ::ipmi::responseReqDataLenInvalid();
664d49ae65SPatrick Venture     }
674d49ae65SPatrick Venture 
684d49ae65SPatrick Venture     // Verify the request buffer contains the object and the string.
69ff3cd8e9SWilly Tu     if (data.size() < (sizeof(struct CableRequest) + request->ifNameLength))
704d49ae65SPatrick Venture     {
71*8d618532SMichael Shen         stdplus::print(stderr, "*dataLen too small: {}\n", data.size());
72ff3cd8e9SWilly Tu         return ::ipmi::responseReqDataLenInvalid();
734d49ae65SPatrick Venture     }
744d49ae65SPatrick Venture 
754d49ae65SPatrick Venture     // Maximum length one can specify, plus null terminator.
764d49ae65SPatrick Venture     char nameBuf[256] = {};
774d49ae65SPatrick Venture     // Copy the string out of the request buffer.
785d789734SWilliam A. Kennington III     std::memcpy(&nameBuf[0], request + 1, request->ifNameLength);
794d49ae65SPatrick Venture     std::string name = nameBuf;
80d2037c6aSPatrick Venture     int64_t count;
814d49ae65SPatrick Venture 
824d49ae65SPatrick Venture     try
834d49ae65SPatrick Venture     {
84d2037c6aSPatrick Venture         count = handler->getRxPackets(name);
854d49ae65SPatrick Venture     }
86d2037c6aSPatrick Venture     catch (const IpmiException& e)
874d49ae65SPatrick Venture     {
88ff3cd8e9SWilly Tu         return ::ipmi::response(e.getIpmiError());
894d49ae65SPatrick Venture     }
904d49ae65SPatrick Venture 
914d49ae65SPatrick Venture     // If we have received packets then there is a cable present.
92ff3cd8e9SWilly Tu     std::uint8_t value = (count > 0) ? 1 : 0;
934d49ae65SPatrick Venture 
94ff3cd8e9SWilly Tu     return ::ipmi::responseSuccess(SysOEMCommands::SysCableCheck,
95ff3cd8e9SWilly Tu                                    std::vector<std::uint8_t>{value});
964d49ae65SPatrick Venture }
974d49ae65SPatrick Venture 
984d49ae65SPatrick Venture } // namespace ipmi
994d49ae65SPatrick Venture } // namespace google
100