From a3fa64fcc489d2cc1776807a37db3464dcc32686 Mon Sep 17 00:00:00 2001
From: Narr the Reg <juangerman-13@hotmail.com>
Date: Fri, 14 Apr 2023 17:46:20 -0600
Subject: [PATCH] service: nfc: Create interface

---
 src/core/hle/service/nfc/nfc.cpp              | 110 ++++++++++++------
 src/core/hle/service/nfc/nfc_device.cpp       |   1 -
 .../nfc/{nfc_user.cpp => nfc_interface.cpp}   |  66 ++++-------
 .../nfc/{nfc_user.h => nfc_interface.h}       |  18 +--
 src/core/hle/service/nfp/nfp.cpp              |  24 +---
 5 files changed, 104 insertions(+), 115 deletions(-)
 rename src/core/hle/service/nfc/{nfc_user.cpp => nfc_interface.cpp} (77%)
 rename src/core/hle/service/nfc/{nfc_user.h => nfc_interface.h} (91%)

diff --git a/src/core/hle/service/nfc/nfc.cpp b/src/core/hle/service/nfc/nfc.cpp
index 6595e34edb..7a8f59725e 100644
--- a/src/core/hle/service/nfc/nfc.cpp
+++ b/src/core/hle/service/nfc/nfc.cpp
@@ -8,12 +8,83 @@
 #include "core/hle/service/ipc_helpers.h"
 #include "core/hle/service/nfc/mifare_user.h"
 #include "core/hle/service/nfc/nfc.h"
-#include "core/hle/service/nfc/nfc_user.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 Interface {
+public:
+    explicit IUser(Core::System& system_) : Interface(system_, "IUser") {
+        // clang-format off
+        static const FunctionInfo functions[] = {
+            {0, &Interface::Initialize, "InitializeOld"},
+            {1, &Interface::Finalize, "FinalizeOld"},
+            {2, &Interface::GetState, "GetStateOld"},
+            {3, &Interface::IsNfcEnabled, "IsNfcEnabledOld"},
+            {400, &Interface::Initialize, "Initialize"},
+            {401, &Interface::Finalize, "Finalize"},
+            {402, &Interface::GetState, "GetState"},
+            {403, &Interface::IsNfcEnabled, "IsNfcEnabled"},
+            {404, &Interface::ListDevices, "ListDevices"},
+            {405, &Interface::GetDeviceState, "GetDeviceState"},
+            {406, &Interface::GetNpadId, "GetNpadId"},
+            {407, &Interface::AttachAvailabilityChangeEvent, "AttachAvailabilityChangeEvent"},
+            {408, &Interface::StartDetection, "StartDetection"},
+            {409, &Interface::StopDetection, "StopDetection"},
+            {410, &Interface::GetTagInfo, "GetTagInfo"},
+            {411, &Interface::AttachActivateEvent, "AttachActivateEvent"},
+            {412, &Interface::AttachDeactivateEvent, "AttachDeactivateEvent"},
+            {1000, nullptr, "ReadMifare"},
+            {1001, nullptr, "WriteMifare"},
+            {1300, &Interface::SendCommandByPassThrough, "SendCommandByPassThrough"},
+            {1301, nullptr, "KeepPassThroughSession"},
+            {1302, nullptr, "ReleasePassThroughSession"},
+        };
+        // clang-format on
+
+        RegisterHandlers(functions);
+    }
+};
+
+class ISystem final : public Interface {
+public:
+    explicit ISystem(Core::System& system_) : Interface{system_, "ISystem"} {
+        // clang-format off
+        static const FunctionInfo functions[] = {
+            {0, &Interface::Initialize, "InitializeOld"},
+            {1, &Interface::Finalize, "FinalizeOld"},
+            {2, &Interface::GetState, "GetStateOld"},
+            {3, &Interface::IsNfcEnabled, "IsNfcEnabledOld"},
+            {100, nullptr, "SetNfcEnabledOld"},
+            {400, &Interface::Initialize, "Initialize"},
+            {401, &Interface::Finalize, "Finalize"},
+            {402, &Interface::GetState, "GetState"},
+            {403, &Interface::IsNfcEnabled, "IsNfcEnabled"},
+            {404, &Interface::ListDevices, "ListDevices"},
+            {405, &Interface::GetDeviceState, "GetDeviceState"},
+            {406, &Interface::GetNpadId, "GetNpadId"},
+            {407, &Interface::AttachAvailabilityChangeEvent, "AttachAvailabilityChangeEvent"},
+            {408, &Interface::StartDetection, "StartDetection"},
+            {409, &Interface::StopDetection, "StopDetection"},
+            {410, &Interface::GetTagInfo, "GetTagInfo"},
+            {411, &Interface::AttachActivateEvent, "AttachActivateEvent"},
+            {412, &Interface::AttachDeactivateEvent, "AttachDeactivateEvent"},
+            {500, nullptr, "SetNfcEnabled"},
+            {510, nullptr, "OutputTestWave"},
+            {1000, nullptr, "ReadMifare"},
+            {1001, nullptr, "WriteMifare"},
+            {1300, &Interface::SendCommandByPassThrough, "SendCommandByPassThrough"},
+            {1301, nullptr, "KeepPassThroughSession"},
+            {1302, nullptr, "ReleasePassThroughSession"},
+        };
+        // clang-format on
+
+        RegisterHandlers(functions);
+    }
+};
+
 class IAm final : public ServiceFramework<IAm> {
 public:
     explicit IAm(Core::System& system_) : ServiceFramework{system_, "NFC::IAm"} {
@@ -95,43 +166,6 @@ private:
     }
 };
 
