void NFCReaderUnit::refreshChipList()
	{
		nfc_safe_call(nfc_initiator_init, d_device);

		// Drop the field for a while
		nfc_safe_call(nfc_device_set_property_bool, d_device, NP_ACTIVATE_FIELD, false);

		// Configure the CRC and Parity settings
		nfc_safe_call(nfc_device_set_property_bool, d_device, NP_HANDLE_CRC, true);
		nfc_safe_call(nfc_device_set_property_bool, d_device, NP_HANDLE_PARITY, true);
		nfc_safe_call(nfc_device_set_property_bool, d_device, NP_AUTO_ISO14443_4, true);

		// Enable field so more power consuming cards can power themselves up
		nfc_safe_call(nfc_device_set_property_bool, d_device, NP_ACTIVATE_FIELD, true);

		// Poll for a ISO14443A (MIFARE) tag
		nfc_target candidates[MAX_CANDIDATES];
		int candidates_count;
		nfc_modulation modulation;
		modulation.nmt = NMT_ISO14443A;
		modulation.nbr = NBR_106;
		if ((candidates_count = nfc_initiator_list_passive_targets(d_device, modulation, candidates, MAX_CANDIDATES)) < 0)
			throw LibLogicalAccessException("ISO14443A nfc_initiator_list_passive_targets error");

		for (int c = 0; c < candidates_count; c++)
		{
			std::string ctype = getCardTypeFromTarget(candidates[c]);
			if (ctype != "")
			{
				std::shared_ptr<Chip> chip = createChip(ctype);
				if (chip)
				{
					d_chips[chip] = candidates[c];
				}
			}
		}

		// Poll for a FELICA tag
		modulation.nmt = NMT_FELICA;
		modulation.nbr = NBR_424; // FIXME NBR_212 should also be supported
		if ((candidates_count = nfc_initiator_list_passive_targets(d_device, modulation, candidates, MAX_CANDIDATES)) < 0)
			throw LibLogicalAccessException("FELICA nfc_initiator_list_passive_targets error");

		for (int c = 0; c < candidates_count; c++)
		{
			std::string ctype = getCardTypeFromTarget(candidates[c]);
			if (ctype != "")
			{
				std::shared_ptr<Chip> chip = createChip(ctype);
				if (chip)
				{
					d_chips[chip] = candidates[c];
				}
			}
		}
	}
    std::shared_ptr<Chip> OK5553ReaderUnit::getChipInAir(unsigned int maxwait)
    {
        LOG(LogLevel::INFOS) << "Starting get chip in air...";

        std::shared_ptr<Chip> chip;
        std::vector<unsigned char> buf;
        unsigned int currentWait = 0;
        while (!chip && (maxwait == 0 || currentWait < maxwait))
        {
            try
            {
                buf = getDefaultOK5553ReaderCardAdapter()->sendAsciiCommand("s");
            }
            catch (std::exception&)
            {
                buf.clear();
            }
            d_successedRATS.clear();
            if (buf.size() > 0)
            {
                buf = asciiToHex(buf);
                if (buf[0] == ChipType::MIFARE)
                {
                    chip = createChip("Mifare");
                    buf.erase(buf.begin());
                    chip->setChipIdentifier(buf);
                }
                else if (buf[0] == ChipType::DESFIRE)
                {
                    chip = createChip("DESFire");
                    buf.erase(buf.begin());
                    chip->setChipIdentifier(buf);
                    std::dynamic_pointer_cast<DESFireISO7816Commands>(chip->getCommands())->getCrypto()->setCryptoContext(std::dynamic_pointer_cast<DESFireProfile>(chip->getProfile()), chip->getChipIdentifier());
                }
                else if (buf[0] == ChipType::MIFAREULTRALIGHT)
                {
                    chip = createChip("MifareUltralight");
                    buf.erase(buf.begin());
                    chip->setChipIdentifier(buf);
                }
            }
            if (!chip)
            {
                std::this_thread::sleep_for(std::chrono::milliseconds(250));
                currentWait += 250;
            }
        }

        return chip;
    }
