summaryrefslogtreecommitdiffstats
path: root/sdevent/io.hpp
blob: f511cf0852a0b71118e07d3a93b50826a5c8bab9 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once

#include <functional>
#include <memory>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <systemd/sd-event.h>
#include <xyz/openbmc_project/Common/error.hpp>

#include "sdevent/source.hpp"
#include "sdevent/event.hpp"

namespace sdevent
{
namespace event
{
namespace io
{

using namespace phosphor::logging;
/** @class IO
 *  @brief Provides C++ bindings to the sd_event_source* io functions.
 */
class IO
{
    private:
        using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
            Error::InternalFailure;

    public:
        /* Define all of the basic class operations:
         *     Not allowed:
         *         - Default constructor to avoid nullptrs.
         *         - Copy operations due to internal unique_ptr.
         *     Allowed:
         *         - Move operations.
         *         - Destructor.
         */
        IO() = delete;
        IO(const IO&) = delete;
        IO& operator=(const IO&) = delete;
        IO(IO&&) = default;
        IO& operator=(IO&&) = default;
        ~IO() = default;

        using Callback = std::function<void(source::Source&)>;

        /** @brief Register an io callback.
         *
         *  @param[in] event - The event to register on.
         *  @param[in] fd - The file descriptor to poll.
         *  @param[in] callback - The callback method.
         */
        IO(
            sdevent::event::Event& event,
            int fd,
            Callback callback)
            : src(nullptr),
              cb(std::make_unique<Callback>(std::move(callback)))
        {
            sd_event_source* source = nullptr;
            auto rc = sd_event_add_io(
                event.get(),
                &source,
                fd,
                EPOLLIN,
                callCallback,
                cb.get());
            if (rc < 0)
            {
                log<level::ERR>("Error in call to sd_event_add_io",
                        entry("RC=%d", rc),
                        entry("FD=%d", fd));
                elog<InternalFailure>();
            }

            src = decltype(src){source, std::false_type()};
        }

        /** @brief Set the IO source enable state. */
        void enable(int enable)
        {
            src.enable(enable);
        }

        /** @brief Query the IO enable state. */
        auto enabled()
        {
            return src.enabled();
        }

    private:
        source::Source src;
        std::unique_ptr<Callback> cb = nullptr;

        static int callCallback(sd_event_source* s, int fd, uint32_t events,
                                void* context)
        {
            source::Source source(s);
            auto c = static_cast<Callback*>(context);
            (*c)(source);

            return 0;
        }
};
} // namespace io
} // namespace event
} // namespace sdevent
OpenPOWER on IntegriCloud