summaryrefslogtreecommitdiffstats
path: root/mainloop.hpp
blob: cff512359a1c2f2784963a2d4648c99d86ffbe14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#pragma once

#include <string>
#include <vector>
#include <experimental/any>
#include <sdbusplus/server.hpp>
#include "sensorset.hpp"
#include "interface.hpp"

using Object = std::map<InterfaceType, std::experimental::any>;

/** @class MainLoop
 *  @brief hwmon-readd main application loop.
 */
class MainLoop
{
    public:
        MainLoop() = delete;
        MainLoop(const MainLoop&) = delete;
        MainLoop& operator=(const MainLoop&) = delete;
        MainLoop(MainLoop&&) = default;
        MainLoop& operator=(MainLoop&&) = default;
        ~MainLoop() = default;

        /** @brief Constructor
         *
         *  @param[in] bus - sdbusplus bus client connection.
         *  @param[in] path - hwmon sysfs instance to manage
         *  @param[in] prefix - DBus busname prefix.
         *  @param[in] root - DBus sensors namespace root.
         *
         *  Any DBus objects are created relative to the DBus
         *  sensors namespace root.
         *
         *  At startup, the application will own a busname with
         *  the format <prefix>.hwmon<n>.
         */
        MainLoop(
            sdbusplus::bus::bus&& bus,
            const std::string& path,
            const char* prefix,
            const char* root);

        /** @brief Start polling loop and process dbus traffic. */
        void run();

        /** @brief Stop loop from another thread.
         *
         *  Typically only used by testcases.
         */
        void shutdown() noexcept;

    private:
        using mapped_type = std::tuple<SensorSet::mapped_type, std::string, Object>;
        using SensorState = std::map<SensorSet::key_type, mapped_type>;

        /** @brief sdbusplus bus client connection. */
        sdbusplus::bus::bus _bus;
        /** @brief sdbusplus freedesktop.ObjectManager storage. */
        sdbusplus::server::manager::manager _manager;
        /** @brief Shutdown requested. */
        volatile bool _shutdown;
        /** @brief Path to hwmon sysfs instance. */
        std::string _path;
        /** @brief DBus busname prefix. */
        const char* _prefix;
        /** @brief DBus sensors namespace root. */
        const char* _root;
        /** @brief DBus object state. */
        SensorState state;
};
OpenPOWER on IntegriCloud