-class ISystem final : public ServiceFramework<ISystem> {
-public:
-    explicit ISystem(Core::System& system_) : ServiceFramework{system_, "ISystem"} {
-        // clang-format off
-        static const FunctionInfo functions[] = {
-            {0, nullptr, "Initialize"},
-            {1, nullptr, "Finalize"},
-            {2, nullptr, "GetStateOld"},
-            {3, nullptr, "IsNfcEnabledOld"},
-            {100, nullptr, "SetNfcEnabledOld"},
-            {400, nullptr, "InitializeSystem"},
-            {401, nullptr, "FinalizeSystem"},
-            {402, nullptr, "GetState"},
-            {403, nullptr, "IsNfcEnabled"},
-            {404, nullptr, "ListDevices"},
-            {405, nullptr, "GetDeviceState"},
-            {406, nullptr, "GetNpadId"},
-            {407, nullptr, "AttachAvailabilityChangeEvent"},
-            {408, nullptr, "StartDetection"},
-            {409, nullptr, "StopDetection"},
-            {410, nullptr, "GetTagInfo"},
-            {411, nullptr, "AttachActivateEvent"},
-            {412, nullptr, "AttachDeactivateEvent"},
-            {500, nullptr, "SetNfcEnabled"},
-            {510, nullptr, "OutputTestWave"},
-            {1000, nullptr, "ReadMifare"},
-            {1001, nullptr, "WriteMifare"},
-            {1300, nullptr, "SendCommandByPassThrough"},
-            {1301, nullptr, "KeepPassThroughSession"},
-            {1302, nullptr, "ReleasePassThroughSession"},
-        };
-        // clang-format on
-
-        RegisterHandlers(functions);
-    }
-};
-
 class NFC_SYS final : public ServiceFramework<NFC_SYS> {
 public:
     explicit NFC_SYS(Core::System& system_) : ServiceFramework{system_, "nfc:sys"} {
diff --git a/src/core/hle/service/nfc/nfc_device.cpp b/src/core/hle/service/nfc/nfc_device.cpp
index c7db74d14f..47bc1a05dc 100644
--- a/src/core/hle/service/nfc/nfc_device.cpp
+++ b/src/core/hle/service/nfc/nfc_device.cpp
@@ -11,7 +11,6 @@
 #include "core/hle/service/ipc_helpers.h"
 #include "core/hle/service/nfc/nfc_device.h"
 #include "core/hle/service/nfc/nfc_result.h"
-#include "core/hle/service/nfc/nfc_user.h"
 
 namespace Service::NFC {
 NfcDevice::NfcDevice(Core::HID::NpadIdType npad_id_, Core::System& system_,
diff --git a/src/core/hle/service/nfc/nfc_user.cpp b/src/core/hle/service/nfc/nfc_interface.cpp
similarity index 77%
rename from src/core/hle/service/nfc/nfc_user.cpp
rename to src/core/hle/service/nfc/nfc_interface.cpp
index 7c162a4f31..be96d0cbc6 100644
--- a/src/core/hle/service/nfc/nfc_user.cpp
+++ b/src/core/hle/service/nfc/nfc_interface.cpp
@@ -7,41 +7,15 @@
 #include "core/hle/kernel/k_event.h"
 #include "core/hle/service/ipc_helpers.h"
 #include "core/hle/service/nfc/nfc_device.h"
+#include "core/hle/service/nfc/nfc_interface.h"
 #include "core/hle/service/nfc/nfc_result.h"
-#include "core/hle/service/nfc/nfc_user.h"
 #include "core/hle/service/time/clock_types.h"
 
 namespace Service::NFC {
 
-IUser::IUser(Core::System& system_)
-    : ServiceFramework{system_, "NFC::IUser"}, service_context{system_, service_name} {
-    static const FunctionInfo functions[] = {
-        {0, &IUser::Initialize, "InitializeOld"},
-        {1, &IUser::Finalize, "FinalizeOld"},
-        {2, &IUser::GetState, "GetStateOld"},
-        {3, &IUser::IsNfcEnabled, "IsNfcEnabledOld"},
-        {400, &IUser::Initialize, "Initialize"},
-        {401, &IUser::Finalize, "Finalize"},
-        {402, &IUser::GetState, "GetState"},
-        {403, &IUser::IsNfcEnabled, "IsNfcEnabled"},
-        {404, &IUser::ListDevices, "ListDevices"},
-        {405, &IUser::GetDeviceState, "GetDeviceState"},
-        {406, &IUser::GetNpadId, "GetNpadId"},
-        {407, &IUser::AttachAvailabilityChangeEvent, "AttachAvailabilityChangeEvent"},
-        {408, &IUser::StartDetection, "StartDetection"},
-        {409, &IUser::StopDetection, "StopDetection"},
-        {410, &IUser::GetTagInfo, "GetTagInfo"},
-        {411, &IUser::AttachActivateEvent, "AttachActivateEvent"},
-        {412, &IUser::AttachDeactivateEvent, "AttachDeactivateEvent"},
-        {1000, nullptr, "ReadMifare"},
-        {1001, nullptr, "WriteMifare"},
-        {1300, &IUser::SendCommandByPassThrough, "SendCommandByPassThrough"},
-        {1301, nullptr, "KeepPassThroughSession"},
-        {1302, nullptr, "ReleasePassThroughSession"},
-    };
-    RegisterHandlers(functions);
-
-    availability_change_event = service_context.CreateEvent("IUser:AvailabilityChangeEvent");
+Interface::Interface(Core::System& system_, const char* name)
+    : ServiceFramework{system_, name}, service_context{system_, service_name} {
+    availability_change_event = service_context.CreateEvent("Interface:AvailabilityChangeEvent");
 
     for (u32 device_index = 0; device_index < 10; device_index++) {
         devices[device_index] =
@@ -50,11 +24,11 @@ IUser::IUser(Core::System& system_)
     }
 }
 
-IUser ::~IUser() {
+Interface ::~Interface() {
     availability_change_event->Close();
 }
 
-void IUser::Initialize(HLERequestContext& ctx) {
+void Interface::Initialize(HLERequestContext& ctx) {
     LOG_INFO(Service_NFC, "called");
 
     state = State::Initialized;
@@ -67,7 +41,7 @@ void IUser::Initialize(HLERequestContext& ctx) {
     rb.Push(ResultSuccess);
 }
 
-void IUser::Finalize(HLERequestContext& ctx) {
+void Interface::Finalize(HLERequestContext& ctx) {
     LOG_INFO(Service_NFC, "called");
 
     state = State::NonInitialized;
@@ -80,7 +54,7 @@ void IUser::Finalize(HLERequestContext& ctx) {
     rb.Push(ResultSuccess);
 }
 
-void IUser::GetState(HLERequestContext& ctx) {
+void Interface::GetState(HLERequestContext& ctx) {
     LOG_DEBUG(Service_NFC, "called");
 
     IPC::ResponseBuilder rb{ctx, 3};
@@ -88,7 +62,7 @@ void IUser::GetState(HLERequestContext& ctx) {
     rb.PushEnum(state);
 }
 
-void IUser::IsNfcEnabled(HLERequestContext& ctx) {
+void Interface::IsNfcEnabled(HLERequestContext& ctx) {
     LOG_DEBUG(Service_NFC, "called");
 
     IPC::ResponseBuilder rb{ctx, 3};
@@ -96,7 +70,7 @@ void IUser::IsNfcEnabled(HLERequestContext& ctx) {
     rb.Push(state != State::NonInitialized);
 }
 
-void IUser::ListDevices(HLERequestContext& ctx) {
+void Interface::ListDevices(HLERequestContext& ctx) {
     LOG_DEBUG(Service_NFC, "called");
 
     if (state == State::NonInitialized) {
@@ -142,7 +116,7 @@ void IUser::ListDevices(HLERequestContext& ctx) {
     rb.Push(static_cast<s32>(nfp_devices.size()));
 }
 
-void IUser::GetDeviceState(HLERequestContext& ctx) {
+void Interface::GetDeviceState(HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
     const auto device_handle{rp.Pop<u64>()};
     LOG_DEBUG(Service_NFC, "called, device_handle={}", device_handle);
@@ -160,7 +134,7 @@ void IUser::GetDeviceState(HLERequestContext& ctx) {
     rb.PushEnum(device.value()->GetCurrentState());
 }
 
-void IUser::GetNpadId(HLERequestContext& ctx) {
+void Interface::GetNpadId(HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
     const auto device_handle{rp.Pop<u64>()};
     LOG_DEBUG(Service_NFC, "called, device_handle={}", device_handle);
@@ -184,7 +158,7 @@ void IUser::GetNpadId(HLERequestContext& ctx) {
     rb.PushEnum(device.value()->GetNpadId());
 }
 
-void IUser::AttachAvailabilityChangeEvent(HLERequestContext& ctx) {
+void Interface::AttachAvailabilityChangeEvent(HLERequestContext& ctx) {
     LOG_INFO(Service_NFC, "called");
 
     if (state == State::NonInitialized) {
@@ -198,7 +172,7 @@ void IUser::AttachAvailabilityChangeEvent(HLERequestContext& ctx) {
     rb.PushCopyObjects(availability_change_event->GetReadableEvent());
 }
 
-void IUser::StartDetection(HLERequestContext& ctx) {
+void Interface::StartDetection(HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
     const auto device_handle{rp.Pop<u64>()};
     const auto nfp_protocol{rp.PopEnum<NFP::TagProtocol>()};
@@ -223,7 +197,7 @@ void IUser::StartDetection(HLERequestContext& ctx) {
     rb.Push(result);
 }
 
-void IUser::StopDetection(HLERequestContext& ctx) {
+void Interface::StopDetection(HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
     const auto device_handle{rp.Pop<u64>()};
     LOG_INFO(Service_NFC, "called, device_handle={}", device_handle);
@@ -247,7 +221,7 @@ void IUser::StopDetection(HLERequestContext& ctx) {
     rb.Push(result);
 }
 
-void IUser::GetTagInfo(HLERequestContext& ctx) {
+void Interface::GetTagInfo(HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
     const auto device_handle{rp.Pop<u64>()};
     LOG_INFO(Service_NFC, "called, device_handle={}", device_handle);
@@ -273,7 +247,7 @@ void IUser::GetTagInfo(HLERequestContext& ctx) {
     rb.Push(result);
 }
 
-void IUser::AttachActivateEvent(HLERequestContext& ctx) {
+void Interface::AttachActivateEvent(HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
     const auto device_handle{rp.Pop<u64>()};
     LOG_DEBUG(Service_NFC, "called, device_handle={}", device_handle);
@@ -297,7 +271,7 @@ void IUser::AttachActivateEvent(HLERequestContext& ctx) {
     rb.PushCopyObjects(device.value()->GetActivateEvent());
 }
 
-void IUser::AttachDeactivateEvent(HLERequestContext& ctx) {
+void Interface::AttachDeactivateEvent(HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
     const auto device_handle{rp.Pop<u64>()};
     LOG_DEBUG(Service_NFC, "called, device_handle={}", device_handle);
@@ -321,7 +295,7 @@ void IUser::AttachDeactivateEvent(HLERequestContext& ctx) {
     rb.PushCopyObjects(device.value()->GetDeactivateEvent());
 }
 
-void IUser::SendCommandByPassThrough(HLERequestContext& ctx) {
+void Interface::SendCommandByPassThrough(HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
     const auto device_handle{rp.Pop<u64>()};
     const auto timeout{rp.PopRaw<Time::Clock::TimeSpanType>()};
@@ -353,7 +327,7 @@ void IUser::SendCommandByPassThrough(HLERequestContext& ctx) {
     rb.Push(static_cast<u32>(out_data.size()));
 }
 
-std::optional<std::shared_ptr<NfcDevice>> IUser::GetNfcDevice(u64 handle) {
+std::optional<std::shared_ptr<NfcDevice>> Interface::GetNfcDevice(u64 handle) {
     for (auto& device : devices) {
         if (device->GetHandle() == handle) {
             return device;
diff --git a/src/core/hle/service/nfc/nfc_user.h b/src/core/hle/service/nfc/nfc_interface.h
similarity index 91%
rename from src/core/hle/service/nfc/nfc_user.h
rename to src/core/hle/service/nfc/nfc_interface.h
index aee046ae85..8c1bf5a599 100644
--- a/src/core/hle/service/nfc/nfc_user.h
+++ b/src/core/hle/service/nfc/nfc_interface.h
@@ -13,16 +13,10 @@
 namespace Service::NFC {
 class NfcDevice;
 
-class IUser final : public ServiceFramework<IUser> {
+class Interface : public ServiceFramework<Interface> {
 public:
-    explicit IUser(Core::System& system_);
-    ~IUser();
-
-private:
-    enum class State : u32 {
-        NonInitialized,
-        Initialized,
-    };
+    explicit Interface(Core::System& system_, const char* name);
+    ~Interface();
 
     void Initialize(HLERequestContext& ctx);
     void Finalize(HLERequestContext& ctx);
@@ -39,6 +33,12 @@ private:
     void AttachDeactivateEvent(HLERequestContext& ctx);
     void SendCommandByPassThrough(HLERequestContext& ctx);
 
+private:
+    enum class State : u32 {
+        NonInitialized,
+        Initialized,
+    };
+
     std::optional<std::shared_ptr<NfcDevice>> GetNfcDevice(u64 handle);
 
     KernelHelpers::ServiceContext service_context;
diff --git a/src/core/hle/service/nfp/nfp.cpp b/src/core/hle/service/nfp/nfp.cpp
index 2714f4bea0..2559fe5982 100644
--- a/src/core/hle/service/nfp/nfp.cpp
+++ b/src/core/hle/service/nfp/nfp.cpp
@@ -152,16 +152,10 @@ private:
     void CreateUserInterface(HLERequestContext& ctx) {
         LOG_DEBUG(Service_NFP, "called");
 
-        if (user_interface == nullptr) {
-            user_interface = std::make_shared<IUser>(system);
-        }
-
         IPC::ResponseBuilder rb{ctx, 2, 0, 1};
         rb.Push(ResultSuccess);
-        rb.PushIpcInterface<IUser>(user_interface);
+        rb.PushIpcInterface<IUser>(system);
     }
-
-    std::shared_ptr<IUser> user_interface;
 };
 
 class ISystemManager final : public ServiceFramework<ISystemManager> {
@@ -180,16 +174,10 @@ private:
     void CreateSystemInterface(HLERequestContext& ctx) {
         LOG_DEBUG(Service_NFP, "called");
 
-        if (system_interface == nullptr) {
-            system_interface = std::make_shared<ISystem>(system);
-        }
-
         IPC::ResponseBuilder rb{ctx, 2, 0, 1};
         rb.Push(ResultSuccess);
-        rb.PushIpcInterface<ISystem>(system_interface);
+        rb.PushIpcInterface<ISystem>(system);
     }
-
-    std::shared_ptr<ISystem> system_interface;
 };
 
 class IDebugManager final : public ServiceFramework<IDebugManager> {
@@ -208,16 +196,10 @@ private:
     void CreateDebugInterface(HLERequestContext& ctx) {
         LOG_DEBUG(Service_NFP, "called");
 
-        if (system_interface == nullptr) {
-            system_interface = std::make_shared<IDebug>(system);
-        }
-
         IPC::ResponseBuilder rb{ctx, 2, 0, 1};
         rb.Push(ResultSuccess);
-        rb.PushIpcInterface<IDebug>(system_interface);
+        rb.PushIpcInterface<IDebug>(system);
     }
-
-    std::shared_ptr<IDebug> system_interface;
 };
 
 void LoopProcess(Core::System& system) {