xref: /openbmc/google-misc/dhcp-done/subprojects/nemora-postd/src/host_manager.hpp (revision 14fe6698f5bb26245bb807eb041eaa7a3c29ac39)
1*14fe6698SNan Zhou // Copyright 2021 Google LLC
2*14fe6698SNan Zhou //
3*14fe6698SNan Zhou // Licensed under the Apache License, Version 2.0 (the "License");
4*14fe6698SNan Zhou // you may not use this file except in compliance with the License.
5*14fe6698SNan Zhou // You may obtain a copy of the License at
6*14fe6698SNan Zhou //
7*14fe6698SNan Zhou //      http://www.apache.org/licenses/LICENSE-2.0
8*14fe6698SNan Zhou //
9*14fe6698SNan Zhou // Unless required by applicable law or agreed to in writing, software
10*14fe6698SNan Zhou // distributed under the License is distributed on an "AS IS" BASIS,
11*14fe6698SNan Zhou // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*14fe6698SNan Zhou // See the License for the specific language governing permissions and
13*14fe6698SNan Zhou // limitations under the License.
14*14fe6698SNan Zhou 
15*14fe6698SNan Zhou #pragma once
16*14fe6698SNan Zhou #include "nemora_types.hpp"
17*14fe6698SNan Zhou 
18*14fe6698SNan Zhou #include <sdbusplus/bus.hpp>
19*14fe6698SNan Zhou #include <sdbusplus/message.hpp>
20*14fe6698SNan Zhou #include <sdbusplus/server.hpp>
21*14fe6698SNan Zhou 
22*14fe6698SNan Zhou #include <mutex>
23*14fe6698SNan Zhou #include <queue>
24*14fe6698SNan Zhou #include <string>
25*14fe6698SNan Zhou #include <unordered_map>
26*14fe6698SNan Zhou #include <vector>
27*14fe6698SNan Zhou 
28*14fe6698SNan Zhou #define POSTCODE_OBJECTPATH "/xyz/openbmc_project/state/boot/raw0"
29*14fe6698SNan Zhou #define POSTCODE_BUSNAME "xyz.openbmc_project.State.Boot.Raw"
30*14fe6698SNan Zhou 
31*14fe6698SNan Zhou class HostManager
32*14fe6698SNan Zhou {
33*14fe6698SNan Zhou   public:
34*14fe6698SNan Zhou     HostManager();
35*14fe6698SNan Zhou     ~HostManager() = default;
36*14fe6698SNan Zhou 
37*14fe6698SNan Zhou     /**
38*14fe6698SNan Zhou      * Callback for POST code DBus listener
39*14fe6698SNan Zhou      * - msg: out-parameter for message received over DBus from callback
40*14fe6698SNan Zhou      *
41*14fe6698SNan Zhou      * - returns: error code or 0 for success
42*14fe6698SNan Zhou      */
43*14fe6698SNan Zhou     int DbusHandleSignal(sdbusplus::message::message& msg);
44*14fe6698SNan Zhou 
45*14fe6698SNan Zhou     /**
46*14fe6698SNan Zhou      * Helper to construct match string for callback registration for POST
47*14fe6698SNan Zhou      * listener
48*14fe6698SNan Zhou      * - returns: match string for use in registering callback
49*14fe6698SNan Zhou      */
50*14fe6698SNan Zhou     static std::string GetMatch();
51*14fe6698SNan Zhou 
52*14fe6698SNan Zhou     /**
53*14fe6698SNan Zhou      * Copies contents of POSTcode vector away to allow for sending via UDP
54*14fe6698SNan Zhou      * - returns: vector filled with current state of postcodes_
55*14fe6698SNan Zhou      */
56*14fe6698SNan Zhou     std::vector<uint64_t> DrainPostcodes();
57*14fe6698SNan Zhou 
58*14fe6698SNan Zhou     /**
59*14fe6698SNan Zhou      * Add POST code to vector, thread-safely
60*14fe6698SNan Zhou      * - postcode: POST code to add, typically only 8 or 16 bits wide
61*14fe6698SNan Zhou      */
62*14fe6698SNan Zhou     void PushPostcode(uint64_t postcode);
63*14fe6698SNan Zhou 
64*14fe6698SNan Zhou   private:
65*14fe6698SNan Zhou     /**
66*14fe6698SNan Zhou      * Business logic of thread listening to DBus for POST codes
67*14fe6698SNan Zhou      */
68*14fe6698SNan Zhou     void PostPollerThread();
69*14fe6698SNan Zhou 
70*14fe6698SNan Zhou     // It's important that postcodes_ be initialized before post_poller_!
71*14fe6698SNan Zhou     std::vector<uint64_t> postcodes_;
72*14fe6698SNan Zhou     std::mutex postcodes_lock_;
73*14fe6698SNan Zhou 
74*14fe6698SNan Zhou     sdbusplus::bus::bus bus_;
75*14fe6698SNan Zhou     sdbusplus::server::match::match signal_;
76*14fe6698SNan Zhou     std::unique_ptr<std::thread> post_poller_;
77*14fe6698SNan Zhou     bool post_poller_enabled_;
78*14fe6698SNan Zhou };
79