OLD | NEW |
| (Empty) |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef DEVICE_NFC_NFC_TAG_TECHNOLOGY_H_ | |
6 #define DEVICE_NFC_NFC_TAG_TECHNOLOGY_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include "base/callback.h" | |
11 #include "base/macros.h" | |
12 #include "device/nfc/nfc_ndef_record.h" | |
13 | |
14 namespace device { | |
15 | |
16 class NfcTag; | |
17 | |
18 // NfcTagTechnology represents an NFC technology that allows a certain type of | |
19 // I/O operation on an NFC tag. NFC tags can support a wide array of protocols. | |
20 // The NfcTagTechnology hierarchy allows both raw and high-level I/O operations | |
21 // on NFC tags. Do not create instances of these objects directly. Instead, | |
22 // obtain a handle directly from an NfcTag object. | |
23 class NfcTagTechnology { | |
24 public: | |
25 // The various I/O technologies that an NFC tag can support. | |
26 enum TechnologyType { | |
27 kTechnologyTypeNfcA = 1 << 0, | |
28 kTechnologyTypeNfcB = 1 << 1, | |
29 kTechnologyTypeNfcF = 1 << 2, | |
30 kTechnologyTypeNfcV = 1 << 3, | |
31 kTechnologyTypeIsoDep = 1 << 4, | |
32 kTechnologyTypeNdef = 1 << 5 | |
33 }; | |
34 typedef uint32_t TechnologyTypeMask; | |
35 | |
36 virtual ~NfcTagTechnology(); | |
37 | |
38 // Returns true, if the underlying tag supports the NFC tag technology that | |
39 // this instance represents. | |
40 virtual bool IsSupportedByTag() const = 0; | |
41 | |
42 // Returns a pointer to the associated NfcTag instance. | |
43 NfcTag* tag() const { return tag_; } | |
44 | |
45 protected: | |
46 // Constructs a technology instance, where |tag| is the NFC tag that this | |
47 // instance will operate on. Clients aren't allowed to instantiate classes | |
48 // directly. They should use the static "Create" methods defined in each | |
49 // subclass to obtain the platform specific implementation. | |
50 explicit NfcTagTechnology(NfcTag* tag); | |
51 | |
52 private: | |
53 NfcTagTechnology(); | |
54 | |
55 // The underlying NfcTag instance that data exchange operations through this | |
56 // instance are performed on. | |
57 NfcTag* tag_; | |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(NfcTagTechnology); | |
60 }; | |
61 | |
62 // NfcNdefTagTechnology allows reading and writing NDEF messages to a tag. This | |
63 // is the most commonly used data exchange format in NFC. NDEF is a data | |
64 // exchange format and is the top most layer of the protocol stack. NDEF itself | |
65 // is not a protocol; data is typically formatted in a way that is defined by | |
66 // the NDEF format and then transmitted via one of the underlying protocols. | |
67 // Hence all tags are capable of NDEF data exchange, however, all tags don't | |
68 // necessarily use NDEF to operate (e.g. a tag may contain a smart chip that | |
69 // does data processing on ISO-DEP based APDUs and ignores NDEF). This is why, | |
70 // even if a tag inherently supports NDEF, operations done via this class may | |
71 // not necessarily succeed. | |
72 class NfcNdefTagTechnology : public NfcTagTechnology { | |
73 public: | |
74 // The ErrorCallback is used by methods to asynchronously report errors. | |
75 typedef base::Closure ErrorCallback; | |
76 | |
77 ~NfcNdefTagTechnology() override; | |
78 | |
79 // Interface for observing changes from NFC tags related to NDEF records. | |
80 class Observer { | |
81 public: | |
82 virtual ~Observer() {} | |
83 | |
84 // This method will be called when an NDEF record |record|, stored on the | |
85 // NFC tag |tag| has been read. This method will be called multiple times | |
86 // as records are read from the tag or when the tag's records change (e.g. | |
87 // when the tag has been rewritten). All received records can be accessed by | |
88 // calling GetNdefMessage(). | |
89 virtual void RecordReceived(NfcTag* tag, const NfcNdefRecord* record) {} | |
90 }; | |
91 | |
92 // Adds and removes observers for events on this NFC tag. If monitoring | |
93 // multiple tags, check the |tag| parameter of observer methods to determine | |
94 // which tag is issuing the event. | |
95 virtual void AddObserver(Observer* observer) = 0; | |
96 virtual void RemoveObserver(Observer* observer) = 0; | |
97 | |
98 // NfcTagTechnology override. | |
99 bool IsSupportedByTag() const override; | |
100 | |
101 // Returns all NDEF records that were received from the tag in the form of an | |
102 // NDEF message. If the returned NDEF message contains no records, this only | |
103 // means that no records have yet been received from the tag. Users should | |
104 // use this method in conjunction with the NfcTag::Observer::RecordsReceived | |
105 // method to be notified when the records are ready. | |
106 virtual const NfcNdefMessage& GetNdefMessage() const = 0; | |
107 | |
108 // Writes the given NDEF message to the underlying tag, overwriting any | |
109 // existing NDEF message on it. On success, |callback| will be invoked. On | |
110 // failure, |error_callback| will be invoked. This method can fail, if the | |
111 // underlying tag does not support NDEF as a technology. | |
112 virtual void WriteNdef(const NfcNdefMessage& message, | |
113 const base::Closure& callback, | |
114 const ErrorCallback& error_callback) = 0; | |
115 | |
116 protected: | |
117 // Constructs a technology instance, where |tag| is the NFC tag that this | |
118 // instance will operate on. | |
119 explicit NfcNdefTagTechnology(NfcTag* tag); | |
120 | |
121 DISALLOW_COPY_AND_ASSIGN(NfcNdefTagTechnology); | |
122 }; | |
123 | |
124 } // namespace device | |
125 | |
126 #endif // DEVICE_NFC_NFC_TAG_TECHNOLOGY_H_ | |
OLD | NEW |