std::shared_ptr<Chip> NFCReaderUnit::getSingleChip() { std::shared_ptr<Chip> chip = d_insertedChip; if (!chip) { std::vector<std::shared_ptr<Chip> > chips = getChipList(); if (chips.size() > 0) chip = chips.front(); } return chip; }
bool ReaderUnit::waitInsertion(const std::vector<unsigned char>& identifier, unsigned int maxwait) { LOG(LogLevel::INFOS) << "Started for identifier " << BufferHelper::getHex(identifier) << " - maxwait " << maxwait; bool inserted = false; boost::posix_time::ptime currentDate = boost::posix_time::second_clock::local_time(); boost::posix_time::ptime maxDate = currentDate + boost::posix_time::milliseconds(maxwait); while (!inserted && currentDate < maxDate) { if (waitInsertion(maxwait)) { LOG(LogLevel::INFOS) << "Chip(s) detected ! Looking in the list to find the chip..."; bool found = false; std::vector<std::shared_ptr<Chip> > chipList = getChipList(); for (std::vector<std::shared_ptr<Chip> >::iterator i = chipList.begin(); i != chipList.end() && !found; ++i) { std::vector<unsigned char> tmp = (*i)->getChipIdentifier(); LOG(LogLevel::INFOS) << "Processing chip " << BufferHelper::getHex(tmp) << "..."; if (tmp == identifier) { LOG(LogLevel::INFOS) << "Chip found !"; // re-assign the chip d_insertedChip = *i; found = true; } } if (found) { inserted = true; } else { d_insertedChip.reset(); } } currentDate = boost::posix_time::second_clock::local_time(); } LOG(LogLevel::INFOS) << "Returns inserted " << inserted << " - expired " << (currentDate >= maxDate); return inserted; }