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