Skip to content

Commit

Permalink
Merge pull request elechouse#8 from awieser/master
Browse files Browse the repository at this point in the history
fixed: ndef size + possibility to set emulated tag read only
  • Loading branch information
xiongyihui committed Nov 22, 2013
2 parents fd35495 + f8b680a commit 07e3480
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 26 deletions.
48 changes: 28 additions & 20 deletions PN532/emulatetag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,6 @@

typedef enum { NONE, CC, NDEF } tag_file; // CC ... Compatibility Container

const uint8_t compatibility_container[] = {
0, 0x0F,
0x20,
0, 0x54,
0, 0xFF,
0x04,
0x06,
0xE1, 0x04,
0xFF, 0xFE,
0x00,
0x00
};

bool EmulateTag::init(){
pn532.begin();
return pn532.SAMConfig();
Expand Down Expand Up @@ -110,6 +97,23 @@ bool EmulateTag::emulate(const uint16_t tgInitAsTargetTimeout){
return false;
}

uint8_t compatibility_container[] = {
0, 0x0F,
0x20,
0, 0x54,
0, 0xFF,
0x04, // T
0x06, // L
0xE1, 0x04, // File identifier
((NDEF_MAX_LENGTH & 0xFF00) >> 8), (NDEF_MAX_LENGTH & 0xFF), // maximum NDEF file size
0x00, // read access 0x0 = granted
0x00 // write access 0x0 = granted | 0xFF = deny
};

if(tagWriteable == false){
compatibility_container[14] = 0xFF;
}

tagWrittenByInitiator = false;

uint8_t rwbuf[128];
Expand Down Expand Up @@ -185,13 +189,17 @@ bool EmulateTag::emulate(const uint16_t tgInitAsTargetTimeout){
}
break;
case ISO7816_UPDATE_BINARY:
if( p1p2_length > NDEF_MAX_LENGTH){
setResponse(MEMORY_FAILURE, rwbuf, &sendlen);
}
else{
memcpy(ndef_file + p1p2_length, rwbuf + C_APDU_DATA, lc);
setResponse(COMMAND_COMPLETE, rwbuf, &sendlen);
tagWrittenByInitiator = true;
if(!tagWriteable){
setResponse(FUNCTION_NOT_SUPPORTED, rwbuf, &sendlen);
} else{
if( p1p2_length > NDEF_MAX_LENGTH){
setResponse(MEMORY_FAILURE, rwbuf, &sendlen);
}
else{
memcpy(ndef_file + p1p2_length, rwbuf + C_APDU_DATA, lc);
setResponse(COMMAND_COMPLETE, rwbuf, &sendlen);
tagWrittenByInitiator = true;
}
}
break;
default:
Expand Down
10 changes: 8 additions & 2 deletions PN532/emulatetag.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ typedef enum {COMMAND_COMPLETE, TAG_NOT_FOUND, FUNCTION_NOT_SUPPORTED, MEMORY_FA
class EmulateTag{

public:
EmulateTag(PN532Interface &interface) : pn532(interface), uidPtr(0), tagWrittenByInitiator(false) { }
EmulateTag(PN532Interface &interface) : pn532(interface), uidPtr(0), tagWrittenByInitiator(false), tagWriteable(true) { }

bool init();

Expand All @@ -42,12 +42,18 @@ EmulateTag(PN532Interface &interface) : pn532(interface), uidPtr(0), tagWrittenB
return tagWrittenByInitiator;
}

void setTagWriteable(bool setWriteable){
tagWriteable = setWriteable;
}

private:
PN532 pn532;
uint8_t ndef_file[NDEF_MAX_LENGTH];
uint8_t* uidPtr;
void setResponse(responseCommand cmd, uint8_t* buf, uint8_t* sendlen, uint8_t sendlenOffset =0);
bool tagWrittenByInitiator;
bool tagWriteable;

void setResponse(responseCommand cmd, uint8_t* buf, uint8_t* sendlen, uint8_t sendlenOffset = 0);
};

#endif
10 changes: 6 additions & 4 deletions PN532/examples/emulate_tag_ndef/emulate_tag_ndef.ino
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ void loop(){

// start emulation (blocks)
nfc.emulate();

// or start emulation with timeout

if(!nfc.emulate(1000)){ // timeout 1 second
/*if(!nfc.emulate(1000)){ // timeout 1 second
Serial.println("timed out");
}
}*/

// deny writing to the tag
// nfc.setTagWriteable(false);

if(nfc.writeOccured()){
Serial.println("\nWrite occured !");
Expand Down

0 comments on commit 07e3480

Please sign in to comment.