<!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: winscard_clnt.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.3 --> <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 <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 Page</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 id="navrow2" class="tabs2"> <ul class="tablist"> <li><a href="files.html"><span>File 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"> <h1>winscard_clnt.c</h1> </div> </div> <div class="contents"> <a href="winscard__clnt_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-2004</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) 2003-2004</span> <a name="l00007"></a>00007 <span class="comment"> * Damien Sauveron <damien.sauveron@labri.fr></span> <a name="l00008"></a>00008 <span class="comment"> * Copyright (C) 2005</span> <a name="l00009"></a>00009 <span class="comment"> * Martin Paljak <martin@paljak.pri.ee></span> <a name="l00010"></a>00010 <span class="comment"> * Copyright (C) 2002-2010</span> <a name="l00011"></a>00011 <span class="comment"> * Ludovic Rousseau <ludovic.rousseau@free.fr></span> <a name="l00012"></a>00012 <span class="comment"> * Copyright (C) 2009</span> <a name="l00013"></a>00013 <span class="comment"> * Jean-Luc Giraud <jlgiraud@googlemail.com></span> <a name="l00014"></a>00014 <span class="comment"> *</span> <a name="l00015"></a>00015 <span class="comment"> * $Id: winscard_clnt.c 5659 2011-03-16 17:35:54Z rousseau $</span> <a name="l00016"></a>00016 <span class="comment"> */</span> <a name="l00017"></a>00017 <a name="l00084"></a>00084 <span class="preprocessor">#include "config.h"</span> <a name="l00085"></a>00085 <span class="preprocessor">#include <stdlib.h></span> <a name="l00086"></a>00086 <span class="preprocessor">#include <string.h></span> <a name="l00087"></a>00087 <span class="preprocessor">#include <sys/types.h></span> <a name="l00088"></a>00088 <span class="preprocessor">#include <fcntl.h></span> <a name="l00089"></a>00089 <span class="preprocessor">#include <unistd.h></span> <a name="l00090"></a>00090 <span class="preprocessor">#include <sys/un.h></span> <a name="l00091"></a>00091 <span class="preprocessor">#include <errno.h></span> <a name="l00092"></a>00092 <span class="preprocessor">#include <stddef.h></span> <a name="l00093"></a>00093 <span class="preprocessor">#include <sys/time.h></span> <a name="l00094"></a>00094 <span class="preprocessor">#include <pthread.h></span> <a name="l00095"></a>00095 <span class="preprocessor">#include <sys/wait.h></span> <a name="l00096"></a>00096 <a name="l00097"></a>00097 <span class="preprocessor">#include "misc.h"</span> <a name="l00098"></a>00098 <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="l00099"></a>00099 <span class="preprocessor">#include "winscard.h"</span> <a name="l00100"></a>00100 <span class="preprocessor">#include "debuglog.h"</span> <a name="l00101"></a>00101 <span class="preprocessor">#include "<a class="code" href="strlcpycat_8h.html" title="prototypes of strlcpy()/strlcat() imported from OpenBSD">strlcpycat.h</a>"</span> <a name="l00102"></a>00102 <a name="l00103"></a>00103 <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="l00104"></a>00104 <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="l00105"></a>00105 <span class="preprocessor">#include "<a class="code" href="sys__generic_8h.html" title="This handles abstract system level calls.">sys_generic.h</a>"</span> <a name="l00106"></a>00106 <span class="preprocessor">#include "<a class="code" href="winscard__msg_8h.html" title="This defines some structures and #defines to be used over the transport layer.">winscard_msg.h</a>"</span> <a name="l00107"></a>00107 <span class="preprocessor">#include "utils.h"</span> <a name="l00108"></a>00108 <a name="l00109"></a>00109 <span class="comment">/* Display, on stderr, a trace of the WinSCard calls with arguments and</span> <a name="l00110"></a>00110 <span class="comment"> * results */</span> <a name="l00111"></a>00111 <span class="preprocessor">#undef DO_TRACE</span> <a name="l00112"></a>00112 <span class="preprocessor"></span> <a name="l00113"></a>00113 <span class="comment">/* Profile the execution time of WinSCard calls */</span> <a name="l00114"></a>00114 <span class="preprocessor">#undef DO_PROFILE</span> <a name="l00115"></a>00115 <span class="preprocessor"></span> <a name="l00116"></a>00116 <span class="comment">/* Check that handles are not shared between (forked) processes</span> <a name="l00117"></a>00117 <span class="comment"> * This check is disabled since some systems uses the same PID for</span> <a name="l00118"></a>00118 <span class="comment"> * different threads of a same process */</span> <a name="l00119"></a>00119 <span class="preprocessor">#undef DO_CHECK_SAME_PROCESS</span> <a name="l00120"></a>00120 <span class="preprocessor"></span> <a name="l00121"></a>00121 <a name="l00123"></a><a class="code" href="winscard__clnt_8c.html#ac7c340dc56974012d266eff9be71e9ed">00123</a> <span class="preprocessor">#define SCARD_PROTOCOL_ANY_OLD 0x1000</span> <a name="l00124"></a>00124 <span class="preprocessor"></span> <a name="l00125"></a>00125 <span class="preprocessor">#ifndef TRUE</span> <a name="l00126"></a>00126 <span class="preprocessor"></span><span class="preprocessor">#define TRUE 1</span> <a name="l00127"></a>00127 <span class="preprocessor"></span><span class="preprocessor">#define FALSE 0</span> <a name="l00128"></a>00128 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00129"></a>00129 <span class="preprocessor"></span> <a name="l00130"></a>00130 <span class="keyword">static</span> <span class="keywordtype">char</span> sharing_shall_block = TRUE; <a name="l00131"></a>00131 <a name="l00132"></a>00132 <span class="preprocessor">#define COLOR_RED "\33[01;31m"</span> <a name="l00133"></a>00133 <span class="preprocessor"></span><span class="preprocessor">#define COLOR_GREEN "\33[32m"</span> <a name="l00134"></a>00134 <span class="preprocessor"></span><span class="preprocessor">#define COLOR_BLUE "\33[34m"</span> <a name="l00135"></a>00135 <span class="preprocessor"></span><span class="preprocessor">#define COLOR_MAGENTA "\33[35m"</span> <a name="l00136"></a>00136 <span class="preprocessor"></span><span class="preprocessor">#define COLOR_NORMAL "\33[0m"</span> <a name="l00137"></a>00137 <span class="preprocessor"></span> <a name="l00138"></a>00138 <span class="preprocessor">#ifdef DO_TRACE</span> <a name="l00139"></a>00139 <span class="preprocessor"></span> <a name="l00140"></a>00140 <span class="preprocessor">#include <stdio.h></span> <a name="l00141"></a>00141 <span class="preprocessor">#include <stdarg.h></span> <a name="l00142"></a>00142 <a name="l00143"></a>00143 <span class="keyword">static</span> <span class="keywordtype">void</span> trace(<span class="keyword">const</span> <span class="keywordtype">char</span> *func, <span class="keyword">const</span> <span class="keywordtype">char</span> direction, <span class="keyword">const</span> <span class="keywordtype">char</span> *fmt, ...) <a name="l00144"></a>00144 { <a name="l00145"></a>00145 va_list args; <a name="l00146"></a>00146 <a name="l00147"></a>00147 fprintf(stderr, COLOR_GREEN <span class="stringliteral">"%c "</span> COLOR_BLUE <span class="stringliteral">"[%lX] "</span> COLOR_GREEN <span class="stringliteral">"%s "</span>, <a name="l00148"></a>00148 direction, pthread_self(), func); <a name="l00149"></a>00149 <a name="l00150"></a>00150 fprintf(stderr, COLOR_MAGENTA); <a name="l00151"></a>00151 va_start(args, fmt); <a name="l00152"></a>00152 vfprintf(stderr, fmt, args); <a name="l00153"></a>00153 va_end(args); <a name="l00154"></a>00154 <a name="l00155"></a>00155 fprintf(stderr, COLOR_NORMAL <span class="stringliteral">"\n"</span>); <a name="l00156"></a>00156 } <a name="l00157"></a>00157 <a name="l00158"></a>00158 <span class="preprocessor">#define API_TRACE_IN(...) trace(__FUNCTION__, '<', __VA_ARGS__);</span> <a name="l00159"></a>00159 <span class="preprocessor"></span><span class="preprocessor">#define API_TRACE_OUT(...) trace(__FUNCTION__, '>', __VA_ARGS__);</span> <a name="l00160"></a>00160 <span class="preprocessor"></span><span class="preprocessor">#else</span> <a name="l00161"></a>00161 <span class="preprocessor"></span><span class="preprocessor">#define API_TRACE_IN(...)</span> <a name="l00162"></a>00162 <span class="preprocessor"></span><span class="preprocessor">#define API_TRACE_OUT(...)</span> <a name="l00163"></a>00163 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00164"></a>00164 <span class="preprocessor"></span> <a name="l00165"></a>00165 <span class="preprocessor">#ifdef DO_PROFILE</span> <a name="l00166"></a>00166 <span class="preprocessor"></span> <a name="l00167"></a>00167 <span class="preprocessor">#define PROFILE_FILE "/tmp/pcsc_profile"</span> <a name="l00168"></a>00168 <span class="preprocessor"></span><span class="preprocessor">#include <stdio.h></span> <a name="l00169"></a>00169 <span class="preprocessor">#include <sys/time.h></span> <a name="l00170"></a>00170 <a name="l00171"></a>00171 <span class="comment">/* we can profile a maximum of 5 simultaneous calls */</span> <a name="l00172"></a>00172 <span class="preprocessor">#define MAX_THREADS 5</span> <a name="l00173"></a>00173 <span class="preprocessor"></span>pthread_t threads[MAX_THREADS]; <a name="l00174"></a>00174 <span class="keyword">struct </span>timeval profile_time_start[MAX_THREADS]; <a name="l00175"></a>00175 FILE *profile_fd; <a name="l00176"></a>00176 <span class="keywordtype">char</span> profile_tty; <a name="l00177"></a>00177 <a name="l00178"></a>00178 <span class="preprocessor">#define PROFILE_START profile_start();</span> <a name="l00179"></a>00179 <span class="preprocessor"></span><span class="preprocessor">#define PROFILE_END(rv) profile_end(__FUNCTION__, rv);</span> <a name="l00180"></a>00180 <span class="preprocessor"></span> <a name="l00181"></a>00181 <span class="keyword">static</span> <span class="keywordtype">void</span> profile_start(<span class="keywordtype">void</span>) <a name="l00182"></a>00182 { <a name="l00183"></a>00183 <span class="keyword">static</span> <span class="keywordtype">char</span> initialized = FALSE; <a name="l00184"></a>00184 pthread_t t; <a name="l00185"></a>00185 <span class="keywordtype">int</span> i; <a name="l00186"></a>00186 <a name="l00187"></a>00187 <span class="keywordflow">if</span> (!initialized) <a name="l00188"></a>00188 { <a name="l00189"></a>00189 <span class="keywordtype">char</span> filename[80]; <a name="l00190"></a>00190 <a name="l00191"></a>00191 initialized = TRUE; <a name="l00192"></a>00192 sprintf(filename, <span class="stringliteral">"%s-%d"</span>, PROFILE_FILE, getuid()); <a name="l00193"></a>00193 profile_fd = fopen(filename, <span class="stringliteral">"a+"</span>); <a name="l00194"></a>00194 <span class="keywordflow">if</span> (NULL == profile_fd) <a name="l00195"></a>00195 { <a name="l00196"></a>00196 fprintf(stderr, COLOR_RED <span class="stringliteral">"Can't open %s: %s"</span> COLOR_NORMAL <span class="stringliteral">"\n"</span>, <a name="l00197"></a>00197 PROFILE_FILE, strerror(errno)); <a name="l00198"></a>00198 exit(-1); <a name="l00199"></a>00199 } <a name="l00200"></a>00200 fprintf(profile_fd, <span class="stringliteral">"\nStart a new profile\n"</span>); <a name="l00201"></a>00201 <a name="l00202"></a>00202 <span class="keywordflow">if</span> (isatty(fileno(stderr))) <a name="l00203"></a>00203 profile_tty = TRUE; <a name="l00204"></a>00204 <span class="keywordflow">else</span> <a name="l00205"></a>00205 profile_tty = FALSE; <a name="l00206"></a>00206 } <a name="l00207"></a>00207 <a name="l00208"></a>00208 t = pthread_self(); <a name="l00209"></a>00209 <span class="keywordflow">for</span> (i=0; i<MAX_THREADS; i++) <a name="l00210"></a>00210 <span class="keywordflow">if</span> (pthread_equal(0, threads[i])) <a name="l00211"></a>00211 { <a name="l00212"></a>00212 threads[i] = t; <a name="l00213"></a>00213 <span class="keywordflow">break</span>; <a name="l00214"></a>00214 } <a name="l00215"></a>00215 <a name="l00216"></a>00216 gettimeofday(&profile_time_start[i], NULL); <a name="l00217"></a>00217 } <span class="comment">/* profile_start */</span> <a name="l00218"></a>00218 <a name="l00219"></a>00219 <span class="keyword">static</span> <span class="keywordtype">void</span> profile_end(<span class="keyword">const</span> <span class="keywordtype">char</span> *f, LONG rv) <a name="l00220"></a>00220 { <a name="l00221"></a>00221 <span class="keyword">struct </span>timeval profile_time_end; <a name="l00222"></a>00222 <span class="keywordtype">long</span> d; <a name="l00223"></a>00223 pthread_t t; <a name="l00224"></a>00224 <span class="keywordtype">int</span> i; <a name="l00225"></a>00225 <a name="l00226"></a>00226 gettimeofday(&profile_time_end, NULL); <a name="l00227"></a>00227 <a name="l00228"></a>00228 t = pthread_self(); <a name="l00229"></a>00229 <span class="keywordflow">for</span> (i=0; i<MAX_THREADS; i++) <a name="l00230"></a>00230 <span class="keywordflow">if</span> (pthread_equal(t, threads[i])) <a name="l00231"></a>00231 <span class="keywordflow">break</span>; <a name="l00232"></a>00232 <a name="l00233"></a>00233 <span class="keywordflow">if</span> (i>=MAX_THREADS) <a name="l00234"></a>00234 { <a name="l00235"></a>00235 fprintf(stderr, COLOR_BLUE <span class="stringliteral">" WARNING: no start info for %s\n"</span>, f); <a name="l00236"></a>00236 <span class="keywordflow">return</span>; <a name="l00237"></a>00237 } <a name="l00238"></a>00238 <a name="l00239"></a>00239 d = <a class="code" href="utils_8c.html#a470f4ac4abe231a7b3346be4fe455912" title="return the difference (as long int) in µs between 2 struct timeval r = a - b">time_sub</a>(&profile_time_end, &profile_time_start[i]); <a name="l00240"></a>00240 <a name="l00241"></a>00241 <span class="comment">/* free this entry */</span> <a name="l00242"></a>00242 threads[i] = 0; <a name="l00243"></a>00243 <a name="l00244"></a>00244 <span class="keywordflow">if</span> (profile_tty) <a name="l00245"></a>00245 { <a name="l00246"></a>00246 <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="l00247"></a>00247 fprintf(stderr, <a name="l00248"></a>00248 COLOR_RED <span class="stringliteral">"RESULT %s "</span> COLOR_MAGENTA <span class="stringliteral">"%ld "</span> <a name="l00249"></a>00249 COLOR_BLUE <span class="stringliteral">"0x%08lX %s"</span> COLOR_NORMAL <span class="stringliteral">"\n"</span>, <a name="l00250"></a>00250 f, d, rv, <a class="code" href="group__API.html#gab5c302876c4c8187143603837a41aa86" title="This function return a human readable text for the given PC/SC error code.">pcsc_stringify_error</a>(rv)); <a name="l00251"></a>00251 <span class="keywordflow">else</span> <a name="l00252"></a>00252 fprintf(stderr, COLOR_RED <span class="stringliteral">"RESULT %s "</span> COLOR_MAGENTA <span class="stringliteral">"%ld"</span> <a name="l00253"></a>00253 COLOR_NORMAL <span class="stringliteral">"\n"</span>, f, d); <a name="l00254"></a>00254 } <a name="l00255"></a>00255 fprintf(profile_fd, <span class="stringliteral">"%s %ld\n"</span>, f, d); <a name="l00256"></a>00256 fflush(profile_fd); <a name="l00257"></a>00257 } <span class="comment">/* profile_end */</span> <a name="l00258"></a>00258 <a name="l00259"></a>00259 <span class="preprocessor">#else</span> <a name="l00260"></a>00260 <span class="preprocessor"></span><span class="preprocessor">#define PROFILE_START</span> <a name="l00261"></a>00261 <span class="preprocessor"></span><span class="preprocessor">#define PROFILE_END(rv)</span> <a name="l00262"></a>00262 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00263"></a>00263 <span class="preprocessor"></span> <a name="l00268"></a><a class="code" href="struct__psChannelMap.html">00268</a> <span class="keyword">struct </span><a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">_psChannelMap</a> <a name="l00269"></a>00269 { <a name="l00270"></a>00270 <a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard; <a name="l00271"></a>00271 LPSTR readerName; <a name="l00272"></a>00272 }; <a name="l00273"></a>00273 <a name="l00274"></a>00274 <span class="keyword">typedef</span> <span class="keyword">struct </span><a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">_psChannelMap</a> <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a>; <a name="l00275"></a>00275 <a name="l00276"></a>00276 <span class="keyword">static</span> <span class="keywordtype">int</span> CHANNEL_MAP_seeker(<span class="keyword">const</span> <span class="keywordtype">void</span> *el, <span class="keyword">const</span> <span class="keywordtype">void</span> *key) <a name="l00277"></a>00277 { <a name="l00278"></a>00278 <span class="keyword">const</span> <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * channelMap = el; <a name="l00279"></a>00279 <a name="l00280"></a>00280 <span class="keywordflow">if</span> ((el == NULL) || (key == NULL)) <a name="l00281"></a>00281 { <a name="l00282"></a>00282 Log3(PCSC_LOG_CRITICAL, <a name="l00283"></a>00283 <span class="stringliteral">"CHANNEL_MAP_seeker called with NULL pointer: el=%X, key=%X"</span>, <a name="l00284"></a>00284 el, key); <a name="l00285"></a>00285 <span class="keywordflow">return</span> 0; <a name="l00286"></a>00286 } <a name="l00287"></a>00287 <a name="l00288"></a>00288 <span class="keywordflow">if</span> (channelMap->hCard == *(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> *)key) <a name="l00289"></a>00289 <span class="keywordflow">return</span> 1; <a name="l00290"></a>00290 <a name="l00291"></a>00291 <span class="keywordflow">return</span> 0; <a name="l00292"></a>00292 } <a name="l00293"></a>00293 <a name="l00299"></a><a class="code" href="struct__psContextMap.html">00299</a> <span class="keyword">struct </span><a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">_psContextMap</a> <a name="l00300"></a>00300 { <a name="l00301"></a><a class="code" href="struct__psContextMap.html#a4e3f21f7252b71833af88ba9f2864577">00301</a> DWORD <a class="code" href="struct__psContextMap.html#a4e3f21f7252b71833af88ba9f2864577" title="Client Connection ID.">dwClientID</a>; <a name="l00302"></a><a class="code" href="struct__psContextMap.html#a4171c839145c22f7594818304bf4e7a9">00302</a> <a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> <a class="code" href="struct__psContextMap.html#a4171c839145c22f7594818304bf4e7a9" title="Application Context ID.">hContext</a>; <a name="l00303"></a><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1">00303</a> pthread_mutex_t * <a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a>; <a name="l00304"></a>00304 <a class="code" href="structlist__t.html" title="list object">list_t</a> channelMapList; <a name="l00305"></a><a class="code" href="struct__psContextMap.html#ac75775235adbaffd6b1293b439d149de">00305</a> <span class="keywordtype">char</span> <a class="code" href="struct__psContextMap.html#ac75775235adbaffd6b1293b439d149de" title="We are in a cancellable call.">cancellable</a>; <a name="l00306"></a>00306 }; <a name="l00307"></a>00307 <span class="keyword">typedef</span> <span class="keyword">struct </span><a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">_psContextMap</a> <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a>; <a name="l00308"></a>00308 <a name="l00309"></a>00309 <span class="keyword">static</span> <a class="code" href="structlist__t.html" title="list object">list_t</a> contextMapList; <a name="l00310"></a>00310 <a name="l00311"></a>00311 <span class="keyword">static</span> <span class="keywordtype">int</span> SCONTEXTMAP_seeker(<span class="keyword">const</span> <span class="keywordtype">void</span> *el, <span class="keyword">const</span> <span class="keywordtype">void</span> *key) <a name="l00312"></a>00312 { <a name="l00313"></a>00313 <span class="keyword">const</span> <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * contextMap = el; <a name="l00314"></a>00314 <a name="l00315"></a>00315 <span class="keywordflow">if</span> ((el == NULL) || (key == NULL)) <a name="l00316"></a>00316 { <a name="l00317"></a>00317 Log3(PCSC_LOG_CRITICAL, <a name="l00318"></a>00318 <span class="stringliteral">"SCONTEXTMAP_seeker called with NULL pointer: el=%X, key=%X"</span>, <a name="l00319"></a>00319 el, key); <a name="l00320"></a>00320 <span class="keywordflow">return</span> 0; <a name="l00321"></a>00321 } <a name="l00322"></a>00322 <a name="l00323"></a>00323 <span class="keywordflow">if</span> (contextMap-><a class="code" href="struct__psContextMap.html#a4171c839145c22f7594818304bf4e7a9" title="Application Context ID.">hContext</a> == *(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> *) key) <a name="l00324"></a>00324 <span class="keywordflow">return</span> 1; <a name="l00325"></a>00325 <a name="l00326"></a>00326 <span class="keywordflow">return</span> 0; <a name="l00327"></a>00327 } <a name="l00328"></a>00328 <a name="l00332"></a><a class="code" href="winscard__clnt_8c.html#a1a5339bb356528ce056a2b3bfcc49444">00332</a> <span class="keyword">static</span> <span class="keywordtype">short</span> <a class="code" href="winscard__clnt_8c.html#a1a5339bb356528ce056a2b3bfcc49444" title="Make sure the initialization code is executed only once.">isExecuted</a> = 0; <a name="l00333"></a>00333 <a name="l00334"></a>00334 <a name="l00339"></a><a class="code" href="winscard__clnt_8c.html#a5fcdb76a493df6af8c2c9983cf38437d">00339</a> <span class="keyword">static</span> pthread_mutex_t <a class="code" href="winscard__clnt_8c.html#a5fcdb76a493df6af8c2c9983cf38437d" title="Ensure that some functions be accessed in thread-safe mode.">clientMutex</a> = PTHREAD_MUTEX_INITIALIZER; <a name="l00340"></a>00340 <a name="l00344"></a><a class="code" href="winscard__clnt_8c.html#ad70f4ad5a6e9b001970ee9afc2130875">00344</a> <span class="keyword">static</span> <a class="code" href="structpubReaderStatesList.html" title="Define an exported public reader state structure so each application gets instant notification of cha...">READER_STATE</a> <a class="code" href="winscard__clnt_8c.html#ad70f4ad5a6e9b001970ee9afc2130875" title="Area used to read status information about the readers.">readerStates</a>[<a class="code" href="pcsclite_8h.html#af750e6e22c809de2b523c17e4a092036" title="Maximum readers context (a slot is count as a reader)">PCSCLITE_MAX_READERS_CONTEXTS</a>]; <a name="l00345"></a>00345 <a name="l00347"></a><a class="code" href="winscard__clnt_8c.html#add194ea1e5a4e8e92533a807b1b2d1ff">00347</a> PCSC_API <a class="code" href="structSCARD__IO__REQUEST.html" title="Protocol Control Information (PCI)">SCARD_IO_REQUEST</a> <a class="code" href="pcsclite_8h.html#abe3a45e274a34ac64e58aa182830ab8d" title="Protocol Control Information for T=0.">g_rgSCardT0Pci</a> = { <a class="code" href="pcsclite_8h.html#a4fe95267c322c3bf35f30fab6b25526c" title="T=0 active protocol.">SCARD_PROTOCOL_T0</a>, <span class="keyword">sizeof</span>(<a class="code" href="structSCARD__IO__REQUEST.html" title="Protocol Control Information (PCI)">SCARD_IO_REQUEST</a>) }; <a name="l00349"></a><a class="code" href="winscard__clnt_8c.html#a1ea1ee5a3fa301c87f05a18e99fcd871">00349</a> PCSC_API <a class="code" href="structSCARD__IO__REQUEST.html" title="Protocol Control Information (PCI)">SCARD_IO_REQUEST</a> <a class="code" href="pcsclite_8h.html#a8e122be2c6b100398fa06abb6e470d26" title="Protocol Control Information for T=1.">g_rgSCardT1Pci</a> = { <a class="code" href="pcsclite_8h.html#a3c09cd155e07044f857130c183d64f03" title="T=1 active protocol.">SCARD_PROTOCOL_T1</a>, <span class="keyword">sizeof</span>(<a class="code" href="structSCARD__IO__REQUEST.html" title="Protocol Control Information (PCI)">SCARD_IO_REQUEST</a>) }; <a name="l00351"></a><a class="code" href="winscard__clnt_8c.html#acadf43275ed54ac6ab7f380ade2eaf9d">00351</a> PCSC_API <a class="code" href="structSCARD__IO__REQUEST.html" title="Protocol Control Information (PCI)">SCARD_IO_REQUEST</a> <a class="code" href="pcsclite_8h.html#ae4c28993abd5a1a7ad77aaadfc92281d" title="Protocol Control Information for raw access.">g_rgSCardRawPci</a> = { <a class="code" href="pcsclite_8h.html#ab58a85ece18f4217fe5372981231d195" title="Raw active protocol.">SCARD_PROTOCOL_RAW</a>, <span class="keyword">sizeof</span>(<a class="code" href="structSCARD__IO__REQUEST.html" title="Protocol Control Information (PCI)">SCARD_IO_REQUEST</a>) }; <a name="l00352"></a>00352 <a name="l00353"></a>00353 <a name="l00354"></a>00354 <span class="keyword">static</span> LONG <a class="code" href="winscard__clnt_8c.html#ac75190d70d571bca9915bef60491534a" title="Functions for managing instances of SCardEstablishContext() These functions keep track of Context han...">SCardAddContext</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a>, DWORD); <a name="l00355"></a>00355 <span class="keyword">static</span> <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a>); <a name="l00356"></a>00356 <span class="keyword">static</span> <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * <a class="code" href="winscard__clnt_8c.html#aafd00ba256e4ba8a81491c970ee336ed" title="Get the address from the Application Context list _psContextMap for the passed context.">SCardGetContextTH</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a>); <a name="l00357"></a>00357 <span class="keyword">static</span> LONG <a class="code" href="winscard__clnt_8c.html#a7e3dc878020f1e5ccdae1a6914abd3b0" title="Removes an Application Context from a control vector.">SCardRemoveContext</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a>); <a name="l00358"></a>00358 <span class="keyword">static</span> LONG SCardCleanContext(<a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> *); <a name="l00359"></a>00359 <a name="l00360"></a>00360 <span class="keyword">static</span> LONG SCardAddHandle(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a>, <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> *, LPCSTR); <a name="l00361"></a>00361 <span class="keyword">static</span> LONG SCardGetContextAndChannelFromHandle(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a>, <a name="l00362"></a>00362 <span class="comment">/*@out@*/</span> <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * *, <span class="comment">/*@out@*/</span> <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * *); <a name="l00363"></a>00363 <span class="keyword">static</span> LONG SCardGetContextAndChannelFromHandleTH(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a>, <a name="l00364"></a>00364 <span class="comment">/*@out@*/</span> <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * *, <span class="comment">/*@out@*/</span> <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * *); <a name="l00365"></a>00365 <span class="keyword">static</span> LONG SCardRemoveHandle(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a>); <a name="l00366"></a>00366 <a name="l00367"></a>00367 <span class="keyword">static</span> LONG SCardGetSetAttrib(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, <span class="keywordtype">int</span> command, DWORD dwAttrId, <a name="l00368"></a>00368 LPBYTE pbAttr, LPDWORD pcbAttrLen); <a name="l00369"></a>00369 <a name="l00370"></a>00370 <span class="preprocessor">#ifdef DO_CHECK_SAME_PROCESS</span> <a name="l00371"></a>00371 <span class="preprocessor"></span>pid_t client_pid = 0; <a name="l00372"></a>00372 <span class="keyword">static</span> LONG SCardCheckSameProcess(<span class="keywordtype">void</span>); <a name="l00373"></a>00373 <span class="preprocessor">#define CHECK_SAME_PROCESS \</span> <a name="l00374"></a>00374 <span class="preprocessor"> rv = SCardCheckSameProcess(); \</span> <a name="l00375"></a>00375 <span class="preprocessor"> if (rv != SCARD_S_SUCCESS) \</span> <a name="l00376"></a>00376 <span class="preprocessor"> return rv;</span> <a name="l00377"></a>00377 <span class="preprocessor"></span><span class="preprocessor">#else</span> <a name="l00378"></a>00378 <span class="preprocessor"></span><span class="preprocessor">#define CHECK_SAME_PROCESS</span> <a name="l00379"></a>00379 <span class="preprocessor"></span><span class="preprocessor">#endif</span> <a name="l00380"></a>00380 <span class="preprocessor"></span> <a name="l00381"></a>00381 <span class="keyword">static</span> LONG getReaderStates(<a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap); <a name="l00382"></a>00382 <a name="l00383"></a>00383 <span class="comment">/*</span> <a name="l00384"></a>00384 <span class="comment"> * Thread safety functions</span> <a name="l00385"></a>00385 <span class="comment"> */</span> <a name="l00392"></a><a class="code" href="winscard__clnt_8c.html#a1f7e1b05b30f1e5a5cd62b2f925222fe">00392</a> <span class="keyword">inline</span> <span class="keyword">static</span> LONG <a class="code" href="winscard__clnt_8c.html#a1f7e1b05b30f1e5a5cd62b2f925222fe" title="Locks a mutex so another thread must wait to use this function.">SCardLockThread</a>(<span class="keywordtype">void</span>) <a name="l00393"></a>00393 { <a name="l00394"></a>00394 <span class="keywordflow">return</span> pthread_mutex_lock(&clientMutex); <a name="l00395"></a>00395 } <a name="l00396"></a>00396 <a name="l00402"></a><a class="code" href="winscard__clnt_8c.html#a5dc8cab71db796c9e91329c22f4ebfa1">00402</a> <span class="keyword">inline</span> <span class="keyword">static</span> LONG <a class="code" href="winscard__clnt_8c.html#a5dc8cab71db796c9e91329c22f4ebfa1" title="Unlocks a mutex so another thread may use the client.">SCardUnlockThread</a>(<span class="keywordtype">void</span>) <a name="l00403"></a>00403 { <a name="l00404"></a>00404 <span class="keywordflow">return</span> pthread_mutex_unlock(&clientMutex); <a name="l00405"></a>00405 } <a name="l00406"></a>00406 <a name="l00407"></a>00407 <span class="keyword">static</span> LONG <a class="code" href="winscard__clnt_8c.html#aa86f1a8ccfbdbe7c64eea5219c7639e6" title="Creates a communication context to the PC/SC Resource Manager.">SCardEstablishContextTH</a>(DWORD, LPCVOID, LPCVOID, <a name="l00408"></a>00408 <span class="comment">/*@out@*/</span> LPSCARDCONTEXT); <a name="l00409"></a>00409 <a name="l00443"></a><a class="code" href="group__API.html#gaa1b8970169fd4883a6dc4a8f43f19b67">00443</a> LONG <a class="code" href="group__API.html#gaa1b8970169fd4883a6dc4a8f43f19b67" title="Creates an Application Context to the PC/SC Resource Manager.">SCardEstablishContext</a>(DWORD dwScope, LPCVOID pvReserved1, <a name="l00444"></a>00444 LPCVOID pvReserved2, LPSCARDCONTEXT phContext) <a name="l00445"></a>00445 { <a name="l00446"></a>00446 LONG rv; <a name="l00447"></a>00447 <a name="l00448"></a>00448 API_TRACE_IN(<span class="stringliteral">"%ld, %p, %p"</span>, dwScope, pvReserved1, pvReserved2) <a name="l00449"></a>00449 PROFILE_START <a name="l00450"></a>00450 <a name="l00451"></a>00451 <span class="comment">/* Check if the server is running */</span> <a name="l00452"></a>00452 rv = <a class="code" href="winscard__clnt_8c.html#aaa02e1f70d68a502cd92ed066c44542f" title="Checks if the server is running.">SCardCheckDaemonAvailability</a>(); <a name="l00453"></a>00453 <span class="keywordflow">if</span> (<a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a> == rv) <a name="l00454"></a>00454 <span class="comment">/* we reconnected to a daemon or we got called from a forked child */</span> <a name="l00455"></a>00455 rv = <a class="code" href="winscard__clnt_8c.html#aaa02e1f70d68a502cd92ed066c44542f" title="Checks if the server is running.">SCardCheckDaemonAvailability</a>(); <a name="l00456"></a>00456 <a name="l00457"></a>00457 <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="l00458"></a>00458 <span class="keywordflow">goto</span> end; <a name="l00459"></a>00459 <a name="l00460"></a>00460 (void)<a class="code" href="winscard__clnt_8c.html#a1f7e1b05b30f1e5a5cd62b2f925222fe" title="Locks a mutex so another thread must wait to use this function.">SCardLockThread</a>(); <a name="l00461"></a>00461 rv = <a class="code" href="winscard__clnt_8c.html#aa86f1a8ccfbdbe7c64eea5219c7639e6" title="Creates a communication context to the PC/SC Resource Manager.">SCardEstablishContextTH</a>(dwScope, pvReserved1, <a name="l00462"></a>00462 pvReserved2, phContext); <a name="l00463"></a>00463 (void)<a class="code" href="winscard__clnt_8c.html#a5dc8cab71db796c9e91329c22f4ebfa1" title="Unlocks a mutex so another thread may use the client.">SCardUnlockThread</a>(); <a name="l00464"></a>00464 <a name="l00465"></a>00465 end: <a name="l00466"></a>00466 PROFILE_END(rv) <a name="l00467"></a>00467 API_TRACE_OUT(<span class="stringliteral">"%ld"</span>, *phContext) <a name="l00468"></a>00468 <a name="l00469"></a>00469 <span class="keywordflow">return</span> rv; <a name="l00470"></a>00470 } <a name="l00471"></a>00471 <a name="l00498"></a><a class="code" href="winscard__clnt_8c.html#aa86f1a8ccfbdbe7c64eea5219c7639e6">00498</a> <span class="keyword">static</span> LONG <a class="code" href="winscard__clnt_8c.html#aa86f1a8ccfbdbe7c64eea5219c7639e6" title="Creates a communication context to the PC/SC Resource Manager.">SCardEstablishContextTH</a>(DWORD dwScope, <a name="l00499"></a>00499 <span class="comment">/*@unused@*/</span> LPCVOID pvReserved1, <a name="l00500"></a>00500 <span class="comment">/*@unused@*/</span> LPCVOID pvReserved2, LPSCARDCONTEXT phContext) <a name="l00501"></a>00501 { <a name="l00502"></a>00502 LONG rv; <a name="l00503"></a>00503 <span class="keyword">struct </span><a class="code" href="structestablish__struct.html" title="Information contained in SCARD_ESTABLISH_CONTEXT Messages.">establish_struct</a> scEstablishStruct; <a name="l00504"></a>00504 uint32_t dwClientID = 0; <a name="l00505"></a>00505 <a name="l00506"></a>00506 (void)pvReserved1; <a name="l00507"></a>00507 (void)pvReserved2; <a name="l00508"></a>00508 <span class="keywordflow">if</span> (phContext == NULL) <a name="l00509"></a>00509 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l00510"></a>00510 <span class="keywordflow">else</span> <a name="l00511"></a>00511 *phContext = 0; <a name="l00512"></a>00512 <a name="l00513"></a>00513 <span class="comment">/*</span> <a name="l00514"></a>00514 <span class="comment"> * Do this only once:</span> <a name="l00515"></a>00515 <span class="comment"> * - Initialize context list.</span> <a name="l00516"></a>00516 <span class="comment"> */</span> <a name="l00517"></a>00517 <span class="keywordflow">if</span> (isExecuted == 0) <a name="l00518"></a>00518 { <a name="l00519"></a>00519 <span class="keywordtype">int</span> lrv; <a name="l00520"></a>00520 <a name="l00521"></a>00521 <span class="comment">/* NOTE: The list will never be freed (No API call exists to</span> <a name="l00522"></a>00522 <span class="comment"> * "close all contexts".</span> <a name="l00523"></a>00523 <span class="comment"> * Applications which load and unload the library will leak</span> <a name="l00524"></a>00524 <span class="comment"> * the list's internal structures. */</span> <a name="l00525"></a>00525 lrv = list_init(&contextMapList); <a name="l00526"></a>00526 <span class="keywordflow">if</span> (lrv < 0) <a name="l00527"></a>00527 { <a name="l00528"></a>00528 Log2(PCSC_LOG_CRITICAL, <span class="stringliteral">"list_init failed with return value: %d"</span>, <a name="l00529"></a>00529 lrv); <a name="l00530"></a>00530 <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="l00531"></a>00531 } <a name="l00532"></a>00532 <a name="l00533"></a>00533 lrv = list_attributes_seeker(&contextMapList, <a name="l00534"></a>00534 SCONTEXTMAP_seeker); <a name="l00535"></a>00535 <span class="keywordflow">if</span> (lrv <0) <a name="l00536"></a>00536 { <a name="l00537"></a>00537 Log2(PCSC_LOG_CRITICAL, <a name="l00538"></a>00538 <span class="stringliteral">"list_attributes_seeker failed with return value: %d"</span>, lrv); <a name="l00539"></a>00539 list_destroy(&contextMapList); <a name="l00540"></a>00540 <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="l00541"></a>00541 } <a name="l00542"></a>00542 <a name="l00543"></a>00543 <span class="keywordflow">if</span> (getenv(<span class="stringliteral">"PCSCLITE_NO_BLOCKING"</span>)) <a name="l00544"></a>00544 { <a name="l00545"></a>00545 Log1(PCSC_LOG_INFO, <span class="stringliteral">"Disable shared blocking"</span>); <a name="l00546"></a>00546 sharing_shall_block = FALSE; <a name="l00547"></a>00547 } <a name="l00548"></a>00548 <a name="l00549"></a>00549 isExecuted = 1; <a name="l00550"></a>00550 } <a name="l00551"></a>00551 <a name="l00552"></a>00552 <a name="l00553"></a>00553 <span class="comment">/* Establishes a connection to the server */</span> <a name="l00554"></a>00554 <span class="keywordflow">if</span> (<a class="code" href="winscard__msg_8c.html#a616a7d779449d1dc6a8a5d925d8212b1" title="Prepares a communication channel for the client to talk to the server.">ClientSetupSession</a>(&dwClientID) != 0) <a name="l00555"></a>00555 { <a name="l00556"></a>00556 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#ad4729ab109ff490285d2ad881c04bee8" title="The Smart card resource manager is not running.">SCARD_E_NO_SERVICE</a>; <a name="l00557"></a>00557 } <a name="l00558"></a>00558 <a name="l00559"></a>00559 { <span class="comment">/* exchange client/server protocol versions */</span> <a name="l00560"></a>00560 <span class="keyword">struct </span><a class="code" href="structversion__struct.html" title="Information transmitted in CMD_VERSION Messages.">version_struct</a> veStr; <a name="l00561"></a>00561 <a name="l00562"></a>00562 veStr.<a class="code" href="structversion__struct.html#a32863448345f9226766e08966d89b726" title="IPC major PROTOCOL_VERSION_MAJOR.">major</a> = <a class="code" href="winscard__msg_8h.html#a2ec4920ff3b207d71c2ae86d807dbdd7" title="Major version of the current message protocol.">PROTOCOL_VERSION_MAJOR</a>; <a name="l00563"></a>00563 veStr.<a class="code" href="structversion__struct.html#a5eba3ab76bf8a5f0b97cd574b487b3d0" title="IPC minor PROTOCOL_VERSION_MINOR.">minor</a> = <a class="code" href="winscard__msg_8h.html#a9ff5cd412ba2c8563fa89754cf534d41" title="Minor version of the current message protocol.">PROTOCOL_VERSION_MINOR</a>; <a name="l00564"></a>00564 veStr.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="l00565"></a>00565 <a name="l00566"></a>00566 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca775afaef84c85d8173a20debb1a9e2ef" title="get the client/server protocol version">CMD_VERSION</a>, dwClientID, <span class="keyword">sizeof</span>(veStr), <a name="l00567"></a>00567 &veStr); <a name="l00568"></a>00568 <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="l00569"></a>00569 <span class="keywordflow">return</span> rv; <a name="l00570"></a>00570 <a name="l00571"></a>00571 <span class="comment">/* Read a message from the server */</span> <a name="l00572"></a>00572 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&veStr, <span class="keyword">sizeof</span>(veStr), dwClientID); <a name="l00573"></a>00573 <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="l00574"></a>00574 { <a name="l00575"></a>00575 Log1(PCSC_LOG_CRITICAL, <a name="l00576"></a>00576 <span class="stringliteral">"Your pcscd is too old and does not support CMD_VERSION"</span>); <a name="l00577"></a>00577 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a93d3cf468d69423eab1d478a7a870408" title="An internal communications error has been detected.">SCARD_F_COMM_ERROR</a>; <a name="l00578"></a>00578 } <a name="l00579"></a>00579 <a name="l00580"></a>00580 Log3(PCSC_LOG_INFO, <span class="stringliteral">"Server is protocol version %d:%d"</span>, <a name="l00581"></a>00581 veStr.<a class="code" href="structversion__struct.html#a32863448345f9226766e08966d89b726" title="IPC major PROTOCOL_VERSION_MAJOR.">major</a>, veStr.<a class="code" href="structversion__struct.html#a5eba3ab76bf8a5f0b97cd574b487b3d0" title="IPC minor PROTOCOL_VERSION_MINOR.">minor</a>); <a name="l00582"></a>00582 <a name="l00583"></a>00583 <span class="keywordflow">if</span> (veStr.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="l00584"></a>00584 <span class="keywordflow">return</span> veStr.rv; <a name="l00585"></a>00585 } <a name="l00586"></a>00586 <a name="l00587"></a>00587 again: <a name="l00588"></a>00588 <span class="comment">/*</span> <a name="l00589"></a>00589 <span class="comment"> * Try to establish an Application Context with the server</span> <a name="l00590"></a>00590 <span class="comment"> */</span> <a name="l00591"></a>00591 scEstablishStruct.dwScope = dwScope; <a name="l00592"></a>00592 scEstablishStruct.hContext = 0; <a name="l00593"></a>00593 scEstablishStruct.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="l00594"></a>00594 <a name="l00595"></a>00595 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dcaf90d9882a83562f678f57ce8de9a51a1" title="used by SCardEstablishContext()">SCARD_ESTABLISH_CONTEXT</a>, dwClientID, <a name="l00596"></a>00596 <span class="keyword">sizeof</span>(scEstablishStruct), (<span class="keywordtype">void</span> *) &scEstablishStruct); <a name="l00597"></a>00597 <a name="l00598"></a>00598 <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="l00599"></a>00599 <span class="keywordflow">return</span> rv; <a name="l00600"></a>00600 <a name="l00601"></a>00601 <span class="comment">/*</span> <a name="l00602"></a>00602 <span class="comment"> * Read the response from the server</span> <a name="l00603"></a>00603 <span class="comment"> */</span> <a name="l00604"></a>00604 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&scEstablishStruct, <span class="keyword">sizeof</span>(scEstablishStruct), <a name="l00605"></a>00605 dwClientID); <a name="l00606"></a>00606 <a name="l00607"></a>00607 <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="l00608"></a>00608 <span class="keywordflow">return</span> rv; <a name="l00609"></a>00609 <a name="l00610"></a>00610 <span class="keywordflow">if</span> (scEstablishStruct.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="l00611"></a>00611 <span class="keywordflow">return</span> scEstablishStruct.rv; <a name="l00612"></a>00612 <a name="l00613"></a>00613 <span class="comment">/* check we do not reuse an existing hContext */</span> <a name="l00614"></a>00614 <span class="keywordflow">if</span> (NULL != <a class="code" href="winscard__clnt_8c.html#aafd00ba256e4ba8a81491c970ee336ed" title="Get the address from the Application Context list _psContextMap for the passed context.">SCardGetContextTH</a>(scEstablishStruct.hContext)) <a name="l00615"></a>00615 <span class="comment">/* we do not need to release the allocated context since</span> <a name="l00616"></a>00616 <span class="comment"> * SCardReleaseContext() does nothing on the server side */</span> <a name="l00617"></a>00617 <span class="keywordflow">goto</span> again; <a name="l00618"></a>00618 <a name="l00619"></a>00619 *phContext = scEstablishStruct.hContext; <a name="l00620"></a>00620 <a name="l00621"></a>00621 <span class="comment">/*</span> <a name="l00622"></a>00622 <span class="comment"> * Allocate the new hContext - if allocator full return an error</span> <a name="l00623"></a>00623 <span class="comment"> */</span> <a name="l00624"></a>00624 rv = <a class="code" href="winscard__clnt_8c.html#ac75190d70d571bca9915bef60491534a" title="Functions for managing instances of SCardEstablishContext() These functions keep track of Context han...">SCardAddContext</a>(*phContext, dwClientID); <a name="l00625"></a>00625 <a name="l00626"></a>00626 <span class="keywordflow">return</span> rv; <a name="l00627"></a>00627 } <a name="l00628"></a>00628 <a name="l00650"></a><a class="code" href="group__API.html#ga6aabcba7744c5c9419fdd6404f73a934">00650</a> LONG <a class="code" href="group__API.html#ga6aabcba7744c5c9419fdd6404f73a934" title="Destroys a communication context to the PC/SC Resource Manager.">SCardReleaseContext</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> hContext) <a name="l00651"></a>00651 { <a name="l00652"></a>00652 LONG rv; <a name="l00653"></a>00653 <span class="keyword">struct </span><a class="code" href="structrelease__struct.html" title="Information contained in SCARD_RELEASE_CONTEXT Messages.">release_struct</a> scReleaseStruct; <a name="l00654"></a>00654 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l00655"></a>00655 <a name="l00656"></a>00656 API_TRACE_IN(<span class="stringliteral">"%ld"</span>, hContext) <a name="l00657"></a>00657 PROFILE_START <a name="l00658"></a>00658 <a name="l00659"></a>00659 CHECK_SAME_PROCESS <a name="l00660"></a>00660 <a name="l00661"></a>00661 <span class="comment">/*</span> <a name="l00662"></a>00662 <span class="comment"> * Make sure this context has been opened</span> <a name="l00663"></a>00663 <span class="comment"> * and get currentContextMap</span> <a name="l00664"></a>00664 <span class="comment"> */</span> <a name="l00665"></a>00665 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l00666"></a>00666 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l00667"></a>00667 { <a name="l00668"></a>00668 rv = <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l00669"></a>00669 <span class="keywordflow">goto</span> error; <a name="l00670"></a>00670 } <a name="l00671"></a>00671 <a name="l00672"></a>00672 (void)pthread_mutex_lock(currentContextMap->mMutex); <a name="l00673"></a>00673 <a name="l00674"></a>00674 <span class="comment">/* check the context is still opened */</span> <a name="l00675"></a>00675 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l00676"></a>00676 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l00677"></a>00677 <span class="comment">/* the context is now invalid</span> <a name="l00678"></a>00678 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l00679"></a>00679 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l00680"></a>00680 { <a name="l00681"></a>00681 rv = <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l00682"></a>00682 <span class="keywordflow">goto</span> error; <a name="l00683"></a>00683 } <a name="l00684"></a>00684 <a name="l00685"></a>00685 scReleaseStruct.hContext = hContext; <a name="l00686"></a>00686 scReleaseStruct.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="l00687"></a>00687 <a name="l00688"></a>00688 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca072877189b15a07bbc7918a7fdd2a573" title="used by SCardReleaseContext()">SCARD_RELEASE_CONTEXT</a>, <a name="l00689"></a>00689 currentContextMap->dwClientID, <a name="l00690"></a>00690 <span class="keyword">sizeof</span>(scReleaseStruct), (<span class="keywordtype">void</span> *) &scReleaseStruct); <a name="l00691"></a>00691 <a name="l00692"></a>00692 <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="l00693"></a>00693 <span class="keywordflow">goto</span> end; <a name="l00694"></a>00694 <a name="l00695"></a>00695 <span class="comment">/*</span> <a name="l00696"></a>00696 <span class="comment"> * Read a message from the server</span> <a name="l00697"></a>00697 <span class="comment"> */</span> <a name="l00698"></a>00698 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&scReleaseStruct, <span class="keyword">sizeof</span>(scReleaseStruct), <a name="l00699"></a>00699 currentContextMap->dwClientID); <a name="l00700"></a>00700 <a name="l00701"></a>00701 <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="l00702"></a>00702 <span class="keywordflow">goto</span> end; <a name="l00703"></a>00703 <a name="l00704"></a>00704 rv = scReleaseStruct.rv; <a name="l00705"></a>00705 end: <a name="l00706"></a>00706 (void)pthread_mutex_unlock(currentContextMap->mMutex); <a name="l00707"></a>00707 <a name="l00708"></a>00708 <span class="comment">/*</span> <a name="l00709"></a>00709 <span class="comment"> * Remove the local context from the stack</span> <a name="l00710"></a>00710 <span class="comment"> */</span> <a name="l00711"></a>00711 (void)<a class="code" href="winscard__clnt_8c.html#a1f7e1b05b30f1e5a5cd62b2f925222fe" title="Locks a mutex so another thread must wait to use this function.">SCardLockThread</a>(); <a name="l00712"></a>00712 (void)<a class="code" href="winscard__clnt_8c.html#a7e3dc878020f1e5ccdae1a6914abd3b0" title="Removes an Application Context from a control vector.">SCardRemoveContext</a>(hContext); <a name="l00713"></a>00713 (void)<a class="code" href="winscard__clnt_8c.html#a5dc8cab71db796c9e91329c22f4ebfa1" title="Unlocks a mutex so another thread may use the client.">SCardUnlockThread</a>(); <a name="l00714"></a>00714 <a name="l00715"></a>00715 error: <a name="l00716"></a>00716 PROFILE_END(rv) <a name="l00717"></a>00717 API_TRACE_OUT(<span class="stringliteral">""</span>) <a name="l00718"></a>00718 <a name="l00719"></a>00719 <span class="keywordflow">return</span> rv; <a name="l00720"></a>00720 } <a name="l00721"></a>00721 <a name="l00778"></a><a class="code" href="group__API.html#ga4e515829752e0a8dbc4d630696a8d6a5">00778</a> LONG <a class="code" href="group__API.html#ga4e515829752e0a8dbc4d630696a8d6a5" title="Establishes a connection to the reader specified in * szReader.">SCardConnect</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> hContext, LPCSTR szReader, <a name="l00779"></a>00779 DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, <a name="l00780"></a>00780 LPDWORD pdwActiveProtocol) <a name="l00781"></a>00781 { <a name="l00782"></a>00782 LONG rv; <a name="l00783"></a>00783 <span class="keyword">struct </span><a class="code" href="structconnect__struct.html" title="contained in SCARD_CONNECT Messages.">connect_struct</a> scConnectStruct; <a name="l00784"></a>00784 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l00785"></a>00785 <a name="l00786"></a>00786 PROFILE_START <a name="l00787"></a>00787 API_TRACE_IN(<span class="stringliteral">"%d %s %d %d"</span>, hContext, szReader, dwShareMode, dwPreferredProtocols) <a name="l00788"></a>00788 <a name="l00789"></a>00789 <span class="comment">/*</span> <a name="l00790"></a>00790 <span class="comment"> * Check for NULL parameters</span> <a name="l00791"></a>00791 <span class="comment"> */</span> <a name="l00792"></a>00792 <span class="keywordflow">if</span> (phCard == NULL || pdwActiveProtocol == NULL) <a name="l00793"></a>00793 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l00794"></a>00794 <span class="keywordflow">else</span> <a name="l00795"></a>00795 *phCard = 0; <a name="l00796"></a>00796 <a name="l00797"></a>00797 <span class="keywordflow">if</span> (szReader == NULL) <a name="l00798"></a>00798 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a1bc24e84bfe33ef68be7251e2423190a" title="The specified reader name is not recognized.">SCARD_E_UNKNOWN_READER</a>; <a name="l00799"></a>00799 <a name="l00800"></a>00800 <span class="comment">/*</span> <a name="l00801"></a>00801 <span class="comment"> * Check for uninitialized strings</span> <a name="l00802"></a>00802 <span class="comment"> */</span> <a name="l00803"></a>00803 <span class="keywordflow">if</span> (strlen(szReader) > MAX_READERNAME) <a name="l00804"></a>00804 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a9ece9fd9e68e4f802e16e951f8f2a642" title="One or more of the supplied parameters values could not be properly interpreted.">SCARD_E_INVALID_VALUE</a>; <a name="l00805"></a>00805 <a name="l00806"></a>00806 CHECK_SAME_PROCESS <a name="l00807"></a>00807 <a name="l00808"></a>00808 <span class="comment">/*</span> <a name="l00809"></a>00809 <span class="comment"> * Make sure this context has been opened</span> <a name="l00810"></a>00810 <span class="comment"> */</span> <a name="l00811"></a>00811 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l00812"></a>00812 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l00813"></a>00813 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l00814"></a>00814 <a name="l00815"></a>00815 (void)pthread_mutex_lock(currentContextMap->mMutex); <a name="l00816"></a>00816 <a name="l00817"></a>00817 <span class="comment">/* check the context is still opened */</span> <a name="l00818"></a>00818 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l00819"></a>00819 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l00820"></a>00820 <span class="comment">/* the context is now invalid</span> <a name="l00821"></a>00821 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l00822"></a>00822 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l00823"></a>00823 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l00824"></a>00824 <a name="l00825"></a>00825 strncpy(scConnectStruct.szReader, szReader, MAX_READERNAME); <a name="l00826"></a>00826 <a name="l00827"></a>00827 scConnectStruct.hContext = hContext; <a name="l00828"></a>00828 scConnectStruct.dwShareMode = dwShareMode; <a name="l00829"></a>00829 scConnectStruct.dwPreferredProtocols = dwPreferredProtocols; <a name="l00830"></a>00830 scConnectStruct.hCard = 0; <a name="l00831"></a>00831 scConnectStruct.dwActiveProtocol = 0; <a name="l00832"></a>00832 scConnectStruct.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="l00833"></a>00833 <a name="l00834"></a>00834 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dcae4539f439555e7f577f08de789fd4e2c" title="used by SCardConnect()">SCARD_CONNECT</a>, currentContextMap->dwClientID, <a name="l00835"></a>00835 <span class="keyword">sizeof</span>(scConnectStruct), (<span class="keywordtype">void</span> *) &scConnectStruct); <a name="l00836"></a>00836 <a name="l00837"></a>00837 <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="l00838"></a>00838 <span class="keywordflow">goto</span> end; <a name="l00839"></a>00839 <a name="l00840"></a>00840 <span class="comment">/*</span> <a name="l00841"></a>00841 <span class="comment"> * Read a message from the server</span> <a name="l00842"></a>00842 <span class="comment"> */</span> <a name="l00843"></a>00843 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&scConnectStruct, <span class="keyword">sizeof</span>(scConnectStruct), <a name="l00844"></a>00844 currentContextMap->dwClientID); <a name="l00845"></a>00845 <a name="l00846"></a>00846 <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="l00847"></a>00847 <span class="keywordflow">goto</span> end; <a name="l00848"></a>00848 <a name="l00849"></a>00849 *phCard = scConnectStruct.hCard; <a name="l00850"></a>00850 *pdwActiveProtocol = scConnectStruct.dwActiveProtocol; <a name="l00851"></a>00851 <a name="l00852"></a>00852 <span class="keywordflow">if</span> (scConnectStruct.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="l00853"></a>00853 { <a name="l00854"></a>00854 <span class="comment">/*</span> <a name="l00855"></a>00855 <span class="comment"> * Keep track of the handle locally</span> <a name="l00856"></a>00856 <span class="comment"> */</span> <a name="l00857"></a>00857 rv = SCardAddHandle(*phCard, currentContextMap, szReader); <a name="l00858"></a>00858 } <a name="l00859"></a>00859 <span class="keywordflow">else</span> <a name="l00860"></a>00860 rv = scConnectStruct.rv; <a name="l00861"></a>00861 <a name="l00862"></a>00862 end: <a name="l00863"></a>00863 (void)pthread_mutex_unlock(currentContextMap->mMutex); <a name="l00864"></a>00864 <a name="l00865"></a>00865 PROFILE_END(rv) <a name="l00866"></a>00866 API_TRACE_OUT(<span class="stringliteral">"%d"</span>, *pdwActiveProtocol) <a name="l00867"></a>00867 <a name="l00868"></a>00868 <span class="keywordflow">return</span> rv; <a name="l00869"></a>00869 } <a name="l00870"></a>00870 <a name="l00944"></a><a class="code" href="group__API.html#gad5d4393ca8c470112ad9468c44ed8940">00944</a> LONG <a class="code" href="group__API.html#gad5d4393ca8c470112ad9468c44ed8940" title="Reestablishes a connection to a reader that was previously connected to using SCardConnect().">SCardReconnect</a>(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, DWORD dwShareMode, <a name="l00945"></a>00945 DWORD dwPreferredProtocols, DWORD dwInitialization, <a name="l00946"></a>00946 LPDWORD pdwActiveProtocol) <a name="l00947"></a>00947 { <a name="l00948"></a>00948 LONG rv; <a name="l00949"></a>00949 <span class="keyword">struct </span><a class="code" href="structreconnect__struct.html" title="contained in SCARD_RECONNECT Messages.">reconnect_struct</a> scReconnectStruct; <a name="l00950"></a>00950 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l00951"></a>00951 <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * pChannelMap; <a name="l00952"></a>00952 <a name="l00953"></a>00953 PROFILE_START <a name="l00954"></a>00954 <a name="l00955"></a>00955 <span class="keywordflow">if</span> (pdwActiveProtocol == NULL) <a name="l00956"></a>00956 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l00957"></a>00957 <a name="l00958"></a>00958 CHECK_SAME_PROCESS <a name="l00959"></a>00959 <a name="l00960"></a>00960 <span class="comment">/*</span> <a name="l00961"></a>00961 <span class="comment"> * Make sure this handle has been opened</span> <a name="l00962"></a>00962 <span class="comment"> */</span> <a name="l00963"></a>00963 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l00964"></a>00964 &pChannelMap); <a name="l00965"></a>00965 <span class="keywordflow">if</span> (rv == -1) <a name="l00966"></a>00966 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l00967"></a>00967 <a name="l00968"></a>00968 (void)pthread_mutex_lock(currentContextMap->mMutex); <a name="l00969"></a>00969 <a name="l00970"></a>00970 <span class="comment">/* check the handle is still valid */</span> <a name="l00971"></a>00971 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l00972"></a>00972 &pChannelMap); <a name="l00973"></a>00973 <span class="keywordflow">if</span> (rv == -1) <a name="l00974"></a>00974 <span class="comment">/* the handle is now invalid</span> <a name="l00975"></a>00975 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l00976"></a>00976 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l00977"></a>00977 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l00978"></a>00978 <a name="l00979"></a>00979 <span class="comment">/* Retry loop for blocking behaviour */</span> <a name="l00980"></a>00980 retry: <a name="l00981"></a>00981 <a name="l00982"></a>00982 scReconnectStruct.hCard = hCard; <a name="l00983"></a>00983 scReconnectStruct.dwShareMode = dwShareMode; <a name="l00984"></a>00984 scReconnectStruct.dwPreferredProtocols = dwPreferredProtocols; <a name="l00985"></a>00985 scReconnectStruct.dwInitialization = dwInitialization; <a name="l00986"></a>00986 scReconnectStruct.dwActiveProtocol = *pdwActiveProtocol; <a name="l00987"></a>00987 scReconnectStruct.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="l00988"></a>00988 <a name="l00989"></a>00989 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca8b46439f3998639c1935c51fb984794f" title="used by SCardReconnect()">SCARD_RECONNECT</a>, currentContextMap->dwClientID, <a name="l00990"></a>00990 <span class="keyword">sizeof</span>(scReconnectStruct), (<span class="keywordtype">void</span> *) &scReconnectStruct); <a name="l00991"></a>00991 <a name="l00992"></a>00992 <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="l00993"></a>00993 <span class="keywordflow">goto</span> end; <a name="l00994"></a>00994 <a name="l00995"></a>00995 <span class="comment">/*</span> <a name="l00996"></a>00996 <span class="comment"> * Read a message from the server</span> <a name="l00997"></a>00997 <span class="comment"> */</span> <a name="l00998"></a>00998 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&scReconnectStruct, <span class="keyword">sizeof</span>(scReconnectStruct), <a name="l00999"></a>00999 currentContextMap->dwClientID); <a name="l01000"></a>01000 <a name="l01001"></a>01001 <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="l01002"></a>01002 <span class="keywordflow">goto</span> end; <a name="l01003"></a>01003 <a name="l01004"></a>01004 rv = scReconnectStruct.rv; <a name="l01005"></a>01005 <a name="l01006"></a>01006 <span class="keywordflow">if</span> (sharing_shall_block && (<a class="code" href="pcsclite_8h.html#a81cd3fc9e7e84c66be2471294152235f" title="The smart card cannot be accessed because of other connections outstanding.">SCARD_E_SHARING_VIOLATION</a> == rv)) <a name="l01007"></a>01007 { <a name="l01008"></a>01008 (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#ac9c6d92f56e8c08459b19c0d4d85639f" title="Lock polling rate.">PCSCLITE_LOCK_POLL_RATE</a>); <a name="l01009"></a>01009 <span class="keywordflow">goto</span> retry; <a name="l01010"></a>01010 } <a name="l01011"></a>01011 <a name="l01012"></a>01012 *pdwActiveProtocol = scReconnectStruct.dwActiveProtocol; <a name="l01013"></a>01013 <a name="l01014"></a>01014 end: <a name="l01015"></a>01015 (void)pthread_mutex_unlock(currentContextMap->mMutex); <a name="l01016"></a>01016 <a name="l01017"></a>01017 PROFILE_END(rv) <a name="l01018"></a>01018 <a name="l01019"></a>01019 <span class="keywordflow">return</span> rv; <a name="l01020"></a>01020 } <a name="l01021"></a>01021 <a name="l01053"></a><a class="code" href="group__API.html#ga4be198045c73ec0deb79e66c0ca1738a">01053</a> LONG <a class="code" href="group__API.html#ga4be198045c73ec0deb79e66c0ca1738a" title="Terminates a connection made through SCardConnect().">SCardDisconnect</a>(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, DWORD dwDisposition) <a name="l01054"></a>01054 { <a name="l01055"></a>01055 LONG rv; <a name="l01056"></a>01056 <span class="keyword">struct </span><a class="code" href="structdisconnect__struct.html" title="contained in SCARD_DISCONNECT Messages.">disconnect_struct</a> scDisconnectStruct; <a name="l01057"></a>01057 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l01058"></a>01058 <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * pChannelMap; <a name="l01059"></a>01059 <a name="l01060"></a>01060 PROFILE_START <a name="l01061"></a>01061 API_TRACE_IN(<span class="stringliteral">"%d %d"</span>, hCard, dwDisposition) <a name="l01062"></a>01062 <a name="l01063"></a>01063 CHECK_SAME_PROCESS <a name="l01064"></a>01064 <a name="l01065"></a>01065 <span class="comment">/*</span> <a name="l01066"></a>01066 <span class="comment"> * Make sure this handle has been opened</span> <a name="l01067"></a>01067 <span class="comment"> */</span> <a name="l01068"></a>01068 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l01069"></a>01069 &pChannelMap); <a name="l01070"></a>01070 <span class="keywordflow">if</span> (rv == -1) <a name="l01071"></a>01071 { <a name="l01072"></a>01072 rv = <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l01073"></a>01073 <span class="keywordflow">goto</span> error; <a name="l01074"></a>01074 } <a name="l01075"></a>01075 <a name="l01076"></a>01076 (void)pthread_mutex_lock(currentContextMap->mMutex); <a name="l01077"></a>01077 <a name="l01078"></a>01078 <span class="comment">/* check the handle is still valid */</span> <a name="l01079"></a>01079 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l01080"></a>01080 &pChannelMap); <a name="l01081"></a>01081 <span class="keywordflow">if</span> (rv == -1) <a name="l01082"></a>01082 <span class="comment">/* the handle is now invalid</span> <a name="l01083"></a>01083 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l01084"></a>01084 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l01085"></a>01085 { <a name="l01086"></a>01086 rv = <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l01087"></a>01087 <span class="keywordflow">goto</span> error; <a name="l01088"></a>01088 } <a name="l01089"></a>01089 <a name="l01090"></a>01090 scDisconnectStruct.hCard = hCard; <a name="l01091"></a>01091 scDisconnectStruct.dwDisposition = dwDisposition; <a name="l01092"></a>01092 scDisconnectStruct.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="l01093"></a>01093 <a name="l01094"></a>01094 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca014966b0829c1dd57df6c62a53c7c9f0" title="used by SCardDisconnect()">SCARD_DISCONNECT</a>, currentContextMap->dwClientID, <a name="l01095"></a>01095 <span class="keyword">sizeof</span>(scDisconnectStruct), (<span class="keywordtype">void</span> *) &scDisconnectStruct); <a name="l01096"></a>01096 <a name="l01097"></a>01097 <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="l01098"></a>01098 <span class="keywordflow">goto</span> end; <a name="l01099"></a>01099 <a name="l01100"></a>01100 <span class="comment">/*</span> <a name="l01101"></a>01101 <span class="comment"> * Read a message from the server</span> <a name="l01102"></a>01102 <span class="comment"> */</span> <a name="l01103"></a>01103 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&scDisconnectStruct, <span class="keyword">sizeof</span>(scDisconnectStruct), <a name="l01104"></a>01104 currentContextMap->dwClientID); <a name="l01105"></a>01105 <a name="l01106"></a>01106 <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="l01107"></a>01107 <span class="keywordflow">goto</span> end; <a name="l01108"></a>01108 <a name="l01109"></a>01109 <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> == scDisconnectStruct.rv) <a name="l01110"></a>01110 (void)SCardRemoveHandle(hCard); <a name="l01111"></a>01111 rv = scDisconnectStruct.rv; <a name="l01112"></a>01112 <a name="l01113"></a>01113 end: <a name="l01114"></a>01114 (void)pthread_mutex_unlock(currentContextMap->mMutex); <a name="l01115"></a>01115 <a name="l01116"></a>01116 error: <a name="l01117"></a>01117 PROFILE_END(rv) <a name="l01118"></a>01118 API_TRACE_OUT(<span class="stringliteral">""</span>) <a name="l01119"></a>01119 <a name="l01120"></a>01120 <span class="keywordflow">return</span> rv; <a name="l01121"></a>01121 } <a name="l01122"></a>01122 <a name="l01158"></a><a class="code" href="group__API.html#gaddb835dce01a0da1d6ca02d33ee7d861">01158</a> LONG <a class="code" href="group__API.html#gaddb835dce01a0da1d6ca02d33ee7d861" title="Establishes a temporary exclusive access mode for doing a serie of commands in a transaction.">SCardBeginTransaction</a>(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard) <a name="l01159"></a>01159 { <a name="l01160"></a>01160 <a name="l01161"></a>01161 LONG rv; <a name="l01162"></a>01162 <span class="keyword">struct </span><a class="code" href="structbegin__struct.html" title="contained in SCARD_BEGIN_TRANSACTION Messages.">begin_struct</a> scBeginStruct; <a name="l01163"></a>01163 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l01164"></a>01164 <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * pChannelMap; <a name="l01165"></a>01165 <a name="l01166"></a>01166 PROFILE_START <a name="l01167"></a>01167 <a name="l01168"></a>01168 CHECK_SAME_PROCESS <a name="l01169"></a>01169 <a name="l01170"></a>01170 <span class="comment">/*</span> <a name="l01171"></a>01171 <span class="comment"> * Make sure this handle has been opened</span> <a name="l01172"></a>01172 <span class="comment"> */</span> <a name="l01173"></a>01173 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l01174"></a>01174 &pChannelMap); <a name="l01175"></a>01175 <span class="keywordflow">if</span> (rv == -1) <a name="l01176"></a>01176 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l01177"></a>01177 <a name="l01178"></a>01178 (void)pthread_mutex_lock(currentContextMap->mMutex); <a name="l01179"></a>01179 <a name="l01180"></a>01180 <span class="comment">/* check the handle is still valid */</span> <a name="l01181"></a>01181 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l01182"></a>01182 &pChannelMap); <a name="l01183"></a>01183 <span class="keywordflow">if</span> (rv == -1) <a name="l01184"></a>01184 <span class="comment">/* the handle is now invalid</span> <a name="l01185"></a>01185 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l01186"></a>01186 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l01187"></a>01187 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l01188"></a>01188 <a name="l01189"></a>01189 scBeginStruct.hCard = hCard; <a name="l01190"></a>01190 scBeginStruct.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="l01191"></a>01191 <a name="l01192"></a>01192 <span class="comment">/*</span> <a name="l01193"></a>01193 <span class="comment"> * Query the server every so often until the sharing violation ends</span> <a name="l01194"></a>01194 <span class="comment"> * and then hold the lock for yourself.</span> <a name="l01195"></a>01195 <span class="comment"> */</span> <a name="l01196"></a>01196 <a name="l01197"></a>01197 <span class="keywordflow">do</span> <a name="l01198"></a>01198 { <a name="l01199"></a>01199 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dcaa49e6ed0bf4f1b9fe85c23699fdac94c" title="used by SCardBeginTransaction()">SCARD_BEGIN_TRANSACTION</a>, <a name="l01200"></a>01200 currentContextMap->dwClientID, <a name="l01201"></a>01201 <span class="keyword">sizeof</span>(scBeginStruct), (<span class="keywordtype">void</span> *) &scBeginStruct); <a name="l01202"></a>01202 <a name="l01203"></a>01203 <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="l01204"></a>01204 <span class="keywordflow">goto</span> end; <a name="l01205"></a>01205 <a name="l01206"></a>01206 <span class="comment">/*</span> <a name="l01207"></a>01207 <span class="comment"> * Read a message from the server</span> <a name="l01208"></a>01208 <span class="comment"> */</span> <a name="l01209"></a>01209 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&scBeginStruct, <span class="keyword">sizeof</span>(scBeginStruct), <a name="l01210"></a>01210 currentContextMap->dwClientID); <a name="l01211"></a>01211 <a name="l01212"></a>01212 <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="l01213"></a>01213 <span class="keywordflow">goto</span> end; <a name="l01214"></a>01214 <a name="l01215"></a>01215 rv = scBeginStruct.rv; <a name="l01216"></a>01216 } <a name="l01217"></a>01217 <span class="keywordflow">while</span> (<a class="code" href="pcsclite_8h.html#a81cd3fc9e7e84c66be2471294152235f" title="The smart card cannot be accessed because of other connections outstanding.">SCARD_E_SHARING_VIOLATION</a> == rv); <a name="l01218"></a>01218 <a name="l01219"></a>01219 end: <a name="l01220"></a>01220 (void)pthread_mutex_unlock(currentContextMap->mMutex); <a name="l01221"></a>01221 <a name="l01222"></a>01222 PROFILE_END(rv) <a name="l01223"></a>01223 <a name="l01224"></a>01224 <span class="keywordflow">return</span> rv; <a name="l01225"></a>01225 } <a name="l01226"></a>01226 <a name="l01267"></a><a class="code" href="group__API.html#gae8742473b404363e5c587f570d7e2f3b">01267</a> LONG <a class="code" href="group__API.html#gae8742473b404363e5c587f570d7e2f3b" title="Ends a previously begun transaction.">SCardEndTransaction</a>(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, DWORD dwDisposition) <a name="l01268"></a>01268 { <a name="l01269"></a>01269 LONG rv; <a name="l01270"></a>01270 <span class="keyword">struct </span><a class="code" href="structend__struct.html" title="contained in SCARD_END_TRANSACTION Messages.">end_struct</a> scEndStruct; <a name="l01271"></a>01271 <span class="keywordtype">int</span> randnum; <a name="l01272"></a>01272 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l01273"></a>01273 <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * pChannelMap; <a name="l01274"></a>01274 <a name="l01275"></a>01275 PROFILE_START <a name="l01276"></a>01276 <a name="l01277"></a>01277 CHECK_SAME_PROCESS <a name="l01278"></a>01278 <a name="l01279"></a>01279 <span class="comment">/*</span> <a name="l01280"></a>01280 <span class="comment"> * Make sure this handle has been opened</span> <a name="l01281"></a>01281 <span class="comment"> */</span> <a name="l01282"></a>01282 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l01283"></a>01283 &pChannelMap); <a name="l01284"></a>01284 <span class="keywordflow">if</span> (rv == -1) <a name="l01285"></a>01285 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l01286"></a>01286 <a name="l01287"></a>01287 (void)pthread_mutex_lock(currentContextMap-><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a>); <a name="l01288"></a>01288 <a name="l01289"></a>01289 <span class="comment">/* check the handle is still valid */</span> <a name="l01290"></a>01290 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l01291"></a>01291 &pChannelMap); <a name="l01292"></a>01292 <span class="keywordflow">if</span> (rv == -1) <a name="l01293"></a>01293 <span class="comment">/* the handle is now invalid</span> <a name="l01294"></a>01294 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l01295"></a>01295 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l01296"></a>01296 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l01297"></a>01297 <a name="l01298"></a>01298 scEndStruct.hCard = hCard; <a name="l01299"></a>01299 scEndStruct.dwDisposition = dwDisposition; <a name="l01300"></a>01300 scEndStruct.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="l01301"></a>01301 <a name="l01302"></a>01302 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dcac03f9c354d75ab9e5b53d0af82b2f7ff" title="used by SCardEndTransaction()">SCARD_END_TRANSACTION</a>, <a name="l01303"></a>01303 currentContextMap-><a class="code" href="struct__psContextMap.html#a4e3f21f7252b71833af88ba9f2864577" title="Client Connection ID.">dwClientID</a>, <a name="l01304"></a>01304 <span class="keyword">sizeof</span>(scEndStruct), (<span class="keywordtype">void</span> *) &scEndStruct); <a name="l01305"></a>01305 <a name="l01306"></a>01306 <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="l01307"></a>01307 <span class="keywordflow">goto</span> end; <a name="l01308"></a>01308 <a name="l01309"></a>01309 <span class="comment">/*</span> <a name="l01310"></a>01310 <span class="comment"> * Read a message from the server</span> <a name="l01311"></a>01311 <span class="comment"> */</span> <a name="l01312"></a>01312 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&scEndStruct, <span class="keyword">sizeof</span>(scEndStruct), <a name="l01313"></a>01313 currentContextMap-><a class="code" href="struct__psContextMap.html#a4e3f21f7252b71833af88ba9f2864577" title="Client Connection ID.">dwClientID</a>); <a name="l01314"></a>01314 <a name="l01315"></a>01315 <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="l01316"></a>01316 <span class="keywordflow">goto</span> end; <a name="l01317"></a>01317 <a name="l01318"></a>01318 <span class="comment">/*</span> <a name="l01319"></a>01319 <span class="comment"> * This helps prevent starvation</span> <a name="l01320"></a>01320 <span class="comment"> */</span> <a name="l01321"></a>01321 randnum = SYS_RandomInt(1000, 10000); <a name="l01322"></a>01322 (void)<a class="code" href="sys__generic_8h.html#a910a2c6f5b5a4eadf3213bdfdd0b9bb1" title="Makes the current process sleep for some microseconds.">SYS_USleep</a>(randnum); <a name="l01323"></a>01323 rv = scEndStruct.rv; <a name="l01324"></a>01324 <a name="l01325"></a>01325 end: <a name="l01326"></a>01326 (void)pthread_mutex_unlock(currentContextMap-><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a>); <a name="l01327"></a>01327 <a name="l01328"></a>01328 PROFILE_END(rv) <a name="l01329"></a>01329 <a name="l01330"></a>01330 <span class="keywordflow">return</span> rv; <a name="l01331"></a>01331 } <a name="l01332"></a>01332 <a name="l01428"></a><a class="code" href="group__API.html#ga18f33eebb45d9e0d2a091ca7a0511d26">01428</a> LONG <a class="code" href="group__API.html#ga18f33eebb45d9e0d2a091ca7a0511d26" title="Returns the current status of the reader connected to by hCard.">SCardStatus</a>(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, LPSTR mszReaderName, <a name="l01429"></a>01429 LPDWORD pcchReaderLen, LPDWORD pdwState, <a name="l01430"></a>01430 LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen) <a name="l01431"></a>01431 { <a name="l01432"></a>01432 DWORD dwReaderLen, dwAtrLen; <a name="l01433"></a>01433 LONG rv; <a name="l01434"></a>01434 <span class="keywordtype">int</span> i; <a name="l01435"></a>01435 <span class="keyword">struct </span><a class="code" href="structstatus__struct.html" title="contained in SCARD_STATUS Messages.">status_struct</a> scStatusStruct; <a name="l01436"></a>01436 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l01437"></a>01437 <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * pChannelMap; <a name="l01438"></a>01438 <span class="keywordtype">char</span> *r; <a name="l01439"></a>01439 <span class="keywordtype">char</span> *bufReader = NULL; <a name="l01440"></a>01440 LPBYTE bufAtr = NULL; <a name="l01441"></a>01441 DWORD dummy = 0; <a name="l01442"></a>01442 <a name="l01443"></a>01443 PROFILE_START <a name="l01444"></a>01444 <a name="l01445"></a>01445 <span class="comment">/* default output values */</span> <a name="l01446"></a>01446 <span class="keywordflow">if</span> (pdwState) <a name="l01447"></a>01447 *pdwState = 0; <a name="l01448"></a>01448 <a name="l01449"></a>01449 <span class="keywordflow">if</span> (pdwProtocol) <a name="l01450"></a>01450 *pdwProtocol = 0; <a name="l01451"></a>01451 <a name="l01452"></a>01452 <span class="comment">/* Check for NULL parameters */</span> <a name="l01453"></a>01453 <span class="keywordflow">if</span> (pcchReaderLen == NULL) <a name="l01454"></a>01454 pcchReaderLen = &dummy; <a name="l01455"></a>01455 <a name="l01456"></a>01456 <span class="keywordflow">if</span> (pcbAtrLen == NULL) <a name="l01457"></a>01457 pcbAtrLen = &dummy; <a name="l01458"></a>01458 <a name="l01459"></a>01459 <span class="comment">/* length passed from caller */</span> <a name="l01460"></a>01460 dwReaderLen = *pcchReaderLen; <a name="l01461"></a>01461 dwAtrLen = *pcbAtrLen; <a name="l01462"></a>01462 <a name="l01463"></a>01463 *pcchReaderLen = 0; <a name="l01464"></a>01464 *pcbAtrLen = 0; <a name="l01465"></a>01465 <a name="l01466"></a>01466 CHECK_SAME_PROCESS <a name="l01467"></a>01467 <a name="l01468"></a>01468 <span class="comment">/*</span> <a name="l01469"></a>01469 <span class="comment"> * Make sure this handle has been opened</span> <a name="l01470"></a>01470 <span class="comment"> */</span> <a name="l01471"></a>01471 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l01472"></a>01472 &pChannelMap); <a name="l01473"></a>01473 <span class="keywordflow">if</span> (rv == -1) <a name="l01474"></a>01474 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l01475"></a>01475 <a name="l01476"></a>01476 (void)pthread_mutex_lock(currentContextMap->mMutex); <a name="l01477"></a>01477 <a name="l01478"></a>01478 <span class="comment">/* check the handle is still valid */</span> <a name="l01479"></a>01479 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l01480"></a>01480 &pChannelMap); <a name="l01481"></a>01481 <span class="keywordflow">if</span> (rv == -1) <a name="l01482"></a>01482 <span class="comment">/* the handle is now invalid</span> <a name="l01483"></a>01483 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l01484"></a>01484 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l01485"></a>01485 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l01486"></a>01486 <a name="l01487"></a>01487 <span class="comment">/* synchronize reader states with daemon */</span> <a name="l01488"></a>01488 rv = getReaderStates(currentContextMap); <a name="l01489"></a>01489 <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="l01490"></a>01490 <span class="keywordflow">goto</span> end; <a name="l01491"></a>01491 <a name="l01492"></a>01492 r = pChannelMap->readerName; <a name="l01493"></a>01493 <span class="keywordflow">for</span> (i = 0; i < <a class="code" href="pcsclite_8h.html#af750e6e22c809de2b523c17e4a092036" title="Maximum readers context (a slot is count as a reader)">PCSCLITE_MAX_READERS_CONTEXTS</a>; i++) <a name="l01494"></a>01494 { <a name="l01495"></a>01495 <span class="comment">/* by default r == NULL */</span> <a name="l01496"></a>01496 <span class="keywordflow">if</span> (r && strcmp(r, readerStates[i].readerName) == 0) <a name="l01497"></a>01497 <span class="keywordflow">break</span>; <a name="l01498"></a>01498 } <a name="l01499"></a>01499 <a name="l01500"></a>01500 <span class="keywordflow">if</span> (i == PCSCLITE_MAX_READERS_CONTEXTS) <a name="l01501"></a>01501 { <a name="l01502"></a>01502 rv = <a class="code" href="pcsclite_8h.html#ad70bc694ffdf40c3b494d9239160d69d" title="The specified reader is not currently available for use.">SCARD_E_READER_UNAVAILABLE</a>; <a name="l01503"></a>01503 <span class="keywordflow">goto</span> end; <a name="l01504"></a>01504 } <a name="l01505"></a>01505 <a name="l01506"></a>01506 <span class="comment">/* Retry loop for blocking behaviour */</span> <a name="l01507"></a>01507 retry: <a name="l01508"></a>01508 <a name="l01509"></a>01509 <span class="comment">/* initialise the structure */</span> <a name="l01510"></a>01510 memset(&scStatusStruct, 0, <span class="keyword">sizeof</span>(scStatusStruct)); <a name="l01511"></a>01511 scStatusStruct.hCard = hCard; <a name="l01512"></a>01512 <a name="l01513"></a>01513 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca8dd333ed3f05b986c67abbd6291d4188" title="used by SCardStatus()">SCARD_STATUS</a>, currentContextMap->dwClientID, <a name="l01514"></a>01514 <span class="keyword">sizeof</span>(scStatusStruct), (<span class="keywordtype">void</span> *) &scStatusStruct); <a name="l01515"></a>01515 <a name="l01516"></a>01516 <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="l01517"></a>01517 <span class="keywordflow">goto</span> end; <a name="l01518"></a>01518 <a name="l01519"></a>01519 <span class="comment">/*</span> <a name="l01520"></a>01520 <span class="comment"> * Read a message from the server</span> <a name="l01521"></a>01521 <span class="comment"> */</span> <a name="l01522"></a>01522 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&scStatusStruct, <span class="keyword">sizeof</span>(scStatusStruct), <a name="l01523"></a>01523 currentContextMap->dwClientID); <a name="l01524"></a>01524 <a name="l01525"></a>01525 <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="l01526"></a>01526 <span class="keywordflow">goto</span> end; <a name="l01527"></a>01527 <a name="l01528"></a>01528 rv = scStatusStruct.rv; <a name="l01529"></a>01529 <a name="l01530"></a>01530 <span class="keywordflow">if</span> (sharing_shall_block && (<a class="code" href="pcsclite_8h.html#a81cd3fc9e7e84c66be2471294152235f" title="The smart card cannot be accessed because of other connections outstanding.">SCARD_E_SHARING_VIOLATION</a> == rv)) <a name="l01531"></a>01531 { <a name="l01532"></a>01532 (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#ac9c6d92f56e8c08459b19c0d4d85639f" title="Lock polling rate.">PCSCLITE_LOCK_POLL_RATE</a>); <a name="l01533"></a>01533 <span class="keywordflow">goto</span> retry; <a name="l01534"></a>01534 } <a name="l01535"></a>01535 <a name="l01536"></a>01536 <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> && rv != <a class="code" href="pcsclite_8h.html#aac3f8d945f8a0e667e0926daaa18dc57" title="The data buffer to receive returned data is too small for the returned data.">SCARD_E_INSUFFICIENT_BUFFER</a>) <a name="l01537"></a>01537 { <a name="l01538"></a>01538 <span class="comment">/*</span> <a name="l01539"></a>01539 <span class="comment"> * An event must have occurred</span> <a name="l01540"></a>01540 <span class="comment"> */</span> <a name="l01541"></a>01541 <span class="keywordflow">goto</span> end; <a name="l01542"></a>01542 } <a name="l01543"></a>01543 <a name="l01544"></a>01544 <span class="comment">/*</span> <a name="l01545"></a>01545 <span class="comment"> * Now continue with the client side SCardStatus</span> <a name="l01546"></a>01546 <span class="comment"> */</span> <a name="l01547"></a>01547 <a name="l01548"></a>01548 *pcchReaderLen = strlen(pChannelMap->readerName) + 1; <a name="l01549"></a>01549 *pcbAtrLen = readerStates[i].<a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a>; <a name="l01550"></a>01550 <a name="l01551"></a>01551 <span class="keywordflow">if</span> (pdwState) <a name="l01552"></a>01552 *pdwState = (readerStates[i].<a class="code" href="structpubReaderStatesList.html#a97623f9f724a2a86db3e755badd0cf21" title="number of card events">eventCounter</a> << 16) + readerStates[i].readerState; <a name="l01553"></a>01553 <a name="l01554"></a>01554 <span class="keywordflow">if</span> (pdwProtocol) <a name="l01555"></a>01555 *pdwProtocol = readerStates[i].<a class="code" href="structpubReaderStatesList.html#a8218d71b0c62a617790e645fca310913" title="SCARD_PROTOCOL_* value.">cardProtocol</a>; <a name="l01556"></a>01556 <a name="l01557"></a>01557 <span class="keywordflow">if</span> (<a class="code" href="pcsclite_8h.html#a67db0943aeb13f9f468eaae39b5ff1b0" title="see SCardFreeMemory()">SCARD_AUTOALLOCATE</a> == dwReaderLen) <a name="l01558"></a>01558 { <a name="l01559"></a>01559 dwReaderLen = *pcchReaderLen; <a name="l01560"></a>01560 bufReader = malloc(dwReaderLen); <a name="l01561"></a>01561 <span class="keywordflow">if</span> (NULL == bufReader) <a name="l01562"></a>01562 { <a name="l01563"></a>01563 rv = <a class="code" href="pcsclite_8h.html#a81694393a1494d5091145734a2f0b36c" title="Not enough memory available to complete this command.">SCARD_E_NO_MEMORY</a>; <a name="l01564"></a>01564 <span class="keywordflow">goto</span> end; <a name="l01565"></a>01565 } <a name="l01566"></a>01566 <span class="keywordflow">if</span> (NULL == mszReaderName) <a name="l01567"></a>01567 { <a name="l01568"></a>01568 rv = <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l01569"></a>01569 <span class="keywordflow">goto</span> end; <a name="l01570"></a>01570 } <a name="l01571"></a>01571 *(<span class="keywordtype">char</span> **)mszReaderName = bufReader; <a name="l01572"></a>01572 } <a name="l01573"></a>01573 <span class="keywordflow">else</span> <a name="l01574"></a>01574 bufReader = mszReaderName; <a name="l01575"></a>01575 <a name="l01576"></a>01576 <span class="comment">/* return SCARD_E_INSUFFICIENT_BUFFER only if buffer pointer is non NULL */</span> <a name="l01577"></a>01577 <span class="keywordflow">if</span> (bufReader) <a name="l01578"></a>01578 { <a name="l01579"></a>01579 <span class="keywordflow">if</span> (*pcchReaderLen > dwReaderLen) <a name="l01580"></a>01580 rv = <a class="code" href="pcsclite_8h.html#aac3f8d945f8a0e667e0926daaa18dc57" title="The data buffer to receive returned data is too small for the returned data.">SCARD_E_INSUFFICIENT_BUFFER</a>; <a name="l01581"></a>01581 <a name="l01582"></a>01582 strncpy(bufReader, pChannelMap->readerName, dwReaderLen); <a name="l01583"></a>01583 } <a name="l01584"></a>01584 <a name="l01585"></a>01585 <span class="keywordflow">if</span> (<a class="code" href="pcsclite_8h.html#a67db0943aeb13f9f468eaae39b5ff1b0" title="see SCardFreeMemory()">SCARD_AUTOALLOCATE</a> == dwAtrLen) <a name="l01586"></a>01586 { <a name="l01587"></a>01587 dwAtrLen = *pcbAtrLen; <a name="l01588"></a>01588 bufAtr = malloc(dwAtrLen); <a name="l01589"></a>01589 <span class="keywordflow">if</span> (NULL == bufAtr) <a name="l01590"></a>01590 { <a name="l01591"></a>01591 rv = <a class="code" href="pcsclite_8h.html#a81694393a1494d5091145734a2f0b36c" title="Not enough memory available to complete this command.">SCARD_E_NO_MEMORY</a>; <a name="l01592"></a>01592 <span class="keywordflow">goto</span> end; <a name="l01593"></a>01593 } <a name="l01594"></a>01594 <span class="keywordflow">if</span> (NULL == pbAtr) <a name="l01595"></a>01595 { <a name="l01596"></a>01596 rv = <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l01597"></a>01597 <span class="keywordflow">goto</span> end; <a name="l01598"></a>01598 } <a name="l01599"></a>01599 *(LPBYTE *)pbAtr = bufAtr; <a name="l01600"></a>01600 } <a name="l01601"></a>01601 <span class="keywordflow">else</span> <a name="l01602"></a>01602 bufAtr = pbAtr; <a name="l01603"></a>01603 <a name="l01604"></a>01604 <span class="keywordflow">if</span> (bufAtr) <a name="l01605"></a>01605 { <a name="l01606"></a>01606 <span class="keywordflow">if</span> (*pcbAtrLen > dwAtrLen) <a name="l01607"></a>01607 rv = <a class="code" href="pcsclite_8h.html#aac3f8d945f8a0e667e0926daaa18dc57" title="The data buffer to receive returned data is too small for the returned data.">SCARD_E_INSUFFICIENT_BUFFER</a>; <a name="l01608"></a>01608 <a name="l01609"></a>01609 memcpy(bufAtr, readerStates[i].cardAtr, min(*pcbAtrLen, dwAtrLen)); <a name="l01610"></a>01610 } <a name="l01611"></a>01611 <a name="l01612"></a>01612 end: <a name="l01613"></a>01613 (void)pthread_mutex_unlock(currentContextMap->mMutex); <a name="l01614"></a>01614 <a name="l01615"></a>01615 PROFILE_END(rv) <a name="l01616"></a>01616 <a name="l01617"></a>01617 <span class="keywordflow">return</span> rv; <a name="l01618"></a>01618 } <a name="l01619"></a>01619 <a name="l01716"></a><a class="code" href="group__API.html#ga33247d5d1257d59e55647c3bb717db24">01716</a> LONG <a class="code" href="group__API.html#ga33247d5d1257d59e55647c3bb717db24" title="Blocks execution until the current availability of the cards in a specific set of readers changes...">SCardGetStatusChange</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> hContext, DWORD dwTimeout, <a name="l01717"></a>01717 <a class="code" href="structSCARD__READERSTATE.html">SCARD_READERSTATE</a> *rgReaderStates, DWORD cReaders) <a name="l01718"></a>01718 { <a name="l01719"></a>01719 <a class="code" href="structSCARD__READERSTATE.html">SCARD_READERSTATE</a> *currReader; <a name="l01720"></a>01720 <a class="code" href="structpubReaderStatesList.html" title="Define an exported public reader state structure so each application gets instant notification of cha...">READER_STATE</a> *rContext; <a name="l01721"></a>01721 <span class="keywordtype">long</span> dwTime; <a name="l01722"></a>01722 DWORD dwBreakFlag = 0; <a name="l01723"></a>01723 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> j; <a name="l01724"></a>01724 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l01725"></a>01725 <span class="keywordtype">int</span> currentReaderCount = 0; <a name="l01726"></a>01726 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="l01727"></a>01727 <a name="l01728"></a>01728 PROFILE_START <a name="l01729"></a>01729 API_TRACE_IN(<span class="stringliteral">"%d %d %d"</span>, hContext, dwTimeout, cReaders) <a name="l01730"></a>01730 <span class="preprocessor">#ifdef DO_TRACE</span> <a name="l01731"></a>01731 <span class="preprocessor"></span> <span class="keywordflow">for</span> (j=0; j<cReaders; j++) <a name="l01732"></a>01732 { <a name="l01733"></a>01733 API_TRACE_IN(<span class="stringliteral">"[%d] %s %X %X"</span>, j, rgReaderStates[j].szReader, <a name="l01734"></a>01734 rgReaderStates[j].dwCurrentState, rgReaderStates[j].dwEventState) <a name="l01735"></a>01735 } <a name="l01736"></a>01736 <span class="preprocessor">#endif</span> <a name="l01737"></a>01737 <span class="preprocessor"></span> <a name="l01738"></a>01738 <span class="keywordflow">if</span> ((rgReaderStates == NULL && cReaders > 0) <a name="l01739"></a>01739 || (cReaders > <a class="code" href="pcsclite_8h.html#af750e6e22c809de2b523c17e4a092036" title="Maximum readers context (a slot is count as a reader)">PCSCLITE_MAX_READERS_CONTEXTS</a>)) <a name="l01740"></a>01740 { <a name="l01741"></a>01741 rv = <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l01742"></a>01742 <span class="keywordflow">goto</span> error; <a name="l01743"></a>01743 } <a name="l01744"></a>01744 <a name="l01745"></a>01745 <span class="comment">/* Check the integrity of the reader states structures */</span> <a name="l01746"></a>01746 <span class="keywordflow">for</span> (j = 0; j < cReaders; j++) <a name="l01747"></a>01747 { <a name="l01748"></a>01748 <span class="keywordflow">if</span> (rgReaderStates[j].szReader == NULL) <a name="l01749"></a>01749 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a9ece9fd9e68e4f802e16e951f8f2a642" title="One or more of the supplied parameters values could not be properly interpreted.">SCARD_E_INVALID_VALUE</a>; <a name="l01750"></a>01750 } <a name="l01751"></a>01751 <a name="l01752"></a>01752 <span class="comment">/* return if all readers are SCARD_STATE_IGNORE */</span> <a name="l01753"></a>01753 <span class="keywordflow">if</span> (cReaders > 0) <a name="l01754"></a>01754 { <a name="l01755"></a>01755 <span class="keywordtype">int</span> nbNonIgnoredReaders = cReaders; <a name="l01756"></a>01756 <a name="l01757"></a>01757 <span class="keywordflow">for</span> (j=0; j<cReaders; j++) <a name="l01758"></a>01758 <span class="keywordflow">if</span> (rgReaderStates[j].dwCurrentState & <a class="code" href="pcsclite_8h.html#a0bfdd63120b09223fd4fed4884cbfeec" title="Ignore this reader.">SCARD_STATE_IGNORE</a>) <a name="l01759"></a>01759 nbNonIgnoredReaders--; <a name="l01760"></a>01760 <a name="l01761"></a>01761 <span class="keywordflow">if</span> (0 == nbNonIgnoredReaders) <a name="l01762"></a>01762 { <a name="l01763"></a>01763 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="l01764"></a>01764 <span class="keywordflow">goto</span> error; <a name="l01765"></a>01765 } <a name="l01766"></a>01766 } <a name="l01767"></a>01767 <span class="keywordflow">else</span> <a name="l01768"></a>01768 { <a name="l01769"></a>01769 <span class="comment">/* reader list is empty */</span> <a name="l01770"></a>01770 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="l01771"></a>01771 <span class="keywordflow">goto</span> error; <a name="l01772"></a>01772 } <a name="l01773"></a>01773 <a name="l01774"></a>01774 CHECK_SAME_PROCESS <a name="l01775"></a>01775 <a name="l01776"></a>01776 <span class="comment">/*</span> <a name="l01777"></a>01777 <span class="comment"> * Make sure this context has been opened</span> <a name="l01778"></a>01778 <span class="comment"> */</span> <a name="l01779"></a>01779 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l01780"></a>01780 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l01781"></a>01781 { <a name="l01782"></a>01782 rv = <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l01783"></a>01783 <span class="keywordflow">goto</span> error; <a name="l01784"></a>01784 } <a name="l01785"></a>01785 <a name="l01786"></a>01786 (void)pthread_mutex_lock(currentContextMap->mMutex); <a name="l01787"></a>01787 <a name="l01788"></a>01788 <span class="comment">/* check the context is still opened */</span> <a name="l01789"></a>01789 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l01790"></a>01790 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l01791"></a>01791 <span class="comment">/* the context is now invalid</span> <a name="l01792"></a>01792 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l01793"></a>01793 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l01794"></a>01794 { <a name="l01795"></a>01795 rv = <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l01796"></a>01796 <span class="keywordflow">goto</span> error; <a name="l01797"></a>01797 } <a name="l01798"></a>01798 <a name="l01799"></a>01799 <span class="comment">/* synchronize reader states with daemon */</span> <a name="l01800"></a>01800 rv = getReaderStates(currentContextMap); <a name="l01801"></a>01801 <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="l01802"></a>01802 <span class="keywordflow">goto</span> end; <a name="l01803"></a>01803 <a name="l01804"></a>01804 <span class="comment">/* Clear the event state for all readers */</span> <a name="l01805"></a>01805 <span class="keywordflow">for</span> (j = 0; j < cReaders; j++) <a name="l01806"></a>01806 rgReaderStates[j].dwEventState = 0; <a name="l01807"></a>01807 <a name="l01808"></a>01808 <span class="comment">/* Now is where we start our event checking loop */</span> <a name="l01809"></a>01809 Log2(PCSC_LOG_DEBUG, <span class="stringliteral">"Event Loop Start, dwTimeout: %ld"</span>, dwTimeout); <a name="l01810"></a>01810 <a name="l01811"></a>01811 <span class="comment">/* Get the initial reader count on the system */</span> <a name="l01812"></a>01812 <span class="keywordflow">for</span> (j=0; j < <a class="code" href="pcsclite_8h.html#af750e6e22c809de2b523c17e4a092036" title="Maximum readers context (a slot is count as a reader)">PCSCLITE_MAX_READERS_CONTEXTS</a>; j++) <a name="l01813"></a>01813 <span class="keywordflow">if</span> (readerStates[j].readerName[0] != <span class="charliteral">'\0'</span>) <a name="l01814"></a>01814 currentReaderCount++; <a name="l01815"></a>01815 <a name="l01816"></a>01816 <span class="keywordflow">if</span> (<a class="code" href="pcsclite_8h.html#aa84a29002ab81c719c0d07bb446296e0" title="Infinite timeout.">INFINITE</a> == dwTimeout) <a name="l01817"></a>01817 dwTime = 60*1000; <span class="comment">/* "infinite" timeout */</span> <a name="l01818"></a>01818 <span class="keywordflow">else</span> <a name="l01819"></a>01819 dwTime = dwTimeout; <a name="l01820"></a>01820 <a name="l01821"></a>01821 j = 0; <a name="l01822"></a>01822 <span class="keywordflow">do</span> <a name="l01823"></a>01823 { <a name="l01824"></a>01824 currReader = &rgReaderStates[j]; <a name="l01825"></a>01825 <a name="l01826"></a>01826 <span class="comment">/* Ignore for IGNORED readers */</span> <a name="l01827"></a>01827 <span class="keywordflow">if</span> (!(currReader->dwCurrentState & <a class="code" href="pcsclite_8h.html#a0bfdd63120b09223fd4fed4884cbfeec" title="Ignore this reader.">SCARD_STATE_IGNORE</a>)) <a name="l01828"></a>01828 { <a name="l01829"></a>01829 <span class="keyword">const</span> <span class="keywordtype">char</span> *readerName; <a name="l01830"></a>01830 <span class="keywordtype">int</span> i; <a name="l01831"></a>01831 <a name="l01832"></a>01832 <span class="comment">/* Looks for correct readernames */</span> <a name="l01833"></a>01833 readerName = currReader->szReader; <a name="l01834"></a>01834 <span class="keywordflow">for</span> (i = 0; i < <a class="code" href="pcsclite_8h.html#af750e6e22c809de2b523c17e4a092036" title="Maximum readers context (a slot is count as a reader)">PCSCLITE_MAX_READERS_CONTEXTS</a>; i++) <a name="l01835"></a>01835 { <a name="l01836"></a>01836 <span class="keywordflow">if</span> (strcmp(readerName, readerStates[i].readerName) == 0) <a name="l01837"></a>01837 <span class="keywordflow">break</span>; <a name="l01838"></a>01838 } <a name="l01839"></a>01839 <a name="l01840"></a>01840 <span class="comment">/* The requested reader name is not recognized */</span> <a name="l01841"></a>01841 <span class="keywordflow">if</span> (i == PCSCLITE_MAX_READERS_CONTEXTS) <a name="l01842"></a>01842 { <a name="l01843"></a>01843 <span class="comment">/* PnP special reader? */</span> <a name="l01844"></a>01844 <span class="keywordflow">if</span> (strcasecmp(readerName, <span class="stringliteral">"\\\\?PnP?\\Notification"</span>) == 0) <a name="l01845"></a>01845 { <a name="l01846"></a>01846 <span class="keywordtype">int</span> k, newReaderCount = 0; <a name="l01847"></a>01847 <a name="l01848"></a>01848 <span class="keywordflow">for</span> (k=0; k < <a class="code" href="pcsclite_8h.html#af750e6e22c809de2b523c17e4a092036" title="Maximum readers context (a slot is count as a reader)">PCSCLITE_MAX_READERS_CONTEXTS</a>; k++) <a name="l01849"></a>01849 <span class="keywordflow">if</span> (readerStates[k].readerName[0] != <span class="charliteral">'\0'</span>) <a name="l01850"></a>01850 newReaderCount++; <a name="l01851"></a>01851 <a name="l01852"></a>01852 <span class="keywordflow">if</span> (newReaderCount != currentReaderCount) <a name="l01853"></a>01853 { <a name="l01854"></a>01854 Log1(PCSC_LOG_INFO, <span class="stringliteral">"Reader list changed"</span>); <a name="l01855"></a>01855 currentReaderCount = newReaderCount; <a name="l01856"></a>01856 <a name="l01857"></a>01857 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l01858"></a>01858 dwBreakFlag = 1; <a name="l01859"></a>01859 } <a name="l01860"></a>01860 } <a name="l01861"></a>01861 <span class="keywordflow">else</span> <a name="l01862"></a>01862 { <a name="l01863"></a>01863 currReader->dwEventState = <a name="l01864"></a>01864 <a class="code" href="pcsclite_8h.html#a96ab0fac1bb56525c5f216428e7dd8e7" title="Reader unknown.">SCARD_STATE_UNKNOWN</a> | <a class="code" href="pcsclite_8h.html#a781e4a27c9512c19935e3addaeaa152f" title="Status unavailable.">SCARD_STATE_UNAVAILABLE</a>; <a name="l01865"></a>01865 <span class="keywordflow">if</span> (!(currReader->dwCurrentState & <a class="code" href="pcsclite_8h.html#a96ab0fac1bb56525c5f216428e7dd8e7" title="Reader unknown.">SCARD_STATE_UNKNOWN</a>)) <a name="l01866"></a>01866 { <a name="l01867"></a>01867 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l01868"></a>01868 <span class="comment">/*</span> <a name="l01869"></a>01869 <span class="comment"> * Spec says use SCARD_STATE_IGNORE but a removed USB</span> <a name="l01870"></a>01870 <span class="comment"> * reader with eventState fed into currentState will</span> <a name="l01871"></a>01871 <span class="comment"> * be ignored forever</span> <a name="l01872"></a>01872 <span class="comment"> */</span> <a name="l01873"></a>01873 dwBreakFlag = 1; <a name="l01874"></a>01874 } <a name="l01875"></a>01875 } <a name="l01876"></a>01876 } <a name="l01877"></a>01877 <span class="keywordflow">else</span> <a name="l01878"></a>01878 { <a name="l01879"></a>01879 uint32_t readerState; <a name="l01880"></a>01880 <a name="l01881"></a>01881 <span class="comment">/* The reader has come back after being away */</span> <a name="l01882"></a>01882 <span class="keywordflow">if</span> (currReader->dwCurrentState & <a class="code" href="pcsclite_8h.html#a96ab0fac1bb56525c5f216428e7dd8e7" title="Reader unknown.">SCARD_STATE_UNKNOWN</a>) <a name="l01883"></a>01883 { <a name="l01884"></a>01884 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l01885"></a>01885 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#a96ab0fac1bb56525c5f216428e7dd8e7" title="Reader unknown.">SCARD_STATE_UNKNOWN</a>; <a name="l01886"></a>01886 Log0(PCSC_LOG_DEBUG); <a name="l01887"></a>01887 dwBreakFlag = 1; <a name="l01888"></a>01888 } <a name="l01889"></a>01889 <a name="l01890"></a>01890 <span class="comment">/* Set the reader status structure */</span> <a name="l01891"></a>01891 rContext = &readerStates[i]; <a name="l01892"></a>01892 <a name="l01893"></a>01893 <span class="comment">/* Now we check all the Reader States */</span> <a name="l01894"></a>01894 readerState = rContext-><a class="code" href="structpubReaderStatesList.html#ae0bfa9f9998463192d8c18cf86915082" title="SCARD_* bit field.">readerState</a>; <a name="l01895"></a>01895 <a name="l01896"></a>01896 <span class="comment">/* only if current state has an non null event counter */</span> <a name="l01897"></a>01897 <span class="keywordflow">if</span> (currReader->dwCurrentState & 0xFFFF0000) <a name="l01898"></a>01898 { <a name="l01899"></a>01899 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> currentCounter; <a name="l01900"></a>01900 <a name="l01901"></a>01901 currentCounter = (currReader->dwCurrentState >> 16) & 0xFFFF; <a name="l01902"></a>01902 <a name="l01903"></a>01903 <span class="comment">/* has the event counter changed since the last call? */</span> <a name="l01904"></a>01904 <span class="keywordflow">if</span> (rContext-><a class="code" href="structpubReaderStatesList.html#a97623f9f724a2a86db3e755badd0cf21" title="number of card events">eventCounter</a> != currentCounter) <a name="l01905"></a>01905 { <a name="l01906"></a>01906 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l01907"></a>01907 Log0(PCSC_LOG_DEBUG); <a name="l01908"></a>01908 dwBreakFlag = 1; <a name="l01909"></a>01909 } <a name="l01910"></a>01910 } <a name="l01911"></a>01911 <a name="l01912"></a>01912 <span class="comment">/* add an event counter in the upper word of dwEventState */</span> <a name="l01913"></a>01913 currReader->dwEventState = ((currReader->dwEventState & 0xffff ) <a name="l01914"></a>01914 | (rContext-><a class="code" href="structpubReaderStatesList.html#a97623f9f724a2a86db3e755badd0cf21" title="number of card events">eventCounter</a> << 16)); <a name="l01915"></a>01915 <a name="l01916"></a>01916 <span class="comment">/* Check if the reader is in the correct state */</span> <a name="l01917"></a>01917 <span class="keywordflow">if</span> (readerState & <a class="code" href="pcsclite_8h.html#a91be8317d47fab4b3eac201850fa917f" title="Unknown state.">SCARD_UNKNOWN</a>) <a name="l01918"></a>01918 { <a name="l01919"></a>01919 <span class="comment">/* reader is in bad state */</span> <a name="l01920"></a>01920 currReader->dwEventState = <a class="code" href="pcsclite_8h.html#a781e4a27c9512c19935e3addaeaa152f" title="Status unavailable.">SCARD_STATE_UNAVAILABLE</a>; <a name="l01921"></a>01921 <span class="keywordflow">if</span> (!(currReader->dwCurrentState & <a class="code" href="pcsclite_8h.html#a781e4a27c9512c19935e3addaeaa152f" title="Status unavailable.">SCARD_STATE_UNAVAILABLE</a>)) <a name="l01922"></a>01922 { <a name="l01923"></a>01923 <span class="comment">/* App thinks reader is in good state and it is not */</span> <a name="l01924"></a>01924 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l01925"></a>01925 Log0(PCSC_LOG_DEBUG); <a name="l01926"></a>01926 dwBreakFlag = 1; <a name="l01927"></a>01927 } <a name="l01928"></a>01928 } <a name="l01929"></a>01929 <span class="keywordflow">else</span> <a name="l01930"></a>01930 { <a name="l01931"></a>01931 <span class="comment">/* App thinks reader in bad state but it is not */</span> <a name="l01932"></a>01932 <span class="keywordflow">if</span> (currReader-> dwCurrentState & <a class="code" href="pcsclite_8h.html#a781e4a27c9512c19935e3addaeaa152f" title="Status unavailable.">SCARD_STATE_UNAVAILABLE</a>) <a name="l01933"></a>01933 { <a name="l01934"></a>01934 currReader->dwEventState &= ~SCARD_STATE_UNAVAILABLE; <a name="l01935"></a>01935 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l01936"></a>01936 Log0(PCSC_LOG_DEBUG); <a name="l01937"></a>01937 dwBreakFlag = 1; <a name="l01938"></a>01938 } <a name="l01939"></a>01939 } <a name="l01940"></a>01940 <a name="l01941"></a>01941 <span class="comment">/* Check for card presence in the reader */</span> <a name="l01942"></a>01942 <span class="keywordflow">if</span> (readerState & <a class="code" href="pcsclite_8h.html#a4d99b1aacfcda4e1a36198f5668ae520" title="Card is present.">SCARD_PRESENT</a>) <a name="l01943"></a>01943 { <a name="l01944"></a>01944 <span class="comment">/* card present but not yet powered up */</span> <a name="l01945"></a>01945 <span class="keywordflow">if</span> (0 == rContext-><a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a>) <a name="l01946"></a>01946 <span class="comment">/* Allow the status thread to convey information */</span> <a name="l01947"></a>01947 (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> + 10); <a name="l01948"></a>01948 <a name="l01949"></a>01949 currReader->cbAtr = rContext-><a class="code" href="structpubReaderStatesList.html#afe108cdd8d9d06f1294ba235303d59a7" title="ATR length.">cardAtrLength</a>; <a name="l01950"></a>01950 memcpy(currReader->rgbAtr, rContext-><a class="code" href="structpubReaderStatesList.html#a865f4e34f374b7b24b3a56008f92676f" title="ATR.">cardAtr</a>, <a name="l01951"></a>01951 currReader->cbAtr); <a name="l01952"></a>01952 } <a name="l01953"></a>01953 <span class="keywordflow">else</span> <a name="l01954"></a>01954 currReader->cbAtr = 0; <a name="l01955"></a>01955 <a name="l01956"></a>01956 <span class="comment">/* Card is now absent */</span> <a name="l01957"></a>01957 <span class="keywordflow">if</span> (readerState & <a class="code" href="pcsclite_8h.html#a8108c39f71de4ea806b0a411eb9906ab" title="Card is absent.">SCARD_ABSENT</a>) <a name="l01958"></a>01958 { <a name="l01959"></a>01959 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#aadf7badda42d372c2bc56d95a7720535" title="Card removed.">SCARD_STATE_EMPTY</a>; <a name="l01960"></a>01960 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#a78d2639a9716b6b27f4d516e43470f8f" title="Card inserted.">SCARD_STATE_PRESENT</a>; <a name="l01961"></a>01961 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#ae73c6e4ff31781d4358b93bbde952cfe" title="App wants status.">SCARD_STATE_UNAWARE</a>; <a name="l01962"></a>01962 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#a0bfdd63120b09223fd4fed4884cbfeec" title="Ignore this reader.">SCARD_STATE_IGNORE</a>; <a name="l01963"></a>01963 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#a96ab0fac1bb56525c5f216428e7dd8e7" title="Reader unknown.">SCARD_STATE_UNKNOWN</a>; <a name="l01964"></a>01964 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#a781e4a27c9512c19935e3addaeaa152f" title="Status unavailable.">SCARD_STATE_UNAVAILABLE</a>; <a name="l01965"></a>01965 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#ab294bb6ccdc6bb035473a3f01697ed12" title="ATR matches card.">SCARD_STATE_ATRMATCH</a>; <a name="l01966"></a>01966 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#aba849aaf01b436d511218b18d96caf83" title="Unresponsive card.">SCARD_STATE_MUTE</a>; <a name="l01967"></a>01967 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#a56b01a133df54d0ca651b1541a7f11d5" title="Shared Mode.">SCARD_STATE_INUSE</a>; <a name="l01968"></a>01968 <a name="l01969"></a>01969 <span class="comment">/* After present the rest are assumed */</span> <a name="l01970"></a>01970 <span class="keywordflow">if</span> (currReader->dwCurrentState & <a class="code" href="pcsclite_8h.html#a78d2639a9716b6b27f4d516e43470f8f" title="Card inserted.">SCARD_STATE_PRESENT</a>) <a name="l01971"></a>01971 { <a name="l01972"></a>01972 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l01973"></a>01973 Log0(PCSC_LOG_DEBUG); <a name="l01974"></a>01974 dwBreakFlag = 1; <a name="l01975"></a>01975 } <a name="l01976"></a>01976 } <a name="l01977"></a>01977 <span class="comment">/* Card is now present */</span> <a name="l01978"></a>01978 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (readerState & SCARD_PRESENT) <a name="l01979"></a>01979 { <a name="l01980"></a>01980 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#a78d2639a9716b6b27f4d516e43470f8f" title="Card inserted.">SCARD_STATE_PRESENT</a>; <a name="l01981"></a>01981 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#aadf7badda42d372c2bc56d95a7720535" title="Card removed.">SCARD_STATE_EMPTY</a>; <a name="l01982"></a>01982 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#ae73c6e4ff31781d4358b93bbde952cfe" title="App wants status.">SCARD_STATE_UNAWARE</a>; <a name="l01983"></a>01983 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#a0bfdd63120b09223fd4fed4884cbfeec" title="Ignore this reader.">SCARD_STATE_IGNORE</a>; <a name="l01984"></a>01984 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#a96ab0fac1bb56525c5f216428e7dd8e7" title="Reader unknown.">SCARD_STATE_UNKNOWN</a>; <a name="l01985"></a>01985 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#a781e4a27c9512c19935e3addaeaa152f" title="Status unavailable.">SCARD_STATE_UNAVAILABLE</a>; <a name="l01986"></a>01986 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#aba849aaf01b436d511218b18d96caf83" title="Unresponsive card.">SCARD_STATE_MUTE</a>; <a name="l01987"></a>01987 <a name="l01988"></a>01988 <span class="keywordflow">if</span> (currReader->dwCurrentState & <a class="code" href="pcsclite_8h.html#aadf7badda42d372c2bc56d95a7720535" title="Card removed.">SCARD_STATE_EMPTY</a>) <a name="l01989"></a>01989 { <a name="l01990"></a>01990 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l01991"></a>01991 Log0(PCSC_LOG_DEBUG); <a name="l01992"></a>01992 dwBreakFlag = 1; <a name="l01993"></a>01993 } <a name="l01994"></a>01994 <a name="l01995"></a>01995 <span class="keywordflow">if</span> (readerState & <a class="code" href="pcsclite_8h.html#a0feed8b3b5ce43d1411359a9b0a3d78c" title="Card not powered.">SCARD_SWALLOWED</a>) <a name="l01996"></a>01996 { <a name="l01997"></a>01997 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#aba849aaf01b436d511218b18d96caf83" title="Unresponsive card.">SCARD_STATE_MUTE</a>; <a name="l01998"></a>01998 <span class="keywordflow">if</span> (!(currReader->dwCurrentState & <a class="code" href="pcsclite_8h.html#aba849aaf01b436d511218b18d96caf83" title="Unresponsive card.">SCARD_STATE_MUTE</a>)) <a name="l01999"></a>01999 { <a name="l02000"></a>02000 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l02001"></a>02001 Log0(PCSC_LOG_DEBUG); <a name="l02002"></a>02002 dwBreakFlag = 1; <a name="l02003"></a>02003 } <a name="l02004"></a>02004 } <a name="l02005"></a>02005 <span class="keywordflow">else</span> <a name="l02006"></a>02006 { <a name="l02007"></a>02007 <span class="comment">/* App thinks card is mute but it is not */</span> <a name="l02008"></a>02008 <span class="keywordflow">if</span> (currReader->dwCurrentState & <a class="code" href="pcsclite_8h.html#aba849aaf01b436d511218b18d96caf83" title="Unresponsive card.">SCARD_STATE_MUTE</a>) <a name="l02009"></a>02009 { <a name="l02010"></a>02010 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l02011"></a>02011 Log0(PCSC_LOG_DEBUG); <a name="l02012"></a>02012 dwBreakFlag = 1; <a name="l02013"></a>02013 } <a name="l02014"></a>02014 } <a name="l02015"></a>02015 } <a name="l02016"></a>02016 <a name="l02017"></a>02017 <span class="comment">/* Now figure out sharing modes */</span> <a name="l02018"></a>02018 <span class="keywordflow">if</span> (rContext-><a class="code" href="structpubReaderStatesList.html#a3466b5e09f39d5857bf6a158a5bb7d30" title="PCSCLITE_SHARING_* sharing status.">readerSharing</a> == <a class="code" href="eventhandler_8h.html#a2adc2d505aee5e810d17d0e8127a4543" title="Reader used in exclusive mode.">PCSCLITE_SHARING_EXCLUSIVE_CONTEXT</a>) <a name="l02019"></a>02019 { <a name="l02020"></a>02020 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#ae371f8ad287d26aceb591537189f962a" title="Exclusive Mode.">SCARD_STATE_EXCLUSIVE</a>; <a name="l02021"></a>02021 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#a56b01a133df54d0ca651b1541a7f11d5" title="Shared Mode.">SCARD_STATE_INUSE</a>; <a name="l02022"></a>02022 <span class="keywordflow">if</span> (currReader->dwCurrentState & <a class="code" href="pcsclite_8h.html#a56b01a133df54d0ca651b1541a7f11d5" title="Shared Mode.">SCARD_STATE_INUSE</a>) <a name="l02023"></a>02023 { <a name="l02024"></a>02024 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l02025"></a>02025 Log0(PCSC_LOG_DEBUG); <a name="l02026"></a>02026 dwBreakFlag = 1; <a name="l02027"></a>02027 } <a name="l02028"></a>02028 } <a name="l02029"></a>02029 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (rContext-><a class="code" href="structpubReaderStatesList.html#a3466b5e09f39d5857bf6a158a5bb7d30" title="PCSCLITE_SHARING_* sharing status.">readerSharing</a> >= <a class="code" href="eventhandler_8h.html#a97a0f3217c43cc5d86b1d92cc73d0f63" title="One application is using the reader.">PCSCLITE_SHARING_LAST_CONTEXT</a>) <a name="l02030"></a>02030 { <a name="l02031"></a>02031 <span class="comment">/* A card must be inserted for it to be INUSE */</span> <a name="l02032"></a>02032 <span class="keywordflow">if</span> (readerState & SCARD_PRESENT) <a name="l02033"></a>02033 { <a name="l02034"></a>02034 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#a56b01a133df54d0ca651b1541a7f11d5" title="Shared Mode.">SCARD_STATE_INUSE</a>; <a name="l02035"></a>02035 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#ae371f8ad287d26aceb591537189f962a" title="Exclusive Mode.">SCARD_STATE_EXCLUSIVE</a>; <a name="l02036"></a>02036 <span class="keywordflow">if</span> (currReader-> dwCurrentState & <a class="code" href="pcsclite_8h.html#ae371f8ad287d26aceb591537189f962a" title="Exclusive Mode.">SCARD_STATE_EXCLUSIVE</a>) <a name="l02037"></a>02037 { <a name="l02038"></a>02038 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l02039"></a>02039 Log0(PCSC_LOG_DEBUG); <a name="l02040"></a>02040 dwBreakFlag = 1; <a name="l02041"></a>02041 } <a name="l02042"></a>02042 } <a name="l02043"></a>02043 } <a name="l02044"></a>02044 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (rContext-><a class="code" href="structpubReaderStatesList.html#a3466b5e09f39d5857bf6a158a5bb7d30" title="PCSCLITE_SHARING_* sharing status.">readerSharing</a> == <a class="code" href="eventhandler_8h.html#a631be0b7327a8f47f9073dcaacc18f98" title="No application is using the reader.">PCSCLITE_SHARING_NO_CONTEXT</a>) <a name="l02045"></a>02045 { <a name="l02046"></a>02046 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#a56b01a133df54d0ca651b1541a7f11d5" title="Shared Mode.">SCARD_STATE_INUSE</a>; <a name="l02047"></a>02047 currReader->dwEventState &= ~<a class="code" href="pcsclite_8h.html#ae371f8ad287d26aceb591537189f962a" title="Exclusive Mode.">SCARD_STATE_EXCLUSIVE</a>; <a name="l02048"></a>02048 <a name="l02049"></a>02049 <span class="keywordflow">if</span> (currReader->dwCurrentState & <a class="code" href="pcsclite_8h.html#a56b01a133df54d0ca651b1541a7f11d5" title="Shared Mode.">SCARD_STATE_INUSE</a>) <a name="l02050"></a>02050 { <a name="l02051"></a>02051 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l02052"></a>02052 Log0(PCSC_LOG_DEBUG); <a name="l02053"></a>02053 dwBreakFlag = 1; <a name="l02054"></a>02054 } <a name="l02055"></a>02055 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (currReader-> dwCurrentState <a name="l02056"></a>02056 & <a class="code" href="pcsclite_8h.html#ae371f8ad287d26aceb591537189f962a" title="Exclusive Mode.">SCARD_STATE_EXCLUSIVE</a>) <a name="l02057"></a>02057 { <a name="l02058"></a>02058 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l02059"></a>02059 Log0(PCSC_LOG_DEBUG); <a name="l02060"></a>02060 dwBreakFlag = 1; <a name="l02061"></a>02061 } <a name="l02062"></a>02062 } <a name="l02063"></a>02063 <a name="l02064"></a>02064 <span class="keywordflow">if</span> (currReader->dwCurrentState == <a class="code" href="pcsclite_8h.html#ae73c6e4ff31781d4358b93bbde952cfe" title="App wants status.">SCARD_STATE_UNAWARE</a>) <a name="l02065"></a>02065 { <a name="l02066"></a>02066 <span class="comment">/*</span> <a name="l02067"></a>02067 <span class="comment"> * Break out of the while .. loop and return status</span> <a name="l02068"></a>02068 <span class="comment"> * once all the status's for all readers is met</span> <a name="l02069"></a>02069 <span class="comment"> */</span> <a name="l02070"></a>02070 currReader->dwEventState |= <a class="code" href="pcsclite_8h.html#abd53085c631a1bade0fb5e974de90d48" title="State has changed.">SCARD_STATE_CHANGED</a>; <a name="l02071"></a>02071 Log0(PCSC_LOG_DEBUG); <a name="l02072"></a>02072 dwBreakFlag = 1; <a name="l02073"></a>02073 } <a name="l02074"></a>02074 } <span class="comment">/* End of SCARD_STATE_UNKNOWN */</span> <a name="l02075"></a>02075 } <span class="comment">/* End of SCARD_STATE_IGNORE */</span> <a name="l02076"></a>02076 <a name="l02077"></a>02077 <span class="comment">/* Counter and resetter */</span> <a name="l02078"></a>02078 j++; <a name="l02079"></a>02079 <span class="keywordflow">if</span> (j == cReaders) <a name="l02080"></a>02080 { <a name="l02081"></a>02081 <span class="comment">/* go back to the first reader */</span> <a name="l02082"></a>02082 j = 0; <a name="l02083"></a>02083 <a name="l02084"></a>02084 <span class="comment">/* Declare all the break conditions */</span> <a name="l02085"></a>02085 <a name="l02086"></a>02086 <span class="comment">/* Break if UNAWARE is set and all readers have been checked */</span> <a name="l02087"></a>02087 <span class="keywordflow">if</span> (dwBreakFlag == 1) <a name="l02088"></a>02088 <span class="keywordflow">break</span>; <a name="l02089"></a>02089 <a name="l02090"></a>02090 <span class="comment">/* Only sleep once for each cycle of reader checks. */</span> <a name="l02091"></a>02091 { <a name="l02092"></a>02092 <span class="keyword">struct </span><a class="code" href="structwait__reader__state__change.html" title="Information contained in CMD_WAIT_READER_STATE_CHANGE Messages.">wait_reader_state_change</a> waitStatusStruct; <a name="l02093"></a>02093 <span class="keyword">struct </span>timeval before, after; <a name="l02094"></a>02094 <a name="l02095"></a>02095 gettimeofday(&before, NULL); <a name="l02096"></a>02096 <a name="l02097"></a>02097 waitStatusStruct.<a class="code" href="structwait__reader__state__change.html#aeda15b3486c6a4026e4ff19c7bddd8b3" title="timeout in ms">timeOut</a> = dwTime; <a name="l02098"></a>02098 waitStatusStruct.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="l02099"></a>02099 <a name="l02100"></a>02100 <span class="comment">/* another thread can do SCardCancel() */</span> <a name="l02101"></a>02101 currentContextMap->cancellable = TRUE; <a name="l02102"></a>02102 <a name="l02103"></a>02103 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca7afd6740fc03a9c4395de07b6b29ad40" title="wait for a reader state change">CMD_WAIT_READER_STATE_CHANGE</a>, <a name="l02104"></a>02104 currentContextMap->dwClientID, <a name="l02105"></a>02105 <span class="keyword">sizeof</span>(waitStatusStruct), &waitStatusStruct); <a name="l02106"></a>02106 <a name="l02107"></a>02107 <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="l02108"></a>02108 <span class="keywordflow">goto</span> end; <a name="l02109"></a>02109 <a name="l02110"></a>02110 <span class="comment">/*</span> <a name="l02111"></a>02111 <span class="comment"> * Read a message from the server</span> <a name="l02112"></a>02112 <span class="comment"> */</span> <a name="l02113"></a>02113 rv = <a class="code" href="winscard__msg_8c.html#a882edc5b2e3fe0d8a70a4ffd00d8455c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceiveTimeout</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca7afd6740fc03a9c4395de07b6b29ad40" title="wait for a reader state change">CMD_WAIT_READER_STATE_CHANGE</a>, <a name="l02114"></a>02114 &waitStatusStruct, <span class="keyword">sizeof</span>(waitStatusStruct), <a name="l02115"></a>02115 currentContextMap->dwClientID, dwTime); <a name="l02116"></a>02116 <a name="l02117"></a>02117 <span class="comment">/* another thread can do SCardCancel() */</span> <a name="l02118"></a>02118 currentContextMap->cancellable = FALSE; <a name="l02119"></a>02119 <a name="l02120"></a>02120 <span class="comment">/* timeout */</span> <a name="l02121"></a>02121 <span class="keywordflow">if</span> (<a class="code" href="pcsclite_8h.html#aa01265b3f55d4f18cdeb4f9cd736b508" title="The user-specified timeout value has expired.">SCARD_E_TIMEOUT</a> == rv) <a name="l02122"></a>02122 { <a name="l02123"></a>02123 <span class="comment">/* ask server to remove us from the event list */</span> <a name="l02124"></a>02124 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca0e6dde8afb7713e6973888b536ec7d79" title="stop waiting for a reader state change">CMD_STOP_WAITING_READER_STATE_CHANGE</a>, <a name="l02125"></a>02125 currentContextMap->dwClientID, <a name="l02126"></a>02126 <span class="keyword">sizeof</span>(waitStatusStruct), &waitStatusStruct); <a name="l02127"></a>02127 <a name="l02128"></a>02128 <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="l02129"></a>02129 <span class="keywordflow">goto</span> end; <a name="l02130"></a>02130 <a name="l02131"></a>02131 <span class="comment">/* Read a message from the server */</span> <a name="l02132"></a>02132 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&waitStatusStruct, <a name="l02133"></a>02133 <span class="keyword">sizeof</span>(waitStatusStruct), <a name="l02134"></a>02134 currentContextMap->dwClientID); <a name="l02135"></a>02135 <a name="l02136"></a>02136 <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="l02137"></a>02137 <span class="keywordflow">goto</span> end; <a name="l02138"></a>02138 } <a name="l02139"></a>02139 <a name="l02140"></a>02140 <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="l02141"></a>02141 <span class="keywordflow">goto</span> end; <a name="l02142"></a>02142 <a name="l02143"></a>02143 <span class="comment">/* an event occurs or SCardCancel() was called */</span> <a name="l02144"></a>02144 <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> != waitStatusStruct.rv) <a name="l02145"></a>02145 { <a name="l02146"></a>02146 rv = waitStatusStruct.rv; <a name="l02147"></a>02147 <span class="keywordflow">goto</span> end; <a name="l02148"></a>02148 } <a name="l02149"></a>02149 <a name="l02150"></a>02150 <span class="comment">/* synchronize reader states with daemon */</span> <a name="l02151"></a>02151 rv = getReaderStates(currentContextMap); <a name="l02152"></a>02152 <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="l02153"></a>02153 <span class="keywordflow">goto</span> end; <a name="l02154"></a>02154 <a name="l02155"></a>02155 <span class="keywordflow">if</span> (<a class="code" href="pcsclite_8h.html#aa84a29002ab81c719c0d07bb446296e0" title="Infinite timeout.">INFINITE</a> != dwTimeout) <a name="l02156"></a>02156 { <a name="l02157"></a>02157 <span class="keywordtype">long</span> <span class="keywordtype">int</span> diff; <a name="l02158"></a>02158 <a name="l02159"></a>02159 gettimeofday(&after, NULL); <a name="l02160"></a>02160 diff = <a class="code" href="utils_8c.html#a470f4ac4abe231a7b3346be4fe455912" title="return the difference (as long int) in µs between 2 struct timeval r = a - b">time_sub</a>(&after, &before); <a name="l02161"></a>02161 dwTime -= diff/1000; <a name="l02162"></a>02162 } <a name="l02163"></a>02163 } <a name="l02164"></a>02164 <a name="l02165"></a>02165 <span class="keywordflow">if</span> (dwTimeout != <a class="code" href="pcsclite_8h.html#aa84a29002ab81c719c0d07bb446296e0" title="Infinite timeout.">INFINITE</a>) <a name="l02166"></a>02166 { <a name="l02167"></a>02167 <span class="comment">/* If time is greater than timeout and all readers have been</span> <a name="l02168"></a>02168 <span class="comment"> * checked</span> <a name="l02169"></a>02169 <span class="comment"> */</span> <a name="l02170"></a>02170 <span class="keywordflow">if</span> (dwTime <= 0) <a name="l02171"></a>02171 { <a name="l02172"></a>02172 rv = <a class="code" href="pcsclite_8h.html#aa01265b3f55d4f18cdeb4f9cd736b508" title="The user-specified timeout value has expired.">SCARD_E_TIMEOUT</a>; <a name="l02173"></a>02173 <span class="keywordflow">goto</span> end; <a name="l02174"></a>02174 } <a name="l02175"></a>02175 } <a name="l02176"></a>02176 } <a name="l02177"></a>02177 } <a name="l02178"></a>02178 <span class="keywordflow">while</span> (1); <a name="l02179"></a>02179 <a name="l02180"></a>02180 end: <a name="l02181"></a>02181 Log1(PCSC_LOG_DEBUG, <span class="stringliteral">"Event Loop End"</span>); <a name="l02182"></a>02182 <a name="l02183"></a>02183 (void)pthread_mutex_unlock(currentContextMap->mMutex); <a name="l02184"></a>02184 <a name="l02185"></a>02185 error: <a name="l02186"></a>02186 PROFILE_END(rv) <a name="l02187"></a>02187 <span class="preprocessor">#ifdef DO_TRACE</span> <a name="l02188"></a>02188 <span class="preprocessor"></span> <span class="keywordflow">for</span> (j=0; j<cReaders; j++) <a name="l02189"></a>02189 { <a name="l02190"></a>02190 API_TRACE_OUT(<span class="stringliteral">"[%d] %s %X %X"</span>, j, rgReaderStates[j].szReader, <a name="l02191"></a>02191 rgReaderStates[j].dwCurrentState, rgReaderStates[j].dwEventState) <a name="l02192"></a>02192 } <a name="l02193"></a>02193 <span class="preprocessor">#endif</span> <a name="l02194"></a>02194 <span class="preprocessor"></span> <a name="l02195"></a>02195 <span class="keywordflow">return</span> rv; <a name="l02196"></a>02196 } <a name="l02197"></a>02197 <a name="l02251"></a><a class="code" href="group__API.html#gac3454d4657110fd7f753b2d3d8f4e32f">02251</a> LONG <a class="code" href="group__API.html#gac3454d4657110fd7f753b2d3d8f4e32f" title="Sends a command directly to the IFD Handler (reader driver) to be processed by the reader...">SCardControl</a>(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, DWORD dwControlCode, LPCVOID pbSendBuffer, <a name="l02252"></a>02252 DWORD cbSendLength, LPVOID pbRecvBuffer, DWORD cbRecvLength, <a name="l02253"></a>02253 LPDWORD lpBytesReturned) <a name="l02254"></a>02254 { <a name="l02255"></a>02255 LONG rv; <a name="l02256"></a>02256 <span class="keyword">struct </span><a class="code" href="structcontrol__struct.html" title="contained in SCARD_CONTROL Messages.">control_struct</a> scControlStruct; <a name="l02257"></a>02257 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l02258"></a>02258 <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * pChannelMap; <a name="l02259"></a>02259 <a name="l02260"></a>02260 PROFILE_START <a name="l02261"></a>02261 <a name="l02262"></a>02262 <span class="comment">/* 0 bytes received by default */</span> <a name="l02263"></a>02263 <span class="keywordflow">if</span> (NULL != lpBytesReturned) <a name="l02264"></a>02264 *lpBytesReturned = 0; <a name="l02265"></a>02265 <a name="l02266"></a>02266 CHECK_SAME_PROCESS <a name="l02267"></a>02267 <a name="l02268"></a>02268 <span class="comment">/*</span> <a name="l02269"></a>02269 <span class="comment"> * Make sure this handle has been opened</span> <a name="l02270"></a>02270 <span class="comment"> */</span> <a name="l02271"></a>02271 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l02272"></a>02272 &pChannelMap); <a name="l02273"></a>02273 <span class="keywordflow">if</span> (rv == -1) <a name="l02274"></a>02274 { <a name="l02275"></a>02275 PROFILE_END(<a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>) <a name="l02276"></a>02276 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l02277"></a>02277 } <a name="l02278"></a>02278 <a name="l02279"></a>02279 (void)pthread_mutex_lock(currentContextMap->mMutex); <a name="l02280"></a>02280 <a name="l02281"></a>02281 <span class="comment">/* check the handle is still valid */</span> <a name="l02282"></a>02282 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l02283"></a>02283 &pChannelMap); <a name="l02284"></a>02284 <span class="keywordflow">if</span> (rv == -1) <a name="l02285"></a>02285 <span class="comment">/* the handle is now invalid</span> <a name="l02286"></a>02286 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l02287"></a>02287 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l02288"></a>02288 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l02289"></a>02289 <a name="l02290"></a>02290 <span class="keywordflow">if</span> ((cbSendLength > <a class="code" href="pcsclite_8h.html#ae128d4b6df34e2dfdb6216b31bae2cc6" title="enhanced (64K + APDU + Lc + Le) Tx/Rx Buffer">MAX_BUFFER_SIZE_EXTENDED</a>) <a name="l02291"></a>02291 || (cbRecvLength > <a class="code" href="pcsclite_8h.html#ae128d4b6df34e2dfdb6216b31bae2cc6" title="enhanced (64K + APDU + Lc + Le) Tx/Rx Buffer">MAX_BUFFER_SIZE_EXTENDED</a>)) <a name="l02292"></a>02292 { <a name="l02293"></a>02293 rv = <a class="code" href="pcsclite_8h.html#aac3f8d945f8a0e667e0926daaa18dc57" title="The data buffer to receive returned data is too small for the returned data.">SCARD_E_INSUFFICIENT_BUFFER</a>; <a name="l02294"></a>02294 <span class="keywordflow">goto</span> end; <a name="l02295"></a>02295 } <a name="l02296"></a>02296 <a name="l02297"></a>02297 scControlStruct.hCard = hCard; <a name="l02298"></a>02298 scControlStruct.dwControlCode = dwControlCode; <a name="l02299"></a>02299 scControlStruct.cbSendLength = cbSendLength; <a name="l02300"></a>02300 scControlStruct.cbRecvLength = cbRecvLength; <a name="l02301"></a>02301 scControlStruct.dwBytesReturned = 0; <a name="l02302"></a>02302 scControlStruct.rv = 0; <a name="l02303"></a>02303 <a name="l02304"></a>02304 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca50f40e02c69625cb2e03e7094ce9d8b1" title="used by SCardControl()">SCARD_CONTROL</a>, currentContextMap->dwClientID, <a name="l02305"></a>02305 <span class="keyword">sizeof</span>(scControlStruct), &scControlStruct); <a name="l02306"></a>02306 <a name="l02307"></a>02307 <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="l02308"></a>02308 <span class="keywordflow">goto</span> end; <a name="l02309"></a>02309 <a name="l02310"></a>02310 <span class="comment">/* write the sent buffer */</span> <a name="l02311"></a>02311 rv = <a class="code" href="winscard__msg_8c.html#aacd09139ce5667d3b651de29c0d62057" title="Sends a menssage from client to server or vice-versa.">MessageSend</a>((<span class="keywordtype">char</span> *)pbSendBuffer, cbSendLength, <a name="l02312"></a>02312 currentContextMap->dwClientID); <a name="l02313"></a>02313 <a name="l02314"></a>02314 <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="l02315"></a>02315 <span class="keywordflow">goto</span> end; <a name="l02316"></a>02316 <a name="l02317"></a>02317 <span class="comment">/*</span> <a name="l02318"></a>02318 <span class="comment"> * Read a message from the server</span> <a name="l02319"></a>02319 <span class="comment"> */</span> <a name="l02320"></a>02320 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&scControlStruct, <span class="keyword">sizeof</span>(scControlStruct), <a name="l02321"></a>02321 currentContextMap->dwClientID); <a name="l02322"></a>02322 <a name="l02323"></a>02323 <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="l02324"></a>02324 <span class="keywordflow">goto</span> end; <a name="l02325"></a>02325 <a name="l02326"></a>02326 <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> == scControlStruct.rv) <a name="l02327"></a>02327 { <a name="l02328"></a>02328 <span class="comment">/* read the received buffer */</span> <a name="l02329"></a>02329 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(pbRecvBuffer, scControlStruct.dwBytesReturned, <a name="l02330"></a>02330 currentContextMap->dwClientID); <a name="l02331"></a>02331 <a name="l02332"></a>02332 <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="l02333"></a>02333 <span class="keywordflow">goto</span> end; <a name="l02334"></a>02334 <a name="l02335"></a>02335 } <a name="l02336"></a>02336 <a name="l02337"></a>02337 <span class="keywordflow">if</span> (NULL != lpBytesReturned) <a name="l02338"></a>02338 *lpBytesReturned = scControlStruct.dwBytesReturned; <a name="l02339"></a>02339 <a name="l02340"></a>02340 rv = scControlStruct.rv; <a name="l02341"></a>02341 <a name="l02342"></a>02342 end: <a name="l02343"></a>02343 (void)pthread_mutex_unlock(currentContextMap->mMutex); <a name="l02344"></a>02344 <a name="l02345"></a>02345 PROFILE_END(rv) <a name="l02346"></a>02346 <a name="l02347"></a>02347 <span class="keywordflow">return</span> rv; <a name="l02348"></a>02348 } <a name="l02349"></a>02349 <a name="l02454"></a><a class="code" href="group__API.html#gaacfec51917255b7a25b94c5104961602">02454</a> LONG <a class="code" href="group__API.html#gaacfec51917255b7a25b94c5104961602" title="Get an attribute from the IFD Handler (reader driver).">SCardGetAttrib</a>(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, DWORD dwAttrId, LPBYTE pbAttr, <a name="l02455"></a>02455 LPDWORD pcbAttrLen) <a name="l02456"></a>02456 { <a name="l02457"></a>02457 LONG ret; <a name="l02458"></a>02458 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *buf = NULL; <a name="l02459"></a>02459 <a name="l02460"></a>02460 PROFILE_START <a name="l02461"></a>02461 <a name="l02462"></a>02462 <span class="keywordflow">if</span> (NULL == pcbAttrLen) <a name="l02463"></a>02463 { <a name="l02464"></a>02464 ret = <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l02465"></a>02465 <span class="keywordflow">goto</span> end; <a name="l02466"></a>02466 } <a name="l02467"></a>02467 <a name="l02468"></a>02468 <span class="keywordflow">if</span> (<a class="code" href="pcsclite_8h.html#a67db0943aeb13f9f468eaae39b5ff1b0" title="see SCardFreeMemory()">SCARD_AUTOALLOCATE</a> == *pcbAttrLen) <a name="l02469"></a>02469 { <a name="l02470"></a>02470 <span class="keywordflow">if</span> (NULL == pbAttr) <a name="l02471"></a>02471 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l02472"></a>02472 <a name="l02473"></a>02473 *pcbAttrLen = <a class="code" href="pcsclite_8h.html#ad4d796b98c583d49e83adabd74a63bf6" title="Maximum Tx/Rx Buffer for short APDU.">MAX_BUFFER_SIZE</a>; <a name="l02474"></a>02474 buf = malloc(*pcbAttrLen); <a name="l02475"></a>02475 <span class="keywordflow">if</span> (NULL == buf) <a name="l02476"></a>02476 { <a name="l02477"></a>02477 ret = <a class="code" href="pcsclite_8h.html#a81694393a1494d5091145734a2f0b36c" title="Not enough memory available to complete this command.">SCARD_E_NO_MEMORY</a>; <a name="l02478"></a>02478 <span class="keywordflow">goto</span> end; <a name="l02479"></a>02479 } <a name="l02480"></a>02480 <a name="l02481"></a>02481 *(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> **)pbAttr = buf; <a name="l02482"></a>02482 } <a name="l02483"></a>02483 <span class="keywordflow">else</span> <a name="l02484"></a>02484 { <a name="l02485"></a>02485 buf = pbAttr; <a name="l02486"></a>02486 <a name="l02487"></a>02487 <span class="comment">/* if only get the length */</span> <a name="l02488"></a>02488 <span class="keywordflow">if</span> (NULL == pbAttr) <a name="l02489"></a>02489 <span class="comment">/* use a reasonable size */</span> <a name="l02490"></a>02490 *pcbAttrLen = <a class="code" href="pcsclite_8h.html#ad4d796b98c583d49e83adabd74a63bf6" title="Maximum Tx/Rx Buffer for short APDU.">MAX_BUFFER_SIZE</a>; <a name="l02491"></a>02491 } <a name="l02492"></a>02492 <a name="l02493"></a>02493 ret = SCardGetSetAttrib(hCard, <a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca9198ad4e3cd0613906124c5243e7c12e" title="used by SCardGetAttrib()">SCARD_GET_ATTRIB</a>, dwAttrId, buf, <a name="l02494"></a>02494 pcbAttrLen); <a name="l02495"></a>02495 <a name="l02496"></a>02496 end: <a name="l02497"></a>02497 PROFILE_END(ret) <a name="l02498"></a>02498 <a name="l02499"></a>02499 <span class="keywordflow">return</span> ret; <a name="l02500"></a>02500 } <a name="l02501"></a>02501 <a name="l02537"></a><a class="code" href="group__API.html#ga060f0038a4ddfd5dd2b8fadf3c3a2e4f">02537</a> LONG <a class="code" href="group__API.html#ga060f0038a4ddfd5dd2b8fadf3c3a2e4f" title="Set an attribute of the IFD Handler.">SCardSetAttrib</a>(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, DWORD dwAttrId, LPCBYTE pbAttr, <a name="l02538"></a>02538 DWORD cbAttrLen) <a name="l02539"></a>02539 { <a name="l02540"></a>02540 LONG ret; <a name="l02541"></a>02541 <a name="l02542"></a>02542 PROFILE_START <a name="l02543"></a>02543 <a name="l02544"></a>02544 <span class="keywordflow">if</span> (NULL == pbAttr || 0 == cbAttrLen) <a name="l02545"></a>02545 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l02546"></a>02546 <a name="l02547"></a>02547 ret = SCardGetSetAttrib(hCard, <a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca068f22e88bd4939797c3c359c9fbf075" title="used by SCardSetAttrib()">SCARD_SET_ATTRIB</a>, dwAttrId, (LPBYTE)pbAttr, <a name="l02548"></a>02548 &cbAttrLen); <a name="l02549"></a>02549 <a name="l02550"></a>02550 PROFILE_END(ret) <a name="l02551"></a>02551 <a name="l02552"></a>02552 <span class="keywordflow">return</span> ret; <a name="l02553"></a>02553 } <a name="l02554"></a>02554 <a name="l02555"></a>02555 <span class="keyword">static</span> LONG SCardGetSetAttrib(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, <span class="keywordtype">int</span> command, DWORD dwAttrId, <a name="l02556"></a>02556 LPBYTE pbAttr, LPDWORD pcbAttrLen) <a name="l02557"></a>02557 { <a name="l02558"></a>02558 LONG rv; <a name="l02559"></a>02559 <span class="keyword">struct </span><a class="code" href="structgetset__struct.html" title="contained in SCARD_GET_ATTRIB and Messages.">getset_struct</a> scGetSetStruct; <a name="l02560"></a>02560 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l02561"></a>02561 <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * pChannelMap; <a name="l02562"></a>02562 <a name="l02563"></a>02563 CHECK_SAME_PROCESS <a name="l02564"></a>02564 <a name="l02565"></a>02565 <span class="comment">/*</span> <a name="l02566"></a>02566 <span class="comment"> * Make sure this handle has been opened</span> <a name="l02567"></a>02567 <span class="comment"> */</span> <a name="l02568"></a>02568 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l02569"></a>02569 &pChannelMap); <a name="l02570"></a>02570 <span class="keywordflow">if</span> (rv == -1) <a name="l02571"></a>02571 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l02572"></a>02572 <a name="l02573"></a>02573 (void)pthread_mutex_lock(currentContextMap->mMutex); <a name="l02574"></a>02574 <a name="l02575"></a>02575 <span class="comment">/* check the handle is still valid */</span> <a name="l02576"></a>02576 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l02577"></a>02577 &pChannelMap); <a name="l02578"></a>02578 <span class="keywordflow">if</span> (rv == -1) <a name="l02579"></a>02579 <span class="comment">/* the handle is now invalid</span> <a name="l02580"></a>02580 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l02581"></a>02581 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l02582"></a>02582 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l02583"></a>02583 <a name="l02584"></a>02584 <span class="keywordflow">if</span> (*pcbAttrLen > <a class="code" href="pcsclite_8h.html#ad4d796b98c583d49e83adabd74a63bf6" title="Maximum Tx/Rx Buffer for short APDU.">MAX_BUFFER_SIZE</a>) <a name="l02585"></a>02585 { <a name="l02586"></a>02586 rv = <a class="code" href="pcsclite_8h.html#aac3f8d945f8a0e667e0926daaa18dc57" title="The data buffer to receive returned data is too small for the returned data.">SCARD_E_INSUFFICIENT_BUFFER</a>; <a name="l02587"></a>02587 <span class="keywordflow">goto</span> end; <a name="l02588"></a>02588 } <a name="l02589"></a>02589 <a name="l02590"></a>02590 scGetSetStruct.hCard = hCard; <a name="l02591"></a>02591 scGetSetStruct.dwAttrId = dwAttrId; <a name="l02592"></a>02592 scGetSetStruct.cbAttrLen = *pcbAttrLen; <a name="l02593"></a>02593 scGetSetStruct.rv = <a class="code" href="pcsclite_8h.html#ad4729ab109ff490285d2ad881c04bee8" title="The Smart card resource manager is not running.">SCARD_E_NO_SERVICE</a>; <a name="l02594"></a>02594 memset(scGetSetStruct.pbAttr, 0, <span class="keyword">sizeof</span>(scGetSetStruct.pbAttr)); <a name="l02595"></a>02595 <span class="keywordflow">if</span> (<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca068f22e88bd4939797c3c359c9fbf075" title="used by SCardSetAttrib()">SCARD_SET_ATTRIB</a> == command) <a name="l02596"></a>02596 memcpy(scGetSetStruct.pbAttr, pbAttr, *pcbAttrLen); <a name="l02597"></a>02597 <a name="l02598"></a>02598 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(command, currentContextMap->dwClientID, <a name="l02599"></a>02599 <span class="keyword">sizeof</span>(scGetSetStruct), &scGetSetStruct); <a name="l02600"></a>02600 <a name="l02601"></a>02601 <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="l02602"></a>02602 <span class="keywordflow">goto</span> end; <a name="l02603"></a>02603 <a name="l02604"></a>02604 <span class="comment">/*</span> <a name="l02605"></a>02605 <span class="comment"> * Read a message from the server</span> <a name="l02606"></a>02606 <span class="comment"> */</span> <a name="l02607"></a>02607 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&scGetSetStruct, <span class="keyword">sizeof</span>(scGetSetStruct), <a name="l02608"></a>02608 currentContextMap->dwClientID); <a name="l02609"></a>02609 <a name="l02610"></a>02610 <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="l02611"></a>02611 <span class="keywordflow">goto</span> end; <a name="l02612"></a>02612 <a name="l02613"></a>02613 <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> == scGetSetStruct.rv) && (<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca9198ad4e3cd0613906124c5243e7c12e" title="used by SCardGetAttrib()">SCARD_GET_ATTRIB</a> == command)) <a name="l02614"></a>02614 { <a name="l02615"></a>02615 <span class="comment">/*</span> <a name="l02616"></a>02616 <span class="comment"> * Copy and zero it so any secret information is not leaked</span> <a name="l02617"></a>02617 <span class="comment"> */</span> <a name="l02618"></a>02618 <span class="keywordflow">if</span> (*pcbAttrLen < scGetSetStruct.cbAttrLen) <a name="l02619"></a>02619 { <a name="l02620"></a>02620 scGetSetStruct.cbAttrLen = *pcbAttrLen; <a name="l02621"></a>02621 scGetSetStruct.rv = <a class="code" href="pcsclite_8h.html#aac3f8d945f8a0e667e0926daaa18dc57" title="The data buffer to receive returned data is too small for the returned data.">SCARD_E_INSUFFICIENT_BUFFER</a>; <a name="l02622"></a>02622 } <a name="l02623"></a>02623 <span class="keywordflow">else</span> <a name="l02624"></a>02624 *pcbAttrLen = scGetSetStruct.cbAttrLen; <a name="l02625"></a>02625 <a name="l02626"></a>02626 <span class="keywordflow">if</span> (pbAttr) <a name="l02627"></a>02627 memcpy(pbAttr, scGetSetStruct.pbAttr, scGetSetStruct.cbAttrLen); <a name="l02628"></a>02628 <a name="l02629"></a>02629 memset(scGetSetStruct.pbAttr, 0x00, <span class="keyword">sizeof</span>(scGetSetStruct.pbAttr)); <a name="l02630"></a>02630 } <a name="l02631"></a>02631 rv = scGetSetStruct.rv; <a name="l02632"></a>02632 <a name="l02633"></a>02633 end: <a name="l02634"></a>02634 (void)pthread_mutex_unlock(currentContextMap->mMutex); <a name="l02635"></a>02635 <a name="l02636"></a>02636 <span class="keywordflow">return</span> rv; <a name="l02637"></a>02637 } <a name="l02638"></a>02638 <a name="l02697"></a><a class="code" href="group__API.html#ga9a2d77242a271310269065e64633ab99">02697</a> LONG <a class="code" href="group__API.html#ga9a2d77242a271310269065e64633ab99" title="Sends an APDU to the smart card contained in the reader connected to by SCardConnect().">SCardTransmit</a>(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, <span class="keyword">const</span> <a class="code" href="structSCARD__IO__REQUEST.html" title="Protocol Control Information (PCI)">SCARD_IO_REQUEST</a> *pioSendPci, <a name="l02698"></a>02698 LPCBYTE pbSendBuffer, DWORD cbSendLength, <a name="l02699"></a>02699 <a class="code" href="structSCARD__IO__REQUEST.html" title="Protocol Control Information (PCI)">SCARD_IO_REQUEST</a> *pioRecvPci, LPBYTE pbRecvBuffer, <a name="l02700"></a>02700 LPDWORD pcbRecvLength) <a name="l02701"></a>02701 { <a name="l02702"></a>02702 LONG rv; <a name="l02703"></a>02703 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l02704"></a>02704 <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * pChannelMap; <a name="l02705"></a>02705 <span class="keyword">struct </span><a class="code" href="structtransmit__struct.html" title="contained in SCARD_TRANSMIT Messages.">transmit_struct</a> scTransmitStruct; <a name="l02706"></a>02706 <a name="l02707"></a>02707 PROFILE_START <a name="l02708"></a>02708 <a name="l02709"></a>02709 <span class="keywordflow">if</span> (pbSendBuffer == NULL || pbRecvBuffer == NULL || <a name="l02710"></a>02710 pcbRecvLength == NULL || pioSendPci == NULL) <a name="l02711"></a>02711 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l02712"></a>02712 <a name="l02713"></a>02713 CHECK_SAME_PROCESS <a name="l02714"></a>02714 <a name="l02715"></a>02715 <span class="comment">/*</span> <a name="l02716"></a>02716 <span class="comment"> * Make sure this handle has been opened</span> <a name="l02717"></a>02717 <span class="comment"> */</span> <a name="l02718"></a>02718 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l02719"></a>02719 &pChannelMap); <a name="l02720"></a>02720 <span class="keywordflow">if</span> (rv == -1) <a name="l02721"></a>02721 { <a name="l02722"></a>02722 *pcbRecvLength = 0; <a name="l02723"></a>02723 PROFILE_END(<a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>) <a name="l02724"></a>02724 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l02725"></a>02725 } <a name="l02726"></a>02726 <a name="l02727"></a>02727 (void)pthread_mutex_lock(currentContextMap-><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a>); <a name="l02728"></a>02728 <a name="l02729"></a>02729 <span class="comment">/* check the handle is still valid */</span> <a name="l02730"></a>02730 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l02731"></a>02731 &pChannelMap); <a name="l02732"></a>02732 <span class="keywordflow">if</span> (rv == -1) <a name="l02733"></a>02733 <span class="comment">/* the handle is now invalid</span> <a name="l02734"></a>02734 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l02735"></a>02735 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l02736"></a>02736 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l02737"></a>02737 <a name="l02738"></a>02738 <span class="keywordflow">if</span> ((cbSendLength > <a class="code" href="pcsclite_8h.html#ae128d4b6df34e2dfdb6216b31bae2cc6" title="enhanced (64K + APDU + Lc + Le) Tx/Rx Buffer">MAX_BUFFER_SIZE_EXTENDED</a>) <a name="l02739"></a>02739 || (*pcbRecvLength > <a class="code" href="pcsclite_8h.html#ae128d4b6df34e2dfdb6216b31bae2cc6" title="enhanced (64K + APDU + Lc + Le) Tx/Rx Buffer">MAX_BUFFER_SIZE_EXTENDED</a>)) <a name="l02740"></a>02740 { <a name="l02741"></a>02741 rv = <a class="code" href="pcsclite_8h.html#aac3f8d945f8a0e667e0926daaa18dc57" title="The data buffer to receive returned data is too small for the returned data.">SCARD_E_INSUFFICIENT_BUFFER</a>; <a name="l02742"></a>02742 <span class="keywordflow">goto</span> end; <a name="l02743"></a>02743 } <a name="l02744"></a>02744 <a name="l02745"></a>02745 <span class="comment">/* Retry loop for blocking behaviour */</span> <a name="l02746"></a>02746 retry: <a name="l02747"></a>02747 <a name="l02748"></a>02748 scTransmitStruct.hCard = hCard; <a name="l02749"></a>02749 scTransmitStruct.cbSendLength = cbSendLength; <a name="l02750"></a>02750 scTransmitStruct.pcbRecvLength = *pcbRecvLength; <a name="l02751"></a>02751 scTransmitStruct.ioSendPciProtocol = pioSendPci-><a class="code" href="structSCARD__IO__REQUEST.html#a06a981d5eb5bae479767d77641d50002" title="Protocol identifier.">dwProtocol</a>; <a name="l02752"></a>02752 scTransmitStruct.ioSendPciLength = pioSendPci-><a class="code" href="structSCARD__IO__REQUEST.html#a90993ce745546245b494a35a9559a083" title="Protocol Control Inf Length.">cbPciLength</a>; <a name="l02753"></a>02753 scTransmitStruct.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="l02754"></a>02754 <a name="l02755"></a>02755 <span class="keywordflow">if</span> (pioRecvPci) <a name="l02756"></a>02756 { <a name="l02757"></a>02757 scTransmitStruct.ioRecvPciProtocol = pioRecvPci-><a class="code" href="structSCARD__IO__REQUEST.html#a06a981d5eb5bae479767d77641d50002" title="Protocol identifier.">dwProtocol</a>; <a name="l02758"></a>02758 scTransmitStruct.ioRecvPciLength = pioRecvPci-><a class="code" href="structSCARD__IO__REQUEST.html#a90993ce745546245b494a35a9559a083" title="Protocol Control Inf Length.">cbPciLength</a>; <a name="l02759"></a>02759 } <a name="l02760"></a>02760 <span class="keywordflow">else</span> <a name="l02761"></a>02761 { <a name="l02762"></a>02762 scTransmitStruct.ioRecvPciProtocol = <a class="code" href="pcsclite_8h.html#ad3896343af44a54d232d407719abad95" title="IFD determines prot.">SCARD_PROTOCOL_ANY</a>; <a name="l02763"></a>02763 scTransmitStruct.ioRecvPciLength = <span class="keyword">sizeof</span>(<a class="code" href="structSCARD__IO__REQUEST.html" title="Protocol Control Information (PCI)">SCARD_IO_REQUEST</a>); <a name="l02764"></a>02764 } <a name="l02765"></a>02765 <a name="l02766"></a>02766 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dcaaa1c43a9cc7497df780b9e5a81a3abcb" title="used by SCardTransmit()">SCARD_TRANSMIT</a>, currentContextMap-><a class="code" href="struct__psContextMap.html#a4e3f21f7252b71833af88ba9f2864577" title="Client Connection ID.">dwClientID</a>, <a name="l02767"></a>02767 <span class="keyword">sizeof</span>(scTransmitStruct), (<span class="keywordtype">void</span> *) &scTransmitStruct); <a name="l02768"></a>02768 <a name="l02769"></a>02769 <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="l02770"></a>02770 <span class="keywordflow">goto</span> end; <a name="l02771"></a>02771 <a name="l02772"></a>02772 <span class="comment">/* write the sent buffer */</span> <a name="l02773"></a>02773 rv = <a class="code" href="winscard__msg_8c.html#aacd09139ce5667d3b651de29c0d62057" title="Sends a menssage from client to server or vice-versa.">MessageSend</a>((<span class="keywordtype">void</span> *)pbSendBuffer, cbSendLength, <a name="l02774"></a>02774 currentContextMap-><a class="code" href="struct__psContextMap.html#a4e3f21f7252b71833af88ba9f2864577" title="Client Connection ID.">dwClientID</a>); <a name="l02775"></a>02775 <a name="l02776"></a>02776 <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="l02777"></a>02777 <span class="keywordflow">goto</span> end; <a name="l02778"></a>02778 <a name="l02779"></a>02779 <span class="comment">/*</span> <a name="l02780"></a>02780 <span class="comment"> * Read a message from the server</span> <a name="l02781"></a>02781 <span class="comment"> */</span> <a name="l02782"></a>02782 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&scTransmitStruct, <span class="keyword">sizeof</span>(scTransmitStruct), <a name="l02783"></a>02783 currentContextMap-><a class="code" href="struct__psContextMap.html#a4e3f21f7252b71833af88ba9f2864577" title="Client Connection ID.">dwClientID</a>); <a name="l02784"></a>02784 <a name="l02785"></a>02785 <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="l02786"></a>02786 <span class="keywordflow">goto</span> end; <a name="l02787"></a>02787 <a name="l02788"></a>02788 <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> == scTransmitStruct.rv) <a name="l02789"></a>02789 { <a name="l02790"></a>02790 <span class="comment">/* read the received buffer */</span> <a name="l02791"></a>02791 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(pbRecvBuffer, scTransmitStruct.pcbRecvLength, <a name="l02792"></a>02792 currentContextMap-><a class="code" href="struct__psContextMap.html#a4e3f21f7252b71833af88ba9f2864577" title="Client Connection ID.">dwClientID</a>); <a name="l02793"></a>02793 <a name="l02794"></a>02794 <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="l02795"></a>02795 <span class="keywordflow">goto</span> end; <a name="l02796"></a>02796 <a name="l02797"></a>02797 <span class="keywordflow">if</span> (pioRecvPci) <a name="l02798"></a>02798 { <a name="l02799"></a>02799 pioRecvPci-><a class="code" href="structSCARD__IO__REQUEST.html#a06a981d5eb5bae479767d77641d50002" title="Protocol identifier.">dwProtocol</a> = scTransmitStruct.ioRecvPciProtocol; <a name="l02800"></a>02800 pioRecvPci-><a class="code" href="structSCARD__IO__REQUEST.html#a90993ce745546245b494a35a9559a083" title="Protocol Control Inf Length.">cbPciLength</a> = scTransmitStruct.ioRecvPciLength; <a name="l02801"></a>02801 } <a name="l02802"></a>02802 } <a name="l02803"></a>02803 <a name="l02804"></a>02804 rv = scTransmitStruct.rv; <a name="l02805"></a>02805 <a name="l02806"></a>02806 <span class="keywordflow">if</span> (sharing_shall_block && (<a class="code" href="pcsclite_8h.html#a81cd3fc9e7e84c66be2471294152235f" title="The smart card cannot be accessed because of other connections outstanding.">SCARD_E_SHARING_VIOLATION</a> == rv)) <a name="l02807"></a>02807 { <a name="l02808"></a>02808 (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#ac9c6d92f56e8c08459b19c0d4d85639f" title="Lock polling rate.">PCSCLITE_LOCK_POLL_RATE</a>); <a name="l02809"></a>02809 <span class="keywordflow">goto</span> retry; <a name="l02810"></a>02810 } <a name="l02811"></a>02811 <a name="l02812"></a>02812 *pcbRecvLength = scTransmitStruct.pcbRecvLength; <a name="l02813"></a>02813 <a name="l02814"></a>02814 end: <a name="l02815"></a>02815 (void)pthread_mutex_unlock(currentContextMap-><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a>); <a name="l02816"></a>02816 <a name="l02817"></a>02817 PROFILE_END(rv) <a name="l02818"></a>02818 <a name="l02819"></a>02819 <span class="keywordflow">return</span> rv; <a name="l02820"></a>02820 } <a name="l02821"></a>02821 <a name="l02872"></a><a class="code" href="group__API.html#ga93b07815789b3cf2629d439ecf20f0d9">02872</a> LONG <a class="code" href="group__API.html#ga93b07815789b3cf2629d439ecf20f0d9" title="This function returns a list of currently available readers on the system.">SCardListReaders</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> hContext, <span class="comment">/*@unused@*/</span> LPCSTR mszGroups, <a name="l02873"></a>02873 LPSTR mszReaders, LPDWORD pcchReaders) <a name="l02874"></a>02874 { <a name="l02875"></a>02875 DWORD dwReadersLen = 0; <a name="l02876"></a>02876 <span class="keywordtype">int</span> i; <a name="l02877"></a>02877 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l02878"></a>02878 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="l02879"></a>02879 <span class="keywordtype">char</span> *buf = NULL; <a name="l02880"></a>02880 <a name="l02881"></a>02881 (void)mszGroups; <a name="l02882"></a>02882 PROFILE_START <a name="l02883"></a>02883 API_TRACE_IN(<span class="stringliteral">"%ld"</span>, hContext) <a name="l02884"></a>02884 <a name="l02885"></a>02885 <span class="comment">/*</span> <a name="l02886"></a>02886 <span class="comment"> * Check for NULL parameters</span> <a name="l02887"></a>02887 <span class="comment"> */</span> <a name="l02888"></a>02888 <span class="keywordflow">if</span> (pcchReaders == NULL) <a name="l02889"></a>02889 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l02890"></a>02890 <a name="l02891"></a>02891 CHECK_SAME_PROCESS <a name="l02892"></a>02892 <a name="l02893"></a>02893 <span class="comment">/*</span> <a name="l02894"></a>02894 <span class="comment"> * Make sure this context has been opened</span> <a name="l02895"></a>02895 <span class="comment"> */</span> <a name="l02896"></a>02896 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l02897"></a>02897 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l02898"></a>02898 { <a name="l02899"></a>02899 PROFILE_END(<a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>) <a name="l02900"></a>02900 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l02901"></a>02901 } <a name="l02902"></a>02902 <a name="l02903"></a>02903 (void)pthread_mutex_lock(currentContextMap->mMutex); <a name="l02904"></a>02904 <a name="l02905"></a>02905 <span class="comment">/* check the context is still opened */</span> <a name="l02906"></a>02906 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l02907"></a>02907 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l02908"></a>02908 <span class="comment">/* the context is now invalid</span> <a name="l02909"></a>02909 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l02910"></a>02910 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l02911"></a>02911 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l02912"></a>02912 <a name="l02913"></a>02913 <span class="comment">/* synchronize reader states with daemon */</span> <a name="l02914"></a>02914 rv = getReaderStates(currentContextMap); <a name="l02915"></a>02915 <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="l02916"></a>02916 <span class="keywordflow">goto</span> end; <a name="l02917"></a>02917 <a name="l02918"></a>02918 dwReadersLen = 0; <a name="l02919"></a>02919 <span class="keywordflow">for</span> (i = 0; i < <a class="code" href="pcsclite_8h.html#af750e6e22c809de2b523c17e4a092036" title="Maximum readers context (a slot is count as a reader)">PCSCLITE_MAX_READERS_CONTEXTS</a>; i++) <a name="l02920"></a>02920 <span class="keywordflow">if</span> (readerStates[i].readerName[0] != <span class="charliteral">'\0'</span>) <a name="l02921"></a>02921 dwReadersLen += strlen(readerStates[i].readerName) + 1; <a name="l02922"></a>02922 <a name="l02923"></a>02923 <span class="comment">/* for the last NULL byte */</span> <a name="l02924"></a>02924 dwReadersLen += 1; <a name="l02925"></a>02925 <a name="l02926"></a>02926 <span class="keywordflow">if</span> (1 == dwReadersLen) <a name="l02927"></a>02927 { <a name="l02928"></a>02928 rv = <a class="code" href="pcsclite_8h.html#a81b59e9319d3fcd0d957d98781b3ebd2" title="Cannot find a smart card reader.">SCARD_E_NO_READERS_AVAILABLE</a>; <a name="l02929"></a>02929 <span class="keywordflow">goto</span> end; <a name="l02930"></a>02930 } <a name="l02931"></a>02931 <a name="l02932"></a>02932 <span class="keywordflow">if</span> (<a class="code" href="pcsclite_8h.html#a67db0943aeb13f9f468eaae39b5ff1b0" title="see SCardFreeMemory()">SCARD_AUTOALLOCATE</a> == *pcchReaders) <a name="l02933"></a>02933 { <a name="l02934"></a>02934 buf = malloc(dwReadersLen); <a name="l02935"></a>02935 <span class="keywordflow">if</span> (NULL == buf) <a name="l02936"></a>02936 { <a name="l02937"></a>02937 rv = <a class="code" href="pcsclite_8h.html#a81694393a1494d5091145734a2f0b36c" title="Not enough memory available to complete this command.">SCARD_E_NO_MEMORY</a>; <a name="l02938"></a>02938 <span class="keywordflow">goto</span> end; <a name="l02939"></a>02939 } <a name="l02940"></a>02940 <span class="keywordflow">if</span> (NULL == mszReaders) <a name="l02941"></a>02941 { <a name="l02942"></a>02942 rv = <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l02943"></a>02943 <span class="keywordflow">goto</span> end; <a name="l02944"></a>02944 } <a name="l02945"></a>02945 *(<span class="keywordtype">char</span> **)mszReaders = buf; <a name="l02946"></a>02946 } <a name="l02947"></a>02947 <span class="keywordflow">else</span> <a name="l02948"></a>02948 { <a name="l02949"></a>02949 buf = mszReaders; <a name="l02950"></a>02950 <a name="l02951"></a>02951 <span class="comment">/* not enough place to store the reader names */</span> <a name="l02952"></a>02952 <span class="keywordflow">if</span> ((NULL != mszReaders) && (*pcchReaders < dwReadersLen)) <a name="l02953"></a>02953 { <a name="l02954"></a>02954 rv = <a class="code" href="pcsclite_8h.html#aac3f8d945f8a0e667e0926daaa18dc57" title="The data buffer to receive returned data is too small for the returned data.">SCARD_E_INSUFFICIENT_BUFFER</a>; <a name="l02955"></a>02955 <span class="keywordflow">goto</span> end; <a name="l02956"></a>02956 } <a name="l02957"></a>02957 } <a name="l02958"></a>02958 <a name="l02959"></a>02959 <span class="keywordflow">if</span> (mszReaders == NULL) <span class="comment">/* text array not allocated */</span> <a name="l02960"></a>02960 <span class="keywordflow">goto</span> end; <a name="l02961"></a>02961 <a name="l02962"></a>02962 <span class="keywordflow">for</span> (i = 0; i < <a class="code" href="pcsclite_8h.html#af750e6e22c809de2b523c17e4a092036" title="Maximum readers context (a slot is count as a reader)">PCSCLITE_MAX_READERS_CONTEXTS</a>; i++) <a name="l02963"></a>02963 { <a name="l02964"></a>02964 <span class="keywordflow">if</span> (readerStates[i].readerName[0] != <span class="charliteral">'\0'</span>) <a name="l02965"></a>02965 { <a name="l02966"></a>02966 <span class="comment">/*</span> <a name="l02967"></a>02967 <span class="comment"> * Build the multi-string</span> <a name="l02968"></a>02968 <span class="comment"> */</span> <a name="l02969"></a>02969 strcpy(buf, readerStates[i].readerName); <a name="l02970"></a>02970 buf += strlen(readerStates[i].readerName)+1; <a name="l02971"></a>02971 } <a name="l02972"></a>02972 } <a name="l02973"></a>02973 *buf = <span class="charliteral">'\0'</span>; <span class="comment">/* Add the last null */</span> <a name="l02974"></a>02974 <a name="l02975"></a>02975 end: <a name="l02976"></a>02976 <span class="comment">/* set the reader names length */</span> <a name="l02977"></a>02977 *pcchReaders = dwReadersLen; <a name="l02978"></a>02978 <a name="l02979"></a>02979 (void)pthread_mutex_unlock(currentContextMap->mMutex); <a name="l02980"></a>02980 <a name="l02981"></a>02981 PROFILE_END(rv) <a name="l02982"></a>02982 API_TRACE_OUT(<span class="stringliteral">"%d"</span>, *pcchReaders) <a name="l02983"></a>02983 <a name="l02984"></a>02984 <span class="keywordflow">return</span> rv; <a name="l02985"></a>02985 } <a name="l02986"></a>02986 <a name="l03000"></a><a class="code" href="group__API.html#ga0522241e3180cb05dfd166e28930e961">03000</a> LONG <a class="code" href="group__API.html#ga0522241e3180cb05dfd166e28930e961" title="Releases memory that has been returned from the resource manager using the SCARD_AUTOALLOCATE length ...">SCardFreeMemory</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> hContext, LPCVOID pvMem) <a name="l03001"></a>03001 { <a name="l03002"></a>03002 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="l03003"></a>03003 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l03004"></a>03004 <a name="l03005"></a>03005 PROFILE_START <a name="l03006"></a>03006 <a name="l03007"></a>03007 CHECK_SAME_PROCESS <a name="l03008"></a>03008 <a name="l03009"></a>03009 <span class="comment">/*</span> <a name="l03010"></a>03010 <span class="comment"> * Make sure this context has been opened</span> <a name="l03011"></a>03011 <span class="comment"> */</span> <a name="l03012"></a>03012 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l03013"></a>03013 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l03014"></a>03014 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l03015"></a>03015 <a name="l03016"></a>03016 free((<span class="keywordtype">void</span> *)pvMem); <a name="l03017"></a>03017 <a name="l03018"></a>03018 PROFILE_END(rv) <a name="l03019"></a>03019 <a name="l03020"></a>03020 <span class="keywordflow">return</span> rv; <a name="l03021"></a>03021 } <a name="l03022"></a>03022 <a name="l03074"></a><a class="code" href="group__API.html#ga9d970d086d5218e080d0079d63f9d496">03074</a> LONG <a class="code" href="group__API.html#ga9d970d086d5218e080d0079d63f9d496" title="Returns a list of currently available reader groups on the system.">SCardListReaderGroups</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> hContext, LPSTR mszGroups, <a name="l03075"></a>03075 LPDWORD pcchGroups) <a name="l03076"></a>03076 { <a name="l03077"></a>03077 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="l03078"></a>03078 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l03079"></a>03079 <span class="keywordtype">char</span> *buf = NULL; <a name="l03080"></a>03080 <a name="l03081"></a>03081 PROFILE_START <a name="l03082"></a>03082 <a name="l03083"></a>03083 <span class="comment">/* Multi-string with two trailing \0 */</span> <a name="l03084"></a>03084 <span class="keyword">const</span> <span class="keywordtype">char</span> ReaderGroup[] = <span class="stringliteral">"SCard$DefaultReaders\0"</span>; <a name="l03085"></a>03085 <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> dwGroups = <span class="keyword">sizeof</span>(ReaderGroup); <a name="l03086"></a>03086 <a name="l03087"></a>03087 CHECK_SAME_PROCESS <a name="l03088"></a>03088 <a name="l03089"></a>03089 <span class="comment">/*</span> <a name="l03090"></a>03090 <span class="comment"> * Make sure this context has been opened</span> <a name="l03091"></a>03091 <span class="comment"> */</span> <a name="l03092"></a>03092 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l03093"></a>03093 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l03094"></a>03094 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l03095"></a>03095 <a name="l03096"></a>03096 (void)pthread_mutex_lock(currentContextMap->mMutex); <a name="l03097"></a>03097 <a name="l03098"></a>03098 <span class="comment">/* check the context is still opened */</span> <a name="l03099"></a>03099 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l03100"></a>03100 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l03101"></a>03101 <span class="comment">/* the context is now invalid</span> <a name="l03102"></a>03102 <span class="comment"> * -> another thread may have called SCardReleaseContext</span> <a name="l03103"></a>03103 <span class="comment"> * -> so the mMutex has been unlocked */</span> <a name="l03104"></a>03104 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l03105"></a>03105 <a name="l03106"></a>03106 <span class="keywordflow">if</span> (<a class="code" href="pcsclite_8h.html#a67db0943aeb13f9f468eaae39b5ff1b0" title="see SCardFreeMemory()">SCARD_AUTOALLOCATE</a> == *pcchGroups) <a name="l03107"></a>03107 { <a name="l03108"></a>03108 buf = malloc(dwGroups); <a name="l03109"></a>03109 <span class="keywordflow">if</span> (NULL == buf) <a name="l03110"></a>03110 { <a name="l03111"></a>03111 rv = <a class="code" href="pcsclite_8h.html#a81694393a1494d5091145734a2f0b36c" title="Not enough memory available to complete this command.">SCARD_E_NO_MEMORY</a>; <a name="l03112"></a>03112 <span class="keywordflow">goto</span> end; <a name="l03113"></a>03113 } <a name="l03114"></a>03114 <span class="keywordflow">if</span> (NULL == mszGroups) <a name="l03115"></a>03115 { <a name="l03116"></a>03116 rv = <a class="code" href="pcsclite_8h.html#a47a6da10ec4ea2cbebfcc4b2a93d786f" title="One or more of the supplied parameters could not be properly interpreted.">SCARD_E_INVALID_PARAMETER</a>; <a name="l03117"></a>03117 <span class="keywordflow">goto</span> end; <a name="l03118"></a>03118 } <a name="l03119"></a>03119 *(<span class="keywordtype">char</span> **)mszGroups = buf; <a name="l03120"></a>03120 } <a name="l03121"></a>03121 <span class="keywordflow">else</span> <a name="l03122"></a>03122 { <a name="l03123"></a>03123 buf = mszGroups; <a name="l03124"></a>03124 <a name="l03125"></a>03125 <span class="keywordflow">if</span> ((NULL != mszGroups) && (*pcchGroups < dwGroups)) <a name="l03126"></a>03126 { <a name="l03127"></a>03127 rv = <a class="code" href="pcsclite_8h.html#aac3f8d945f8a0e667e0926daaa18dc57" title="The data buffer to receive returned data is too small for the returned data.">SCARD_E_INSUFFICIENT_BUFFER</a>; <a name="l03128"></a>03128 <span class="keywordflow">goto</span> end; <a name="l03129"></a>03129 } <a name="l03130"></a>03130 } <a name="l03131"></a>03131 <a name="l03132"></a>03132 <span class="keywordflow">if</span> (buf) <a name="l03133"></a>03133 memcpy(buf, ReaderGroup, dwGroups); <a name="l03134"></a>03134 <a name="l03135"></a>03135 end: <a name="l03136"></a>03136 *pcchGroups = dwGroups; <a name="l03137"></a>03137 <a name="l03138"></a>03138 (void)pthread_mutex_unlock(currentContextMap->mMutex); <a name="l03139"></a>03139 <a name="l03140"></a>03140 PROFILE_END(rv) <a name="l03141"></a>03141 <a name="l03142"></a>03142 <span class="keywordflow">return</span> rv; <a name="l03143"></a>03143 } <a name="l03144"></a>03144 <a name="l03174"></a><a class="code" href="group__API.html#gaacbbc0c6d6c0cbbeb4f4debf6fbeeee6">03174</a> LONG <a class="code" href="group__API.html#gaacbbc0c6d6c0cbbeb4f4debf6fbeeee6" title="This function cancels all pending blocking requests on the SCardGetStatusChange() function...">SCardCancel</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> hContext) <a name="l03175"></a>03175 { <a name="l03176"></a>03176 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l03177"></a>03177 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="l03178"></a>03178 uint32_t dwClientID = 0; <a name="l03179"></a>03179 <span class="keyword">struct </span><a class="code" href="structcancel__struct.html" title="contained in SCARD_CANCEL Messages.">cancel_struct</a> scCancelStruct; <a name="l03180"></a>03180 <a name="l03181"></a>03181 PROFILE_START <a name="l03182"></a>03182 API_TRACE_IN(<span class="stringliteral">"%d"</span>, hContext) <a name="l03183"></a>03183 <a name="l03184"></a>03184 <span class="comment">/*</span> <a name="l03185"></a>03185 <span class="comment"> * Make sure this context has been opened</span> <a name="l03186"></a>03186 <span class="comment"> */</span> <a name="l03187"></a>03187 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l03188"></a>03188 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l03189"></a>03189 { <a name="l03190"></a>03190 rv = <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l03191"></a>03191 <span class="keywordflow">goto</span> error; <a name="l03192"></a>03192 } <a name="l03193"></a>03193 <a name="l03194"></a>03194 <span class="keywordflow">if</span> (! currentContextMap->cancellable) <a name="l03195"></a>03195 { <a name="l03196"></a>03196 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="l03197"></a>03197 <span class="keywordflow">goto</span> error; <a name="l03198"></a>03198 } <a name="l03199"></a>03199 <a name="l03200"></a>03200 <span class="comment">/* create a new connection to the server */</span> <a name="l03201"></a>03201 <span class="keywordflow">if</span> (<a class="code" href="winscard__msg_8c.html#a616a7d779449d1dc6a8a5d925d8212b1" title="Prepares a communication channel for the client to talk to the server.">ClientSetupSession</a>(&dwClientID) != 0) <a name="l03202"></a>03202 { <a name="l03203"></a>03203 rv = <a class="code" href="pcsclite_8h.html#ad4729ab109ff490285d2ad881c04bee8" title="The Smart card resource manager is not running.">SCARD_E_NO_SERVICE</a>; <a name="l03204"></a>03204 <span class="keywordflow">goto</span> error; <a name="l03205"></a>03205 } <a name="l03206"></a>03206 <a name="l03207"></a>03207 scCancelStruct.hContext = hContext; <a name="l03208"></a>03208 scCancelStruct.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="l03209"></a>03209 <a name="l03210"></a>03210 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dcac0d6cd84c358c5f349cf40f4235791d0" title="used by SCardCancel()">SCARD_CANCEL</a>, dwClientID, <a name="l03211"></a>03211 <span class="keyword">sizeof</span>(scCancelStruct), (<span class="keywordtype">void</span> *) &scCancelStruct); <a name="l03212"></a>03212 <a name="l03213"></a>03213 <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="l03214"></a>03214 <span class="keywordflow">goto</span> end; <a name="l03215"></a>03215 <a name="l03216"></a>03216 <span class="comment">/*</span> <a name="l03217"></a>03217 <span class="comment"> * Read a message from the server</span> <a name="l03218"></a>03218 <span class="comment"> */</span> <a name="l03219"></a>03219 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&scCancelStruct, <span class="keyword">sizeof</span>(scCancelStruct), dwClientID); <a name="l03220"></a>03220 <a name="l03221"></a>03221 <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="l03222"></a>03222 <span class="keywordflow">goto</span> end; <a name="l03223"></a>03223 <a name="l03224"></a>03224 rv = scCancelStruct.rv; <a name="l03225"></a>03225 end: <a name="l03226"></a>03226 <a class="code" href="winscard__msg_8c.html#a43b3c0b77aeac885864df13a39b5d44f" title="Closes the socket used by the client to communicate with the server.">ClientCloseSession</a>(dwClientID); <a name="l03227"></a>03227 <a name="l03228"></a>03228 error: <a name="l03229"></a>03229 PROFILE_END(rv) <a name="l03230"></a>03230 API_TRACE_OUT(<span class="stringliteral">""</span>) <a name="l03231"></a>03231 <a name="l03232"></a>03232 <span class="keywordflow">return</span> rv; <a name="l03233"></a>03233 } <a name="l03234"></a>03234 <a name="l03258"></a><a class="code" href="group__API.html#ga722eb66bcc44d391f700ff9065cc080b">03258</a> LONG <a class="code" href="group__API.html#ga722eb66bcc44d391f700ff9065cc080b" title="Check if a SCARDCONTEXT is valid.">SCardIsValidContext</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> hContext) <a name="l03259"></a>03259 { <a name="l03260"></a>03260 LONG rv; <a name="l03261"></a>03261 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l03262"></a>03262 <a name="l03263"></a>03263 PROFILE_START <a name="l03264"></a>03264 API_TRACE_IN(<span class="stringliteral">"%ld"</span>, hContext) <a name="l03265"></a>03265 <a name="l03266"></a>03266 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="l03267"></a>03267 <a name="l03268"></a>03268 <span class="comment">/* Check if the _same_ server is running */</span> <a name="l03269"></a>03269 CHECK_SAME_PROCESS <a name="l03270"></a>03270 <a name="l03271"></a>03271 <span class="comment">/*</span> <a name="l03272"></a>03272 <span class="comment"> * Make sure this context has been opened</span> <a name="l03273"></a>03273 <span class="comment"> */</span> <a name="l03274"></a>03274 currentContextMap = <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(hContext); <a name="l03275"></a>03275 <span class="keywordflow">if</span> (currentContextMap == NULL) <a name="l03276"></a>03276 rv = <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l03277"></a>03277 <a name="l03278"></a>03278 PROFILE_END(rv) <a name="l03279"></a>03279 API_TRACE_OUT(<span class="stringliteral">""</span>) <a name="l03280"></a>03280 <a name="l03281"></a>03281 <span class="keywordflow">return</span> rv; <a name="l03282"></a>03282 } <a name="l03283"></a>03283 <a name="l03300"></a><a class="code" href="winscard__clnt_8c.html#ac75190d70d571bca9915bef60491534a">03300</a> <span class="keyword">static</span> LONG <a class="code" href="winscard__clnt_8c.html#ac75190d70d571bca9915bef60491534a" title="Functions for managing instances of SCardEstablishContext() These functions keep track of Context han...">SCardAddContext</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> hContext, DWORD dwClientID) <a name="l03301"></a>03301 { <a name="l03302"></a>03302 <span class="keywordtype">int</span> lrv; <a name="l03303"></a>03303 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * newContextMap; <a name="l03304"></a>03304 <a name="l03305"></a>03305 newContextMap = malloc(<span class="keyword">sizeof</span>(<a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a>)); <a name="l03306"></a>03306 <span class="keywordflow">if</span> (NULL == newContextMap) <a name="l03307"></a>03307 <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="l03308"></a>03308 <a name="l03309"></a>03309 Log2(PCSC_LOG_DEBUG, <span class="stringliteral">"Allocating new SCONTEXTMAP @%X"</span>, newContextMap); <a name="l03310"></a>03310 newContextMap-><a class="code" href="struct__psContextMap.html#a4171c839145c22f7594818304bf4e7a9" title="Application Context ID.">hContext</a> = hContext; <a name="l03311"></a>03311 newContextMap-><a class="code" href="struct__psContextMap.html#a4e3f21f7252b71833af88ba9f2864577" title="Client Connection ID.">dwClientID</a> = dwClientID; <a name="l03312"></a>03312 newContextMap-><a class="code" href="struct__psContextMap.html#ac75775235adbaffd6b1293b439d149de" title="We are in a cancellable call.">cancellable</a> = FALSE; <a name="l03313"></a>03313 <a name="l03314"></a>03314 newContextMap-><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a> = malloc(<span class="keyword">sizeof</span>(pthread_mutex_t)); <a name="l03315"></a>03315 <span class="keywordflow">if</span> (NULL == newContextMap-><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a>) <a name="l03316"></a>03316 { <a name="l03317"></a>03317 Log2(PCSC_LOG_DEBUG, <span class="stringliteral">"Freeing SCONTEXTMAP @%X"</span>, newContextMap); <a name="l03318"></a>03318 free(newContextMap); <a name="l03319"></a>03319 <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="l03320"></a>03320 } <a name="l03321"></a>03321 (void)pthread_mutex_init(newContextMap-><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a>, NULL); <a name="l03322"></a>03322 <a name="l03323"></a>03323 lrv = list_init(&(newContextMap->channelMapList)); <a name="l03324"></a>03324 <span class="keywordflow">if</span> (lrv < 0) <a name="l03325"></a>03325 { <a name="l03326"></a>03326 Log2(PCSC_LOG_CRITICAL, <span class="stringliteral">"list_init failed with return value: %d"</span>, lrv); <a name="l03327"></a>03327 <span class="keywordflow">goto</span> error; <a name="l03328"></a>03328 } <a name="l03329"></a>03329 <a name="l03330"></a>03330 lrv = list_attributes_seeker(&(newContextMap->channelMapList), <a name="l03331"></a>03331 CHANNEL_MAP_seeker); <a name="l03332"></a>03332 <span class="keywordflow">if</span> (lrv <0) <a name="l03333"></a>03333 { <a name="l03334"></a>03334 Log2(PCSC_LOG_CRITICAL, <a name="l03335"></a>03335 <span class="stringliteral">"list_attributes_seeker failed with return value: %d"</span>, lrv); <a name="l03336"></a>03336 list_destroy(&(newContextMap->channelMapList)); <a name="l03337"></a>03337 <span class="keywordflow">goto</span> error; <a name="l03338"></a>03338 } <a name="l03339"></a>03339 <a name="l03340"></a>03340 lrv = list_append(&contextMapList, newContextMap); <a name="l03341"></a>03341 <span class="keywordflow">if</span> (lrv < 0) <a name="l03342"></a>03342 { <a name="l03343"></a>03343 Log2(PCSC_LOG_CRITICAL, <span class="stringliteral">"list_append failed with return value: %d"</span>, <a name="l03344"></a>03344 lrv); <a name="l03345"></a>03345 list_destroy(&(newContextMap->channelMapList)); <a name="l03346"></a>03346 <span class="keywordflow">goto</span> error; <a name="l03347"></a>03347 } <a name="l03348"></a>03348 <a name="l03349"></a>03349 <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="l03350"></a>03350 <a name="l03351"></a>03351 error: <a name="l03352"></a>03352 <a name="l03353"></a>03353 (void)pthread_mutex_destroy(newContextMap-><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a>); <a name="l03354"></a>03354 free(newContextMap-><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a>); <a name="l03355"></a>03355 free(newContextMap); <a name="l03356"></a>03356 <a name="l03357"></a>03357 <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="l03358"></a>03358 } <a name="l03359"></a>03359 <a name="l03372"></a><a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699">03372</a> <span class="keyword">static</span> <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * <a class="code" href="winscard__clnt_8c.html#a8a3c749483d20ff887797b0879244699" title="Get the index from the Application Context vector _psContextMap for the passed context.">SCardGetContext</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> hContext) <a name="l03373"></a>03373 { <a name="l03374"></a>03374 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l03375"></a>03375 <a name="l03376"></a>03376 (void)<a class="code" href="winscard__clnt_8c.html#a1f7e1b05b30f1e5a5cd62b2f925222fe" title="Locks a mutex so another thread must wait to use this function.">SCardLockThread</a>(); <a name="l03377"></a>03377 currentContextMap = <a class="code" href="winscard__clnt_8c.html#aafd00ba256e4ba8a81491c970ee336ed" title="Get the address from the Application Context list _psContextMap for the passed context.">SCardGetContextTH</a>(hContext); <a name="l03378"></a>03378 (void)<a class="code" href="winscard__clnt_8c.html#a5dc8cab71db796c9e91329c22f4ebfa1" title="Unlocks a mutex so another thread may use the client.">SCardUnlockThread</a>(); <a name="l03379"></a>03379 <a name="l03380"></a>03380 <span class="keywordflow">return</span> currentContextMap; <a name="l03381"></a>03381 } <a name="l03382"></a>03382 <a name="l03395"></a><a class="code" href="winscard__clnt_8c.html#aafd00ba256e4ba8a81491c970ee336ed">03395</a> <span class="keyword">static</span> <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * <a class="code" href="winscard__clnt_8c.html#aafd00ba256e4ba8a81491c970ee336ed" title="Get the address from the Application Context list _psContextMap for the passed context.">SCardGetContextTH</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> hContext) <a name="l03396"></a>03396 { <a name="l03397"></a>03397 <span class="keywordflow">return</span> list_seek(&contextMapList, &hContext); <a name="l03398"></a>03398 } <a name="l03399"></a>03399 <a name="l03409"></a><a class="code" href="winscard__clnt_8c.html#a7e3dc878020f1e5ccdae1a6914abd3b0">03409</a> <span class="keyword">static</span> LONG <a class="code" href="winscard__clnt_8c.html#a7e3dc878020f1e5ccdae1a6914abd3b0" title="Removes an Application Context from a control vector.">SCardRemoveContext</a>(<a class="code" href="pcsclite_8h.html#a22530ffaff18b5d3e32260a5f1ce4abd" title="hContext returned by SCardEstablishContext()">SCARDCONTEXT</a> hContext) <a name="l03410"></a>03410 { <a name="l03411"></a>03411 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l03412"></a>03412 currentContextMap = <a class="code" href="winscard__clnt_8c.html#aafd00ba256e4ba8a81491c970ee336ed" title="Get the address from the Application Context list _psContextMap for the passed context.">SCardGetContextTH</a>(hContext); <a name="l03413"></a>03413 <a name="l03414"></a>03414 <span class="keywordflow">if</span> (NULL == currentContextMap) <a name="l03415"></a>03415 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l03416"></a>03416 <span class="keywordflow">else</span> <a name="l03417"></a>03417 <span class="keywordflow">return</span> SCardCleanContext(currentContextMap); <a name="l03418"></a>03418 } <a name="l03419"></a>03419 <a name="l03420"></a>03420 <span class="keyword">static</span> LONG SCardCleanContext(<a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * targetContextMap) <a name="l03421"></a>03421 { <a name="l03422"></a>03422 <span class="keywordtype">int</span> list_index, lrv; <a name="l03423"></a>03423 <span class="keywordtype">int</span> listSize; <a name="l03424"></a>03424 <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * currentChannelMap; <a name="l03425"></a>03425 <a name="l03426"></a>03426 targetContextMap-><a class="code" href="struct__psContextMap.html#a4171c839145c22f7594818304bf4e7a9" title="Application Context ID.">hContext</a> = 0; <a name="l03427"></a>03427 (void)<a class="code" href="winscard__msg_8c.html#a43b3c0b77aeac885864df13a39b5d44f" title="Closes the socket used by the client to communicate with the server.">ClientCloseSession</a>(targetContextMap-><a class="code" href="struct__psContextMap.html#a4e3f21f7252b71833af88ba9f2864577" title="Client Connection ID.">dwClientID</a>); <a name="l03428"></a>03428 targetContextMap-><a class="code" href="struct__psContextMap.html#a4e3f21f7252b71833af88ba9f2864577" title="Client Connection ID.">dwClientID</a> = 0; <a name="l03429"></a>03429 (void)pthread_mutex_destroy(targetContextMap-><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a>); <a name="l03430"></a>03430 free(targetContextMap-><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a>); <a name="l03431"></a>03431 targetContextMap-><a class="code" href="struct__psContextMap.html#a221ef594a986ccfa18dfafb5d7954bc1" title="Mutex for this context.">mMutex</a> = NULL; <a name="l03432"></a>03432 <a name="l03433"></a>03433 listSize = list_size(&(targetContextMap->channelMapList)); <a name="l03434"></a>03434 <span class="keywordflow">for</span> (list_index = 0; list_index < listSize; list_index++) <a name="l03435"></a>03435 { <a name="l03436"></a>03436 currentChannelMap = list_get_at(&(targetContextMap->channelMapList), <a name="l03437"></a>03437 list_index); <a name="l03438"></a>03438 <span class="keywordflow">if</span> (NULL == currentChannelMap) <a name="l03439"></a>03439 { <a name="l03440"></a>03440 Log2(PCSC_LOG_CRITICAL, <span class="stringliteral">"list_get_at failed for index %d"</span>, <a name="l03441"></a>03441 list_index); <a name="l03442"></a>03442 <span class="keywordflow">continue</span>; <a name="l03443"></a>03443 } <a name="l03444"></a>03444 <span class="keywordflow">else</span> <a name="l03445"></a>03445 { <a name="l03446"></a>03446 free(currentChannelMap->readerName); <a name="l03447"></a>03447 free(currentChannelMap); <a name="l03448"></a>03448 } <a name="l03449"></a>03449 <a name="l03450"></a>03450 } <a name="l03451"></a>03451 list_destroy(&(targetContextMap->channelMapList)); <a name="l03452"></a>03452 <a name="l03453"></a>03453 lrv = list_delete(&contextMapList, targetContextMap); <a name="l03454"></a>03454 <span class="keywordflow">if</span> (lrv < 0) <a name="l03455"></a>03455 { <a name="l03456"></a>03456 Log2(PCSC_LOG_CRITICAL, <a name="l03457"></a>03457 <span class="stringliteral">"list_delete failed with return value: %d"</span>, lrv); <a name="l03458"></a>03458 } <a name="l03459"></a>03459 <a name="l03460"></a>03460 free(targetContextMap); <a name="l03461"></a>03461 <a name="l03462"></a>03462 <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="l03463"></a>03463 } <a name="l03464"></a>03464 <a name="l03465"></a>03465 <span class="comment">/*</span> <a name="l03466"></a>03466 <span class="comment"> * Functions for managing hCard values returned from SCardConnect.</span> <a name="l03467"></a>03467 <span class="comment"> */</span> <a name="l03468"></a>03468 <a name="l03469"></a>03469 <span class="keyword">static</span> LONG SCardAddHandle(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap, <a name="l03470"></a>03470 LPCSTR readerName) <a name="l03471"></a>03471 { <a name="l03472"></a>03472 <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * newChannelMap; <a name="l03473"></a>03473 <span class="keywordtype">int</span> lrv = -1; <a name="l03474"></a>03474 <a name="l03475"></a>03475 newChannelMap = malloc(<span class="keyword">sizeof</span>(<a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a>)); <a name="l03476"></a>03476 <span class="keywordflow">if</span> (NULL == newChannelMap) <a name="l03477"></a>03477 <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="l03478"></a>03478 <a name="l03479"></a>03479 newChannelMap->hCard = hCard; <a name="l03480"></a>03480 newChannelMap->readerName = strdup(readerName); <a name="l03481"></a>03481 <a name="l03482"></a>03482 lrv = list_append(&(currentContextMap->channelMapList), newChannelMap); <a name="l03483"></a>03483 <span class="keywordflow">if</span> (lrv < 0) <a name="l03484"></a>03484 { <a name="l03485"></a>03485 free(newChannelMap->readerName); <a name="l03486"></a>03486 free(newChannelMap); <a name="l03487"></a>03487 Log2(PCSC_LOG_CRITICAL, <span class="stringliteral">"list_append failed with return value: %d"</span>, <a name="l03488"></a>03488 lrv); <a name="l03489"></a>03489 <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="l03490"></a>03490 } <a name="l03491"></a>03491 <a name="l03492"></a>03492 <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="l03493"></a>03493 } <a name="l03494"></a>03494 <a name="l03495"></a>03495 <span class="keyword">static</span> LONG SCardRemoveHandle(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard) <a name="l03496"></a>03496 { <a name="l03497"></a>03497 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l03498"></a>03498 <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * currentChannelMap; <a name="l03499"></a>03499 <span class="keywordtype">int</span> lrv; <a name="l03500"></a>03500 LONG rv; <a name="l03501"></a>03501 <a name="l03502"></a>03502 rv = SCardGetContextAndChannelFromHandle(hCard, &currentContextMap, <a name="l03503"></a>03503 &currentChannelMap); <a name="l03504"></a>03504 <span class="keywordflow">if</span> (rv == -1) <a name="l03505"></a>03505 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l03506"></a>03506 <a name="l03507"></a>03507 free(currentChannelMap->readerName); <a name="l03508"></a>03508 <a name="l03509"></a>03509 lrv = list_delete(&(currentContextMap->channelMapList), currentChannelMap); <a name="l03510"></a>03510 <span class="keywordflow">if</span> (lrv < 0) <a name="l03511"></a>03511 { <a name="l03512"></a>03512 Log2(PCSC_LOG_CRITICAL, <a name="l03513"></a>03513 <span class="stringliteral">"list_delete failed with return value: %d"</span>, lrv); <a name="l03514"></a>03514 } <a name="l03515"></a>03515 <a name="l03516"></a>03516 free(currentChannelMap); <a name="l03517"></a>03517 <a name="l03518"></a>03518 <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="l03519"></a>03519 } <a name="l03520"></a>03520 <a name="l03521"></a>03521 <span class="keyword">static</span> LONG SCardGetContextAndChannelFromHandle(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, <a name="l03522"></a>03522 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> **targetContextMap, <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> ** targetChannelMap) <a name="l03523"></a>03523 { <a name="l03524"></a>03524 LONG rv; <a name="l03525"></a>03525 <a name="l03526"></a>03526 <span class="keywordflow">if</span> (0 == hCard) <a name="l03527"></a>03527 <span class="keywordflow">return</span> -1; <a name="l03528"></a>03528 <a name="l03529"></a>03529 (void)<a class="code" href="winscard__clnt_8c.html#a1f7e1b05b30f1e5a5cd62b2f925222fe" title="Locks a mutex so another thread must wait to use this function.">SCardLockThread</a>(); <a name="l03530"></a>03530 rv = SCardGetContextAndChannelFromHandleTH(hCard, targetContextMap, <a name="l03531"></a>03531 targetChannelMap); <a name="l03532"></a>03532 (void)<a class="code" href="winscard__clnt_8c.html#a5dc8cab71db796c9e91329c22f4ebfa1" title="Unlocks a mutex so another thread may use the client.">SCardUnlockThread</a>(); <a name="l03533"></a>03533 <a name="l03534"></a>03534 <span class="keywordflow">return</span> rv; <a name="l03535"></a>03535 } <a name="l03536"></a>03536 <a name="l03537"></a>03537 <span class="keyword">static</span> LONG SCardGetContextAndChannelFromHandleTH(<a class="code" href="pcsclite_8h.html#af328aca3e11de737ecd771bcf1f75fb5" title="hCard returned by SCardConnect()">SCARDHANDLE</a> hCard, <a name="l03538"></a>03538 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> **targetContextMap, <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> ** targetChannelMap) <a name="l03539"></a>03539 { <a name="l03540"></a>03540 <span class="keywordtype">int</span> listSize; <a name="l03541"></a>03541 <span class="keywordtype">int</span> list_index; <a name="l03542"></a>03542 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l03543"></a>03543 <a class="code" href="struct__psChannelMap.html" title="Represents an Application Context Channel.">CHANNEL_MAP</a> * currentChannelMap; <a name="l03544"></a>03544 <a name="l03545"></a>03545 <span class="comment">/* Best to get the caller a crash early if we fail unsafely */</span> <a name="l03546"></a>03546 *targetContextMap = NULL; <a name="l03547"></a>03547 *targetChannelMap = NULL; <a name="l03548"></a>03548 <a name="l03549"></a>03549 listSize = list_size(&contextMapList); <a name="l03550"></a>03550 <a name="l03551"></a>03551 <span class="keywordflow">for</span> (list_index = 0; list_index < listSize; list_index++) <a name="l03552"></a>03552 { <a name="l03553"></a>03553 currentContextMap = list_get_at(&contextMapList, list_index); <a name="l03554"></a>03554 <span class="keywordflow">if</span> (currentContextMap == NULL) <a name="l03555"></a>03555 { <a name="l03556"></a>03556 Log2(PCSC_LOG_CRITICAL, <span class="stringliteral">"list_get_at failed for index %d"</span>, <a name="l03557"></a>03557 list_index); <a name="l03558"></a>03558 <span class="keywordflow">continue</span>; <a name="l03559"></a>03559 } <a name="l03560"></a>03560 currentChannelMap = list_seek(&(currentContextMap->channelMapList), <a name="l03561"></a>03561 &hCard); <a name="l03562"></a>03562 <span class="keywordflow">if</span> (currentChannelMap != NULL) <a name="l03563"></a>03563 { <a name="l03564"></a>03564 *targetContextMap = currentContextMap; <a name="l03565"></a>03565 *targetChannelMap = currentChannelMap; <a name="l03566"></a>03566 <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="l03567"></a>03567 } <a name="l03568"></a>03568 } <a name="l03569"></a>03569 <a name="l03570"></a>03570 <span class="keywordflow">return</span> -1; <a name="l03571"></a>03571 } <a name="l03572"></a>03572 <a name="l03584"></a><a class="code" href="winscard__clnt_8c.html#aaa02e1f70d68a502cd92ed066c44542f">03584</a> LONG <a class="code" href="winscard__clnt_8c.html#aaa02e1f70d68a502cd92ed066c44542f" title="Checks if the server is running.">SCardCheckDaemonAvailability</a>(<span class="keywordtype">void</span>) <a name="l03585"></a>03585 { <a name="l03586"></a>03586 LONG rv; <a name="l03587"></a>03587 <span class="keyword">struct </span>stat statBuffer; <a name="l03588"></a>03588 <span class="keywordtype">char</span> *socketName; <a name="l03589"></a>03589 <a name="l03590"></a>03590 socketName = getSocketName(); <a name="l03591"></a>03591 rv = stat(socketName, &statBuffer); <a name="l03592"></a>03592 <a name="l03593"></a>03593 <span class="keywordflow">if</span> (rv != 0) <a name="l03594"></a>03594 { <a name="l03595"></a>03595 Log3(PCSC_LOG_INFO, <span class="stringliteral">"PCSC Not Running: %s: %s"</span>, <a name="l03596"></a>03596 socketName, strerror(errno)); <a name="l03597"></a>03597 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#ad4729ab109ff490285d2ad881c04bee8" title="The Smart card resource manager is not running.">SCARD_E_NO_SERVICE</a>; <a name="l03598"></a>03598 } <a name="l03599"></a>03599 <a name="l03600"></a>03600 <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="l03601"></a>03601 } <a name="l03602"></a>03602 <a name="l03603"></a>03603 <span class="preprocessor">#ifdef DO_CHECK_SAME_PROCESS</span> <a name="l03604"></a>03604 <span class="preprocessor"></span><span class="keyword">static</span> LONG SCardInvalidateHandles(<span class="keywordtype">void</span>) <a name="l03605"></a>03605 { <a name="l03606"></a>03606 <span class="comment">/* invalid all handles */</span> <a name="l03607"></a>03607 (void)<a class="code" href="winscard__clnt_8c.html#a1f7e1b05b30f1e5a5cd62b2f925222fe" title="Locks a mutex so another thread must wait to use this function.">SCardLockThread</a>(); <a name="l03608"></a>03608 <a name="l03609"></a>03609 <span class="keywordflow">while</span> (list_size(&contextMapList) != 0) <a name="l03610"></a>03610 { <a name="l03611"></a>03611 <a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap; <a name="l03612"></a>03612 <a name="l03613"></a>03613 currentContextMap = list_get_at(&contextMapList, 0); <a name="l03614"></a>03614 <span class="keywordflow">if</span> (currentContextMap != NULL) <a name="l03615"></a>03615 (void)SCardCleanContext(currentContextMap); <a name="l03616"></a>03616 <span class="keywordflow">else</span> <a name="l03617"></a>03617 Log1(PCSC_LOG_CRITICAL, <span class="stringliteral">"list_get_at returned NULL"</span>); <a name="l03618"></a>03618 } <a name="l03619"></a>03619 <a name="l03620"></a>03620 (void)<a class="code" href="winscard__clnt_8c.html#a5dc8cab71db796c9e91329c22f4ebfa1" title="Unlocks a mutex so another thread may use the client.">SCardUnlockThread</a>(); <a name="l03621"></a>03621 <a name="l03622"></a>03622 <span class="keywordflow">return</span> <a class="code" href="pcsclite_8h.html#a05bdea2f2341e78d920885d1cc357676" title="The supplied handle was invalid.">SCARD_E_INVALID_HANDLE</a>; <a name="l03623"></a>03623 } <a name="l03624"></a>03624 <a name="l03625"></a>03625 <span class="keyword">static</span> LONG SCardCheckSameProcess(<span class="keywordtype">void</span>) <a name="l03626"></a>03626 { <a name="l03627"></a>03627 <span class="comment">/* after fork() need to restart */</span> <a name="l03628"></a>03628 <span class="keywordflow">if</span> ((client_pid && client_pid != getpid())) <a name="l03629"></a>03629 { <a name="l03630"></a>03630 Log1(PCSC_LOG_INFO, <span class="stringliteral">"Client forked"</span>); <a name="l03631"></a>03631 <span class="keywordflow">return</span> SCardInvalidateHandles(); <a name="l03632"></a>03632 } <a name="l03633"></a>03633 <a name="l03634"></a>03634 client_pid = getpid(); <a name="l03635"></a>03635 <a name="l03636"></a>03636 <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="l03637"></a>03637 } <a name="l03638"></a>03638 <span class="preprocessor">#endif</span> <a name="l03639"></a>03639 <span class="preprocessor"></span> <a name="l03640"></a>03640 <span class="keyword">static</span> LONG getReaderStates(<a class="code" href="struct__psContextMap.html" title="Represents the an Application Context on the Client side.">SCONTEXTMAP</a> * currentContextMap) <a name="l03641"></a>03641 { <a name="l03642"></a>03642 int32_t dwClientID = currentContextMap-><a class="code" href="struct__psContextMap.html#a4e3f21f7252b71833af88ba9f2864577" title="Client Connection ID.">dwClientID</a>; <a name="l03643"></a>03643 LONG rv; <a name="l03644"></a>03644 <a name="l03645"></a>03645 rv = <a class="code" href="winscard__msg_8c.html#a922f8b35802c2449f80a9bd02a25b05b" title="Wrapper for the MessageSend() function.">MessageSendWithHeader</a>(<a class="code" href="winscard__msg_8h.html#add241284f0460327d523aec6e1cd17dca192ae79f1c5d3d5f72f2b1c66d0b1cb8" title="get the readers state">CMD_GET_READERS_STATE</a>, dwClientID, 0, NULL); <a name="l03646"></a>03646 <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="l03647"></a>03647 <span class="keywordflow">return</span> rv; <a name="l03648"></a>03648 <a name="l03649"></a>03649 <span class="comment">/* Read a message from the server */</span> <a name="l03650"></a>03650 rv = <a class="code" href="winscard__msg_8c.html#adbb09969d5d410393ae441b9a18c7a0c" title="Called by the Client to get the reponse from the server or vice-versa.">MessageReceive</a>(&readerStates, <span class="keyword">sizeof</span>(readerStates), dwClientID); <a name="l03651"></a>03651 <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="l03652"></a>03652 <span class="keywordflow">return</span> rv; <a name="l03653"></a>03653 <a name="l03654"></a>03654 <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="l03655"></a>03655 } <a name="l03656"></a>03656 </pre></div></div> </div> <hr class="footer"/><address class="footer"><small>Generated on Thu Mar 31 2011 for pcsc-lite by  <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.3 </small></address> </body> </html>