summaryrefslogtreecommitdiffstats
path: root/src/nfc/pcsc/qpcsccard_p.h
blob: 0fe580e22124e5a6f28084b2f7145514129edf04 (plain)
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#ifndef QPCSCCARD_P_H
#define QPCSCCARD_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include "qpcsc_p.h"
#include "qndefmessage.h"
#include "qnearfieldtarget.h"
#include "ndef/qndefaccessfsm_p.h"

QT_BEGIN_NAMESPACE

class QTimer;

class QPcscCard : public QObject
{
    Q_OBJECT
public:
    QPcscCard(SCARDHANDLE handle, DWORD protocol, QObject *parent = nullptr);
    ~QPcscCard();

    bool isValid() const { return m_isValid; }
    void invalidate();

    bool checkCardPresent();
    Q_INVOKABLE void enableAutodelete();

    QByteArray readUid();
    int readMaxInputLength();

    bool supportsNdef() const { return m_supportsNdef; }

private:
    SCARDHANDLE m_handle;
    SCARD_IO_REQUEST m_ioPci;
    bool m_isValid = true;
    bool m_supportsNdef;
    bool m_autodelete = false;
    // Indicates that an _automatic_ transaction was started
    bool m_inAutoTransaction = false;
    QTimer *m_keepAliveTimer;

    std::unique_ptr<QNdefAccessFsm> m_tagDetectionFsm;

    enum AutoTransaction { NoAutoTransaction, StartAutoTransaction };

    QPcsc::RawCommandResult sendCommand(const QByteArray &command, AutoTransaction autoTransaction);
    void performNdefDetection();

    class Transaction
    {
    public:
        Transaction(QPcscCard *card);
        ~Transaction();

    private:
        QPcscCard *m_card;
        bool m_initiated = false;
    };

public Q_SLOTS:
    void onDisconnectRequest();
    void onTargetDestroyed();
    void onSendCommandRequest(const QNearFieldTarget::RequestId &request,
                              const QByteArray &command);
    void onReadNdefMessagesRequest(const QNearFieldTarget::RequestId &request);
    void onWriteNdefMessagesRequest(const QNearFieldTarget::RequestId &request,
                                    const QList<QNdefMessage> &messages);

private Q_SLOTS:
    void onKeepAliveTimeout();

Q_SIGNALS:
    void disconnected();
    void invalidated();

    void requestCompleted(const QNearFieldTarget::RequestId &request,
                          QNearFieldTarget::Error reason, const QVariant &result);
    void ndefMessageRead(const QNdefMessage &message);
};

QT_END_NAMESPACE

#endif // QPCSCCARD_P_H