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
18 #include <sdbusplus/bus.hpp>
19 
20 #include <iostream>
21 #include "config.h"
22 
23 static constexpr bool debug = false;
24 
25 using IanaType = std::array<uint8_t, 3>;
26 using flashSize = std::array<uint8_t, 4>;
27 
28 static constexpr IanaType iana = {0x15, 0xA0, 0x0}; // Meta's IANA
29 
30 static void instances(std::string s, std::vector<std::string>& host)
31 {
32     size_t pos = 0;
33 
34     while ((pos = s.find(" ")) != std::string::npos)
35     {
36         host.push_back(s.substr(0, pos));
37         s.erase(0, pos + 1);
38     }
39     host.push_back(s);
40 }
41 
42 inline std::optional<size_t> findHost(size_t id)
43 {
44     std::vector<std::string> hosts = {};
45 
46     if (hostInstances == "0")
47     {
48         return id;
49     }
50 
51     instances(hostInstances, hosts);
52 
53     std::sort(hosts.begin(), hosts.end());
54 
55     /*  ctx host ID for host 1 is 0, host 2 is 1 ... host N is N-1
56      *  Therefore, host Index is incremented to 1 to map with the dbus
57      *  object path created.
58      */
59     std::string num = std::to_string(id + 1);
60 
61     auto instance = std::lower_bound(hosts.begin(), hosts.end(), num);
62 
63     if (instance != std::end(hosts))
64     {
65         return id + 1;
66     }
67 
68     return std::nullopt;
69 }
70 
71 inline static void printRegistration(unsigned int netfn, unsigned int cmd)
72 {
73     if constexpr (debug)
74     {
75         std::cout << "Registering NetFn:[0x" << std::hex << std::uppercase
76                   << netfn << "], Cmd:[0x" << cmd << "]\n";
77     }
78 }
79 
80 inline static void ipmiPrintAndRegister(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
81                                         ipmi_context_t context,
82                                         ipmid_callback_t handler,
83                                         ipmi_cmd_privilege_t priv)
84 {
85     printRegistration(netfn, cmd);
86     ipmi_register_callback(netfn, cmd, context, handler, priv);
87 }
88 
89 inline static void printCommand(unsigned int netfn, unsigned int cmd)
90 {
91     if constexpr (debug)
92     {
93         std::cout << "Executing NetFn:[0x" << std::hex << std::uppercase
94                   << netfn << "], Cmd:[0x" << cmd << "]\n";
95     }
96 }
97 
98 namespace ipmi
99 {
100 using DbusVariant = std::variant<std::string, bool, uint8_t, uint16_t, int16_t,
101                                  uint32_t, int32_t, uint64_t, int64_t, double>;
102 } // namespace ipmi
103