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