// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/CardAndTerminalManager.java
// public enum Protocol {
// 	T0("T=0"), T1("T=1"), TCL("T=CL"), ANY("*");
// 
// 	private final String protocol;
// 
// 	Protocol(String protocol) {
// 		this.protocol = protocol;
// 	}
// 
// 	String getProtocol() {
// 		return protocol;
// 	}
// }
// 
// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/event/BeIDCardEventsListener.java
// public interface BeIDCardEventsListener {
// 	void eIDCardEventsInitialized();
// 
// 	void eIDCardInserted(CardTerminal cardTerminal, BeIDCard card);
// 
// 	void eIDCardRemoved(CardTerminal cardTerminal, BeIDCard card);
// }
// 
// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/event/CardEventsListener.java
// public interface CardEventsListener {
// 	void cardEventsInitialized();
// 
// 	void cardInserted(CardTerminal cardTerminal, Card card);
// 
// 	void cardRemoved(CardTerminal cardTerminal);
// }
// 
// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/impl/LocaleManager.java
// public final class LocaleManager {
// 
// 	private static Locale locale;
// 
// 	private LocaleManager() {
// 		super();
// 	}
// 
// 	public static void setLocale(Locale newLocale) {
// 		LocaleManager.locale = newLocale;
// 	}
// 
// 	public static Locale getLocale() {
// 		if (LocaleManager.locale == null) {
// 			LocaleManager.locale = Locale.getDefault();
// 		}
// 
// 		return LocaleManager.locale;
// 	}
// }
// 
// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/impl/VoidLogger.java
// public final class VoidLogger implements Logger {
// 
// 	public VoidLogger() {
// 		super();
// 	}
// 
// 	@Override
// 	public void error(final String message) {
// 		// empty
// 	}
// 
// 	@Override
// 	public void debug(final String message) {
// 		// empty
// 	}
// }
// 
// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/spi/Logger.java
// public interface Logger {
// 
// 	/**
// 	 * Error messages receiver.
// 	 *
// 	 * @param message the error message.
// 	 */
// 	void error(String message);
// 
// 	/**
// 	 * Debug messages receiver.
// 	 *
// 	 * @param message the debug message.
// 	 */
// 	void debug(String message);
// }


import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import be.bosa.commons.eid.client.CardAndTerminalManager.Protocol;
import be.bosa.commons.eid.client.event.BeIDCardEventsListener;
import be.bosa.commons.eid.client.event.CardEventsListener;
import be.bosa.commons.eid.client.impl.LocaleManager;
import be.bosa.commons.eid.client.impl.VoidLogger;
import be.bosa.commons.eid.client.spi.Logger;
import javax.smartcardio.ATR;
import javax.smartcardio.Card;
import javax.smartcardio.CardTerminal;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
/*
 * Commons eID Project.
 * Copyright (C) 2014 - 2018 BOSA.
 *
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License version 3.0 as published by
 * the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; if not, see https://www.gnu.org/licenses/.
 */

package be.bosa.commons.eid.client;



/**
 * A BeIDCardManager uses a {@link CardAndTerminalManager} to detect Card
 * Insertion and Removal Events, distinguishes between Belgian eID and other
 * cards, calls any registered BeIDCardEventsListeners for eID cards inserted
 * and removed, and any registered CardEventsListener for other cards being
 * inserted and removed. Note that by default, a BeIDCardManager will only
 * connect to cards using card protocol "T=0" to ensure optimal compatibility
 * with Belgian eID cards in all card readers, meaning that if you wish to use
 * its "other card" facility you may have to supply your own
 * CardAndTerminalManager with a protocol setting of "ALL".
 *
 * @author Frank Marien
 * @author Frank Cornelis
 */
public class BeIDCardManager {

