1 // Copyright (c) 2021 Intel Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <boost/asio/io_context.hpp>
18 #include <sdbusplus/asio/connection.hpp>
19 
20 #define DEBUG_PRINT                                                            \
21     if constexpr (false)                                                       \
22     std::cerr
23 
24 namespace cpu_info
25 {
26 
27 /** Host states which are of interest just to cpuinfo use cases. */
28 enum class HostState
29 {
30     /** Host CPU is powered off. */
31     off,
32     /** Host CPU is powered on, but BIOS has not completed POST. */
33     postInProgress,
34     /** BIOS has completed POST. */
35     postComplete
36 };
37 
38 /** Current host state - initialized to "off" */
39 extern HostState hostState;
40 
41 /**
42  * Register D-Bus match handlers to keep hostState current. The D-Bus logic is
43  * entirely asynchronous, so hostState will never change from "off" until after
44  * this function is called and the asio event loop is running.
45  *
46  * @param[in]   conn    D-Bus ASIO connection.
47  */
48 void hostStateSetup(const std::shared_ptr<sdbusplus::asio::connection>& conn);
49 
50 /**
51  * Callback which is run whenever the HostState changes. First parameter is the
52  * old state, and second parameter is the new current state.
53  */
54 using HostStateHandler = std::function<void(HostState, HostState)>;
55 void addHostStateCallback(HostStateHandler cb);
56 
bit(uint8_t index)57 constexpr uint64_t bit(uint8_t index)
58 {
59     return (1ull << index);
60 }
61 
62 /**
63  * Extract a bitfield from an input data by shifting and masking.
64  *
65  * @tparam Dest Destination type - mostly useful to avoid an extra static_cast
66  *              at the call site. Defaults to the Src type if unspecified.
67  * @tparam Src  Automatically deduced from the first positional parameter.
68  *
69  * @param data  Input data.
70  * @param loBit 0-based index of the least significant bit to return.
71  * @param hiBit 0-based index of the most significant bit to return.
72  */
73 template <typename Dest = std::monostate, typename Src>
mask(Src data,unsigned int loBit,unsigned int hiBit)74 auto mask(Src data, unsigned int loBit, unsigned int hiBit)
75 {
76     assert(hiBit >= loBit);
77     uint64_t d = data;
78     d >>= loBit;
79     d &= (1ull << (hiBit - loBit + 1)) - 1;
80     if constexpr (std::is_same_v<Dest, std::monostate>)
81     {
82         return static_cast<Src>(d);
83     }
84     else
85     {
86         return static_cast<Dest>(d);
87     }
88 }
89 
90 namespace dbus
91 {
92 boost::asio::io_context& getIOContext();
93 std::shared_ptr<sdbusplus::asio::connection> getConnection();
94 } // namespace dbus
95 
96 } // namespace cpu_info
97