Topic: [SOLVED] emulate-tag abort

Hi there!

I'm using a SCL3711 (pn533) dongle on a Ubuntu 11.04 to emulate an NFC Forum type2 tag. I followed the current example for the type4 tag and I know that it works, since I read the tag correctly with another reader. So far so good. My issue is that I need to abort the blocking call (nft_target_init), and I'm doing it just like the example (nfc_abort_command), but the hardware/bus/... gets in an error state that only is reseted by a hard unplug.

The problem is on the following connect (it starts to complain about permissions, i.e., it can't open the USB device correctly). The output from disconnect is: "ERROR: Unexpected PN53x reply!"

In my dmesg:
[32228.396309] usb 2-1.1: usbfs: USBDEVFS_CONTROL failed cmd test_send_marte rqt 128 rq 6 len 255 ret -110
[32229.395827] usb 2-1.1: usbfs: USBDEVFS_CONTROL failed cmd test_send_marte rqt 128 rq 6 len 255 ret -110

Also, my program doesn't work with revisions later than -r 1010 (i.e., 1011 screws it).

Do you have any hints on this ??

Thank in advance

Re: [SOLVED] emulate-tag abort

Hello,

Could you provide the source code? I'll try here with the same hardware and software setup.

Romuald Conty

Re: [SOLVED] emulate-tag abort

#include <stdlib.h>
#include <signal.h>
#include <nfc/nfc.h>
#include <nfc/nfc-emulation.h>

#include "nfc-utils.h"


static nfc_device_t *pnd;

void stop_emulation (int sig)
{
    if (pnd) {
        nfc_abort_command(pnd);
        nfc_disconnect(pnd);
        exit(EXIT_SUCCESS);
    }
    else {
        exit (EXIT_FAILURE);
    }
}

int nfcforum_tag2_io (struct nfc_emulator *emulator, const byte_t *data_in, const size_t data_in_len, byte_t *data_out, const size_t data_out_len)
{
    int res = 0;
   unsigned char * __msg = ((msg_t*)(emulator->user_data))-> data;

    printf ("    In: ");
    print_hex (data_in, data_in_len);

  if ((data_in_len == 2))  {
      switch(data_in[0]) {
          case 0x30:
            printf(" Sending block %i to device\n", data_in[1]);
            memcpy(data_out, __msg+(data_in[1]*4), 16);
            res = 16;
            break;
       }
  }
  return res;

}


int main(int argc, char *argv[]){
    msg_t *m;
    nfc_target_t nt = {
        .nm = {
            .nmt = NMT_ISO14443A,
            .nbr = NBR_UNDEFINED, // Will be updated by nfc_target_init()
        },
        .nti = {
            .nai = {
                .abtAtqa = { 0x00, 0x0C },
                .abtUid = { 0x08, 0x00, 0xb0, 0x0b },
                .szUidLen = 4,
                .btSak = 0x00,
                .abtAts = { 0x75, 0x77, 0x81, 0x02, 0x80 },
                .szAtsLen = 0,
            },
        }
    };

    struct nfc_emulation_state_machine state_machine = {
        .io   = nfcforum_tag2_io
    };

    struct nfc_emulator emulator = {
        .target= &nt,
        .state_machine = &state_machine,
    };

    signal (SIGINT, stop_emulation);
    pnd = nfc_connect (NULL);

    if (pnd == NULL) {
        ERR("Unable to connect to NFC device");
        exit (EXIT_FAILURE);
    }

    printf ("Connected to NFC device: %s\n", pnd->acName);
    printf ("Emulating NDEF tag now, please touch it with a second NFC device\n");

    if ( argc == 3 )    {
        m = create_ndef_message(argv[1], argv[2]);
    }
    else {
       exit(-1);
    }
    emulator.user_data = m;
    nfc_emulate_target (pnd, &emulator);

    nfc_disconnect(pnd);

    printf("over & out\n");
    return 0;
}

Remember: it doesn't with revisions later than 1010

Re: [SOLVED] emulate-tag abort

Hello,

I see your code and stop me if I'm wrong but you have two separate problems:
- USB problems (needs hardreset)
- Ultralight/NFC-ForumTag2 emulation problem.

About the emulation problem, I'm not able to reproduce your problem without writing some code (I haven't ready-to-work Tag2 reader and your code is not complete)... So could you post here the two full debug logs from r1010 and from r1011 (prefer http://pastebin.com/ or similar) with a Tag2 read from another device.

About the USB problems, the PN53x USB connection is "easy" to broke: in some corner cases, the PN53x stops to work due to an invalid exchange. In r1081, an hack have been implemented in pn53x_usb driver to potentially recoves from your problem.
BTW, your code needs to be improved:
Current code:

void stop_emulation (int sig)
{
    if (pnd) {
        nfc_abort_command(pnd);
        nfc_disconnect(pnd);
        exit(EXIT_SUCCESS);
    }
    else {
        exit (EXIT_FAILURE);
    }
}

When you send a nfc_abort_command(), you NEED to wait the current running function ends (ie. nfc_emulate_target()).
So, you code should now be:

void stop_emulation (int sig)
{
    if (pnd) {
        nfc_abort_command(pnd);
        // No disconnection, no exit
    }
    else {
        exit (EXIT_FAILURE);
    }
}

and your nfc_emulate_target() code should be:

    if (nfc_emulate_target (pnd, &emulator) < 0) {
      goto error;
    }

    nfc_disconnect(pnd);

    printf("over & out\n");
    return EXIT_SUCCESS;

error:
    if (pnd) {
      nfc_perror (pnd, argv[0]);
      nfc_disconnect (pnd);
      exit (EXIT_FAILURE);
    }

I hope it helps

Romuald Conty

Re: [SOLVED] emulate-tag abort

Hello,

There is now a dedicated [experimental] NFC Forum Tag Type 2 emulator, tested with Google Nexus S phone (Android 2.3.3) in libnfc's examples.

Feel free to improve it!

Romuald Conty