xref: /openbmc/google-misc/acpi-power-state-daemon/subprojects/nemora-postd/src/host_manager.hpp (revision 92809b4e27af8f7e918f2a7b7594e851d5f384f2)
114fe6698SNan Zhou // Copyright 2021 Google LLC
214fe6698SNan Zhou //
314fe6698SNan Zhou // Licensed under the Apache License, Version 2.0 (the "License");
414fe6698SNan Zhou // you may not use this file except in compliance with the License.
514fe6698SNan Zhou // You may obtain a copy of the License at
614fe6698SNan Zhou //
714fe6698SNan Zhou //      http://www.apache.org/licenses/LICENSE-2.0
814fe6698SNan Zhou //
914fe6698SNan Zhou // Unless required by applicable law or agreed to in writing, software
1014fe6698SNan Zhou // distributed under the License is distributed on an "AS IS" BASIS,
1114fe6698SNan Zhou // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1214fe6698SNan Zhou // See the License for the specific language governing permissions and
1314fe6698SNan Zhou // limitations under the License.
1414fe6698SNan Zhou 
1514fe6698SNan Zhou #pragma once
1614fe6698SNan Zhou #include "nemora_types.hpp"
1714fe6698SNan Zhou 
1814fe6698SNan Zhou #include <sdbusplus/bus.hpp>
19*92809b4eSPatrick Williams #include <sdbusplus/bus/match.hpp>
2014fe6698SNan Zhou #include <sdbusplus/message.hpp>
2114fe6698SNan Zhou 
2214fe6698SNan Zhou #include <mutex>
2314fe6698SNan Zhou #include <queue>
2414fe6698SNan Zhou #include <string>
25adc7133bSPatrick Williams #include <thread>
2614fe6698SNan Zhou #include <unordered_map>
2714fe6698SNan Zhou #include <vector>
2814fe6698SNan Zhou 
2914fe6698SNan Zhou #define POSTCODE_OBJECTPATH "/xyz/openbmc_project/state/boot/raw0"
3014fe6698SNan Zhou #define POSTCODE_BUSNAME "xyz.openbmc_project.State.Boot.Raw"
3114fe6698SNan Zhou 
3214fe6698SNan Zhou class HostManager
3314fe6698SNan Zhou {
3414fe6698SNan Zhou   public:
3514fe6698SNan Zhou     HostManager();
3614fe6698SNan Zhou     ~HostManager() = default;
3714fe6698SNan Zhou 
3814fe6698SNan Zhou     /**
3914fe6698SNan Zhou      * Callback for POST code DBus listener
4014fe6698SNan Zhou      * - msg: out-parameter for message received over DBus from callback
4114fe6698SNan Zhou      *
4214fe6698SNan Zhou      * - returns: error code or 0 for success
4314fe6698SNan Zhou      */
44*92809b4eSPatrick Williams     int DbusHandleSignal(sdbusplus::message_t& msg);
4514fe6698SNan Zhou 
4614fe6698SNan Zhou     /**
4714fe6698SNan Zhou      * Helper to construct match string for callback registration for POST
4814fe6698SNan Zhou      * listener
4914fe6698SNan Zhou      * - returns: match string for use in registering callback
5014fe6698SNan Zhou      */
5114fe6698SNan Zhou     static std::string GetMatch();
5214fe6698SNan Zhou 
5314fe6698SNan Zhou     /**
5414fe6698SNan Zhou      * Copies contents of POSTcode vector away to allow for sending via UDP
5514fe6698SNan Zhou      * - returns: vector filled with current state of postcodes_
5614fe6698SNan Zhou      */
5714fe6698SNan Zhou     std::vector<uint64_t> DrainPostcodes();
5814fe6698SNan Zhou 
5914fe6698SNan Zhou     /**
6014fe6698SNan Zhou      * Add POST code to vector, thread-safely
6114fe6698SNan Zhou      * - postcode: POST code to add, typically only 8 or 16 bits wide
6214fe6698SNan Zhou      */
6314fe6698SNan Zhou     void PushPostcode(uint64_t postcode);
6414fe6698SNan Zhou 
6514fe6698SNan Zhou   private:
6614fe6698SNan Zhou     /**
6714fe6698SNan Zhou      * Business logic of thread listening to DBus for POST codes
6814fe6698SNan Zhou      */
6914fe6698SNan Zhou     void PostPollerThread();
7014fe6698SNan Zhou 
7114fe6698SNan Zhou     // It's important that postcodes_ be initialized before post_poller_!
7214fe6698SNan Zhou     std::vector<uint64_t> postcodes_;
7314fe6698SNan Zhou     std::mutex postcodes_lock_;
7414fe6698SNan Zhou 
75*92809b4eSPatrick Williams     sdbusplus::bus_t bus_;
76*92809b4eSPatrick Williams     sdbusplus::bus::match_t signal_;
7714fe6698SNan Zhou     std::unique_ptr<std::thread> post_poller_;
7814fe6698SNan Zhou     bool post_poller_enabled_;
7914fe6698SNan Zhou };
80