Пример #1
0
/*
 * Automagically allocate a MifareTag given a device and target info.
 */
MifareTag
freefare_tag_new (nfc_device *device, nfc_iso14443a_info nai)
{
    bool found = false;
    struct supported_tag *tag_info;
    MifareTag tag;

    /* Ensure the target is supported */
    for (size_t i = 0; i < sizeof (supported_tags) / sizeof (struct supported_tag); i++) {
	if (((nai.szUidLen == 4) || (nai.abtUid[0] == NXP_MANUFACTURER_CODE)) &&
	    (nai.btSak == supported_tags[i].SAK) &&
	    (!supported_tags[i].ATS_min_length || ((nai.szAtsLen >= supported_tags[i].ATS_min_length) &&
						   (0 == memcmp (nai.abtAts, supported_tags[i].ATS, supported_tags[i].ATS_compare_length)))) &&
	    ((supported_tags[i].check_tag_on_reader == NULL) ||
	     supported_tags[i].check_tag_on_reader(device, nai))) {

	    tag_info = &(supported_tags[i]);
	    found = true;
	    break;
	}
    }

    if (!found)
	return NULL;

    /* Allocate memory for the found MIFARE target */
    switch (tag_info->type) {
    case CLASSIC_1K:
    case CLASSIC_4K:
	tag = mifare_classic_tag_new ();
	break;
    case DESFIRE:
	tag = mifare_desfire_tag_new ();
	break;
    case ULTRALIGHT:
    case ULTRALIGHT_C:
	tag = mifare_ultralight_tag_new ();
	break;
    }

    if (!tag)
	return NULL;

    /*
     * Initialize common fields
     * (Target specific fields are initialized in mifare_*_tag_new())
     */
    tag->device = device;
    tag->info = nai;
    tag->active = 0;
    tag->tag_info = tag_info;

    return tag;
}
Пример #2
0
/*
 * Get a list of the MIFARE targets near to the provided NFC initiator.
 *
 * The list has to be freed using the freefare_free_tags() function.
 */
MifareTag *
freefare_get_tags (nfc_device_t *device)
{
    MifareTag *tags = NULL;
    int tag_count = 0;

    nfc_initiator_init(device);

    // Drop the field for a while
    nfc_configure(device,NDO_ACTIVATE_FIELD,false);

    // Let the reader only try once to find a tag
    nfc_configure(device,NDO_INFINITE_SELECT,false);

    // Configure the CRC and Parity settings
    nfc_configure(device,NDO_HANDLE_CRC,true);
    nfc_configure(device,NDO_HANDLE_PARITY,true);

    // Enable field so more power consuming cards can power themselves up
    nfc_configure(device,NDO_ACTIVATE_FIELD,true);

    // Poll for a ISO14443A (MIFARE) tag
    nfc_target_info_t target_info;

    tags = malloc(sizeof (void *));
    if(!tags) return NULL;
    tags[0] = NULL;
    
    while (nfc_initiator_select_passive_target(device,NM_ISO14443A_106,NULL,0,&target_info)) {

		bool found = false;
		struct supported_tag *tag_info;

		for (size_t i = 0; i < sizeof (supported_tags) / sizeof (struct supported_tag); i++) {
			if (((target_info.nai.szUidLen == 4) || (target_info.nai.abtUid[0] == NXP_MANUFACTURER_CODE)) &&
			(target_info.nai.btSak == supported_tags[i].SAK) &&
			(target_info.nai.szAtsLen == supported_tags[i].ATS_length) &&
			(0 == memcmp (target_info.nai.abtAts, supported_tags[i].ATS, supported_tags[i].ATS_length))) {

			tag_info = &(supported_tags[i]);
			found = true;
			break;
			}
		}

		if (!found)
			goto deselect;

		tag_count++;

		/* (Re)Allocate memory for the found MIFARE targets array */
		MifareTag *p = realloc (tags, (tag_count + 1) * sizeof (MifareTag));
		if (p)
			tags = p;
		else
			return tags; // FAIL! Return what has been found so far.

		/* Allocate memory for the found MIFARE target */
		switch (tag_info->type) {
			case CLASSIC_1K:
			case CLASSIC_4K:
			tags[tag_count-1] = mifare_classic_tag_new ();
			break;
			case DESFIRE:
			tags[tag_count-1] = mifare_desfire_tag_new (0); //JIM
			break;
			case ULTRALIGHT:
			tags[tag_count-1] = mifare_ultralight_tag_new ();
			break;
		}

		if (!tags[tag_count-1])
			return tags; // FAIL! Return what has been found before.

		/*
		 * Initialize common fields
		 * (Target specific fields are initialized in mifare_*_tag_new())
		 */
		(tags[tag_count-1])->device = device;
		(tags[tag_count-1])->info = target_info.nai;
		(tags[tag_count-1])->active = 0;
		(tags[tag_count-1])->tag_info = tag_info;
		tags[tag_count] = NULL;

deselect:
		nfc_initiator_deselect_target (device);
    }

    return tags;
}
Пример #3
0
/*
 * Automagically allocate a FreefareTag given a device and target info.
 */
FreefareTag
freefare_tag_new (nfc_device *device, nfc_target target)
{
    bool found = false;
    struct supported_tag *tag_info;
    FreefareTag tag;

    /* Ensure the target is supported */
    for (size_t i = 0; i < sizeof (supported_tags) / sizeof (struct supported_tag); i++) {
        if (target.nm.nmt != supported_tags[i].modulation_type)
            continue;

        if (target.nm.nmt == NMT_FELICA) {
            tag_info = &(supported_tags[i]);
            found = true;
            break;
        }
        if ((target.nm.nmt == NMT_ISO14443A) && ((target.nti.nai.szUidLen == 4) || (target.nti.nai.abtUid[0] == NXP_MANUFACTURER_CODE) || (target.nti.nai.abtUid[0] == RANDOM_UID)) &&
                (target.nti.nai.btSak == supported_tags[i].SAK) &&
                (!supported_tags[i].ATS_min_length || ((target.nti.nai.szAtsLen >= supported_tags[i].ATS_min_length) &&
                        (0 == memcmp (target.nti.nai.abtAts, supported_tags[i].ATS, supported_tags[i].ATS_compare_length)))) &&
                ((supported_tags[i].check_tag_on_reader == NULL) ||
                 supported_tags[i].check_tag_on_reader(device, target.nti.nai))) {

            tag_info = &(supported_tags[i]);
            found = true;
            break;
        }
    }

    if (!found)
        return NULL;

    /* Allocate memory for the found MIFARE target */
    switch (tag_info->type) {
    case FELICA:
        tag = felica_tag_new ();
        break;
    case MIFARE_CLASSIC_1K:
    case MIFARE_CLASSIC_4K:
        tag = mifare_classic_tag_new ();
        break;
    case MIFARE_DESFIRE:
        tag = mifare_desfire_tag_new ();
        break;
    case MIFARE_ULTRALIGHT:
    case MIFARE_ULTRALIGHT_C:
        tag = mifare_ultralight_tag_new ();
        break;
    }

    if (!tag)
        return NULL;

    /*
     * Initialize common fields
     * (Target specific fields are initialized in mifare_*_tag_new())
     */
    tag->device = device;
    tag->info = target;
    tag->active = 0;
    tag->tag_info = tag_info;

    return tag;
}