summaryrefslogtreecommitdiffstats
path: root/src/input_common/helpers/joycon_protocol/nfc.h
blob: c9e9af03fa5d7b60c73186ddca23ff15519a2fbf (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
// 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);

    bool HasAmiibo();

    bool IsEnabled() const;

private:
    // Number of times the function will be delayed until it outputs valid data
    static constexpr std::size_t AMIIBO_UPDATE_DELAY = 15;

    struct TagFoundData {
        u8 type;
        std::vector<u8> uuid;
    };

    DriverResult WaitUntilNfcIsReady();

    DriverResult WaitUntilNfcIsPolling();

    DriverResult IsTagInRange(TagFoundData& data, std::size_t timeout_limit = 1);

    DriverResult GetAmiiboData(std::vector<u8>& data);

    DriverResult SendStartPollingRequest(MCUCommandResponse& output);

    DriverResult SendStopPollingRequest(MCUCommandResponse& output);

    DriverResult SendNextPackageRequest(MCUCommandResponse& output, u8 packet_id);

    DriverResult SendReadAmiiboRequest(MCUCommandResponse& output, NFCPages ntag_pages);

    NFCReadBlockCommand GetReadBlockCommand(NFCPages pages) const;

    bool is_enabled{};
    std::size_t update_counter{};
};

} // namespace InputCommon::Joycon