[Pcsclite-cvs-commit] PCSC/src prothandler.c,1.11,1.12 prothandler.h,1.4,1.5

rousseau@haydn.debian.org rousseau@haydn.debian.org


Update of /cvsroot/pcsclite/PCSC/src
In directory haydn:/tmp/cvs-serv26865

Modified Files:
	prothandler.c prothandler.h 
Log Message:
PHSetProtocol():
- add a ucDefault argument as the default protocol to use
- completely redesign the function so that IFDSetPTS() is always called
  to tell the driver which protocol to use and to initialise its
  internal state


Index: prothandler.c
===================================================================
RCS file: /cvsroot/pcsclite/PCSC/src/prothandler.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- prothandler.c	18 Jun 2004 08:44:31 -0000	1.11
+++ prothandler.c	18 Jun 2004 14:46:31 -0000	1.12
@@ -74,88 +74,62 @@
  */
 
 DWORD PHSetProtocol(struct ReaderContext * rContext,
-	DWORD dwPreferred, UCHAR ucAvailable)
+	DWORD dwPreferred, UCHAR ucAvailable, UCHAR ucDefault)
 {
+	DWORD protocol;
 	LONG rv;
-	DWORD protocol = rContext->readerState->cardProtocol;
+	UCHAR ucChosen;
 
+	/* App has specified no protocol */
 	if (dwPreferred == 0)
+		return -1;
+
+	/* requested protocol is not available */
+	if (! (dwPreferred & ucAvailable))
 	{
-		/*
-		 * App has specified no protocol 
+		/* Note:
+		 * dwPreferred must be either SCARD_PROTOCOL_T0 or SCARD_PROTOCOL_T1
+		 * if dwPreferred == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 the test
+		 * (SCARD_PROTOCOL_T0 == dwPreferred) will not work as expected
+		 * and the debug message will not be correct.
+		 *
+		 * This case may only occur if
+		 * dwPreferred == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1
+		 * and ucAvailable == 0 since we have (dwPreferred & ucAvailable) == 0
+		 * and the case ucAvailable == 0 should never occur (the card is at
+		 * least T=0 or T=1)
 		 */
+		DebugLogB("Protocol T=%d requested but unsupported by the card",
+			(SCARD_PROTOCOL_T0 == dwPreferred) ? 0 : 1);
 		return -1;
 	}
 
-	if ((protocol == SCARD_PROTOCOL_T1) &&
-		((dwPreferred & SCARD_PROTOCOL_T1) == 0) &&
-		(dwPreferred & SCARD_PROTOCOL_T0))
-	{
-		if (SCARD_PROTOCOL_T0 & ucAvailable)
-		{
-			DebugLogA("Attempting PTS to T=0");
-
-			/*
-			 * Case 1: T1 is default but is not preferred 
-			 *
-			 * Action: Change to T=0 protocol.  
-			 */
-			rv = IFDSetPTS(rContext, SCARD_PROTOCOL_T0, 0x00, 0x00, 0x00, 0x00);
+	/* set default value */
+	protocol = ucDefault;
 
-			if (rv != IFD_SUCCESS)
-				protocol = SCARD_PROTOCOL_T1;
-			else
-				protocol = SCARD_PROTOCOL_T0;
-		}
+	/* we try to use T=1 first */
+	if (dwPreferred & SCARD_PROTOCOL_T1)
+		ucChosen = SCARD_PROTOCOL_T1;
+	else
+		if (dwPreferred & SCARD_PROTOCOL_T0)
+			ucChosen = SCARD_PROTOCOL_T0;
 		else
-		{
-			/*
-			 * App wants an unsupported protocol 
-			 */
-			DebugLogA("Protocol T=0 requested but unsupported by the card");
-
+			/* App wants unsupported protocol */
 			return -1;
-		}
-
-	} else if ((protocol == SCARD_PROTOCOL_T0) &&
-		((dwPreferred & SCARD_PROTOCOL_T0) == 0) &&
-		(dwPreferred & SCARD_PROTOCOL_T1))
-	{
-		if (ucAvailable & SCARD_PROTOCOL_T1)
-		{
-			DebugLogA("Attempting PTS to T=1");
 
-			/*
-			 * Case 2: T=0 is default but T=1 is preferred 
-			 *
-			 * Action: Change to T=1 only if supported 
-			 */
-			rv = IFDSetPTS(rContext, SCARD_PROTOCOL_T1, 0x00,
-				0x00, 0x00, 0x00);
-
-			if (rv != SCARD_S_SUCCESS)
-				protocol = SCARD_PROTOCOL_T0;
-			else
-				protocol = SCARD_PROTOCOL_T1;
-		}
-		else
-		{
-			/*
-			 * App wants unsupported protocol 
-			 */
-			DebugLogA("Protocol T=1 requested but unsupported by the card");
+	DebugLogB("Attempting PTS to T=%d",
+		(SCARD_PROTOCOL_T0 == ucChosen ? 0 : 1));
+	rv = IFDSetPTS(rContext, ucChosen, 0x00, 0x00, 0x00, 0x00);
 
-			return -1;
-		}
-	}
+	if (IFD_SUCCESS == rv)
+		protocol = ucChosen;
 	else
-	{
-		/*
-		 * Case 3: Default protocol is preferred 
-		 *
-		 * Action: No need to change protocols 
-		 */
-	}
+		if (IFD_NOT_SUPPORTED == rv)
+			DebugLogB("PTS not supported by driver, using T=%d",
+				(SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
+		else
+			DebugLogB("PTS failed, using T=%d",
+				(SCARD_PROTOCOL_T0 == protocol) ? 0 : 1);
 
 	return protocol;
 }

Index: prothandler.h
===================================================================
RCS file: /cvsroot/pcsclite/PCSC/src/prothandler.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- prothandler.h	18 Oct 2003 17:19:36 -0000	1.4
+++ prothandler.h	18 Jun 2004 14:46:31 -0000	1.5
@@ -19,7 +19,7 @@
 
 	UCHAR PHGetDefaultProtocol(PUCHAR, DWORD);
 	UCHAR PHGetAvailableProtocols(PUCHAR, DWORD);
-	DWORD PHSetProtocol(struct ReaderContext *, DWORD, UCHAR);
+	DWORD PHSetProtocol(struct ReaderContext *, DWORD, UCHAR, UCHAR);
 
 #ifdef __cplusplus
 }