1 // Copyright 2021 Google LLC
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 #include "nemora_types.hpp"
17 
18 #include <sdbusplus/bus.hpp>
19 #include <sdbusplus/message.hpp>
20 #include <sdbusplus/server.hpp>
21 
22 #include <mutex>
23 #include <queue>
24 #include <string>
25 #include <unordered_map>
26 #include <vector>
27 
28 #define POSTCODE_OBJECTPATH "/xyz/openbmc_project/state/boot/raw0"
29 #define POSTCODE_BUSNAME "xyz.openbmc_project.State.Boot.Raw"
30 
31 class HostManager
32 {
33   public:
34     HostManager();
35     ~HostManager() = default;
36 
37     /**
38      * Callback for POST code DBus listener
39      * - msg: out-parameter for message received over DBus from callback
40      *
41      * - returns: error code or 0 for success
42      */
43     int DbusHandleSignal(sdbusplus::message::message& msg);
44 
45     /**
46      * Helper to construct match string for callback registration for POST
47      * listener
48      * - returns: match string for use in registering callback
49      */
50     static std::string GetMatch();
51 
52     /**
53      * Copies contents of POSTcode vector away to allow for sending via UDP
54      * - returns: vector filled with current state of postcodes_
55      */
56     std::vector<uint64_t> DrainPostcodes();
57 
58     /**
59      * Add POST code to vector, thread-safely
60      * - postcode: POST code to add, typically only 8 or 16 bits wide
61      */
62     void PushPostcode(uint64_t postcode);
63 
64   private:
65     /**
66      * Business logic of thread listening to DBus for POST codes
67      */
68     void PostPollerThread();
69 
70     // It's important that postcodes_ be initialized before post_poller_!
71     std::vector<uint64_t> postcodes_;
72     std::mutex postcodes_lock_;
73 
74     sdbusplus::bus::bus bus_;
75     sdbusplus::server::match::match signal_;
76     std::unique_ptr<std::thread> post_poller_;
77     bool post_poller_enabled_;
78 };
79