Esempio n. 1
0
int main(int argc, char *argv[])
{
	cstring hexStr = "E05A9F020600000000500082025C009F360200019F260886087FD3A9FE6FB49F100706010A03A010009F3303E0F0C09F1A0200365F2A0200369F1E08424B00081905CB9F950500000080009A030809229C01009F37041035EA1F8A00";
	byte BA[512];
	int32 len = 0;
	int ret = 0;
	char hexString[1024];
	
	ret = hexStringToByteArray (BA, &len, hexStr);

	printf("ret = %d, len=%d\n", ret, len);
	
	if(ret)
		return -1;

	printf("byte array result:\r\n");
	
	for(ret = 0; ret < len; ret++)
		printf("%02X", BA[ret]);

	printf("\r\n");

	ret = byteArrayToHexString (hexString, BA, len);

	printf("ret = %d, len=%d\n", ret, strlen(hexString));
	
	if(ret)
		return -1;

	printf("hex string result:\r\n%s", hexString);
	printf("\r\n");
	
	return 0;
}
Esempio n. 2
0
//---------------------------------------------------------------------
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 );
    }
}