summaryrefslogtreecommitdiff
path: root/src/state/active_state.hpp
blob: 541e27ea7f8a6a1fc24290b3955684991747fd38 (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
#pragma once

#include "basic_state.hpp"
#include "deactivating_state.hpp"

struct ActiveState : public BasicStateT<ActiveState>
{
    static std::string_view stateName()
    {
        return "ActiveState";
    }

    ActiveState(interfaces::MountPointStateMachine& machine,
                std::unique_ptr<resource::Process> process,
                std::unique_ptr<resource::Gadget> gadget) :
        BasicStateT(machine),
        process(std::move(process)), gadget(std::move(gadget)){};

    virtual std::unique_ptr<BasicState> onEnter()
    {
        handler = [this](const boost::system::error_code& ec) {
            if (ec)
            {
                return;
            }

            auto now = std::chrono::steady_clock::now();

            auto stats = UsbGadget::getStats(std::string(machine.getName()));
            if (stats && (*stats != lastStats))
            {
                lastStats = std::move(*stats);
                lastAccess = now;
            }

            auto timeSinceLastAccess =
                std::chrono::duration_cast<std::chrono::seconds>(now -
                                                                 lastAccess);
            if (timeSinceLastAccess >= Configuration::inactivityTimeout)
            {
                LogMsg(Logger::Info, machine.getName(),
                       " Inactivity timer expired (",
                       Configuration::inactivityTimeout.count(),
                       "s) - Unmounting");
                // unmount media & stop retriggering timer
                boost::asio::spawn(
                    machine.getIoc(),
                    [& machine = machine](boost::asio::yield_context yield) {
                        machine.emitUnmountEvent();
                    });
                return;
            }
            else
            {
                machine.getConfig().remainingInactivityTimeout =
                    Configuration::inactivityTimeout - timeSinceLastAccess;
            }

            timer.expires_from_now(std::chrono::seconds(1));
            timer.async_wait(handler);
        };
        timer.expires_from_now(std::chrono::seconds(1));
        timer.async_wait(handler);

        return nullptr;
    }

    std::unique_ptr<BasicState> handleEvent(UdevStateChangeEvent event)
    {
        return std::make_unique<DeactivatingState>(
            machine, std::move(process), std::move(gadget), std::move(event));
    }

    std::unique_ptr<BasicState> handleEvent(SubprocessStoppedEvent event)
    {
        return std::make_unique<DeactivatingState>(
            machine, std::move(process), std::move(gadget), std::move(event));
    }

    std::unique_ptr<BasicState> handleEvent(UnmountEvent event)
    {
        return std::make_unique<DeactivatingState>(machine, std::move(process),
                                                   std::move(gadget));
    }

    template <class AnyEvent>
    std::unique_ptr<BasicState> handleEvent(AnyEvent event)
    {
        LogMsg(Logger::Error, "Invalid event: ", event.eventName);
        return nullptr;
    }

  private:
    boost::asio::steady_timer timer{machine.getIoc()};
    std::unique_ptr<resource::Process> process;
    std::unique_ptr<resource::Gadget> gadget;
    std::function<void(const boost::system::error_code&)> handler;
    std::chrono::time_point<std::chrono::steady_clock> lastAccess;
    std::string lastStats;
};