blob: e318f135ce530e9ad948f394002d828ba6722bbc [file] [log] [blame]
Vijay Khemkae7d23d02019-03-08 13:13:40 -08001/*
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
Vijay Khemkae7d23d02019-03-08 13:13:40 -080018#include <sdbusplus/bus.hpp>
19
Vijay Khemka63c99be2020-05-27 19:14:35 -070020#include <iostream>
Jayashree Dhanapal4ec80562022-06-28 15:41:47 +053021#include "config.h"
Vijay Khemka63c99be2020-05-27 19:14:35 -070022
Vijay Khemkae7d23d02019-03-08 13:13:40 -080023static constexpr bool debug = false;
24
Jayashree Dhanapal4ec80562022-06-28 15:41:47 +053025static void instances(std::string s, std::vector<std::string>& host)
26{
27 size_t pos = 0;
28
29 while ((pos = s.find(" ")) != std::string::npos)
30 {
31 host.push_back(s.substr(0, pos));
32 s.erase(0, pos + 1);
33 }
34 host.push_back(s);
35}
36
37inline std::optional<size_t> findHost(size_t id)
38{
39 std::vector<std::string> hosts = {};
40
41 if (hostInstances == "0")
42 {
43 return id;
44 }
45
46 instances(hostInstances, hosts);
47
48 std::sort(hosts.begin(), hosts.end());
49
50 /* ctx host ID for host 1 is 0, host 2 is 1 ... host N is N-1
51 * Therefore, host Index is incremented to 1 to map with the dbus
52 * object path created.
53 */
54 std::string num = std::to_string(id + 1);
55
56 auto instance = std::lower_bound(hosts.begin(), hosts.end(), num);
57
58 if (instance != std::end(hosts))
59 {
60 return id + 1;
61 }
62
63 return std::nullopt;
64}
65
Vijay Khemkae7d23d02019-03-08 13:13:40 -080066inline static void printRegistration(unsigned int netfn, unsigned int cmd)
67{
68 if constexpr (debug)
69 {
70 std::cout << "Registering NetFn:[0x" << std::hex << std::uppercase
71 << netfn << "], Cmd:[0x" << cmd << "]\n";
72 }
73}
74
75inline static void ipmiPrintAndRegister(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
76 ipmi_context_t context,
77 ipmid_callback_t handler,
78 ipmi_cmd_privilege_t priv)
79{
80 printRegistration(netfn, cmd);
81 ipmi_register_callback(netfn, cmd, context, handler, priv);
82}
83
84inline static void printCommand(unsigned int netfn, unsigned int cmd)
85{
86 if constexpr (debug)
87 {
88 std::cout << "Executing NetFn:[0x" << std::hex << std::uppercase
89 << netfn << "], Cmd:[0x" << cmd << "]\n";
90 }
91}
92
93namespace ipmi
94{
Patrick Williamsbec18ea2020-05-13 17:43:23 -050095using DbusVariant = std::variant<std::string, bool, uint8_t, uint16_t, int16_t,
96 uint32_t, int32_t, uint64_t, int64_t, double>;
Vijay Khemkae7d23d02019-03-08 13:13:40 -080097} // namespace ipmi