summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/time/system_clock_core.cpp
blob: da078241f755e9781aab10534a8c4a33c1020c49 (plain) (blame)
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
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "core/hle/service/time/steady_clock_core.h"
#include "core/hle/service/time/system_clock_context_update_callback.h"
#include "core/hle/service/time/system_clock_core.h"

namespace Service::Time::Clock {

SystemClockCore::SystemClockCore(SteadyClockCore& steady_clock_core_)
    : steady_clock_core{steady_clock_core_} {
    context.steady_time_point.clock_source_id = steady_clock_core.GetClockSourceId();
}

SystemClockCore::~SystemClockCore() = default;

Result SystemClockCore::GetCurrentTime(Core::System& system, s64& posix_time) const {
    posix_time = 0;

    const SteadyClockTimePoint current_time_point{steady_clock_core.GetCurrentTimePoint(system)};

    SystemClockContext clock_context{};
    if (const Result result{GetClockContext(system, clock_context)}; result != ResultSuccess) {
        return result;
    }

    if (current_time_point.clock_source_id != clock_context.steady_time_point.clock_source_id) {
        return ERROR_TIME_MISMATCH;
    }

    posix_time = clock_context.offset + current_time_point.time_point;

    return ResultSuccess;
}

Result SystemClockCore::SetCurrentTime(Core::System& system, s64 posix_time) {
    const SteadyClockTimePoint current_time_point{steady_clock_core.GetCurrentTimePoint(system)};
    const SystemClockContext clock_context{posix_time - current_time_point.time_point,
                                           current_time_point};

    if (const Result result{SetClockContext(clock_context)}; result != ResultSuccess) {
        return result;
    }
    return Flush(clock_context);
}

Result SystemClockCore::Flush(const SystemClockContext& clock_context) {
    if (!system_clock_context_update_callback) {
        return ResultSuccess;
    }
    return system_clock_context_update_callback->Update(clock_context);
}

Result SystemClockCore::SetSystemClockContext(const SystemClockContext& clock_context) {
    if (const Result result{SetClockContext(clock_context)}; result != ResultSuccess) {
        return result;
    }
    return Flush(clock_context);
}

bool SystemClockCore::IsClockSetup(Core::System& system) const {
    SystemClockContext value{};
    if (GetClockContext(system, value) == ResultSuccess) {
        const SteadyClockTimePoint steady_clock_time_point{
            steady_clock_core.GetCurrentTimePoint(system)};
        return steady_clock_time_point.clock_source_id == value.steady_time_point.clock_source_id;
    }
    return {};
}

} // namespace Service::Time::Clock