Пример #1
0
        /*
         * SetNfcMode
         * ----------
         * Configure the reader's NFC interface for NFC Forum Tag emulation
         */
        void SetNfcMode(int type, bool silent)
        {
            string s;

            if ((type == 2) || (type == 4))
            {
                s = String.Format("Failed to activate NFC Forum type {0} Tag emulation", type);
            }
            else
            {
                s = "Failed to leave NFC Tag emulation mode";
            }

            /* 1st open a direct connection to the reader */
            /* ------------------------------------------ */

            SCardChannel channel = new SCardChannel(reader);

            channel.ShareMode = SCARD.SHARE_DIRECT;
            channel.Protocol  = 0;

            if (!channel.Connect())
            {
                Trace.WriteLine("Connect error " + channel.LastError + " (" + channel.LastErrorAsString + ")");

                if (!silent)
                {
                    MessageBox.Show("Direct connection to the reader has failed with error '" + channel.LastErrorAsString + "'.",
                                    s,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
                }
                return;
            }

            /* 2nd send a proprietary command within a SCardControl */
            /* ---------------------------------------------------- */

            byte[] command = null;

            switch (type)
            {
            /* Command to start the NFC card emulation */
            /* --------------------------------------- */

            case 2:
                command = new byte[] { 0x83,          /* SPROX_NFC_FUNC */
                                       0x10,          /* Control card emulation mode */
                                       0x02,          /* Select NFC Forum type 2 Tag emulation */
                                       0x11           /* First activation */
                };
                break;

            case 4:
                command = new byte[] { 0x83,          /* SPROX_NFC_FUNC */
                                       0x10,          /* Control card emulation mode */
                                       0x04,          /* Select NFC Forum type 4 Tag emulation */
                                       0x11           /* First activation */
                };
                break;

            /* Command to stop the NFC card emulation */
            /* -------------------------------------- */

            case 0:
                command = new byte[] { 0x83,          /* SPROX_NFC_FUNC */
                                       0x10,          /* Control card emulation mode */
                                       0x00,          /* Stop card emulation */
                                       0x00 };
                break;

            default:
                MessageBox.Show("Internal error.");
                return;
            }

            Trace.WriteLine("< " + new CardBuffer(command).AsString(" "));

            byte[] response = channel.Control(command);

            if ((response == null) || (response.Length == 0))
            {
                /* No response -> SCardControl error */
                /* --------------------------------- */

                Trace.WriteLine("Control error " + channel.LastError + " (" + channel.LastErrorAsString + ")");

                channel.Disconnect();
                if (!silent)
                {
                    MessageBox.Show("The command has not been delivered to the reader, error is '" + channel.LastErrorAsString + "'.",
                                    s,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
                }

                return;
            }

            Trace.WriteLine("> " + new CardBuffer(response).AsString(" "));

            if (response[0] != 0)
            {
                /* Error code returned by the reader */
                /* --------------------------------- */

                channel.Disconnect();
                if (!silent)
                {
                    MessageBox.Show("The command has not been executed by the reader, error code is '-" + response[0] + "'.",
                                    s,
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Exclamation);
                }

                emulation_running = false;
                return;
            }

            /* Drive the LEDs and buzzer */
            /* ------------------------- */

            if (type > 0)
            {
                emulation_running = true;
                channel.Leds(1, 1, 1);
                channel.Buzzer(100);
            }
            else
            {
                emulation_running = false;
                channel.LedsDefault();
                channel.BuzzerDefault();
            }

            /* Done */
            /* ---- */

            channel.Disconnect();
            ShowStatus();
        }