| Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (c)  2018 Intel 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 |  | 
|  | 17 | #pragma once | 
| Patrick Williams | 2405ae9 | 2023-05-10 07:50:09 -0500 | [diff] [blame] | 18 | #include "config.h" | 
|  | 19 |  | 
|  | 20 | #include <ipmid/api.hpp> | 
| Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 21 | #include <sdbusplus/bus.hpp> | 
|  | 22 |  | 
| Vijay Khemka | 63c99be | 2020-05-27 19:14:35 -0700 | [diff] [blame] | 23 | #include <iostream> | 
|  | 24 |  | 
| Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 25 | static constexpr bool debug = false; | 
|  | 26 |  | 
| Bonnie Lo | 25b79bf | 2022-12-16 15:41:48 +0800 | [diff] [blame] | 27 | using IanaType = std::array<uint8_t, 3>; | 
| Kumar Thangavel | 05d0ce9 | 2022-11-17 17:53:57 +0530 | [diff] [blame] | 28 | using flashSize = std::array<uint8_t, 4>; | 
| Bonnie Lo | 25b79bf | 2022-12-16 15:41:48 +0800 | [diff] [blame] | 29 |  | 
|  | 30 | static constexpr IanaType iana = {0x15, 0xA0, 0x0}; // Meta's IANA | 
|  | 31 |  | 
| Jayashree Dhanapal | 4ec8056 | 2022-06-28 15:41:47 +0530 | [diff] [blame] | 32 | static void instances(std::string s, std::vector<std::string>& host) | 
|  | 33 | { | 
|  | 34 | size_t pos = 0; | 
|  | 35 |  | 
|  | 36 | while ((pos = s.find(" ")) != std::string::npos) | 
|  | 37 | { | 
|  | 38 | host.push_back(s.substr(0, pos)); | 
|  | 39 | s.erase(0, pos + 1); | 
|  | 40 | } | 
|  | 41 | host.push_back(s); | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | inline std::optional<size_t> findHost(size_t id) | 
|  | 45 | { | 
|  | 46 | std::vector<std::string> hosts = {}; | 
|  | 47 |  | 
|  | 48 | if (hostInstances == "0") | 
|  | 49 | { | 
|  | 50 | return id; | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | instances(hostInstances, hosts); | 
|  | 54 |  | 
|  | 55 | std::sort(hosts.begin(), hosts.end()); | 
|  | 56 |  | 
|  | 57 | /*  ctx host ID for host 1 is 0, host 2 is 1 ... host N is N-1 | 
|  | 58 | *  Therefore, host Index is incremented to 1 to map with the dbus | 
|  | 59 | *  object path created. | 
|  | 60 | */ | 
|  | 61 | std::string num = std::to_string(id + 1); | 
|  | 62 |  | 
|  | 63 | auto instance = std::lower_bound(hosts.begin(), hosts.end(), num); | 
|  | 64 |  | 
|  | 65 | if (instance != std::end(hosts)) | 
|  | 66 | { | 
|  | 67 | return id + 1; | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | return std::nullopt; | 
|  | 71 | } | 
|  | 72 |  | 
| Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 73 | inline static void printRegistration(unsigned int netfn, unsigned int cmd) | 
|  | 74 | { | 
|  | 75 | if constexpr (debug) | 
|  | 76 | { | 
|  | 77 | std::cout << "Registering NetFn:[0x" << std::hex << std::uppercase | 
|  | 78 | << netfn << "], Cmd:[0x" << cmd << "]\n"; | 
|  | 79 | } | 
|  | 80 | } | 
|  | 81 |  | 
| Patrick Williams | 010dee0 | 2024-08-16 15:19:44 -0400 | [diff] [blame] | 82 | inline static void ipmiPrintAndRegister( | 
|  | 83 | ipmi_netfn_t netfn, ipmi_cmd_t cmd, ipmi_context_t context, | 
|  | 84 | ipmid_callback_t handler, ipmi_cmd_privilege_t priv) | 
| Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 85 | { | 
|  | 86 | printRegistration(netfn, cmd); | 
|  | 87 | ipmi_register_callback(netfn, cmd, context, handler, priv); | 
|  | 88 | } | 
|  | 89 |  | 
|  | 90 | inline static void printCommand(unsigned int netfn, unsigned int cmd) | 
|  | 91 | { | 
|  | 92 | if constexpr (debug) | 
|  | 93 | { | 
|  | 94 | std::cout << "Executing NetFn:[0x" << std::hex << std::uppercase | 
|  | 95 | << netfn << "], Cmd:[0x" << cmd << "]\n"; | 
|  | 96 | } | 
|  | 97 | } | 
|  | 98 |  | 
| cchoux | b2ae88b | 2023-09-13 00:35:36 +0800 | [diff] [blame] | 99 | std::optional<std::pair<uint8_t, uint8_t>> getMbFruDevice(void); | 
|  | 100 |  | 
| Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 101 | namespace ipmi | 
|  | 102 | { | 
| Patrick Williams | bec18ea | 2020-05-13 17:43:23 -0500 | [diff] [blame] | 103 | using DbusVariant = std::variant<std::string, bool, uint8_t, uint16_t, int16_t, | 
|  | 104 | uint32_t, int32_t, uint64_t, int64_t, double>; | 
| Vijay Khemka | e7d23d0 | 2019-03-08 13:13:40 -0800 | [diff] [blame] | 105 | } // namespace ipmi |