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

#include "activating_state.hpp"
#include "basic_state.hpp"
#include "logger.hpp"

#include <chrono>
#include <memory>
#include <optional>
#include <string>
#include <system_error>

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

    struct Error
    {
        std::errc code;
        std::string message;
    };

    ReadyState(interfaces::MountPointStateMachine& machine) :
        BasicStateT(machine)
    {
        machine.notify();
    };

    ReadyState(interfaces::MountPointStateMachine& machine, const std::errc& ec,
               const std::string& message) :
        BasicStateT(machine),
        error{{ec, message}}
    {
        LogMsg(Logger::Error, machine.getName(),
               " Errno = ", static_cast<int>(ec), " : ", message);
        machine.notify(std::make_error_code(ec));
    }

    std::unique_ptr<BasicState> onEnter() override
    {
        // Cleanup after previously mounted device
        LogMsg(Logger::Debug, "exitCode: ", machine.getExitCode());
        machine.getTarget() = std::nullopt;
        machine.getConfig().remainingInactivityTimeout =
            std::chrono::seconds(0);
        return nullptr;
    }

    std::unique_ptr<BasicState> handleEvent(MountEvent event)
    {
        machine.notificationStart();
        if (event.target)
        {
            machine.getTarget() = std::move(event.target);
        }
        return std::make_unique<ActivatingState>(machine);
    }

    [[noreturn]] std::unique_ptr<BasicState> handleEvent(UnmountEvent event)
    {
        LogMsg(Logger::Error, "Invalid event: ", event.eventName);
        throw sdbusplus::exception::SdBusError(
            EPERM, "Operation not permitted in ready state");
    }

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

    std::optional<Error> error;
};