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_PEER_H_
      6 #define DEVICE_NFC_NFC_PEER_H_
      7 
      8 #include <map>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/callback.h"
     13 #include "device/nfc/nfc_ndef_record.h"
     14 
     15 namespace device {
     16 
     17 // NfcPeer represents a remote NFC adapter that is available for P2P
     18 // communication with the local adapter. Instances of NfcPeer allow two
     19 // kinds of P2P interaction that is supported by NFC:
     20 //
     21 //    - NDEF. Specifically, reading NDEF records found on the peer device and
     22 //      pushing NDEF records to it (e.g. via SNEP or Android Beam), in the form
     23 //      of an NDEF message as specified by the NFC forum.
     24 //    - Initiating a handover. On platforms that support it, handover can be
     25 //      used to quickly bootstrap a Bluetooth or WiFi based connection between
     26 //      the two devices over NFC.
     27 class NfcPeer {
     28  public:
     29   // NFC handover types.
     30   enum HandoverType {
     31     kHandoverTypeBluetooth,
     32     kHandoverTypeWiFi
     33   };
     34 
     35   // Interface for observing changes from NFC peer devices.
     36   class Observer {
     37    public:
     38     virtual ~Observer() {}
     39 
     40     // This method will be called when an NDEF record |record| from the peer
     41     // device |peer| is received. Users can use this method to be notified of
     42     // new records on the device and when the initial set of records are
     43     // received from it, if any. All records received from |peer| can be
     44     // accessed by calling |peer->GetNdefMessage()|.
     45     virtual void RecordReceived(NfcPeer* peer, const NfcNdefRecord* record) {}
     46   };
     47 
     48   // The ErrorCallback is used by methods to asynchronously report errors.
     49   typedef base::Closure ErrorCallback;
     50 
     51   virtual ~NfcPeer();
     52 
     53   // Adds and removes observers for events on this NFC peer. If monitoring
     54   // multiple peers, check the |peer| parameter of observer methods to
     55   // determine which peer is issuing the event.
     56   virtual void AddObserver(Observer* observer) = 0;
     57   virtual void RemoveObserver(Observer* observer) = 0;
     58 
     59   // Returns the unique identifier assigned to this peer.
     60   virtual std::string GetIdentifier() const = 0;
     61 
     62   // Returns all NDEF records that were received from the peer device in the
     63   // form of a NDEF message. If the returned NDEF message contains no records,
     64   // this only means that no records have yet been received from the device.
     65   // Users should use this method in conjunction with the Observer methods
     66   // to be notified when the records are ready.
     67   virtual const NfcNdefMessage& GetNdefMessage() const = 0;
     68 
     69   // Sends the NDEF records contained in |message| to the peer device. On
     70   // success, |callback| will be invoked. On failure, |error_callback| will be
     71   // invoked.
     72   virtual void PushNdef(const NfcNdefMessage& message,
     73                         const base::Closure& callback,
     74                         const ErrorCallback& error_callback) = 0;
     75 
     76   // Initiates WiFi or Bluetooth pairing with the NFC peer device based on
     77   // |handover_type|. On success, |callback| will be invoked. On failure,
     78   // |error_callback| will be invoked.
     79   virtual void StartHandover(HandoverType handover_type,
     80                              const base::Closure& callback,
     81                              const ErrorCallback& error_callback) = 0;
     82 
     83  protected:
     84   NfcPeer();
     85 
     86  private:
     87   DISALLOW_COPY_AND_ASSIGN(NfcPeer);
     88 };
     89 
     90 }  // namespace device
     91 
     92 #endif  // DEVICE_NFC_NFC_PEER_H_
     93