aboutsummaryrefslogtreecommitdiff
path: root/src/core/hle/service/nfc/nfc.cpp
blob: d3a986faea939dd7b1ffe63d1df85c7e466e5c7d (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
// SPDX-FileCopyrightText: Copyright 2026 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <memory>

#include "common/logging.h"
#include "common/settings.h"
#include "core/hle/service/ipc_helpers.h"
#include "core/hle/service/nfc/nfc.h"
#include "core/hle/service/nfc/nfc_interface.h"
#include "core/hle/service/server_manager.h"
#include "core/hle/service/service.h"

namespace Service::NFC {

class IUser final : public NfcInterface {
public:
    explicit IUser(Core::System& system_) : NfcInterface(system_, "NFC::IUser", BackendType::Nfc) {}

    std::optional<FunctionInfoBase> FindRequest(u32 key) override {
        return HandlerTableGenerateWithFind(key, functions);
    }
    static constexpr auto functions = CreateStaticMapWithClass<IUser>(
        FunctionInfoTyped<IUser>{0, &NfcInterface::Initialize, "InitializeOld"},
        FunctionInfoTyped<IUser>{1, &NfcInterface::Finalize, "FinalizeOld"},
        FunctionInfoTyped<IUser>{2, &NfcInterface::GetState, "GetStateOld"},
        FunctionInfoTyped<IUser>{3, &NfcInterface::IsNfcEnabled, "IsNfcEnabledOld"},
        FunctionInfoTyped<IUser>{400, &NfcInterface::Initialize, "Initialize"},
        FunctionInfoTyped<IUser>{401, &NfcInterface::Finalize, "Finalize"},
        FunctionInfoTyped<IUser>{402, &NfcInterface::GetState, "GetState"},
        FunctionInfoTyped<IUser>{403, &NfcInterface::IsNfcEnabled, "IsNfcEnabled"},
        FunctionInfoTyped<IUser>{404, &NfcInterface::ListDevices, "ListDevices"},
        FunctionInfoTyped<IUser>{405, &NfcInterface::GetDeviceState, "GetDeviceState"},
        FunctionInfoTyped<IUser>{406, &NfcInterface::GetNpadId, "GetNpadId"},
        FunctionInfoTyped<IUser>{407, &NfcInterface::AttachAvailabilityChangeEvent, "AttachAvailabilityChangeEvent"},
        FunctionInfoTyped<IUser>{408, &NfcInterface::StartDetection, "StartDetection"},
        FunctionInfoTyped<IUser>{409, &NfcInterface::StopDetection, "StopDetection"},
        FunctionInfoTyped<IUser>{410, &NfcInterface::GetTagInfo, "GetTagInfo"},
        FunctionInfoTyped<IUser>{411, &NfcInterface::AttachActivateEvent, "AttachActivateEvent"},
        FunctionInfoTyped<IUser>{412, &NfcInterface::AttachDeactivateEvent, "AttachDeactivateEvent"},
        FunctionInfoTyped<IUser>{1000, &NfcInterface::ReadMifare, "ReadMifare"},
        FunctionInfoTyped<IUser>{1001, &NfcInterface::WriteMifare ,"WriteMifare"},
        FunctionInfoTyped<IUser>{1300, &NfcInterface::SendCommandByPassThrough, "SendCommandByPassThrough"},
        FunctionInfoTyped<IUser>{1301, nullptr, "KeepPassThroughSession"},
        FunctionInfoTyped<IUser>{1302, nullptr, "ReleasePassThroughSession"}
    );
};

class ISystem final : public NfcInterface {
public:
    explicit ISystem(Core::System& system_) : NfcInterface{system_, "NFC::ISystem", BackendType::Nfc} {}

    std::optional<FunctionInfoBase> FindRequest(u32 key) override {
        return HandlerTableGenerateWithFind(key, functions);
    }
    static constexpr auto functions = CreateStaticMapWithClass<ISystem>(
        FunctionInfoTyped<ISystem>{0, &NfcInterface::Initialize, "InitializeOld"},
        FunctionInfoTyped<ISystem>{1, &NfcInterface::Finalize, "FinalizeOld"},
        FunctionInfoTyped<ISystem>{2, &NfcInterface::GetState, "GetStateOld"},
        FunctionInfoTyped<ISystem>{3, &NfcInterface::IsNfcEnabled, "IsNfcEnabledOld"},
        FunctionInfoTyped<ISystem>{100, &NfcInterface::SetNfcEnabled, "SetNfcEnabledOld"},
        FunctionInfoTyped<ISystem>{400, &NfcInterface::Initialize, "Initialize"},
        FunctionInfoTyped<ISystem>{401, &NfcInterface::Finalize, "Finalize"},
        FunctionInfoTyped<ISystem>{402, &NfcInterface::GetState, "GetState"},
        FunctionInfoTyped<ISystem>{403, &NfcInterface::IsNfcEnabled, "IsNfcEnabled"},
        FunctionInfoTyped<ISystem>{404, &NfcInterface::ListDevices, "ListDevices"},
        FunctionInfoTyped<ISystem>{405, &NfcInterface::GetDeviceState, "GetDeviceState"},
        FunctionInfoTyped<ISystem>{406, &NfcInterface::GetNpadId, "GetNpadId"},
        FunctionInfoTyped<ISystem>{407, &NfcInterface::AttachAvailabilityChangeEvent, "AttachAvailabilityChangeEvent"},
        FunctionInfoTyped<ISystem>{408, &NfcInterface::StartDetection, "StartDetection"},
        FunctionInfoTyped<ISystem>{409, &NfcInterface::StopDetection, "StopDetection"},
        FunctionInfoTyped<ISystem>{410, &NfcInterface::GetTagInfo, "GetTagInfo"},
        FunctionInfoTyped<ISystem>{411, &NfcInterface::AttachActivateEvent, "AttachActivateEvent"},
        FunctionInfoTyped<ISystem>{412, &NfcInterface::AttachDeactivateEvent, "AttachDeactivateEvent"},
        FunctionInfoTyped<ISystem>{500, &NfcInterface::SetNfcEnabled, "SetNfcEnabled"},
        FunctionInfoTyped<ISystem>{510, nullptr, "OutputTestWave"},
        FunctionInfoTyped<ISystem>{1000, &NfcInterface::ReadMifare, "ReadMifare"},
        FunctionInfoTyped<ISystem>{1001, &NfcInterface::WriteMifare, "WriteMifare"},
        FunctionInfoTyped<ISystem>{1300, &NfcInterface::SendCommandByPassThrough, "SendCommandByPassThrough"},
        FunctionInfoTyped<ISystem>{1301, nullptr, "KeepPassThroughSession"},
        FunctionInfoTyped<ISystem>{1302, nullptr, "ReleasePassThroughSession"}
    );
};

// MFInterface has an unique interface but it's identical to NfcInterface so we can keep the code
// simpler
using MFInterface = NfcInterface;
class MFIUser final : public MFInterface {
public:
    explicit MFIUser(Core::System& system_) : MFInterface{system_, "NFC::MFInterface", BackendType::Mifare} {}

    std::optional<FunctionInfoBase> FindRequest(u32 key) override {
        return HandlerTableGenerateWithFind(key, functions);
    }
    static constexpr auto functions = CreateStaticMapWithClass<MFIUser>(
        FunctionInfoTyped<MFIUser>{0, &MFIUser::Initialize, "Initialize"},
        FunctionInfoTyped<MFIUser>{1, &MFIUser::Finalize, "Finalize"},
        FunctionInfoTyped<MFIUser>{2, &MFIUser::ListDevices, "ListDevices"},
        FunctionInfoTyped<MFIUser>{3, &MFIUser::StartDetection, "StartDetection"},
        FunctionInfoTyped<MFIUser>{4, &MFIUser::StopDetection, "StopDetection"},
        FunctionInfoTyped<MFIUser>{5, &MFIUser::ReadMifare, "Read"},
        FunctionInfoTyped<MFIUser>{6, &MFIUser::WriteMifare, "Write"},
        FunctionInfoTyped<MFIUser>{7, &MFIUser::GetTagInfo, "GetTagInfo"},
        FunctionInfoTyped<MFIUser>{8, &MFIUser::AttachActivateEvent, "GetActivateEventHandle"},
        FunctionInfoTyped<MFIUser>{9, &MFIUser::AttachDeactivateEvent, "GetDeactivateEventHandle"},
        FunctionInfoTyped<MFIUser>{10, &MFIUser::GetState, "GetState"},
        FunctionInfoTyped<MFIUser>{11, &MFIUser::GetDeviceState, "GetDeviceState"},
        FunctionInfoTyped<MFIUser>{12, &MFIUser::GetNpadId, "GetNpadId"},
        FunctionInfoTyped<MFIUser>{13, &MFIUser::AttachAvailabilityChangeEvent, "GetAvailabilityChangeEventHandle"}
    );
};

class IAm final : public ServiceFramework<IAm> {
public:
    explicit IAm(Core::System& system_) : ServiceFramework{system_, "NFC::IAm"} {}

    static constexpr auto functions = CreateStaticMap(
        FunctionInfo{0, nullptr, "Initialize"},
        FunctionInfo{1, nullptr, "Finalize"},
        FunctionInfo{2, nullptr, "NotifyForegroundApplet"}
    );
    std::optional<FunctionInfoBase> FindRequest(u32 key) override {
        return HandlerTableGenerateWithFind(key, functions);
    }
};

class NFC_AM final : public ServiceFramework<NFC_AM> {
public:
    explicit NFC_AM(Core::System& system_) : ServiceFramework{system_, "nfc:am"} {}

    std::optional<FunctionInfoBase> FindRequest(u32 key) override {
        return HandlerTableGenerateWithFind(key, functions);
    }

private:
    void CreateAmNfcInterface(HLERequestContext& ctx) {
        LOG_DEBUG(Service_NFC, "called");

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

    static constexpr auto functions = CreateStaticMap(
        FunctionInfo{0, &NFC_AM::CreateAmNfcInterface, "CreateAmNfcInterface"}
    );
};

class NFC_MF_U final : public ServiceFramework<NFC_MF_U> {
public:
    explicit NFC_MF_U(Core::System& system_) : ServiceFramework{system_, "nfc:mf:u"} {}

    std::optional<FunctionInfoBase> FindRequest(u32 key) override {
        return HandlerTableGenerateWithFind(key, functions);
    }

private:
    void CreateUserNfcInterface(HLERequestContext& ctx) {
        LOG_DEBUG(Service_NFC, "called");

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

    static constexpr auto functions = CreateStaticMap(
        FunctionInfo{0, &NFC_MF_U::CreateUserNfcInterface, "CreateUserNfcInterface"}
    );
};

class NFC_U final : public ServiceFramework<NFC_U> {
public:
    explicit NFC_U(Core::System& system_) : ServiceFramework{system_, "nfc:user"} {}

    std::optional<FunctionInfoBase> FindRequest(u32 key) override {
        return HandlerTableGenerateWithFind(key, functions);
    }

private:
    void CreateUserNfcInterface(HLERequestContext& ctx) {
        LOG_DEBUG(Service_NFC, "called");

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

    static constexpr auto functions = CreateStaticMap(
        FunctionInfo{0, &NFC_U::CreateUserNfcInterface, "CreateUserNfcInterface"}
    );
};

class NFC_SYS final : public ServiceFramework<NFC_SYS> {
public:
    explicit NFC_SYS(Core::System& system_) : ServiceFramework{system_, "nfc:sys"} {}

    std::optional<FunctionInfoBase> FindRequest(u32 key) override {
        return HandlerTableGenerateWithFind(key, functions);
    }

private:
    void CreateSystemNfcInterface(HLERequestContext& ctx) {
        LOG_DEBUG(Service_NFC, "called");

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

    static constexpr auto functions = CreateStaticMap(
        FunctionInfo{0, &NFC_SYS::CreateSystemNfcInterface, "CreateSystemNfcInterface"}
    );
};

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

    server_manager->RegisterNamedService("nfc:am", std::make_shared<NFC_AM>(system));
    server_manager->RegisterNamedService("nfc:mf:u", std::make_shared<NFC_MF_U>(system));
    server_manager->RegisterNamedService("nfc:user", std::make_shared<NFC_U>(system));
    server_manager->RegisterNamedService("nfc:sys", std::make_shared<NFC_SYS>(system));

    ServerManager::RunServer(std::move(server_manager));
}

} // namespace Service::NFC