Blame view

PN532/emulatetag.h 1.85 KB
1a2e5ee4   henyxia   Big revision
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
  /**************************************************************************/
  /*!
      @file     emulatetag.h
      @author   Armin Wieser
      @license  BSD
  
      Implemented using NFC forum documents & library of libnfc
  */
  /**************************************************************************/
  
  #ifndef __EMULATETAG_H__
  #define __EMULATETAG_H__
  
  #include "PN532.h"
  
  #define NDEF_MAX_LENGTH 128  // altough ndef can handle up to 0xfffe in size, arduino cannot.
  typedef enum {COMMAND_COMPLETE, TAG_NOT_FOUND, FUNCTION_NOT_SUPPORTED, MEMORY_FAILURE, END_OF_FILE_BEFORE_REACHED_LE_BYTES} responseCommand;
  
  class EmulateTag{
  
  public:
  EmulateTag(PN532Interface &interface) : pn532(interface), uidPtr(0), tagWrittenByInitiator(false), tagWriteable(true), updateNdefCallback(0) { }
    
    bool init();
  
    bool emulate(const uint16_t tgInitAsTargetTimeout = 0);
  
    /*
     * @param uid pointer to byte array of length 3 (uid is 4 bytes - first byte is fixed) or zero for uid 
     */
    void setUid(uint8_t* uid = 0);
  
    void setNdefFile(const uint8_t* ndef, const int16_t ndefLength);
  
    void getContent(uint8_t** buf, uint16_t* length){
      *buf = ndef_file + 2; // first 2 bytes = length
      *length = (ndef_file[0] << 8) + ndef_file[1];
    }
  
    bool writeOccured(){
      return tagWrittenByInitiator;
    }
  
    void setTagWriteable(bool setWriteable){
      tagWriteable = setWriteable;
    }
  
    uint8_t* getNdefFilePtr(){
      return ndef_file;
    }
  
    uint8_t getNdefMaxLength(){
      return NDEF_MAX_LENGTH;
    }
  
    void attach(void (*func)(uint8_t *buf, uint16_t length)) {
      updateNdefCallback = func;
    };
  
  private:
    PN532 pn532;
    uint8_t ndef_file[NDEF_MAX_LENGTH];
    uint8_t* uidPtr;
    bool tagWrittenByInitiator;
    bool tagWriteable;
    void (*updateNdefCallback)(uint8_t *ndef, uint16_t length);
  
    void setResponse(responseCommand cmd, uint8_t* buf, uint8_t* sendlen, uint8_t sendlenOffset = 0);
  };
  
  #endif