yuzu/src/input_common/helpers/joycon_protocol/nfc.h

83 lines
2.6 KiB
C
Raw Normal View History

2022-12-24 07:43:08 +04:00
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
// Based on dkms-hid-nintendo implementation, CTCaer joycon toolkit and dekuNukem reverse
// engineering https://github.com/nicman23/dkms-hid-nintendo/blob/master/src/hid-nintendo.c
// https://github.com/CTCaer/jc_toolkit
// https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering
#pragma once
#include <vector>
#include "input_common/helpers/joycon_protocol/common_protocol.h"
#include "input_common/helpers/joycon_protocol/joycon_types.h"
namespace InputCommon::Joycon {
class NfcProtocol final : private JoyconCommonProtocol {
public:
explicit NfcProtocol(std::shared_ptr<JoyconHandle> handle);
DriverResult EnableNfc();
DriverResult DisableNfc();
DriverResult StartNFCPollingMode();
DriverResult ScanAmiibo(std::vector<u8>& data);
2023-05-22 23:39:23 +04:00
DriverResult WriteAmiibo(std::span<const u8> data);
2022-12-24 07:43:08 +04:00
bool HasAmiibo();
bool IsEnabled() const;
private:
2023-05-15 06:33:04 +04:00
// Number of times the function will be delayed until it outputs valid data
2023-05-14 14:14:58 +04:00
static constexpr std::size_t AMIIBO_UPDATE_DELAY = 15;
2022-12-24 07:43:08 +04:00
struct TagFoundData {
u8 type;
2023-05-22 23:39:23 +04:00
u8 uuid_size;
TagUUID uuid;
2022-12-24 07:43:08 +04:00
};
2023-05-22 23:39:23 +04:00
DriverResult WaitUntilNfcIs(NFCStatus status);
2022-12-24 07:43:08 +04:00
2023-05-17 13:59:25 +04:00
DriverResult IsTagInRange(TagFoundData& data, std::size_t timeout_limit = 1);
2022-12-24 07:43:08 +04:00
DriverResult GetAmiiboData(std::vector<u8>& data);
2023-05-22 23:39:23 +04:00
DriverResult WriteAmiiboData(const TagUUID& tag_uuid, std::span<const u8> data);
DriverResult SendStartPollingRequest(MCUCommandResponse& output,
bool is_second_attempt = false);
2022-12-24 07:43:08 +04:00
2023-01-30 06:12:20 +04:00
DriverResult SendStopPollingRequest(MCUCommandResponse& output);
2022-12-24 07:43:08 +04:00
2023-05-17 13:59:25 +04:00
DriverResult SendNextPackageRequest(MCUCommandResponse& output, u8 packet_id);
2022-12-24 07:43:08 +04:00
2023-01-30 06:12:20 +04:00
DriverResult SendReadAmiiboRequest(MCUCommandResponse& output, NFCPages ntag_pages);
2022-12-24 07:43:08 +04:00
2023-05-22 23:39:23 +04:00
DriverResult SendWriteAmiiboRequest(MCUCommandResponse& output, const TagUUID& tag_uuid);
DriverResult SendWriteDataAmiiboRequest(MCUCommandResponse& output, u8 block_id,
bool is_last_packet, std::span<const u8> data);
std::vector<u8> SerializeWritePackage(const NFCWritePackage& package) const;
NFCWritePackage MakeAmiiboWritePackage(const TagUUID& tag_uuid, std::span<const u8> data) const;
NFCDataChunk MakeAmiiboChunk(u8 page, u8 size, std::span<const u8> data) const;
2023-01-20 23:19:57 +04:00
NFCReadBlockCommand GetReadBlockCommand(NFCPages pages) const;
2022-12-24 07:43:08 +04:00
2023-05-22 23:39:23 +04:00
TagUUID GetTagUUID(std::span<const u8> data) const;
2022-12-24 07:43:08 +04:00
bool is_enabled{};
2023-05-14 14:14:58 +04:00
std::size_t update_counter{};
2022-12-24 07:43:08 +04:00
};
} // namespace InputCommon::Joycon