yuzu/src/core/hle/service/nfp/nfp.cpp

1099 lines
36 KiB
C++
Raw Normal View History

2022-04-23 22:49:07 +04:00
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2020-12-28 19:15:37 +04:00
#include <array>
#include <atomic>
2022-09-09 03:05:23 +04:00
#include "common/fs/file.h"
#include "common/fs/path_util.h"
2020-12-28 19:15:37 +04:00
#include "common/logging/log.h"
2022-09-09 03:05:23 +04:00
#include "common/string_util.h"
2020-12-28 19:15:37 +04:00
#include "core/core.h"
2022-02-10 22:04:38 +04:00
#include "core/hid/emulated_controller.h"
#include "core/hid/hid_core.h"
#include "core/hid/hid_types.h"
2020-12-28 19:15:37 +04:00
#include "core/hle/ipc_helpers.h"
2021-09-29 21:55:52 +04:00
#include "core/hle/kernel/k_event.h"
2022-03-17 21:04:21 +04:00
#include "core/hle/service/mii/mii_manager.h"
2022-09-09 03:05:23 +04:00
#include "core/hle/service/nfp/amiibo_crypto.h"
2020-12-28 19:15:37 +04:00
#include "core/hle/service/nfp/nfp.h"
#include "core/hle/service/nfp/nfp_user.h"
namespace Service::NFP {
namespace ErrCodes {
2022-06-30 05:24:05 +04:00
constexpr Result DeviceNotFound(ErrorModule::NFP, 64);
constexpr Result WrongDeviceState(ErrorModule::NFP, 73);
2022-09-09 03:05:23 +04:00
constexpr Result NfcDisabled(ErrorModule::NFP, 80);
constexpr Result WriteAmiiboFailed(ErrorModule::NFP, 88);
constexpr Result TagRemoved(ErrorModule::NFP, 97);
2022-06-30 05:24:05 +04:00
constexpr Result ApplicationAreaIsNotInitialized(ErrorModule::NFP, 128);
2022-09-09 03:05:23 +04:00
constexpr Result WrongApplicationAreaId(ErrorModule::NFP, 152);
2022-06-30 05:24:05 +04:00
constexpr Result ApplicationAreaExist(ErrorModule::NFP, 168);
2020-12-28 19:15:37 +04:00
} // namespace ErrCodes
2022-02-10 22:04:38 +04:00
IUser::IUser(Module::Interface& nfp_interface_, Core::System& system_)
: ServiceFramework{system_, "NFP::IUser"}, service_context{system_, service_name},
nfp_interface{nfp_interface_} {
static const FunctionInfo functions[] = {
{0, &IUser::Initialize, "Initialize"},
{1, &IUser::Finalize, "Finalize"},
{2, &IUser::ListDevices, "ListDevices"},
{3, &IUser::StartDetection, "StartDetection"},
{4, &IUser::StopDetection, "StopDetection"},
{5, &IUser::Mount, "Mount"},
{6, &IUser::Unmount, "Unmount"},
{7, &IUser::OpenApplicationArea, "OpenApplicationArea"},
{8, &IUser::GetApplicationArea, "GetApplicationArea"},
{9, &IUser::SetApplicationArea, "SetApplicationArea"},
2022-09-09 03:05:23 +04:00
{10, &IUser::Flush, "Flush"},
2022-02-10 22:04:38 +04:00
{11, nullptr, "Restore"},
{12, &IUser::CreateApplicationArea, "CreateApplicationArea"},
{13, &IUser::GetTagInfo, "GetTagInfo"},
{14, &IUser::GetRegisterInfo, "GetRegisterInfo"},
{15, &IUser::GetCommonInfo, "GetCommonInfo"},
{16, &IUser::GetModelInfo, "GetModelInfo"},
{17, &IUser::AttachActivateEvent, "AttachActivateEvent"},
{18, &IUser::AttachDeactivateEvent, "AttachDeactivateEvent"},
{19, &IUser::GetState, "GetState"},
{20, &IUser::GetDeviceState, "GetDeviceState"},
{21, &IUser::GetNpadId, "GetNpadId"},
{22, &IUser::GetApplicationAreaSize, "GetApplicationAreaSize"},
{23, &IUser::AttachAvailabilityChangeEvent, "AttachAvailabilityChangeEvent"},
2022-09-09 03:05:23 +04:00
{24, &IUser::RecreateApplicationArea, "RecreateApplicationArea"},
2022-02-10 22:04:38 +04:00
};
RegisterHandlers(functions);
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
availability_change_event = service_context.CreateEvent("IUser:AvailabilityChangeEvent");
}
2021-05-06 06:45:54 +04:00
2022-02-10 22:04:38 +04:00
void IUser::Initialize(Kernel::HLERequestContext& ctx) {
LOG_INFO(Service_NFC, "called");
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
state = State::Initialized;
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
nfp_interface.Initialize();
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2, 0};
rb.Push(ResultSuccess);
}
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
void IUser::Finalize(Kernel::HLERequestContext& ctx) {
LOG_INFO(Service_NFP, "called");
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
state = State::NonInitialized;
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
nfp_interface.Finalize();
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ResultSuccess);
}
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
void IUser::ListDevices(Kernel::HLERequestContext& ctx) {
LOG_INFO(Service_NFP, "called");
2020-12-28 19:15:37 +04:00
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
std::vector<u64> devices;
// TODO(german77): Loop through all interfaces
devices.push_back(nfp_interface.GetHandle());
2022-09-09 03:05:23 +04:00
if (devices.size() == 0) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
return;
}
2022-02-10 22:04:38 +04:00
ctx.WriteBuffer(devices);
IPC::ResponseBuilder rb{ctx, 3};
rb.Push(ResultSuccess);
rb.Push(static_cast<s32>(devices.size()));
}
void IUser::StartDetection(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
const auto nfp_protocol{rp.Pop<s32>()};
LOG_INFO(Service_NFP, "called, device_handle={}, nfp_protocol={}", device_handle, nfp_protocol);
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
const auto result = nfp_interface.StartDetection(nfp_protocol);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(result);
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
void IUser::StopDetection(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
const auto result = nfp_interface.StopDetection();
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(result);
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::Mount(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
const auto model_type{rp.PopEnum<ModelType>()};
const auto mount_target{rp.PopEnum<MountTarget>()};
LOG_INFO(Service_NFP, "called, device_handle={}, model_type={}, mount_target={}", device_handle,
model_type, mount_target);
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
const auto result = nfp_interface.Mount();
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(result);
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::Unmount(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
const auto result = nfp_interface.Unmount();
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(result);
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::OpenApplicationArea(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
const auto access_id{rp.Pop<u32>()};
LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}, access_id={}", device_handle,
access_id);
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
const auto result = nfp_interface.OpenApplicationArea(access_id);
2020-12-28 19:15:37 +04:00
IPC::ResponseBuilder rb{ctx, 2};
2022-02-10 22:04:38 +04:00
rb.Push(result);
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::GetApplicationArea(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
2022-09-09 03:05:23 +04:00
ApplicationArea data{};
2022-02-10 22:04:38 +04:00
const auto result = nfp_interface.GetApplicationArea(data);
ctx.WriteBuffer(data);
2020-12-28 19:15:37 +04:00
IPC::ResponseBuilder rb{ctx, 3};
2022-02-10 22:04:38 +04:00
rb.Push(result);
rb.Push(static_cast<u32>(data.size()));
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::SetApplicationArea(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
const auto data{ctx.ReadBuffer()};
LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}, data_size={}", device_handle,
data.size());
2020-12-28 19:15:37 +04:00
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
const auto result = nfp_interface.SetApplicationArea(data);
2020-12-28 19:15:37 +04:00
IPC::ResponseBuilder rb{ctx, 2};
2022-02-10 22:04:38 +04:00
rb.Push(result);
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
2020-12-28 19:15:37 +04:00
2022-09-09 03:05:23 +04:00
void IUser::Flush(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}", device_handle);
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
const auto result = nfp_interface.Flush();
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(result);
return;
}
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
2022-02-10 22:04:38 +04:00
void IUser::CreateApplicationArea(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
const auto access_id{rp.Pop<u32>()};
const auto data{ctx.ReadBuffer()};
LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}, data_size={}, access_id={}",
device_handle, access_id, data.size());
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
const auto result = nfp_interface.CreateApplicationArea(access_id, data);
2020-12-28 19:15:37 +04:00
IPC::ResponseBuilder rb{ctx, 2};
2022-02-10 22:04:38 +04:00
rb.Push(result);
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::GetTagInfo(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
TagInfo tag_info{};
const auto result = nfp_interface.GetTagInfo(tag_info);
ctx.WriteBuffer(tag_info);
2020-12-28 19:15:37 +04:00
IPC::ResponseBuilder rb{ctx, 2};
2022-02-10 22:04:38 +04:00
rb.Push(result);
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::GetRegisterInfo(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
RegisterInfo register_info{};
const auto result = nfp_interface.GetRegisterInfo(register_info);
ctx.WriteBuffer(register_info);
2020-12-28 19:15:37 +04:00
IPC::ResponseBuilder rb{ctx, 2};
2022-02-10 22:04:38 +04:00
rb.Push(result);
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::GetCommonInfo(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
CommonInfo common_info{};
const auto result = nfp_interface.GetCommonInfo(common_info);
ctx.WriteBuffer(common_info);
2020-12-28 19:15:37 +04:00
IPC::ResponseBuilder rb{ctx, 2};
2022-02-10 22:04:38 +04:00
rb.Push(result);
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::GetModelInfo(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_INFO(Service_NFP, "called, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
ModelInfo model_info{};
const auto result = nfp_interface.GetModelInfo(model_info);
ctx.WriteBuffer(model_info);
2020-12-28 19:15:37 +04:00
IPC::ResponseBuilder rb{ctx, 2};
2022-02-10 22:04:38 +04:00
rb.Push(result);
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::AttachActivateEvent(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
2020-12-28 19:15:37 +04:00
IPC::ResponseBuilder rb{ctx, 2, 1};
2021-06-03 04:36:17 +04:00
rb.Push(ResultSuccess);
2022-02-10 22:04:38 +04:00
rb.PushCopyObjects(nfp_interface.GetActivateEvent());
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
void IUser::AttachDeactivateEvent(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle);
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
IPC::ResponseBuilder rb{ctx, 2, 1};
2021-06-03 04:36:17 +04:00
rb.Push(ResultSuccess);
2022-02-10 22:04:38 +04:00
rb.PushCopyObjects(nfp_interface.GetDeactivateEvent());
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
void IUser::GetState(Kernel::HLERequestContext& ctx) {
LOG_DEBUG(Service_NFC, "called");
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 3, 0};
rb.Push(ResultSuccess);
rb.PushEnum(state);
}
void IUser::GetDeviceState(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle);
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
IPC::ResponseBuilder rb{ctx, 3};
2021-06-03 04:36:17 +04:00
rb.Push(ResultSuccess);
2022-02-10 22:04:38 +04:00
rb.PushEnum(nfp_interface.GetCurrentState());
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::GetNpadId(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
2020-12-28 19:15:37 +04:00
IPC::ResponseBuilder rb{ctx, 3};
2021-06-03 04:36:17 +04:00
rb.Push(ResultSuccess);
2022-02-10 22:04:38 +04:00
rb.PushEnum(nfp_interface.GetNpadId());
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::GetApplicationAreaSize(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
LOG_DEBUG(Service_NFP, "called, device_handle={}", device_handle);
2020-12-28 19:15:37 +04:00
2022-02-10 22:04:38 +04:00
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
2020-12-28 19:15:37 +04:00
IPC::ResponseBuilder rb{ctx, 3};
2021-06-03 04:36:17 +04:00
rb.Push(ResultSuccess);
2022-09-09 03:05:23 +04:00
rb.Push(sizeof(ApplicationArea));
2022-02-10 22:04:38 +04:00
return;
2020-12-28 19:15:37 +04:00
}
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
2021-09-29 21:55:52 +04:00
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
void IUser::AttachAvailabilityChangeEvent(Kernel::HLERequestContext& ctx) {
LOG_DEBUG(Service_NFP, "(STUBBED) called");
2022-09-09 03:05:23 +04:00
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
2022-02-10 22:04:38 +04:00
IPC::ResponseBuilder rb{ctx, 2, 1};
rb.Push(ResultSuccess);
rb.PushCopyObjects(availability_change_event->GetReadableEvent());
}
2022-09-09 03:05:23 +04:00
void IUser::RecreateApplicationArea(Kernel::HLERequestContext& ctx) {
IPC::RequestParser rp{ctx};
const auto device_handle{rp.Pop<u64>()};
const auto access_id{rp.Pop<u32>()};
const auto data{ctx.ReadBuffer()};
LOG_WARNING(Service_NFP, "(STUBBED) called, device_handle={}, data_size={}, access_id={}",
device_handle, access_id, data.size());
if (state == State::NonInitialized) {
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::NfcDisabled);
return;
}
// TODO(german77): Loop through all interfaces
if (device_handle == nfp_interface.GetHandle()) {
const auto result = nfp_interface.RecreateApplicationArea(access_id, data);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(result);
return;
}
LOG_ERROR(Service_NFP, "Handle not found, device_handle={}", device_handle);
IPC::ResponseBuilder rb{ctx, 2};
rb.Push(ErrCodes::DeviceNotFound);
}
2022-02-10 22:04:38 +04:00
Module::Interface::Interface(std::shared_ptr<Module> module_, Core::System& system_,
const char* name)
: ServiceFramework{system_, name}, module{std::move(module_)},
npad_id{Core::HID::NpadIdType::Player1}, service_context{system_, service_name} {
activate_event = service_context.CreateEvent("IUser:NFPActivateEvent");
deactivate_event = service_context.CreateEvent("IUser:NFPDeactivateEvent");
}
Module::Interface::~Interface() = default;
2020-12-28 19:15:37 +04:00
void Module::Interface::CreateUserInterface(Kernel::HLERequestContext& ctx) {
LOG_DEBUG(Service_NFP, "called");
IPC::ResponseBuilder rb{ctx, 2, 0, 1};
2021-06-03 04:36:17 +04:00
rb.Push(ResultSuccess);
2022-02-10 22:04:38 +04:00
rb.PushIpcInterface<IUser>(*this, system);
2020-12-28 19:15:37 +04:00
}
2022-09-09 03:05:23 +04:00
bool Module::Interface::LoadAmiiboFile(const std::string& filename) {
2022-02-14 05:49:37 +04:00
constexpr auto tag_size_without_password = sizeof(NTAG215File) - sizeof(NTAG215Password);
2022-09-09 03:05:23 +04:00
const Common::FS::IOFile amiibo_file{filename, Common::FS::FileAccessMode::Read,
Common::FS::FileType::BinaryFile};
2022-02-14 05:49:37 +04:00
2022-09-09 03:05:23 +04:00
if (!amiibo_file.IsOpen()) {
LOG_ERROR(Service_NFP, "Amiibo is already on use");
return false;
}
2022-02-14 05:49:37 +04:00
2022-09-09 03:05:23 +04:00
// Workaround for files with missing password data
std::array<u8, sizeof(EncryptedNTAG215File)> buffer{};
if (amiibo_file.Read(buffer) < tag_size_without_password) {
LOG_ERROR(Service_NFP, "Failed to read amiibo file");
2022-02-10 22:04:38 +04:00
return false;
}
2022-09-09 03:05:23 +04:00
memcpy(&encrypted_tag_data, buffer.data(), sizeof(EncryptedNTAG215File));
2022-02-10 22:04:38 +04:00
2022-09-09 03:05:23 +04:00
if (!AmiiboCrypto::IsAmiiboValid(encrypted_tag_data)) {
LOG_INFO(Service_NFP, "Invalid amiibo");
return false;
2022-02-14 05:49:37 +04:00
}
2022-09-09 03:05:23 +04:00
file_path = filename;
return true;
}
2022-02-10 22:04:38 +04:00
2022-09-09 03:05:23 +04:00
bool Module::Interface::LoadAmiibo(const std::string& filename) {
if (device_state != DeviceState::SearchingForTag) {
LOG_ERROR(Service_NFP, "Game is not looking for amiibos, current state {}", device_state);
2020-12-28 19:15:37 +04:00
return false;
}
2022-09-09 03:05:23 +04:00
if (!LoadAmiiboFile(filename)) {
return false;
}
2022-02-10 22:04:38 +04:00
device_state = DeviceState::TagFound;
activate_event->GetWritableEvent().Signal();
return true;
}
void Module::Interface::CloseAmiibo() {
LOG_INFO(Service_NFP, "Remove amiibo");
device_state = DeviceState::TagRemoved;
2022-09-09 03:05:23 +04:00
is_data_decoded = false;
2022-02-10 22:04:38 +04:00
is_application_area_initialized = false;
2022-09-09 03:05:23 +04:00
encrypted_tag_data = {};
tag_data = {};
2022-02-10 22:04:38 +04:00
deactivate_event->GetWritableEvent().Signal();
}
Kernel::KReadableEvent& Module::Interface::GetActivateEvent() const {
return activate_event->GetReadableEvent();
}
Kernel::KReadableEvent& Module::Interface::GetDeactivateEvent() const {
return deactivate_event->GetReadableEvent();
}
void Module::Interface::Initialize() {
device_state = DeviceState::Initialized;
2022-09-09 03:05:23 +04:00
is_data_decoded = false;
is_application_area_initialized = false;
encrypted_tag_data = {};
tag_data = {};
2022-02-10 22:04:38 +04:00
}
void Module::Interface::Finalize() {
2022-09-09 03:05:23 +04:00
if (device_state == DeviceState::TagMounted) {
Unmount();
}
if (device_state == DeviceState::SearchingForTag || device_state == DeviceState::TagRemoved) {
StopDetection();
}
2022-02-10 22:04:38 +04:00
device_state = DeviceState::Unaviable;
}
2022-06-30 05:24:05 +04:00
Result Module::Interface::StartDetection(s32 protocol_) {
2022-02-10 22:04:38 +04:00
auto npad_device = system.HIDCore().GetEmulatedController(npad_id);
// TODO(german77): Add callback for when nfc data is available
if (device_state == DeviceState::Initialized || device_state == DeviceState::TagRemoved) {
npad_device->SetPollingMode(Common::Input::PollingMode::NFC);
device_state = DeviceState::SearchingForTag;
protocol = protocol_;
return ResultSuccess;
}
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
return ErrCodes::WrongDeviceState;
}
2022-06-30 05:24:05 +04:00
Result Module::Interface::StopDetection() {
2022-02-10 22:04:38 +04:00
auto npad_device = system.HIDCore().GetEmulatedController(npad_id);
npad_device->SetPollingMode(Common::Input::PollingMode::Active);
if (device_state == DeviceState::TagFound || device_state == DeviceState::TagMounted) {
CloseAmiibo();
return ResultSuccess;
}
if (device_state == DeviceState::SearchingForTag || device_state == DeviceState::TagRemoved) {
device_state = DeviceState::Initialized;
return ResultSuccess;
}
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
return ErrCodes::WrongDeviceState;
}
2022-09-09 03:05:23 +04:00
Result Module::Interface::Flush() {
// Ignore write command if we can't encrypt the data
if (!is_data_decoded) {
2022-02-10 22:04:38 +04:00
return ResultSuccess;
}
2022-09-09 03:05:23 +04:00
constexpr auto tag_size_without_password = sizeof(NTAG215File) - sizeof(NTAG215Password);
EncryptedNTAG215File tmp_encrypted_tag_data{};
const Common::FS::IOFile amiibo_file{file_path, Common::FS::FileAccessMode::ReadWrite,
Common::FS::FileType::BinaryFile};
if (!amiibo_file.IsOpen()) {
LOG_ERROR(Core, "Amiibo is already on use");
return ErrCodes::WriteAmiiboFailed;
}
// Workaround for files with missing password data
std::array<u8, sizeof(EncryptedNTAG215File)> buffer{};
if (amiibo_file.Read(buffer) < tag_size_without_password) {
LOG_ERROR(Core, "Failed to read amiibo file");
return ErrCodes::WriteAmiiboFailed;
}
memcpy(&tmp_encrypted_tag_data, buffer.data(), sizeof(EncryptedNTAG215File));
if (!AmiiboCrypto::IsAmiiboValid(tmp_encrypted_tag_data)) {
LOG_INFO(Service_NFP, "Invalid amiibo");
return ErrCodes::WriteAmiiboFailed;
}
bool is_uuid_equal = memcmp(tmp_encrypted_tag_data.uuid.data(), tag_data.uuid.data(), 8) == 0;
bool is_character_equal = tmp_encrypted_tag_data.user_memory.model_info.character_id ==
tag_data.model_info.character_id;
if (!is_uuid_equal || !is_character_equal) {
LOG_ERROR(Service_NFP, "Not the same amiibo");
return ErrCodes::WriteAmiiboFailed;
}
if (!AmiiboCrypto::EncodeAmiibo(tag_data, encrypted_tag_data)) {
LOG_ERROR(Service_NFP, "Failed to encode data");
return ErrCodes::WriteAmiiboFailed;
}
// Return to the start of the file
if (!amiibo_file.Seek(0)) {
2022-09-24 06:39:39 +04:00
LOG_ERROR(Service_NFP, "Error writing to file");
2022-09-09 03:05:23 +04:00
return ErrCodes::WriteAmiiboFailed;
}
if (!amiibo_file.Write(encrypted_tag_data)) {
2022-09-24 06:39:39 +04:00
LOG_ERROR(Service_NFP, "Error writing to file");
2022-09-09 03:05:23 +04:00
return ErrCodes::WriteAmiiboFailed;
}
return ResultSuccess;
}
Result Module::Interface::Mount() {
if (device_state != DeviceState::TagFound) {
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
return ErrCodes::WrongDeviceState;
}
is_data_decoded = AmiiboCrypto::DecodeAmiibo(encrypted_tag_data, tag_data);
LOG_INFO(Service_NFP, "Is amiibo decoded {}", is_data_decoded);
is_application_area_initialized = false;
device_state = DeviceState::TagMounted;
return ResultSuccess;
2022-02-10 22:04:38 +04:00
}
2022-06-30 05:24:05 +04:00
Result Module::Interface::Unmount() {
2022-09-09 03:05:23 +04:00
if (device_state != DeviceState::TagMounted) {
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
return ErrCodes::WrongDeviceState;
2022-02-10 22:04:38 +04:00
}
2022-09-09 03:05:23 +04:00
is_data_decoded = false;
is_application_area_initialized = false;
device_state = DeviceState::TagFound;
return ResultSuccess;
2022-02-10 22:04:38 +04:00
}
2022-06-30 05:24:05 +04:00
Result Module::Interface::GetTagInfo(TagInfo& tag_info) const {
2022-09-09 03:05:23 +04:00
if (device_state != DeviceState::TagFound && device_state != DeviceState::TagMounted) {
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
return ErrCodes::WrongDeviceState;
2022-02-10 22:04:38 +04:00
}
2022-09-09 03:05:23 +04:00
tag_info = {
.uuid = encrypted_tag_data.uuid,
.uuid_length = static_cast<u8>(encrypted_tag_data.uuid.size()),
.protocol = protocol,
.tag_type = static_cast<u32>(encrypted_tag_data.user_memory.model_info.amiibo_type),
};
return ResultSuccess;
2022-02-10 22:04:38 +04:00
}
2022-06-30 05:24:05 +04:00
Result Module::Interface::GetCommonInfo(CommonInfo& common_info) const {
2022-02-10 22:04:38 +04:00
if (device_state != DeviceState::TagMounted) {
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
return ErrCodes::WrongDeviceState;
}
2022-09-09 03:05:23 +04:00
if (is_data_decoded && tag_data.settings.settings.amiibo_initialized != 0) {
const auto& settings = tag_data.settings;
// TODO: Validate this data
common_info = {
.last_write_year = settings.write_date.GetYear(),
.last_write_month = settings.write_date.GetMonth(),
.last_write_day = settings.write_date.GetDay(),
.write_counter = settings.crc_counter,
.version = 1,
.application_area_size = sizeof(ApplicationArea),
};
return ResultSuccess;
}
// Generate a generic answer
2022-02-10 22:04:38 +04:00
common_info = {
.last_write_year = 2022,
.last_write_month = 2,
.last_write_day = 7,
2022-09-09 03:05:23 +04:00
.write_counter = 0,
2022-02-10 22:04:38 +04:00
.version = 1,
2022-09-09 03:05:23 +04:00
.application_area_size = sizeof(ApplicationArea),
2022-02-10 22:04:38 +04:00
};
return ResultSuccess;
}
2022-06-30 05:24:05 +04:00
Result Module::Interface::GetModelInfo(ModelInfo& model_info) const {
2022-02-10 22:04:38 +04:00
if (device_state != DeviceState::TagMounted) {
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
return ErrCodes::WrongDeviceState;
}
2022-09-09 03:05:23 +04:00
const auto& model_info_data = encrypted_tag_data.user_memory.model_info;
model_info = {
.character_id = model_info_data.character_id,
.character_variant = model_info_data.character_variant,
.amiibo_type = model_info_data.amiibo_type,
.model_number = model_info_data.model_number,
.series = model_info_data.series,
.constant_value = model_info_data.constant_value,
};
2022-02-10 22:04:38 +04:00
return ResultSuccess;
}
2022-06-30 05:24:05 +04:00
Result Module::Interface::GetRegisterInfo(RegisterInfo& register_info) const {
2022-02-10 22:04:38 +04:00
if (device_state != DeviceState::TagMounted) {
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
2022-09-09 03:05:23 +04:00
if (device_state == DeviceState::TagRemoved) {
return ErrCodes::TagRemoved;
}
2022-02-10 22:04:38 +04:00
return ErrCodes::WrongDeviceState;
}
Service::Mii::MiiManager manager;
2022-09-09 03:05:23 +04:00
if (is_data_decoded && tag_data.settings.settings.amiibo_initialized != 0) {
const auto& settings = tag_data.settings;
// TODO: Validate this data
register_info = {
.mii_char_info = manager.ConvertV3ToCharInfo(tag_data.owner_mii),
.first_write_year = settings.init_date.GetYear(),
.first_write_month = settings.init_date.GetMonth(),
.first_write_day = settings.init_date.GetDay(),
.amiibo_name = GetAmiiboName(settings),
.font_region = {},
};
return ResultSuccess;
}
// Generate a generic answer
2022-02-10 22:04:38 +04:00
register_info = {
.mii_char_info = manager.BuildDefault(0),
.first_write_year = 2022,
.first_write_month = 2,
.first_write_day = 7,
.amiibo_name = {'Y', 'u', 'z', 'u', 'A', 'm', 'i', 'i', 'b', 'o', 0},
2022-09-09 03:05:23 +04:00
.font_region = {},
2022-02-10 22:04:38 +04:00
};
return ResultSuccess;
}
2022-06-30 05:24:05 +04:00
Result Module::Interface::OpenApplicationArea(u32 access_id) {
2022-02-10 22:04:38 +04:00
if (device_state != DeviceState::TagMounted) {
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
2022-09-09 03:05:23 +04:00
if (device_state == DeviceState::TagRemoved) {
return ErrCodes::TagRemoved;
}
2022-02-10 22:04:38 +04:00
return ErrCodes::WrongDeviceState;
}
2022-09-09 03:05:23 +04:00
// Fallback for lack of amiibo keys
if (!is_data_decoded) {
LOG_WARNING(Service_NFP, "Application area is not initialized");
return ErrCodes::ApplicationAreaIsNotInitialized;
2022-02-10 22:04:38 +04:00
}
2022-09-09 03:05:23 +04:00
if (tag_data.settings.settings.appdata_initialized == 0) {
2022-02-10 22:04:38 +04:00
LOG_WARNING(Service_NFP, "Application area is not initialized");
return ErrCodes::ApplicationAreaIsNotInitialized;
}
2022-09-09 03:05:23 +04:00
if (tag_data.application_area_id != access_id) {
LOG_WARNING(Service_NFP, "Wrong application area id");
return ErrCodes::WrongApplicationAreaId;
}
is_application_area_initialized = true;
2022-02-10 22:04:38 +04:00
return ResultSuccess;
}
2022-09-09 03:05:23 +04:00
Result Module::Interface::GetApplicationArea(ApplicationArea& data) const {
2022-02-10 22:04:38 +04:00
if (device_state != DeviceState::TagMounted) {
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
2022-09-09 03:05:23 +04:00
if (device_state == DeviceState::TagRemoved) {
return ErrCodes::TagRemoved;
}
2022-02-10 22:04:38 +04:00
return ErrCodes::WrongDeviceState;
}
2022-09-09 03:05:23 +04:00
2022-02-10 22:04:38 +04:00
if (!is_application_area_initialized) {
LOG_ERROR(Service_NFP, "Application area is not initialized");
return ErrCodes::ApplicationAreaIsNotInitialized;
}
2022-09-09 03:05:23 +04:00
data = tag_data.application_area;
2022-02-10 22:04:38 +04:00
return ResultSuccess;
}
2022-06-30 05:24:05 +04:00
Result Module::Interface::SetApplicationArea(const std::vector<u8>& data) {
2022-02-10 22:04:38 +04:00
if (device_state != DeviceState::TagMounted) {
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
2022-09-09 03:05:23 +04:00
if (device_state == DeviceState::TagRemoved) {
return ErrCodes::TagRemoved;
}
2022-02-10 22:04:38 +04:00
return ErrCodes::WrongDeviceState;
}
2022-09-09 03:05:23 +04:00
2022-02-10 22:04:38 +04:00
if (!is_application_area_initialized) {
LOG_ERROR(Service_NFP, "Application area is not initialized");
return ErrCodes::ApplicationAreaIsNotInitialized;
}
2022-09-09 03:05:23 +04:00
if (data.size() != sizeof(ApplicationArea)) {
LOG_ERROR(Service_NFP, "Wrong data size {}", data.size());
return ResultUnknown;
}
std::memcpy(&tag_data.application_area, data.data(), sizeof(ApplicationArea));
2022-02-10 22:04:38 +04:00
return ResultSuccess;
}
2022-06-30 05:24:05 +04:00
Result Module::Interface::CreateApplicationArea(u32 access_id, const std::vector<u8>& data) {
2022-02-10 22:04:38 +04:00
if (device_state != DeviceState::TagMounted) {
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
2022-09-09 03:05:23 +04:00
if (device_state == DeviceState::TagRemoved) {
return ErrCodes::TagRemoved;
}
2022-02-10 22:04:38 +04:00
return ErrCodes::WrongDeviceState;
}
2022-09-09 03:05:23 +04:00
if (tag_data.settings.settings.appdata_initialized != 0) {
2022-02-10 22:04:38 +04:00
LOG_ERROR(Service_NFP, "Application area already exist");
return ErrCodes::ApplicationAreaExist;
}
2022-09-09 03:05:23 +04:00
if (data.size() != sizeof(ApplicationArea)) {
LOG_ERROR(Service_NFP, "Wrong data size {}", data.size());
return ResultUnknown;
}
std::memcpy(&tag_data.application_area, data.data(), sizeof(ApplicationArea));
tag_data.application_area_id = access_id;
2022-02-10 22:04:38 +04:00
return ResultSuccess;
}
2022-09-09 03:05:23 +04:00
Result Module::Interface::RecreateApplicationArea(u32 access_id, const std::vector<u8>& data) {
if (device_state != DeviceState::TagMounted) {
LOG_ERROR(Service_NFP, "Wrong device state {}", device_state);
if (device_state == DeviceState::TagRemoved) {
return ErrCodes::TagRemoved;
}
return ErrCodes::WrongDeviceState;
}
2022-02-10 22:04:38 +04:00
2022-09-09 03:05:23 +04:00
if (data.size() != sizeof(ApplicationArea)) {
LOG_ERROR(Service_NFP, "Wrong data size {}", data.size());
return ResultUnknown;
}
std::memcpy(&tag_data.application_area, data.data(), sizeof(ApplicationArea));
tag_data.application_area_id = access_id;
2022-02-10 22:04:38 +04:00
2022-09-09 03:05:23 +04:00
return ResultSuccess;
2022-02-10 22:04:38 +04:00
}
u64 Module::Interface::GetHandle() const {
// Generate a handle based of the npad id
return static_cast<u64>(npad_id);
}
DeviceState Module::Interface::GetCurrentState() const {
return device_state;
}
Core::HID::NpadIdType Module::Interface::GetNpadId() const {
2022-09-09 03:05:23 +04:00
// Return first connected npad id as a workaround for lack of a single nfc interface per
// controller
return system.HIDCore().GetFirstNpadId();
2020-12-28 19:15:37 +04:00
}
2022-09-09 03:05:23 +04:00
AmiiboName Module::Interface::GetAmiiboName(const AmiiboSettings& settings) const {
std::array<char16_t, amiibo_name_length> settings_amiibo_name{};
AmiiboName amiibo_name{};
// Convert from big endian to little endian
for (std::size_t i = 0; i < amiibo_name_length; i++) {
settings_amiibo_name[i] = static_cast<u16>(settings.amiibo_name[i]);
}
// Convert from utf16 to utf8
const auto amiibo_name_utf8 = Common::UTF16ToUTF8(settings_amiibo_name.data());
memcpy(amiibo_name.data(), amiibo_name_utf8.data(), amiibo_name_utf8.size());
return amiibo_name;
2020-12-28 19:15:37 +04:00
}
void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system) {
auto module = std::make_shared<Module>();
std::make_shared<NFP_User>(module, system)->InstallAsService(service_manager);
}
} // namespace Service::NFP