Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 6c21d55247420ace69cad1d05267fc34 > files > 47

pcsc-lite-doc-1.7.2-4.fc15.noarch.rpm

<!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: eventhandler.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.7.4 -->
<div id="top">
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">pcsc-lite&#160;<span id="projectnumber">1.7.2</span></div>
  </td>
 </tr>
 </tbody>
</table>
</div>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;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 id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><a class="el" href="dir_7a4553ee0c3b13d0e595a5d1659110a6.html">src</a>      </li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">eventhandler.c</div>  </div>
</div>
<div class="contents">
<a href="eventhandler_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) 2000-2002</span>
<a name="l00005"></a>00005 <span class="comment"> *  David Corcoran &lt;corcoran@linuxnet.com&gt;</span>
<a name="l00006"></a>00006 <span class="comment"> * Copyright (C) 2002-2010</span>
<a name="l00007"></a>00007 <span class="comment"> *  Ludovic Rousseau &lt;ludovic.rousseau@free.fr&gt;</span>
<a name="l00008"></a>00008 <span class="comment"> *</span>
<a name="l00009"></a>00009 <span class="comment"> * $Id: eventhandler.c 5567 2011-01-29 13:18:49Z rousseau $</span>
<a name="l00010"></a>00010 <span class="comment"> */</span>
<a name="l00011"></a>00011 
<a name="l00018"></a>00018 <span class="preprocessor">#include &quot;config.h&quot;</span>
<a name="l00019"></a>00019 <span class="preprocessor">#include &lt;sys/types.h&gt;</span>
<a name="l00020"></a>00020 <span class="preprocessor">#include &lt;sys/stat.h&gt;</span>
<a name="l00021"></a>00021 <span class="preprocessor">#include &lt;errno.h&gt;</span>
<a name="l00022"></a>00022 <span class="preprocessor">#include &lt;fcntl.h&gt;</span>
<a name="l00023"></a>00023 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00024"></a>00024 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00025"></a>00025 <span class="preprocessor">#include &lt;pthread.h&gt;</span>
<a name="l00026"></a>00026 
<a name="l00027"></a>00027 <span class="preprocessor">#include &quot;misc.h&quot;</span>
<a name="l00028"></a>00028 <span class="preprocessor">#include &quot;<a class="code" href="pcscd_8h.html" title="This keeps a list of defines for pcsc-lite.">pcscd.h</a>&quot;</span>
<a name="l00029"></a>00029 <span class="preprocessor">#include &quot;<a class="code" href="debuglog_8h.html" title="This handles debugging.">debuglog.h</a>&quot;</span>
<a name="l00030"></a>00030 <span class="preprocessor">#include &quot;<a class="code" href="readerfactory_8h.html" title="This keeps track of a list of currently available reader structures.">readerfactory.h</a>&quot;</span>
<a name="l00031"></a>00031 <span class="preprocessor">#include &quot;<a class="code" href="eventhandler_8h.html" title="This handles card insertion/removal events, updates ATR, protocol, and status information.">eventhandler.h</a>&quot;</span>
<a name="l00032"></a>00032 <span class="preprocessor">#include &quot;<a class="code" href="dyn__generic_8h.html" title="This abstracts dynamic library loading functions.">dyn_generic.h</a>&quot;</span>
<a name="l00033"></a>00033 <span class="preprocessor">#include &quot;<a class="code" href="sys__generic_8h.html" title="This handles abstract system level calls.">sys_generic.h</a>&quot;</span>
<a name="l00034"></a>00034 <span class="preprocessor">#include &quot;<a class="code" href="ifdwrapper_8h.html" title="This wraps the dynamic ifdhandler functions.">ifdwrapper.h</a>&quot;</span>
<a name="l00035"></a>00035 <span class="preprocessor">#include &quot;<a class="code" href="prothandler_8h.html" title="This handles protocol defaults, PTS, etc.">prothandler.h</a>&quot;</span>
<a name="l00036"></a>00036 <span class="preprocessor">#include &quot;<a class="code" href="strlcpycat_8h.html" title="prototypes of strlcpy()/strlcat() imported from OpenBSD">strlcpycat.h</a>&quot;</span>
<a name="l00037"></a>00037 <span class="preprocessor">#include &quot;utils.h&quot;</span>
<a name="l00038"></a>00038 <span class="preprocessor">#include &quot;<a class="code" href="winscard__svc_8h.html" title="This demarshalls functions over the message queue and keeps track of clients and their handles...">winscard_svc.h</a>&quot;</span>
<a name="l00039"></a>00039 <span class="preprocessor">#include &quot;simclist.h&quot;</span>
<a name="l00040"></a>00040 
<a name="l00041"></a><a class="code" href="eventhandler_8c.html#a337175c6345dfa8f40f0d459c50664b8">00041</a> <span class="keyword">static</span> <a class="code" href="structlist__t.html" title="list object">list_t</a> <a class="code" href="eventhandler_8c.html#a337175c6345dfa8f40f0d459c50664b8" title="list of client file descriptors">ClientsWaitingForEvent</a>;   
<a name="l00042"></a><a class="code" href="eventhandler_8c.html#a70bd81b80fa11826029c70144319f869">00042</a> pthread_mutex_t <a class="code" href="eventhandler_8c.html#a70bd81b80fa11826029c70144319f869" title="lock for the above list">ClientsWaitingForEvent_lock</a>;    
<a name="l00044"></a>00044 <span class="keyword">static</span> <span class="keywordtype">void</span> EHStatusHandlerThread(<a class="code" href="structReaderContext.html">READER_CONTEXT</a> *);
<a name="l00045"></a>00045 
<a name="l00046"></a>00046 LONG EHRegisterClientForEvent(int32_t filedes)
<a name="l00047"></a>00047 {
<a name="l00048"></a>00048     (void)pthread_mutex_lock(&amp;<a class="code" href="eventhandler_8c.html#a70bd81b80fa11826029c70144319f869" title="lock for the above list">ClientsWaitingForEvent_lock</a>);
<a name="l00049"></a>00049 
<a name="l00050"></a>00050     (void)list_append(&amp;ClientsWaitingForEvent, &amp;filedes);
<a name="l00051"></a>00051 
<a name="l00052"></a>00052     (void)pthread_mutex_unlock(&amp;<a class="code" href="eventhandler_8c.html#a70bd81b80fa11826029c70144319f869" title="lock for the above list">ClientsWaitingForEvent_lock</a>);
<a name="l00053"></a>00053 
<a name="l00054"></a>00054     <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>;
<a name="l00055"></a>00055 } <span class="comment">/* EHRegisterClientForEvent */</span>
<a name="l00056"></a>00056 
<a name="l00061"></a><a class="code" href="eventhandler_8h.html#a37ebfbc3287ee2ea69a60e87fead19e4">00061</a> LONG <a class="code" href="eventhandler_8c.html#a37ebfbc3287ee2ea69a60e87fead19e4" title="Try to unregisted a client If no client is found then do not log an error.">EHTryToUnregisterClientForEvent</a>(int32_t filedes)
<a name="l00062"></a>00062 {
<a name="l00063"></a>00063     LONG rv = <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>;
<a name="l00064"></a>00064     <span class="keywordtype">int</span> ret;
<a name="l00065"></a>00065 
<a name="l00066"></a>00066     (void)pthread_mutex_lock(&amp;<a class="code" href="eventhandler_8c.html#a70bd81b80fa11826029c70144319f869" title="lock for the above list">ClientsWaitingForEvent_lock</a>);
<a name="l00067"></a>00067 
<a name="l00068"></a>00068     ret = list_delete(&amp;ClientsWaitingForEvent, &amp;filedes);
<a name="l00069"></a>00069 
<a name="l00070"></a>00070     (void)pthread_mutex_unlock(&amp;<a class="code" href="eventhandler_8c.html#a70bd81b80fa11826029c70144319f869" title="lock for the above list">ClientsWaitingForEvent_lock</a>);
<a name="l00071"></a>00071 
<a name="l00072"></a>00072     <span class="keywordflow">if</span> (ret &lt; 0)
<a name="l00073"></a>00073         rv = <a class="code" href="pcsclite_8h.html#a081a564acaf166f74592906f7dff2d86" title="An internal consistency check failed.">SCARD_F_INTERNAL_ERROR</a>;
<a name="l00074"></a>00074 
<a name="l00075"></a>00075     <span class="keywordflow">return</span> rv;
<a name="l00076"></a>00076 } <span class="comment">/* EHTryToUnregisterClientForEvent */</span>
<a name="l00077"></a>00077 
<a name="l00081"></a><a class="code" href="eventhandler_8h.html#aa1391089c844599baf0cb4c95f8b0f75">00081</a> LONG <a class="code" href="eventhandler_8c.html#aa1391089c844599baf0cb4c95f8b0f75" title="Unregister a client and log an error if the client is not found.">EHUnregisterClientForEvent</a>(int32_t filedes)
<a name="l00082"></a>00082 {
<a name="l00083"></a>00083     LONG rv = <a class="code" href="eventhandler_8c.html#a37ebfbc3287ee2ea69a60e87fead19e4" title="Try to unregisted a client If no client is found then do not log an error.">EHTryToUnregisterClientForEvent</a>(filedes);
<a name="l00084"></a>00084 
<a name="l00085"></a>00085     <span class="keywordflow">if</span> (rv &lt; 0)
<a name="l00086"></a>00086         Log2(PCSC_LOG_ERROR, <span class="stringliteral">&quot;Can&#39;t remove client: %d&quot;</span>, filedes);
<a name="l00087"></a>00087 
<a name="l00088"></a>00088     <span class="keywordflow">return</span> rv;
<a name="l00089"></a>00089 } <span class="comment">/* EHUnregisterClientForEvent */</span>
<a name="l00090"></a>00090 
<a name="l00094"></a><a class="code" href="eventhandler_8h.html#af758bdf0f8f42781f0b24a042fda65c9">00094</a> LONG <a class="code" href="eventhandler_8c.html#af758bdf0f8f42781f0b24a042fda65c9" title="Sends an asynchronous event to any waiting client.">EHSignalEventToClients</a>(<span class="keywordtype">void</span>)
<a name="l00095"></a>00095 {
<a name="l00096"></a>00096     LONG rv = <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>;
<a name="l00097"></a>00097     int32_t filedes;
<a name="l00098"></a>00098 
<a name="l00099"></a>00099     (void)pthread_mutex_lock(&amp;<a class="code" href="eventhandler_8c.html#a70bd81b80fa11826029c70144319f869" title="lock for the above list">ClientsWaitingForEvent_lock</a>);
<a name="l00100"></a>00100 
<a name="l00101"></a>00101     (void)list_iterator_start(&amp;ClientsWaitingForEvent);
<a name="l00102"></a>00102     <span class="keywordflow">while</span> (list_iterator_hasnext(&amp;ClientsWaitingForEvent))
<a name="l00103"></a>00103     {
<a name="l00104"></a>00104         filedes = *(int32_t *)list_iterator_next(&amp;ClientsWaitingForEvent);
<a name="l00105"></a>00105         rv = MSGSignalClient(filedes, <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>);
<a name="l00106"></a>00106     }
<a name="l00107"></a>00107     (void)list_iterator_stop(&amp;ClientsWaitingForEvent);
<a name="l00108"></a>00108 
<a name="l00109"></a>00109     (void)list_clear(&amp;ClientsWaitingForEvent);
<a name="l00110"></a>00110 
<a name="l00111"></a>00111     (void)pthread_mutex_unlock(&amp;<a class="code" href="eventhandler_8c.html#a70bd81b80fa11826029c70144319f869" title="lock for the above list">ClientsWaitingForEvent_lock</a>);
<a name="l00112"></a>00112 
<a name="l00113"></a>00113     <span class="keywordflow">return</span> rv;
<a name="l00114"></a>00114 } <span class="comment">/* EHSignalEventToClients */</span>
<a name="l00115"></a>00115 
<a name="l00116"></a>00116 LONG EHInitializeEventStructures(<span class="keywordtype">void</span>)
<a name="l00117"></a>00117 {
<a name="l00118"></a>00118     (void)list_init(&amp;ClientsWaitingForEvent);
<a name="l00119"></a>00119 
<a name="l00120"></a>00120     <span class="comment">/* request to store copies, and provide the metric function */</span>
<a name="l00121"></a>00121     (void)list_attributes_copy(&amp;ClientsWaitingForEvent, list_meter_int32_t, 1);
<a name="l00122"></a>00122 
<a name="l00123"></a>00123     <span class="comment">/* setting the comparator, so the list can sort, find the min, max etc */</span>
<a name="l00124"></a>00124     (void)list_attributes_comparator(&amp;ClientsWaitingForEvent, list_comparator_int32_t);
<a name="l00125"></a>00125 
<a name="l00126"></a>00126     (void)pthread_mutex_init(&amp;<a class="code" href="eventhandler_8c.html#a70bd81b80fa11826029c70144319f869" title="lock for the above list">ClientsWaitingForEvent_lock</a>, NULL);
<a name="l00127"></a>00127 
<a name="l00128"></a>00128     <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>;
<a name="l00129"></a>00129 }
<a name="l00130"></a>00130 
<a name="l00131"></a>00131 LONG EHDestroyEventHandler(<a class="code" href="structReaderContext.html">READER_CONTEXT</a> * rContext)
<a name="l00132"></a>00132 {
<a name="l00133"></a>00133     <span class="keywordtype">int</span> rv;
<a name="l00134"></a>00134     DWORD dwGetSize;
<a name="l00135"></a>00135     UCHAR ucGetData[1];
<a name="l00136"></a>00136 
<a name="l00137"></a>00137     <span class="keywordflow">if</span> (<span class="charliteral">&#39;\0&#39;</span> == rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a2911a61e0a38e8245a963debf872bfb9" title="reader name">readerName</a>[0])
<a name="l00138"></a>00138     {
<a name="l00139"></a>00139         Log1(PCSC_LOG_INFO, <span class="stringliteral">&quot;Thread already stomped.&quot;</span>);
<a name="l00140"></a>00140         <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>;
<a name="l00141"></a>00141     }
<a name="l00142"></a>00142 
<a name="l00143"></a>00143     <span class="comment">/*</span>
<a name="l00144"></a>00144 <span class="comment">     * Set the thread to 0 to exit thread</span>
<a name="l00145"></a>00145 <span class="comment">     */</span>
<a name="l00146"></a>00146     rContext-&gt;<a class="code" href="structReaderContext.html#a709439b280f5c7366f806423bfb5fdb5" title="Lock Id.">hLockId</a> = 0xFFFF;
<a name="l00147"></a>00147 
<a name="l00148"></a>00148     Log1(PCSC_LOG_INFO, <span class="stringliteral">&quot;Stomping thread.&quot;</span>);
<a name="l00149"></a>00149 
<a name="l00150"></a>00150     <span class="comment">/* kill the &quot;polling&quot; thread */</span>
<a name="l00151"></a>00151     dwGetSize = <span class="keyword">sizeof</span>(ucGetData);
<a name="l00152"></a>00152     rv = <a class="code" href="ifdwrapper_8c.html#af9ec3bb9c077c400c6c0bc4b4e19155f" title="Get&#39;s capabilities in the reader.">IFDGetCapabilities</a>(rContext, <a class="code" href="ifdhandler_8h.html#ab110837652c9e2ad902199148fe6193f" title="the polling thread can be killed">TAG_IFD_POLLING_THREAD_KILLABLE</a>,
<a name="l00153"></a>00153         &amp;dwGetSize, ucGetData);
<a name="l00154"></a>00154 
<a name="l00155"></a>00155 <span class="preprocessor">#ifdef HAVE_PTHREAD_CANCEL</span>
<a name="l00156"></a>00156 <span class="preprocessor"></span>    <span class="keywordflow">if</span> ((<a class="code" href="ifdhandler_8h.html#ae45ef2a236e5e6f15eb6b24a7bdf2d72" title="no error">IFD_SUCCESS</a> == rv) &amp;&amp; (1 == dwGetSize) &amp;&amp; ucGetData[0])
<a name="l00157"></a>00157     {
<a name="l00158"></a>00158         Log1(PCSC_LOG_INFO, <span class="stringliteral">&quot;Killing polling thread&quot;</span>);
<a name="l00159"></a>00159         (void)pthread_cancel(rContext-&gt;<a class="code" href="structReaderContext.html#a718c57847779ba5a7a45a648c86d2aa7" title="Event polling thread.">pthThread</a>);
<a name="l00160"></a>00160     }
<a name="l00161"></a>00161     <span class="keywordflow">else</span>
<a name="l00162"></a>00162 <span class="preprocessor">#endif</span>
<a name="l00163"></a>00163 <span class="preprocessor"></span>    {
<a name="l00164"></a>00164         <span class="comment">/* ask to stop the &quot;polling&quot; thread */</span>
<a name="l00165"></a>00165         RESPONSECODE (*fct)(DWORD) = NULL;
<a name="l00166"></a>00166 
<a name="l00167"></a>00167         dwGetSize = <span class="keyword">sizeof</span>(fct);
<a name="l00168"></a>00168         rv = <a class="code" href="ifdwrapper_8c.html#af9ec3bb9c077c400c6c0bc4b4e19155f" title="Get&#39;s capabilities in the reader.">IFDGetCapabilities</a>(rContext, <a class="code" href="ifdhandler_8h.html#a2b2dca31df9201bebdf61697cec1df2f" title="method used to stop the polling thread (instead of just pthread_kill())">TAG_IFD_STOP_POLLING_THREAD</a>,
<a name="l00169"></a>00169             &amp;dwGetSize, (PUCHAR)&amp;fct);
<a name="l00170"></a>00170 
<a name="l00171"></a>00171         <span class="keywordflow">if</span> ((<a class="code" href="ifdhandler_8h.html#ae45ef2a236e5e6f15eb6b24a7bdf2d72" title="no error">IFD_SUCCESS</a> == rv) &amp;&amp; (dwGetSize == <span class="keyword">sizeof</span>(fct)))
<a name="l00172"></a>00172         {
<a name="l00173"></a>00173             Log1(PCSC_LOG_INFO, <span class="stringliteral">&quot;Request stoping of polling thread&quot;</span>);
<a name="l00174"></a>00174             fct(rContext-&gt;<a class="code" href="structReaderContext.html#a83d6e2127b4cc5e01f2012608487d31a" title="Current Reader Slot.">slot</a>);
<a name="l00175"></a>00175         }
<a name="l00176"></a>00176         <span class="keywordflow">else</span>
<a name="l00177"></a>00177             Log1(PCSC_LOG_INFO, <span class="stringliteral">&quot;Waiting polling thread&quot;</span>);
<a name="l00178"></a>00178     }
<a name="l00179"></a>00179 
<a name="l00180"></a>00180     <span class="comment">/* wait for the thread to finish */</span>
<a name="l00181"></a>00181     rv = pthread_join(rContext-&gt;<a class="code" href="structReaderContext.html#a718c57847779ba5a7a45a648c86d2aa7" title="Event polling thread.">pthThread</a>, NULL);
<a name="l00182"></a>00182     <span class="keywordflow">if</span> (rv)
<a name="l00183"></a>00183         Log2(PCSC_LOG_ERROR, <span class="stringliteral">&quot;pthread_join failed: %s&quot;</span>, strerror(rv));
<a name="l00184"></a>00184 
<a name="l00185"></a>00185     <span class="comment">/* Zero the thread */</span>
<a name="l00186"></a>00186     rContext-&gt;<a class="code" href="structReaderContext.html#a718c57847779ba5a7a45a648c86d2aa7" title="Event polling thread.">pthThread</a> = 0;
<a name="l00187"></a>00187 
<a name="l00188"></a>00188     Log1(PCSC_LOG_INFO, <span class="stringliteral">&quot;Thread stomped.&quot;</span>);
<a name="l00189"></a>00189 
<a name="l00190"></a>00190     <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>;
<a name="l00191"></a>00191 }
<a name="l00192"></a>00192 
<a name="l00193"></a>00193 LONG EHSpawnEventHandler(<a class="code" href="structReaderContext.html">READER_CONTEXT</a> * rContext)
<a name="l00194"></a>00194 {
<a name="l00195"></a>00195     LONG rv;
<a name="l00196"></a>00196     DWORD dwStatus = 0;
<a name="l00197"></a>00197 
<a name="l00198"></a>00198     rv = <a class="code" href="ifdwrapper_8c.html#a53f77b110a7765f683e0629aad54772e" title="Provide statistical information about the IFD and ICC including insertions, atr, powering status/etc...">IFDStatusICC</a>(rContext, &amp;dwStatus);
<a name="l00199"></a>00199     <span class="keywordflow">if</span> (rv != <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>)
<a name="l00200"></a>00200     {
<a name="l00201"></a>00201         Log2(PCSC_LOG_ERROR, <span class="stringliteral">&quot;Initial Check Failed on %s&quot;</span>,
<a name="l00202"></a>00202             rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a2911a61e0a38e8245a963debf872bfb9" title="reader name">readerName</a>);
<a name="l00203"></a>00203         <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a27abb2c7397e0b23939cf216111d4e9a" title="An internal error has been detected, but the source is unknown.">SCARD_F_UNKNOWN_ERROR</a>;
<a name="l00204"></a>00204     }
<a name="l00205"></a>00205 
<a name="l00206"></a>00206     rv = ThreadCreate(&amp;rContext-&gt;<a class="code" href="structReaderContext.html#a718c57847779ba5a7a45a648c86d2aa7" title="Event polling thread.">pthThread</a>, 0,
<a name="l00207"></a>00207         (PCSCLITE_THREAD_FUNCTION( ))EHStatusHandlerThread, (LPVOID) rContext);
<a name="l00208"></a>00208     <span class="keywordflow">if</span> (rv)
<a name="l00209"></a>00209     {
<a name="l00210"></a>00210         Log2(PCSC_LOG_ERROR, <span class="stringliteral">&quot;ThreadCreate failed: %s&quot;</span>, strerror(rv));
<a name="l00211"></a>00211         <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a81694393a1494d5091145734a2f0b36c" title="Not enough memory available to complete this command.">SCARD_E_NO_MEMORY</a>;
<a name="l00212"></a>00212     }
<a name="l00213"></a>00213     <span class="keywordflow">else</span>
<a name="l00214"></a>00214         <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>;
<a name="l00215"></a>00215 }
<a name="l00216"></a>00216 
<a name="l00217"></a>00217 <span class="keyword">static</span> <span class="keywordtype">void</span> EHStatusHandlerThread(<a class="code" href="structReaderContext.html">READER_CONTEXT</a> * rContext)
<a name="l00218"></a>00218 {
<a name="l00219"></a>00219     LONG rv;
<a name="l00220"></a>00220     <span class="keyword">const</span> <span class="keywordtype">char</span> *readerName;
<a name="l00221"></a>00221     DWORD dwStatus;
<a name="l00222"></a>00222     uint32_t readerState;
<a name="l00223"></a>00223     int32_t readerSharing;
<a name="l00224"></a>00224     DWORD dwCurrentState;
<a name="l00225"></a>00225     DWORD dwAtrLen;
<a name="l00226"></a>00226 
<a name="l00227"></a>00227     <span class="comment">/*</span>
<a name="l00228"></a>00228 <span class="comment">     * Zero out everything</span>
<a name="l00229"></a>00229 <span class="comment">     */</span>
<a name="l00230"></a>00230     dwStatus = 0;
<a name="l00231"></a>00231 
<a name="l00232"></a>00232     readerName = rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a2911a61e0a38e8245a963debf872bfb9" title="reader name">readerName</a>;
<a name="l00233"></a>00233 
<a name="l00234"></a>00234     rv = <a class="code" href="ifdwrapper_8c.html#a53f77b110a7765f683e0629aad54772e" title="Provide statistical information about the IFD and ICC including insertions, atr, powering status/etc...">IFDStatusICC</a>(rContext, &amp;dwStatus);
<a name="l00235"></a>00235 
<a name="l00236"></a>00236     <span class="keywordflow">if</span> ((<a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a> == rv) &amp;&amp; (dwStatus &amp; <a class="code" href="pcsclite_8h.html#a4d99b1aacfcda4e1a36198f5668ae520" title="Card is present.">SCARD_PRESENT</a>))
<a name="l00237"></a>00237     {
<a name="l00238"></a>00238 <span class="preprocessor">#ifdef DISABLE_AUTO_POWER_ON</span>
<a name="l00239"></a>00239 <span class="preprocessor"></span>        rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a> = 0;
<a name="l00240"></a>00240         rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a8218d71b0c62a617790e645fca310913" title="SCARD_PROTOCOL_* value.">cardProtocol</a> = <a class="code" href="pcsclite_8h.html#a3b42ca2bc25ec7ff3211a25110b4fc60" title="protocol not set">SCARD_PROTOCOL_UNDEFINED</a>;
<a name="l00241"></a>00241         readerState = <a class="code" href="pcsclite_8h.html#a4d99b1aacfcda4e1a36198f5668ae520" title="Card is present.">SCARD_PRESENT</a>;
<a name="l00242"></a>00242         Log1(PCSC_LOG_INFO, <span class="stringliteral">&quot;Skip card power on&quot;</span>);
<a name="l00243"></a>00243 <span class="preprocessor">#else</span>
<a name="l00244"></a>00244 <span class="preprocessor"></span>        dwAtrLen = <span class="keyword">sizeof</span>(rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a865f4e34f374b7b24b3a56008f92676f" title="ATR.">cardAtr</a>);
<a name="l00245"></a>00245         rv = <a class="code" href="ifdwrapper_8c.html#a5a891c2a91b2cce4f950fe6d466ea9a2" title="Power up/down or reset&#39;s an ICC located in the IFD.">IFDPowerICC</a>(rContext, <a class="code" href="ifdhandler_8h.html#a103a0059929b2de2a5426ed5dc5fa307" title="power up the card">IFD_POWER_UP</a>,
<a name="l00246"></a>00246             rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a865f4e34f374b7b24b3a56008f92676f" title="ATR.">cardAtr</a>, &amp;dwAtrLen);
<a name="l00247"></a>00247         rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a> = dwAtrLen;
<a name="l00248"></a>00248 
<a name="l00249"></a>00249         <span class="comment">/* the protocol is unset after a power on */</span>
<a name="l00250"></a>00250         rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a8218d71b0c62a617790e645fca310913" title="SCARD_PROTOCOL_* value.">cardProtocol</a> = <a class="code" href="pcsclite_8h.html#a3b42ca2bc25ec7ff3211a25110b4fc60" title="protocol not set">SCARD_PROTOCOL_UNDEFINED</a>;
<a name="l00251"></a>00251 
<a name="l00252"></a>00252         <span class="keywordflow">if</span> (rv == <a class="code" href="ifdhandler_8h.html#ae45ef2a236e5e6f15eb6b24a7bdf2d72" title="no error">IFD_SUCCESS</a>)
<a name="l00253"></a>00253         {
<a name="l00254"></a>00254             readerState = SCARD_PRESENT | <a class="code" href="pcsclite_8h.html#a73c52c7df905c748f00b22c88827d65a" title="Card is powered.">SCARD_POWERED</a> | <a class="code" href="pcsclite_8h.html#af261075b3c31cd33def0d5af9eaed921" title="Ready for PTS.">SCARD_NEGOTIABLE</a>;
<a name="l00255"></a>00255             rContext-&gt;<a class="code" href="structReaderContext.html#af19f336bd37ddc379cbd32106acb82e7" title="auto power off state">powerState</a> = <a class="code" href="pcscd_8h.html#adc29c2ff13d900c2f185ee95427fb06ca7be8439cb9f5abff2a92f7949e52aa4a" title="powered">POWER_STATE_POWERED</a>;
<a name="l00256"></a>00256             Log1(PCSC_LOG_DEBUG, <span class="stringliteral">&quot;powerState: POWER_STATE_POWERED&quot;</span>);
<a name="l00257"></a>00257 
<a name="l00258"></a>00258             <span class="keywordflow">if</span> (rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a> &gt; 0)
<a name="l00259"></a>00259             {
<a name="l00260"></a>00260                 LogXxd(PCSC_LOG_INFO, <span class="stringliteral">&quot;Card ATR: &quot;</span>,
<a name="l00261"></a>00261                     rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a865f4e34f374b7b24b3a56008f92676f" title="ATR.">cardAtr</a>,
<a name="l00262"></a>00262                     rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a>);
<a name="l00263"></a>00263             }
<a name="l00264"></a>00264             <span class="keywordflow">else</span>
<a name="l00265"></a>00265                 Log1(PCSC_LOG_INFO, <span class="stringliteral">&quot;Card ATR: (NULL)&quot;</span>);
<a name="l00266"></a>00266         }
<a name="l00267"></a>00267         <span class="keywordflow">else</span>
<a name="l00268"></a>00268         {
<a name="l00269"></a>00269             readerState = SCARD_PRESENT | <a class="code" href="pcsclite_8h.html#a0feed8b3b5ce43d1411359a9b0a3d78c" title="Card not powered.">SCARD_SWALLOWED</a>;
<a name="l00270"></a>00270             rContext-&gt;<a class="code" href="structReaderContext.html#af19f336bd37ddc379cbd32106acb82e7" title="auto power off state">powerState</a> = <a class="code" href="pcscd_8h.html#adc29c2ff13d900c2f185ee95427fb06ca364e061ee6f8ca8f305b09118078c0f4" title="auto power off">POWER_STATE_UNPOWERED</a>;
<a name="l00271"></a>00271             Log1(PCSC_LOG_DEBUG, <span class="stringliteral">&quot;powerState: POWER_STATE_UNPOWERED&quot;</span>);
<a name="l00272"></a>00272             Log3(PCSC_LOG_ERROR, <span class="stringliteral">&quot;Error powering up card: %d 0x%04X&quot;</span>, rv, rv);
<a name="l00273"></a>00273         }
<a name="l00274"></a>00274 <span class="preprocessor">#endif</span>
<a name="l00275"></a>00275 <span class="preprocessor"></span>
<a name="l00276"></a>00276         dwCurrentState = <a class="code" href="pcsclite_8h.html#a4d99b1aacfcda4e1a36198f5668ae520" title="Card is present.">SCARD_PRESENT</a>;
<a name="l00277"></a>00277     }
<a name="l00278"></a>00278     <span class="keywordflow">else</span>
<a name="l00279"></a>00279     {
<a name="l00280"></a>00280         readerState = <a class="code" href="pcsclite_8h.html#a8108c39f71de4ea806b0a411eb9906ab" title="Card is absent.">SCARD_ABSENT</a>;
<a name="l00281"></a>00281         rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a> = 0;
<a name="l00282"></a>00282         rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a8218d71b0c62a617790e645fca310913" title="SCARD_PROTOCOL_* value.">cardProtocol</a> = <a class="code" href="pcsclite_8h.html#a3b42ca2bc25ec7ff3211a25110b4fc60" title="protocol not set">SCARD_PROTOCOL_UNDEFINED</a>;
<a name="l00283"></a>00283 
<a name="l00284"></a>00284         dwCurrentState = <a class="code" href="pcsclite_8h.html#a8108c39f71de4ea806b0a411eb9906ab" title="Card is absent.">SCARD_ABSENT</a>;
<a name="l00285"></a>00285     }
<a name="l00286"></a>00286 
<a name="l00287"></a>00287     <span class="comment">/*</span>
<a name="l00288"></a>00288 <span class="comment">     * Set all the public attributes to this reader</span>
<a name="l00289"></a>00289 <span class="comment">     */</span>
<a name="l00290"></a>00290     rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#ae0bfa9f9998463192d8c18cf86915082" title="SCARD_* bit field.">readerState</a> = readerState;
<a name="l00291"></a>00291     rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a3466b5e09f39d5857bf6a158a5bb7d30" title="PCSCLITE_SHARING_* sharing status.">readerSharing</a> = readerSharing = rContext-&gt;<a class="code" href="structReaderContext.html#ac0153acdf44acec67845e32db4733ae7" title="Number of open contexts.">contexts</a>;
<a name="l00292"></a>00292 
<a name="l00293"></a>00293     (void)<a class="code" href="eventhandler_8c.html#af758bdf0f8f42781f0b24a042fda65c9" title="Sends an asynchronous event to any waiting client.">EHSignalEventToClients</a>();
<a name="l00294"></a>00294 
<a name="l00295"></a>00295     <span class="keywordflow">while</span> (1)
<a name="l00296"></a>00296     {
<a name="l00297"></a>00297         dwStatus = 0;
<a name="l00298"></a>00298 
<a name="l00299"></a>00299         rv = <a class="code" href="ifdwrapper_8c.html#a53f77b110a7765f683e0629aad54772e" title="Provide statistical information about the IFD and ICC including insertions, atr, powering status/etc...">IFDStatusICC</a>(rContext, &amp;dwStatus);
<a name="l00300"></a>00300 
<a name="l00301"></a>00301         <span class="keywordflow">if</span> (rv != <a class="code" href="pcsclite_8h.html#a4a3c56f69570971d366642a563b34a75" title="error codes from http://msdn.microsoft.com/en-us/library/aa924526.aspx">SCARD_S_SUCCESS</a>)
<a name="l00302"></a>00302         {
<a name="l00303"></a>00303             Log2(PCSC_LOG_ERROR, <span class="stringliteral">&quot;Error communicating to: %s&quot;</span>, readerName);
<a name="l00304"></a>00304 
<a name="l00305"></a>00305             <span class="comment">/*</span>
<a name="l00306"></a>00306 <span class="comment">             * Set error status on this reader while errors occur</span>
<a name="l00307"></a>00307 <span class="comment">             */</span>
<a name="l00308"></a>00308             rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#ae0bfa9f9998463192d8c18cf86915082" title="SCARD_* bit field.">readerState</a> = <a class="code" href="pcsclite_8h.html#a91be8317d47fab4b3eac201850fa917f" title="Unknown state.">SCARD_UNKNOWN</a>;
<a name="l00309"></a>00309             rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a> = 0;
<a name="l00310"></a>00310             rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a8218d71b0c62a617790e645fca310913" title="SCARD_PROTOCOL_* value.">cardProtocol</a> = <a class="code" href="pcsclite_8h.html#a3b42ca2bc25ec7ff3211a25110b4fc60" title="protocol not set">SCARD_PROTOCOL_UNDEFINED</a>;
<a name="l00311"></a>00311 
<a name="l00312"></a>00312             dwCurrentState = <a class="code" href="pcsclite_8h.html#a91be8317d47fab4b3eac201850fa917f" title="Unknown state.">SCARD_UNKNOWN</a>;
<a name="l00313"></a>00313 
<a name="l00314"></a>00314             (void)<a class="code" href="eventhandler_8c.html#af758bdf0f8f42781f0b24a042fda65c9" title="Sends an asynchronous event to any waiting client.">EHSignalEventToClients</a>();
<a name="l00315"></a>00315         }
<a name="l00316"></a>00316 
<a name="l00317"></a>00317         <span class="keywordflow">if</span> (dwStatus &amp; <a class="code" href="pcsclite_8h.html#a8108c39f71de4ea806b0a411eb9906ab" title="Card is absent.">SCARD_ABSENT</a>)
<a name="l00318"></a>00318         {
<a name="l00319"></a>00319             <span class="keywordflow">if</span> (dwCurrentState == SCARD_PRESENT ||
<a name="l00320"></a>00320                 dwCurrentState == <a class="code" href="pcsclite_8h.html#a91be8317d47fab4b3eac201850fa917f" title="Unknown state.">SCARD_UNKNOWN</a>)
<a name="l00321"></a>00321             {
<a name="l00322"></a>00322                 <span class="comment">/*</span>
<a name="l00323"></a>00323 <span class="comment">                 * Change the status structure</span>
<a name="l00324"></a>00324 <span class="comment">                 */</span>
<a name="l00325"></a>00325                 Log2(PCSC_LOG_INFO, <span class="stringliteral">&quot;Card Removed From %s&quot;</span>, readerName);
<a name="l00326"></a>00326                 <span class="comment">/*</span>
<a name="l00327"></a>00327 <span class="comment">                 * Notify the card has been removed</span>
<a name="l00328"></a>00328 <span class="comment">                 */</span>
<a name="l00329"></a>00329                 (void)RFSetReaderEventState(rContext, <a class="code" href="pcscd_8h.html#ac3addaa035656d9240290695e46d2b87" title="Card was removed.">SCARD_REMOVED</a>);
<a name="l00330"></a>00330 
<a name="l00331"></a>00331                 rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a> = 0;
<a name="l00332"></a>00332                 rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a8218d71b0c62a617790e645fca310913" title="SCARD_PROTOCOL_* value.">cardProtocol</a> = <a class="code" href="pcsclite_8h.html#a3b42ca2bc25ec7ff3211a25110b4fc60" title="protocol not set">SCARD_PROTOCOL_UNDEFINED</a>;
<a name="l00333"></a>00333                 rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#ae0bfa9f9998463192d8c18cf86915082" title="SCARD_* bit field.">readerState</a> = <a class="code" href="pcsclite_8h.html#a8108c39f71de4ea806b0a411eb9906ab" title="Card is absent.">SCARD_ABSENT</a>;
<a name="l00334"></a>00334                 dwCurrentState = <a class="code" href="pcsclite_8h.html#a8108c39f71de4ea806b0a411eb9906ab" title="Card is absent.">SCARD_ABSENT</a>;
<a name="l00335"></a>00335 
<a name="l00336"></a>00336                 rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a97623f9f724a2a86db3e755badd0cf21" title="number of card events">eventCounter</a>++;
<a name="l00337"></a>00337 
<a name="l00338"></a>00338                 (void)<a class="code" href="eventhandler_8c.html#af758bdf0f8f42781f0b24a042fda65c9" title="Sends an asynchronous event to any waiting client.">EHSignalEventToClients</a>();
<a name="l00339"></a>00339             }
<a name="l00340"></a>00340 
<a name="l00341"></a>00341         }
<a name="l00342"></a>00342         <span class="keywordflow">else</span> <span class="keywordflow">if</span> (dwStatus &amp; SCARD_PRESENT)
<a name="l00343"></a>00343         {
<a name="l00344"></a>00344             <span class="keywordflow">if</span> (dwCurrentState == SCARD_ABSENT ||
<a name="l00345"></a>00345                 dwCurrentState == <a class="code" href="pcsclite_8h.html#a91be8317d47fab4b3eac201850fa917f" title="Unknown state.">SCARD_UNKNOWN</a>)
<a name="l00346"></a>00346             {
<a name="l00347"></a>00347 <span class="preprocessor">#ifdef DISABLE_AUTO_POWER_ON</span>
<a name="l00348"></a>00348 <span class="preprocessor"></span>                rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a> = 0;
<a name="l00349"></a>00349                 rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a8218d71b0c62a617790e645fca310913" title="SCARD_PROTOCOL_* value.">cardProtocol</a> = <a class="code" href="pcsclite_8h.html#a3b42ca2bc25ec7ff3211a25110b4fc60" title="protocol not set">SCARD_PROTOCOL_UNDEFINED</a>;
<a name="l00350"></a>00350                 rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#ae0bfa9f9998463192d8c18cf86915082" title="SCARD_* bit field.">readerState</a> = <a class="code" href="pcsclite_8h.html#a4d99b1aacfcda4e1a36198f5668ae520" title="Card is present.">SCARD_PRESENT</a>;
<a name="l00351"></a>00351                 rContext-&gt;<a class="code" href="structReaderContext.html#af19f336bd37ddc379cbd32106acb82e7" title="auto power off state">powerState</a> = <a class="code" href="pcscd_8h.html#adc29c2ff13d900c2f185ee95427fb06ca364e061ee6f8ca8f305b09118078c0f4" title="auto power off">POWER_STATE_UNPOWERED</a>;
<a name="l00352"></a>00352                 Log1(PCSC_LOG_DEBUG, <span class="stringliteral">&quot;powerState: POWER_STATE_UNPOWERED&quot;</span>);
<a name="l00353"></a>00353                 readerState = <a class="code" href="pcsclite_8h.html#a4d99b1aacfcda4e1a36198f5668ae520" title="Card is present.">SCARD_PRESENT</a>;
<a name="l00354"></a>00354                 rv = <a class="code" href="ifdhandler_8h.html#ae45ef2a236e5e6f15eb6b24a7bdf2d72" title="no error">IFD_SUCCESS</a>;
<a name="l00355"></a>00355                 Log1(PCSC_LOG_INFO, <span class="stringliteral">&quot;Skip card power on&quot;</span>);
<a name="l00356"></a>00356 <span class="preprocessor">#else</span>
<a name="l00357"></a>00357 <span class="preprocessor"></span>                <span class="comment">/*</span>
<a name="l00358"></a>00358 <span class="comment">                 * Power and reset the card</span>
<a name="l00359"></a>00359 <span class="comment">                 */</span>
<a name="l00360"></a>00360                 dwAtrLen = <span class="keyword">sizeof</span>(rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a865f4e34f374b7b24b3a56008f92676f" title="ATR.">cardAtr</a>);
<a name="l00361"></a>00361                 rv = <a class="code" href="ifdwrapper_8c.html#a5a891c2a91b2cce4f950fe6d466ea9a2" title="Power up/down or reset&#39;s an ICC located in the IFD.">IFDPowerICC</a>(rContext, <a class="code" href="ifdhandler_8h.html#a103a0059929b2de2a5426ed5dc5fa307" title="power up the card">IFD_POWER_UP</a>,
<a name="l00362"></a>00362                     rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a865f4e34f374b7b24b3a56008f92676f" title="ATR.">cardAtr</a>, &amp;dwAtrLen);
<a name="l00363"></a>00363                 rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a> = dwAtrLen;
<a name="l00364"></a>00364 
<a name="l00365"></a>00365                 <span class="comment">/* the protocol is unset after a power on */</span>
<a name="l00366"></a>00366                 rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a8218d71b0c62a617790e645fca310913" title="SCARD_PROTOCOL_* value.">cardProtocol</a> = <a class="code" href="pcsclite_8h.html#a3b42ca2bc25ec7ff3211a25110b4fc60" title="protocol not set">SCARD_PROTOCOL_UNDEFINED</a>;
<a name="l00367"></a>00367 
<a name="l00368"></a>00368                 <span class="keywordflow">if</span> (rv == <a class="code" href="ifdhandler_8h.html#ae45ef2a236e5e6f15eb6b24a7bdf2d72" title="no error">IFD_SUCCESS</a>)
<a name="l00369"></a>00369                 {
<a name="l00370"></a>00370                     rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#ae0bfa9f9998463192d8c18cf86915082" title="SCARD_* bit field.">readerState</a> = SCARD_PRESENT | <a class="code" href="pcsclite_8h.html#a73c52c7df905c748f00b22c88827d65a" title="Card is powered.">SCARD_POWERED</a> | <a class="code" href="pcsclite_8h.html#af261075b3c31cd33def0d5af9eaed921" title="Ready for PTS.">SCARD_NEGOTIABLE</a>;
<a name="l00371"></a>00371                     rContext-&gt;<a class="code" href="structReaderContext.html#af19f336bd37ddc379cbd32106acb82e7" title="auto power off state">powerState</a> = <a class="code" href="pcscd_8h.html#adc29c2ff13d900c2f185ee95427fb06ca7be8439cb9f5abff2a92f7949e52aa4a" title="powered">POWER_STATE_POWERED</a>;
<a name="l00372"></a>00372                     Log1(PCSC_LOG_DEBUG, <span class="stringliteral">&quot;powerState: POWER_STATE_POWERED&quot;</span>);
<a name="l00373"></a>00373                 }
<a name="l00374"></a>00374                 <span class="keywordflow">else</span>
<a name="l00375"></a>00375                 {
<a name="l00376"></a>00376                     rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#ae0bfa9f9998463192d8c18cf86915082" title="SCARD_* bit field.">readerState</a> = SCARD_PRESENT | <a class="code" href="pcsclite_8h.html#a0feed8b3b5ce43d1411359a9b0a3d78c" title="Card not powered.">SCARD_SWALLOWED</a>;
<a name="l00377"></a>00377                     rContext-&gt;<a class="code" href="structReaderContext.html#af19f336bd37ddc379cbd32106acb82e7" title="auto power off state">powerState</a> = <a class="code" href="pcscd_8h.html#adc29c2ff13d900c2f185ee95427fb06ca364e061ee6f8ca8f305b09118078c0f4" title="auto power off">POWER_STATE_UNPOWERED</a>;
<a name="l00378"></a>00378                     Log1(PCSC_LOG_DEBUG, <span class="stringliteral">&quot;powerState: POWER_STATE_UNPOWERED&quot;</span>);
<a name="l00379"></a>00379                     rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a> = 0;
<a name="l00380"></a>00380                 }
<a name="l00381"></a>00381 <span class="preprocessor">#endif</span>
<a name="l00382"></a>00382 <span class="preprocessor"></span>
<a name="l00383"></a>00383                 dwCurrentState = <a class="code" href="pcsclite_8h.html#a4d99b1aacfcda4e1a36198f5668ae520" title="Card is present.">SCARD_PRESENT</a>;
<a name="l00384"></a>00384 
<a name="l00385"></a>00385                 rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a97623f9f724a2a86db3e755badd0cf21" title="number of card events">eventCounter</a>++;
<a name="l00386"></a>00386 
<a name="l00387"></a>00387                 Log2(PCSC_LOG_INFO, <span class="stringliteral">&quot;Card inserted into %s&quot;</span>, readerName);
<a name="l00388"></a>00388 
<a name="l00389"></a>00389                 (void)<a class="code" href="eventhandler_8c.html#af758bdf0f8f42781f0b24a042fda65c9" title="Sends an asynchronous event to any waiting client.">EHSignalEventToClients</a>();
<a name="l00390"></a>00390 
<a name="l00391"></a>00391                 <span class="keywordflow">if</span> (rv == <a class="code" href="ifdhandler_8h.html#ae45ef2a236e5e6f15eb6b24a7bdf2d72" title="no error">IFD_SUCCESS</a>)
<a name="l00392"></a>00392                 {
<a name="l00393"></a>00393                     <span class="keywordflow">if</span> (rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a> &gt; 0)
<a name="l00394"></a>00394                     {
<a name="l00395"></a>00395                         LogXxd(PCSC_LOG_INFO, <span class="stringliteral">&quot;Card ATR: &quot;</span>,
<a name="l00396"></a>00396                             rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a865f4e34f374b7b24b3a56008f92676f" title="ATR.">cardAtr</a>,
<a name="l00397"></a>00397                             rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a>);
<a name="l00398"></a>00398                     }
<a name="l00399"></a>00399                     <span class="keywordflow">else</span>
<a name="l00400"></a>00400                         Log1(PCSC_LOG_INFO, <span class="stringliteral">&quot;Card ATR: (NULL)&quot;</span>);
<a name="l00401"></a>00401                 }
<a name="l00402"></a>00402                 <span class="keywordflow">else</span>
<a name="l00403"></a>00403                     Log1(PCSC_LOG_ERROR,<span class="stringliteral">&quot;Error powering up card.&quot;</span>);
<a name="l00404"></a>00404             }
<a name="l00405"></a>00405         }
<a name="l00406"></a>00406 
<a name="l00407"></a>00407         <span class="comment">/*</span>
<a name="l00408"></a>00408 <span class="comment">         * Sharing may change w/o an event pass it on</span>
<a name="l00409"></a>00409 <span class="comment">         */</span>
<a name="l00410"></a>00410         <span class="keywordflow">if</span> (readerSharing != rContext-&gt;<a class="code" href="structReaderContext.html#ac0153acdf44acec67845e32db4733ae7" title="Number of open contexts.">contexts</a>)
<a name="l00411"></a>00411         {
<a name="l00412"></a>00412             readerSharing = rContext-&gt;<a class="code" href="structReaderContext.html#ac0153acdf44acec67845e32db4733ae7" title="Number of open contexts.">contexts</a>;
<a name="l00413"></a>00413             rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a3466b5e09f39d5857bf6a158a5bb7d30" title="PCSCLITE_SHARING_* sharing status.">readerSharing</a> = readerSharing;
<a name="l00414"></a>00414             (void)<a class="code" href="eventhandler_8c.html#af758bdf0f8f42781f0b24a042fda65c9" title="Sends an asynchronous event to any waiting client.">EHSignalEventToClients</a>();
<a name="l00415"></a>00415         }
<a name="l00416"></a>00416 
<a name="l00417"></a>00417         <span class="keywordflow">if</span> (rContext-&gt;<a class="code" href="structReaderContext.html#a613ccbba508d3812b5a5ffe174ad7fe2" title="Card Event sync.">pthCardEvent</a>)
<a name="l00418"></a>00418         {
<a name="l00419"></a>00419             <span class="keywordtype">int</span> ret;
<a name="l00420"></a>00420             <span class="keywordtype">int</span> timeout;
<a name="l00421"></a>00421 
<a name="l00422"></a>00422 <span class="preprocessor">#ifndef DISABLE_ON_DEMAND_POWER_ON</span>
<a name="l00423"></a>00423 <span class="preprocessor"></span>            <span class="keywordflow">if</span> (<a class="code" href="pcscd_8h.html#adc29c2ff13d900c2f185ee95427fb06ca7be8439cb9f5abff2a92f7949e52aa4a" title="powered">POWER_STATE_POWERED</a> == rContext-&gt;<a class="code" href="structReaderContext.html#af19f336bd37ddc379cbd32106acb82e7" title="auto power off state">powerState</a>)
<a name="l00424"></a>00424                 <span class="comment">/* The card is powered but not yet used */</span>
<a name="l00425"></a>00425                 timeout = <a class="code" href="pcscd_8h.html#a470bcb4ab0dad6b913bd9e8772b21715" title="time to wait before powering down an unused card">PCSCLITE_POWER_OFF_GRACE_PERIOD</a>;
<a name="l00426"></a>00426             <span class="keywordflow">else</span>
<a name="l00427"></a>00427                 <span class="comment">/* The card is already in use or not used at all */</span>
<a name="l00428"></a>00428 <span class="preprocessor">#endif</span>
<a name="l00429"></a>00429 <span class="preprocessor"></span>                timeout = <a class="code" href="pcscd_8h.html#a21483070e9bf58f30b6e646c245dde48" title="normal timeout for pthCardEvent driver function when no card or card in use">PCSCLITE_STATUS_EVENT_TIMEOUT</a>;
<a name="l00430"></a>00430 
<a name="l00431"></a>00431             ret = rContext-&gt;<a class="code" href="structReaderContext.html#a613ccbba508d3812b5a5ffe174ad7fe2" title="Card Event sync.">pthCardEvent</a>(rContext-&gt;<a class="code" href="structReaderContext.html#a83d6e2127b4cc5e01f2012608487d31a" title="Current Reader Slot.">slot</a>, timeout);
<a name="l00432"></a>00432             <span class="keywordflow">if</span> (<a class="code" href="ifdhandler_8h.html#ae45ef2a236e5e6f15eb6b24a7bdf2d72" title="no error">IFD_SUCCESS</a> != ret)
<a name="l00433"></a>00433                 (void)<a class="code" href="sys__generic_8h.html#a910a2c6f5b5a4eadf3213bdfdd0b9bb1" title="Makes the current process sleep for some microseconds.">SYS_USleep</a>(<a class="code" href="pcscd_8h.html#a3a41215bb0b34269bc221fad8470262b" title="Status polling rate.">PCSCLITE_STATUS_POLL_RATE</a>);
<a name="l00434"></a>00434         }
<a name="l00435"></a>00435         <span class="keywordflow">else</span>
<a name="l00436"></a>00436             (<span class="keywordtype">void</span>)<a class="code" href="sys__generic_8h.html#a910a2c6f5b5a4eadf3213bdfdd0b9bb1" title="Makes the current process sleep for some microseconds.">SYS_USleep</a>(<a class="code" href="pcscd_8h.html#a3a41215bb0b34269bc221fad8470262b" title="Status polling rate.">PCSCLITE_STATUS_POLL_RATE</a>);
<a name="l00437"></a>00437 
<a name="l00438"></a>00438 <span class="preprocessor">#ifndef DISABLE_ON_DEMAND_POWER_ON</span>
<a name="l00439"></a>00439 <span class="preprocessor"></span>        <span class="comment">/* the card is powered but not used */</span>
<a name="l00440"></a>00440         (void)pthread_mutex_lock(&amp;rContext-&gt;<a class="code" href="structReaderContext.html#a48cbdd28a24fa823fdb1c079326104c2" title="powerState mutex">powerState_lock</a>);
<a name="l00441"></a>00441         <span class="keywordflow">if</span> (<a class="code" href="pcscd_8h.html#adc29c2ff13d900c2f185ee95427fb06ca7be8439cb9f5abff2a92f7949e52aa4a" title="powered">POWER_STATE_POWERED</a> == rContext-&gt;<a class="code" href="structReaderContext.html#af19f336bd37ddc379cbd32106acb82e7" title="auto power off state">powerState</a>)
<a name="l00442"></a>00442         {
<a name="l00443"></a>00443             <span class="comment">/* power down */</span>
<a name="l00444"></a>00444             <a class="code" href="ifdwrapper_8c.html#a5a891c2a91b2cce4f950fe6d466ea9a2" title="Power up/down or reset&#39;s an ICC located in the IFD.">IFDPowerICC</a>(rContext, <a class="code" href="ifdhandler_8h.html#a57f2b27f291e616cfca666f7b91781c3" title="power down the card">IFD_POWER_DOWN</a>, NULL, NULL);
<a name="l00445"></a>00445             rContext-&gt;<a class="code" href="structReaderContext.html#af19f336bd37ddc379cbd32106acb82e7" title="auto power off state">powerState</a> = <a class="code" href="pcscd_8h.html#adc29c2ff13d900c2f185ee95427fb06ca364e061ee6f8ca8f305b09118078c0f4" title="auto power off">POWER_STATE_UNPOWERED</a>;
<a name="l00446"></a>00446             Log1(PCSC_LOG_DEBUG, <span class="stringliteral">&quot;powerState: POWER_STATE_UNPOWERED&quot;</span>);
<a name="l00447"></a>00447 
<a name="l00448"></a>00448             <span class="comment">/* the protocol is unset after a power down */</span>
<a name="l00449"></a>00449             rContext-&gt;<a class="code" href="structReaderContext.html#ad83f23e5ebcf7ddd14d27928da96a97e" title="link to the reader state">readerState</a>-&gt;<a class="code" href="structpubReaderStatesList.html#a8218d71b0c62a617790e645fca310913" title="SCARD_PROTOCOL_* value.">cardProtocol</a> = <a class="code" href="pcsclite_8h.html#a3b42ca2bc25ec7ff3211a25110b4fc60" title="protocol not set">SCARD_PROTOCOL_UNDEFINED</a>;
<a name="l00450"></a>00450         }
<a name="l00451"></a>00451 
<a name="l00452"></a>00452         <span class="comment">/* the card was in use */</span>
<a name="l00453"></a>00453         <span class="keywordflow">if</span> (<a class="code" href="pcscd_8h.html#adc29c2ff13d900c2f185ee95427fb06caf024e22b873abf167c350828a94c6858" title="card was in use">POWER_STATE_GRACE_PERIOD</a> == rContext-&gt;<a class="code" href="structReaderContext.html#af19f336bd37ddc379cbd32106acb82e7" title="auto power off state">powerState</a>)
<a name="l00454"></a>00454         {
<a name="l00455"></a>00455             <span class="comment">/* the next state should be UNPOWERED unless the</span>
<a name="l00456"></a>00456 <span class="comment">             * card is used again */</span>
<a name="l00457"></a>00457             rContext-&gt;<a class="code" href="structReaderContext.html#af19f336bd37ddc379cbd32106acb82e7" title="auto power off state">powerState</a> = <a class="code" href="pcscd_8h.html#adc29c2ff13d900c2f185ee95427fb06ca7be8439cb9f5abff2a92f7949e52aa4a" title="powered">POWER_STATE_POWERED</a>;
<a name="l00458"></a>00458             Log1(PCSC_LOG_DEBUG, <span class="stringliteral">&quot;powerState: POWER_STATE_POWERED&quot;</span>);
<a name="l00459"></a>00459         }
<a name="l00460"></a>00460         (void)pthread_mutex_unlock(&amp;rContext-&gt;<a class="code" href="structReaderContext.html#a48cbdd28a24fa823fdb1c079326104c2" title="powerState mutex">powerState_lock</a>);
<a name="l00461"></a>00461 <span class="preprocessor">#endif</span>
<a name="l00462"></a>00462 <span class="preprocessor"></span>
<a name="l00463"></a>00463         <span class="keywordflow">if</span> (rContext-&gt;<a class="code" href="structReaderContext.html#a709439b280f5c7366f806423bfb5fdb5" title="Lock Id.">hLockId</a> == 0xFFFF)
<a name="l00464"></a>00464         {
<a name="l00465"></a>00465             <span class="comment">/*</span>
<a name="l00466"></a>00466 <span class="comment">             * Exit and notify the caller</span>
<a name="l00467"></a>00467 <span class="comment">             */</span>
<a name="l00468"></a>00468             (void)<a class="code" href="eventhandler_8c.html#af758bdf0f8f42781f0b24a042fda65c9" title="Sends an asynchronous event to any waiting client.">EHSignalEventToClients</a>();
<a name="l00469"></a>00469             Log1(PCSC_LOG_INFO, <span class="stringliteral">&quot;Die&quot;</span>);
<a name="l00470"></a>00470             rContext-&gt;<a class="code" href="structReaderContext.html#a709439b280f5c7366f806423bfb5fdb5" title="Lock Id.">hLockId</a> = 0;
<a name="l00471"></a>00471             (void)pthread_exit(NULL);
<a name="l00472"></a>00472         }
<a name="l00473"></a>00473     }
<a name="l00474"></a>00474 }
<a name="l00475"></a>00475 
</pre></div></div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Thu Sep 15 2011 for pcsc-lite by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.4 </small></address>
</body>
</html>