//--------------------------------------------------------------------- uint8_t getCommand() { if ( !client_.available() ) return (uint8_t)(-1); uint8_t in_buf = '\0'; size_t bytes_read = client_.readBytes( (char*)&in_buf, 1 ); if ( bytes_read < 1 ) return (uint8_t)(-1); return in_buf; }
//--------------------------------------------------------------------- void emulateNfcTag() { client_.println(CMD_NFC_EMULATION_STARTED); uint8_t tag_writeable = 0; char uid_str[6]; uint8_t uid[] = {0, 0, 0}; int16_t bytes_read = 0; bool err = false; bytes_read = client_.read( &tag_writeable, 1 ); if ( bytes_read != 1 ) { #ifdef DEBUG_SERIAL Serial.print( "ERROR: emulateNfcTag: received " ); Serial.print( bytes_read ); Serial.println( " bytes"); #endif return; } bytes_read = client_.readBytes( uid_str, 6 ); err = hexStringToByteArray( uid_str, 6, uid ); if ( bytes_read != 6 ) { #ifdef DEBUG_SERIAL Serial.print( "ERROR: emulateNfcTag: received " ); Serial.print( bytes_read ); Serial.println( " bytes"); #endif return; } if ( err ) return; #ifdef DEBUG_SERIAL Serial.print( "DEBUG: tag writeable = "); Serial.println( tag_writeable - '0' ); Serial.print( "DEBUG: uid = " ); Serial.print( uid[0], HEX ); Serial.print( " " ); Serial.print( uid[1], HEX ); Serial.print( " " ); Serial.println( uid[2], HEX ); #endif // read hexstring from input and set it as NDEF message uint8_t *ndef_fd = nfc_.getNdefFilePtr(); uint8_t max_ndef_len = nfc_.getNdefMaxLength(); #ifdef DEBUG_SERIAL Serial.print( "DEBUG: max ndef len = " ); Serial.println( max_ndef_len ); #endif char ndef_msg_str[max_ndef_len]; bytes_read = client_.readBytes( ndef_msg_str, max_ndef_len ); bytes_read = ( bytes_read > max_ndef_len ) ? max_ndef_len : bytes_read; err = hexStringToByteArray( ndef_msg_str, bytes_read - 1, ndef_fd ); if ( err ) return; nfc_.setTagWriteable( tag_writeable == CMD_NFC_TAG_WRITEABLE ); nfc_.setUid( uid ); changeSPI( PIN_PN532_CS ); if ( !nfc_.emulate(NFC_EMULATION_TIMEOUT_MS) ) { delay(1000); changeSPI( PIN_ETHERNET_CS ); client_.println( CMD_NFC_EMULATION_TIMEOUT ); #ifdef DEBUG_SERIAL Serial.print( "ERROR: "); Serial.println( CMD_NFC_EMULATION_TIMEOUT ); #endif } else { delay(1000); changeSPI( PIN_ETHERNET_CS ); if ( nfc_.writeOccured() ) { uint8_t *tagbuf; uint16_t length; nfc_.getContent( &tagbuf, &length ); client_.println( NDEF_MODIFIED ); #ifdef DEBUG_SERIAL Serial.print( "ERROR: "); Serial.println( NDEF_MODIFIED ); #endif for ( uint8_t i = 0; i < length; i++ ) { if ( tagbuf[i] < 0x10 ) client_.print( "0" ); client_.print( tagbuf[i], HEX ); } client_.print( "\n" ); } client_.println( CMD_NFC_EMULATION_FINISHED ); } }