summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/pctl/pctl_module.cpp
blob: f966c5c8b0c209eda869e5b509b7bdd08362e4b2 (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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/logging/log.h"
#include "core/core.h"
#include "core/file_sys/control_metadata.h"
#include "core/file_sys/patch_manager.h"
#include "core/hle/service/ipc_helpers.h"
#include "core/hle/service/pctl/pctl.h"
#include "core/hle/service/pctl/pctl_module.h"
#include "core/hle/service/server_manager.h"

namespace Service::PCTL {

namespace Error {

constexpr Result ResultNoFreeCommunication{ErrorModule::PCTL, 101};
constexpr Result ResultStereoVisionRestricted{ErrorModule::PCTL, 104};
constexpr Result ResultNoCapability{ErrorModule::PCTL, 131};
constexpr Result ResultNoRestrictionEnabled{ErrorModule::PCTL, 181};

} // namespace Error

class IParentalControlService final : public ServiceFramework<IParentalControlService> {
public:
    explicit IParentalControlService(Core::System& system_, Capability capability_)
        : ServiceFramework{system_, "IParentalControlService"}, capability{capability_} {
        // clang-format off
        static const FunctionInfo functions[] = {
            {1, &IParentalControlService::Initialize, "Initialize"},
            {1001, &IParentalControlService::CheckFreeCommunicationPermission, "CheckFreeCommunicationPermission"},
            {1002, nullptr, "ConfirmLaunchApplicationPermission"},
            {1003, nullptr, "ConfirmResumeApplicationPermission"},
            {1004, nullptr, "ConfirmSnsPostPermission"},
            {1005, nullptr, "ConfirmSystemSettingsPermission"},
            {1006, nullptr, "IsRestrictionTemporaryUnlocked"},
            {1007, nullptr, "RevertRestrictionTemporaryUnlocked"},
            {1008, nullptr, "EnterRestrictedSystemSettings"},
            {1009, nullptr, "LeaveRestrictedSystemSettings"},
            {1010, nullptr, "IsRestrictedSystemSettingsEntered"},
            {1011, nullptr, "RevertRestrictedSystemSettingsEntered"},
            {1012, nullptr, "GetRestrictedFeatures"},
            {1013, &IParentalControlService::ConfirmStereoVisionPermission, "ConfirmStereoVisionPermission"},
            {1014, nullptr, "ConfirmPlayableApplicationVideoOld"},
            {1015, nullptr, "ConfirmPlayableApplicationVideo"},
            {1016, nullptr, "ConfirmShowNewsPermission"},
            {1017, &IParentalControlService::EndFreeCommunication, "EndFreeCommunication"},
            {1018, &IParentalControlService::IsFreeCommunicationAvailable, "IsFreeCommunicationAvailable"},
            {1031, &IParentalControlService::IsRestrictionEnabled, "IsRestrictionEnabled"},
            {1032, nullptr, "GetSafetyLevel"},
            {1033, nullptr, "SetSafetyLevel"},
            {1034, nullptr, "GetSafetyLevelSettings"},
            {1035, nullptr, "GetCurrentSettings"},
            {1036, nullptr, "SetCustomSafetyLevelSettings"},
            {1037, nullptr, "GetDefaultRatingOrganization"},
            {1038, nullptr, "SetDefaultRatingOrganization"},
            {1039, nullptr, "GetFreeCommunicationApplicationListCount"},
            {1042, nullptr, "AddToFreeCommunicationApplicationList"},
            {1043, nullptr, "DeleteSettings"},
            {1044, nullptr, "GetFreeCommunicationApplicationList"},
            {1045, nullptr, "UpdateFreeCommunicationApplicationList"},
            {1046, nullptr, "DisableFeaturesForReset"},
            {1047, nullptr, "NotifyApplicationDownloadStarted"},
            {1048, nullptr, "NotifyNetworkProfileCreated"},
            {1049, nullptr, "ResetFreeCommunicationApplicationList"},
            {1061, &IParentalControlService::ConfirmStereoVisionRestrictionConfigurable, "ConfirmStereoVisionRestrictionConfigurable"},
            {1062, &IParentalControlService::GetStereoVisionRestriction, "GetStereoVisionRestriction"},
            {1063, &IParentalControlService::SetStereoVisionRestriction, "SetStereoVisionRestriction"},
            {1064, &IParentalControlService::ResetConfirmedStereoVisionPermission, "ResetConfirmedStereoVisionPermission"},
            {1065, &IParentalControlService::IsStereoVisionPermitted, "IsStereoVisionPermitted"},
            {1201, nullptr, "UnlockRestrictionTemporarily"},
            {1202, nullptr, "UnlockSystemSettingsRestriction"},
            {1203, nullptr, "SetPinCode"},
            {1204, nullptr, "GenerateInquiryCode"},
            {1205, nullptr, "CheckMasterKey"},
            {1206, nullptr, "GetPinCodeLength"},
            {1207, nullptr, "GetPinCodeChangedEvent"},
            {1208, nullptr, "GetPinCode"},
            {1403, nullptr, "IsPairingActive"},
            {1406, nullptr, "GetSettingsLastUpdated"},
            {1411, nullptr, "GetPairingAccountInfo"},
            {1421, nullptr, "GetAccountNickname"},
            {1424, nullptr, "GetAccountState"},
            {1425, nullptr, "RequestPostEvents"},
            {1426, nullptr, "GetPostEventInterval"},
            {1427, nullptr, "SetPostEventInterval"},
            {1432, nullptr, "GetSynchronizationEvent"},
            {1451, nullptr, "StartPlayTimer"},
            {1452, nullptr, "StopPlayTimer"},
            {1453, nullptr, "IsPlayTimerEnabled"},
            {1454, nullptr, "GetPlayTimerRemainingTime"},
            {1455, nullptr, "IsRestrictedByPlayTimer"},
            {1456, nullptr, "GetPlayTimerSettings"},
            {1457, nullptr, "GetPlayTimerEventToRequestSuspension"},
            {1458, nullptr, "IsPlayTimerAlarmDisabled"},
            {1471, nullptr, "NotifyWrongPinCodeInputManyTimes"},
            {1472, nullptr, "CancelNetworkRequest"},
            {1473, nullptr, "GetUnlinkedEvent"},
            {1474, nullptr, "ClearUnlinkedEvent"},
            {1601, nullptr, "DisableAllFeatures"},
            {1602, nullptr, "PostEnableAllFeatures"},
            {1603, nullptr, "IsAllFeaturesDisabled"},
            {1901, nullptr, "DeleteFromFreeCommunicationApplicationListForDebug"},
            {1902, nullptr, "ClearFreeCommunicationApplicationListForDebug"},
            {1903, nullptr, "GetExemptApplicationListCountForDebug"},
            {1904, nullptr, "GetExemptApplicationListForDebug"},
            {1905, nullptr, "UpdateExemptApplicationListForDebug"},
            {1906, nullptr, "AddToExemptApplicationListForDebug"},
            {1907, nullptr, "DeleteFromExemptApplicationListForDebug"},
            {1908, nullptr, "ClearExemptApplicationListForDebug"},
            {1941, nullptr, "DeletePairing"},
            {1951, nullptr, "SetPlayTimerSettingsForDebug"},
            {1952, nullptr, "GetPlayTimerSpentTimeForTest"},
            {1953, nullptr, "SetPlayTimerAlarmDisabledForDebug"},
            {2001, nullptr, "RequestPairingAsync"},
            {2002, nullptr, "FinishRequestPairing"},
            {2003, nullptr, "AuthorizePairingAsync"},
            {2004, nullptr, "FinishAuthorizePairing"},
            {2005, nullptr, "RetrievePairingInfoAsync"},
            {2006, nullptr, "FinishRetrievePairingInfo"},
            {2007, nullptr, "UnlinkPairingAsync"},
            {2008, nullptr, "FinishUnlinkPairing"},
            {2009, nullptr, "GetAccountMiiImageAsync"},
            {2010, nullptr, "FinishGetAccountMiiImage"},
            {2011, nullptr, "GetAccountMiiImageContentTypeAsync"},
            {2012, nullptr, "FinishGetAccountMiiImageContentType"},
            {2013, nullptr, "SynchronizeParentalControlSettingsAsync"},
            {2014, nullptr, "FinishSynchronizeParentalControlSettings"},
            {2015, nullptr, "FinishSynchronizeParentalControlSettingsWithLastUpdated"},
            {2016, nullptr, "RequestUpdateExemptionListAsync"},
        };
        // clang-format on
        RegisterHandlers(functions);
    }

private:
    bool CheckFreeCommunicationPermissionImpl() const {
        if (states.temporary_unlocked) {
            return true;
        }
        if ((states.application_info.parental_control_flag & 1) == 0) {
            return true;
        }
        if (pin_code[0] == '\0') {
            return true;
        }
        if (!settings.is_free_communication_default_on) {
            return true;
        }
        // TODO(ogniK): Check for blacklisted/exempted applications. Return false can happen here
        // but as we don't have multiproceses support yet, we can just assume our application is
        // valid for the time being
        return true;
    }

    bool ConfirmStereoVisionPermissionImpl() const {
        if (states.temporary_unlocked) {
            return true;
        }
        if (pin_code[0] == '\0') {
            return true;
        }
        if (!settings.is_stero_vision_restricted) {
            return false;
        }
        return true;
    }

    void SetStereoVisionRestrictionImpl(bool is_restricted) {
        if (settings.disabled) {
            return;
        }

        if (pin_code[0] == '\0') {
            return;
        }
        settings.is_stero_vision_restricted = is_restricted;
    }

    void Initialize(HLERequestContext& ctx) {
        LOG_DEBUG(Service_PCTL, "called");
        IPC::ResponseBuilder rb{ctx, 2};

        if (False(capability & (Capability::Application | Capability::System))) {
            LOG_ERROR(Service_PCTL, "Invalid capability! capability={:X}", capability);
            return;
        }

        // TODO(ogniK): Recovery flag initialization for pctl:r

        const auto tid = system.GetApplicationProcessProgramID();
        if (tid != 0) {
            const FileSys::PatchManager pm{tid, system.GetFileSystemController(),
                                           system.GetContentProvider()};
            const auto control = pm.GetControlMetadata();
            if (control.first) {
                states.tid_from_event = 0;
                states.launch_time_valid = false;
                states.is_suspended = false;
                states.free_communication = false;
                states.stereo_vision = false;
                states.application_info = ApplicationInfo{
                    .tid = tid,
                    .age_rating = control.first->GetRatingAge(),
                    .parental_control_flag = control.first->GetParentalControlFlag(),
                    .capability = capability,
                };

                if (False(capability & (Capability::System | Capability::Recovery))) {
                    // TODO(ogniK): Signal application launch event
                }
            }
        }

        rb.Push(ResultSuccess);
    }

    void CheckFreeCommunicationPermission(HLERequestContext& ctx) {
        LOG_DEBUG(Service_PCTL, "called");

        IPC::ResponseBuilder rb{ctx, 2};
        if (!CheckFreeCommunicationPermissionImpl()) {
            rb.Push(Error::ResultNoFreeCommunication);
        } else {
            rb.Push(ResultSuccess);
        }

        states.free_communication = true;
    }

    void ConfirmStereoVisionPermission(HLERequestContext& ctx) {
        LOG_DEBUG(Service_PCTL, "called");
        states.stereo_vision = true;

        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(ResultSuccess);
    }

    void EndFreeCommunication(HLERequestContext& ctx) {
        LOG_WARNING(Service_PCTL, "(STUBBED) called");

        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(ResultSuccess);
    }

    void IsFreeCommunicationAvailable(HLERequestContext& ctx) {
        LOG_WARNING(Service_PCTL, "(STUBBED) called");

        IPC::ResponseBuilder rb{ctx, 2};
        if (!CheckFreeCommunicationPermissionImpl()) {
            rb.Push(Error::ResultNoFreeCommunication);
        } else {
            rb.Push(ResultSuccess);
        }
    }

    void IsRestrictionEnabled(HLERequestContext& ctx) {
        LOG_DEBUG(Service_PCTL, "called");

        IPC::ResponseBuilder rb{ctx, 3};
        if (False(capability & (Capability::Status | Capability::Recovery))) {
            LOG_ERROR(Service_PCTL, "Application does not have Status or Recovery capabilities!");
            rb.Push(Error::ResultNoCapability);
            rb.Push(false);
            return;
        }

        rb.Push(pin_code[0] != '\0');
    }

    void ConfirmStereoVisionRestrictionConfigurable(HLERequestContext& ctx) {
        LOG_DEBUG(Service_PCTL, "called");

        IPC::ResponseBuilder rb{ctx, 2};

        if (False(capability & Capability::StereoVision)) {
            LOG_ERROR(Service_PCTL, "Application does not have StereoVision capability!");
            rb.Push(Error::ResultNoCapability);
            return;
        }

        if (pin_code[0] == '\0') {
            rb.Push(Error::ResultNoRestrictionEnabled);
            return;
        }

        rb.Push(ResultSuccess);
    }

    void IsStereoVisionPermitted(HLERequestContext& ctx) {
        LOG_DEBUG(Service_PCTL, "called");

        IPC::ResponseBuilder rb{ctx, 3};
        if (!ConfirmStereoVisionPermissionImpl()) {
            rb.Push(Error::ResultStereoVisionRestricted);
            rb.Push(false);
        } else {
            rb.Push(ResultSuccess);
            rb.Push(true);
        }
    }

    void SetStereoVisionRestriction(HLERequestContext& ctx) {
        IPC::RequestParser rp{ctx};
        const auto can_use = rp.Pop<bool>();
        LOG_DEBUG(Service_PCTL, "called, can_use={}", can_use);

        IPC::ResponseBuilder rb{ctx, 2};
        if (False(capability & Capability::StereoVision)) {
            LOG_ERROR(Service_PCTL, "Application does not have StereoVision capability!");
            rb.Push(Error::ResultNoCapability);
            return;
        }

        SetStereoVisionRestrictionImpl(can_use);
        rb.Push(ResultSuccess);
    }

    void GetStereoVisionRestriction(HLERequestContext& ctx) {
        LOG_DEBUG(Service_PCTL, "called");

        IPC::ResponseBuilder rb{ctx, 3};
        if (False(capability & Capability::StereoVision)) {
            LOG_ERROR(Service_PCTL, "Application does not have StereoVision capability!");
            rb.Push(Error::ResultNoCapability);
            rb.Push(false);
            return;
        }

        rb.Push(ResultSuccess);
        rb.Push(settings.is_stero_vision_restricted);
    }

    void ResetConfirmedStereoVisionPermission(HLERequestContext& ctx) {
        LOG_DEBUG(Service_PCTL, "called");

        states.stereo_vision = false;

        IPC::ResponseBuilder rb{ctx, 2};
        rb.Push(ResultSuccess);
    }

    struct ApplicationInfo {
        u64 tid{};
        std::array<u8, 32> age_rating{};
        u32 parental_control_flag{};
        Capability capability{};
    };

    struct States {
        u64 current_tid{};
        ApplicationInfo application_info{};
        u64 tid_from_event{};
        bool launch_time_valid{};
        bool is_suspended{};
        bool temporary_unlocked{};
        bool free_communication{};
        bool stereo_vision{};
    };

    struct ParentalControlSettings {
        bool is_stero_vision_restricted{};
        bool is_free_communication_default_on{};
        bool disabled{};
    };

    States states{};
    ParentalControlSettings settings{};
    std::array<char, 8> pin_code{};
    Capability capability{};
};

void Module::Interface::CreateService(HLERequestContext& ctx) {
    LOG_DEBUG(Service_PCTL, "called");

    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
    rb.Push(ResultSuccess);
    // TODO(ogniK): Get TID from process

    rb.PushIpcInterface<IParentalControlService>(system, capability);
}

void Module::Interface::CreateServiceWithoutInitialize(HLERequestContext& ctx) {
    LOG_DEBUG(Service_PCTL, "called");

    IPC::ResponseBuilder rb{ctx, 2, 0, 1};
    rb.Push(ResultSuccess);
    rb.PushIpcInterface<IParentalControlService>(system, capability);
}

Module::Interface::Interface(Core::System& system_, std::shared_ptr<Module> module_,
                             const char* name_, Capability capability_)
    : ServiceFramework{system_, name_}, module{std::move(module_)}, capability{capability_} {}

Module::Interface::~Interface() = default;

void LoopProcess(Core::System& system) {
    auto server_manager = std::make_unique<ServerManager>(system);

    auto module = std::make_shared<Module>();
    server_manager->RegisterNamedService(
        "pctl", std::make_shared<PCTL>(system, module, "pctl",
                                       Capability::Application | Capability::SnsPost |
                                           Capability::Status | Capability::StereoVision));
    // TODO(ogniK): Implement remaining capabilities
    server_manager->RegisterNamedService(
        "pctl:a", std::make_shared<PCTL>(system, module, "pctl:a", Capability::None));
    server_manager->RegisterNamedService(
        "pctl:r", std::make_shared<PCTL>(system, module, "pctl:r", Capability::None));
    server_manager->RegisterNamedService(
        "pctl:s", std::make_shared<PCTL>(system, module, "pctl:s", Capability::None));
    ServerManager::RunServer(std::move(server_manager));
}

} // namespace Service::PCTL