<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>pcsc-lite: prothandler.c Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.2-20100208 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>Globals</span></a></li> </ul> </div> <div class="navpath"><a class="el" href="dir_c68a5dfdca54003c765e303a18006edb.html">src</a> </div> </div> <div class="contents"> <h1>prothandler.c</h1><a href="prothandler_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> * MUSCLE SmartCard Development ( http://www.linuxnet.com )</span> <a name="l00003"></a>00003 <span class="comment"> *</span> <a name="l00004"></a>00004 <span class="comment"> * Copyright (C) 1999</span> <a name="l00005"></a>00005 <span class="comment"> * David Corcoran <corcoran@linuxnet.com></span> <a name="l00006"></a>00006 <span class="comment"> * Copyright (C) 2004</span> <a name="l00007"></a>00007 <span class="comment"> * Ludovic Rousseau <ludovic.rousseau@free.fr></span> <a name="l00008"></a>00008 <span class="comment"> *</span> <a name="l00009"></a>00009 <span class="comment"> * $Id: prothandler.c 2896 2008-04-22 09:20:00Z rousseau $</span> <a name="l00010"></a>00010 <span class="comment"> */</span> <a name="l00011"></a>00011 <a name="l00017"></a>00017 <span class="preprocessor">#include "config.h"</span> <a name="l00018"></a>00018 <span class="preprocessor">#include <string.h></span> <a name="l00019"></a>00019 <a name="l00020"></a>00020 <span class="preprocessor">#include "misc.h"</span> <a name="l00021"></a>00021 <span class="preprocessor">#include "<a class="code" href="pcscd_8h.html" title="This keeps a list of defines for pcsc-lite.">pcscd.h</a>"</span> <a name="l00022"></a>00022 <span class="preprocessor">#include "<a class="code" href="ifdhandler_8h.html" title="This provides reader specific low-level calls.">ifdhandler.h</a>"</span> <a name="l00023"></a>00023 <span class="preprocessor">#include "<a class="code" href="debuglog_8h.html" title="This handles debugging.">debuglog.h</a>"</span> <a name="l00024"></a>00024 <span class="preprocessor">#include "<a class="code" href="readerfactory_8h.html" title="This keeps track of a list of currently available reader structures.">readerfactory.h</a>"</span> <a name="l00025"></a>00025 <span class="preprocessor">#include "<a class="code" href="prothandler_8h.html" title="This handles protocol defaults, PTS, etc.">prothandler.h</a>"</span> <a name="l00026"></a>00026 <span class="preprocessor">#include "<a class="code" href="atrhandler_8h.html" title="This keeps track of smartcard protocols, timing issues and Answer to Reset ATR handling...">atrhandler.h</a>"</span> <a name="l00027"></a>00027 <span class="preprocessor">#include "<a class="code" href="ifdwrapper_8h.html" title="This wraps the dynamic ifdhandler functions.">ifdwrapper.h</a>"</span> <a name="l00028"></a>00028 <span class="preprocessor">#include "<a class="code" href="eventhandler_8h.html" title="This handles card insertion/removal events, updates ATR, protocol, and status information...">eventhandler.h</a>"</span> <a name="l00029"></a>00029 <a name="l00035"></a><a class="code" href="prothandler_8h.html#ab90a8ce8ccece0e82beb131d99e42724">00035</a> UCHAR <a class="code" href="prothandler_8c.html#a30e7c843c2a5a626a1df5abe4a116e99" title="Get the default protocol used immediately after reset.">PHGetDefaultProtocol</a>(PUCHAR pucAtr, DWORD dwLength) <a name="l00036"></a>00036 { <a name="l00037"></a>00037 <a class="code" href="struct__SMARTCARD__EXTENSION.html">SMARTCARD_EXTENSION</a> sSmartCard; <a name="l00038"></a>00038 <a name="l00039"></a>00039 <span class="comment">/*</span> <a name="l00040"></a>00040 <span class="comment"> * Zero out everything</span> <a name="l00041"></a>00041 <span class="comment"> */</span> <a name="l00042"></a>00042 memset(&sSmartCard, 0x00, <span class="keyword">sizeof</span>(<a class="code" href="struct__SMARTCARD__EXTENSION.html">SMARTCARD_EXTENSION</a>)); <a name="l00043"></a>00043 <a name="l00044"></a>00044 <span class="keywordflow">if</span> (<a class="code" href="atrhandler_8c.html#a84127d39d788360749d94c602ab3e129" title="Uncomment the following for ATR debugging or use .">ATRDecodeAtr</a>(&sSmartCard, pucAtr, dwLength)) <a name="l00045"></a>00045 <span class="keywordflow">return</span> sSmartCard.CardCapabilities.CurrentProtocol; <a name="l00046"></a>00046 <span class="keywordflow">else</span> <a name="l00047"></a>00047 <span class="keywordflow">return</span> 0x00; <a name="l00048"></a>00048 } <a name="l00049"></a>00049 <a name="l00055"></a><a class="code" href="prothandler_8h.html#ae1bf5f71af644725f12dcdb2e26e4111">00055</a> UCHAR <a class="code" href="prothandler_8c.html#a028195440f572393f25e2daf6d4df05b" title="Get the protocols supported by the card.">PHGetAvailableProtocols</a>(PUCHAR pucAtr, DWORD dwLength) <a name="l00056"></a>00056 { <a name="l00057"></a>00057 <a class="code" href="struct__SMARTCARD__EXTENSION.html">SMARTCARD_EXTENSION</a> sSmartCard; <a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="comment">/*</span> <a name="l00060"></a>00060 <span class="comment"> * Zero out everything</span> <a name="l00061"></a>00061 <span class="comment"> */</span> <a name="l00062"></a>00062 memset(&sSmartCard, 0x00, <span class="keyword">sizeof</span>(<a class="code" href="struct__SMARTCARD__EXTENSION.html">SMARTCARD_EXTENSION</a>)); <a name="l00063"></a>00063 <a name="l00064"></a>00064 <span class="keywordflow">if</span> (<a class="code" href="atrhandler_8c.html#a84127d39d788360749d94c602ab3e129" title="Uncomment the following for ATR debugging or use .">ATRDecodeAtr</a>(&sSmartCard, pucAtr, dwLength)) <a name="l00065"></a>00065 <span class="keywordflow">return</span> sSmartCard.CardCapabilities.AvailableProtocols; <a name="l00066"></a>00066 <span class="keywordflow">else</span> <a name="l00067"></a>00067 <span class="keywordflow">return</span> 0x00; <a name="l00068"></a>00068 } <a name="l00069"></a>00069 <a name="l00080"></a><a class="code" href="prothandler_8h.html#a3f27bce2e5c3ba9e7d97240d6c01a746">00080</a> DWORD <a class="code" href="prothandler_8c.html#a4522f0d45b2ab13cf3d264a605eec02d" title="Determine which protocol to use.">PHSetProtocol</a>(<span class="keyword">struct</span> <a class="code" href="structReaderContext.html">ReaderContext</a> * rContext, <a name="l00081"></a>00081 DWORD dwPreferred, UCHAR ucAvailable, UCHAR ucDefault) <a name="l00082"></a>00082 { <a name="l00083"></a>00083 DWORD protocol; <a name="l00084"></a>00084 LONG rv; <a name="l00085"></a>00085 UCHAR ucChosen; <a name="l00086"></a>00086 <a name="l00087"></a>00087 <span class="comment">/* App has specified no protocol */</span> <a name="l00088"></a>00088 <span class="keywordflow">if</span> (dwPreferred == 0) <a name="l00089"></a>00089 <span class="keywordflow">return</span> SET_PROTOCOL_WRONG_ARGUMENT; <a name="l00090"></a>00090 <a name="l00091"></a>00091 <span class="comment">/* requested protocol is not available */</span> <a name="l00092"></a>00092 <span class="keywordflow">if</span> (! (dwPreferred & ucAvailable)) <a name="l00093"></a>00093 { <a name="l00094"></a>00094 <span class="comment">/* Note:</span> <a name="l00095"></a>00095 <span class="comment"> * dwPreferred must be either SCARD_PROTOCOL_T0 or SCARD_PROTOCOL_T1</span> <a name="l00096"></a>00096 <span class="comment"> * if dwPreferred == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1 the test</span> <a name="l00097"></a>00097 <span class="comment"> * (SCARD_PROTOCOL_T0 == dwPreferred) will not work as expected</span> <a name="l00098"></a>00098 <span class="comment"> * and the debug message will not be correct.</span> <a name="l00099"></a>00099 <span class="comment"> *</span> <a name="l00100"></a>00100 <span class="comment"> * This case may only occur if</span> <a name="l00101"></a>00101 <span class="comment"> * dwPreferred == SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1</span> <a name="l00102"></a>00102 <span class="comment"> * and ucAvailable == 0 since we have (dwPreferred & ucAvailable) == 0</span> <a name="l00103"></a>00103 <span class="comment"> * and the case ucAvailable == 0 should never occur (the card is at</span> <a name="l00104"></a>00104 <span class="comment"> * least T=0 or T=1)</span> <a name="l00105"></a>00105 <span class="comment"> */</span> <a name="l00106"></a>00106 Log2(PCSC_LOG_ERROR, <span class="stringliteral">"Protocol T=%d requested but unsupported by the card"</span>, <a name="l00107"></a>00107 (<a class="code" href="pcsclite_8h.html#a4fe95267c322c3bf35f30fab6b25526c" title="T=0 active protocol.">SCARD_PROTOCOL_T0</a> == dwPreferred) ? 0 : 1); <a name="l00108"></a>00108 <span class="keywordflow">return</span> SET_PROTOCOL_WRONG_ARGUMENT; <a name="l00109"></a>00109 } <a name="l00110"></a>00110 <a name="l00111"></a>00111 <span class="comment">/* set default value */</span> <a name="l00112"></a>00112 protocol = ucDefault; <a name="l00113"></a>00113 <a name="l00114"></a>00114 <span class="comment">/* keep only the available protocols */</span> <a name="l00115"></a>00115 dwPreferred &= ucAvailable; <a name="l00116"></a>00116 <a name="l00117"></a>00117 <span class="comment">/* we try to use T=1 first */</span> <a name="l00118"></a>00118 <span class="keywordflow">if</span> (dwPreferred & <a class="code" href="pcsclite_8h.html#a3c09cd155e07044f857130c183d64f03" title="T=1 active protocol.">SCARD_PROTOCOL_T1</a>) <a name="l00119"></a>00119 ucChosen = SCARD_PROTOCOL_T1; <a name="l00120"></a>00120 <span class="keywordflow">else</span> <a name="l00121"></a>00121 <span class="keywordflow">if</span> (dwPreferred & <a class="code" href="pcsclite_8h.html#a4fe95267c322c3bf35f30fab6b25526c" title="T=0 active protocol.">SCARD_PROTOCOL_T0</a>) <a name="l00122"></a>00122 ucChosen = SCARD_PROTOCOL_T0; <a name="l00123"></a>00123 <span class="keywordflow">else</span> <a name="l00124"></a>00124 <span class="comment">/* App wants unsupported protocol */</span> <a name="l00125"></a>00125 <span class="keywordflow">return</span> SET_PROTOCOL_WRONG_ARGUMENT; <a name="l00126"></a>00126 <a name="l00127"></a>00127 Log2(PCSC_LOG_INFO, <span class="stringliteral">"Attempting PTS to T=%d"</span>, <a name="l00128"></a>00128 (SCARD_PROTOCOL_T0 == ucChosen ? 0 : 1)); <a name="l00129"></a>00129 rv = <a class="code" href="ifdwrapper_8c.html#a1f2d9c683aa5360f8576ef2c09ed4f21" title="Set the protocol type selection (PTS).">IFDSetPTS</a>(rContext, ucChosen, 0x00, 0x00, 0x00, 0x00); <a name="l00130"></a>00130 <a name="l00131"></a>00131 <span class="keywordflow">if</span> (IFD_SUCCESS == rv) <a name="l00132"></a>00132 protocol = ucChosen; <a name="l00133"></a>00133 <span class="keywordflow">else</span> <a name="l00134"></a>00134 <span class="keywordflow">if</span> (IFD_NOT_SUPPORTED == rv) <a name="l00135"></a>00135 Log2(PCSC_LOG_INFO, <span class="stringliteral">"PTS not supported by driver, using T=%d"</span>, <a name="l00136"></a>00136 (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1); <a name="l00137"></a>00137 <span class="keywordflow">else</span> <a name="l00138"></a>00138 <span class="keywordflow">if</span> (IFD_PROTOCOL_NOT_SUPPORTED == rv) <a name="l00139"></a>00139 Log2(PCSC_LOG_INFO, <span class="stringliteral">"PTS protocol not supported, using T=%d"</span>, <a name="l00140"></a>00140 (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1); <a name="l00141"></a>00141 <span class="keywordflow">else</span> <a name="l00142"></a>00142 { <a name="l00143"></a>00143 Log3(PCSC_LOG_INFO, <span class="stringliteral">"PTS failed (%d), using T=%d"</span>, rv, <a name="l00144"></a>00144 (SCARD_PROTOCOL_T0 == protocol) ? 0 : 1); <a name="l00145"></a>00145 <a name="l00146"></a>00146 <span class="comment">/* ISO 7816-3:1997 ch. 7.2 PPS protocol page 14</span> <a name="l00147"></a>00147 <span class="comment"> * - If the PPS exchange is unsuccessful, then the interface device</span> <a name="l00148"></a>00148 <span class="comment"> * shall either reset or reject the card.</span> <a name="l00149"></a>00149 <span class="comment"> */</span> <a name="l00150"></a>00150 <span class="keywordflow">return</span> SET_PROTOCOL_PPS_FAILED; <a name="l00151"></a>00151 } <a name="l00152"></a>00152 <a name="l00153"></a>00153 <span class="keywordflow">return</span> protocol; <a name="l00154"></a>00154 } <a name="l00155"></a>00155 </pre></div></div> <hr class="footer"/><address style="text-align: right;"><small>Generated by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2-20100208 </small></address> </body> </html>