Example #1
0
        /**m* SCardChannel/Transmit
         *
         * NAME
         *   SCardChannel.Transmit()
         *
         * SYNOPSIS
         *   bool  Transmit()
         *   RAPDU Transmit(CAPDU capdu)
         *   bool  Transmit(CAPDU capdu, out RAPDU rapdu)
         *   void  Transmit(CAPDU capdu, TransmitDoneCallback callback)
         *
         * DESCRIPTION
         *   Sends a command APDU (CAPDU) to the connected card, and retrieves its response APDU (RAPDU)
         *
         * SOURCE
         *
         *   SCardChannel card = new SCardChannel( ... reader ... );
         *   if (!card.Connect( SCARD.PROTOCOL_T0|SCARD.PROTOCOL_T1 ))
         *   {
         *     // handle error
         *   }
         *
         *
         *   // Example 1
         *   // ---------
         *
         *   card.Command = new CAPDU("00 A4 00 00 02 3F 00");
         *   if (!card.Transmit())
         *   {
         *     // handle error
         *   }
         *   MessageBox.Show("Card answered: " + card.Response.AsString(" "));
         *
         *
         *   // Example 2
         *   // ---------
         *
         *   RAPDU response = card.Transmit(new CAPDU("00 A4 00 00 02 3F 00")))
         *   if (response == null)
         *   {
         *     // handle error
         *   }
         *   MessageBox.Show("Card answered: " + response.AsString(" "));
         *
         *
         *   // Example 3
         *   // ---------
         *
         *   CAPDU command  = new CAPDU("00 A4 00 00 02 3F 00");
         *   RAPDU response = new RAPDU();
         *   if (!card.Transmit(command, out response))
         *   {
         *     // handle error
         *   }
         *   MessageBox.Show("Card answered: " + response.AsString(" "));
         *
         *
         *   // Example 4
         *   // ---------
         *
         *   // In this example the Transmit is performed by a background thread
         *   // We supply a delegate so the main class (window/form) will be notified
         *   // when Transmit will return
         *
         *   delegate void OnTransmitDoneInvoker(RAPDU response);
         *
         *   void OnTransmitDone(RAPDU response)
         *   {
         *     // Ensure we're back in the context of the main thread (application's message pump)
         *     if (this.InvokeRequired)
         *     {
         *       this.BeginInvoke(new OnTransmitDoneInvoker(OnTransmitDone), response);
         *       return;
         *     }
         *
         *     if (response == null)
         *     {
         *       // handle error
         *     }
         *
         *     MessageBox.Show("Card answered: " + response.AsString(" "));
         *   }
         *
         *  card.Transmit(new CAPDU("00 A4 00 00 02 3F 00"), new SCardChannel.TransmitDoneCallback(OnTransmitDone));
         *
         *
         * SEE ALSO
         *   SCardChannel.Connect
         *   SCardChannel.Transmit
         *   SCardChannel.Command
         *   SCardChannel.Response
         *
         **/
        #region Transmit
        public virtual bool Transmit()
        {
            uint rsp_length = 32 * 1024;

            byte[] rsp_buffer = new byte[rsp_length];
            uint   rc;
            IntPtr SendPci = IntPtr.Zero;

            switch (_active_protocol)
            {
            case SCARD.PROTOCOL_T0:
                SendPci = SCARD.PCI_T0();
                break;

            case SCARD.PROTOCOL_T1:
                SendPci = SCARD.PCI_T1();
                break;

            case SCARD.PROTOCOL_RAW:
                SendPci = SCARD.PCI_RAW();
                break;

            default:
                break;
            }

            _rapdu = null;

            Trace.WriteLine("Transmit << " + _capdu.AsString());

            rc = SCARD.Transmit(_hCard,
                                SendPci,
                                _capdu.GetBytes(),
                                (uint)_capdu.Length,
                                IntPtr.Zero,             /* RecvPci is likely to remain NULL */
                                rsp_buffer,
                                ref rsp_length);

            if (rc != SCARD.S_SUCCESS)
            {
                Trace.WriteLine("Transmit : " + rc);
                _last_error = rc;
                return(false);
            }

            _rapdu = new RAPDU(rsp_buffer, (int)rsp_length);

            Trace.WriteLine("Transmit >> " + _rapdu.AsString());

            return(true);
        }
        public override bool Transmit()
        {
            byte[] buffer;

            _last_error = ReaderDevice.Transmit(Slot, _capdu.GetBytes(), out buffer);

            if (_last_error != SCARD.S_SUCCESS)
            {
                return(false);
            }

            Trace.WriteLine("Transmit>" + BinConvert.ToHex(buffer));

            _rapdu = new RAPDU(buffer);

            Trace.WriteLine("Transmit>" + _rapdu.AsString());

            return(true);
        }