Example #3
0
 std::shared_ptr<Chip> ReaderUnit::createChip(std::string type, const std::vector<unsigned char>& identifier)
 {
     LOG(LogLevel::INFOS) << "Creating chip for card type {" << type << "} and identifier " << BufferHelper::getHex(identifier) << "...";
     std::shared_ptr<Chip> chip = createChip(type);
     chip->setChipIdentifier(identifier);
     return chip;
 }
    std::shared_ptr<Chip> IdOnDemandReaderUnit::getChipInAir()
    {
        std::shared_ptr<Chip> chip;

        // Change the reader state, but no other function to achieve the card detection...
        if (read())
        {
			chip = createChip(CHIP_GENERICTAG);
        }

        return chip;
    }
	bool OSDPReaderUnit::waitInsertion(unsigned int maxwait)
	{
		unsigned int currentWait = 0;
        bool inserted = false;

        do
        {
            std::shared_ptr<OSDPChannel> poll = m_commands->poll();

            LOG(LogLevel::INFOS) << "Reader poll command: " << std::hex << poll->getCommandsType();

            if (poll->getCommandsType() == OSDPCommandsType::XRD)
            {
                std::vector<unsigned char>& data = poll->getData();
                if (data.size() > 2 && data[0x01] == 0x01) //osdp_PRES
                    inserted = true;
            }
            else
            {
                if (poll->getCommandsType() == OSDPCommandsType::LSTATR && poll->getData().size() > 1) {
                    LOG(LogLevel::INFOS) << "Tamper status changed to: " << static_cast<bool>(poll->getData()[0x00] != 0);
					m_tamperStatus = static_cast<bool>(poll->getData()[0x00] != 0);
                }
                std::this_thread::sleep_for(std::chrono::milliseconds(100));
                currentWait += 100;
            }
        } while (!inserted && (maxwait == 0 || currentWait < maxwait));

		if (inserted)
		{
			s_led_cmd	osdp_LED_cmd = { 0,0,TemporaryControleCode::SetTemporaryState,5,5, OSDPColor::Green, OSDPColor::Black,30,0,
				PermanentControlCode::SetPermanentState,1,0,OSDPColor::Red,OSDPColor::Black };
			m_commands->led(osdp_LED_cmd);

			s_buz_cmd	osdp_BUZ_cmd = { 0,2,1,1,3 };
			m_commands->buz(osdp_BUZ_cmd);

			d_insertedChip = createChip(d_card_type);
		}

		return inserted;
	}
std::shared_ptr<Chip> STidPRGReaderUnit::getCurrentChip()
{
    auto ret = getDefaultReaderCardAdapter()->sendCommand({0x21, 0, 0, 0});
    if (ret.size() > 3)
    {
        auto uid_len = ret[2];
        if (static_cast<size_t>(3 + uid_len) >= ret.size())
        {
            auto card_uid = std::vector<uint8_t>(ret.begin() + 3, ret.end());
            auto chip     = createChip("Prox", card_uid);
            auto cmd      = std::make_shared<ProxCommand>();
            cmd->setReaderCardAdapter(getDefaultReaderCardAdapter());
            chip->setCommands(cmd);
            return chip;
        }
        else
            assert(0);
    }
    return nullptr;
}
    bool OK5553ReaderUnit::waitInsertion(unsigned int maxwait)
    {
        bool inserted = false;
        if (removalIdentifier.size() > 0)
        {
            d_insertedChip = createChip((d_card_type == "UNKNOWN") ? "GenericTag" : d_card_type);
            d_insertedChip->setChipIdentifier(removalIdentifier);
            removalIdentifier.clear();
            inserted = true;
        }
        else
        {
            std::shared_ptr<Chip> chip = getChipInAir(maxwait);
            if (chip)
            {
                d_insertedChip = chip;
                inserted = true;
            }
        }

        return inserted;
    }