	private static final byte[] ATR_PATTERN = new byte[]{0x3b, (byte) 0x98,
			0x00, 0x40, 0x00, (byte) 0x00, 0x00, 0x00, 0x01, 0x01, (byte) 0xad,
			0x13, 0x10,};
	private static final byte[] ATR_MASK = new byte[]{(byte) 0xff, (byte) 0xff,
			0x00, (byte) 0xff, 0x00, 0x00, 0x00, 0x00, (byte) 0xff,
			(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xf0,};

	private final CardAndTerminalManager cardAndTerminalManager;
	private final Map<CardTerminal, BeIDCard> terminalsAndCards;
	private final Set<BeIDCardEventsListener> beIdListeners;
// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/CardAndTerminalManager.java
// public enum Protocol {
// 	T0("T=0"), T1("T=1"), TCL("T=CL"), ANY("*");
// 
// 	private final String protocol;
// 
// 	Protocol(String protocol) {
// 		this.protocol = protocol;
// 	}
// 
// 	String getProtocol() {
// 		return protocol;
// 	}
// }
// 
// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/event/BeIDCardEventsListener.java
// public interface BeIDCardEventsListener {
// 	void eIDCardEventsInitialized();
// 
// 	void eIDCardInserted(CardTerminal cardTerminal, BeIDCard card);
// 
// 	void eIDCardRemoved(CardTerminal cardTerminal, BeIDCard card);
// }
// 
// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/event/CardEventsListener.java
// public interface CardEventsListener {
// 	void cardEventsInitialized();
// 
// 	void cardInserted(CardTerminal cardTerminal, Card card);
// 
// 	void cardRemoved(CardTerminal cardTerminal);
// }
// 
// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/impl/LocaleManager.java
// public final class LocaleManager {
// 
// 	private static Locale locale;
// 
// 	private LocaleManager() {
// 		super();
// 	}
// 
// 	public static void setLocale(Locale newLocale) {
// 		LocaleManager.locale = newLocale;
// 	}
// 
// 	public static Locale getLocale() {
// 		if (LocaleManager.locale == null) {
// 			LocaleManager.locale = Locale.getDefault();
// 		}
// 
// 		return LocaleManager.locale;
// 	}
// }
// 
// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/impl/VoidLogger.java
// public final class VoidLogger implements Logger {
// 
// 	public VoidLogger() {
// 		super();
// 	}
// 
// 	@Override
// 	public void error(final String message) {
// 		// empty
// 	}
// 
// 	@Override
// 	public void debug(final String message) {
// 		// empty
// 	}
// }
// 
// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/spi/Logger.java
// public interface Logger {
// 
// 	/**
// 	 * Error messages receiver.
// 	 *
// 	 * @param message the error message.
// 	 */
// 	void error(String message);
// 
// 	/**
// 	 * Debug messages receiver.
// 	 *
// 	 * @param message the debug message.
// 	 */
// 	void debug(String message);
// }


// Path: commons-eid-client/src/main/java/be/bosa/commons/eid/client/BeIDCardManager.java
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import be.bosa.commons.eid.client.CardAndTerminalManager.Protocol;
import be.bosa.commons.eid.client.event.BeIDCardEventsListener;
import be.bosa.commons.eid.client.event.CardEventsListener;
import be.bosa.commons.eid.client.impl.LocaleManager;
import be.bosa.commons.eid.client.impl.VoidLogger;
import be.bosa.commons.eid.client.spi.Logger;
import javax.smartcardio.ATR;
import javax.smartcardio.Card;
import javax.smartcardio.CardTerminal;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;

/*
 * Commons eID Project.
 * Copyright (C) 2014 - 2018 BOSA.
 *
 * This is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License version 3.0 as published by
 * the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software; if not, see https://www.gnu.org/licenses/.
 */

package be.bosa.commons.eid.client;



/**
 * A BeIDCardManager uses a {@link CardAndTerminalManager} to detect Card
 * Insertion and Removal Events, distinguishes between Belgian eID and other
 * cards, calls any registered BeIDCardEventsListeners for eID cards inserted
 * and removed, and any registered CardEventsListener for other cards being
 * inserted and removed. Note that by default, a BeIDCardManager will only
 * connect to cards using card protocol "T=0" to ensure optimal compatibility
 * with Belgian eID cards in all card readers, meaning that if you wish to use
 * its "other card" facility you may have to supply your own
 * CardAndTerminalManager with a protocol setting of "ALL".
 *
 * @author Frank Marien
 * @author Frank Cornelis
 */
public class BeIDCardManager {

	private static final byte[] ATR_PATTERN = new byte[]{0x3b, (byte) 0x98,
			0x00, 0x40, 0x00, (byte) 0x00, 0x00, 0x00, 0x01, 0x01, (byte) 0xad,
			0x13, 0x10,};
	private static final byte[] ATR_MASK = new byte[]{(byte) 0xff, (byte) 0xff,
			0x00, (byte) 0xff, 0x00, 0x00, 0x00, 0x00, (byte) 0xff,
			(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xf0,};

	private final CardAndTerminalManager cardAndTerminalManager;
	private final Map<CardTerminal, BeIDCard> terminalsAndCards;
	private final Set<BeIDCardEventsListener> beIdListeners;
	private final Set<CardEventsListener> otherCardListeners;